ruby2ruby 2.3.2 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 14e891234e3089376394e56c1a06d97198a44294
4
- data.tar.gz: aa54d78c6ddefd682cf2dfce42c2b30a30882325
3
+ metadata.gz: 5b53ed4dcaf3f49d555eaac182c0db834ef853b6
4
+ data.tar.gz: e9beb44116c010a09b853fdca582565b380db3a0
5
5
  SHA512:
6
- metadata.gz: 1498c9b51e93f477b02d8114bbd1718bfbdce2f588681d1e8162d70297ce6ceeba34c5f2ced5bc689ecccc6ce0bbb058dc2695a6380c753af96ae15db35166ff
7
- data.tar.gz: 9ecce60ed4b40ac244f786f3e3f6dbfaa4d111ce221328f66efbb0dcd4208c4e2b3752c920279012b4771333e9afb690198f141ae9c4b129dc146a1841f73811
6
+ metadata.gz: a253a034894e65cfdaa8b5d96ab884db76c828b6b5ef2bec0f5c93a2beea9bba808919d0553342d1ba63e16bb52341f3159818cc2457951524289e32ea55743c
7
+ data.tar.gz: 4a0fdf340723d063e6f421654ad2cb8d458c47f6bf9a340acf6a13794a196dac556986c31b4512daf5f0a0864a438759c17fe918bfec226496333fd200c28bc9
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -1,3 +1,18 @@
1
+ === 2.4.0 / 2017-07-17
2
+
3
+ * 1 major enhancement:
4
+
5
+ * Major overhaul. Process methods no longer mutate arguments.
6
+
7
+ * 1 minor enhancement:
8
+
9
+ * Now SEXP_STRICT=1 clean.
10
+
11
+ * 2 bug fixes:
12
+
13
+ * Fixed 2.4 Fixnum deprecation warning
14
+ * Fixed for new 24 parser.
15
+
1
16
  === 2.3.2 / 2016-11-30
2
17
 
3
18
  * 5 bug fixes:
@@ -1,7 +1,7 @@
1
1
  .autotest
2
- History.txt
2
+ History.rdoc
3
3
  Manifest.txt
4
- README.txt
4
+ README.rdoc
5
5
  Rakefile
6
6
  bin/r2r_show
7
7
  lib/ruby2ruby.rb
