Class: Instruction
- Inherits:
-
Object
- Object
- Instruction
- Defined in:
- lib/rpeg/parsing_machine.rb
Overview
Instances are generated during program generation in Pattern and consumed in the ParsingMachine
-
op_code: the instruction op
-
offset: the address offset used in jumps, calls, etc.
-
aux: extra information used by instructions like capture
-
in LPEG this is used to carefully pack data by bit-twiddling, etc., but we can use anything, such as structs, etc., as needed
-
-
data: this is called “key” in LPEG and is used to store pointers to Lua-based objects, etc.
-
we will just store Ruby objects here.
-
it contains things like the set of characters for Charset instructions, etc.
-
-
dec: “decorations” for other things like labels that might be useful later in debugging, etc.
-
it is ignored by the VM
-
Constant Summary collapse
- OP_CODES =
%i[ char charset any jump choice call return commit back_commit partial_commit span op_end fail fail_twice open_capture close_capture close_run_time full_capture behind test_char test_charset test_any ].each do |op| const_set op.upcase, op end
- OP_WIDTH =
OP_CODES.map(&:length).max
- DECORATION_WIDTH =
15
Instance Attribute Summary collapse
-
#aux ⇒ Object
readonly
Returns the value of attribute aux.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#dec ⇒ Object
Returns the value of attribute dec.
-
#offset ⇒ Object
Returns the value of attribute offset.
-
#op_code ⇒ Object
readonly
Returns the value of attribute op_code.
Instance Method Summary collapse
-
#initialize(op_code, offset: nil, data: nil, aux: nil, dec: nil) ⇒ Instruction
constructor
A new instance of Instruction.
- #to_s ⇒ Object
Constructor Details
#initialize(op_code, offset: nil, data: nil, aux: nil, dec: nil) ⇒ Instruction
Returns a new instance of Instruction.
30 31 32 33 34 35 36 37 38 |
# File 'lib/rpeg/parsing_machine.rb', line 30 def initialize(op_code, offset: nil, data: nil, aux: nil, dec: nil) raise "Bad instruction op_code #{op_code}" unless OP_CODES.include?(op_code) @op_code = op_code @offset = offset @data = data @aux = aux @dec = dec end |
Instance Attribute Details
#aux ⇒ Object (readonly)
Returns the value of attribute aux.
27 28 29 |
# File 'lib/rpeg/parsing_machine.rb', line 27 def aux @aux end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
27 28 29 |
# File 'lib/rpeg/parsing_machine.rb', line 27 def data @data end |
#dec ⇒ Object
Returns the value of attribute dec.
28 29 30 |
# File 'lib/rpeg/parsing_machine.rb', line 28 def dec @dec end |
#offset ⇒ Object
Returns the value of attribute offset.
28 29 30 |
# File 'lib/rpeg/parsing_machine.rb', line 28 def offset @offset end |
#op_code ⇒ Object (readonly)
Returns the value of attribute op_code.
27 28 29 |
# File 'lib/rpeg/parsing_machine.rb', line 27 def op_code @op_code end |
Instance Method Details
#to_s ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/rpeg/parsing_machine.rb', line 40 def to_s return @to_s if @to_s str = (dec || "").to_s.rjust(DECORATION_WIDTH) + " :" str << op_code.to_s.upcase.rjust(OP_WIDTH + 1) str << " offset: #{offset}" if [TEST_CHAR, TEST_ANY, TEST_CHARSET].include?(op_code) case op_code when CHAR, TEST_CHAR str << " #{data.dump}" when BEHIND str << " #{aux}" when CHARSET, SPAN, TEST_CHARSET str << " #{charset_rep(data)}" when JUMP, CHOICE, CALL, COMMIT, BACK_COMMIT, PARTIAL_COMMIT str << " #{offset}" when RETURN, OP_END, FAIL, FAIL_TWICE, ANY, TEST_ANY # no-op when OPEN_CAPTURE, CLOSE_CAPTURE, FULL_CAPTURE, CLOSE_RUN_TIME str << " data:#{data}, aux:#{aux}" else raise "Unhandled op_code #{op_code} in Instruction#to_s" end @to_s = str end |