Class: Minjs::ECMA262::StVar

Inherits:
Statement show all
Defined in:
lib/minjs/ecma262/statement.rb

Overview

Base class of ECMA262 VariableStatement element.

See Also:

Instance Attribute Summary collapse

Attributes inherited from Base

#parent

Instance Method Summary collapse

Methods inherited from Statement

#empty?, #priority, #to_exp?, #to_return?

Methods inherited from Base

#add_remove_paren, #concat

Constructor Details

#initialize(var_env, vars) ⇒ StVar

vars:

[[name0,init0],[name1,init1],...]


152
153
154
155
# File 'lib/minjs/ecma262/statement.rb', line 152

def initialize(var_env, vars)
  @vars = vars
  @var_env = var_env
end

Instance Attribute Details

#var_envObject (readonly)

Returns the value of attribute var_env.



147
148
149
# File 'lib/minjs/ecma262/statement.rb', line 147

def var_env
  @var_env
end

#varsObject (readonly)

Returns the value of attribute vars.



146
147
148
# File 'lib/minjs/ecma262/statement.rb', line 146

def vars
  @vars
end

Instance Method Details

#==(obj) ⇒ Object

compare object



192
193
194
# File 'lib/minjs/ecma262/statement.rb', line 192

def ==(obj)
  self.class == obj.class and @vars == obj.vars
end

#add_parenObject

add parenthesis if need



238
239
240
241
242
243
244
245
# File 'lib/minjs/ecma262/statement.rb', line 238

def add_paren
  @vars.each do |x|
    if x[1] and x[1].priority > PRIORITY_ASSIGNMENT
      x[1] = ExpParen.new(x[1])
    end
  end
  self
end

#deep_dupObject

duplicate object

See Also:



159
160
161
162
163
164
# File 'lib/minjs/ecma262/statement.rb', line 159

def deep_dup
  self.class.new(@var_env,
                 @vars.collect{|x,y|
                   [x.deep_dup, y ? y.deep_dup : nil]
                 })
end

#normalizationObject

If variable has no initializer, this method moves it to latter



214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/minjs/ecma262/statement.rb', line 214

def normalization
  v1 = []
  v2 = []
  @vars.each do |x|
    if x[1].nil?
      v2.push(x)
    else
      v1.push(x)
    end
  end
  @vars = v1.concat(v2)
end

#remove_parenObject

remove parenthesis if possible



228
229
230
231
232
233
234
235
# File 'lib/minjs/ecma262/statement.rb', line 228

def remove_paren
  @vars.each do |x|
    if x[1] and x[1].kind_of? ExpParen and x[1].val.priority <= PRIORITY_ASSIGNMENT
      x[1] = x[1].val
    end
  end
  self
end

#replace(from, to) ⇒ Object

Replaces children object.

See Also:



168
169
170
171
172
173
174
175
176
177
178
# File 'lib/minjs/ecma262/statement.rb', line 168

def replace(from, to)
  @vars.each do |x|
    if x[0] .eql? from
      x[0] = to
      break
    elsif x[1] and x[1] .eql? from
      x[1] = to
      break
    end
  end
end

#to_js(options = {}) ⇒ Object

Returns a ECMAScript string containg the representation of element.

See Also:



198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/minjs/ecma262/statement.rb', line 198

def to_js(options = {})
    t = concat(options, :var, @vars.collect{|x|
                 if x[1]
                   concat options, x[0], '=', x[1]
                 else
                   concat options, x[0]
                 end
               }.join(","))
  if t.length > 0
    concat(options, t, ";")
  else
    ""
  end
end

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

Traverses this children and itself with given block.

Yields:

Yield Parameters:



181
182
183
184
185
186
187
188
189
# File 'lib/minjs/ecma262/statement.rb', line 181

def traverse(parent, &block)
  @vars.each do |x|
    x[0].traverse(self, &block)
    if x[1]
      x[1].traverse(self, &block)
    end
  end
  yield parent, self
end