Преглед на файлове

Basics für neue Ausführungsweise, lineare Abarbeitung mit Stack

Kajetan Johannes Hammerle преди 8 години
родител
ревизия
26569200b4

+ 6 - 1
nbproject/private/private.xml

@@ -2,6 +2,11 @@
 <project-private xmlns="http://www.netbeans.org/ns/project-private/1">
     <editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
     <open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
-        <group/>
+        <group>
+            <file>file:/Users/kajetanjohannes/Dropbox/Projekte/Informatik/SnuviScript/src/me/hammerle/snuviscript/SnuviScript.java</file>
+            <file>file:/Users/kajetanjohannes/Dropbox/Projekte/Informatik/SnuviScript/src/me/hammerle/code/CodeParser.java</file>
+            <file>file:/Users/kajetanjohannes/Dropbox/Projekte/Informatik/SnuviScript/src/me/hammerle/code/Code.java</file>
+            <file>file:/Users/kajetanjohannes/Dropbox/Projekte/Informatik/SnuviScript/src/me/hammerle/code/Script.java</file>
+        </group>
     </open-files>
 </project-private>

+ 397 - 0
src/me/hammerle/code/Code.java

@@ -0,0 +1,397 @@
+package me.hammerle.code;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Stack;
+import me.hammerle.math.Fraction;
+
+public class Code implements Comparable<Code>
+{
+    private final int line;
+    private final int subline;
+    
+    private final String function;
+    private final int level;
+    private final int pars;
+    
+    private final Object value;
+    private int jump;
+    
+    public Code(String function, int level, int pars, int line, int subline, Object value, int jump)
+    {
+        this.function = function;
+        this.level = level;
+        this.pars = pars;
+        this.line = line;
+        this.subline = subline;
+        this.value = value;
+        this.jump = 0;
+    }
+    
+    public Code(String function, int level, int pars, int line, int subline)
+    {
+        this(function.trim(), level, pars, line, subline, null, 0);
+    }
+    
+    public Code(int level, int pars, int line, int subline, Object value)
+    {
+        this(null, level, pars, line, subline, value, 0);
+    }
+    
+    public void executeFunction(Script sc, Stack<Object> stack)
+    {
+        if(value != null)
+        {
+            if(value.getClass() == Variable.class)
+            {
+                stack.push(sc.getVar(((Variable) value).getName()));
+                return;
+            }
+            stack.push(value);
+            return;
+        }
+        Object[] input = null;
+        if(pars > 0)
+        {
+            input = new Object[pars];
+            for(int i = 0; i < pars; i++)
+            {
+                input[i] = stack.pop();
+            }
+        }
+        Object output = CodeParser.parseFunction(sc, function, input, jump);
+        if(output != Void.TYPE)
+        {
+            stack.push(output);
+        }
+    }
+
+    @Override
+    public String toString() 
+    {
+        StringBuilder sb = new StringBuilder(">>>");
+        for(int i = 1; i < level; i++)
+        {
+            sb.append(">>>");
+        }
+        sb.append(" (");
+        sb.append(line);
+        sb.append(") ");
+        
+        if(value != null)
+        {
+            sb.append("push ");
+            sb.append(value.getClass());
+            sb.append(" value = '");
+            sb.append(value);
+            sb.append("'");
+            return sb.toString();
+        }
+        
+        sb.append("function ");
+        sb.append(function);
+        sb.append(" (");
+        for(int i = 0; i < pars; i++)
+        {
+            sb.append("X,");
+        }
+        if(pars > 0)
+        {
+            sb.deleteCharAt(sb.length() - 1);
+        }
+        sb.append(")");
+        if(jump != 0)
+        {
+            sb.append(" (");
+            sb.append(jump);
+            sb.append(")");
+        }
+        return sb.toString();
+    }
+    
+    @Override
+    public int compareTo(Code o) 
+    {
+        int i = Integer.compare(line, o.line);
+        if(i == 0)
+        {
+            return Integer.compare(o.subline, subline);
+        }
+        return i;
+    }
+    
+    // -----------------------------------------------------------------------------------
+    // Code-Builder
+    // -----------------------------------------------------------------------------------
+    
+    private static int sublines = 0;
+    
+    private static int findEndOfLine(String code, int pos)
+    {
+        int length = code.length();
+        char c;
+        boolean text = false;
+        while(pos < length)
+        {
+            c = code.charAt(pos);
+            if(text && c != '"')
+            {
+                pos++;
+                continue;
+            }
+            switch(c)
+            {
+                case ';':
+                case '{':
+                case '}':
+                    return pos;
+                case '"':
+                    text = !text;
+                    break;
+            }
+            pos++;
+        }
+        return -1;
+    }
+    
+    public static Code[] generate(String code)
+    {
+        ArrayList<Code> list = new ArrayList<>();
+        sublines = 0;
+        int length = code.length();
+        int level = 1;
+        int pos = 0;
+        int old;
+        int line = 0;
+        while(pos < length)
+        {
+            old = pos;
+            pos = findEndOfLine(code, pos);
+            switch(code.charAt(pos))
+            {
+                case '{':
+                    line++;
+                    splitFunctions(list, code.substring(old, pos).trim(), level, line);
+                    level++;
+                    break;
+                case '}': 
+                    level--;
+                    break;
+                case ';': 
+                    line++;
+                    splitFunctions(list, code.substring(old, pos).trim(), level, line);
+                    break;
+            }
+            pos++;
+        }
+        Collections.sort(list);
+        
+        String function;
+        int baseLevel;
+        for(int i = 0; i < list.size(); i++)
+        {
+            function = list.get(i).function;
+            if(function == null)
+            {
+                continue;
+            }
+            switch(function)
+            {
+                case "if":
+                case "while":
+                case "try":
+                case "catch":
+                    break;
+                default:
+                    continue;
+            }
+            baseLevel = list.get(i).level;
+            for(int j = i + 1; j < list.size(); j++)
+            {
+                if(list.get(j).level == baseLevel)
+                {
+                    list.get(i).jump = j - i;
+                    break;
+                }
+            }
+            if(list.get(i).jump == 0)
+            {
+               list.get(i).jump = list.size() - i; 
+            }
+        }
+        
+        list.forEach(c -> System.out.println(c.toString()));
+        return list.toArray(new Code[list.size()]);
+    } 
+    
+    private static int findOpenBracket(String code, int pos)
+    {
+        int length = code.length();
+        char c;
+        boolean text = false;
+        while(pos < length)
+        {
+            c = code.charAt(pos);
+            if(text && c != '"')
+            {
+                pos++;
+                continue;
+            }
+            switch(c)
+            {
+                case '(':
+                    return pos;
+                case '"':
+                    text = !text;
+                    break;
+            }
+            pos++;
+        }
+        return length;
+    }
+    
+    private static int findClosingBracket(String code, int pos)
+    {
+        int length = code.length();
+        char c;
+        boolean text = false;
+        int brackets = 0;
+        while(pos < length)
+        {
+            c = code.charAt(pos);
+            if(text && c != '"')
+            {
+                pos++;
+                continue;
+            }
+            switch(c)
+            {
+                case '(':
+                    brackets++;
+                    break;
+                case ')':
+                    brackets--;
+                    break;
+                case '"':
+                    text = !text;
+                    break;
+            }
+            if(brackets == 0)
+            {
+                return pos;
+            }
+            pos++;
+        }
+        return code.length() - 1;
+    }
+    
+    private static ArrayList<String> splitComma(String code)
+    {
+        ArrayList<String> list = new ArrayList<>();
+        int length = code.length();
+        int pos = 0;
+        int old = 0;
+        int brackets = 0;
+        char c;
+        boolean text = false;
+        while(pos < length)
+        {
+            c = code.charAt(pos);
+            if(text && c != '"')
+            {
+                pos++;
+                continue;
+            }
+            
+            switch(c)
+            {
+                case '"':
+                    text = !text;
+                    break;
+                case '(':
+                    brackets++;
+                    break;
+                case ')':
+                    brackets--;
+                    break;
+                case ',':
+                    if(brackets == 0)
+                    {
+                        list.add(code.substring(old, pos));
+                        old = pos + 1;
+                    }
+                    break;
+            }
+            pos++;
+        }
+        if(old < pos)
+        {
+            list.add(code.substring(old, pos));
+        }
+        return list;
+    }
+    
+    private static void splitFunctions(ArrayList<Code> list, String f, int level, int line)
+    {
+        sublines++;
+        int start = findOpenBracket(f, 0);
+        int end = findClosingBracket(f, start);
+        if(start >= end - 1)
+        {
+            if(start == f.length())
+            {
+                list.add(new Code(level, 0, line, sublines, convertInput(f.substring(0, start))));
+                return;
+            }
+            list.add(new Code(f.substring(0, start), level, 0, line, sublines));
+            return;
+        }
+        ArrayList<String> splitted = splitComma(f.substring(start + 1, end));
+        if(start == f.length())
+        {
+            list.add(new Code(level, 0, line, sublines, convertInput(f.substring(0, start))));
+        }
+        else
+        {
+            list.add(new Code(f.substring(0, start), level, splitted.size(), line, sublines));
+        }
+        splitted.forEach(s -> splitFunctions(list, s, level, line));
+    }
+     
+    private static Object convertInput(String s)
+    {
+        if(s == null)
+        {
+            return null;
+        }
+        s = s.trim();
+        if(s.startsWith("\"") && s.endsWith("\""))
+        {
+            if(s.length() <= 1)
+            {
+                return "\"";
+            }
+            return s.substring(1, s.length() - 1);
+        }
+        else if(s.equals("true"))
+        {
+            return true;
+        }
+        else if(s.equals("false"))
+        {
+            return false;
+        }
+        else if(s.equals("null"))
+        {
+            return null;
+        }
+        try
+        {
+            return new Fraction(Long.parseLong(s));
+        }
+        catch(NumberFormatException ex)
+        {
+            return new Variable(s);
+        }
+    }
+}

