main
Alpha Chen 2 years ago
parent 59f286c139
commit dade99a51f

@ -18,6 +18,7 @@ module Lox
expr :Literal, :value
expr :Logical, :left, :op, :right
expr :Set, :object, :name, :value
expr :Super, :keyword, :method
expr :This, :keyword
expr :Unary, :op, :right
expr :Variable, :name

@ -316,6 +316,14 @@ module Lox
return Expr::Literal.new(true) if match?(:TRUE)
return Expr::Literal.new(nil) if match?(:NIL)
return Expr::Literal.new(prev.literal) if match?(:NUMBER, :STRING)
if match?(:SUPER)
keyword = prev
consume!(:DOT, "Expect '.' after 'super'.")
method = consume!(:IDENTIFIER, "Expect superclass method name.")
return Expr::Super.new(keyword, method)
end
return Expr::This.new(prev) if match?(:THIS)
return Expr::Variable.new(prev) if match?(:IDENTIFIER)

@ -484,6 +484,28 @@ class TestInterpreter < Lox::Test
SRC
end
def test_calling_superclass_methods
assert_interpreted <<~OUT, <<~SRC
Fry until golden brown.
Pipe full of custard and coat with chocolate.
OUT
class Doughnut {
cook() {
print "Fry until golden brown.";
}
}
class BostonCream < Doughnut {
cook() {
super.cook();
print "Pipe full of custard and coat with chocolate.";
}
}
BostonCream().cook();
SRC
end
private
def assert_interpreted(expected, src)

Loading…
Cancel
Save