Class: Minjs::ECMA262::StatementList

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

Overview

Class of ECMA262 Statement List

See Also:

Direct Known Subclasses

SourceElements

Instance Attribute Summary collapse

Attributes inherited from Base

#parent

Instance Method Summary collapse

Methods inherited from Base

#add_remove_paren, #concat

Constructor Details

#initialize(statement_list) ⇒ StatementList

Returns a new instance of StatementList.



118
119
120
# File 'lib/minjs/ecma262/base.rb', line 118

def initialize(statement_list)
  @statement_list = statement_list #array
end

Instance Attribute Details

#statement_listObject (readonly)

Returns the value of attribute statement_list.



116
117
118
# File 'lib/minjs/ecma262/base.rb', line 116

def statement_list
  @statement_list
end

Instance Method Details

#==(obj) ⇒ Object

compare object



225
226
227
# File 'lib/minjs/ecma262/base.rb', line 225

def ==(obj)
  @statement_list == obj.statement_list
end

#[](i) ⇒ Statement

Returns the statement at index

Parameters:

  • i

    index

Returns:



268
269
270
# File 'lib/minjs/ecma262/base.rb', line 268

def [](i)
  @statement_list[i]
end

#[]=(i, st) ⇒ Object

Sets the statement at index.

Parameters:

  • i

    index

  • st

    statement



275
276
277
# File 'lib/minjs/ecma262/base.rb', line 275

def []=(i, st)
  @statement_list[i] = st
end

#deep_dupObject

duplicate object

See Also:



188
189
190
# File 'lib/minjs/ecma262/base.rb', line 188

def deep_dup
  self.class.new(@statement_list.collect{|s| s.deep_dup})
end

#each(&block) ⇒ Object



261
262
263
# File 'lib/minjs/ecma262/base.rb', line 261

def each(&block)
  @statement_list.each(&block)
end

#groupingObject

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



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# 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) ⇒ Fixnum

Returns index of statement.

Parameters:

  • st

    statement.

Returns:

  • (Fixnum)

    index of statement.



282
283
284
# File 'lib/minjs/ecma262/base.rb', line 282

def index(st)
  @statement_list.index(st)
end

#lengthObject

Returns number of the statements



236
237
238
# File 'lib/minjs/ecma262/base.rb', line 236

def length
  @statement_list.length
end

#remove(st) ⇒ Object

Removes statement from statement list

Parameters:

  • st

    statement



203
204
205
# File 'lib/minjs/ecma262/base.rb', line 203

def remove(st)
  @statement_list.delete(st)
end

#remove_empty_statementObject

Removes empty statement in this statement list



208
209
210
211
212
# File 'lib/minjs/ecma262/base.rb', line 208

def remove_empty_statement
  @statement_list.reject!{|x|
    x.class == StEmpty
  }
end

#replace(from, to) ⇒ Object

Replaces children object

See Also:



194
195
196
197
198
199
# 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 = {}) ⇒ Object

Converts statement list to expression and returns it.



249
250
251
252
253
254
255
256
257
258
259
# 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?Boolean

return true if this can convert to expression.

Returns:



241
242
243
244
245
246
# 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 = {}) ⇒ Object

Returns a ECMAScript string containg the representation of element.

See Also:



231
232
233
# File 'lib/minjs/ecma262/base.rb', line 231

def to_js(options = {})
  concat options, @statement_list
end

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

Traverses this children and itself with given block.

Yields:

Yield Parameters:

See Also:



216
217
218
219
220
221
222
# 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