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



410
411
412
# File 'lib/syntax_tree/yarv/instructions.rb', line 410

def initialize(type)
  @type = type
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



408
409
410
# File 'lib/syntax_tree/yarv/instructions.rb', line 408

def type
  @type
end

Instance Method Details

#call(vm) ⇒ Object



438
439
440
441
442
443
444
445
446
447
448
# File 'lib/syntax_tree/yarv/instructions.rb', line 438

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



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

def canonical
  self
end

#disasm(fmt) ⇒ Object



414
415
416
# File 'lib/syntax_tree/yarv/instructions.rb', line 414

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

#lengthObject



422
423
424
# File 'lib/syntax_tree/yarv/instructions.rb', line 422

def length
  2
end

#popsObject



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

def pops
  2
end

#pushesObject



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

def pushes
  1
end

#to_a(_iseq) ⇒ Object



418
419
420
# File 'lib/syntax_tree/yarv/instructions.rb', line 418

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