File without changes
data/Rakefile CHANGED
@@ -9,6 +9,7 @@ Hoe.add_include_dirs("lib",
9
9
 
10
10
  Hoe.plugin :seattlerb
11
11
  Hoe.plugin :isolate
12
+ Hoe.plugin :rdoc
12
13
 
13
14
  Hoe.spec 'ruby2ruby' do
14
15
  developer 'Ryan Davis', 'ryand-ruby@zenspider.com'
@@ -31,7 +31,7 @@ end
31
31
  # Generate ruby code from a sexp.
32
32
 
33
33
  class Ruby2Ruby < SexpProcessor
34
- VERSION = "2.3.2" # :nodoc:
34
+ VERSION = "2.4.0" # :nodoc:
35
35
 
36
36
  # cutoff for one-liners
37
37
  LINE_LENGTH = 78
@@ -82,7 +82,7 @@ class Ruby2Ruby < SexpProcessor
82
82
  def initialize # :nodoc:
83
83
  super
84
84
  @indent = " "
85
- self.auto_shift_type = true
85
+ self.require_empty = false
86
86
  self.strict = true
87
87
  self.expected = String
88
88
 
@@ -94,71 +94,72 @@ class Ruby2Ruby < SexpProcessor
94
94
  ############################################################
95
95
  # Processors
96
96
 
97
- def process_alias(exp) # :nodoc:
98
- parenthesize "alias #{process(exp.shift)} #{process(exp.shift)}"
97
+ def process_alias exp # :nodoc:
98
+ _, lhs, rhs = exp
99
+
100
+ parenthesize "alias #{process lhs} #{process rhs}"
99
101
  end
100
102
 
101
- def process_and(exp) # :nodoc:
102
- parenthesize "#{process exp.shift} and #{process exp.shift}"
103
+ def process_and exp # :nodoc:
104
+ _, lhs, rhs = exp
105
+
106
+ parenthesize "#{process lhs} and #{process rhs}"
103
107
  end
104
108
 
105
- def process_arglist(exp) # custom made node # :nodoc:
106
- code = []
107
- until exp.empty? do
108
- arg = exp.shift
109
- to_wrap = arg.first == :rescue
110
- arg_code = process arg
111
- code << (to_wrap ? "(#{arg_code})" : arg_code)
112
- end
113
- code.join ', '
109
+ def process_arglist exp # custom made node # :nodoc:
110
+ _, *args = exp
111
+
112
+ args.map { |arg|
113
+ code = process arg
114
+ arg.sexp_type == :rescue ? "(#{code})" : code
115
+ }.join ", "
114
116
  end
115
117
 
116
- def process_args(exp) # :nodoc:
117
- args = []
118
+ def process_args exp # :nodoc:
119
+ _, *args = exp
118
120
 
119
- until exp.empty? do
120
- arg = exp.shift
121
+ args = args.map { |arg|
121
122
  case arg
122
123
  when Symbol then
123
- args << arg
124
+ arg
124
125
  when Sexp then
125
- case arg.first
126
+ case arg.sexp_type
126
127
  when :lasgn then
127
- args << process(arg)
128
+ process(arg)
128
129
  when :masgn then
129
- args << process(arg)
130
+ process(arg)
130
131
  when :kwarg then
131
132
  _, k, v = arg
132
- args << "#{k}: #{process v}"
133
+ "#{k}: #{process v}"
133
134
  else
134
135
  raise "unknown arg type #{arg.first.inspect}"
135
136
  end
136
137
  else
137
138
  raise "unknown arg type #{arg.inspect}"
138
139
  end
139
- end
140
+ }
140
141
 
141
142
  "(#{args.join ', '})"
142
143
  end
143
144
 
144
- def process_array(exp) # :nodoc:
145
- "[#{process_arglist(exp)}]"
145
+ def process_array exp # :nodoc:
146
+ "[#{process_arglist exp}]"
146
147
  end
147
148
 
148
- def process_attrasgn(exp) # :nodoc:
149
- receiver = process exp.shift
150
- name = exp.shift
151
- rhs = exp.pop
152
- args = s(:array, *exp)
153
- exp.clear
149
+ def process_attrasgn exp # :nodoc:
150
+ _, recv, name, *args = exp
151
+
152
+ rhs = args.pop
153
+ args = s(:array, *args)
154
+ receiver = process recv
154
155
 
155
156
  case name
156
157
  when :[]= then
157
158
  args = process args
158
159
  "#{receiver}#{args} = #{process rhs}"
159
160
  else
160
- raise "dunno what to do: #{args.inspect}" unless args.size == 1 # s(:array)
161
- name = name.to_s.sub(/=$/, '')
161
+ raise "dunno what to do: #{args.inspect}" if args.size != 1 # s(:array)
162
+ name = name.to_s.chomp "="
162
163
  if rhs && rhs != s(:arglist) then
163
164
  "#{receiver}.#{name} = #{process(rhs)}"
164
165
  else
@@ -167,51 +168,54 @@ class Ruby2Ruby < SexpProcessor
167
168
  end
168
169
  end
169
170
 
170
- def process_back_ref(exp) # :nodoc:
171
- "$#{exp.shift}"
171
+ def process_back_ref exp # :nodoc:
172
+ _, n = exp
173
+
174
+ "$#{n}"
172
175
  end
173
176
 
174
177
  # TODO: figure out how to do rescue and ensure ENTIRELY w/o begin
175
- def process_begin(exp) # :nodoc:
176
- code = []
177
- code << "begin"
178
- until exp.empty?
179
- src = process(exp.shift)
180
- src = indent(src) unless src =~ /(^|\n)(rescue|ensure)/ # ensure no level 0 rescues
181
- code << src
182
- end
183
- code << "end"
184
- return code.join("\n")
178
+ def process_begin exp # :nodoc:
179
+ _, *rest = exp
180
+
181
+ code = rest.map { |sexp|
182
+ src = process sexp
183
+ src = indent src unless src =~ /(^|\n)(rescue|ensure)/ # ensure no level 0 rescues
184
+ src
185
+ }
186
+ code.unshift "begin"
187
+ code.push "end"
188
+
189
+ code.join "\n"
185
190
  end
186
191
 
187
- def process_block(exp) # :nodoc:
188
- result = []
192
+ def process_block exp # :nodoc:
193
+ _, *body = exp
189
194
 
190
- exp << nil if exp.empty?
191
- until exp.empty? do
192
- code = exp.shift
193
- if code.nil? or code.first == :nil then
194
- result << "# do nothing\n"
195
- else
196
- result << process(code)
197
- end
198
- end
195
+ result = body.map { |sexp|
196
+ process sexp
197
+ }
199
198
 
199
+ result << "# do nothing\n" if result.empty?
200
200
  result = parenthesize result.join "\n"
201
201
  result += "\n" unless result.start_with? "("
202
202
 
203
- return result
203
+ result
204
204
  end
205
205
 
206
206
  def process_block_pass exp # :nodoc:
207
- raise "huh?: #{exp.inspect}" if exp.size > 1
207
+ raise "huh?: #{exp.inspect}" if exp.size > 2
208
+
209
+ _, sexp = exp
208
210
 
209
- "&#{process exp.shift}"
211
+ "&#{process sexp}"
210
212
  end
211
213
 
212
- def process_break(exp) # :nodoc:
213
- val = exp.empty? ? nil : process(exp.shift)
214
- # HACK "break" + (val ? " #{val}" : "")
214
+ def process_break exp # :nodoc:
215
+ _, arg = exp
216
+
217
+ val = process arg
218
+
215
219
  if val then
216
220
  "break #{val}"
217
221
  else
@@ -220,37 +224,39 @@ class Ruby2Ruby < SexpProcessor
220
224
  end
221
225
 
222
226
  def process_call(exp, safe_call = false) # :nodoc:
223
- receiver_node_type = exp.first.nil? ? nil : exp.first.first
224
- receiver = process exp.shift
227
+ _, recv, name, *args = exp
228
+
229
+ receiver_node_type = recv && recv.sexp_type
230
+ receiver = process recv
225
231
  receiver = "(#{receiver})" if ASSIGN_NODES.include? receiver_node_type
226
232
 
227
- name = exp.shift
228
- args = []
233
+ # args = []
229
234
 
230
235
  # this allows us to do both old and new sexp forms:
231
- exp.push(*exp.pop[1..-1]) if exp.size == 1 && exp.first.first == :arglist
236
+ # exp.push(*exp.pop[1..-1]) if exp.size == 1 && exp.first.first == :arglist
232
237
 
233
238
  @calls.push name
234
239
 
235
240
  in_context :arglist do
236
- until exp.empty? do
237
- arg_type = exp.first.sexp_type
238
- is_empty_hash = (exp.first == s(:hash))
239
- arg = process exp.shift
241
+ max = args.size-1
242
+ args = args.map.with_index { |arg, i|
243
+ arg_type = arg.sexp_type
244
+ is_empty_hash = arg == s(:hash)
245
+ arg = process arg
240
246
 
241
247
  next if arg.empty?
242
248
 
243
249
  strip_hash = (arg_type == :hash and
244
250
  not BINARY.include? name and
245
251
  not is_empty_hash and
246
- (exp.empty? or exp.first.sexp_type == :splat))
252
+ (i == max or args[i+1].sexp_type == :splat))
247
253
  wrap_arg = Ruby2Ruby::ASSIGN_NODES.include? arg_type
248
254
 
249
255
  arg = arg[2..-3] if strip_hash
250
256
  arg = "(#{arg})" if wrap_arg
251
257
 
252
- args << arg
253
- end
258
+ arg
259
+ }.compact
254
260
  end
255
261
 
256
262
  case name
@@ -287,127 +293,121 @@ class Ruby2Ruby < SexpProcessor
287
293
  @calls.pop
288
294
  end
289
295
 
290
- def process_safe_call(exp) # :nodoc:
291
- process_call(exp, :safe)
296
+ def process_safe_call exp # :nodoc:
297
+ process_call exp, :safe
292
298
  end
293
299
 
294
- def process_case(exp) # :nodoc:
300
+ def process_case exp # :nodoc:
301
+ _, expr, *rest = exp
302
+
295
303
  result = []
296
- expr = process exp.shift
304
+
305
+ expr = process expr
306
+
297
307
  if expr then
298
308
  result << "case #{expr}"
299
309
  else
300
310
  result << "case"
301
311
  end
