ruby2ruby 1.1.4 → 1.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/History.txt +15 -0
  2. data/lib/ruby2ruby.rb +77 -23
  3. metadata +3 -3
data/History.txt CHANGED
@@ -1,3 +1,18 @@
1
+ == 1.1.5 / 2007-02-13
2
+
3
+ * 3 minor enhancements:
4
+ * Can now heckle ActiveRecord::Base in full.
5
+ * Cleaned up 1-liner generating code.
6
+ * Made clean/simple rescues 1-liners.
7
+ * 7 bug fixes:
8
+ * Finally got the rest of block_pass working.
9
+ * Fixed block_pass on procs in iters. UGH!
10
+ * Fixed attrasgn in masgn.
11
+ * Fixed splat in masgn.
12
+ * Fixed unary/prefix methods.
13
+ * Fixed attrasgn for []= where there were multiple args inside [].
14
+ * Fixed a couple resbody bugs.
15
+
1
16
  == 1.1.4 / 2007-01-15
2
17
 
3
18
  * 4 minor enhancements:
data/lib/ruby2ruby.rb CHANGED
@@ -11,7 +11,8 @@ class NilClass # Objective-C trick
11
11
  end
12
12
 
13
13
  class RubyToRuby < SexpProcessor
14
- VERSION = '1.1.4'
14
+ VERSION = '1.1.5'
15
+ LINE_LENGTH = 78
15
16
 
16
17
  def self.translate(klass_or_str, method = nil)
17
18
  self.new.process(ParseTree.translate(klass_or_str, method))
@@ -119,11 +120,15 @@ class RubyToRuby < SexpProcessor
119
120
 
120
121
  case name
121
122
  when :[]= then
122
- lhs = process args.delete_at(1)
123
+ rhs = process args.pop
123
124
  args[0] = :arglist
124
- "#{receiver}[#{lhs}] = #{process(args)}"
125
+ "#{receiver}[#{process(args)}] = #{rhs}"
125
126
  else
126
- "#{receiver}.#{name.to_s[0..-2]} = #{process(args)[1..-2]}"
127
+ if args then
128
+ "#{receiver}.#{name.to_s[0..-2]} = #{process(args)[1..-2]}"
129
+ else
130
+ "#{receiver}.#{name.to_s[0..-2]}"
131
+ end
127
132
  end
128
133
  end
129
134
 
@@ -164,11 +169,22 @@ class RubyToRuby < SexpProcessor
164
169
  end
165
170
 
166
171
  def process_block_pass(exp)
167
- bname = [:lvar, "&" + process(exp.shift)]
172
+ bname = [:block_arg, process(exp.shift)]
168
173
  call = exp.shift
169
- has_args = Array === call.last and call.last.first == :array
170
- call << [:array] unless has_args
171
- call.last << bname
174
+
175
+ if Array === call.last then # HACK - I _really_ need rewrites to happen first
176
+ case call.last.first
177
+ when :splat then
178
+ call << [:array, call.pop]
179
+ when :array then
180
+ # do nothing
181
+ else
182
+ call << [:array] unless has_args
183
+ end
184
+ call.last << bname
185
+ else
186
+ call << [:array, bname]
187
+ end
172
188
 
173
189
  process(call)
174
190
  end
@@ -193,6 +209,10 @@ class RubyToRuby < SexpProcessor
193
209
  "(#{receiver} #{name} #{args})"
194
210
  when :[] then
195
211
  "#{receiver}[#{args}]"
212
+ when :"-@" then
213
+ "-#{receiver}"
214
+ when :"+@" then
215
+ "+#{receiver}"
196
216
  else
197
217
  unless receiver.nil? then
