v.util: add util.tabs() (#9211)

pull/9217/head
zakuro 2021-03-09 20:03:25 +09:00 committed by GitHub
parent c6ff3d13a7
commit 00399b49ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 32 additions and 39 deletions

View File

@ -11,9 +11,6 @@ import v.pref
const (
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
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() {
if f.indent < fmt.tabs.len {
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.out.write_string(util.tabs(f.indent))
f.line_len += f.indent * 4
}

View File

@ -24,9 +24,6 @@ const (
cmp_str = ['eq', 'ne', 'gt', 'lt', 'ge', 'le']
// when operands are switched
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 {
@ -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')
}
if g.indent > 0 && g.empty_line {
if g.indent < c.tabs.len {
g.out.write_string(c.tabs[g.indent])
} else {
for _ in 0 .. g.indent {
g.out.write_string('\t')
}
}
g.out.write_string(util.tabs(g.indent))
}
g.out.write_string(s)
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')
}
if g.indent > 0 && g.empty_line {
if g.indent < c.tabs.len {
g.out.write_string(c.tabs[g.indent])
} else {
for _ in 0 .. g.indent {
g.out.write_string('\t')
}
}
g.out.write_string(util.tabs(g.indent))
}
g.out.writeln(s)
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 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
ret_styp := g.typ(func.func.return_type)
@ -2100,7 +2085,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
} else {
if is_decl {
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 ')
}
@ -2116,7 +2101,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
}
if is_inside_ternary && is_decl {
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.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
cur_line := if is_gen_or_and_assign_rhs {
line := g.go_before_stmt(0)
g.out.write_string(c.tabs[g.indent])
g.out.write_string(util.tabs(g.indent))
line
} else {
''

View File

@ -154,7 +154,7 @@ fn (mut g Gen) comp_at(node ast.AtExpr) {
fn (mut g Gen) comp_if(node ast.IfExpr) {
line := if node.is_expr {
stmt_str := g.go_before_stmt(0)
g.write(tabs[g.indent])
g.write(util.tabs(g.indent))
stmt_str.trim_space()
} else {
''

View File

@ -415,7 +415,7 @@ fn (mut g Gen) call_expr(node ast.CallExpr) {
// `x := foo() or { ...}`
// cut everything that has been generated to prepend optional variable creation
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*/')
line
} else {

View File

@ -5,6 +5,7 @@ module c
import v.ast
import v.table
import v.util
fn (mut g Gen) index_expr(node ast.IndexExpr) {
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
cur_line := if is_gen_or_and_assign_rhs {
line := g.go_before_stmt(0)
g.out.write_string(tabs[g.indent])
g.out.write_string(util.tabs(g.indent))
line
} 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
cur_line := if is_gen_or_and_assign_rhs {
line := g.go_before_stmt(0)
g.out.write_string(tabs[g.indent])
g.out.write_string(util.tabs(g.indent))
line
} else {
''

View File

@ -21,8 +21,6 @@ const (
'int_literal', 'float_literal', 'size_t', 'bool', 'string', 'map', 'array']
shallow_equatables = [table.Kind.i8, .i16, .int, .i64, .byte, .u16, .u32, .u64, .f32, .f64,
.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 {
@ -261,7 +259,7 @@ fn verror(msg string) {
[inline]
pub fn (mut g JsGen) gen_indent() {
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
}

View File

@ -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 .
pub fn vhash() string {
mut buf := [50]byte{}