302
- until exp.empty?
303
- pt = exp.shift
304
- if pt and pt.first == :when
305
- result << "#{process(pt)}"
312
+
313
+ result.concat rest.map { |pt|
314
+ if pt and pt.sexp_type == :when
315
+ "#{process pt}"
306
316
  else
307
- code = indent(process(pt))
317
+ code = indent process pt
308
318
  code = indent("# do nothing") if code =~ /^\s*$/
309
- result << "else\n#{code}"
319
+ "else\n#{code}"
310
320
  end
311
- end
321
+ }
322
+
312
323
  result << "end"
313
- result.join("\n")
324
+
325
+ result.join "\n"
314
326
  end
315
327
 
316
- def process_cdecl(exp) # :nodoc:
317
- lhs = exp.shift
328
+ def process_cdecl exp # :nodoc:
329
+ _, lhs, rhs = exp
318
330
  lhs = process lhs if Sexp === lhs
319
- unless exp.empty? then
320
- rhs = process(exp.shift)
331
+ if rhs then
332
+ rhs = process rhs
321
333
  "#{lhs} = #{rhs}"
322
334
  else
323
335
  lhs.to_s
324
336
  end
325
337
  end
326
338
 
327
- def process_class(exp) # :nodoc:
339
+ def process_class exp # :nodoc:
328
340
  "#{exp.comments}class #{util_module_or_class(exp, true)}"
329
341
  end
330
342
 
331
- def process_colon2(exp) # :nodoc:
332
- "#{process(exp.shift)}::#{exp.shift}"
343
+ def process_colon2 exp # :nodoc:
344
+ _, lhs, rhs = exp
345
+
346
+ "#{process lhs}::#{rhs}"
333
347
  end
334
348
 
335
- def process_colon3(exp) # :nodoc:
336
- "::#{exp.shift}"
349
+ def process_colon3 exp # :nodoc:
350
+ _, rhs = exp
351
+
352
+ "::#{rhs}"
337
353
  end
338
354
 
339
- def process_const(exp) # :nodoc:
340
- exp.shift.to_s
355
+ def process_const exp # :nodoc:
356
+ _, name = exp
357
+
358
+ name.to_s
341
359
  end
342
360
 
343
- def process_cvar(exp) # :nodoc:
344
- "#{exp.shift}"
361
+ def process_cvar exp # :nodoc:
362
+ _, name = exp
363
+
364
+ name.to_s
345
365
  end
346
366
 
347
- def process_cvasgn(exp) # :nodoc:
348
- "#{exp.shift} = #{process(exp.shift)}"
367
+ def process_cvasgn exp # :nodoc:
368
+ _, lhs, rhs = exp
369
+
370
+ "#{lhs} = #{process rhs}"
349
371
  end
350
372
 
351
- def process_cvdecl(exp) # :nodoc:
352
- "#{exp.shift} = #{process(exp.shift)}"
373
+ def process_cvdecl exp # :nodoc:
374
+ _, lhs, rhs = exp
375
+
376
+ "#{lhs} = #{process rhs}"
353
377
  end
354
378
 
355
- def process_defined(exp) # :nodoc:
356
- "defined? #{process(exp.shift)}"
379
+ def process_defined exp # :nodoc:
380
+ _, rhs = exp
381
+ "defined? #{process rhs}"
357
382
  end
358
383
 
359
384
  def process_defn(exp) # :nodoc:
360
- type1 = exp[1].first
361
- type2 = exp[2].first rescue nil
362
- expect = [:ivar, :iasgn, :attrset]
363
-
364
- # s(name, args, ivar|iasgn|attrset)
365
- if exp.size == 3 and type1 == :args and expect.include? type2 then
366
- name = exp.first # don't shift in case we pass through
367
- case type2
368
- when :ivar then
369
- ivar_name = exp.ivar.last
370
-
371
- meth_name = ivar_name.to_s[1..-1].to_sym
372
- expected = s(meth_name, s(:args), s(:ivar, ivar_name))
373
-
374
- if exp == expected then
375
- exp.clear
376
- return "attr_reader #{name.inspect}"
377
- end
378
- when :attrset then
379
- # TODO: deprecate? this is a PT relic
380
- exp.clear
381
- return "attr_writer :#{name.to_s[0..-2]}"
382
- when :iasgn then
383
- ivar_name = exp.iasgn[1]
384
- meth_name = "#{ivar_name.to_s[1..-1]}=".to_sym
385
- arg_name = exp.args.last
386
- expected = s(meth_name, s(:args, arg_name),
387
- s(:iasgn, ivar_name, s(:lvar, arg_name)))
388
-
389
- if exp == expected then
390
- exp.clear
391
- return "attr_writer :#{name.to_s[0..-2]}"
392
- end
393
- else
394
- raise "Unknown defn type: #{exp.inspect}"
395
- end
396
- end
385
+ _, name, args, *body = exp
397
386
 
398
387
  comm = exp.comments
399
- name = exp.shift
400
- args = process exp.shift
388
+ args = process args
401
389
  args = "" if args == "()"
402
390
 
403
- exp.shift if exp == s(s(:nil)) # empty it out of a default nil expression
404
-
405
- # REFACTOR: use process_block but get it happier wrt parenthesize
406
- body = []
407
- until exp.empty? do
408
- body << process(exp.shift)
391
+ body = s() if body == s(s(:nil)) # empty it out of a default nil expression
392
+
393
+ # s(:defn, name, args, ivar|iasgn)
394
+ case exp
395
+ when s{ s(:defn, atom, t(:args), s(:ivar, atom)) } then # TODO: atom -> _
396
+ _, ivar = body.first
397
+ ivar = ivar.to_s[1..-1] # remove leading @
398
+ reader = name.to_s
399
+ return "attr_reader #{name.inspect}" if reader == ivar
400
+ when s{ s(:defn, atom, t(:args), s(:iasgn, atom, s(:lvar, atom))) } then
401
+ _, ivar, _val = body.first
402
+ ivar = ivar.to_s[1..-1] # remove leading @
403
+ reader = name.to_s.chomp "="
404
+ return "attr_writer :#{reader}" if reader == ivar
409
405
  end
410
406
 
407
+ body = body.map { |ssexp|
408
+ process ssexp
409
+ }
410
+
411
411
  simple = body.size <= 1
412
412
 
413
413
  body << "# do nothing" if body.empty?
@@ -419,29 +419,36 @@ class Ruby2Ruby < SexpProcessor
419
419
  return "#{comm}def #{name}#{args}\n#{body}\nend".gsub(/\n\s*\n+/, "\n")
420
420
  end
421
421
 
