refactor resolver

main
Alpha Chen 2 years ago
parent 02bea3faea
commit 9140a71593

@ -9,10 +9,11 @@ module Lox
@scopes = [] @scopes = []
end end
def resolve(*values) def resolve(*resolvees)
values.each do resolvees.each do |resolvee|
value.accept(self) resolvee.accept(self)
end end
nil
end end
def visit_block(stmt) def visit_block(stmt)
@ -22,9 +23,7 @@ module Lox
nil nil
end end
def visit_expr(stmt) def visit_expr(stmt) = resolve(stmt.expr)
resolve(stmt.expr)
nil
end end
def visit_function(stmt) def visit_function(stmt)
@ -36,21 +35,12 @@ module Lox
end end
def visit_if(stmt) def visit_if(stmt)
resolve(stmt.condition) resolve(stmt.condition, stmt.then)
resolve(stmt.then)
resolve(stmt.else) if stmt.else resolve(stmt.else) if stmt.else
nil
end end
def visit_print(stmt) def visit_print(stmt) = resolve(stmt.expr)
resolve(stmt.expr) def visit_return(stmt) = resolve(stmt.value) if stmt.value
nil
end
def visit_return(stmt)
resolve(stmt.value) if stmt.value
nil
end
def visit_var(stmt) def visit_var(stmt)
declare(stmt.name) declare(stmt.name)
@ -59,11 +49,7 @@ module Lox
nil nil
end end
def visit_while(stmt) def visit_while(stmt) = resolve(stmt.condition, stmt.body)
resolve(stmt.condition)
resolve(stmt.body)
nil
end
def visit_variable(expr) def visit_variable(expr)
if !@scopes.empty? && @scopes.last[expr.name.lexeme] == false if !@scopes.empty? && @scopes.last[expr.name.lexeme] == false
@ -80,35 +66,12 @@ module Lox
nil nil
end end
def visit_binary(expr) def visit_binary(expr) = resolve(expr.left, expr.right)
resolve(expr.left) def visit_call(expr) = resolve(expr.callee, *expr.args)
resolve(expr.right) def visit_grouping(expr) = resolve(expr.expr)
nil def visit_literal(expr) = nil
end def visit_logical(expr) = resolve(expr.left, expr.right)
def visit_unary(expr) = resolve(expr.right)
def visit_call(expr)
resolve(expr.callee, *expr.args)
nil
end
def visit_grouping(expr)
resolve(expr.expr)
nil
end
def visit_literal(expr)
nil
end
def visit_logical(expr)
resolve(expr.left, expr.right)
nil
end
def visit_unary(expr)
resolve(expr.right)
nil
end
private private

Loading…
Cancel
Save