Class: Minjs::ECMA262::StSwitch

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

Overview

Base class of ECMA262 SwitchStatement 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(exp, blocks) ⇒ StSwitch

block: [condition, blocks]



1223
1224
1225
1226
# File 'lib/minjs/ecma262/statement.rb', line 1223

def initialize(exp, blocks)
  @exp = exp
  @blocks = blocks
end

Instance Attribute Details

#blocksObject (readonly)

Returns the value of attribute blocks.



1218
1219
1220
# File 'lib/minjs/ecma262/statement.rb', line 1218

def blocks
  @blocks
end

#expObject (readonly)

Returns the value of attribute exp.



1218
1219
1220
# File 'lib/minjs/ecma262/statement.rb', line 1218

def exp
  @exp
end

Instance Method Details

#==(obj) ⇒ Object

compare object



1263
1264
1265
1266
1267
# File 'lib/minjs/ecma262/statement.rb', line 1263

def ==(obj)
  self.class == obj.class and
    @exp == obj.exp and
    @blocks == obj.blocks
end

#add_parenObject

add parenthesis if need



1297
1298
1299
# File 'lib/minjs/ecma262/statement.rb', line 1297

def add_paren
  self
end

#deep_dupObject

duplicate object

See Also:



1230
1231
1232
1233
1234
1235
1236
1237
1238
# File 'lib/minjs/ecma262/statement.rb', line 1230

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

#remove_parenObject

remove parenthesis if possible



1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
# File 'lib/minjs/ecma262/statement.rb', line 1284

def remove_paren
  if @exp.kind_of? ExpParen
    @exp = @exp.val
  end
  @blocks.each do |b|
    if b[0] and b[0].kind_of? ExpParen
      b[0] = b[0].val
    end
  end
  self
end

#replace(from, to) ⇒ Object

Replaces children object.

See Also:



1242
1243
1244
1245
1246
1247
1248
# File 'lib/minjs/ecma262/statement.rb', line 1242

def replace(from, to)
  if @exp .eql? from
    @exp = to
  elsif @blocks .eql? from
    @blocks = to
  end
end

#to_js(options = {}) ⇒ Object

Returns a ECMAScript string containg the representation of element.

See Also:



1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
# File 'lib/minjs/ecma262/statement.rb', line 1271

def to_js(options = {})
  t = concat(options, :switch, "(", @exp, ")", "{")
  @blocks.each do |b|
    if b[0]
      t = concat(options, t, :case, b[0], ":", b[1])
    else
      t = concat(options, t, :default, ":", b[1])
    end
  end
  t = concat(options, t, "}")
end

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

Traverses this children and itself with given block.

Yields:

Yield Parameters:



1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
# File 'lib/minjs/ecma262/statement.rb', line 1251

def traverse(parent, &blocks)
  @exp.traverse(self, &blocks)
  @blocks.each do |b|
    if b[0]
      b[0].traverse(self, &blocks)
    end
    b[1].traverse(self, &blocks)
  end
  yield parent, self
end