422
- def process_defs(exp) # :nodoc:
423
- lhs = exp.shift
424
- var = [:self, :cvar, :dvar, :ivar, :gvar, :lvar].include? lhs.first
425
- name = exp.shift
422
+ def process_defs exp # :nodoc:
423
+ _, lhs, name, args, *body = exp
424
+ var = [:self, :cvar, :dvar, :ivar, :gvar, :lvar].include? lhs.sexp_type
426
425
 
427
- lhs = process(lhs)
426
+ lhs = process lhs
428
427
  lhs = "(#{lhs})" unless var
429
428
 
430
- exp.unshift "#{lhs}.#{name}"
431
- process_defn(exp)
429
+ name = "#{lhs}.#{name}"
430
+
431
+ process_defn s(:defn, name, args, *body)
432
432
  end
433
433
 
434
434
  def process_dot2(exp) # :nodoc:
435
- "(#{process exp.shift}..#{process exp.shift})"
435
+ _, lhs, rhs = exp
436
+
437
+ "(#{process lhs}..#{process rhs})"
436
438
  end
437
439
 
438
440
  def process_dot3(exp) # :nodoc:
439
- "(#{process exp.shift}...#{process exp.shift})"
441
+ _, lhs, rhs = exp
442
+
443
+ "(#{process lhs}...#{process rhs})"
440
444
  end
441
445
 
442
- def process_dregx(exp) # :nodoc:
443
- options = re_opt exp.pop if Fixnum === exp.last
444
- "/" << util_dthing(:dregx, exp) << "/#{options}"
446
+ def process_dregx exp # :nodoc:
447
+ _, str, *rest = exp
448
+
449
+ options = re_opt rest.pop if Integer === rest.last
450
+
451
+ "/" << util_dthing(:dregx, s(:dregx, str, *rest)) << "/#{options}"
445
452
  end
446
453
 
447
454
  def process_dregx_once(exp) # :nodoc:
@@ -461,8 +468,9 @@ class Ruby2Ruby < SexpProcessor
461
468
  end
462
469
 
463
470
  def process_ensure(exp) # :nodoc:
464
- body = process exp.shift
465
- ens = exp.shift
471
+ _, body, ens = exp
472
+
473
+ body = process body
466
474
  ens = nil if ens == s(:nil)
467
475
  ens = process(ens) || "# do nothing"
468
476
  ens = "begin\n#{ens}\nend\n" if ens =~ /(^|\n)rescue/
@@ -473,80 +481,91 @@ class Ruby2Ruby < SexpProcessor
473
481
  return "#{body}\nensure\n#{indent ens}"
474
482
  end
475
483
 
476
- def process_evstr(exp) # :nodoc:
477
- exp.empty? ? '' : process(exp.shift)
484
+ def process_evstr exp # :nodoc:
485
+ _, x = exp
486
+
487
+ x ? process(x) : ""
478
488
  end
479
489
 
480
- def process_false(exp) # :nodoc:
490
+ def process_false exp # :nodoc:
481
491
  "false"
482
492
  end
483
493
 
484
- def process_flip2(exp) # :nodoc:
485
- "#{process(exp.shift)}..#{process(exp.shift)}"
494
+ def process_flip2 exp # :nodoc:
495
+ _, lhs, rhs = exp
496
+
497
+ "#{process lhs}..#{process rhs}"
486
498
  end
487
499
 
488
- def process_flip3(exp) # :nodoc:
489
- "#{process(exp.shift)}...#{process(exp.shift)}"
500
+ def process_flip3 exp # :nodoc:
501
+ _, lhs, rhs = exp
502
+
503
+ "#{process lhs}...#{process rhs}"
490
504
  end
491
505
 
492
- def process_for(exp) # :nodoc:
493
- recv = process exp.shift
494
- iter = process exp.shift
495
- body = exp.empty? ? nil : process(exp.shift)
506
+ def process_for exp # :nodoc:
507
+ _, recv, iter, body = exp
508
+
509
+ recv = process recv
510
+ iter = process iter
511
+ body = process(body) || "# do nothing"
496
512
 
497
513
  result = ["for #{iter} in #{recv} do"]
498
- result << indent(body ? body : "# do nothing")
514
+ result << indent(body)
499
515
  result << "end"
500
516
 
501
- result.join("\n")
517
+ result.join "\n"
502
518
  end
503
519
 
504
- def process_gasgn(exp) # :nodoc:
505
- process_iasgn(exp)
520
+ def process_gasgn exp # :nodoc:
521
+ process_iasgn exp
506
522
  end
507
523
 
508
- def process_gvar(exp) # :nodoc:
509
- return exp.shift.to_s
524
+ def process_gvar exp # :nodoc:
525
+ _, name = exp
526
+
527
+ name.to_s
510
528
  end
511
529
 
512
530
  def process_hash(exp) # :nodoc:
513
- result = []
531
+ _, *pairs = exp
514
532
 
515
- until exp.empty?
516
- s = exp.shift
517
- t = s.sexp_type
518
- lhs = process s
533
+ result = []
519
534
 
520
- case t
521
- when :kwsplat then
522
- result << lhs
535
+ result = pairs.each_slice(2).map { |k, v|
536
+ if k.sexp_type == :kwsplat then
537
+ "%s" % process(k)
523
538
  else
524
- rhs = exp.shift
525
- t = rhs.first
526
- rhs = process rhs
539
+ t = v.sexp_type
540
+
541
+ lhs = process k
542
+ rhs = process v
527
543
  rhs = "(#{rhs})" unless HASH_VAL_NO_PAREN.include? t
528
544
 
529
- result << "#{lhs} => #{rhs}"
545
+ "%s => %s" % [lhs, rhs]
530
546
  end
531
- end
547
+ }
532
548
 
533
549
  return result.empty? ? "{}" : "{ #{result.join(', ')} }"
534
550
  end
535
551
 
536
552
  def process_iasgn(exp) # :nodoc:
537
- lhs = exp.shift
538
- if exp.empty? then # part of an masgn
553
+ _, lhs, rhs = exp
554
+
555
+ if rhs then
556
+ "#{lhs} = #{process rhs}"
557
+ else # part of an masgn
539
558
  lhs.to_s
540
- else
541
- "#{lhs} = #{process exp.shift}"
542
559
  end
543
560
  end
544
561
 
545
562
  def process_if(exp) # :nodoc:
546
- expand = Ruby2Ruby::ASSIGN_NODES.include? exp.first.first
547
- c = process exp.shift
548
- t = process exp.shift
549
- f = process exp.shift
563
+ _, c, t, f = exp
564
+
565
+ expand = Ruby2Ruby::ASSIGN_NODES.include? c.sexp_type
566
+ c = process c
567
+ t = process t
568
+ f = process f
550
569
 
