Browse Source

Extended ISnuviLogger

Kajetan Johannes Hammerle 7 years ago
parent
commit
3942af6ef3

BIN
.DS_Store


+ 6 - 7
src/me/hammerle/code/Code.java

@@ -2,7 +2,6 @@ package me.hammerle.code;
 
 import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.Map.Entry;
 import java.util.Stack;
 import java.util.TreeSet;
 import me.hammerle.exceptions.PreScriptException;
@@ -331,7 +330,7 @@ public class Code implements Comparable<Code>
         {
             s = code.substring(pos, Math.min(pos + add, length));
             // additionel check for e.g. difference between + and +=
-            if(s.startsWith(find) && !s.startsWith(find + "=") && code.charAt(pos - 1) != '=') 
+            if(s.startsWith(find) && !s.startsWith(find + "=") && (pos == 0 || code.charAt(pos - 1) != '=')) 
             {
                 return pos;
             }
@@ -343,7 +342,7 @@ public class Code implements Comparable<Code>
     private static int findSyntaxIgnoreString(String find, StringBuilder code, int pos)
     {
         int length = code.length();
-        int add = find.length() + 1;
+        int add = find.length();
         char c;
         boolean text = false;
         String s;
@@ -363,7 +362,7 @@ public class Code implements Comparable<Code>
             }
             s = code.substring(pos, Math.min(pos + add, length));
             // additionel check for e.g. difference between + and +=
-            if(s.startsWith(find) && !s.startsWith(find + "=") && code.charAt(pos - 1) != '=') 
+            if(s.equals(find)) 
             {
                 return pos;
             }
@@ -448,7 +447,7 @@ public class Code implements Comparable<Code>
         return code.replace(find, replacement);
     }
     
-    private static int countChar(char find, String code)
+    public static int countChar(char find, String code)
     {
         int counter = 0;
         int pos = 0;
@@ -661,7 +660,7 @@ public class Code implements Comparable<Code>
             if(actual.startsWith("@"))
             {
                 // sets the right layer of the goto, the exact position is set later
-                if(gotos.put(actual.substring(1), line) != null)
+                if(gotos.put(actual.substring(1), tree.size()) != null)
                 {
                     throw new PreScriptException(scriptName, realLines, code.substring(old, Math.min(code.length(), pos + 20)), "double goto");
                 }
@@ -743,7 +742,7 @@ public class Code implements Comparable<Code>
 
         // end
         //java.util.Arrays.stream(c).forEach(co -> System.out.println(co.toString()));
-        //gotos.forEach((k, v) -> System.out.println(k + "   " + v));
+        //gotos.forEach((k, v) -> System.out.println("Lable " + k + "   " + v));
         System.out.println("END GENERATE - " + (System.currentTimeMillis() - startTime));     
         //System.exit(0);
         return c;

+ 1 - 0
src/me/hammerle/code/ISnuviLogger.java

@@ -5,4 +5,5 @@ import me.hammerle.exceptions.SnuviException;
 public interface ISnuviLogger 
 {
     public void printException(SnuviException ex);
+    public void printException(SnuviException ex, Script s);
 }

+ 4 - 5
src/me/hammerle/code/Script.java

@@ -215,7 +215,7 @@ public class Script
             {
                 if(ex.getClass() != HoldCodeException.class)
                 {
-                    parser.logger.printException(new SnuviException(ex, this));
+                    parser.logger.printException(new SnuviException(ex, this), this);
                 }
                 position++;
             }
@@ -320,11 +320,10 @@ public class Script
             return;
         }
         String s = code[position + 1].getFunction();
-        if(s == null || !s.equals("else"))
+        if(s != null && s.equals("else"))
         {
-            return;
+            position++;
         }
-        position++;
     }
     
     // -----------------------------------------------------------------------------------
@@ -338,7 +337,7 @@ public class Script
     
     public void saveTryJumpLine()
     {
-        tryJumpLine = position + code[position].getJumpLine();
+        tryJumpLine = position + code[position].getJumpLine() + 1;
     }
     
     public void resetTryJumpLine()

+ 12 - 5
src/me/hammerle/code/SnuviParser.java

@@ -298,7 +298,7 @@ public class SnuviParser
         registerConsumer("try", (args, sc) -> 
                 sc.saveTryJumpLine());                   
         registerConsumer("catch", (args, sc) -> 
-                sc.gotoSpecialJumpLine());
+                {sc.gotoSpecialJumpLine(); sc.resetTryJumpLine();});
         registerConsumer("if", (args, sc) -> 
                 ifFunction(args, sc));   
         registerConsumer("else", (args, sc) -> 
@@ -366,11 +366,11 @@ public class SnuviParser
             }
             if(ex instanceof SnuviException)
             {
-                logger.printException((SnuviException) ex);
+                logger.printException((SnuviException) ex, sc);
             }
             else
             {
-                logger.printException(new SnuviException(ex, sc));
+                logger.printException(new SnuviException(ex, sc), sc);
             }
             sc.resetLoopCounter();
             throw new HoldCodeException();
