Alpha Chen 2 years ago
parent 1e16555963
commit 613c419b5c

@ -7,13 +7,6 @@ require "lox/scanner"
class TestResolver < Lox::Test class TestResolver < Lox::Test
def setup
@ast_printer = Lox::AstPrinter.new
@scanner = Lox::Scanner.new
@interpreter = Interpreter.new
@resolver = Lox::Resolver.new(@interpreter)
end
def test_resolver def test_resolver
assert_resolved [ assert_resolved [
["(var j)", 0], ["(var j)", 0],
@ -47,17 +40,37 @@ class TestResolver < Lox::Test
SRC SRC
end end
def test_same_var_name_in_scope
assert_raises Lox::ResolverError do
resolve(<<~SRC)
fun bad() {
var a = "first";
var a = "second";
}
SRC
end
end
private private
def assert_resolved(expected, src) def assert_resolved(expected, src)
stmts = Lox::Parser.new(@scanner.scan(src)).parse! resolves = resolve(src)
@resolver.resolve(*stmts);
assert_equal expected, @interpreter.resolves.map {|expr, depth| ast_printer = Lox::AstPrinter.new
[@ast_printer.print(expr), depth] assert_equal expected, resolves.map {|expr, depth|
[ast_printer.print(expr), depth]
} }
end end
def resolve(src)
stmts = Lox::Parser.new(Lox::Scanner.new.scan(src)).parse!
interpreter = Interpreter.new
Lox::Resolver.new(interpreter).resolve(*stmts)
interpreter.resolves
end
class Interpreter class Interpreter
attr_reader :resolves attr_reader :resolves

Loading…
Cancel
Save