Browse Source

refactoring the SPSAssembler class to fit into the web emulator

Klaas, Wilfried 6 years ago
parent
commit
2e94d758a7

+ 41 - 11
src/main/java/de/mcs/tools/sps/SPSAssembler.java

@@ -76,13 +76,13 @@ public class SPSAssembler {
   private static final String START_MACRO_DEFINITION = ".macro";
   private static final String DEFAULT_PACKAGE_FILTER = "de.mcs.tools.sps"; //$NON-NLS-1$
   private static File source;
-  private static HARDWARE destination;
   private static File destinationFile;
   private static String outputFormat;
   private static File includes;
   private static Outputter outputter;
   private static String destinationStr;
 
+  private HARDWARE destination;
   private int lineNumber;
   // private int srcLineNumber;
   private Map<String, Integer> labels;
@@ -91,6 +91,7 @@ public class SPSAssembler {
   private boolean inMacro;
   private Macro actualMacro;
   private Map<String, Macro> macros;
+  private List<Integer> lineNumbers;
 
   @SwitchOption(shortKey = 'h', longKey = "help", name = "help", help = "show this help page", required = false, defaultValue = false)
   public static void doHelp(boolean value) {
@@ -137,7 +138,7 @@ public class SPSAssembler {
    */
   public static void main(String[] args) {
     try {
-      destination = HARDWARE.HOLTEK;
+      HARDWARE destination = HARDWARE.HOLTEK;
 
       CommandlineProcessor.processCommandline(SPSAssembler.class, args);
 
@@ -173,6 +174,7 @@ public class SPSAssembler {
 
       try {
         SPSAssembler spsAssembler = new SPSAssembler();
+        spsAssembler.setDestination(destination);
         spsAssembler.doWork(source);
 
         List<Mnemonic> mnemonics = spsAssembler.getMnemonics();
@@ -293,15 +295,21 @@ public class SPSAssembler {
     labels = new HashMap<>();
     inBlockComment = false;
     mnemonics = new ArrayList<>();
+    lineNumbers = new ArrayList<>();
+
     inMacro = false;
     actualMacro = null;
     macros = new HashMap<>();
   }
 
-  private void doWork(File source) throws IOException, SyntaxError {
+  public void doWork(File source) throws IOException, SyntaxError {
     System.out.printf("start parsing file: %s\r\n", source.getName());
 
     List<String> sourceFile = Files.readAllLines(source.toPath(), Charset.forName("UTF-8"));
+    doCompile(sourceFile);
+  }
+
+  public void doCompile(List<String> sourceFile) throws SyntaxError, IOException {
     for (int i = 0; i < sourceFile.size(); i++) {
       String line = sourceFile.get(i);
 
@@ -330,7 +338,7 @@ public class SPSAssembler {
 
   private void paddingSubroutines() throws SyntaxError {
     // checking location of Subroutines
-    if (destination.equals(HARDWARE.ARDUINOSPS) || destination.equals(HARDWARE.TINYSPS)) {
+    if (getDestination().equals(HARDWARE.ARDUINOSPS) || getDestination().equals(HARDWARE.TINYSPS)) {
       int position = 0;
       boolean foundSub = false;
       for (int i = 0; i < mnemonics.size(); i++) {
@@ -345,6 +353,7 @@ public class SPSAssembler {
         int count = 256 - position;
         for (int i = 0; i < count; i++) {
           mnemonics.add(position, new NOP(""));
+          lineNumbers.add(position, -1);
         }
       }
     }
@@ -361,7 +370,7 @@ public class SPSAssembler {
       prgSize++;
     }
     int maxSize = 128;
-    switch (destination) {
+    switch (getDestination()) {
     case ARDUINOSPS:
     case TINYSPS:
     case ATMEGA8:
@@ -372,10 +381,10 @@ public class SPSAssembler {
     }
     if (prgSize > maxSize) {
       throw new HardwareException(
-          String.format("Program exceeding size (%d) for hardeware %s (%d).", prgSize, destination, maxSize));
+          String.format("Program exceeding size (%d) for hardeware %s (%d).", prgSize, getDestination(), maxSize));
     }
 
-    switch (destination) {
+    switch (getDestination()) {
     case ARDUINOSPS:
       maxSize = 1024;
     case TINYSPS:
@@ -386,7 +395,7 @@ public class SPSAssembler {
     }
     if (mnemonics.size() > maxSize) {
       throw new HardwareException(
-          String.format("Program exceeding size (%d) for hardeware %s (%d).", prgSize, destination, maxSize));
+          String.format("Program exceeding size (%d) for hardeware %s (%d).", prgSize, getDestination(), maxSize));
     }
   }
 
@@ -417,10 +426,11 @@ public class SPSAssembler {
   private void checkingHardware() throws SyntaxError {
     for (Iterator<Mnemonic> iterator = mnemonics.iterator(); iterator.hasNext();) {
       Mnemonic mnemonic = iterator.next();
-      if (!mnemonic.allowedHardware().contains(destination)) {
+      if (!mnemonic.allowedHardware().contains(getDestination())) {
         throw new SyntaxError(mnemonic.getLineNumber(),
             String.format("the mnemonic \"%s\" with the argument \"%s\" is not availble on the choosen hardware \"%s\"",
-                mnemonic.getName(), mnemonic.getArgument() == null ? "" : mnemonic.getArgument(), destination.name()));
+                mnemonic.getName(), mnemonic.getArgument() == null ? "" : mnemonic.getArgument(),
+                getDestination().name()));
       }
     }
   }
@@ -483,6 +493,7 @@ public class SPSAssembler {
           if (mnemonic != null) {
             lineNumber++;
             mnemonics.add(mnemonic);
+            lineNumbers.add(srcLineNumber);
           }
         }
       }
@@ -604,7 +615,7 @@ public class SPSAssembler {
     return line;
   }
 
-  private List<Mnemonic> getMnemonics() {
+  public List<Mnemonic> getMnemonics() {
     return mnemonics;
   }
 
@@ -615,4 +626,23 @@ public class SPSAssembler {
   private Map<String, Integer> getLabels() {
     return labels;
   }
+
+  /**
+   * @return the destination
+   */
+  public HARDWARE getDestination() {
+    return destination;
+  }
+
+  /**
+   * @param destination
+   *          the destination to set
+   */
+  public void setDestination(HARDWARE destination) {
+    this.destination = destination;
+  }
+
+  public List<Integer> getLines() {
+    return lineNumbers;
+  }
 }

+ 13 - 3
src/main/java/de/mcs/tools/sps/emulator/AbstractEmulator.java

@@ -21,13 +21,16 @@
  */
 package de.mcs.tools.sps.emulator;
 
+import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
 import de.mcs.tools.sps.SPSAssembler;
 import de.mcs.tools.sps.emulator.model.WebSessionModel;
 import de.mcs.tools.sps.emulator.model.WorkingModel;
 import de.mcs.tools.sps.emulator.model.WorkingModel.WORKINGSTATE;
+import de.mcs.tools.sps.exceptions.SyntaxError;
 import de.mcs.tools.sps.mnemonic.Mnemonic;
 
 /**
@@ -50,8 +53,7 @@ public abstract class AbstractEmulator implements Emulator {
   }
 
   /**
-   * starting the emulatiing process. Resetting all registers, addresses,
-   * stacks, outputs...
+   * starting the emulatiing process. Resetting all registers, addresses, stacks, outputs...
    * 
    * @return return true, if everything is ok.
    */
@@ -105,7 +107,15 @@ public abstract class AbstractEmulator implements Emulator {
   @Override
   public boolean compile() {
     SPSAssembler spsAssembler = new SPSAssembler();
-    spsAssembler.doWork(source);
+    List<String> source = Arrays.asList(model.getProgram().getSource());
+    try {
+      spsAssembler.doCompile(source);
+      List<Mnemonic> mnemonics = spsAssembler.getMnemonics();
+      List<Integer> lineNumbers = spsAssembler.getLines();
+    } catch (SyntaxError | IOException e) {
+      // TODO Auto-generated catch block
+      e.printStackTrace();
+    }
 
     List<Mnemonic> mnemonics = spsAssembler.getMnemonics();
     return false;