Class: Minjs::ECMA262::StIf

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

Overview

Base class of ECMA262 IfStatement element.

See Also:

Instance Attribute Summary collapse

Attributes inherited from Base

#parent

Instance Method Summary collapse

Methods inherited from Statement

#empty?, #priority

Methods inherited from Base

#add_remove_paren, #concat

Constructor Details

#initialize(cond, then_st, else_st) ⇒ StIf

Returns a new instance of StIf.



354
355
356
357
358
# File 'lib/minjs/ecma262/statement.rb', line 354

def initialize(cond, then_st, else_st)
  @cond = cond
  @then_st = then_st
  @else_st = else_st
end

Instance Attribute Details

#condObject (readonly)

Returns the value of attribute cond.



352
353
354
# File 'lib/minjs/ecma262/statement.rb', line 352

def cond
  @cond
end

#else_stObject (readonly)

Returns the value of attribute else_st.



352
353
354
# File 'lib/minjs/ecma262/statement.rb', line 352

def else_st
  @else_st
end

#then_stObject (readonly)

Returns the value of attribute then_st.



352
353
354
# File 'lib/minjs/ecma262/statement.rb', line 352

def then_st
  @then_st
end

Instance Method Details

#==(obj) ⇒ Object

compare object



389
390
391
392
393
394
# File 'lib/minjs/ecma262/statement.rb', line 389

def ==(obj)
  self.class == obj.class and
    @cond == obj.cond and
    @then_st == obj.then_st and
    @else_st == obj.else_st
end

#add_parenObject

add parenthesis if need



486
487
488
# File 'lib/minjs/ecma262/statement.rb', line 486

def add_paren
  self
end

#deep_dupObject

duplicate object

See Also:



384
385
386
# File 'lib/minjs/ecma262/statement.rb', line 384

def deep_dup
  self.class.new(@cond.deep_dup, @then_st.deep_dup, @else_st ? @else_st.deep_dup : nil)
end

#remove_empty_statementObject

Removes empty statement in this then-clause or else-clause



491
492
493
494
495
496
497
498
# File 'lib/minjs/ecma262/statement.rb', line 491

def remove_empty_statement
  if @then_st.kind_of? StBlock
    @then_st.remove_empty_statement
  end
  if @else_st.kind_of? StBlock
    @else_st.remove_empty_statement
  end
end

#remove_parenObject

remove parenthesis if possible



478
479
480
481
482
483
# File 'lib/minjs/ecma262/statement.rb', line 478

def remove_paren
  if @cond.kind_of? ExpParen
    @cond = @cond.val
  end
  self
end

#replace(from, to) ⇒ Object

Replaces children object.

See Also:



362
363
364
365
366
367
368
369
370
# File 'lib/minjs/ecma262/statement.rb', line 362

def replace(from, to)
  if from .eql? @cond
    @cond = to
  elsif from .eql? @then_st
    @then_st = to
  elsif from .eql? @else_st
    @else_st = to
  end
end

#to_exp(options = {}) ⇒ Object

Converts statement to expression and returns it.



448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/minjs/ecma262/statement.rb', line 448

def to_exp(options = {})
  cond = @cond.deep_dup
  if !@else_st
    then_exp = @then_st.to_exp(options)
    if(options[:cond])
      if cond.kind_of? ExpLogicalNot
        add_remove_paren ExpCond.new(cond.val, ECMA262Numeric.new(0), then_exp)
      else
        add_remove_paren ExpCond.new(cond, then_exp, ECMA262Numeric.new(0))
      end
    else
      if cond.kind_of? ExpLogicalNot
        add_remove_paren ExpLogicalOr.new(cond.val, then_exp)
      else
        add_remove_paren ExpLogicalAnd.new(cond, then_exp)
      end
    end
  else
    then_exp = @then_st.to_exp(options)
    else_exp = @else_st.to_exp(options)

    if cond.kind_of? ExpLogicalNot
      add_remove_paren ExpCond.new(cond.val, else_exp, then_exp)
    else
      add_remove_paren ExpCond.new(cond, then_exp, else_exp)
    end
  end
end

#to_exp?Boolean

true if statement can convert to expression

Returns:



437
438
439
440
441
442
443
444
445
# File 'lib/minjs/ecma262/statement.rb', line 437

def to_exp?
  if !@else_st
    return false if @then_st.to_exp? == false
  else
    return false if @then_st.to_exp? == false
    return false if @else_st.to_exp? == false
  end
  return true
end

#to_js(options = {}) ⇒ Object

Returns a ECMAScript string containg the representation of element.

See Also:



398
399
400
401
402
403
404
# File 'lib/minjs/ecma262/statement.rb', line 398

def to_js(options = {})
  if @else_st
    concat options, :if, "(", @cond, ")", @then_st, :else, @else_st
  else
    concat options, :if, "(", @cond, ")", @then_st
  end
end

#to_returnObject

Converts block to ‘return statement’ and returns it



416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/minjs/ecma262/statement.rb', line 416

def to_return
  then_exp = then_st.exp;
  if @else_st
    else_exp = else_st.exp;
  end

  if then_exp.nil?
    then_exp = ExpVoid.new(ECMA262Numeric.new(0))
  end
  if @else_st and else_exp.nil?
    else_exp = ExpVoid.new(ECMA262Numeric.new(0))
  end
  if @else_st
    ret = add_remove_paren StReturn.new(ExpCond.new(@cond, then_exp, else_exp))
  else
    ret = add_remove_paren StReturn.new(ExpLogicalAnd.new(@cond, then_exp))
  end
  ret
end

#to_return?Boolean

return true if statement can convert to return statement.

Returns:



407
408
409
410
411
412
413
# File 'lib/minjs/ecma262/statement.rb', line 407

def to_return?
  if !@else_st
    return false
  else
    return true if @then_st.class == StReturn and @else_st.class == StReturn
  end
end

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

Traverses this children and itself with given block.

Yields:

Yield Parameters:



373
374
375
376
377
378
379
380
# File 'lib/minjs/ecma262/statement.rb', line 373

def traverse(parent, &block)
  @cond.traverse(self, &block)
  @then_st.traverse(self, &block)
  if @else_st
    @else_st.traverse(self, &block)
  end
  yield parent, self
end