|
|
@ -9,6 +9,7 @@ module Lox
|
|
|
|
def initialize(env=Environment.new)
|
|
|
|
def initialize(env=Environment.new)
|
|
|
|
@globals = env
|
|
|
|
@globals = env
|
|
|
|
@env = @globals
|
|
|
|
@env = @globals
|
|
|
|
|
|
|
|
@locals = {}
|
|
|
|
|
|
|
|
|
|
|
|
@globals.define("clock", Class.new {
|
|
|
|
@globals.define("clock", Class.new {
|
|
|
|
def arity = 0
|
|
|
|
def arity = 0
|
|
|
@ -28,6 +29,10 @@ module Lox
|
|
|
|
def evaluate(expr) = expr.accept(self)
|
|
|
|
def evaluate(expr) = expr.accept(self)
|
|
|
|
def execute(stmt) = stmt.accept(self)
|
|
|
|
def execute(stmt) = stmt.accept(self)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def resolve(expr, depth)
|
|
|
|
|
|
|
|
@locals[expr] = depth
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def visit_block(stmt)
|
|
|
|
def visit_block(stmt)
|
|
|
|
execute_block(stmt.stmts, Environment.new(@env))
|
|
|
|
execute_block(stmt.stmts, Environment.new(@env))
|
|
|
|
nil
|
|
|
|
nil
|
|
|
@ -116,7 +121,16 @@ module Lox
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def visit_variable(expr)
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def visit_assign(expr)
|
|
|
|
def visit_assign(expr)
|
|
|
|