FossilOrigin-Name: 5d014cbe85c713fbe1fccf2a053db4b4474c0eee5e909d7cfd7de58fcbb03f77
private
alpha 2 years ago
parent cf72deef8b
commit 22a126dd0b

@ -1,12 +1,7 @@
require_relative "visitable"
module Lox
module Expr
module Visitable
def accept(visitor)
klass = self.class.to_s.split("::").last.downcase
visitor.send("visit_#{klass}", self)
end
end
Binary = Struct.new(:left, :op, :right) do
include Visitable
end

@ -1,14 +1,24 @@
module Lox
class Interpreter
# The book does printing and error catching here, but
# we're going to do it in the runner instead.
def interpret(expr)
value = evaluate(expr)
stringify(value)
def interpret(stmts)
stmts.each do |stmt|
evaluate(stmt)
end
end
def evaluate(stmt) = stmt.accept(self)
def visit_expr(expr)
evaluate(expr.expr)
nil
end
def evaluate(expr) = expr.accept(self)
def visit_print(expr)
puts stringify(evaluate(expr.expr))
nil
end
def visit_grouping(expr) = evaluate(expr.expr)
def visit_literal(expr) = expr.value

@ -1,11 +1,41 @@
require_relative "error"
require_relative "expr"
require_relative "stmt"
module Lox
class Parser
class State < Struct.new(:tokens, :current)
def initialize(tokens)
super(tokens, 0)
@tokens = tokens
@current = 0
end
def parse!
statements = []
until eot?
statements << statement
end
statements
end
private
def statement
return print if match?(:PRINT)
expressionStmt
end
def print
value = expression
consume!(:SEMICOLON, "Expect ';' after value.")
Stmt::Print.new(value)
end
def expressionStmt
value = expression
consume!(:SEMICOLON, "Expect ';' after value.")
Stmt::Expr.new(value)
end
def expression = equality
@ -34,7 +64,6 @@ module Lox
expr
end
def term
expr = factor
@ -104,13 +133,13 @@ module Lox
end
def advance!
self.current += 1 unless eot?
@current += 1 unless eot?
prev
end
def eot? = peek.type === :EOF
def peek = tokens.fetch(current)
def prev = tokens.fetch(current - 1)
def peek = @tokens.fetch(@current)
def prev = @tokens.fetch(@current - 1)
def synchronize!
advance!
@ -124,12 +153,4 @@ module Lox
end
end
# In the book, this returns nil when there's an error, but
# that feels weird so let's move that error handling up the
# stack for now.
def parse(tokens)
state = State.new(tokens)
state.expression
end
end
end

@ -0,0 +1,13 @@
require_relative "visitable"
module Lox
module Stmt
Expr = Struct.new(:expr) do
include Visitable
end
Print = Struct.new(:expr) do
include Visitable
end
end
end

@ -0,0 +1,8 @@
module Lox
module Visitable
def accept(visitor)
klass = self.class.to_s.split("::").last.downcase
visitor.send("visit_#{klass}", self)
end
end
end

