|
|
@ -2,14 +2,14 @@ require_relative "environment"
|
|
|
|
|
|
|
|
|
|
|
|
module Lox
|
|
|
|
module Lox
|
|
|
|
class Function
|
|
|
|
class Function
|
|
|
|
def initialize(decl, closure)
|
|
|
|
def initialize(decl, closure, is_initializer)
|
|
|
|
@decl, @closure = decl, closure
|
|
|
|
@decl, @closure, @is_initializer = decl, closure, is_initializer
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def bind(instance)
|
|
|
|
def bind(instance)
|
|
|
|
env = Environment.new(@closure)
|
|
|
|
env = Environment.new(@closure)
|
|
|
|
env.define("this", instance)
|
|
|
|
env.define("this", instance)
|
|
|
|
Function.new(@decl, env)
|
|
|
|
Function.new(@decl, env, @is_initializer)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def arity = @decl.params.size
|
|
|
|
def arity = @decl.params.size
|
|
|
@ -20,9 +20,13 @@ module Lox
|
|
|
|
env.define(name, value)
|
|
|
|
env.define(name, value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
catch(:return) {
|
|
|
|
return_value = catch(:return) {
|
|
|
|
interpreter.execute_block(@decl.body, env)
|
|
|
|
interpreter.execute_block(@decl.body, env)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return @closure.get_at(0, "this") if @is_initializer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return_value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def to_s = "<fn #{@decl.name.lexeme}>"
|
|
|
|
def to_s = "<fn #{@decl.name.lexeme}>"
|
|
|
|