main
Alpha Chen 2 years ago
parent 97371d13be
commit 437a2b9633

@ -7,7 +7,7 @@ module Lox
@scopes = [] @scopes = []
end end
def resolve(values) def resolve(*values)
values.each do values.each do
value.accept(self) value.accept(self)
end end
@ -15,11 +15,18 @@ module Lox
def visit_block(stmt) def visit_block(stmt)
with_scope do with_scope do
resolve(stmt.stmts) resolve(*stmt.stmts)
end end
nil nil
end end
def visit_var(stmt)
declare(stmt.name)
resolve(stmt.initializer) if stmt.initializer
define(stmt.name)
nil
end
private private
def with_block def with_block
@ -28,6 +35,19 @@ module Lox
@scopes.pop @scopes.pop
end end
def declare(name)
scope = @scopes.last
return if scope.nil?
scope[name.lexeme] = false
end
def define(name)
scope = @scopes.last
return if scope.nil?
scopes[name.lexeme] = true
end
end end
end end

Loading…
Cancel
Save