From f816e5c391d3f56742bf503b96233f83d3efe91b Mon Sep 17 00:00:00 2001 From: laurenz Date: Wed, 8 May 2024 11:44:15 +0200 Subject: [PATCH 1/6] left part of an assignement can be a FieldId --- src/main/antlr/Decaf.g4 | 2 +- .../java/de/maishai/StatementGenerator.java | 20 ++++++++++++------- src/main/java/de/maishai/antlr/Decaf.interp | 2 +- .../java/de/maishai/antlr/DecafParser.java | 8 ++++---- .../de/maishai/ast/records/Assignment.java | 2 +- 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/main/antlr/Decaf.g4 b/src/main/antlr/Decaf.g4 index 0171f6e..c5d392c 100644 --- a/src/main/antlr/Decaf.g4 +++ b/src/main/antlr/Decaf.g4 @@ -46,7 +46,7 @@ unaryOp : SUB | NOT; fieldId : (THIS '.')? (recipient '.')* id; -assign : id assignSign expr ; +assign : fieldId assignSign expr ; methCall : (THIS '.')? (recipient '.')* methName; recipient : methName | id; methName : id '(' args? ')'; diff --git a/src/main/java/de/maishai/StatementGenerator.java b/src/main/java/de/maishai/StatementGenerator.java index 62b7800..00113e5 100644 --- a/src/main/java/de/maishai/StatementGenerator.java +++ b/src/main/java/de/maishai/StatementGenerator.java @@ -67,9 +67,15 @@ public class StatementGenerator extends DecafBaseVisitor { } private Assignment generateAssign(DecafParser.AssignContext ctx) { - Id id = new Id(ctx.id().getText()); - Expression expr = resolveFancyAssign(ctx.assignSign(), id, new ExpressionGenerator().visit(ctx.expr())); - return new Assignment(id, expr); + FieldId fieldId = generateField(ctx.fieldId()); + Expression expr = resolveFancyAssign(ctx.assignSign(), fieldId, new ExpressionGenerator().visit(ctx.expr())); + return new Assignment(fieldId, expr); + } + + private FieldId generateField(DecafParser.FieldIdContext fieldIdContext) { + return new FieldId(fieldIdContext.THIS() != null, + ExpressionGenerator.generateRecipient(fieldIdContext.recipient(), + null), new Id(fieldIdContext.id().getText())); } //StatementExpression @@ -101,15 +107,15 @@ public class StatementGenerator extends DecafBaseVisitor { return new New(type, args); } - public static Expression resolveFancyAssign(DecafParser.AssignSignContext ctx, Id id, Expression expression) { + public static Expression resolveFancyAssign(DecafParser.AssignSignContext ctx, FieldId fieldId, Expression expression) { if (ctx.ASSIGN() != null) return expression; if (ctx.ADD_ASSIGN() != null) - return new Binary(id, Operator.ADD, expression); + return new Binary(fieldId, Operator.ADD, expression); if (ctx.SUB_ASSIGN() != null) - return new Binary(id, Operator.SUB, expression); + return new Binary(fieldId, Operator.SUB, expression); if (ctx.MUL_ASSIGN() != null) - return new Binary(id, Operator.MUL, expression); + return new Binary(fieldId, Operator.MUL, expression); throw new RuntimeException(); } } diff --git a/src/main/java/de/maishai/antlr/Decaf.interp b/src/main/java/de/maishai/antlr/Decaf.interp index fff3e52..d2c4c01 100644 --- a/src/main/java/de/maishai/antlr/Decaf.interp +++ b/src/main/java/de/maishai/antlr/Decaf.interp @@ -127,4 +127,4 @@ id atn: -[4, 1, 47, 280, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 2, 8, 7, 8, 2, 9, 7, 9, 2, 10, 7, 10, 2, 11, 7, 11, 2, 12, 7, 12, 2, 13, 7, 13, 2, 14, 7, 14, 2, 15, 7, 15, 2, 16, 7, 16, 2, 17, 7, 17, 2, 18, 7, 18, 2, 19, 7, 19, 2, 20, 7, 20, 2, 21, 7, 21, 2, 22, 7, 22, 2, 23, 7, 23, 2, 24, 7, 24, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 3, 0, 56, 8, 0, 1, 0, 1, 0, 1, 0, 5, 0, 61, 8, 0, 10, 0, 12, 0, 64, 9, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 3, 1, 3, 1, 4, 1, 4, 3, 4, 80, 8, 4, 1, 5, 1, 5, 1, 5, 1, 5, 3, 5, 86, 8, 5, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 3, 6, 93, 8, 6, 1, 6, 1, 6, 1, 6, 1, 7, 1, 7, 1, 7, 1, 7, 1, 7, 1, 7, 1, 7, 1, 7, 1, 7, 1, 8, 3, 8, 108, 8, 8, 1, 8, 1, 8, 1, 8, 3, 8, 113, 8, 8, 1, 8, 1, 8, 1, 8, 1, 9, 1, 9, 1, 9, 5, 9, 121, 8, 9, 10, 9, 12, 9, 124, 9, 9, 1, 10, 1, 10, 1, 10, 1, 11, 1, 11, 1, 11, 5, 11, 132, 8, 11, 10, 11, 12, 11, 135, 9, 11, 1, 11, 1, 11, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 3, 12, 146, 8, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 3, 12, 185, 8, 12, 1, 13, 1, 13, 1, 13, 1, 13, 1, 13, 3, 13, 192, 8, 13, 1, 13, 1, 13, 3, 13, 196, 8, 13, 1, 14, 1, 14, 1, 14, 1, 14, 1, 14, 1, 14, 1, 14, 1, 14, 1, 14, 1, 14, 1, 14, 1, 14, 3, 14, 210, 8, 14, 1, 14, 1, 14, 1, 14, 1, 14, 5, 14, 216, 8, 14, 10, 14, 12, 14, 219, 9, 14, 1, 15, 1, 15, 1, 16, 1, 16, 1, 17, 1, 17, 3, 17, 227, 8, 17, 1, 17, 1, 17, 1, 17, 5, 17, 232, 8, 17, 10, 17, 12, 17, 235, 9, 17, 1, 17, 1, 17, 1, 18, 1, 18, 1, 18, 1, 18, 1, 19, 1, 19, 3, 19, 245, 8, 19, 1, 19, 1, 19, 1, 19, 5, 19, 250, 8, 19, 10, 19, 12, 19, 253, 9, 19, 1, 19, 1, 19, 1, 20, 1, 20, 3, 20, 259, 8, 20, 1, 21, 1, 21, 1, 21, 3, 21, 264, 8, 21, 1, 21, 1, 21, 1, 22, 1, 22, 1, 22, 5, 22, 271, 8, 22, 10, 22, 12, 22, 274, 9, 22, 1, 23, 1, 23, 1, 24, 1, 24, 1, 24, 0, 1, 28, 25, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 0, 4, 1, 0, 34, 37, 1, 0, 23, 33, 2, 0, 23, 23, 38, 38, 2, 0, 43, 44, 46, 46, 292, 0, 50, 1, 0, 0, 0, 2, 67, 1, 0, 0, 0, 4, 71, 1, 0, 0, 0, 6, 75, 1, 0, 0, 0, 8, 79, 1, 0, 0, 0, 10, 85, 1, 0, 0, 0, 12, 87, 1, 0, 0, 0, 14, 97, 1, 0, 0, 0, 16, 107, 1, 0, 0, 0, 18, 117, 1, 0, 0, 0, 20, 125, 1, 0, 0, 0, 22, 128, 1, 0, 0, 0, 24, 184, 1, 0, 0, 0, 26, 195, 1, 0, 0, 0, 28, 209, 1, 0, 0, 0, 30, 220, 1, 0, 0, 0, 32, 222, 1, 0, 0, 0, 34, 226, 1, 0, 0, 0, 36, 238, 1, 0, 0, 0, 38, 244, 1, 0, 0, 0, 40, 258, 1, 0, 0, 0, 42, 260, 1, 0, 0, 0, 44, 267, 1, 0, 0, 0, 46, 275, 1, 0, 0, 0, 48, 277, 1, 0, 0, 0, 50, 51, 5, 19, 0, 0, 51, 52, 5, 1, 0, 0, 52, 53, 3, 48, 24, 0, 53, 55, 5, 2, 0, 0, 54, 56, 3, 14, 7, 0, 55, 54, 1, 0, 0, 0, 55, 56, 1, 0, 0, 0, 56, 62, 1, 0, 0, 0, 57, 61, 3, 2, 1, 0, 58, 61, 3, 12, 6, 0, 59, 61, 3, 16, 8, 0, 60, 57, 1, 0, 0, 0, 60, 58, 1, 0, 0, 0, 60, 59, 1, 0, 0, 0, 61, 64, 1, 0, 0, 0, 62, 60, 1, 0, 0, 0, 62, 63, 1, 0, 0, 0, 63, 65, 1, 0, 0, 0, 64, 62, 1, 0, 0, 0, 65, 66, 5, 3, 0, 0, 66, 1, 1, 0, 0, 0, 67, 68, 3, 10, 5, 0, 68, 69, 3, 48, 24, 0, 69, 70, 5, 4, 0, 0, 70, 3, 1, 0, 0, 0, 71, 72, 3, 10, 5, 0, 72, 73, 3, 48, 24, 0, 73, 74, 5, 4, 0, 0, 74, 5, 1, 0, 0, 0, 75, 76, 7, 0, 0, 0, 76, 7, 1, 0, 0, 0, 77, 80, 3, 10, 5, 0, 78, 80, 5, 41, 0, 0, 79, 77, 1, 0, 0, 0, 79, 78, 1, 0, 0, 0, 80, 9, 1, 0, 0, 0, 81, 86, 5, 39, 0, 0, 82, 86, 5, 40, 0, 0, 83, 86, 5, 42, 0, 0, 84, 86, 3, 48, 24, 0, 85, 81, 1, 0, 0, 0, 85, 82, 1, 0, 0, 0, 85, 83, 1, 0, 0, 0, 85, 84, 1, 0, 0, 0, 86, 11, 1, 0, 0, 0, 87, 88, 5, 19, 0, 0, 88, 89, 3, 8, 4, 0, 89, 90, 3, 48, 24, 0, 90, 92, 5, 5, 0, 0, 91, 93, 3, 18, 9, 0, 92, 91, 1, 0, 0, 0, 92, 93, 1, 0, 0, 0, 93, 94, 1, 0, 0, 0, 94, 95, 5, 6, 0, 0, 95, 96, 3, 22, 11, 0, 96, 13, 1, 0, 0, 0, 97, 98, 5, 19, 0, 0, 98, 99, 5, 7, 0, 0, 99, 100, 5, 41, 0, 0, 100, 101, 5, 8, 0, 0, 101, 102, 5, 5, 0, 0, 102, 103, 5, 9, 0, 0, 103, 104, 5, 6, 0, 0, 104, 105, 3, 22, 11, 0, 105, 15, 1, 0, 0, 0, 106, 108, 5, 19, 0, 0, 107, 106, 1, 0, 0, 0, 107, 108, 1, 0, 0, 0, 108, 109, 1, 0, 0, 0, 109, 110, 3, 48, 24, 0, 110, 112, 5, 5, 0, 0, 111, 113, 3, 18, 9, 0, 112, 111, 1, 0, 0, 0, 112, 113, 1, 0, 0, 0, 113, 114, 1, 0, 0, 0, 114, 115, 5, 6, 0, 0, 115, 116, 3, 22, 11, 0, 116, 17, 1, 0, 0, 0, 117, 122, 3, 20, 10, 0, 118, 119, 5, 10, 0, 0, 119, 121, 3, 20, 10, 0, 120, 118, 1, 0, 0, 0, 121, 124, 1, 0, 0, 0, 122, 120, 1, 0, 0, 0, 122, 123, 1, 0, 0, 0, 123, 19, 1, 0, 0, 0, 124, 122, 1, 0, 0, 0, 125, 126, 3, 10, 5, 0, 126, 127, 3, 48, 24, 0, 127, 21, 1, 0, 0, 0, 128, 133, 5, 2, 0, 0, 129, 132, 3, 4, 2, 0, 130, 132, 3, 24, 12, 0, 131, 129, 1, 0, 0, 0, 131, 130, 1, 0, 0, 0, 132, 135, 1, 0, 0, 0, 133, 131, 1, 0, 0, 0, 133, 134, 1, 0, 0, 0, 134, 136, 1, 0, 0, 0, 135, 133, 1, 0, 0, 0, 136, 137, 5, 3, 0, 0, 137, 23, 1, 0, 0, 0, 138, 139, 5, 11, 0, 0, 139, 140, 5, 5, 0, 0, 140, 141, 3, 28, 14, 0, 141, 142, 5, 6, 0, 0, 142, 145, 3, 22, 11, 0, 143, 144, 5, 12, 0, 0, 144, 146, 3, 22, 11, 0, 145, 143, 1, 0, 0, 0, 145, 146, 1, 0, 0, 0, 146, 185, 1, 0, 0, 0, 147, 148, 5, 13, 0, 0, 148, 149, 5, 5, 0, 0, 149, 150, 3, 36, 18, 0, 150, 151, 5, 4, 0, 0, 151, 152, 3, 28, 14, 0, 152, 153, 5, 4, 0, 0, 153, 154, 3, 36, 18, 0, 154, 155, 5, 6, 0, 0, 155, 156, 3, 22, 11, 0, 156, 185, 1, 0, 0, 0, 157, 158, 5, 14, 0, 0, 158, 159, 5, 5, 0, 0, 159, 160, 3, 28, 14, 0, 160, 161, 5, 6, 0, 0, 161, 162, 3, 22, 11, 0, 162, 185, 1, 0, 0, 0, 163, 164, 5, 15, 0, 0, 164, 165, 3, 22, 11, 0, 165, 166, 5, 14, 0, 0, 166, 167, 5, 5, 0, 0, 167, 168, 3, 28, 14, 0, 168, 169, 5, 6, 0, 0, 169, 185, 1, 0, 0, 0, 170, 171, 5, 16, 0, 0, 171, 172, 3, 28, 14, 0, 172, 173, 5, 4, 0, 0, 173, 185, 1, 0, 0, 0, 174, 175, 5, 16, 0, 0, 175, 185, 5, 4, 0, 0, 176, 177, 5, 17, 0, 0, 177, 185, 5, 4, 0, 0, 178, 179, 3, 36, 18, 0, 179, 180, 5, 4, 0, 0, 180, 185, 1, 0, 0, 0, 181, 182, 3, 26, 13, 0, 182, 183, 5, 4, 0, 0, 183, 185, 1, 0, 0, 0, 184, 138, 1, 0, 0, 0, 184, 147, 1, 0, 0, 0, 184, 157, 1, 0, 0, 0, 184, 163, 1, 0, 0, 0, 184, 170, 1, 0, 0, 0, 184, 174, 1, 0, 0, 0, 184, 176, 1, 0, 0, 0, 184, 178, 1, 0, 0, 0, 184, 181, 1, 0, 0, 0, 185, 25, 1, 0, 0, 0, 186, 196, 3, 38, 19, 0, 187, 188, 5, 20, 0, 0, 188, 189, 3, 10, 5, 0, 189, 191, 5, 5, 0, 0, 190, 192, 3, 44, 22, 0, 191, 190, 1, 0, 0, 0, 191, 192, 1, 0, 0, 0, 192, 193, 1, 0, 0, 0, 193, 194, 5, 6, 0, 0, 194, 196, 1, 0, 0, 0, 195, 186, 1, 0, 0, 0, 195, 187, 1, 0, 0, 0, 196, 27, 1, 0, 0, 0, 197, 198, 6, 14, -1, 0, 198, 199, 3, 32, 16, 0, 199, 200, 3, 28, 14, 6, 200, 210, 1, 0, 0, 0, 201, 210, 3, 46, 23, 0, 202, 203, 5, 5, 0, 0, 203, 204, 3, 28, 14, 0, 204, 205, 5, 6, 0, 0, 205, 210, 1, 0, 0, 0, 206, 210, 3, 34, 17, 0, 207, 210, 3, 26, 13, 0, 208, 210, 5, 21, 0, 0, 209, 197, 1, 0, 0, 0, 209, 201, 1, 0, 0, 0, 209, 202, 1, 0, 0, 0, 209, 206, 1, 0, 0, 0, 209, 207, 1, 0, 0, 0, 209, 208, 1, 0, 0, 0, 210, 217, 1, 0, 0, 0, 211, 212, 10, 7, 0, 0, 212, 213, 3, 30, 15, 0, 213, 214, 3, 28, 14, 8, 214, 216, 1, 0, 0, 0, 215, 211, 1, 0, 0, 0, 216, 219, 1, 0, 0, 0, 217, 215, 1, 0, 0, 0, 217, 218, 1, 0, 0, 0, 218, 29, 1, 0, 0, 0, 219, 217, 1, 0, 0, 0, 220, 221, 7, 1, 0, 0, 221, 31, 1, 0, 0, 0, 222, 223, 7, 2, 0, 0, 223, 33, 1, 0, 0, 0, 224, 225, 5, 22, 0, 0, 225, 227, 5, 18, 0, 0, 226, 224, 1, 0, 0, 0, 226, 227, 1, 0, 0, 0, 227, 233, 1, 0, 0, 0, 228, 229, 3, 40, 20, 0, 229, 230, 5, 18, 0, 0, 230, 232, 1, 0, 0, 0, 231, 228, 1, 0, 0, 0, 232, 235, 1, 0, 0, 0, 233, 231, 1, 0, 0, 0, 233, 234, 1, 0, 0, 0, 234, 236, 1, 0, 0, 0, 235, 233, 1, 0, 0, 0, 236, 237, 3, 48, 24, 0, 237, 35, 1, 0, 0, 0, 238, 239, 3, 48, 24, 0, 239, 240, 3, 6, 3, 0, 240, 241, 3, 28, 14, 0, 241, 37, 1, 0, 0, 0, 242, 243, 5, 22, 0, 0, 243, 245, 5, 18, 0, 0, 244, 242, 1, 0, 0, 0, 244, 245, 1, 0, 0, 0, 245, 251, 1, 0, 0, 0, 246, 247, 3, 40, 20, 0, 247, 248, 5, 18, 0, 0, 248, 250, 1, 0, 0, 0, 249, 246, 1, 0, 0, 0, 250, 253, 1, 0, 0, 0, 251, 249, 1, 0, 0, 0, 251, 252, 1, 0, 0, 0, 252, 254, 1, 0, 0, 0, 253, 251, 1, 0, 0, 0, 254, 255, 3, 42, 21, 0, 255, 39, 1, 0, 0, 0, 256, 259, 3, 42, 21, 0, 257, 259, 3, 48, 24, 0, 258, 256, 1, 0, 0, 0, 258, 257, 1, 0, 0, 0, 259, 41, 1, 0, 0, 0, 260, 261, 3, 48, 24, 0, 261, 263, 5, 5, 0, 0, 262, 264, 3, 44, 22, 0, 263, 262, 1, 0, 0, 0, 263, 264, 1, 0, 0, 0, 264, 265, 1, 0, 0, 0, 265, 266, 5, 6, 0, 0, 266, 43, 1, 0, 0, 0, 267, 272, 3, 28, 14, 0, 268, 269, 5, 10, 0, 0, 269, 271, 3, 28, 14, 0, 270, 268, 1, 0, 0, 0, 271, 274, 1, 0, 0, 0, 272, 270, 1, 0, 0, 0, 272, 273, 1, 0, 0, 0, 273, 45, 1, 0, 0, 0, 274, 272, 1, 0, 0, 0, 275, 276, 7, 3, 0, 0, 276, 47, 1, 0, 0, 0, 277, 278, 5, 45, 0, 0, 278, 49, 1, 0, 0, 0, 24, 55, 60, 62, 79, 85, 92, 107, 112, 122, 131, 133, 145, 184, 191, 195, 209, 217, 226, 233, 244, 251, 258, 263, 272] \ No newline at end of file +[4, 1, 47, 280, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 2, 8, 7, 8, 2, 9, 7, 9, 2, 10, 7, 10, 2, 11, 7, 11, 2, 12, 7, 12, 2, 13, 7, 13, 2, 14, 7, 14, 2, 15, 7, 15, 2, 16, 7, 16, 2, 17, 7, 17, 2, 18, 7, 18, 2, 19, 7, 19, 2, 20, 7, 20, 2, 21, 7, 21, 2, 22, 7, 22, 2, 23, 7, 23, 2, 24, 7, 24, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 3, 0, 56, 8, 0, 1, 0, 1, 0, 1, 0, 5, 0, 61, 8, 0, 10, 0, 12, 0, 64, 9, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 3, 1, 3, 1, 4, 1, 4, 3, 4, 80, 8, 4, 1, 5, 1, 5, 1, 5, 1, 5, 3, 5, 86, 8, 5, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 3, 6, 93, 8, 6, 1, 6, 1, 6, 1, 6, 1, 7, 1, 7, 1, 7, 1, 7, 1, 7, 1, 7, 1, 7, 1, 7, 1, 7, 1, 8, 3, 8, 108, 8, 8, 1, 8, 1, 8, 1, 8, 3, 8, 113, 8, 8, 1, 8, 1, 8, 1, 8, 1, 9, 1, 9, 1, 9, 5, 9, 121, 8, 9, 10, 9, 12, 9, 124, 9, 9, 1, 10, 1, 10, 1, 10, 1, 11, 1, 11, 1, 11, 5, 11, 132, 8, 11, 10, 11, 12, 11, 135, 9, 11, 1, 11, 1, 11, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 3, 12, 146, 8, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 1, 12, 3, 12, 185, 8, 12, 1, 13, 1, 13, 1, 13, 1, 13, 1, 13, 3, 13, 192, 8, 13, 1, 13, 1, 13, 3, 13, 196, 8, 13, 1, 14, 1, 14, 1, 14, 1, 14, 1, 14, 1, 14, 1, 14, 1, 14, 1, 14, 1, 14, 1, 14, 1, 14, 3, 14, 210, 8, 14, 1, 14, 1, 14, 1, 14, 1, 14, 5, 14, 216, 8, 14, 10, 14, 12, 14, 219, 9, 14, 1, 15, 1, 15, 1, 16, 1, 16, 1, 17, 1, 17, 3, 17, 227, 8, 17, 1, 17, 1, 17, 1, 17, 5, 17, 232, 8, 17, 10, 17, 12, 17, 235, 9, 17, 1, 17, 1, 17, 1, 18, 1, 18, 1, 18, 1, 18, 1, 19, 1, 19, 3, 19, 245, 8, 19, 1, 19, 1, 19, 1, 19, 5, 19, 250, 8, 19, 10, 19, 12, 19, 253, 9, 19, 1, 19, 1, 19, 1, 20, 1, 20, 3, 20, 259, 8, 20, 1, 21, 1, 21, 1, 21, 3, 21, 264, 8, 21, 1, 21, 1, 21, 1, 22, 1, 22, 1, 22, 5, 22, 271, 8, 22, 10, 22, 12, 22, 274, 9, 22, 1, 23, 1, 23, 1, 24, 1, 24, 1, 24, 0, 1, 28, 25, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 0, 4, 1, 0, 34, 37, 1, 0, 23, 33, 2, 0, 23, 23, 38, 38, 2, 0, 43, 44, 46, 46, 292, 0, 50, 1, 0, 0, 0, 2, 67, 1, 0, 0, 0, 4, 71, 1, 0, 0, 0, 6, 75, 1, 0, 0, 0, 8, 79, 1, 0, 0, 0, 10, 85, 1, 0, 0, 0, 12, 87, 1, 0, 0, 0, 14, 97, 1, 0, 0, 0, 16, 107, 1, 0, 0, 0, 18, 117, 1, 0, 0, 0, 20, 125, 1, 0, 0, 0, 22, 128, 1, 0, 0, 0, 24, 184, 1, 0, 0, 0, 26, 195, 1, 0, 0, 0, 28, 209, 1, 0, 0, 0, 30, 220, 1, 0, 0, 0, 32, 222, 1, 0, 0, 0, 34, 226, 1, 0, 0, 0, 36, 238, 1, 0, 0, 0, 38, 244, 1, 0, 0, 0, 40, 258, 1, 0, 0, 0, 42, 260, 1, 0, 0, 0, 44, 267, 1, 0, 0, 0, 46, 275, 1, 0, 0, 0, 48, 277, 1, 0, 0, 0, 50, 51, 5, 19, 0, 0, 51, 52, 5, 1, 0, 0, 52, 53, 3, 48, 24, 0, 53, 55, 5, 2, 0, 0, 54, 56, 3, 14, 7, 0, 55, 54, 1, 0, 0, 0, 55, 56, 1, 0, 0, 0, 56, 62, 1, 0, 0, 0, 57, 61, 3, 2, 1, 0, 58, 61, 3, 12, 6, 0, 59, 61, 3, 16, 8, 0, 60, 57, 1, 0, 0, 0, 60, 58, 1, 0, 0, 0, 60, 59, 1, 0, 0, 0, 61, 64, 1, 0, 0, 0, 62, 60, 1, 0, 0, 0, 62, 63, 1, 0, 0, 0, 63, 65, 1, 0, 0, 0, 64, 62, 1, 0, 0, 0, 65, 66, 5, 3, 0, 0, 66, 1, 1, 0, 0, 0, 67, 68, 3, 10, 5, 0, 68, 69, 3, 48, 24, 0, 69, 70, 5, 4, 0, 0, 70, 3, 1, 0, 0, 0, 71, 72, 3, 10, 5, 0, 72, 73, 3, 48, 24, 0, 73, 74, 5, 4, 0, 0, 74, 5, 1, 0, 0, 0, 75, 76, 7, 0, 0, 0, 76, 7, 1, 0, 0, 0, 77, 80, 3, 10, 5, 0, 78, 80, 5, 41, 0, 0, 79, 77, 1, 0, 0, 0, 79, 78, 1, 0, 0, 0, 80, 9, 1, 0, 0, 0, 81, 86, 5, 39, 0, 0, 82, 86, 5, 40, 0, 0, 83, 86, 5, 42, 0, 0, 84, 86, 3, 48, 24, 0, 85, 81, 1, 0, 0, 0, 85, 82, 1, 0, 0, 0, 85, 83, 1, 0, 0, 0, 85, 84, 1, 0, 0, 0, 86, 11, 1, 0, 0, 0, 87, 88, 5, 19, 0, 0, 88, 89, 3, 8, 4, 0, 89, 90, 3, 48, 24, 0, 90, 92, 5, 5, 0, 0, 91, 93, 3, 18, 9, 0, 92, 91, 1, 0, 0, 0, 92, 93, 1, 0, 0, 0, 93, 94, 1, 0, 0, 0, 94, 95, 5, 6, 0, 0, 95, 96, 3, 22, 11, 0, 96, 13, 1, 0, 0, 0, 97, 98, 5, 19, 0, 0, 98, 99, 5, 7, 0, 0, 99, 100, 5, 41, 0, 0, 100, 101, 5, 8, 0, 0, 101, 102, 5, 5, 0, 0, 102, 103, 5, 9, 0, 0, 103, 104, 5, 6, 0, 0, 104, 105, 3, 22, 11, 0, 105, 15, 1, 0, 0, 0, 106, 108, 5, 19, 0, 0, 107, 106, 1, 0, 0, 0, 107, 108, 1, 0, 0, 0, 108, 109, 1, 0, 0, 0, 109, 110, 3, 48, 24, 0, 110, 112, 5, 5, 0, 0, 111, 113, 3, 18, 9, 0, 112, 111, 1, 0, 0, 0, 112, 113, 1, 0, 0, 0, 113, 114, 1, 0, 0, 0, 114, 115, 5, 6, 0, 0, 115, 116, 3, 22, 11, 0, 116, 17, 1, 0, 0, 0, 117, 122, 3, 20, 10, 0, 118, 119, 5, 10, 0, 0, 119, 121, 3, 20, 10, 0, 120, 118, 1, 0, 0, 0, 121, 124, 1, 0, 0, 0, 122, 120, 1, 0, 0, 0, 122, 123, 1, 0, 0, 0, 123, 19, 1, 0, 0, 0, 124, 122, 1, 0, 0, 0, 125, 126, 3, 10, 5, 0, 126, 127, 3, 48, 24, 0, 127, 21, 1, 0, 0, 0, 128, 133, 5, 2, 0, 0, 129, 132, 3, 4, 2, 0, 130, 132, 3, 24, 12, 0, 131, 129, 1, 0, 0, 0, 131, 130, 1, 0, 0, 0, 132, 135, 1, 0, 0, 0, 133, 131, 1, 0, 0, 0, 133, 134, 1, 0, 0, 0, 134, 136, 1, 0, 0, 0, 135, 133, 1, 0, 0, 0, 136, 137, 5, 3, 0, 0, 137, 23, 1, 0, 0, 0, 138, 139, 5, 11, 0, 0, 139, 140, 5, 5, 0, 0, 140, 141, 3, 28, 14, 0, 141, 142, 5, 6, 0, 0, 142, 145, 3, 22, 11, 0, 143, 144, 5, 12, 0, 0, 144, 146, 3, 22, 11, 0, 145, 143, 1, 0, 0, 0, 145, 146, 1, 0, 0, 0, 146, 185, 1, 0, 0, 0, 147, 148, 5, 13, 0, 0, 148, 149, 5, 5, 0, 0, 149, 150, 3, 36, 18, 0, 150, 151, 5, 4, 0, 0, 151, 152, 3, 28, 14, 0, 152, 153, 5, 4, 0, 0, 153, 154, 3, 36, 18, 0, 154, 155, 5, 6, 0, 0, 155, 156, 3, 22, 11, 0, 156, 185, 1, 0, 0, 0, 157, 158, 5, 14, 0, 0, 158, 159, 5, 5, 0, 0, 159, 160, 3, 28, 14, 0, 160, 161, 5, 6, 0, 0, 161, 162, 3, 22, 11, 0, 162, 185, 1, 0, 0, 0, 163, 164, 5, 15, 0, 0, 164, 165, 3, 22, 11, 0, 165, 166, 5, 14, 0, 0, 166, 167, 5, 5, 0, 0, 167, 168, 3, 28, 14, 0, 168, 169, 5, 6, 0, 0, 169, 185, 1, 0, 0, 0, 170, 171, 5, 16, 0, 0, 171, 172, 3, 28, 14, 0, 172, 173, 5, 4, 0, 0, 173, 185, 1, 0, 0, 0, 174, 175, 5, 16, 0, 0, 175, 185, 5, 4, 0, 0, 176, 177, 5, 17, 0, 0, 177, 185, 5, 4, 0, 0, 178, 179, 3, 36, 18, 0, 179, 180, 5, 4, 0, 0, 180, 185, 1, 0, 0, 0, 181, 182, 3, 26, 13, 0, 182, 183, 5, 4, 0, 0, 183, 185, 1, 0, 0, 0, 184, 138, 1, 0, 0, 0, 184, 147, 1, 0, 0, 0, 184, 157, 1, 0, 0, 0, 184, 163, 1, 0, 0, 0, 184, 170, 1, 0, 0, 0, 184, 174, 1, 0, 0, 0, 184, 176, 1, 0, 0, 0, 184, 178, 1, 0, 0, 0, 184, 181, 1, 0, 0, 0, 185, 25, 1, 0, 0, 0, 186, 196, 3, 38, 19, 0, 187, 188, 5, 20, 0, 0, 188, 189, 3, 10, 5, 0, 189, 191, 5, 5, 0, 0, 190, 192, 3, 44, 22, 0, 191, 190, 1, 0, 0, 0, 191, 192, 1, 0, 0, 0, 192, 193, 1, 0, 0, 0, 193, 194, 5, 6, 0, 0, 194, 196, 1, 0, 0, 0, 195, 186, 1, 0, 0, 0, 195, 187, 1, 0, 0, 0, 196, 27, 1, 0, 0, 0, 197, 198, 6, 14, -1, 0, 198, 199, 3, 32, 16, 0, 199, 200, 3, 28, 14, 6, 200, 210, 1, 0, 0, 0, 201, 210, 3, 46, 23, 0, 202, 203, 5, 5, 0, 0, 203, 204, 3, 28, 14, 0, 204, 205, 5, 6, 0, 0, 205, 210, 1, 0, 0, 0, 206, 210, 3, 34, 17, 0, 207, 210, 3, 26, 13, 0, 208, 210, 5, 21, 0, 0, 209, 197, 1, 0, 0, 0, 209, 201, 1, 0, 0, 0, 209, 202, 1, 0, 0, 0, 209, 206, 1, 0, 0, 0, 209, 207, 1, 0, 0, 0, 209, 208, 1, 0, 0, 0, 210, 217, 1, 0, 0, 0, 211, 212, 10, 7, 0, 0, 212, 213, 3, 30, 15, 0, 213, 214, 3, 28, 14, 8, 214, 216, 1, 0, 0, 0, 215, 211, 1, 0, 0, 0, 216, 219, 1, 0, 0, 0, 217, 215, 1, 0, 0, 0, 217, 218, 1, 0, 0, 0, 218, 29, 1, 0, 0, 0, 219, 217, 1, 0, 0, 0, 220, 221, 7, 1, 0, 0, 221, 31, 1, 0, 0, 0, 222, 223, 7, 2, 0, 0, 223, 33, 1, 0, 0, 0, 224, 225, 5, 22, 0, 0, 225, 227, 5, 18, 0, 0, 226, 224, 1, 0, 0, 0, 226, 227, 1, 0, 0, 0, 227, 233, 1, 0, 0, 0, 228, 229, 3, 40, 20, 0, 229, 230, 5, 18, 0, 0, 230, 232, 1, 0, 0, 0, 231, 228, 1, 0, 0, 0, 232, 235, 1, 0, 0, 0, 233, 231, 1, 0, 0, 0, 233, 234, 1, 0, 0, 0, 234, 236, 1, 0, 0, 0, 235, 233, 1, 0, 0, 0, 236, 237, 3, 48, 24, 0, 237, 35, 1, 0, 0, 0, 238, 239, 3, 34, 17, 0, 239, 240, 3, 6, 3, 0, 240, 241, 3, 28, 14, 0, 241, 37, 1, 0, 0, 0, 242, 243, 5, 22, 0, 0, 243, 245, 5, 18, 0, 0, 244, 242, 1, 0, 0, 0, 244, 245, 1, 0, 0, 0, 245, 251, 1, 0, 0, 0, 246, 247, 3, 40, 20, 0, 247, 248, 5, 18, 0, 0, 248, 250, 1, 0, 0, 0, 249, 246, 1, 0, 0, 0, 250, 253, 1, 0, 0, 0, 251, 249, 1, 0, 0, 0, 251, 252, 1, 0, 0, 0, 252, 254, 1, 0, 0, 0, 253, 251, 1, 0, 0, 0, 254, 255, 3, 42, 21, 0, 255, 39, 1, 0, 0, 0, 256, 259, 3, 42, 21, 0, 257, 259, 3, 48, 24, 0, 258, 256, 1, 0, 0, 0, 258, 257, 1, 0, 0, 0, 259, 41, 1, 0, 0, 0, 260, 261, 3, 48, 24, 0, 261, 263, 5, 5, 0, 0, 262, 264, 3, 44, 22, 0, 263, 262, 1, 0, 0, 0, 263, 264, 1, 0, 0, 0, 264, 265, 1, 0, 0, 0, 265, 266, 5, 6, 0, 0, 266, 43, 1, 0, 0, 0, 267, 272, 3, 28, 14, 0, 268, 269, 5, 10, 0, 0, 269, 271, 3, 28, 14, 0, 270, 268, 1, 0, 0, 0, 271, 274, 1, 0, 0, 0, 272, 270, 1, 0, 0, 0, 272, 273, 1, 0, 0, 0, 273, 45, 1, 0, 0, 0, 274, 272, 1, 0, 0, 0, 275, 276, 7, 3, 0, 0, 276, 47, 1, 0, 0, 0, 277, 278, 5, 45, 0, 0, 278, 49, 1, 0, 0, 0, 24, 55, 60, 62, 79, 85, 92, 107, 112, 122, 131, 133, 145, 184, 191, 195, 209, 217, 226, 233, 244, 251, 258, 263, 272] \ No newline at end of file diff --git a/src/main/java/de/maishai/antlr/DecafParser.java b/src/main/java/de/maishai/antlr/DecafParser.java index 461e1f4..862308e 100644 --- a/src/main/java/de/maishai/antlr/DecafParser.java +++ b/src/main/java/de/maishai/antlr/DecafParser.java @@ -1904,8 +1904,8 @@ public class DecafParser extends Parser { @SuppressWarnings("CheckReturnValue") public static class AssignContext extends ParserRuleContext { - public IdContext id() { - return getRuleContext(IdContext.class,0); + public FieldIdContext fieldId() { + return getRuleContext(FieldIdContext.class,0); } public AssignSignContext assignSign() { return getRuleContext(AssignSignContext.class,0); @@ -1939,7 +1939,7 @@ public class DecafParser extends Parser { enterOuterAlt(_localctx, 1); { setState(238); - id(); + fieldId(); setState(239); assignSign(); setState(240); @@ -2494,7 +2494,7 @@ public class DecafParser extends Parser { "\u00eb\u0001\u0000\u0000\u0000\u00e9\u00e7\u0001\u0000\u0000\u0000\u00e9"+ "\u00ea\u0001\u0000\u0000\u0000\u00ea\u00ec\u0001\u0000\u0000\u0000\u00eb"+ "\u00e9\u0001\u0000\u0000\u0000\u00ec\u00ed\u00030\u0018\u0000\u00ed#\u0001"+ - "\u0000\u0000\u0000\u00ee\u00ef\u00030\u0018\u0000\u00ef\u00f0\u0003\u0006"+ + "\u0000\u0000\u0000\u00ee\u00ef\u0003\"\u0011\u0000\u00ef\u00f0\u0003\u0006"+ "\u0003\u0000\u00f0\u00f1\u0003\u001c\u000e\u0000\u00f1%\u0001\u0000\u0000"+ "\u0000\u00f2\u00f3\u0005\u0016\u0000\u0000\u00f3\u00f5\u0005\u0012\u0000"+ "\u0000\u00f4\u00f2\u0001\u0000\u0000\u0000\u00f4\u00f5\u0001\u0000\u0000"+ diff --git a/src/main/java/de/maishai/ast/records/Assignment.java b/src/main/java/de/maishai/ast/records/Assignment.java index 1d5d654..b783ae2 100644 --- a/src/main/java/de/maishai/ast/records/Assignment.java +++ b/src/main/java/de/maishai/ast/records/Assignment.java @@ -2,5 +2,5 @@ package de.maishai.ast.records; -public record Assignment(Id loc, Expression value) implements Statement { +public record Assignment(FieldId loc, Expression value) implements Statement { } From 0163f5c0b6f389dd5e0b2cafdf957908d14b22ef Mon Sep 17 00:00:00 2001 From: laurenz Date: Wed, 8 May 2024 11:47:56 +0200 Subject: [PATCH 2/6] fixed compile error --- .../java/de/maishai/typedast/typedclass/TypedAssignment.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java b/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java index c185962..2804ae8 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java @@ -36,7 +36,7 @@ public class TypedAssignment implements TypedStatement { public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { Assignment untyped = (Assignment) unTypedAST; TypedAssignment typedAssignment = new TypedAssignment(); - typedAssignment.setVarName(untyped.loc().name()); + typedAssignment.setVarName(untyped.loc().id().name()); typedAssignment.setValue((TypedExpression) value.convertToTypedAST(localVar, classes, untyped.value())); return typedAssignment; } From 587d95f54626add179208d3ef91fd270a285eeaf Mon Sep 17 00:00:00 2001 From: JonathanFleischmann Date: Wed, 8 May 2024 14:10:48 +0200 Subject: [PATCH 3/6] adapted abstract syntax test files to new ast --- .../AbstractSyntax_ClassWithConstructor.java | 45 +++++++++++++++---- ...tax_ClassWithConstructorAndMethodCall.java | 10 ++++- ...ax_ClassWithConstructorWithParameters.java | 27 ++++++++--- ...bstractSyntax_ClassWithMethodAndField.java | 5 ++- ...tax_ClassWithMoreComplexMethodAndMain.java | 25 ++++++++--- 5 files changed, 90 insertions(+), 22 deletions(-) diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructor.java b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructor.java index 14aee94..fb450bb 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructor.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructor.java @@ -52,21 +52,33 @@ public class AbstractSyntax_ClassWithConstructor { ); List statementList = List.of( new Assignment( - new Id("x"), + new FieldId( + true, + null, + new Id("x")), new IntLiteral(10) ), new For( new Assignment( - new Id("i"), + new FieldId( + false, + null, + new Id("i")), new IntLiteral(0) ), new Binary( - new Id("i"), + new FieldId( + false, + null, + new Id("i")), Operator.LT, new IntLiteral(6) ), new Assignment( - new Id("i"), + new FieldId( + false, + null, + new Id("i")), new Binary( new Id("i"), Operator.ADD, @@ -83,7 +95,10 @@ public class AbstractSyntax_ClassWithConstructor { List.of( new For( new Assignment( - new Id("j"), + new FieldId( + false, + null, + new Id("j")), new IntLiteral(0) ), new Binary( @@ -92,14 +107,27 @@ public class AbstractSyntax_ClassWithConstructor { new Id("x") ), new Assignment( - new Id("j"), - new IntLiteral(1) + new FieldId( + false, + null, + new Id("j")), + new Binary( + new FieldId( + false, + null, + new Id("j")), + Operator.ADD, + new IntLiteral(1) + ) ), new Block( List.of(), List.of( new Assignment( - new Id("x"), + new FieldId( + true, + null, + new Id("x")), new Binary( new Id("x"), Operator.MUL, @@ -112,6 +140,7 @@ public class AbstractSyntax_ClassWithConstructor { ) ) ) + ); Block block = new Block( localVariables, diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorAndMethodCall.java b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorAndMethodCall.java index 627846f..b3beba8 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorAndMethodCall.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorAndMethodCall.java @@ -54,7 +54,10 @@ public class AbstractSyntax_ClassWithConstructorAndMethodCall { List.of(), List.of( new Assignment( - new Id("x"), + new FieldId( + true, + null, + new Id("x")), new IntLiteral(10) ), new While( @@ -68,7 +71,10 @@ public class AbstractSyntax_ClassWithConstructorAndMethodCall { List.of(), List.of( new Assignment( - new Id("x"), + new FieldId( + true, + null, + new Id("x")), new Binary( new Id("x"), Operator.MUL, diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorWithParameters.java b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorWithParameters.java index 2652d37..b311324 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorWithParameters.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorWithParameters.java @@ -57,7 +57,10 @@ public class AbstractSyntax_ClassWithConstructorWithParameters { List.of(), List.of( new Assignment( - new Id("x"), + new FieldId( + true, + null, + new Id("x")), new Id("startValue") ), new While( @@ -75,12 +78,15 @@ public class AbstractSyntax_ClassWithConstructorWithParameters { ), List.of( new Assignment( - new Id("innerRepetitions"), + new FieldId( + false, + null, + new Id("innerRepetitions")), new Id("x") ), new While( new Binary( - new Id("repetitions"), + new Id("innerRepetitions"), Operator.GT, new IntLiteral(0) ), @@ -88,7 +94,10 @@ public class AbstractSyntax_ClassWithConstructorWithParameters { List.of(), List.of( new Assignment( - new Id("x"), + new FieldId( + true, + null, + new Id("x")), new Binary( new Id("x"), Operator.MUL, @@ -96,7 +105,10 @@ public class AbstractSyntax_ClassWithConstructorWithParameters { ) ), new Assignment( - new Id("innerRepetitions"), + new FieldId( + false, + null, + new Id("innerRepetitions")), new Binary(new Id("innerRepetitions"), Operator.SUB, new IntLiteral(1)) @@ -106,7 +118,10 @@ public class AbstractSyntax_ClassWithConstructorWithParameters { ) ), new Assignment( - new Id("repetitions"), + new FieldId( + false, + null, + new Id("repetitions")), new Binary(new Id("repetitions"), Operator.SUB, new IntLiteral(1) diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMethodAndField.java b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMethodAndField.java index ff54632..7c6834d 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMethodAndField.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMethodAndField.java @@ -53,7 +53,10 @@ class AbstractSyntax_ClassWithMethodAndField { List.of(), List.of( new Assignment( - new Id("c"), + new FieldId( + true, + null, + new Id("c")), new Id("character") ) ) diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMoreComplexMethodAndMain.java b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMoreComplexMethodAndMain.java index 4232798..cb4670b 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMoreComplexMethodAndMain.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMoreComplexMethodAndMain.java @@ -63,11 +63,17 @@ public class AbstractSyntax_ClassWithMoreComplexMethodAndMain { ), List.of( new Assignment( - new Id("i"), + new FieldId( + false, + null, + new Id("i")), new IntLiteral(11) ), new Assignment( - new Id("returnValue"), + new FieldId( + false, + null, + new Id("returnValue")), new BoolLiteral(false) ), new While( @@ -80,14 +86,20 @@ public class AbstractSyntax_ClassWithMoreComplexMethodAndMain { List.of(), List.of( new Assignment( - new Id("i"), + new FieldId( + true, + null, + new Id("i")), new Binary(new Id("i"), Operator.SUB, new IntLiteral(1)) ), new Assignment( - new Id("returnValue"), + new FieldId( + false, + null, + new Id("returnValue")), new Unary( UnaryOperator.NOT, new Id("returnValue") @@ -115,7 +127,10 @@ public class AbstractSyntax_ClassWithMoreComplexMethodAndMain { TypeClassWithMoreComplexMethodAndMain.setObjectType(new Id("ClassWithMoreComplexMethodAndMain")); List statementList = List.of( new Assignment( - new Id("instance"), + new FieldId( + true, + null, + new Id("instance")), new New( TypeClassWithMoreComplexMethodAndMain, List.of() From 0ac708c8ae740a34a94d94b141311243635c98f0 Mon Sep 17 00:00:00 2001 From: laurenz Date: Wed, 8 May 2024 14:27:51 +0200 Subject: [PATCH 4/6] changes to AST: id is gone, Declaration is one thing, renaming. only one Type --- src/main/antlr/Decaf.g4 | 6 +-- src/main/java/de/maishai/ASTGenerator.java | 42 +++++++------------ src/main/java/de/maishai/BlockGenerator.java | 4 +- .../java/de/maishai/ExpressionGenerator.java | 32 ++++++++------ .../java/de/maishai/StatementGenerator.java | 31 +++++++------- .../java/de/maishai/VariableGenerator.java | 10 ++--- src/main/java/de/maishai/antlr/Decaf.interp | 2 +- .../java/de/maishai/antlr/DecafParser.java | 2 +- src/main/java/de/maishai/ast/AssignSign.java | 8 ---- src/main/java/de/maishai/ast/ReturnType.java | 26 ------------ src/main/java/de/maishai/ast/Type.java | 9 ++-- .../de/maishai/ast/records/Assignment.java | 2 +- .../java/de/maishai/ast/records/Block.java | 2 +- .../java/de/maishai/ast/records/Class.java | 2 +- .../de/maishai/ast/records/Constructor.java | 2 +- .../de/maishai/ast/records/Declaration.java | 6 +++ .../de/maishai/ast/records/Expression.java | 2 +- .../java/de/maishai/ast/records/Field.java | 6 --- .../java/de/maishai/ast/records/FieldId.java | 4 -- .../maishai/ast/records/FieldVarAccess.java | 4 ++ src/main/java/de/maishai/ast/records/Id.java | 5 --- .../de/maishai/ast/records/LocalVariable.java | 7 ---- .../java/de/maishai/ast/records/Method.java | 4 +- .../de/maishai/ast/records/MethodCall.java | 2 +- .../java/de/maishai/ast/records/Node.java | 2 +- .../de/maishai/ast/records/ReturnVoid.java | 5 --- .../de/maishai/ast/records/Statement.java | 2 +- .../java/de/maishai/typedast/TypeMapper.java | 13 +----- .../typedast/typedclass/TypedAssignment.java | 3 +- .../typedast/typedclass/TypedBlock.java | 4 +- .../typedast/typedclass/TypedConstructor.java | 2 +- .../typedast/typedclass/TypedField.java | 8 ++-- .../typedast/typedclass/TypedFieldId.java | 12 +++--- .../typedclass/TypedLocalVariable.java | 7 ++-- .../typedast/typedclass/TypedMethod.java | 4 +- .../typedast/typedclass/TypedReturnVoid.java | 2 +- .../AbstractSyntax_ClassWithConstructor.java | 6 +-- ...tax_ClassWithConstructorAndMethodCall.java | 7 ++-- ...ax_ClassWithConstructorWithParameters.java | 6 +-- .../AbstractSyntax_ClassWithField.java | 9 ++-- .../AbstractSyntax_ClassWithMethod.java | 1 - ...bstractSyntax_ClassWithMethodAndField.java | 6 +-- ...tax_ClassWithMoreComplexMethodAndMain.java | 6 +-- .../AbstractSyntax_PublicClass.java | 1 - ...edAbstractSyntax_ClassWithConstructor.java | 1 - ...tax_ClassWithConstructorAndMethodCall.java | 13 +----- ...ax_ClassWithConstructorWithParameters.java | 7 ++-- ...bstractSyntax_ClassWithMethodAndField.java | 6 +-- ...tax_ClassWithMoreComplexMethodAndMain.java | 6 +-- 49 files changed, 135 insertions(+), 224 deletions(-) delete mode 100644 src/main/java/de/maishai/ast/AssignSign.java delete mode 100644 src/main/java/de/maishai/ast/ReturnType.java create mode 100644 src/main/java/de/maishai/ast/records/Declaration.java delete mode 100644 src/main/java/de/maishai/ast/records/Field.java delete mode 100644 src/main/java/de/maishai/ast/records/FieldId.java create mode 100644 src/main/java/de/maishai/ast/records/FieldVarAccess.java delete mode 100644 src/main/java/de/maishai/ast/records/Id.java delete mode 100644 src/main/java/de/maishai/ast/records/LocalVariable.java delete mode 100644 src/main/java/de/maishai/ast/records/ReturnVoid.java diff --git a/src/main/antlr/Decaf.g4 b/src/main/antlr/Decaf.g4 index d3ba5b5..f379921 100644 --- a/src/main/antlr/Decaf.g4 +++ b/src/main/antlr/Decaf.g4 @@ -36,7 +36,7 @@ expr : expr binaryOp expr #BinaryOperation | unaryOp expr #UnaryOperation | literal #Constant | '(' expr ')' #Expression - | fieldId #Identifier + | fieldVarAccess #Identifier | stmtexpr #StatementExpressionexpr | NULL #Null ; @@ -44,9 +44,9 @@ expr : expr binaryOp expr #BinaryOperation binaryOp : ADD | SUB | MUL | GT | LT | GE | LE | EQ | NE | AND | OR; unaryOp : SUB | NOT; -fieldId : (THIS '.')? (recipient '.')* id; +fieldVarAccess : (THIS '.')? (recipient '.')* id; -assign : fieldId assignSign expr ; +assign : fieldVarAccess assignSign expr ; methCall : (THIS '.')? (recipient '.')* methName; recipient : methName | id; methName : id '(' args? ')'; diff --git a/src/main/java/de/maishai/ASTGenerator.java b/src/main/java/de/maishai/ASTGenerator.java index ad4681a..ff7fbf9 100644 --- a/src/main/java/de/maishai/ASTGenerator.java +++ b/src/main/java/de/maishai/ASTGenerator.java @@ -1,7 +1,6 @@ package de.maishai; import de.maishai.antlr.DecafParser; -import de.maishai.ast.ReturnType; import de.maishai.ast.Type; import de.maishai.ast.records.*; import de.maishai.ast.records.Class; @@ -13,9 +12,9 @@ public class ASTGenerator { public static Class generateAST(DecafParser.ClassContext ctx) { - List fields = new ArrayList<>(); + List declarations = new ArrayList<>(); if (ctx.field() != null) { - fields = ctx.field().stream().map(ASTGenerator::generateFieldVariable).toList(); + declarations = ctx.field().stream().map(ASTGenerator::generateFieldVariable).toList(); } MainMethod mainMethod = null; if(ctx.mainmeth() != null){ @@ -29,14 +28,12 @@ public class ASTGenerator { if (ctx.meth() != null) { meths = ctx.meth().stream().map(ASTGenerator::generateMethod).toList(); } - Id classId = new Id(ctx.id().getText()); - return new Class(classId, fields, meths, mainMethod, constructors); + return new Class(ctx.id().getText(), declarations, meths, mainMethod, constructors); } - public static Field generateFieldVariable(DecafParser.FieldContext ctx) { - Id id = new Id(ctx.id().getText()); + public static Declaration generateFieldVariable(DecafParser.FieldContext ctx) { Type type = ASTGenerator.getType(ctx.type()); - return new Field(id, type); + return new Declaration(ctx.id().getText(), type); } public static Parameter generateParameter(DecafParser.ParamContext ctx) { @@ -49,8 +46,7 @@ public class ASTGenerator { params = ctx.params().param().stream().map(ASTGenerator::generateParameter).toList(); } Block block = new BlockGenerator().visit(ctx.block()); - Id methId = new Id(ctx.id().getText()); - return new Method(getReturnType(ctx.returntype()), methId, params, block); + return new Method(getType(ctx.returntype()), ctx.id().getText(), params, block); } public static Constructor generateConstructor(DecafParser.ConstructorContext ctx) { @@ -60,8 +56,7 @@ public class ASTGenerator { params = ctx.params().param().stream().map(ASTGenerator::generateParameter).toList(); } Block block = new BlockGenerator().visit(ctx.block()); - Id constructorId = new Id(ctx.id().getText()); - return new Constructor(isPublic, constructorId, params, block); + return new Constructor(isPublic, ctx.id().getText(), params, block); } public static MainMethod generateMainMethod(DecafParser.MainmethContext ctx) { @@ -78,27 +73,18 @@ public class ASTGenerator { return Type.CHAR; if (ctx.id() != null) { Type type = Type.OBJECT; - type.setObjectType(new Id(ctx.id().getText())); + type.setObjectType(ctx.id().getText()); return type; } throw new RuntimeException("No type found!"); } - - public static ReturnType getReturnType(DecafParser.ReturntypeContext ctx) { - if (ctx.type().INT() != null) - return ReturnType.INT; - if (ctx.type().BOOL() != null) - return ReturnType.BOOL; - if (ctx.type().CHAR() != null) - return ReturnType.CHAR; - if (ctx.VOID() != null) { - return ReturnType.VOID; + public static Type getType(DecafParser.ReturntypeContext ctx) { + if (ctx.type() != null){ + return getType(ctx.type()); } - if (ctx.type().id() != null){ - ReturnType type = ReturnType.OBJECT; - type.setObjectType(new Id(ctx.type().id().getText())); - return type; + if (ctx.VOID() != null){ + return Type.VOID; } - throw new RuntimeException("No return type found!"); + throw new RuntimeException("No type found!"); } } diff --git a/src/main/java/de/maishai/BlockGenerator.java b/src/main/java/de/maishai/BlockGenerator.java index 075ad1a..162da80 100644 --- a/src/main/java/de/maishai/BlockGenerator.java +++ b/src/main/java/de/maishai/BlockGenerator.java @@ -2,16 +2,16 @@ package de.maishai; import de.maishai.antlr.DecafBaseVisitor; import de.maishai.antlr.DecafParser; +import de.maishai.ast.records.Declaration; import de.maishai.ast.records.Statement; import de.maishai.ast.records.Block; -import de.maishai.ast.records.LocalVariable; import java.util.List; public class BlockGenerator extends DecafBaseVisitor { @Override public Block visitBlock(DecafParser.BlockContext ctx) { - List vars = ctx.localVar().stream().map(var -> new VariableGenerator().visit(var)).toList(); + List vars = ctx.localVar().stream().map(var -> new VariableGenerator().visit(var)).toList(); List statements = ctx.stmt().stream().map(stmt -> new StatementGenerator().visit(stmt)).toList(); return new Block(vars, statements); } diff --git a/src/main/java/de/maishai/ExpressionGenerator.java b/src/main/java/de/maishai/ExpressionGenerator.java index 7586d84..d723a1c 100644 --- a/src/main/java/de/maishai/ExpressionGenerator.java +++ b/src/main/java/de/maishai/ExpressionGenerator.java @@ -47,9 +47,9 @@ public class ExpressionGenerator extends DecafBaseVisitor { Expression recipient = null; if (ctx.fieldId().recipient() != null) { List recipientList = ctx.fieldId().recipient(); - recipient = generateRecipient(recipientList, null); + recipient = generateRecursiveOwnerChain(recipientList, null); } - return new FieldId(isField, recipient, new Id(ctx.getText())); + return new FieldVarAccess(isField, recipient, ctx.getText()); } public static Expression generateConstant(DecafParser.LiteralContext ctx) { @@ -87,18 +87,17 @@ public class ExpressionGenerator extends DecafBaseVisitor { @Override public Expression visitMethodCall(DecafParser.MethodCallContext ctx) { boolean isField = ctx.methCall().THIS() != null; - Expression recipient = null; + Expression recursiveOwnerChain = null; if (ctx.methCall().recipient() != null) { List recipientList = ctx.methCall().recipient(); - recipient = generateRecipient(recipientList, null); + recursiveOwnerChain = generateRecursiveOwnerChain(recipientList, null); } - Id id = new Id(ctx.methCall().methName().id().getText()); List args = new ArrayList<>(); for (var expr : ctx.methCall().methName().args().expr()) { Expression astExpr = expr.accept(this); args.add(astExpr); } - return new MethodCall(isField, recipient, id, args); + return new MethodCall(new FieldVarAccess(isField, recursiveOwnerChain, ctx.methCall().methName().id().getText()), args); } @Override @@ -112,18 +111,25 @@ public class ExpressionGenerator extends DecafBaseVisitor { return new New(type, args); } - public static Expression generateRecipient(List ctxList, Expression recipient) { + public static Expression generateRecursiveOwnerChain(List ctxList, FieldVarAccess recipient) { if (ctxList.isEmpty()) { return recipient; } DecafParser.RecipientContext ctx = ctxList.get(0); ctxList.remove(0); - Id id = new Id(ctx.methName().id().getText()); - List args = new ArrayList<>(); - for (var expr : ctx.methName().args().expr()) { - Expression astExpr = expr.accept(new ExpressionGenerator()); - args.add(astExpr); + if (ctx.id() != null) { + return new FieldVarAccess(false, generateRecursiveOwnerChain(ctxList, recipient), ctx.id().getText()); } - return new MethodCall(null, generateRecipient(ctxList, recipient), id, args); + if (ctx.methName() != null) { + List args = new ArrayList<>(); + + for (var expr : ctx.methName().args().expr()) { + Expression astExpr = expr.accept(new ExpressionGenerator()); + args.add(astExpr); + } + return new MethodCall(new FieldVarAccess(false, generateRecursiveOwnerChain(ctxList, recipient), ctx.methName().id().getText()), args); + + } + throw new RuntimeException(); } } diff --git a/src/main/java/de/maishai/StatementGenerator.java b/src/main/java/de/maishai/StatementGenerator.java index 0cc22a0..6b4978f 100644 --- a/src/main/java/de/maishai/StatementGenerator.java +++ b/src/main/java/de/maishai/StatementGenerator.java @@ -53,7 +53,7 @@ public class StatementGenerator extends DecafBaseVisitor { @Override public Statement visitReturnVoid(DecafParser.ReturnVoidContext ctx) { - return new ReturnVoid(); + return new Return(null); } @Override @@ -67,33 +67,32 @@ public class StatementGenerator extends DecafBaseVisitor { } private Assignment generateAssign(DecafParser.AssignContext ctx) { - FieldId fieldId = generateField(ctx.fieldId()); - Expression expr = resolveFancyAssign(ctx.assignSign(), fieldId, new ExpressionGenerator().visit(ctx.expr())); - return new Assignment(fieldId, expr); + FieldVarAccess fieldVarAccess = generateField(ctx.fieldId()); + Expression expr = resolveFancyAssign(ctx.assignSign(), fieldVarAccess, new ExpressionGenerator().visit(ctx.expr())); + return new Assignment(fieldVarAccess, expr); } - private FieldId generateField(DecafParser.FieldIdContext fieldIdContext) { - return new FieldId(fieldIdContext.THIS() != null, - ExpressionGenerator.generateRecipient(fieldIdContext.recipient(), - null), new Id(fieldIdContext.id().getText())); + private FieldVarAccess generateField(DecafParser.FieldIdContext fieldIdContext) { + return new FieldVarAccess(fieldIdContext.THIS() != null, + ExpressionGenerator.generateRecursiveOwnerChain(fieldIdContext.recipient(), + null), fieldIdContext.id().getText()); } //StatementExpression @Override public Statement visitMethodCall(DecafParser.MethodCallContext ctx) { boolean isField = ctx.methCall().THIS() != null; - Expression recipient = null; + Expression recursiveOwnerChain = null; if (ctx.methCall().recipient() != null) { List recipientList = ctx.methCall().recipient(); - recipient = ExpressionGenerator.generateRecipient(recipientList, null); + recursiveOwnerChain = ExpressionGenerator.generateRecursiveOwnerChain(recipientList, null); } - Id id = new Id(ctx.methCall().methName().id().getText()); List args = new ArrayList<>(); for (var expr : ctx.methCall().methName().args().expr()) { Expression astExpr = expr.accept(new ExpressionGenerator()); args.add(astExpr); } - return new MethodCall(isField, recipient, id, args); + return new MethodCall(new FieldVarAccess(isField, recursiveOwnerChain, ctx.methCall().methName().id().getText()), args); } @Override @@ -107,15 +106,15 @@ public class StatementGenerator extends DecafBaseVisitor { return new New(type, args); } - public static Expression resolveFancyAssign(DecafParser.AssignSignContext ctx, FieldId fieldId, Expression expression) { + public static Expression resolveFancyAssign(DecafParser.AssignSignContext ctx, FieldVarAccess fieldVarAccess, Expression expression) { if (ctx.ASSIGN() != null) return expression; if (ctx.ADD_ASSIGN() != null) - return new Binary(fieldId, Operator.ADD, expression); + return new Binary(fieldVarAccess, Operator.ADD, expression); if (ctx.SUB_ASSIGN() != null) - return new Binary(fieldId, Operator.SUB, expression); + return new Binary(fieldVarAccess, Operator.SUB, expression); if (ctx.MUL_ASSIGN() != null) - return new Binary(fieldId, Operator.MUL, expression); + return new Binary(fieldVarAccess, Operator.MUL, expression); throw new RuntimeException("No assign sign found!"); } } diff --git a/src/main/java/de/maishai/VariableGenerator.java b/src/main/java/de/maishai/VariableGenerator.java index a4de07f..70d0dbe 100644 --- a/src/main/java/de/maishai/VariableGenerator.java +++ b/src/main/java/de/maishai/VariableGenerator.java @@ -4,15 +4,13 @@ package de.maishai; import de.maishai.antlr.DecafBaseVisitor; import de.maishai.antlr.DecafParser; import de.maishai.ast.Type; -import de.maishai.ast.records.Id; -import de.maishai.ast.records.LocalVariable; +import de.maishai.ast.records.Declaration; -public class VariableGenerator extends DecafBaseVisitor { +public class VariableGenerator extends DecafBaseVisitor { @Override - public LocalVariable visitLocalVar(DecafParser.LocalVarContext ctx) { - Id id = new Id(ctx.id().getText()); + public Declaration visitLocalVar(DecafParser.LocalVarContext ctx) { Type type = ASTGenerator.getType(ctx.type()); - return new LocalVariable(id, type); + return new Declaration(ctx.id().getText(), type); } } diff --git a/src/main/java/de/maishai/antlr/Decaf.interp b/src/main/java/de/maishai/antlr/Decaf.interp index d2c4c01..016d501 100644 --- a/src/main/java/de/maishai/antlr/Decaf.interp +++ b/src/main/java/de/maishai/antlr/Decaf.interp @@ -116,7 +116,7 @@ stmtexpr expr binaryOp unaryOp -fieldId +fieldVarAccess assign methCall recipient diff --git a/src/main/java/de/maishai/antlr/DecafParser.java b/src/main/java/de/maishai/antlr/DecafParser.java index 862308e..ca9882c 100644 --- a/src/main/java/de/maishai/antlr/DecafParser.java +++ b/src/main/java/de/maishai/antlr/DecafParser.java @@ -35,7 +35,7 @@ public class DecafParser extends Parser { return new String[] { "class", "field", "localVar", "assignSign", "returntype", "type", "meth", "mainmeth", "constructor", "params", "param", "block", "stmt", "stmtexpr", - "expr", "binaryOp", "unaryOp", "fieldId", "assign", "methCall", "recipient", + "expr", "binaryOp", "unaryOp", "fieldId", "assign", "methCall", "recursiveOwnerChain", "methName", "args", "literal", "id" }; } diff --git a/src/main/java/de/maishai/ast/AssignSign.java b/src/main/java/de/maishai/ast/AssignSign.java deleted file mode 100644 index 98830a9..0000000 --- a/src/main/java/de/maishai/ast/AssignSign.java +++ /dev/null @@ -1,8 +0,0 @@ -package de.maishai.ast; - -public enum AssignSign { - ASSIGN, - ADD_ASSIGN, - SUB_ASSIGN, - MUL_ASSIGN, -} diff --git a/src/main/java/de/maishai/ast/ReturnType.java b/src/main/java/de/maishai/ast/ReturnType.java deleted file mode 100644 index bd8ea7e..0000000 --- a/src/main/java/de/maishai/ast/ReturnType.java +++ /dev/null @@ -1,26 +0,0 @@ -package de.maishai.ast; - -import de.maishai.ast.records.Id; - -public enum ReturnType { - INT, - BOOL, - CHAR, - VOID, - OBJECT; - - Id objectType; - - public Id getObjectType() { - if(this == ReturnType.OBJECT){ - return objectType; - } - return null; - } - - public void setObjectType(Id objectType) { - if(this == ReturnType.OBJECT){ - this.objectType = objectType; - } - } -} diff --git a/src/main/java/de/maishai/ast/Type.java b/src/main/java/de/maishai/ast/Type.java index 4cbe465..e4a3b86 100644 --- a/src/main/java/de/maishai/ast/Type.java +++ b/src/main/java/de/maishai/ast/Type.java @@ -1,23 +1,22 @@ package de.maishai.ast; -import de.maishai.ast.records.Id; - public enum Type { INT, BOOL, CHAR, + VOID, OBJECT; - Id objectType; + String objectType; - public Id getObjectType() { + public String getObjectType() { if(this == Type.OBJECT){ return objectType; } return null; } - public void setObjectType(Id objectType) { + public void setObjectType(String objectType) { if(this == Type.OBJECT){ this.objectType = objectType; } diff --git a/src/main/java/de/maishai/ast/records/Assignment.java b/src/main/java/de/maishai/ast/records/Assignment.java index b783ae2..7d4fd97 100644 --- a/src/main/java/de/maishai/ast/records/Assignment.java +++ b/src/main/java/de/maishai/ast/records/Assignment.java @@ -2,5 +2,5 @@ package de.maishai.ast.records; -public record Assignment(FieldId loc, Expression value) implements Statement { +public record Assignment(FieldVarAccess location, Expression value) implements Statement { } diff --git a/src/main/java/de/maishai/ast/records/Block.java b/src/main/java/de/maishai/ast/records/Block.java index 9184609..81f49e2 100644 --- a/src/main/java/de/maishai/ast/records/Block.java +++ b/src/main/java/de/maishai/ast/records/Block.java @@ -4,5 +4,5 @@ package de.maishai.ast.records; import java.util.List; -public record Block(List vars, List stmts) implements Node { +public record Block(List localVariables, List stmts) implements Node { } diff --git a/src/main/java/de/maishai/ast/records/Class.java b/src/main/java/de/maishai/ast/records/Class.java index f21e2e6..eb9b5a1 100644 --- a/src/main/java/de/maishai/ast/records/Class.java +++ b/src/main/java/de/maishai/ast/records/Class.java @@ -2,5 +2,5 @@ package de.maishai.ast.records; import java.util.List; -public record Class(Id id , List fields, List methods, MainMethod mainMethod, List constructors) implements Node { +public record Class(String classname , List fieldDeclarations, List methods, MainMethod mainMethod, List constructors) implements Node { } diff --git a/src/main/java/de/maishai/ast/records/Constructor.java b/src/main/java/de/maishai/ast/records/Constructor.java index d316b4a..2c4b5e6 100644 --- a/src/main/java/de/maishai/ast/records/Constructor.java +++ b/src/main/java/de/maishai/ast/records/Constructor.java @@ -2,5 +2,5 @@ package de.maishai.ast.records; import java.util.List; -public record Constructor(Boolean isPublic, Id id, List params, Block block) implements Node { +public record Constructor(Boolean isPublic, String className, List params, Block block) implements Node { } diff --git a/src/main/java/de/maishai/ast/records/Declaration.java b/src/main/java/de/maishai/ast/records/Declaration.java new file mode 100644 index 0000000..248b9b1 --- /dev/null +++ b/src/main/java/de/maishai/ast/records/Declaration.java @@ -0,0 +1,6 @@ +package de.maishai.ast.records; + +import de.maishai.ast.Type; + +public record Declaration(String name, Type type) implements Node { +} diff --git a/src/main/java/de/maishai/ast/records/Expression.java b/src/main/java/de/maishai/ast/records/Expression.java index 0a85a76..2463f86 100644 --- a/src/main/java/de/maishai/ast/records/Expression.java +++ b/src/main/java/de/maishai/ast/records/Expression.java @@ -1,5 +1,5 @@ package de.maishai.ast.records; -public sealed interface Expression extends Node permits Binary, BoolLiteral, CharLiteral, FieldId, Id, IntLiteral, MethodCall, New, Unary { +public sealed interface Expression extends Node permits Binary, BoolLiteral, CharLiteral, FieldVarAccess, IntLiteral, MethodCall, New, Unary { } diff --git a/src/main/java/de/maishai/ast/records/Field.java b/src/main/java/de/maishai/ast/records/Field.java deleted file mode 100644 index 546481a..0000000 --- a/src/main/java/de/maishai/ast/records/Field.java +++ /dev/null @@ -1,6 +0,0 @@ -package de.maishai.ast.records; - -import de.maishai.ast.Type; - -public record Field(Id name, Type type) implements Node { -} diff --git a/src/main/java/de/maishai/ast/records/FieldId.java b/src/main/java/de/maishai/ast/records/FieldId.java deleted file mode 100644 index 96772fa..0000000 --- a/src/main/java/de/maishai/ast/records/FieldId.java +++ /dev/null @@ -1,4 +0,0 @@ -package de.maishai.ast.records; - -public record FieldId(Boolean field, Expression recipient, Id id) implements Expression { -} diff --git a/src/main/java/de/maishai/ast/records/FieldVarAccess.java b/src/main/java/de/maishai/ast/records/FieldVarAccess.java new file mode 100644 index 0000000..4b6e548 --- /dev/null +++ b/src/main/java/de/maishai/ast/records/FieldVarAccess.java @@ -0,0 +1,4 @@ +package de.maishai.ast.records; + +public record FieldVarAccess(Boolean field, Expression recursiveOwnerChain, String id) implements Expression { +} diff --git a/src/main/java/de/maishai/ast/records/Id.java b/src/main/java/de/maishai/ast/records/Id.java deleted file mode 100644 index cc3e8cf..0000000 --- a/src/main/java/de/maishai/ast/records/Id.java +++ /dev/null @@ -1,5 +0,0 @@ -package de.maishai.ast.records; - - -public record Id(String name) implements Expression { -} diff --git a/src/main/java/de/maishai/ast/records/LocalVariable.java b/src/main/java/de/maishai/ast/records/LocalVariable.java deleted file mode 100644 index 1edf284..0000000 --- a/src/main/java/de/maishai/ast/records/LocalVariable.java +++ /dev/null @@ -1,7 +0,0 @@ -package de.maishai.ast.records; - - -import de.maishai.ast.Type; - -public record LocalVariable(Id name, Type type) implements Node { -} diff --git a/src/main/java/de/maishai/ast/records/Method.java b/src/main/java/de/maishai/ast/records/Method.java index a11e9bc..3f6bd6a 100644 --- a/src/main/java/de/maishai/ast/records/Method.java +++ b/src/main/java/de/maishai/ast/records/Method.java @@ -2,9 +2,9 @@ package de.maishai.ast.records; -import de.maishai.ast.ReturnType; +import de.maishai.ast.Type; import java.util.List; -public record Method(ReturnType type, Id id, List params, Block block) implements Node { +public record Method(Type type, String methodName, List params, Block block) implements Node { } diff --git a/src/main/java/de/maishai/ast/records/MethodCall.java b/src/main/java/de/maishai/ast/records/MethodCall.java index 15f7d4d..fd37163 100644 --- a/src/main/java/de/maishai/ast/records/MethodCall.java +++ b/src/main/java/de/maishai/ast/records/MethodCall.java @@ -2,5 +2,5 @@ package de.maishai.ast.records; import java.util.List; -public record MethodCall(Boolean field, Expression recipient, Id id, List args) implements Expression, Statement { +public record MethodCall(FieldVarAccess recipient, List args) implements Expression, Statement { } diff --git a/src/main/java/de/maishai/ast/records/Node.java b/src/main/java/de/maishai/ast/records/Node.java index baf4bff..388c547 100644 --- a/src/main/java/de/maishai/ast/records/Node.java +++ b/src/main/java/de/maishai/ast/records/Node.java @@ -1,4 +1,4 @@ package de.maishai.ast.records; -public sealed interface Node permits Block, Class, Constructor, Expression, Field, LocalVariable, MainMethod, Method, Parameter, Statement { +public sealed interface Node permits Block, Class, Constructor, Expression, Declaration, MainMethod, Method, Parameter, Statement { } diff --git a/src/main/java/de/maishai/ast/records/ReturnVoid.java b/src/main/java/de/maishai/ast/records/ReturnVoid.java deleted file mode 100644 index b3d1f08..0000000 --- a/src/main/java/de/maishai/ast/records/ReturnVoid.java +++ /dev/null @@ -1,5 +0,0 @@ -package de.maishai.ast.records; - - -public record ReturnVoid() implements Statement { -} diff --git a/src/main/java/de/maishai/ast/records/Statement.java b/src/main/java/de/maishai/ast/records/Statement.java index 181c398..5a36534 100644 --- a/src/main/java/de/maishai/ast/records/Statement.java +++ b/src/main/java/de/maishai/ast/records/Statement.java @@ -1,4 +1,4 @@ package de.maishai.ast.records; -public sealed interface Statement extends Node permits Assignment, Break, DoWhile, For, IfElse, MethodCall, New, Return, ReturnVoid, While { +public sealed interface Statement extends Node permits Assignment, Break, DoWhile, For, IfElse, MethodCall, New, Return, While { } diff --git a/src/main/java/de/maishai/typedast/TypeMapper.java b/src/main/java/de/maishai/typedast/TypeMapper.java index 35a0227..d5fecaa 100644 --- a/src/main/java/de/maishai/typedast/TypeMapper.java +++ b/src/main/java/de/maishai/typedast/TypeMapper.java @@ -8,18 +8,7 @@ public class TypeMapper { return Type.BOOL; } else if (type == de.maishai.ast.Type.INT) { return Type.INT; - } - throw new RuntimeException("Unknown type"); - } - - public static Type getReturnType(de.maishai.ast.ReturnType type) { - if (type == de.maishai.ast.ReturnType.CHAR) { - return Type.CHAR; - } else if (type == de.maishai.ast.ReturnType.BOOL) { - return Type.BOOL; - } else if (type == de.maishai.ast.ReturnType.INT) { - return Type.INT; - } else if (type == de.maishai.ast.ReturnType.VOID) { + } else if (type == de.maishai.ast.Type.VOID){ return Type.VOID; } throw new RuntimeException("Unknown type"); diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java b/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java index 2804ae8..593adf0 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java @@ -1,6 +1,5 @@ package de.maishai.typedast.typedclass; -import de.maishai.ast.AssignSign; import de.maishai.ast.records.Assignment; import de.maishai.typedast.MethodContext; import de.maishai.typedast.TypedExpression; @@ -36,7 +35,7 @@ public class TypedAssignment implements TypedStatement { public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { Assignment untyped = (Assignment) unTypedAST; TypedAssignment typedAssignment = new TypedAssignment(); - typedAssignment.setVarName(untyped.loc().id().name()); + typedAssignment.setVarName(untyped.location().id()); typedAssignment.setValue((TypedExpression) value.convertToTypedAST(localVar, classes, untyped.value())); return typedAssignment; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java b/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java index 624f14f..935e536 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java @@ -1,7 +1,7 @@ package de.maishai.typedast.typedclass; import de.maishai.ast.records.Block; -import de.maishai.ast.records.LocalVariable; +import de.maishai.ast.records.Declaration; import de.maishai.typedast.MethodContext; import de.maishai.typedast.TypedNode; import de.maishai.typedast.TypedStatement; @@ -36,7 +36,7 @@ public class TypedBlock implements TypedNode { Block untyped = (Block) unTypedAST; TypedBlock typedBlock = new TypedBlock(); - for (LocalVariable var : untyped.vars()) { + for (Declaration var : untyped.localVariables()) { TypedLocalVariable typedVar = new TypedLocalVariable(); typedVar.convertToTypedAST(localVar, classes, var); typedBlock.getVars().add(typedVar); diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java b/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java index 97289e2..7b6d861 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java @@ -47,7 +47,7 @@ public class TypedConstructor implements TypedNode { public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { Constructor untyped = (Constructor) unTypedAST; TypedConstructor typedConstructor = new TypedConstructor(); - typedConstructor.setName(untyped.id().name()); + typedConstructor.setName(untyped.className()); if (untyped.params().isEmpty()) { typedConstructor.setTypedParameters(null); diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedField.java b/src/main/java/de/maishai/typedast/typedclass/TypedField.java index 93e3c7c..30ccae3 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedField.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedField.java @@ -1,6 +1,6 @@ package de.maishai.typedast.typedclass; -import de.maishai.ast.records.Field; +import de.maishai.ast.records.Declaration; import de.maishai.typedast.TypedNode; import de.maishai.typedast.Type; import de.maishai.typedast.TypeMapper; @@ -30,10 +30,10 @@ public class TypedField implements TypedNode { @Override public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { - Field untypedField = (Field) unTypedAST; + Declaration untypedDeclaration = (Declaration) unTypedAST; TypedField typedField = new TypedField(); - typedField.setVarName(untypedField.name().name()); - typedField.setType(TypeMapper.getType(untypedField.type())); + typedField.setVarName(untypedDeclaration.name()); + typedField.setType(TypeMapper.getType(untypedDeclaration.type())); return typedField; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedFieldId.java b/src/main/java/de/maishai/typedast/typedclass/TypedFieldId.java index 595022a..2822c01 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedFieldId.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedFieldId.java @@ -1,6 +1,6 @@ package de.maishai.typedast.typedclass; -import de.maishai.ast.records.FieldId; +import de.maishai.ast.records.FieldVarAccess; import de.maishai.typedast.TypedExpression; import de.maishai.typedast.TypedNode; import de.maishai.typedast.Type; @@ -14,7 +14,7 @@ public class TypedFieldId implements TypedExpression { private String name; @Override public Type typeCheck(Map localVar, Map classes) { - //TODO: Implement typeCheck for FieldId + //TODO: Implement typeCheck for FieldVarAccess if(field){ return null; }else{ @@ -24,11 +24,11 @@ public class TypedFieldId implements TypedExpression { @Override public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { - FieldId untypedFieldId = (FieldId) unTypedAST; + FieldVarAccess untypedFieldVarAccess = (FieldVarAccess) unTypedAST; TypedFieldId typedFieldId = new TypedFieldId(); - typedFieldId.setField(untypedFieldId.field()); - typedFieldId.setRecipient((TypedExpression) recipient.convertToTypedAST(localVar, classes, untypedFieldId.recipient())); - typedFieldId.setName(untypedFieldId.id().name()); + typedFieldId.setField(untypedFieldVarAccess.field()); + typedFieldId.setRecipient((TypedExpression) recipient.convertToTypedAST(localVar, classes, untypedFieldVarAccess.recursiveOwnerChain())); + typedFieldId.setName(untypedFieldVarAccess.id()); return typedFieldId; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java b/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java index e201020..df562ab 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java @@ -1,6 +1,7 @@ package de.maishai.typedast.typedclass; -import de.maishai.ast.records.LocalVariable; +import com.sun.jdi.LocalVariable; +import de.maishai.ast.records.Declaration; import de.maishai.typedast.MethodContext; import de.maishai.typedast.TypedNode; import de.maishai.typedast.Type; @@ -29,10 +30,10 @@ public final class TypedLocalVariable implements TypedNode { @Override public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { - LocalVariable untypedLocalVariable = (LocalVariable) unTypedAST; + Declaration untypedLocalVariable = (Declaration) unTypedAST; TypedLocalVariable typedLocalVariable = new TypedLocalVariable(); - typedLocalVariable.setName(untypedLocalVariable.name().name()); + typedLocalVariable.setName(untypedLocalVariable.name()); typedLocalVariable.setType(TypeMapper.getType(untypedLocalVariable.type())); return typedLocalVariable; diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java b/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java index 167258d..2951c9d 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java @@ -36,8 +36,8 @@ public class TypedMethod implements TypedNode { Method untypedMethod = (Method) unTypedAST; TypedMethod typedMethod = new TypedMethod(); - typedMethod.setName(untypedMethod.id().name()); - typedMethod.setReturnType(TypeMapper.getReturnType(untypedMethod.type())); + typedMethod.setName(untypedMethod.methodName()); + typedMethod.setReturnType(TypeMapper.getType(untypedMethod.type())); if(untypedMethod.params().isEmpty()){ //TODO: Implement this diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedReturnVoid.java b/src/main/java/de/maishai/typedast/typedclass/TypedReturnVoid.java index 3d9942e..cabcbcd 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedReturnVoid.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedReturnVoid.java @@ -20,7 +20,7 @@ public class TypedReturnVoid implements TypedStatement { @Override public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { Method untyped = (Method) unTypedAST; - if(untyped.type() != de.maishai.ast.ReturnType.VOID){ + if(untyped.type() != de.maishai.ast.Type.VOID){ throw new RuntimeException("Return type is not void"); } return new TypedReturnVoid(); diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructor.java b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructor.java index 14aee94..ce548e3 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructor.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructor.java @@ -20,8 +20,8 @@ import java.util.List; public class AbstractSyntax_ClassWithConstructor { public static Class get() { - List fields = List.of( - new Field( + List declarations = List.of( + new Declaration( new Id("x"), Type.INT ) @@ -30,7 +30,7 @@ public class AbstractSyntax_ClassWithConstructor { List constructors = getConstructors(); return new Class( new Id("ClassWithConstructor"), - fields, + declarations, methods, null, constructors diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorAndMethodCall.java b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorAndMethodCall.java index 627846f..e644748 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorAndMethodCall.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorAndMethodCall.java @@ -19,7 +19,6 @@ import de.maishai.ast.Operator; -import de.maishai.ast.ReturnType; import de.maishai.ast.Type; import de.maishai.ast.records.*; import de.maishai.ast.records.Class; @@ -28,8 +27,8 @@ import java.util.List; public class AbstractSyntax_ClassWithConstructorAndMethodCall { public static Class get() { - List fieldList = List.of( - new Field( + List declarationList = List.of( + new Declaration( new Id("x"), Type.INT ) @@ -38,7 +37,7 @@ public class AbstractSyntax_ClassWithConstructorAndMethodCall { List constructorList = getConstructors(); return new Class( new Id("ClassWithConstructorAndMethodCall"), - fieldList, + declarationList, methodList, null, constructorList diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorWithParameters.java b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorWithParameters.java index 2652d37..57fb4f3 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorWithParameters.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorWithParameters.java @@ -22,8 +22,8 @@ import java.util.List; public class AbstractSyntax_ClassWithConstructorWithParameters { public static Class get() { - List fields = List.of( - new Field( + List declarations = List.of( + new Declaration( new Id("x"), Type.INT ) @@ -31,7 +31,7 @@ public class AbstractSyntax_ClassWithConstructorWithParameters { List constructors = getConstructors(); return new Class( new Id("ClassWithConstructorWithParameters"), - fields, + declarations, List.of(), null, constructors diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithField.java b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithField.java index 7b763b1..662c943 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithField.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithField.java @@ -5,16 +5,15 @@ import de.maishai.ast.records.Class; import de.maishai.ast.Type; import de.maishai.ast.records.Constructor; -import de.maishai.ast.records.Field; -import de.maishai.ast.records.Id; +import de.maishai.ast.records.Declaration; import de.maishai.ast.records.Method; import java.util.List; public class AbstractSyntax_ClassWithField { public static Class get() { - List fields = List.of( - new Field( + List declarations = List.of( + new Declaration( new Id("x"), Type.INT ) @@ -23,7 +22,7 @@ public class AbstractSyntax_ClassWithField { List constructors = List.of(); return new Class( new Id("ClassWithAssignment"), - fields, + declarations, methods, null, constructors diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMethod.java b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMethod.java index c074149..07e76b5 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMethod.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMethod.java @@ -4,7 +4,6 @@ // } //} -import de.maishai.ast.ReturnType; import de.maishai.ast.records.Class; import de.maishai.ast.records.*; diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMethodAndField.java b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMethodAndField.java index ff54632..c05eff0 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMethodAndField.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMethodAndField.java @@ -18,8 +18,8 @@ import java.util.List; class AbstractSyntax_ClassWithMethodAndField { public static Class get() { - List fields = List.of( - new Field( + List declarations = List.of( + new Declaration( new Id("c"), Type.CHAR ) @@ -28,7 +28,7 @@ class AbstractSyntax_ClassWithMethodAndField { List constructors = getConstructors(); return new Class( new Id("ClassWithMethodAndField"), - fields, + declarations, methods, null, constructors diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMoreComplexMethodAndMain.java b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMoreComplexMethodAndMain.java index 4232798..25b8d3e 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMoreComplexMethodAndMain.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMoreComplexMethodAndMain.java @@ -28,8 +28,8 @@ public class AbstractSyntax_ClassWithMoreComplexMethodAndMain { public static Class get() { Type TypeClassWithMoreComplexMethodAndMain = Type.OBJECT; TypeClassWithMoreComplexMethodAndMain.setObjectType(new Id("ClassWithMoreComplexMethodAndMain")); - List fields = List.of( - new Field( + List declarations = List.of( + new Declaration( new Id("instance"), TypeClassWithMoreComplexMethodAndMain ) @@ -38,7 +38,7 @@ public class AbstractSyntax_ClassWithMoreComplexMethodAndMain { List constructors = List.of(); return new Class( new Id("ClassWithMoreComplexMethodAndMain"), - fields, + declarations, methods, getMainMethod(), constructors diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_PublicClass.java b/src/main/resources/AbstractSyntax/AbstractSyntax_PublicClass.java index 8f8165c..5b725d8 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_PublicClass.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_PublicClass.java @@ -2,7 +2,6 @@ //} import de.maishai.ast.records.Class; -import de.maishai.ast.records.Id; import java.util.List; diff --git a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructor.java b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructor.java index 9fd103f..1030c88 100644 --- a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructor.java +++ b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructor.java @@ -12,7 +12,6 @@ // } //} -import de.maishai.ast.AssignSign; import de.maishai.ast.Operator; import de.maishai.typedast.Type; import de.maishai.typedast.typedclass.*; diff --git a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructorAndMethodCall.java b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructorAndMethodCall.java index 9d20841..89cf824 100644 --- a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructorAndMethodCall.java +++ b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructorAndMethodCall.java @@ -18,19 +18,10 @@ //} -import de.maishai.ast.AssignSign; -import de.maishai.ast.Operator; -import de.maishai.ast.ReturnType; -import de.maishai.ast.Type; -import de.maishai.ast.records.*; -import de.maishai.ast.records.Class; - -import java.util.List; - public class TypedAbstractSyntax_ClassWithConstructorAndMethodCall { // public static Class get() { -// List fieldList = List.of( -// new Field( +// List fieldList = List.of( +// new Declaration( // new Id("x"), // Type.INT // ) diff --git a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructorWithParameters.java b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructorWithParameters.java index 5f073d5..3042614 100644 --- a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructorWithParameters.java +++ b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructorWithParameters.java @@ -13,7 +13,6 @@ // } // } //} -import de.maishai.ast.AssignSign; import de.maishai.ast.Operator; import de.maishai.ast.records.*; import de.maishai.ast.Type; @@ -23,8 +22,8 @@ import java.util.List; public class TypedAbstractSyntax_ClassWithConstructorWithParameters { // public static Class get() { -// List fields = List.of( -// new Field( +// List fieldDeclarations = List.of( +// new Declaration( // new Id("x"), // Type.INT // ) @@ -33,7 +32,7 @@ public class TypedAbstractSyntax_ClassWithConstructorWithParameters { // return new Class( // true, // new Id("ClassWithConstructorWithParameters"), -// fields, +// fieldDeclarations, // List.of(), // null, // constructors diff --git a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithMethodAndField.java b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithMethodAndField.java index d74f623..97bf34b 100644 --- a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithMethodAndField.java +++ b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithMethodAndField.java @@ -18,8 +18,8 @@ import java.util.List; class TypedAbstractSyntax_ClassWithMethodAndField { // public static Class get() { -// List fields = List.of( -// new Field( +// List fieldDeclarations = List.of( +// new Declaration( // new Id("c"), // Type.CHAR // ) @@ -29,7 +29,7 @@ class TypedAbstractSyntax_ClassWithMethodAndField { // return new Class( // false, // new Id("ClassWithMethodAndField"), -// fields, +// fieldDeclarations, // methods, // null, // constructors diff --git a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithMoreComplexMethodAndMain.java b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithMoreComplexMethodAndMain.java index d3682fd..3c41ad9 100644 --- a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithMoreComplexMethodAndMain.java +++ b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithMoreComplexMethodAndMain.java @@ -28,8 +28,8 @@ public class TypedAbstractSyntax_ClassWithMoreComplexMethodAndMain { // public static Class get() { // Type TypeClassWithMoreComplexMethodAndMain = Type.OBJECT; // TypeClassWithMoreComplexMethodAndMain.setObjectType(new Id("ClassWithMoreComplexMethodAndMain")); -// List fields = List.of( -// new Field( +// List fieldDeclarations = List.of( +// new Declaration( // new Id("instance"), // TypeClassWithMoreComplexMethodAndMain // ) @@ -39,7 +39,7 @@ public class TypedAbstractSyntax_ClassWithMoreComplexMethodAndMain { // return new Class( // true, // new Id("ClassWithMoreComplexMethodAndMain"), -// fields, +// fieldDeclarations, // methods, // getMainMethod(), // constructors From 5d44fc661ff5b55caf3dfe3241b306cf69735773 Mon Sep 17 00:00:00 2001 From: laurenz Date: Wed, 8 May 2024 14:53:01 +0200 Subject: [PATCH 5/6] removed main method from ast, constructors are always public --- src/main/antlr/Decaf.g4 | 2 +- src/main/java/de/maishai/ASTGenerator.java | 14 ++------- src/main/java/de/maishai/Compiler.java | 16 ++++++++-- .../java/de/maishai/ast/records/Class.java | 2 +- .../de/maishai/ast/records/Constructor.java | 2 +- .../de/maishai/ast/records/MainMethod.java | 5 --- .../java/de/maishai/ast/records/Node.java | 2 +- .../typedast/typedclass/TypedClass.java | 1 - .../typedast/typedclass/TypedMainMethod.java | 31 ------------------- 9 files changed, 19 insertions(+), 56 deletions(-) delete mode 100644 src/main/java/de/maishai/ast/records/MainMethod.java delete mode 100644 src/main/java/de/maishai/typedast/typedclass/TypedMainMethod.java diff --git a/src/main/antlr/Decaf.g4 b/src/main/antlr/Decaf.g4 index f379921..65e25e7 100644 --- a/src/main/antlr/Decaf.g4 +++ b/src/main/antlr/Decaf.g4 @@ -11,7 +11,7 @@ type : INT | BOOL | CHAR | id; meth : PUBLIC returntype id '(' params? ')' block; mainmeth : PUBLIC 'static' 'void' 'main' '(' 'String[] args' ')' block; -constructor: PUBLIC? id '(' params? ')' block; +constructor: PUBLIC id '(' params? ')' block; params : param (',' param)*; param : type id; diff --git a/src/main/java/de/maishai/ASTGenerator.java b/src/main/java/de/maishai/ASTGenerator.java index ff7fbf9..de83204 100644 --- a/src/main/java/de/maishai/ASTGenerator.java +++ b/src/main/java/de/maishai/ASTGenerator.java @@ -16,10 +16,6 @@ public class ASTGenerator { if (ctx.field() != null) { declarations = ctx.field().stream().map(ASTGenerator::generateFieldVariable).toList(); } - MainMethod mainMethod = null; - if(ctx.mainmeth() != null){ - mainMethod = generateMainMethod(ctx.mainmeth()); - } List constructors = new ArrayList<>(); if (ctx.constructor() != null) { constructors = ctx.constructor().stream().map(ASTGenerator::generateConstructor).toList(); @@ -28,7 +24,7 @@ public class ASTGenerator { if (ctx.meth() != null) { meths = ctx.meth().stream().map(ASTGenerator::generateMethod).toList(); } - return new Class(ctx.id().getText(), declarations, meths, mainMethod, constructors); + return new Class(ctx.id().getText(), declarations, meths, constructors); } public static Declaration generateFieldVariable(DecafParser.FieldContext ctx) { @@ -50,18 +46,12 @@ public class ASTGenerator { } public static Constructor generateConstructor(DecafParser.ConstructorContext ctx) { - Boolean isPublic = ctx.PUBLIC() != null; List params = new ArrayList<>(); if (ctx.params() != null) { params = ctx.params().param().stream().map(ASTGenerator::generateParameter).toList(); } Block block = new BlockGenerator().visit(ctx.block()); - return new Constructor(isPublic, ctx.id().getText(), params, block); - } - - public static MainMethod generateMainMethod(DecafParser.MainmethContext ctx) { - Block block = new BlockGenerator().visit(ctx.block()); - return new MainMethod(block); + return new Constructor( ctx.id().getText(), params, block); } public static Type getType(DecafParser.TypeContext ctx) { diff --git a/src/main/java/de/maishai/Compiler.java b/src/main/java/de/maishai/Compiler.java index 92e75fc..a992bb2 100644 --- a/src/main/java/de/maishai/Compiler.java +++ b/src/main/java/de/maishai/Compiler.java @@ -15,6 +15,16 @@ import java.io.IOException; */ public class Compiler { + public static void main(String[] args) { + generateAST(""" + public class E2ETests { + public static void main(String[] args) { + + } + } + """); + } + public static Class generateAST(String fromSource) { CharStream input = CharStreams.fromString(fromSource); DecafLexer lexer = new DecafLexer(input); @@ -63,7 +73,7 @@ public class Compiler { CodeGenUtils.writeClassfile(bytes, classname); } - public static void main(String[] args) { - generateByteCodeFileFromFile("src/main/resources/JavaTestfiles/PublicClass.java", "OnlyClass"); - } +// public static void main(String[] args) { +// generateByteCodeFileFromFile("src/main/resources/JavaTestfiles/PublicClass.java", "OnlyClass"); +// } } diff --git a/src/main/java/de/maishai/ast/records/Class.java b/src/main/java/de/maishai/ast/records/Class.java index eb9b5a1..4f5d93a 100644 --- a/src/main/java/de/maishai/ast/records/Class.java +++ b/src/main/java/de/maishai/ast/records/Class.java @@ -2,5 +2,5 @@ package de.maishai.ast.records; import java.util.List; -public record Class(String classname , List fieldDeclarations, List methods, MainMethod mainMethod, List constructors) implements Node { +public record Class(String classname , List fieldDeclarations, List methods, List constructors) implements Node { } diff --git a/src/main/java/de/maishai/ast/records/Constructor.java b/src/main/java/de/maishai/ast/records/Constructor.java index 2c4b5e6..d7f40c6 100644 --- a/src/main/java/de/maishai/ast/records/Constructor.java +++ b/src/main/java/de/maishai/ast/records/Constructor.java @@ -2,5 +2,5 @@ package de.maishai.ast.records; import java.util.List; -public record Constructor(Boolean isPublic, String className, List params, Block block) implements Node { +public record Constructor(String className, List params, Block block) implements Node { } diff --git a/src/main/java/de/maishai/ast/records/MainMethod.java b/src/main/java/de/maishai/ast/records/MainMethod.java deleted file mode 100644 index fdeedb1..0000000 --- a/src/main/java/de/maishai/ast/records/MainMethod.java +++ /dev/null @@ -1,5 +0,0 @@ -package de.maishai.ast.records; - - -public record MainMethod(Block block) implements Node { -} diff --git a/src/main/java/de/maishai/ast/records/Node.java b/src/main/java/de/maishai/ast/records/Node.java index 388c547..2e3cf76 100644 --- a/src/main/java/de/maishai/ast/records/Node.java +++ b/src/main/java/de/maishai/ast/records/Node.java @@ -1,4 +1,4 @@ package de.maishai.ast.records; -public sealed interface Node permits Block, Class, Constructor, Expression, Declaration, MainMethod, Method, Parameter, Statement { +public sealed interface Node permits Block, Class, Constructor, Expression, Declaration, Method, Parameter, Statement { } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedClass.java b/src/main/java/de/maishai/typedast/typedclass/TypedClass.java index 6bb3477..4af9288 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedClass.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedClass.java @@ -18,7 +18,6 @@ public class TypedClass implements TypedNode { private String className; private List typedFields; private List typedMethods; - private TypedMainMethod typedMainMethod; private List typedConstructors; @Override diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedMainMethod.java b/src/main/java/de/maishai/typedast/typedclass/TypedMainMethod.java deleted file mode 100644 index 1c60d7a..0000000 --- a/src/main/java/de/maishai/typedast/typedclass/TypedMainMethod.java +++ /dev/null @@ -1,31 +0,0 @@ -package de.maishai.typedast.typedclass; - -import de.maishai.ast.records.MainMethod; -import de.maishai.typedast.TypedNode; -import de.maishai.typedast.Type; -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; - -import java.util.Map; - -@Data -@AllArgsConstructor -@NoArgsConstructor -public class TypedMainMethod implements TypedNode { - private TypedBlock typedBlock; - @Override - public Type typeCheck(Map localVar, Map classes) { - return typedBlock.typeCheck(localVar, classes); - } - - @Override - public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { - MainMethod untyped = (MainMethod) unTypedAST; - TypedMainMethod typedMainMethod = new TypedMainMethod(); - typedMainMethod.setTypedBlock((TypedBlock) typedBlock.convertToTypedAST(localVar, classes, untyped.block())); - - return typedMainMethod; - } -} - From 4c34a8db4498747c4e1753d1e50891d3dc94f812 Mon Sep 17 00:00:00 2001 From: laurenz Date: Wed, 8 May 2024 15:00:46 +0200 Subject: [PATCH 6/6] removed ast.Type in favor of typedast.Type --- src/main/java/de/maishai/ASTGenerator.java | 6 ++--- src/main/java/de/maishai/Compiler.java | 16 +++--------- .../java/de/maishai/ExpressionGenerator.java | 2 +- .../java/de/maishai/StatementGenerator.java | 2 +- .../java/de/maishai/VariableGenerator.java | 2 +- src/main/java/de/maishai/ast/Type.java | 26 ------------------- .../de/maishai/ast/records/Declaration.java | 2 +- .../java/de/maishai/ast/records/Method.java | 2 +- src/main/java/de/maishai/ast/records/New.java | 2 +- .../de/maishai/ast/records/Parameter.java | 2 +- .../java/de/maishai/typedast/TypeMapper.java | 17 ------------ .../typedast/typedclass/TypedField.java | 3 +-- .../typedclass/TypedLocalVariable.java | 4 +-- .../typedast/typedclass/TypedMethod.java | 8 +++--- .../maishai/typedast/typedclass/TypedNew.java | 2 +- .../typedast/typedclass/TypedReturnVoid.java | 2 +- ...tax_ClassWithConstructorAndMethodCall.java | 1 - ...ax_ClassWithConstructorWithParameters.java | 1 - .../AbstractSyntax_ClassWithField.java | 1 - ...ax_ClassWithConstructorWithParameters.java | 6 ----- 20 files changed, 20 insertions(+), 87 deletions(-) delete mode 100644 src/main/java/de/maishai/ast/Type.java delete mode 100644 src/main/java/de/maishai/typedast/TypeMapper.java diff --git a/src/main/java/de/maishai/ASTGenerator.java b/src/main/java/de/maishai/ASTGenerator.java index de83204..1192347 100644 --- a/src/main/java/de/maishai/ASTGenerator.java +++ b/src/main/java/de/maishai/ASTGenerator.java @@ -1,9 +1,9 @@ package de.maishai; import de.maishai.antlr.DecafParser; -import de.maishai.ast.Type; import de.maishai.ast.records.*; import de.maishai.ast.records.Class; +import de.maishai.typedast.Type; import java.util.ArrayList; import java.util.List; @@ -62,9 +62,7 @@ public class ASTGenerator { if (ctx.CHAR() != null) return Type.CHAR; if (ctx.id() != null) { - Type type = Type.OBJECT; - type.setObjectType(ctx.id().getText()); - return type; + return Type.REFERENCE(ctx.id().getText()); } throw new RuntimeException("No type found!"); } diff --git a/src/main/java/de/maishai/Compiler.java b/src/main/java/de/maishai/Compiler.java index a992bb2..92e75fc 100644 --- a/src/main/java/de/maishai/Compiler.java +++ b/src/main/java/de/maishai/Compiler.java @@ -15,16 +15,6 @@ import java.io.IOException; */ public class Compiler { - public static void main(String[] args) { - generateAST(""" - public class E2ETests { - public static void main(String[] args) { - - } - } - """); - } - public static Class generateAST(String fromSource) { CharStream input = CharStreams.fromString(fromSource); DecafLexer lexer = new DecafLexer(input); @@ -73,7 +63,7 @@ public class Compiler { CodeGenUtils.writeClassfile(bytes, classname); } -// public static void main(String[] args) { -// generateByteCodeFileFromFile("src/main/resources/JavaTestfiles/PublicClass.java", "OnlyClass"); -// } + public static void main(String[] args) { + generateByteCodeFileFromFile("src/main/resources/JavaTestfiles/PublicClass.java", "OnlyClass"); + } } diff --git a/src/main/java/de/maishai/ExpressionGenerator.java b/src/main/java/de/maishai/ExpressionGenerator.java index d723a1c..aeb4419 100644 --- a/src/main/java/de/maishai/ExpressionGenerator.java +++ b/src/main/java/de/maishai/ExpressionGenerator.java @@ -5,8 +5,8 @@ import de.maishai.antlr.DecafParser; import de.maishai.ast.UnaryOperator; import de.maishai.ast.records.Expression; import de.maishai.ast.Operator; -import de.maishai.ast.Type; import de.maishai.ast.records.*; +import de.maishai.typedast.Type; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/de/maishai/StatementGenerator.java b/src/main/java/de/maishai/StatementGenerator.java index 6b4978f..a0997d9 100644 --- a/src/main/java/de/maishai/StatementGenerator.java +++ b/src/main/java/de/maishai/StatementGenerator.java @@ -5,8 +5,8 @@ import de.maishai.antlr.DecafParser; import de.maishai.ast.Operator; import de.maishai.ast.records.Expression; import de.maishai.ast.records.Statement; -import de.maishai.ast.Type; import de.maishai.ast.records.*; +import de.maishai.typedast.Type; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/de/maishai/VariableGenerator.java b/src/main/java/de/maishai/VariableGenerator.java index 70d0dbe..99808cc 100644 --- a/src/main/java/de/maishai/VariableGenerator.java +++ b/src/main/java/de/maishai/VariableGenerator.java @@ -3,8 +3,8 @@ package de.maishai; import de.maishai.antlr.DecafBaseVisitor; import de.maishai.antlr.DecafParser; -import de.maishai.ast.Type; import de.maishai.ast.records.Declaration; +import de.maishai.typedast.Type; public class VariableGenerator extends DecafBaseVisitor { diff --git a/src/main/java/de/maishai/ast/Type.java b/src/main/java/de/maishai/ast/Type.java deleted file mode 100644 index e4a3b86..0000000 --- a/src/main/java/de/maishai/ast/Type.java +++ /dev/null @@ -1,26 +0,0 @@ -package de.maishai.ast; - -public enum Type { - INT, - BOOL, - CHAR, - VOID, - OBJECT; - - String objectType; - - public String getObjectType() { - if(this == Type.OBJECT){ - return objectType; - } - return null; - } - - public void setObjectType(String objectType) { - if(this == Type.OBJECT){ - this.objectType = objectType; - } - } - - -} diff --git a/src/main/java/de/maishai/ast/records/Declaration.java b/src/main/java/de/maishai/ast/records/Declaration.java index 248b9b1..4cb300e 100644 --- a/src/main/java/de/maishai/ast/records/Declaration.java +++ b/src/main/java/de/maishai/ast/records/Declaration.java @@ -1,6 +1,6 @@ package de.maishai.ast.records; -import de.maishai.ast.Type; +import de.maishai.typedast.Type; public record Declaration(String name, Type type) implements Node { } diff --git a/src/main/java/de/maishai/ast/records/Method.java b/src/main/java/de/maishai/ast/records/Method.java index 3f6bd6a..81e5336 100644 --- a/src/main/java/de/maishai/ast/records/Method.java +++ b/src/main/java/de/maishai/ast/records/Method.java @@ -2,7 +2,7 @@ package de.maishai.ast.records; -import de.maishai.ast.Type; +import de.maishai.typedast.Type; import java.util.List; diff --git a/src/main/java/de/maishai/ast/records/New.java b/src/main/java/de/maishai/ast/records/New.java index 5a9ec9e..1077324 100644 --- a/src/main/java/de/maishai/ast/records/New.java +++ b/src/main/java/de/maishai/ast/records/New.java @@ -2,7 +2,7 @@ package de.maishai.ast.records; -import de.maishai.ast.Type; +import de.maishai.typedast.Type; import java.util.List; diff --git a/src/main/java/de/maishai/ast/records/Parameter.java b/src/main/java/de/maishai/ast/records/Parameter.java index 5cbe5a1..3998293 100644 --- a/src/main/java/de/maishai/ast/records/Parameter.java +++ b/src/main/java/de/maishai/ast/records/Parameter.java @@ -1,7 +1,7 @@ package de.maishai.ast.records; -import de.maishai.ast.Type; +import de.maishai.typedast.Type; public record Parameter(String name, Type type) implements Node { } diff --git a/src/main/java/de/maishai/typedast/TypeMapper.java b/src/main/java/de/maishai/typedast/TypeMapper.java deleted file mode 100644 index d5fecaa..0000000 --- a/src/main/java/de/maishai/typedast/TypeMapper.java +++ /dev/null @@ -1,17 +0,0 @@ -package de.maishai.typedast; - -public class TypeMapper { - public static Type getType(de.maishai.ast.Type type) { - if (type == de.maishai.ast.Type.CHAR) { - return Type.CHAR; - } else if (type == de.maishai.ast.Type.BOOL) { - return Type.BOOL; - } else if (type == de.maishai.ast.Type.INT) { - return Type.INT; - } else if (type == de.maishai.ast.Type.VOID){ - return Type.VOID; - } - throw new RuntimeException("Unknown type"); - } - -} \ No newline at end of file diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedField.java b/src/main/java/de/maishai/typedast/typedclass/TypedField.java index 30ccae3..411a800 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedField.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedField.java @@ -3,7 +3,6 @@ package de.maishai.typedast.typedclass; import de.maishai.ast.records.Declaration; import de.maishai.typedast.TypedNode; import de.maishai.typedast.Type; -import de.maishai.typedast.TypeMapper; import lombok.AllArgsConstructor; import lombok.Data; import lombok.RequiredArgsConstructor; @@ -33,7 +32,7 @@ public class TypedField implements TypedNode { Declaration untypedDeclaration = (Declaration) unTypedAST; TypedField typedField = new TypedField(); typedField.setVarName(untypedDeclaration.name()); - typedField.setType(TypeMapper.getType(untypedDeclaration.type())); + typedField.setType(untypedDeclaration.type()); return typedField; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java b/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java index df562ab..bb5b483 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java @@ -1,11 +1,9 @@ package de.maishai.typedast.typedclass; -import com.sun.jdi.LocalVariable; import de.maishai.ast.records.Declaration; import de.maishai.typedast.MethodContext; import de.maishai.typedast.TypedNode; import de.maishai.typedast.Type; -import de.maishai.typedast.TypeMapper; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @@ -34,7 +32,7 @@ public final class TypedLocalVariable implements TypedNode { TypedLocalVariable typedLocalVariable = new TypedLocalVariable(); typedLocalVariable.setName(untypedLocalVariable.name()); - typedLocalVariable.setType(TypeMapper.getType(untypedLocalVariable.type())); + typedLocalVariable.setType(untypedLocalVariable.type()); return typedLocalVariable; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java b/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java index 2951c9d..c23f5e2 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java @@ -37,7 +37,7 @@ public class TypedMethod implements TypedNode { TypedMethod typedMethod = new TypedMethod(); typedMethod.setName(untypedMethod.methodName()); - typedMethod.setReturnType(TypeMapper.getType(untypedMethod.type())); + typedMethod.setReturnType(untypedMethod.type()); if(untypedMethod.params().isEmpty()){ //TODO: Implement this @@ -58,13 +58,13 @@ public class TypedMethod implements TypedNode { TypedParameter typedParameter = new TypedParameter(); typedParameter.setParaName(parameter.name()); - if(parameter.type() == de.maishai.ast.Type.CHAR){ + if(parameter.type() == Type.CHAR){ typedParameter.setType(Type.CHAR); } - if(parameter.type() == de.maishai.ast.Type.BOOL){ + if(parameter.type() == Type.BOOL){ typedParameter.setType(Type.BOOL); } - if(parameter.type() == de.maishai.ast.Type.INT){ + if(parameter.type() == Type.INT){ typedParameter.setType(Type.INT); } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedNew.java b/src/main/java/de/maishai/typedast/typedclass/TypedNew.java index 20760ed..40fe0c8 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedNew.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedNew.java @@ -38,7 +38,7 @@ public class TypedNew implements TypedExpression, TypedStatement { New untyped = (New) unTypedAST; TypedNew typedNew = new TypedNew(); - typedNew.setType(TypeMapper.getType(untyped.type())); + typedNew.setType(untyped.type()); if(untyped.args().isEmpty()){ //TODO: Implement this diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedReturnVoid.java b/src/main/java/de/maishai/typedast/typedclass/TypedReturnVoid.java index cabcbcd..0032d57 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedReturnVoid.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedReturnVoid.java @@ -20,7 +20,7 @@ public class TypedReturnVoid implements TypedStatement { @Override public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { Method untyped = (Method) unTypedAST; - if(untyped.type() != de.maishai.ast.Type.VOID){ + if(untyped.type() != Type.VOID){ throw new RuntimeException("Return type is not void"); } return new TypedReturnVoid(); diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorAndMethodCall.java b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorAndMethodCall.java index 3514a81..6393665 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorAndMethodCall.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorAndMethodCall.java @@ -19,7 +19,6 @@ import de.maishai.ast.Operator; -import de.maishai.ast.Type; import de.maishai.ast.records.*; import de.maishai.ast.records.Class; diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorWithParameters.java b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorWithParameters.java index eba9cea..dc41211 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorWithParameters.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorWithParameters.java @@ -15,7 +15,6 @@ //} import de.maishai.ast.Operator; import de.maishai.ast.records.*; -import de.maishai.ast.Type; import de.maishai.ast.records.Class; import java.util.List; diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithField.java b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithField.java index 662c943..6d2be52 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithField.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithField.java @@ -3,7 +3,6 @@ //} import de.maishai.ast.records.Class; -import de.maishai.ast.Type; import de.maishai.ast.records.Constructor; import de.maishai.ast.records.Declaration; import de.maishai.ast.records.Method; diff --git a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructorWithParameters.java b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructorWithParameters.java index 3042614..706463f 100644 --- a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructorWithParameters.java +++ b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructorWithParameters.java @@ -13,12 +13,6 @@ // } // } //} -import de.maishai.ast.Operator; -import de.maishai.ast.records.*; -import de.maishai.ast.Type; -import de.maishai.ast.records.Class; - -import java.util.List; public class TypedAbstractSyntax_ClassWithConstructorWithParameters { // public static Class get() {