Refactored TypedAST and added a new exception type

This commit is contained in:
ahmad 2024-07-05 09:40:19 +02:00
parent 976f9c2ba6
commit 92c08ac3de
4 changed files with 19 additions and 10 deletions

View File

@ -0,0 +1,7 @@
package de.maishai.typedast.ExceptionHandler;
public class InvalidWhileConditionException extends RuntimeException{
public InvalidWhileConditionException() {
super("Invalid while condition");
}
}

View File

@ -19,11 +19,11 @@ public final class TypedDeclaration implements TypedNode {
private Type type; private Type type;
public TypedDeclaration(TypedProgram typedProgram, Declaration declaration) { public TypedDeclaration(TypedProgram typedProgram, Declaration declaration) {
convertToTypedDeclaration(typedProgram, declaration); convertToTypedDeclaration(declaration);
typeCheck(typedProgram); typeCheck(typedProgram);
} }
private void convertToTypedDeclaration(TypedProgram typedProgram, Declaration declaration) { private void convertToTypedDeclaration(Declaration declaration) {
name = declaration.name(); name = declaration.name();
type = declaration.type(); type = declaration.type();
} }

View File

@ -35,15 +35,17 @@ public class TypedIfElse implements TypedStatement {
@Override @Override
public Type typeCheck(TypedProgram typedProgram) { public Type typeCheck(TypedProgram typedProgram) {
Type ifType = ifTypedBlock.typeCheck(typedProgram);
Type elseType = elseTypedBlock.typeCheck(typedProgram);
if (ifTypedBlock.typeCheck(typedProgram) == elseTypedBlock.typeCheck(typedProgram)) { if (ifType == elseType) {
type = ifTypedBlock.typeCheck(typedProgram); type = ifType;
} }
if (elseTypedBlock.typeCheck(typedProgram) == Type.VOID) { if (elseType == Type.VOID) {
type = ifTypedBlock.typeCheck(typedProgram); type = ifType;
} }
if (ifTypedBlock.typeCheck(typedProgram) == Type.VOID) { if (ifType == Type.VOID) {
type = elseTypedBlock.typeCheck(typedProgram); type = elseType;
} }
return type; return type;

View File

@ -1,6 +1,7 @@
package de.maishai.typedast.typedclass; package de.maishai.typedast.typedclass;
import de.maishai.ast.records.While; import de.maishai.ast.records.While;
import de.maishai.typedast.ExceptionHandler.InvalidWhileConditionException;
import de.maishai.typedast.MethodContext; import de.maishai.typedast.MethodContext;
import de.maishai.typedast.Type; import de.maishai.typedast.Type;
import de.maishai.typedast.TypedExpression; import de.maishai.typedast.TypedExpression;
@ -28,11 +29,10 @@ public class TypedWhile implements TypedStatement {
cond = convertExpression(typedProgram, unTypedWhile.cond()); cond = convertExpression(typedProgram, unTypedWhile.cond());
typedBlock = new TypedBlock(typedProgram, unTypedWhile.block()); typedBlock = new TypedBlock(typedProgram, unTypedWhile.block());
} }
@Override @Override
public Type typeCheck(TypedProgram typedProgram) { public Type typeCheck(TypedProgram typedProgram) {
if (cond.typeCheck(typedProgram) != Type.BOOL) { if (cond.typeCheck(typedProgram) != Type.BOOL) {
throw new RuntimeException("While condition must be a boolean"); throw new InvalidWhileConditionException();
} }
type = typedBlock.typeCheck(typedProgram); type = typedBlock.typeCheck(typedProgram);
return type; return type;