+ 16 - 25
src/me/hammerle/scriptsystem/CodeParser.java → src/me/hammerle/code/CodeParser.java

@@ -1,38 +1,30 @@
-package me.hammerle.scriptsystem;
+package me.hammerle.code;
 
 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.IOException;
 import me.hammerle.exceptions.IllegalStringException;
-import java.math.BigDecimal;
-import java.math.RoundingMode;
 import java.time.ZonedDateTime;
-import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Calendar;
-import java.util.Collections;
 import java.util.GregorianCalendar;
 import java.util.List;
-import java.util.stream.Collectors;
 import java.awt.Font;
-import java.util.HashMap;
 import javax.imageio.ImageIO;
 import me.hammerle.exceptions.IllegalColorException;
-import me.hammerle.exceptions.NoChildTreeException;
 import me.hammerle.graphics.*;
 import me.hammerle.snuviscript.SnuviScript;
 
 public class CodeParser 
 {    
     @SuppressWarnings("unchecked")
-    public static Object parseFunction(ScriptData sd, String function, List<Object> args)
+    public static Object parseFunction(Script sd, String function, Object[] args, int jump)
     {
         try
         {
             switch(function.toLowerCase())
             {       
                 // Rechenoperationen
-                case "add":
+                /*case "add":
                     return args.stream().mapToDouble(s -> ScriptUtils.getDouble(s)).sum();
                 case "sub":
                     return ScriptUtils.getDouble(args.get(0)) - ScriptUtils.getDouble(args.get(1));
@@ -183,10 +175,10 @@ public class CodeParser
                 case "debug":
                     System.out.println(args.get(0));
                     break;
-                /*case "loadlib":
+                case "loadlib":
                     sd.loadLibrary(args.get(0)); break;
                 case "lib":
-                    SnuviScript.scriptController.startLibrary(qd, args); return false;*/
+                    SnuviScript.scriptController.startLibrary(qd, args); return false;
                 case "reset":
                     sd.resetOverflowProtection(); break;                   
                 case "wait":
@@ -251,12 +243,11 @@ public class CodeParser
 
                 // Is Valid Checker    
                 case "isvalidnumber":
-                    return generalValidChecker(() -> ScriptUtils.getDouble(args.get(0)));
+                    return generalValidChecker(() -> ScriptUtils.getDouble(args.get(0)));*/
 
                 default:
                     throw new NoSuchMethodException();                      
             }
-            return 0;
         }
         catch(Exception ex)
         {
@@ -266,9 +257,9 @@ public class CodeParser
         }
     }
     
-    public static void printQuestException(ScriptData sd, Exception ex, String line)
+    public static void printQuestException(Script sd, Exception ex, String line)
     {
-        sd.resetOverflowProtection();
+        sd.resetLoopCounter();
         ScriptUtils.printDebug("Error in");
         ScriptUtils.printDebugList("Script: " + sd.getName());
         ScriptUtils.printDebugList("Zeile: " + line);
@@ -306,21 +297,21 @@ public class CodeParser
                 args.get(4).toString(), new Font(args.get(5).toString(), ScriptUtils.getInteger(args.get(6)), ScriptUtils.getInteger(args.get(7)))));
     }
 
