ParseTree 3.0.1-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,69 @@
1
+ class Method
2
+ def with_class_and_method_name
3
+ if self.inspect =~ /<Method: (.*)\#(.*)>/ then
4
+ klass = eval $1
5
+ method = $2.intern
6
+ raise "Couldn't determine class from #{self.inspect}" if klass.nil?
7
+ return yield(klass, method)
8
+ else
9
+ raise "Can't parse signature: #{self.inspect}"
10
+ end
11
+ end
12
+
13
+ def to_sexp
14
+ require 'parse_tree'
15
+ require 'unified_ruby'
16
+ parser = ParseTree.new(false)
17
+ unifier = Unifier.new
18
+ with_class_and_method_name do |klass, method|
19
+ old_sexp = parser.parse_tree_for_method(klass, method)
20
+ unifier.process(old_sexp) # HACK
21
+ end
22
+ end
23
+
24
+ def to_ruby
25
+ sexp = self.to_sexp
26
+ Ruby2Ruby.new.process sexp
27
+ end
28
+ end
29
+
30
+ class ProcStoreTmp
31
+ @@n = 0
32
+ def self.new_name
33
+ @@n += 1
34
+ return :"myproc#{@@n}"
35
+ end
36
+ end
37
+
38
+ class UnboundMethod
39
+ def to_ruby
40
+ name = ProcStoreTmp.new_name
41
+ ProcStoreTmp.send(:define_method, name, self)
42
+ m = ProcStoreTmp.new.method(name)
43
+ result = m.to_ruby.sub(/def #{name}(?:\(([^\)]*)\))?/,
44
+ 'proc { |\1|').sub(/end\Z/, '}')
45
+ return result
46
+ end
47
+ end
48
+
49
+ class Proc
50
+ def to_method
51
+ name = ProcStoreTmp.new_name
52
+ ProcStoreTmp.send(:define_method, name, self)
53
+ ProcStoreTmp.new.method(name)
54
+ end
55
+
56
+ def to_sexp
57
+ sexp = self.to_method.to_sexp
58
+ body = sexp.scope.block
59
+ args = sexp.args
60
+ args = nil if args.size == 1
61
+ body = body[1] if body.size == 2
62
+
63
+ s(:iter, s(:call, nil, :proc, s(:arglist)), args, body)
64
+ end
65
+
66
+ def to_ruby
67
+ Ruby2Ruby.new.process(self.to_sexp).sub(/^\Aproc do/, 'proc {').sub(/end\Z/, '}')
68
+ end
69
+ end
@@ -0,0 +1,315 @@
1
+ require 'sexp_processor'
2
+
3
+ $TESTING ||= false
4
+
5
+ module UnifiedRuby
6
+ def process exp
7
+ exp = Sexp.from_array exp unless Sexp === exp or exp.nil?
8
+ super
9
+ end
10
+
11
+ def rewrite_attrasgn(exp)
12
+ last = exp.last
13
+
14
+ if Sexp === last then
15
+ last[0] = :arglist if last[0] == :array
16
+ else
17
+ exp << s(:arglist)
18
+ end
19
+
20
+ exp
21
+ end
22
+
23
+ def rewrite_bmethod(exp)
24
+ exp[0] = :scope
25
+
26
+ has_splat = exp.masgn.array.splat.lasgn rescue false
27
+
28
+ args =
29
+ if has_splat then
30
+ arg = exp.masgn(true).array.splat.lasgn.sexp_body
31
+ raise "nope: #{arg.size}" unless arg.size == 1
32
+ s(:args, :"*#{arg.last}")
33
+ else
34
+ args = exp.lasgn(true)
35
+ if args then
36
+ s(:args, *args.sexp_body)
37
+ else
38
+ exp.delete_at 1 # nil
39
+ s(:args)
40
+ end
41
+ end
42
+
43
+ exp = s(:scope, s(:block, *exp.sexp_body)) unless exp.block
44
+ exp.block.insert 1, args
45
+ exp.find_and_replace_all(:dvar, :lvar)
46
+
47
+ exp
48
+ end
49
+
50
+ def rewrite_argscat exp
51
+ _, ary, val = exp
52
+ ary = s(:array, ary) unless ary.first == :array
53
+ ary << s(:splat, val)
54
+ end
55
+
56
+ def rewrite_argspush exp
57
+ exp[0] = :arglist
58
+ exp
59
+ end
60
+
61
+ def rewrite_block_pass exp
62
+ if exp.size == 3 then
63
+ _, block, recv = exp
64
+ case recv.first
65
+ when :super then
66
+ recv << s(:block_pass, block)
67
+ exp = recv
68
+ when :call then
69
+ recv.last << s(:block_pass, block)
70
+ exp = recv
71
+ else
72
+ raise "huh?: #{recv.inspect}"
73
+ end
74
+ end
75
+
76
+ exp
77
+ end
78
+
79
+ def rewrite_call(exp)
80
+ args = exp.last
81
+ case args
82
+ when nil
83
+ exp.pop
84
+ when Array
85
+ case args.first
86
+ when :array, :arglist then
87
+ args[0] = :arglist
88
+ when :argscat, :splat then
89
+ exp[-1] = s(:arglist, args)
90
+ else
91
+ raise "unknown type in call #{args.first.inspect} in #{exp.inspect}"
92
+ end
93
+ return exp
94
+ end
95
+
96
+ exp << s(:arglist)
97
+
98
+ exp
99
+ end
100
+
101
+ def rewrite_dasgn(exp)
102
+ exp[0] = :lasgn
103
+ exp
104
+ end
105
+
106
+ alias :rewrite_dasgn_curr :rewrite_dasgn
107
+
108
+ ##
109
+ # :defn is one of the most complex of all the ASTs in ruby. We do
110
+ # one of 3 different translations:
111
+ #
112
+ # 1) From:
113
+ #
114
+ # s(:defn, :name, s(:scope, s(:block, s(:args, ...), ...)))
115
+ # s(:defn, :name, s(:bmethod, s(:masgn, s(:dasgn_curr, :args)), s(:block, ...)))
116
+ # s(:defn, :name, s(:fbody, s(:bmethod, s(:masgn, s(:dasgn_curr, :splat)), s(:block, ...))))
117
+ #
118
+ # to:
119
+ #
120
+ # s(:defn, :name, s(:args, ...), s(:scope, s:(block, ...)))
121
+ #
122
+ # 2) From:
123
+ #
124
+ # s(:defn, :writer=, s(:attrset, :@name))
125
+ #
126
+ # to:
127
+ #
128
+ # s(:defn, :writer=, s(:args), s(:attrset, :@name))
129
+ #
130
+ # 3) From:
131
+ #
132
+ # s(:defn, :reader, s(:ivar, :@name))
133
+ #
134
+ # to:
135
+ #
136
+ # s(:defn, :reader, s(:args), s(:ivar, :@name))
137
+ #
138
+
139
+ def rewrite_defn(exp)
140
+ weirdo = exp.ivar || exp.attrset
141
+
142
+ fbody = exp.fbody(true)
143
+ exp.push(fbody.scope) if fbody
144
+
145
+ args = exp.scope.block.args(true) unless weirdo
146
+ exp.insert 2, args if args
147
+
148
+ # move block_arg up and in
149
+ block_arg = exp.scope.block.block_arg(true) rescue nil
150
+ if block_arg
151
+ block = args.block(true)
152
+ args << :"&#{block_arg.last}"
153
+ args << block if block
154
+ end
155
+
156
+ # patch up attr_accessor methods
157
+ if weirdo then
158
+ case
159
+ when exp.ivar then
160
+ exp.insert 2, s(:args)
161
+ when exp.attrset then
162
+ exp.insert 2, s(:args, :arg)
163
+ else
164
+ raise "unknown wierdo: #{wierdo.inpsect}"
165
+ end
166
+ end
167
+
168
+ exp
169
+ end
170
+
171
+ def rewrite_defs(exp)
172
+ receiver = exp.delete_at 1
173
+
174
+ # TODO: I think this would be better as rewrite_scope, but that breaks others
175
+ exp = s(exp.shift, exp.shift,
176
+ s(:scope,
177
+ s(:block, exp.scope.args))) if exp.scope.args
178
+
179
+ result = rewrite_defn(exp)
180
+ result.insert 1, receiver
181
+
182
+ result
183
+ end
184
+
185
+ def rewrite_dmethod(exp)
186
+ exp.shift # type
187
+ exp.shift # dmethod name
188
+ exp.shift # scope / block / body
189
+ end
190
+
191
+ def rewrite_dvar(exp)
192
+ exp[0] = :lvar
193
+ exp
194
+ end
195
+
196
+ def rewrite_fcall(exp)
197
+ exp[0] = :call
198
+ exp.insert 1, nil
199
+ exp.push nil if exp.size <= 3
200
+
201
+ rewrite_call(exp)
202
+ end
203
+
204
+ def rewrite_masgn(exp)
205
+ raise "wtf: #{exp}" unless exp.size == 4
206
+
207
+ t, lhs, lhs_splat, rhs = exp
208
+
209
+ lhs ||= s(:array)
210
+
211
+ if lhs_splat then
212
+ lhs_splat = s(:splat, lhs_splat) unless lhs_splat[0] == :splat
213
+ lhs << lhs_splat
214
+ end
215
+
216
+ s(t, lhs, rhs).compact
217
+ end
218
+
219
+ def rewrite_op_asgn1(exp)
220
+ exp[2][0] = :arglist # if exp[2][0] == :array
221
+ exp
222
+ end
223
+
224
+ def rewrite_rescue(exp)
225
+ # SKETCHY HACK return exp if exp.size > 4
226
+ ignored = exp.shift
227
+ body = exp.shift unless exp.first.first == :resbody
228
+ resbody = exp.shift
229
+ els = exp.shift unless exp.first.first == :resbody unless exp.empty?
230
+ rest = exp.empty? ? nil : exp # graceful re-rewriting (see rewrite_begin)
231
+
232
+ resbodies = []
233
+
234
+ unless rest then
235
+ while resbody do
236
+ resbodies << resbody
237
+ resbody = resbody.resbody(true)
238
+ end
239
+
240
+ resbodies.each do |resbody|
241
+ if resbody[2] && resbody[2][0] == :block && resbody[2].size == 2 then
242
+ resbody[2] = resbody[2][-1]
243
+ end
244
+ end
245
+ else
246
+ resbodies = [resbody] + rest
247
+ end
248
+
249
+ resbodies << els if els
250
+
251
+ s(:rescue, body, *resbodies).compact
252
+ end
253
+
254
+ def rewrite_resbody(exp)
255
+ exp[1] ||= s(:array) # no args
256
+
257
+ body = exp[2]
258
+ if body then
259
+ case body.first
260
+ when :lasgn, :iasgn then
261
+ exp[1] << exp.delete_at(2) if body[-1] == s(:gvar, :$!)
262
+ when :block then
263
+ exp[1] << body.delete_at(1) if [:lasgn, :iasgn].include?(body[1][0]) &&
264
+ body[1][-1] == s(:gvar, :$!)
265
+ end
266
+ end
267
+
268
+ exp << nil if exp.size == 2 # no body
269
+
270
+ exp
271
+ end
272
+
273
+ def rewrite_begin(exp)
274
+ raise "wtf: #{exp.inspect}" if exp.size > 2
275
+ exp.last
276
+ end
277
+
278
+ def rewrite_vcall(exp)
279
+ exp.push nil
280
+ rewrite_fcall(exp)
281
+ end
282
+
283
+ def rewrite_yield(exp)
284
+ # Literal array in yield, not args.
285
+ if exp.size == 3 and exp.pop == true then
286
+ # exp[1] = s(:arglist, exp[1])
287
+ elsif exp.size == 2 && exp.last.first == :array then
288
+ exp.push(*exp.pop[1..-1])
289
+ end
290
+
291
+ exp
292
+ end
293
+
294
+ def rewrite_super(exp)
295
+ exp.push(*exp.pop[1..-1]) if exp.size == 2 && exp.last.first == :array
296
+ exp
297
+ end
298
+
299
+ def rewrite_zarray(exp)
300
+ exp[0] = :array
301
+ exp
302
+ end
303
+ end
304
+
305
+ ##
306
+ # Quick and easy SexpProcessor that unified the sexp structure.
307
+
308
+ class Unifier < SexpProcessor
309
+ include UnifiedRuby
310
+
311
+ def initialize
312
+ super
313
+ @unsupported.delete :newline
314
+ end
315
+ end
data/lib/unique.rb ADDED
@@ -0,0 +1,15 @@
1
+ ##
2
+ # Unique creates unique variable names.
3
+
4
+ class Unique
5
+ def self.reset # mostly for testing
6
+ @@curr = 0
7
+ end
8
+
9
+ def self.next
10
+ @@curr += 1
11
+ "temp_#{@@curr}".intern
12
+ end
13
+
14
+ reset
15
+ end
@@ -0,0 +1,4312 @@
1
+ $TESTING = true
2
+
3
+ require 'test/unit/testcase'
4
+ require 'sexp_processor' # for deep_clone
5
+
6
+ # key:
7
+ # wwtt = what were they thinking?
8
+
9
+ # TODO: <ko1_> 1.8.7 support {|&b|} syntax
10
+
11
+ class Examples
12
+ attr_reader :reader
13
+ attr_writer :writer
14
+
15
+ def a_method(x); x+1; end
16
+ alias an_alias a_method
17
+
18
+ define_method(:bmethod_noargs) do
19
+ x + 1
20
+ end
21
+
22
+ define_method(:unsplatted) do |x|
23
+ x + 1
24
+ end
25
+
26
+ define_method :splatted do |*args|
27
+ y = args.first
28
+ y + 42
29
+ end
30
+
31
+ define_method :dmethod_added, instance_method(:a_method) if
32
+ RUBY_VERSION < "1.9"
33
+ end
34
+
35
+ class ParseTreeTestCase < Test::Unit::TestCase
36
+ unless defined? Mini then
37
+ undef_method :default_test rescue nil
38
+ alias :refute_nil :assert_not_nil
39
+ end
40
+
41
+ attr_accessor :processor # to be defined by subclass
42
+
43
+ def setup
44
+ super
45
+ @processor = nil
46
+ end
47
+
48
+ def after_process_hook klass, node, data, input_name, output_name
49
+ end
50
+
51
+ def before_process_hook klass, node, data, input_name, output_name
52
+ end
53
+
54
+ def self.add_test name, data, klass = self.name[4..-1]
55
+ name = name.to_s
56
+ klass = klass.to_s
57
+
58
+ if testcases.has_key? name then
59
+ if testcases[name].has_key? klass then
60
+ warn "testcase #{klass}##{name} already has data"
61
+ else
62
+ testcases[name][klass] = data
63
+ end
64
+ else
65
+ warn "testcase #{name} does not exist"
66
+ end
67
+ end
68
+
69
+ def self.add_tests name, hash
70
+ name = name.to_s
71
+
72
+ hash.each do |klass, data|
73
+ warn "testcase #{klass}##{name} already has data" if
74
+ testcases[name].has_key? klass
75
+ testcases[name][klass] = data
76
+ end
77
+ end
78
+
79
+ def self.clone_same
80
+ @@testcases.each do |node, data|
81
+ data.each do |key, val|
82
+ if val == :same then
83
+ prev_key = self.previous(key)
84
+ data[key] = data[prev_key].deep_clone
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+ def self.copy_test_case nonverbose, klass
91
+ verbose = nonverbose + "_mri_verbose_flag"
92
+ testcases[verbose][klass] = testcases[nonverbose][klass]
93
+ end
94
+
95
+ def self.generate_test klass, node, data, input_name, output_name
96
+ klass.send(:define_method, "test_#{node}".to_sym) do
97
+ flunk "Processor is nil" if processor.nil?
98
+
99
+ assert data.has_key?(input_name), "Unknown input data"
100
+ assert data.has_key?(output_name), "Missing test data"
101
+
102
+ $missing[node] << output_name unless data.has_key? output_name
103
+
104
+ input = data[input_name].deep_clone
105
+ expected = data[output_name].deep_clone
106
+
107
+ case expected
108
+ when :unsupported then
109
+ assert_raises(UnsupportedNodeError) do
110
+ processor.process(input)
111
+ end
112
+ else
113
+ extra_expected = []
114
+ extra_input = []
115
+
116
+ _, expected, extra_expected = *expected if
117
+ Array === expected and expected.first == :defx
118
+ _, input, extra_input = *input if
119
+ Array === input and input.first == :defx
120
+
121
+ # OMG... I can't believe I have to do this this way. these
122
+ # hooks are here instead of refactoring this define_method
123
+ # body into an assertion. It'll allow subclasses to hook in
124
+ # and add behavior before or after the processor does it's
125
+ # thing. If you go the body refactor route, some of the
126
+ # RawParseTree test casese fail for completely bogus reasons.
127
+
128
+ before_process_hook klass, node, data, input_name, output_name
129
+ refute_nil data[input_name], "testcase does not exist?"
130
+ @result = processor.process input
131
+ assert_equal(expected, @result,
132
+ "failed on input: #{data[input_name].inspect}")
133
+ after_process_hook klass, node, data, input_name, output_name
134
+
135
+ extra_input.each do |extra|
136
+ processor.process(extra)
137
+ end
138
+ extra = processor.extra_methods rescue []
139
+ assert_equal extra_expected, extra
140
+ end
141
+ end
142
+ end
143
+
144
+ def self.generate_tests klass
145
+ install_missing_reporter
146
+ clone_same
147
+
148
+ output_name = klass.name.to_s.sub(/^Test/, '')
149
+
150
+ input_name = self.previous(output_name)
151
+
152
+ @@testcases.each do |node, data|
153
+ next if [:skip, :unsupported].include? data[input_name]
154
+ next if data[output_name] == :skip
155
+
156
+ generate_test klass, node, data, input_name, output_name
157
+ end
158
+ end
159
+
160
+ def self.inherited klass
161
+ super
162
+
163
+ generate_tests klass unless klass.name =~ /TestCase/
164
+ end
165
+
166
+ def self.install_missing_reporter
167
+ unless defined? $missing then
168
+ $missing = Hash.new { |h,k| h[k] = [] }
169
+ at_exit {
170
+ at_exit {
171
+ warn ""
172
+ $missing.sort.each do |name, klasses|
173
+ warn "add_tests(#{name.inspect},"
174
+ klasses.map! { |klass| " #{klass.inspect} => :same" }
175
+ warn klasses.join("\n") + ")"
176
+ end
177
+ warn ""
178
+ }
179
+ }
180
+ end
181
+ end
182
+
183
+ def self.previous(key, extra=0) # FIX: remove R2C code
184
+ idx = @@testcase_order.index(key)
185
+
186
+ raise "Unknown class #{key} in @@testcase_order" if idx.nil?
187
+
188
+ case key
189
+ when "RubyToRubyC" then
190
+ idx -= 1
191
+ end
192
+ @@testcase_order[idx - 1 - extra]
193
+ end
194
+
195
+ def self.testcase_order; @@testcase_order; end
196
+ def self.testcases; @@testcases; end
197
+
198
+ def self.unsupported_tests *tests
199
+ tests.flatten.each do |name|
200
+ add_test name, :unsupported
201
+ end
202
+ end
203
+
204
+ ############################################################
205
+ # Shared TestCases:
206
+
207
+ @@testcase_order = %w(Ruby RawParseTree ParseTree)
208
+
209
+ @@testcases = Hash.new { |h,k| h[k] = {} }
210
+
211
+ add_tests("alias",
212
+ "Ruby" => "class X\n alias :y :x\nend",
213
+ "RawParseTree" => [:class, :X, nil,
214
+ [:scope, [:alias, [:lit, :y], [:lit, :x]]]],
215
+ "ParseTree" => s(:class, :X, nil,
216
+ s(:scope, s(:alias, s(:lit, :y), s(:lit, :x)))),
217
+ "Ruby2Ruby" => "class X\n alias_method :y, :x\nend")
218
+
219
+ add_tests("alias_ugh",
220
+ "Ruby" => "class X\n alias y x\nend",
221
+ "RawParseTree" => [:class, :X, nil,
222
+ [:scope, [:alias, [:lit, :y], [:lit, :x]]]],
223
+ "ParseTree" => s(:class, :X, nil,
224
+ s(:scope, s(:alias, s(:lit, :y), s(:lit, :x)))),
225
+ "Ruby2Ruby" => "class X\n alias_method :y, :x\nend")
226
+
227
+ add_tests("and",
228
+ "Ruby" => "(a and b)",
229
+ "RawParseTree" => [:and, [:vcall, :a], [:vcall, :b]],
230
+ "ParseTree" => s(:and,
231
+ s(:call, nil, :a, s(:arglist)),
232
+ s(:call, nil, :b, s(:arglist))))
233
+
234
+ add_tests("argscat_inside",
235
+ "Ruby" => "a = [b, *c]",
236
+ "RawParseTree" => [:lasgn, :a,
237
+ [:argscat,
238
+ [:array, [:vcall, :b]], [:vcall, :c]]],
239
+ "ParseTree" => s(:lasgn, :a,
240
+ s(:array,
241
+ s(:call, nil, :b, s(:arglist)),
242
+ s(:splat, s(:call, nil, :c, s(:arglist))))))
243
+
244
+ add_tests("argscat_svalue",
245
+ "Ruby" => "a = b, c, *d",
246
+ "RawParseTree" => [:lasgn, :a,
247
+ [:svalue,
248
+ [:argscat,
249
+ [:array, [:vcall, :b], [:vcall, :c]],
250
+ [:vcall, :d]]]],
251
+ "ParseTree" => s(:lasgn, :a,
252
+ s(:svalue,
253
+ s(:array,
254
+ s(:call, nil, :b, s(:arglist)),
255
+ s(:call, nil, :c, s(:arglist)),
256
+ s(:splat,
257
+ s(:call, nil, :d, s(:arglist)))))))
258
+
259
+ add_tests("argspush",
260
+ "Ruby" => "a[*b] = c",
261
+ "RawParseTree" => [:attrasgn,
262
+ [:vcall, :a],
263
+ :[]=,
264
+ [:argspush,
265
+ [:splat,
266
+ [:vcall, :b]],
267
+ [:vcall, :c]]],
268
+ "ParseTree" => s(:attrasgn,
269
+ s(:call, nil, :a, s(:arglist)),
270
+ :[]=,
271
+ s(:arglist,
272
+ s(:splat,
273
+ s(:call, nil, :b, s(:arglist))),
274
+ s(:call, nil, :c, s(:arglist)))))
275
+
276
+ add_tests("array",
277
+ "Ruby" => "[1, :b, \"c\"]",
278
+ "RawParseTree" => [:array, [:lit, 1], [:lit, :b], [:str, "c"]],
279
+ "ParseTree" => s(:array, s(:lit, 1), s(:lit, :b), s(:str, "c")))
280
+
281
+ add_tests("array_pct_W",
282
+ "Ruby" => "%W[a b c]",
283
+ "RawParseTree" => [:array, [:str, "a"], [:str, "b"], [:str, "c"]],
284
+ "ParseTree" => s(:array,
285
+ s(:str, "a"), s(:str, "b"), s(:str, "c")),
286
+ "Ruby2Ruby" => "[\"a\", \"b\", \"c\"]")
287
+
288
+ add_tests("array_pct_W_dstr",
289
+ "Ruby" => "%W[a #\{@b} c]",
290
+ "RawParseTree" => [:array,
291
+ [:str, "a"],
292
+ [:dstr, "", [:evstr, [:ivar, :@b]]],
293
+ [:str, "c"]],
294
+ "ParseTree" => s(:array,
295
+ s(:str, "a"),
296
+ s(:dstr, "", s(:evstr, s(:ivar, :@b))),
297
+ s(:str, "c")),
298
+ "Ruby2Ruby" => "[\"a\", \"#\{@b}\", \"c\"]")
299
+
300
+ add_tests("array_pct_w",
301
+ "Ruby" => "%w[a b c]",
302
+ "RawParseTree" => [:array, [:str, "a"], [:str, "b"], [:str, "c"]],
303
+ "ParseTree" => s(:array,
304
+ s(:str, "a"), s(:str, "b"), s(:str, "c")),
305
+ "Ruby2Ruby" => "[\"a\", \"b\", \"c\"]")
306
+
307
+ add_tests("array_pct_w_dstr",
308
+ "Ruby" => "%w[a #\{@b} c]",
309
+ "RawParseTree" => [:array,
310
+ [:str, "a"],
311
+ [:str, "#\{@b}"],
312
+ [:str, "c"]],
313
+ "ParseTree" => s(:array,
314
+ s(:str, "a"),
315
+ s(:str, "#\{@b}"),
316
+ s(:str, "c")),
317
+ "Ruby2Ruby" => "[\"a\", \"\\\#{@b}\", \"c\"]") # TODO: huh?
318
+
319
+ add_tests("attrasgn",
320
+ "Ruby" => "y = 0\n42.method = y\n",
321
+ "RawParseTree" => [:block,
322
+ [:lasgn, :y, [:lit, 0]],
323
+ [:attrasgn, [:lit, 42], :method=,
324
+ [:array, [:lvar, :y]]]],
325
+ "ParseTree" => s(:block,
326
+ s(:lasgn, :y, s(:lit, 0)),
327
+ s(:attrasgn, s(:lit, 42), :method=,
328
+ s(:arglist, s(:lvar, :y)))))
329
+
330
+ add_tests("attrasgn_index_equals",
331
+ "Ruby" => "a[42] = 24",
332
+ "RawParseTree" => [:attrasgn, [:vcall, :a], :[]=,
333
+ [:array, [:lit, 42], [:lit, 24]]],
334
+ "ParseTree" => s(:attrasgn,
335
+ s(:call, nil, :a, s(:arglist)),
336
+ :[]=,
337
+ s(:arglist, s(:lit, 42), s(:lit, 24))))
338
+
339
+ add_tests("attrasgn_index_equals_space",
340
+ "Ruby" => "a = []; a [42] = 24",
341
+ "RawParseTree" => [:block,
342
+ [:lasgn, :a, [:zarray]],
343
+ [:attrasgn, [:lvar, :a], :[]=,
344
+ [:array, [:lit, 42], [:lit, 24]]]],
345
+ "ParseTree" => s(:block,
346
+ s(:lasgn, :a, s(:array)),
347
+ s(:attrasgn, s(:lvar, :a), :[]=,
348
+ s(:arglist, s(:lit, 42), s(:lit, 24)))),
349
+ "Ruby2Ruby" => "a = []\na[42] = 24\n")
350
+
351
+ add_tests("attrset",
352
+ "Ruby" => [Examples, :writer=],
353
+ "RawParseTree" => [:defn, :writer=, [:attrset, :@writer]],
354
+ "ParseTree" => s(:defn, :writer=,
355
+ s(:args, :arg),
356
+ s(:attrset, :@writer)),
357
+ "Ruby2Ruby" => "attr_writer :writer")
358
+
359
+ add_tests("back_ref",
360
+ "Ruby" => "[$&, $`, $', $+]",
361
+ "RawParseTree" => [:array,
362
+ [:back_ref, :&],
363
+ [:back_ref, :"`"],
364
+ [:back_ref, :"'"],
365
+ [:back_ref, :+]],
366
+ "ParseTree" => s(:array,
367
+ s(:back_ref, :&),
368
+ s(:back_ref, :"`"),
369
+ s(:back_ref, :"'"),
370
+ s(:back_ref, :+)))
371
+
372
+ add_tests("begin",
373
+ "Ruby" => "begin\n (1 + 1)\nend",
374
+ "RawParseTree" => [:begin,
375
+ [:call, [:lit, 1], :+, [:array, [:lit, 1]]]],
376
+ "ParseTree" => s(:call, s(:lit, 1), :+,
377
+ s(:arglist, s(:lit, 1))),
378
+ "Ruby2Ruby" => "(1 + 1)")
379
+
380
+ add_tests("begin_def",
381
+ "Ruby" => "def m\n begin\n\n end\nend",
382
+ "RawParseTree" => [:defn, :m, [:scope, [:block, [:args], [:nil]]]],
383
+ "ParseTree" => s(:defn, :m, s(:args),
384
+ s(:scope, s(:block, s(:nil)))),
385
+ "Ruby2Ruby" => "def m\n # do nothing\nend")
386
+
387
+ add_tests("begin_rescue_ensure",
388
+ "Ruby" => "begin\n a\nrescue\n # do nothing\nensure\n # do nothing\nend",
389
+ "RawParseTree" => [:begin,
390
+ [:ensure,
391
+ [:rescue,
392
+ [:vcall, :a],
393
+ [:resbody, nil]],
394
+ [:nil]]],
395
+ "ParseTree" => s(:ensure,
396
+ s(:rescue,
397
+ s(:call, nil, :a, s(:arglist)),
398
+ s(:resbody, s(:array), nil)),
399
+ s(:nil)))
400
+
401
+ add_tests("begin_rescue_ensure_all_empty",
402
+ "Ruby" => "begin\n # do nothing\nrescue\n # do nothing\nensure\n # do nothing\nend",
403
+ "RawParseTree" => [:begin,
404
+ [:ensure,
405
+ [:rescue,
406
+ [:resbody, nil]],
407
+ [:nil]]],
408
+ "ParseTree" => s(:ensure,
409
+ s(:rescue,
410
+ s(:resbody, s(:array), nil)),
411
+ s(:nil)))
412
+
413
+ add_tests("begin_rescue_twice",
414
+ "Ruby" => "begin\n a\nrescue => mes\n # do nothing\nend\nbegin\n b\nrescue => mes\n # do nothing\nend\n",
415
+ "RawParseTree" => [:block,
416
+ [:begin,
417
+ [:rescue,
418
+ [:vcall, :a],
419
+ [:resbody, nil,
420
+ [:lasgn, :mes, [:gvar, :$!]]]]],
421
+ [:begin,
422
+ [:rescue,
423
+ [:vcall, :b],
424
+ [:resbody, nil,
425
+ [:lasgn, :mes, [:gvar, :$!]]]]]],
426
+ "ParseTree" => s(:block,
427
+ s(:rescue,
428
+ s(:call, nil, :a, s(:arglist)),
429
+ s(:resbody,
430
+ s(:array, s(:lasgn, :mes, s(:gvar, :$!))),
431
+ nil)),
432
+ s(:rescue,
433
+ s(:call, nil, :b, s(:arglist)),
434
+ s(:resbody,
435
+ s(:array,
436
+ s(:lasgn, :mes, s(:gvar, :$!))),
437
+ nil))))
438
+
439
+ add_tests("begin_rescue_twice_mri_verbose_flag",
440
+ "RawParseTree" => [:block,
441
+ [:rescue, # no begin
442
+ [:vcall, :a],
443
+ [:resbody, nil,
444
+ [:lasgn, :mes, [:gvar, :$!]]]],
445
+ [:begin,
446
+ [:rescue,
447
+ [:vcall, :b],
448
+ [:resbody, nil,
449
+ [:lasgn, :mes, [:gvar, :$!]]]]]])
450
+
451
+ copy_test_case "begin_rescue_twice", "Ruby"
452
+ copy_test_case "begin_rescue_twice", "ParseTree"
453
+
454
+ add_tests("block_attrasgn",
455
+ "Ruby" => "def self.setup(ctx)\n bind = allocate\n bind.context = ctx\n return bind\nend",
456
+ "RawParseTree" => [:defs, [:self], :setup,
457
+ [:scope,
458
+ [:block,
459
+ [:args, :ctx],
460
+ [:lasgn, :bind, [:vcall, :allocate]],
461
+ [:attrasgn, [:lvar, :bind], :context=,
462
+ [:array, [:lvar, :ctx]]],
463
+ [:return, [:lvar, :bind]]]]],
464
+ "ParseTree" => s(:defs, s(:self), :setup,
465
+ s(:args, :ctx),
466
+ s(:scope,
467
+ s(:block,
468
+ s(:lasgn, :bind,
469
+ s(:call, nil, :allocate, s(:arglist))),
470
+ s(:attrasgn, s(:lvar, :bind), :context=,
471
+ s(:arglist, s(:lvar, :ctx))),
472
+ s(:return, s(:lvar, :bind))))))
473
+
474
+
475
+ add_tests("block_lasgn",
476
+ "Ruby" => "x = (y = 1\n(y + 2))",
477
+ "RawParseTree" => [:lasgn, :x,
478
+ [:block,
479
+ [:lasgn, :y, [:lit, 1]],
480
+ [:call, [:lvar, :y], :+, [:array, [:lit, 2]]]]],
481
+ "ParseTree" => s(:lasgn, :x,
482
+ s(:block,
483
+ s(:lasgn, :y, s(:lit, 1)),
484
+ s(:call, s(:lvar, :y), :+,
485
+ s(:arglist, s(:lit, 2))))))
486
+
487
+ add_tests("block_mystery_block",
488
+ "Ruby" => "a(b) do\n if b then\n true\n else\n c = false\n d { |x| c = true }\n c\n end\nend",
489
+ "RawParseTree" => [:iter,
490
+ [:fcall, :a, [:array, [:vcall, :b]]],
491
+ nil,
492
+ [:if,
493
+ [:vcall, :b],
494
+ [:true],
495
+ [:block,
496
+ [:dasgn_curr, :c, [:false]],
497
+ [:iter,
498
+ [:fcall, :d],
499
+ [:dasgn_curr, :x],
500
+ [:dasgn, :c, [:true]]],
501
+ [:dvar, :c]]]],
502
+ "ParseTree" => s(:iter,
503
+ s(:call, nil, :a,
504
+ s(:arglist, s(:call, nil, :b, s(:arglist)))),
505
+ nil,
506
+ s(:if,
507
+ s(:call, nil, :b, s(:arglist)),
508
+ s(:true),
509
+ s(:block,
510
+ s(:lasgn, :c, s(:false)),
511
+ s(:iter,
512
+ s(:call, nil, :d, s(:arglist)),
513
+ s(:lasgn, :x),
514
+ s(:lasgn, :c, s(:true))),
515
+ s(:lvar, :c)))))
516
+
517
+ add_tests("block_pass_args_and_splat",
518
+ "Ruby" => "def blah(*args, &block)\n other(42, *args, &block)\nend",
519
+ "RawParseTree" => [:defn, :blah,
520
+ [:scope,
521
+ [:block,
522
+ [:args, :"*args"],
523
+ [:block_arg, :block],
524
+ [:block_pass,
525
+ [:lvar, :block],
526
+ [:fcall, :other,
527
+ [:argscat,
528
+ [:array, [:lit, 42]], [:lvar, :args]]]]]]],
529
+ "ParseTree" => s(:defn, :blah,
530
+ s(:args, :"*args", :"&block"),
531
+ s(:scope,
532
+ s(:block,
533
+ s(:call, nil, :other,
534
+ s(:arglist,
535
+ s(:lit, 42),
536
+ s(:splat, s(:lvar, :args)),
537
+ s(:block_pass, s(:lvar, :block))))))))
538
+
539
+ add_tests("block_pass_call_0",
540
+ "Ruby" => "a.b(&c)",
541
+ "RawParseTree" => [:block_pass,
542
+ [:vcall, :c], [:call, [:vcall, :a], :b]],
543
+ "ParseTree" => s(:call,
544
+ s(:call, nil, :a, s(:arglist)),
545
+ :b,
546
+ s(:arglist,
547
+ s(:block_pass,
548
+ s(:call, nil, :c, s(:arglist))))))
549
+
550
+ add_tests("block_pass_call_1",
551
+ "Ruby" => "a.b(4, &c)",
552
+ "RawParseTree" => [:block_pass,
553
+ [:vcall, :c],
554
+ [:call, [:vcall, :a], :b, [:array, [:lit, 4]]]],
555
+ "ParseTree" => s(:call,
556
+ s(:call, nil, :a, s(:arglist)),
557
+ :b,
558
+ s(:arglist,
559
+ s(:lit, 4),
560
+ s(:block_pass,
561
+ s(:call, nil, :c, s(:arglist))))))
562
+
563
+ add_tests("block_pass_call_n",
564
+ "Ruby" => "a.b(1, 2, 3, &c)",
565
+ "RawParseTree" => [:block_pass,
566
+ [:vcall, :c],
567
+ [:call, [:vcall, :a], :b,
568
+ [:array, [:lit, 1], [:lit, 2], [:lit, 3]]]],
569
+ "ParseTree" => s(:call,
570
+ s(:call, nil, :a, s(:arglist)),
571
+ :b,
572
+ s(:arglist,
573
+ s(:lit, 1), s(:lit, 2), s(:lit, 3),
574
+ s(:block_pass,
575
+ s(:call, nil, :c, s(:arglist))))))
576
+
577
+ add_tests("block_pass_fcall_0",
578
+ "Ruby" => "a(&b)",
579
+ "RawParseTree" => [:block_pass, [:vcall, :b], [:fcall, :a]],
580
+ "ParseTree" => s(:call, nil, :a,
581
+ s(:arglist,
582
+ s(:block_pass,
583
+ s(:call, nil, :b, s(:arglist))))))
584
+
585
+ add_tests("block_pass_fcall_1",
586
+ "Ruby" => "a(4, &b)",
587
+ "RawParseTree" => [:block_pass,
588
+ [:vcall, :b],
589
+ [:fcall, :a, [:array, [:lit, 4]]]],
590
+ "ParseTree" => s(:call, nil, :a,
591
+ s(:arglist,
592
+ s(:lit, 4),
593
+ s(:block_pass,
594
+ s(:call, nil, :b, s(:arglist))))))
595
+
596
+ add_tests("block_pass_fcall_n",
597
+ "Ruby" => "a(1, 2, 3, &b)",
598
+ "RawParseTree" => [:block_pass,
599
+ [:vcall, :b],
600
+ [:fcall, :a,
601
+ [:array, [:lit, 1], [:lit, 2], [:lit, 3]]]],
602
+ "ParseTree" => s(:call, nil, :a,
603
+ s(:arglist,
604
+ s(:lit, 1), s(:lit, 2), s(:lit, 3),
605
+ s(:block_pass,
606
+ s(:call, nil, :b, s(:arglist))))))
607
+
608
+ add_tests("block_pass_omgwtf",
609
+ "Ruby" => "define_attr_method(:x, :sequence_name, &Proc.new { |*args| nil })",
610
+ "RawParseTree" => [:block_pass,
611
+ [:iter,
612
+ [:call, [:const, :Proc], :new],
613
+ [:masgn, nil, [:dasgn_curr, :args], nil],
614
+ [:nil]],
615
+ [:fcall, :define_attr_method,
616
+ [:array, [:lit, :x], [:lit, :sequence_name]]]],
617
+ "ParseTree" => s(:call, nil, :define_attr_method,
618
+ s(:arglist,
619
+ s(:lit, :x),
620
+ s(:lit, :sequence_name),
621
+ s(:block_pass,
622
+ s(:iter,
623
+ s(:call, s(:const, :Proc), :new,
624
+ s(:arglist)),
625
+ s(:masgn,
626
+ s(:array, s(:splat, s(:lasgn, :args)))),
627
+ s(:nil))))))
628
+
629
+ add_tests("block_pass_splat",
630
+ "Ruby" => "def blah(*args, &block)\n other(*args, &block)\nend",
631
+ "RawParseTree" => [:defn, :blah,
632
+ [:scope,
633
+ [:block,
634
+ [:args, :"*args"],
635
+ [:block_arg, :block],
636
+ [:block_pass,
637
+ [:lvar, :block],
638
+ [:fcall, :other,
639
+ [:splat, [:lvar, :args]]]]]]],
640
+ "ParseTree" => s(:defn, :blah,
641
+ s(:args, :"*args", :"&block"),
642
+ s(:scope,
643
+ s(:block,
644
+ s(:call, nil, :other,
645
+ s(:arglist,
646
+ s(:splat, s(:lvar, :args)),
647
+ s(:block_pass, s(:lvar, :block))))))))
648
+
649
+ add_tests("block_pass_thingy",
650
+ "Ruby" => "r.read_body(dest, &block)",
651
+ "RawParseTree" => [:block_pass,
652
+ [:vcall, :block],
653
+ [:call, [:vcall, :r], :read_body,
654
+ [:array, [:vcall, :dest]]]],
655
+ "ParseTree" => s(:call,
656
+ s(:call, nil, :r, s(:arglist)),
657
+ :read_body,
658
+ s(:arglist,
659
+ s(:call, nil, :dest, s(:arglist)),
660
+ s(:block_pass,
661
+ s(:call, nil, :block, s(:arglist))))))
662
+
663
+ add_tests("block_stmt_after",
664
+ "Ruby" => "def f\n begin\n b\n rescue\n c\n end\n\n d\nend",
665
+ "RawParseTree" => [:defn, :f,
666
+ [:scope,
667
+ [:block,
668
+ [:args],
669
+ [:begin,
670
+ [:rescue,
671
+ [:vcall, :b],
672
+ [:resbody, nil, [:vcall, :c]]]],
673
+ [:vcall, :d]]]],
674
+ "ParseTree" => s(:defn, :f,
675
+ s(:args),
676
+ s(:scope,
677
+ s(:block,
678
+ s(:rescue,
679
+ s(:call, nil, :b, s(:arglist)),
680
+ s(:resbody,
681
+ s(:array),
682
+ s(:call, nil, :c, s(:arglist)))),
683
+ s(:call, nil, :d, s(:arglist))))),
684
+ "Ruby2Ruby" => "def f\n b rescue c\n d\nend")
685
+
686
+ add_tests("block_stmt_after_mri_verbose_flag",
687
+ "RawParseTree" => [:defn, :f,
688
+ [:scope,
689
+ [:block,
690
+ [:args],
691
+ [:rescue, # no begin
692
+ [:vcall, :b],
693
+ [:resbody, nil, [:vcall, :c]]],
694
+ [:vcall, :d]]]])
695
+
696
+ copy_test_case "block_stmt_after", "Ruby"
697
+ copy_test_case "block_stmt_after", "ParseTree"
698
+ copy_test_case "block_stmt_after", "Ruby2Ruby"
699
+
700
+ add_tests("block_stmt_before",
701
+ "Ruby" => "def f\n a\n begin\n b\n rescue\n c\n end\nend",
702
+ "RawParseTree" => [:defn, :f,
703
+ [:scope,
704
+ [:block,
705
+ [:args],
706
+ [:vcall, :a],
707
+ [:begin,
708
+ [:rescue, [:vcall, :b],
709
+ [:resbody, nil, [:vcall, :c]]]]]]],
710
+ "ParseTree" => s(:defn, :f,
711
+ s(:args),
712
+ s(:scope,
713
+ s(:block,
714
+ s(:call, nil, :a, s(:arglist)),
715
+ s(:rescue, s(:call, nil, :b, s(:arglist)),
716
+ s(:resbody,
717
+ s(:array),
718
+ s(:call, nil, :c, s(:arglist))))))),
719
+ "Ruby2Ruby" => "def f\n a\n b rescue c\nend")
720
+
721
+ # oddly... this one doesn't HAVE any differences when verbose... new?
722
+ copy_test_case "block_stmt_before", "Ruby"
723
+ copy_test_case "block_stmt_before", "ParseTree"
724
+ copy_test_case "block_stmt_before", "RawParseTree"
725
+ copy_test_case "block_stmt_before", "Ruby2Ruby"
726
+
727
+ add_tests("block_stmt_both",
728
+ "Ruby" => "def f\n a\n begin\n b\n rescue\n c\n end\n d\nend",
729
+ "RawParseTree" => [:defn, :f,
730
+ [:scope,
731
+ [:block,
732
+ [:args],
733
+ [:vcall, :a],
734
+ [:begin,
735
+ [:rescue,
736
+ [:vcall, :b],
737
+ [:resbody,
738
+ nil,
739
+ [:vcall, :c]]]],
740
+ [:vcall, :d]]]],
741
+ "ParseTree" => s(:defn, :f, s(:args),
742
+ s(:scope,
743
+ s(:block,
744
+ s(:call, nil, :a, s(:arglist)),
745
+ s(:rescue,
746
+ s(:call, nil, :b, s(:arglist)),
747
+ s(:resbody,
748
+ s(:array),
749
+ s(:call, nil, :c, s(:arglist)))),
750
+ s(:call, nil, :d, s(:arglist))))),
751
+ "Ruby2Ruby" => "def f\n a\n b rescue c\n d\nend")
752
+
753
+ add_tests("block_stmt_both_mri_verbose_flag",
754
+ "RawParseTree" => [:defn, :f,
755
+ [:scope,
756
+ [:block,
757
+ [:args],
758
+ [:vcall, :a],
759
+ [:rescue, # no begin
760
+ [:vcall, :b],
761
+ [:resbody,
762
+ nil,
763
+ [:vcall, :c]]],
764
+ [:vcall, :d]]]])
765
+
766
+ copy_test_case "block_stmt_both", "Ruby"
767
+ copy_test_case "block_stmt_both", "ParseTree"
768
+ copy_test_case "block_stmt_both", "Ruby2Ruby"
769
+
770
+ add_tests("bmethod",
771
+ "Ruby" => [Examples, :unsplatted],
772
+ "RawParseTree" => [:defn, :unsplatted,
773
+ [:bmethod,
774
+ [:dasgn_curr, :x],
775
+ [:call, [:dvar, :x], :+, [:array, [:lit, 1]]]]],
776
+ "ParseTree" => s(:defn, :unsplatted,
777
+ s(:args, :x),
778
+ s(:scope,
779
+ s(:block,
780
+ s(:call,
781
+ s(:lvar, :x),
782
+ :+,
783
+ s(:arglist, s(:lit, 1)))))),
784
+ "Ruby2Ruby" => "def unsplatted(x)\n (x + 1)\nend")
785
+
786
+ add_tests("bmethod_noargs",
787
+ "Ruby" => [Examples, :bmethod_noargs],
788
+ "RawParseTree" => [:defn, :bmethod_noargs,
789
+ [:bmethod,
790
+ nil,
791
+ [:call,
792
+ [:vcall, :x], :"+", [:array, [:lit, 1]]]]],
793
+ "ParseTree" => s(:defn, :bmethod_noargs,
794
+ s(:args),
795
+ s(:scope,
796
+ s(:block,
797
+ s(:call,
798
+ s(:call, nil, :x, s(:arglist)),
799
+ :"+",
800
+ s(:arglist, s(:lit, 1)))))),
801
+ "Ruby2Ruby" => "def bmethod_noargs\n (x + 1)\nend")
802
+
803
+ add_tests("bmethod_splat",
804
+ "Ruby" => [Examples, :splatted],
805
+ "RawParseTree" => [:defn, :splatted,
806
+ [:bmethod,
807
+ [:masgn, nil, [:dasgn_curr, :args], nil],
808
+ [:block,
809
+ [:dasgn_curr, :y,
810
+ [:call, [:dvar, :args], :first]],
811
+ [:call, [:dvar, :y], :+,
812
+ [:array, [:lit, 42]]]]]],
813
+ "ParseTree" => s(:defn, :splatted,
814
+ s(:args, :"*args"),
815
+ s(:scope,
816
+ s(:block,
817
+ s(:lasgn, :y,
818
+ s(:call, s(:lvar, :args), :first,
819
+ s(:arglist))),
820
+ s(:call, s(:lvar, :y), :+,
821
+ s(:arglist, s(:lit, 42)))))),
822
+ "Ruby2Ruby" => "def splatted(*args)\n y = args.first\n (y + 42)\nend")
823
+
824
+ add_tests("break",
825
+ "Ruby" => "loop { break if true }",
826
+ "RawParseTree" => [:iter,
827
+ [:fcall, :loop], nil,
828
+ [:if, [:true], [:break], nil]],
829
+ "ParseTree" => s(:iter,
830
+ s(:call, nil, :loop, s(:arglist)), nil,
831
+ s(:if, s(:true), s(:break), nil)))
832
+
833
+ add_tests("break_arg",
834
+ "Ruby" => "loop { break 42 if true }",
835
+ "RawParseTree" => [:iter,
836
+ [:fcall, :loop], nil,
837
+ [:if, [:true], [:break, [:lit, 42]], nil]],
838
+ "ParseTree" => s(:iter,
839
+ s(:call, nil, :loop, s(:arglist)), nil,
840
+ s(:if, s(:true), s(:break, s(:lit, 42)), nil)))
841
+
842
+ add_tests("call",
843
+ "Ruby" => "self.method",
844
+ "RawParseTree" => [:call, [:self], :method],
845
+ "ParseTree" => s(:call, s(:self), :method, s(:arglist)))
846
+
847
+ add_tests("call_arglist",
848
+ "Ruby" => "o.puts(42)",
849
+ "RawParseTree" => [:call, [:vcall, :o], :puts,
850
+ [:array, [:lit, 42]]],
851
+ "ParseTree" => s(:call, s(:call, nil, :o, s(:arglist)), :puts,
852
+ s(:arglist, s(:lit, 42))))
853
+
854
+ add_tests("call_arglist_hash",
855
+ "Ruby" => "o.m(:a => 1, :b => 2)",
856
+ "RawParseTree" => [:call,
857
+ [:vcall, :o], :m,
858
+ [:array,
859
+ [:hash,
860
+ [:lit, :a], [:lit, 1],
861
+ [:lit, :b], [:lit, 2]]]],
862
+ "ParseTree" => s(:call,
863
+ s(:call, nil, :o, s(:arglist)), :m,
864
+ s(:arglist,
865
+ s(:hash,
866
+ s(:lit, :a), s(:lit, 1),
867
+ s(:lit, :b), s(:lit, 2)))))
868
+
869
+ add_tests("call_arglist_norm_hash",
870
+ "Ruby" => "o.m(42, :a => 1, :b => 2)",
871
+ "RawParseTree" => [:call,
872
+ [:vcall, :o], :m,
873
+ [:array,
874
+ [:lit, 42],
875
+ [:hash,
876
+ [:lit, :a], [:lit, 1],
877
+ [:lit, :b], [:lit, 2]]]],
878
+ "ParseTree" => s(:call,
879
+ s(:call, nil, :o, s(:arglist)), :m,
880
+ s(:arglist,
881
+ s(:lit, 42),
882
+ s(:hash,
883
+ s(:lit, :a), s(:lit, 1),
884
+ s(:lit, :b), s(:lit, 2)))))
885
+
886
+ add_tests("call_arglist_norm_hash_splat",
887
+ "Ruby" => "o.m(42, :a => 1, :b => 2, *c)",
888
+ "RawParseTree" => [:call,
889
+ [:vcall, :o], :m,
890
+ [:argscat,
891
+ [:array,
892
+ [:lit, 42],
893
+ [:hash,
894
+ [:lit, :a], [:lit, 1],
895
+ [:lit, :b], [:lit, 2]]],
896
+ [:vcall, :c]]],
897
+ "ParseTree" => s(:call,
898
+ s(:call, nil, :o, s(:arglist)), :m,
899
+ s(:arglist,
900
+ s(:lit, 42),
901
+ s(:hash,
902
+ s(:lit, :a), s(:lit, 1),
903
+ s(:lit, :b), s(:lit, 2)),
904
+ s(:splat, s(:call, nil, :c, s(:arglist))))))
905
+
906
+ add_tests("call_arglist_space",
907
+ "Ruby" => "a (1,2,3)",
908
+ "RawParseTree" => [:fcall, :a,
909
+ [:array, [:lit, 1], [:lit, 2], [:lit, 3]]],
910
+ "ParseTree" => s(:call, nil, :a,
911
+ s(:arglist,
912
+ s(:lit, 1), s(:lit, 2), s(:lit, 3))),
913
+ "Ruby2Ruby" => "a(1, 2, 3)")
914
+
915
+ add_tests("call_command",
916
+ "Ruby" => "1.b(c)",
917
+ "RawParseTree" => [:call, [:lit, 1], :b, [:array, [:vcall, :c]]],
918
+ "ParseTree" => s(:call,
919
+ s(:lit, 1),
920
+ :b,
921
+ s(:arglist, s(:call, nil, :c, s(:arglist)))))
922
+
923
+ add_tests("call_expr",
924
+ "Ruby" => "(v = (1 + 1)).zero?",
925
+ "RawParseTree" => [:call,
926
+ [:lasgn, :v,
927
+ [:call, [:lit, 1], :+, [:array, [:lit, 1]]]],
928
+ :zero?],
929
+ "ParseTree" => s(:call,
930
+ s(:lasgn, :v,
931
+ s(:call, s(:lit, 1), :+,
932
+ s(:arglist, s(:lit, 1)))),
933
+ :zero?, s(:arglist)))
934
+
935
+ add_tests("call_index",
936
+ "Ruby" => "a = []\na[42]\n",
937
+ "RawParseTree" => [:block,
938
+ [:lasgn, :a, [:zarray]],
939
+ [:call, [:lvar, :a], :[], [:array, [:lit, 42]]]],
940
+ "ParseTree" => s(:block,
941
+ s(:lasgn, :a, s(:array)),
942
+ s(:call, s(:lvar, :a), :[],
943
+ s(:arglist, s(:lit, 42)))))
944
+
945
+ add_tests("call_index_no_args",
946
+ "Ruby" => "a[]",
947
+ "RawParseTree" => [:call, [:vcall, :a], :[]],
948
+ "ParseTree" => s(:call, s(:call, nil, :a, s(:arglist)),
949
+ :[], s(:arglist)))
950
+
951
+ add_tests("call_index_space",
952
+ "Ruby" => "a = []\na [42]\n",
953
+ "RawParseTree" => [:block,
954
+ [:lasgn, :a, [:zarray]],
955
+ [:call, [:lvar, :a], :[], [:array, [:lit, 42]]]],
956
+ "ParseTree" => s(:block,
957
+ s(:lasgn, :a, s(:array)),
958
+ s(:call, s(:lvar, :a), :[],
959
+ s(:arglist, s(:lit, 42)))),
960
+ "Ruby2Ruby" => "a = []\na[42]\n")
961
+
962
+ add_tests("call_unary_neg",
963
+ "Ruby" => "-2**31",
964
+ "RawParseTree" => [:call,
965
+ [:call, [:lit, 2], :**, [:array, [:lit, 31]]],
966
+ :-@],
967
+ "ParseTree" => s(:call,
968
+ s(:call,
969
+ s(:lit, 2),
970
+ :**,
971
+ s(:arglist, s(:lit, 31))),
972
+ :-@, s(:arglist)),
973
+ "Ruby2Ruby" => "-(2 ** 31)")
974
+
975
+ add_tests("case",
976
+ "Ruby" => "var = 2\nresult = \"\"\ncase var\nwhen 1 then\n puts(\"something\")\n result = \"red\"\nwhen 2, 3 then\n result = \"yellow\"\nwhen 4 then\n # do nothing\nelse\n result = \"green\"\nend\ncase result\nwhen \"red\" then\n var = 1\nwhen \"yellow\" then\n var = 2\nwhen \"green\" then\n var = 3\nelse\n # do nothing\nend\n",
977
+ "RawParseTree" => [:block,
978
+ [:lasgn, :var, [:lit, 2]],
979
+ [:lasgn, :result, [:str, ""]],
980
+ [:case,
981
+ [:lvar, :var],
982
+ [:when,
983
+ [:array, [:lit, 1]],
984
+ [:block,
985
+ [:fcall, :puts,
986
+ [:array, [:str, "something"]]],
987
+ [:lasgn, :result, [:str, "red"]]]],
988
+ [:when,
989
+ [:array, [:lit, 2], [:lit, 3]],
990
+ [:lasgn, :result, [:str, "yellow"]]],
991
+ [:when, [:array, [:lit, 4]], nil],
992
+ [:lasgn, :result, [:str, "green"]]],
993
+ [:case,
994
+ [:lvar, :result],
995
+ [:when, [:array, [:str, "red"]],
996
+ [:lasgn, :var, [:lit, 1]]],
997
+ [:when, [:array, [:str, "yellow"]],
998
+ [:lasgn, :var, [:lit, 2]]],
999
+ [:when, [:array, [:str, "green"]],
1000
+ [:lasgn, :var, [:lit, 3]]],
1001
+ nil]],
1002
+ "ParseTree" => s(:block,
1003
+ s(:lasgn, :var, s(:lit, 2)),
1004
+ s(:lasgn, :result, s(:str, "")),
1005
+ s(:case,
1006
+ s(:lvar, :var),
1007
+ s(:when,
1008
+ s(:array, s(:lit, 1)),
1009
+ s(:block,
1010
+ s(:call, nil, :puts,
1011
+ s(:arglist, s(:str, "something"))),
1012
+ s(:lasgn, :result, s(:str, "red")))),
1013
+ s(:when,
1014
+ s(:array, s(:lit, 2), s(:lit, 3)),
1015
+ s(:lasgn, :result, s(:str, "yellow"))),
1016
+ s(:when, s(:array, s(:lit, 4)), nil),
1017
+ s(:lasgn, :result, s(:str, "green"))),
1018
+ s(:case,
1019
+ s(:lvar, :result),
1020
+ s(:when, s(:array, s(:str, "red")),
1021
+ s(:lasgn, :var, s(:lit, 1))),
1022
+ s(:when, s(:array, s(:str, "yellow")),
1023
+ s(:lasgn, :var, s(:lit, 2))),
1024
+ s(:when, s(:array, s(:str, "green")),
1025
+ s(:lasgn, :var, s(:lit, 3))),
1026
+ nil)))
1027
+
1028
+ add_tests("case_nested",
1029
+ "Ruby" => "var1 = 1\nvar2 = 2\nresult = nil\ncase var1\nwhen 1 then\n case var2\n when 1 then\n result = 1\n when 2 then\n result = 2\n else\n result = 3\n end\nwhen 2 then\n case var2\n when 1 then\n result = 4\n when 2 then\n result = 5\n else\n result = 6\n end\nelse\n result = 7\nend\n",
1030
+ "RawParseTree" => [:block,
1031
+ [:lasgn, :var1, [:lit, 1]],
1032
+ [:lasgn, :var2, [:lit, 2]],
1033
+ [:lasgn, :result, [:nil]],
1034
+ [:case,
1035
+ [:lvar, :var1],
1036
+ [:when, [:array, [:lit, 1]],
1037
+ [:case,
1038
+ [:lvar, :var2],
1039
+ [:when, [:array, [:lit, 1]],
1040
+ [:lasgn, :result, [:lit, 1]]],
1041
+ [:when, [:array, [:lit, 2]],
1042
+ [:lasgn, :result, [:lit, 2]]],
1043
+ [:lasgn, :result, [:lit, 3]]]],
1044
+ [:when, [:array, [:lit, 2]],
1045
+ [:case,
1046
+ [:lvar, :var2],
1047
+ [:when, [:array, [:lit, 1]],
1048
+ [:lasgn, :result, [:lit, 4]]],
1049
+ [:when, [:array, [:lit, 2]],
1050
+ [:lasgn, :result, [:lit, 5]]],
1051
+ [:lasgn, :result, [:lit, 6]]]],
1052
+ [:lasgn, :result, [:lit, 7]]]],
1053
+ "ParseTree" => s(:block,
1054
+ s(:lasgn, :var1, s(:lit, 1)),
1055
+ s(:lasgn, :var2, s(:lit, 2)),
1056
+ s(:lasgn, :result, s(:nil)),
1057
+ s(:case,
1058
+ s(:lvar, :var1),
1059
+ s(:when, s(:array, s(:lit, 1)),
1060
+ s(:case,
1061
+ s(:lvar, :var2),
1062
+ s(:when, s(:array, s(:lit, 1)),
1063
+ s(:lasgn, :result, s(:lit, 1))),
1064
+ s(:when, s(:array, s(:lit, 2)),
1065
+ s(:lasgn, :result, s(:lit, 2))),
1066
+ s(:lasgn, :result, s(:lit, 3)))),
1067
+ s(:when, s(:array, s(:lit, 2)),
1068
+ s(:case,
1069
+ s(:lvar, :var2),
1070
+ s(:when, s(:array, s(:lit, 1)),
1071
+ s(:lasgn, :result, s(:lit, 4))),
1072
+ s(:when, s(:array, s(:lit, 2)),
1073
+ s(:lasgn, :result, s(:lit, 5))),
1074
+ s(:lasgn, :result, s(:lit, 6)))),
1075
+ s(:lasgn, :result, s(:lit, 7)))))
1076
+
1077
+ add_tests("case_nested_inner_no_expr",
1078
+ "Ruby" => "case a\nwhen b then\n case\n when (d and e) then\n f\n else\n # do nothing\n end\nelse\n # do nothing\nend",
1079
+ "RawParseTree" => [:case, [:vcall, :a],
1080
+ [:when, [:array, [:vcall, :b]],
1081
+ [:case, nil,
1082
+ [:when,
1083
+ [:array, [:and, [:vcall, :d], [:vcall, :e]]],
1084
+ [:vcall, :f]],
1085
+ nil]],
1086
+ nil],
1087
+ "ParseTree" => s(:case, s(:call, nil, :a, s(:arglist)),
1088
+ s(:when,
1089
+ s(:array, s(:call, nil, :b, s(:arglist))),
1090
+ s(:case, nil,
1091
+ s(:when,
1092
+ s(:array,
1093
+ s(:and,
1094
+ s(:call, nil, :d, s(:arglist)),
1095
+ s(:call, nil, :e, s(:arglist)))),
1096
+ s(:call, nil, :f, s(:arglist))),
1097
+ nil)),
1098
+ nil))
1099
+
1100
+ add_tests("case_no_expr",
1101
+ "Ruby" => "case\nwhen (a == 1) then\n :a\nwhen (a == 2) then\n :b\nelse\n :c\nend",
1102
+ "RawParseTree" => [:case, nil,
1103
+ [:when,
1104
+ [:array,
1105
+ [:call, [:vcall, :a], :==,
1106
+ [:array, [:lit, 1]]]],
1107
+ [:lit, :a]],
1108
+ [:when,
1109
+ [:array,
1110
+ [:call, [:vcall, :a], :==,
1111
+ [:array, [:lit, 2]]]],
1112
+ [:lit, :b]],
1113
+ [:lit, :c]],
1114
+ "ParseTree" => s(:case, nil,
1115
+ s(:when,
1116
+ s(:array,
1117
+ s(:call,
1118
+ s(:call, nil, :a, s(:arglist)),
1119
+ :==,
1120
+ s(:arglist, s(:lit, 1)))),
1121
+ s(:lit, :a)),
1122
+ s(:when,
1123
+ s(:array,
1124
+ s(:call,
1125
+ s(:call, nil, :a, s(:arglist)),
1126
+ :==,
1127
+ s(:arglist, s(:lit, 2)))),
1128
+ s(:lit, :b)),
1129
+ s(:lit, :c)))
1130
+
1131
+ add_tests("case_splat",
1132
+ "Ruby" => "case a\nwhen :b, *c then\n d\nelse\n e\nend",
1133
+ "RawParseTree" => [:case, [:vcall, :a],
1134
+ [:when,
1135
+ [:array,
1136
+ [:lit, :b], [:when, [:vcall, :c], nil]], # wtf?
1137
+ [:vcall, :d]],
1138
+ [:vcall, :e]],
1139
+ "ParseTree" => s(:case, s(:call, nil, :a, s(:arglist)),
1140
+ s(:when,
1141
+ s(:array,
1142
+ s(:lit, :b),
1143
+ s(:when,
1144
+ s(:call, nil, :c, s(:arglist)),
1145
+ nil)), # wtf?
1146
+ s(:call, nil, :d, s(:arglist))),
1147
+ s(:call, nil, :e, s(:arglist))))
1148
+
1149
+ add_tests("cdecl",
1150
+ "Ruby" => "X = 42",
1151
+ "RawParseTree" => [:cdecl, :X, [:lit, 42]],
1152
+ "ParseTree" => s(:cdecl, :X, s(:lit, 42)))
1153
+
1154
+ add_tests("class_plain",
1155
+ "Ruby" => "class X\n puts((1 + 1))\n def blah\n puts(\"hello\")\n end\nend",
1156
+ "RawParseTree" => [:class,
1157
+ :X,
1158
+ nil,
1159
+ [:scope,
1160
+ [:block,
1161
+ [:fcall, :puts,
1162
+ [:array,
1163
+ [:call, [:lit, 1], :+,
1164
+ [:array, [:lit, 1]]]]],
1165
+ [:defn, :blah,
1166
+ [:scope,
1167
+ [:block,
1168
+ [:args],
1169
+ [:fcall, :puts,
1170
+ [:array, [:str, "hello"]]]]]]]]],
1171
+ "ParseTree" => s(:class,
1172
+ :X,
1173
+ nil,
1174
+ s(:scope,
1175
+ s(:block,
1176
+ s(:call, nil, :puts,
1177
+ s(:arglist,
1178
+ s(:call, s(:lit, 1), :+,
1179
+ s(:arglist, s(:lit, 1))))),
1180
+ s(:defn, :blah,
1181
+ s(:args),
1182
+ s(:scope,
1183
+ s(:block,
1184
+ s(:call, nil, :puts,
1185
+ s(:arglist,
1186
+ s(:str, "hello"))))))))))
1187
+
1188
+ add_tests("class_scoped",
1189
+ "Ruby" => "class X::Y\n c\nend",
1190
+ "RawParseTree" => [:class, [:colon2, [:const, :X], :Y], nil,
1191
+ [:scope, [:vcall, :c]]],
1192
+ "ParseTree" => s(:class, s(:colon2, s(:const, :X), :Y), nil,
1193
+ s(:scope, s(:call, nil, :c, s(:arglist)))))
1194
+
1195
+ add_tests("class_scoped3",
1196
+ "Ruby" => "class ::Y\n c\nend",
1197
+ "RawParseTree" => [:class, [:colon3, :Y], nil,
1198
+ [:scope, [:vcall, :c]]],
1199
+ "ParseTree" => s(:class, s(:colon3, :Y), nil,
1200
+ s(:scope, s(:call, nil, :c, s(:arglist)))))
1201
+
1202
+ add_tests("class_super_array",
1203
+ "Ruby" => "class X < Array\nend",
1204
+ "RawParseTree" => [:class,
1205
+ :X,
1206
+ [:const, :Array],
1207
+ [:scope]],
1208
+ "ParseTree" => s(:class,
1209
+ :X,
1210
+ s(:const, :Array),
1211
+ s(:scope)))
1212
+
1213
+ add_tests("class_super_expr",
1214
+ "Ruby" => "class X < expr\nend",
1215
+ "RawParseTree" => [:class,
1216
+ :X,
1217
+ [:vcall, :expr],
1218
+ [:scope]],
1219
+ "ParseTree" => s(:class,
1220
+ :X,
1221
+ s(:call, nil, :expr, s(:arglist)),
1222
+ s(:scope)))
1223
+
1224
+ add_tests("class_super_object",
1225
+ "Ruby" => "class X < Object\nend",
1226
+ "RawParseTree" => [:class,
1227
+ :X,
1228
+ [:const, :Object],
1229
+ [:scope]],
1230
+ "ParseTree" => s(:class,
1231
+ :X,
1232
+ s(:const, :Object),
1233
+ s(:scope)))
1234
+
1235
+ add_tests("colon2",
1236
+ "Ruby" => "X::Y",
1237
+ "RawParseTree" => [:colon2, [:const, :X], :Y],
1238
+ "ParseTree" => s(:colon2, s(:const, :X), :Y))
1239
+
1240
+ add_tests("colon3",
1241
+ "Ruby" => "::X",
1242
+ "RawParseTree" => [:colon3, :X],
1243
+ "ParseTree" => s(:colon3, :X))
1244
+
1245
+ add_tests("const",
1246
+ "Ruby" => "X",
1247
+ "RawParseTree" => [:const, :X],
1248
+ "ParseTree" => s(:const, :X))
1249
+
1250
+ add_tests("constX",
1251
+ "Ruby" => "X = 1",
1252
+ "RawParseTree" => [:cdecl, :X, [:lit, 1]],
1253
+ "ParseTree" => s(:cdecl, :X, s(:lit, 1)))
1254
+
1255
+ add_tests("constY",
1256
+ "Ruby" => "::X = 1",
1257
+ "RawParseTree" => [:cdecl, [:colon3, :X], [:lit, 1]],
1258
+ "ParseTree" => s(:cdecl, s(:colon3, :X), s(:lit, 1)))
1259
+
1260
+ add_tests("constZ",
1261
+ "Ruby" => "X::Y = 1",
1262
+ "RawParseTree" => [:cdecl, [:colon2, [:const, :X], :Y], [:lit, 1]],
1263
+ "ParseTree" => s(:cdecl,
1264
+ s(:colon2, s(:const, :X), :Y),
1265
+ s(:lit, 1)))
1266
+
1267
+ add_tests("cvar",
1268
+ "Ruby" => "@@x",
1269
+ "RawParseTree" => [:cvar, :@@x],
1270
+ "ParseTree" => s(:cvar, :@@x))
1271
+
1272
+ add_tests("cvasgn",
1273
+ "Ruby" => "def x\n @@blah = 1\nend",
1274
+ "RawParseTree" => [:defn, :x,
1275
+ [:scope,
1276
+ [:block, [:args],
1277
+ [:cvasgn, :@@blah, [:lit, 1]]]]],
1278
+ "ParseTree" => s(:defn, :x,
1279
+ s(:args),
1280
+ s(:scope,
1281
+ s(:block,
1282
+ s(:cvasgn, :@@blah, s(:lit, 1))))))
1283
+
1284
+ add_tests("cvasgn_cls_method",
1285
+ "Ruby" => "def self.quiet_mode=(boolean)\n @@quiet_mode = boolean\nend",
1286
+ "RawParseTree" => [:defs, [:self], :quiet_mode=,
1287
+ [:scope,
1288
+ [:block,
1289
+ [:args, :boolean],
1290
+ [:cvasgn, :@@quiet_mode, [:lvar, :boolean]]]]],
1291
+ "ParseTree" => s(:defs, s(:self), :quiet_mode=,
1292
+ s(:args, :boolean),
1293
+ s(:scope,
1294
+ s(:block,
1295
+ s(:cvasgn, :@@quiet_mode,
1296
+ s(:lvar, :boolean))))))
1297
+
1298
+ add_tests("cvdecl",
1299
+ "Ruby" => "class X\n @@blah = 1\nend",
1300
+ "RawParseTree" => [:class, :X, nil,
1301
+ [:scope, [:cvdecl, :@@blah, [:lit, 1]]]],
1302
+ "ParseTree" => s(:class, :X, nil,
1303
+ s(:scope, s(:cvdecl, :@@blah, s(:lit, 1)))))
1304
+
1305
+ add_tests("dasgn_0",
1306
+ "Ruby" => "a.each { |x| b.each { |y| x = (x + 1) } if true }",
1307
+ "RawParseTree" => [:iter,
1308
+ [:call, [:vcall, :a], :each],
1309
+ [:dasgn_curr, :x],
1310
+ [:if, [:true],
1311
+ [:iter,
1312
+ [:call, [:vcall, :b], :each],
1313
+ [:dasgn_curr, :y],
1314
+ [:dasgn, :x,
1315
+ [:call, [:dvar, :x], :+,
1316
+ [:array, [:lit, 1]]]]],
1317
+ nil]],
1318
+ "ParseTree" => s(:iter,
1319
+ s(:call, s(:call, nil, :a, s(:arglist)), :each,
1320
+ s(:arglist)),
1321
+ s(:lasgn, :x),
1322
+ s(:if, s(:true),
1323
+ s(:iter,
1324
+ s(:call, s(:call, nil, :b, s(:arglist)),
1325
+ :each,
1326
+ s(:arglist)),
1327
+ s(:lasgn, :y),
1328
+ s(:lasgn, :x,
1329
+ s(:call, s(:lvar, :x), :+,
1330
+ s(:arglist, s(:lit, 1))))),
1331
+ nil)))
1332
+
1333
+ add_tests("dasgn_1",
1334
+ "Ruby" => "a.each { |x| b.each { |y| c = (c + 1) } if true }",
1335
+ "RawParseTree" => [:iter,
1336
+ [:call, [:vcall, :a], :each],
1337
+ [:dasgn_curr, :x],
1338
+ [:if, [:true],
1339
+ [:iter,
1340
+ [:call, [:vcall, :b], :each],
1341
+ [:dasgn_curr, :y],
1342
+ [:dasgn_curr, :c,
1343
+ [:call, [:dvar, :c], :+,
1344
+ [:array, [:lit, 1]]]]],
1345
+ nil]],
1346
+ "ParseTree" => s(:iter,
1347
+ s(:call, s(:call, nil, :a, s(:arglist)), :each,
1348
+ s(:arglist)),
1349
+ s(:lasgn, :x),
1350
+ s(:if, s(:true),
1351
+ s(:iter,
1352
+ s(:call, s(:call, nil, :b, s(:arglist)),
1353
+ :each,
1354
+ s(:arglist)),
1355
+ s(:lasgn, :y),
1356
+ s(:lasgn, :c,
1357
+ s(:call, s(:lvar, :c), :+,
1358
+ s(:arglist, s(:lit, 1))))),
1359
+ nil)))
1360
+
1361
+ add_tests("dasgn_2",
1362
+ "Ruby" => "a.each do |x|\n if true then\n c = 0\n b.each { |y| c = (c + 1) }\n end\nend", # FIX: hate that extra newline!
1363
+ "RawParseTree" => [:iter,
1364
+ [:call, [:vcall, :a], :each],
1365
+ [:dasgn_curr, :x],
1366
+ [:if, [:true],
1367
+ [:block,
1368
+ [:dasgn_curr, :c, [:lit, 0]],
1369
+ [:iter,
1370
+ [:call, [:vcall, :b], :each],
1371
+ [:dasgn_curr, :y],
1372
+ [:dasgn, :c,
1373
+ [:call, [:dvar, :c], :+,
1374
+ [:array, [:lit, 1]]]]]],
1375
+ nil]],
1376
+ "ParseTree" => s(:iter,
1377
+ s(:call, s(:call, nil, :a, s(:arglist)), :each,
1378
+ s(:arglist)),
1379
+ s(:lasgn, :x),
1380
+ s(:if, s(:true),
1381
+ s(:block,
1382
+ s(:lasgn, :c, s(:lit, 0)),
1383
+ s(:iter,
1384
+ s(:call, s(:call, nil, :b, s(:arglist)),
1385
+ :each,
1386
+ s(:arglist)),
1387
+ s(:lasgn, :y),
1388
+ s(:lasgn, :c,
1389
+ s(:call, s(:lvar, :c), :+,
1390
+ s(:arglist, s(:lit, 1)))))),
1391
+ nil)))
1392
+
1393
+ add_tests("dasgn_curr",
1394
+ "Ruby" => "data.each do |x, y|\n a = 1\n b = a\n b = a = x\nend",
1395
+ "RawParseTree" => [:iter,
1396
+ [:call, [:vcall, :data], :each],
1397
+ [:masgn,
1398
+ [:array, [:dasgn_curr, :x], [:dasgn_curr, :y]],
1399
+ nil, nil],
1400
+ [:block,
1401
+ [:dasgn_curr, :a, [:lit, 1]],
1402
+ [:dasgn_curr, :b, [:dvar, :a]],
1403
+ [:dasgn_curr, :b,
1404
+ [:dasgn_curr, :a, [:dvar, :x]]]]],
1405
+ "ParseTree" => s(:iter,
1406
+ s(:call, s(:call, nil, :data,
1407
+ s(:arglist)), :each, s(:arglist)),
1408
+ s(:masgn,
1409
+ s(:array, s(:lasgn, :x), s(:lasgn, :y))),
1410
+ s(:block,
1411
+ s(:lasgn, :a, s(:lit, 1)),
1412
+ s(:lasgn, :b, s(:lvar, :a)),
1413
+ s(:lasgn, :b, s(:lasgn, :a, s(:lvar, :x))))))
1414
+
1415
+ add_tests("dasgn_icky",
1416
+ "Ruby" => "a do\n v = nil\n assert_block(full_message) do\n begin\n yield\n rescue Exception => v\n break\n end\n end\nend",
1417
+ "RawParseTree" => [:iter,
1418
+ [:fcall, :a],
1419
+ nil,
1420
+ [:block,
1421
+ [:dasgn_curr, :v, [:nil]],
1422
+ [:iter,
1423
+ [:fcall, :assert_block,
1424
+ [:array, [:vcall, :full_message]]],
1425
+ nil,
1426
+ [:begin,
1427
+ [:rescue,
1428
+ [:yield],
1429
+ [:resbody,
1430
+ [:array, [:const, :Exception]],
1431
+ [:block,
1432
+ [:dasgn, :v,
1433
+ [:gvar, :$!]], [:break]]]]]]]],
1434
+ "ParseTree" => s(:iter,
1435
+ s(:call, nil, :a, s(:arglist)),
1436
+ nil,
1437
+ s(:block,
1438
+ s(:lasgn, :v, s(:nil)),
1439
+ s(:iter,
1440
+ s(:call, nil, :assert_block,
1441
+ s(:arglist,
1442
+ s(:call, nil, :full_message,
1443
+ s(:arglist)))),
1444
+ nil,
1445
+ s(:rescue,
1446
+ s(:yield),
1447
+ s(:resbody,
1448
+ s(:array,
1449
+ s(:const, :Exception),
1450
+ s(:lasgn, :v, s(:gvar, :$!))),
1451
+ s(:break)))))))
1452
+
1453
+ add_tests("dasgn_mixed",
1454
+ "Ruby" => "t = 0\nns.each { |n| t += n }\n",
1455
+ "RawParseTree" => [:block,
1456
+ [:lasgn, :t, [:lit, 0]],
1457
+ [:iter,
1458
+ [:call, [:vcall, :ns], :each],
1459
+ [:dasgn_curr, :n],
1460
+ [:lasgn, :t,
1461
+ [:call, [:lvar, :t], :+,
1462
+ [:array, [:dvar, :n]]]]]],
1463
+ "ParseTree" => s(:block,
1464
+ s(:lasgn, :t, s(:lit, 0)),
1465
+ s(:iter,
1466
+ s(:call, s(:call, nil, :ns,
1467
+ s(:arglist)), :each, s(:arglist)),
1468
+ s(:lasgn, :n),
1469
+ s(:lasgn, :t,
1470
+ s(:call, s(:lvar, :t), :+,
1471
+ s(:arglist, s(:lvar, :n)))))),
1472
+ "Ruby2Ruby" => "t = 0\nns.each { |n| t = (t + n) }\n")
1473
+
1474
+ add_tests("defined",
1475
+ "Ruby" => "defined? $x",
1476
+ "RawParseTree" => [:defined, [:gvar, :$x]],
1477
+ "ParseTree" => s(:defined, s(:gvar, :$x)))
1478
+
1479
+ # TODO: make all the defn_args* p their arglist
1480
+ add_tests("defn_args_block",
1481
+ "Ruby" => "def f(&block)\n # do nothing\nend",
1482
+ "RawParseTree" => [:defn, :f,
1483
+ [:scope,
1484
+ [:block,
1485
+ [:args],
1486
+ [:block_arg, :block],
1487
+ [:nil]]]],
1488
+ "ParseTree" => s(:defn, :f,
1489
+ s(:args, :"&block"),
1490
+ s(:scope, s(:block, s(:nil)))))
1491
+
1492
+ add_tests("defn_args_mand",
1493
+ "Ruby" => "def f(mand)\n # do nothing\nend",
1494
+ "RawParseTree" => [:defn, :f,
1495
+ [:scope,
1496
+ [:block,
1497
+ [:args, :mand],
1498
+ [:nil]]]],
1499
+ "ParseTree" => s(:defn, :f,
1500
+ s(:args, :mand),
1501
+ s(:scope, s(:block, s(:nil)))))
1502
+
1503
+ add_tests("defn_args_mand_block",
1504
+ "Ruby" => "def f(mand, &block)\n # do nothing\nend",
1505
+ "RawParseTree" => [:defn, :f,
1506
+ [:scope,
1507
+ [:block,
1508
+ [:args, :mand],
1509
+ [:block_arg, :block],
1510
+ [:nil]]]],
1511
+ "ParseTree" => s(:defn, :f,
1512
+ s(:args, :mand, :"&block"),
1513
+ s(:scope, s(:block, s(:nil)))))
1514
+
1515
+ add_tests("defn_args_mand_opt",
1516
+ "Ruby" => "def f(mand, opt = 42)\n # do nothing\nend",
1517
+ "RawParseTree" => [:defn, :f,
1518
+ [:scope,
1519
+ [:block,
1520
+ [:args, :mand, :opt,
1521
+ [:block,
1522
+ [:lasgn, :opt, [:lit, 42]]]],
1523
+ [:nil]]]],
1524
+ "ParseTree" => s(:defn, :f,
1525
+ s(:args, :mand, :opt,
1526
+ s(:block,
1527
+ s(:lasgn, :opt, s(:lit, 42)))),
1528
+ s(:scope, s(:block, s(:nil)))))
1529
+
1530
+ add_tests("defn_args_mand_opt_block",
1531
+ "Ruby" => "def f(mand, opt = 42, &block)\n # do nothing\nend",
1532
+ "RawParseTree" => [:defn, :f,
1533
+ [:scope,
1534
+ [:block,
1535
+ [:args, :mand, :opt,
1536
+ [:block,
1537
+ [:lasgn, :opt, [:lit, 42]]]],
1538
+ [:block_arg, :block],
1539
+ [:nil]]]],
1540
+ "ParseTree" => s(:defn, :f,
1541
+ s(:args, :mand, :opt, :"&block",
1542
+ s(:block,
1543
+ s(:lasgn, :opt, s(:lit, 42)))),
1544
+ s(:scope, s(:block, s(:nil)))))
1545
+
1546
+ add_tests("defn_args_mand_opt_splat",
1547
+ "Ruby" => "def f(mand, opt = 42, *rest)\n # do nothing\nend",
1548
+ "RawParseTree" => [:defn, :f,
1549
+ [:scope,
1550
+ [:block,
1551
+ [:args, :mand, :opt, :"*rest",
1552
+ [:block,
1553
+ [:lasgn, :opt, [:lit, 42]]]],
1554
+ [:nil]]]],
1555
+ "ParseTree" => s(:defn, :f,
1556
+ s(:args, :mand, :opt, :"*rest",
1557
+ s(:block,
1558
+ s(:lasgn, :opt, s(:lit, 42)))),
1559
+ s(:scope, s(:block, s(:nil)))))
1560
+
1561
+ add_tests("defn_args_mand_opt_splat_block",
1562
+ "Ruby" => "def f(mand, opt = 42, *rest, &block)\n # do nothing\nend",
1563
+ "RawParseTree" => [:defn, :f,
1564
+ [:scope,
1565
+ [:block,
1566
+ [:args, :mand, :opt, :"*rest",
1567
+ [:block,
1568
+ [:lasgn, :opt, [:lit, 42]]]],
1569
+ [:block_arg, :block],
1570
+ [:nil]]]],
1571
+ "ParseTree" => s(:defn, :f,
1572
+ s(:args, :mand, :opt, :"*rest", :"&block",
1573
+ s(:block,
1574
+ s(:lasgn, :opt, s(:lit, 42)))),
1575
+ s(:scope, s(:block, s(:nil)))))
1576
+
1577
+ add_tests("defn_args_mand_opt_splat_no_name",
1578
+ "Ruby" => "def x(a, b = 42, *)\n # do nothing\nend",
1579
+ "RawParseTree" => [:defn, :x,
1580
+ [:scope,
1581
+ [:block,
1582
+ [:args, :a, :b, :"*",
1583
+ [:block, [:lasgn, :b, [:lit, 42]]]],
1584
+ [:nil]]]],
1585
+ "ParseTree" => s(:defn, :x,
1586
+ s(:args, :a, :b, :"*",
1587
+ s(:block, s(:lasgn, :b, s(:lit, 42)))),
1588
+ s(:scope,
1589
+ s(:block,
1590
+ s(:nil)))))
1591
+
1592
+ add_tests("defn_args_mand_splat",
1593
+ "Ruby" => "def f(mand, *rest)\n # do nothing\nend",
1594
+ "RawParseTree" => [:defn, :f,
1595
+ [:scope,
1596
+ [:block,
1597
+ [:args, :mand, :"*rest"],
1598
+ [:nil]]]],
1599
+ "ParseTree" => s(:defn, :f,
1600
+ s(:args, :mand, :"*rest"),
1601
+ s(:scope, s(:block, s(:nil)))))
1602
+
1603
+ add_tests("defn_args_mand_splat_block",
1604
+ "Ruby" => "def f(mand, *rest, &block)\n # do nothing\nend",
1605
+ "RawParseTree" => [:defn, :f,
1606
+ [:scope,
1607
+ [:block,
1608
+ [:args, :mand, :"*rest"],
1609
+ [:block_arg, :block],
1610
+ [:nil]]]],
1611
+ "ParseTree" => s(:defn, :f,
1612
+ s(:args, :mand, :"*rest", :"&block"),
1613
+ s(:scope, s(:block, s(:nil)))))
1614
+
1615
+ add_tests("defn_args_mand_splat_no_name",
1616
+ "Ruby" => "def x(a, *args)\n p(a, args)\nend",
1617
+ "RawParseTree" => [:defn, :x,
1618
+ [:scope,
1619
+ [:block,
1620
+ [:args, :a, :"*args"],
1621
+ [:fcall, :p,
1622
+ [:array, [:lvar, :a], [:lvar, :args]]]]]],
1623
+ "ParseTree" => s(:defn, :x,
1624
+ s(:args, :a, :"*args"),
1625
+ s(:scope,
1626
+ s(:block,
1627
+ s(:call, nil, :p,
1628
+ s(:arglist, s(:lvar, :a), s(:lvar, :args)))))))
1629
+
1630
+ add_tests("defn_args_none",
1631
+ "Ruby" => "def empty\n # do nothing\nend",
1632
+ "RawParseTree" => [:defn, :empty,
1633
+ [:scope, [:block, [:args], [:nil]]]],
1634
+ "ParseTree" => s(:defn, :empty,
1635
+ s(:args),
1636
+ s(:scope, s(:block, s(:nil)))))
1637
+
1638
+ add_tests("defn_args_opt",
1639
+ "Ruby" => "def f(opt = 42)\n # do nothing\nend",
1640
+ "RawParseTree" => [:defn, :f,
1641
+ [:scope,
1642
+ [:block,
1643
+ [:args, :opt,
1644
+ [:block,
1645
+ [:lasgn, :opt, [:lit, 42]]]],
1646
+ [:nil]]]],
1647
+ "ParseTree" => s(:defn, :f,
1648
+ s(:args, :opt,
1649
+ s(:block,
1650
+ s(:lasgn, :opt, s(:lit, 42)))),
1651
+ s(:scope, s(:block, s(:nil)))))
1652
+
1653
+ add_tests("defn_args_opt_block",
1654
+ "Ruby" => "def f(opt = 42, &block)\n # do nothing\nend",
1655
+ "RawParseTree" => [:defn, :f,
1656
+ [:scope,
1657
+ [:block,
1658
+ [:args, :opt,
1659
+ [:block,
1660
+ [:lasgn, :opt, [:lit, 42]]]],
1661
+ [:block_arg, :block],
1662
+ [:nil]]]],
1663
+ "ParseTree" => s(:defn, :f,
1664
+ s(:args, :opt, :"&block",
1665
+ s(:block,
1666
+ s(:lasgn, :opt, s(:lit, 42)))),
1667
+ s(:scope, s(:block, s(:nil)))))
1668
+
1669
+ add_tests("defn_args_opt_splat",
1670
+ "Ruby" => "def f(opt = 42, *rest)\n # do nothing\nend",
1671
+ "RawParseTree" => [:defn, :f,
1672
+ [:scope,
1673
+ [:block,
1674
+ [:args, :opt, :"*rest",
1675
+ [:block,
1676
+ [:lasgn, :opt, [:lit, 42]]]],
1677
+ [:nil]]]],
1678
+ "ParseTree" => s(:defn, :f,
1679
+ s(:args, :opt, :"*rest",
1680
+ s(:block,
1681
+ s(:lasgn, :opt, s(:lit, 42)))),
1682
+ s(:scope, s(:block, s(:nil)))))
1683
+
1684
+ add_tests("defn_args_opt_splat_block",
1685
+ "Ruby" => "def f(opt = 42, *rest, &block)\n # do nothing\nend",
1686
+ "RawParseTree" => [:defn, :f,
1687
+ [:scope,
1688
+ [:block,
1689
+ [:args, :opt, :"*rest",
1690
+ [:block,
1691
+ [:lasgn, :opt, [:lit, 42]]]],
1692
+ [:block_arg, :block],
1693
+ [:nil]]]],
1694
+ "ParseTree" => s(:defn, :f,
1695
+ s(:args, :opt, :"*rest", :"&block",
1696
+ s(:block,
1697
+ s(:lasgn, :opt, s(:lit, 42)))),
1698
+ s(:scope, s(:block, s(:nil)))))
1699
+
1700
+ add_tests("defn_args_opt_splat_no_name",
1701
+ "Ruby" => "def x(b = 42, *)\n # do nothing\nend",
1702
+ "RawParseTree" => [:defn, :x,
1703
+ [:scope,
1704
+ [:block,
1705
+ [:args, :b, :"*",
1706
+ [:block, [:lasgn, :b, [:lit, 42]]]],
1707
+ [:nil]]]],
1708
+ "ParseTree" => s(:defn, :x,
1709
+ s(:args, :b, :"*",
1710
+ s(:block, s(:lasgn, :b, s(:lit, 42)))),
1711
+ s(:scope,
1712
+ s(:block,
1713
+ s(:nil)))))
1714
+
1715
+ add_tests("defn_args_splat",
1716
+ "Ruby" => "def f(*rest)\n # do nothing\nend",
1717
+ "RawParseTree" => [:defn, :f,
1718
+ [:scope,
1719
+ [:block,
1720
+ [:args, :"*rest"],
1721
+ [:nil]]]],
1722
+ "ParseTree" => s(:defn, :f,
1723
+ s(:args, :"*rest"),
1724
+ s(:scope, s(:block, s(:nil)))))
1725
+
1726
+ add_tests("defn_args_splat_no_name",
1727
+ "Ruby" => "def x(*)\n # do nothing\nend",
1728
+ "RawParseTree" => [:defn, :x,
1729
+ [:scope,
1730
+ [:block,
1731
+ [:args, :"*"],
1732
+ [:nil]]]],
1733
+ "ParseTree" => s(:defn, :x,
1734
+ s(:args, :"*"),
1735
+ s(:scope,
1736
+ s(:block,
1737
+ s(:nil)))))
1738
+
1739
+ add_tests("defn_or",
1740
+ "Ruby" => "def |(o)\n # do nothing\nend",
1741
+ "RawParseTree" => [:defn, :|,
1742
+ [:scope, [:block, [:args, :o], [:nil]]]],
1743
+ "ParseTree" => s(:defn, :|,
1744
+ s(:args, :o),
1745
+ s(:scope, s(:block, s(:nil)))))
1746
+
1747
+ add_tests("defn_rescue",
1748
+ "Ruby" => "def eql?(resource)\n (self.uuid == resource.uuid)\nrescue\n false\nend",
1749
+ "RawParseTree" => [:defn, :eql?,
1750
+ [:scope,
1751
+ [:block,
1752
+ [:args, :resource],
1753
+ [:rescue,
1754
+ [:call,
1755
+ [:call, [:self], :uuid],
1756
+ :==,
1757
+ [:array,
1758
+ [:call, [:lvar, :resource], :uuid]]],
1759
+ [:resbody, nil, [:false]]]]]],
1760
+ "ParseTree" => s(:defn, :eql?,
1761
+ s(:args, :resource),
1762
+ s(:scope,
1763
+ s(:block,
1764
+ s(:rescue,
1765
+ s(:call,
1766
+ s(:call, s(:self), :uuid, s(:arglist)),
1767
+ :==,
1768
+ s(:arglist,
1769
+ s(:call, s(:lvar, :resource),
1770
+ :uuid, s(:arglist)))),
1771
+ s(:resbody, s(:array), s(:false)))))),
1772
+ "Ruby2Ruby" => "def eql?(resource)\n (self.uuid == resource.uuid) rescue false\nend")
1773
+
1774
+ add_tests("defn_rescue_mri_verbose_flag",
1775
+ "Ruby" => "def eql?(resource)\n (self.uuid == resource.uuid)\nrescue\n false\nend",
1776
+ "RawParseTree" => [:defn, :eql?,
1777
+ [:scope,
1778
+ [:block,
1779
+ [:args, :resource],
1780
+ [:rescue,
1781
+ [:call,
1782
+ [:call, [:self], :uuid],
1783
+ :==,
1784
+ [:array,
1785
+ [:call, [:lvar, :resource], :uuid]]],
1786
+ [:resbody, nil, [:false]]]]]],
1787
+ "ParseTree" => s(:defn, :eql?,
1788
+ s(:args, :resource),
1789
+ s(:scope,
1790
+ s(:block,
1791
+ s(:rescue,
1792
+ s(:call,
1793
+ s(:call, s(:self), :uuid, s(:arglist)),
1794
+ :==,
1795
+ s(:arglist,
1796
+ s(:call, s(:lvar, :resource),
1797
+ :uuid, s(:arglist)))),
1798
+ s(:resbody, s(:array), s(:false)))))),
1799
+ "Ruby2Ruby" => "def eql?(resource)\n (self.uuid == resource.uuid) rescue false\nend")
1800
+
1801
+ add_tests("defn_something_eh",
1802
+ "Ruby" => "def something?\n # do nothing\nend",
1803
+ "RawParseTree" => [:defn, :something?,
1804
+ [:scope, [:block, [:args], [:nil]]]],
1805
+ "ParseTree" => s(:defn, :something?,
1806
+ s(:args),
1807
+ s(:scope, s(:block, s(:nil)))))
1808
+
1809
+ add_tests("defn_splat_no_name",
1810
+ "Ruby" => "def x(a, *)\n p(a)\nend",
1811
+ "RawParseTree" => [:defn, :x,
1812
+ [:scope,
1813
+ [:block,
1814
+ [:args, :a, :"*"],
1815
+ [:fcall, :p,
1816
+ [:array, [:lvar, :a]]]]]],
1817
+ "ParseTree" => s(:defn, :x,
1818
+ s(:args, :a, :"*"),
1819
+ s(:scope,
1820
+ s(:block,
1821
+ s(:call, nil, :p,
1822
+ s(:arglist, s(:lvar, :a)))))))
1823
+
1824
+ add_tests("defn_zarray",
1825
+ "Ruby" => "def zarray\n a = []\n return a\nend",
1826
+ "RawParseTree" => [:defn, :zarray,
1827
+ [:scope,
1828
+ [:block, [:args],
1829
+ [:lasgn, :a, [:zarray]],
1830
+ [:return, [:lvar, :a]]]]],
1831
+ "ParseTree" => s(:defn, :zarray,
1832
+ s(:args),
1833
+ s(:scope,
1834
+ s(:block,
1835
+ s(:lasgn, :a, s(:array)),
1836
+ s(:return, s(:lvar, :a))))))
1837
+
1838
+ add_tests("defs",
1839
+ "Ruby" => "def self.x(y)\n (y + 1)\nend",
1840
+ "RawParseTree" => [:defs, [:self], :x,
1841
+ [:scope,
1842
+ [:block,
1843
+ [:args, :y],
1844
+ [:call, [:lvar, :y], :+,
1845
+ [:array, [:lit, 1]]]]]],
1846
+ "ParseTree" => s(:defs, s(:self), :x,
1847
+ s(:args, :y),
1848
+ s(:scope,
1849
+ s(:block,
1850
+ s(:call, s(:lvar, :y), :+,
1851
+ s(:arglist, s(:lit, 1)))))))
1852
+
1853
+ add_tests("defs_empty",
1854
+ "Ruby" => "def self.empty\n # do nothing\nend",
1855
+ "RawParseTree" => [:defs, [:self], :empty,
1856
+ [:scope, [:args]]],
1857
+ "ParseTree" => s(:defs, s(:self), :empty,
1858
+ s(:args),
1859
+ s(:scope, s(:block))))
1860
+
1861
+ add_tests("defs_empty_args",
1862
+ "Ruby" => "def self.empty(*)\n # do nothing\nend",
1863
+ "RawParseTree" => [:defs, [:self], :empty,
1864
+ [:scope, [:args, :*]]],
1865
+ "ParseTree" => s(:defs, s(:self), :empty,
1866
+ s(:args, :*),
1867
+ s(:scope, s(:block))))
1868
+
1869
+ add_tests("defs_expr_wtf",
1870
+ "Ruby" => "def (a.b).empty(*)\n # do nothing\nend",
1871
+ "RawParseTree" => [:defs,
1872
+ [:call, [:vcall, :a], :b],
1873
+ :empty,
1874
+ [:scope, [:args, :*]]],
1875
+ "ParseTree" => s(:defs,
1876
+ s(:call,
1877
+ s(:call, nil, :a, s(:arglist)),
1878
+ :b, s(:arglist)),
1879
+ :empty,
1880
+ s(:args, :*),
1881
+ s(:scope, s(:block))))
1882
+
1883
+ add_tests("dmethod",
1884
+ "Ruby" => [Examples, :dmethod_added],
1885
+ "RawParseTree" => [:defn, :dmethod_added,
1886
+ [:dmethod,
1887
+ :a_method,
1888
+ [:scope,
1889
+ [:block,
1890
+ [:args, :x],
1891
+ [:call, [:lvar, :x], :+,
1892
+ [:array, [:lit, 1]]]]]]],
1893
+ "ParseTree" => s(:defn, :dmethod_added,
1894
+ s(:args, :x),
1895
+ s(:scope,
1896
+ s(:block,
1897
+ s(:call, s(:lvar, :x), :+,
1898
+ s(:arglist, s(:lit, 1)))))),
1899
+ "Ruby2Ruby" => "def dmethod_added(x)\n (x + 1)\nend")
1900
+
1901
+ add_tests("dot2",
1902
+ "Ruby" => "(a..b)",
1903
+ "RawParseTree" => [:dot2, [:vcall, :a], [:vcall, :b]],
1904
+ "ParseTree" => s(:dot2,
1905
+ s(:call, nil, :a, s(:arglist)),
1906
+ s(:call, nil, :b, s(:arglist))))
1907
+
1908
+ add_tests("dot3",
1909
+ "Ruby" => "(a...b)",
1910
+ "RawParseTree" => [:dot3, [:vcall, :a], [:vcall, :b]],
1911
+ "ParseTree" => s(:dot3,
1912
+ s(:call, nil, :a, s(:arglist)),
1913
+ s(:call, nil, :b, s(:arglist))))
1914
+
1915
+ add_tests("dregx",
1916
+ "Ruby" => "/x#\{(1 + 1)}y/",
1917
+ "RawParseTree" => [:dregx, "x",
1918
+ [:evstr,
1919
+ [:call, [:lit, 1], :+, [:array, [:lit, 1]]]],
1920
+ [:str, "y"]],
1921
+ "ParseTree" => s(:dregx, "x",
1922
+ s(:evstr,
1923
+ s(:call, s(:lit, 1), :+,
1924
+ s(:arglist, s(:lit, 1)))),
1925
+ s(:str, "y")))
1926
+
1927
+ add_tests("dregx_interp",
1928
+ "Ruby" => "/#\{@rakefile}/",
1929
+ "RawParseTree" => [:dregx, '', [:evstr, [:ivar, :@rakefile]]],
1930
+ "ParseTree" => s(:dregx, '', s(:evstr, s(:ivar, :@rakefile))))
1931
+
1932
+ add_tests("dregx_interp_empty",
1933
+ "Ruby" => "/a#\{}b/",
1934
+ "RawParseTree" => [:dregx, 'a', [:evstr], [:str, "b"]],
1935
+ "ParseTree" => s(:dregx, 'a', s(:evstr), s(:str, "b")))
1936
+
1937
+ add_tests("dregx_n",
1938
+ "Ruby" => '/#{1}/n',
1939
+ "RawParseTree" => [:dregx, '', [:evstr, [:lit, 1]], /x/n.options],
1940
+ "ParseTree" => s(:dregx, '',
1941
+ s(:evstr, s(:lit, 1)), /x/n.options),
1942
+ "Ruby2Ruby" => "/#\{1}/") # HACK - need to support regexp flag
1943
+
1944
+ add_tests("dregx_once",
1945
+ "Ruby" => "/x#\{(1 + 1)}y/o",
1946
+ "RawParseTree" => [:dregx_once, "x",
1947
+ [:evstr,
1948
+ [:call, [:lit, 1], :+, [:array, [:lit, 1]]]],
1949
+ [:str, "y"]],
1950
+ "ParseTree" => s(:dregx_once, "x",
1951
+ s(:evstr,
1952
+ s(:call, s(:lit, 1), :+,
1953
+ s(:arglist, s(:lit, 1)))),
1954
+ s(:str, "y")))
1955
+
1956
+ add_tests("dregx_once_n_interp",
1957
+ "Ruby" => "/#\{IAC}#\{SB}/no",
1958
+ "RawParseTree" => [:dregx_once, '',
1959
+ [:evstr, [:const, :IAC]],
1960
+ [:evstr, [:const, :SB]], /x/n.options],
1961
+ "ParseTree" => s(:dregx_once, '',
1962
+ s(:evstr, s(:const, :IAC)),
1963
+ s(:evstr, s(:const, :SB)), /x/n.options),
1964
+ "Ruby2Ruby" => "/#\{IAC}#\{SB}/o") # HACK
1965
+
1966
+ add_tests("dstr",
1967
+ "Ruby" => "argl = 1\n\"x#\{argl}y\"\n",
1968
+ "RawParseTree" => [:block,
1969
+ [:lasgn, :argl, [:lit, 1]],
1970
+ [:dstr, "x", [:evstr, [:lvar, :argl]],
1971
+ [:str, "y"]]],
1972
+ "ParseTree" => s(:block,
1973
+ s(:lasgn, :argl, s(:lit, 1)),
1974
+ s(:dstr, "x", s(:evstr, s(:lvar, :argl)),
1975
+ s(:str, "y"))))
1976
+
1977
+ add_tests("dstr_2",
1978
+ "Ruby" => "argl = 1\n\"x#\{(\"%.2f\" % 3.14159)}y\"\n",
1979
+ "RawParseTree" => [:block,
1980
+ [:lasgn, :argl, [:lit, 1]],
1981
+ [:dstr,
1982
+ "x",
1983
+ [:evstr,
1984
+ [:call, [:str, "%.2f"], :%,
1985
+ [:array, [:lit, 3.14159]]]],
1986
+ [:str, "y"]]],
1987
+ "ParseTree" => s(:block,
1988
+ s(:lasgn, :argl, s(:lit, 1)),
1989
+ s(:dstr,
1990
+ "x",
1991
+ s(:evstr,
1992
+ s(:call, s(:str, "%.2f"), :%,
1993
+ s(:arglist, s(:lit, 3.14159)))),
1994
+ s(:str, "y"))))
1995
+
1996
+ add_tests("dstr_3",
1997
+ "Ruby" => "max = 2\nargl = 1\n\"x#\{(\"%.#\{max}f\" % 3.14159)}y\"\n",
1998
+ "RawParseTree" => [:block,
1999
+ [:lasgn, :max, [:lit, 2]],
2000
+ [:lasgn, :argl, [:lit, 1]],
2001
+ [:dstr, "x",
2002
+ [:evstr,
2003
+ [:call,
2004
+ [:dstr, "%.",
2005
+ [:evstr, [:lvar, :max]],
2006
+ [:str, "f"]],
2007
+ :%,
2008
+ [:array, [:lit, 3.14159]]]],
2009
+ [:str, "y"]]],
2010
+ "ParseTree" => s(:block,
2011
+ s(:lasgn, :max, s(:lit, 2)),
2012
+ s(:lasgn, :argl, s(:lit, 1)),
2013
+ s(:dstr, "x",
2014
+ s(:evstr,
2015
+ s(:call,
2016
+ s(:dstr, "%.",
2017
+ s(:evstr, s(:lvar, :max)),
2018
+ s(:str, "f")),
2019
+ :%,
2020
+ s(:arglist, s(:lit, 3.14159)))),
2021
+ s(:str, "y"))))
2022
+
2023
+ add_tests("dstr_concat",
2024
+ "Ruby" => '"#{22}aa" "cd#{44}" "55" "#{66}"',
2025
+ "RawParseTree" => [:dstr,
2026
+ "",
2027
+ [:evstr, [:lit, 22]],
2028
+ [:str, "aa"],
2029
+ [:str, "cd"],
2030
+ [:evstr, [:lit, 44]],
2031
+ [:str, "55"],
2032
+ [:evstr, [:lit, 66]]],
2033
+ "ParseTree" => s(:dstr,
2034
+ "",
2035
+ s(:evstr, s(:lit, 22)),
2036
+ s(:str, "aa"),
2037
+ s(:str, "cd"),
2038
+ s(:evstr, s(:lit, 44)),
2039
+ s(:str, "55"),
2040
+ s(:evstr, s(:lit, 66))),
2041
+ "Ruby2Ruby" => '"#{22}aacd#{44}55#{66}"')
2042
+
2043
+ add_tests("dstr_gross",
2044
+ "Ruby" => '"a #$global b #@ivar c #@@cvar d"',
2045
+ "RawParseTree" => [:dstr, "a ",
2046
+ [:evstr, [:gvar, :$global]],
2047
+ [:str, " b "],
2048
+ [:evstr, [:ivar, :@ivar]],
2049
+ [:str, " c "],
2050
+ [:evstr, [:cvar, :@@cvar]],
2051
+ [:str, " d"]],
2052
+ "ParseTree" => s(:dstr, "a ",
2053
+ s(:evstr, s(:gvar, :$global)),
2054
+ s(:str, " b "),
2055
+ s(:evstr, s(:ivar, :@ivar)),
2056
+ s(:str, " c "),
2057
+ s(:evstr, s(:cvar, :@@cvar)),
2058
+ s(:str, " d")),
2059
+ "Ruby2Ruby" => '"a #{$global} b #{@ivar} c #{@@cvar} d"')
2060
+
2061
+ add_tests("dstr_heredoc_expand",
2062
+ "Ruby" => "<<EOM\n blah\n#\{1 + 1}blah\nEOM\n",
2063
+ "RawParseTree" => [:dstr, " blah\n",
2064
+ [:evstr, [:call, [:lit, 1], :+,
2065
+ [:array, [:lit, 1]]]],
2066
+ [:str, "blah\n"]],
2067
+ "ParseTree" => s(:dstr, " blah\n",
2068
+ s(:evstr, s(:call, s(:lit, 1), :+,
2069
+ s(:arglist, s(:lit, 1)))),
2070
+ s(:str, "blah\n")),
2071
+ "Ruby2Ruby" => "\" blah\\n#\{(1 + 1)}blah\\n\"")
2072
+
2073
+ add_tests("dstr_heredoc_windoze_sucks",
2074
+ "Ruby" => "<<-EOF\r\ndef test_#\{action}_valid_feed\r\n EOF\r\n",
2075
+ "RawParseTree" => [:dstr,
2076
+ 'def test_',
2077
+ [:evstr, [:vcall, :action]],
2078
+ [:str, "_valid_feed\n"]],
2079
+ "ParseTree" => s(:dstr,
2080
+ 'def test_',
2081
+ s(:evstr, s(:call, nil, :action, s(:arglist))),
2082
+ s(:str, "_valid_feed\n")),
2083
+ "Ruby2Ruby" => "\"def test_#\{action}_valid_feed\\n\"")
2084
+
2085
+ add_tests("dstr_heredoc_yet_again",
2086
+ "Ruby" => "<<-EOF\ns1 '#\{RUBY_PLATFORM}' s2\n#\{__FILE__}\n EOF\n",
2087
+ "RawParseTree" => [:dstr, "s1 '",
2088
+ [:evstr, [:const, :RUBY_PLATFORM]],
2089
+ [:str, "' s2\n"],
2090
+ [:str, "(string)"],
2091
+ [:str, "\n"]],
2092
+ "ParseTree" => s(:dstr, "s1 '",
2093
+ s(:evstr, s(:const, :RUBY_PLATFORM)),
2094
+ s(:str, "' s2\n"),
2095
+ s(:str, "(string)"),
2096
+ s(:str, "\n")),
2097
+ "Ruby2Ruby" => "\"s1 '#\{RUBY_PLATFORM}' s2\\n(string)\\n\"")
2098
+
2099
+ add_tests("dstr_nest",
2100
+ "Ruby" => "%Q[before [#\{nest}] after]",
2101
+ "RawParseTree" => [:dstr, "before [",
2102
+ [:evstr, [:vcall, :nest]], [:str, "] after"]],
2103
+ "ParseTree" => s(:dstr, "before [",
2104
+ s(:evstr, s(:call, nil, :nest, s(:arglist))),
2105
+ s(:str, "] after")),
2106
+ "Ruby2Ruby" => "\"before [#\{nest}] after\"")
2107
+
2108
+ add_tests("dstr_str_lit_start",
2109
+ "Ruby" => '"#{"blah"}#{__FILE__}:#{__LINE__}: warning: #{$!.message} (#{$!.class})"',
2110
+ "RawParseTree" => [:dstr,
2111
+ "blah(string):",
2112
+ [:evstr, [:lit, 1]],
2113
+ [:str, ": warning: "],
2114
+ [:evstr, [:call, [:gvar, :$!], :message]],
2115
+ [:str, " ("],
2116
+ [:evstr, [:call, [:gvar, :$!], :class]],
2117
+ [:str, ")"]],
2118
+ "ParseTree" => s(:dstr,
2119
+ "blah(string):",
2120
+ s(:evstr, s(:lit, 1)),
2121
+ s(:str, ": warning: "),
2122
+ s(:evstr, s(:call, s(:gvar, :$!), :message,
2123
+ s(:arglist))),
2124
+ s(:str, " ("),
2125
+ s(:evstr, s(:call, s(:gvar, :$!), :class,
2126
+ s(:arglist))),
2127
+ s(:str, ")")),
2128
+ "Ruby2Ruby" => '"blah(string):#{1}: warning: #{$!.message} (#{$!.class})"')
2129
+
2130
+ add_tests("dstr_the_revenge",
2131
+ "Ruby" => '"before #{from} middle #{to} (#{__FILE__}:#{__LINE__})"',
2132
+ "RawParseTree" => [:dstr,
2133
+ "before ",
2134
+ [:evstr, [:vcall, :from]],
2135
+ [:str, " middle "],
2136
+ [:evstr, [:vcall, :to]],
2137
+ [:str, " ("],
2138
+ [:str, "(string)"],
2139
+ [:str, ":"],
2140
+ [:evstr, [:lit, 1]],
2141
+ [:str, ")"]],
2142
+ "ParseTree" => s(:dstr,
2143
+ "before ",
2144
+ s(:evstr, s(:call, nil, :from, s(:arglist))),
2145
+ s(:str, " middle "),
2146
+ s(:evstr, s(:call, nil, :to, s(:arglist))),
2147
+ s(:str, " ("),
2148
+ s(:str, "(string)"),
2149
+ s(:str, ":"),
2150
+ s(:evstr, s(:lit, 1)),
2151
+ s(:str, ")")),
2152
+ "Ruby2Ruby" => '"before #{from} middle #{to} ((string):#{1})"')
2153
+
2154
+ add_tests("dsym",
2155
+ "Ruby" => ":\"x#\{(1 + 1)}y\"",
2156
+ "RawParseTree" => [:dsym, "x",
2157
+ [:evstr, [:call, [:lit, 1], :+,
2158
+ [:array, [:lit, 1]]]], [:str, "y"]],
2159
+ "ParseTree" => s(:dsym, "x",
2160
+ s(:evstr, s(:call, s(:lit, 1), :+,
2161
+ s(:arglist, s(:lit, 1)))), s(:str, "y")))
2162
+
2163
+ add_tests("dxstr",
2164
+ "Ruby" => "t = 5\n`touch #\{t}`\n",
2165
+ "RawParseTree" => [:block,
2166
+ [:lasgn, :t, [:lit, 5]],
2167
+ [:dxstr, 'touch ', [:evstr, [:lvar, :t]]]],
2168
+ "ParseTree" => s(:block,
2169
+ s(:lasgn, :t, s(:lit, 5)),
2170
+ s(:dxstr, 'touch ', s(:evstr, s(:lvar, :t)))))
2171
+
2172
+ add_tests("ensure",
2173
+ "Ruby" => "begin\n (1 + 1)\nrescue SyntaxError => e1\n 2\nrescue Exception => e2\n 3\nelse\n 4\nensure\n 5\nend",
2174
+ "RawParseTree" => [:begin,
2175
+ [:ensure,
2176
+ [:rescue,
2177
+ [:call, [:lit, 1], :+, [:array, [:lit, 1]]],
2178
+ [:resbody,
2179
+ [:array, [:const, :SyntaxError]],
2180
+ [:block,
2181
+ [:lasgn, :e1, [:gvar, :$!]], [:lit, 2]],
2182
+ [:resbody,
2183
+ [:array, [:const, :Exception]],
2184
+ [:block,
2185
+ [:lasgn, :e2, [:gvar, :$!]], [:lit, 3]]]],
2186
+ [:lit, 4]],
2187
+ [:lit, 5]]],
2188
+ "ParseTree" => s(:ensure,
2189
+ s(:rescue,
2190
+ s(:call, s(:lit, 1), :+,
2191
+ s(:arglist, s(:lit, 1))),
2192
+ s(:resbody,
2193
+ s(:array,
2194
+ s(:const, :SyntaxError),
2195
+ s(:lasgn, :e1, s(:gvar, :$!))),
2196
+ s(:lit, 2)),
2197
+ s(:resbody,
2198
+ s(:array,
2199
+ s(:const, :Exception),
2200
+ s(:lasgn, :e2, s(:gvar, :$!))),
2201
+ s(:lit, 3)),
2202
+ s(:lit, 4)),
2203
+ s(:lit, 5)))
2204
+
2205
+ add_tests("false",
2206
+ "Ruby" => "false",
2207
+ "RawParseTree" => [:false],
2208
+ "ParseTree" => s(:false))
2209
+
2210
+ add_tests("fbody",
2211
+ "Ruby" => [Examples, :an_alias],
2212
+ "RawParseTree" => [:defn, :an_alias,
2213
+ [:fbody,
2214
+ [:scope,
2215
+ [:block,
2216
+ [:args, :x],
2217
+ [:call, [:lvar, :x], :+,
2218
+ [:array, [:lit, 1]]]]]]],
2219
+ "ParseTree" => s(:defn, :an_alias,
2220
+ s(:args, :x),
2221
+ s(:scope,
2222
+ s(:block,
2223
+ s(:call, s(:lvar, :x), :+,
2224
+ s(:arglist, s(:lit, 1)))))),
2225
+ "Ruby2Ruby" => "def an_alias(x)\n (x + 1)\nend")
2226
+
2227
+ add_tests("fcall_arglist",
2228
+ "Ruby" => "m(42)",
2229
+ "RawParseTree" => [:fcall, :m, [:array, [:lit, 42]]],
2230
+ "ParseTree" => s(:call, nil, :m, s(:arglist, s(:lit, 42))))
2231
+
2232
+ add_tests("fcall_arglist_hash",
2233
+ "Ruby" => "m(:a => 1, :b => 2)",
2234
+ "RawParseTree" => [:fcall, :m,
2235
+ [:array,
2236
+ [:hash,
2237
+ [:lit, :a], [:lit, 1],
2238
+ [:lit, :b], [:lit, 2]]]],
2239
+ "ParseTree" => s(:call, nil, :m,
2240
+ s(:arglist,
2241
+ s(:hash,
2242
+ s(:lit, :a), s(:lit, 1),
2243
+ s(:lit, :b), s(:lit, 2)))))
2244
+
2245
+ add_tests("fcall_arglist_norm_hash",
2246
+ "Ruby" => "m(42, :a => 1, :b => 2)",
2247
+ "RawParseTree" => [:fcall, :m,
2248
+ [:array,
2249
+ [:lit, 42],
2250
+ [:hash,
2251
+ [:lit, :a], [:lit, 1],
2252
+ [:lit, :b], [:lit, 2]]]],
2253
+ "ParseTree" => s(:call, nil, :m,
2254
+ s(:arglist,
2255
+ s(:lit, 42),
2256
+ s(:hash,
2257
+ s(:lit, :a), s(:lit, 1),
2258
+ s(:lit, :b), s(:lit, 2)))))
2259
+
2260
+ add_tests("fcall_arglist_norm_hash_splat",
2261
+ "Ruby" => "m(42, :a => 1, :b => 2, *c)",
2262
+ "RawParseTree" => [:fcall, :m,
2263
+ [:argscat,
2264
+ [:array,
2265
+ [:lit, 42],
2266
+ [:hash,
2267
+ [:lit, :a], [:lit, 1],
2268
+ [:lit, :b], [:lit, 2]]],
2269
+ [:vcall, :c]]],
2270
+ "ParseTree" => s(:call, nil, :m,
2271
+ s(:arglist,
2272
+ s(:lit, 42),
2273
+ s(:hash,
2274
+ s(:lit, :a), s(:lit, 1),
2275
+ s(:lit, :b), s(:lit, 2)),
2276
+ s(:splat, s(:call, nil, :c, s(:arglist))))))
2277
+
2278
+ add_tests("fcall_block",
2279
+ "Ruby" => "a(:b) { :c }",
2280
+ "RawParseTree" => [:iter,
2281
+ [:fcall, :a, [:array, [:lit, :b]]], nil,
2282
+ [:lit, :c]],
2283
+ "ParseTree" => s(:iter,
2284
+ s(:call, nil, :a,
2285
+ s(:arglist, s(:lit, :b))), nil,
2286
+ s(:lit, :c)))
2287
+
2288
+ add_tests("fcall_index_space",
2289
+ "Ruby" => "a [42]",
2290
+ "RawParseTree" => [:fcall, :a, [:array, [:array, [:lit, 42]]]],
2291
+ "ParseTree" => s(:call, nil, :a,
2292
+ s(:arglist, s(:array, s(:lit, 42)))),
2293
+ "Ruby2Ruby" => "a([42])")
2294
+
2295
+ add_tests("fcall_keyword",
2296
+ "Ruby" => "42 if block_given?",
2297
+ "RawParseTree" => [:if, [:fcall, :block_given?], [:lit, 42], nil],
2298
+ "ParseTree" => s(:if,
2299
+ s(:call, nil, :block_given?, s(:arglist)),
2300
+ s(:lit, 42), nil))
2301
+
2302
+ add_tests("flip2",
2303
+ "Ruby" => "x = if ((i % 4) == 0)..((i % 3) == 0) then\n i\nelse\n nil\nend",
2304
+ "RawParseTree" => [:lasgn,
2305
+ :x,
2306
+ [:if,
2307
+ [:flip2,
2308
+ [:call,
2309
+ [:call, [:vcall, :i], :%,
2310
+ [:array, [:lit, 4]]],
2311
+ :==,
2312
+ [:array, [:lit, 0]]],
2313
+ [:call,
2314
+ [:call, [:vcall, :i], :%,
2315
+ [:array, [:lit, 3]]],
2316
+ :==,
2317
+ [:array, [:lit, 0]]]],
2318
+ [:vcall, :i],
2319
+ [:nil]]],
2320
+ "ParseTree" => s(:lasgn,
2321
+ :x,
2322
+ s(:if,
2323
+ s(:flip2,
2324
+ s(:call,
2325
+ s(:call, s(:call, nil, :i, s(:arglist)),
2326
+ :%,
2327
+ s(:arglist, s(:lit, 4))),
2328
+ :==,
2329
+ s(:arglist, s(:lit, 0))),
2330
+ s(:call,
2331
+ s(:call, s(:call, nil, :i, s(:arglist)),
2332
+ :%,
2333
+ s(:arglist, s(:lit, 3))),
2334
+ :==,
2335
+ s(:arglist, s(:lit, 0)))),
2336
+ s(:call, nil, :i, s(:arglist)),
2337
+ s(:nil))))
2338
+
2339
+ add_tests("flip2_method",
2340
+ "Ruby" => "if 1..2.a?(b) then\n nil\nend",
2341
+ "RawParseTree" => [:if,
2342
+ [:flip2,
2343
+ [:lit, 1],
2344
+ [:call, [:lit, 2], :a?,
2345
+ [:array, [:vcall, :b]]]],
2346
+ [:nil],
2347
+ nil],
2348
+ "ParseTree" => s(:if,
2349
+ s(:flip2,
2350
+ s(:lit, 1),
2351
+ s(:call, s(:lit, 2), :a?,
2352
+ s(:arglist,
2353
+ s(:call, nil, :b, s(:arglist))))),
2354
+ s(:nil),
2355
+ nil))
2356
+
2357
+ add_tests("flip3",
2358
+ "Ruby" => "x = if ((i % 4) == 0)...((i % 3) == 0) then\n i\nelse\n nil\nend",
2359
+ "RawParseTree" => [:lasgn,
2360
+ :x,
2361
+ [:if,
2362
+ [:flip3,
2363
+ [:call,
2364
+ [:call, [:vcall, :i], :%,
2365
+ [:array, [:lit, 4]]],
2366
+ :==,
2367
+ [:array, [:lit, 0]]],
2368
+ [:call,
2369
+ [:call, [:vcall, :i], :%,
2370
+ [:array, [:lit, 3]]],
2371
+ :==,
2372
+ [:array, [:lit, 0]]]],
2373
+ [:vcall, :i],
2374
+ [:nil]]],
2375
+ "ParseTree" => s(:lasgn,
2376
+ :x,
2377
+ s(:if,
2378
+ s(:flip3,
2379
+ s(:call,
2380
+ s(:call, s(:call, nil, :i, s(:arglist)),
2381
+ :%,
2382
+ s(:arglist, s(:lit, 4))),
2383
+ :==,
2384
+ s(:arglist, s(:lit, 0))),
2385
+ s(:call,
2386
+ s(:call, s(:call, nil, :i, s(:arglist)),
2387
+ :%,
2388
+ s(:arglist, s(:lit, 3))),
2389
+ :==,
2390
+ s(:arglist, s(:lit, 0)))),
2391
+ s(:call, nil, :i, s(:arglist)),
2392
+ s(:nil))))
2393
+
2394
+ add_tests("for",
2395
+ "Ruby" => "for o in ary do\n puts(o)\nend",
2396
+ "RawParseTree" => [:for,
2397
+ [:vcall, :ary],
2398
+ [:lasgn, :o],
2399
+ [:fcall, :puts, [:array, [:lvar, :o]]]],
2400
+ "ParseTree" => s(:for,
2401
+ s(:call, nil, :ary, s(:arglist)),
2402
+ s(:lasgn, :o),
2403
+ s(:call, nil, :puts,
2404
+ s(:arglist, s(:lvar, :o)))))
2405
+
2406
+ add_tests("for_no_body",
2407
+ "Ruby" => "for i in (0..max) do\n # do nothing\nend",
2408
+ "RawParseTree" => [:for,
2409
+ [:dot2, [:lit, 0], [:vcall, :max]],
2410
+ [:lasgn, :i]],
2411
+ "ParseTree" => s(:for,
2412
+ s(:dot2,
2413
+ s(:lit, 0),
2414
+ s(:call, nil, :max, s(:arglist))),
2415
+ s(:lasgn, :i)))
2416
+
2417
+ add_tests("gasgn",
2418
+ "Ruby" => "$x = 42",
2419
+ "RawParseTree" => [:gasgn, :$x, [:lit, 42]],
2420
+ "ParseTree" => s(:gasgn, :$x, s(:lit, 42)))
2421
+
2422
+ add_tests("global",
2423
+ "Ruby" => "$stderr",
2424
+ "RawParseTree" => [:gvar, :$stderr],
2425
+ "ParseTree" => s(:gvar, :$stderr))
2426
+
2427
+ add_tests("gvar",
2428
+ "Ruby" => "$x",
2429
+ "RawParseTree" => [:gvar, :$x],
2430
+ "ParseTree" => s(:gvar, :$x))
2431
+
2432
+ add_tests("gvar_underscore",
2433
+ "Ruby" => "$_",
2434
+ "RawParseTree" => [:gvar, :$_],
2435
+ "ParseTree" => s(:gvar, :$_))
2436
+
2437
+ add_tests("gvar_underscore_blah",
2438
+ "Ruby" => "$__blah",
2439
+ "RawParseTree" => [:gvar, :$__blah],
2440
+ "ParseTree" => s(:gvar, :$__blah))
2441
+
2442
+ add_tests("hash",
2443
+ "Ruby" => "{ 1 => 2, 3 => 4 }",
2444
+ "RawParseTree" => [:hash,
2445
+ [:lit, 1], [:lit, 2],
2446
+ [:lit, 3], [:lit, 4]],
2447
+ "ParseTree" => s(:hash,
2448
+ s(:lit, 1), s(:lit, 2),
2449
+ s(:lit, 3), s(:lit, 4)))
2450
+
2451
+ add_tests("hash_rescue",
2452
+ "Ruby" => "{ 1 => (2 rescue 3) }",
2453
+ "RawParseTree" => [:hash,
2454
+ [:lit, 1],
2455
+ [:rescue,
2456
+ [:lit, 2],
2457
+ [:resbody, nil, [:lit, 3]]]],
2458
+ "ParseTree" => s(:hash,
2459
+ s(:lit, 1),
2460
+ s(:rescue,
2461
+ s(:lit, 2),
2462
+ s(:resbody, s(:array), s(:lit, 3)))))
2463
+
2464
+ add_tests("iasgn",
2465
+ "Ruby" => "@a = 4",
2466
+ "RawParseTree" => [:iasgn, :@a, [:lit, 4]],
2467
+ "ParseTree" => s(:iasgn, :@a, s(:lit, 4)))
2468
+
2469
+ add_tests("if_block_condition",
2470
+ "Ruby" => "if (x = 5\n(x + 1)) then\n nil\nend",
2471
+ "RawParseTree" => [:if,
2472
+ [:block,
2473
+ [:lasgn, :x, [:lit, 5]],
2474
+ [:call,
2475
+ [:lvar, :x],
2476
+ :+,
2477
+ [:array, [:lit, 1]]]],
2478
+ [:nil],
2479
+ nil],
2480
+ "ParseTree" => s(:if,
2481
+ s(:block,
2482
+ s(:lasgn, :x, s(:lit, 5)),
2483
+ s(:call,
2484
+ s(:lvar, :x),
2485
+ :+,
2486
+ s(:arglist, s(:lit, 1)))),
2487
+ s(:nil),
2488
+ nil))
2489
+
2490
+ add_tests("if_lasgn_short",
2491
+ "Ruby" => "if x = obj.x then\n x.do_it\nend",
2492
+ "RawParseTree" => [:if,
2493
+ [:lasgn, :x,
2494
+ [:call, [:vcall, :obj], :x]],
2495
+ [:call,
2496
+ [:lvar, :x], :do_it],
2497
+ nil],
2498
+ "ParseTree" => s(:if,
2499
+ s(:lasgn, :x,
2500
+ s(:call,
2501
+ s(:call, nil, :obj, s(:arglist)),
2502
+ :x, s(:arglist))),
2503
+ s(:call, s(:lvar, :x), :do_it, s(:arglist)),
2504
+ nil))
2505
+
2506
+ add_tests("if_nested",
2507
+ "Ruby" => "return if false unless true",
2508
+ "RawParseTree" => [:if, [:true], nil,
2509
+ [:if, [:false], [:return], nil]],
2510
+ "ParseTree" => s(:if, s(:true), nil,
2511
+ s(:if, s(:false), s(:return), nil)))
2512
+
2513
+ add_tests("if_post",
2514
+ "Ruby" => "a if b",
2515
+ "RawParseTree" => [:if, [:vcall, :b], [:vcall, :a], nil],
2516
+ "ParseTree" => s(:if, s(:call, nil, :b, s(:arglist)),
2517
+ s(:call, nil, :a, s(:arglist)), nil))
2518
+
2519
+ add_tests("if_post_not",
2520
+ "Ruby" => "a if not b",
2521
+ "RawParseTree" => [:if, [:vcall, :b], nil, [:vcall, :a]],
2522
+ "ParseTree" => s(:if, s(:call, nil, :b, s(:arglist)), nil,
2523
+ s(:call, nil, :a, s(:arglist))),
2524
+ "Ruby2Ruby" => "a unless b")
2525
+
2526
+ add_tests("if_pre",
2527
+ "Ruby" => "if b then a end",
2528
+ "RawParseTree" => [:if, [:vcall, :b], [:vcall, :a], nil],
2529
+ "ParseTree" => s(:if, s(:call, nil, :b, s(:arglist)),
2530
+ s(:call, nil, :a, s(:arglist)), nil),
2531
+ "Ruby2Ruby" => "a if b")
2532
+
2533
+ add_tests("if_pre_not",
2534
+ "Ruby" => "if not b then a end",
2535
+ "RawParseTree" => [:if, [:vcall, :b], nil, [:vcall, :a]],
2536
+ "ParseTree" => s(:if, s(:call, nil, :b, s(:arglist)), nil,
2537
+ s(:call, nil, :a, s(:arglist))),
2538
+ "Ruby2Ruby" => "a unless b")
2539
+
2540
+ add_tests("iter_call_arglist_space",
2541
+ "Ruby" => "a (1) {|c|d}",
2542
+ "RawParseTree" => [:iter,
2543
+ [:fcall, :a, [:array, [:lit, 1]]],
2544
+ [:dasgn_curr, :c],
2545
+ [:vcall, :d]],
2546
+ "ParseTree" => s(:iter,
2547
+ s(:call, nil, :a, s(:arglist, s(:lit, 1))),
2548
+ s(:lasgn, :c),
2549
+ s(:call, nil, :d, s(:arglist))),
2550
+ "Ruby2Ruby" => "a(1) { |c| d }")
2551
+
2552
+ add_tests("iter_dasgn_curr_dasgn_madness",
2553
+ "Ruby" => "as.each { |a|\n b += a.b(false) }",
2554
+ "RawParseTree" => [:iter,
2555
+ [:call, [:vcall, :as], :each],
2556
+ [:dasgn_curr, :a],
2557
+ [:dasgn_curr,
2558
+ :b,
2559
+ [:call,
2560
+ [:dvar, :b],
2561
+ :+,
2562
+ [:array,
2563
+ [:call, [:dvar, :a], :b,
2564
+ [:array, [:false]]]]]]],
2565
+ "ParseTree" => s(:iter,
2566
+ s(:call,
2567
+ s(:call, nil, :as, s(:arglist)),
2568
+ :each, s(:arglist)),
2569
+ s(:lasgn, :a),
2570
+ s(:lasgn, :b,
2571
+ s(:call,
2572
+ s(:lvar, :b),
2573
+ :+,
2574
+ s(:arglist,
2575
+ s(:call, s(:lvar, :a), :b,
2576
+ s(:arglist, s(:false))))))),
2577
+ "Ruby2Ruby" => "as.each { |a| b = (b + a.b(false)) }")
2578
+
2579
+ add_tests("iter_downto",
2580
+ "Ruby" => "3.downto(1) { |n| puts(n.to_s) }",
2581
+ "RawParseTree" => [:iter,
2582
+ [:call, [:lit, 3], :downto, [:array, [:lit, 1]]],
2583
+ [:dasgn_curr, :n],
2584
+ [:fcall, :puts,
2585
+ [:array, [:call, [:dvar, :n], :to_s]]]],
2586
+ "ParseTree" => s(:iter,
2587
+ s(:call, s(:lit, 3), :downto,
2588
+ s(:arglist, s(:lit, 1))),
2589
+ s(:lasgn, :n),
2590
+ s(:call, nil, :puts,
2591
+ s(:arglist,
2592
+ s(:call, s(:lvar, :n),
2593
+ :to_s, s(:arglist))))))
2594
+
2595
+ add_tests("iter_each_lvar",
2596
+ "Ruby" => "array = [1, 2, 3]\narray.each { |x| puts(x.to_s) }\n",
2597
+ "RawParseTree" => [:block,
2598
+ [:lasgn, :array,
2599
+ [:array, [:lit, 1], [:lit, 2], [:lit, 3]]],
2600
+ [:iter,
2601
+ [:call, [:lvar, :array], :each],
2602
+ [:dasgn_curr, :x],
2603
+ [:fcall, :puts,
2604
+ [:array, [:call, [:dvar, :x], :to_s]]]]],
2605
+ "ParseTree" => s(:block,
2606
+ s(:lasgn, :array,
2607
+ s(:array,
2608
+ s(:lit, 1), s(:lit, 2), s(:lit, 3))),
2609
+ s(:iter,
2610
+ s(:call, s(:lvar, :array), :each,
2611
+ s(:arglist)),
2612
+ s(:lasgn, :x),
2613
+ s(:call, nil, :puts,
2614
+ s(:arglist, s(:call, s(:lvar, :x),
2615
+ :to_s, s(:arglist)))))))
2616
+
2617
+ add_tests("iter_each_nested",
2618
+ "Ruby" => "array1 = [1, 2, 3]\narray2 = [4, 5, 6, 7]\narray1.each do |x|\n array2.each do |y|\n puts(x.to_s)\n puts(y.to_s)\n end\nend\n",
2619
+ "RawParseTree" => [:block,
2620
+ [:lasgn, :array1,
2621
+ [:array, [:lit, 1], [:lit, 2], [:lit, 3]]],
2622
+ [:lasgn, :array2,
2623
+ [:array,
2624
+ [:lit, 4], [:lit, 5], [:lit, 6], [:lit, 7]]],
2625
+ [:iter,
2626
+ [:call,
2627
+ [:lvar, :array1], :each],
2628
+ [:dasgn_curr, :x],
2629
+ [:iter,
2630
+ [:call,
2631
+ [:lvar, :array2], :each],
2632
+ [:dasgn_curr, :y],
2633
+ [:block,
2634
+ [:fcall, :puts,
2635
+ [:array, [:call, [:dvar, :x], :to_s]]],
2636
+ [:fcall, :puts,
2637
+ [:array, [:call, [:dvar, :y], :to_s]]]]]]],
2638
+ "ParseTree" => s(:block,
2639
+ s(:lasgn, :array1,
2640
+ s(:array,
2641
+ s(:lit, 1), s(:lit, 2), s(:lit, 3))),
2642
+ s(:lasgn, :array2,
2643
+ s(:array,
2644
+ s(:lit, 4), s(:lit, 5),
2645
+ s(:lit, 6), s(:lit, 7))),
2646
+ s(:iter,
2647
+ s(:call,
2648
+ s(:lvar, :array1), :each, s(:arglist)),
2649
+ s(:lasgn, :x),
2650
+ s(:iter,
2651
+ s(:call,
2652
+ s(:lvar, :array2), :each, s(:arglist)),
2653
+ s(:lasgn, :y),
2654
+ s(:block,
2655
+ s(:call, nil, :puts,
2656
+ s(:arglist,
2657
+ s(:call, s(:lvar, :x),
2658
+ :to_s, s(:arglist)))),
2659
+ s(:call, nil, :puts,
2660
+ s(:arglist,
2661
+ s(:call, s(:lvar, :y),
2662
+ :to_s, s(:arglist)))))))))
2663
+
2664
+ add_tests("iter_loop_empty",
2665
+ "Ruby" => "loop { }",
2666
+ "RawParseTree" => [:iter, [:fcall, :loop], nil],
2667
+ "ParseTree" => s(:iter, s(:call, nil, :loop, s(:arglist)), nil))
2668
+
2669
+ add_tests("iter_masgn_2",
2670
+ "Ruby" => "a { |b, c| p(c) }",
2671
+ "RawParseTree" => [:iter,
2672
+ [:fcall, :a],
2673
+ [:masgn,
2674
+ [:array, [:dasgn_curr, :b], [:dasgn_curr, :c]],
2675
+ nil, nil],
2676
+ [:fcall, :p, [:array, [:dvar, :c]]]],
2677
+ "ParseTree" => s(:iter,
2678
+ s(:call, nil, :a, s(:arglist)),
2679
+ s(:masgn,
2680
+ s(:array, s(:lasgn, :b), s(:lasgn, :c))),
2681
+ s(:call, nil, :p, s(:arglist, s(:lvar, :c)))))
2682
+
2683
+ add_tests("iter_masgn_args_splat",
2684
+ "Ruby" => "a { |b, c, *d| p(c) }",
2685
+ "RawParseTree" => [:iter,
2686
+ [:fcall, :a],
2687
+ [:masgn,
2688
+ [:array, [:dasgn_curr, :b], [:dasgn_curr, :c]],
2689
+ [:dasgn_curr, :d], nil],
2690
+ [:fcall, :p, [:array, [:dvar, :c]]]],
2691
+ "ParseTree" => s(:iter,
2692
+ s(:call, nil, :a, s(:arglist)),
2693
+ s(:masgn,
2694
+ s(:array,
2695
+ s(:lasgn, :b),
2696
+ s(:lasgn, :c),
2697
+ s(:splat, s(:lasgn, :d)))),
2698
+ s(:call, nil, :p, s(:arglist, s(:lvar, :c)))))
2699
+
2700
+ add_tests("iter_masgn_args_splat_no_name",
2701
+ "Ruby" => "a { |b, c, *| p(c) }",
2702
+ "RawParseTree" => [:iter,
2703
+ [:fcall, :a],
2704
+ [:masgn,
2705
+ [:array, [:dasgn_curr, :b], [:dasgn_curr, :c]],
2706
+ [:splat], nil],
2707
+ [:fcall, :p, [:array, [:dvar, :c]]]],
2708
+ "ParseTree" => s(:iter,
2709
+ s(:call, nil, :a, s(:arglist)),
2710
+ s(:masgn,
2711
+ s(:array,
2712
+ s(:lasgn, :b),
2713
+ s(:lasgn, :c),
2714
+ s(:splat))),
2715
+ s(:call, nil, :p, s(:arglist, s(:lvar, :c)))))
2716
+
2717
+ add_tests("iter_masgn_splat",
2718
+ "Ruby" => "a { |*c| p(c) }",
2719
+ "RawParseTree" => [:iter,
2720
+ [:fcall, :a],
2721
+ [:masgn, nil, [:dasgn_curr, :c], nil],
2722
+ [:fcall, :p, [:array, [:dvar, :c]]]],
2723
+ "ParseTree" => s(:iter,
2724
+ s(:call, nil, :a, s(:arglist)),
2725
+ s(:masgn, s(:array, s(:splat, s(:lasgn, :c)))),
2726
+ s(:call, nil, :p, s(:arglist, s(:lvar, :c)))))
2727
+
2728
+ add_tests("iter_masgn_splat_no_name",
2729
+ "Ruby" => "a { |*| p(c) }",
2730
+ "RawParseTree" => [:iter,
2731
+ [:fcall, :a],
2732
+ [:masgn, nil, [:splat], nil],
2733
+ [:fcall, :p, [:array, [:vcall, :c]]]],
2734
+ "ParseTree" => s(:iter,
2735
+ s(:call, nil, :a, s(:arglist)),
2736
+ s(:masgn, s(:array, s(:splat))),
2737
+ s(:call, nil, :p,
2738
+ s(:arglist, s(:call, nil, :c, s(:arglist))))))
2739
+
2740
+ add_tests("iter_shadowed_var",
2741
+ "Ruby" => "a do |x|\n b do |x|\n puts x\n end\nend",
2742
+ "RawParseTree" => [:iter,
2743
+ [:fcall, :a],
2744
+ [:dasgn_curr, :x],
2745
+ [:iter,
2746
+ [:fcall, :b],
2747
+ [:dasgn, :x],
2748
+ [:fcall, :puts, [:array, [:dvar, :x]]]]],
2749
+ "ParseTree" => s(:iter,
2750
+ s(:call, nil, :a, s(:arglist)),
2751
+ s(:lasgn, :x),
2752
+ s(:iter,
2753
+ s(:call, nil, :b, s(:arglist)),
2754
+ s(:lasgn, :x),
2755
+ s(:call, nil, :puts,
2756
+ s(:arglist, s(:lvar, :x))))),
2757
+ "Ruby2Ruby" => "a { |x| b { |x| puts(x) } }")
2758
+
2759
+ add_tests("iter_upto",
2760
+ "Ruby" => "1.upto(3) { |n| puts(n.to_s) }",
2761
+ "RawParseTree" => [:iter,
2762
+ [:call, [:lit, 1], :upto, [:array, [:lit, 3]]],
2763
+ [:dasgn_curr, :n],
2764
+ [:fcall, :puts,
2765
+ [:array, [:call, [:dvar, :n], :to_s]]]],
2766
+ "ParseTree" => s(:iter,
2767
+ s(:call, s(:lit, 1), :upto,
2768
+ s(:arglist, s(:lit, 3))),
2769
+ s(:lasgn, :n),
2770
+ s(:call, nil, :puts,
2771
+ s(:arglist,
2772
+ s(:call, s(:lvar, :n), :to_s,
2773
+ s(:arglist))))))
2774
+
2775
+ add_tests("iter_while",
2776
+ "Ruby" => "argl = 10\nwhile (argl >= 1) do\n puts(\"hello\")\n argl = (argl - 1)\nend\n",
2777
+ "RawParseTree" => [:block,
2778
+ [:lasgn, :argl, [:lit, 10]],
2779
+ [:while,
2780
+ [:call, [:lvar, :argl], :">=",
2781
+ [:array, [:lit, 1]]],
2782
+ [:block,
2783
+ [:fcall, :puts, [:array, [:str, "hello"]]],
2784
+ [:lasgn,
2785
+ :argl,
2786
+ [:call, [:lvar, :argl],
2787
+ :"-", [:array, [:lit, 1]]]]], true]],
2788
+ "ParseTree" => s(:block,
2789
+ s(:lasgn, :argl, s(:lit, 10)),
2790
+ s(:while,
2791
+ s(:call, s(:lvar, :argl), :">=",
2792
+ s(:arglist, s(:lit, 1))),
2793
+ s(:block,
2794
+ s(:call, nil, :puts,
2795
+ s(:arglist, s(:str, "hello"))),
2796
+ s(:lasgn,
2797
+ :argl,
2798
+ s(:call, s(:lvar, :argl), :"-",
2799
+ s(:arglist, s(:lit, 1))))), true)))
2800
+
2801
+ add_tests("ivar",
2802
+ "Ruby" => [Examples, :reader],
2803
+ "RawParseTree" => [:defn, :reader, [:ivar, :@reader]],
2804
+ "ParseTree" => s(:defn, :reader, # FIX should be unified?
2805
+ s(:args),
2806
+ s(:ivar, :@reader)),
2807
+ "Ruby2Ruby" => "attr_reader :reader")
2808
+
2809
+ add_tests("lasgn_array",
2810
+ "Ruby" => "var = [\"foo\", \"bar\"]",
2811
+ "RawParseTree" => [:lasgn, :var,
2812
+ [:array,
2813
+ [:str, "foo"],
2814
+ [:str, "bar"]]],
2815
+ "ParseTree" => s(:lasgn, :var,
2816
+ s(:array,
2817
+ s(:str, "foo"),
2818
+ s(:str, "bar"))))
2819
+
2820
+ add_tests("lasgn_call",
2821
+ "Ruby" => "c = (2 + 3)",
2822
+ "RawParseTree" => [:lasgn, :c, [:call, [:lit, 2], :+,
2823
+ [:array, [:lit, 3]]]],
2824
+ "ParseTree" => s(:lasgn, :c, s(:call, s(:lit, 2), :+,
2825
+ s(:arglist, s(:lit, 3)))))
2826
+
2827
+ add_tests("lit_bool_false",
2828
+ "Ruby" => "false",
2829
+ "RawParseTree" => [:false],
2830
+ "ParseTree" => s(:false))
2831
+
2832
+ add_tests("lit_bool_true",
2833
+ "Ruby" => "true",
2834
+ "RawParseTree" => [:true],
2835
+ "ParseTree" => s(:true))
2836
+
2837
+ add_tests("lit_float",
2838
+ "Ruby" => "1.1",
2839
+ "RawParseTree" => [:lit, 1.1],
2840
+ "ParseTree" => s(:lit, 1.1))
2841
+
2842
+ add_tests("lit_long",
2843
+ "Ruby" => "1",
2844
+ "RawParseTree" => [:lit, 1],
2845
+ "ParseTree" => s(:lit, 1))
2846
+
2847
+ add_tests("lit_long_negative",
2848
+ "Ruby" => "-1",
2849
+ "RawParseTree" => [:lit, -1],
2850
+ "ParseTree" => s(:lit, -1))
2851
+
2852
+ add_tests("lit_range2",
2853
+ "Ruby" => "(1..10)",
2854
+ "RawParseTree" => [:lit, 1..10],
2855
+ "ParseTree" => s(:lit, 1..10))
2856
+
2857
+ add_tests("lit_range3",
2858
+ "Ruby" => "(1...10)",
2859
+ "RawParseTree" => [:lit, 1...10],
2860
+ "ParseTree" => s(:lit, 1...10))
2861
+
2862
+ # TODO: discuss and decide which lit we like
2863
+ # it "converts a regexp to an sexp" do
2864
+ # "/blah/".to_sexp.should == s(:regex, "blah", 0)
2865
+ # "/blah/i".to_sexp.should == s(:regex, "blah", 1)
2866
+ # "/blah/u".to_sexp.should == s(:regex, "blah", 64)
2867
+ # end
2868
+
2869
+ add_tests("lit_regexp",
2870
+ "Ruby" => "/x/",
2871
+ "RawParseTree" => [:lit, /x/],
2872
+ "ParseTree" => s(:lit, /x/))
2873
+
2874
+ add_tests("lit_regexp_i_wwtt",
2875
+ "Ruby" => 'str.split(//i)',
2876
+ "RawParseTree" => [:call, [:vcall, :str], :split,
2877
+ [:array, [:lit, //i]]],
2878
+ "ParseTree" => s(:call, s(:call, nil, :str, s(:arglist)), :split,
2879
+ s(:arglist, s(:lit, //i))))
2880
+
2881
+ add_tests("lit_regexp_n",
2882
+ "Ruby" => "/x/n", # HACK differs on 1.9 - this is easiest
2883
+ "RawParseTree" => [:lit, /x/n],
2884
+ "ParseTree" => s(:lit, /x/n),
2885
+ "Ruby2Ruby" => /x/n.inspect)
2886
+
2887
+ add_tests("lit_regexp_once",
2888
+ "Ruby" => "/x/o",
2889
+ "RawParseTree" => [:lit, /x/],
2890
+ "ParseTree" => s(:lit, /x/),
2891
+ "Ruby2Ruby" => "/x/")
2892
+
2893
+ add_tests("lit_sym",
2894
+ "Ruby" => ":x",
2895
+ "RawParseTree" => [:lit, :x],
2896
+ "ParseTree" => s(:lit, :x))
2897
+
2898
+ add_tests("lit_sym_splat",
2899
+ "Ruby" => ":\"*args\"",
2900
+ "RawParseTree" => [:lit, :"*args"],
2901
+ "ParseTree" => s(:lit, :"*args"))
2902
+
2903
+ add_tests("lvar_def_boundary",
2904
+ "Ruby" => "b = 42\ndef a\n c do\n begin\n do_stuff\n rescue RuntimeError => b\n puts(b)\n end\n end\nend\n",
2905
+ "RawParseTree" => [:block,
2906
+ [:lasgn, :b, [:lit, 42]],
2907
+ [:defn, :a,
2908
+ [:scope,
2909
+ [:block,
2910
+ [:args],
2911
+ [:iter,
2912
+ [:fcall, :c],
2913
+ nil,
2914
+ [:begin,
2915
+ [:rescue,
2916
+ [:vcall, :do_stuff],
2917
+ [:resbody,
2918
+ [:array, [:const, :RuntimeError]],
2919
+ [:block,
2920
+ [:dasgn_curr, :b, [:gvar, :$!]],
2921
+ [:fcall, :puts,
2922
+ [:array, [:dvar, :b]]]]]]]]]]]],
2923
+ "ParseTree" => s(:block,
2924
+ s(:lasgn, :b, s(:lit, 42)),
2925
+ s(:defn, :a,
2926
+ s(:args),
2927
+ s(:scope,
2928
+ s(:block,
2929
+ s(:iter,
2930
+ s(:call, nil, :c, s(:arglist)),
2931
+ nil,
2932
+ s(:rescue,
2933
+ s(:call, nil, :do_stuff, s(:arglist)),
2934
+ s(:resbody,
2935
+ s(:array,
2936
+ s(:const, :RuntimeError),
2937
+ s(:lasgn, :b, s(:gvar, :$!))),
2938
+ s(:call, nil, :puts,
2939
+ s(:arglist,
2940
+ s(:lvar, :b)))))))))))
2941
+
2942
+ add_tests("masgn",
2943
+ "Ruby" => "a, b = c, d",
2944
+ "RawParseTree" => [:masgn,
2945
+ [:array, [:lasgn, :a], [:lasgn, :b]], nil,
2946
+ [:array, [:vcall, :c], [:vcall, :d]]],
2947
+ "ParseTree" => s(:masgn,
2948
+ s(:array, s(:lasgn, :a), s(:lasgn, :b)),
2949
+ s(:array, s(:call, nil, :c, s(:arglist)),
2950
+ s(:call, nil, :d, s(:arglist)))))
2951
+
2952
+ add_tests("masgn_argscat",
2953
+ "Ruby" => "a, b, *c = 1, 2, *[3, 4]",
2954
+ "RawParseTree" => [:masgn,
2955
+ [:array, [:lasgn, :a], [:lasgn, :b]],
2956
+ [:lasgn, :c],
2957
+ [:argscat,
2958
+ [:array, [:lit, 1], [:lit, 2]],
2959
+ [:array, [:lit, 3], [:lit, 4]]]],
2960
+ "ParseTree" => s(:masgn,
2961
+ s(:array,
2962
+ s(:lasgn, :a),
2963
+ s(:lasgn, :b),
2964
+ s(:splat, s(:lasgn, :c))),
2965
+ s(:array,
2966
+ s(:lit, 1), s(:lit, 2),
2967
+ s(:splat,
2968
+ s(:array, s(:lit, 3), s(:lit, 4))))))
2969
+
2970
+ add_tests("masgn_attrasgn",
2971
+ "Ruby" => "a, b.c = d, e",
2972
+ "RawParseTree" => [:masgn,
2973
+ [:array, [:lasgn, :a],
2974
+ [:attrasgn, [:vcall, :b], :c=]], nil,
2975
+ [:array, [:vcall, :d], [:vcall, :e]]],
2976
+ "ParseTree" => s(:masgn,
2977
+ s(:array,
2978
+ s(:lasgn, :a),
2979
+ s(:attrasgn,
2980
+ s(:call, nil, :b, s(:arglist)),
2981
+ :c=, s(:arglist))),
2982
+ s(:array,
2983
+ s(:call, nil, :d, s(:arglist)),
2984
+ s(:call, nil, :e, s(:arglist)))))
2985
+
2986
+ add_tests("masgn_attrasgn_array_rhs",
2987
+ "Ruby" => "a.b, a.c, _ = q",
2988
+ "RawParseTree" => [:masgn,
2989
+ [:array,
2990
+ [:attrasgn, [:vcall, :a], :b=],
2991
+ [:attrasgn, [:vcall, :a], :c=],
2992
+ [:lasgn, :_]], nil,
2993
+ [:to_ary, [:vcall, :q]]],
2994
+ "ParseTree" => s(:masgn,
2995
+ s(:array,
2996
+ s(:attrasgn,
2997
+ s(:call, nil, :a, s(:arglist)),
2998
+ :b=, s(:arglist)),
2999
+ s(:attrasgn,
3000
+ s(:call, nil, :a, s(:arglist)),
3001
+ :c=, s(:arglist)),
3002
+ s(:lasgn, :_)),
3003
+ s(:to_ary,
3004
+ s(:call, nil, :q, s(:arglist)))))
3005
+
3006
+ add_tests("masgn_attrasgn_idx",
3007
+ "Ruby" => "a, i, j = [], 1, 2\na[i], a[j] = a[j], a[i]\n",
3008
+ "RawParseTree" => [:block,
3009
+ [:masgn,
3010
+ [:array,
3011
+ [:lasgn, :a], [:lasgn, :i], [:lasgn, :j]], nil,
3012
+ [:array, [:zarray], [:lit, 1], [:lit, 2]]],
3013
+ [:masgn,
3014
+ [:array,
3015
+ [:attrasgn,
3016
+ [:lvar, :a], :[]=, [:array, [:lvar, :i]]],
3017
+ [:attrasgn,
3018
+ [:lvar, :a], :[]=, [:array, [:lvar, :j]]]],
3019
+ nil,
3020
+ [:array,
3021
+ [:call, [:lvar, :a], :[],
3022
+ [:array, [:lvar, :j]]],
3023
+ [:call, [:lvar, :a], :[],
3024
+ [:array, [:lvar, :i]]]]]],
3025
+ "ParseTree" => s(:block,
3026
+ s(:masgn,
3027
+ s(:array,
3028
+ s(:lasgn, :a),
3029
+ s(:lasgn, :i), s(:lasgn, :j)),
3030
+ s(:array, s(:array), s(:lit, 1), s(:lit, 2))),
3031
+ s(:masgn,
3032
+ s(:array,
3033
+ s(:attrasgn, s(:lvar, :a), :[]=,
3034
+ s(:arglist, s(:lvar, :i))),
3035
+ s(:attrasgn, s(:lvar, :a), :[]=,
3036
+ s(:arglist, s(:lvar, :j)))),
3037
+ s(:array,
3038
+ s(:call, s(:lvar, :a), :[],
3039
+ s(:arglist, s(:lvar, :j))),
3040
+ s(:call, s(:lvar, :a), :[],
3041
+ s(:arglist, s(:lvar, :i)))))))
3042
+
3043
+ add_tests("masgn_cdecl",
3044
+ "Ruby" => "A, B, C = 1, 2, 3",
3045
+ "RawParseTree" => [:masgn,
3046
+ [:array, [:cdecl, :A], [:cdecl, :B],
3047
+ [:cdecl, :C]], nil,
3048
+ [:array, [:lit, 1], [:lit, 2], [:lit, 3]]],
3049
+ "ParseTree" => s(:masgn,
3050
+ s(:array, s(:cdecl, :A), s(:cdecl, :B),
3051
+ s(:cdecl, :C)),
3052
+ s(:array, s(:lit, 1), s(:lit, 2), s(:lit, 3))))
3053
+
3054
+
3055
+ add_tests("masgn_iasgn",
3056
+ "Ruby" => "a, @b = c, d",
3057
+ "RawParseTree" => [:masgn,
3058
+ [:array, [:lasgn, :a], [:iasgn, :"@b"]], nil,
3059
+ [:array, [:vcall, :c], [:vcall, :d]]],
3060
+ "ParseTree" => s(:masgn,
3061
+ s(:array, s(:lasgn, :a), s(:iasgn, :"@b")),
3062
+ s(:array,
3063
+ s(:call, nil, :c, s(:arglist)),
3064
+ s(:call, nil, :d, s(:arglist)))))
3065
+
3066
+ add_tests("masgn_masgn",
3067
+ "Ruby" => "a, (b, c) = [1, [2, 3]]",
3068
+ "RawParseTree" => [:masgn,
3069
+ [:array,
3070
+ [:lasgn, :a],
3071
+ [:masgn,
3072
+ [:array,
3073
+ [:lasgn, :b],
3074
+ [:lasgn, :c]], nil, nil]],
3075
+ nil,
3076
+ [:to_ary,
3077
+ [:array,
3078
+ [:lit, 1],
3079
+ [:array,
3080
+ [:lit, 2],
3081
+ [:lit, 3]]]]],
3082
+ "ParseTree" => s(:masgn,
3083
+ s(:array,
3084
+ s(:lasgn, :a),
3085
+ s(:masgn,
3086
+ s(:array,
3087
+ s(:lasgn, :b),
3088
+ s(:lasgn, :c)))),
3089
+ s(:to_ary,
3090
+ s(:array,
3091
+ s(:lit, 1),
3092
+ s(:array,
3093
+ s(:lit, 2),
3094
+ s(:lit, 3))))))
3095
+
3096
+ add_tests("masgn_splat",
3097
+ "Ruby" => "a, b, *c = d, e, f, g",
3098
+ "RawParseTree" => [:masgn,
3099
+ [:array, [:lasgn, :a], [:lasgn, :b]],
3100
+ [:lasgn, :c],
3101
+ [:array,
3102
+ [:vcall, :d], [:vcall, :e],
3103
+ [:vcall, :f], [:vcall, :g]]],
3104
+ "ParseTree" => s(:masgn,
3105
+ s(:array,
3106
+ s(:lasgn, :a),
3107
+ s(:lasgn, :b),
3108
+ s(:splat, s(:lasgn, :c))),
3109
+ s(:array,
3110
+ s(:call, nil, :d, s(:arglist)),
3111
+ s(:call, nil, :e, s(:arglist)),
3112
+ s(:call, nil, :f, s(:arglist)),
3113
+ s(:call, nil, :g, s(:arglist)))))
3114
+
3115
+ add_tests("masgn_splat_no_name_to_ary",
3116
+ "Ruby" => "a, b, * = c",
3117
+ "RawParseTree" => [:masgn,
3118
+ [:array, [:lasgn, :a], [:lasgn, :b]],
3119
+ [:splat],
3120
+ [:to_ary, [:vcall, :c]]],
3121
+ "ParseTree" => s(:masgn,
3122
+ s(:array,
3123
+ s(:lasgn, :a),
3124
+ s(:lasgn, :b),
3125
+ s(:splat)),
3126
+ s(:to_ary, s(:call, nil, :c, s(:arglist)))))
3127
+
3128
+ add_tests("masgn_splat_no_name_trailing",
3129
+ "Ruby" => "a, b, = c",
3130
+ "RawParseTree" => [:masgn,
3131
+ [:array, [:lasgn, :a], [:lasgn, :b]], nil,
3132
+ [:to_ary, [:vcall, :c]]],
3133
+ "ParseTree" => s(:masgn,
3134
+ s(:array, s(:lasgn, :a), s(:lasgn, :b)),
3135
+ s(:to_ary, s(:call, nil, :c, s(:arglist)))),
3136
+ "Ruby2Ruby" => "a, b = c") # TODO: check this is right
3137
+
3138
+ add_tests("masgn_splat_to_ary",
3139
+ "Ruby" => "a, b, *c = d",
3140
+ "RawParseTree" => [:masgn,
3141
+ [:array, [:lasgn, :a], [:lasgn, :b]],
3142
+ [:lasgn, :c],
3143
+ [:to_ary, [:vcall, :d]]],
3144
+ "ParseTree" => s(:masgn,
3145
+ s(:array,
3146
+ s(:lasgn, :a),
3147
+ s(:lasgn, :b),
3148
+ s(:splat, s(:lasgn, :c))),
3149
+ s(:to_ary, s(:call, nil, :d, s(:arglist)))))
3150
+
3151
+ add_tests("masgn_splat_to_ary2",
3152
+ "Ruby" => "a, b, *c = d.e(\"f\")",
3153
+ "RawParseTree" => [:masgn,
3154
+ [:array, [:lasgn, :a], [:lasgn, :b]],
3155
+ [:lasgn, :c],
3156
+ [:to_ary,
3157
+ [:call, [:vcall, :d], :e,
3158
+ [:array, [:str, 'f']]]]],
3159
+ "ParseTree" => s(:masgn,
3160
+ s(:array,
3161
+ s(:lasgn, :a),
3162
+ s(:lasgn, :b),
3163
+ s(:splat, s(:lasgn, :c))),
3164
+ s(:to_ary,
3165
+ s(:call,
3166
+ s(:call, nil, :d, s(:arglist)),
3167
+ :e,
3168
+ s(:arglist, s(:str, 'f'))))))
3169
+
3170
+ add_tests("match",
3171
+ "Ruby" => "1 if /x/",
3172
+ "RawParseTree" => [:if, [:match, [:lit, /x/]], [:lit, 1], nil],
3173
+ "ParseTree" => s(:if, s(:match, s(:lit, /x/)), s(:lit, 1), nil))
3174
+
3175
+ add_tests("match2",
3176
+ "Ruby" => "/x/ =~ \"blah\"",
3177
+ "RawParseTree" => [:match2, [:lit, /x/], [:str, "blah"]],
3178
+ "ParseTree" => s(:match2, s(:lit, /x/), s(:str, "blah")))
3179
+
3180
+ add_tests("match3",
3181
+ "Ruby" => "\"blah\" =~ /x/",
3182
+ "RawParseTree" => [:match3, [:lit, /x/], [:str, "blah"]],
3183
+ "ParseTree" => s(:match3, s(:lit, /x/), s(:str, "blah")))
3184
+
3185
+ add_tests("module",
3186
+ "Ruby" => "module X\n def y\n # do nothing\n end\nend",
3187
+ "RawParseTree" => [:module, :X,
3188
+ [:scope,
3189
+ [:defn, :y,
3190
+ [:scope, [:block, [:args], [:nil]]]]]],
3191
+ "ParseTree" => s(:module, :X,
3192
+ s(:scope,
3193
+ s(:defn, :y,
3194
+ s(:args),
3195
+ s(:scope, s(:block, s(:nil)))))))
3196
+
3197
+ add_tests("module_scoped",
3198
+ "Ruby" => "module X::Y\n c\nend",
3199
+ "RawParseTree" => [:module, [:colon2, [:const, :X], :Y],
3200
+ [:scope, [:vcall, :c]]],
3201
+ "ParseTree" => s(:module, s(:colon2, s(:const, :X), :Y),
3202
+ s(:scope, s(:call, nil, :c, s(:arglist)))))
3203
+
3204
+ add_tests("module_scoped3",
3205
+ "Ruby" => "module ::Y\n c\nend",
3206
+ "RawParseTree" => [:module, [:colon3, :Y], [:scope, [:vcall, :c]]],
3207
+ "ParseTree" => s(:module,
3208
+ s(:colon3, :Y),
3209
+ s(:scope, s(:call, nil, :c, s(:arglist)))))
3210
+
3211
+ add_tests("next",
3212
+ "Ruby" => "loop { next if false }",
3213
+ "RawParseTree" => [:iter,
3214
+ [:fcall, :loop],
3215
+ nil,
3216
+ [:if, [:false], [:next], nil]],
3217
+ "ParseTree" => s(:iter,
3218
+ s(:call, nil, :loop, s(:arglist)),
3219
+ nil,
3220
+ s(:if, s(:false), s(:next), nil)))
3221
+
3222
+ add_tests("next_arg",
3223
+ "Ruby" => "loop { next 42 if false }",
3224
+ "RawParseTree" => [:iter,
3225
+ [:fcall, :loop],
3226
+ nil,
3227
+ [:if, [:false], [:next, [:lit, 42]], nil]],
3228
+ "ParseTree" => s(:iter,
3229
+ s(:call, nil, :loop, s(:arglist)),
3230
+ nil,
3231
+ s(:if, s(:false), s(:next, s(:lit, 42)), nil)))
3232
+
3233
+ add_tests("not",
3234
+ "Ruby" => "(not true)",
3235
+ "RawParseTree" => [:not, [:true]],
3236
+ "ParseTree" => s(:not, s(:true)))
3237
+
3238
+ add_tests("nth_ref",
3239
+ "Ruby" => "$1",
3240
+ "RawParseTree" => [:nth_ref, 1],
3241
+ "ParseTree" => s(:nth_ref, 1))
3242
+
3243
+ add_tests("op_asgn1",
3244
+ "Ruby" => "b = []\nb[1] ||= 10\nb[2] &&= 11\nb[3] += 12\n",
3245
+ "RawParseTree" => [:block,
3246
+ [:lasgn, :b, [:zarray]],
3247
+ [:op_asgn1, [:lvar, :b],
3248
+ [:array, [:lit, 1]], :"||", [:lit, 10]],
3249
+ [:op_asgn1, [:lvar, :b],
3250
+ [:array, [:lit, 2]], :"&&", [:lit, 11]],
3251
+ [:op_asgn1, [:lvar, :b],
3252
+ [:array, [:lit, 3]], :+, [:lit, 12]]],
3253
+ "ParseTree" => s(:block,
3254
+ s(:lasgn, :b, s(:array)),
3255
+ s(:op_asgn1, s(:lvar, :b),
3256
+ s(:arglist, s(:lit, 1)), :"||", s(:lit, 10)),
3257
+ s(:op_asgn1, s(:lvar, :b),
3258
+ s(:arglist, s(:lit, 2)), :"&&", s(:lit, 11)),
3259
+ s(:op_asgn1, s(:lvar, :b),
3260
+ s(:arglist, s(:lit, 3)), :+, s(:lit, 12))))
3261
+
3262
+ add_tests("op_asgn1_ivar",
3263
+ "Ruby" => "@b = []\n@b[1] ||= 10\n@b[2] &&= 11\n@b[3] += 12\n",
3264
+ "RawParseTree" => [:block,
3265
+ [:iasgn, :@b, [:zarray]],
3266
+ [:op_asgn1, [:ivar, :@b],
3267
+ [:array, [:lit, 1]], :"||", [:lit, 10]],
3268
+ [:op_asgn1, [:ivar, :@b],
3269
+ [:array, [:lit, 2]], :"&&", [:lit, 11]],
3270
+ [:op_asgn1, [:ivar, :@b],
3271
+ [:array, [:lit, 3]], :+, [:lit, 12]]],
3272
+ "ParseTree" => s(:block,
3273
+ s(:iasgn, :@b, s(:array)),
3274
+ s(:op_asgn1, s(:ivar, :@b),
3275
+ s(:arglist, s(:lit, 1)), :"||", s(:lit, 10)),
3276
+ s(:op_asgn1, s(:ivar, :@b),
3277
+ s(:arglist, s(:lit, 2)), :"&&", s(:lit, 11)),
3278
+ s(:op_asgn1, s(:ivar, :@b),
3279
+ s(:arglist, s(:lit, 3)), :+, s(:lit, 12))))
3280
+
3281
+ add_tests("op_asgn2",
3282
+ "Ruby" => "s = Struct.new(:var)\nc = s.new(nil)\nc.var ||= 20\nc.var &&= 21\nc.var += 22\nc.d.e.f ||= 42\n",
3283
+ "RawParseTree" => [:block,
3284
+ [:lasgn, :s,
3285
+ [:call, [:const, :Struct],
3286
+ :new, [:array, [:lit, :var]]]],
3287
+ [:lasgn, :c,
3288
+ [:call, [:lvar, :s], :new, [:array, [:nil]]]],
3289
+
3290
+ [:op_asgn2, [:lvar, :c], :var=, :"||",
3291
+ [:lit, 20]],
3292
+ [:op_asgn2, [:lvar, :c], :var=, :"&&",
3293
+ [:lit, 21]],
3294
+ [:op_asgn2, [:lvar, :c], :var=, :+, [:lit, 22]],
3295
+
3296
+ [:op_asgn2,
3297
+ [:call,
3298
+ [:call, [:lvar, :c], :d], :e], :f=, :"||",
3299
+ [:lit, 42]]],
3300
+ "ParseTree" => s(:block,
3301
+ s(:lasgn, :s,
3302
+ s(:call, s(:const, :Struct),
3303
+ :new, s(:arglist, s(:lit, :var)))),
3304
+ s(:lasgn, :c,
3305
+ s(:call, s(:lvar, :s),
3306
+ :new, s(:arglist, s(:nil)))),
3307
+ s(:op_asgn2, s(:lvar, :c),
3308
+ :var=, :"||", s(:lit, 20)),
3309
+ s(:op_asgn2, s(:lvar, :c),
3310
+ :var=, :"&&", s(:lit, 21)),
3311
+ s(:op_asgn2, s(:lvar, :c),
3312
+ :var=, :+, s(:lit, 22)),
3313
+ s(:op_asgn2,
3314
+ s(:call,
3315
+ s(:call, s(:lvar, :c), :d, s(:arglist)),
3316
+ :e, s(:arglist)),
3317
+ :f=, :"||", s(:lit, 42))))
3318
+
3319
+ add_tests("op_asgn2_self",
3320
+ "Ruby" => "self.Bag ||= Bag.new",
3321
+ "RawParseTree" => [:op_asgn2, [:self], :"Bag=", :"||",
3322
+ [:call, [:const, :Bag], :new]],
3323
+ "ParseTree" => s(:op_asgn2, s(:self), :"Bag=", :"||",
3324
+ s(:call, s(:const, :Bag), :new, s(:arglist))))
3325
+
3326
+ add_tests("op_asgn_and",
3327
+ "Ruby" => "a = 0\na &&= 2\n",
3328
+ "RawParseTree" => [:block,
3329
+ [:lasgn, :a, [:lit, 0]],
3330
+ [:op_asgn_and,
3331
+ [:lvar, :a], [:lasgn, :a, [:lit, 2]]]],
3332
+ "ParseTree" => s(:block,
3333
+ s(:lasgn, :a, s(:lit, 0)),
3334
+ s(:op_asgn_and,
3335
+ s(:lvar, :a), s(:lasgn, :a, s(:lit, 2)))))
3336
+
3337
+ add_tests("op_asgn_and_ivar2",
3338
+ "Ruby" => "@fetcher &&= new(Gem.configuration[:http_proxy])",
3339
+ "RawParseTree" => [:op_asgn_and,
3340
+ [:ivar, :@fetcher],
3341
+ [:iasgn,
3342
+ :@fetcher,
3343
+ [:fcall,
3344
+ :new,
3345
+ [:array,
3346
+ [:call,
3347
+ [:call, [:const, :Gem], :configuration],
3348
+ :[],
3349
+ [:array, [:lit, :http_proxy]]]]]]],
3350
+ "ParseTree" => s(:op_asgn_and,
3351
+ s(:ivar, :@fetcher),
3352
+ s(:iasgn,
3353
+ :@fetcher,
3354
+ s(:call, nil,
3355
+ :new,
3356
+ s(:arglist,
3357
+ s(:call,
3358
+ s(:call, s(:const, :Gem),
3359
+ :configuration,
3360
+ s(:arglist)),
3361
+ :[],
3362
+ s(:arglist, s(:lit, :http_proxy))))))))
3363
+
3364
+ add_tests("op_asgn_or",
3365
+ "Ruby" => "a = 0\na ||= 1\n",
3366
+ "RawParseTree" => [:block,
3367
+ [:lasgn, :a, [:lit, 0]],
3368
+ [:op_asgn_or,
3369
+ [:lvar, :a], [:lasgn, :a, [:lit, 1]]]],
3370
+ "ParseTree" => s(:block,
3371
+ s(:lasgn, :a, s(:lit, 0)),
3372
+ s(:op_asgn_or,
3373
+ s(:lvar, :a), s(:lasgn, :a, s(:lit, 1)))))
3374
+
3375
+ add_tests("op_asgn_or_block",
3376
+ "Ruby" => "a ||= begin\n b\n rescue\n c\n end",
3377
+ "RawParseTree" => [:op_asgn_or,
3378
+ [:lvar, :a],
3379
+ [:lasgn, :a,
3380
+ [:rescue,
3381
+ [:vcall, :b],
3382
+ [:resbody, nil, [:vcall, :c]]]]],
3383
+ "ParseTree" => s(:op_asgn_or,
3384
+ s(:lvar, :a),
3385
+ s(:lasgn, :a,
3386
+ s(:rescue,
3387
+ s(:call, nil, :b, s(:arglist)),
3388
+ s(:resbody, s(:array),
3389
+ s(:call, nil, :c, s(:arglist)))))),
3390
+ "Ruby2Ruby" => "a ||= b rescue c")
3391
+
3392
+ add_tests("op_asgn_or_ivar",
3393
+ "Ruby" => "@v ||= { }",
3394
+ "RawParseTree" => [:op_asgn_or,
3395
+ [:ivar, :@v],
3396
+ [:iasgn, :@v, [:hash]]],
3397
+ "ParseTree" => s(:op_asgn_or,
3398
+ s(:ivar, :@v),
3399
+ s(:iasgn, :@v, s(:hash))))
3400
+
3401
+ add_tests("op_asgn_or_ivar2",
3402
+ "Ruby" => "@fetcher ||= new(Gem.configuration[:http_proxy])",
3403
+ "RawParseTree" => [:op_asgn_or,
3404
+ [:ivar, :@fetcher],
3405
+ [:iasgn,
3406
+ :@fetcher,
3407
+ [:fcall,
3408
+ :new,
3409
+ [:array,
3410
+ [:call,
3411
+ [:call, [:const, :Gem], :configuration],
3412
+ :[],
3413
+ [:array, [:lit, :http_proxy]]]]]]],
3414
+ "ParseTree" => s(:op_asgn_or,
3415
+ s(:ivar, :@fetcher),
3416
+ s(:iasgn,
3417
+ :@fetcher,
3418
+ s(:call, nil, :new,
3419
+ s(:arglist,
3420
+ s(:call,
3421
+ s(:call, s(:const, :Gem),
3422
+ :configuration,
3423
+ s(:arglist)),
3424
+ :[],
3425
+ s(:arglist, s(:lit, :http_proxy))))))))
3426
+
3427
+ add_tests("or",
3428
+ "Ruby" => "(a or b)",
3429
+ "RawParseTree" => [:or, [:vcall, :a], [:vcall, :b]],
3430
+ "ParseTree" => s(:or,
3431
+ s(:call, nil, :a, s(:arglist)),
3432
+ s(:call, nil, :b, s(:arglist))))
3433
+
3434
+ add_tests("or_big",
3435
+ "Ruby" => "((a or b) or (c and d))",
3436
+ "RawParseTree" => [:or,
3437
+ [:or, [:vcall, :a], [:vcall, :b]],
3438
+ [:and, [:vcall, :c], [:vcall, :d]]],
3439
+ "ParseTree" => s(:or,
3440
+ s(:or,
3441
+ s(:call, nil, :a, s(:arglist)),
3442
+ s(:call, nil, :b, s(:arglist))),
3443
+ s(:and,
3444
+ s(:call, nil, :c, s(:arglist)),
3445
+ s(:call, nil, :d, s(:arglist)))))
3446
+
3447
+ add_tests("or_big2",
3448
+ "Ruby" => "((a || b) || (c && d))",
3449
+ "RawParseTree" => [:or,
3450
+ [:or, [:vcall, :a], [:vcall, :b]],
3451
+ [:and, [:vcall, :c], [:vcall, :d]]],
3452
+ "ParseTree" => s(:or,
3453
+ s(:or,
3454
+ s(:call, nil, :a, s(:arglist)),
3455
+ s(:call, nil, :b, s(:arglist))),
3456
+ s(:and,
3457
+ s(:call, nil, :c, s(:arglist)),
3458
+ s(:call, nil, :d, s(:arglist)))),
3459
+ "Ruby2Ruby" => "((a or b) or (c and d))")
3460
+
3461
+ add_tests("parse_floats_as_args",
3462
+ "Ruby" => "def x(a=0.0,b=0.0)\n a+b\nend",
3463
+ "RawParseTree" => [:defn, :x,
3464
+ [:scope,
3465
+ [:block,
3466
+ [:args, :a, :b,
3467
+ [:block,
3468
+ [:lasgn, :a, [:lit, 0.0]],
3469
+ [:lasgn, :b, [:lit, 0.0]]]],
3470
+ [:call, [:lvar, :a], :+,
3471
+ [:array, [:lvar, :b]]]]]],
3472
+ "ParseTree" => s(:defn, :x,
3473
+ s(:args, :a, :b,
3474
+ s(:block,
3475
+ s(:lasgn, :a, s(:lit, 0.0)),
3476
+ s(:lasgn, :b, s(:lit, 0.0)))),
3477
+ s(:scope,
3478
+ s(:block,
3479
+ s(:call, s(:lvar, :a), :+,
3480
+ s(:arglist, s(:lvar, :b)))))),
3481
+ "Ruby2Ruby" => "def x(a = 0.0, b = 0.0)\n (a + b)\nend")
3482
+
3483
+ add_tests("postexe",
3484
+ "Ruby" => "END { 1 }",
3485
+ "RawParseTree" => [:iter, [:postexe], nil, [:lit, 1]],
3486
+ "ParseTree" => s(:iter, s(:postexe), nil, s(:lit, 1)))
3487
+
3488
+ add_tests("proc_args_0",
3489
+ "Ruby" => "proc { || (x + 1) }",
3490
+ "RawParseTree" => [:iter,
3491
+ [:fcall, :proc],
3492
+ 0,
3493
+ [:call, [:vcall, :x], :+, [:array, [:lit, 1]]]],
3494
+ "ParseTree" => s(:iter,
3495
+ s(:call, nil, :proc, s(:arglist)),
3496
+ 0,
3497
+ s(:call,
3498
+ s(:call, nil, :x, s(:arglist)),
3499
+ :+,
3500
+ s(:arglist, s(:lit, 1)))))
3501
+
3502
+ add_tests("proc_args_1",
3503
+ "Ruby" => "proc { |x| (x + 1) }",
3504
+ "RawParseTree" => [:iter,
3505
+ [:fcall, :proc],
3506
+ [:dasgn_curr, :x],
3507
+ [:call, [:dvar, :x], :+, [:array, [:lit, 1]]]],
3508
+ "ParseTree" => s(:iter,
3509
+ s(:call, nil, :proc, s(:arglist)),
3510
+ s(:lasgn, :x),
3511
+ s(:call, s(:lvar, :x), :+,
3512
+ s(:arglist, s(:lit, 1)))))
3513
+
3514
+ add_tests("proc_args_2",
3515
+ "Ruby" => "proc { |x, y| (x + y) }",
3516
+ "RawParseTree" => [:iter,
3517
+ [:fcall, :proc],
3518
+ [:masgn, [:array,
3519
+ [:dasgn_curr, :x],
3520
+ [:dasgn_curr, :y]], nil, nil],
3521
+ [:call, [:dvar, :x], :+, [:array, [:dvar, :y]]]],
3522
+ "ParseTree" => s(:iter,
3523
+ s(:call, nil, :proc, s(:arglist)),
3524
+ s(:masgn,
3525
+ s(:array,
3526
+ s(:lasgn, :x),
3527
+ s(:lasgn, :y))),
3528
+ s(:call, s(:lvar, :x), :+,
3529
+ s(:arglist, s(:lvar, :y)))))
3530
+
3531
+ add_tests("proc_args_no",
3532
+ "Ruby" => "proc { (x + 1) }",
3533
+ "RawParseTree" => [:iter,
3534
+ [:fcall, :proc],
3535
+ nil,
3536
+ [:call, [:vcall, :x], :+, [:array, [:lit, 1]]]],
3537
+ "ParseTree" => s(:iter,
3538
+ s(:call, nil, :proc, s(:arglist)),
3539
+ nil,
3540
+ s(:call, s(:call, nil, :x, s(:arglist)),
3541
+ :+, s(:arglist, s(:lit, 1)))))
3542
+
3543
+ add_tests("redo",
3544
+ "Ruby" => "loop { redo if false }",
3545
+ "RawParseTree" => [:iter,
3546
+ [:fcall, :loop], nil,
3547
+ [:if, [:false], [:redo], nil]],
3548
+ "ParseTree" => s(:iter,
3549
+ s(:call, nil, :loop, s(:arglist)),
3550
+ nil,
3551
+ s(:if, s(:false), s(:redo), nil)))
3552
+
3553
+ # TODO: need a resbody w/ multiple classes and a splat
3554
+
3555
+ add_tests("rescue",
3556
+ "Ruby" => "blah rescue nil",
3557
+ "RawParseTree" => [:rescue,
3558
+ [:vcall, :blah], [:resbody, nil, [:nil]]],
3559
+ "ParseTree" => s(:rescue,
3560
+ s(:call, nil, :blah, s(:arglist)),
3561
+ s(:resbody, s(:array), s(:nil))))
3562
+
3563
+ add_tests("rescue_block_body",
3564
+ "Ruby" => "begin\n a\nrescue => e\n c\n d\nend",
3565
+ "RawParseTree" => [:begin,
3566
+ [:rescue,
3567
+ [:vcall, :a],
3568
+ [:resbody, nil,
3569
+ [:block,
3570
+ [:lasgn, :e, [:gvar, :$!]],
3571
+ [:vcall, :c],
3572
+ [:vcall, :d]]]]],
3573
+ "ParseTree" => s(:rescue,
3574
+ s(:call, nil, :a, s(:arglist)),
3575
+ s(:resbody,
3576
+ s(:array, s(:lasgn, :e, s(:gvar, :$!))),
3577
+ s(:block,
3578
+ s(:call, nil, :c, s(:arglist)),
3579
+ s(:call, nil, :d, s(:arglist))))))
3580
+
3581
+ add_tests("rescue_block_body_ivar",
3582
+ "Ruby" => "begin\n a\nrescue => @e\n c\n d\nend",
3583
+ "RawParseTree" => [:begin,
3584
+ [:rescue,
3585
+ [:vcall, :a],
3586
+ [:resbody, nil,
3587
+ [:block,
3588
+ [:iasgn, :@e, [:gvar, :$!]],
3589
+ [:vcall, :c],
3590
+ [:vcall, :d]]]]],
3591
+ "ParseTree" => s(:rescue,
3592
+ s(:call, nil, :a, s(:arglist)),
3593
+ s(:resbody,
3594
+ s(:array, s(:iasgn, :@e, s(:gvar, :$!))),
3595
+ s(:block,
3596
+ s(:call, nil, :c, s(:arglist)),
3597
+ s(:call, nil, :d, s(:arglist))))))
3598
+
3599
+ add_tests("rescue_block_body_3",
3600
+ "Ruby" => "begin\n a\nrescue A\n b\nrescue B\n c\nrescue C\n d\nend",
3601
+ "RawParseTree" => [:begin,
3602
+ [:rescue,
3603
+ [:vcall, :a],
3604
+ [:resbody, [:array, [:const, :A]],
3605
+ [:vcall, :b],
3606
+ [:resbody, [:array, [:const, :B]],
3607
+ [:vcall, :c],
3608
+ [:resbody, [:array, [:const, :C]],
3609
+ [:vcall, :d]]]]]],
3610
+ "ParseTree" => s(:rescue,
3611
+ s(:call, nil, :a, s(:arglist)),
3612
+ s(:resbody, s(:array, s(:const, :A)),
3613
+ s(:call, nil, :b, s(:arglist))),
3614
+ s(:resbody, s(:array, s(:const, :B)),
3615
+ s(:call, nil, :c, s(:arglist))),
3616
+ s(:resbody, s(:array, s(:const, :C)),
3617
+ s(:call, nil, :d, s(:arglist)))))
3618
+
3619
+ add_tests("rescue_block_nada",
3620
+ "Ruby" => "begin\n blah\nrescue\n # do nothing\nend",
3621
+ "RawParseTree" => [:begin,
3622
+ [:rescue, [:vcall, :blah], [:resbody, nil]]],
3623
+ "ParseTree" => s(:rescue,
3624
+ s(:call, nil, :blah, s(:arglist)),
3625
+ s(:resbody, s(:array), nil)))
3626
+
3627
+ add_tests("rescue_exceptions",
3628
+ "Ruby" => "begin\n blah\nrescue RuntimeError => r\n # do nothing\nend",
3629
+ "RawParseTree" => [:begin,
3630
+ [:rescue,
3631
+ [:vcall, :blah],
3632
+ [:resbody,
3633
+ [:array, [:const, :RuntimeError]],
3634
+ [:lasgn, :r, [:gvar, :$!]]]]],
3635
+ "ParseTree" => s(:rescue,
3636
+ s(:call, nil, :blah, s(:arglist)),
3637
+ s(:resbody,
3638
+ s(:array,
3639
+ s(:const, :RuntimeError),
3640
+ s(:lasgn, :r, s(:gvar, :$!))),
3641
+ nil)))
3642
+
3643
+
3644
+ add_tests("rescue_lasgn",
3645
+ "Ruby" => "begin\n 1\nrescue\n var = 2\nend",
3646
+ "RawParseTree" => [:begin,
3647
+ [:rescue,
3648
+ [:lit, 1],
3649
+ [:resbody, nil, [:lasgn, :var, [:lit, 2]]]]],
3650
+ "ParseTree" => s(:rescue,
3651
+ s(:lit, 1),
3652
+ s(:resbody,
3653
+ s(:array),
3654
+ s(:lasgn, :var, s(:lit, 2)))),
3655
+ "Ruby2Ruby" => "1 rescue var = 2")
3656
+
3657
+ add_tests("rescue_lasgn_var",
3658
+ "Ruby" => "begin\n 1\nrescue => e\n var = 2\nend",
3659
+ "RawParseTree" => [:begin,
3660
+ [:rescue,
3661
+ [:lit, 1],
3662
+ [:resbody, nil,
3663
+ [:block,
3664
+ [:lasgn, :e, [:gvar, :$!]],
3665
+ [:lasgn, :var, [:lit, 2]]]]]],
3666
+ "ParseTree" => s(:rescue,
3667
+ s(:lit, 1),
3668
+ s(:resbody,
3669
+ s(:array, s(:lasgn, :e, s(:gvar, :$!))),
3670
+ s(:lasgn, :var, s(:lit, 2)))))
3671
+
3672
+ add_tests("rescue_lasgn_var_empty",
3673
+ "Ruby" => "begin\n 1\nrescue => e\n # do nothing\nend",
3674
+ "RawParseTree" => [:begin,
3675
+ [:rescue,
3676
+ [:lit, 1],
3677
+ [:resbody, nil, [:lasgn, :e, [:gvar, :$!]]]]],
3678
+ "ParseTree" => s(:rescue,
3679
+ s(:lit, 1),
3680
+ s(:resbody,
3681
+ s(:array, s(:lasgn, :e, s(:gvar, :$!))),
3682
+ nil)))
3683
+
3684
+ add_tests("rescue_iasgn_var_empty",
3685
+ "Ruby" => "begin\n 1\nrescue => @e\n # do nothing\nend",
3686
+ "RawParseTree" => [:begin,
3687
+ [:rescue,
3688
+ [:lit, 1],
3689
+ [:resbody, nil, [:iasgn, :@e, [:gvar, :$!]]]]],
3690
+ "ParseTree" => s(:rescue,
3691
+ s(:lit, 1),
3692
+ s(:resbody,
3693
+ s(:array, s(:iasgn, :@e, s(:gvar, :$!))),
3694
+ nil)))
3695
+ add_tests("retry",
3696
+ "Ruby" => "retry",
3697
+ "RawParseTree" => [:retry],
3698
+ "ParseTree" => s(:retry))
3699
+
3700
+ add_tests("return_0",
3701
+ "Ruby" => "return",
3702
+ "RawParseTree" => [:return],
3703
+ "ParseTree" => s(:return))
3704
+
3705
+ add_tests("return_1",
3706
+ "Ruby" => "return 1",
3707
+ "RawParseTree" => [:return, [:lit, 1]],
3708
+ "ParseTree" => s(:return, s(:lit, 1)))
3709
+
3710
+ add_tests("return_n",
3711
+ "Ruby" => "return 1, 2, 3",
3712
+ "RawParseTree" => [:return, [:array,
3713
+ [:lit, 1], [:lit, 2], [:lit, 3]]],
3714
+ "ParseTree" => s(:return, s(:array,
3715
+ s(:lit, 1), s(:lit, 2), s(:lit, 3))),
3716
+ "Ruby2Ruby" => "return [1, 2, 3]")
3717
+
3718
+ add_tests("sclass",
3719
+ "Ruby" => "class << self\n 42\nend",
3720
+ "RawParseTree" => [:sclass, [:self], [:scope, [:lit, 42]]],
3721
+ "ParseTree" => s(:sclass, s(:self), s(:scope, s(:lit, 42))))
3722
+
3723
+ add_tests("sclass_trailing_class",
3724
+ "Ruby" => "class A\n class << self\n a\n end\n class B\n end\nend",
3725
+ "RawParseTree" => [:class, :A, nil,
3726
+ [:scope,
3727
+ [:block,
3728
+ [:sclass, [:self], [:scope, [:vcall, :a]]],
3729
+ [:class, :B, nil, [:scope]]]]],
3730
+ "ParseTree" => s(:class, :A, nil,
3731
+ s(:scope,
3732
+ s(:block,
3733
+ s(:sclass, s(:self),
3734
+ s(:scope,
3735
+ s(:call, nil, :a, s(:arglist)))),
3736
+ s(:class, :B, nil, s(:scope))))))
3737
+
3738
+ add_tests("splat",
3739
+ "Ruby" => "def x(*b)\n a(*b)\nend",
3740
+ "RawParseTree" => [:defn, :x,
3741
+ [:scope,
3742
+ [:block,
3743
+ [:args, :"*b"],
3744
+ [:fcall, :a, [:splat, [:lvar, :b]]]]]],
3745
+ "ParseTree" => s(:defn, :x,
3746
+ s(:args, :"*b"),
3747
+ s(:scope,
3748
+ s(:block,
3749
+ s(:call, nil, :a,
3750
+ s(:arglist, s(:splat, s(:lvar, :b))))))))
3751
+
3752
+ add_tests("str",
3753
+ "Ruby" => '"x"',
3754
+ "RawParseTree" => [:str, "x"],
3755
+ "ParseTree" => s(:str, "x"))
3756
+
3757
+ add_tests("str_concat_newline", # FIX? make prettier? possible?
3758
+ "Ruby" => '"before" \\
3759
+ " after"',
3760
+ "RawParseTree" => [:str, "before after"],
3761
+ "ParseTree" => s(:str, "before after"),
3762
+ "Ruby2Ruby" => '"before after"')
3763
+
3764
+ add_tests("str_concat_space",
3765
+ "Ruby" => '"before" " after"',
3766
+ "RawParseTree" => [:str, "before after"],
3767
+ "ParseTree" => s(:str, "before after"),
3768
+ "Ruby2Ruby" => '"before after"')
3769
+
3770
+ add_tests("str_heredoc",
3771
+ "Ruby" => "<<'EOM'\n blah\nblah\nEOM",
3772
+ "RawParseTree" => [:str, " blah\nblah\n"],
3773
+ "ParseTree" => s(:str, " blah\nblah\n"),
3774
+ "Ruby2Ruby" => "\" blah\\nblah\\n\"")
3775
+
3776
+ add_tests("str_heredoc_call",
3777
+ "Ruby" => "<<'EOM'.strip\n blah\nblah\nEOM",
3778
+ "RawParseTree" => [:call, [:str, " blah\nblah\n"], :strip],
3779
+ "ParseTree" => s(:call, s(:str, " blah\nblah\n"),
3780
+ :strip, s(:arglist)),
3781
+ "Ruby2Ruby" => "\" blah\\nblah\\n\".strip")
3782
+
3783
+ add_tests("str_heredoc_double",
3784
+ "Ruby" => "a += <<-H1 + b + <<-H2\n first\nH1\n second\nH2",
3785
+ "RawParseTree" => [:lasgn, :a,
3786
+ [:call,
3787
+ [:lvar, :a],
3788
+ :+,
3789
+ [:array,
3790
+ [:call,
3791
+ [:call, [:str, " first\n"], :+,
3792
+ [:array, [:vcall, :b]]],
3793
+ :+,
3794
+ [:array, [:str, " second\n"]]]]]],
3795
+ "ParseTree" => s(:lasgn, :a,
3796
+ s(:call,
3797
+ s(:lvar, :a),
3798
+ :+,
3799
+ s(:arglist,
3800
+ s(:call,
3801
+ s(:call, s(:str, " first\n"), :+,
3802
+ s(:arglist,
3803
+ s(:call, nil, :b, s(:arglist)))),
3804
+ :+,
3805
+ s(:arglist, s(:str, " second\n")))))),
3806
+ "Ruby2Ruby" => "a = (a + ((\" first\\n\" + b) + \" second\\n\"))")
3807
+
3808
+ add_tests("str_heredoc_indent",
3809
+ "Ruby" => "<<-EOM\n blah\nblah\n\n EOM",
3810
+ "RawParseTree" => [:str, " blah\nblah\n\n"],
3811
+ "ParseTree" => s(:str, " blah\nblah\n\n"),
3812
+ "Ruby2Ruby" => "\" blah\\nblah\\n\\n\"")
3813
+
3814
+ add_tests("str_interp_file",
3815
+ "Ruby" => '"file = #{__FILE__}\n"',
3816
+ "RawParseTree" => [:str, "file = (string)\n"],
3817
+ "ParseTree" => s(:str, "file = (string)\n"),
3818
+ "Ruby2Ruby" => '"file = (string)\\n"')
3819
+
3820
+ add_tests("structure_extra_block_for_dvar_scoping",
3821
+ "Ruby" => "a.b do |c, d|\n unless e.f(c) then\n g = false\n d.h { |x, i| g = true }\n end\nend",
3822
+ "RawParseTree" => [:iter,
3823
+ [:call, [:vcall, :a], :b],
3824
+ [:masgn, [:array,
3825
+ [:dasgn_curr, :c],
3826
+ [:dasgn_curr, :d]], nil, nil],
3827
+ [:if,
3828
+ [:call, [:vcall, :e], :f,
3829
+ [:array, [:dvar, :c]]],
3830
+ nil,
3831
+ [:block,
3832
+ [:dasgn_curr, :g, [:false]],
3833
+ [:iter,
3834
+ [:call, [:dvar, :d], :h],
3835
+ [:masgn, [:array,
3836
+ [:dasgn_curr, :x],
3837
+ [:dasgn_curr, :i]], nil, nil],
3838
+ [:dasgn, :g, [:true]]]]]],
3839
+ "ParseTree" => s(:iter,
3840
+ s(:call,
3841
+ s(:call, nil, :a, s(:arglist)),
3842
+ :b, s(:arglist)),
3843
+ s(:masgn, s(:array,
3844
+ s(:lasgn, :c),
3845
+ s(:lasgn, :d))),
3846
+ s(:if,
3847
+ s(:call, s(:call, nil, :e, s(:arglist)), :f,
3848
+ s(:arglist, s(:lvar, :c))),
3849
+ nil,
3850
+ s(:block,
3851
+ s(:lasgn, :g, s(:false)),
3852
+ s(:iter,
3853
+ s(:call, s(:lvar, :d), :h, s(:arglist)),
3854
+ s(:masgn, s(:array,
3855
+ s(:lasgn, :x),
3856
+ s(:lasgn, :i))),
3857
+ s(:lasgn, :g, s(:true)))))))
3858
+
3859
+ add_tests("structure_remove_begin_1",
3860
+ "Ruby" => "a << begin\n b\n rescue\n c\n end",
3861
+ "RawParseTree" => [:call, [:vcall, :a], :<<,
3862
+ [:array, [:rescue, [:vcall, :b],
3863
+ [:resbody, nil, [:vcall, :c]]]]],
3864
+ "ParseTree" => s(:call, s(:call, nil, :a, s(:arglist)), :<<,
3865
+ s(:arglist,
3866
+ s(:rescue,
3867
+ s(:call, nil, :b, s(:arglist)),
3868
+ s(:resbody, s(:array),
3869
+ s(:call, nil, :c, s(:arglist)))))),
3870
+ "Ruby2Ruby" => "(a << b rescue c)")
3871
+
3872
+ add_tests("structure_remove_begin_2",
3873
+ "Ruby" => "a = if c\n begin\n b\n rescue\n nil\n end\n end\na",
3874
+ "RawParseTree" => [:block,
3875
+ [:lasgn,
3876
+ :a,
3877
+ [:if, [:vcall, :c],
3878
+ [:rescue,
3879
+ [:vcall, :b],
3880
+ [:resbody, nil, [:nil]]],
3881
+ nil]],
3882
+ [:lvar, :a]],
3883
+ "ParseTree" => s(:block,
3884
+ s(:lasgn,
3885
+ :a,
3886
+ s(:if, s(:call, nil, :c, s(:arglist)),
3887
+ s(:rescue, s(:call, nil, :b, s(:arglist)),
3888
+ s(:resbody,
3889
+ s(:array), s(:nil))),
3890
+ nil)),
3891
+ s(:lvar, :a)),
3892
+ "Ruby2Ruby" => "a = b rescue nil if c\na\n") # OMG that's awesome
3893
+
3894
+ add_tests("structure_unused_literal_wwtt",
3895
+ "Ruby" => "\"prevent the above from infecting rdoc\"\n\nmodule Graffle\nend",
3896
+ "RawParseTree" => [:module, :Graffle, [:scope]],
3897
+ "ParseTree" => s(:module, :Graffle, s(:scope)),
3898
+ "Ruby2Ruby" => "module Graffle\nend")
3899
+
3900
+ add_tests("super_0",
3901
+ "Ruby" => "def x\n super()\nend",
3902
+ "RawParseTree" => [:defn, :x,
3903
+ [:scope,
3904
+ [:block, [:args], [:super]]]],
3905
+ "ParseTree" => s(:defn, :x,
3906
+ s(:args),
3907
+ s(:scope, s(:block, s(:super)))))
3908
+
3909
+ add_tests("super_1",
3910
+ "Ruby" => "def x\n super(4)\nend",
3911
+ "RawParseTree" => [:defn, :x,
3912
+ [:scope,
3913
+ [:block,
3914
+ [:args],
3915
+ [:super, [:array, [:lit, 4]]]]]],
3916
+ "ParseTree" => s(:defn, :x,
3917
+ s(:args),
3918
+ s(:scope,
3919
+ s(:block,
3920
+ s(:super, s(:lit, 4))))))
3921
+
3922
+ add_tests("super_1_array",
3923
+ "Ruby" => "def x\n super([24, 42])\nend",
3924
+ "RawParseTree" => [:defn, :x,
3925
+ [:scope,
3926
+ [:block,
3927
+ [:args],
3928
+ [:super,
3929
+ [:array,
3930
+ [:array, [:lit, 24], [:lit, 42]]]]]]],
3931
+ "ParseTree" => s(:defn, :x,
3932
+ s(:args),
3933
+ s(:scope,
3934
+ s(:block,
3935
+ s(:super, s(:array,
3936
+ s(:lit, 24),
3937
+ s(:lit, 42)))))))
3938
+
3939
+ add_tests("super_n",
3940
+ "Ruby" => "def x\n super(24, 42)\nend",
3941
+ "RawParseTree" => [:defn, :x,
3942
+ [:scope,
3943
+ [:block,
3944
+ [:args],
3945
+ [:super, [:array, [:lit, 24], [:lit, 42]]]]]],
3946
+ "ParseTree" => s(:defn, :x,
3947
+ s(:args),
3948
+ s(:scope,
3949
+ s(:block,
3950
+ s(:super, s(:lit, 24), s(:lit, 42))))))
3951
+
3952
+ add_tests("super_block_pass",
3953
+ "Ruby" => "super(a, &b)",
3954
+ "RawParseTree" => [:block_pass,
3955
+ [:vcall, :b], [:super, [:array, [:vcall, :a]]]],
3956
+ "ParseTree" => s(:super,
3957
+ s(:call, nil, :a, s(:arglist)),
3958
+ s(:block_pass,
3959
+ s(:call, nil, :b, s(:arglist)))))
3960
+
3961
+ add_tests("super_block_splat",
3962
+ "Ruby" => "super(a, *b)",
3963
+ "RawParseTree" => [:super,
3964
+ [:argscat,
3965
+ [:array, [:vcall, :a]],
3966
+ [:vcall, :b]]],
3967
+ "ParseTree" => s(:super,
3968
+ s(:call, nil, :a, s(:arglist)),
3969
+ s(:splat, s(:call, nil, :b, s(:arglist)))))
3970
+
3971
+ add_tests("svalue",
3972
+ "Ruby" => "a = *b",
3973
+ "RawParseTree" => [:lasgn, :a, [:svalue, [:splat, [:vcall, :b]]]],
3974
+ "ParseTree" => s(:lasgn, :a,
3975
+ s(:svalue,
3976
+ s(:splat, s(:call, nil, :b, s(:arglist))))))
3977
+
3978
+ add_tests("to_ary",
3979
+ "Ruby" => "a, b = c",
3980
+ "RawParseTree" => [:masgn,
3981
+ [:array, [:lasgn, :a], [:lasgn, :b]], nil,
3982
+ [:to_ary, [:vcall, :c]]],
3983
+ "ParseTree" => s(:masgn,
3984
+ s(:array, s(:lasgn, :a), s(:lasgn, :b)),
3985
+ s(:to_ary, s(:call, nil, :c, s(:arglist)))))
3986
+
3987
+ add_tests("true",
3988
+ "Ruby" => "true",
3989
+ "RawParseTree" => [:true],
3990
+ "ParseTree" => s(:true))
3991
+
3992
+ add_tests("undef",
3993
+ "Ruby" => "undef :x",
3994
+ "RawParseTree" => [:undef, [:lit, :x]],
3995
+ "ParseTree" => s(:undef, s(:lit, :x)))
3996
+
3997
+ add_tests("undef_2",
3998
+ "Ruby" => "undef :x, :y",
3999
+ "RawParseTree" => [:block,
4000
+ [:undef, [:lit, :x]],
4001
+ [:undef, [:lit, :y]]],
4002
+ "ParseTree" => s(:block,
4003
+ s(:undef, s(:lit, :x)),
4004
+ s(:undef, s(:lit, :y))),
4005
+ "Ruby2Ruby" => "undef :x\nundef :y\n")
4006
+
4007
+ add_tests("undef_3",
4008
+ "Ruby" => "undef :x, :y, :z",
4009
+ "RawParseTree" => [:block,
4010
+ [:undef, [:lit, :x]],
4011
+ [:undef, [:lit, :y]],
4012
+ [:undef, [:lit, :z]]],
4013
+ "ParseTree" => s(:block,
4014
+ s(:undef, s(:lit, :x)),
4015
+ s(:undef, s(:lit, :y)),
4016
+ s(:undef, s(:lit, :z))),
4017
+ "Ruby2Ruby" => "undef :x\nundef :y\nundef :z\n")
4018
+
4019
+ add_tests("undef_block_1",
4020
+ "Ruby" => "f1\nundef :x\n", # TODO: don't like the extra return
4021
+ "RawParseTree" => [:block,
4022
+ [:vcall, :f1],
4023
+ [:undef, [:lit, :x]]],
4024
+ "ParseTree" => s(:block,
4025
+ s(:call, nil, :f1, s(:arglist)),
4026
+ s(:undef, s(:lit, :x))))
4027
+
4028
+ add_tests("undef_block_2",
4029
+ "Ruby" => "f1\nundef :x, :y",
4030
+ "RawParseTree" => [:block,
4031
+ [:vcall, :f1],
4032
+ [:block,
4033
+ [:undef, [:lit, :x]],
4034
+ [:undef, [:lit, :y]],
4035
+ ]],
4036
+ "ParseTree" => s(:block,
4037
+ s(:call, nil, :f1, s(:arglist)),
4038
+ s(:block,
4039
+ s(:undef, s(:lit, :x)),
4040
+ s(:undef, s(:lit, :y)))),
4041
+ "Ruby2Ruby" => "f1\n(undef :x\nundef :y)\n")
4042
+
4043
+ add_tests("undef_block_3",
4044
+ "Ruby" => "f1\nundef :x, :y, :z",
4045
+ "RawParseTree" => [:block,
4046
+ [:vcall, :f1],
4047
+ [:block,
4048
+ [:undef, [:lit, :x]],
4049
+ [:undef, [:lit, :y]],
4050
+ [:undef, [:lit, :z]],
4051
+ ]],
4052
+ "ParseTree" => s(:block,
4053
+ s(:call, nil, :f1, s(:arglist)),
4054
+ s(:block,
4055
+ s(:undef, s(:lit, :x)),
4056
+ s(:undef, s(:lit, :y)),
4057
+ s(:undef, s(:lit, :z)))),
4058
+ "Ruby2Ruby" => "f1\n(undef :x\nundef :y\nundef :z)\n")
4059
+
4060
+ add_tests("undef_block_3_post",
4061
+ "Ruby" => "undef :x, :y, :z\nf2",
4062
+ "RawParseTree" => [:block,
4063
+ [:undef, [:lit, :x]],
4064
+ [:undef, [:lit, :y]],
4065
+ [:undef, [:lit, :z]],
4066
+ [:vcall, :f2]],
4067
+ "ParseTree" => s(:block,
4068
+ s(:undef, s(:lit, :x)),
4069
+ s(:undef, s(:lit, :y)),
4070
+ s(:undef, s(:lit, :z)),
4071
+ s(:call, nil, :f2, s(:arglist))),
4072
+ "Ruby2Ruby" => "undef :x\nundef :y\nundef :z\nf2\n")
4073
+
4074
+ add_tests("undef_block_wtf",
4075
+ "Ruby" => "f1\nundef :x, :y, :z\nf2",
4076
+ "RawParseTree" => [:block,
4077
+ [:vcall, :f1],
4078
+ [:block,
4079
+ [:undef, [:lit, :x]],
4080
+ [:undef, [:lit, :y]],
4081
+ [:undef, [:lit, :z]]],
4082
+ [:vcall, :f2]],
4083
+ "ParseTree" => s(:block,
4084
+ s(:call, nil, :f1, s(:arglist)),
4085
+ s(:block,
4086
+ s(:undef, s(:lit, :x)),
4087
+ s(:undef, s(:lit, :y)),
4088
+ s(:undef, s(:lit, :z))),
4089
+ s(:call, nil, :f2, s(:arglist))),
4090
+ "Ruby2Ruby" => "f1\n(undef :x\nundef :y\nundef :z)\nf2\n")
4091
+
4092
+
4093
+ add_tests("unless_post",
4094
+ "Ruby" => "a unless b",
4095
+ "RawParseTree" => [:if, [:vcall, :b], nil, [:vcall, :a]],
4096
+ "ParseTree" => s(:if, s(:call, nil, :b, s(:arglist)), nil,
4097
+ s(:call, nil, :a, s(:arglist))))
4098
+
4099
+ add_tests("unless_post_not",
4100
+ "Ruby" => "a unless not b",
4101
+ "RawParseTree" => [:if, [:vcall, :b], [:vcall, :a], nil],
4102
+ "ParseTree" => s(:if, s(:call, nil, :b, s(:arglist)),
4103
+ s(:call, nil, :a, s(:arglist)), nil),
4104
+ "Ruby2Ruby" => "a if b")
4105
+
4106
+ add_tests("unless_pre",
4107
+ "Ruby" => "unless b then a end",
4108
+ "RawParseTree" => [:if, [:vcall, :b], nil, [:vcall, :a]],
4109
+ "ParseTree" => s(:if, s(:call, nil, :b, s(:arglist)), nil,
4110
+ s(:call, nil, :a, s(:arglist))),
4111
+ "Ruby2Ruby" => "a unless b")
4112
+
4113
+ add_tests("unless_pre_not",
4114
+ "Ruby" => "unless not b then a end",
4115
+ "RawParseTree" => [:if, [:vcall, :b], [:vcall, :a], nil],
4116
+ "ParseTree" => s(:if, s(:call, nil, :b, s(:arglist)),
4117
+ s(:call, nil, :a, s(:arglist)), nil),
4118
+ "Ruby2Ruby" => "a if b")
4119
+
4120
+ add_tests("until_post",
4121
+ "Ruby" => "begin\n (1 + 1)\nend until false",
4122
+ "RawParseTree" => [:until, [:false],
4123
+ [:call, [:lit, 1], :+,
4124
+ [:array, [:lit, 1]]], false],
4125
+ "ParseTree" => s(:until, s(:false),
4126
+ s(:call, s(:lit, 1), :+,
4127
+ s(:arglist, s(:lit, 1))), false))
4128
+
4129
+ add_tests("until_post_not",
4130
+ "Ruby" => "begin\n (1 + 1)\nend until not true",
4131
+ "RawParseTree" => [:while, [:true],
4132
+ [:call, [:lit, 1], :+,
4133
+ [:array, [:lit, 1]]], false],
4134
+ "ParseTree" => s(:while, s(:true),
4135
+ s(:call, s(:lit, 1), :+,
4136
+ s(:arglist, s(:lit, 1))), false),
4137
+ "Ruby2Ruby" => "begin\n (1 + 1)\nend while true")
4138
+
4139
+ add_tests("until_pre",
4140
+ "Ruby" => "until false do\n (1 + 1)\nend",
4141
+ "RawParseTree" => [:until, [:false],
4142
+ [:call, [:lit, 1], :+,
4143
+ [:array, [:lit, 1]]], true],
4144
+ "ParseTree" => s(:until, s(:false),
4145
+ s(:call, s(:lit, 1), :+,
4146
+ s(:arglist, s(:lit, 1))), true))
4147
+
4148
+ add_tests("until_pre_mod",
4149
+ "Ruby" => "(1 + 1) until false",
4150
+ "RawParseTree" => [:until, [:false],
4151
+ [:call, [:lit, 1], :+,
4152
+ [:array, [:lit, 1]]], true],
4153
+ "ParseTree" => s(:until, s(:false),
4154
+ s(:call, s(:lit, 1), :+,
4155
+ s(:arglist, s(:lit, 1))), true),
4156
+ "Ruby2Ruby" => "until false do\n (1 + 1)\nend")
4157
+
4158
+ add_tests("until_pre_not",
4159
+ "Ruby" => "until not true do\n (1 + 1)\nend",
4160
+ "RawParseTree" => [:while, [:true],
4161
+ [:call, [:lit, 1], :+,
4162
+ [:array, [:lit, 1]]], true],
4163
+ "ParseTree" => s(:while, s(:true),
4164
+ s(:call, s(:lit, 1), :+,
4165
+ s(:arglist, s(:lit, 1))), true),
4166
+ "Ruby2Ruby" => "while true do\n (1 + 1)\nend")
4167
+
4168
+ add_tests("until_pre_not_mod",
4169
+ "Ruby" => "(1 + 1) until not true",
4170
+ "RawParseTree" => [:while, [:true],
4171
+ [:call, [:lit, 1], :+,
4172
+ [:array, [:lit, 1]]], true],
4173
+ "ParseTree" => s(:while, s(:true),
4174
+ s(:call, s(:lit, 1), :+,
4175
+ s(:arglist, s(:lit, 1))), true),
4176
+ "Ruby2Ruby" => "while true do\n (1 + 1)\nend")
4177
+
4178
+ add_tests("valias",
4179
+ "Ruby" => "alias $y $x",
4180
+ "RawParseTree" => [:valias, :$y, :$x],
4181
+ "ParseTree" => s(:valias, :$y, :$x))
4182
+
4183
+ add_tests("vcall",
4184
+ "Ruby" => "method",
4185
+ "RawParseTree" => [:vcall, :method],
4186
+ "ParseTree" => s(:call, nil, :method, s(:arglist)))
4187
+
4188
+ add_tests("while_post",
4189
+ "Ruby" => "begin\n (1 + 1)\nend while false",
4190
+ "RawParseTree" => [:while, [:false],
4191
+ [:call, [:lit, 1], :+,
4192
+ [:array, [:lit, 1]]], false],
4193
+ "ParseTree" => s(:while, s(:false),
4194
+ s(:call, s(:lit, 1), :+,
4195
+ s(:arglist, s(:lit, 1))), false))
4196
+
4197
+ add_tests("while_post2",
4198
+ "Ruby" => "begin\n (1 + 2)\n (3 + 4)\nend while false",
4199
+ "RawParseTree" => [:while, [:false],
4200
+ [:block,
4201
+ [:call, [:lit, 1], :+, [:array, [:lit, 2]]],
4202
+ [:call, [:lit, 3], :+, [:array, [:lit, 4]]]],
4203
+ false],
4204
+ "ParseTree" => s(:while, s(:false),
4205
+ s(:block,
4206
+ s(:call, s(:lit, 1), :+,
4207
+ s(:arglist, s(:lit, 2))),
4208
+ s(:call, s(:lit, 3), :+,
4209
+ s(:arglist, s(:lit, 4)))),
4210
+ false))
4211
+
4212
+ add_tests("while_post_not",
4213
+ "Ruby" => "begin\n (1 + 1)\nend while not true",
4214
+ "RawParseTree" => [:until, [:true],
4215
+ [:call, [:lit, 1], :+,
4216
+ [:array, [:lit, 1]]], false],
4217
+ "ParseTree" => s(:until, s(:true),
4218
+ s(:call, s(:lit, 1), :+,
4219
+ s(:arglist, s(:lit, 1))), false),
4220
+ "Ruby2Ruby" => "begin\n (1 + 1)\nend until true")
4221
+
4222
+ add_tests("while_pre",
4223
+ "Ruby" => "while false do\n (1 + 1)\nend",
4224
+ "RawParseTree" => [:while, [:false],
4225
+ [:call, [:lit, 1], :+,
4226
+ [:array, [:lit, 1]]], true],
4227
+ "ParseTree" => s(:while, s(:false),
4228
+ s(:call, s(:lit, 1), :+,
4229
+ s(:arglist, s(:lit, 1))), true))
4230
+
4231
+ add_tests("while_pre_mod",
4232
+ "Ruby" => "(1 + 1) while false",
4233
+ "RawParseTree" => [:while, [:false],
4234
+ [:call, [:lit, 1], :+,
4235
+ [:array, [:lit, 1]]], true],
4236
+ "ParseTree" => s(:while, s(:false),
4237
+ s(:call, s(:lit, 1), :+,
4238
+ s(:arglist, s(:lit, 1))), true),
4239
+ "Ruby2Ruby" => "while false do\n (1 + 1)\nend") # FIX can be one liner
4240
+
4241
+ add_tests("while_pre_nil",
4242
+ "Ruby" => "while false do\nend",
4243
+ "RawParseTree" => [:while, [:false], nil, true],
4244
+ "ParseTree" => s(:while, s(:false), nil, true))
4245
+
4246
+ add_tests("while_pre_not",
4247
+ "Ruby" => "while not true do\n (1 + 1)\nend",
4248
+ "RawParseTree" => [:until, [:true],
4249
+ [:call, [:lit, 1], :+,
4250
+ [:array, [:lit, 1]]], true],
4251
+ "ParseTree" => s(:until, s(:true),
4252
+ s(:call, s(:lit, 1), :+,
4253
+ s(:arglist, s(:lit, 1))), true),
4254
+ "Ruby2Ruby" => "until true do\n (1 + 1)\nend")
4255
+
4256
+ add_tests("while_pre_not_mod",
4257
+ "Ruby" => "(1 + 1) while not true",
4258
+ "RawParseTree" => [:until, [:true],
4259
+ [:call, [:lit, 1], :+,
4260
+ [:array, [:lit, 1]]], true],
4261
+ "ParseTree" => s(:until, s(:true),
4262
+ s(:call, s(:lit, 1), :+,
4263
+ s(:arglist, s(:lit, 1))), true),
4264
+ "Ruby2Ruby" => "until true do\n (1 + 1)\nend") # FIX
4265
+
4266
+ add_tests("xstr",
4267
+ "Ruby" => "`touch 5`",
4268
+ "RawParseTree" => [:xstr, 'touch 5'],
4269
+ "ParseTree" => s(:xstr, 'touch 5'))
4270
+
4271
+ add_tests("yield_0",
4272
+ "Ruby" => "yield",
4273
+ "RawParseTree" => [:yield],
4274
+ "ParseTree" => s(:yield))
4275
+
4276
+ add_tests("yield_1",
4277
+ "Ruby" => "yield(42)",
4278
+ "RawParseTree" => [:yield, [:lit, 42]],
4279
+ "ParseTree" => s(:yield, s(:lit, 42)))
4280
+
4281
+ add_tests("yield_n",
4282
+ "Ruby" => "yield(42, 24)",
4283
+ "RawParseTree" => [:yield, [:array, [:lit, 42], [:lit, 24]]],
4284
+ "ParseTree" => s(:yield, s(:lit, 42), s(:lit, 24)))
4285
+
4286
+ add_tests("yield_splat",
4287
+ "Ruby" => "yield(*ary)",
4288
+ "RawParseTree" => [:yield, [:splat, [:vcall, :ary]]],
4289
+ "ParseTree" => s(:yield, s(:splat, s(:call, nil, :ary, s(:arglist)))))
4290
+
4291
+ add_tests("yield_array",
4292
+ "Ruby" => "yield([42, 24])",
4293
+ "RawParseTree" => [:yield, [:array, [:lit, 42], [:lit, 24]], true],
4294
+ "ParseTree" => s(:yield, s(:array, s(:lit, 42), s(:lit, 24))))
4295
+
4296
+ add_tests("yield_zarray",
4297
+ "Ruby" => "yield([])",
4298
+ "RawParseTree" => [:yield, [:zarray], true],
4299
+ "ParseTree" => s(:yield, s(:array)))
4300
+
4301
+ add_tests("zarray",
4302
+ "Ruby" => "a = []",
4303
+ "RawParseTree" => [:lasgn, :a, [:zarray]],
4304
+ "ParseTree" => s(:lasgn, :a, s(:array)))
4305
+
4306
+ add_tests("zsuper",
4307
+ "Ruby" => "def x\n super\nend",
4308
+ "RawParseTree" => [:defn, :x,
4309
+ [:scope, [:block, [:args], [:zsuper]]]],
4310
+ "ParseTree" => s(:defn, :x, s(:args),
4311
+ s(:scope, s(:block, s(:zsuper)))))
4312
+ end