Class: Minjs::ECMA262::StForVar

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

Overview

Base class of ECMA262 ‘for(var;;)’ IterationStatement 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, var_decl_list, exp2, exp3, statement) ⇒ StForVar

var_decl_list

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


726
727
728
729
730
731
732
# File 'lib/minjs/ecma262/statement.rb', line 726

def initialize(var_env, var_decl_list, exp2, exp3, statement)
  @var_env = var_env
  @var_decl_list = var_decl_list
  @exp2 = exp2
  @exp3 = exp3
  @statement = statement
end

Instance Attribute Details

#exp2Object (readonly)

Returns the value of attribute exp2.



720
721
722
# File 'lib/minjs/ecma262/statement.rb', line 720

def exp2
  @exp2
end

#exp3Object (readonly)

Returns the value of attribute exp3.



720
721
722
# File 'lib/minjs/ecma262/statement.rb', line 720

def exp3
  @exp3
end

#statementObject (readonly)

Returns the value of attribute statement.



720
721
722
# File 'lib/minjs/ecma262/statement.rb', line 720

def statement
  @statement
end

#var_decl_listObject (readonly)

Returns the value of attribute var_decl_list.



720
721
722
# File 'lib/minjs/ecma262/statement.rb', line 720

def var_decl_list
  @var_decl_list
end

#var_envObject (readonly)

Returns the value of attribute var_env.



719
720
721
# File 'lib/minjs/ecma262/statement.rb', line 719

def var_env
  @var_env
end

Instance Method Details

#==(obj) ⇒ Object

compare object



787
788
789
790
791
792
793
# File 'lib/minjs/ecma262/statement.rb', line 787

def ==(obj)
  self.class == obj.class and
    @var_decl_list == obj.var_decl_list and
    @exp2 == obj.exp2 and
    @exp3 == obj.exp3 and
    @statement == obj.statement
end

#add_parenObject

add parenthesis if need



832
833
834
# File 'lib/minjs/ecma262/statement.rb', line 832

def add_paren
  self
end

#deep_dupObject

duplicate object

See Also:



736
737
738
739
740
741
742
743
744
# File 'lib/minjs/ecma262/statement.rb', line 736

def deep_dup
  self.class.new(@var_env,
                 @var_decl_list.collect{|x,y|
                   [x.deep_dup, y.deep_dup]
                 },
                 @exp2 && @exp2.deep_dup,
                 @exp3 && @exp3.deep_dup,
                 @statement.deep_dup)
end

#remove_parenObject

remove parenthesis if possible



816
817
818
819
820
821
822
823
824
825
826
827
828
829
# File 'lib/minjs/ecma262/statement.rb', line 816

def remove_paren
  @var_decl_list.each do|x|
    if x[1] and x[1].kind_of? ExpParen
      x[1] = x[1].val
    end
  end
  if @exp2.kind_of? ExpParen
    @exp2 = @exp2.val
  end
  if @exp3.kind_of? ExpParen
    @exp3 = @exp3.val
  end
  self
end

#replace(from, to) ⇒ Object

Replaces children object.

See Also:



748
749
750
751
752
# File 'lib/minjs/ecma262/statement.rb', line 748

def replace(from, to)
  if from .eql? @statement
    @statement = to
  end
end

#to_js(options = {}) ⇒ Object

Returns a ECMAScript string containg the representation of element.

See Also:



797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
# File 'lib/minjs/ecma262/statement.rb', line 797

def to_js(options = {})
  if @statement.kind_of? StBlock and @statement.statement_list.length == 1
    statement = @statement.statement_list.statement_list[0]
  else
    statement = @statement
  end

  _var_decl_list = @var_decl_list.collect{|x|
    if x[1] #with initialiser
      concat options, x[0], '=', x[1]
    else
      concat options, x[0]
    end
  }.join(",")
  t = concat(options, :for, "(var", _var_decl_list, ";;", @exp2, ";;", @exp3, ")")
  concat options, t, statement
end

#to_st_forObject

converts for-var-statement to for-statement



769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
# File 'lib/minjs/ecma262/statement.rb', line 769

def to_st_for
  tt = nil
  @var_decl_list.each{|x|
    if x[1]
      t = ExpAssign.new(x[0], x[1])
    else
      t = x[0]
    end
    if tt.nil?
      tt = t
    else
      tt = ExpComma.new(tt, t)
    end
  }
  StFor.new(tt, @exp2, @exp3, @statement)
end

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

Traverses this children and itself with given block.

Yields:

Yield Parameters:



755
756
757
758
759
760
761
762
763
764
765
766
# File 'lib/minjs/ecma262/statement.rb', line 755

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