Class: BrainLove::Compiler
- Inherits:
-
Object
- Object
- BrainLove::Compiler
- Defined in:
- lib/brain_love/compiler.rb
Instance Attribute Summary collapse
-
#bytecode ⇒ Object
readonly
Returns the value of attribute bytecode.
-
#ip ⇒ Object
readonly
Returns the value of attribute ip.
Instance Method Summary collapse
- #current_ip ⇒ Object
- #dec_byte ⇒ Object
- #dec_dp ⇒ Object
- #getc ⇒ Object
- #inc_byte ⇒ Object
- #inc_dp ⇒ Object
-
#initialize ⇒ Compiler
constructor
A new instance of Compiler.
- #jmpbnz(offset) ⇒ Object
- #jmpfz(at, offset) ⇒ Object
- #noop ⇒ Object
- #putc ⇒ Object
- #visit_DecrementByte(_) ⇒ Object
- #visit_DecrementPointer(_) ⇒ Object
- #visit_IncrementByte(_) ⇒ Object
- #visit_IncrementPointer(_) ⇒ Object
- #visit_InputByte(_) ⇒ Object
- #visit_Loop(_loop) ⇒ Object
- #visit_OutputByte(_) ⇒ Object
- #visit_Statements(statements) ⇒ Object
Constructor Details
#initialize ⇒ Compiler
Returns a new instance of Compiler.
6 7 8 9 |
# File 'lib/brain_love/compiler.rb', line 6 def initialize @bytecode = "" @ip = 0 end |
Instance Attribute Details
#bytecode ⇒ Object (readonly)
Returns the value of attribute bytecode.
4 5 6 |
# File 'lib/brain_love/compiler.rb', line 4 def bytecode @bytecode end |
#ip ⇒ Object (readonly)
Returns the value of attribute ip.
4 5 6 |
# File 'lib/brain_love/compiler.rb', line 4 def ip @ip end |
Instance Method Details
#current_ip ⇒ Object
48 49 50 |
# File 'lib/brain_love/compiler.rb', line 48 def current_ip @bytecode.bytesize end |
#dec_byte ⇒ Object
23 24 25 |
# File 'lib/brain_love/compiler.rb', line 23 def dec_byte @bytecode << VM::DEC_BYTE.chr end |
#dec_dp ⇒ Object
15 16 17 |
# File 'lib/brain_love/compiler.rb', line 15 def dec_dp @bytecode << VM::DEC_DP.chr end |
#getc ⇒ Object
31 32 33 |
# File 'lib/brain_love/compiler.rb', line 31 def getc @bytecode << VM::GETC.chr end |
#inc_byte ⇒ Object
19 20 21 |
# File 'lib/brain_love/compiler.rb', line 19 def inc_byte @bytecode << VM::INC_BYTE.chr end |
#inc_dp ⇒ Object
11 12 13 |
# File 'lib/brain_love/compiler.rb', line 11 def inc_dp @bytecode << VM::INC_DP.chr end |
#jmpbnz(offset) ⇒ Object
40 41 42 |
# File 'lib/brain_love/compiler.rb', line 40 def jmpbnz(offset) @bytecode << VM::JMPBNZ.chr << offset.chr end |
#jmpfz(at, offset) ⇒ Object
35 36 37 38 |
# File 'lib/brain_love/compiler.rb', line 35 def jmpfz(at, offset) @bytecode[at] = VM::JMPFZ.chr @bytecode[at + 1] = offset.chr end |
#noop ⇒ Object
44 45 46 |
# File 'lib/brain_love/compiler.rb', line 44 def noop @bytecode << VM::NOOP end |
#putc ⇒ Object
27 28 29 |
# File 'lib/brain_love/compiler.rb', line 27 def putc @bytecode << VM::PUTC.chr end |
#visit_DecrementByte(_) ⇒ Object
77 78 79 |
# File 'lib/brain_love/compiler.rb', line 77 def visit_DecrementByte(_) dec_byte end |
#visit_DecrementPointer(_) ⇒ Object
65 66 67 |
# File 'lib/brain_love/compiler.rb', line 65 def visit_DecrementPointer(_) dec_dp end |
#visit_IncrementByte(_) ⇒ Object
73 74 75 |
# File 'lib/brain_love/compiler.rb', line 73 def visit_IncrementByte(_) inc_byte end |
#visit_IncrementPointer(_) ⇒ Object
69 70 71 |
# File 'lib/brain_love/compiler.rb', line 69 def visit_IncrementPointer(_) inc_dp end |
#visit_InputByte(_) ⇒ Object
85 86 87 |
# File 'lib/brain_love/compiler.rb', line 85 def visit_InputByte(_) getc end |
#visit_Loop(_loop) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/brain_love/compiler.rb', line 56 def visit_Loop(_loop) jmpf_ip = current_ip noop noop _loop.value.accept(self) jmpbnz(current_ip - jmpf_ip) jmpfz(jmpf_ip, current_ip) end |
#visit_OutputByte(_) ⇒ Object
81 82 83 |
# File 'lib/brain_love/compiler.rb', line 81 def visit_OutputByte(_) putc end |
#visit_Statements(statements) ⇒ Object
52 53 54 |
# File 'lib/brain_love/compiler.rb', line 52 def visit_Statements(statements) statements.value.each { |st| st.accept(self) } end |