-    private static void ifFunction(List<Object> args, ScriptData sd) throws NoChildTreeException
+    private static void ifFunction(List<Object> args, Script sd)
     {
         if(args.stream().allMatch(s -> s.equals(true)))
         {
-            sd.goDeeper();
+            //sd.goDeeper();
         }
     }
                   
-    private static void sortList(List<Object> args, ScriptData sd) 
+    private static void sortList(List<Object> args, Script sd) 
     {     
-        List<Object> list = (List<Object>) sd.getVar(args.get(0));
-        sd.setVar(args.get(0), list.stream().sorted().collect(Collectors.toList()));
+        //List<Object> list = (List<Object>) sd.getVar(args.get(0));
+        //sd.setVar(args.get(0), list.stream().sorted().collect(Collectors.toList()));
     }
                    
-    private static void scheduleGoto(List<Object> args, ScriptData qd)
+    private static void scheduleGoto(List<Object> args, Script qd)
     {
         /*SnuviScheduler.doScheduledTask(() -> 
         {
@@ -340,7 +331,7 @@ public class CodeParser
         }, Integer.parseInt(args.get(1)));*/
     }
 
-    private static String getNextDay(List<Object> args, ScriptData qd)       
+    private static String getNextDay(List<Object> args, Script qd)       
     {
         GregorianCalendar cal = GregorianCalendar.from(ZonedDateTime.now());
         cal.setTimeInMillis(Long.parseLong(args.get(0).toString()));
@@ -352,7 +343,7 @@ public class CodeParser
         return String.valueOf(cal.getTimeInMillis());   
     } 
     
