1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
# frozen_string_literal: true
module YARP
class CompilerTest < Test::Unit::TestCase
def test_empty_program
test_yarp_eval("")
end
############################################################################
# Literals #
############################################################################
def test_FalseNode
test_yarp_eval("false")
end
def test_FloatNode
test_yarp_eval("1.2")
test_yarp_eval("1.2e3")
test_yarp_eval("+1.2e+3")
test_yarp_eval("-1.2e-3")
end
def test_ImaginaryNode
test_yarp_eval("1i")
test_yarp_eval("+1.0i")
test_yarp_eval("1ri")
end
def test_IntegerNode
test_yarp_eval("1")
test_yarp_eval("+1")
test_yarp_eval("-1")
test_yarp_eval("0x10")
test_yarp_eval("0b10")
test_yarp_eval("0o10")
test_yarp_eval("010")
end
def test_NilNode
test_yarp_eval("nil")
end
def test_RationalNode
test_yarp_eval("1.2r")
test_yarp_eval("+1.2r")
end
def test_SelfNode
test_yarp_eval("self")
end
def test_TrueNode
test_yarp_eval("true")
end
############################################################################
# Reads #
############################################################################
def test_ClassVariableReadNode
test_yarp_eval("class YARP::CompilerTest; @@yct = 1; @@yct; end")
end
def test_ConstantPathNode
test_yarp_eval("YARP::CompilerTest")
end
def test_ConstantReadNode
test_yarp_eval("YARP")
end
def test_GlobalVariableReadNode
test_yarp_eval("$yct = 1; $yct")
end
def test_InstanceVariableReadNode
test_yarp_eval("class YARP::CompilerTest; @yct = 1; @yct; end")
end
def test_LocalVariableReadNode
test_yarp_eval("yct = 1; yct")
end
############################################################################
# Writes #
############################################################################
def test_ClassVariableTargetNode
test_yarp_eval("class YARP::CompilerTest; @@yct, @@yct1 = 1; end")
end
def test_ClassVariableWriteNode
test_yarp_eval("class YARP::CompilerTest; @@yct = 1; end")
end
def test_ClassVariableAndWriteNode
test_yarp_eval("class YARP::CompilerTest; @@yct = 0; @@yct &&= 1; end")
end
def test_ClassVariableOrWriteNode
test_yarp_eval("class YARP::CompilerTest; @@yct = 1; @@yct ||= 0; end")
test_yarp_eval("class YARP::CompilerTest; @@yct = nil; @@yct ||= 1; end")
end
def test_ClassVariableOperatorWriteNode
test_yarp_eval("class YARP::CompilerTest; @@yct = 0; @@yct += 1; end")
end
def test_ConstantTargetNode
# We don't call test_yarp_eval directly in this case becuase we
# don't want to assign the constant mutliple times if we run
# with `--repeat-count`
# Instead, we eval manually here, and remove the constant to
constant_names = ["YCT", "YCT2"]
source = "#{constant_names.join(",")} = 1"
yarp_eval = RubyVM::InstructionSequence.compile_yarp(source).eval
assert_equal yarp_eval, 1
constant_names.map { |name|
Object.send(:remove_const, name)
}
end
def test_ConstantWriteNode
# We don't call test_yarp_eval directly in this case becuase we
# don't want to assign the constant mutliple times if we run
# with `--repeat-count`
# Instead, we eval manually here, and remove the constant to
constant_name = "YCT"
source = "#{constant_name} = 1"
yarp_eval = RubyVM::InstructionSequence.compile_yarp(source).eval
assert_equal yarp_eval, 1
Object.send(:remove_const, constant_name)
end
def test_ConstantPathTargetNode
verbose = $VERBOSE
# Create some temporary nested constants
Object.send(:const_set, "MyFoo", Object)
Object.const_get("MyFoo").send(:const_set, "Bar", Object)
constant_names = ["MyBar", "MyFoo::Bar", "MyFoo::Bar::Baz"]
source = "#{constant_names.join(",")} = Object"
iseq = RubyVM::InstructionSequence.compile_yarp(source)
$VERBOSE = nil
yarp_eval = iseq.eval
$VERBOSE = verbose
assert_equal yarp_eval, Object
ensure
## Teardown temp constants
Object.const_get("MyFoo").send(:remove_const, "Bar")
Object.send(:remove_const, "MyFoo")
Object.send(:remove_const, "MyBar")
$VERBOSE = verbose
end
def test_ConstantPathWriteNode
# test_yarp_eval("YARP::YCT = 1")
end
def test_GlobalVariableTargetNode
test_yarp_eval("$yct, $yct1 = 1")
end
def test_GlobalVariableWriteNode
test_yarp_eval("$yct = 1")
end
def test_GlobalVariableAndWriteNode
test_yarp_eval("$yct = 0; $yct &&= 1")
end
def test_GlobalVariableOrWriteNode
test_yarp_eval("$yct ||= 1")
end
def test_GlobalVariableOperatorWriteNode
test_yarp_eval("$yct = 0; $yct += 1")
end
def test_InstanceVariableTargetNode
test_yarp_eval("class YARP::CompilerTest; @yct, @yct1 = 1; end")
end
def test_InstanceVariableWriteNode
test_yarp_eval("class YARP::CompilerTest; @yct = 1; end")
end
def test_InstanceVariableAndWriteNode
test_yarp_eval("@yct = 0; @yct &&= 1")
end
def test_InstanceVariableOrWriteNode
test_yarp_eval("@yct ||= 1")
end
def test_InstanceVariableOperatorWriteNode
test_yarp_eval("@yct = 0; @yct += 1")
end
def test_LocalVariableTargetNode
test_yarp_eval("yct, yct1 = 1")
end
def test_LocalVariableWriteNode
test_yarp_eval("yct = 1")
end
def test_LocalVariableAndWriteNode
test_yarp_eval("yct = 0; yct &&= 1")
end
def test_LocalVariableOrWriteNode
test_yarp_eval("yct ||= 1")
end
def test_LocalVariableOperatorWriteNode
test_yarp_eval("yct = 0; yct += 1")
end
############################################################################
# String-likes #
############################################################################
def test_EmbeddedVariableNode
# test_yarp_eval('class YARP::CompilerTest; @yct = 1; "#@yct"; end')
# test_yarp_eval('class YARP::CompilerTest; @@yct = 1; "#@@yct"; end')
test_yarp_eval('$yct = 1; "#$yct"')
end
def test_InterpolatedRegularExpressionNode
test_yarp_eval('$yct = 1; /1 #$yct 1/')
test_yarp_eval('/1 #{1 + 2} 1/')
test_yarp_eval('/1 #{"2"} #{1 + 2} 1/')
end
def test_InterpolatedStringNode
test_yarp_eval('$yct = 1; "1 #$yct 1"')
test_yarp_eval('"1 #{1 + 2} 1"')
end
def test_InterpolatedSymbolNode
test_yarp_eval('$yct = 1; :"1 #$yct 1"')
test_yarp_eval(':"1 #{1 + 2} 1"')
end
def test_InterpolatedXStringNode
test_yarp_eval('`echo #{1}`')
test_yarp_eval('`printf #{"100"}`')
end
def test_RegularExpressionNode
test_yarp_eval('/yct/')
end
def test_StringConcatNode
# test_yarp_eval('"YARP" "::" "CompilerTest"')
end
def test_StringNode
test_yarp_eval('"yct"')
end
def test_SymbolNode
test_yarp_eval(":yct")
end
def test_XStringNode
# test_yarp_eval(<<~RUBY)
# class YARP::CompilerTest
# def self.`(command) = command * 2
# `yct`
# end
# RUBY
end
############################################################################
# Jumps #
############################################################################
def test_AndNode
test_yarp_eval("true && 1")
test_yarp_eval("false && 1")
end
def test_OrNode
test_yarp_eval("true || 1")
test_yarp_eval("false || 1")
end
############################################################################
# Scopes/statements #
############################################################################
def test_ParenthesesNode
test_yarp_eval("()")
test_yarp_eval("(1)")
end
private
def compare_eval(source)
ruby_eval = RubyVM::InstructionSequence.compile(source).eval
yarp_eval = RubyVM::InstructionSequence.compile_yarp(source).eval
assert_equal ruby_eval, yarp_eval
end
def test_yarp_eval(source)
compare_eval(source)
begin
$VERBOSE, verbose_bak = nil, $VERBOSE
# Test "popped" functionality
compare_eval("#{source}; 1")
ensure
$VERBOSE = verbose_bak
end
end
end
end
|