main
Alpha Chen 2 years ago
parent 59f286c139
commit dade99a51f

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

@ -316,6 +316,14 @@ module Lox
return Expr::Literal.new(true) if match?(:TRUE) return Expr::Literal.new(true) if match?(:TRUE)
return Expr::Literal.new(nil) if match?(:NIL) return Expr::Literal.new(nil) if match?(:NIL)
return Expr::Literal.new(prev.literal) if match?(:NUMBER, :STRING) 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::This.new(prev) if match?(:THIS)
return Expr::Variable.new(prev) if match?(:IDENTIFIER) return Expr::Variable.new(prev) if match?(:IDENTIFIER)

@ -484,6 +484,28 @@ class TestInterpreter < Lox::Test
SRC SRC
end 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 private
def assert_interpreted(expected, src) def assert_interpreted(expected, src)

Loading…
Cancel
Save