Skip to content

Commit 500ba7a

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

File tree

2 files changed

+131
-72
lines changed

2 files changed

+131
-72
lines changed

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

Lines changed: 86 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
*/
1414
public abstract class AST {
1515

16-
protected AST() {
16+
public int lineNumber;
17+
protected AST(int lineNumber) {
18+
this.lineNumber = lineNumber;
1719
}
1820
public abstract void accept(ASTVisitor visitor);
1921

@@ -25,7 +27,11 @@ public String toString() {
2527

2628
public static class Program extends AST {
2729
public final List<Decl> decls = new ArrayList<>();
28-
public Scope scope;
30+
public Scope scope = null;
31+
32+
public Program(int lineNumber) {
33+
super(lineNumber);
34+
}
2935

3036
@Override
3137
public StringBuilder toStr(StringBuilder sb) {
@@ -47,6 +53,9 @@ public void accept(ASTVisitor visitor) {
4753
}
4854

4955
public static abstract class Decl extends AST {
56+
protected Decl(int lineNumber) {
57+
super(lineNumber);
58+
}
5059
}
5160

5261
public enum VarType
@@ -61,7 +70,8 @@ public static class VarDecl extends Decl {
6170
public final VarType varType;
6271
public final TypeExpr typeExpr;
6372
public Symbol symbol;
64-
public VarDecl(final String name, VarType varType, final TypeExpr typeExpr) {
73+
public VarDecl(final String name, VarType varType, final TypeExpr typeExpr, int lineNumber) {
74+
super(lineNumber);
6575
this.name = name;
6676
this.varType = varType;
6777
this.typeExpr = typeExpr;
@@ -87,7 +97,8 @@ public static class StructDecl extends Decl {
8797
public final VarDecl[] fields;
8898
public Scope scope;
8999
public Symbol symbol;
90-
public StructDecl(final String name, final VarDecl[] fields) {
100+
public StructDecl(final String name, final VarDecl[] fields, int lineNumber) {
101+
super(lineNumber);
91102
this.name = name;
92103
this.fields = fields;
93104
}
@@ -123,10 +134,11 @@ public static class FuncDecl extends Decl {
123134
public Scope scope;
124135
public Symbol symbol;
125136

126-
public FuncDecl(final String name, final VarDecl[] args, final TypeExpr returnType, BlockStmt block) {
137+
public FuncDecl(final String name, final VarDecl[] args, final TypeExpr returnType, BlockStmt block, int lineNumber) {
138+
super(lineNumber);
127139
this.name = name;
128140
this.args = args;
129-
this.returnType = new ReturnTypeExpr(returnType);
141+
this.returnType = new ReturnTypeExpr(returnType, lineNumber);
130142
this.block = block;
131143
}
132144

@@ -162,16 +174,25 @@ public void accept(ASTVisitor visitor) {
162174

163175
public abstract static class Expr extends AST {
164176
public EZType type;
177+
178+
protected Expr(int lineNumber) {
179+
super(lineNumber);
180+
}
165181
}
166182

167183
public abstract static class TypeExpr extends Expr {
184+
protected TypeExpr(int lineNumber) {
185+
super(lineNumber);
186+
}
187+
168188
public abstract String name();
169189
}
170190

171191
public static class SimpleTypeExpr extends TypeExpr {
172192
protected final String name;
173193

174-
public SimpleTypeExpr(String name) {
194+
public SimpleTypeExpr(String name, int lineNumber) {
195+
super(lineNumber);
175196
this.name = name;
176197
}
177198
@Override
@@ -191,8 +212,8 @@ public void accept(ASTVisitor visitor) {
191212

192213
public static class NullableSimpleTypeExpr extends SimpleTypeExpr {
193214

194-
public NullableSimpleTypeExpr(String name) {
195-
super(name);
215+
public NullableSimpleTypeExpr(String name, int lineNumber) {
216+
super(name, lineNumber);
196217
}
197218
@Override
198219
public StringBuilder toStr(StringBuilder sb) {
@@ -213,7 +234,8 @@ public void accept(ASTVisitor visitor) {
213234
public static class ArrayTypeExpr extends TypeExpr {
214235
public final SimpleTypeExpr elementType;
215236

216-
public ArrayTypeExpr(SimpleTypeExpr elementType) {
237+
public ArrayTypeExpr(SimpleTypeExpr elementType, int lineNumber) {
238+
super(lineNumber);
217239
this.elementType = elementType;
218240
}
219241
@Override
@@ -236,8 +258,8 @@ public void accept(ASTVisitor visitor) {
236258

237259
public static class NullableArrayTypeExpr extends ArrayTypeExpr {
238260

239-
public NullableArrayTypeExpr(SimpleTypeExpr elementType) {
240-
super(elementType);
261+
public NullableArrayTypeExpr(SimpleTypeExpr elementType, int lineNumber) {
262+
super(elementType, lineNumber);
241263
}
242264
@Override
243265
public StringBuilder toStr(StringBuilder sb) {
@@ -260,7 +282,8 @@ public void accept(ASTVisitor visitor) {
260282
public static class ReturnTypeExpr extends Expr {
261283
public final TypeExpr returnType;
262284

263-
public ReturnTypeExpr(TypeExpr returnType) {
285+
public ReturnTypeExpr(TypeExpr returnType, int lineNumber) {
286+
super(lineNumber);
264287
this.returnType = returnType;
265288
}
266289

@@ -283,7 +306,8 @@ public StringBuilder toStr(StringBuilder sb) {
283306
public static class NameExpr extends Expr {
284307
public String name;
285308
public Symbol symbol;
286-
public NameExpr(String name) {
309+
public NameExpr(String name, int lineNumber) {
310+
super(lineNumber);
287311
this.name = name;
288312
}
289313
@Override
@@ -304,7 +328,8 @@ public static class BinaryExpr extends Expr {
304328
public final Token op;
305329
public final Expr expr1;
306330
public final Expr expr2;
307-
public BinaryExpr(Token op, Expr expr1, Expr expr2) {
331+
public BinaryExpr(Token op, Expr expr1, Expr expr2, int lineNumber) {
332+
super(lineNumber);
308333
this.op = op;
309334
this.expr1 = expr1;
310335
this.expr2 = expr2;
@@ -332,7 +357,8 @@ public void accept(ASTVisitor visitor) {
332357
public static class UnaryExpr extends Expr {
333358
public final Token op;
334359
public final Expr expr;
335-
public UnaryExpr(Token op, Expr expr) {
360+
public UnaryExpr(Token op, Expr expr, int lineNumber) {
361+
super(lineNumber);
336362
this.op = op;
337363
this.expr = expr;
338364
}
@@ -356,6 +382,7 @@ public void accept(ASTVisitor visitor) {
356382
public static class LiteralExpr extends Expr {
357383
public final Token value;
358384
public LiteralExpr(Token value) {
385+
super(value.lineNumber);
359386
this.value = value;
360387
}
361388
@Override
@@ -376,7 +403,8 @@ public void accept(ASTVisitor visitor) {
376403
public static class ArrayLoadExpr extends Expr {
377404
public final Expr array;
378405
public final Expr expr;
379-
public ArrayLoadExpr(Expr array, Expr expr) {
406+
public ArrayLoadExpr(Expr array, Expr expr, int lineNumber) {
407+
super(lineNumber);
380408
this.array = array;
381409
this.expr = expr;
382410
}
@@ -404,7 +432,8 @@ public static class ArrayStoreExpr extends Expr {
404432
public final Expr array;
405433
public final Expr expr;
406434
public final Expr value;
407-
public ArrayStoreExpr(Expr array, Expr expr, Expr value) {
435+
public ArrayStoreExpr(Expr array, Expr expr, Expr value, int lineNumber) {
436+
super(lineNumber);
408437
this.array = array;
409438
this.expr = expr;
410439
this.value = value;
@@ -435,8 +464,8 @@ public void accept(ASTVisitor visitor) {
435464
* Specializes how we display
436465
*/
437466
public static class ArrayInitExpr extends ArrayStoreExpr {
438-
public ArrayInitExpr(Expr array, Expr expr, Expr value) {
439-
super(array, expr, value);
467+
public ArrayInitExpr(Expr array, Expr expr, Expr value, int lineNumber) {
468+
super(array, expr, value, lineNumber);
440469
}
441470
@Override
442471
public StringBuilder toStr(StringBuilder sb) {
@@ -448,7 +477,8 @@ public StringBuilder toStr(StringBuilder sb) {
448477
public static class GetFieldExpr extends Expr {
449478
public final Expr object;
450479
public final String fieldName;
451-
public GetFieldExpr(Expr object, String fieldName) {
480+
public GetFieldExpr(Expr object, String fieldName, int lineNumber) {
481+
super(lineNumber);
452482
this.object = object;
453483
this.fieldName = fieldName;
454484
}
@@ -472,7 +502,8 @@ public static class SetFieldExpr extends Expr {
472502
public final Expr object;
473503
public final String fieldName;
474504
public final Expr value;
475-
public SetFieldExpr(Expr object, String fieldName, Expr value) {
505+
public SetFieldExpr(Expr object, String fieldName, Expr value, int lineNumber) {
506+
super(lineNumber);
476507
this.object = object;
477508
this.fieldName = fieldName;
478509
this.value = value;
@@ -500,8 +531,8 @@ public void accept(ASTVisitor visitor) {
500531
* Specializes how we display
501532
*/
502533
public static class InitFieldExpr extends SetFieldExpr {
503-
public InitFieldExpr(Expr object, String fieldName, Expr value) {
504-
super(object, fieldName, value);
534+
public InitFieldExpr(Expr object, String fieldName, Expr value, int lineNumber) {
535+
super(object, fieldName, value, lineNumber);
505536
}
506537
@Override
507538
public StringBuilder toStr(StringBuilder sb) {
@@ -514,7 +545,8 @@ public StringBuilder toStr(StringBuilder sb) {
514545
public static class CallExpr extends Expr {
515546
public final Expr callee;
516547
public final List<Expr> args;
517-
public CallExpr(Expr callee, List<Expr> args) {
548+
public CallExpr(Expr callee, List<Expr> args, int lineNumber) {
549+
super(lineNumber);
518550
this.callee = callee;
519551
this.args = new ArrayList<>(args);
520552
}
@@ -553,12 +585,14 @@ public static class NewExpr extends Expr {
553585
public final TypeExpr typeExpr;
554586
public final Expr len;
555587
public final Expr initValue;
556-
public NewExpr(TypeExpr typeExpr) {
588+
public NewExpr(TypeExpr typeExpr, int lineNumber) {
589+
super(lineNumber);
557590
this.typeExpr = typeExpr;
558591
this.len = null;
559592
this.initValue = null;
560593
}
561-
public NewExpr(TypeExpr typeExpr, Expr len, Expr initValue) {
594+
public NewExpr(TypeExpr typeExpr, Expr len, Expr initValue, int lineNumber) {
595+
super(lineNumber);
562596
this.typeExpr = typeExpr;
563597
this.len = len;
564598
this.initValue = initValue;
@@ -591,7 +625,8 @@ public void accept(ASTVisitor visitor) {
591625
public static class InitExpr extends Expr {
592626
public final NewExpr newExpr;
593627
public final List<Expr> initExprList;
594-
public InitExpr(NewExpr newExpr, List<Expr> initExprList) {
628+
public InitExpr(NewExpr newExpr, List<Expr> initExprList, int lineNumber) {
629+
super(lineNumber);
595630
this.initExprList = initExprList;
596631
this.newExpr = newExpr;
597632
}
@@ -624,13 +659,17 @@ public void accept(ASTVisitor visitor) {
624659
}
625660

626661
public abstract static class Stmt extends AST {
662+
protected Stmt(int lineNumber) {
663+
super(lineNumber);
664+
}
627665
}
628666

629667
public static class IfElseStmt extends Stmt {
630668
public final Expr condition;
631669
public final Stmt ifStmt;
632670
public final Stmt elseStmt;
633-
public IfElseStmt(Expr expr, Stmt ifStmt, Stmt elseStmt) {
671+
public IfElseStmt(Expr expr, Stmt ifStmt, Stmt elseStmt, int lineNumber) {
672+
super(lineNumber);
634673
this.condition = expr;
635674
this.ifStmt = ifStmt;
636675
this.elseStmt = elseStmt;
@@ -664,7 +703,8 @@ public void accept(ASTVisitor visitor) {
664703
public static class WhileStmt extends Stmt {
665704
public final Expr condition;
666705
public Stmt stmt;
667-
public WhileStmt(Expr expr) {
706+
public WhileStmt(Expr expr, int lineNumber) {
707+
super(lineNumber);
668708
this.condition = expr;
669709
}
670710
@Override
@@ -689,7 +729,8 @@ public void accept(ASTVisitor visitor) {
689729

690730
public static class BreakStmt extends Stmt {
691731
public final WhileStmt whileStmt;
692-
public BreakStmt(WhileStmt whileStmt) {
732+
public BreakStmt(WhileStmt whileStmt, int lineNumber) {
733+
super(lineNumber);
693734
this.whileStmt = whileStmt;
694735
}
695736
@Override
@@ -708,7 +749,8 @@ public void accept(ASTVisitor visitor) {
708749

709750
public static class ContinueStmt extends Stmt {
710751
public final WhileStmt whileStmt;
711-
public ContinueStmt(WhileStmt whileStmt) {
752+
public ContinueStmt(WhileStmt whileStmt, int lineNumber) {
753+
super(lineNumber);
712754
this.whileStmt = whileStmt;
713755
}
714756
@Override
@@ -727,7 +769,8 @@ public void accept(ASTVisitor visitor) {
727769

728770
public static class ReturnStmt extends Stmt {
729771
public final AST.Expr expr;
730-
public ReturnStmt(AST.Expr expr) {
772+
public ReturnStmt(AST.Expr expr, int lineNumber) {
773+
super(lineNumber);
731774
this.expr = expr;
732775
}
733776
@Override
@@ -755,7 +798,8 @@ public void accept(ASTVisitor visitor) {
755798
public static class AssignStmt extends Stmt {
756799
public final NameExpr nameExpr;
757800
public final Expr rhs;
758-
public AssignStmt(NameExpr nameExpr, Expr rhs) {
801+
public AssignStmt(NameExpr nameExpr, Expr rhs, int lineNumber) {
802+
super(lineNumber);
759803
this.nameExpr = nameExpr;
760804
this.rhs = rhs;
761805
}
@@ -783,7 +827,8 @@ public static class VarStmt extends Stmt {
783827
public Symbol.VarSymbol symbol;
784828
public final AST.Expr expr;
785829

786-
public VarStmt(String symbol, Expr expr) {
830+
public VarStmt(String symbol, Expr expr, int lineNumber) {
831+
super(lineNumber);
787832
this.varName = symbol;
788833
this.expr = expr;
789834
}
@@ -805,7 +850,8 @@ public void accept(ASTVisitor visitor) {
805850

806851
public static class ExprStmt extends Stmt {
807852
public final Expr expr;
808-
public ExprStmt(Expr expr) {
853+
public ExprStmt(Expr expr, int lineNumber) {
854+
super(lineNumber);
809855
this.expr = expr;
810856
}
811857
@Override
@@ -825,7 +871,8 @@ public void accept(ASTVisitor visitor) {
825871

826872
public static class VarDeclStmt extends Stmt {
827873
public final VarDecl varDecl;
828-
public VarDeclStmt(VarDecl varDec) {
874+
public VarDeclStmt(VarDecl varDec, int lineNumber) {
875+
super(lineNumber);
829876
this.varDecl = varDec;
830877
}
831878
@Override
@@ -847,7 +894,8 @@ public void accept(ASTVisitor visitor) {
847894
public static class BlockStmt extends Stmt {
848895
public final List<Stmt> stmtList = new ArrayList<>();
849896
public Scope scope;
850-
public BlockStmt() {
897+
public BlockStmt(int lineNumber) {
898+
super(lineNumber);
851899
}
852900
@Override
853901
public StringBuilder toStr(StringBuilder sb) {

0 commit comments

Comments
 (0)