Skip to content

Commit c1c34b1

Browse files
Merge pull request #28 from CompilerProgramming/develop
A number of updates to the parser, type checking etc while preparing for null checks
2 parents ec2e09e + f01de7b commit c1c34b1

File tree

16 files changed

+493
-565
lines changed

16 files changed

+493
-565
lines changed

optvm/src/test/java/com/compilerprogramming/ezlang/compiler/TestCompiler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func foo()->[Int] {
214214
String result = compileSrc(src);
215215
Assert.assertEquals("""
216216
L0:
217-
%t0 = New([Int,Int])
217+
%t0 = New([Int])
218218
%t0.append(1)
219219
%t0.append(2)
220220
%t0.append(3)
@@ -235,7 +235,7 @@ func foo(n: Int) -> [Int] {
235235
Assert.assertEquals("""
236236
L0:
237237
arg n
238-
%t1 = New([Int,Int])
238+
%t1 = New([Int])
239239
%t1.append(n)
240240
ret %t1
241241
goto L1

optvm/src/test/java/com/compilerprogramming/ezlang/compiler/TestSSATransform.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ func bar(x: Int)->Int {
788788
return x;
789789
}
790790
791-
func foo() {
791+
func foo()->Int {
792792
return bar(10);
793793
}
794794
""";

optvm/src/test/java/com/compilerprogramming/ezlang/interpreter/TestInterpreter.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func fib(n: Int)->Int {
167167
return f2;
168168
}
169169
170-
func foo() {
170+
func foo()->Int {
171171
return fib(10);
172172
}
173173
""";
@@ -229,7 +229,7 @@ func bar(data: [Int]) {
229229
else
230230
data[2] = 123 + j + 10
231231
}
232-
func foo() {
232+
func foo()->Int {
233233
var data = new [Int] {0,0,0}
234234
bar(data)
235235
return data[0]+data[1]+data[2];
@@ -253,7 +253,7 @@ func bar(data: [Int]) {
253253
else
254254
data[2] = 123 - j + 10
255255
}
256-
func foo() {
256+
func foo()->Int {
257257
var data = new [Int] {0,0,0}
258258
bar(data)
259259
return data[0]+data[1]+data[2];
@@ -277,7 +277,7 @@ func bar(data: [Int]) {
277277
data[2] = 15
278278
data[3] = j + 21
279279
}
280-
func foo() {
280+
func foo()->Int {
281281
var data = new [Int] {0,0,0,0}
282282
bar(data)
283283
return data[0]+data[1]+data[2]+data[3];
@@ -301,7 +301,7 @@ func bar(data: [Int]) {
301301
data[2] = j * 15
302302
data[3] = j * 21
303303
}
304-
func foo() {
304+
func foo()->Int {
305305
var data = new [Int] {0,0,0,0}
306306
bar(data)
307307
return data[0]+data[1]+data[2]+data[3];
@@ -329,7 +329,7 @@ func bar(data: [Int]) {
329329
}
330330
data[3] = j + 21
331331
}
332-
func foo() {
332+
func foo()->Int {
333333
var data = new [Int] {1,0,0,0}
334334
bar(data)
335335
return data[0]+data[1]+data[2]+data[3]
@@ -361,7 +361,7 @@ func bar(data: [Int]) {
361361
}
362362
data[3] = (j+k) * 21
363363
}
364-
func foo() {
364+
func foo()->Int {
365365
var data = new [Int] {1,0,0,0}
366366
bar(data)
367367
return data[0]+data[1]+data[2]+data[3]
@@ -389,7 +389,7 @@ func bar(data: [Int]) {
389389
data[1] = j
390390
data[2] = i
391391
}
392-
func foo() {
392+
func foo()->Int {
393393
var data = new [Int] {2,0,0}
394394
bar(data)
395395
return data[0]+data[1]+data[2];
@@ -411,7 +411,7 @@ func bar(data: [Int]) {
411411
else j = data[0]
412412
data[0] = j * 21 + data[1]
413413
}
414-
func foo() {
414+
func foo()->Int {
415415
var data = new [Int] {2,3}
416416
bar(data)
417417
return data[0]+data[1];
@@ -433,7 +433,7 @@ func bar(data: [Int]) {
433433
j = j * 21 + 25 / j
434434
data[1] = j
435435
}
436-
func foo() {
436+
func foo()->Int {
437437
var data = new [Int] {5,3}
438438
bar(data)
439439
return data[0]+data[1];

parser/src/main/java/com/compilerprogramming/ezlang/parser/Parser.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,12 @@ private AST.Expr parsePrimary(Lexer lexer) {
358358
return x;
359359
}
360360
case IDENT -> {
361-
if (isToken(currentToken, "new")) {
361+
if (isToken(currentToken, "null")) {
362+
var x = new AST.LiteralExpr(currentToken);
363+
nextToken(lexer);
364+
return x;
365+
}
366+
else if (isToken(currentToken, "new")) {
362367
return parseNew(lexer);
363368
}
364369
else {

registervm/src/main/java/com/compilerprogramming/ezlang/compiler/CompiledFunction.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public class CompiledFunction {
1313

1414
public BasicBlock entry;
1515
public BasicBlock exit;
16-
private int bid = 0;
17-
private BasicBlock currentBlock;
16+
private int BID = 0;
17+
public BasicBlock currentBlock;
1818
private BasicBlock currentBreakTarget;
1919
private BasicBlock currentContinueTarget;
2020
public int maxLocalReg;
@@ -33,7 +33,7 @@ public class CompiledFunction {
3333
public CompiledFunction(Symbol.FunctionTypeSymbol functionSymbol) {
3434
AST.FuncDecl funcDecl = (AST.FuncDecl) functionSymbol.functionDecl;
3535
setVirtualRegisters(funcDecl.scope);
36-
this.bid = 0;
36+
this.BID = 0;
3737
this.entry = this.currentBlock = createBlock();
3838
this.exit = createBlock();
3939
this.currentBreakTarget = null;
@@ -71,11 +71,11 @@ private void setVirtualRegisters(Scope scope) {
7171
}
7272

7373
private BasicBlock createBlock() {
74-
return new BasicBlock(bid++);
74+
return new BasicBlock(BID++);
7575
}
7676

7777
private BasicBlock createLoopHead() {
78-
return new BasicBlock(bid++, true);
78+
return new BasicBlock(BID++, true);
7979
}
8080

8181
private void compileBlock(AST.BlockStmt block) {
@@ -90,7 +90,7 @@ private void compileReturn(AST.ReturnStmt returnStmt) {
9090
if (isIndexed)
9191
codeIndexedLoad();
9292
if (virtualStack.size() == 1)
93-
code(new Instruction.Return(pop()));
93+
code(new Instruction.Ret(pop()));
9494
else if (virtualStack.size() > 1)
9595
throw new CompilerException("Virtual stack has more than one item at return");
9696
}
@@ -109,6 +109,7 @@ private void compileStatement(AST.Stmt statement) {
109109
case AST.VarStmt letStmt -> {
110110
compileLet(letStmt);
111111
}
112+
case AST.VarDeclStmt varDeclStmt -> {}
112113
case AST.IfElseStmt ifElseStmt -> {
113114
compileIf(ifElseStmt);
114115
}

0 commit comments

Comments
 (0)