diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 5737e70490..ebc6eb9d35 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -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 } diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 559ac3e3bd..a97f06dd3e 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -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('}') diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 819085e2ec..f1eb44070d 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -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