@ -7,62 +7,61 @@ require "lox/scanner"
class TestInterpreter < Lox::Test
def setup
@scanner = Lox::Scanner.new
@parser = Lox::Parser.new
@interpreter = Lox::Interpreter.new
end
def test_literal
assert_interpreted(42.0, "42")
end
# def test_literal
# assert_evaluated(42.0, "42")
# end
def test_grouping
assert_interpreted(42.0, "(42)")
assert_evaluated "42", "(42)"
end
def test_unary
assert_interpreted(-42.0, "-42")
assert_interpreted(false, "!42")
assert_interpreted(false, "!true")
assert_interpreted(true, "!false")
assert_interpreted(true, "!nil")
assert_evaluated "-42", "-42"
assert_evaluated "false", "!42"
assert_evaluated "false", "!true"
assert_evaluated "true", "!false"
assert_evaluated "true", "!nil"
end
def test_binary
assert_interpreted(42.0, "100 - 58")
assert_interpreted(42.0, "84 / 2")
assert_interpreted(42.0, "21 * 2")
assert_evaluated "42", "100 - 58"
assert_evaluated "42", "84 / 2"
assert_evaluated "42", "21 * 2"
# precedence
assert_interpreted(42.0, "2 * 25 - 8")
assert_evaluated "42", "2 * 25 - 8"
assert_interpreted(42.0, "40 + 2")
assert_interpreted("42", "\"4\" + \"2\"")
assert_evaluated "42", "40 + 2"
assert_evaluated "42", "\"4\" + \"2\""
assert_interpreted(true, "1 > 0")
assert_interpreted(false, "0 > 0")
assert_interpreted(false, "0 > 1")
assert_evaluated "true", "1 > 0"
assert_evaluated "false", "0 > 0"
assert_evaluated "false", "0 > 1"
assert_interpreted(true, "1 >= 0")
assert_interpreted(true, "0 >= 0")
assert_interpreted(false, "0 >= 1")
assert_evaluated "true", "1 >= 0"
assert_evaluated "true", "0 >= 0"
assert_evaluated "false", "0 >= 1"
assert_interpreted(false, "1 < 0")
assert_interpreted(false, "0 < 0")
assert_interpreted(true, "0 < 1")
assert_evaluated "false", "1 < 0"
assert_evaluated "false", "0 < 0"
assert_evaluated "true", "0 < 1"
assert_interpreted(false, "1 <= 0")
assert_interpreted(true, "0 <= 0")
assert_interpreted(true, "0 <= 1")
assert_evaluated "false", "1 <= 0"
assert_evaluated "true", "0 <= 0"
assert_evaluated "true", "0 <= 1"
assert_interpreted(true, "0 != 1")
assert_interpreted(false, "0 != 0")
assert_interpreted(false, "nil != nil")
assert_interpreted(true, "nil != 1")
assert_evaluated "true", "0 != 1"
assert_evaluated "false", "0 != 0"
assert_evaluated "false", "nil != nil"
assert_evaluated "true", "nil != 1"
assert_interpreted(false, "0 == 1")
assert_interpreted(true, "0 == 0")
assert_interpreted(true, "nil == nil")
assert_interpreted(false, "nil == 1")
assert_evaluated "false", "0 == 1"
assert_evaluated "true", "0 == 0"
assert_evaluated "true", "nil == nil"
assert_evaluated "false", "nil == 1"
end
def test_errors
@ -74,31 +73,53 @@ class TestInterpreter < Lox::Test
"false + 23",
].each do |src|
assert_raises Lox::RuntimeError do
evaluate(src)
assert_evaluated nil, src
end
end
end
def test_stringify
assert_equal "nil", interpret("nil")
assert_equal "42", interpret("42")
assert_equal "42.1", interpret("42.1")
assert_equal "foo", interpret("\"foo\"")
assert_evaluated "nil", "nil"
assert_evaluated "42", "42"
assert_evaluated "42.1", "42.1"
assert_evaluated "foo", "\"foo\""
end
def test_multiple_statements
assert_interpreted <<~EXPECTED.chomp, <<~SRC
one
true
3
EXPECTED
print "one";
print true;
print 2 + 1;
SRC
end
private
def evaluate(src)
expr = @parser.parse(@scanner.scan(src))
@interpreter.evaluate(expr)
def assert_interpreted(expected, src)
output = with_stdout {
stmts = Lox::Parser.new(@scanner.scan(src)).parse!
@interpreter.interpret(stmts)
}
assert_equal expected, output
end
def interpret(src)
expr = @parser.parse(@scanner.scan(src))
@interpreter.interpret(expr)
def assert_evaluated(expected, src)
assert_interpreted(expected, "print #{src};")
end
def assert_interpreted(expected, src)
assert_equal expected, evaluate(src)
def with_stdout
original = $stdout
$stdout = StringIO.new
yield
output = $stdout.string.chomp
$stdout = original
output
end
end

@ -56,7 +56,7 @@ class TestParser < Lox::Test
def parse(src, name)
tokens = @scanner.scan(src)
Lox::Parser::State.new(tokens).send(name)
Lox::Parser.new(tokens).send(name)
end
def assert_parsed(expected, name, src)

Loading…
Cancel
Save