main
Alpha Chen 2 years ago
parent bdb7d9f6c9
commit 32ba8bf6c3

@ -9,6 +9,7 @@ module Lox
def initialize(env=Environment.new)
@globals = env
@env = @globals
@locals = {}
@globals.define("clock", Class.new {
def arity = 0
@ -28,6 +29,10 @@ module Lox
def evaluate(expr) = expr.accept(self)
def execute(stmt) = stmt.accept(self)
def resolve(expr, depth)
@locals[expr] = depth
end
def visit_block(stmt)
execute_block(stmt.stmts, Environment.new(@env))
nil
@ -116,7 +121,16 @@ module Lox
end
def visit_variable(expr)
@env.get(expr.name)
lookup_var(expr.name, expr)
end
def lookup_var(name, expr)
if @locals.has_key?(expr)
distance = @locals.fetch(expr)
@env.get_at(distance, name.lexeme)
else
@globals.get(name)
end
end
def visit_assign(expr)

Loading…
Cancel
Save