Module: MiniRuby::Opcode

Extended by:
T::Sig
Defined in:
lib/miniruby/opcode.rb

Overview

Contains the definitions of all operation codes supported by the Virtual Machine.

Class Method Summary collapse

Class Method Details

.define(**opcodes) ⇒ Object

: (**Integer opcodes) -> void



40
41
42
43
# File 'lib/miniruby/opcode.rb', line 40

def define(**opcodes)
  @name_to_opcode = opcodes.transform_keys(&:to_s)
  @opcode_to_name = @name_to_opcode.invert
end

.from_name(name) ⇒ Object

Returns the opcode related to the given name. Returns -1 when there is no opcode with the given name. : (String name) -> Integer



35
36
37
# File 'lib/miniruby/opcode.rb', line 35

def from_name(name)
  @name_to_opcode.fetch(name, -1)
end

.name(opcode) ⇒ Object

Converts an opcode to its name, returns ‘UNKNOWN’ when there is no opcode with the given index. : (Integer opcode) -> String



28
29
30
# File 'lib/miniruby/opcode.rb', line 28

def name(opcode)
  @opcode_to_name.fetch(opcode, 'UNKNOWN')
end

.next_idObject

: -> Integer



18
19
20
21
22
23
# File 'lib/miniruby/opcode.rb', line 18

def next_id
  i = @next_id
  @next_id += 1

  i
end