Refactored TypedAST and removed imports with star *

This commit is contained in:
ahmad 2024-07-05 02:15:48 +02:00
parent ecd0b801c7
commit 976f9c2ba6
23 changed files with 95 additions and 68 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,14 +1,8 @@
package de.maishai.typedast.ExceptionHandler; package de.maishai.typedast.ExceptionHandler;
public class ParameterAlreadyExistsException extends RuntimeException{ public class ParameterAlreadyExistsException extends RuntimeException {
public ParameterAlreadyExistsException(String paraName) { public ParameterAlreadyExistsException(String paraName) {
super("Parameter '" + paraName + "' already exists"); 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

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

View File

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

View File

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

View File

@ -1,8 +1,24 @@
package de.maishai.typedast.Util; package de.maishai.typedast.Util;
import de.maishai.ast.records.*; import de.maishai.ast.records.CharLiteral;
import de.maishai.ast.records.Expression;
import de.maishai.ast.records.IntLiteral;
import de.maishai.ast.records.BoolLiteral;
import de.maishai.ast.records.Binary;
import de.maishai.ast.records.FieldVarAccess;
import de.maishai.ast.records.MethodCall;
import de.maishai.ast.records.New;
import de.maishai.ast.records.Unary;
import de.maishai.typedast.TypedExpression; import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.typedclass.*; import de.maishai.typedast.typedclass.TypedBoolLiteral;
import de.maishai.typedast.typedclass.TypedCharLiteral;
import de.maishai.typedast.typedclass.TypedIntLiteral;
import de.maishai.typedast.typedclass.TypedBinary;
import de.maishai.typedast.typedclass.TypedFieldVarAccess;
import de.maishai.typedast.typedclass.TypedMethodCall;
import de.maishai.typedast.typedclass.TypedNew;
import de.maishai.typedast.typedclass.TypedUnary;
import de.maishai.typedast.typedclass.TypedProgram;
public class TypedExpressionUtil { public class TypedExpressionUtil {

View File

@ -6,7 +6,6 @@ import de.maishai.ast.records.Declaration;
import de.maishai.ast.records.Method; import de.maishai.ast.records.Method;
import de.maishai.typedast.ClassContext; import de.maishai.typedast.ClassContext;
import de.maishai.typedast.Type; import de.maishai.typedast.Type;
import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.TypedNode; import de.maishai.typedast.TypedNode;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
@ -138,21 +137,21 @@ public class TypedClass implements TypedNode {
} }
public Type getParameterTypeInCurrentMethod(String parameterName) { public Type getParameterTypeInCurrentMethod(String parameterName) {
if(parameterName == null) { if (parameterName == null) {
throw new RuntimeException("Parameter name is null"); throw new RuntimeException("Parameter name is null");
} }
return getCurrentMethod().getTypedParameters().stream().filter(parameter -> parameter.getParaName().equals(parameterName)).findFirst().get().getType(); return getCurrentMethod().getTypedParameters().stream().filter(parameter -> parameter.getParaName().equals(parameterName)).findFirst().get().getType();
} }
public Type getParameterTypeInCurrentConstructor(String parameterName) { public Type getParameterTypeInCurrentConstructor(String parameterName) {
if(parameterName == null) { if (parameterName == null) {
throw new RuntimeException("Parameter name is null"); throw new RuntimeException("Parameter name is null");
} }
return getCurrentConstructor().getTypedParameters().stream().filter(parameter -> parameter.getParaName().equals(parameterName)).findFirst().get().getType(); return getCurrentConstructor().getTypedParameters().stream().filter(parameter -> parameter.getParaName().equals(parameterName)).findFirst().get().getType();
} }
public boolean isMethodOfCurrentClass(String methodName) { public boolean isMethodOfCurrentClass(String methodName) {
if(methodName == null) { if (methodName == null) {
return false; return false;
} }
@ -165,7 +164,7 @@ public class TypedClass implements TypedNode {
} }
public Type getMethodType(String methodName) { public Type getMethodType(String methodName) {
if(methodName == null) { if (methodName == null) {
throw new RuntimeException("Method name is null"); throw new RuntimeException("Method name is null");
} }
@ -178,7 +177,7 @@ public class TypedClass implements TypedNode {
} }
public boolean isThereField(String fieldName) { public boolean isThereField(String fieldName) {
if(fieldName == null) { if (fieldName == null) {
return false; return false;
} }
@ -191,7 +190,7 @@ public class TypedClass implements TypedNode {
} }
public Type getFieldType(String fieldName) { public Type getFieldType(String fieldName) {
if(fieldName == null) { if (fieldName == null) {
throw new RuntimeException("Field name is null"); throw new RuntimeException("Field name is null");
} }
for (TypedDeclaration f : typedDeclarations) { for (TypedDeclaration f : typedDeclarations) {

View File

@ -33,13 +33,14 @@ public class TypedConstructor implements TypedNode {
this.typedParameters = typedParameters; this.typedParameters = typedParameters;
this.typedBlock = typedBlock; this.typedBlock = typedBlock;
} }
public TypedConstructor(Constructor unTypedConstructor, String className) { public TypedConstructor(Constructor unTypedConstructor, String className) {
convertToTypedConstructor(unTypedConstructor, className); convertToTypedConstructor(unTypedConstructor, className);
} }
private void convertToTypedConstructor(Constructor unTypedConstructor, String className) { private void convertToTypedConstructor(Constructor unTypedConstructor, String className) {
if(!unTypedConstructor.className().equals(className)) { if (!unTypedConstructor.className().equals(className)) {
throw new RuntimeException("Constructor name "+ unTypedConstructor.className() +" must be the same as class name" + className); throw new RuntimeException("Constructor name " + unTypedConstructor.className() + " must be the same as class name" + className);
} }
name = unTypedConstructor.className(); name = unTypedConstructor.className();
convertToTypedParameter(unTypedConstructor.params()); convertToTypedParameter(unTypedConstructor.params());
@ -47,7 +48,7 @@ public class TypedConstructor implements TypedNode {
} }
private void convertToTypedParameter(List<Parameter> params) { private void convertToTypedParameter(List<Parameter> params) {
if(params.isEmpty()) { if (params.isEmpty()) {
return; return;
} }
@ -75,22 +76,22 @@ public class TypedConstructor implements TypedNode {
} }
public boolean isLocalVariablePresent(String localVarName) { public boolean isLocalVariablePresent(String localVarName) {
if(localVarName == null) { if (localVarName == null) {
return false; return false;
} }
return localVariables.stream().anyMatch(localVariable -> localVariable.getName().equals(localVarName)); return localVariables.stream().anyMatch(localVariable -> localVariable.getName().equals(localVarName));
} }
public boolean isParameterPresent(String parameterName) { public boolean isParameterPresent(String parameterName) {
if(parameterName == null) { if (parameterName == null) {
return false; return false;
} }
return typedParameters.stream().anyMatch(parameter -> parameter.getParaName().equals(parameterName)); return typedParameters.stream().anyMatch(parameter -> parameter.getParaName().equals(parameterName));
} }
public boolean isLocalVariableInConstructor(String localVarName) { public boolean isLocalVariableInConstructor(String localVarName) {
if(localVarName == null) { if (localVarName == null) {
return false; return false;
} }
@ -98,7 +99,7 @@ public class TypedConstructor implements TypedNode {
} }
public Type getLocalVariableType(String localVarName) { public Type getLocalVariableType(String localVarName) {
if(localVarName == null) { if (localVarName == null) {
throw new RuntimeException("Local variable name is null"); throw new RuntimeException("Local variable name is null");
} }
@ -106,7 +107,7 @@ public class TypedConstructor implements TypedNode {
} }
private void checkIfParameterExists(String paraName) { private void checkIfParameterExists(String paraName) {
if(paraName == null) { if (paraName == null) {
throw new RuntimeException("Parameter name is null"); throw new RuntimeException("Parameter name is null");
} }
@ -116,7 +117,7 @@ public class TypedConstructor implements TypedNode {
} }
public void deleteLocalVariableInConstructor(String localVarName) { public void deleteLocalVariableInConstructor(String localVarName) {
if(localVarName == null) { if (localVarName == null) {
throw new RuntimeException("Local variable name is null"); throw new RuntimeException("Local variable name is null");
} }
localVariables.removeIf(localVariable -> localVariable.getName().equals(localVarName)); localVariables.removeIf(localVariable -> localVariable.getName().equals(localVarName));

View File

@ -33,7 +33,7 @@ public final class TypedDeclaration implements TypedNode {
TypedClass currentClass = typedProgram.getCurrentClass(); TypedClass currentClass = typedProgram.getCurrentClass();
if (type.getReference() != null && !currentClass.getClassName().equals(type.getReference()) && !typedProgram.isTypedClassPresent(type.getReference())) { if (type.getReference() != null && !currentClass.getClassName().equals(type.getReference()) && !typedProgram.isTypedClassPresent(type.getReference())) {
throw new RuntimeException("Type " + type.getReference() + " not found"); throw new RuntimeException("Type " + type.getReference() + " not found");
} }
if (currentClass.isThereField(name)) { if (currentClass.isThereField(name)) {

View File

@ -1,7 +1,10 @@
package de.maishai.typedast.typedclass; package de.maishai.typedast.typedclass;
import de.maishai.ast.records.*; import de.maishai.ast.records.DoWhile;
import de.maishai.typedast.*; import de.maishai.typedast.MethodContext;
import de.maishai.typedast.Type;
import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.TypedStatement;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import org.objectweb.asm.Label; import org.objectweb.asm.Label;

View File

@ -1,7 +1,10 @@
package de.maishai.typedast.typedclass; package de.maishai.typedast.typedclass;
import de.maishai.ast.records.*; import de.maishai.ast.records.For;
import de.maishai.typedast.*; import de.maishai.typedast.MethodContext;
import de.maishai.typedast.Type;
import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.TypedStatement;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;

View File

@ -1,7 +1,10 @@
package de.maishai.typedast.typedclass; package de.maishai.typedast.typedclass;
import de.maishai.ast.records.*; import de.maishai.ast.records.IfElse;
import de.maishai.typedast.*; import de.maishai.typedast.MethodContext;
import de.maishai.typedast.Type;
import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.TypedStatement;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;

View File

@ -2,8 +2,13 @@ package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Method; import de.maishai.ast.records.Method;
import de.maishai.ast.records.Parameter; import de.maishai.ast.records.Parameter;
import de.maishai.typedast.*; import de.maishai.typedast.CodeGenUtils;
import de.maishai.typedast.ExceptionHandler.ParameterAlreadyExistsException; import de.maishai.typedast.ExceptionHandler.ParameterAlreadyExistsException;
import de.maishai.typedast.Type;
import de.maishai.typedast.TypedNode;
import de.maishai.typedast.TypedStatement;
import de.maishai.typedast.MethodContext;
import de.maishai.typedast.ClassContext;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;

View File

@ -2,8 +2,12 @@ package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Expression; import de.maishai.ast.records.Expression;
import de.maishai.ast.records.New; import de.maishai.ast.records.New;
import de.maishai.typedast.*; import de.maishai.typedast.MethodContext;
import de.maishai.typedast.ExceptionHandler.NotMatchConstructorException; import de.maishai.typedast.ExceptionHandler.NotMatchConstructorException;
import de.maishai.typedast.Type;
import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.TypedStatement;
import de.maishai.typedast.CodeGenUtils;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import org.objectweb.asm.Opcodes; import org.objectweb.asm.Opcodes;

View File

@ -16,6 +16,7 @@ public class TypedParameter implements TypedNode {
public TypedParameter(Parameter unTypedParameter) { public TypedParameter(Parameter unTypedParameter) {
convertToTypedParameter(unTypedParameter); convertToTypedParameter(unTypedParameter);
} }
private void convertToTypedParameter(Parameter unTypedParameter) { private void convertToTypedParameter(Parameter unTypedParameter) {
paraName = unTypedParameter.name(); paraName = unTypedParameter.name();
type = unTypedParameter.type(); type = unTypedParameter.type();

View File

@ -1,7 +1,11 @@
package de.maishai.typedast.typedclass; package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Print; import de.maishai.ast.records.Print;
import de.maishai.typedast.*; import de.maishai.typedast.MethodContext;
import de.maishai.typedast.Type;
import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.TypedStatement;
import de.maishai.typedast.CodeGenUtils;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.objectweb.asm.Opcodes; import org.objectweb.asm.Opcodes;

View File

@ -45,7 +45,7 @@ public class TypedProgram {
} }
public boolean isClassWithNamePresent(String className) { public boolean isClassWithNamePresent(String className) {
if(className == null){ if (className == null) {
return false; return false;
} }
@ -53,34 +53,35 @@ public class TypedProgram {
} }
public Type getTypeOfFieldNameInClass(String className, String fieldName) { public Type getTypeOfFieldNameInClass(String className, String fieldName) {
if(className == null || fieldName == null){ if (className == null || fieldName == null) {
return null; return null;
} }
return typedClasses.stream().filter(clas -> clas.getClassName().equals(className)).findFirst().get().getFieldType(fieldName); return typedClasses.stream().filter(clas -> clas.getClassName().equals(className)).findFirst().get().getFieldType(fieldName);
} }
public Type getTypeOfFieldOrMethodNameInClass(String className, String fieldName) { public Type getTypeOfFieldOrMethodNameInClass(String className, String fieldName) {
if(className == null || fieldName == null){ if (className == null || fieldName == null) {
return null; return null;
} }
TypedClass c = typedClasses.stream().filter(clas -> clas.getClassName().equals(className)).findFirst().get(); TypedClass c = typedClasses.stream().filter(clas -> clas.getClassName().equals(className)).findFirst().get();
if(c.isThereField(fieldName)){ if (c.isThereField(fieldName)) {
return c.getFieldType(fieldName); return c.getFieldType(fieldName);
}else if(c.isMethodOfCurrentClass(fieldName)){ } else if (c.isMethodOfCurrentClass(fieldName)) {
return c.getMethodType(fieldName); return c.getMethodType(fieldName);
} }
return null; return null;
} }
public TypedClass getTypedClass(String className) { public TypedClass getTypedClass(String className) {
if(className == null){ if (className == null) {
return null; return null;
} }
return typedClasses.stream().filter(clas -> clas.getClassName().equals(className)).findFirst().get(); return typedClasses.stream().filter(clas -> clas.getClassName().equals(className)).findFirst().get();
} }
public boolean isTypedClassPresent(String className) { public boolean isTypedClassPresent(String className) {
if(className == null){ if (className == null) {
return false; return false;
} }
return typedClasses.stream().anyMatch(clas -> clas.getClassName().equals(className)); return typedClasses.stream().anyMatch(clas -> clas.getClassName().equals(className));

View File

@ -42,8 +42,8 @@ public class TypedReturn implements TypedStatement {
if (currentClass.isCurrentMethodPresent() && currentMethod.getReturnType().getKind() != this.type.getKind()) { if (currentClass.isCurrentMethodPresent() && currentMethod.getReturnType().getKind() != this.type.getKind()) {
throw new TypeOfReturnNotMatchException(currentMethod.getReturnType().getKind().name(), throw new TypeOfReturnNotMatchException(currentMethod.getReturnType().getKind().name(),
this.type.getKind().name(), currentMethod.getName()); this.type.getKind().name(), currentMethod.getName());
} }
return type; return type;
} }