From cf72deef8b39c845c04210899f9caf8ce972e0c6 Mon Sep 17 00:00:00 2001 From: alpha Date: Tue, 26 Jul 2022 20:28:18 +0000 Subject: [PATCH] make a Visitable module FossilOrigin-Name: 3ffc71c707c994c44a9f33025ed50ab91d2a7e60be6c639b6ed28798d82728a2 --- ruby/lib/lox/expr.rb | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) 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