cgen: generate indents for more readable C code

pull/4101/head
Alexander Medvednikov 2020-03-22 10:12:43 +01:00
parent 53a9329ab6
commit ab3f6d9202
5 changed files with 54 additions and 31 deletions

View File

@ -762,6 +762,9 @@ pub fn home_dir() string {
$if windows { $if windows {
return os.getenv('USERPROFILE') + os.path_separator return os.getenv('USERPROFILE') + os.path_separator
} $else { } $else {
//println('home_dir() call')
//res:= os.getenv('HOME') + os.path_separator
//println('res="$res"')
return os.getenv('HOME') + os.path_separator return os.getenv('HOME') + os.path_separator
} }
} }

View File

@ -388,6 +388,7 @@ pub struct CompIf {
pub: pub:
cond Expr cond Expr
stmts []Stmt stmts []Stmt
mut:
else_stmts []Stmt else_stmts []Stmt
} }

View File

@ -10,7 +10,7 @@ import (
) )
const ( 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 = ['', ' ', ' ', ' ', ' '] // tabs = ['', ' ', ' ', ' ', ' ']
max_len = 80 max_len = 80
) )
@ -100,20 +100,20 @@ fn (f mut Fmt) stmts(stmts []ast.Stmt) {
fn (f mut Fmt) stmt(node ast.Stmt) { fn (f mut Fmt) stmt(node ast.Stmt) {
match node { match node {
ast.AssignStmt { ast.AssignStmt {
for i,ident in it.left { for i, ident in it.left {
var_info := ident.var_info() var_info := ident.var_info()
if var_info.is_mut { if var_info.is_mut {
f.write('mut ') f.write('mut ')
} }
f.expr(ident) f.expr(ident)
if i < it.left.len-1 { if i < it.left.len - 1 {
f.write(', ') f.write(', ')
} }
} }
f.write(' $it.op.str() ') f.write(' $it.op.str() ')
for i,val in it.right { for i, val in it.right {
f.expr(val) f.expr(val)
if i < it.right.len-1 { if i < it.right.len - 1 {
f.write(', ') f.write(', ')
} }
} }
@ -131,7 +131,7 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
f.writeln('continue') f.writeln('continue')
} }
else {} else {}
} }
} }
ast.ConstDecl { ast.ConstDecl {
if it.is_pub { if it.is_pub {
@ -241,11 +241,10 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
f.stmts(it.stmts) f.stmts(it.stmts)
f.writeln('}') f.writeln('}')
} }
ast.Import { ast.Import {}
// already handled in f.imports // already handled in f.imports
}
ast.TypeDecl { ast.TypeDecl {
f.type_decl( it ) f.type_decl(it)
} }
else { else {
eprintln('fmt stmt: unknown node: ' + typeof(node)) eprintln('fmt stmt: unknown node: ' + typeof(node))
@ -260,7 +259,7 @@ fn (f mut Fmt) type_decl(node ast.TypeDecl) {
if it.is_pub { if it.is_pub {
f.write('pub ') f.write('pub ')
} }
ptype := f.table.type_to_str( it.parent_type ) ptype := f.table.type_to_str(it.parent_type)
f.write('type $it.name $ptype') f.write('type $it.name $ptype')
} }
ast.SumTypeDecl { ast.SumTypeDecl {
@ -272,7 +271,7 @@ fn (f mut Fmt) type_decl(node ast.TypeDecl) {
for t in it.sub_types { for t in it.sub_types {
sum_type_names << f.table.type_to_str(t) sum_type_names << f.table.type_to_str(t)
} }
f.write( sum_type_names.join(' | ') ) f.write(sum_type_names.join(' | '))
} }
else { else {
eprintln('fmt type_decl: unknown ' + typeof(node)) eprintln('fmt type_decl: unknown ' + typeof(node))
@ -385,7 +384,7 @@ fn (f mut Fmt) expr(node ast.Expr) {
} }
ast.IfExpr { ast.IfExpr {
single_line := it.branches.len == 2 && it.has_else // single_line := it.branches.len == 2 && it.has_else //
&& it.branches[0].stmts.len == 1 && it.branches[1].stmts.len == 1 && it.branches[0].stmts.len == 1 && it.branches[1].stmts.len == 1
f.single_line_if = single_line f.single_line_if = single_line
for i, branch in it.branches { for i, branch in it.branches {
if i == 0 { if i == 0 {
@ -393,12 +392,12 @@ fn (f mut Fmt) expr(node ast.Expr) {
f.expr(branch.cond) f.expr(branch.cond)
f.write(' {') f.write(' {')
} }
else if i < it.branches.len-1 || !it.has_else { else if i < it.branches.len - 1 || !it.has_else {
f.write('} else if ') f.write('} else if ')
f.expr(branch.cond) f.expr(branch.cond)
f.write(' {') f.write(' {')
} }
else if i == it.branches.len-1 && it.has_else { else if i == it.branches.len - 1 && it.has_else {
f.write('} else {') f.write('} else {')
} }
if single_line { if single_line {
@ -468,7 +467,7 @@ fn (f mut Fmt) expr(node ast.Expr) {
for i, branch in it.branches { for i, branch in it.branches {
// normal branch // normal branch
if i < it.branches.len - 1 { if i < it.branches.len - 1 {
for j,expr in branch.exprs { for j, expr in branch.exprs {
f.expr(expr) f.expr(expr)
if j < branch.exprs.len - 1 { if j < branch.exprs.len - 1 {
f.write(', ') f.write(', ')
@ -481,7 +480,8 @@ fn (f mut Fmt) expr(node ast.Expr) {
} }
if (branch.stmts.len == 0) { if (branch.stmts.len == 0) {
f.writeln(' {}') f.writeln(' {}')
} else { }
else {
f.writeln(' {') f.writeln(' {')
f.stmts(branch.stmts) f.stmts(branch.stmts)
f.writeln('}') f.writeln('}')

View File

@ -28,8 +28,15 @@ mut:
stmt_start_pos int stmt_start_pos int
right_is_opt bool right_is_opt bool
autofree 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 { pub fn cgen(files []ast.File, table &table.Table) string {
println('start cgen2') println('start cgen2')
mut g := Gen{ mut g := Gen{
@ -40,13 +47,14 @@ pub fn cgen(files []ast.File, table &table.Table) string {
table: table table: table
fn_decl: 0 fn_decl: 0
autofree: true autofree: true
indent: -1
} }
g.init() g.init()
for file in files { for file in files {
// println('cgen "$g.file.path" $file.stmts.len') // println('cgen "$g.file.path" $file.stmts.len')
g.file = file g.file = file
if g.file.path == '' || g.file.path.ends_with('.vv') { if g.file.path == '' || g.file.path.ends_with('.vv') || g.file.path.contains('/vlib/') {
// cgen test // cgen test or building V
g.autofree = false g.autofree = false
} }
g.stmts(file.stmts) g.stmts(file.stmts)
@ -191,11 +199,20 @@ pub fn (g mut Gen) write_variadic_types() {
pub fn (g &Gen) save() {} pub fn (g &Gen) save() {}
pub fn (g mut Gen) write(s string) { 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.out.write(s)
g.empty_line = false
} }
pub fn (g mut Gen) writeln(s string) { 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.out.writeln(s)
g.empty_line = true
} }
pub fn (g mut Gen) new_tmp_var() string { 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) { fn (g mut Gen) stmts(stmts []ast.Stmt) {
g.indent++
for stmt in stmts { for stmt in stmts {
g.stmt(stmt) g.stmt(stmt)
if !g.inside_ternary { // if !g.inside_ternary {
g.writeln('') // g.writeln('')
} // }
} }
g.indent--
} }
fn (g mut Gen) stmt(node ast.Stmt) { fn (g mut Gen) stmt(node ast.Stmt) {
@ -242,8 +261,8 @@ fn (g mut Gen) stmt(node ast.Stmt) {
ast.CompIf { ast.CompIf {
// TODO // TODO
g.writeln('//#ifdef ') g.writeln('//#ifdef ')
g.expr(it.cond) // g.expr(it.cond)
g.stmts(it.stmts) // g.stmts(it.stmts)
g.writeln('//#endif') g.writeln('//#endif')
} }
ast.DeferStmt { ast.DeferStmt {
@ -277,6 +296,7 @@ fn (g mut Gen) stmt(node ast.Stmt) {
ast.FnDecl { ast.FnDecl {
g.fn_decl = it // &it g.fn_decl = it // &it
g.gen_fn_decl(it) g.gen_fn_decl(it)
g.writeln('')
} }
ast.ForCStmt { ast.ForCStmt {
g.write('for (') 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);') g.writeln('os__args = os__init_os_args(argc, (byteptr*)argv);')
} }
for stmt in it.stmts { g.stmts(it.stmts)
// g.write('\t')
g.stmt(stmt)
}
// //////////// // ////////////
if g.autofree { if g.autofree {
scope := g.file.scope.innermost(it.pos.pos - 1) scope := g.file.scope.innermost(it.pos.pos - 1)

View File

@ -14,11 +14,13 @@ pub fn (p mut Parser) comp_if() ast.CompIf {
if p.tok.kind == .question { if p.tok.kind == .question {
p.next() p.next()
} }
p.parse_block() mut node := ast.CompIf{
stmts: p.parse_block()
}
if p.tok.kind == .dollar && p.peek_tok.kind == .key_else { if p.tok.kind == .dollar && p.peek_tok.kind == .key_else {
p.next() p.next()
p.check(.key_else) p.check(.key_else)
p.parse_block() node.else_stmts = p.parse_block()
} }
return ast.CompIf{} return node
} }