removed ast.Type in favor of typedast.Type

This commit is contained in:
laurenz 2024-05-08 15:00:46 +02:00
parent 5d44fc661f
commit 4c34a8db44
20 changed files with 20 additions and 87 deletions

View File

@ -1,9 +1,9 @@
package de.maishai; package de.maishai;
import de.maishai.antlr.DecafParser; import de.maishai.antlr.DecafParser;
import de.maishai.ast.Type;
import de.maishai.ast.records.*; import de.maishai.ast.records.*;
import de.maishai.ast.records.Class; import de.maishai.ast.records.Class;
import de.maishai.typedast.Type;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -62,9 +62,7 @@ public class ASTGenerator {
if (ctx.CHAR() != null) if (ctx.CHAR() != null)
return Type.CHAR; return Type.CHAR;
if (ctx.id() != null) { if (ctx.id() != null) {
Type type = Type.OBJECT; return Type.REFERENCE(ctx.id().getText());
type.setObjectType(ctx.id().getText());
return type;
} }
throw new RuntimeException("No type found!"); throw new RuntimeException("No type found!");
} }

View File

@ -15,16 +15,6 @@ import java.io.IOException;
*/ */
public class Compiler { 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) { public static Class generateAST(String fromSource) {
CharStream input = CharStreams.fromString(fromSource); CharStream input = CharStreams.fromString(fromSource);
DecafLexer lexer = new DecafLexer(input); DecafLexer lexer = new DecafLexer(input);
@ -73,7 +63,7 @@ public class Compiler {
CodeGenUtils.writeClassfile(bytes, classname); CodeGenUtils.writeClassfile(bytes, classname);
} }
// public static void main(String[] args) { public static void main(String[] args) {
// generateByteCodeFileFromFile("src/main/resources/JavaTestfiles/PublicClass.java", "OnlyClass"); generateByteCodeFileFromFile("src/main/resources/JavaTestfiles/PublicClass.java", "OnlyClass");
// } }
} }

View File

@ -5,8 +5,8 @@ import de.maishai.antlr.DecafParser;
import de.maishai.ast.UnaryOperator; import de.maishai.ast.UnaryOperator;
import de.maishai.ast.records.Expression; import de.maishai.ast.records.Expression;
import de.maishai.ast.Operator; import de.maishai.ast.Operator;
import de.maishai.ast.Type;
import de.maishai.ast.records.*; import de.maishai.ast.records.*;
import de.maishai.typedast.Type;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;

View File

@ -5,8 +5,8 @@ import de.maishai.antlr.DecafParser;
import de.maishai.ast.Operator; import de.maishai.ast.Operator;
import de.maishai.ast.records.Expression; import de.maishai.ast.records.Expression;
import de.maishai.ast.records.Statement; import de.maishai.ast.records.Statement;
import de.maishai.ast.Type;
import de.maishai.ast.records.*; import de.maishai.ast.records.*;
import de.maishai.typedast.Type;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;

View File

@ -3,8 +3,8 @@ package de.maishai;
import de.maishai.antlr.DecafBaseVisitor; import de.maishai.antlr.DecafBaseVisitor;
import de.maishai.antlr.DecafParser; import de.maishai.antlr.DecafParser;
import de.maishai.ast.Type;
import de.maishai.ast.records.Declaration; import de.maishai.ast.records.Declaration;
import de.maishai.typedast.Type;
public class VariableGenerator extends DecafBaseVisitor<Declaration> { public class VariableGenerator extends DecafBaseVisitor<Declaration> {

View File

@ -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;
}
}
}

View File

@ -1,6 +1,6 @@
package de.maishai.ast.records; package de.maishai.ast.records;
import de.maishai.ast.Type; import de.maishai.typedast.Type;
public record Declaration(String name, Type type) implements Node { public record Declaration(String name, Type type) implements Node {
} }

View File

@ -2,7 +2,7 @@ package de.maishai.ast.records;
import de.maishai.ast.Type; import de.maishai.typedast.Type;
import java.util.List; import java.util.List;

View File

@ -2,7 +2,7 @@ package de.maishai.ast.records;
import de.maishai.ast.Type; import de.maishai.typedast.Type;
import java.util.List; import java.util.List;

View File

@ -1,7 +1,7 @@
package de.maishai.ast.records; package de.maishai.ast.records;
import de.maishai.ast.Type; import de.maishai.typedast.Type;
public record Parameter(String name, Type type) implements Node { public record Parameter(String name, Type type) implements Node {
} }

View File

@ -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");
}
}

View File

