Skip to content

Commit

Permalink
fix: correctly handle all unrecognized label name cases (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerAberbach authored Sep 18, 2022
1 parent e77c0b4 commit e12b62c
Showing 1 changed file with 31 additions and 21 deletions.
52 changes: 31 additions & 21 deletions src/main/java/com/tomeraberbach/mano/assembly/Compiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -333,27 +333,37 @@ private static int get12BitNumber(String lexeme, int range) {
private void replaceLabels(ArrayList<String> labels) {
// Loops through the instructions to substitute labelMap for hex
for (int i = 0; i < instructions.size(); i++) {
// Checks if there was a label argument for the current instruction
if (labels.get(i) != null) {
// Checks if the label was ever defined
if (this.labelMap.containsKey(labels.get(i))) {
// Adds the address of the label to the instruction hex
instructions.set(i, instructions.get(i).argument(this.labelMap.get(labels.get(i))));
} else {
switch (instructions.get(i).tokens().length) {
case 1:
errors.add("Unrecognized label name, " + instructions.get(i).tokens()[0] + ".");
break;
case 2:
errors.add(
"Unrecognized label name, "
+ instructions.get(i).tokens()[1]
+ " or potentially missing argument after "
+ instructions.get(i).tokens()[0]
+ ".");
break;
}
}
String label = labels.get(i);

if (label == null) {
continue;
}

// Checks if the label was ever defined
if (this.labelMap.containsKey(label)) {
// Adds the address of the label to the instruction hex
instructions.set(i, instructions.get(i).argument(this.labelMap.get(label)));
continue;
}

Token[] tokens = instructions.get(i).tokens();

switch (tokens.length) {
case 1:
errors.add("Unrecognized label name, " + tokens[0] + ".");
break;
case 2:
case 3:
errors.add(
"Unrecognized label name, "
+ tokens[1]
+ " or potentially missing argument after "
+ tokens[0]
+ ".");
break;
default:
errors.add(
"An unexpected error occurred while processing the following: " + tokens + ".");
}
}
}
Expand Down

0 comments on commit e12b62c

Please sign in to comment.