Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly handle all unrecognized label name cases #9

Merged
merged 1 commit into from
Sep 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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