class Minjs::ECMA262::Base

ECMA262 Elements

Attributes

parent[RW]

Public Instance Methods

==(obj) click to toggle source

compare object

# File lib/minjs/ecma262/base.rb, line 82
def ==(obj)
  puts "warning: #{self.class}: == not implement"
  raise "warning: #{self.class}: == not implement"
end
add_remove_paren(node = self) click to toggle source

add / remove parenthesis if need

# File lib/minjs/ecma262/base.rb, line 88
def add_remove_paren(node = self)
  node.traverse(nil) {|st, parent|
    if st.respond_to? :remove_paren
      st.add_paren
      st.remove_paren
    end
  }
  node
end
concat(options, *args) click to toggle source

concatenate some of ECMA262 elements and convert it to ECMAScript

@param args ECMA262 element @option options :debug [Boolean] if set, output is easy to read.

# File lib/minjs/ecma262/base.rb, line 19
def concat(options, *args)
  prev = nil
  j = []
  args.flatten.each do|x|
    sep = ''
    nl = ''
    if x.kind_of? Base
      js = x.to_js(options);
    else
      js = x.to_s
    end
    if prev
      if prev.match(/[\w\$]\z/) and js.match(/\A[\w\$]/)
        sep = ' '
      end
      # ';;' means 'empty statement' or separator of 'for statement'
      # that must not be deleted
      if prev.match(/;;\Z/)
        prev.sub!(/;;\Z/, ";")
      elsif prev.match(/;\Z/) and js == "}"
        prev.sub!(/;\Z/, "")
      elsif prev.match(/;\Z/) and js == ";"
        prev.sub!(/;\Z/, "")
      elsif prev.match(/[\-]\Z/) and js.match(/^\-/)
        sep = ' '
      elsif prev.match(/[\+]\Z/) and js.match(/^\+/)
        sep = ' '
      end
    end
    #for debug
    unless options[:no_debug]
      if (@logger and @logger.debug?) || options[:debug]
        if js.match(/;\z/)
          nl = "\n"
        end
        if js.match(/}\z/)
          nl = "\n"
        end
      end
    end
    js = "#{sep}#{js}#{nl}";
    j.push(js)
    prev = js
  end
  j.join("")
end
deep_dup() click to toggle source

duplicate object

duplicate this object’s children (if own) and itself.

# File lib/minjs/ecma262/base.rb, line 77
def deep_dup
  puts "warning: #{self.class}: deep_dup not implement"
end
replace(from, to) click to toggle source

Replaces child (if own it) object

@param from [Base] from @param to [Base] to

# File lib/minjs/ecma262/base.rb, line 70
def replace(from, to)
  puts "warning: #{self.class}: replace not implement"
end
to_js(options = {}) click to toggle source

Returns a ECMAScript string containg the representation of element. @param options [Hash] options for Base#concat @return [String] ECMAScript string.

# File lib/minjs/ecma262/base.rb, line 10
def to_js(options = {})
  self.class.to_s + "??"
end
traverse(parent, &block) click to toggle source

Traverses this children and itself with given block.

If this element has children, traverse children first, then yield block with parent and self.

@param parent [Base] parent element. @yield [parent, self] parent and this element. @yieldparam [Base] self this element. @yieldparam [Base] parent parent element.

# File lib/minjs/ecma262/base.rb, line 107
def traverse(parent, &block)

end