From 4c34a8db4498747c4e1753d1e50891d3dc94f812 Mon Sep 17 00:00:00 2001 From: laurenz Date: Wed, 8 May 2024 15:00:46 +0200 Subject: [PATCH] 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() {