From c7d756b0835027495d254de8b54c3eb581368318 Mon Sep 17 00:00:00 2001 From: ahmad Date: Fri, 10 May 2024 19:03:18 +0200 Subject: [PATCH] done TODOs for TypedReturn but not all --- .../typedast/Util/TypedExpressionUtil.java | 7 +--- .../typedast/typedclass/TypedBlock.java | 9 ++++- .../typedast/typedclass/TypedClass.java | 10 ++--- .../typedast/typedclass/TypedMethod.java | 18 ++++++++- .../maishai/typedast/typedclass/TypedNew.java | 40 +++++++++++++------ .../typedast/typedclass/TypedReturn.java | 7 ++-- 6 files changed, 60 insertions(+), 31 deletions(-) diff --git a/src/main/java/de/maishai/typedast/Util/TypedExpressionUtil.java b/src/main/java/de/maishai/typedast/Util/TypedExpressionUtil.java index d879dad..305affe 100644 --- a/src/main/java/de/maishai/typedast/Util/TypedExpressionUtil.java +++ b/src/main/java/de/maishai/typedast/Util/TypedExpressionUtil.java @@ -40,12 +40,7 @@ public class TypedExpressionUtil { return typedMethodCall; } else if (expression instanceof New newStmt) { - TypedNew typedNew = new TypedNew(); - typedNew.setType(newStmt.type()); - for (var arg : newStmt.args()) { - typedNew.getArgs().add(getKindOfExpression(localVar, clas, arg)); - } - return typedNew; + return new TypedNew(localVar, clas, newStmt); } else if (expression instanceof Unary unary) { TypedUnary typedUnary = new TypedUnary(); diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java b/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java index 43a8dcd..304c7c7 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java @@ -25,6 +25,10 @@ public class TypedBlock implements TypedNode { public void blockToTypedBlock(Map localVar, TypedClass clas, Block unTypedBlock) { + if(unTypedBlock == null) { + return; + } + for (Declaration var : unTypedBlock.localVariables()) { TypedLocalVariable typedVar = new TypedLocalVariable(localVar, clas, var); vars.add(typedVar); @@ -68,8 +72,9 @@ public class TypedBlock implements TypedNode { continue; } if (stmt instanceof New newStmt) { - TypedNew typedNew = new TypedNew(); - //TODO: implement this + TypedNew typedNew = new TypedNew(localVar, clas, newStmt); + typedNew.typeCheck(localVar, clas); + stmts.add(typedNew); continue; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedClass.java b/src/main/java/de/maishai/typedast/typedclass/TypedClass.java index eca1067..9b25c6e 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedClass.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedClass.java @@ -37,16 +37,16 @@ public class TypedClass implements TypedNode { typedFields.add(new TypedField(localVar, this, field)); } + for (Constructor constructor : c.constructors()) { + typedConstructors.add(new TypedConstructor(localVar, this, constructor)); + typedConstructors.get(typedConstructors.size() - 1).convertToBlock(localVar, this, constructor); + } + for (Method method : c.methods()) { typedMethods.add(new TypedMethod(localVar, this, method)); typedMethods.get(typedMethods.size() - 1).convertToTypedBlock(localVar, this, method); } - - for (Constructor constructor : c.constructors()) { - typedConstructors.add(new TypedConstructor(localVar, this, constructor)); - typedConstructors.get(typedConstructors.size() - 1).convertToBlock(localVar, this, constructor); - } } @Override diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java b/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java index ae706d9..ce72e25 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java @@ -33,14 +33,28 @@ public class TypedMethod implements TypedNode { for (Parameter parameter : unTypedMethod.params()) { typedParameters.add(new TypedParameter(localVar, clas, parameter)); } - typedBlock = new TypedBlock(localVar, clas, unTypedMethod.block()); + for (var method : clas.getTypedMethods()) { + if (method.getName().equals(name) && method.getTypedParameters().size() == typedParameters.size() + && method.getReturnType().equals(returnType)) { + + for (int i = 0; i < method.getTypedParameters().size(); i++){ + if(method.getTypedParameters().get(i).getType().equals(typedParameters.get(i).getType())){ + throw new RuntimeException("Method " + name + " already exists"); + } + } + + if (method.getTypedParameters().isEmpty() && typedParameters.isEmpty()) { + throw new RuntimeException("Method " + name + " already exists"); + } + } + } } + public void convertToTypedBlock(Map localVar, TypedClass clas, Method unTypedMethod) { typedBlock = new TypedBlock(localVar, clas, unTypedMethod.block()); } - @Override public Type typeCheck(Map localVar, TypedClass clas) { if (localVar.containsKey(name)) { diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedNew.java b/src/main/java/de/maishai/typedast/typedclass/TypedNew.java index f72dcba..ca922b8 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedNew.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedNew.java @@ -1,39 +1,55 @@ package de.maishai.typedast.typedclass; +import de.maishai.ast.records.Expression; import de.maishai.ast.records.New; import de.maishai.ast.records.Node; +import de.maishai.ast.records.Parameter; import de.maishai.typedast.*; import lombok.Data; import org.objectweb.asm.MethodVisitor; +import java.util.ArrayList; import java.util.List; import java.util.Map; +import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression; + @Data public class TypedNew implements TypedExpression, TypedStatement { private Type type; - private List args; + private List args = new ArrayList<>(); - public TypedNew convertToTypedNew(New unTypedNew) { - //TODO: Implement this - return null; + public TypedNew(Map localVar, TypedClass clas, New unTypedNew) { + convertToTypedNew(localVar, clas, unTypedNew); } + public void convertToTypedNew(Map localVar, TypedClass clas, New unTypedNew) { + type = unTypedNew.type(); + for (Expression arg : unTypedNew.args()) { + args.add(getKindOfExpression(localVar, clas, arg)); + } + } + @Override public Type typeCheck(Map localVar, TypedClass clas) { if(clas == null){ throw new RuntimeException("Class not found"); } - if (clas.getTypedFields().size() != args.size()) { - throw new RuntimeException("number of arguments is invalid for class" + this.type.getReference()); - } - for (int i = 0; i < args.size(); i++) { - Type type = args.get(i).typeCheck(localVar, clas); - if (!type.equals(clas.getTypedFields().get(i).getType())) { - throw new RuntimeException("False type for argument " + i + " in class " + type.getKind()); + for(var constructor : clas.getTypedConstructors()){ + if(constructor.getTypedParameters().size() == args.size()){ + boolean valid = true; + for(int i = 0; i < args.size(); i++){ + if(!constructor.getTypedParameters().get(i).getType().equals(args.get(i).typeCheck(localVar, clas))){ + valid = false; + break; + } + } + if(valid){ + return Type.REFERENCE(clas.getClassName()); + } } } - return Type.VOID; + throw new RuntimeException("No matching constructor found"); } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java b/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java index 0458f07..86b9a0b 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java @@ -28,10 +28,10 @@ public class TypedReturn implements TypedStatement { @Override public Type typeCheck(Map localVar, TypedClass clas) { - + //TODO: not localvar for(var typedMethod : clas.getTypedMethods()) { - if(!localVar.containsKey(typedMethod.getName())) { - if(typedMethod.getReturnType() != this.type) { + if(localVar.containsKey(typedMethod.getName())) { + if(typedMethod.getReturnType().getKind() != this.type.getKind()) { //TODO: exception auslagern StringBuilder exp = new StringBuilder(); exp.append("\nMismatched return type: "); @@ -42,7 +42,6 @@ public class TypedReturn implements TypedStatement { } } } - return type; }