Class: SyntaxTree::YARV::CheckMatch

Inherits:
Instruction 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

Methods inherited from Instruction

#branch_targets, #canonical, #falls_through?, #leaves?, #side_effects?

Constructor Details

#initialize(type) ⇒ CheckMatch

Returns a new instance of CheckMatch.



426
427
428
# File 'lib/syntax_tree/yarv/instructions.rb', line 426

def initialize(type)
  @type = type
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



424
425
426
# File 'lib/syntax_tree/yarv/instructions.rb', line 424

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



442
443
444
# File 'lib/syntax_tree/yarv/instructions.rb', line 442

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

#call(vm) ⇒ Object



458
459
460
461
462
463
464
465
466
467
468
# File 'lib/syntax_tree/yarv/instructions.rb', line 458

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

#deconstruct_keys(_keys) ⇒ Object



438
439
440
# File 'lib/syntax_tree/yarv/instructions.rb', line 438

def deconstruct_keys(_keys)
  { type: type }
end

#disasm(fmt) ⇒ Object



430
431
432
# File 'lib/syntax_tree/yarv/instructions.rb', line 430

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

#lengthObject



446
447
448
# File 'lib/syntax_tree/yarv/instructions.rb', line 446

def length
  2
end

#popsObject



450
451
452
# File 'lib/syntax_tree/yarv/instructions.rb', line 450

def pops
  2
end

#pushesObject



454
455
456
# File 'lib/syntax_tree/yarv/instructions.rb', line 454

def pushes
  1
end

#to_a(_iseq) ⇒ Object



434
435
436
# File 'lib/syntax_tree/yarv/instructions.rb', line 434

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