Outsourced the Exceptions in TypedAst

This commit is contained in:
ahmad 2024-07-05 02:02:43 +02:00
parent 6af67326f1
commit ecd0b801c7
22 changed files with 155 additions and 30 deletions

View File

@ -0,0 +1,10 @@
package de.maishai.typedast.ExceptionHandler;
public class FieldAlreadyDeclaredException extends RuntimeException{
public FieldAlreadyDeclaredException(String name) {
super("Field " + name + " already declared");
}
}

View File

@ -0,0 +1,8 @@
package de.maishai.typedast.ExceptionHandler;
public class ImproperFieldAccessException extends RuntimeException{
public ImproperFieldAccessException(String name) {
super("Field Variable "+ name+ " should be used with `this´");
}
}

View File

@ -0,0 +1,7 @@
package de.maishai.typedast.ExceptionHandler;
public class LocalVariableAlreadDeclaredException extends RuntimeException {
public LocalVariableAlreadDeclaredException(String name){
super("local Variable " + name + " already declared");
}
}

View File

@ -0,0 +1,7 @@
package de.maishai.typedast.ExceptionHandler;
public class NonStaticUsageInMainException extends RuntimeException{
public NonStaticUsageInMainException(){
super("Main Method, is not allowed to have fields, they are not static");
}
}

View File

@ -0,0 +1,9 @@
package de.maishai.typedast.ExceptionHandler;
public class NotFoundMethodOrConstructor extends RuntimeException {
public NotFoundMethodOrConstructor() {
super("Method or constructor not found in class");
}
}

View File

@ -0,0 +1,13 @@
package de.maishai.typedast.ExceptionHandler;
public class NotMatchConstructorException extends RuntimeException {
public NotMatchConstructorException(String constructorName) {
super(constructorName);
}
public NotMatchConstructorException() {
super("Not matching constructor found");
}
}

View File

@ -0,0 +1,8 @@
package de.maishai.typedast.ExceptionHandler;
public class OperatorException extends RuntimeException{
public OperatorException(String operator, String type){
super(operator + " operator must be applied to " + type);
}
}

View File

@ -0,0 +1,14 @@
package de.maishai.typedast.ExceptionHandler;
public class ParameterAlreadyExistsException extends RuntimeException{
public ParameterAlreadyExistsException(String paraName) {
super("Parameter '" + paraName + "' already exists");
}
public ParameterAlreadyExistsException(String paraName, String methodOrConstructorName) {
super("Parameter '" + paraName + "' already exists in " + methodOrConstructorName);
}
public ParameterAlreadyExistsException() {
super("Parameter already exists");
}
}

View File

@ -0,0 +1,9 @@
package de.maishai.typedast.ExceptionHandler;
public class TypeMismatchException extends RuntimeException{
public TypeMismatchException(String operator) {
super("Type mismatch in operator: " + operator);
}
}

View File

@ -0,0 +1,17 @@
package de.maishai.typedast.ExceptionHandler;
import de.maishai.typedast.Type;
public class TypeOfReturnNotMatchException extends RuntimeException{
public TypeOfReturnNotMatchException(String excepted, String actual, String name){
super("Mismatched return type: " +
" Expected: " + excepted +
" Actual: " + actual +
" Method name: " + name
);
}
public TypeOfReturnNotMatchException(String name){
super("Constructor " + name + " must not have a return");
}
}

View File

@ -0,0 +1,13 @@
package de.maishai.typedast.ExceptionHandler;
public class VariableNotDeclaredException extends RuntimeException{
public VariableNotDeclaredException(String variableName) {
super("Variable '" + variableName + "' not declared");
}
public VariableNotDeclaredException(String variableName, String constrOrMethod) {
super("Variable '" + variableName + "' not declared in'" + constrOrMethod + "'");
}
}

View File

