lib/furnace/ast/node.rb in furnace-0.2.6 vs lib/furnace/ast/node.rb in furnace-0.3.0.beta1

- old
+ new

@@ -1,38 +1,35 @@ module Furnace::AST class Node - attr_accessor :type, :children, :metadata + attr_reader :type, :children - def initialize(type, children=[], metadata={}) - @type, @children, @metadata = type.to_sym, children, metadata + def initialize(type, children=[], properties={}) + @type, @children = type.to_sym, children.to_a + properties.each do |name, value| + instance_variable_set :"@#{name}", value + end + freeze end - def update(type, children=nil, metadata={}) - @type = type - @children = children || @children + def update(type=nil, children=nil, properties={}) + new_type = type || @type + new_children = children || @children - # If something non-nil is passed, including default value, then merge. - # Else, clear metadata store. - if metadata - @metadata.merge!(metadata) + if @type == new_type && + @children == new_children && + properties.empty? + self else - @metadata = {} + dup.send :initialize, new_type, new_children, properties end - - self end - def dup - node = super - node.children = @children.dup - node.metadata = @metadata.dup - node - end - def ==(other) - if other.respond_to? :to_astlet - other = other.to_astlet + if equal?(other) + true + elsif other.respond_to? :to_ast + other = other.to_ast other.type == self.type && other.children == self.children else false end @@ -43,21 +40,14 @@ end def to_sexp(indent=0) str = "#{" " * indent}(#{fancy_type}" - if @metadata[:ellipsis] - str << " <omitted>)" - - return str - end - children.each do |child| if (!children[0].is_a?(Node) && child.is_a?(Node)) || (children[0].is_a?(Node) && child.is_a?(Node) && - child.children.any? { |c| c.is_a?(Node) || c.is_a?(Array) }) || - (child.is_a?(Node) && child.metadata[:label]) + child.children.any? { |c| c.is_a?(Node) || c.is_a?(Array) }) str << "\n#{child.to_sexp(indent + 1)}" else str << " #{child.inspect}" end end @@ -66,35 +56,16 @@ str end alias :inspect :to_sexp - def to_astlet + def to_ast self end protected def fancy_type - dasherized = @type.to_s.gsub('_', '-') - - if @metadata.any? - metainfo = @metadata.dup - metainfo.delete :label - metainfo.delete :origin - metainfo.delete :ellipsis - - if metainfo.any? - metainfo = "#{metainfo.inspect}:" - else - metainfo = nil - end - end - - if @metadata[:label] - "#{@metadata[:label]}:#{metainfo}#{dasherized}" - else - "#{metainfo}#{dasherized}" - end + @type.to_s.gsub('_', '-') end end end \ No newline at end of file