Class: BrainLove::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/brain_love/compiler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCompiler

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

#bytecodeObject (readonly)

Returns the value of attribute bytecode.



4
5
6
# File 'lib/brain_love/compiler.rb', line 4

def bytecode
  @bytecode
end

#ipObject (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_ipObject



48
49
50
# File 'lib/brain_love/compiler.rb', line 48

def current_ip
  @bytecode.bytesize
end

#dec_byteObject



23
24
25
# File 'lib/brain_love/compiler.rb', line 23

def dec_byte
  @bytecode << VM::DEC_BYTE.chr
end

#dec_dpObject



15
16
17
# File 'lib/brain_love/compiler.rb', line 15

def dec_dp
  @bytecode << VM::DEC_DP.chr
end

#getcObject



31
32
33
# File 'lib/brain_love/compiler.rb', line 31

def getc
  @bytecode << VM::GETC.chr
end

#inc_byteObject



19
20
21
# File 'lib/brain_love/compiler.rb', line 19

def inc_byte
  @bytecode << VM::INC_BYTE.chr
end

#inc_dpObject



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

#noopObject



44
45
46
# File 'lib/brain_love/compiler.rb', line 44

def noop
  @bytecode << VM::NOOP
end

#putcObject



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