-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathManualTest.java
More file actions
228 lines (204 loc) · 10.1 KB
/
ManualTest.java
File metadata and controls
228 lines (204 loc) · 10.1 KB
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
package CBuilder;
import CBuilder.literals.IntLiteral;
import CBuilder.objects.*;
import CBuilder.objects.functions.Argument;
import CBuilder.objects.functions.Function;
import CBuilder.variables.Assignment;
import CBuilder.variables.VariableDeclaration;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
/**
* Allows to test the Builder without involving the ast or anything.
*
* <p>TODO remove this when manual testing of this is not needed anymore
*/
public class ManualTest {
/**
* Mini-Python code of program generated: ```python a b
*
* <p>a = b d = -30
*
* <p>type(a) type(d)
*
* <p>idA = id(a) print(idA)
*
* <p>print(id(a)) id(print(d)) id(type(a))
*
* <p>e = id(type(print(d))) e = print(-50) ```
*/
static void generateProgram(Path output) {
ProgramBuilder builder = new ProgramBuilder();
builder.addVariable(new VariableDeclaration("a"));
builder.addVariable(new VariableDeclaration("b"));
builder.addStatement(new Assignment(new Reference("a"), new Reference("b")));
// does cause build errors with -Wall since the assignment is indeed quite useless
// (and there's no good solution to ignore the specific error on assignment)
// builder.addVariable(new VariableDeclaration("c"));
// builder.addStatement(new Assignment(new VariableReference("c"), new
// VariableReference("c")));
builder.addVariable(new VariableDeclaration("d"));
builder.addStatement(new Assignment(new Reference("d"), new IntLiteral(-30)));
builder.addStatement(
new Call(new Reference("type"), List.of(new Expression[] {new Reference("a")})));
builder.addStatement(
new Call(new Reference("type"), List.of(new Expression[] {new Reference("d")})));
builder.addVariable(new VariableDeclaration("idA"));
builder.addStatement(
new Assignment(
new Reference("idA"),
new Call(
new Reference("id"),
List.of(new Expression[] {new Reference("a")}))));
builder.addStatement(
new Call(new Reference("print"), List.of(new Expression[] {new Reference("idA")})));
// call each built-in function in a nested context to make sure it correctly
// implements refCounts
builder.addStatement(
new Call(
new Reference("print"),
List.of(
new Expression[] {
new Call(
new Reference("id"),
List.of(new Expression[] {new Reference("a")}))
})));
builder.addStatement(
new Call(
new Reference("id"),
List.of(
new Expression[] {
new Call(
new Reference("print"),
List.of(new Expression[] {new Reference("d")}))
})));
builder.addStatement(
new Call(
new Reference("id"),
List.of(
new Expression[] {
new Call(
new Reference("type"),
List.of(new Expression[] {new Reference("a")}))
})));
// assign a nested function call to a variable
builder.addVariable(new VariableDeclaration("e"));
builder.addStatement(
new Call(
new Reference("id"),
List.of(
new Expression[] {
new Call(
new Reference("type"),
List.of(
new Expression[] {
new Call(
new Reference("print"),
List.of(
new Expression[] {
new Reference("d")
}))
}))
})));
// call a function on a literal and assign that to a variable
builder.addStatement(
new Assignment(
new Reference("e"),
new Call(
new Reference("print"),
List.of(new Expression[] {new IntLiteral(-50)}))));
builder.addStatement(
new Call(new Reference("type"), List.of(new Expression[] {new Reference("type")})));
builder.addFunction(
new Function(
"printA",
List.of(
new Statement[] {
new Call(
new Reference("print"),
List.of(new Expression[] {new Reference("a")}))
}),
List.of(new Argument[] {new Argument("a", 0)}),
List.of()));
builder.addStatement(
new Call(
new Reference("printA"), List.of(new Expression[] {new Reference("idA")})));
builder.addClass(
new MPyClass(
"B",
new Reference("__MPyType_Object"),
List.of(
new Function[] {
new Function(
"print",
List.of(
new Statement[] {
new Call(
new Reference("print"),
List.of(
new Expression[] {
new Reference("self")
}))
}),
List.of(new Argument[] {new Argument("self", 0)}),
List.of()),
new Function(
"__init__",
List.of(
new Statement[] {
new SuperCall(List.of()),
new AttributeAssignment(
new AttributeReference(
"b", new Reference("self")),
new IntLiteral(100))
}),
List.of(new Argument[] {new Argument("self", 0)}),
List.of())
}),
new HashMap<>()));
builder.addClass(
new MPyClass(
"C",
new Reference("B"),
List.of(
new Function[] {
new Function(
"__init__",
List.of(new Statement[] {new SuperCall(List.of())}),
List.of(new Argument[] {new Argument("self", 0)}),
List.of())
}),
new HashMap<>()));
builder.addVariable(new VariableDeclaration("bObj"));
builder.addStatement(
new Assignment(new Reference("bObj"), new Call(new Reference("B"), List.of())));
builder.addStatement(
new Call(new AttributeReference("print", new Reference("bObj")), List.of()));
// print(bObj.b)
builder.addStatement(
new Call(
new Reference("print"),
List.of(
new Expression[] {
new AttributeReference("b", new Reference("bObj"))
})));
builder.addVariable(new VariableDeclaration("cObj"));
builder.addStatement(
new Assignment(new Reference("cObj"), new Call(new Reference("C"), List.of())));
builder.addStatement(
new Call(new AttributeReference("print", new Reference("cObj")), List.of()));
builder.addStatement(
new Call(
new Reference("print"),
List.of(
new Expression[] {
new AttributeReference("b", new Reference("cObj"))
})));
builder.writeProgram(output);
}
public static void main(String[] args) {
java.nio.file.Path fileOutput =
java.nio.file.FileSystems.getDefault().getPath("build/compilerOutput/ManualTest/");
generateProgram(fileOutput);
}
}