Update TypedMethod and TypedReturn and ClassCanBeTyped

This commit is contained in:
ahmad 2024-05-12 20:59:35 +02:00
parent 8b5ec63e4b
commit 6b89cd0958
3 changed files with 9 additions and 2 deletions

View File

@ -67,7 +67,7 @@ public class TypedMethod implements TypedNode {
@Override @Override
public Type typeCheck(TypedClass clas) { public Type typeCheck(TypedClass clas) {
if(returnType != Type.VOID){ if(returnType != Type.VOID){
if(typedBlock.typeCheck(clas) != returnType){ if(typedBlock.typeCheck(clas).getKind() != returnType.getKind()){
throw new RuntimeException("Method " + name + " must return " + returnType); throw new RuntimeException("Method " + name + " must return " + returnType);
} }
} }

View File

@ -24,7 +24,11 @@ public class TypedReturn implements TypedStatement {
public void convertToTypedReturn(TypedClass clas, Return unTypedReturn) { public void convertToTypedReturn(TypedClass clas, Return unTypedReturn) {
ret = convertExpression(clas, unTypedReturn.ret()); ret = convertExpression(clas, unTypedReturn.ret());
type = ret.getType(); if(ret == null){
type = Type.VOID;
}else{
type = ret.getType();
}
} }
@Override @Override

View File

@ -3,6 +3,7 @@ public class ClassCanBeTyped {
int x; int x;
int y; int y;
ClassCanBeTyped b; ClassCanBeTyped b;
ClassCanBeTyped c;
public ClassCanBeTyped(int x) { public ClassCanBeTyped(int x) {
this.x = x; this.x = x;
} }
@ -16,6 +17,8 @@ public class ClassCanBeTyped {
b = new ClassCanBeTyped(x); b = new ClassCanBeTyped(x);
b.x = 10 + a; b.x = 10 + a;
b.y = 20; b.y = 20;
b.c.x = 20 + a;
b.c.b.y = b.x;
return b; return b;
} }