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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
|
# File 'lib/phaad/generator.rb', line 51
def process(sexp)
case sexp.first
when :void_stmt
when :@int, :@float
emit sexp[1]
when :@tstring_content
emit "\"#{sexp[1].gsub('"', '\"')}\""
when :string_content
if sexp.size > 1
sexp[1..-1].each_with_index do |exp, i|
unless exp[0] == :string_embexpr && exp[1][0][0] == :void_stmt
process exp
emit " . " if i < sexp.size - 2
end
end
else
emit '""'
end
when :string_literal
process(sexp[1])
when :string_embexpr
no_brackets = [:var_ref, :method_add_arg, :command]
emit "(" unless no_brackets.include?(sexp[1][0][0])
process sexp[1][0]
emit ")" unless no_brackets.include?(sexp[1][0][0])
when :regexp_literal
emit '"/'
emit sexp[1][0][1]
process sexp[2]
emit '"'
when :@regexp_end
emit sexp[1]
when :symbol_literal
emit sexp[1][1][1].inspect
when :@label
emit sexp[1][0..-2].inspect
when :dyna_symbol
process(sexp[1][0])
when :assign
process(sexp[1])
emit " = "
process(sexp[2])
when :massign
lhs = sexp[1]
rhs = sexp[2]
rhs.shift if rhs.first == :mrhs_new_from_args
rhs = rhs[0] + [rhs[1]]
unless lhs.size == rhs.size
raise NotImplementedError, sexp.inspect
end
lhs.zip(rhs).each do |l, r|
process l
emit " = "
process r
emit ";\n"
end
when :opassign
raise NotImplementedError, sexp.inspect unless sexp[2][0] == :@op
process(sexp[1])
if sexp[2][1] == "<<="
emit " .= "
else
emit " #{sexp[2][1]} "
end
process(sexp[3])
when :return
emit "return "
process sexp[1]
when :var_field
process(sexp[1])
when :@ident
no_dollar = ["__NAMESPACE__", "__DIR__", "__METHOD__", "__CLASS__", "__FUNCTION__"]
emit "$" unless no_dollar.include?(sexp[1])
emit sexp[1]
when :@ivar
emit "$this->"
emit sexp[1][1..-1]
when :@const
emit sexp[1]
when :break
emit "break"
when :next
emit "continue"
when :method_add_arg
if sexp[1][0] == :fcall && sexp[1][1][0] == :@ident
emit sexp[1][1][1]
emit "("
process sexp[2][1] if sexp[2][1]
emit ")"
elsif sexp[1][0] == :call
process sexp[1]
emit "("
process sexp[2][1] if sexp[2][1]
emit ")"
else
raise NotImplementedError, sexp.inspect
end
when :ifop
process sexp[1]
emit " ? "
process sexp[2]
emit " : "
process sexp[3]
when :command
if sexp[1][0] == :@ident
no_brackets = %w{global echo var public}
emit sexp[1][1]
emit no_brackets.include?(sexp[1][1]) ? " " : "("
if sexp[2][0] == :args_add_block
process sexp[2]
else
sexp[2].each(&method(:process))
end
emit ")" unless no_brackets.include?(sexp[1][1])
else
raise NotImplementedError, sexp.inspect
end
when :args_add_block
sexp[1].each do |s|
process s
emit ", " unless s == sexp[1].last
end
when :call
if sexp[1][1][0] == :@const
if sexp[3][1] == "new"
emit "new "
process sexp[1]
else
process sexp[1]
emit "::"
emit sexp[3][1]
end
else
process sexp[1]
emit "->"
emit sexp[3][1]
end
when :command_call
if sexp[1][0] == :var_ref && sexp[1][1][0] == :@ident && sexp[2] == :"." &&
sexp[3][0] == :@ident && sexp[4][0] == :args_add_block
process sexp[1]
emit "->"
emit sexp[3][1]
emit "("
process sexp[4]
emit ")"
elsif sexp[1][0] == :var_ref && sexp[1][1][0] == :@const &&
sexp[3][0] == :@ident && sexp[4][0] == :args_add_block
if sexp[3][1] == "new"
emit "new "
process sexp[1]
else
process sexp[1]
emit "::"
emit sexp[3][1]
end
emit "("
process sexp[4]
emit ")"
else
raise NotImplementedError, sexp.inspect
end
when :if, :elsif, :unless
if sexp.first == :if
emit "if("
elsif sexp.first == :unless
emit "if(!("
else
emit "elseif("
end
process sexp[1]
emit ")" if sexp.first == :unless
emit ") {\n"
process_statements(sexp[2])
emit "}"
if sexp[3]
emit " "
process sexp[3]
else
emit "\n"
end
when :else
emit "else {\n"
process_statements(sexp[1]) if sexp[1]
emit "}\n"
process sexp[3] if sexp[3]
when :if_mod, :unless_mod
emit "if("
emit "!(" if sexp.first == :unless_mod
process sexp[1]
emit ")" if sexp.first == :unless_mod
emit ") {\n"
process_statements [sexp[2]]
emit "}\n"
when :while, :until
emit "while("
emit "!(" if sexp.first == :until
process sexp[1]
emit ")" if sexp.first == :until
emit ") {\n"
process_statements sexp[2]
emit "}\n"
when :while_mod, :until_mod
emit "while("
emit "!(" if sexp.first == :until_mod
process sexp[1]
emit ")" if sexp.first == :until_mod
emit ") {\n"
process_statements [sexp[2]]
emit "}\n"
when :for
if !sexp[1][0].is_a?(Array) && sexp[2][0] == :dot2 || sexp[2][0] == :dot3
emit "for("
process sexp[1]
emit " = "
process sexp[2][1]
emit "; "
process sexp[1]
emit sexp[2][0] == :dot2 ? " <= " : " < "
process sexp[2][2]
emit "; "
process sexp[1]
emit "++"
else
emit "foreach("
process sexp[2]
emit " as "
if !sexp[1][0].is_a?(Array)
process sexp[1]
elsif sexp[1][0].is_a?(Array) && sexp[1].size == 2
process sexp[1][0]
emit " => "
process sexp[1][1]
else
raise NotImplementedError, sexp.inspect
end
end
emit ") {\n"
process_statements sexp[3]
emit "}\n"
when :case
emit "switch("
process sexp[1]
emit ") {\n"
indent
exp = sexp[2]
while exp
if exp[0] == :when
exp[1].each_with_index do |each_case, i|
emit "case "
process each_case
emit ":"
emit " " if i < exp[1].size - 1
end
emit "\n"
process_statements exp[2] if exp[2]
elsif exp[0] == :else
emit "default:"
emit "\n"
process_statements exp[1] if exp[1]
else
raise NotImplementedError, sexp.inspect
end
indent
emit "break;\n"
outdent
exp = exp[3]
end
outdent
emit "}\n"
when :def
raise NotImplementedError, sexp.inspect unless sexp[1][0] == :@ident
emit "function "
emit sexp[1][1]
emit "("
if sexp[2][0] == :params
process sexp[2] elsif sexp[2][0] == :paren && sexp[2][1][0] == :params
process sexp[2][1]
else
raise NotImplementedError, sexp.inspect
end
emit ") {\n"
process_statements [sexp[3]]
emit "}\n"
when :defs
raise NotImplementedError, sexp.inspect unless sexp[3][0] == :@ident
emit "static function "
emit sexp[3][1]
emit "("
if sexp[4][0] == :params
process sexp[4] elsif sexp[4][0] == :paren && sexp[4][1][0] == :params
process sexp[4][1]
else
raise NotImplementedError, sexp.inspect
end
emit ") {\n"
process_statements [sexp[5]]
emit "}\n"
when :class
if sexp[1][1][0] != :@const || (sexp[2] && sexp[2][1][0] != :@const)
raise NotImplementedError, sexp.inspect
end
emit "class "
emit sexp[1][1][1]
emit " extends #{sexp[2][1][1]}" if sexp[2]
emit " {\n"
process_statements [sexp[3]]
emit "}\n"
when :params
first = true
if sexp[1]
sexp[1].each do |param|
emit ", " unless first
first = false
process param
end
end
if sexp[2]
sexp[2].each do |param|
emit ", " unless first
first = false
process param[0]
emit " = "
process param[1]
end
end
when :array, :hash, :bare_assoc_hash
emit "array("
if sexp[1]
if sexp[1][0] == :assoclist_from_args
process sexp[1]
else
sexp[1].each_with_index do |param, i|
process param
emit ", " if i < sexp[1].size - 1
end
end
end
emit ")"
when :assoclist_from_args
sexp[1].each_with_index do |param, i|
process param
emit ", " if i < sexp[1].size - 1
end
when :assoc_new
process sexp[1]
emit " => "
process sexp[2]
when :bodystmt
process_statements(sexp[1], :indent => false)
when :paren
emit "("
if sexp[1].size == 1
process sexp[1][0]
else
raise NotImplementedError, sexp.inspect
end
emit ")"
when :aref_field
process sexp[1]
emit "["
process sexp[2] if sexp[2]
emit "]"
when :aref
process sexp[1]
emit "["
if sexp[2][1].size == 1
process sexp[2] if sexp[2][1][0]
else
raise NotImplementedError, sexp.inspect
end
emit "]"
when :unary
case sexp[1]
when :+@, :-@, :~, :'!'
emit sexp[1].to_s[0]
process sexp[2]
else
raise NotImplementedError, sexp.inspect
end
when :binary
case sexp[2]
when :+, :-, :*, :/, :%, :|, :&, :^, :'&&', :'||', :==, :'!=', :>, :<,
:>=, :<=, :===
process(sexp[1])
emit " #{sexp[2]} "
process(sexp[3])
when :and
process(sexp[1])
emit " && "
process(sexp[3])
when :or
process(sexp[1])
emit " || "
process(sexp[3])
when :<<
process(sexp[1])
emit " . "
process(sexp[3])
when :**
emit "pow("
process(sexp[1])
emit ", "
process(sexp[3])
emit ")"
when :=~
emit "preg_match("
process(sexp[3])
emit ", "
process(sexp[1])
emit ")"
when :'!~'
emit "!preg_match("
process(sexp[3])
emit ", "
process(sexp[1])
emit ")"
else
raise NotImplementedError, sexp.inspect
end
when :var_ref
if sexp[1][0] == :@kw
case sexp[1][1]
when 'false', 'true'
emit sexp[1][1].upcase
when 'nil'
emit 'NULL'
when '__FILE__', '__LINE__'
emit sexp[1][1]
else
raise NotImplementedError, sexp.inspect
end
elsif sexp[1][0] == :@ident
process sexp[1]
elsif sexp[1][0] == :@ivar
process sexp[1]
elsif sexp[1][0] == :@const
process sexp[1]
else
raise NotImplementedError, sexp.inspect
end
else
raise NotImplementedError, sexp.inspect
end
end
|