diff --git a/ruby/lib/lox/expr.rb b/ruby/lib/lox/expr.rb index 38860a2..99bfbd1 100644 --- a/ruby/lib/lox/expr.rb +++ b/ruby/lib/lox/expr.rb @@ -1,19 +1,26 @@ 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 - def accept(visitor) = visitor.visit_binary(self) + include Visitable end Grouping = Struct.new(:expr) do - def accept(visitor) = visitor.visit_grouping(self) + include Visitable end Literal = Struct.new(:value) do - def accept(visitor) = visitor.visit_literal(self) + include Visitable end Unary = Struct.new(:op, :right) do - def accept(visitor) = visitor.visit_unary(self) + include Visitable end end end