class AST::Node
redefining in order to allow type itself to be any type e.g. String
, Symbol, Fixnum
, etc.
Public Class Methods
new(type, children=[], properties={})
click to toggle source
The `properties` hash is passed to {#assign_properties}.
# File lib/ast_ext/node.rb, line 9 def initialize(type, children=[], properties={}) @type, @children = type, children.to_a.freeze assign_properties(properties) @hash = [@type.object_id, @children, self.class].hash freeze end
Public Instance Methods
print(logic=nil)
click to toggle source
@param logic [Hash] hash of operators allowed in this AST
containing each operator's print properties @return [String] string reconstituted from polish-notation into notation normally required by each operator
# File lib/ast_ext/node.rb, line 21 def print(logic=nil) return type.to_s if children.empty? str = '' op = type.respond_to?(:text) ? type : logic[type.to_s] case op.position when :prefix str << op.symbol children.each do |c| str << c.print(logic) end when :postfix children.each do |c| str << c.print(logic) end str << op.symbol when :infix if op.arity > 2 str << children.first.print(logic) << op.symbol << children[1].print(logic) << op.pair.to_s << children.last.print else str << (children.first.respond_to?(:print) ? children.first.print(logic) : children.first.to_s) << op.symbol << children.last.print end else # should not happen end str end