Skip to content

Commit

Permalink
Assigment af array bliver nu tjekket
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlHejlesen committed May 4, 2024
1 parent 266521c commit 1ce0fd5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
28 changes: 17 additions & 11 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 @@ -101,39 +101,45 @@ public void errorHandler(String errorMessage) {
}

/*
* VI skal hente værdi fra table
* VI skal hente Type 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;
Boolean found = false;
if (!found) {
Type arrayType = scopes.getLast().get(identifier);


Boolean validTypes = true;
Boolean validTypesaccesTypes = true;
Type sizeType = Type.UNKNOWN;
int arguementNumber = 0;
List<AstNode> sizes = node.getSizes();
Type allowedAccesTypesForArrays = Type.INT;

List<AstNode> sizes = node.getIndices();
for (int i = 0; i < sizes.size(); i++) {
AstNode astNode = sizes.get(i);
sizeType = getType(astNode);
if (sizeType != arrayType) {
if (sizeType != allowedAccesTypesForArrays) {
arguementNumber = i;
validTypes = false;
validTypesaccesTypes = false;

break;
}
}
if (validTypes) {
scopes.getLast().put(identifier, arrayType);
} else {
errorHandler("Tried to declare the array:" + identifier + " but argument: " + arguementNumber
if (!validTypesaccesTypes) {
errorHandler("Tried to assign the array:" + identifier + " but acces value: " + arguementNumber
+ " is of type:" + sizeType + " and should be:" + arrayType);
}

// Tjek venstre mod højre
Type assignType = getType(node.getValue());
if (arrayType != assignType) {
errorHandler("Tried to assign the type:" + assignType + " to the array:" + identifier
+ " that has the type:" + arrayType + ", and that is ilegal");
}

} else {
errorHandler("Identifier:" + identifier + " is alredy used, rename it");
}
Expand Down
2 changes: 1 addition & 1 deletion 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][2] = 71
array[1]["hej"] = "hej"

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

0 comments on commit 1ce0fd5

Please sign in to comment.