|
|
@ -10,17 +10,24 @@ module Lox
|
|
|
|
def visit_print(expr) = parenthesize("print", expr.expr)
|
|
|
|
def visit_print(expr) = parenthesize("print", expr.expr)
|
|
|
|
|
|
|
|
|
|
|
|
def visit_var(expr)
|
|
|
|
def visit_var(expr)
|
|
|
|
if init = expr.initializer
|
|
|
|
exprs = [expr.initializer].reject(&:nil?)
|
|
|
|
"(assign #{expr.name.lexeme} #{expr.initializer.value})"
|
|
|
|
parenthesize("var #{expr.name.lexeme}", *exprs)
|
|
|
|
else
|
|
|
|
end
|
|
|
|
"(var #{expr.name.lexeme})"
|
|
|
|
|
|
|
|
end
|
|
|
|
def visit_variable(expr)
|
|
|
|
|
|
|
|
"(var #{expr.name.lexeme})"
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def visit_assign(expr)
|
|
|
|
|
|
|
|
parenthesize("assign #{expr.name.lexeme}", expr.value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
|
|
def parenthesize(name, *exprs)
|
|
|
|
def parenthesize(name, *exprs)
|
|
|
|
"(#{name} #{exprs.map {|expr| expr.accept(self) }.join(" ")})"
|
|
|
|
inside = [name]
|
|
|
|
|
|
|
|
inside.concat(exprs.map {|expr| expr.accept(self) })
|
|
|
|
|
|
|
|
"(#{inside.join(" ")})"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|