Class: SyntaxTree::YARV::CheckType
- Inherits:
-
Object
- Object
- SyntaxTree::YARV::CheckType
- Defined in:
- lib/syntax_tree/yarv/instructions.rb
Overview
### Summary
checktype checks if the value on top of the stack is of a certain type. The type is the only argument. It pops the value off the stack and pushes a boolean onto the stack indicating whether or not the value is of the given type.
### Usage
~~~ruby foo in [bar] ~~~
Constant Summary collapse
- TYPE_OBJECT =
0x01- TYPE_CLASS =
0x02- TYPE_MODULE =
0x03- TYPE_FLOAT =
0x04- TYPE_STRING =
0x05- TYPE_REGEXP =
0x06- TYPE_ARRAY =
0x07- TYPE_HASH =
0x08- TYPE_STRUCT =
0x09- TYPE_BIGNUM =
0x0a- TYPE_FILE =
0x0b- TYPE_DATA =
0x0c- TYPE_MATCH =
0x0d- TYPE_COMPLEX =
0x0e- TYPE_RATIONAL =
0x0f- TYPE_NIL =
0x11- TYPE_TRUE =
0x12- TYPE_FALSE =
0x13- TYPE_SYMBOL =
0x14- TYPE_FIXNUM =
0x15- TYPE_UNDEF =
0x16
Instance Attribute Summary collapse
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #call(vm) ⇒ Object
- #canonical ⇒ Object
- #disasm(fmt) ⇒ Object
-
#initialize(type) ⇒ CheckType
constructor
A new instance of CheckType.
- #length ⇒ Object
- #pops ⇒ Object
- #pushes ⇒ Object
- #to_a(_iseq) ⇒ Object
Constructor Details
#initialize(type) ⇒ CheckType
506 507 508 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 506 def initialize(type) @type = type end |
Instance Attribute Details
#type ⇒ Object (readonly)
Returns the value of attribute type.
504 505 506 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 504 def type @type end |
Instance Method Details
#call(vm) ⇒ Object
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 584 def call(vm) object = vm.pop result = case type when TYPE_OBJECT raise NotImplementedError, "checktype TYPE_OBJECT" when TYPE_CLASS object.is_a?(Class) when TYPE_MODULE object.is_a?(Module) when TYPE_FLOAT object.is_a?(Float) when TYPE_STRING object.is_a?(String) when TYPE_REGEXP object.is_a?(Regexp) when TYPE_ARRAY object.is_a?(Array) when TYPE_HASH object.is_a?(Hash) when TYPE_STRUCT object.is_a?(Struct) when TYPE_BIGNUM raise NotImplementedError, "checktype TYPE_BIGNUM" when TYPE_FILE object.is_a?(File) when TYPE_DATA raise NotImplementedError, "checktype TYPE_DATA" when TYPE_MATCH raise NotImplementedError, "checktype TYPE_MATCH" when TYPE_COMPLEX object.is_a?(Complex) when TYPE_RATIONAL object.is_a?(Rational) when TYPE_NIL object.nil? when TYPE_TRUE object == true when TYPE_FALSE object == false when TYPE_SYMBOL object.is_a?(Symbol) when TYPE_FIXNUM object.is_a?(Integer) when TYPE_UNDEF raise NotImplementedError, "checktype TYPE_UNDEF" end vm.push(result) end |
#canonical ⇒ Object
580 581 582 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 580 def canonical self end |
#disasm(fmt) ⇒ Object
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 510 def disasm(fmt) name = case type when TYPE_OBJECT "T_OBJECT" when TYPE_CLASS "T_CLASS" when TYPE_MODULE "T_MODULE" when TYPE_FLOAT "T_FLOAT" when TYPE_STRING "T_STRING" when TYPE_REGEXP "T_REGEXP" when TYPE_ARRAY "T_ARRAY" when TYPE_HASH "T_HASH" when TYPE_STRUCT "T_STRUCT" when TYPE_BIGNUM "T_BIGNUM" when TYPE_FILE "T_FILE" when TYPE_DATA "T_DATA" when TYPE_MATCH "T_MATCH" when TYPE_COMPLEX "T_COMPLEX" when TYPE_RATIONAL "T_RATIONAL" when TYPE_NIL "T_NIL" when TYPE_TRUE "T_TRUE" when TYPE_FALSE "T_FALSE" when TYPE_SYMBOL "T_SYMBOL" when TYPE_FIXNUM "T_FIXNUM" when TYPE_UNDEF "T_UNDEF" end fmt.instruction("checktype", [name]) end |
#length ⇒ Object
564 565 566 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 564 def length 2 end |
#pops ⇒ Object
568 569 570 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 568 def pops 1 end |
#pushes ⇒ Object
572 573 574 575 576 577 578 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 572 def pushes # TODO: This is incorrect. The instruction only pushes a single value # onto the stack. However, if this is set to 1, we no longer match the # output of RubyVM::InstructionSequence. So leaving this here until we # can investigate further. 2 end |
#to_a(_iseq) ⇒ Object
560 561 562 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 560 def to_a(_iseq) [:checktype, type] end |