From 7c9f273e3335e0a7db8af7618187410181d1ddfb Mon Sep 17 00:00:00 2001 From: yuyi Date: Sat, 15 Apr 2023 19:28:01 +0800 Subject: [PATCH] parser: fix comptime_for in repl (script mode) (fix #5976) (#17963) --- vlib/v/parser/parser.v | 21 +++++++++++++-------- vlib/v/slow_tests/repl/comptime_for.repl | 10 ++++++++++ 2 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 vlib/v/slow_tests/repl/comptime_for.repl diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 1a5de77e3e..6239f70a3a 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -751,15 +751,20 @@ pub fn (mut p Parser) top_stmt() ast.Stmt { if p.peek_tok.kind == .eof { return p.unexpected(got: 'eof') } - if_expr := p.if_expr(true) - cur_stmt := ast.ExprStmt{ - expr: if_expr - pos: if_expr.pos - } - if comptime_if_expr_contains_top_stmt(if_expr) { - return cur_stmt + if p.peek_tok.kind == .key_for { + comptime_for_stmt := p.comptime_for() + return p.other_stmts(comptime_for_stmt) } else { - return p.other_stmts(cur_stmt) + if_expr := p.if_expr(true) + cur_stmt := ast.ExprStmt{ + expr: if_expr + pos: if_expr.pos + } + if comptime_if_expr_contains_top_stmt(if_expr) { + return cur_stmt + } else { + return p.other_stmts(cur_stmt) + } } } .hash { diff --git a/vlib/v/slow_tests/repl/comptime_for.repl b/vlib/v/slow_tests/repl/comptime_for.repl new file mode 100644 index 0000000000..9b971fea07 --- /dev/null +++ b/vlib/v/slow_tests/repl/comptime_for.repl @@ -0,0 +1,10 @@ +struct Person { + name string + age int +} +$for field in Person.fields { + println(field.name) +} +===output=== +name +age