-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathBytecodeTests.java
270 lines (222 loc) · 9.84 KB
/
BytecodeTests.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
import norswap.autumn.Autumn;
import norswap.autumn.ParseOptions;
import norswap.autumn.ParseResult;
import norswap.sigh.SemanticAnalysis;
import norswap.sigh.SighGrammar;
import norswap.sigh.ast.SighNode;
import norswap.sigh.bytecode.ByteArrayClassLoader;
import norswap.sigh.bytecode.BytecodeCompiler;
import norswap.sigh.bytecode.CompilationResult;
import norswap.uranium.Reactor;
import norswap.utils.IO;
import norswap.utils.visitors.Walker;
import org.testng.annotations.Test;
import static norswap.utils.Util.cast;
import static org.testng.Assert.assertEquals;
@SuppressWarnings("FieldCanBeLocal")
public class BytecodeTests
{
// TODO test multi dimensional arrays
// ---------------------------------------------------------------------------------------------
/**
* Checks that the input program can be compiled, run and prints the {@code expected} string if
* non-null (to which a newline is appended if not empty).
*/
public void check (String input, String expected)
{
SighGrammar grammar = new SighGrammar();
ParseOptions options = ParseOptions.builder().recordCallStack(true).get();
ParseResult parseResult = Autumn.parse(grammar.root, input, options);
if (!parseResult.fullMatch) throw new AssertionError(parseResult.toString());
SighNode tree = cast(parseResult.topValue());
Reactor reactor = new Reactor();
Walker<SighNode> walker = SemanticAnalysis.createWalker(reactor);
walker.walk(tree);
reactor.run();
if (!reactor.errors().isEmpty())
throw new AssertionError(reactor.reportErrors(Object::toString));
String className = "BytecodeTestsRun";
BytecodeCompiler compiler = new BytecodeCompiler(reactor);
CompilationResult result = compiler.compile(className, tree);
// using a new loader each time allows to overwrite the class every time.
Class<?> mainClass = result.load(new ByteArrayClassLoader());
if (expected == null) {
CompilationResult.callMain(mainClass);
return;
}
// TODO utils capture stdout with runnable
String capture = IO.captureStdout(() -> {
CompilationResult.callMain(mainClass);
return null;
}).a;
if (!expected.isEmpty())
expected = expected + "\n";
assertEquals(capture, expected);
}
// ---------------------------------------------------------------------------------------------
/**
* Checks that the input <b>expression</b> can be converted to a string and printed, and that the printed
* string corresponds to the {@code expected} string.
*/
public void checkExpr (String input, String expected) {
check("print(\"\" + ("+ input + "))", expected);
}
// ---------------------------------------------------------------------------------------------
@Test
public void basicPrintTest() {
// explicit unroll basic tests that involve print
check("print(\"hello\")", "hello");
check("print(\"hello\") ; return", "hello");
check("print(\"\" + 1)", "1");
check("print(\"\" + 1 + 2)", "12");
check("print(1 + 2 + \"\")", "3");
check("print(\"\" + (1 + 2))", "3");
}
// ---------------------------------------------------------------------------------------------
@Test
public void testLiteralsAndUnary () {
checkExpr("42", "42");
checkExpr("42.0", "42.0");
checkExpr("\"hello\"", "hello");
checkExpr("(42)", "42");
checkExpr("[1, 2, 3]", "[1, 2, 3]");
checkExpr("true", "true");
checkExpr("false", "false");
checkExpr("null", "null");
checkExpr("!false", "true");
checkExpr("!true", "false");
checkExpr("!!true", "true");
}
// ---------------------------------------------------------------------------------------------
@Test
public void testNumericBinary () {
checkExpr("1 + 2", "3");
checkExpr("2 - 1", "1");
checkExpr("2 * 3", "6");
checkExpr("2 / 3", "0");
checkExpr("3 / 2", "1");
checkExpr("2 % 3", "2");
checkExpr("3 % 2", "1");
checkExpr("1.0 + 2.0", "3.0");
checkExpr("2.0 - 1.0", "1.0");
checkExpr("2.0 * 3.0", "6.0");
checkExpr("2.0 / 3.0", "" + (2d / 3d));
checkExpr("3.0 / 2.0", "" + (3d / 2d));
checkExpr("2.0 % 3.0", "2.0");
checkExpr("3.0 % 2.0", "1.0");
checkExpr("1 + 2.0", "3.0");
checkExpr("2 - 1.0", "1.0");
checkExpr("2 * 3.0", "6.0");
checkExpr("2 / 3.0", "" + (2d / 3d));
checkExpr("3 / 2.0", "" + (3d / 2d));
checkExpr("2 % 3.0", "2.0");
checkExpr("3 % 2.0", "1.0");
checkExpr("1.0 + 2", "3.0");
checkExpr("2.0 - 1", "1.0");
checkExpr("2.0 * 3", "6.0");
checkExpr("2.0 / 3", "" + (2d / 3d));
checkExpr("3.0 / 2", "" + (3d / 2d));
checkExpr("2.0 % 3", "2.0");
checkExpr("3.0 % 2", "1.0");
checkExpr("2 * (4-1) * 4.0 / 6 % (2+1)", "1.0");
}
// ---------------------------------------------------------------------------------------------
@Test public void testLogic() {
// check boolean logic
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; print(\"\" + x)", "1");
check("var x: Float = 2.0; print(\"\" + x)", "2.0");
check("var x: Int = 0; x = 3; print(\"\" + x)", "3");
// TODO fails
//check("var x: String = \"0\"; print(x = \"S\")", "S");
check("var x: String = \"0\"; x = \"S\"; print(x)", "S");
// implicit conversions
check("var x: Float = 1; x = 2; print(\"\" + x)", "2.0");
}
// ---------------------------------------------------------------------------------------------
@Test public void testArrays() {
checkExpr("[1, 2, 3]", "[1, 2, 3]");
checkExpr("[\"a\", \"b\", \"c\"]", "[a, b, c]");
checkExpr("[1.0, 2.0]", "[1.0, 2.0]");
checkExpr("[1, 2, 3][0]", "1");
checkExpr("[1, 2, 3][1]", "2");
checkExpr("[1, 2, 3][2]", "3");
checkExpr("[\"a\", \"b\", \"c\"][0]", "a");
checkExpr("[\"a\", \"b\", \"c\"][2]", "c");
check("var x: Float[] = [1.0, 2.0]; x[0] = 3.0; print(\"\" + x[0])", "3.0");
check("var x: Float[] = [1.0, 2.0]; x[0] = 3; print(\"\" + x[0])", "3.0");
}
private final String printa = "print(\"a\")";
private final String printb = "print(\"b\")";
private final String printx = "print(\"\" + (x))";
private final String printy = "print(\"\" + (y))";
@Test public void testVariables() {
check("var x: Int = 1;" + printx, "1");
check("var x: String = \"a\";" + printx, "a");
check("var x: Int = 1 ; " + printx + " ; x = 2 ; " + printx, "1\n2");
// longs and double have double width
check("var x: Int = 1 ; var y: Float = 2.0 ;" + printx + printy, "1\n2.0");
check("var x: Int = 1 ; var y: String = \"a\" ;" + printx + printy, "1\na");
check("var x: String = \"a\" ; var y: Int = 1 ;" + printx + printy, "a\n1");
check("var x: Float = 1.0 ; var y: String = \"a\" ;" + printx + printy, "1.0\na");
// implicit conversion
check("var x: Float = 1 ;" + printx, "1.0");
check("var x: Float = 1 ;" + printx + "x = 2 ;" + printx, "1.0\n2.0");
}
@Test public void testIfWhile() {
check("if 1 == 1 " + printa, "a");
check("if 1 == 1 " + printa + "else " + printb, "a");
check("if 1 == 0 " + printa + "else " + printb, "b");
check("var x: Int = 1 ; while x == 3 { " + printx + "}", "");
check("var x: Int = 1 ; while x <= 3 { " + printx + " ; x = x + 1 }", "1\n2\n3");
}
@Test public void testMethod() {
check("fun test (x: String):String { return x } print(test(\"a\"))", "a");
check("fun test (x: String) { print(x) } ; test(\"a\")", "a");
check("fun test () { fun foo() { print(\"a\") } foo() foo() } test()", "a\na");
}
private final String makePair =
"struct Pair { var x: Int ; var y: Float }" +
"var x: Pair = $Pair(1, 2.0) ;";
@Test public void testStructs() {
check(makePair + "print(\"\" + x.x + \":\" + x.y)", "1:2.0");
check(makePair + "x.x = 3; print(\"\" + x.x)", "3");
check(makePair + "x.y = 3; print(\"\" + x.y)", "3.0");
}
}