@ -1,6 +1,10 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Assignment;
import de.maishai.typedast.ExceptionHandler.ImproperFieldAccessException;
import de.maishai.typedast.ExceptionHandler.NonStaticUsageInMainException;
import de.maishai.typedast.ExceptionHandler.TypeMismatchException;
import de.maishai.typedast.ExceptionHandler.VariableNotDeclaredException;
import de.maishai.typedast.MethodContext;
import de.maishai.typedast.Type;
import de.maishai.typedast.TypedExpression;
@ -37,7 +41,7 @@ public class TypedAssignment implements TypedStatement {
TypedClass currentClass = typedProgram.getCurrentClass();
if (Boolean.TRUE.equals(currentClass.isCurrentMainMethodPresent() && location.getField()) && location.getRecursiveOwnerChain() == null) {
throw new RuntimeException("Main Method, is not allowed to have fields, they are not static");
throw new NonStaticUsageInMainException();
}
Type typeLeft = getTypeLeft(typedProgram);
@ -47,7 +51,7 @@ public class TypedAssignment implements TypedStatement {
type = typeLeft;
return typeLeft;
}
throw new RuntimeException("type of left not equals with type of right");
throw new TypeMismatchException(typeLeft.getKind().toString());
}
private Type getTypeLeft(TypedProgram typedProgram) {
@ -66,7 +70,7 @@ public class TypedAssignment implements TypedStatement {
return getTypeFromConstructorOrField(currentClass.getCurrentConstructor(), name, currentClass);
}
throw new RuntimeException("Variable " + name + " not declared");
throw new VariableNotDeclaredException(name);
}
private Type getTypeFromMethodOrField(TypedMethod currentMethod, String name, TypedClass currentClass) {
@ -77,9 +81,9 @@ public class TypedAssignment implements TypedStatement {
if (currentMethod.isLocalVariableInMethod(name)) {
return currentMethod.getLocalVariableType(name);
} else if (currentClass.isThereField(name)) {
throw new RuntimeException("Field Variable " + name + " should be used with `this´");
throw new ImproperFieldAccessException(name);
} else {
throw new RuntimeException("Variable " + name + " not declared in method");
throw new VariableNotDeclaredException(name, currentMethod.getName());
}
}
@ -92,9 +96,9 @@ public class TypedAssignment implements TypedStatement {
if (currentConstructor.isLocalVariableInConstructor(name)) {
return currentConstructor.getLocalVariableType(name);
} else if (currentClass.isThereField(name)) {
throw new RuntimeException("Field Variable " + name + " should be used with `this´");
throw new ImproperFieldAccessException(name);
} else {
throw new RuntimeException("Variable " + name + " not declared in constructor");
throw new VariableNotDeclaredException(name, currentConstructor.getName());
}
}

View File

@ -2,6 +2,7 @@ package de.maishai.typedast.typedclass;
import de.maishai.ast.Operator;
import de.maishai.ast.records.Binary;
import de.maishai.typedast.ExceptionHandler.TypeMismatchException;
import de.maishai.typedast.MethodContext;
import de.maishai.typedast.Type;
import de.maishai.typedast.TypedExpression;
@ -43,14 +44,14 @@ public class TypedBinary implements TypedExpression {
type = Type.INT;
return Type.INT;
} else {
throw new RuntimeException("Type mismatch in " + op);
throw new TypeMismatchException(op.toString());
}
} else if (op == Operator.GT || op == Operator.LT || op == Operator.GE || op == Operator.LE) {
if (leftType == Type.INT && rightType == Type.INT) {
type = Type.BOOL;
return Type.BOOL;
} else {
throw new RuntimeException("Type mismatch in " + op);
throw new TypeMismatchException(op.toString());
}
} else if (op == Operator.EQ || op == Operator.NE ) {
if (leftType == Type.INT && rightType == Type.INT || leftType == Type.BOOL && rightType == Type.BOOL
@ -58,14 +59,14 @@ public class TypedBinary implements TypedExpression {
type = Type.BOOL;
return Type.BOOL;
} else {
throw new RuntimeException("Type mismatch in " + op);
throw new TypeMismatchException(op.toString());
}
} else if (op == Operator.AND || op == Operator.OR ){
if (leftType == Type.BOOL && rightType == Type.BOOL) {
type = Type.BOOL;
return Type.BOOL;
} else {
throw new RuntimeException("Type mismatch in " + op);
throw new TypeMismatchException(op.toString());
}
}

View File

@ -3,6 +3,7 @@ package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Constructor;
import de.maishai.ast.records.Parameter;
import de.maishai.typedast.*;
import de.maishai.typedast.ExceptionHandler.ParameterAlreadyExistsException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.RequiredArgsConstructor;
@ -110,7 +111,7 @@ public class TypedConstructor implements TypedNode {
}
if (typedParameters.stream().anyMatch(parameter -> parameter.getParaName().equals(paraName))) {
throw new RuntimeException("Parameter " + paraName + " already exists");
throw new ParameterAlreadyExistsException(paraName);
}
}

View File

@ -1,6 +1,7 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Declaration;
import de.maishai.typedast.ExceptionHandler.FieldAlreadyDeclaredException;
import de.maishai.typedast.TypedNode;
import de.maishai.typedast.Type;
import lombok.AllArgsConstructor;
@ -36,7 +37,7 @@ public final class TypedDeclaration implements TypedNode {
}
if (currentClass.isThereField(name)) {
throw new RuntimeException("Field " + name + " already declared");
throw new FieldAlreadyDeclaredException(name);
}
return type;
}

View File

@ -1,6 +1,7 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.FieldVarAccess;
import de.maishai.typedast.ExceptionHandler.VariableNotDeclaredException;
import de.maishai.typedast.MethodContext;
import de.maishai.typedast.Type;
import de.maishai.typedast.TypedExpression;
@ -80,7 +81,7 @@ public class TypedFieldVarAccess implements TypedExpression {
} else if (currentClass.isCurrentMethodPresent()) {
return checkMethodVariableType(typedProgram);
} else {
throw new RuntimeException("Variable " + name + " not declared");
throw new VariableNotDeclaredException(name);
}
}

View File

