From 37950dee82ff298506e58ae8a0fe885e7cd0539a Mon Sep 17 00:00:00 2001 From: Ahmad Date: Thu, 9 May 2024 15:45:46 +0200 Subject: [PATCH] Add a help class "TypedExpressionUtil" --- .../typedast/Util/TypedExpressionUtil.java | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/main/java/de/maishai/typedast/Util/TypedExpressionUtil.java diff --git a/src/main/java/de/maishai/typedast/Util/TypedExpressionUtil.java b/src/main/java/de/maishai/typedast/Util/TypedExpressionUtil.java new file mode 100644 index 0000000..72f3dd9 --- /dev/null +++ b/src/main/java/de/maishai/typedast/Util/TypedExpressionUtil.java @@ -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 localVar, Map 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; + } + } +}