diff --git a/src/main/java/de/maishai/typedast/Help/TypedExpressionHelp.java b/src/main/java/de/maishai/typedast/Help/TypedExpressionHelp.java index 2cef079..7a1af45 100644 --- a/src/main/java/de/maishai/typedast/Help/TypedExpressionHelp.java +++ b/src/main/java/de/maishai/typedast/Help/TypedExpressionHelp.java @@ -8,44 +8,28 @@ public class TypedExpressionHelp { public static TypedExpression convertExpression( TypedProgram typedProgram, Expression expression) { if (expression instanceof BoolLiteral boolLiteral) { - TypedBoolLiteral typedBoolLiteral = new TypedBoolLiteral( typedProgram, boolLiteral); - typedBoolLiteral.typeCheck( typedProgram); - return typedBoolLiteral; + return new TypedBoolLiteral(boolLiteral); } else if (expression instanceof CharLiteral charLiteral) { - TypedCharLiteral typedCharLiteral = new TypedCharLiteral( typedProgram, charLiteral); - typedCharLiteral.typeCheck( typedProgram); - return typedCharLiteral; + return new TypedCharLiteral(charLiteral); } else if (expression instanceof IntLiteral intLiteral) { - TypedIntLiteral typedIntLiteral = new TypedIntLiteral( typedProgram, intLiteral); - typedIntLiteral.typeCheck( typedProgram); - return typedIntLiteral; + return new TypedIntLiteral(intLiteral); } else if (expression instanceof Binary binary) { - TypedBinary typedBinary = new TypedBinary( typedProgram, binary); - typedBinary.typeCheck( typedProgram); - return typedBinary; + return new TypedBinary(typedProgram, binary); } else if (expression instanceof FieldVarAccess fieldVarAccess) { - TypedFieldVarAccess typedFieldVarAccess = new TypedFieldVarAccess( typedProgram, fieldVarAccess); - typedFieldVarAccess.typeCheck( typedProgram); - return typedFieldVarAccess; + return new TypedFieldVarAccess(typedProgram, fieldVarAccess); } else if (expression instanceof MethodCall methodCall) { - TypedMethodCall typedMethodCall = new TypedMethodCall( typedProgram, methodCall); - typedMethodCall.typeCheck(typedProgram); - return typedMethodCall; + return new TypedMethodCall(typedProgram, methodCall); } else if (expression instanceof New newStmt) { - TypedNew typedNew = new TypedNew( typedProgram, newStmt); - typedNew.typeCheck( typedProgram); - return typedNew; + return new TypedNew(typedProgram, newStmt); } else if (expression instanceof Unary unary) { - TypedUnary typedUnary = new TypedUnary( typedProgram, unary); - typedUnary.typeCheck( typedProgram); - return typedUnary; + return new TypedUnary(typedProgram, unary); } 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 3bb9ae9..8576a84 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java @@ -20,12 +20,13 @@ public class TypedAssignment implements TypedStatement { public TypedAssignment(TypedProgram typedProgram, Assignment untyped) { convertToTypedAssignment(typedProgram, untyped); + typeCheck(typedProgram); } public void convertToTypedAssignment(TypedProgram typedProgram, Assignment untyped) { value = convertExpression(typedProgram, untyped.value()); location = new TypedFieldVarAccess(typedProgram, untyped.location()); - location.typeCheck(typedProgram); + //location.typeCheck(typedProgram); } @Override diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java b/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java index cbb35b4..30171b2 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java @@ -27,6 +27,7 @@ public class TypedBinary implements TypedExpression { public TypedBinary(TypedProgram typedProgram, Binary unTypedBinary) { convertToTypedBinary(typedProgram, unTypedBinary); + typeCheck(typedProgram); } public void convertToTypedBinary(TypedProgram typedProgram, Binary unTypedBinary) { diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java b/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java index 9a48cf8..52cac9e 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java @@ -18,11 +18,12 @@ import java.util.List; public class TypedBlock implements TypedNode { private List vars = new ArrayList<>(); private List stmts = new ArrayList<>(); - private Type type; + private Type type = Type.VOID; public TypedBlock(TypedProgram typedProgram, Block unTypedBlock) { convertToTypedBlock(typedProgram, unTypedBlock); + clearLocalVariable(typedProgram); } public TypedBlock(List vars, List stmts) { @@ -41,45 +42,31 @@ public class TypedBlock implements TypedNode { vars.add(new TypedLocalVariable(typedProgram, declaration)); } if (stmt instanceof Assignment assignment) { - TypedAssignment typedAssignment = new TypedAssignment(typedProgram, assignment); - typedAssignment.typeCheck(typedProgram); - stmts.add(typedAssignment); + stmts.add(new TypedAssignment(typedProgram, assignment)); continue; } if (stmt instanceof For forStmt) { - TypedFor typedFor = new TypedFor(typedProgram, forStmt); - typedFor.typeCheck(typedProgram); - stmts.add(typedFor); + stmts.add(new TypedFor(typedProgram, forStmt)); continue; } if (stmt instanceof IfElse ifElse) { - TypedIfElse typedIfElse = new TypedIfElse(typedProgram, ifElse); - typedIfElse.typeCheck(typedProgram); - stmts.add(typedIfElse); + stmts.add(new TypedIfElse(typedProgram, ifElse)); continue; } if (stmt instanceof While whileStmt) { - TypedWhile typedWhile = new TypedWhile(typedProgram, whileStmt); - typedWhile.typeCheck(typedProgram); - stmts.add(typedWhile); + stmts.add(new TypedWhile(typedProgram, whileStmt)); continue; } if (stmt instanceof DoWhile doWhile) { - TypedDoWhile typedDoWhile = new TypedDoWhile(typedProgram, doWhile); - typedDoWhile.typeCheck(typedProgram); - stmts.add(typedDoWhile); + stmts.add(new TypedDoWhile(typedProgram, doWhile)); continue; } if (stmt instanceof Return returnStmt) { - TypedReturn typedReturn = new TypedReturn(typedProgram, returnStmt); - typedReturn.typeCheck(typedProgram); - stmts.add(typedReturn); + stmts.add(new TypedReturn(typedProgram, returnStmt)); continue; } if (stmt instanceof New newStmt) { - TypedNew typedNew = new TypedNew(typedProgram, newStmt); - typedNew.typeCheck(typedProgram); - stmts.add(typedNew); + stmts.add(new TypedNew(typedProgram, newStmt)); continue; } @@ -94,9 +81,7 @@ public class TypedBlock implements TypedNode { } if (stmt instanceof MethodCall methodCall) { - TypedMethodCall typedMethodCall = new TypedMethodCall(typedProgram, methodCall); - typedMethodCall.typeCheck(typedProgram); - stmts.add(typedMethodCall); + stmts.add(new TypedMethodCall(typedProgram, methodCall)); } if (stmt instanceof Print printStmt) { @@ -105,24 +90,26 @@ public class TypedBlock implements TypedNode { stmts.add(typedPrint); } } - this.typeCheck(typedProgram); } @Override public Type typeCheck(TypedProgram typedProgram) { - Type chekType = null; - for (TypedStatement stmt : stmts) { - stmt.typeCheck(typedProgram); - if (stmt instanceof TypedReturn returnStmt) { - chekType = returnStmt.getType(); + return type; + } + + private void clearLocalVariable(TypedProgram typedProgram){ + for(var typedLocalVariable : vars){ + if (typedProgram.getCurrentClass().isCurrentMethodPresent() && !typedProgram.getCurrentClass().isCurrentConstructorPresent()) { + if (typedProgram.getCurrentClass().getCurrentMethod().isLocalVariableInMethod(typedLocalVariable.getName())) { + typedProgram.getCurrentClass().getCurrentMethod().deleteLocalVariableInMethod(typedLocalVariable.getName()); + } + } + if (!typedProgram.getCurrentClass().isCurrentMethodPresent() && typedProgram.getCurrentClass().isCurrentConstructorPresent()) { + if (typedProgram.getCurrentClass().getCurrentConstructor().isLocalVariableInConstructor(typedLocalVariable.getName())) { + typedProgram.getCurrentClass().getCurrentConstructor().deleteLocalVariableInConstructor(typedLocalVariable.getName()); + } } } - - if (chekType == null) { - chekType = Type.VOID; - } - type = chekType; - return type; } public void codeGen(MethodContext ctx) { diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java b/src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java index c92301c..75884f3 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java @@ -18,11 +18,11 @@ public class TypedBoolLiteral implements TypedExpression { private Type type; - public TypedBoolLiteral(TypedProgram typedProgram, BoolLiteral unTypedBoolLiteral) { - convertToTypedBoolLiteral(typedProgram, unTypedBoolLiteral); + public TypedBoolLiteral(BoolLiteral unTypedBoolLiteral) { + convertToTypedBoolLiteral(unTypedBoolLiteral); } - public void convertToTypedBoolLiteral(TypedProgram typedProgram, BoolLiteral unTypedBoolLiteral) { + public void convertToTypedBoolLiteral(BoolLiteral unTypedBoolLiteral) { value = unTypedBoolLiteral.value(); type = Type.BOOL; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedCharLiteral.java b/src/main/java/de/maishai/typedast/typedclass/TypedCharLiteral.java index 74b71d7..ee28483 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedCharLiteral.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedCharLiteral.java @@ -13,11 +13,11 @@ public class TypedCharLiteral implements TypedExpression { private char value; private Type type; - public TypedCharLiteral(TypedProgram typedProgram, CharLiteral unTypedCharLiteral) { - convertToCharLiteral(typedProgram, unTypedCharLiteral); + public TypedCharLiteral( CharLiteral unTypedCharLiteral) { + convertToCharLiteral(unTypedCharLiteral); } - public void convertToCharLiteral(TypedProgram typedProgram, CharLiteral unTypedCharLiteral) { + public void convertToCharLiteral(CharLiteral unTypedCharLiteral) { value = unTypedCharLiteral.value(); type = Type.CHAR; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java b/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java index 9fa5953..873866d 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java @@ -71,6 +71,9 @@ public class TypedConstructor implements TypedNode { this.typedBlock = new TypedBlock(typedProgram, unTypedConstructor.block()); typeCheck(typedProgram); } + public void deleteLocalVariableInConstructor(String localVarName){ + localVariables.removeIf(localVariable -> localVariable.getName().equals(localVarName)); + } @Override public Type typeCheck(TypedProgram typedProgram) { diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedDoWhile.java b/src/main/java/de/maishai/typedast/typedclass/TypedDoWhile.java index 75ec717..31294eb 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedDoWhile.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedDoWhile.java @@ -18,6 +18,7 @@ public class TypedDoWhile implements TypedStatement { public TypedDoWhile(TypedProgram typedProgram, DoWhile unTypedDoWhile) { convertToTypedDoWhile(typedProgram, unTypedDoWhile); + typeCheck(typedProgram); } public void convertToTypedDoWhile(TypedProgram typedProgram, DoWhile unTypedDoWhile) { diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java b/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java index 0dd4bea..123a982 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java @@ -25,6 +25,7 @@ public class TypedFieldVarAccess implements TypedExpression { public TypedFieldVarAccess(TypedProgram typedProgram, FieldVarAccess unTypedFieldVarAccess) { convertToTypedFieldVarAccess(typedProgram, unTypedFieldVarAccess); + typeCheck(typedProgram); } public void convertToTypedFieldVarAccess(TypedProgram typedProgram, FieldVarAccess unTypedFieldVarAccess) { diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedFor.java b/src/main/java/de/maishai/typedast/typedclass/TypedFor.java index 60533df..406f6b2 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedFor.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedFor.java @@ -22,6 +22,7 @@ public class TypedFor implements TypedStatement { public TypedFor(TypedProgram typedProgram, For unTypedFor) { convertToTypedFor(typedProgram, unTypedFor); + typeCheck(typedProgram); } public void convertToTypedFor(TypedProgram typedProgram, For unTypedFor) { diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java b/src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java index 70114b0..3ffcf8b 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java @@ -21,6 +21,7 @@ public class TypedIfElse implements TypedStatement { public TypedIfElse(TypedProgram typedProgram, IfElse unTypedIfElse) { convertToTypedIfElse(typedProgram, unTypedIfElse); + typeCheck(typedProgram); } public void convertToTypedIfElse(TypedProgram typedProgram, IfElse unTypedIfElse) { diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedIntLiteral.java b/src/main/java/de/maishai/typedast/typedclass/TypedIntLiteral.java index 9a43fa5..08ec98c 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedIntLiteral.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedIntLiteral.java @@ -20,11 +20,11 @@ public class TypedIntLiteral implements TypedExpression { private Type type; - public TypedIntLiteral(TypedProgram typedProgram, IntLiteral unTypedIntLiteral) { - convertToTypedIntLiteral(typedProgram, unTypedIntLiteral); + public TypedIntLiteral(IntLiteral unTypedIntLiteral) { + convertToTypedIntLiteral(unTypedIntLiteral); } - public void convertToTypedIntLiteral(TypedProgram typedProgram, IntLiteral unTypedIntLiteral) { + public void convertToTypedIntLiteral(IntLiteral unTypedIntLiteral) { value = unTypedIntLiteral.value(); type = Type.INT; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java b/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java index 97e787f..3acbf7f 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java @@ -66,6 +66,9 @@ public class TypedMethod implements TypedNode { return isLocalVariablePresent(localVarName) || isParameterPresent(localVarName); } + public void deleteLocalVariableInMethod(String localVarName){ + localVariables.removeIf(localVariable -> localVariable.getName().equals(localVarName)); + } public Type getLocalVariableType(String localVarName) { return localVariables.stream().filter(localVariable -> localVariable.getName().equals(localVarName)).findFirst().get().getType(); } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java b/src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java index 41f779e..e6ea8a6 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java @@ -25,11 +25,12 @@ public class TypedMethodCall implements TypedExpression, TypedStatement { public TypedMethodCall(TypedProgram typedProgram, MethodCall unTypedMethodCall) { convertToTypedMethodCall(typedProgram, unTypedMethodCall); + typeCheck(typedProgram); } public void convertToTypedMethodCall(TypedProgram typedProgram, MethodCall unTypedMethodCall) { recipient = new TypedFieldVarAccess(typedProgram, unTypedMethodCall.recipient()); - recipient.typeCheck(typedProgram); + //recipient.typeCheck(typedProgram); for (Expression arg : unTypedMethodCall.args()) { args.add(convertExpression(typedProgram, arg)); } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedNew.java b/src/main/java/de/maishai/typedast/typedclass/TypedNew.java index b4c7084..8442f31 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedNew.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedNew.java @@ -21,6 +21,7 @@ public class TypedNew implements TypedExpression, TypedStatement { public TypedNew(TypedProgram typedProgram, New unTypedNew) { convertToTypedNew(typedProgram, unTypedNew); + typeCheck(typedProgram); } public void convertToTypedNew(TypedProgram typedProgram, New unTypedNew) { diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java b/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java index 8f31457..5d33978 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java @@ -19,6 +19,7 @@ public class TypedReturn implements TypedStatement { public TypedReturn(TypedProgram typedProgram, Return unTypedReturn) { convertToTypedReturn(typedProgram, unTypedReturn); + typeCheck(typedProgram); } public void convertToTypedReturn(TypedProgram typedProgram, Return unTypedReturn) { diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedUnary.java b/src/main/java/de/maishai/typedast/typedclass/TypedUnary.java index cc2dbfa..152aa79 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedUnary.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedUnary.java @@ -18,6 +18,7 @@ public class TypedUnary implements TypedExpression { public TypedUnary(TypedProgram typedProgram, Unary unTypedUnary) { convertToTypedUnary(typedProgram, unTypedUnary); + typeCheck(typedProgram); } public void convertToTypedUnary(TypedProgram typedProgram, Unary unTypedUnary) { diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedWhile.java b/src/main/java/de/maishai/typedast/typedclass/TypedWhile.java index dac9507..8119725 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedWhile.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedWhile.java @@ -17,6 +17,7 @@ public class TypedWhile implements TypedStatement { public TypedWhile(TypedProgram typedProgram, While unTypedWhile) { convertToTypedWhile(typedProgram, unTypedWhile); + typeCheck(typedProgram); } public void convertToTypedWhile(TypedProgram typedProgram, While unTypedWhile) {