Browse Source

bugfix with negative numbers, added deprecated functions

Kajetan Johannes Hammerle 7 years ago
parent
commit
69b28b71c8

+ 18 - 0
src/me/hammerle/code/LineCompiler.java

@@ -4,6 +4,7 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Stack;
 import me.hammerle.exceptions.PreScriptException;
+import me.hammerle.math.Fraction;
 
 public class LineCompiler 
 {
@@ -16,12 +17,16 @@ public class LineCompiler
     
     private String scriptName;
     
+    // helper to support things like print(-3);
+    private boolean minus;
+    
     public LineCompiler(SnuviParser parser, String scriptName)
     {
         realLine = 0;
         commaCounter = new Stack<>();
         line = "";
         layer = 0;
+        minus = false;
         this.scriptName = scriptName;
     }
 
@@ -29,6 +34,7 @@ public class LineCompiler
     {
         this.realLine = realLine;
         commaCounter.clear();
+        minus = false;
         this.line = line;
         this.layer = layer;
     }
@@ -85,6 +91,7 @@ public class LineCompiler
                         }
                     }
                     co.add(new Code(Code.convertInput(strings, s, true), realLine, layer));
+                    minus = false;
                 }
                 i += 1 - syntax.getFunction().length();
                 old = i;
@@ -111,6 +118,7 @@ public class LineCompiler
                     s = line.substring(old, pos + 1).toLowerCase();
                     if(syntax.isArray())
                     {
+                        minus = false;
                         co.add(new Code(s, realLine, layer));
                         if(!syntaxStack.isEmpty())
                         {
@@ -137,6 +145,7 @@ public class LineCompiler
                     }
                     else
                     {
+                        minus = false;
                         if(line.startsWith("()", pos + 1))
                         {
                             co.add(new Code(s, 0, realLine, layer));
@@ -162,6 +171,10 @@ public class LineCompiler
                 {
                     commaCounter.push(commaCounter.pop() + 1);
                 }
+                else if(syntax == Syntax.SUB)
+                {
+                    minus = true;
+                }
             }
         }
         
@@ -196,6 +209,11 @@ public class LineCompiler
             }
             else
             {
+                if(minus)
+                {
+                    minus = false;
+                    co.add(new Code(new Fraction(0), realLine, layer));
+                }
                 co.add(new Code(sy.getFunction(), sy.getParameters(), realLine, layer));
             }
         }

+ 86 - 6
src/me/hammerle/code/SnuviParser.java

@@ -140,6 +140,7 @@ public class SnuviParser
         // -------------------------------------------------------------------------------    
         registerFunction("%", (args, sc) -> 
                 new Fraction(getInt(args[0]) % getInt(args[1])));
+        registerAlias("math.mod", "%");
         registerFunction("math.abs", (args, sc) -> 
                 ((Fraction) args[0]).abs());
         registerFunction("math.pow", (args, sc) -> 
@@ -213,9 +214,10 @@ public class SnuviParser
         // arrays
         // -------------------------------------------------------------------------------    
         registerConsumer("array.new", (args, sc) ->                                                
-                sc.setVar(args[0].toString(), new Object[ScriptUtils.getInt(args[1])]));
+                sc.setVar(args[0].toString(), new Object[ScriptUtils.getInt(args[1])]));      
         registerConsumer("array.=", (args, sc) ->                            
                 ((Object[]) sc.getVar(args[0].toString()))[ScriptUtils.getInt(args[1])] = args[2]);
+        registerAlias("array.set", "array.=");
         registerFunction("array.get", (args, sc) ->                            
                 ((Object[]) sc.getVar(args[0].toString()))[ScriptUtils.getInt(args[1])]);
         registerFunction("array.++", (args, sc) -> 
@@ -272,6 +274,42 @@ public class SnuviParser
                     int i = ScriptUtils.getInt(args[1]);
                     o[i] = ((Fraction) o[i]).div((Fraction) args[2]);
                 });
