66import com .compilerprogramming .ezlang .semantic .SemaDefineTypes ;
77import com .compilerprogramming .ezlang .types .Symbol ;
88import com .compilerprogramming .ezlang .types .TypeDictionary ;
9+ import org .junit .Assert ;
910import org .junit .Test ;
1011
1112import java .util .BitSet ;
1213
1314public class TestCompiler {
1415
15- void compileSrc (String src , String functionName ) {
16+ String compileSrc (String src ) {
1617 Parser parser = new Parser ();
1718 var program = parser .parse (new Lexer (src ));
1819 var typeDict = new TypeDictionary ();
@@ -22,12 +23,14 @@ void compileSrc(String src, String functionName) {
2223 sema2 .analyze (program );
2324 RegisterVMCompiler byteCodeCompiler = new RegisterVMCompiler ();
2425 byteCodeCompiler .compile (typeDict );
26+ StringBuilder sb = new StringBuilder ();
2527 for (Symbol s : typeDict .bindings .values ()) {
2628 if (s instanceof Symbol .FunctionTypeSymbol f ) {
2729 var functionBuilder = (FunctionBuilder ) f .code ;
28- System . out . println ( BasicBlock .toStr (new StringBuilder () , functionBuilder .entry , new BitSet () ));
30+ BasicBlock .toStr (sb , functionBuilder .entry , new BitSet ());
2931 }
3032 }
33+ return sb .toString ();
3134 }
3235
3336 @ Test
@@ -37,7 +40,13 @@ func foo(n: Int)->Int {
3740 return 1;
3841 }
3942 """ ;
40- compileSrc (src , "foo" );
43+ String result = compileSrc (src );
44+ Assert .assertEquals ("""
45+ L0:
46+ \t RET = 1
47+ \t goto L1
48+ L1:
49+ """ , result );
4150 }
4251
4352 @ Test
@@ -47,7 +56,13 @@ func foo(n: Int)->Int {
4756 return -1;
4857 }
4958 """ ;
50- compileSrc (src , "foo" );
59+ String result = compileSrc (src );
60+ Assert .assertEquals ("""
61+ L0:
62+ \t RET = -1
63+ \t goto L1
64+ L1:
65+ """ , result );
5166 }
5267
5368 @ Test
@@ -57,7 +72,13 @@ func foo(n: Int)->Int {
5772 return n;
5873 }
5974 """ ;
60- compileSrc (src , "foo" );
75+ String result = compileSrc (src );
76+ Assert .assertEquals ("""
77+ L0:
78+ \t RET = Local{0}
79+ \t goto L1
80+ L1:
81+ """ , result );
6182 }
6283
6384 @ Test
@@ -67,7 +88,8 @@ func foo(n: Int)->Int {
6788 return -n;
6889 }
6990 """ ;
70- compileSrc (src , "foo" );
91+ String result = compileSrc (src );
92+
7193 }
7294
7395 @ Test
@@ -77,7 +99,8 @@ func foo(n: Int)->Int {
7799 return n+1;
78100 }
79101 """ ;
80- compileSrc (src , "foo" );
102+ String result = compileSrc (src );
103+
81104 }
82105 @ Test
83106 public void testFunction6 () {
@@ -86,7 +109,8 @@ func foo(n: Int)->Int {
86109 return 1+1;
87110 }
88111 """ ;
89- compileSrc (src , "foo" );
112+ String result = compileSrc (src );
113+
90114 }
91115 @ Test
92116 public void testFunction7 () {
@@ -95,7 +119,8 @@ func foo(n: Int)->Int {
95119 return 1+1-1;
96120 }
97121 """ ;
98- compileSrc (src , "foo" );
122+ String result = compileSrc (src );
123+
99124 }
100125 @ Test
101126 public void testFunction8 () {
@@ -104,7 +129,8 @@ func foo(n: Int)->Int {
104129 return 2==2;
105130 }
106131 """ ;
107- compileSrc (src , "foo" );
132+ String result = compileSrc (src );
133+
108134 }
109135 @ Test
110136 public void testFunction9 () {
@@ -113,7 +139,8 @@ func foo(n: Int)->Int {
113139 return 1!=1;
114140 }
115141 """ ;
116- compileSrc (src , "foo" );
142+ String result = compileSrc (src );
143+
117144 }
118145
119146 @ Test
@@ -123,7 +150,8 @@ func foo(n: [Int])->Int {
123150 return n[0];
124151 }
125152 """ ;
126- compileSrc (src , "foo" );
153+ String result = compileSrc (src );
154+
127155 }
128156
129157 @ Test
@@ -133,7 +161,8 @@ func foo(n: [Int])->Int {
133161 return n[0]+n[1];
134162 }
135163 """ ;
136- compileSrc (src , "foo" );
164+ String result = compileSrc (src );
165+
137166 }
138167
139168 @ Test
@@ -143,7 +172,8 @@ func foo()->[Int] {
143172 return new [Int] { 1, 2, 3 };
144173 }
145174 """ ;
146- compileSrc (src , "foo" );
175+ String result = compileSrc (src );
176+
147177 }
148178
149179 @ Test
@@ -153,7 +183,8 @@ func foo(n: Int) -> [Int] {
153183 return new [Int] { n };
154184 }
155185 """ ;
156- compileSrc (src , "foo" );
186+ String result = compileSrc (src );
187+
157188 }
158189
159190 @ Test
@@ -163,7 +194,8 @@ func add(x: Int, y: Int) -> Int {
163194 return x+y;
164195 }
165196 """ ;
166- compileSrc (src , "add" );
197+ String result = compileSrc (src );
198+
167199 }
168200
169201 @ Test
@@ -178,7 +210,8 @@ func foo(p: Person) -> Person {
178210 p.age = 10;
179211 }
180212 """ ;
181- compileSrc (src , "foo" );
213+ String result = compileSrc (src );
214+
182215 }
183216
184217 @ Test
@@ -193,7 +226,8 @@ func foo() -> Person {
193226 return new Person { age=10, children=0 };
194227 }
195228 """ ;
196- compileSrc (src , "foo" );
229+ String result = compileSrc (src );
230+
197231 }
198232
199233 @ Test
@@ -204,7 +238,8 @@ func foo(array: [Int]) {
204238 array[1] = 2
205239 }
206240 """ ;
207- compileSrc (src , "foo" );
241+ String result = compileSrc (src );
242+
208243 }
209244
210245 @ Test
@@ -216,7 +251,8 @@ func min(x: Int, y: Int) -> Int {
216251 return y;
217252 }
218253 """ ;
219- compileSrc (src , "min" );
254+ String result = compileSrc (src );
255+
220256 }
221257
222258 @ Test
@@ -228,7 +264,8 @@ func loop() {
228264 return;
229265 }
230266 """ ;
231- compileSrc (src , "loop" );
267+ String result = compileSrc (src );
268+
232269 }
233270
234271 @ Test
@@ -240,7 +277,8 @@ func loop() {
240277 return;
241278 }
242279 """ ;
243- compileSrc (src , "loop" );
280+ String result = compileSrc (src );
281+
244282 }
245283
246284 @ Test
@@ -253,7 +291,8 @@ func loop(n: Int) {
253291 return;
254292 }
255293 """ ;
256- compileSrc (src , "loop" );
294+ String result = compileSrc (src );
295+
257296 }
258297
259298 @ Test
@@ -262,7 +301,8 @@ public void testFunction19() {
262301 func foo() {}
263302 func bar() { foo(); }
264303 """ ;
265- compileSrc (src , "bar" );
304+ String result = compileSrc (src );
305+
266306 }
267307
268308 @ Test
@@ -271,7 +311,8 @@ public void testFunction20() {
271311 func foo(x: Int, y: Int) {}
272312 func bar() { foo(1,2); }
273313 """ ;
274- compileSrc (src , "bar" );
314+ String result = compileSrc (src );
315+
275316 }
276317
277318 @ Test
@@ -280,6 +321,80 @@ public void testFunction21() {
280321 func foo(x: Int, y: Int)->Int { return x+y; }
281322 func bar()->Int { var t = foo(1,2); return t+1; }
282323 """ ;
283- compileSrc (src , "bar" );
324+ String result = compileSrc (src );
325+
326+ }
327+
328+ @ Test
329+ public void testFunction22 () {
330+ String src = """
331+ struct Person
332+ {
333+ var age: Int
334+ var children: Int
335+ }
336+ func foo(p: Person) -> Int {
337+ return p.age;
338+ }
339+ """ ;
340+ String result = compileSrc (src );
341+
342+ }
343+
344+ @ Test
345+ public void testFunction23 () {
346+ String src = """
347+ struct Person
348+ {
349+ var age: Int
350+ var parent: Person
351+ }
352+ func foo(p: Person) -> Int {
353+ return p.parent.age;
354+ }
355+ """ ;
356+ String result = compileSrc (src );
357+
358+ }
359+
360+ @ Test
361+ public void testFunction24 () {
362+ String src = """
363+ struct Person
364+ {
365+ var age: Int
366+ var parent: Person
367+ }
368+ func foo(p: [Person], i: Int) -> Int {
369+ return p[i].parent.age;
370+ }
371+ """ ;
372+ String result = compileSrc (src );
373+
374+ }
375+ @ Test
376+ public void testFunction25 () {
377+ String src = """
378+ func foo(x: Int, y: Int)->Int { return x+y; }
379+ func bar(a: Int)->Int { var t = foo(a,2); return t+1; }
380+ """ ;
381+ String result = compileSrc (src );
382+ Assert .assertEquals ("""
383+ L0:
384+ \t T0 = Local{0}+Local{1}
385+ \t RET = T0
386+ \t goto L1
387+ L1:
388+ L0:
389+ \t T0 = foo
390+ \t T1 = Local{0}
391+ \t T2 = 2
392+ \t T0(T1, T2)
393+ \t Local{1} = T0
394+ \t T0 = Local{1}+1
395+ \t RET = T0
396+ \t goto L1
397+ L1:
398+ """ , result );
284399 }
285400}
0 commit comments