198
218
  "#{receiver}.#{name}#{args ? "(#{args})" : args}"
@@ -449,7 +469,7 @@ class RubyToRuby < SexpProcessor
449
469
  if t then
450
470
  unless f then
451
471
  r = "#{t} if #{c}"
452
- return r if (@indent+r).size < 78 and r !~ /\n/
472
+ return r if (@indent+r).size < LINE_LENGTH and r !~ /\n/
453
473
  end
454
474
  r = "if #{c} then\n#{indent(t)}\n"
455
475
  r << "else\n#{indent(f)}\n" if f
@@ -457,7 +477,7 @@ class RubyToRuby < SexpProcessor
457
477
  r
458
478
  else
459
479
  r = "#{f} unless #{c}"
460
- return r if (@indent+r).size < 78 and r !~ /\n/
480
+ return r if (@indent+r).size < LINE_LENGTH and r !~ /\n/
461
481
  "unless #{c} then\n#{indent(f)}\nend"
462
482
  end
463
483
  end
@@ -475,6 +495,19 @@ class RubyToRuby < SexpProcessor
475
495
 
476
496
  iter.sub!(/\(\)$/, '')
477
497
 
498
+ # REFACTOR: ugh
499
+ result = []
500
+ result << "#{iter} {"
501
+ result << " |#{args}|" if args
502
+ if body then
503
+ result << " #{body.strip} "
504
+ else
505
+ result << ' '
506
+ end
507
+ result << "}"
508
+ result = result.join
509
+ return result if result !~ /\n/ and result.size < LINE_LENGTH
510
+
478
511
  result = []
479
512
  result << "#{iter} #{b}"
480
513
  result << " |#{args}|" if args
@@ -512,13 +545,29 @@ class RubyToRuby < SexpProcessor
512
545
  exp.shift.to_s
513
546
  end
514
547
 
548
+ def splat(sym)
549
+ :"*#{sym}"
550
+ end
551
+
515
552
  def process_masgn(exp)
516
553
  lhs = exp.shift
517
- rhs = exp.shift rescue nil
554
+ rhs = exp.empty? ? nil : exp.shift
555
+
556
+ unless exp.empty? then
557
+ rhs[-1] = splat(rhs[-1])
558
+ lhs << rhs
559
+ rhs = exp.shift
560
+ end
518
561
 
519
- assert_type lhs, :array
520
- lhs.shift
521
- lhs = lhs.map { |l| process(l) }
562
+ case lhs.first
563
+ when :array then
564
+ lhs.shift
565
+ lhs = lhs.map { |l| process(l) }
566
+ when :dasgn_curr then
567
+ lhs = [ splat(lhs.last) ]
568
+ else
569
+ raise "no clue: #{lhs.inspect}"
570
+ end
522
571
 
523
572
  unless rhs.nil? then
524
573
  # HACK - but seems to work (see to_ary test) assert_type rhs, :array
@@ -622,15 +671,14 @@ class RubyToRuby < SexpProcessor
622
671
  list = sexp.shift
623
672
  body = sexp.shift
624
673
 
625
- var = if list.last.first == :lasgn then
674
+ var = if list and list.last.first == :lasgn then
626
675
  list.pop[1]
627
676
  else
628
677
  nil
629
678
  end
630
679
 
631
- list[0] = :arglist
632
-
633
680
  if list then
681
+ list[0] = :arglist
634
682
  code << "rescue #{process(list)}"
635
683
  else
636
684
  code << "rescue"
@@ -654,6 +702,7 @@ class RubyToRuby < SexpProcessor
654
702
  end
655
703
 
656
704
  def process_rescue(exp)
705
+ # TODO: rewrite this
657
706
  # TODO: proper formatting depends on knowing the context
658
707
  #
659
708
  # a = b rescue c => [lasgn a [rescue b c]]
@@ -673,9 +722,14 @@ class RubyToRuby < SexpProcessor
673
722
  code << "else"
674
723
  code << indent(els)
675
724
  else
676
- code << "end\n" unless stack.first == "process_block"
725
+ unless stack.first == "process_block" then
726
+ code << "end\n"
727
+ else
728
+ r = [body, resbody.gsub(/rescue\n\s+/, 'rescue ')].join(' ')
729
+ code = [r] if (@indent+r).size < LINE_LENGTH and r !~ /\n/
730
+ end
677
731
  end
678
- code.join("\n")
732
+ code.join("\n").chomp
679
733
  else # a rescue b and others
680
734
  body = process exp.shift
681
735
  assert_type exp.first, :resbody
@@ -842,7 +896,7 @@ class RubyToRuby < SexpProcessor
842
896
  assert_type body, :scope
843
897
  assert_type body[1], :block
844
898
  when :scope, :fbody then
845
- body = body[1] if body.first == :fbody
899
+ body = body.pop if body.first == :fbody
846
900
  case body.first
847
901
  when :scope then
848
902
  args = body.block.args(true)
@@ -858,12 +912,12 @@ class RubyToRuby < SexpProcessor
858
912
  body.block.delete_at(1) # nuke the decl # REFACTOR
859
913
  masgn = body.masgn(true)
860
914
  if masgn then
861
- splat = :"*#{masgn[-1][-1]}"
915
+ splat = self.splat(masgn[-1][-1])
862
916
  args.push(splat)
863
917
  else
864
918
  dasgn_curr = body.dasgn_curr(true)
865
919
  if dasgn_curr then
866
- arg = :"*#{dasgn_curr[-1]}"
920
+ arg = self.splat(dasgn_curr[-1])
867
921
  args.push(arg)
868
922
  end
869
923
  end
@@ -887,7 +941,7 @@ class RubyToRuby < SexpProcessor
887
941
  dasgn = body.masgn(true)
888
942
  # DAMNIT body.block.dasgn_curr(true) - multiple values so can't use
889
943
  body.block.delete_at(1) # nuke the decl
890
- splat = :"*#{dasgn[-1][-1]}"
944
+ splat = self.splat(dasgn[-1][-1])
891
945
  args.push(splat)
892
946
  body.find_and_replace_all(:dvar, :lvar)
893
947
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0.9
3
3
  specification_version: 1
4
4
  name: ruby2ruby
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.1.4
7
- date: 2007-01-15 00:00:00 -08:00
6
+ version: 1.1.5
7
+ date: 2007-02-13 00:00:00 -08:00
8
8
  summary: ruby2ruby provides a means of generating pure ruby code easily from ParseTree's Sexps.
9
9
  require_paths:
10
10
  - lib
@@ -64,5 +64,5 @@ dependencies:
64
64
  requirements:
65
65
  - - ">="
66
66
  - !ruby/object:Gem::Version
67
- version: 1.1.7
67
+ version: 1.2.0
68
68
  version: