Explorar el Código

implemented missing ! and ~ syntax, bugfixes

Kajetan Johannes Hammerle hace 7 años
padre
commit
9f035ce01e

+ 3 - 1
src/me/hammerle/snuviscript/code/BasicFunction.java

@@ -21,6 +21,8 @@ public final class BasicFunction
     public Object execute(Script sc, InputProvider[] input)
     {
         sc.currentFunction = name;
-        return f.apply(sc, input);
+        Object o = f.apply(sc, input);
+        sc.currentFunction = name;
+        return o;
     }
 }

+ 20 - 3
src/me/hammerle/snuviscript/code/Compiler.java

@@ -331,8 +331,8 @@ public class Compiler
         //System.out.println("__________________________________");
         
         Instruction[] input = code.toArray(new Instruction[code.size()]);
-        /*
-        for(Instruction in : input)
+        
+        /*for(Instruction in : input)
         {
             System.out.println(in);
         }
@@ -570,12 +570,16 @@ public class Compiler
                 continue;
             }
             sy = Syntax.getSyntax(parts[i]);
-            if(sy != Syntax.UNKNOWN && sy != Syntax.MAYBE)
+            if(sy != Syntax.UNKNOWN)
             {
                 if(stackCounter <= 0)
                 {
                     switch(sy)
                     {
+                        case INVERT:
+                            break;
+                        case BIT_INVERT:
+                            break;
                         case SUB:
                             sy = Syntax.UNARY_SUB;
                             break;
@@ -589,6 +593,15 @@ public class Compiler
                             throw new PreScriptException("missing syntax argument", line);
                     }
                 }
+                else
+                {
+                    switch(sy)
+                    {
+                        case INVERT:
+                        case BIT_INVERT:
+                            throw new PreScriptException("missing syntax argument", line);
+                    }
+                }
                 // pushing weaker functions
                 int weight = sy.getWeight();
                 while(!syntax.isEmpty() && syntax.peek().getWeight() <= weight)
@@ -688,6 +701,10 @@ public class Compiler
         {
             return Fraction.fromDouble(Double.parseDouble(input));
         }
+        else if(input.startsWith("\"") && input.endsWith("\""))
+        {
+            return input.substring(1, input.length() - 1);
+        }
         return input;
     }
     

+ 1 - 1
src/me/hammerle/snuviscript/code/Function.java

@@ -60,7 +60,7 @@ public class Function extends InputProvider
     @Override
     public int getInt(Script sc)
     {
-        return (Integer) function.execute(sc, input);
+        return ((Number) function.execute(sc, input)).intValue();
     }
     
     @Override

+ 23 - 7
src/me/hammerle/snuviscript/code/FunctionLoader.java

@@ -361,19 +361,19 @@ public class FunctionLoader
         });
         registerFunction("text.class", (sc, in) -> in[0].get(sc).getClass().getSimpleName());      
         registerFunction("text.tolowercase", (sc, in) -> SnuviUtils.connect(sc, in, 0).toLowerCase());
-        registerAlias("tolowercase", "text.tolowercase");
+        registerAlias("text.tolowercase", "tolowercase");
         registerFunction("text.touppercase", (sc, in) -> SnuviUtils.connect(sc, in, 0).toUpperCase());
-        registerAlias("touppercase", "text.touppercase");
+        registerAlias("text.touppercase", "touppercase");
         registerFunction("text.split", (sc, in) ->      
         {
             in[0].set(sc, Arrays.stream(SnuviUtils.connect(sc, in, 2).split(in[1].getString(sc))).map(s -> Compiler.convert(s)).collect(Collectors.toList()));
             return Void.TYPE;
         });  
-        registerAlias("split", "text.split");
+        registerAlias("text.split", "split");
         registerFunction("text.concatlist", (sc, in) -> ((List<Object>) in[0].get(sc)).stream().limit(in[3].getInt(sc) + 1).skip(in[2].getInt(sc)).map(o -> String.valueOf(o)).collect(Collectors.joining(in[1].getString(sc))));       
-        registerAlias("concatlist", "text.concatlist");
+        registerAlias("text.concatlist", "concatlist");
         registerFunction("text.concat", (sc, in) -> SnuviUtils.connect(sc, in, 0)); 
-        registerAlias("concat", "text.concat");
+        registerAlias("text.concat", "concat");
         registerFunction("text", (sc, in) -> String.valueOf(in[0].get(sc)));       
         registerFunction("text.substring", (sc, in) -> in[0].getString(sc).substring(in[1].getInt(sc), in[2].getInt(sc))); 
         registerFunction("text.length", (sc, in) ->  in[0].getString(sc).length()); 
@@ -458,6 +458,11 @@ public class FunctionLoader
         });      
         registerFunction("config.exists", (sc, in) -> ((SnuviConfig) in[0].get(sc)).exists());
         registerFunction("config.save", (sc, in) -> ((SnuviConfig) in[0].get(sc)).save());
+        registerFunction("config.load", (sc, in) -> 
+        {
+            ((SnuviConfig) in[0].get(sc)).load();
+            return Void.TYPE;
+        });
         registerFunction("config.delete", (sc, in) -> ((SnuviConfig) in[0].get(sc)).delete());
         registerFunction("config.set", (sc, in) ->      
         {
@@ -499,7 +504,6 @@ public class FunctionLoader
             in[0].set(sc, in[1].get(sc));
             return Void.TYPE;
         });
-        registerAlias("=", "setvar");
         registerFunction("+=", (sc, in) -> 
         {
             in[0].set(sc, in[0].getFraction(sc).add(in[1].getFraction(sc)));
@@ -578,7 +582,19 @@ public class FunctionLoader
         });
         
         // var stuff
-        registerFunction("getvar", (sc, in) -> sc.getVar(in[0].getString(sc)).get(sc));      
+        registerFunction("getvar", (sc, in) -> sc.getVar(in[0].getString(sc)).get(sc));   
+        registerFunction("setvar", (sc, in) -> 
+        {
+            sc.getVar(in[0].getString(sc)).set(sc, in[1].get(sc));
+            return Void.TYPE;
+        });
+        registerFunction("removevar", (sc, in) -> 
+        {
+            sc.getVar(in[0].getString(sc)).set(sc, null);
+            return Void.TYPE;
+        });  
+        
+        
         registerFunction("wait", (sc, in) -> 
         {
             sc.isWaiting = true;

+ 16 - 3
src/me/hammerle/snuviscript/code/Script.java

@@ -152,12 +152,12 @@ public final class Script
                     continue;
                 }
                 logger.print(ex.getLocalizedMessage(), ex, currentFunction, name, this, code[currentLine].getRealLine() + 1);
-                ex.printStackTrace();
+                //ex.printStackTrace();
                 return returnValue;
             }
             time = System.nanoTime() - time;
             cpuTime += time;
-            if(cpuTime > 10_000_000)
+            if(cpuTime > 15_000_000)
             {
                 if(subScript)
                 {
@@ -223,12 +223,25 @@ public final class Script
         if(subScript)
         {
             map = localVars.peek();
+            Variable var = map.get(name);
+            if(var == null)
+            {
+                var = new LocalVariable(name);
+                map.put(name, var);
+            }
+            return var;
         }
         else
         {
             map = vars;
+            Variable var = map.get(name);
+            if(var == null)
+            {
+                var = new Variable(name);
+                map.put(name, var);
+            }
+            return var;
         }
-        return map.get(name);
     }
     
     public void setVar(String name, Object value)

+ 9 - 2
src/me/hammerle/snuviscript/code/Syntax.java

@@ -3,11 +3,12 @@ package me.hammerle.snuviscript.code;
 public enum Syntax
 {
     UNKNOWN(" ", 0, 0),
-    MAYBE("", 0, 0),
     INC("++", 2, 1),
     POST_INC("p+", 2, 1),
     DEC("--", 2, 1),
     POST_DEC("p-", 2, 1),
+    INVERT("!", 2, 1),
+    BIT_INVERT("~", 2, 1),
     MUL("*", 3),
     DIV("/", 3),
     MOD("%", 3),
@@ -51,6 +52,12 @@ public enum Syntax
         {
             switch(s.charAt(0))
             {
+                case '~':
+                    if(size == 1)
+                    {
+                        return BIT_INVERT;
+                    }
+                    break;
                 case '*':
                     if(size == 1)
                     {
@@ -148,7 +155,7 @@ public enum Syntax
                 case '!':
                     if(size == 1)
                     {
-                        return MAYBE;
+                        return INVERT;
                     }
                     else if(size == 2 && s.charAt(1) == '=')
                     {

+ 6 - 7
test.sbasic

@@ -1,7 +1,6 @@
-a = 3;
-b_s = 7;
-event.load("testevent");
-print(b_s);
-print(b_s);
-print(b_s);
-wait();
+array.new(a[2]);
+a[0] = 3;
+a[0] += 3;
+a[0]++;
+a[1] = 5;
+print(a);