-    private static boolean waitFor(List<Object> args, ScriptData qd) throws UnsupportedOperationException
+    private static boolean waitFor(List<Object> args, Script qd) throws UnsupportedOperationException
     {           
         /*SnuviScript.scriptController.resetOverflowProtection();
         int i = Integer.parseInt(args.get(0));

+ 1 - 1
src/me/hammerle/scriptsystem/GraphicEngine.java → src/me/hammerle/code/GraphicEngine.java

@@ -1,4 +1,4 @@
-package me.hammerle.scriptsystem;
+package me.hammerle.code;
 
 import java.awt.Graphics;
 import java.util.TreeMap;

+ 95 - 0
src/me/hammerle/code/Script.java

@@ -0,0 +1,95 @@
+package me.hammerle.code;
+
+import java.util.HashMap;
+import me.hammerle.exceptions.CodeTooLongException;
+import me.hammerle.exceptions.GotoLabelNotFoundException;
+
+public class Script 
+{
+    private final int id;
+    private final String name;  
+
+    private final HashMap<String, Object> variables;    
+    private final HashMap<String, Integer> gotos;
+    
+    private final Code[] code;
+    private int position;
+
+    private int loopCounter;
+    
+    public Script(int id, String name, String code)
+    {       
+        this.id = id;
+        this.name = name;
+        
+        variables = new HashMap<>();
+        gotos = new HashMap<>();
+        
+        this.code = Code.generate(code);
+        
+        position = 0;
+
+        loopCounter = 0;
+    }
+    
+    // -----------------------------------------------------------------------------------
+    // Script-Daten
+    // -----------------------------------------------------------------------------------
+    
+    public int getId() 
+    {
+        return id;
+    }
+
+    public String getName() 
+    {
+        return name;
+    }
+    
+    // -----------------------------------------------------------------------------------
+    // Variablen
+    // -----------------------------------------------------------------------------------
+    
+    public void setVar(String var, Object value)
+    {
+        variables.put(var, value);   
+    }
+    
+    public Object getVar(String var)
+    {
+        return variables.get(var);
+    }
+    
+    public void removeVar(String var)
+    {
+        variables.remove(var);
+    }
+    
+    // -----------------------------------------------------------------------------------
+    // Goto
+    // -----------------------------------------------------------------------------------
+    
+    public void gotoLabel(String label) throws GotoLabelNotFoundException, CodeTooLongException
+    {
+        try
+        {
+            int i = gotos.get(label);
+            loopCounter++;
+            if(loopCounter > 50)
+            {
+                resetLoopCounter();
+                throw new CodeTooLongException();
+            }
+            position = i;
+        }
+        catch(NullPointerException ex)
+        {
+            throw new GotoLabelNotFoundException(label);
+        }
+    }  
+    
+    public void resetLoopCounter()
+    {
+        loopCounter = 0;
+    }
+}

+ 9 - 9
src/me/hammerle/scriptsystem/ScriptControl.java → src/me/hammerle/code/ScriptControl.java

@@ -1,4 +1,4 @@
-package me.hammerle.scriptsystem;
+package me.hammerle.code;
 
 import java.util.Collection;
 import java.util.HashMap;
@@ -6,7 +6,7 @@ import java.util.HashMap;
 public class ScriptControl
 {
     private int idCounter;
-    private final HashMap<Integer, ScriptData> scripts;
+    private final HashMap<Integer, Script> scripts;
     
     public ScriptControl() 
     {
@@ -18,7 +18,7 @@ public class ScriptControl
     // Script - Verwaltung
     // -------------------------------------------------------------------------
 
-    public ScriptData getScript(int id)
+    public Script getScript(int id)
     {
         return scripts.get(id);
     }
@@ -29,17 +29,17 @@ public class ScriptControl
         scripts.clear();
     }
     
-    public Collection<ScriptData> getScripts()
+    public Collection<Script> getScripts()
     {
         return scripts.values();
     }
     
-    private void add(ScriptData sd)
+    private void add(Script sd)
     {
         scripts.put(sd.getId(), sd);
     }
     
-    public void term(ScriptData sd)
+    public void term(Script sd)
     {
         /*if(sd == null)
         {
@@ -59,11 +59,11 @@ public class ScriptControl
     // Scripts laden und starten
     // -----------------------
   
-    public void startScript(String scriptName)
+    /*public void startScript(String scriptName)
     { 
-        ScriptData sd = new ScriptData(idCounter, scriptName);
+        Script sd = new Script(idCounter, scriptName);
         idCounter++;
         add(sd);
         sd.runCode();
-    }
+    }*/
 }

+ 1 - 1
src/me/hammerle/scriptsystem/ScriptEngine.java → src/me/hammerle/code/ScriptEngine.java

@@ -1,4 +1,4 @@
-package me.hammerle.scriptsystem;
+package me.hammerle.code;
 
 import java.awt.Color;
 import java.awt.event.KeyAdapter;

+ 1 - 1
src/me/hammerle/scriptsystem/ScriptUtils.java → src/me/hammerle/code/ScriptUtils.java

@@ -1,4 +1,4 @@
-package me.hammerle.scriptsystem;
+package me.hammerle.code;
 
 import java.awt.Color;
 import java.util.Random;

+ 22 - 0
src/me/hammerle/code/Variable.java

@@ -0,0 +1,22 @@
+package me.hammerle.code;
+
+public class Variable 
+{
+    private final String name;
+  
+    public Variable(String name)
+    {
+        this.name = name;
+    }
+    
+    public String getName()
+    {
+        return name;
+    }
+
+    @Override
+    public String toString() 
+    {
+        return name;
+    }  
+}

+ 11 - 0
src/me/hammerle/console/ConsoleUtils.java

