Class: Koota::Compiler
Overview
Constant Summary
Constants included
from VM::Opcodes
VM::Opcodes::CALL, VM::Opcodes::HALT, VM::Opcodes::JRND, VM::Opcodes::JUMP, VM::Opcodes::PICK, VM::Opcodes::PUT, VM::Opcodes::RET
Instance Method Summary
collapse
Instance Method Details
#call(ast, refs = {}) ⇒ Object
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/koota/compiler.rb', line 13
def call(ast, refs = {})
@memory = []
@refs = refs
@links = {
calls: {},
picks: {}
}
@name = ''
compile(ast)
(@memory << HALT).tap do
link_calls(@links[:calls])
link_picks(@links[:picks])
end
end
|
#compile_atom(atom) ⇒ Object
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/koota/compiler.rb', line 123
def compile_atom(atom)
atom.each_char do |char|
symbolified_char = char.to_sym
if @refs[symbolified_char] && @name != symbolified_char
@links[:calls][@memory.length] = symbolified_char
add_bytecode(CALL, 0, 0)
else
add_bytecode(PUT, *Encode.utf8(char))
end
end
end
|
#compile_choice(*choices) ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/koota/compiler.rb', line 75
def compile_choice(*choices)
pick_offset = @memory.length
add_bytecode(PICK, 0, 0)
jumps = []
offsets = []
choices.each_with_index do |choice, i|
offsets << @memory.length
compile(choice)
unless i == choices.length - 1
jumps << @memory.length
add_bytecode(JUMP, 0, 0)
end
end
encoded_jump = Encode.short(@memory.length)
jumps.each do |offset|
@memory[offset + 1], @memory[offset + 2] = encoded_jump
end
@links[:picks][pick_offset] = offsets
end
|
#compile_maybe(maybe) ⇒ Object
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/koota/compiler.rb', line 112
def compile_maybe(maybe)
jrnd_offset = @memory.length
add_bytecode(JRND, 0, 0)
compile(maybe)
@memory[jrnd_offset + 1], @memory[jrnd_offset + 2] = Encode.short(@memory.length)
end
|
#compile_pattern(*sequence) ⇒ Object
71
72
73
|
# File 'lib/koota/compiler.rb', line 71
def compile_pattern(*sequence)
sequence.each { |it| compile(it) }
end
|
#compile_raw(raw) ⇒ Object
141
142
143
144
145
|
# File 'lib/koota/compiler.rb', line 141
def compile_raw(raw)
raw.each_char do |char|
add_bytecode(PUT, *Encode.utf8(char))
end
end
|
#link_calls(links) ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/koota/compiler.rb', line 30
def link_calls(links)
return if links.empty?
offsets = {}
@refs.each do |ref, ref_ast|
next unless links.value?(ref)
offsets[ref] = @memory.length
@name = ref
compile(ref_ast)
@name = ''
add_bytecode(RET) end
links.each do |offset, ref|
@memory[offset + 1], @memory[offset + 2] = Encode.short(offsets[ref])
end
end
|
#link_picks(links) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/koota/compiler.rb', line 56
def link_picks(links)
return if links.empty?
links.each do |offset, picks|
@memory[offset + 1], @memory[offset + 2] = Encode.short(@memory.length)
add_bytecode(*Encode.short(picks.length))
picks.each do |pick|
add_bytecode(*Encode.short(pick))
end
end
end
|