551
570
  c = "(#{c.chomp})" if c =~ /\n/
552
571
 
@@ -579,9 +598,10 @@ class Ruby2Ruby < SexpProcessor
579
598
  end
580
599
 
581
600
  def process_iter(exp) # :nodoc:
582
- iter = process exp.shift
583
- args = exp.shift
584
- body = exp.empty? ? nil : process(exp.shift)
601
+ _, iter, args, body = exp
602
+
603
+ iter = process iter
604
+ body = process body if body
585
605
 
586
606
  args = case args
587
607
  when 0 then
@@ -624,21 +644,25 @@ class Ruby2Ruby < SexpProcessor
624
644
  end
625
645
 
626
646
  def process_ivar(exp) # :nodoc:
627
- exp.shift.to_s
647
+ _, name = exp
648
+ name.to_s
628
649
  end
629
650
 
630
651
  def process_kwsplat(exp)
631
- "**#{process exp.shift}"
652
+ _, kw = exp
653
+ "**#{process kw}"
632
654
  end
633
655
 
634
656
  def process_lasgn(exp) # :nodoc:
635
- s = "#{exp.shift}"
636
- s += " = #{process exp.shift}" unless exp.empty?
657
+ _, name, value = exp
658
+
659
+ s = "#{name}"
660
+ s += " = #{process value}" if value
637
661
  s
638
662
  end
639
663
 
640
- def process_lit(exp) # :nodoc:
641
- obj = exp.shift
664
+ def process_lit exp # :nodoc:
665
+ _, obj = exp
642
666
  case obj
643
667
  when Range then
644
668
  "(#{obj.inspect})"
@@ -648,23 +672,25 @@ class Ruby2Ruby < SexpProcessor
648
672
  end
649
673
 
650
674
  def process_lvar(exp) # :nodoc:
651
- exp.shift.to_s
675
+ _, name = exp
676
+ name.to_s
652
677
  end
653
678
 
654
679
  def process_masgn(exp) # :nodoc:
655
680
  # s(:masgn, s(:array, s(:lasgn, :var), ...), s(:to_ary, <val>, ...))
656
681
  # s(:iter, <call>, s(:args, s(:masgn, :a, :b)), <body>)
657
682
 
683
+ _, *exp = exp # HACK
684
+
658
685
  case exp.first
659
686
  when Sexp then
660
- lhs = exp.shift
661
- rhs = exp.empty? ? nil : exp.shift
687
+ lhs, rhs = exp
662
688
 
663
- case lhs.first
689
+ case lhs.sexp_type
664
690
  when :array then
665
- lhs.shift # node type
691
+ _, *lhs = lhs # HACK
666
692
  lhs = lhs.map do |l|
667
- case l.first
693
+ case l.sexp_type
668
694
  when :masgn then
669
695
  "(#{process(l)})"
670
696
  else
@@ -675,8 +701,8 @@ class Ruby2Ruby < SexpProcessor
675
701
  raise "no clue: #{lhs.inspect}"
676
702
  end
677
703
 
678
- unless rhs.nil? then
679
- t = rhs.first
704
+ if rhs then
705
+ t = rhs.sexp_type
680
706
  rhs = process rhs
681
707
  rhs = rhs[1..-2] if t == :array # FIX: bad? I dunno
682
708
  return "#{lhs.join(", ")} = #{rhs}"
@@ -692,20 +718,28 @@ class Ruby2Ruby < SexpProcessor
692
718
  end
693
719
  end
694
720
 
695
- def process_match(exp) # :nodoc:
696
- "#{process(exp.shift)}"
721
+ def process_match exp # :nodoc:
722
+ _, rhs = exp
723
+
724
+ "#{process rhs}"
697
725
  end
698
726
 
699
- def process_match2(exp) # :nodoc:
700
- lhs = process(exp.shift)
701
- rhs = process(exp.shift)
727
+ def process_match2 exp # :nodoc:
728
+ # s(:match2, s(:lit, /x/), s(:str, "blah"))
729
+ _, lhs, rhs = exp
730
+
731
+ lhs = process lhs
732
+ rhs = process rhs
733
+
702
734
  "#{lhs} =~ #{rhs}"
703
735
  end
704
736
 
705
- def process_match3(exp) # :nodoc:
706
- rhs = process(exp.shift)
707
- left_type = exp.first.sexp_type
708
- lhs = process(exp.shift)
737
+ def process_match3 exp # :nodoc:
738
+ _, rhs, lhs = exp # yes, backwards
739
+
740
+ left_type = lhs.sexp_type
741
+ lhs = process lhs
742
+ rhs = process rhs
709
743
 
710
744
  if ASSIGN_NODES.include? left_type then
711
745
  "(#{lhs}) =~ #{rhs}"
@@ -719,7 +753,9 @@ class Ruby2Ruby < SexpProcessor
719
753
  end
720
754
 
721
755
  def process_next(exp) # :nodoc:
722
- val = exp.empty? ? nil : process(exp.shift)
756
+ _, rhs = exp
757
+
758
+ val = rhs && process(rhs) # maybe push down into if and test rhs?
723
759
  if val then
724
760
  "next #{val}"
725
761
  else
@@ -732,40 +768,43 @@ class Ruby2Ruby < SexpProcessor
732
768
  end
733
769
 
734
770
  def process_not(exp) # :nodoc:
735
- "(not #{process exp.shift})"
771
+ _, sexp = exp
772
+ "(not #{process sexp})"
736
773
  end
737
774
 
738
775
  def process_nth_ref(exp) # :nodoc:
739
- "$#{exp.shift}"
776
+ _, n = exp
777
+ "$#{n}"
740
778
  end
741
779
 
742
- def process_op_asgn(exp) # :nodoc:
780
+ def process_op_asgn exp # :nodoc:
743
781
  # [[:lvar, :x], [:call, nil, :z, [:lit, 1]], :y, :"||"]
744
- lhs = process(exp.shift)
745
- rhs = process(exp.shift)
746
- index = exp.shift
747
- op = exp.shift
782
+ _, lhs, rhs, index, op = exp
783
+
784
+ lhs = process lhs
785
+ rhs = process rhs
748
786
 
749
787
  "#{lhs}.#{index} #{op}= #{rhs}"
750
788
  end
751
789
 
752
790
  def process_op_asgn1(exp) # :nodoc:
753
791
  # [[:lvar, :b], [:arglist, [:lit, 1]], :"||", [:lit, 10]]
