From bdabe5696c7f3317f89c4faa70a8fc55128b7d51 Mon Sep 17 00:00:00 2001 From: Mattias Ulbrich Date: Tue, 6 Dec 2022 15:47:29 +0100 Subject: [PATCH] Support for "label=number" --- .../edu/kit/kastel/formal/mimaflux/MimaAsm.g4 | 5 +++++ .../kit/kastel/formal/mimaflux/LabelResolver.java | 15 ++++++++++++--- .../kastel/formal/mimaflux/ProgramVisitor.java | 9 +++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/main/antlr/edu/kit/kastel/formal/mimaflux/MimaAsm.g4 b/src/main/antlr/edu/kit/kastel/formal/mimaflux/MimaAsm.g4 index 149688b..2ce5adf 100644 --- a/src/main/antlr/edu/kit/kastel/formal/mimaflux/MimaAsm.g4 +++ b/src/main/antlr/edu/kit/kastel/formal/mimaflux/MimaAsm.g4 @@ -24,6 +24,7 @@ file : line : adr_spec + | label_spec | command ; @@ -31,6 +32,10 @@ adr_spec : '*' '=' NUMBER ; +label_spec : + ID '=' NUMBER + ; + command : (label=ID ':')? ( mnemomicWith (idArg=ID | numberArg=NUMBER) | mnemomicWithout) diff --git a/src/main/java/edu/kit/kastel/formal/mimaflux/LabelResolver.java b/src/main/java/edu/kit/kastel/formal/mimaflux/LabelResolver.java index f88fe05..48cbbdd 100644 --- a/src/main/java/edu/kit/kastel/formal/mimaflux/LabelResolver.java +++ b/src/main/java/edu/kit/kastel/formal/mimaflux/LabelResolver.java @@ -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; @@ -24,16 +26,23 @@ public class LabelResolver { public void resolve(List commands) { labelMap = new HashMap(); - for (Command command : commands) { + ListIterator 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 it = commands.listIterator(); + it = commands.listIterator(); while (it.hasNext()) { Command command = it.next(); if(command.labelArg() != null) { diff --git a/src/main/java/edu/kit/kastel/formal/mimaflux/ProgramVisitor.java b/src/main/java/edu/kit/kastel/formal/mimaflux/ProgramVisitor.java index a3dc7e9..0efcb46 100644 --- a/src/main/java/edu/kit/kastel/formal/mimaflux/ProgramVisitor.java +++ b/src/main/java/edu/kit/kastel/formal/mimaflux/ProgramVisitor.java @@ -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; @@ -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;