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;
has_init bool
cond Expr // i < 10;
has_cond bool
inc Expr // i++;
has_inc bool
stmts []Stmt
pos token.Position
}

View File

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

View File

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