Kajetan Johannes Hammerle 7 anni fa
parent
commit
6b76902b08

+ 5 - 45
src/me/hammerle/code/LineCompiler.java

@@ -64,14 +64,6 @@ public class LineCompiler
                 {
                     throw new PreScriptException(scriptName, realLine, "unexpected character '" + c + "'");
                 }
-                else if(syntax.isIncOrDec())
-                {
-                    Code change = co.get(co.size() - 1);
-                    if("array.get".equals(change.function))
-                    {
-                        change.function = "array." + syntax.getFunction();
-                    }
-                }
                 s = line.substring(i + 1, old).toLowerCase();
                 if(!s.isEmpty())
                 {
@@ -116,45 +108,15 @@ public class LineCompiler
                     }
                     old++;
                     s = line.substring(old, pos + 1).toLowerCase();
-                    if(syntax.isArray())
+                    minus = false;
+                    if(line.startsWith("()", pos + 1))
                     {
-                        minus = false;
-                        co.add(new Code(s, realLine, layer));
-                        if(!syntaxStack.isEmpty())
-                        {
-                            Stack<Syntax> arrayFunction = syntaxStack.pop();
-                            if(!arrayFunction.isEmpty())
-                            {
-                                Syntax sy = arrayFunction.pop();
-                                if(sy.isIncOrDec())
-                                {
-                                    if(sy == Syntax.INC)
-                                    {
-                                        sy = Syntax.POST_INC;
-                                    }
-                                    else if(sy == Syntax.DEC)
-                                    {
-                                        sy = Syntax.POST_DEC;
-                                    }
-                                }
-                                co.add(new Code("array." + sy.getFunction(), sy.isIncOrDec() ? 2 : 3, realLine, layer));
-                                continue;
-                            }
-                        }
-                        co.add(new Code("array.get", 2, realLine, layer));
+                        commaCounter.pop();
+                        co.add(new Code(s, 0, realLine, layer));
                     }
                     else
                     {
-                        minus = false;
-                        if(line.startsWith("()", pos + 1))
-                        {
-                            commaCounter.pop();
-                            co.add(new Code(s, 0, realLine, layer));
-                        }
-                        else
-                        {
-                            co.add(new Code(s, commaCounter.pop() + 1, realLine, layer));
-                        }
+                        co.add(new Code(s, commaCounter.pop() + 1, realLine, layer));
                     }
                     continue;
                 }