754
- lhs = process(exp.shift)
755
- index = process(exp.shift)
756
- msg = exp.shift
757
- rhs = process(exp.shift)
792
+ _, lhs, index, msg, rhs = exp
793
+
794
+ lhs = process lhs
795
+ index = process index
796
+ rhs = process rhs
758
797
 
759
798
  "#{lhs}[#{index}] #{msg}= #{rhs}"
760
799
  end
761
800
 
762
- def process_op_asgn2(exp) # :nodoc:
801
+ def process_op_asgn2 exp # :nodoc:
763
802
  # [[:lvar, :c], :var=, :"||", [:lit, 20]]
764
- lhs = process(exp.shift)
765
- index = exp.shift.to_s[0..-2]
766
- msg = exp.shift
803
+ _, lhs, index, msg, rhs = exp
767
804
 
768
- rhs = process(exp.shift)
805
+ lhs = process lhs
806
+ index = index.to_s[0..-2]
807
+ rhs = process rhs
769
808
 
770
809
  "#{lhs}.#{index} #{msg}= #{rhs}"
771
810
  end
@@ -773,19 +812,21 @@ class Ruby2Ruby < SexpProcessor
773
812
  def process_op_asgn_and(exp) # :nodoc:
774
813
  # a &&= 1
775
814
  # [[:lvar, :a], [:lasgn, :a, [:lit, 1]]]
776
- exp.shift
777
- process(exp.shift).sub(/\=/, '&&=')
815
+ _, _lhs, rhs = exp
816
+ process(rhs).sub(/\=/, '&&=')
778
817
  end
779
818
 
780
819
  def process_op_asgn_or(exp) # :nodoc:
781
820
  # a ||= 1
782
821
  # [[:lvar, :a], [:lasgn, :a, [:lit, 1]]]
783
- exp.shift
784
- process(exp.shift).sub(/\=/, '||=')
822
+ _, _lhs, rhs = exp
823
+ process(rhs).sub(/\=/, '||=')
785
824
  end
786
825
 
787
826
  def process_or(exp) # :nodoc:
788
- "(#{process exp.shift} or #{process exp.shift})"
827
+ _, lhs, rhs = exp
828
+
829
+ "(#{process lhs} or #{process rhs})"
789
830
  end
790
831
 
791
832
  def process_postexe(exp) # :nodoc:
@@ -797,8 +838,13 @@ class Ruby2Ruby < SexpProcessor
797
838
  end
798
839
 
799
840
  def process_resbody exp # :nodoc:
800
- args = exp.shift
801
- body = finish(exp)
841
+ # s(:resbody, s(:array), s(:return, s(:str, "a")))
842
+ _, args, *body = exp
843
+
844
+ body = body.compact.map { |sexp|
845
+ process sexp
846
+ }
847
+
802
848
  body << "# do nothing" if body.empty?
803
849
 
804
850
  name = args.lasgn true
@@ -811,21 +857,25 @@ class Ruby2Ruby < SexpProcessor
811
857
  end
812
858
 
813
859
  def process_rescue exp # :nodoc:
814
- body = process(exp.shift) unless exp.first.first == :resbody
815
- els = process(exp.pop) unless exp.last.first == :resbody
860
+ _, *rest = exp
861
+ exp = nil
862
+
863
+ body = process rest.shift unless rest.first.sexp_type == :resbody
864
+ els = process rest.pop unless rest.last && rest.last.sexp_type == :resbody
816
865
 
817
866
  body ||= "# do nothing"
818
- simple = exp.size == 1 && exp.resbody.size <= 3 &&
819
- !exp.resbody.block &&
820
- !exp.resbody.return
821
-
822
- resbodies = []
823
- until exp.empty? do
824
- resbody = exp.shift
825
- simple &&= resbody[1] == s(:array)
826
- simple &&= resbody[2] != nil && resbody[2].node_type != :block
827
- resbodies << process(resbody)
828
- end
867
+
868
+ # TODO: I don't like this using method_missing, but I need to ensure tests
869
+ simple = rest.size == 1 && rest.first.size <= 3 &&
870
+ !rest.first.block &&
871
+ !rest.first.return
872
+
873
+ resbodies = rest.map { |resbody|
874
+ _, rb_args, rb_body, *rb_rest = resbody
875
+ simple &&= rb_args == s(:array)
876
+ simple &&= rb_rest.empty? && rb_body && rb_body.node_type != :block
877
+ process resbody
878
+ }
829
879
 
830
880
  if els then
831
881
  "#{indent body}\n#{resbodies.join("\n")}\nelse\n#{indent els}"
@@ -841,11 +891,12 @@ class Ruby2Ruby < SexpProcessor
841
891
  "retry"
842
892
  end
843
893
 
844
- def process_return(exp) # :nodoc:
845
- if exp.empty? then
894
+ def process_return exp # :nodoc:
895
+ _, rhs = exp
896
+
897
+ unless rhs then
846
898
  "return"
847
899
  else
848
- rhs = exp.shift
849
900
  rhs_type = rhs.sexp_type
850
901
  rhs = process rhs
851
902
  rhs = "(#{rhs})" if ASSIGN_NODES.include? rhs_type
@@ -853,47 +904,53 @@ class Ruby2Ruby < SexpProcessor
853
904
  end
854
905
  end
855
906
 
856
- def process_safe_attrasgn(exp) # :nodoc:
857
- receiver = process exp.shift
858
- name = exp.shift
859
- rhs = exp.pop
860
- args = exp.pop # should be nil
861
- exp.clear
907
+ def process_safe_attrasgn exp # :nodoc:
908
+ _, receiver, name, *rest = exp
909
+
910
+ receiver = process receiver
911
+ rhs = rest.pop
912
+ args = rest.pop # should be nil
862
913
 
863
914
  raise "dunno what to do: #{args.inspect}" if args
864
915
 
865
916
  name = name.to_s.sub(/=$/, '')
866
917
 
867
918
  if rhs && rhs != s(:arglist) then
868
- "#{receiver}&.#{name} = #{process(rhs)}"
919
+ "#{receiver}&.#{name} = #{process rhs}"
869
920
  else
870
921
  raise "dunno what to do: #{rhs.inspect}"
871
922
  end
872
923
  end
873
924
 
874
- def process_safe_op_asgn(exp) # :nodoc:
925
+ def process_safe_op_asgn exp # :nodoc:
875
926
  # [[:lvar, :x], [:call, nil, :z, [:lit, 1]], :y, :"||"]
