Add a help class "TypedExpressionUtil"

This commit is contained in:
Ahmad 2024-05-09 15:45:46 +02:00
parent f329ad6c59
commit 37950dee82

View File

@ -0,0 +1,68 @@
package de.maishai.typedast.Util;
import de.maishai.ast.records.*;
import de.maishai.typedast.Type;
import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.typedclass.*;
import java.util.Map;
public class TypedExpressionUtil {
public static TypedExpression GET_KIND_OF_EXPRESSION(Map<String, Type> localVar, Map<String, TypedClass> classes, Expression expression) {
if (expression instanceof BoolLiteral boolLiteral) {
TypedBoolLiteral typedBoolLiteral = new TypedBoolLiteral();
typedBoolLiteral.setValue(boolLiteral.value());
return typedBoolLiteral;
}
else if (expression instanceof CharLiteral charLiteral) {
TypedCharLiteral typedCharLiteral = new TypedCharLiteral()
.convertToCharLiteral(localVar, classes, charLiteral);
return typedCharLiteral;
}
else if (expression instanceof IntLiteral intLiteral) {
TypedIntLiteral typedIntLiteral = new TypedIntLiteral()
.convertToTypedIntLiteral(localVar, classes, intLiteral);
return typedIntLiteral;
}
else if (expression instanceof Binary binary) {
TypedBinary typedBinary = new TypedBinary()
.convertToTypedBinary(localVar, classes, binary);
typedBinary.typeCheck(localVar, classes);
return typedBinary;
}
else if (expression instanceof FieldVarAccess fieldVarAccess) {
TypedFieldVarAccess typedFieldVarAccess = new TypedFieldVarAccess()
.convertToTypedFieldVarAccess(localVar, classes, fieldVarAccess);
return typedFieldVarAccess;
}
else if (expression instanceof MethodCall methodCall) {
TypedMethodCall typedMethodCall = new TypedMethodCall();
//TODO: make this beautiful to read after full implementation of TypedMethodCall
TypedFieldVarAccess typedFieldVarAccess = new TypedFieldVarAccess();
typedFieldVarAccess.setName(methodCall.recipient().id());
typedFieldVarAccess.setField(methodCall.recipient().field());
typedMethodCall.setRecipient(typedFieldVarAccess);
for (var arg : methodCall.args()) {
typedMethodCall.getArgs().add(GET_KIND_OF_EXPRESSION(localVar, classes, arg));
}
return typedMethodCall;
}
else if (expression instanceof New newStmt) {
TypedNew typedNew = new TypedNew();
typedNew.setType(newStmt.type());
for (var arg : newStmt.args()) {
typedNew.getArgs().add(GET_KIND_OF_EXPRESSION(localVar, classes, arg));
}
return typedNew;
}
else if (expression instanceof Unary unary) {
TypedUnary typedUnary = new TypedUnary();
typedUnary.setOp(unary.op());
typedUnary.setRight(GET_KIND_OF_EXPRESSION(localVar, classes, unary.right()));
return typedUnary;
} else {
return null;
}
}
}