cgen: skip inc generation for ForCStmt, when empty in the v source

pull/4323/head
Delyan Angelov 2020-04-10 10:00:14 +03:00
parent d5fb68e3d6
commit 7d564e9791
3 changed files with 14 additions and 3 deletions

View File

@ -434,7 +434,9 @@ pub:
init Stmt // i := 0; init Stmt // i := 0;
has_init bool has_init bool
cond Expr // i < 10; cond Expr // i < 10;
has_cond bool
inc Expr // i++; inc Expr // i++;
has_inc bool
stmts []Stmt stmts []Stmt
pos token.Position pos token.Position
} }

View File

@ -407,10 +407,13 @@ fn (g mut Gen) stmt(node ast.Stmt) {
} else { } else {
g.stmt(it.init) g.stmt(it.init)
} }
if it.has_cond {
g.expr(it.cond) g.expr(it.cond)
}
g.write('; ') g.write('; ')
// g.stmt(it.inc) if it.has_inc {
g.expr(it.inc) g.expr(it.inc)
}
g.writeln(') {') g.writeln(') {')
g.stmts(it.stmts) g.stmts(it.stmts)
g.writeln('}') g.writeln('}')

View File

@ -1059,6 +1059,8 @@ fn (p mut Parser) for_stmt() ast.Stmt {
// mut inc := ast.Stmt{} // mut inc := ast.Stmt{}
mut inc := ast.Expr{} mut inc := ast.Expr{}
mut has_init := false mut has_init := false
mut has_cond := false
mut has_inc := false
if p.peek_tok.kind in [.assign, .decl_assign] { if p.peek_tok.kind in [.assign, .decl_assign] {
init = p.assign_stmt() init = p.assign_stmt()
has_init = true has_init = true
@ -1070,11 +1072,13 @@ fn (p mut Parser) for_stmt() ast.Stmt {
if p.tok.kind != .semicolon { if p.tok.kind != .semicolon {
mut typ := table.void_type mut typ := table.void_type
cond = p.expr(0) cond = p.expr(0)
has_cond = true
} }
p.check(.semicolon) p.check(.semicolon)
if p.tok.kind != .lcbr { if p.tok.kind != .lcbr {
// inc = p.stmt() // inc = p.stmt()
inc = p.expr(0) inc = p.expr(0)
has_inc = true
} }
p.inside_for = false p.inside_for = false
stmts := p.parse_block() stmts := p.parse_block()
@ -1082,6 +1086,8 @@ fn (p mut Parser) for_stmt() ast.Stmt {
return ast.ForCStmt{ return ast.ForCStmt{
stmts: stmts stmts: stmts
has_init: has_init has_init: has_init
has_cond: has_cond
has_inc: has_inc
init: init init: init
cond: cond cond: cond
inc: inc inc: inc