Class: Minjs::ECMA262::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/minjs/ecma262/base.rb

Overview

ECMA262 Elements

Direct Known Subclasses

Expression, Literal, Prog, Statement, StatementList

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



5
6
7
# File 'lib/minjs/ecma262/base.rb', line 5

def parent
  @parent
end

Instance Method Details

#==(obj) ⇒ Object

compare object



82
83
84
85
# 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) ⇒ Object

add / remove parenthesis if need



88
89
90
91
92
93
94
95
96
# 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) ⇒ Object

concatenate some of ECMA262 elements and convert it to ECMAScript

Parameters:

  • args

    ECMA262 element

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • :debug (Boolean)

    if set, output is easy to read.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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_dupObject

duplicate object

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



77
78
79
# File 'lib/minjs/ecma262/base.rb', line 77

def deep_dup
  puts "warning: #{self.class}: deep_dup not implement"
end

#replace(from, to) ⇒ Object

Replaces child (if own it) object

Parameters:



70
71
72
# File 'lib/minjs/ecma262/base.rb', line 70

def replace(from, to)
  puts "warning: #{self.class}: replace not implement"
end

#to_js(options = {}) ⇒ String

Returns a ECMAScript string containg the representation of element.

Parameters:

  • options (Hash) (defaults to: {})

    options for Base#concat

Returns:

  • (String)

    ECMAScript string.



10
11
12
# File 'lib/minjs/ecma262/base.rb', line 10

def to_js(options = {})
  self.class.to_s + "??"
end

#traverse(parent) {|parent, self| ... } ⇒ Object

Traverses this children and itself with given block.

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

Parameters:

  • parent (Base)

    parent element.

Yields:

  • (parent, self)

    parent and this element.

Yield Parameters:

  • self (Base)

    this element.

  • parent (Base)

    parent element.



107
108
109
# File 'lib/minjs/ecma262/base.rb', line 107

def traverse(parent, &block)

end