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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
# File 'lib/apricot/compiler.rb', line 91
def bytecode(g, form, quoted = false, macroexpand = true)
pos(g, form)
case form
when Identifier
if quoted
g.push_const :Apricot
g.find_const :Identifier
g.push_literal form.name
g.send :intern, 1
else
if form.constant?
g.push_const form.const_names.first
form.const_names.drop(1).each {|n| g.find_const n }
elsif form == SELF
g.push_self
elsif form.qualified?
QualifiedReference.new(form.unqualified_name, form.qualifier).bytecode(g)
else
g.scope.find_var(form.name).bytecode(g)
end
end
when Seq
if quoted || form.empty?
g.push_const :Apricot
g.find_const :List
if form.empty?
g.find_const :EMPTY_LIST
else
form.each {|e| bytecode(g, e, true) }
g.send :[], form.count
end
else
callee, args = form.first, form.rest
if callee.is_a?(Identifier)
if special = SpecialForm[callee.name]
special.bytecode(g, args)
return
end
if macroexpand
form = Apricot.macroexpand(form)
if form.is_a?(Seq)
bytecode(g, form, false, false)
else
bytecode(g, form)
end
return
end
meta = callee.meta
if meta && meta[:inline] && (!meta[:'inline-arities'] ||
meta[:'inline-arities'].apricot_call(args.count))
begin
inlined_form = meta[:inline].apricot_call(*args)
rescue => e
g.compile_error "Inliner function for '#{callee.name}' raised an exception:\n #{e}"
end
g.tail_position = false
bytecode(g, inlined_form)
return
end
if callee.fn? || callee.method?
qualifier_id = Identifier.intern(callee.qualifier.name)
first_name, *rest_names = qualifier_id.const_names
g.push_const first_name
rest_names.each {|n| g.find_const(n) }
args.each {|arg| bytecode(g, arg) }
g.send callee.unqualified_name, args.count
return
end
end
g.tail_position = false
bytecode(g, callee)
args.each {|arg| bytecode(g, arg) }
g.send :apricot_call, args.count
end
when Symbol
g.push_literal form
when Array
form.each {|e| bytecode(g, e, quoted) }
g.make_array form.size
when String
g.push_literal form
g.string_dup
when Hash
g.push_const :Hash
g.push form.size
g.send :new_from_literal, 1
form.each_pair do |key, value|
g.dup
bytecode(g, key, quoted)
bytecode(g, value, quoted)
g.send :[]=, 2
g.pop
end
when Fixnum
g.push form
when true
g.push :true
when nil
g.push :nil
when false
g.push :false
when Float
g.push_unique_literal form
when Regexp
once(g) do
g.push_const :Regexp
g.push_literal form.source
g.push form.options
g.send :new, 2
end
when Rational
once(g) do
g.push_self
g.push form.numerator
g.push form.denominator
g.send :Rational, 2, true
end
when Set
g.push_const :Set
g.send :new, 0
form.each do |elem|
bytecode(g, elem, quoted)
g.send :add, 1
end
when Bignum
g.push_unique_literal form
else
g.compile_error "Can't generate bytecode for #{form} (#{form.class})"
end
end
|