Skip to content

Commit

Permalink
Update TypeChecker.java
Browse files Browse the repository at this point in the history
  • Loading branch information
MShadiF committed May 6, 2024
1 parent 108cbc1 commit 32301c8
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions src/main/java/dk/aau/cs_24_sw_4_16/carl/Semantic_A/TypeChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

import java.util.*;

import static java.lang.Integer.parseInt;

public class TypeChecker {

HashMap<String, Type> typeOfReturnFunction;
HashMap<String, List<Type>> functionParameters;
List<String> listOfInbuiltFunctions = new ArrayList<>(Arrays.asList("print", "generateGrid", "generateRooms",
"generateCorridors", "generateSpawns", "printMap", "generatePrint", "writeToFile", "overlap",
"tileInformationStringBuilder", "setSeed"));
HashMap<String, HashMap<String, Type>> strucvariablesTable;
HashMap<String, HashMap<String, Type>> structVariablesTable;
HashMap<String, Type> structTypes;
HashMap<String, Type> eTable;// variable table, identifier(x) og node(int)
Stack<HashMap<String, Type>> scopes; // scope table, variable identifier(x) og node
Expand All @@ -33,7 +35,7 @@ public TypeChecker() {
scopes.add(eTable);
typeOfReturnFunction = new HashMap<>();
functionParameters = new HashMap<>();
strucvariablesTable = new HashMap<>();
structVariablesTable = new HashMap<>();
structTypes = new HashMap<>();
}

Expand Down Expand Up @@ -76,7 +78,6 @@ public void visitStatements(StatementNode node) {
}
if (node.getNode() instanceof ReturnStatementNode) {
hasReturnStatement = true;
System.out.println("we get in return statement" + hasReturnStatement);
visitReturnNode((ReturnStatementNode) node.getNode());
}
if (node.getNode() instanceof StructureDefinitionNode) {
Expand All @@ -92,6 +93,7 @@ public void visitStatements(StatementNode node) {
visitPropertyAssignment((PropertyAssignmentNode) node.getNode());
}
if (node.getNode() instanceof MethodCallNode) {
errorHandler("ewjrngkewgjewkejnweklrglewrkngewkrgkwen");
visitMethodCall((MethodCallNode) node.getNode());
}
}
Expand Down Expand Up @@ -257,29 +259,33 @@ public Type visitPropertyAccessNode(PropertyAccessNode node) {
// }

String firstIdentifier = node.getIdentifiers().get(0).toString();
if (!strucvariablesTable.containsKey(firstIdentifier)) {
if (!structVariablesTable.containsKey(firstIdentifier) && !validPropertyAccess.contains(firstIdentifier)) {
errorHandler("could not find the struct variable: " + firstIdentifier);
}

HashMap<String, Type> listOfIdentifiers = strucvariablesTable.get(firstIdentifier);
HashMap<String, Type> listOfIdentifiers = structVariablesTable.get(firstIdentifier);



if (!validPropertyAccess.contains(firstIdentifier) && node.getIdentifiers().size() <= 1 || !listOfIdentifiers.containsKey(node.getIdentifiers().get(1).toString())) {
errorHandler("you need 3 arguments");
}

if (strucvariablesTable.containsKey(firstIdentifier) && node.getIdentifiers().size() >= 2 && listOfIdentifiers.containsKey(node.getIdentifiers().get(1).toString())) {
if (structVariablesTable.containsKey(firstIdentifier) && node.getIdentifiers().size() >= 2 && listOfIdentifiers.containsKey(node.getIdentifiers().get(1).toString())) {
Type identifierType = listOfIdentifiers.get(node.getIdentifiers().get(1).toString());
return getType(identifierType.getTypeName());
}
return Type.UNKNOWN;
}

public void visitMethodCall(MethodCallNode node) {
System.out.println("ekjrgnekrjgkewrjg");
Type type = visitPropertyAccessNode(node.getPropertyAccessContext());
System.out.println(type);
List<String> names = new ArrayList<>(structTypes.keySet());
try {
int i = Integer.parseInt(node.getValue().toString());
System.out.println("Parsed integer value: " + i);
} catch (NumberFormatException e) {
System.err.println("Error parsing integer: " + e.getMessage());
}
}

public Type relationOperatorTypeCheck(RelationsAndLogicalOperatorNode node) {
Expand Down Expand Up @@ -566,8 +572,7 @@ public void visitFunctionCall(FunctionCallNode node) {
*/
public void visitStruct(StructureDefinitionNode node) {
String identifier = node.getIdentifier().toString();
System.out.println("structtypes" + structTypes);
if (!strucvariablesTable.containsKey(identifier)) {
if (!structVariablesTable.containsKey(identifier)) {

Type structType = getType(node.getType());

Expand All @@ -580,13 +585,12 @@ public void visitStruct(StructureDefinitionNode node) {
visitVariableDeclarationforStructs(declaration);
}
structTypes.put(identifier, structType);
strucvariablesTable.put(identifier, localETable);
structVariablesTable.put(identifier, localETable);
scopes.remove(localETable);

} else {
errorHandler("Struct " + node.getIdentifier().toString() + " already exists");
}
System.out.println(strucvariablesTable);
}

private void visitVariableDeclarationforStructs(VariableDeclarationNode node) {
Expand Down Expand Up @@ -643,6 +647,8 @@ public Type getType(Object node) {
type = getType(((ArrayAccessNode) node).getIdentifier());
} else if (node instanceof PropertyAccessNode) {
type = visitPropertyAccessNode((PropertyAccessNode) node);
} else if (node instanceof MethodCallNode) {
visitMethodCall((MethodCallNode) node);
} else if (node instanceof BinaryOperatorNode) {
type = binaryOperatorTypeCheck((BinaryOperatorNode) node);
} else if (node instanceof RelationsAndLogicalOperatorNode) {
Expand Down

0 comments on commit 32301c8

Please sign in to comment.