diff --git a/src/main/java/de/maishai/typedast/Help/TypedExpressionHelp.java b/src/main/java/de/maishai/typedast/Help/TypedExpressionHelp.java index 368c513..2f664e0 100644 --- a/src/main/java/de/maishai/typedast/Help/TypedExpressionHelp.java +++ b/src/main/java/de/maishai/typedast/Help/TypedExpressionHelp.java @@ -9,45 +9,45 @@ import java.util.Map; public class TypedExpressionHelp { - public static TypedExpression convertExpression(Map localVar, TypedClass clas, Expression expression) { + public static TypedExpression convertExpression( TypedClass clas, Expression expression) { if (expression instanceof BoolLiteral boolLiteral) { - TypedBoolLiteral typedBoolLiteral = new TypedBoolLiteral(localVar, clas, boolLiteral); - typedBoolLiteral.typeCheck(localVar, clas); + TypedBoolLiteral typedBoolLiteral = new TypedBoolLiteral( clas, boolLiteral); + typedBoolLiteral.typeCheck( clas); return typedBoolLiteral; } else if (expression instanceof CharLiteral charLiteral) { - TypedCharLiteral typedCharLiteral = new TypedCharLiteral(localVar, clas, charLiteral); - typedCharLiteral.typeCheck(localVar, clas); + TypedCharLiteral typedCharLiteral = new TypedCharLiteral( clas, charLiteral); + typedCharLiteral.typeCheck( clas); return typedCharLiteral; } else if (expression instanceof IntLiteral intLiteral) { - TypedIntLiteral typedIntLiteral = new TypedIntLiteral(localVar, clas, intLiteral); - typedIntLiteral.typeCheck(localVar, clas); + TypedIntLiteral typedIntLiteral = new TypedIntLiteral( clas, intLiteral); + typedIntLiteral.typeCheck( clas); return typedIntLiteral; } else if (expression instanceof Binary binary) { - TypedBinary typedBinary = new TypedBinary(localVar, clas, binary); - typedBinary.typeCheck(localVar, clas); + TypedBinary typedBinary = new TypedBinary( clas, binary); + typedBinary.typeCheck( clas); return typedBinary; } else if (expression instanceof FieldVarAccess fieldVarAccess) { - TypedFieldVarAccess typedFieldVarAccess = new TypedFieldVarAccess(localVar, clas, fieldVarAccess); - typedFieldVarAccess.typeCheck(localVar, clas); + TypedFieldVarAccess typedFieldVarAccess = new TypedFieldVarAccess( clas, fieldVarAccess); + typedFieldVarAccess.typeCheck( clas); return typedFieldVarAccess; } else if (expression instanceof MethodCall methodCall) { - TypedMethodCall typedMethodCall = new TypedMethodCall(localVar, clas, methodCall); - typedMethodCall.typeCheck(localVar, clas); + TypedMethodCall typedMethodCall = new TypedMethodCall( clas, methodCall); + typedMethodCall.typeCheck( clas); return typedMethodCall; } else if (expression instanceof New newStmt) { - TypedNew typedNew = new TypedNew(localVar, clas, newStmt); - typedNew.typeCheck(localVar, clas); + TypedNew typedNew = new TypedNew( clas, newStmt); + typedNew.typeCheck( clas); return typedNew; } else if (expression instanceof Unary unary) { - TypedUnary typedUnary = new TypedUnary(localVar, clas, unary); - typedUnary.typeCheck(localVar, clas); + TypedUnary typedUnary = new TypedUnary( clas, unary); + typedUnary.typeCheck( clas); return typedUnary; } else { return null; diff --git a/src/main/java/de/maishai/typedast/TypedNode.java b/src/main/java/de/maishai/typedast/TypedNode.java index fe3f653..a608e77 100644 --- a/src/main/java/de/maishai/typedast/TypedNode.java +++ b/src/main/java/de/maishai/typedast/TypedNode.java @@ -1,10 +1,7 @@ package de.maishai.typedast; -import de.maishai.ast.records.Node; import de.maishai.typedast.typedclass.TypedClass; -import java.util.Map; - public interface TypedNode { - Type typeCheck(Map localVar, TypedClass clas); + Type typeCheck(TypedClass clas); } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java b/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java index fed0d27..b7313bc 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java @@ -21,18 +21,18 @@ public class TypedAssignment implements TypedStatement { // private TypedExpression recursiveOwnerChain; private Type type; - public TypedAssignment(Map localVar, TypedClass clas, Assignment untyped) { - convertToTypedAssignment(localVar, clas, untyped); + public TypedAssignment(TypedClass clas, Assignment untyped) { + convertToTypedAssignment(clas, untyped); } - public void convertToTypedAssignment(Map localVar, TypedClass clas, Assignment untyped) { + public void convertToTypedAssignment(TypedClass clas, Assignment untyped) { varName = untyped.location().id(); - value = convertExpression(localVar, clas, untyped.value()); - // recursiveOwnerChain = convertExpression(localVar, clas, untyped.location().recursiveOwnerChain()); + value = convertExpression(clas, untyped.value()); + // recursiveOwnerChain = convertExpression( clas, untyped.location().recursiveOwnerChain()); } @Override - public Type typeCheck(Map localVar, TypedClass clas) { + public Type typeCheck(TypedClass clas) { Type typeLeft = null; if (clas.isThereField(varName)) { @@ -54,7 +54,7 @@ public class TypedAssignment implements TypedStatement { } } - Type typeRight = value.typeCheck(localVar, clas); + Type typeRight = value.typeCheck(clas); if (typeLeft.equals(typeRight)) { type = typeLeft; diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java b/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java index e882eea..1b5572b 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java @@ -21,20 +21,20 @@ public class TypedBinary implements TypedExpression { private TypedExpression right; private Type type; - public TypedBinary(Map localVar, TypedClass clas, Binary unTypedBinary) { - convertToTypedBinary(localVar, clas, unTypedBinary); + public TypedBinary(TypedClass clas, Binary unTypedBinary) { + convertToTypedBinary(clas, unTypedBinary); } - public void convertToTypedBinary(Map localVar, TypedClass clas, Binary unTypedBinary) { - left = convertExpression(localVar, clas, unTypedBinary.left()); - right = convertExpression(localVar, clas, unTypedBinary.right()); + public void convertToTypedBinary(TypedClass clas, Binary unTypedBinary) { + left = convertExpression(clas, unTypedBinary.left()); + right = convertExpression(clas, unTypedBinary.right()); op = unTypedBinary.op(); } @Override - public Type typeCheck(Map localVar, TypedClass clas) { - Type leftType = left.typeCheck(localVar, clas); - Type rightType = right.typeCheck(localVar, clas); + public Type typeCheck(TypedClass clas) { + Type leftType = left.typeCheck(clas); + Type rightType = right.typeCheck(clas); if (op == Operator.ADD || op == Operator.SUB || op == Operator.MUL) { if (leftType == Type.INT && rightType == Type.INT) { diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java b/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java index c1eeaf3..d1e3449 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java @@ -23,8 +23,8 @@ public class TypedBlock implements TypedNode { private Type type; - public TypedBlock(Map localVar, TypedClass clas, Block unTypedBlock) { - convertToTypedBlock(localVar, clas, unTypedBlock); + public TypedBlock(TypedClass clas, Block unTypedBlock) { + convertToTypedBlock(clas, unTypedBlock); } public TypedBlock(List vars, List stmts) { @@ -32,56 +32,56 @@ public class TypedBlock implements TypedNode { this.stmts = stmts; } - public void convertToTypedBlock(Map localVar, TypedClass clas, Block unTypedBlock) { + public void convertToTypedBlock(TypedClass clas, Block unTypedBlock) { if (unTypedBlock == null) { return; } for (Declaration var : unTypedBlock.localVariables()) { - vars.add(new TypedLocalVariable(localVar, clas, var)); + vars.add(new TypedLocalVariable(clas, var)); } for (var stmt : unTypedBlock.stmts()) { if (stmt instanceof Assignment assignment) { - TypedAssignment typedAssignment = new TypedAssignment(localVar, clas, assignment); - typedAssignment.typeCheck(localVar, clas); + TypedAssignment typedAssignment = new TypedAssignment(clas, assignment); + typedAssignment.typeCheck(clas); stmts.add(typedAssignment); continue; } if (stmt instanceof For forStmt) { - TypedFor typedFor = new TypedFor(localVar, clas, forStmt); - typedFor.typeCheck(localVar, clas); + TypedFor typedFor = new TypedFor(clas, forStmt); + typedFor.typeCheck(clas); stmts.add(typedFor); continue; } if (stmt instanceof IfElse ifElse) { - TypedIfElse typedIfElse = new TypedIfElse(localVar, clas, ifElse); - typedIfElse.typeCheck(localVar, clas); + TypedIfElse typedIfElse = new TypedIfElse(clas, ifElse); + typedIfElse.typeCheck(clas); stmts.add(typedIfElse); continue; } if (stmt instanceof While whileStmt) { - TypedWhile typedWhile = new TypedWhile(localVar, clas, whileStmt); - typedWhile.typeCheck(localVar, clas); + TypedWhile typedWhile = new TypedWhile(clas, whileStmt); + typedWhile.typeCheck(clas); stmts.add(typedWhile); continue; } if (stmt instanceof DoWhile doWhile) { - TypedDoWhile typedDoWhile = new TypedDoWhile(localVar, clas, doWhile); - typedDoWhile.typeCheck(localVar, clas); + TypedDoWhile typedDoWhile = new TypedDoWhile(clas, doWhile); + typedDoWhile.typeCheck(clas); stmts.add(typedDoWhile); continue; } if (stmt instanceof Return returnStmt) { - TypedReturn typedReturn = new TypedReturn(localVar, clas, returnStmt); - typedReturn.typeCheck(localVar, clas); + TypedReturn typedReturn = new TypedReturn(clas, returnStmt); + typedReturn.typeCheck(clas); stmts.add(typedReturn); continue; } if (stmt instanceof New newStmt) { - TypedNew typedNew = new TypedNew(localVar, clas, newStmt); - typedNew.typeCheck(localVar, clas); + TypedNew typedNew = new TypedNew(clas, newStmt); + typedNew.typeCheck(clas); stmts.add(typedNew); continue; } @@ -92,20 +92,20 @@ public class TypedBlock implements TypedNode { } if (stmt instanceof MethodCall methodCall) { - TypedMethodCall typedMethodCall = new TypedMethodCall(localVar, clas, methodCall); - typedMethodCall.typeCheck(localVar, clas); + TypedMethodCall typedMethodCall = new TypedMethodCall(clas, methodCall); + typedMethodCall.typeCheck(clas); stmts.add(typedMethodCall); } } - this.typeCheck(localVar, clas); + this.typeCheck(clas); } @Override - public Type typeCheck(Map localVar, TypedClass clas) { + public Type typeCheck(TypedClass clas) { for (TypedStatement stmt : stmts) { - stmt.typeCheck(localVar, clas); + stmt.typeCheck(clas); } //TODO: Type von Return zurückgeben diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java b/src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java index 027a362..a9ed546 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java @@ -21,17 +21,17 @@ public class TypedBoolLiteral implements TypedExpression { private Type type; - public TypedBoolLiteral(Map localVar, TypedClass clas, BoolLiteral unTypedBoolLiteral) { - convertToTypedBoolLiteral(localVar, clas, unTypedBoolLiteral); + public TypedBoolLiteral(TypedClass clas, BoolLiteral unTypedBoolLiteral) { + convertToTypedBoolLiteral(clas, unTypedBoolLiteral); } - public void convertToTypedBoolLiteral(Map localVar, TypedClass clas, BoolLiteral unTypedBoolLiteral) { + public void convertToTypedBoolLiteral(TypedClass clas, BoolLiteral unTypedBoolLiteral) { value = unTypedBoolLiteral.value(); type = Type.BOOL; } @Override - public Type typeCheck(Map localVar, TypedClass clas) { + public Type typeCheck(TypedClass clas) { return type; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedBreak.java b/src/main/java/de/maishai/typedast/typedclass/TypedBreak.java index 8e22ca0..b61c1e4 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedBreak.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBreak.java @@ -15,11 +15,12 @@ import java.util.Map; public class TypedBreak implements TypedStatement { private Type type = Type.VOID; - public TypedBreak convertToTypedBreak(Map localVar, TypedClass clas, Break unTypedBreak) { + public TypedBreak convertToTypedBreak(TypedClass clas, Break unTypedBreak) { return this; } + @Override - public Type typeCheck(Map localVar, TypedClass clas) { + public Type typeCheck(TypedClass clas) { 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 42e64c5..83486d2 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedCharLiteral.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedCharLiteral.java @@ -16,17 +16,17 @@ public class TypedCharLiteral implements TypedExpression { private char value; private Type type; - public TypedCharLiteral(Map localVar, TypedClass clas, CharLiteral unTypedCharLiteral) { - convertToCharLiteral(localVar, clas, unTypedCharLiteral); + public TypedCharLiteral(TypedClass clas, CharLiteral unTypedCharLiteral) { + convertToCharLiteral(clas, unTypedCharLiteral); } - public void convertToCharLiteral(Map localVar, TypedClass clas, CharLiteral unTypedCharLiteral) { + public void convertToCharLiteral(TypedClass clas, CharLiteral unTypedCharLiteral) { value = unTypedCharLiteral.value(); type = Type.CHAR; } @Override - public Type typeCheck(Map localVar, TypedClass clas) { + public Type typeCheck(TypedClass clas) { return type; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedClass.java b/src/main/java/de/maishai/typedast/typedclass/TypedClass.java index 247fd15..08bebcf 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedClass.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedClass.java @@ -29,8 +29,8 @@ public class TypedClass implements TypedNode { private TypedConstructor currentConstructor; private Type type; - public TypedClass(Map localVar, Class c) { - convertToTypedClass(localVar, c); + public TypedClass(Class c) { + convertToTypedClass(c); } public void enterCurrentMethod(TypedMethod method) { @@ -81,28 +81,28 @@ public class TypedClass implements TypedNode { return false; } - public void convertToTypedClass(Map localVar, Class c) { + public void convertToTypedClass(Class c) { className = c.classname(); type = Type.REFERENCE(className); for (Declaration declaration : c.fieldDeclarations()) { - typedDeclarations.add(new TypedDeclaration(localVar, this, declaration)); + typedDeclarations.add(new TypedDeclaration(this, declaration)); } // Am Anfang werden die Konstruktoren und Methoden in die Listen eingefügt // Methoden können verwendet werden, bevor deren Blöcke ausgeführt werden for (Constructor constructor : c.constructors()) { - typedConstructors.add(new TypedConstructor(localVar, this, constructor)); + typedConstructors.add(new TypedConstructor(this, constructor)); } for (Method method : c.methods()) { - typedMethods.add(new TypedMethod(localVar, this, method)); + typedMethods.add(new TypedMethod(this, method)); } // Hier werden die Blöcke der Konstruktoren und Methoden ausgeführt int i = 0; for (Constructor constructor : c.constructors()) { enterCurrentConstructor(typedConstructors.get(i)); - typedConstructors.get(i).convertToBlock(localVar, this, constructor); + typedConstructors.get(i).convertToBlock(this, constructor); exitCurrentConstructor(); i++; } @@ -110,7 +110,7 @@ public class TypedClass implements TypedNode { int j = 0; for (Method method : c.methods()) { enterCurrentMethod(typedMethods.get(j)); - typedMethods.get(j).convertToTypedBlock(localVar, this, method); + typedMethods.get(j).convertToTypedBlock(this, method); exitCurrentMethod(); j++; } @@ -118,14 +118,14 @@ public class TypedClass implements TypedNode { } @Override - public Type typeCheck(Map localVar, TypedClass clas) { + public Type typeCheck(TypedClass clas) { return type; } public TypedNode startConversion(Class c) { Map local = new HashMap<>(); - return new TypedClass(local, c); + return new TypedClass(c); } public boolean isParameterWitNameInMethod(String parameterName) { diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java b/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java index 4ef748d..8942e3b 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java @@ -34,11 +34,11 @@ public class TypedConstructor implements TypedNode { this.typedBlock = typedBlock; } - public TypedConstructor(Map localVar, TypedClass clas, Constructor unTypedConstructor) { - convertToTypedConstructor(localVar, clas, unTypedConstructor); + public TypedConstructor(TypedClass clas, Constructor unTypedConstructor) { + convertToTypedConstructor(clas, unTypedConstructor); } - public boolean isLocalVariablePresent(String localVarName) { + public boolean isLocalVariablePresent(String localVarName) { return localVariables.stream().anyMatch(localVariable -> localVariable.getName().equals(localVarName)); } @@ -49,25 +49,26 @@ public class TypedConstructor implements TypedNode { public boolean isLocalVariableInConstructor(String localVarName) { return isLocalVariablePresent(localVarName) || isParameterPresent(localVarName); } + public Type getLocalVariableType(String localVarName) { return localVariables.stream().filter(localVariable -> localVariable.getName().equals(localVarName)).findFirst().get().getType(); } - public void convertToTypedConstructor(Map localVar, TypedClass clas, Constructor unTypedConstructor) { + public void convertToTypedConstructor(TypedClass clas, Constructor unTypedConstructor) { name = unTypedConstructor.className(); for (Parameter param : unTypedConstructor.params()) { - typedParameters.add(new TypedParameter(localVar, clas, param)); + typedParameters.add(new TypedParameter(clas, param)); } type = Type.VOID; } - public void convertToBlock(Map localVar, TypedClass clas, Constructor unTypedConstructor) { - this.typedBlock = new TypedBlock(localVar, clas, unTypedConstructor.block()); + public void convertToBlock(TypedClass clas, Constructor unTypedConstructor) { + this.typedBlock = new TypedBlock(clas, unTypedConstructor.block()); } @Override - public Type typeCheck(Map localVar, TypedClass clas) { + public Type typeCheck(TypedClass clas) { //TODO: check if return is there return type; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedDeclaration.java b/src/main/java/de/maishai/typedast/typedclass/TypedDeclaration.java index 31948ff..e8939c6 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedDeclaration.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedDeclaration.java @@ -22,21 +22,18 @@ public final class TypedDeclaration implements TypedNode { private String name; private Type type; - public TypedDeclaration(Map localVar, TypedClass clas, Declaration declaration) { - convertToTypedDeclaration(localVar, clas, declaration); + public TypedDeclaration(TypedClass clas, Declaration declaration) { + convertToTypedDeclaration(clas, declaration); } - public void convertToTypedDeclaration(Map localVar, TypedClass clas, Declaration declaration) { - if (localVar.containsKey(declaration.name())) { - throw new RuntimeException("Variable " + declaration.name() + " already declared"); - } + public void convertToTypedDeclaration(TypedClass clas, Declaration declaration) { name = declaration.name(); type = declaration.type(); - typeCheck(localVar, clas); + typeCheck(clas); } @Override - public Type typeCheck(Map localVar, TypedClass clas) { + public Type typeCheck(TypedClass clas) { if (clas.isThereField(name)) { throw new RuntimeException("Field " + name + " already declared"); } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedDoWhile.java b/src/main/java/de/maishai/typedast/typedclass/TypedDoWhile.java index 5590261..25f2990 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedDoWhile.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedDoWhile.java @@ -15,21 +15,21 @@ public class TypedDoWhile implements TypedStatement { private TypedExpression cond; private Type type; - public TypedDoWhile(Map localVar, TypedClass clas, DoWhile unTypedDoWhile) { - convertToTypedDoWhile(localVar, clas, unTypedDoWhile); + public TypedDoWhile(TypedClass clas, DoWhile unTypedDoWhile) { + convertToTypedDoWhile(clas, unTypedDoWhile); } - public void convertToTypedDoWhile(Map localVar, TypedClass clas, DoWhile unTypedDoWhile) { - typedBlock = new TypedBlock(localVar, clas, unTypedDoWhile.block()); - cond = convertExpression(localVar, clas, unTypedDoWhile.cond()); + public void convertToTypedDoWhile(TypedClass clas, DoWhile unTypedDoWhile) { + typedBlock = new TypedBlock(clas, unTypedDoWhile.block()); + cond = convertExpression(clas, unTypedDoWhile.cond()); } @Override - public Type typeCheck(Map localVar, TypedClass clas) { - if (cond.typeCheck(localVar, clas) != Type.BOOL) { + public Type typeCheck(TypedClass clas) { + if (cond.typeCheck(clas) != Type.BOOL) { throw new RuntimeException("Condition must be boolean"); } - typedBlock.typeCheck(localVar, clas); + typedBlock.typeCheck(clas); this.type = Type.VOID; return Type.VOID; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java b/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java index fa04377..fad4f23 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java @@ -20,18 +20,18 @@ public class TypedFieldVarAccess implements TypedExpression { private String name; private Type type; - public TypedFieldVarAccess(Map localVar, TypedClass clas, FieldVarAccess unTypedFieldVarAccess) { - convertToTypedFieldVarAccess(localVar, clas, unTypedFieldVarAccess); + public TypedFieldVarAccess(TypedClass clas, FieldVarAccess unTypedFieldVarAccess) { + convertToTypedFieldVarAccess(clas, unTypedFieldVarAccess); } - public void convertToTypedFieldVarAccess(Map localVar, TypedClass clas, FieldVarAccess unTypedFieldVarAccess) { + public void convertToTypedFieldVarAccess(TypedClass clas, FieldVarAccess unTypedFieldVarAccess) { field = unTypedFieldVarAccess.field(); - recursiveOwnerChain = convertExpression(localVar, clas, unTypedFieldVarAccess.recursiveOwnerChain()); + recursiveOwnerChain = convertExpression(clas, unTypedFieldVarAccess.recursiveOwnerChain()); name = unTypedFieldVarAccess.id(); } @Override - public Type typeCheck(Map localVar, TypedClass clas) { + public Type typeCheck(TypedClass clas) { if (field) { if (clas.isThereField(name)) { type = clas.getFieldType(name); @@ -52,11 +52,11 @@ public class TypedFieldVarAccess implements TypedExpression { type = clas.getParameterTypeInCurrentMethod(name); return clas.getParameterTypeInCurrentMethod(name); } - } + }/* if (localVar.containsKey(name)) { type = localVar.get(name); return localVar.get(name); - } + }*/ } throw new RuntimeException("Variable " + name + " not declared "); diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedFor.java b/src/main/java/de/maishai/typedast/typedclass/TypedFor.java index 0d6957c..8f8f4cd 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedFor.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedFor.java @@ -22,25 +22,25 @@ public class TypedFor implements TypedStatement { //TODO: type of block in for loop private Type type; - public TypedFor(Map localVar, TypedClass clas, For unTypedFor) { - convertToTypedFor(localVar, clas, unTypedFor); + public TypedFor(TypedClass clas, For unTypedFor) { + convertToTypedFor(clas, unTypedFor); } - public void convertToTypedFor(Map localVar, TypedClass clas, For unTypedFor) { - assign = new TypedAssignment(localVar, clas, unTypedFor.assign()); - cond = convertExpression(localVar, clas, unTypedFor.cond()); - inc = new TypedAssignment(localVar, clas, unTypedFor.inc()); - typedBlock = new TypedBlock(localVar, clas, unTypedFor.block()); + public void convertToTypedFor(TypedClass clas, For unTypedFor) { + assign = new TypedAssignment(clas, unTypedFor.assign()); + cond = convertExpression(clas, unTypedFor.cond()); + inc = new TypedAssignment(clas, unTypedFor.inc()); + typedBlock = new TypedBlock(clas, unTypedFor.block()); } @Override - public Type typeCheck(Map localVar, TypedClass clas) { - assign.typeCheck(localVar, clas); - if (!cond.typeCheck(localVar, clas).equals(Type.BOOL)) { + public Type typeCheck(TypedClass clas) { + assign.typeCheck(clas); + if (!cond.typeCheck(clas).equals(Type.BOOL)) { throw new RuntimeException("Condition must be a boolean"); } - inc.typeCheck(localVar, clas); - type = typedBlock.typeCheck(localVar, clas); + inc.typeCheck(clas); + type = typedBlock.typeCheck(clas); return type; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java b/src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java index d17867b..ea06146 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java @@ -21,29 +21,29 @@ public class TypedIfElse implements TypedStatement { //TODO: add Type private Type type; - public TypedIfElse(Map localVar, TypedClass clas, IfElse unTypedIfElse) { - convertToTypedIfElse(localVar, clas, unTypedIfElse); + public TypedIfElse(TypedClass clas, IfElse unTypedIfElse) { + convertToTypedIfElse(clas, unTypedIfElse); } - public void convertToTypedIfElse(Map localVar, TypedClass clas, IfElse unTypedIfElse) { - ifTypedBlock = new TypedBlock(localVar, clas, unTypedIfElse.ifBlock()); - elseTypedBlock = new TypedBlock(localVar, clas, unTypedIfElse.elseBlock()); - typedCon = convertExpression(localVar, clas, unTypedIfElse.cond()); + public void convertToTypedIfElse(TypedClass clas, IfElse unTypedIfElse) { + ifTypedBlock = new TypedBlock(clas, unTypedIfElse.ifBlock()); + elseTypedBlock = new TypedBlock(clas, unTypedIfElse.elseBlock()); + typedCon = convertExpression(clas, unTypedIfElse.cond()); } @Override - public Type typeCheck(Map localVar, TypedClass clas) { + public Type typeCheck(TypedClass clas) { - if (typedCon.typeCheck(localVar, clas) != Type.BOOL) { + if (typedCon.typeCheck(clas) != Type.BOOL) { throw new RuntimeException("If condition must be a boolean"); } - if (ifTypedBlock.typeCheck(localVar, clas) != Type.VOID) { + if (ifTypedBlock.typeCheck(clas) != Type.VOID) { throw new RuntimeException("If block must be of type void"); } //TODO: catch all cases of return when one method has a return type - if (ifTypedBlock.typeCheck(localVar, clas) == elseTypedBlock.typeCheck(localVar, clas)) { - type = ifTypedBlock.typeCheck(localVar, clas); + if (ifTypedBlock.typeCheck(clas) == elseTypedBlock.typeCheck(clas)) { + type = ifTypedBlock.typeCheck(clas); } return type; diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedIntLiteral.java b/src/main/java/de/maishai/typedast/typedclass/TypedIntLiteral.java index 33bf693..9e1f51b 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedIntLiteral.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedIntLiteral.java @@ -20,17 +20,17 @@ public class TypedIntLiteral implements TypedExpression { private Type type; - public TypedIntLiteral(Map localVar, TypedClass clas, IntLiteral unTypedIntLiteral) { - convertToTypedIntLiteral(localVar, clas, unTypedIntLiteral); + public TypedIntLiteral(TypedClass clas, IntLiteral unTypedIntLiteral) { + convertToTypedIntLiteral(clas, unTypedIntLiteral); } - public void convertToTypedIntLiteral(Map localVar, TypedClass clas, IntLiteral unTypedIntLiteral) { + public void convertToTypedIntLiteral(TypedClass clas, IntLiteral unTypedIntLiteral) { value = unTypedIntLiteral.value(); type = Type.INT; } @Override - public Type typeCheck(Map localVar, TypedClass clas) { + public Type typeCheck(TypedClass clas) { return type; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java b/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java index 0c301a6..1a282e4 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java @@ -23,34 +23,31 @@ public final class TypedLocalVariable implements TypedNode { private String name; private Type type; - public TypedLocalVariable(Map localVar, TypedClass clas, Declaration declaration) { - convertToTypedLocalVariable(localVar, clas, declaration); + public TypedLocalVariable(TypedClass clas, Declaration declaration) { + convertToTypedLocalVariable(clas, declaration); } - public void convertToTypedLocalVariable(Map localVar, TypedClass clas, Declaration declaration) { - if (localVar.containsKey(declaration.name())) { - throw new RuntimeException("Variable " + declaration.name() + " already declared"); - } + public void convertToTypedLocalVariable(TypedClass clas, Declaration declaration) { name = declaration.name(); type = declaration.type(); - typeCheck(localVar, clas); + typeCheck(clas); } @Override - public Type typeCheck(Map localVar, TypedClass clas) { - if (clas.isCurrentMethodPresent() && !clas.isCurrentConstructorPresent()){ - if(clas.getCurrentMethod().isLocalVariableInMethod(name)){ - throw new RuntimeException("Variable " + name + " already declared"); - } - clas.getCurrentMethod().getLocalVariables().add(this); - return type; - } - if (!clas.isCurrentMethodPresent() && clas.isCurrentConstructorPresent()){ - if(clas.getCurrentConstructor().isLocalVariableInConstructor(name)){ - throw new RuntimeException("Variable " + name + " already declared"); - } - clas.getCurrentConstructor().getLocalVariables().add(this); - return type; + public Type typeCheck(TypedClass clas) { + if (clas.isCurrentMethodPresent() && !clas.isCurrentConstructorPresent()) { + if (clas.getCurrentMethod().isLocalVariableInMethod(name)) { + throw new RuntimeException("Variable " + name + " already declared"); + } + clas.getCurrentMethod().getLocalVariables().add(this); + return type; + } + if (!clas.isCurrentMethodPresent() && clas.isCurrentConstructorPresent()) { + if (clas.getCurrentConstructor().isLocalVariableInConstructor(name)) { + throw new RuntimeException("Variable " + name + " already declared"); + } + clas.getCurrentConstructor().getLocalVariables().add(this); + return type; } throw new RuntimeException("not found method or constructor in class"); diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java b/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java index cdee9cf..e9c4f38 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java @@ -23,16 +23,16 @@ public class TypedMethod implements TypedNode { private TypedBlock typedBlock; private List localVariables = new ArrayList<>(); - public TypedMethod(Map localVar, TypedClass clas, Method unTypedMethod) { - convertToTypedMethod(localVar, clas, unTypedMethod); + public TypedMethod(TypedClass clas, Method unTypedMethod) { + convertToTypedMethod(clas, unTypedMethod); } - public void convertToTypedMethod(Map localVar, TypedClass clas, Method unTypedMethod) { + public void convertToTypedMethod(TypedClass clas, Method unTypedMethod) { name = unTypedMethod.methodName(); returnType = unTypedMethod.type(); for (var parameter : unTypedMethod.params()) { - typedParameters.add(new TypedParameter(localVar, clas, parameter)); + typedParameters.add(new TypedParameter(clas, parameter)); } clas.getTypedMethods().stream().filter(method -> method.equals(this)).findFirst().ifPresentOrElse( @@ -41,15 +41,17 @@ public class TypedMethod implements TypedNode { }, () -> { }); - localVar.put(name, returnType); + //localVar.put(name, returnType); } - public void convertToTypedBlock(Map localVar, TypedClass clas, Method unTypedMethod) { - typedBlock = new TypedBlock(localVar, clas, unTypedMethod.block()); + public void convertToTypedBlock(TypedClass clas, Method unTypedMethod) { + typedBlock = new TypedBlock(clas, unTypedMethod.block()); } + public boolean isLocalVariablePresent(String localVarName) { return localVariables.stream().anyMatch(localVariable -> localVariable.getName().equals(localVarName)); } + public boolean isParameterPresent(String parameterName) { return typedParameters.stream().anyMatch(parameter -> parameter.getParaName().equals(parameterName)); } @@ -57,20 +59,21 @@ public class TypedMethod implements TypedNode { public boolean isLocalVariableInMethod(String localVarName) { return isLocalVariablePresent(localVarName) || isParameterPresent(localVarName); } + public Type getLocalVariableType(String localVarName) { return localVariables.stream().filter(localVariable -> localVariable.getName().equals(localVarName)).findFirst().get().getType(); } @Override - public Type typeCheck(Map localVar, TypedClass clas) { - if (localVar.containsKey(name)) { + public Type typeCheck(TypedClass clas) { + /* if (localVar.containsKey(name)) { throw new RuntimeException("Method " + name + " already exists"); } localVar.put(name, returnType); for (TypedParameter typedParameter : typedParameters) { - typedParameter.typeCheck(localVar, clas); - } + typedParameter.typeCheck( clas); + }*/ return returnType; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java b/src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java index 3969a2f..f7e5a87 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java @@ -19,19 +19,19 @@ public class TypedMethodCall implements TypedExpression, TypedStatement { private List args; private Type type; - public TypedMethodCall(Map localVar, TypedClass clas, MethodCall unTypedMethodCall) { - convertToTypedMethodCall(localVar, clas, unTypedMethodCall); + public TypedMethodCall(TypedClass clas, MethodCall unTypedMethodCall) { + convertToTypedMethodCall(clas, unTypedMethodCall); } - public void convertToTypedMethodCall(Map localVar, TypedClass clas, MethodCall unTypedMethodCall) { - recipient = new TypedFieldVarAccess(localVar, clas, unTypedMethodCall.recipient()); + public void convertToTypedMethodCall(TypedClass clas, MethodCall unTypedMethodCall) { + recipient = new TypedFieldVarAccess(clas, unTypedMethodCall.recipient()); for (Expression arg : unTypedMethodCall.args()) { - args.add(convertExpression(localVar, clas, arg)); + args.add(convertExpression(clas, arg)); } } @Override - public Type typeCheck(Map localVar, TypedClass clas) { + public Type typeCheck(TypedClass clas) { //TODO: implement this 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 d60509e..b5b885b 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedNew.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedNew.java @@ -17,28 +17,29 @@ public class TypedNew implements TypedExpression, TypedStatement { private Type type; private List args = new ArrayList<>(); - public TypedNew(Map localVar, TypedClass clas, New unTypedNew) { - convertToTypedNew(localVar, clas, unTypedNew); + public TypedNew(TypedClass clas, New unTypedNew) { + convertToTypedNew(clas, unTypedNew); } - public void convertToTypedNew(Map localVar, TypedClass clas, New unTypedNew) { + + public void convertToTypedNew(TypedClass clas, New unTypedNew) { type = unTypedNew.type(); for (Expression arg : unTypedNew.args()) { - args.add(convertExpression(localVar, clas, arg)); + args.add(convertExpression(clas, arg)); } } @Override - public Type typeCheck(Map localVar, TypedClass clas) { - for(var constructor : clas.getTypedConstructors()){ - if(constructor.getTypedParameters().size() == args.size()){ + public Type typeCheck(TypedClass clas) { + for (var constructor : clas.getTypedConstructors()) { + if (constructor.getTypedParameters().size() == args.size()) { boolean valid = true; - for(int i = 0; i < args.size(); i++){ - if(!constructor.getTypedParameters().get(i).getType().equals(args.get(i).typeCheck(localVar, clas))){ + for (int i = 0; i < args.size(); i++) { + if (!constructor.getTypedParameters().get(i).getType().equals(args.get(i).typeCheck(clas))) { valid = false; break; } } - if(valid){ + if (valid) { return Type.REFERENCE(clas.getClassName()); } } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedParameter.java b/src/main/java/de/maishai/typedast/typedclass/TypedParameter.java index d5fbbfb..e58662e 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedParameter.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedParameter.java @@ -14,11 +14,11 @@ public class TypedParameter implements TypedNode { private String paraName; private Type type; - public TypedParameter(Map localVar, TypedClass clas, Parameter unTypedParameter) { - convertToTypedParameter(localVar, clas, unTypedParameter); + public TypedParameter(TypedClass clas, Parameter unTypedParameter) { + convertToTypedParameter(clas, unTypedParameter); } - public void convertToTypedParameter(Map localVar, TypedClass clas, Parameter unTypedParameter) { + public void convertToTypedParameter(TypedClass clas, Parameter unTypedParameter) { paraName = unTypedParameter.name(); type = unTypedParameter.type(); } @@ -34,7 +34,7 @@ public class TypedParameter implements TypedNode { @Override - public Type typeCheck(Map localVar, TypedClass clas) { + public Type typeCheck(TypedClass clas) { if (clas.isCurrentMethodPresent()) { if (clas.isParameterWitNameInMethod(paraName)) { diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java b/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java index 053c3e5..afcac8f 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java @@ -18,17 +18,17 @@ public class TypedReturn implements TypedStatement { private TypedExpression ret; private Type type; - public TypedReturn(Map localVar, TypedClass clas, Return unTypedReturn) { - convertToTypedReturn(localVar, clas, unTypedReturn); + public TypedReturn(TypedClass clas, Return unTypedReturn) { + convertToTypedReturn(clas, unTypedReturn); } - public void convertToTypedReturn(Map localVar, TypedClass clas, Return unTypedReturn) { - ret = convertExpression(localVar, clas, unTypedReturn.ret()); + public void convertToTypedReturn(TypedClass clas, Return unTypedReturn) { + ret = convertExpression(clas, unTypedReturn.ret()); type = ret.getType(); } @Override - public Type typeCheck(Map localVar, TypedClass clas) { + public Type typeCheck(TypedClass clas) { if (clas.isCurrentMethodPresent()) { if (clas.getCurrentMethod().getReturnType().getKind() != this.type.getKind()) { StringBuilder exp = new StringBuilder(); diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedUnary.java b/src/main/java/de/maishai/typedast/typedclass/TypedUnary.java index f69d043..5c06c00 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedUnary.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedUnary.java @@ -19,27 +19,27 @@ public class TypedUnary implements TypedExpression { private TypedExpression right; private Type type; - public TypedUnary(Map localVar, TypedClass clas, Unary unTypedUnary) { - convertToTypedUnary(localVar, clas, unTypedUnary); + public TypedUnary(TypedClass clas, Unary unTypedUnary) { + convertToTypedUnary(clas, unTypedUnary); } - public void convertToTypedUnary(Map localVar, TypedClass clas, Unary unTypedUnary) { + public void convertToTypedUnary(TypedClass clas, Unary unTypedUnary) { op = unTypedUnary.op(); - right = convertExpression(localVar, clas, unTypedUnary.right()); + right = convertExpression(clas, unTypedUnary.right()); } @Override - public Type typeCheck(Map localVar, TypedClass clas) { + public Type typeCheck(TypedClass clas) { if (op == UnaryOperator.NOT) { - if (right.typeCheck(localVar, clas) != Type.BOOL) { + if (right.typeCheck(clas) != Type.BOOL) { throw new RuntimeException("Not operator must be applied to boolean"); } return Type.BOOL; } if (op == UnaryOperator.SUB) { - if (right.typeCheck(localVar, clas) != Type.INT) { + if (right.typeCheck(clas) != Type.INT) { throw new RuntimeException("Minus operator must be applied to int"); } return Type.INT; diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedWhile.java b/src/main/java/de/maishai/typedast/typedclass/TypedWhile.java index 4fcd915..7bf1f10 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedWhile.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedWhile.java @@ -15,21 +15,21 @@ public class TypedWhile implements TypedStatement { private TypedBlock typedBlock; private Type type; - public TypedWhile(Map localVar, TypedClass clas, While unTypedWhile) { - convertToTypedWhile(localVar, clas, unTypedWhile); + public TypedWhile(TypedClass clas, While unTypedWhile) { + convertToTypedWhile(clas, unTypedWhile); } - public void convertToTypedWhile(Map localVar, TypedClass clas, While unTypedWhile) { - cond = convertExpression(localVar, clas, unTypedWhile.cond()); - typedBlock = new TypedBlock(localVar, clas, unTypedWhile.block()); + public void convertToTypedWhile(TypedClass clas, While unTypedWhile) { + cond = convertExpression(clas, unTypedWhile.cond()); + typedBlock = new TypedBlock(clas, unTypedWhile.block()); } @Override - public Type typeCheck(Map localVar, TypedClass clas) { - if (cond.typeCheck(localVar, clas) != Type.BOOL) { + public Type typeCheck(TypedClass clas) { + if (cond.typeCheck(clas) != Type.BOOL) { throw new RuntimeException("While condition must be a boolean"); } - type = typedBlock.typeCheck(localVar, clas); + type = typedBlock.typeCheck(clas); return type; }