@@ -228,8 +190,6 @@ public class LineCompiler
         {
             case '(': return Syntax.OPEN_BRACKET;
             case ')': return Syntax.CLOSE_BRACKET;
-            case '[': return Syntax.OPEN_SBRACKET;
-            case ']': return Syntax.CLOSE_SBRACKET;
             case '!': return Syntax.NOT;
             case '~': return Syntax.BIT_NOT;
             case '*': return Syntax.MUL;

+ 53 - 21
src/me/hammerle/code/SnuviParser.java

@@ -24,6 +24,7 @@ import java.util.function.BiConsumer;
 import java.util.function.Consumer;
 import java.util.function.Predicate;
 import java.util.stream.Collectors;
+import me.hammerle.config.SnuviConfig;
 import me.hammerle.exceptions.FileIOException;
 import me.hammerle.exceptions.GotoLabelNotFoundException;
 import me.hammerle.exceptions.PreScriptException;
@@ -166,7 +167,7 @@ public class SnuviParser
         registerFunction("math.ln", (args, sc) -> 
                 ((Fraction) args[0]).log());
         registerFunction("math.log", (args, sc) -> 
-                ((Fraction) args[0]).log());
+                ((Fraction) args[0]).log10());
         registerFunction("math.random", (args, sc) -> 
                 new Fraction(ScriptUtils.randomInt(getInt(args[0]), getInt(args[1]))));
         registerFunction("math.round", (args, sc) -> 
@@ -213,7 +214,7 @@ public class SnuviParser
         // -------------------------------------------------------------------------------  
         // arrays
         // -------------------------------------------------------------------------------    
-        registerConsumer("array.new", (args, sc) ->                                                
+        /*registerConsumer("array.new", (args, sc) ->                                                
                 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]);
@@ -311,7 +312,7 @@ public class SnuviParser
                     o[i] = ((Fraction) o[i]).or((Fraction) args[2]);
                 });
         registerFunction("array.getsize", (args, sc) ->                            
-                ((Object[]) args[0]).length);
+                new Fraction(((Object[]) args[0]).length));
         registerConsumer("array.swap", (args, sc) ->                                           
                 {
                     Object[] o = (Object[]) args[0];
@@ -360,7 +361,7 @@ public class SnuviParser
                     {
                         Arrays.fill((Object[]) args[0], ScriptUtils.getInt(args[2]), ScriptUtils.getInt(args[3]), args[1]); 
                     }
-                });
+                });*/
                 
 
         // -------------------------------------------------------------------------------  
@@ -371,21 +372,23 @@ public class SnuviParser
         registerFunction("map.exists", (args, sc) ->                                                
                 args[0] instanceof Map);  
         registerConsumer("map.add", (args, sc) ->                            
-                ((HashMap) args[0]).put(args[1], args[2]));
+                ((Map) args[0]).put(args[1], args[2]));
         registerConsumer("map.remove", (args, sc) ->                            
-                ((HashMap) args[0]).remove(args[1]));
+                ((Map) args[0]).remove(args[1]));
         registerFunction("map.contains", (args, sc) ->                            
-                ((HashMap) args[0]).containsKey(args[1]));
+                ((Map) args[0]).containsKey(args[1]));
         registerFunction("map.getsize", (args, sc) ->                            
-                new Fraction(((HashMap) args[0]).size()));
+                new Fraction(((Map) args[0]).size()));
         registerFunction("map.get", (args, sc) ->                            
-                ((HashMap) args[0]).get(args[1]));
+                ((Map) args[0]).get(args[1]));
+        registerFunction("map.getordefault", (args, sc) ->                            
+                ((Map) args[0]).getOrDefault(args[1], args[2]));
         registerConsumer("map.clear", (args, sc) ->                            
-                ((HashMap) args[0]).clear());
+                ((Map) args[0]).clear());
         registerConsumer("map.keys", (args, sc) ->                            
-                sc.setVar(args[0].toString(), ((HashMap) args[1]).keySet().stream().collect(Collectors.toList())));
+                sc.setVar(args[0].toString(), ((Map) args[1]).keySet().stream().collect(Collectors.toList())));
         registerConsumer("map.values", (args, sc) ->                            
-                sc.setVar(args[0].toString(), ((HashMap) args[1]).values().stream().collect(Collectors.toList())));
+                sc.setVar(args[0].toString(), ((Map) args[1]).values().stream().collect(Collectors.toList())));
         
         // -------------------------------------------------------------------------------  
         // sets
@@ -395,15 +398,15 @@ public class SnuviParser
         registerFunction("set.exists", (args, sc) ->                                                
                 args[0] instanceof Set);  
         registerConsumer("set.add", (args, sc) ->                            
-                ((HashSet) args[0]).add(args[1]));
+                ((Set) args[0]).add(args[1]));
         registerConsumer("set.remove", (args, sc) ->                            
-                ((HashSet) args[0]).remove(args[1]));
+                ((Set) args[0]).remove(args[1]));
         registerFunction("set.contains", (args, sc) ->                            
-                ((HashSet) args[0]).contains(args[1]));
+                ((Set) args[0]).contains(args[1]));
         registerFunction("set.getsize", (args, sc) ->                            
-                new Fraction(((HashSet) args[0]).size()));
+                new Fraction(((Set) args[0]).size()));
         registerConsumer("set.tolist", (args, sc) ->                            
-                sc.setVar(args[0].toString(), ((HashSet) args[1]).stream().collect(Collectors.toList())));
+                sc.setVar(args[0].toString(), ((Set) args[1]).stream().collect(Collectors.toList())));
 
         // -------------------------------------------------------------------------------  
         // time
@@ -428,6 +431,8 @@ public class SnuviParser
         // -------------------------------------------------------------------------------    
         // text
         // -------------------------------------------------------------------------------   
