Class: Minjs::ECMA262::StVar
- Defined in:
- lib/minjs/ecma262/statement.rb
Overview
Base class of ECMA262 VariableStatement element.
Instance Attribute Summary collapse
-
#var_env ⇒ Object
readonly
Returns the value of attribute var_env.
-
#vars ⇒ Object
readonly
Returns the value of attribute vars.
Attributes inherited from Base
Instance Method Summary collapse
-
#==(obj) ⇒ Object
compare object.
-
#add_paren ⇒ Object
add parenthesis if need.
-
#deep_dup ⇒ Object
duplicate object.
-
#initialize(var_env, vars) ⇒ StVar
constructor
vars: [[name0,init0],,…].
-
#normalization ⇒ Object
If variable has no initializer, this method moves it to latter.
-
#remove_paren ⇒ Object
remove parenthesis if possible.
-
#replace(from, to) ⇒ Object
Replaces children object.
-
#to_js(options = {}) ⇒ Object
Returns a ECMAScript string containg the representation of element.
-
#traverse(parent) {|parent, _self| ... } ⇒ Object
Traverses this children and itself with given block.
Methods inherited from Statement
#empty?, #priority, #to_exp?, #to_return?
Methods inherited from Base
Constructor Details
#initialize(var_env, vars) ⇒ StVar
vars:
[[name0,init0],[name1,init1],...]
152 153 154 155 |
# File 'lib/minjs/ecma262/statement.rb', line 152 def initialize(var_env, vars) @vars = vars @var_env = var_env end |
Instance Attribute Details
#var_env ⇒ Object (readonly)
Returns the value of attribute var_env.
147 148 149 |
# File 'lib/minjs/ecma262/statement.rb', line 147 def var_env @var_env end |
#vars ⇒ Object (readonly)
Returns the value of attribute vars.
146 147 148 |
# File 'lib/minjs/ecma262/statement.rb', line 146 def vars @vars end |
Instance Method Details
#==(obj) ⇒ Object
compare object
192 193 194 |
# File 'lib/minjs/ecma262/statement.rb', line 192 def ==(obj) self.class == obj.class and @vars == obj.vars end |
#add_paren ⇒ Object
add parenthesis if need
238 239 240 241 242 243 244 245 |
# File 'lib/minjs/ecma262/statement.rb', line 238 def add_paren @vars.each do |x| if x[1] and x[1].priority > PRIORITY_ASSIGNMENT x[1] = ExpParen.new(x[1]) end end self end |
#deep_dup ⇒ Object
duplicate object
159 160 161 162 163 164 |
# File 'lib/minjs/ecma262/statement.rb', line 159 def deep_dup self.class.new(@var_env, @vars.collect{|x,y| [x.deep_dup, y ? y.deep_dup : nil] }) end |
#normalization ⇒ Object
If variable has no initializer, this method moves it to latter
214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/minjs/ecma262/statement.rb', line 214 def normalization v1 = [] v2 = [] @vars.each do |x| if x[1].nil? v2.push(x) else v1.push(x) end end @vars = v1.concat(v2) end |
#remove_paren ⇒ Object
remove parenthesis if possible
228 229 230 231 232 233 234 235 |
# File 'lib/minjs/ecma262/statement.rb', line 228 def remove_paren @vars.each do |x| if x[1] and x[1].kind_of? ExpParen and x[1].val.priority <= PRIORITY_ASSIGNMENT x[1] = x[1].val end end self end |
#replace(from, to) ⇒ Object
Replaces children object.
168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/minjs/ecma262/statement.rb', line 168 def replace(from, to) @vars.each do |x| if x[0] .eql? from x[0] = to break elsif x[1] and x[1] .eql? from x[1] = to break end end end |
#to_js(options = {}) ⇒ Object
Returns a ECMAScript string containg the representation of element.
198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/minjs/ecma262/statement.rb', line 198 def to_js( = {}) t = concat(, :var, @vars.collect{|x| if x[1] concat , x[0], '=', x[1] else concat , x[0] end }.join(",")) if t.length > 0 concat(, t, ";") else "" end end |
#traverse(parent) {|parent, _self| ... } ⇒ Object
Traverses this children and itself with given block.
181 182 183 184 185 186 187 188 189 |
# File 'lib/minjs/ecma262/statement.rb', line 181 def traverse(parent, &block) @vars.each do |x| x[0].traverse(self, &block) if x[1] x[1].traverse(self, &block) end end yield parent, self end |