main
Alpha Chen 2 years ago
parent 32ba8bf6c3
commit 78f3d0ee76

@ -46,5 +46,9 @@ module Lox
raise RuntimeError.new(name, "Undefined variable '#{lexeme}'.")
end
end
def assign_at(distance, name, value)
ancestor(distance).values[name] = value
end
end
end

@ -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)
end
value
end

Loading…
Cancel
Save