+        registerConsumer("array.%=", (args, sc) -> 
+                {
+                    Object[] o =  (Object[]) sc.getVar(args[0].toString());
+                    int i = ScriptUtils.getInt(args[1]);
+                    o[i] = ScriptUtils.getInt(o[i]) % ScriptUtils.getInt(args[2]);
+                });
+        registerConsumer("array.<<=", (args, sc) -> 
+                {
+                    Object[] o =  (Object[]) sc.getVar(args[0].toString());
+                    int i = ScriptUtils.getInt(args[1]);
+                    o[i] = ((Fraction) o[i]).leftShift(ScriptUtils.getInt(args[2]));
+                });
+        registerConsumer("array.>>=", (args, sc) -> 
+                {
+                    Object[] o =  (Object[]) sc.getVar(args[0].toString());
+                    int i = ScriptUtils.getInt(args[1]);
+                    o[i] = ((Fraction) o[i]).rightShift(ScriptUtils.getInt(args[2]));
+                });
+        registerConsumer("array.&=", (args, sc) -> 
+                {
+                    Object[] o =  (Object[]) sc.getVar(args[0].toString());
+                    int i = ScriptUtils.getInt(args[1]);
+                    o[i] = ((Fraction) o[i]).and((Fraction) args[2]);
+                });
+        registerConsumer("array.^=", (args, sc) -> 
+                {
+                    Object[] o =  (Object[]) sc.getVar(args[0].toString());
+                    int i = ScriptUtils.getInt(args[1]);
+                    o[i] = ((Fraction) o[i]).xor((Fraction) args[2]);
+                });
+        registerConsumer("array.|=", (args, sc) -> 
+                {
+                    Object[] o =  (Object[]) sc.getVar(args[0].toString());
+                    int i = ScriptUtils.getInt(args[1]);
+                    o[i] = ((Fraction) o[i]).or((Fraction) args[2]);
+                });
         registerFunction("array.getsize", (args, sc) ->                            
                 ((Object[]) args[0]).length);
         registerConsumer("array.swap", (args, sc) ->                                           
@@ -448,7 +486,7 @@ public class SnuviParser
                 ((File) args[0]).delete());
         registerFunction("file.getname", (args, sc) ->         
                 ((File) args[0]).getName());
