|
|
@ -1,8 +1,11 @@
|
|
|
|
require_relative "environment"
|
|
|
|
require_relative "environment"
|
|
|
|
|
|
|
|
require_relative "function"
|
|
|
|
|
|
|
|
|
|
|
|
module Lox
|
|
|
|
module Lox
|
|
|
|
class Interpreter
|
|
|
|
class Interpreter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
attr_reader :globals
|
|
|
|
|
|
|
|
|
|
|
|
def initialize(env=Environment.new)
|
|
|
|
def initialize(env=Environment.new)
|
|
|
|
@globals = env
|
|
|
|
@globals = env
|
|
|
|
@env = @globals
|
|
|
|
@env = @globals
|
|
|
@ -46,6 +49,12 @@ module Lox
|
|
|
|
nil
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def visit_function(stmt)
|
|
|
|
|
|
|
|
function = Function.new(stmt)
|
|
|
|
|
|
|
|
@env.define(stmt.name.lexeme, function)
|
|
|
|
|
|
|
|
nil
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def visit_if(stmt)
|
|
|
|
def visit_if(stmt)
|
|
|
|
if truthy?(evaluate(stmt.cond))
|
|
|
|
if truthy?(evaluate(stmt.cond))
|
|
|
|
evaluate(stmt.then)
|
|
|
|
evaluate(stmt.then)
|
|
|
|