@@ -388,6 +388,11 @@ public class SnuviParser
     
     public boolean termUnsafe(Script sc)
     {
+        if(sc == null)
+        {
+            return false;
+        }
+        sc.setInvalid();
         return scripts.remove(sc.getId()) != null;
     }
     
@@ -434,7 +439,7 @@ public class SnuviParser
         }
         catch(PreScriptException ex)
         {
-            logger.printException(ex);
+            logger.printException(ex, script);
             return null;
         }
     }
@@ -461,8 +466,10 @@ public class SnuviParser
                 if(t != null && t instanceof SnuviException)
                 {
                     logger.printException((SnuviException) t);
+                    return null;
                 }
             }
+            ex.printStackTrace();
             return null;
         }
     }
@@ -568,7 +575,7 @@ public class SnuviParser
             }
             catch(Exception ex)
             {
-                logger.printException(new SnuviException(ex, sc.getName(), "scheduled goto"));
+                logger.printException(new SnuviException(ex, sc.getName(), "scheduled goto"), sc);
             }
         }, getInt(args[0]));
     }

+ 37 - 24
src/me/hammerle/snuviscript/SnuviScript.java

@@ -1,5 +1,6 @@
 package me.hammerle.snuviscript;
 
+import me.hammerle.code.ISnuviLogger;
 import me.hammerle.code.Script;
 import me.hammerle.code.SnuviParser;
 import me.hammerle.exceptions.SnuviException;
@@ -10,17 +11,35 @@ public class SnuviScript
 {
     public static void main(String[] args) 
     {
-        SnuviParser parser = new SnuviParser((SnuviException ex) -> 
+        SnuviParser parser = new SnuviParser(new ISnuviLogger() 
         {
-            System.out.println("Exception " + ex);
-            System.out.println(ex.getOriginalException());
-            System.out.println(ex.getCode());
-            if(ex instanceof PreScriptException)
+            @Override
+            public void printException(SnuviException ex, Script s) 
+            {
+                System.out.println("Exception " + ex);
+                System.out.println(ex.getOriginalException());
+                System.out.println(ex.getCode());
+                if(ex instanceof PreScriptException)
+                {
+                    System.out.println(((PreScriptException) ex).getException());
+                }
+                System.out.println(ex.getLine());
+                System.out.println(ex.getScriptName());
+            }
+
+            @Override
+            public void printException(SnuviException ex) 
             {
-                System.out.println(((PreScriptException) ex).getException());
+                System.out.println("Exception " + ex);
+                System.out.println(ex.getOriginalException());
+                System.out.println(ex.getCode());
+                if(ex instanceof PreScriptException)
+                {
+                    System.out.println(((PreScriptException) ex).getException());
+                }
+                System.out.println(ex.getLine());
+                System.out.println(ex.getScriptName());
             }
-            System.out.println(ex.getLine());
-            System.out.println(ex.getScriptName());
         }, new ISnuviScheduler() 
         {
             @Override
@@ -39,8 +58,9 @@ public class SnuviScript
         });
         parser.registerConsumer("debug", (o, sc) -> System.out.println(o[0]));
         parser.registerFunction("ggv", (o, sc) -> o[0]);
+        parser.registerFunction("read.item", (o, sc) -> o[0]);
         
-        StringBuilder sb = new StringBuilder("wusi = 1;\n");
+        /*StringBuilder sb = new StringBuilder("wusi = 1;\n");
         int counter = 0;
         for(int i = 0; i < 1000; i++)
         {
@@ -58,23 +78,16 @@ public class SnuviScript
                 counter = 0;
             }
         }
-        sb.append("debug(wusi);");
+        sb.append("debug(wusi);");*/
         
-        /*String s = "i = 3;\n" +
-"while(i < 10)\n" +
-"{\n" +
-"    j = 5;\n" +
-"    debug(i);\n" +
-"    while(j < 10)\n" +
-"    {\n" +
-"        debug(j);\n" +
-"        j += 1;\n" +
-"    }\n" +
-"    i += 1;\n" +
-"}";*/
+        String s = "setVar(\"kürbis\",read.item(\"{id:'minecraft:pumpki"
+                + "n_pie',Count:64b,tag:{display:{Lore:[0:'Ein fast ganze"
+                + "r Kürbiskuchen,',1:'bestehend aus sieben Achteln.'],Nam"
+                + "e:'KürbisMinusEinAchtelKuchen'}},Damage:0s}\")); debug(kürbis);";
         //System.out.println(s);
         //System.out.println("___________");
-        parser.startScript(Script.class, "test", sb.toString(), true);
-        //parser.startScript(Script.class, "test", s, true);
+        //parser.startScript(Script.class, "test", sb.toString(), true);
+        parser.startScript(Script.class, "test", s, true);
+        //parser.getScript(0).runCode();
     }   
 }