Class: Minjs::ECMA262::StForInVar

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

Overview

Base class of ECMA262 ‘for(var in)’ 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, exp2, statement) ⇒ StForInVar

Returns a new instance of StForInVar.



918
919
920
921
922
923
# File 'lib/minjs/ecma262/statement.rb', line 918

def initialize(var_env, var_decl, exp2, statement)
  @var_env = var_env
  @var_decl = var_decl
  @exp2 = exp2
  @statement = statement
end

Instance Attribute Details

#exp2Object (readonly)

Returns the value of attribute exp2.



916
917
918
# File 'lib/minjs/ecma262/statement.rb', line 916

def exp2
  @exp2
end

#statementObject (readonly)

Returns the value of attribute statement.



916
917
918
# File 'lib/minjs/ecma262/statement.rb', line 916

def statement
  @statement
end

#var_declObject (readonly)

Returns the value of attribute var_decl.



916
917
918
# File 'lib/minjs/ecma262/statement.rb', line 916

def var_decl
  @var_decl
end

#var_envObject (readonly)

Returns the value of attribute var_env.



915
916
917
# File 'lib/minjs/ecma262/statement.rb', line 915

def var_env
  @var_env
end

Instance Method Details

#==(obj) ⇒ Object

compare object



962
963
964
965
966
967
# File 'lib/minjs/ecma262/statement.rb', line 962

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

#add_parenObject

add parenthesis if need



999
1000
1001
# File 'lib/minjs/ecma262/statement.rb', line 999

def add_paren
  self
end

#deep_dupObject

duplicate object

See Also:



927
928
929
930
931
932
# File 'lib/minjs/ecma262/statement.rb', line 927

def deep_dup
  self.class.new(@var_env,
                 [@var_decl[0].deep_dup, @var_decl[1] ? @var_decl[1].deep_dup : nil],
                 @exp2.deep_dup,
                 @statement.deep_dup)
end

#remove_parenObject

remove parenthesis if possible



988
989
990
991
992
993
994
995
996
# File 'lib/minjs/ecma262/statement.rb', line 988

def remove_paren
  if @var_decl[1] and @var_decl[1].kind_of? ExpParen
    @var_decl[1] = @var_decl[1].val
  end
  if @exp2.kind_of? ExpParen
    @exp2 = @exp2.val
  end
  self
end

#replace(from, to) ⇒ Object

Replaces children object.

See Also:



945
946
947
948
949
# File 'lib/minjs/ecma262/statement.rb', line 945

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:



971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
# File 'lib/minjs/ecma262/statement.rb', line 971

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

  if @var_decl[1] #with initialiser
    _var_decl = concat(options, @var_decl[0], '=', @var_decl[1])
  else
    _var_decl = concat(options, @var_decl[0])
  end

  concat options, :for, "(", :var, _var_decl, :in, @exp2, ")", statement
end

#to_st_for_inObject

converts for-in-var-statement to for-var-statement



952
953
954
955
956
957
958
959
# File 'lib/minjs/ecma262/statement.rb', line 952

def to_st_for_in
  if @var_decl[1]
    t = ExpAssign.new(@var_decl[0], @var_decl[1])
  else
    t = @var_decl[0]
  end
  StForIn.new(t, @exp2, @statement)
end

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

Traverses this children and itself with given block.

Yields:

Yield Parameters:



935
936
937
938
939
940
941
# File 'lib/minjs/ecma262/statement.rb', line 935

def traverse(parent, &block)
  @var_decl[0].traverse(self, &block)
  @var_decl[1].traverse(self, &block) if @var_decl[1]
  @exp2.traverse(self, &block)
  @statement.traverse(self, &block)
  yield parent, self
end