@ -3,7 +3,6 @@ package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Declaration; import de.maishai.ast.records.Declaration;
import de.maishai.typedast.TypedNode; import de.maishai.typedast.TypedNode;
import de.maishai.typedast.Type; import de.maishai.typedast.Type;
import de.maishai.typedast.TypeMapper;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@ -33,7 +32,7 @@ public class TypedField implements TypedNode {
Declaration untypedDeclaration = (Declaration) unTypedAST; Declaration untypedDeclaration = (Declaration) unTypedAST;
TypedField typedField = new TypedField(); TypedField typedField = new TypedField();
typedField.setVarName(untypedDeclaration.name()); typedField.setVarName(untypedDeclaration.name());
typedField.setType(TypeMapper.getType(untypedDeclaration.type())); typedField.setType(untypedDeclaration.type());
return typedField; return typedField;
} }

View File

@ -1,11 +1,9 @@
package de.maishai.typedast.typedclass; package de.maishai.typedast.typedclass;
import com.sun.jdi.LocalVariable;
import de.maishai.ast.records.Declaration; import de.maishai.ast.records.Declaration;
import de.maishai.typedast.MethodContext; import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedNode; import de.maishai.typedast.TypedNode;
import de.maishai.typedast.Type; import de.maishai.typedast.Type;
import de.maishai.typedast.TypeMapper;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
@ -34,7 +32,7 @@ public final class TypedLocalVariable implements TypedNode {
TypedLocalVariable typedLocalVariable = new TypedLocalVariable(); TypedLocalVariable typedLocalVariable = new TypedLocalVariable();
typedLocalVariable.setName(untypedLocalVariable.name()); typedLocalVariable.setName(untypedLocalVariable.name());
typedLocalVariable.setType(TypeMapper.getType(untypedLocalVariable.type())); typedLocalVariable.setType(untypedLocalVariable.type());
return typedLocalVariable; return typedLocalVariable;
} }

View File

@ -37,7 +37,7 @@ public class TypedMethod implements TypedNode {
TypedMethod typedMethod = new TypedMethod(); TypedMethod typedMethod = new TypedMethod();
typedMethod.setName(untypedMethod.methodName()); typedMethod.setName(untypedMethod.methodName());
typedMethod.setReturnType(TypeMapper.getType(untypedMethod.type())); typedMethod.setReturnType(untypedMethod.type());
if(untypedMethod.params().isEmpty()){ if(untypedMethod.params().isEmpty()){
//TODO: Implement this //TODO: Implement this
@ -58,13 +58,13 @@ public class TypedMethod implements TypedNode {
TypedParameter typedParameter = new TypedParameter(); TypedParameter typedParameter = new TypedParameter();
typedParameter.setParaName(parameter.name()); typedParameter.setParaName(parameter.name());
if(parameter.type() == de.maishai.ast.Type.CHAR){ if(parameter.type() == Type.CHAR){
typedParameter.setType(Type.CHAR); typedParameter.setType(Type.CHAR);
} }
if(parameter.type() == de.maishai.ast.Type.BOOL){ if(parameter.type() == Type.BOOL){
typedParameter.setType(Type.BOOL); typedParameter.setType(Type.BOOL);
} }
if(parameter.type() == de.maishai.ast.Type.INT){ if(parameter.type() == Type.INT){
typedParameter.setType(Type.INT); typedParameter.setType(Type.INT);
} }

View File

@ -38,7 +38,7 @@ public class TypedNew implements TypedExpression, TypedStatement {
New untyped = (New) unTypedAST; New untyped = (New) unTypedAST;
TypedNew typedNew = new TypedNew(); TypedNew typedNew = new TypedNew();
typedNew.setType(TypeMapper.getType(untyped.type())); typedNew.setType(untyped.type());
if(untyped.args().isEmpty()){ if(untyped.args().isEmpty()){
//TODO: Implement this //TODO: Implement this

View File

@ -20,7 +20,7 @@ public class TypedReturnVoid implements TypedStatement {
@Override @Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) { public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
Method untyped = (Method) 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"); throw new RuntimeException("Return type is not void");
} }
return new TypedReturnVoid(); return new TypedReturnVoid();

View File

@ -19,7 +19,6 @@
import de.maishai.ast.Operator; import de.maishai.ast.Operator;
import de.maishai.ast.Type;
import de.maishai.ast.records.*; import de.maishai.ast.records.*;
import de.maishai.ast.records.Class; import de.maishai.ast.records.Class;

View File

@ -15,7 +15,6 @@
//} //}
import de.maishai.ast.Operator; import de.maishai.ast.Operator;
import de.maishai.ast.records.*; import de.maishai.ast.records.*;
import de.maishai.ast.Type;
import de.maishai.ast.records.Class; import de.maishai.ast.records.Class;
import java.util.List; import java.util.List;

View File

@ -3,7 +3,6 @@
//} //}
import de.maishai.ast.records.Class; import de.maishai.ast.records.Class;
import de.maishai.ast.Type;
import de.maishai.ast.records.Constructor; import de.maishai.ast.records.Constructor;
import de.maishai.ast.records.Declaration; import de.maishai.ast.records.Declaration;
import de.maishai.ast.records.Method; import de.maishai.ast.records.Method;

View File

@ -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 class TypedAbstractSyntax_ClassWithConstructorWithParameters {
// public static Class get() { // public static Class get() {