class Minjs::ECMA262::StatementList

Class of ECMA262 Statement List

@see www.ecma-international.org/ecma-262 ECMA262 12.1

Attributes

statement_list[R]

Public Class Methods

new(statement_list) click to toggle source
# File lib/minjs/ecma262/base.rb, line 118
def initialize(statement_list)
  @statement_list = statement_list #array
end

Public Instance Methods

==(obj) click to toggle source

compare object

# File lib/minjs/ecma262/base.rb, line 225
def ==(obj)
  @statement_list == obj.statement_list
end
[](i) click to toggle source

Returns the statement at index @param i index @return [Statement] statement

# File lib/minjs/ecma262/base.rb, line 268
def [](i)
  @statement_list[i]
end
[]=(i, st) click to toggle source

Sets the statement at index. @param i index @param st statement

# File lib/minjs/ecma262/base.rb, line 275
def []=(i, st)
  @statement_list[i] = st
end
deep_dup() click to toggle source

duplicate object @see Base#deep_dup

# File lib/minjs/ecma262/base.rb, line 188
def deep_dup
  self.class.new(@statement_list.collect{|s| s.deep_dup})
end
each(&block) click to toggle source
# File lib/minjs/ecma262/base.rb, line 261
def each(&block)
  @statement_list.each(&block)
end
grouping() click to toggle source

Groups statements and reduce number of them as few as posibble.

# File lib/minjs/ecma262/base.rb, line 123
def grouping
  remove_empty_statement
  new_sl = []
  sl = []
  g = []
  @statement_list.each do |st|
    if st.to_exp?
      g.push(st)
    else
      if g.length > 0
        sl.push(g)
      end
      sl.push([st])
      g = []
    end
  end
  if g.length > 0
    sl.push(g)
  end

  sl.each do |g|
    if g.length == 1
      new_sl.push(g[0])
    else
      i = 1
      t = ExpParen.new(g[0].to_exp)
      while i < g.length
        t = ExpComma.new(t, ExpParen.new(g[i].to_exp))
        i += 1
      end
      new_sl.push(StExp.new(t))
    end
  end

  if idx = new_sl.index{|x| x.class == StReturn}
    idx += 1
    while idx < new_sl.length
      if new_sl[idx].kind_of? StVar
        ;
      elsif new_sl[idx].kind_of? StFunc
        ;
      else
        new_sl[idx] = StEmpty.new
      end
      idx += 1
    end
  end

  if self.kind_of? SourceElements
    if new_sl[-1].kind_of? StReturn and new_sl[-1].exp.nil?
      new_sl.pop
    end
  end

  if new_sl[-1].kind_of? StReturn and new_sl[-2].kind_of? StExp
    if new_sl[-1].exp
      new_sl[-2] = StReturn.new(ExpComma.new(new_sl[-2].exp, new_sl[-1].exp))
      new_sl.pop
    end
  end
  @statement_list = new_sl
end
index(st) click to toggle source

Returns index of statement. @param st statement. @return [Fixnum] index of statement.

# File lib/minjs/ecma262/base.rb, line 282
def index(st)
  @statement_list.index(st)
end
length() click to toggle source

Returns number of the statements

# File lib/minjs/ecma262/base.rb, line 236
def length
  @statement_list.length
end
remove(st) click to toggle source

Removes statement from statement list @param st statement

# File lib/minjs/ecma262/base.rb, line 203
def remove(st)
  @statement_list.delete(st)
end
remove_empty_statement() click to toggle source

Removes empty statement in this statement list

# File lib/minjs/ecma262/base.rb, line 208
def remove_empty_statement
  @statement_list.reject!{|x|
    x.class == StEmpty
  }
end
replace(from, to) click to toggle source

Replaces children object @see Base#replace

# File lib/minjs/ecma262/base.rb, line 194
def replace(from, to)
  idx = @statement_list.index(from)
  if idx
    @statement_list[idx] = to
  end
end
to_exp(options = {}) click to toggle source

Converts statement list to expression and returns it.

# File lib/minjs/ecma262/base.rb, line 249
def to_exp(options = {})
  return nil if to_exp? == false
  t = @statement_list[0].to_exp(options)
  return t.to_exp(options) if @statement_list.length <= 1
  i = 1
  while(i < @statement_list.length)
    t = ExpComma.new(t, @statement_list[i])
    i += 1
  end
  t
end
to_exp?() click to toggle source

return true if this can convert to expression.

# File lib/minjs/ecma262/base.rb, line 241
def to_exp?
  @statement_list.each do |s|
    return false if s.to_exp? == false
  end
  return true
end
to_js(options = {}) click to toggle source

Returns a ECMAScript string containg the representation of element. @see Base#to_js

# File lib/minjs/ecma262/base.rb, line 231
def to_js(options = {})
  concat options, @statement_list
end
traverse(parent) { |parent, self| ... } click to toggle source

Traverses this children and itself with given block. @see Base#traverse

# File lib/minjs/ecma262/base.rb, line 216
def traverse(parent, &block)
  _self = self
  @statement_list.each do|st|
    st.traverse(self, &block)
  end
  yield parent, self
end