|
|
@ -7,6 +7,7 @@ module Lox
|
|
|
|
@interpreter = interpreter
|
|
|
|
@interpreter = interpreter
|
|
|
|
|
|
|
|
|
|
|
|
@scopes = []
|
|
|
|
@scopes = []
|
|
|
|
|
|
|
|
@current_func = :NONE
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def resolve(*resolvees)
|
|
|
|
def resolve(*resolvees)
|
|
|
@ -29,7 +30,7 @@ module Lox
|
|
|
|
declare(stmt.name)
|
|
|
|
declare(stmt.name)
|
|
|
|
define(stmt.name)
|
|
|
|
define(stmt.name)
|
|
|
|
|
|
|
|
|
|
|
|
resolve_function(stmt)
|
|
|
|
resolve_function(stmt, :FUNCTION)
|
|
|
|
nil
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
@ -40,6 +41,7 @@ module Lox
|
|
|
|
|
|
|
|
|
|
|
|
def visit_print(stmt) = resolve(stmt.expr)
|
|
|
|
def visit_print(stmt) = resolve(stmt.expr)
|
|
|
|
def visit_return(stmt)
|
|
|
|
def visit_return(stmt)
|
|
|
|
|
|
|
|
raise ResolverError.new(stmt.keyword, "Can't return from top-level code.") if @current_func == :NONE
|
|
|
|
resolve(stmt.value) if stmt.value
|
|
|
|
resolve(stmt.value) if stmt.value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
@ -106,7 +108,8 @@ module Lox
|
|
|
|
@interpreter.resolve(expr, depth)
|
|
|
|
@interpreter.resolve(expr, depth)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def resolve_function(fn)
|
|
|
|
def resolve_function(fn, type)
|
|
|
|
|
|
|
|
with_func_type(type) do
|
|
|
|
with_scope do
|
|
|
|
with_scope do
|
|
|
|
fn.params.each do |param|
|
|
|
|
fn.params.each do |param|
|
|
|
|
declare(param)
|
|
|
|
declare(param)
|
|
|
@ -115,6 +118,16 @@ module Lox
|
|
|
|
resolve(*fn.body)
|
|
|
|
resolve(*fn.body)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def with_func_type(type)
|
|
|
|
|
|
|
|
enclosing_func = @current_func
|
|
|
|
|
|
|
|
@current_func = type
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@current_func = enclosing_func
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|