diff --git a/src/main/java/dk/aau/cs_24_sw_4_16/carl/Semantic_A/TypeChecker.java b/src/main/java/dk/aau/cs_24_sw_4_16/carl/Semantic_A/TypeChecker.java index 480b063..319cd2e 100644 --- a/src/main/java/dk/aau/cs_24_sw_4_16/carl/Semantic_A/TypeChecker.java +++ b/src/main/java/dk/aau/cs_24_sw_4_16/carl/Semantic_A/TypeChecker.java @@ -12,7 +12,7 @@ public class TypeChecker { "generateCorridors", "generateSpawns", "printMap", "generatePrint", "writeToFile", "overlap", "tileInformationStringBuilder", "setSeed")); HashMap> strucvariablesTable; - HashMap structTypes; + HashMap structTypes; // HashMap fTable; // function table, // identifier(x) og node @@ -24,6 +24,7 @@ public class TypeChecker { Boolean hasReturnStatement = false; String currentActiveFunction = ""; public Boolean thereWasAnError = false; + String currentIdentifierCheck = ""; public TypeChecker() { @@ -40,6 +41,7 @@ public TypeChecker() { } public void visitor(AstNode node) { + System.out.println(node); if (node instanceof ProgramNode) { visitProgramNode((ProgramNode) node); } @@ -83,6 +85,12 @@ public void visitStatements(StatementNode node) { if (node.getNode() instanceof StructureDefinitionNode) { visitStruct((StructureDefinitionNode) node.getNode()); } + if (node.getNode() instanceof ArrayDefinitionNode) { + visistarrayDekleration((ArrayDefinitionNode) node.getNode()); + } + if (node.getNode() instanceof ArrayAssignmentNode) { + visistArrayAssignment((ArrayAssignmentNode) node.getNode()); + } } public void errorHandler(String errorMessage) { @@ -92,6 +100,90 @@ public void errorHandler(String errorMessage) { System.err.println(errorMessage); } + /* + * VI skal hente værdi fra table + * Tjekke alle argumenter + * tjekke højre side + * tjekke for existens + */ + public void visistArrayAssignment(ArrayAssignmentNode node) { + String identifier = node.getIdentifier().toString(); + Boolean found =false; + if (!found) { + Type arrayType = scopes.getLast().get(identifier); + + + Boolean validTypes = true; + Type sizeType = Type.UNKNOWN; + int arguementNumber = 0; + List sizes = node.getSizes(); + for (int i = 0; i < sizes.size(); i++) { + AstNode astNode = sizes.get(i); + sizeType = getType(astNode); + if (sizeType != arrayType) { + arguementNumber = i; + validTypes = false; + + break; + } + } + if (validTypes) { + scopes.getLast().put(identifier, arrayType); + } else { + errorHandler("Tried to declare the array:" + identifier + " but argument: " + arguementNumber + + " is of type:" + sizeType + " and should be:" + arrayType); + } + + } else { + errorHandler("Identifier:" + identifier + " is alredy used, rename it"); + } + } + + /* + * Vi skal hente den dekleerede type + * VI skal tjekke om den eksistere + * vi skal tjekke om alle argumenterne er valide. + */ + public void visistarrayDekleration(ArrayDefinitionNode node) { + + String identifier = node.getIdentifier().toString(); + boolean found = scopes.getFirst().containsKey(identifier); + + for (int i = activeScope.getLast(); i < scopes.size(); i++) { + if (scopes.get(i).containsKey(identifier)) { + found = true; + } + } + if (!found) { + Type arrayType = getType(node.getType()); + + Boolean validTypes = true; + Type sizeType = Type.UNKNOWN; + int arguementNumber = 0; + List sizes = node.getSizes(); + for (int i = 0; i < sizes.size(); i++) { + AstNode astNode = sizes.get(i); + sizeType = getType(astNode); + if (sizeType != arrayType) { + arguementNumber = i; + validTypes = false; + + break; + } + } + if (validTypes) { + scopes.getLast().put(identifier, arrayType); + } else { + errorHandler("Tried to declare the array:" + identifier + " but argument: " + arguementNumber + + " is of type:" + sizeType + " and should be:" + arrayType); + } + + } else { + errorHandler("Identifier:" + identifier + " is alredy used, rename it"); + } + + } + public Type relationOperatorTypeCheck(RelationsAndLogicalOperatorNode node) { AstNode left = node.getLeft(); @@ -176,11 +268,14 @@ private void visitVariableDeclaration(VariableDeclarationNode node) { } public Type getVariable(IdentifierNode node) { + if (scopes.getFirst().containsKey(node.getIdentifier().toString())) { + currentIdentifierCheck = node.getIdentifier().toString(); return scopes.getFirst().get(node.getIdentifier().toString()); } for (int i = activeScope.getLast(); i < scopes.size(); i++) { if (scopes.get(i).containsKey(node.getIdentifier().toString())) { + currentIdentifierCheck = node.getIdentifier().toString(); return scopes.get(i).get(node.getIdentifier().toString()); } } @@ -373,11 +468,10 @@ public void visitFunctionCall(FunctionCallNode node) { */ public void visitStruct(StructureDefinitionNode node) { String identifier = node.getIdentifier().toString(); - System.out.println("structtypes"+structTypes); + System.out.println("structtypes" + structTypes); if (!strucvariablesTable.containsKey(identifier)) { - - - Type structType =getType(node.getType()); + + Type structType = getType(node.getType()); HashMap localETable = new HashMap<>(); @@ -390,7 +484,7 @@ public void visitStruct(StructureDefinitionNode node) { structTypes.put(identifier, structType); strucvariablesTable.put(identifier, localETable); scopes.remove(localETable); - + } else { errorHandler("Struct " + node.getIdentifier().toString() + " already exists"); } @@ -459,7 +553,7 @@ public Type getType(Object node) { } else if (node instanceof BoolNode) { type = Type.BOOLEAN; } else if (node instanceof ExpressionNode) { - + type = getType(((ExpressionNode) node).getNode()); } else if (node instanceof StringNode) { type = Type.STRING; } else if (node instanceof TypeNode) { diff --git a/test.carl b/test.carl index 9149b0d..7be9b22 100644 --- a/test.carl +++ b/test.carl @@ -1,15 +1,10 @@ var number:int = 20 - +var haha:string = "hej" var difficulty:int = 20 -var Goblin : enemy ={ - var difficulty : int = 1 - var health : int = 500 - var symbol : string= "O" - var difficulty:int = 20 -} -var Goblin : enemy ={ - var difficulty : int = 1 - var health : int = 500 - var symbol : string= "O" -} \ No newline at end of file +var array: int[3][3] + + +array[1][2] = 71 + +//var x: int = array[1][2] \ No newline at end of file