Class: Cyrel::AST::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/cyrel/ast/node.rb

Overview

Base class for all AST nodes Because every tree needs roots, even if they’re just pretending to be organized

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object

Equality for testing and debugging Because sometimes you need to know if two trees fell in the same forest



23
24
25
# File 'lib/cyrel/ast/node.rb', line 23

def ==(other)
  self.class == other.class && state == other.state
end

#accept(visitor) ⇒ Object

Accept a visitor for the visitor pattern It’s like accepting guests, but these guests judge your entire structure



10
11
12
13
14
15
16
17
18
19
# File 'lib/cyrel/ast/node.rb', line 10

def accept(visitor)
  method_name = "visit_#{self.class.name.demodulize.underscore}"
  if visitor.respond_to?(method_name)
    visitor.send(method_name, self)
  else
    raise NotImplementedError,
          "Visitor #{visitor.class} doesn't know how to visit #{self.class}. " \
          "Did you forget to implement #{method_name}?"
  end
end