FossilOrigin-Name: f908d358ac9f54ee9935ba8f2b55c54e072cdda14ed3693b6959a828aef34a65
private
alpha 2 years ago
parent 44ab6454fa
commit fb649eeaba

@ -8,7 +8,14 @@ module Lox
def visit_unary(expr) = parenthesize(expr.op.lexeme, expr.right)
def visit_print(expr) = parenthesize("print", expr.expr)
def visit_var(expr) = "(var #{expr.name.lexeme} #{expr.initializer.value})"
def visit_var(expr)
if init = expr.initializer
"(assign #{expr.name.lexeme} #{expr.initializer.value})"
else
"(var #{expr.name.lexeme})"
end
end
private

@ -2,6 +2,10 @@ require_relative "visitable"
module Lox
module Expr
Assign = Struct.new(:name, :value) do
include Visitable
end
Binary = Struct.new(:left, :op, :right) do
include Visitable
end

@ -54,6 +54,21 @@ module Lox
Stmt::Expr.new(value)
end
def assignment
expr = equality
if match?(:EQUAL)
eq = prev
value = assignment
raise ParseError.new(eq, "Invalid assignment target.") unless expr.instance_of?(Expr::Variable)
Expr::Assign.new(expr.name, value)
end
expr
end
def expression = equality
def equality

@ -52,7 +52,11 @@ class TestParser < Lox::Test
end
def test_var
assert_parsed "(var foo 42.0)", :declaration, "var foo = 42.0;"
assert_parsed "(var foo)", :declaration, "var foo;"
end
def test_assign
assert_parsed "(assign foo 42.0)", :declaration, "var foo = 42.0;"
end
private

Loading…
Cancel
Save