Browse Source

more error prone macro processing.

Klaas, Wilfried 6 years ago
parent
commit
34f856d02c

+ 12 - 2
examples/Blink.tps

@@ -1,3 +1,13 @@
+.macro blink
+PORT #0B0101
+WAIT 200ms
+PORT #0B1010
+WAIT 200ms
+.endmacro
+
+:loop
+.blink
+RJMP :loop
 /* 
 Kommentar über mehrere Zeilen
 */
@@ -10,14 +20,14 @@ WAIT time
 .endmacro
 
 .include macro_blink
-:loop
+:loop1
 .macro1 #0x0f 200ms
 
 PORT #0x0F ;Zeilenkommentar
 WAIT 200ms
 PORT #0x00
 WAIT 200ms
-RJMP :loop
+RJMP :loop1
 
 .macro empty
 .endmacro

+ 18 - 5
src/main/java/de/mcs/tools/sps/Macro.java

@@ -24,6 +24,7 @@ package de.mcs.tools.sps;
 import java.util.ArrayList;
 import java.util.List;
 
+import de.mcs.tools.sps.exceptions.IllegalArgument;
 import de.mcs.tools.sps.exceptions.SyntaxError;
 
 /**
@@ -72,7 +73,17 @@ public class Macro {
   }
 
   public List<String> processMacro(String[] values, int lineNumber) throws SyntaxError {
-    if (values.length != args.length) {
+    if ((values != null) && (args == null)) {
+      throw new IllegalArgument(lineNumber,
+          String.format("count of macro arguments (%d) are not the same as in macro definition (%d) of macro \"%s\".",
+              values.length, 0, name));
+    }
+    if ((values == null) && (args != null)) {
+      throw new IllegalArgument(lineNumber,
+          String.format("count of macro arguments (%d) are not the same as in macro definition (%d) of macro \"%s\".",
+              0, args.length, name));
+    }
+    if (values != null && values.length != args.length) {
       throw new SyntaxError(lineNumber,
           String.format("count of macro arguments (%d) are not the same as in macro definition (%d) of macro \"%s\".",
               values.length, args.length, name));
@@ -80,10 +91,12 @@ public class Macro {
     List<String> mnemonics = new ArrayList<>();
     lines.forEach(l -> {
       String line = l;
-      for (int i = 0; i < args.length; i++) {
-        String key = args[i];
-        String value = values[i];
-        line = line.replace(key, value);
+      if (args != null) {
+        for (int i = 0; i < args.length; i++) {
+          String key = args[i];
+          String value = values[i];
+          line = line.replace(key, value);
+        }
       }
       mnemonics.add(line);
     });

+ 7 - 8
src/main/java/de/mcs/tools/sps/SPSAssembler.java

@@ -137,7 +137,7 @@ public class SPSAssembler {
 
       CommandlineProcessor.processCommandline(SPSAssembler.class, args);
 
-      registerOutputter();
+      registerAllOutputter();
 
       if ((source == null) || (!source.exists())) {
         CommandlineProcessor.showHelp(
@@ -171,6 +171,7 @@ public class SPSAssembler {
 
         outputFile(mnemonics);
 
+        System.out.println("assembling succesfully");
       } catch (SyntaxError e) {
         e.printStackTrace();
         System.err.println(e.getMessage());
@@ -186,8 +187,8 @@ public class SPSAssembler {
    * @throws IllegalAccessException
    * @throws Exception
    */
-  private static void registerOutputter() throws InstantiationException, IllegalAccessException, Exception {
-    Set<Class<?>> outputClasses = registerOutputter(SPSOutputter.class);
+  private static void registerAllOutputter() throws InstantiationException, IllegalAccessException, Exception {
+    Set<Class<?>> outputClasses = searchOutputter(SPSOutputter.class);
     for (Class<?> outClass : outputClasses) {
       SPSOutputter annotation = outClass.getAnnotation(SPSOutputter.class);
       if (annotation.name().equalsIgnoreCase(outputFormat)) {
@@ -200,14 +201,12 @@ public class SPSAssembler {
     }
   }
 
-  private static Set<Class<?>> registerOutputter(Class<? extends Annotation>... annotationClasses) {
+  private static Set<Class<?>> searchOutputter(Class<? extends Annotation> annotationClass) {
     ConfigurationBuilder builder = new ConfigurationBuilder();
     builder.addUrls(ClasspathHelper.forPackage(DEFAULT_PACKAGE_FILTER));
     Reflections reflections = new Reflections(builder);
     Set<Class<?>> classes = new HashSet<Class<?>>();
-    for (Class<? extends Annotation> class1 : annotationClasses) {
-      classes.addAll(reflections.getTypesAnnotatedWith(class1));
-    }
+    classes.addAll(reflections.getTypesAnnotatedWith(annotationClass));
     return classes;
   }
 
@@ -417,7 +416,7 @@ public class SPSAssembler {
       // seems to be a macro usage
       name = name.substring(1);
       String[] arguments = label.split(" ");
-      if (arguments.length < 2) {
+      if (arguments.length < 1) {
         throw new IllegalArgument(srcLineNumber, "Need at least one argument for the macro name.");
       }
       String[] args = null;