mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: generate indents for more readable C code
This commit is contained in:
parent
53a9329ab6
commit
ab3f6d9202
@ -762,6 +762,9 @@ pub fn home_dir() string {
|
||||
$if windows {
|
||||
return os.getenv('USERPROFILE') + os.path_separator
|
||||
} $else {
|
||||
//println('home_dir() call')
|
||||
//res:= os.getenv('HOME') + os.path_separator
|
||||
//println('res="$res"')
|
||||
return os.getenv('HOME') + os.path_separator
|
||||
}
|
||||
}
|
||||
|
@ -388,6 +388,7 @@ pub struct CompIf {
|
||||
pub:
|
||||
cond Expr
|
||||
stmts []Stmt
|
||||
mut:
|
||||
else_stmts []Stmt
|
||||
}
|
||||
|
||||
|
@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
tabs = ['', '\t', '\t\t', '\t\t\t', '\t\t\t\t', '\t\t\t\t\t', '\t\t\t\t\t\t']
|
||||
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']
|
||||
// tabs = ['', ' ', ' ', ' ', ' ']
|
||||
max_len = 80
|
||||
)
|
||||
@ -241,9 +241,8 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
|
||||
f.stmts(it.stmts)
|
||||
f.writeln('}')
|
||||
}
|
||||
ast.Import {
|
||||
ast.Import {}
|
||||
// already handled in f.imports
|
||||
}
|
||||
ast.TypeDecl {
|
||||
f.type_decl(it)
|
||||
}
|
||||
@ -481,7 +480,8 @@ fn (f mut Fmt) expr(node ast.Expr) {
|
||||
}
|
||||
if (branch.stmts.len == 0) {
|
||||
f.writeln(' {}')
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
f.writeln(' {')
|
||||
f.stmts(branch.stmts)
|
||||
f.writeln('}')
|
||||
|
@ -28,8 +28,15 @@ mut:
|
||||
stmt_start_pos int
|
||||
right_is_opt bool
|
||||
autofree bool
|
||||
indent int
|
||||
empty_line bool
|
||||
}
|
||||
|
||||
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']
|
||||
)
|
||||
|
||||
pub fn cgen(files []ast.File, table &table.Table) string {
|
||||
println('start cgen2')
|
||||
mut g := Gen{
|
||||
@ -40,13 +47,14 @@ pub fn cgen(files []ast.File, table &table.Table) string {
|
||||
table: table
|
||||
fn_decl: 0
|
||||
autofree: true
|
||||
indent: -1
|
||||
}
|
||||
g.init()
|
||||
for file in files {
|
||||
// println('cgen "$g.file.path" $file.stmts.len')
|
||||
g.file = file
|
||||
if g.file.path == '' || g.file.path.ends_with('.vv') {
|
||||
// cgen test
|
||||
if g.file.path == '' || g.file.path.ends_with('.vv') || g.file.path.contains('/vlib/') {
|
||||
// cgen test or building V
|
||||
g.autofree = false
|
||||
}
|
||||
g.stmts(file.stmts)
|
||||
@ -191,11 +199,20 @@ pub fn (g mut Gen) write_variadic_types() {
|
||||
pub fn (g &Gen) save() {}
|
||||
|
||||
pub fn (g mut Gen) write(s string) {
|
||||
if g.indent > 0 && g.empty_line {
|
||||
g.out.write(tabs[g.indent])
|
||||
// g.line_len += g.indent * 4
|
||||
}
|
||||
g.out.write(s)
|
||||
g.empty_line = false
|
||||
}
|
||||
|
||||
pub fn (g mut Gen) writeln(s string) {
|
||||
if g.indent > 0 && g.empty_line {
|
||||
g.out.write(tabs[g.indent])
|
||||
}
|
||||
g.out.writeln(s)
|
||||
g.empty_line = true
|
||||
}
|
||||
|
||||
pub fn (g mut Gen) new_tmp_var() string {
|
||||
@ -208,12 +225,14 @@ pub fn (g mut Gen) reset_tmp_count() {
|
||||
}
|
||||
|
||||
fn (g mut Gen) stmts(stmts []ast.Stmt) {
|
||||
g.indent++
|
||||
for stmt in stmts {
|
||||
g.stmt(stmt)
|
||||
if !g.inside_ternary {
|
||||
g.writeln('')
|
||||
}
|
||||
// if !g.inside_ternary {
|
||||
// g.writeln('')
|
||||
// }
|
||||
}
|
||||
g.indent--
|
||||
}
|
||||
|
||||
fn (g mut Gen) stmt(node ast.Stmt) {
|
||||
@ -242,8 +261,8 @@ fn (g mut Gen) stmt(node ast.Stmt) {
|
||||
ast.CompIf {
|
||||
// TODO
|
||||
g.writeln('//#ifdef ')
|
||||
g.expr(it.cond)
|
||||
g.stmts(it.stmts)
|
||||
// g.expr(it.cond)
|
||||
// g.stmts(it.stmts)
|
||||
g.writeln('//#endif')
|
||||
}
|
||||
ast.DeferStmt {
|
||||
@ -277,6 +296,7 @@ fn (g mut Gen) stmt(node ast.Stmt) {
|
||||
ast.FnDecl {
|
||||
g.fn_decl = it // &it
|
||||
g.gen_fn_decl(it)
|
||||
g.writeln('')
|
||||
}
|
||||
ast.ForCStmt {
|
||||
g.write('for (')
|
||||
@ -545,10 +565,7 @@ fn (g mut Gen) gen_fn_decl(it ast.FnDecl) {
|
||||
}
|
||||
g.writeln('os__args = os__init_os_args(argc, (byteptr*)argv);')
|
||||
}
|
||||
for stmt in it.stmts {
|
||||
// g.write('\t')
|
||||
g.stmt(stmt)
|
||||
}
|
||||
g.stmts(it.stmts)
|
||||
// ////////////
|
||||
if g.autofree {
|
||||
scope := g.file.scope.innermost(it.pos.pos - 1)
|
||||
|
@ -14,11 +14,13 @@ pub fn (p mut Parser) comp_if() ast.CompIf {
|
||||
if p.tok.kind == .question {
|
||||
p.next()
|
||||
}
|
||||
p.parse_block()
|
||||
mut node := ast.CompIf{
|
||||
stmts: p.parse_block()
|
||||
}
|
||||
if p.tok.kind == .dollar && p.peek_tok.kind == .key_else {
|
||||
p.next()
|
||||
p.check(.key_else)
|
||||
p.parse_block()
|
||||
node.else_stmts = p.parse_block()
|
||||
}
|
||||
return ast.CompIf{}
|
||||
return node
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user