Class: Minjs::ECMA262::StIf
- Defined in:
- lib/minjs/ecma262/statement.rb
Overview
Base class of ECMA262 IfStatement element.
Instance Attribute Summary collapse
-
#cond ⇒ Object
readonly
Returns the value of attribute cond.
-
#else_st ⇒ Object
readonly
Returns the value of attribute else_st.
-
#then_st ⇒ Object
readonly
Returns the value of attribute then_st.
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(cond, then_st, else_st) ⇒ StIf
constructor
A new instance of StIf.
-
#remove_empty_statement ⇒ Object
Removes empty statement in this then-clause or else-clause.
-
#remove_paren ⇒ Object
remove parenthesis if possible.
-
#replace(from, to) ⇒ Object
Replaces children object.
-
#to_exp(options = {}) ⇒ Object
Converts statement to expression and returns it.
-
#to_exp? ⇒ Boolean
true if statement can convert to expression.
-
#to_js(options = {}) ⇒ Object
Returns a ECMAScript string containg the representation of element.
-
#to_return ⇒ Object
Converts block to ‘return statement’ and returns it.
-
#to_return? ⇒ Boolean
return true if statement can convert to return statement.
-
#traverse(parent) {|parent, _self| ... } ⇒ Object
Traverses this children and itself with given block.
Methods inherited from Statement
Methods inherited from Base
Constructor Details
#initialize(cond, then_st, else_st) ⇒ StIf
Returns a new instance of StIf.
354 355 356 357 358 |
# 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 |
Instance Attribute Details
#cond ⇒ Object (readonly)
Returns the value of attribute cond.
352 353 354 |
# File 'lib/minjs/ecma262/statement.rb', line 352 def cond @cond end |
#else_st ⇒ Object (readonly)
Returns the value of attribute else_st.
352 353 354 |
# File 'lib/minjs/ecma262/statement.rb', line 352 def else_st @else_st end |
#then_st ⇒ Object (readonly)
Returns the value of attribute then_st.
352 353 354 |
# File 'lib/minjs/ecma262/statement.rb', line 352 def then_st @then_st end |
Instance Method Details
#==(obj) ⇒ Object
compare object
389 390 391 392 393 394 |
# 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 ⇒ Object
add parenthesis if need
486 487 488 |
# File 'lib/minjs/ecma262/statement.rb', line 486 def add_paren self end |
#deep_dup ⇒ Object
duplicate object
384 385 386 |
# 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 ⇒ Object
Removes empty statement in this then-clause or else-clause
491 492 493 494 495 496 497 498 |
# 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 ⇒ Object
remove parenthesis if possible
478 479 480 481 482 483 |
# 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) ⇒ Object
Replaces children object.
362 363 364 365 366 367 368 369 370 |
# 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 = {}) ⇒ Object
Converts statement to expression and returns it.
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 |
# File 'lib/minjs/ecma262/statement.rb', line 448 def to_exp( = {}) cond = @cond.deep_dup if !@else_st then_exp = @then_st.to_exp() if([: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() else_exp = @else_st.to_exp() 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? ⇒ Boolean
true if statement can convert to expression
437 438 439 440 441 442 443 444 445 |
# 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 = {}) ⇒ Object
Returns a ECMAScript string containg the representation of element.
398 399 400 401 402 403 404 |
# File 'lib/minjs/ecma262/statement.rb', line 398 def to_js( = {}) if @else_st concat , :if, "(", @cond, ")", @then_st, :else, @else_st else concat , :if, "(", @cond, ")", @then_st end end |
#to_return ⇒ Object
Converts block to ‘return statement’ and returns it
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 |
# 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? ⇒ Boolean
return true if statement can convert to return statement.
407 408 409 410 411 412 413 |
# 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| ... } ⇒ Object
Traverses this children and itself with given block.
373 374 375 376 377 378 379 380 |
# 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 |