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