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 {
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
}
}

View File

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

View File

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

View File

@ -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)

View File

@ -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
}