Class: Minjs::ECMA262::StatementList
- Defined in:
- lib/minjs/ecma262/base.rb
Overview
Class of ECMA262 Statement List
Direct Known Subclasses
Instance Attribute Summary collapse
-
#statement_list ⇒ Object
readonly
Returns the value of attribute statement_list.
Attributes inherited from Base
Instance Method Summary collapse
-
#==(obj) ⇒ Object
compare object.
-
#[](i) ⇒ Statement
Returns the statement at index.
-
#[]=(i, st) ⇒ Object
Sets the statement at index.
-
#deep_dup ⇒ Object
duplicate object.
- #each(&block) ⇒ Object
-
#grouping ⇒ Object
Groups statements and reduce number of them as few as posibble.
-
#index(st) ⇒ Fixnum
Returns index of statement.
-
#initialize(statement_list) ⇒ StatementList
constructor
A new instance of StatementList.
-
#length ⇒ Object
Returns number of the statements.
-
#remove(st) ⇒ Object
Removes statement from statement list.
-
#remove_empty_statement ⇒ Object
Removes empty statement in this statement list.
-
#replace(from, to) ⇒ Object
Replaces children object.
-
#to_exp(options = {}) ⇒ Object
Converts statement list to expression and returns it.
-
#to_exp? ⇒ Boolean
return true if this can convert to expression.
-
#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 Base
Constructor Details
#initialize(statement_list) ⇒ StatementList
Returns a new instance of StatementList.
118 119 120 |
# File 'lib/minjs/ecma262/base.rb', line 118 def initialize(statement_list) @statement_list = statement_list #array end |
Instance Attribute Details
#statement_list ⇒ Object (readonly)
Returns the value of attribute statement_list.
116 117 118 |
# File 'lib/minjs/ecma262/base.rb', line 116 def statement_list @statement_list end |
Instance Method Details
#==(obj) ⇒ Object
compare object
225 226 227 |
# File 'lib/minjs/ecma262/base.rb', line 225 def ==(obj) @statement_list == obj.statement_list end |
#[](i) ⇒ Statement
Returns the statement at index
268 269 270 |
# File 'lib/minjs/ecma262/base.rb', line 268 def [](i) @statement_list[i] end |
#[]=(i, st) ⇒ Object
Sets the statement at index.
275 276 277 |
# File 'lib/minjs/ecma262/base.rb', line 275 def []=(i, st) @statement_list[i] = st end |
#deep_dup ⇒ Object
duplicate object
188 189 190 |
# File 'lib/minjs/ecma262/base.rb', line 188 def deep_dup self.class.new(@statement_list.collect{|s| s.deep_dup}) end |
#each(&block) ⇒ Object
261 262 263 |
# File 'lib/minjs/ecma262/base.rb', line 261 def each(&block) @statement_list.each(&block) end |
#grouping ⇒ Object
Groups statements and reduce number of them as few as posibble.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/minjs/ecma262/base.rb', line 123 def grouping remove_empty_statement new_sl = [] sl = [] g = [] @statement_list.each do |st| if st.to_exp? g.push(st) else if g.length > 0 sl.push(g) end sl.push([st]) g = [] end end if g.length > 0 sl.push(g) end sl.each do |g| if g.length == 1 new_sl.push(g[0]) else i = 1 t = ExpParen.new(g[0].to_exp) while i < g.length t = ExpComma.new(t, ExpParen.new(g[i].to_exp)) i += 1 end new_sl.push(StExp.new(t)) end end if idx = new_sl.index{|x| x.class == StReturn} idx += 1 while idx < new_sl.length if new_sl[idx].kind_of? StVar ; elsif new_sl[idx].kind_of? StFunc ; else new_sl[idx] = StEmpty.new end idx += 1 end end if self.kind_of? SourceElements if new_sl[-1].kind_of? StReturn and new_sl[-1].exp.nil? new_sl.pop end end if new_sl[-1].kind_of? StReturn and new_sl[-2].kind_of? StExp if new_sl[-1].exp new_sl[-2] = StReturn.new(ExpComma.new(new_sl[-2].exp, new_sl[-1].exp)) new_sl.pop end end @statement_list = new_sl end |
#index(st) ⇒ Fixnum
Returns index of statement.
282 283 284 |
# File 'lib/minjs/ecma262/base.rb', line 282 def index(st) @statement_list.index(st) end |
#length ⇒ Object
Returns number of the statements
236 237 238 |
# File 'lib/minjs/ecma262/base.rb', line 236 def length @statement_list.length end |
#remove(st) ⇒ Object
Removes statement from statement list
203 204 205 |
# File 'lib/minjs/ecma262/base.rb', line 203 def remove(st) @statement_list.delete(st) end |
#remove_empty_statement ⇒ Object
Removes empty statement in this statement list
208 209 210 211 212 |
# File 'lib/minjs/ecma262/base.rb', line 208 def remove_empty_statement @statement_list.reject!{|x| x.class == StEmpty } end |
#replace(from, to) ⇒ Object
Replaces children object
194 195 196 197 198 199 |
# File 'lib/minjs/ecma262/base.rb', line 194 def replace(from, to) idx = @statement_list.index(from) if idx @statement_list[idx] = to end end |
#to_exp(options = {}) ⇒ Object
Converts statement list to expression and returns it.
249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/minjs/ecma262/base.rb', line 249 def to_exp( = {}) return nil if to_exp? == false t = @statement_list[0].to_exp() return t.to_exp() if @statement_list.length <= 1 i = 1 while(i < @statement_list.length) t = ExpComma.new(t, @statement_list[i]) i += 1 end t end |
#to_exp? ⇒ Boolean
return true if this can convert to expression.
241 242 243 244 245 246 |
# File 'lib/minjs/ecma262/base.rb', line 241 def to_exp? @statement_list.each do |s| return false if s.to_exp? == false end return true end |
#to_js(options = {}) ⇒ Object
Returns a ECMAScript string containg the representation of element.
231 232 233 |
# File 'lib/minjs/ecma262/base.rb', line 231 def to_js( = {}) concat , @statement_list end |
#traverse(parent) {|parent, _self| ... } ⇒ Object
Traverses this children and itself with given block.
216 217 218 219 220 221 222 |
# File 'lib/minjs/ecma262/base.rb', line 216 def traverse(parent, &block) _self = self @statement_list.each do|st| st.traverse(self, &block) end yield parent, self end |