From 272c25d345bba634ed752e73425c723039da606c Mon Sep 17 00:00:00 2001 From: ahmad Date: Sat, 11 May 2024 14:37:47 +0200 Subject: [PATCH] Made the code better to read --- .../TypedExpressionHelp.java} | 32 +++++++++---------- .../typedast/typedclass/TypedAssignment.java | 3 +- .../typedast/typedclass/TypedBinary.java | 4 +-- .../typedast/typedclass/TypedBlock.java | 5 +-- .../typedast/typedclass/TypedBoolLiteral.java | 2 +- .../typedast/typedclass/TypedCharLiteral.java | 5 +-- .../typedast/typedclass/TypedDoWhile.java | 2 +- .../typedclass/TypedFieldVarAccess.java | 3 +- .../maishai/typedast/typedclass/TypedFor.java | 2 +- .../typedast/typedclass/TypedIfElse.java | 2 +- .../typedast/typedclass/TypedIntLiteral.java | 13 ++++---- .../typedast/typedclass/TypedMethodCall.java | 20 +++++++++--- .../maishai/typedast/typedclass/TypedNew.java | 4 +-- .../typedast/typedclass/TypedReturn.java | 2 +- .../typedast/typedclass/TypedUnary.java | 10 +++--- .../typedast/typedclass/TypedWhile.java | 2 +- 16 files changed, 58 insertions(+), 53 deletions(-) rename src/main/java/de/maishai/typedast/{Util/TypedExpressionUtil.java => Help/TypedExpressionHelp.java} (59%) diff --git a/src/main/java/de/maishai/typedast/Util/TypedExpressionUtil.java b/src/main/java/de/maishai/typedast/Help/TypedExpressionHelp.java similarity index 59% rename from src/main/java/de/maishai/typedast/Util/TypedExpressionUtil.java rename to src/main/java/de/maishai/typedast/Help/TypedExpressionHelp.java index d30b544..4407ea2 100644 --- a/src/main/java/de/maishai/typedast/Util/TypedExpressionUtil.java +++ b/src/main/java/de/maishai/typedast/Help/TypedExpressionHelp.java @@ -1,4 +1,4 @@ -package de.maishai.typedast.Util; +package de.maishai.typedast.Help; import de.maishai.ast.records.*; import de.maishai.typedast.Type; @@ -7,17 +7,23 @@ import de.maishai.typedast.typedclass.*; import java.util.Map; -public class TypedExpressionUtil { +public class TypedExpressionHelp { public static TypedExpression getKindOfExpression(Map localVar, TypedClass clas, Expression expression) { if (expression instanceof BoolLiteral boolLiteral) { - return new TypedBoolLiteral(localVar, clas, boolLiteral); + TypedBoolLiteral typedBoolLiteral = new TypedBoolLiteral(localVar, clas, boolLiteral); + typedBoolLiteral.typeCheck(localVar, clas); + return typedBoolLiteral; } else if (expression instanceof CharLiteral charLiteral) { - return new TypedCharLiteral(localVar, clas, charLiteral); + TypedCharLiteral typedCharLiteral = new TypedCharLiteral(localVar, clas, charLiteral); + typedCharLiteral.typeCheck(localVar, clas); + return typedCharLiteral; } else if (expression instanceof IntLiteral intLiteral) { - return new TypedIntLiteral(localVar, clas, intLiteral); + TypedIntLiteral typedIntLiteral = new TypedIntLiteral(localVar, clas, intLiteral); + typedIntLiteral.typeCheck(localVar, clas); + return typedIntLiteral; } else if (expression instanceof Binary binary) { TypedBinary typedBinary = new TypedBinary(localVar, clas, binary); @@ -30,24 +36,16 @@ public class TypedExpressionUtil { 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(getKindOfExpression(localVar, clas, arg)); - } + TypedMethodCall typedMethodCall = new TypedMethodCall(localVar, clas, methodCall); + typedMethodCall.typeCheck(localVar, clas); return typedMethodCall; } else if (expression instanceof New newStmt) { return new TypedNew(localVar, clas, newStmt); } else if (expression instanceof Unary unary) { - TypedUnary typedUnary = new TypedUnary(); - typedUnary.setOp(unary.op()); - typedUnary.setRight(getKindOfExpression(localVar, clas, unary.right())); + TypedUnary typedUnary = new TypedUnary(localVar, clas, unary); + typedUnary.typeCheck(localVar, clas); return typedUnary; } else { return null; diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java b/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java index faca568..3c5bfa1 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java @@ -3,7 +3,6 @@ package de.maishai.typedast.typedclass; import de.maishai.ast.records.Assignment; import de.maishai.typedast.MethodContext; import de.maishai.typedast.TypedExpression; -import de.maishai.typedast.TypedNode; import de.maishai.typedast.TypedStatement; import de.maishai.typedast.Type; import lombok.Data; @@ -12,7 +11,7 @@ import org.objectweb.asm.Opcodes; import java.util.Map; -import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression; +import static de.maishai.typedast.Help.TypedExpressionHelp.getKindOfExpression; @Data public class TypedAssignment implements TypedStatement { diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java b/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java index 30350a5..d8b493c 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java @@ -2,10 +2,8 @@ package de.maishai.typedast.typedclass; import de.maishai.ast.Operator; import de.maishai.ast.records.Binary; -import de.maishai.ast.records.Node; import de.maishai.typedast.MethodContext; import de.maishai.typedast.TypedExpression; -import de.maishai.typedast.TypedNode; import de.maishai.typedast.Type; import lombok.Data; import lombok.NoArgsConstructor; @@ -13,7 +11,7 @@ import org.objectweb.asm.MethodVisitor; import java.util.Map; -import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression; +import static de.maishai.typedast.Help.TypedExpressionHelp.getKindOfExpression; @Data @NoArgsConstructor diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java b/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java index 94d0b72..76708d1 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java @@ -92,8 +92,9 @@ public class TypedBlock implements TypedNode { } if (stmt instanceof MethodCall methodCall) { - TypedMethodCall typedMethodCall = new TypedMethodCall(); - //TODO: implement this + TypedMethodCall typedMethodCall = new TypedMethodCall(localVar, clas, methodCall); + typedMethodCall.typeCheck(localVar, clas); + stmts.add(typedMethodCall); } } this.typeCheck(localVar, clas); diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java b/src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java index 7159ab1..ae6a978 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java @@ -26,11 +26,11 @@ public class TypedBoolLiteral implements TypedExpression { public void convertToTypedBoolLiteral(Map localVar, TypedClass clas, BoolLiteral unTypedBoolLiteral) { value = unTypedBoolLiteral.value(); + type = Type.BOOL; } @Override public Type typeCheck(Map localVar, TypedClass clas) { - type = Type.BOOL; return type; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedCharLiteral.java b/src/main/java/de/maishai/typedast/typedclass/TypedCharLiteral.java index 2e50184..d4683d8 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedCharLiteral.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedCharLiteral.java @@ -13,17 +13,18 @@ import java.util.Map; @Data public class TypedCharLiteral implements TypedExpression { private char value; - private Type type = Type.CHAR; + private Type type; public TypedCharLiteral(Map localVar, TypedClass clas, CharLiteral unTypedCharLiteral) { convertToCharLiteral(localVar, clas, unTypedCharLiteral); } public void convertToCharLiteral(Map localVar, TypedClass clas, CharLiteral unTypedCharLiteral) { value = unTypedCharLiteral.value(); + type = Type.CHAR; } @Override public Type typeCheck(Map localVar, TypedClass clas) { - return Type.CHAR; + return type; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedDoWhile.java b/src/main/java/de/maishai/typedast/typedclass/TypedDoWhile.java index 4d60e22..d04f5fe 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedDoWhile.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedDoWhile.java @@ -7,7 +7,7 @@ import org.objectweb.asm.MethodVisitor; import java.util.Map; -import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression; +import static de.maishai.typedast.Help.TypedExpressionHelp.getKindOfExpression; @Data public class TypedDoWhile implements TypedStatement { diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java b/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java index 168aaef..0b2d9fe 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java @@ -10,7 +10,7 @@ import org.objectweb.asm.MethodVisitor; import java.util.Map; -import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression; +import static de.maishai.typedast.Help.TypedExpressionHelp.getKindOfExpression; @Data @NoArgsConstructor @@ -27,7 +27,6 @@ public class TypedFieldVarAccess implements TypedExpression { field = unTypedFieldVarAccess.field(); recursiveOwnerChain = getKindOfExpression(localVar, clas, unTypedFieldVarAccess.recursiveOwnerChain()); name = unTypedFieldVarAccess.id(); - } @Override diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedFor.java b/src/main/java/de/maishai/typedast/typedclass/TypedFor.java index 60a16c5..0fa1876 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedFor.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedFor.java @@ -9,7 +9,7 @@ import org.objectweb.asm.MethodVisitor; import java.util.Map; -import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression; +import static de.maishai.typedast.Help.TypedExpressionHelp.getKindOfExpression; @Data @AllArgsConstructor diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java b/src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java index 9b19e88..29498e5 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java @@ -10,7 +10,7 @@ import org.objectweb.asm.Opcodes; import java.util.Map; -import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression; +import static de.maishai.typedast.Help.TypedExpressionHelp.getKindOfExpression; @Data @NoArgsConstructor diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedIntLiteral.java b/src/main/java/de/maishai/typedast/typedclass/TypedIntLiteral.java index e02ec90..8b19937 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedIntLiteral.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedIntLiteral.java @@ -17,22 +17,23 @@ import java.util.Map; @RequiredArgsConstructor public class TypedIntLiteral implements TypedExpression { private Integer value; + private Type type; + public TypedIntLiteral(Map localVar, TypedClass clas, IntLiteral unTypedIntLiteral) { convertToTypedIntLiteral(localVar, clas, unTypedIntLiteral); } - public void convertToTypedIntLiteral(Map localVar, TypedClass clas, IntLiteral unTypedIntLiteral) { value = unTypedIntLiteral.value(); + type = Type.INT; } - private Type type = Type.INT; - public TypedIntLiteral(Integer value) { - this.value = value; - } @Override public Type typeCheck(Map localVar, TypedClass clas) { - return Type.INT; + return type; + } + public TypedIntLiteral(Integer value) { + this.value = value; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java b/src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java index 17ea534..96dbf18 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java @@ -1,28 +1,38 @@ package de.maishai.typedast.typedclass; +import de.maishai.ast.records.Expression; import de.maishai.ast.records.MethodCall; -import de.maishai.ast.records.Node; import de.maishai.typedast.*; import lombok.Data; +import lombok.NoArgsConstructor; import org.objectweb.asm.MethodVisitor; import java.util.List; import java.util.Map; +import static de.maishai.typedast.Help.TypedExpressionHelp.getKindOfExpression; + @Data +@NoArgsConstructor public class TypedMethodCall implements TypedExpression, TypedStatement { private TypedFieldVarAccess recipient; private List args; private Type type; - public TypedMethodCall convertToTypedMethodCall(MethodCall unTypedMethodCall) { - //TODO: Implement this - return null; + public TypedMethodCall(Map localVar, TypedClass clas, MethodCall unTypedMethodCall) { + convertToTypedMethodCall(localVar, clas, unTypedMethodCall); + } + + public void convertToTypedMethodCall(Map localVar, TypedClass clas, MethodCall unTypedMethodCall) { + recipient = new TypedFieldVarAccess(localVar, clas, unTypedMethodCall.recipient()); + for (Expression arg : unTypedMethodCall.args()) { + args.add(getKindOfExpression(localVar, clas, arg)); + } } @Override public Type typeCheck(Map localVar, TypedClass clas) { - //TODO: Implement typeCheck for MethodCall + //TODO: Implement typeCheck return null; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedNew.java b/src/main/java/de/maishai/typedast/typedclass/TypedNew.java index ca922b8..8ace078 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedNew.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedNew.java @@ -2,8 +2,6 @@ package de.maishai.typedast.typedclass; import de.maishai.ast.records.Expression; import de.maishai.ast.records.New; -import de.maishai.ast.records.Node; -import de.maishai.ast.records.Parameter; import de.maishai.typedast.*; import lombok.Data; import org.objectweb.asm.MethodVisitor; @@ -12,7 +10,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; -import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression; +import static de.maishai.typedast.Help.TypedExpressionHelp.getKindOfExpression; @Data public class TypedNew implements TypedExpression, TypedStatement { diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java b/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java index 83ba604..e960d99 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java @@ -10,7 +10,7 @@ import org.objectweb.asm.Opcodes; import java.util.Map; -import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression; +import static de.maishai.typedast.Help.TypedExpressionHelp.getKindOfExpression; @Data @NoArgsConstructor diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedUnary.java b/src/main/java/de/maishai/typedast/typedclass/TypedUnary.java index b28190f..4f82630 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedUnary.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedUnary.java @@ -1,11 +1,9 @@ package de.maishai.typedast.typedclass; import de.maishai.ast.UnaryOperator; -import de.maishai.ast.records.Node; import de.maishai.ast.records.Unary; import de.maishai.typedast.MethodContext; import de.maishai.typedast.TypedExpression; -import de.maishai.typedast.TypedNode; import de.maishai.typedast.Type; import lombok.Data; import org.objectweb.asm.MethodVisitor; @@ -13,7 +11,7 @@ import org.objectweb.asm.Opcodes; import java.util.Map; -import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression; +import static de.maishai.typedast.Help.TypedExpressionHelp.getKindOfExpression; @Data public class TypedUnary implements TypedExpression { @@ -21,10 +19,12 @@ public class TypedUnary implements TypedExpression { private TypedExpression right; private Type type; - public TypedUnary convertToTypedUnary(Map localVar, TypedClass clas, Unary unTypedUnary) { + public TypedUnary(Map localVar, TypedClass clas, Unary unTypedUnary){ + convertToTypedUnary(localVar, clas, unTypedUnary); + } + public void convertToTypedUnary(Map localVar, TypedClass clas, Unary unTypedUnary) { op = unTypedUnary.op(); right = getKindOfExpression(localVar, clas, unTypedUnary.right()); - return this; } @Override public Type typeCheck(Map localVar, TypedClass clas) { diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedWhile.java b/src/main/java/de/maishai/typedast/typedclass/TypedWhile.java index 8e83180..291445d 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedWhile.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedWhile.java @@ -7,7 +7,7 @@ import org.objectweb.asm.MethodVisitor; import java.util.Map; -import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression; +import static de.maishai.typedast.Help.TypedExpressionHelp.getKindOfExpression; @Data public class TypedWhile implements TypedStatement {