done TODOs for TypedReturn but not all

This commit is contained in:
ahmad 2024-05-10 19:03:18 +02:00
parent 875d0adac4
commit c7d756b083
6 changed files with 60 additions and 31 deletions

View File

@ -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();

View File

@ -25,6 +25,10 @@ public class TypedBlock implements TypedNode {
public void blockToTypedBlock(Map<String, Type> 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;
}

View File

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

View File

@ -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<String, Type> localVar, TypedClass clas, Method unTypedMethod) {
typedBlock = new TypedBlock(localVar, clas, unTypedMethod.block());
}
@Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
if (localVar.containsKey(name)) {

View File

@ -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<TypedExpression> args;
private List<TypedExpression> args = new ArrayList<>();
public TypedNew convertToTypedNew(New unTypedNew) {
//TODO: Implement this
return null;
public TypedNew(Map<String, Type> localVar, TypedClass clas, New unTypedNew) {
convertToTypedNew(localVar, clas, unTypedNew);
}
public void convertToTypedNew(Map<String, Type> localVar, TypedClass clas, New unTypedNew) {
type = unTypedNew.type();
for (Expression arg : unTypedNew.args()) {
args.add(getKindOfExpression(localVar, clas, arg));
}
}
@Override
public Type typeCheck(Map<String, Type> 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;
}
}
return Type.VOID;
if(valid){
return Type.REFERENCE(clas.getClassName());
}
}
}
throw new RuntimeException("No matching constructor found");
}

View File

@ -28,10 +28,10 @@ public class TypedReturn implements TypedStatement {
@Override
public Type typeCheck(Map<String, Type> 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;
}