v.util: add util.tabs() (#9211)
parent
c6ff3d13a7
commit
00399b49ab
|
@ -11,9 +11,6 @@ import v.pref
|
||||||
|
|
||||||
const (
|
const (
|
||||||
bs = '\\'
|
bs = '\\'
|
||||||
tabs = ['', '\t', '\t\t', '\t\t\t', '\t\t\t\t', '\t\t\t\t\t', '\t\t\t\t\t\t', '\t\t\t\t\t\t\t',
|
|
||||||
'\t\t\t\t\t\t\t\t',
|
|
||||||
]
|
|
||||||
// when to break a line dependant on penalty
|
// when to break a line dependant on penalty
|
||||||
max_len = [0, 35, 60, 85, 93, 100]
|
max_len = [0, 35, 60, 85, 93, 100]
|
||||||
)
|
)
|
||||||
|
@ -110,14 +107,7 @@ pub fn (mut f Fmt) writeln(s string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut f Fmt) write_indent() {
|
fn (mut f Fmt) write_indent() {
|
||||||
if f.indent < fmt.tabs.len {
|
f.out.write_string(util.tabs(f.indent))
|
||||||
f.out.write_string(fmt.tabs[f.indent])
|
|
||||||
} else {
|
|
||||||
// too many indents, do it the slow way:
|
|
||||||
for _ in 0 .. f.indent {
|
|
||||||
f.out.write_string('\t')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
f.line_len += f.indent * 4
|
f.line_len += f.indent * 4
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,9 +24,6 @@ const (
|
||||||
cmp_str = ['eq', 'ne', 'gt', 'lt', 'ge', 'le']
|
cmp_str = ['eq', 'ne', 'gt', 'lt', 'ge', 'le']
|
||||||
// when operands are switched
|
// when operands are switched
|
||||||
cmp_rev = ['eq', 'ne', 'lt', 'gt', 'le', 'ge']
|
cmp_rev = ['eq', 'ne', 'lt', 'gt', 'le', 'ge']
|
||||||
tabs = ['', '\t', '\t\t', '\t\t\t', '\t\t\t\t', '\t\t\t\t\t', '\t\t\t\t\t\t', '\t\t\t\t\t\t\t',
|
|
||||||
'\t\t\t\t\t\t\t\t',
|
|
||||||
]
|
|
||||||
)
|
)
|
||||||
|
|
||||||
struct Gen {
|
struct Gen {
|
||||||
|
@ -844,13 +841,7 @@ pub fn (mut g Gen) write(s string) {
|
||||||
eprintln('gen file: ${g.file.path:-30} | last_fn_c_name: ${g.last_fn_c_name:-45} | write: $s')
|
eprintln('gen file: ${g.file.path:-30} | last_fn_c_name: ${g.last_fn_c_name:-45} | write: $s')
|
||||||
}
|
}
|
||||||
if g.indent > 0 && g.empty_line {
|
if g.indent > 0 && g.empty_line {
|
||||||
if g.indent < c.tabs.len {
|
g.out.write_string(util.tabs(g.indent))
|
||||||
g.out.write_string(c.tabs[g.indent])
|
|
||||||
} else {
|
|
||||||
for _ in 0 .. g.indent {
|
|
||||||
g.out.write_string('\t')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
g.out.write_string(s)
|
g.out.write_string(s)
|
||||||
g.empty_line = false
|
g.empty_line = false
|
||||||
|
@ -861,13 +852,7 @@ pub fn (mut g Gen) writeln(s string) {
|
||||||
eprintln('gen file: ${g.file.path:-30} | last_fn_c_name: ${g.last_fn_c_name:-45} | writeln: $s')
|
eprintln('gen file: ${g.file.path:-30} | last_fn_c_name: ${g.last_fn_c_name:-45} | writeln: $s')
|
||||||
}
|
}
|
||||||
if g.indent > 0 && g.empty_line {
|
if g.indent > 0 && g.empty_line {
|
||||||
if g.indent < c.tabs.len {
|
g.out.write_string(util.tabs(g.indent))
|
||||||
g.out.write_string(c.tabs[g.indent])
|
|
||||||
} else {
|
|
||||||
for _ in 0 .. g.indent {
|
|
||||||
g.out.write_string('\t')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
g.out.writeln(s)
|
g.out.writeln(s)
|
||||||
g.empty_line = true
|
g.empty_line = true
|
||||||
|
@ -2088,7 +2073,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
|
||||||
}
|
}
|
||||||
if right_sym.kind == .function && is_decl {
|
if right_sym.kind == .function && is_decl {
|
||||||
if is_inside_ternary && is_decl {
|
if is_inside_ternary && is_decl {
|
||||||
g.out.write_string(c.tabs[g.indent - g.inside_ternary])
|
g.out.write_string(util.tabs(g.indent - g.inside_ternary))
|
||||||
}
|
}
|
||||||
func := right_sym.info as table.FnType
|
func := right_sym.info as table.FnType
|
||||||
ret_styp := g.typ(func.func.return_type)
|
ret_styp := g.typ(func.func.return_type)
|
||||||
|
@ -2100,7 +2085,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
|
||||||
} else {
|
} else {
|
||||||
if is_decl {
|
if is_decl {
|
||||||
if is_inside_ternary {
|
if is_inside_ternary {
|
||||||
g.out.write_string(c.tabs[g.indent - g.inside_ternary])
|
g.out.write_string(util.tabs(g.indent - g.inside_ternary))
|
||||||
}
|
}
|
||||||
g.write('$styp ')
|
g.write('$styp ')
|
||||||
}
|
}
|
||||||
|
@ -2116,7 +2101,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
|
||||||
}
|
}
|
||||||
if is_inside_ternary && is_decl {
|
if is_inside_ternary && is_decl {
|
||||||
g.write(';\n$cur_line')
|
g.write(';\n$cur_line')
|
||||||
g.out.write_string(c.tabs[g.indent])
|
g.out.write_string(util.tabs(g.indent))
|
||||||
g.expr(left)
|
g.expr(left)
|
||||||
}
|
}
|
||||||
g.is_assign_lhs = false
|
g.is_assign_lhs = false
|
||||||
|
@ -2811,7 +2796,7 @@ fn (mut g Gen) expr(node ast.Expr) {
|
||||||
is_gen_or_and_assign_rhs := gen_or && !g.discard_or_result
|
is_gen_or_and_assign_rhs := gen_or && !g.discard_or_result
|
||||||
cur_line := if is_gen_or_and_assign_rhs {
|
cur_line := if is_gen_or_and_assign_rhs {
|
||||||
line := g.go_before_stmt(0)
|
line := g.go_before_stmt(0)
|
||||||
g.out.write_string(c.tabs[g.indent])
|
g.out.write_string(util.tabs(g.indent))
|
||||||
line
|
line
|
||||||
} else {
|
} else {
|
||||||
''
|
''
|
||||||
|
|
|
@ -154,7 +154,7 @@ fn (mut g Gen) comp_at(node ast.AtExpr) {
|
||||||
fn (mut g Gen) comp_if(node ast.IfExpr) {
|
fn (mut g Gen) comp_if(node ast.IfExpr) {
|
||||||
line := if node.is_expr {
|
line := if node.is_expr {
|
||||||
stmt_str := g.go_before_stmt(0)
|
stmt_str := g.go_before_stmt(0)
|
||||||
g.write(tabs[g.indent])
|
g.write(util.tabs(g.indent))
|
||||||
stmt_str.trim_space()
|
stmt_str.trim_space()
|
||||||
} else {
|
} else {
|
||||||
''
|
''
|
||||||
|
|
|
@ -415,7 +415,7 @@ fn (mut g Gen) call_expr(node ast.CallExpr) {
|
||||||
// `x := foo() or { ...}`
|
// `x := foo() or { ...}`
|
||||||
// cut everything that has been generated to prepend optional variable creation
|
// cut everything that has been generated to prepend optional variable creation
|
||||||
line := g.go_before_stmt(0)
|
line := g.go_before_stmt(0)
|
||||||
g.out.write_string(tabs[g.indent])
|
g.out.write_string(util.tabs(g.indent))
|
||||||
// g.write('/*is_gen_or_and_assign_rhs*/')
|
// g.write('/*is_gen_or_and_assign_rhs*/')
|
||||||
line
|
line
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -5,6 +5,7 @@ module c
|
||||||
|
|
||||||
import v.ast
|
import v.ast
|
||||||
import v.table
|
import v.table
|
||||||
|
import v.util
|
||||||
|
|
||||||
fn (mut g Gen) index_expr(node ast.IndexExpr) {
|
fn (mut g Gen) index_expr(node ast.IndexExpr) {
|
||||||
if node.index is ast.RangeExpr {
|
if node.index is ast.RangeExpr {
|
||||||
|
@ -178,7 +179,7 @@ fn (mut g Gen) index_of_array(node ast.IndexExpr, sym table.TypeSymbol) {
|
||||||
is_gen_or_and_assign_rhs := gen_or && !g.discard_or_result
|
is_gen_or_and_assign_rhs := gen_or && !g.discard_or_result
|
||||||
cur_line := if is_gen_or_and_assign_rhs {
|
cur_line := if is_gen_or_and_assign_rhs {
|
||||||
line := g.go_before_stmt(0)
|
line := g.go_before_stmt(0)
|
||||||
g.out.write_string(tabs[g.indent])
|
g.out.write_string(util.tabs(g.indent))
|
||||||
line
|
line
|
||||||
} else {
|
} else {
|
||||||
''
|
''
|
||||||
|
@ -358,7 +359,7 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym table.TypeSymbol) {
|
||||||
is_gen_or_and_assign_rhs := gen_or && !g.discard_or_result
|
is_gen_or_and_assign_rhs := gen_or && !g.discard_or_result
|
||||||
cur_line := if is_gen_or_and_assign_rhs {
|
cur_line := if is_gen_or_and_assign_rhs {
|
||||||
line := g.go_before_stmt(0)
|
line := g.go_before_stmt(0)
|
||||||
g.out.write_string(tabs[g.indent])
|
g.out.write_string(util.tabs(g.indent))
|
||||||
line
|
line
|
||||||
} else {
|
} else {
|
||||||
''
|
''
|
||||||
|
|
|
@ -21,8 +21,6 @@ const (
|
||||||
'int_literal', 'float_literal', 'size_t', 'bool', 'string', 'map', 'array']
|
'int_literal', 'float_literal', 'size_t', 'bool', 'string', 'map', 'array']
|
||||||
shallow_equatables = [table.Kind.i8, .i16, .int, .i64, .byte, .u16, .u32, .u64, .f32, .f64,
|
shallow_equatables = [table.Kind.i8, .i16, .int, .i64, .byte, .u16, .u32, .u64, .f32, .f64,
|
||||||
.int_literal, .float_literal, .size_t, .bool, .string]
|
.int_literal, .float_literal, .size_t, .bool, .string]
|
||||||
tabs = ['', '\t', '\t\t', '\t\t\t', '\t\t\t\t', '\t\t\t\t\t', '\t\t\t\t\t\t',
|
|
||||||
'\t\t\t\t\t\t\t', '\t\t\t\t\t\t\t\t', '\t\t\t\t\t\t\t\t\t', '\t\t\t\t\t\t\t\t\t', '\t\t\t\t\t\t\t\t\t']
|
|
||||||
)
|
)
|
||||||
|
|
||||||
struct Namespace {
|
struct Namespace {
|
||||||
|
@ -261,7 +259,7 @@ fn verror(msg string) {
|
||||||
[inline]
|
[inline]
|
||||||
pub fn (mut g JsGen) gen_indent() {
|
pub fn (mut g JsGen) gen_indent() {
|
||||||
if g.ns.indent > 0 && g.empty_line {
|
if g.ns.indent > 0 && g.empty_line {
|
||||||
g.ns.out.write_string(js.tabs[g.ns.indent])
|
g.ns.out.write_string(util.tabs(g.ns.indent))
|
||||||
}
|
}
|
||||||
g.empty_line = false
|
g.empty_line = false
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,25 @@ pub const (
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
const_tabs = [
|
||||||
|
'',
|
||||||
|
'\t',
|
||||||
|
'\t\t',
|
||||||
|
'\t\t\t',
|
||||||
|
'\t\t\t\t',
|
||||||
|
'\t\t\t\t\t',
|
||||||
|
'\t\t\t\t\t\t',
|
||||||
|
'\t\t\t\t\t\t\t',
|
||||||
|
'\t\t\t\t\t\t\t\t',
|
||||||
|
'\t\t\t\t\t\t\t\t\t',
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
pub fn tabs(n int) string {
|
||||||
|
return if n < util.const_tabs.len { util.const_tabs[n] } else { '\t'.repeat(n) }
|
||||||
|
}
|
||||||
|
|
||||||
// vhash() returns the build string C.V_COMMIT_HASH . See cmd/tools/gen_vc.v .
|
// vhash() returns the build string C.V_COMMIT_HASH . See cmd/tools/gen_vc.v .
|
||||||
pub fn vhash() string {
|
pub fn vhash() string {
|
||||||
mut buf := [50]byte{}
|
mut buf := [50]byte{}
|
||||||
|
|
Loading…
Reference in New Issue