fix parsing bug w/no conditional in a for loop

main
Alpha Chen 2 years ago
parent 963e123418
commit 1b0a2b3a7a

@ -68,10 +68,10 @@ module Lox
expression_stmt
end
condition = !check?(:SEMICOLON) ? expression : Expr::Literal(true)
condition = check?(:SEMICOLON) ? Expr::Literal.new(true) : expression
consume!(:SEMICOLON, "Expect ';' after loop condition.")
increment = !check?(:RIGHT_PAREN) ? expression : nil
increment = check?(:RIGHT_PAREN) ? nil : expression
consume!(:RIGHT_PAREN, "Expect ')' after for clauses.")
body = statement

@ -90,6 +90,10 @@ class TestParser < Lox::Test
assert_parsed <<~AST.chomp, :statement, "for(var i=0; i<3; i=i+1) print i;"
(block (var i 0.0) (while (< (var i) 3.0) (block (print (var i)) (assign i (+ (var i) 1.0)))))
AST
assert_parsed <<~AST.chomp, :statement, "for(var i=0; ; i=i+1) print i;"
(block (var i 0.0) (while true (block (print (var i)) (assign i (+ (var i) 1.0)))))
AST
end
private

Loading…
Cancel
Save