@ -1,6 +1,8 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Declaration;
import de.maishai.typedast.ExceptionHandler.LocalVariableAlreadDeclaredException;
import de.maishai.typedast.ExceptionHandler.NotFoundMethodOrConstructor;
import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedNode;
import de.maishai.typedast.Type;
@ -34,19 +36,19 @@ public final class TypedLocalVariable implements TypedNode {
if (currentClass.isCurrentMethodPresent() && !currentClass.isCurrentConstructorPresent()) {
if (currentMethod.isLocalVariableInMethod(name)) {
throw new RuntimeException("Variable " + name + " already declared");
throw new LocalVariableAlreadDeclaredException(name);
}
currentMethod.getLocalVariables().add(this);
return type;
}
if (!currentClass.isCurrentMethodPresent() && currentClass.isCurrentConstructorPresent()) {
if (currentConstructor.isLocalVariableInConstructor(name)) {
throw new RuntimeException("Variable " + name + " already declared");
throw new LocalVariableAlreadDeclaredException(name);
}
currentConstructor.getLocalVariables().add(this);
return type;
}
throw new RuntimeException("not found method or constructor in class");
throw new NotFoundMethodOrConstructor();
}
public void codeGen(MethodVisitor mv, MethodContext ctx) {

View File

@ -3,6 +3,7 @@ package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Method;
import de.maishai.ast.records.Parameter;
import de.maishai.typedast.*;
import de.maishai.typedast.ExceptionHandler.ParameterAlreadyExistsException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@ -52,7 +53,7 @@ public class TypedMethod implements TypedNode {
public void checkIfParameterExists(String parameterName) {
if (typedParameters.stream().anyMatch(parameter -> parameter.getParaName().equals(parameterName))) {
throw new RuntimeException("Parameter " + parameterName + " already exists");
throw new ParameterAlreadyExistsException(parameterName);
}
}

View File

@ -3,6 +3,7 @@ package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Expression;
import de.maishai.ast.records.New;
import de.maishai.typedast.*;
import de.maishai.typedast.ExceptionHandler.NotMatchConstructorException;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.objectweb.asm.Opcodes;
@ -57,7 +58,7 @@ public class TypedNew implements TypedExpression, TypedStatement {
}
}
}
throw new RuntimeException("No matching constructor found");
throw new NotMatchConstructorException();
}
@Override

View File

@ -1,6 +1,7 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Parameter;
import de.maishai.typedast.ExceptionHandler.ParameterAlreadyExistsException;
import de.maishai.typedast.Type;
import de.maishai.typedast.TypedNode;
import lombok.AllArgsConstructor;
@ -25,11 +26,11 @@ public class TypedParameter implements TypedNode {
if (typedProgram.getCurrentClass().isCurrentMethodPresent()) {
if (typedProgram.getCurrentClass().isParameterWitNameInCurrentMethod(paraName)) {
throw new RuntimeException("Parameter " + paraName + " already exists");
throw new ParameterAlreadyExistsException(paraName);
}
} else if (typedProgram.getCurrentClass().isCurrentConstructorPresent()) {
if (typedProgram.getCurrentClass().isParameterWitNameInCurrentConstructor(paraName)) {
throw new RuntimeException("Parameter " + paraName + " already exists");
throw new ParameterAlreadyExistsException(paraName);
}
}

View File

@ -2,6 +2,7 @@ package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Return;
import de.maishai.typedast.ExceptionHandler.TypeOfReturnNotMatchException;
import de.maishai.typedast.MethodContext;
import de.maishai.typedast.Type;
import de.maishai.typedast.TypedExpression;
@ -40,14 +41,9 @@ public class TypedReturn implements TypedStatement {
TypedMethod currentMethod = currentClass.getCurrentMethod();
if (currentClass.isCurrentMethodPresent() && currentMethod.getReturnType().getKind() != this.type.getKind()) {
StringBuilder exp = new StringBuilder();
exp.append("\nMismatched return type: ");
exp.append("\nExpected: ").append(currentMethod.getReturnType().getKind());
exp.append("\nActual: ").append(this.type.getKind());
exp.append("\nMethod name: ").append(currentMethod.getName());
throw new RuntimeException(exp.toString());
throw new TypeOfReturnNotMatchException(currentMethod.getReturnType().getKind().name(),
this.type.getKind().name(), currentMethod.getName());
}
return type;
}

View File

@ -2,6 +2,7 @@ package de.maishai.typedast.typedclass;
import de.maishai.ast.UnaryOperator;
import de.maishai.ast.records.Unary;
import de.maishai.typedast.ExceptionHandler.OperatorException;
import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.Type;
@ -33,7 +34,7 @@ public class TypedUnary implements TypedExpression {
if (op == UnaryOperator.NOT) {
if (right.typeCheck(typedProgram) != Type.BOOL) {
throw new RuntimeException("Not operator must be applied to boolean");
throw new OperatorException(UnaryOperator.NOT.name(), Type.BOOL.getKind().name());
}
type = Type.BOOL;
return type;
@ -41,7 +42,7 @@ public class TypedUnary implements TypedExpression {
if (op == UnaryOperator.SUB) {
if (right.typeCheck(typedProgram) != Type.INT) {
throw new RuntimeException("Minus operator must be applied to int");
throw new OperatorException(UnaryOperator.SUB.name(), Type.INT.getKind().name());
}
type = Type.INT;
return type;