class Minjs::ECMA262::StIf

Base class of ECMA262 IfStatement element.

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

Attributes

cond[R]
else_st[R]
then_st[R]

Public Class Methods

new(cond, then_st, else_st) click to toggle source
# 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

Public Instance Methods

==(obj) click to toggle source

compare object

# 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_paren() click to toggle source

add parenthesis if need

# File lib/minjs/ecma262/statement.rb, line 486
def add_paren
  self
end
deep_dup() click to toggle source

duplicate object @see Base#deep_dup

# 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_statement() click to toggle source

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

# 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_paren() click to toggle source

remove parenthesis if possible

# 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) click to toggle source

Replaces children object. @see Base#replace

# 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 = {}) click to toggle source

Converts statement to expression and returns it.

# 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?() click to toggle source

true if statement can convert to expression

# 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 = {}) click to toggle source

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

# 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_return() click to toggle source

Converts block to ‘return statement’ and returns it

# 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?() click to toggle source

return true if statement can convert to return statement.

# 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| ... } click to toggle source

Traverses this children and itself with given block.

# 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