@@ -0,0 +1,11 @@
+package me.hammerle.console;
+
+import me.hammerle.snuviscript.SnuviScript;
+
+public class ConsoleUtils 
+{
+    public static void sendMessage(String s)
+    {
+        SnuviScript.console.output.pushText(s);
+    }
+}

+ 1 - 0
src/me/hammerle/console/InputBox.java

@@ -22,6 +22,7 @@ public class InputBox extends BlackBox
                     }
                     else
                     {
+                        // HIER WIRD CODE AUSGEFÜHRT
                         c.output.pushText(area.getText());
                         area.setText(null);
                         e.consume();

+ 2 - 2
src/me/hammerle/console/OutputBox.java

@@ -9,11 +9,11 @@ public class OutputBox extends BlackBox
         super(c);
         super.setPreferredSize(new Dimension(660, 345));
         area.setEditable(false);
-        area.setText("--- Hello " + System.getProperty("user.name") + " ---");
+        area.setText("--- Hello " + System.getProperty("user.name") + " ---\n");
     }
     
     public void pushText(String s)
     {
-        area.append("\n> " + s);
+        area.append("> " + s + "\n");
     }
 }

+ 0 - 6
src/me/hammerle/exceptions/GoHigherAtRootTreeException.java

@@ -1,6 +0,0 @@
-package me.hammerle.exceptions;
-
-public class GoHigherAtRootTreeException extends Exception
-{
-    
-}

+ 1 - 1
src/me/hammerle/exceptions/IllegalStringException.java

@@ -1,6 +1,6 @@
 package me.hammerle.exceptions;
 
