Class: LexerKit::IR::Instruction
- Inherits:
-
Object
- Object
- LexerKit::IR::Instruction
- Defined in:
- lib/lexer_kit/ir/instruction.rb
Overview
Instruction represents a single VM instruction. Each instruction has an opcode and an optional argument.
Instance Attribute Summary collapse
-
#arg ⇒ Object
readonly
Returns the value of attribute arg.
-
#opcode ⇒ Object
readonly
Returns the value of attribute opcode.
Class Method Summary collapse
-
.from_binary(bytes) ⇒ Instruction
Decode from binary.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(opcode, arg = 0) ⇒ Instruction
constructor
A new instance of Instruction.
- #inspect ⇒ Object
-
#to_binary ⇒ String
Encode to binary (4 bytes: 1 opcode + 3 arg).
- #to_s ⇒ Object
Constructor Details
#initialize(opcode, arg = 0) ⇒ Instruction
Returns a new instance of Instruction.
12 13 14 15 |
# File 'lib/lexer_kit/ir/instruction.rb', line 12 def initialize(opcode, arg = 0) @opcode = opcode @arg = arg end |
Instance Attribute Details
#arg ⇒ Object (readonly)
Returns the value of attribute arg.
8 9 10 |
# File 'lib/lexer_kit/ir/instruction.rb', line 8 def arg @arg end |
#opcode ⇒ Object (readonly)
Returns the value of attribute opcode.
8 9 10 |
# File 'lib/lexer_kit/ir/instruction.rb', line 8 def opcode @opcode end |
Class Method Details
.from_binary(bytes) ⇒ Instruction
Decode from binary
31 32 33 34 35 |
# File 'lib/lexer_kit/ir/instruction.rb', line 31 def self.from_binary(bytes) opcode, a1, a2, a3 = bytes.unpack("C4") arg = (a1 << 16) | (a2 << 8) | a3 new(opcode, arg) end |
Instance Method Details
#==(other) ⇒ Object
45 46 47 |
# File 'lib/lexer_kit/ir/instruction.rb', line 45 def ==(other) other.is_a?(Instruction) && @opcode == other.opcode && @arg == other.arg end |
#inspect ⇒ Object
41 42 43 |
# File 'lib/lexer_kit/ir/instruction.rb', line 41 def inspect "#<Instruction #{self}>" end |
#to_binary ⇒ String
Encode to binary (4 bytes: 1 opcode + 3 arg)
19 20 21 22 23 24 25 26 |
# File 'lib/lexer_kit/ir/instruction.rb', line 19 def to_binary [ @opcode, (@arg >> 16) & 0xFF, (@arg >> 8) & 0xFF, @arg & 0xFF ].pack("C4") end |