Class: SyntaxTree::YARV::CheckMatch

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree/yarv/instructions.rb

Overview

### Summary

checkmatch checks if the current pattern matches the current value. It pops the target and the pattern off the stack and pushes a boolean onto the stack if it matches or not.

### Usage

~~~ruby foo in Foo ~~~

Constant Summary collapse

VM_CHECKMATCH_TYPE_WHEN =
1
VM_CHECKMATCH_TYPE_CASE =
2
VM_CHECKMATCH_TYPE_RESCUE =
3
VM_CHECKMATCH_TYPE_MASK =
0x03
VM_CHECKMATCH_ARRAY =
0x04

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ CheckMatch

Returns a new instance of CheckMatch.



460
461
462
# File 'lib/syntax_tree/yarv/instructions.rb', line 460

def initialize(type)
  @type = type
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



458
459
460
# File 'lib/syntax_tree/yarv/instructions.rb', line 458

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



476
477
478
# File 'lib/syntax_tree/yarv/instructions.rb', line 476

def ==(other)
  other.is_a?(CheckMatch) && other.type == type
end

#call(vm) ⇒ Object



496
497
498
499
500
501
502
503
504
505
506
# File 'lib/syntax_tree/yarv/instructions.rb', line 496

def call(vm)
  target, pattern = vm.pop(2)

  vm.push(
    if type & VM_CHECKMATCH_ARRAY > 0
      pattern.any? { |item| check?(item, target) }
    else
      check?(pattern, target)
    end
  )
end

#canonicalObject



492
493
494
# File 'lib/syntax_tree/yarv/instructions.rb', line 492

def canonical
  self
end

#deconstruct_keys(_keys) ⇒ Object



472
473
474
# File 'lib/syntax_tree/yarv/instructions.rb', line 472

def deconstruct_keys(_keys)
  { type: type }
end

#disasm(fmt) ⇒ Object



464
465
466
# File 'lib/syntax_tree/yarv/instructions.rb', line 464

def disasm(fmt)
  fmt.instruction("checkmatch", [fmt.object(type)])
end

#lengthObject



480
481
482
# File 'lib/syntax_tree/yarv/instructions.rb', line 480

def length
  2
end

#popsObject



484
485
486
# File 'lib/syntax_tree/yarv/instructions.rb', line 484

def pops
  2
end

#pushesObject



488
489
490
# File 'lib/syntax_tree/yarv/instructions.rb', line 488

def pushes
  1
end

#to_a(_iseq) ⇒ Object



468
469
470
# File 'lib/syntax_tree/yarv/instructions.rb', line 468

def to_a(_iseq)
  [:checkmatch, type]
end