-public class IllegalStringException extends RuntimeException
+public class IllegalStringException extends Exception
 {
     private final String s;
     

+ 1 - 1
src/me/hammerle/exceptions/InfiniteLoopException.java

@@ -1,6 +1,6 @@
 package me.hammerle.exceptions;
 
-public class InfiniteLoopException extends RuntimeException
+public class InfiniteLoopException extends Exception
 {
     
 }

+ 0 - 6
src/me/hammerle/exceptions/NoChildTreeException.java

@@ -1,6 +0,0 @@
-package me.hammerle.exceptions;
-
-public class NoChildTreeException extends Exception
-{
-    
-}

+ 9 - 0
src/me/hammerle/exceptions/PrescriptException.java

@@ -0,0 +1,9 @@
+package me.hammerle.exceptions;
+
+public class PrescriptException extends IllegalStringException
+{
+    public PrescriptException(String s) 
+    {
+        super(s);
+    }
+}

+ 32 - 0
src/me/hammerle/files/FileUtils.java

@@ -0,0 +1,32 @@
+package me.hammerle.files;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.util.List;
+import me.hammerle.code.ScriptUtils;
+import me.hammerle.console.ConsoleUtils;
+
+public class FileUtils 
+{
+    public static List<String> readCode(String filename)
+    {
+        File script = new File(filename + ".sv");  
+        if(script.exists())
+        {
+            try 
+            {
+                return Files.readAllLines(script.toPath());
+            } 
+            catch (IOException ex) 
+            {
+                ConsoleUtils.sendMessage("File '" + filename + "' cannot be read.");
+            }
+        }
+        else
+        {
+            ScriptUtils.printError("File '" + filename + "' does not exist.");
+        }
+        return null;
+    }
+}

+ 0 - 504
src/me/hammerle/scriptsystem/ScriptData.java

@@ -1,504 +0,0 @@
-package me.hammerle.scriptsystem;
-
-import java.io.File;
-import java.io.IOException;
-import java.nio.file.Files;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.HashMap;
-import java.util.HashSet;
-import me.hammerle.exceptions.CodeTooLongException;
-import me.hammerle.exceptions.GoHigherAtRootTreeException;
-import me.hammerle.exceptions.GotoLabelNotFoundException;
-import me.hammerle.exceptions.NoChildTreeException;
-
-public class ScriptData 
-{
-    private final int id;
-
-    private final Tree<String> code;
-    private String currentCode;
-    private int position;
-
-    private final HashMap<String, Object> variables;    
-    private final HashMap<String, Integer[]> gotos;
-    private final HashSet<String> events;
-    
-    private final String scriptName;  
-    
-    private boolean isWaiting;
-    //private boolean isValid;
-    private int overflowProtection;
-    
-    public ScriptData(int id, String filename)
-    {       
-        this.id = id;
-        
-        code = new Tree<>();
-        position = 0;
-        
-        variables = new HashMap<>();
-        gotos = new HashMap<>();
-        events = new HashSet<>();
-        
-        scriptName = filename;
-        
-        readCode(filename);
-
-        isWaiting = false;
-        //isValid = true;
-        overflowProtection = 0;
-    }
-    
-    public void goDeeper() throws NoChildTreeException
-    {
-        code.goDeeper();
-    }
-    
-    private void readCode(String filename)
-    {
-        File script = new File(filename + ".txt");  
-        List<String> lines;
-        if(script.exists())
-        {
-            try 
-            {
-                lines = Files.readAllLines(script.toPath());
-            } 
-            catch (IOException ex) 
-            {
-                ScriptUtils.printError("Datei '" + filename + "' kann nicht gelesen werden.");
-                return;
-            }
-        }
-        else
-        {
-            ScriptUtils.printError("Datei '" + filename + "' wurde nicht gefunden.");
-            return;
-        }
-        
-        int line = 0;
-        int i = 0;
-        int old = 0;
-        int bracket = 0;
-        boolean bracketEnd = false;
-        int bracketEndPos = 0;
-        boolean oneLineChild = false;
-        boolean comment = false;       
-        int commentPos = 0;
-        StringBuilder codeLine = new StringBuilder();
-        while(line < lines.size())
-        {
-            if(old == codeLine.length() && !comment)
-            {
-                codeLine = new StringBuilder(lines.get(line).trim());
-                i = 0;
-                old = 0;
-            }
-            else
-            {
-                codeLine.append(lines.get(line).trim());
-            }
-            
-            //System.out.println(codeLine);
-            
-            while(i < codeLine.length())
-            {
-                switch(codeLine.charAt(i))
-                {
-                    case '@':
-                        if(comment)
-                        {
-                            break;
-                        }
-                        old = i;
-                        while(i < codeLine.length() && codeLine.charAt(i) != ' ')
-                        {
-                            i++;
-                        }
-                        try
-                        {
-                            code.selectLastChild(); // Nur für richtige Position
-                        }
-                        catch(NoChildTreeException ex)
-                        {
-                        }
-                        gotos.put(codeLine.substring(old + 1, i), code.getCurrentPosition());
-                        codeLine.delete(old, i + 1);
-                        i = old - 1;
-                        break;
-                    case '/':
-                        if(!comment && i + 1 < codeLine.length() && codeLine.charAt(i + 1) == '*')
-                        {
-                            comment = true;
-                            commentPos = i;
-                        }
-                        if(!comment && i + 1 < codeLine.length() && codeLine.charAt(i + 1) == '/')
-                        {
-                            codeLine.delete(commentPos, codeLine.length());
-                        }
-                        break;
-                    case '*':
-                        if(comment && i + 1 < codeLine.length() && codeLine.charAt(i + 1) == '/')
-                        {
-                            comment = false;
-                            codeLine.delete(commentPos, i + 2);
-                            i = commentPos;
-                        }
-                        break;
-                    case ';':
-                        if(comment)
-                        {
-                            break;
-                        }
-                        bracketEnd = false;
-                        code.addChild(codeLine.substring(old, i));
-                        old = i + 1;
-                        if(oneLineChild)
-                        {
-                            oneLineChild = false;
-                            try
-                            {
-                                code.goHigher();
-                            }
-                            catch(GoHigherAtRootTreeException ex)
-                            {
-                                System.out.println("Das sollte nicht passieren");
-                                return;
-                            }
-                        }
-                        break;
-                    case '(':
-                        if(!comment && !oneLineChild)
-                        {
-                            bracket++;
-                            if(bracketEnd)
-                            {
-                                bracketEnd = false;
-                                oneLineChild = true;
-                                code.addChild(codeLine.substring(old, bracketEndPos));
-                                old = bracketEndPos;
-                                try
-                                {
-                                    code.selectLastChild();
-                                    code.goDeeper();
-                                }
-                                catch(NoChildTreeException ex)
-                                {
-                                    System.out.println("Das sollte nicht passieren");
-                                }
-                            }
-                        }
-                        break;
-                    case ')':
-                        if(comment || oneLineChild)
-                        {
-                            break;
-                        }
-                        bracket--;
-                        if(bracket < 0)
-                        {
-                            ScriptUtils.printError(") ohne ( in Zeile " + (line + 1));
-                            return;
-                        }
-                        if(bracket == 0)
-                        {
-                            bracketEnd = true;   
-                            bracketEndPos = i + 1;
-                        }
-                        break;
-                    case '{':
-                        if(comment)
-                        {
-                            break;
-                        }
-                        bracketEnd = false;
-                        code.addChild(codeLine.substring(old, i));
-                        old = i + 1;
-                        try
-                        {
-                            code.selectLastChild();
-                            code.goDeeper();
-                        }
-                        catch(NoChildTreeException ex)
-                        {
-                            System.out.println("Das sollte nicht passieren");
-                        }
-                        break;
-                    case '}':
-                        if(comment)
-                        {
-                            break;
-                        }
-                        old = i + 1;
-                        try
-                        {
-                            code.goHigher();
-                        }
-                        catch(GoHigherAtRootTreeException ex)
-                        {
-                            ScriptUtils.printError("} ohne { in Zeile " + (line + 1));
-                            return;
-                        }
-                        break;
-                }
-                i++;
-            } 
-            line++;
-        }
-        code.goToRoot();
-    }
-    
-    public void runCode()
-    {
-        try
-        {
-            while(!isWaiting)
-            {
-                try
-                {
-                    code.selectNextChild();
-                }
-                catch(NoChildTreeException ex)
-                {
-                    //System.out.println("NACH OBEN!");
-                    code.goHigher();
-                    continue;
-                }
-                currentCode = code.getCurrentChildData();
-                findFunction(0, currentCode.length() - 1);
-            }
-        }
-        catch(NoChildTreeException | GoHigherAtRootTreeException ex)
-        {
-            System.out.println("END PROGRAM");
-        }
-    }
-    
-    private Object findFunction(int from, int to)
-    {
-        if(to <= from) // Springt in { } ohne Kopf
-        {
-            try
-            {
-                code.goDeeper();
-            }
-            catch(NoChildTreeException ex)
-            {
-                System.out.println("SOLLTE NICHT SEIN");
-            }
-            return null;
-        }
-        int old = from;
-        int bracket;
-        ArrayList<Object> list = new ArrayList<>();
-        while(currentCode.charAt(from) != '(')
-        {
-            from++;
-        }
-        bracket = from;
-        from++;
-        
-        int last = from;
-        int counter = 0;
-        boolean string = false;
-        while(from <= to)
-        {
-            if(string)
-            {              
-                if(currentCode.charAt(from) == '"')
-                {
-                    string = !string;
-                }
-                from++;
-                continue;
-            }
-            switch(currentCode.charAt(from))
-            {
-                case '"':
-                    string = !string;
-                    break;
-                case ',':
-                    if(last != from)
-                    {
-                        list.add(currentCode.substring(last, from).trim());
-                    }
-                    last = from + 1;
-                    break;
-                case ')':
-                    if(counter > 0)
-                    {
-                        last = from + 1;
-                        counter--;
-                        break;
-                    }
-                    position = from;
-                    if(last != from)
-                    {
-                        list.add(currentCode.substring(last, from).trim());
-                    }
-                    return doFunction(currentCode.substring(old, bracket).trim(), list);
-                case '(':
-                    list.add(findFunction(last, to));  
-                    from = position - 1;
-                    last = from;
-                    counter++;
-                    break;
-            }  
-            from++;
-        }         
-        return null;
-    }
-    
-    private Object doFunction(String function, List<Object> args)
-    {
-        for(int i = 0; i < args.size(); i++)
-        {
-            args.set(i, readParameter(args.get(i)));
-        }
-        Object o = CodeParser.parseFunction(this, function, args);
-        if(o == null)
-        {
-            isWaiting = true;
-        }
-        return o;
-    }
-    
-    private Object readParameter(Object o)
-    {
-        if(!(o instanceof String))
-        {
-            return o;
-        }
-        String s = o.toString();
-        if(s.startsWith("\"") && s.endsWith("\""))
-        {
-            return s.substring(1, s.length() - 1);
-        }
-        else if(s.startsWith("$"))
-        {
-            return getVar(s);
-        }
-        try
-        {
-            return ScriptUtils.getNumber(s);
-        }
-        catch(NumberFormatException ex)
-        {
-            return s;
-        }
-    }
-    
-    public int getId() 
-    {
-        return id;
-    }
-
-    public String getName() 
-    {
-        return scriptName;
-    }
-    
-    /*public boolean isWaiting()
-    {
-        return isWaiting;
-    }
-    
-    public void setWaiting(Boolean bool) 
-    {
-        isWaiting = bool;
-    }
-    
-    public boolean isValid() 
-    {
-        return isValid;
-    }
-    
-    public void setValid(Boolean bool) 
-    {
-        isValid = bool;
-    }*/
-    
-    public Object getVar(String var)
-    {
-        if(var.startsWith("$"))
-        {
-            return variables.get(var);
-        }
-        return variables.get("$" + var);
-    }
-    
-    public Object getVar(Object var)
-    {
-        return getVar(var.toString());
-    }
-    
-    public HashMap<String, Object> getVars()
-    {
-        return variables;
-    }
-    
-    public void removeVar(String var)
-    {
-        if(var.startsWith("$"))
-        {
-            variables.remove(var);
-        }
-        variables.remove("$" + var);
-    }
-    
-    public void removeVar(Object var)
-    {
-        removeVar(var.toString());
-    }
-    
-    public void setVar(String var, Object value)
-    {
-        if(!var.startsWith("$"))
-        {
-            var = "$" + var;
-        }
-        variables.put(var, value);   
-    }
-    
-    public void setVar(Object var, Object value)
-    {
-        setVar(var.toString(), value);
-    }
-    
-    public void gotoLabel(String label) throws NoChildTreeException, CodeTooLongException
-    {
-        Integer[] i = gotos.get(label);
-        if(i == null)
-        {
-            throw new GotoLabelNotFoundException(label);
-        }
-        overflowProtection++;
-        if(overflowProtection > 50)
-        {
-            overflowProtection = 0;
-            throw new CodeTooLongException();
-        }
-        code.goToPosition(i);
-    }  
-    
-    public void resetOverflowProtection()
-    {
-        overflowProtection = 0;
-    }
-    
-    public void loadEvent(Object event)
-    {
-        events.add(event.toString());
-    }
-    
-    public void unloadEvent(Object event)
-    {
-        events.remove(event.toString());
-    }
-    
-    public boolean isEventLoaded(Object event)
-    {
-        return events.contains(event.toString());
-    }
-}

+ 0 - 154
src/me/hammerle/scriptsystem/Tree.java

@@ -1,154 +0,0 @@
-package me.hammerle.scriptsystem;
-
-import java.util.ArrayList;
-import java.util.Stack;
-import me.hammerle.exceptions.GoHigherAtRootTreeException;
-import me.hammerle.exceptions.NoChildTreeException;
-
-public class Tree<T> 
-{
-    private final Node<T> root;
-    private Node<T> currentNode;
-    private final Stack<Integer> position;
-
-    public Tree() 
-    {
-        root = new Node<>();
-        root.parent = null;
-        root.data = null;
-        root.children = new ArrayList<>();
-        root.selectedChild = -1;
-        
-        currentNode = root;
-        
-        position = new Stack<>();
-    }
-
-    private static class Node<T> 
-    {
-        private T data;
-        private Node<T> parent;
-        private ArrayList<Node<T>> children;
-        private int selectedChild;
-    }
-    
-    public void addChild(T child)
-    {
-        Node<T> node = new Node<>();
-        node.data = child;
-        node.parent = currentNode;
-        node.children = new ArrayList<>();    
-        node.selectedChild = -1;
-        currentNode.children.add(node);
-    }
-    
-    public void goHigher() throws GoHigherAtRootTreeException
-    {
-        currentNode.selectedChild = 0;
-        currentNode = currentNode.parent;
-        if(currentNode == null)
-        {
-            currentNode = root;
-            throw new GoHigherAtRootTreeException();
-        }
-        position.pop();
-    }
-    
-    public void goDeeper() throws NoChildTreeException
-    {
-        try
-        {   
-            int i = currentNode.selectedChild;
-            currentNode = currentNode.children.get(i);
-            position.add(i);
-            currentNode.selectedChild = -1;
-        }
-        catch(IndexOutOfBoundsException ex)
-        {
-            throw new NoChildTreeException();
-        }
-    }
-    
-    public void selectChild(int index) throws NoChildTreeException
-    {
-        if(index >= currentNode.children.size())
-        {
-            throw new NoChildTreeException();
-        }
-        currentNode.selectedChild = index;
-    }
-    
-    public void selectNextChild() throws NoChildTreeException
-    {
-        currentNode.selectedChild++;
-        if(currentNode.selectedChild >= currentNode.children.size())
-        {
-            currentNode.selectedChild--;
-            throw new NoChildTreeException();
-        }
-    }
-    
-    public void selectLastChild() throws NoChildTreeException
-    {
-        currentNode.selectedChild = currentNode.children.size() - 1;
-        if(currentNode.selectedChild == -1)
-        {
-            throw new NoChildTreeException();
-        }
-    }
-    
-    public T getCurrentData()
-    {
-        return currentNode.data;
-    }
-    
-    public T getCurrentChildData() throws NoChildTreeException
-    {
-        try
-        {   
-            return currentNode.children.get(currentNode.selectedChild).data;
-        }
-        catch(IndexOutOfBoundsException ex)
-        {
-            throw new NoChildTreeException();
-        }
-    }
-    
-    public void goToRoot()
-    {
-        try
-        {
-            while(true)
-            {
-                goHigher();
-            }
-        }
-        catch(GoHigherAtRootTreeException ex)
-        {
-            currentNode.selectedChild = -1;
-        }     
-    }
-    
-    public void goToPosition(Integer... i) throws NoChildTreeException
-    {
-        goToRoot();
-        for(int a = 0; a < i.length - 1; a++)
-        {
-            selectChild(i[a]);
-            goDeeper();
-        }
-        selectChild(i[i.length - 1]);
-    }
-    
-    public boolean isRootNodeSelected()
-    {
-        return currentNode.data == null;
-    }
-    
-    public Integer[] getCurrentPosition()
-    {
-        Integer[] i = position.toArray(new Integer[position.size() + 1]);
-        i[position.size()] = currentNode.selectedChild;
-        return i;
-    }
-}

+ 20 - 3
src/me/hammerle/snuviscript/SnuviScript.java

@@ -1,21 +1,38 @@
 package me.hammerle.snuviscript;
 
+import me.hammerle.code.Script;
 import me.hammerle.console.Console;
-import me.hammerle.scriptsystem.ScriptEngine;
-import me.hammerle.scriptsystem.ScriptControl;
+import me.hammerle.code.ScriptEngine;
+import me.hammerle.code.ScriptControl;
 
 public class SnuviScript 
 {
     public static ScriptEngine screen;
     public static ScriptControl scriptController;
     
+    public static Console console;
+    
     public static void main(String[] args) 
     {
-        Console console = new Console();
+        //console = new Console();
         
         /*for(String s : GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames())
         {
             System.out.println(s);
         }*/
+        
+        String s = "if(equal(list.getIndex(args, 1), \"remove\")) \n{\n" +
+"	setVar(\"mailid\", list.getIndex(args, 2));\n" +
+"	try() \n	{\n" +
+"		add(mailid, 1); \n" +
+"	} \n	catch() \n	{\n" +
+"		player.speak(player, \"§3Mail\", \"Zahl erwartet: %mail remove <mailid>\");\n" +
+"		goto(\"wait\");\n" +
+"	}\n" +
+"	gmap.remove(concat(\"mailin-\", player.getUuid(player)), mailid);\n" +
+"	gmap.remove(concat(\"mailout-\", player.getUuid(player)), mailid);\n" +
+"	goto(\"wait\");\n}";
+        System.out.println(s);
+        Script test = new Script(1, "Test", s);
     }   
 }