Skip to content

Commit

Permalink
Support for "label=number"
Browse files Browse the repository at this point in the history
  • Loading branch information
mattulbrich committed Dec 6, 2022
1 parent 5d639fa commit bdabe56
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/main/antlr/edu/kit/kastel/formal/mimaflux/MimaAsm.g4
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@ file :

line :
adr_spec
| label_spec
| command
;

adr_spec :
'*' '=' NUMBER
;

label_spec :
ID '=' NUMBER
;

command :
(label=ID ':')? ( mnemomicWith (idArg=ID | numberArg=NUMBER)
| mnemomicWithout)
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/edu/kit/kastel/formal/mimaflux/LabelResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package edu.kit.kastel.formal.mimaflux;

import edu.kit.kastel.formal.mimaflux.MimaAsmParser.CommandContext;

import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
Expand All @@ -24,16 +26,23 @@ public class LabelResolver {

public void resolve(List<Command> commands) {
labelMap = new HashMap<String, Integer>();
for (Command command : commands) {
ListIterator<Command> it = commands.listIterator();
while (it.hasNext()) {
Command command = it.next();
if (command.label() != null) {
if (labelMap.containsKey(command.label())) {
throw new TokenedException(command.ctx().getStart(), "Symbol '" + command.label() + "' already defined");
CommandContext ctx = command.ctx();
throw new TokenedException(ctx != null ? ctx.getStart() : null, "Symbol '" + command.label() + "' already defined");
}
labelMap.put(command.label(), command.address());
}
if (command.instruction() == null) {
// This was a label definition only.
it.remove();
}
}

ListIterator<Command> it = commands.listIterator();
it = commands.listIterator();
while (it.hasNext()) {
Command command = it.next();
if(command.labelArg() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import edu.kit.kastel.formal.mimaflux.MimaAsmParser.Adr_specContext;
import edu.kit.kastel.formal.mimaflux.MimaAsmParser.CommandContext;
import edu.kit.kastel.formal.mimaflux.MimaAsmParser.Label_specContext;
import org.antlr.v4.runtime.tree.TerminalNode;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -35,6 +37,13 @@ public Void visitAdr_spec(Adr_specContext ctx) {
return null;
}

@Override
public Void visitLabel_spec(Label_specContext ctx) {
Integer number = Integer.decode(ctx.NUMBER().getText());
commands.add(new Command(number, ctx.ID().getText(), null, null, 0, null));
return null;
}

@Override
public Void visitCommand(CommandContext ctx) {
String label = null;
Expand Down

0 comments on commit bdabe56

Please sign in to comment.