Skip to content

Commit aa87faf

Browse files
add line numbers to ast
1 parent 500ba7a commit aa87faf

File tree

13 files changed

+146
-137
lines changed

13 files changed

+146
-137
lines changed

common/src/main/java/com/compilerprogramming/ezlang/exceptions/CompilerException.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,17 @@
1717
package com.compilerprogramming.ezlang.exceptions;
1818

1919
public class CompilerException extends RuntimeException {
20-
public CompilerException(String message) {
20+
int lineNumber;
21+
22+
public CompilerException(String message, int lineNumber) {
2123
super(message);
24+
this.lineNumber = lineNumber;
25+
}
26+
public CompilerException(String message) {
27+
this(message,-1);
2228
}
23-
2429
public CompilerException(String message, Throwable cause) {
2530
super(message, cause);
31+
lineNumber = -1;
2632
}
2733
}

lexer/src/main/java/com/compilerprogramming/ezlang/lexer/Lexer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public Token scan() {
178178
private Token scanOthers() {
179179
if (Character.isDigit(input[position])) return parseNumber();
180180
else if (isIdentifierStart(input[position])) return parseIdentifier();
181-
throw new CompilerException("Unexpected character " + input[position] + " at line " + lineNumber);
181+
throw new CompilerException("Unexpected character " + input[position] + " at line " + lineNumber, lineNumber);
182182
}
183183

184184
public int lineNumber() {return lineNumber;}

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ private void compileReturn(AST.ReturnStmt returnStmt) {
134134
if (virtualStack.size() == 1)
135135
codeReturn(pop());
136136
else if (virtualStack.size() > 1)
137-
throw new CompilerException("Virtual stack has more than one item at return");
137+
throw new CompilerException("Virtual stack has more than one item at return", returnStmt.lineNumber);
138138
}
139139
jumpTo(exit);
140140
}
@@ -196,14 +196,14 @@ private void compileExprStmt(AST.ExprStmt exprStmt) {
196196

197197
private void compileContinue(AST.ContinueStmt continueStmt) {
198198
if (currentContinueTarget == null)
199-
throw new CompilerException("No continue target found");
199+
throw new CompilerException("No continue target found", continueStmt.lineNumber);
200200
assert !issa.isSealed(currentContinueTarget);
201201
jumpTo(currentContinueTarget);
202202
}
203203

204204
private void compileBreak(AST.BreakStmt breakStmt) {
205205
if (currentBreakTarget == null)
206-
throw new CompilerException("No break target found");
206+
throw new CompilerException("No break target found", breakStmt.lineNumber);
207207
assert !issa.isSealed(currentBreakTarget);
208208
jumpTo(currentBreakTarget);
209209
}
@@ -332,7 +332,7 @@ private boolean compileCallExpr(AST.CallExpr callExpr) {
332332
EZType.EZTypeFunction calleeType = null;
333333
if (callee instanceof Operand.LocalFunctionOperand functionOperand)
334334
calleeType = functionOperand.functionType;
335-
else throw new CompilerException("Cannot call a non function type");
335+
else throw new CompilerException("Cannot call a non function type", callExpr.lineNumber);
336336
var returnStackPos = virtualStack.size();
337337
List<Operand.RegisterOperand> args = new ArrayList<>();
338338
for (AST.Expr expr: callExpr.args) {
@@ -375,7 +375,7 @@ private boolean compileFieldExpr(AST.GetFieldExpr fieldExpr) {
375375
EZType.EZTypeStruct typeStruct = getStructType(fieldExpr.object.type);
376376
int fieldIndex = typeStruct.getFieldIndex(fieldExpr.fieldName);
377377
if (fieldIndex < 0)
378-
throw new CompilerException("Field " + fieldExpr.fieldName + " not found");
378+
throw new CompilerException("Field " + fieldExpr.fieldName + " not found", fieldExpr.lineNumber);
379379
boolean indexed = compileExpr(fieldExpr.object);
380380
if (indexed)
381381
codeIndexedLoad();
@@ -398,7 +398,7 @@ private boolean compileSetFieldExpr(AST.SetFieldExpr setFieldExpr) {
398398
EZType.EZTypeStruct structType = (EZType.EZTypeStruct) setFieldExpr.object.type;
399399
int fieldIndex = structType.getFieldIndex(setFieldExpr.fieldName);
400400
if (fieldIndex == -1)
401-
throw new CompilerException("Field " + setFieldExpr.fieldName + " not found in struct " + structType.name);
401+
throw new CompilerException("Field " + setFieldExpr.fieldName + " not found in struct " + structType.name, setFieldExpr.lineNumber);
402402
if (setFieldExpr instanceof AST.InitFieldExpr)
403403
pushOperand(top());
404404
else
@@ -500,7 +500,7 @@ private boolean compileBinaryExpr(AST.BinaryExpr binaryExpr) {
500500
switch (opCode) {
501501
case "==": value = 1; break;
502502
case "!=": value = 0; break;
503-
default: throw new CompilerException("Invalid binary op");
503+
default: throw new CompilerException("Invalid binary op", binaryExpr.lineNumber);
504504
}
505505
pushConstant(value, typeDictionary.INT);
506506
}
@@ -519,7 +519,7 @@ else if (left instanceof Operand.ConstantOperand leftconstant &&
519519
case ">": value = leftconstant.value > rightconstant.value ? 1 : 0; break;
520520
case "<=": value = leftconstant.value <= rightconstant.value ? 1 : 0; break;
521521
case ">=": value = leftconstant.value <= rightconstant.value ? 1 : 0; break;
522-
default: throw new CompilerException("Invalid binary op");
522+
default: throw new CompilerException("Invalid binary op", binaryExpr.lineNumber);
523523
}
524524
pushConstant(value, leftconstant.type);
525525
}
@@ -542,7 +542,7 @@ private boolean compileUnaryExpr(AST.UnaryExpr unaryExpr) {
542542
case "-": pushConstant(-constant.value, constant.type); break;
543543
// Maybe below we should explicitly set Int
544544
case "!": pushConstant(constant.value == 0?1:0, constant.type); break;
545-
default: throw new CompilerException("Invalid unary op");
545+
default: throw new CompilerException("Invalid unary op", unaryExpr.lineNumber);
546546
}
547547
}
548548
else {
@@ -557,7 +557,7 @@ private boolean compileConstantExpr(AST.LiteralExpr constantExpr) {
557557
pushConstant(constantExpr.value.num.intValue(), constantExpr.type);
558558
else if (constantExpr.type instanceof EZType.EZTypeNull)
559559
pushNullConstant(constantExpr.type);
560-
else throw new CompilerException("Invalid constant type");
560+
else throw new CompilerException("Invalid constant type", constantExpr.lineNumber);
561561
return false;
562562
}
563563

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ private void nextToken(Lexer lexer) {
2121
}
2222

2323
private void error(Token t, String errorMessage) {
24-
throw new CompilerException("Line " + t.lineNumber + ": " + errorMessage + " got " + t.str);
24+
throw new CompilerException("Line " + t.lineNumber + ": " + errorMessage + " got " + t.str, t.lineNumber);
2525
}
2626

2727
private void matchPunctuation(Lexer lexer, String value) {
@@ -265,7 +265,7 @@ else if (lhs instanceof AST.GetFieldExpr getFieldExpr) {
265265
else if (lhs instanceof AST.NameExpr nameExpr) {
266266
return new AST.AssignStmt(nameExpr, rhs, lineNumber);
267267
}
268-
else throw new CompilerException("Expected a name, expr[] or expr.field");
268+
else throw new CompilerException("Expected a name, expr[] or expr.field", lineNumber);
269269
}
270270
}
271271

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private void compileReturn(AST.ReturnStmt returnStmt) {
9595
if (virtualStack.size() == 1)
9696
code(new Instruction.Ret(pop()));
9797
else if (virtualStack.size() > 1)
98-
throw new CompilerException("Virtual stack has more than one item at return");
98+
throw new CompilerException("Virtual stack has more than one item at return", returnStmt.lineNumber);
9999
}
100100
jumpTo(exit);
101101
}
@@ -156,13 +156,13 @@ private void compileExprStmt(AST.ExprStmt exprStmt) {
156156

157157
private void compileContinue(AST.ContinueStmt continueStmt) {
158158
if (currentContinueTarget == null)
159-
throw new CompilerException("No continue target found");
159+
throw new CompilerException("No continue target found", continueStmt.lineNumber);
160160
jumpTo(currentContinueTarget);
161161
}
162162

163163
private void compileBreak(AST.BreakStmt breakStmt) {
164164
if (currentBreakTarget == null)
165-
throw new CompilerException("No break target found");
165+
throw new CompilerException("No break target found", breakStmt.lineNumber);
166166
jumpTo(currentBreakTarget);
167167
}
168168

@@ -284,7 +284,7 @@ private boolean compileCallExpr(AST.CallExpr callExpr) {
284284
EZType.EZTypeFunction calleeType = null;
285285
if (callee instanceof Operand.LocalFunctionOperand functionOperand)
286286
calleeType = functionOperand.functionType;
287-
else throw new CompilerException("Cannot call a non function type");
287+
else throw new CompilerException("Cannot call a non function type", callExpr.lineNumber);
288288
var returnStackPos = virtualStack.size();
289289
List<Operand.RegisterOperand> args = new ArrayList<>();
290290
for (AST.Expr expr: callExpr.args) {
@@ -312,7 +312,7 @@ private boolean compileCallExpr(AST.CallExpr callExpr) {
312312
return false;
313313
}
314314

315-
private EZType.EZTypeStruct getStructType(EZType t) {
315+
private EZType.EZTypeStruct getStructType(EZType t, int lineNumber) {
316316
if (t instanceof EZType.EZTypeStruct typeStruct) {
317317
return typeStruct;
318318
}
@@ -321,14 +321,14 @@ else if (t instanceof EZType.EZTypeNullable ptr &&
321321
return typeStruct;
322322
}
323323
else
324-
throw new CompilerException("Unexpected type: " + t);
324+
throw new CompilerException("Unexpected type: " + t, lineNumber);
325325
}
326326

327327
private boolean compileFieldExpr(AST.GetFieldExpr fieldExpr) {
328-
EZType.EZTypeStruct typeStruct = getStructType(fieldExpr.object.type);
328+
EZType.EZTypeStruct typeStruct = getStructType(fieldExpr.object.type, fieldExpr.lineNumber);
329329
int fieldIndex = typeStruct.getFieldIndex(fieldExpr.fieldName);
330330
if (fieldIndex < 0)
331-
throw new CompilerException("Field " + fieldExpr.fieldName + " not found");
331+
throw new CompilerException("Field " + fieldExpr.fieldName + " not found", fieldExpr.lineNumber);
332332
boolean indexed = compileExpr(fieldExpr.object);
333333
if (indexed)
334334
codeIndexedLoad();
@@ -351,7 +351,7 @@ private boolean compileSetFieldExpr(AST.SetFieldExpr setFieldExpr) {
351351
EZType.EZTypeStruct structType = (EZType.EZTypeStruct) setFieldExpr.object.type;
352352
int fieldIndex = structType.getFieldIndex(setFieldExpr.fieldName);
353353
if (fieldIndex == -1)
354-
throw new CompilerException("Field " + setFieldExpr.fieldName + " not found in struct " + structType.name);
354+
throw new CompilerException("Field " + setFieldExpr.fieldName + " not found in struct " + structType.name, setFieldExpr.lineNumber);
355355
if (setFieldExpr instanceof AST.InitFieldExpr)
356356
pushOperand(top());
357357
else
@@ -453,7 +453,7 @@ private boolean compileBinaryExpr(AST.BinaryExpr binaryExpr) {
453453
switch (opCode) {
454454
case "==": value = 1; break;
455455
case "!=": value = 0; break;
456-
default: throw new CompilerException("Invalid binary op");
456+
default: throw new CompilerException("Invalid binary op", binaryExpr.lineNumber);
457457
}
458458
pushConstant(value, typeDictionary.INT);
459459
}
@@ -472,7 +472,7 @@ else if (left instanceof Operand.ConstantOperand leftconstant &&
472472
case ">": value = leftconstant.value > rightconstant.value ? 1 : 0; break;
473473
case "<=": value = leftconstant.value <= rightconstant.value ? 1 : 0; break;
474474
case ">=": value = leftconstant.value <= rightconstant.value ? 1 : 0; break;
475-
default: throw new CompilerException("Invalid binary op");
475+
default: throw new CompilerException("Invalid binary op", binaryExpr.lineNumber);
476476
}
477477
pushConstant(value, leftconstant.type);
478478
}
@@ -495,7 +495,7 @@ private boolean compileUnaryExpr(AST.UnaryExpr unaryExpr) {
495495
case "-": pushConstant(-constant.value, constant.type); break;
496496
// Maybe below we should explicitly set Int
497497
case "!": pushConstant(constant.value == 0?1:0, constant.type); break;
498-
default: throw new CompilerException("Invalid unary op");
498+
default: throw new CompilerException("Invalid unary op", unaryExpr.lineNumber);
499499
}
500500
}
501501
else {
@@ -510,7 +510,7 @@ private boolean compileConstantExpr(AST.LiteralExpr constantExpr) {
510510
pushConstant(constantExpr.value.num.intValue(), constantExpr.type);
511511
else if (constantExpr.type instanceof EZType.EZTypeNull)
512512
pushNullConstant(constantExpr.type);
513-
else throw new CompilerException("Invalid constant type");
513+
else throw new CompilerException("Invalid constant type", constantExpr.lineNumber);
514514
return false;
515515
}
516516

@@ -611,7 +611,7 @@ else if (type instanceof EZType.EZTypeStruct typeStruct) {
611611
code(new Instruction.NewStruct(typeStruct, temp));
612612
}
613613
else
614-
throw new CompilerException("Unexpected type: " + type);
614+
throw new CompilerException("Unexpected type: " + type, len.lineNumber);
615615
}
616616

617617
private void codeNewArray(EZType.EZTypeArray typeArray, AST.Expr len, AST.Expr initVal) {

registervm/src/main/java/com/compilerprogramming/ezlang/interpreter/Interpreter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ else if (arg instanceof Operand.NullConstantOperand) {
135135
case "-": execStack.stack[base + unaryInst.result().frameSlot()] = new Value.IntegerValue(-integerValue.value); break;
136136
// Maybe below we should explicitly set Int
137137
case "!": execStack.stack[base + unaryInst.result().frameSlot()] = new Value.IntegerValue(integerValue.value==0?1:0); break;
138-
default: throw new CompilerException("Invalid unary op");
138+
default: throw new InterpreterException("Invalid unary op");
139139
}
140140
}
141141
else

0 commit comments

Comments
 (0)