-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytecode_compiler.rb
201 lines (157 loc) · 3.36 KB
/
bytecode_compiler.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
module Lambra
class BytecodeCompiler
attr_reader :generator
alias g generator
SPECIAL_FORMS = %w(def)
def initialize
@generator = Rubinius::Generator.new
end
def compile(ast, debugging=false)
if debugging
require 'pp'
pp ast.to_sexp
end
if ast.respond_to?(:filename) && ast.filename
g.file = ast.filename
else
g.file = :"(lambra)"
end
g.set_line ast.line || 1
ast.accept(self)
debug if debugging
g.ret
finalize
end
def visit_List(o)
set_line(o)
return g.push_nil if o.elements.count.zero?
car = o.elements[0]
cdr = o.elements[1..-1]
return visit_SpecialForm(car.name, cdr) if special_form?(car.name)
args = cdr.count
visit_Symbol(car)
# # TODO: lazy evaluation
cdr.each do |arg|
arg.accept(self)
end
g.send :call, args
end
def visit_SpecialForm(car, cdr)
case car.to_s
when 'def'
name = cdr.shift.name
g.push_cpath_top
g.find_const :Scope
g.push_literal name
cdr.first.accept(self)
g.send :[]=, 2
end
end
def visit_Symbol(o)
set_line(o)
g.push_cpath_top
g.find_const :Scope
g.push_literal o.name
g.send :fetch, 1
end
def visit_Number(o)
set_line(o)
g.push_literal o.value
end
def visit_Character(o)
set_line(o)
g.push_literal o.value
end
def visit_String(o)
set_line(o)
g.push_literal o.value
end
def visit_True(o)
set_line(o)
g.push_true
end
def visit_False(o)
set_line(o)
g.push_false
end
def visit_Nil(o)
set_line(o)
g.push_nil
end
def visit_Keyword(o)
set_line(o)
g.push_cpath_top
g.find_const :Keyword
g.push_literal o.name
g.send :new, 1
end
def visit_Sequence(o)
set_line(o)
o.elements.compact.each do |element|
element.accept(self)
end
end
def visit_Vector(o)
count = o.elements.size
set_line(o)
o.elements.each do |x|
x.accept(self)
end
g.make_array count
end
def visit_Set(o)
set_line(o)
g.push_cpath_top
g.find_const :Set
count = o.elements.count
o.elements.each do |elem|
elem.accept(self)
end
g.make_array count
g.send :new, 1
end
def visit_Map(o)
set_line(o)
ary = o.to_a
count = ary.size
i = 0
g.push_cpath_top
g.find_const :Hash
g.push count # / 2
g.send :new_from_literal, 1
while i < count
k = ary[i].first
v = ary[i].last
g.dup
k.accept(self)
v.accept(self)
g.send :[]=, 2
g.pop
i += 1
end
end
def finalize
# g.local_names = s.variables
# g.local_count = s.variables.size
g.close
g
end
def set_line(o)
g.set_line o.line if o.line
end
def debug(gen = self.g)
p '*****'
ip = 0
while instruction = gen.stream[ip]
instruct = Rubinius::InstructionSet[instruction]
ip += instruct.size
puts instruct.name
end
p '**end**'
end
private
def special_form?(name)
SPECIAL_FORMS.include?(name.to_s)
end
end
end