From cb4c637ce481d6953fdff38dc507369b4c4371ec Mon Sep 17 00:00:00 2001 From: ahmad Date: Thu, 9 May 2024 19:59:40 +0200 Subject: [PATCH] Change the parameter classes to one class in TypedNode --- .../java/de/maishai/typedast/TypedNode.java | 5 +-- .../typedast/Util/TypedExpressionUtil.java | 18 +++++----- .../typedast/typedclass/TypedAssignment.java | 12 +++---- .../typedast/typedclass/TypedBinary.java | 27 ++++++++------- .../typedast/typedclass/TypedBlock.java | 32 ++++++++--------- .../typedast/typedclass/TypedBoolLiteral.java | 7 ++-- .../typedast/typedclass/TypedBreak.java | 5 +-- .../typedast/typedclass/TypedCharLiteral.java | 9 ++--- .../typedast/typedclass/TypedClass.java | 34 +++++++++---------- .../typedast/typedclass/TypedConstructor.java | 15 ++++---- .../typedast/typedclass/TypedDoWhile.java | 18 +++++----- .../typedast/typedclass/TypedField.java | 7 ++-- .../typedclass/TypedFieldVarAccess.java | 10 +++--- .../maishai/typedast/typedclass/TypedFor.java | 28 +++++++-------- .../typedast/typedclass/TypedIfElse.java | 22 ++++++------ .../typedast/typedclass/TypedIntLiteral.java | 6 ++-- .../typedclass/TypedLocalVariable.java | 7 ++-- .../typedast/typedclass/TypedMethod.java | 18 +++++----- .../typedast/typedclass/TypedMethodCall.java | 5 +-- .../maishai/typedast/typedclass/TypedNew.java | 18 +++++----- .../typedast/typedclass/TypedParameter.java | 5 +-- .../typedast/typedclass/TypedReturn.java | 10 +++--- .../typedast/typedclass/TypedUnary.java | 15 ++++---- .../typedast/typedclass/TypedWhile.java | 18 +++++----- 24 files changed, 180 insertions(+), 171 deletions(-) diff --git a/src/main/java/de/maishai/typedast/TypedNode.java b/src/main/java/de/maishai/typedast/TypedNode.java index b3b1caa..c7912e9 100644 --- a/src/main/java/de/maishai/typedast/TypedNode.java +++ b/src/main/java/de/maishai/typedast/TypedNode.java @@ -1,10 +1,11 @@ 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, Map classes); - TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST); + Type typeCheck(Map localVar, TypedClass clas); + TypedNode convertToTypedAST(Map localVar, TypedClass clas, Node unTypedAST); } diff --git a/src/main/java/de/maishai/typedast/Util/TypedExpressionUtil.java b/src/main/java/de/maishai/typedast/Util/TypedExpressionUtil.java index 58872ee..11622f9 100644 --- a/src/main/java/de/maishai/typedast/Util/TypedExpressionUtil.java +++ b/src/main/java/de/maishai/typedast/Util/TypedExpressionUtil.java @@ -9,7 +9,7 @@ import java.util.Map; public class TypedExpressionUtil { - public static TypedExpression getKindOfExpression(Map localVar, Map classes, Expression expression) { + public static TypedExpression getKindOfExpression(Map localVar, TypedClass clas, Expression expression) { if (expression instanceof BoolLiteral boolLiteral) { TypedBoolLiteral typedBoolLiteral = new TypedBoolLiteral(); typedBoolLiteral.setValue(boolLiteral.value()); @@ -17,23 +17,23 @@ public class TypedExpressionUtil { } else if (expression instanceof CharLiteral charLiteral) { TypedCharLiteral typedCharLiteral = new TypedCharLiteral() - .convertToCharLiteral(localVar, classes, charLiteral); + .convertToCharLiteral(localVar, clas, charLiteral); return typedCharLiteral; } else if (expression instanceof IntLiteral intLiteral) { TypedIntLiteral typedIntLiteral = new TypedIntLiteral() - .convertToTypedIntLiteral(localVar, classes, intLiteral); + .convertToTypedIntLiteral(localVar, clas, intLiteral); return typedIntLiteral; } else if (expression instanceof Binary binary) { TypedBinary typedBinary = new TypedBinary() - .convertToTypedBinary(localVar, classes, binary); - typedBinary.typeCheck(localVar, classes); + .convertToTypedBinary(localVar, clas, binary); + typedBinary.typeCheck(localVar, clas); return typedBinary; } else if (expression instanceof FieldVarAccess fieldVarAccess) { TypedFieldVarAccess typedFieldVarAccess = new TypedFieldVarAccess() - .convertToTypedFieldVarAccess(localVar, classes, fieldVarAccess); + .convertToTypedFieldVarAccess(localVar, clas, fieldVarAccess); return typedFieldVarAccess; } else if (expression instanceof MethodCall methodCall) { @@ -44,7 +44,7 @@ public class TypedExpressionUtil { typedFieldVarAccess.setField(methodCall.recipient().field()); typedMethodCall.setRecipient(typedFieldVarAccess); for (var arg : methodCall.args()) { - typedMethodCall.getArgs().add(getKindOfExpression(localVar, classes, arg)); + typedMethodCall.getArgs().add(getKindOfExpression(localVar, clas, arg)); } return typedMethodCall; } @@ -52,14 +52,14 @@ public class TypedExpressionUtil { TypedNew typedNew = new TypedNew(); typedNew.setType(newStmt.type()); for (var arg : newStmt.args()) { - typedNew.getArgs().add(getKindOfExpression(localVar, classes, arg)); + typedNew.getArgs().add(getKindOfExpression(localVar, clas, arg)); } return typedNew; } else if (expression instanceof Unary unary) { TypedUnary typedUnary = new TypedUnary(); typedUnary.setOp(unary.op()); - typedUnary.setRight(getKindOfExpression(localVar, classes, unary.right())); + typedUnary.setRight(getKindOfExpression(localVar, clas, unary.right())); 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 a62b1c8..efb8900 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java @@ -20,19 +20,19 @@ public class TypedAssignment implements TypedStatement { private TypedExpression value; private Type type; - public TypedAssignment convertToTypedAssignment(Map localVar, Map classes, Assignment untyped) { + public TypedAssignment convertToTypedAssignment(Map localVar, TypedClass clas, Assignment untyped) { varName = untyped.location().id(); - value = getKindOfExpression(localVar, classes, untyped.value()); + value = getKindOfExpression(localVar, clas, untyped.value()); return this; } @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, TypedClass clas) { if (!localVar.containsKey(varName)) { throw new RuntimeException("Variable not declared"); } Type typeLeft = localVar.get(varName); - Type typeRight = value.typeCheck(localVar, classes); + Type typeRight = value.typeCheck(localVar, clas); if (typeLeft.equals(typeRight) ) { type = typeLeft; @@ -42,11 +42,11 @@ public class TypedAssignment implements TypedStatement { } @Override - public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public TypedNode convertToTypedAST(Map localVar, TypedClass clas, de.maishai.ast.records.Node unTypedAST) { Assignment untyped = (Assignment) unTypedAST; TypedAssignment typedAssignment = new TypedAssignment(); typedAssignment.setVarName(untyped.location().id()); - typedAssignment.setValue((TypedExpression) value.convertToTypedAST(localVar, classes, untyped.value())); + typedAssignment.setValue((TypedExpression) value.convertToTypedAST(localVar, clas, untyped.value())); return typedAssignment; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java b/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java index f502f4e..e2b6b0a 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java @@ -2,6 +2,7 @@ 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; @@ -20,34 +21,34 @@ public class TypedBinary implements TypedExpression { private TypedExpression right; private Type type; - public TypedBinary convertToTypedBinary(Map localVar, Map classes, Binary unTypedBinary) { - left = getKindOfExpression(localVar, classes, unTypedBinary.left()); - right = getKindOfExpression(localVar, classes, unTypedBinary.right()); + public TypedBinary convertToTypedBinary(Map localVar, TypedClass clas, Binary unTypedBinary) { + left = getKindOfExpression(localVar, clas, unTypedBinary.left()); + right = getKindOfExpression(localVar, clas, unTypedBinary.right()); op = unTypedBinary.op(); return this; } @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, TypedClass clas) { if (op == Operator.ADD || op == Operator.SUB || op == Operator.MUL) { - if (left.typeCheck(localVar, classes) == Type.INT && - right.typeCheck(localVar, classes) == Type.INT) { + if (left.typeCheck(localVar, clas) == Type.INT && + right.typeCheck(localVar, clas) == Type.INT) { type = Type.INT; return Type.INT; } else { throw new RuntimeException("Type mismatch in " + op); } } else if (op == Operator.GT || op == Operator.LT || op == Operator.GE || op == Operator.LE || op == Operator.EQ || op == Operator.NE || op == Operator.AND || op == Operator.OR) { - if (left.typeCheck(localVar, classes) == Type.INT && - right.typeCheck(localVar, classes) == Type.INT) { + if (left.typeCheck(localVar, clas) == Type.INT && + right.typeCheck(localVar, clas) == Type.INT) { type = Type.BOOL; return Type.BOOL; } else { throw new RuntimeException("Type mismatch in " + op); } } else if (op == Operator.AND || op == Operator.OR) { - if (left.typeCheck(localVar, classes) == Type.BOOL && - right.typeCheck(localVar, classes) == Type.BOOL) { + if (left.typeCheck(localVar, clas) == Type.BOOL && + right.typeCheck(localVar, clas) == Type.BOOL) { type = Type.BOOL; return Type.BOOL; } else { @@ -60,13 +61,13 @@ public class TypedBinary implements TypedExpression { } @Override - public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public TypedNode convertToTypedAST(Map localVar, TypedClass clas, Node unTypedAST) { Binary binary = (Binary) unTypedAST; TypedBinary typedBinary = new TypedBinary(); - typedBinary.setLeft((TypedExpression) this.left.convertToTypedAST(localVar, classes, binary.left())); + typedBinary.setLeft((TypedExpression) this.left.convertToTypedAST(localVar, clas, binary.left())); typedBinary.setOp(binary.op()); - typedBinary.setRight((TypedExpression) this.right.convertToTypedAST(localVar, classes, binary.right())); + typedBinary.setRight((TypedExpression) this.right.convertToTypedAST(localVar, clas, binary.right())); return typedBinary; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java b/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java index c4c0461..38f3241 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java @@ -19,7 +19,7 @@ public class TypedBlock implements TypedNode { private List stmts; - public TypedBlock blockToTypedBlock(Map localVar, Map classes, Block unTypedBlock) { + public TypedBlock blockToTypedBlock(Map localVar, TypedClass clas, Block unTypedBlock) { if (unTypedBlock == null) { return null; @@ -34,7 +34,7 @@ public class TypedBlock implements TypedNode { } else { for (Declaration var : unTypedBlock.localVariables()) { TypedLocalVariable typedVar = new TypedLocalVariable() - .convertToTypedLocalVariable(localVar, classes, var); + .convertToTypedLocalVariable(localVar, clas, var); typedBlock.getVars().add(typedVar); } } @@ -46,41 +46,41 @@ public class TypedBlock implements TypedNode { for (var stmt : unTypedBlock.stmts()) { if (stmt instanceof Assignment assignment) { TypedAssignment typedAssignment = new TypedAssignment() - .convertToTypedAssignment(localVar, classes, assignment); - typedAssignment.typeCheck(localVar, classes); + .convertToTypedAssignment(localVar, clas, assignment); + typedAssignment.typeCheck(localVar, clas); typedBlock.getStmts().add(typedAssignment); continue; } if (stmt instanceof For forStmt) { - TypedFor typedFor = new TypedFor().convertToTypedFor(localVar, classes, forStmt); - typedFor.typeCheck(localVar, classes); + TypedFor typedFor = new TypedFor().convertToTypedFor(localVar, clas, forStmt); + typedFor.typeCheck(localVar, clas); typedBlock.getStmts().add(typedFor); continue; } if (stmt instanceof IfElse ifElse) { TypedIfElse typedIfElse = new TypedIfElse() - .convertToTypedIfElse(localVar, classes, ifElse); - typedIfElse.typeCheck(localVar, classes); + .convertToTypedIfElse(localVar, clas, ifElse); + typedIfElse.typeCheck(localVar, clas); typedBlock.getStmts().add(typedIfElse); continue; } if (stmt instanceof While whileStmt) { TypedWhile typedWhile = new TypedWhile() - .convertToTypedWhile(localVar, classes, whileStmt); + .convertToTypedWhile(localVar, clas, whileStmt); //TODO: check the type typedBlock.getStmts().add(typedWhile); continue; } if (stmt instanceof DoWhile doWhile) { TypedDoWhile typedDoWhile = new TypedDoWhile() - .convertToTypedDoWhile(localVar, classes, doWhile); + .convertToTypedDoWhile(localVar, clas, doWhile); //TODO: check the type typedBlock.getStmts().add(typedDoWhile); continue; } if (stmt instanceof Return returnStmt) { TypedReturn typedReturn = new TypedReturn() - .convertToTypedReturn(localVar, classes, returnStmt); + .convertToTypedReturn(localVar, clas, returnStmt); //TODO: check the type typedBlock.getStmts().add(typedReturn); continue; @@ -93,7 +93,7 @@ public class TypedBlock implements TypedNode { if (stmt instanceof Break) { TypedBreak typedBreak = new TypedBreak(); - typedBreak.typeCheck(localVar, classes); + typedBreak.typeCheck(localVar, clas); typedBlock.getStmts().add(typedBreak); continue; } @@ -109,19 +109,19 @@ public class TypedBlock implements TypedNode { } @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, TypedClass clas) { if (vars == null) { vars = List.of(); } else { for (TypedLocalVariable var : vars) { - var.typeCheck(localVar, classes); + var.typeCheck(localVar, clas); } } if (stmts == null) { stmts = List.of(); } else { for (TypedStatement stmt : stmts) { - stmt.typeCheck(localVar, classes); + stmt.typeCheck(localVar, clas); } } @@ -129,7 +129,7 @@ public class TypedBlock implements TypedNode { } @Override - public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public TypedNode convertToTypedAST(Map localVar, TypedClass clas, Node unTypedAST) { Block untyped = (Block) unTypedAST; TypedBlock typedBlock = new TypedBlock(); typedBlock.setStmts(new ArrayList<>()); diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java b/src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java index da15678..1899c6a 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java @@ -1,6 +1,7 @@ package de.maishai.typedast.typedclass; import de.maishai.ast.records.BoolLiteral; +import de.maishai.ast.records.Node; import de.maishai.typedast.MethodContext; import de.maishai.typedast.TypedExpression; import de.maishai.typedast.TypedNode; @@ -16,19 +17,19 @@ public class TypedBoolLiteral implements TypedExpression { private Boolean value; private Type type; - public TypedBoolLiteral convertToTypedBoolLiteral(Map localVar, Map classes,BoolLiteral unTypedBoolLiteral) { + public TypedBoolLiteral convertToTypedBoolLiteral(Map localVar, TypedClass clas, BoolLiteral unTypedBoolLiteral) { value = unTypedBoolLiteral.value(); return this; } @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, TypedClass clas) { type = Type.BOOL; return Type.BOOL; } @Override - public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public TypedNode convertToTypedAST(Map localVar, TypedClass clas, Node unTypedAST) { BoolLiteral untyped = (BoolLiteral) unTypedAST; TypedBoolLiteral typedBoolLiteral = new TypedBoolLiteral(); typedBoolLiteral.setValue(untyped.value()); diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedBreak.java b/src/main/java/de/maishai/typedast/typedclass/TypedBreak.java index 218a08b..ba2fcc0 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedBreak.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBreak.java @@ -1,6 +1,7 @@ package de.maishai.typedast.typedclass; import de.maishai.ast.records.Break; +import de.maishai.ast.records.Node; import de.maishai.typedast.MethodContext; import de.maishai.typedast.TypedNode; import de.maishai.typedast.TypedStatement; @@ -17,13 +18,13 @@ public class TypedBreak implements TypedStatement { return this; } @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, TypedClass clas) { type = Type.VOID; return type; } @Override - public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public TypedNode convertToTypedAST(Map localVar, TypedClass clas, Node unTypedAST) { Break untyped = (Break) unTypedAST; if(untyped == null) { throw new RuntimeException("Break not found"); diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedCharLiteral.java b/src/main/java/de/maishai/typedast/typedclass/TypedCharLiteral.java index ffad42f..1af20aa 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedCharLiteral.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedCharLiteral.java @@ -1,6 +1,7 @@ package de.maishai.typedast.typedclass; import de.maishai.ast.records.CharLiteral; +import de.maishai.ast.records.Node; import de.maishai.typedast.MethodContext; import de.maishai.typedast.TypedExpression; import de.maishai.typedast.TypedNode; @@ -12,19 +13,19 @@ import java.util.Map; @Data public class TypedCharLiteral implements TypedExpression { private char value; + private Type type = Type.CHAR; - public TypedCharLiteral convertToCharLiteral(Map localVar, Map classes, CharLiteral unTypedCharLiteral) { + public TypedCharLiteral convertToCharLiteral(Map localVar, TypedClass clas, CharLiteral unTypedCharLiteral) { value = unTypedCharLiteral.value(); return this; } @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, TypedClass clas) { return Type.CHAR; } - private Type type; @Override - public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public TypedNode convertToTypedAST(Map localVar, TypedClass clas, Node unTypedAST) { CharLiteral untyped = (CharLiteral) unTypedAST; TypedCharLiteral typedCharLiteral = new TypedCharLiteral(); typedCharLiteral.setValue(untyped.value()); diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedClass.java b/src/main/java/de/maishai/typedast/typedclass/TypedClass.java index 31d3b28..885fdda 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedClass.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedClass.java @@ -1,10 +1,8 @@ package de.maishai.typedast.typedclass; import de.maishai.ast.Operator; +import de.maishai.ast.records.*; import de.maishai.ast.records.Class; -import de.maishai.ast.records.Constructor; -import de.maishai.ast.records.Declaration; -import de.maishai.ast.records.Method; import de.maishai.typedast.CodeGenUtils; import de.maishai.typedast.Type; import lombok.AllArgsConstructor; @@ -29,33 +27,33 @@ public class TypedClass implements TypedNode { private List typedConstructors; @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, TypedClass clas) { - if (classes.containsKey(className)) { - throw new RuntimeException("Class " + className + " already exists"); + if (clas == null) { + throw new RuntimeException("Class not found"); } - classes.put(className, this); + for (TypedField field : typedFields) { - field.typeCheck(localVar, classes); + field.typeCheck(localVar, clas); } - for(TypedConstructor constructor : typedConstructors) { - constructor.typeCheck(localVar, classes); + for (TypedConstructor constructor : typedConstructors) { + constructor.typeCheck(localVar, clas); } for (TypedMethod typedMethod : typedMethods) { - typedMethod.typeCheck(localVar, classes); + typedMethod.typeCheck(localVar, clas); } return Type.REFERENCE(className); } public TypedNode startConversion(de.maishai.ast.records.Class c) { Map local = new HashMap<>(); - Map classMap = new HashMap<>(); + TypedClass classMap = new TypedClass(); return convertToTypedAST(local, classMap, c); } @Override - public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public TypedNode convertToTypedAST(Map localVar, TypedClass clas, Node unTypedAST) { TypedClass typedClass = new TypedClass(); Class c = (Class) unTypedAST; typedClass.setClassName(c.classname()); @@ -64,19 +62,19 @@ public class TypedClass implements TypedNode { typedClass.setTypedConstructors(new ArrayList<>()); for (Declaration field : c.fieldDeclarations()) { - TypedField typedField = new TypedField(); - typedClass.getTypedFields().add(typedField.convertToTypedField(localVar, classes, field)); + TypedField typedField = new TypedField(); + typedClass.getTypedFields().add(typedField.convertToTypedField(localVar, clas, field)); } for (Method method : c.methods()) { TypedMethod typedMethod = new TypedMethod(); - typedClass.getTypedMethods().add(typedMethod.convertToTypedMethod(localVar, classes,method)); + typedClass.getTypedMethods().add(typedMethod.convertToTypedMethod(localVar, clas, method)); } for (Constructor constructor : c.constructors()) { TypedConstructor typedConstructor = new TypedConstructor(); - typedClass.getTypedConstructors().add(typedConstructor.constructorToTypedConstructor(localVar, classes, constructor)); + typedClass.getTypedConstructors().add(typedConstructor.constructorToTypedConstructor(localVar, clas, constructor)); } return typedClass; @@ -111,7 +109,7 @@ public class TypedClass implements TypedNode { c.typedFields = List.of(f1, f2); //Constructors - TypedConstructor constructor = new TypedConstructor( "SomeClass", List.of(new TypedParameter("test", Type.INT)), new TypedBlock(new ArrayList<>(), new ArrayList<>())); + TypedConstructor constructor = new TypedConstructor("SomeClass", List.of(new TypedParameter("test", Type.INT)), new TypedBlock(new ArrayList<>(), new ArrayList<>())); c.typedConstructors = List.of(constructor); //Methods diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java b/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java index 1906763..7d71a9f 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java @@ -1,6 +1,7 @@ package de.maishai.typedast.typedclass; import de.maishai.ast.records.Constructor; +import de.maishai.ast.records.Node; import de.maishai.ast.records.Parameter; import de.maishai.typedast.CodeGenUtils; import de.maishai.typedast.MethodContext; @@ -26,7 +27,7 @@ public class TypedConstructor implements TypedNode { private List typedParameters; private TypedBlock typedBlock; - public TypedConstructor constructorToTypedConstructor(Map localVar, Map classes, Constructor unTypedConstructor) { + public TypedConstructor constructorToTypedConstructor(Map localVar, TypedClass clas, Constructor unTypedConstructor) { TypedConstructor typedConstructor = new TypedConstructor(); typedConstructor.setName(unTypedConstructor.className()); typedConstructor.setTypedParameters(new ArrayList<>()); @@ -44,27 +45,27 @@ public class TypedConstructor implements TypedNode { } TypedBlock typedBlock = new TypedBlock(); - typedConstructor.setTypedBlock(typedBlock.blockToTypedBlock(localVar, classes ,unTypedConstructor.block())); + typedConstructor.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas ,unTypedConstructor.block())); return typedConstructor; } @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, TypedClass clas) { if (localVar.containsKey(name)) { throw new RuntimeException("constructor already declared"); } localVar.put(name, Type.VOID); if (typedParameters != null) { for (TypedParameter param : typedParameters) { - param.typeCheck(localVar, classes); + param.typeCheck(localVar, clas); } } - typedBlock.typeCheck(localVar, classes); + typedBlock.typeCheck(localVar, clas); return Type.VOID; } @Override - public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public TypedNode convertToTypedAST(Map localVar, TypedClass clas, Node unTypedAST) { Constructor untyped = (Constructor) unTypedAST; TypedConstructor typedConstructor = new TypedConstructor(); typedConstructor.setName(untyped.className()); @@ -83,7 +84,7 @@ public class TypedConstructor implements TypedNode { } TypedBlock typedBlock = new TypedBlock(); - typedConstructor.setTypedBlock(typedBlock.blockToTypedBlock(localVar, classes, untyped.block())); + typedConstructor.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas, untyped.block())); return typedConstructor; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedDoWhile.java b/src/main/java/de/maishai/typedast/typedclass/TypedDoWhile.java index 08de32e..4b69d04 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedDoWhile.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedDoWhile.java @@ -16,29 +16,29 @@ public class TypedDoWhile implements TypedStatement { public TypedDoWhile() { } - public TypedDoWhile convertToTypedDoWhile(Map localVar, Map classes, DoWhile unTypedDoWhile) { - typedBlock = new TypedBlock().blockToTypedBlock(localVar, classes, unTypedDoWhile.block()); - cond = getKindOfExpression(localVar, classes, unTypedDoWhile.cond()); + public TypedDoWhile convertToTypedDoWhile(Map localVar, TypedClass clas, DoWhile unTypedDoWhile) { + typedBlock = new TypedBlock().blockToTypedBlock(localVar, clas, unTypedDoWhile.block()); + cond = getKindOfExpression(localVar, clas, unTypedDoWhile.cond()); return this; } @Override - public Type typeCheck(Map localVar, Map classes) { - if (cond.typeCheck(localVar, classes) != Type.BOOL) { + public Type typeCheck(Map localVar, TypedClass clas) { + if (cond.typeCheck(localVar, clas) != Type.BOOL) { throw new RuntimeException("Condition must be boolean"); } - typedBlock.typeCheck(localVar, classes); + typedBlock.typeCheck(localVar, clas); return Type.VOID; } @Override - public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public TypedNode convertToTypedAST(Map localVar, TypedClass clas, Node unTypedAST) { DoWhile untyped = (DoWhile) unTypedAST; TypedDoWhile typedDoWhile = new TypedDoWhile(); TypedBlock typedBlock = new TypedBlock(); - typedDoWhile.setTypedBlock(typedBlock.blockToTypedBlock(localVar, classes, untyped.block())); - typedDoWhile.setCond((TypedExpression) cond.convertToTypedAST(localVar, classes, untyped.cond())); + typedDoWhile.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas, untyped.block())); + typedDoWhile.setCond((TypedExpression) cond.convertToTypedAST(localVar, clas, untyped.cond())); return typedDoWhile; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedField.java b/src/main/java/de/maishai/typedast/typedclass/TypedField.java index 1217d7e..4732fd5 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedField.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedField.java @@ -1,6 +1,7 @@ package de.maishai.typedast.typedclass; import de.maishai.ast.records.Declaration; +import de.maishai.ast.records.Node; import de.maishai.typedast.TypedNode; import de.maishai.typedast.Type; import lombok.AllArgsConstructor; @@ -18,7 +19,7 @@ public class TypedField implements TypedNode { private String varName; private Type type; - public TypedField convertToTypedField(Map localVar, Map classes, Declaration declaration){ + public TypedField convertToTypedField(Map localVar, TypedClass clas, Declaration declaration){ if(localVar.containsKey(declaration.name())){ throw new RuntimeException("Variable " + declaration.name() + " already declared"); } @@ -29,7 +30,7 @@ public class TypedField implements TypedNode { } @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, TypedClass clas) { if (localVar.containsKey(varName)) { throw new RuntimeException("Variable " + varName + " already declared"); } @@ -38,7 +39,7 @@ public class TypedField implements TypedNode { } @Override - public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public TypedNode convertToTypedAST(Map localVar, TypedClass clas, Node unTypedAST) { Declaration untypedDeclaration = (Declaration) unTypedAST; TypedField typedField = new TypedField(); diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java b/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java index 5352360..07d0cf5 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java @@ -19,16 +19,16 @@ public class TypedFieldVarAccess implements TypedExpression { private String name; private Type type; - public TypedFieldVarAccess convertToTypedFieldVarAccess(Map localVar, Map classes, FieldVarAccess unTypedFieldVarAccess) { + public TypedFieldVarAccess convertToTypedFieldVarAccess(Map localVar, TypedClass clas, FieldVarAccess unTypedFieldVarAccess) { field = unTypedFieldVarAccess.field(); - recursiveOwnerChain = getKindOfExpression(localVar, classes, unTypedFieldVarAccess.recursiveOwnerChain()); + recursiveOwnerChain = getKindOfExpression(localVar, clas, unTypedFieldVarAccess.recursiveOwnerChain()); name = unTypedFieldVarAccess.id(); type = localVar.get(unTypedFieldVarAccess.id()); return this; } @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, TypedClass clas) { Type type = localVar.get(name); if (type == null) { @@ -42,11 +42,11 @@ public class TypedFieldVarAccess implements TypedExpression { return type; } @Override - public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public TypedNode convertToTypedAST(Map localVar, TypedClass clas, Node unTypedAST) { FieldVarAccess untypedFieldVarAccess = (FieldVarAccess) unTypedAST; TypedFieldVarAccess typedFieldVarAccess = new TypedFieldVarAccess(); typedFieldVarAccess.setField(untypedFieldVarAccess.field()); - typedFieldVarAccess.setRecursiveOwnerChain((TypedExpression) recursiveOwnerChain.convertToTypedAST(localVar, classes, untypedFieldVarAccess.recursiveOwnerChain())); + typedFieldVarAccess.setRecursiveOwnerChain((TypedExpression) recursiveOwnerChain.convertToTypedAST(localVar, clas, untypedFieldVarAccess.recursiveOwnerChain())); typedFieldVarAccess.setName(untypedFieldVarAccess.id()); return typedFieldVarAccess; diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedFor.java b/src/main/java/de/maishai/typedast/typedclass/TypedFor.java index 93ef00a..e92a19c 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedFor.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedFor.java @@ -20,36 +20,36 @@ public class TypedFor implements TypedStatement { private TypedAssignment inc; private TypedBlock typedBlock; - public TypedFor convertToTypedFor(Map localVar, Map classes, For unTypedFor) { - assign = new TypedAssignment().convertToTypedAssignment(localVar, classes, unTypedFor.assign()); - cond = getKindOfExpression(localVar, classes, unTypedFor.cond()); - inc = new TypedAssignment().convertToTypedAssignment(localVar, classes, unTypedFor.inc()); - typedBlock = new TypedBlock().blockToTypedBlock(localVar, classes, unTypedFor.block()); + public TypedFor convertToTypedFor(Map localVar,TypedClass clas, For unTypedFor) { + assign = new TypedAssignment().convertToTypedAssignment(localVar, clas, unTypedFor.assign()); + cond = getKindOfExpression(localVar, clas, unTypedFor.cond()); + inc = new TypedAssignment().convertToTypedAssignment(localVar, clas, unTypedFor.inc()); + typedBlock = new TypedBlock().blockToTypedBlock(localVar, clas, unTypedFor.block()); return this; } @Override - public Type typeCheck(Map localVar, Map classes) { - assign.typeCheck(localVar, classes); - if (!cond.typeCheck(localVar, classes).equals(Type.BOOL)) { + public Type typeCheck(Map localVar, TypedClass clas) { + assign.typeCheck(localVar, clas); + if (!cond.typeCheck(localVar, clas).equals(Type.BOOL)) { throw new RuntimeException("Condition must be a boolean"); } - inc.typeCheck(localVar, classes); - return typedBlock.typeCheck(localVar, classes); + inc.typeCheck(localVar, clas); + return typedBlock.typeCheck(localVar, clas); } @Override - public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public TypedNode convertToTypedAST(Map localVar, TypedClass clas, Node unTypedAST) { For untyped = (For) unTypedAST; TypedFor typedFor = new TypedFor(); TypedAssignment assign = new TypedAssignment(); - typedFor.setAssign(assign.convertToTypedAssignment(localVar,classes,untyped.assign())); + typedFor.setAssign(assign.convertToTypedAssignment(localVar,clas,untyped.assign())); TypedAssignment lnc = new TypedAssignment(); - typedFor.setInc(lnc.convertToTypedAssignment(localVar,classes,untyped.inc())); + typedFor.setInc(lnc.convertToTypedAssignment(localVar,clas,untyped.inc())); typedFor.setCond(null); TypedBlock typedBlock = new TypedBlock(); - typedFor.setTypedBlock(typedBlock.blockToTypedBlock(localVar, classes, untyped.block())); + typedFor.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas, untyped.block())); return typedFor; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java b/src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java index 97d3c6a..94a638e 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java @@ -17,36 +17,36 @@ public class TypedIfElse implements TypedStatement { private TypedBlock ifTypedBlock; private TypedBlock elseTypedBlock; - public TypedIfElse convertToTypedIfElse(Map localVar, Map classes, IfElse unTypedIfElse) { - ifTypedBlock = new TypedBlock().blockToTypedBlock(localVar, classes, unTypedIfElse.ifBlock()); - elseTypedBlock = new TypedBlock().blockToTypedBlock(localVar, classes, unTypedIfElse.elseBlock()); - typedCon = getKindOfExpression(localVar, classes, unTypedIfElse.cond()); + public TypedIfElse convertToTypedIfElse(Map localVar, TypedClass clas, IfElse unTypedIfElse) { + ifTypedBlock = new TypedBlock().blockToTypedBlock(localVar, clas, unTypedIfElse.ifBlock()); + elseTypedBlock = new TypedBlock().blockToTypedBlock(localVar, clas, unTypedIfElse.elseBlock()); + typedCon = getKindOfExpression(localVar, clas, unTypedIfElse.cond()); return this; } @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, TypedClass clas) { - if (typedCon.typeCheck(localVar, classes) != Type.BOOL) { + if (typedCon.typeCheck(localVar, clas) != Type.BOOL) { throw new RuntimeException("If condition must be a boolean"); } - if (ifTypedBlock.typeCheck(localVar, classes) != Type.VOID) { + if (ifTypedBlock.typeCheck(localVar, clas) != Type.VOID) { throw new RuntimeException("If block must be of type void"); } return Type.VOID; } @Override - public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public TypedNode convertToTypedAST(Map localVar, TypedClass clas, Node unTypedAST) { IfElse ifElse = (IfElse) unTypedAST; TypedIfElse typedIfElse = new TypedIfElse(); - typedIfElse.setTypedCon((TypedExpression) typedCon.convertToTypedAST(localVar, classes, ifElse.cond())); + typedIfElse.setTypedCon((TypedExpression) typedCon.convertToTypedAST(localVar, clas, ifElse.cond())); TypedBlock ifTypedBlock = new TypedBlock(); TypedBlock elseTypedBlock = new TypedBlock(); - typedIfElse.setIfTypedBlock(ifTypedBlock.blockToTypedBlock(localVar, classes, ifElse.ifBlock())); - typedIfElse.setElseTypedBlock(elseTypedBlock.blockToTypedBlock(localVar, classes, ifElse.elseBlock())); + typedIfElse.setIfTypedBlock(ifTypedBlock.blockToTypedBlock(localVar, clas, ifElse.ifBlock())); + typedIfElse.setElseTypedBlock(elseTypedBlock.blockToTypedBlock(localVar, clas, ifElse.elseBlock())); return typedIfElse; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedIntLiteral.java b/src/main/java/de/maishai/typedast/typedclass/TypedIntLiteral.java index bfb2301..2427c1a 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedIntLiteral.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedIntLiteral.java @@ -18,7 +18,7 @@ import java.util.Map; public class TypedIntLiteral implements TypedExpression { private Integer value; - public TypedIntLiteral convertToTypedIntLiteral(Map localVar, Map classes, IntLiteral unTypedIntLiteral) { + public TypedIntLiteral convertToTypedIntLiteral(Map localVar, TypedClass clas, IntLiteral unTypedIntLiteral) { value = unTypedIntLiteral.value(); return this; } @@ -28,12 +28,12 @@ public class TypedIntLiteral implements TypedExpression { this.value = value; } @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, TypedClass clas) { return Type.INT; } @Override - public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public TypedNode convertToTypedAST(Map localVar, TypedClass clas, de.maishai.ast.records.Node unTypedAST) { IntLiteral untyped = (IntLiteral) unTypedAST; return new TypedIntLiteral(untyped.value()); } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java b/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java index 58304c8..f731aa1 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java @@ -1,6 +1,7 @@ package de.maishai.typedast.typedclass; import de.maishai.ast.records.Declaration; +import de.maishai.ast.records.Node; import de.maishai.typedast.MethodContext; import de.maishai.typedast.TypedNode; import de.maishai.typedast.Type; @@ -16,7 +17,7 @@ import java.util.Map; public final class TypedLocalVariable implements TypedNode { private String name; private Type type; - public TypedLocalVariable convertToTypedLocalVariable(Map localVar, Map classes, Declaration declaration){ + public TypedLocalVariable convertToTypedLocalVariable(Map localVar, TypedClass clas, Declaration declaration){ if(localVar.containsKey(declaration.name())){ throw new RuntimeException("Variable " + declaration.name() + " already declared"); } @@ -27,7 +28,7 @@ public final class TypedLocalVariable implements TypedNode { } @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, TypedClass clas) { Type type = localVar.get(name); if(type == this.type) { return type; @@ -36,7 +37,7 @@ public final class TypedLocalVariable implements TypedNode { } @Override - public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public TypedNode convertToTypedAST(Map localVar, TypedClass clas, Node unTypedAST) { Declaration untypedLocalVariable = (Declaration) unTypedAST; TypedLocalVariable typedLocalVariable = new TypedLocalVariable(); diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java b/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java index bc6d915..fb9ab95 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java @@ -1,6 +1,7 @@ package de.maishai.typedast.typedclass; import de.maishai.ast.records.Method; +import de.maishai.ast.records.Node; import de.maishai.ast.records.Parameter; import de.maishai.typedast.*; import lombok.Data; @@ -19,10 +20,11 @@ public class TypedMethod implements TypedNode { private List typedParameters; private TypedBlock typedBlock; - public TypedMethod convertToTypedMethod(Map localVar, Map classes, Method unTypedMethod) { + public TypedMethod convertToTypedMethod(Map localVar, TypedClass clas, Method unTypedMethod) { TypedMethod typedMethod = new TypedMethod(); typedMethod.setName(unTypedMethod.methodName()); typedMethod.setTypedParameters(new ArrayList<>()); + typedMethod.setReturnType(unTypedMethod.type()); if(unTypedMethod.params().isEmpty()){ typedMethod.setTypedParameters(List.of()); @@ -31,27 +33,27 @@ public class TypedMethod implements TypedNode { TypedParameter typedParameter = new TypedParameter(); typedParameter.setParaName(parameter.name()); typedParameter.setType(parameter.type()); - typedParameters.add(typedParameter); + typedMethod.getTypedParameters().add(typedParameter); } } TypedBlock block = new TypedBlock(); - typedMethod.setTypedBlock(block.blockToTypedBlock(localVar, classes, unTypedMethod.block())); + typedMethod.setTypedBlock(block.blockToTypedBlock(localVar, clas, unTypedMethod.block())); return typedMethod; } @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, TypedClass clas) { if(localVar.containsKey(name)) { throw new RuntimeException("Method " + name + " already exists"); } localVar.put(name, returnType); for(TypedParameter typedParameter : typedParameters) { - typedParameter.typeCheck(localVar, classes); + typedParameter.typeCheck(localVar, clas); } - return typedBlock.typeCheck(localVar, classes); + return typedBlock.typeCheck(localVar, clas); } @Override - public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public TypedNode convertToTypedAST(Map localVar, TypedClass clas, Node unTypedAST) { Method untypedMethod = (Method) unTypedAST; TypedMethod typedMethod = new TypedMethod(); @@ -67,7 +69,7 @@ public class TypedMethod implements TypedNode { } TypedBlock typedBlock = new TypedBlock(); - typedMethod.setTypedBlock(typedBlock.blockToTypedBlock(localVar, classes,untypedMethod.block())); + typedMethod.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas,untypedMethod.block())); return typedMethod; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java b/src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java index 08b76e5..6a71fa7 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java @@ -1,6 +1,7 @@ package de.maishai.typedast.typedclass; import de.maishai.ast.records.MethodCall; +import de.maishai.ast.records.Node; import de.maishai.typedast.*; import lombok.Data; import org.objectweb.asm.MethodVisitor; @@ -20,13 +21,13 @@ public class TypedMethodCall implements TypedExpression, TypedStatement { } @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, TypedClass clas) { //TODO: Implement typeCheck for MethodCall return null; } @Override - public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public TypedNode convertToTypedAST(Map localVar, TypedClass clas, Node unTypedAST) { //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 fd4d006..679f4dc 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedNew.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedNew.java @@ -1,6 +1,7 @@ package de.maishai.typedast.typedclass; import de.maishai.ast.records.New; +import de.maishai.ast.records.Node; import de.maishai.typedast.*; import lombok.Data; import org.objectweb.asm.MethodVisitor; @@ -18,20 +19,17 @@ public class TypedNew implements TypedExpression, TypedStatement { return null; } @Override - public Type typeCheck(Map localVar, Map classes) { - // new A(1, 2, 3) - if (!classes.containsKey(this.type.getReference())) { - throw new RuntimeException("Class " + this.type.getReference() + " not found"); + public Type typeCheck(Map localVar, TypedClass clas) { + if(clas == null){ + throw new RuntimeException("Class not found"); } - TypedClass klasse = classes.get(this.type.getReference()); - if (klasse.getTypedFields().size() != args.size()) { + if (clas.getTypedFields().size() != args.size()) { throw new RuntimeException("number of arguments is invalid for class" + this.type.getReference()); } - // compare the types of the arguments for (int i = 0; i < args.size(); i++) { - Type type = args.get(i).typeCheck(localVar, classes); - if (!type.equals(klasse.getTypedFields().get(i).getType())) { + Type type = args.get(i).typeCheck(localVar, clas); + if (!type.equals(clas.getTypedFields().get(i).getType())) { throw new RuntimeException("False type for argument " + i + " in class " + type.getKind()); } } @@ -39,7 +37,7 @@ public class TypedNew implements TypedExpression, TypedStatement { } @Override - public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public TypedNode convertToTypedAST(Map localVar, TypedClass clas, de.maishai.ast.records.Node unTypedAST) { New untyped = (New) unTypedAST; TypedNew typedNew = new TypedNew(); diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedParameter.java b/src/main/java/de/maishai/typedast/typedclass/TypedParameter.java index fa0afb4..8fe0844 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedParameter.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedParameter.java @@ -1,5 +1,6 @@ package de.maishai.typedast.typedclass; +import de.maishai.ast.records.Node; import de.maishai.ast.records.Parameter; import de.maishai.typedast.TypedNode; import de.maishai.typedast.Type; @@ -23,7 +24,7 @@ public class TypedParameter implements TypedNode { } @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, TypedClass clas) { if (localVar.containsKey(paraName)) { throw new RuntimeException("Parameter " + paraName + " already exists"); } @@ -32,7 +33,7 @@ public class TypedParameter implements TypedNode { } @Override - public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public TypedNode convertToTypedAST(Map localVar, TypedClass clas, de.maishai.ast.records.Node unTypedAST) { Parameter untyped = (Parameter) unTypedAST; TypedParameter typedParameter = new TypedParameter(); typedParameter.setParaName(untyped.name()); diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java b/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java index 66ca53e..1a9453f 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java @@ -15,22 +15,22 @@ import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression; public class TypedReturn implements TypedStatement { private TypedExpression ret; - public TypedReturn convertToTypedReturn(Map localVar, Map classes, Return unTypedReturn) { - ret = getKindOfExpression(localVar, classes, unTypedReturn.ret()); + public TypedReturn convertToTypedReturn(Map localVar, TypedClass clas, Return unTypedReturn) { + ret = getKindOfExpression(localVar, clas, unTypedReturn.ret()); return this; } @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, TypedClass clas) { //TODO: Implement typeCheck for Return return null; } @Override - public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public TypedNode convertToTypedAST(Map localVar, TypedClass clas, Node unTypedAST) { Return untyped = (Return) unTypedAST; TypedReturn typedReturn = new TypedReturn(); - typedReturn.setRet((TypedExpression) ret.convertToTypedAST(localVar, classes, untyped.ret())); + typedReturn.setRet((TypedExpression) ret.convertToTypedAST(localVar, clas, untyped.ret())); return typedReturn; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedUnary.java b/src/main/java/de/maishai/typedast/typedclass/TypedUnary.java index 48df0a7..5bf753c 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedUnary.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedUnary.java @@ -1,6 +1,7 @@ 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; @@ -20,23 +21,23 @@ public class TypedUnary implements TypedExpression { private TypedExpression right; private Type type; - public TypedUnary convertToTypedUnary(Map localVar, Map classes, Unary unTypedUnary) { + public TypedUnary convertToTypedUnary(Map localVar, TypedClass clas, Unary unTypedUnary) { op = unTypedUnary.op(); - right = getKindOfExpression(localVar, classes, unTypedUnary.right()); + right = getKindOfExpression(localVar, clas, unTypedUnary.right()); return this; } @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, TypedClass clas) { if(op == UnaryOperator.NOT){ - if(right.typeCheck(localVar, classes) != Type.BOOL){ + if(right.typeCheck(localVar, clas) != Type.BOOL){ throw new RuntimeException("Not operator must be applied to boolean"); } return Type.BOOL; } if(op == UnaryOperator.SUB){ - if(right.typeCheck(localVar, classes) != Type.INT){ + if(right.typeCheck(localVar, clas) != Type.INT){ throw new RuntimeException("Minus operator must be applied to int"); } return Type.INT; @@ -45,11 +46,11 @@ public class TypedUnary implements TypedExpression { } @Override - public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public TypedNode convertToTypedAST(Map localVar, TypedClass clas, de.maishai.ast.records.Node unTypedAST) { Unary untyped = (Unary) unTypedAST; TypedUnary typedUnary = new TypedUnary(); typedUnary.setOp(untyped.op()); - typedUnary.setRight((TypedExpression) right.convertToTypedAST(localVar, classes, untyped.right())); + typedUnary.setRight((TypedExpression) right.convertToTypedAST(localVar, clas, untyped.right())); return typedUnary; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedWhile.java b/src/main/java/de/maishai/typedast/typedclass/TypedWhile.java index 244499c..d18dfea 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedWhile.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedWhile.java @@ -14,27 +14,27 @@ public class TypedWhile implements TypedStatement { private TypedExpression cond; private TypedBlock typedBlock; - public TypedWhile convertToTypedWhile(Map localVar, Map classes, While unTypedWhile) { - cond = getKindOfExpression(localVar, classes, unTypedWhile.cond()); - typedBlock = new TypedBlock().blockToTypedBlock(localVar, classes, unTypedWhile.block()); + public TypedWhile convertToTypedWhile(Map localVar, TypedClass clas, While unTypedWhile) { + cond = getKindOfExpression(localVar, clas, unTypedWhile.cond()); + typedBlock = new TypedBlock().blockToTypedBlock(localVar, clas, unTypedWhile.block()); return this; } @Override - public Type typeCheck(Map localVar, Map classes) { - if(cond.typeCheck(localVar, classes) != Type.BOOL){ + public Type typeCheck(Map localVar, TypedClass clas) { + if(cond.typeCheck(localVar, clas) != Type.BOOL){ throw new RuntimeException("While condition must be a boolean"); } - typedBlock.typeCheck(localVar, classes); + typedBlock.typeCheck(localVar, clas); return Type.BOOL; } @Override - public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public TypedNode convertToTypedAST(Map localVar, TypedClass clas, Node unTypedAST) { While untyped = (While) unTypedAST; TypedWhile typedWhile = new TypedWhile(); - typedWhile.setCond((TypedExpression) cond.convertToTypedAST(localVar, classes, untyped.cond())); + typedWhile.setCond((TypedExpression) cond.convertToTypedAST(localVar, clas, untyped.cond())); TypedBlock typedBlock = new TypedBlock(); - typedWhile.setTypedBlock(typedBlock.blockToTypedBlock(localVar, classes, untyped.block())); + typedWhile.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas, untyped.block())); return typedWhile; }