-
Notifications
You must be signed in to change notification settings - Fork 37
/
InterpreterTests.java
354 lines (281 loc) · 12.4 KB
/
InterpreterTests.java
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import norswap.autumn.AutumnTestFixture;
import norswap.autumn.Grammar;
import norswap.autumn.Grammar.rule;
import norswap.autumn.ParseResult;
import norswap.autumn.positions.LineMapString;
import norswap.sigh.SemanticAnalysis;
import norswap.sigh.SighGrammar;
import norswap.sigh.ast.SighNode;
import norswap.sigh.interpreter.Interpreter;
import norswap.sigh.interpreter.Null;
import norswap.uranium.Reactor;
import norswap.uranium.SemanticError;
import norswap.utils.IO;
import norswap.utils.TestFixture;
import norswap.utils.data.wrappers.Pair;
import norswap.utils.visitors.Walker;
import org.testng.annotations.Test;
import java.util.HashMap;
import java.util.Set;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertThrows;
public final class InterpreterTests extends TestFixture {
// TODO peeling
// ---------------------------------------------------------------------------------------------
private final SighGrammar grammar = new SighGrammar();
private final AutumnTestFixture autumnFixture = new AutumnTestFixture();
{
autumnFixture.runTwice = false;
autumnFixture.bottomClass = this.getClass();
}
// ---------------------------------------------------------------------------------------------
private Grammar.rule rule;
// ---------------------------------------------------------------------------------------------
private void check (String input, Object expectedReturn) {
assertNotNull(rule, "You forgot to initialize the rule field.");
check(rule, input, expectedReturn, null);
}
// ---------------------------------------------------------------------------------------------
private void check (String input, Object expectedReturn, String expectedOutput) {
assertNotNull(rule, "You forgot to initialize the rule field.");
check(rule, input, expectedReturn, expectedOutput);
}
// ---------------------------------------------------------------------------------------------
private void check (rule rule, String input, Object expectedReturn, String expectedOutput) {
// TODO
// (1) write proper parsing tests
// (2) write some kind of automated runner, and use it here
autumnFixture.rule = rule;
ParseResult parseResult = autumnFixture.success(input);
SighNode root = parseResult.topValue();
Reactor reactor = new Reactor();
Walker<SighNode> walker = SemanticAnalysis.createWalker(reactor);
Interpreter interpreter = new Interpreter(reactor);
walker.walk(root);
reactor.run();
Set<SemanticError> errors = reactor.errors();
if (!errors.isEmpty()) {
LineMapString map = new LineMapString("<test>", input);
String report = reactor.reportErrors(it ->
it.toString() + " (" + ((SighNode) it).span.startString(map) + ")");
// String tree = AttributeTreeFormatter.format(root, reactor,
// new ReflectiveFieldWalker<>(SighNode.class, PRE_VISIT, POST_VISIT));
// System.err.println(tree);
throw new AssertionError(report);
}
Pair<String, Object> result = IO.captureStdout(() -> interpreter.interpret(root));
assertEquals(result.b, expectedReturn);
if (expectedOutput != null) assertEquals(result.a, expectedOutput);
}
// ---------------------------------------------------------------------------------------------
private void checkExpr (String input, Object expectedReturn, String expectedOutput) {
rule = grammar.root;
check("return " + input, expectedReturn, expectedOutput);
}
// ---------------------------------------------------------------------------------------------
private void checkExpr (String input, Object expectedReturn) {
rule = grammar.root;
check("return " + input, expectedReturn);
}
// ---------------------------------------------------------------------------------------------
private void checkThrows (String input, Class<? extends Throwable> expected) {
assertThrows(expected, () -> check(input, null));
}
// ---------------------------------------------------------------------------------------------
@Test
public void testLiteralsAndUnary () {
checkExpr("42", 42L);
checkExpr("42.0", 42.0d);
checkExpr("\"hello\"", "hello");
checkExpr("(42)", 42L);
checkExpr("[1, 2, 3]", new Object[]{1L, 2L, 3L});
checkExpr("true", true);
checkExpr("false", false);
checkExpr("null", Null.INSTANCE);
checkExpr("!false", true);
checkExpr("!true", false);
checkExpr("!!true", true);
}
// ---------------------------------------------------------------------------------------------
@Test
public void testNumericBinary () {
checkExpr("1 + 2", 3L);
checkExpr("2 - 1", 1L);
checkExpr("2 * 3", 6L);
checkExpr("2 / 3", 0L);
checkExpr("3 / 2", 1L);
checkExpr("2 % 3", 2L);
checkExpr("3 % 2", 1L);
checkExpr("1.0 + 2.0", 3.0d);
checkExpr("2.0 - 1.0", 1.0d);
checkExpr("2.0 * 3.0", 6.0d);
checkExpr("2.0 / 3.0", 2d / 3d);
checkExpr("3.0 / 2.0", 3d / 2d);
checkExpr("2.0 % 3.0", 2.0d);
checkExpr("3.0 % 2.0", 1.0d);
checkExpr("1 + 2.0", 3.0d);
checkExpr("2 - 1.0", 1.0d);
checkExpr("2 * 3.0", 6.0d);
checkExpr("2 / 3.0", 2d / 3d);
checkExpr("3 / 2.0", 3d / 2d);
checkExpr("2 % 3.0", 2.0d);
checkExpr("3 % 2.0", 1.0d);
checkExpr("1.0 + 2", 3.0d);
checkExpr("2.0 - 1", 1.0d);
checkExpr("2.0 * 3", 6.0d);
checkExpr("2.0 / 3", 2d / 3d);
checkExpr("3.0 / 2", 3d / 2d);
checkExpr("2.0 % 3", 2.0d);
checkExpr("3.0 % 2", 1.0d);
checkExpr("2 * (4-1) * 4.0 / 6 % (2+1)", 1.0d);
}
// ---------------------------------------------------------------------------------------------
@Test
public void testOtherBinary () {
checkExpr("true && true", true);
checkExpr("true || true", true);
checkExpr("true || false", true);
checkExpr("false || true", true);
checkExpr("false && true", false);
checkExpr("true && false", false);
checkExpr("false && false", false);
checkExpr("false || false", false);
checkExpr("1 + \"a\"", "1a");
checkExpr("\"a\" + 1", "a1");
checkExpr("\"a\" + true", "atrue");
checkExpr("1 == 1", true);
checkExpr("1 == 2", false);
checkExpr("1.0 == 1.0", true);
checkExpr("1.0 == 2.0", false);
checkExpr("true == true", true);
checkExpr("false == false", true);
checkExpr("true == false", false);
checkExpr("1 == 1.0", true);
checkExpr("[1] == [1]", false);
checkExpr("1 != 1", false);
checkExpr("1 != 2", true);
checkExpr("1.0 != 1.0", false);
checkExpr("1.0 != 2.0", true);
checkExpr("true != true", false);
checkExpr("false != false", false);
checkExpr("true != false", true);
checkExpr("1 != 1.0", false);
checkExpr("\"hi\" != \"hi2\"", true);
checkExpr("[1] != [1]", true);
// test short circuit
checkExpr("true || print(\"x\") == \"y\"", true, "");
checkExpr("false && print(\"x\") == \"y\"", false, "");
}
// ---------------------------------------------------------------------------------------------
@Test
public void testVarDecl () {
check("var x: Int = 1; return x", 1L);
check("var x: Float = 2.0; return x", 2d);
check("var x: Int = 0; return x = 3", 3L);
check("var x: String = \"0\"; return x = \"S\"", "S");
// implicit conversions
check("var x: Float = 1; x = 2; return x", 2.0d);
}
// ---------------------------------------------------------------------------------------------
@Test
public void testRootAndBlock () {
rule = grammar.root;
check("return", null);
check("return 1", 1L);
check("return 1; return 2", 1L);
check("print(\"a\")", null, "a\n");
check("print(\"a\" + 1)", null, "a1\n");
check("print(\"a\"); print(\"b\")", null, "a\nb\n");
check("{ print(\"a\"); print(\"b\") }", null, "a\nb\n");
check(
"var x: Int = 1;" +
"{ print(\"\" + x); var x: Int = 2; print(\"\" + x) }" +
"print(\"\" + x)",
null, "1\n2\n1\n");
}
// ---------------------------------------------------------------------------------------------
@Test
public void testCalls () {
check(
"fun add (a: Int, b: Int): Int { return a + b } " +
"return add(4, 7)",
11L);
HashMap<String, Object> point = new HashMap<>();
point.put("x", 1L);
point.put("y", 2L);
check(
"struct Point { var x: Int; var y: Int }" +
"return $Point(1, 2)",
point);
check("var str: String = null; return print(str + 1)", "null1", "null1\n");
}
// ---------------------------------------------------------------------------------------------
@Test
public void testArrayStructAccess () {
checkExpr("[1][0]", 1L);
checkExpr("[1.0][0]", 1d);
checkExpr("[1, 2][1]", 2L);
// TODO check that this fails (& maybe improve so that it generates a better message?)
// or change to make it legal (introduce a top type, and make it a top type array if thre
// is no inference context available)
// checkExpr("[].length", 0L);
checkExpr("[1].length", 1L);
checkExpr("[1, 2].length", 2L);
checkThrows("var array: Int[] = null; return array[0]", NullPointerException.class);
checkThrows("var array: Int[] = null; return array.length", NullPointerException.class);
check("var x: Int[] = [0, 1]; x[0] = 3; return x[0]", 3L);
checkThrows("var x: Int[] = []; x[0] = 3; return x[0]",
ArrayIndexOutOfBoundsException.class);
checkThrows("var x: Int[] = null; x[0] = 3",
NullPointerException.class);
check(
"struct P { var x: Int; var y: Int }" +
"return $P(1, 2).y",
2L);
checkThrows(
"struct P { var x: Int; var y: Int }" +
"var p: P = null;" +
"return p.y",
NullPointerException.class);
check(
"struct P { var x: Int; var y: Int }" +
"var p: P = $P(1, 2);" +
"p.y = 42;" +
"return p.y",
42L);
checkThrows(
"struct P { var x: Int; var y: Int }" +
"var p: P = null;" +
"p.y = 42",
NullPointerException.class);
}
// ---------------------------------------------------------------------------------------------
@Test
public void testIfWhile () {
check("if (true) return 1 else return 2", 1L);
check("if (false) return 1 else return 2", 2L);
check("if (false) return 1 else if (true) return 2 else return 3 ", 2L);
check("if (false) return 1 else if (false) return 2 else return 3 ", 3L);
check("var i: Int = 0; while (i < 3) { print(\"\" + i); i = i + 1 } ", null, "0\n1\n2\n");
}
// ---------------------------------------------------------------------------------------------
@Test
public void testInference () {
check("var array: Int[] = []", null);
check("var array: String[] = []", null);
check("fun use_array (array: Int[]) {} ; use_array([])", null);
}
// ---------------------------------------------------------------------------------------------
@Test
public void testTypeAsValues () {
check("struct S{} ; return \"\"+ S", "S");
check("struct S{} ; var type: Type = S ; return \"\"+ type", "S");
}
// ---------------------------------------------------------------------------------------------
@Test public void testUnconditionalReturn()
{
check("fun f(): Int { if (true) return 1 else return 2 } ; return f()", 1L);
}
// ---------------------------------------------------------------------------------------------
// NOTE(norswap): Not incredibly complete, but should cover the basics.
}