876
- lhs = process(exp.shift)
877
- rhs = process(exp.shift)
878
- index = exp.shift
879
- op = exp.shift
927
+ _, lhs, rhs, index, op = exp
928
+
929
+ lhs = process lhs
930
+ rhs = process rhs
880
931
 
881
932
  "#{lhs}&.#{index} #{op}= #{rhs}"
882
933
  end
883
934
 
884
935
  def process_safe_op_asgn2(exp) # :nodoc:
885
936
  # [[:lvar, :c], :var=, :"||", [:lit, 20]]
886
- lhs = process(exp.shift)
887
- index = exp.shift.to_s[0..-2]
888
- msg = exp.shift
889
937
 
890
- rhs = process(exp.shift)
938
+ _, lhs, index, msg, rhs = exp
939
+
940
+ lhs = process lhs
941
+ index = index.to_s[0..-2]
942
+ rhs = process rhs
891
943
 
892
944
  "#{lhs}&.#{index} #{msg}= #{rhs}"
893
945
  end
894
946
 
895
947
  def process_sclass(exp) # :nodoc:
896
- "class << #{process(exp.shift)}\n#{indent(process_block(exp))}\nend"
948
+ _, recv, *block = exp
949
+
950
+ recv = process recv
951
+ block = indent process_block s(:block, *block)
952
+
953
+ "class << #{recv}\n#{block}\nend"
897
954
  end
898
955
 
899
956
  def process_self(exp) # :nodoc:
@@ -901,33 +958,41 @@ class Ruby2Ruby < SexpProcessor
901
958
  end
902
959
 
903
960
  def process_splat(exp) # :nodoc:
904
- if exp.empty? then
961
+ _, arg = exp
962
+ if arg.nil? then
905
963
  "*"
906
964
  else
907
- "*#{process(exp.shift)}"
965
+ "*#{process arg}"
908
966
  end
909
967
  end
910
968
 
911
969
  def process_str(exp) # :nodoc:
912
- return exp.shift.dump
970
+ _, s = exp
971
+ s.dump
913
972
  end
914
973
 
915
974
  def process_super(exp) # :nodoc:
916
- args = finish exp
975
+ _, *args = exp
917
976
 
918
- "super(#{args.join(', ')})"
977
+ args = args.map { |arg|
978
+ process arg
979
+ }
980
+
981
+ "super(#{args.join ", "})"
919
982
  end
920
983
 
921
984
  def process_svalue(exp) # :nodoc:
922
- code = []
923
- until exp.empty? do
924
- code << process(exp.shift)
925
- end
926
- code.join(", ")
985
+ _, *args = exp
986
+
987
+ args.map { |arg|
988
+ process arg
989
+ }.join ", "
927
990
  end
928
991
 
929
- def process_to_ary(exp) # :nodoc:
930
- process(exp.shift)
992
+ def process_to_ary exp # :nodoc:
993
+ _, sexp = exp
994
+
995
+ process sexp
931
996
  end
932
997
 
933
998
  def process_true(exp) # :nodoc:
@@ -935,38 +1000,42 @@ class Ruby2Ruby < SexpProcessor
935
1000
  end
936
1001
 
937
1002
  def process_undef(exp) # :nodoc:
938
- "undef #{process(exp.shift)}"
1003
+ _, name = exp
1004
+
1005
+ "undef #{process name}"
939
1006
  end
940
1007
 
941
1008
  def process_until(exp) # :nodoc:
942
1009
  cond_loop(exp, 'until')
943
1010
  end
944
1011
 
945
- def process_valias(exp) # :nodoc:
946
- "alias #{exp.shift} #{exp.shift}"
1012
+ def process_valias exp # :nodoc:
1013
+ _, lhs, rhs = exp
1014
+
1015
+ "alias #{lhs} #{rhs}"
947
1016
  end
948
1017
 
949
- def process_when(exp) # :nodoc:
950
- src = []
1018
+ def process_when exp # :nodoc:
1019
+ s(:when, s(:array, s(:lit, 1)),
1020
+ s(:call, nil, :puts, s(:str, "something")),
1021
+ s(:lasgn, :result, s(:str, "red")))
951
1022
 
952
- if self.context[1] == :array then # ugh. matz! why not an argscat?!?
953
- val = process(exp.shift)
954
- exp.shift # empty body
955
- return "*#{val}"
956
- end
1023
+ _, lhs, *rhs = exp
957
1024
 
958
- until exp.empty?
959
- cond = process(exp.shift).to_s[1..-2]
960
- code = indent(finish(exp).join("\n"))
961
- code = indent "# do nothing" if code =~ /\A\s*\Z/
962
- src << "when #{cond} then\n#{code.chomp}"
963
- end
1025
+ cond = process(lhs)[1..-2]
1026
+
1027
+ rhs = rhs.compact.map { |sexp|
1028
+ indent process sexp
1029
+ }
964
1030
 
965
- src.join("\n")
1031
+ rhs << indent("# do nothing") if rhs.empty?
1032
+ rhs = rhs.join "\n"
1033
+
1034
+ "when #{cond} then\n#{rhs.chomp}"
966
1035
  end
967
1036
 
968
1037
  def process_while(exp) # :nodoc:
969
- cond_loop(exp, 'while')
1038
+ cond_loop exp, "while"
970
1039
  end
971
1040
 
972
1041
  def process_xstr(exp) # :nodoc:
@@ -974,10 +1043,11 @@ class Ruby2Ruby < SexpProcessor
974
1043
  end
975
1044
 
976
1045
  def process_yield(exp) # :nodoc:
977
- args = []
978
- until exp.empty? do
979
- args << process(exp.shift)
980
- end
1046
+ _, *args = exp
1047
+
1048
+ args = args.map { |arg|
1049
+ process arg
1050
+ }
981
1051
 
982
1052
  unless args.empty? then
983
1053
  "yield(#{args.join(', ')})"
@@ -995,8 +1065,9 @@ class Ruby2Ruby < SexpProcessor
995
1065
 
996
1066
  def rewrite_attrasgn exp # :nodoc:
997
1067
  if context.first(2) == [:array, :masgn] then
998
- exp[0] = :call
999
- exp[2] = exp[2].to_s.sub(/=$/, '').to_sym
1068
+ _, recv, msg, *args = exp
1069
+
1070
+ exp = s(:call, recv, msg.to_s.chomp("=").to_sym, *args)
1000
1071
  end
1001
1072
 
1002
1073
  exp
@@ -1008,8 +1079,9 @@ class Ruby2Ruby < SexpProcessor
1008
1079
  end
