Skip to content

Commit

Permalink
Array acces virker nu og typen bliver resovlet
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlHejlesen committed May 4, 2024
1 parent 1ce0fd5 commit 856557a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public void visitStatements(StatementNode node) {
if (node.getNode() instanceof ArrayAssignmentNode) {
visistArrayAssignment((ArrayAssignmentNode) node.getNode());
}

}

public void errorHandler(String errorMessage) {
Expand All @@ -100,6 +101,41 @@ public void errorHandler(String errorMessage) {
System.err.println(errorMessage);
}

/*
* Vi skal tjekke om den er ordenligt skrevet og retunere en type?
*/
public void visistArrayAccesNodeFn(ArrayAccessNode 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 allowedAccesTypesForArrays = Type.INT;
Boolean validTypesaccesTypes = true;
Type sizeType = Type.UNKNOWN;
int arguementNumber = 0;

List<AstNode> sizes = node.getIndices();
for (int i = 0; i < sizes.size(); i++) {
AstNode astNode = sizes.get(i);
sizeType = getType(astNode);
if (sizeType != allowedAccesTypesForArrays) {
arguementNumber = i;
errorHandler("Tried to assign the array:" + identifier + " but acces value: " + arguementNumber
+ " is of type:" + sizeType + " and should be INT");
validTypesaccesTypes = false;

break;
}
}

}
}

/*
* VI skal hente Type fra table
* Tjekke alle argumenter
Expand All @@ -109,6 +145,13 @@ public void errorHandler(String errorMessage) {
public void visistArrayAssignment(ArrayAssignmentNode node) {
String identifier = node.getIdentifier().toString();
Boolean found = false;
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 = scopes.getLast().get(identifier);

Expand Down Expand Up @@ -545,6 +588,11 @@ public Type getType(Object node) {
type = getVariable((IdentifierNode) node); // Vis x giv mig x value
}

} else if (node instanceof ArrayAccessNode) {

visistArrayAccesNodeFn(((ArrayAccessNode) node));

type = getType(((ArrayAccessNode) node).getIdentifier());
} else if (node instanceof BinaryOperatorNode) {
type = binaryOperatorTypeCheck((BinaryOperatorNode) node);
} else if (node instanceof RelationsAndLogicalOperatorNode) {
Expand Down
4 changes: 2 additions & 2 deletions test.carl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ var difficulty:int = 20
var array: int[3][3]


array[1]["hej"] = "hej"

//var x: int = array[1][2]

var x: int = array[2][2]

0 comments on commit 856557a

Please sign in to comment.