-        registerFunction("file.", (args, sc) ->         
+        registerFunction("file.rename", (args, sc) ->         
                 ((File) args[0]).renameTo(new File(args[1].toString())));
         registerConsumer("file.getlist", (args, sc) ->         
                 sc.setVar(args[0].toString(),  Arrays.asList(((File) args[0]).listFiles())));
@@ -462,8 +500,10 @@ public class SnuviParser
         // -------------------------------------------------------------------------------    
         registerFunction("+", (args, sc) -> 
                 ((Fraction) args[0]).add((Fraction) args[1]));
+        registerAlias("add", "+");
         registerFunction("-", (args, sc) -> 
                 ((Fraction) args[0]).sub((Fraction) args[1]));
+        registerAlias("sub", "-");
         registerConsumer("+=", (args, sc) -> 
                 {
                     String s = args[0].toString();
@@ -484,6 +524,36 @@ public class SnuviParser
                     String s = args[0].toString();
                     sc.setVar(s, ((Fraction) sc.getVar(s)).div((Fraction) args[1]));
                 });
+        registerConsumer("%=", (args, sc) -> 
+                {
+                    String s = args[0].toString();
+                    sc.setVar(s, ScriptUtils.getInt(sc.getVar(s)) % ScriptUtils.getInt(args[1]));
+                });
+        registerConsumer("<<=", (args, sc) -> 
+                {
+                    String s = args[0].toString();
+                    sc.setVar(s, ((Fraction) sc.getVar(s)).leftShift(ScriptUtils.getInt(args[1])));
+                });
+        registerConsumer(">>=", (args, sc) -> 
+                {
+                    String s = args[0].toString();
+                    sc.setVar(s, ((Fraction) sc.getVar(s)).rightShift(ScriptUtils.getInt(args[1])));
+                });
+        registerConsumer("&=", (args, sc) -> 
+                {
+                    String s = args[0].toString();
+                    sc.setVar(s, ((Fraction) sc.getVar(s)).and((Fraction) args[1]));
+                });
+        registerConsumer("^=", (args, sc) -> 
+                {
+                    String s = args[0].toString();
+                    sc.setVar(s, ((Fraction) sc.getVar(s)).xor((Fraction) args[1]));
+                });
+        registerConsumer("|=", (args, sc) -> 
+                {
+                    String s = args[0].toString();
+                    sc.setVar(s, ((Fraction) sc.getVar(s)).or((Fraction) args[1]));
+                });
         registerFunction("inc", (args, sc) -> 
                 {
                     String s = args[0].toString();
@@ -500,26 +570,26 @@ public class SnuviParser
                     return n;
                 });
         registerAlias("--", "dec");
-        registerFunction("postinc", (args, sc) -> 
+        registerFunction("p+", (args, sc) -> 
                 {
                     String s = args[0].toString();
                     Fraction n = (Fraction) sc.getVar(s);
                     sc.setVar(s, n.add(new Fraction(1)));
                     return n;
                 });
-        registerAlias("p+", "postinc");
-        registerFunction("postdec", (args, sc) -> 
+        registerFunction("p-", (args, sc) -> 
                 {
                     String s = args[0].toString();
                     Fraction n = (Fraction) sc.getVar(s);
                     sc.setVar(s, n.add(new Fraction(-1)));
                     return n;
                 });
-        registerAlias("p-", "postdec");
         registerFunction("*", (args, sc) -> 
                 ((Fraction) args[0]).mul((Fraction) args[1]));
+        registerAlias("mul", "*");
         registerFunction("/", (args, sc) -> 
                 ((Fraction) args[0]).div((Fraction) args[1]));
+        registerAlias("div", "/");
         registerFunction("getvar", (args, sc) -> 
                 sc.getVar(args[0].toString()));
         registerConsumer("setvar", (args, sc) -> 
@@ -586,22 +656,32 @@ public class SnuviParser
                 sc.jump()); 
         registerFunction("==", (args, sc) -> 
                 isEqual(args));
+        registerAlias("equal", "==");
+        registerAlias("equals", "==");
         registerFunction("<", (args, sc) -> 
                 ((Fraction) args[0]).compareTo((Fraction) args[1]) < 0);
+        registerAlias("less", "<");
         registerFunction(">", (args, sc) -> 
                 ((Fraction) args[0]).compareTo((Fraction) args[1]) > 0);
+        registerAlias("greater", ">");
         registerFunction("!=", (args, sc) -> 
                 !isEqual(args));
+        registerAlias("notequal", "!=");
         registerFunction("<=", (args, sc) -> 
                 ((Fraction) args[0]).compareTo((Fraction) args[1]) <= 0);
+        registerAlias("lessequal", "<=");
         registerFunction(">=", (args, sc) -> 
                 ((Fraction) args[0]).compareTo((Fraction) args[1]) >= 0);
+        registerAlias("greaterequal", ">=");
         registerFunction("!", (args, sc) -> 
                 !((boolean) args[0]));
+        registerAlias("invert", "!");
         registerFunction("&&", (args, sc) -> 
                 Arrays.stream(args).allMatch(s -> s.equals(true)));
+        registerAlias("and", "&&");
         registerFunction("||", (args, sc) -> 
                 Arrays.stream(args).anyMatch(s -> s.equals(true)));
+        registerAlias("or", "||");
         registerConsumer("waitfor", (args, sc) ->     
                 waitFor(args, sc));
         registerConsumer("term", (args, sc) -> 

+ 11 - 97
src/me/hammerle/snuviscript/SnuviScript.java

@@ -60,103 +60,17 @@ public class SnuviScript
         });
         parser.registerConsumer("print", (o, sc) -> System.out.println(o[0]));
         parser.registerFunction("get", (o, sc) -> new Fraction(1));
-        //parser.registerFunction("ggv", (o, sc) -> o[0]);
-        //parser.registerFunction("read.item", (o, sc) -> o[0]);
-             
-        /*for(int j = 1; j < Integer.MAX_VALUE; j *= 2)
-        {
-            System.out.println("Size " + j);
-            StringBuilder sb = new StringBuilder("error(); wusi = 1;\n");
-            sb.append("time = time.get();");
-            int counter = 0;
-            for(int i = 0; i < j; i++)
-            {
-                switch(counter)
-                {
-                    case 0: sb.append("wusi += 1;\n"); break;
-                    case 1: sb.append("wusi *= 2;\n"); break;
-                    case 2: sb.append("wusi = wusi - 1;\n"); break;
-                    case 3: sb.append("wusi /= 2;\n"); break;
-                    case 4: sb.append("wusi += 1;\n"); break;
-                }
-                counter++;
-                if(counter >= 5)
-                {
-                    counter = 0;
-                }
-            }
-            sb.append("debug(wusi);");
-            sb.append("debug(concat(\"time \", time.get() - time));");*/
-
-            /*String s = "debug(\"Start\");\n" +
-    "wusi = time.get();\n" +
-    "if(wusi >= 0)\n" +
-    "{\n" +
-    "    debug(\"ja\");\n" +
-    "    while(wusi < 10)\n" +
-    "    {\n" +
-    "        hallo = wusi;\n" +
-    "        while(hallo < 3)\n" +
-    "        {\n" +
-    "            debug(\"hallo\");\n" +
-    "            hallo += 1;\n" +
-    "        }\n" +
-    "        debug(wusi);\n" +
-    "        wusi += 1;\n" +
-    "    }\n" +
-    "}\n" +
-    "else\n" +
-    "{\n" +
-    "    debug(\"nein\");\n" +
-    "}\n" +
-    "debug(\"Ende\");";*/
-            //String s = "wusi = 3; wusi /= 2; debug(wusi);";
-            // array.set("wusi", 1, arrays.get("wusi", 1) + 4)
-            //String s = "debug(wusi[1]);";
-            /*String s = "print(\"wusi\");\n" +
-"print(hallo(1, 3, 5));\n" +
-"term();\n" +
-"\n" +
-"@hallo;\npopArg(); \n" +
-"pushArg(popArg() + popArg() + popArg());\n" +
-"return;";*/
             
-            //String s = "print(1 + 2 * 3 * 5 + 2);";
-            //String s = "print(1 + get(3 - 2) + 1);";
-            //String s = "print(1 + get(1, 2) == 1 || get() == 1);";
-            //String s = "print(12 + 234 == 32 || 4 == 45);";           
-            //String s = "setvar(\"test\", 3); print(test + get(2, 5 + get(9, 8 + 3) + 6) == 4);";
-            
-            //String s = "print(array[2 + 3]);";
-            //String s = "a = 3; a++; print(a);";
-            //String s = "array[2 + 3] = 5;";
-            //String s = "a(1+b(2), c(3) + 3, d(7,e(10 + 3)))";
-            //String s = "++wusi[1];";
-            //String s = "array.new(\"wusi\", 10); wusi[2] = 3; wusi[2]++; print(wusi[2]);";
-        
-            //String s = "print(1 + get() == 1);";
-            String s = "hallo = 1; print(1.23);";
-            System.out.println(s);
-            System.out.println();
-            //String s = "i = 0; while(i++ < 10) { debug(i);}";
-            //String s = "error(); debug(1+1);";
-            //System.out.println(s);
-            //System.out.println("___________");
-            //parser.startScript(Script.class, "test", sb.toString(), true);
-            try
-            {
-                parser.startScript("test", s.toString());
-            }
-            catch(PreScriptException ex)
-            {
-                logger.printException(ex, "none" , "test", ex.getLine());
-            }
-        //}
-        //parser.getScript(0).runCode();
-
-        //Fraction f = Fraction.fromDouble(Fraction.PI.doubleValue());
-        //Object o = Code.convertInput("32.323");
-        //System.out.println(o + " " + o.getClass().getSimpleName());
-        //System.out.println(Fraction.fromDouble(0.324234235));
+        String s = "print(1, -1, 1);";
+        System.out.println(s);
+        System.out.println();
+        try
+        {
+            parser.startScript("test", s);
+        }
+        catch(PreScriptException ex)
+        {
+            logger.printException(ex, "none" , "test", ex.getLine());
+        }
     }   
 }