1009
1080
 
1010
1081
  def rewrite_resbody exp # :nodoc:
1011
- raise "no exception list in #{exp.inspect}" unless exp.size > 2 && exp[1]
1012
- raise exp[1].inspect if exp[1][0] != :array
1082
+ _, args, *_rest = exp
1083
+ raise "no exception list in #{exp.inspect}" unless exp.size > 2 && args
1084
+ raise args.inspect if args.sexp_type != :array
1013
1085
  # for now, do nothing, just check and freak if we see an errant structure
1014
1086
  exp
1015
1087
  end
@@ -1019,9 +1091,10 @@ class Ruby2Ruby < SexpProcessor
1019
1091
  complex ||= exp.size > 3
1020
1092
  complex ||= exp.resbody.block
1021
1093
  complex ||= exp.resbody.size > 3
1022
- complex ||= exp.find_nodes(:resbody).any? { |n| n[1] != s(:array) }
1023
- complex ||= exp.find_nodes(:resbody).any? { |n| n.last.nil? }
1024
- complex ||= exp.find_nodes(:resbody).any? { |n| n[2] and n[2].node_type == :block }
1094
+ resbodies = exp.find_nodes(:resbody)
1095
+ complex ||= resbodies.any? { |n| n[1] != s(:array) }
1096
+ complex ||= resbodies.any? { |n| n.last.nil? }
1097
+ complex ||= resbodies.any? { |(_, _, body)| body and body.node_type == :block }
1025
1098
 
1026
1099
  handled = context.first == :ensure
1027
1100
 
@@ -1031,9 +1104,9 @@ class Ruby2Ruby < SexpProcessor
1031
1104
  end
1032
1105
 
1033
1106
  def rewrite_svalue(exp) # :nodoc:
1034
- case exp.last.first
1107
+ case exp.last.sexp_type
1035
1108
  when :array
1036
- s(:svalue, *exp[1][1..-1])
1109
+ s(:svalue, *exp[1].sexp_body)
1037
1110
  when :splat
1038
1111
  exp
1039
1112
  else
@@ -1048,9 +1121,10 @@ class Ruby2Ruby < SexpProcessor
1048
1121
  # Generate a post-or-pre conditional loop.
1049
1122
 
1050
1123
  def cond_loop(exp, name)
1051
- cond = process(exp.shift)
1052
- body = process(exp.shift)
1053
- head_controlled = exp.shift
1124
+ _, cond, body, head_controlled = exp
1125
+
1126
+ cond = process cond
1127
+ body = process body
1054
1128
 
1055
1129
  body = indent(body).chomp if body
1056
1130
 
@@ -1064,6 +1138,7 @@ class Ruby2Ruby < SexpProcessor
1064
1138
  code << body if body
1065
1139
  code << "end #{name} #{cond}"
1066
1140
  end
1141
+
1067
1142
  code.join("\n")
1068
1143
  end
1069
1144
 
@@ -1084,22 +1159,10 @@ class Ruby2Ruby < SexpProcessor
1084
1159
  end
1085
1160
  end
1086
1161
 
1087
- ##
1088
- # Process all the remaining stuff in +exp+ and return the results
1089
- # sans-nils.
1090
-
1091
- def finish exp # REFACTOR: work this out of the rest of the processors
1092
- body = []
1093
- until exp.empty? do
1094
- body << process(exp.shift)
1095
- end
1096
- body.compact
1097
- end
1098
-
1099
1162
  ##
1100
1163
  # Indent all lines of +s+ to the current indent level.
1101
1164
 
1102
- def indent(s)
1165
+ def indent s
1103
1166
  s.to_s.split(/\n/).map{|line| @indent + line}.join("\n")
1104
1167
  end
1105
1168
 
@@ -1135,29 +1198,28 @@ class Ruby2Ruby < SexpProcessor
1135
1198
  # Utility method to generate something interpolated.
1136
1199
 
1137
1200
  def util_dthing(type, exp)
1138
- s = []
1201
+ _, str, *rest = exp
1139
1202
 
1140
1203
  # first item in sexp is a string literal
1141
- s << dthing_escape(type, exp.shift)
1204
+ str = dthing_escape(type, str)
1142
1205
 
1143
- until exp.empty?
1144
- pt = exp.shift
1206
+ rest = rest.map { |pt|
1145
1207
  case pt
1146
- when Sexp then
1147
- case pt.first
1208
+ when Sexp then # TODO: what the fuck? why??
1209
+ case pt.sexp_type
1148
1210
  when :str then
1149
- s << dthing_escape(type, pt.last)
1211
+ dthing_escape(type, pt.last)
1150
1212
  when :evstr then
1151
- s << '#{' << process(pt) << '}' # do not use interpolation here
1213
+ '#{%s}' % [process(pt)]
1152
1214
  else
1153
1215
  raise "unknown type: #{pt.inspect}"
1154
1216
  end
1155
1217
  else
1156
1218
  raise "unhandled value in d-thing: #{pt.inspect}"
1157
1219
  end
1158
- end
1220
+ }
1159
1221
 
1160
- s.join
1222
+ [str, rest].join
1161
1223
  end
1162
1224
 
1163
1225
  ##
@@ -1166,29 +1228,30 @@ class Ruby2Ruby < SexpProcessor
1166
1228
  def util_module_or_class(exp, is_class=false)
1167
1229
  result = []
1168
1230
 
1169
- name = exp.shift
1231
+ _, name, *body = exp
1232
+ superk = body.shift if is_class
1233
+
1170
1234
  name = process name if Sexp === name
1171
1235
 
1172
1236
  result << name
1173
1237
 
1174
- if is_class then
1175
- superk = process(exp.shift)
1238
+ if superk then
1239
+ superk = process superk
1176
1240
  result << " < #{superk}" if superk
1177
1241
  end
1178
1242
 
1179
1243
  result << "\n"
1180
1244
 
1181
- body = []
1182
- begin
1183
- code = process(exp.shift) unless exp.empty?
1184
- body << code.chomp unless code.nil? or code.chomp.empty?
1185
- end until exp.empty?
1245
+ body = body.map { |sexp|
1246
+ process(sexp).chomp
1247
+ }
1186
1248
 
1187
1249
  unless body.empty? then
1188
1250
  body = indent(body.join("\n\n")) + "\n"
1189
1251
  else
1190
1252
  body = ""
1191
1253
  end
1254
+
1192
1255
  result << body
1193
1256
  result << "end"
1194
1257