+        registerFunction("text.matches", (args, sc) ->         
+                args[0].toString().matches(args[1].toString())); 
         registerFunction("text.number", (args, sc) ->         
                 fractionToDoubleString((Fraction) args[0])); 
         registerFunction("text.class", (args, sc) ->         
@@ -478,16 +483,14 @@ public class SnuviParser
         // files
         // ------------------------------------------------------------------------------- 
         
-        registerFunction("file.new", (args, sc) ->         
-                new File(args[0].toString()));
+        registerConsumer("file.new", (args, sc) ->    
+                sc.setVar(args[0].toString(), new File(args[1].toString())));
         registerFunction("file.exists", (args, sc) ->         
                 ((File) args[0]).exists());
         registerFunction("file.delete", (args, sc) ->         
                 ((File) args[0]).delete());
         registerFunction("file.getname", (args, sc) ->         
                 ((File) args[0]).getName());
-        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())));
         registerConsumer("file.read", (args, sc) ->         
@@ -495,6 +498,27 @@ public class SnuviParser
         registerConsumer("file.write", (args, sc) ->         
                 writeFile(args, sc));
         
+        // ---------------------------------------------------------------------  
+        // config
+        // ---------------------------------------------------------------------
+        
+        registerConsumer("config.new", (args, sc) ->         
+                sc.setVar(args[0].toString(), new SnuviConfig(logger, args[1].toString(), args[2].toString())));
+        registerFunction("config.exists", (args, sc) ->         
+                ((SnuviConfig) args[0]).exists());
+        registerFunction("config.save", (args, sc) ->         
+                ((SnuviConfig) args[0]).save());
+        registerFunction("config.delete", (args, sc) ->         
+                ((SnuviConfig) args[0]).delete());
+        registerConsumer("config.set", (args, sc) ->         
+                ((SnuviConfig) args[0]).set(args[1].toString(), args[2]));
+        registerFunction("config.getbool", (args, sc) ->         
+                ((SnuviConfig) args[0]).getBoolean(args[1].toString(), args[2] == null ? null : (boolean) args[2]));
+        registerFunction("config.getfraction", (args, sc) ->         
+                ((SnuviConfig) args[0]).getFraction(args[1].toString(), (Fraction) args[2]));
+        registerFunction("config.getstring", (args, sc) ->         
+                ((SnuviConfig) args[0]).getString(args[1].toString(), args[2] == null ? null : args[2].toString()));
+        
         // -------------------------------------------------------------------------------    
         // commands without library
         // -------------------------------------------------------------------------------    
@@ -694,6 +718,14 @@ public class SnuviParser
                     sc.setVar(first, sc.getVar(sec));
                     sc.setVar(sec, helper);
                 });
+        registerFunction("islong", (args, sc) ->                                           
+                {
+                    if(args[0] instanceof Fraction)
+                    {
+                        return ((Fraction) args[0]).isLong();
+                    }
+                    return false;
+                });
     }
     
     @SuppressWarnings("unchecked")

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

@@ -5,8 +5,6 @@ public enum Syntax
     UNKNOWN(" ", 0),
     OPEN_BRACKET("(", 1, (byte) 2),
     CLOSE_BRACKET(")", 1, (byte) 1),
-    OPEN_SBRACKET("[", 1, (byte) 4),
-    CLOSE_SBRACKET("]", 1, (byte) 3),
     INC("++", 2, (byte) 5, (byte) 1),
     POST_INC("p+", 2, (byte) 5, (byte) 1),
     DEC("--", 2, (byte) 5, (byte) 1),
@@ -79,12 +77,12 @@ public enum Syntax
 
     public boolean shouldStartLayer()
     {
-        return status == 1 || status == 3;
+        return status == 1;
     }
 
     public boolean shouldEndLayer()
     {
-        return status == 2 || status == 4;
+        return status == 2;
     }
     
     public boolean isFunction()
@@ -92,11 +90,6 @@ public enum Syntax
         return status >= 0;
     }
 
-    public boolean isArray()
-    {
-        return status == 3 || status == 4;
-    }
-    
     public boolean isIncOrDec()
     {
         return status == 5;

+ 5 - 0
src/me/hammerle/config/SnuviConfig.java

@@ -189,6 +189,11 @@ public class SnuviConfig
         return get(key, Fraction.class, Fraction.fromDouble(error)).intValue();
     }
     
+    public final Fraction getFraction(String key, Fraction error)
+    {
+        return get(key, Fraction.class, error);
+    }
+    
     public final boolean getBoolean(String key, boolean error)
     {
         return get(key, Boolean.class, error);

+ 5 - 0
src/me/hammerle/math/Fraction.java

@@ -286,6 +286,11 @@ public final class Fraction extends Number implements Comparable<Fraction>
         return numerator < 0;
     }
     
+    public boolean isLong()
+    {
+        return denominator == 1;
+    }
+    
     // -------------------------------------------------------------------------
     // functions from math library
     // -------------------------------------------------------------------------

+ 1 - 5
src/me/hammerle/snuviscript/SnuviScript.java

@@ -61,11 +61,7 @@ public class SnuviScript
         parser.registerConsumer("print", (o, sc) -> System.out.println(o[0]));
         parser.registerFunction("get", (o, sc) -> new Fraction(1));
             
-        String s = "i = 0;\n" +
-            "while(i++ < 3) {\n" +
-            "  print(i);\n" +
-            "}\n" +
-            "term();";
+        String s = "print(5*(-3));";
         System.out.println(s);
         System.out.println();
         try