@ -46,5 +46,9 @@ module Lox
raise RuntimeError.new(name, "Undefined variable '#{lexeme}'.")
end
def assign_at(distance, name, value)
ancestor(distance).values[name] = value
@ -135,7 +135,14 @@ module Lox
def visit_assign(expr)
value = evaluate(expr.value)
@env.assign(expr.name, value)
if @locals.has_key?(expr)
distance = @locals.get(expr)
@env.assign_at(distance, expr.name, value)
else
@globals.assign(expr.name, value)
value