Browse Source

removed try / catch

Kajetan Johannes Hammerle 3 years ago
parent
commit
3afe99343f

+ 0 - 23
src/me/hammerle/snuviscript/code/Compiler.java

@@ -16,7 +16,6 @@ import me.hammerle.snuviscript.inputprovider.LocalVariable;
 import me.hammerle.snuviscript.inputprovider.Variable;
 import me.hammerle.snuviscript.instructions.Array;
 import me.hammerle.snuviscript.instructions.Break;
-import me.hammerle.snuviscript.instructions.Catch;
 import me.hammerle.snuviscript.instructions.Constant;
 import me.hammerle.snuviscript.instructions.Continue;
 import me.hammerle.snuviscript.instructions.Else;
@@ -29,7 +28,6 @@ import me.hammerle.snuviscript.instructions.If;
 import me.hammerle.snuviscript.instructions.IfGoto;
 import me.hammerle.snuviscript.instructions.Instruction;
 import me.hammerle.snuviscript.instructions.Return;
-import me.hammerle.snuviscript.instructions.Try;
 import me.hammerle.snuviscript.instructions.UserFunction;
 import me.hammerle.snuviscript.instructions.While;
 
@@ -180,9 +178,6 @@ public class Compiler {
             case WHILE:
                 handleWhile();
                 break;
-            case TRY:
-                handleTry();
-                break;
             default:
                 index = oldIndex;
                 lineExpression = false;
@@ -370,24 +365,6 @@ public class Compiler {
         setBreakContinueJumps(whileStart, whileEnd);
     }
 
-    private void handleTry() {
-        Try t = new Try(previous().getLine());
-        instr.add(t);
-        consume(OPEN_CURVED_BRACKET);
-        while(!match(true, CLOSE_CURVED_BRACKET)) {
-            line();
-        }
-        consume(CATCH);
-        Catch c = new Catch(previous().getLine());
-        instr.add(c);
-        t.setJump(instr.size() - 1);
-        consume(OPEN_CURVED_BRACKET);
-        while(!match(true, CLOSE_CURVED_BRACKET)) {
-            line();
-        }
-        c.setJump(instr.size() - 1);
-    }
-
     private void expression() {
         if(isAtEnd()) {
             throw new PreScriptException(String.format("expected expression got %s", peek().getType()), peek().getLine());

+ 2 - 22
src/me/hammerle/snuviscript/code/Script.java

@@ -36,8 +36,6 @@ public final class Script {
     private final HashMap<String, Integer> functions = new HashMap<>();
 
     private Stack<Boolean> ifState = new Stack<>();
-    private Stack<Integer> stackElements = new Stack<>();
-    private int errorLine = -1;
     private Stack<String> inFunction = new Stack<>();
     private Stack<Boolean> returnVarPop = new Stack<>();
 
@@ -86,9 +84,8 @@ public final class Script {
         //System.out.println("_________________________");
         long endTime = System.nanoTime() + 15_000_000;
         while(lineIndex < code.length && !isWaiting && !isHolded) {
-            Instruction instr;
+            Instruction instr = code[lineIndex];
             try {
-                instr = code[lineIndex];
                 //System.out.println("EXECUTE: " + instr + " " + dataStack);
                 if(instr.getArguments() > 0) {
                     InputProvider[] args = InputProviderArrayPool.get(instr.getArguments());
@@ -105,17 +102,7 @@ public final class Script {
                 if(stackTrace) {
                     ex.printStackTrace();
                 }
-                if(errorLine != -1) {
-                    int elements = stackElements.pop();
-                    while(dataStack.size() > elements) {
-                        dataStack.pop();
-                    }
-
-                    lineIndex = errorLine + 1;
-                    errorLine = -1;
-                    continue;
-                }
-                scriptManager.getLogger().print(null, ex, code[lineIndex].getName(), name, this, new StackTrace(code[lineIndex].getLine(), returnStack, code));
+                scriptManager.getLogger().print(null, ex, instr.getName(), name, this, new StackTrace(instr.getLine(), returnStack, code));
                 break;
             }
 
@@ -188,13 +175,6 @@ public final class Script {
         return ifState.peek();
     }
 
-    public void setErrorLine(int line) {
-        errorLine = line;
-        if(line != -1) {
-            stackElements.push(dataStack.size());
-        }
-    }
-
     public void handleFunction(String function, InputProvider[] in) throws Exception {
         Integer sub = functions.get(function);
         if(sub == null) {

+ 0 - 22
src/me/hammerle/snuviscript/instructions/Catch.java

@@ -1,22 +0,0 @@
-package me.hammerle.snuviscript.instructions;
-
-import me.hammerle.snuviscript.inputprovider.InputProvider;
-import me.hammerle.snuviscript.code.Script;
-
-public class Catch extends Goto {
-    public Catch(int line) {
-        super(line, 0);
-    }
-
-    @Override
-    public InputProvider execute(Script sc, InputProvider[] o) throws Exception {
-        sc.jumpTo(getJump());
-        sc.setErrorLine(-1);
-        return null;
-    }
-
-    @Override
-    public String getName() {
-        return "catch";
-    }
-}

+ 0 - 21
src/me/hammerle/snuviscript/instructions/Try.java

@@ -1,21 +0,0 @@
-package me.hammerle.snuviscript.instructions;
-
-import me.hammerle.snuviscript.inputprovider.InputProvider;
-import me.hammerle.snuviscript.code.Script;
-
-public class Try extends Goto {
-    public Try(int line) {
-        super(line, 0);
-    }
-
-    @Override
-    public InputProvider execute(Script sc, InputProvider[] o) throws Exception {
-        sc.setErrorLine(getJump());
-        return null;
-    }
-
-    @Override
-    public String getName() {
-        return "try";
-    }
-}

+ 5 - 5
src/me/hammerle/snuviscript/test/Test.java

@@ -24,10 +24,10 @@ public class Test {
         testCompiler();
         testOutput();
         
-        LOGGER.reset();
-        PARSER.startScript("test", "./test/test.test");
-        SCHEDULER.execute();
-        LOGGER.printAll();
+        //LOGGER.reset();
+        //PARSER.startScript("test", "./test/test.test");
+        //SCHEDULER.execute();
+        //LOGGER.printAll();
     }
 
     private static void testOutput() {
@@ -38,7 +38,7 @@ public class Test {
 
             LOGGER.reset();
 
-            Script sc = new Script(PARSER, null, null, inFile.getName(), inFile.getPath());
+            Script sc = new Script(PARSER, null, inFile.getName(), inFile.getPath());
             sc.run();
 
             if(LOGGER.check(checkFile)) {

+ 2 - 2
src/me/hammerle/snuviscript/tokenizer/TokenType.java

@@ -16,8 +16,8 @@ public enum TokenType {
     LESS("<"), LESS_EQUAL("<="), GREATER(">"), GREATER_EQUAL(">="), EQUAL("=="), NOT_EQUAL("!="),
     BIT_AND("&"), BIT_XOR("^"), BIT_OR("|"),
     AND("&&"), OR("||"), SET("="),
-    IF("if"), ELSE("else"), ELSEIF("else if"), WHILE("while"), TRY("try"),
-    CATCH("catch"), FOR("for"), FUNCTION("function"), BREAK("break"),
+    IF("if"), ELSE("else"), ELSEIF("else if"), WHILE("while"),
+    FOR("for"), FUNCTION("function"), BREAK("break"),
     CONTINUE("continue"), RETURN("return"),
     EOF("end of file");
 

+ 0 - 6
src/me/hammerle/snuviscript/tokenizer/Tokenizer.java

@@ -106,12 +106,6 @@ public class Tokenizer {
             case "while":
                 add(WHILE);
                 break;
-            case "try":
-                add(TRY);
-                break;
-            case "catch":
-                add(CATCH);
-                break;
             case "for":
                 add(FOR);
                 break;

+ 0 - 18
test/try_catch/double_try_catch

@@ -1,18 +0,0 @@
-print(1);
-try 
-{
-	var = 1 + 3;
-}
-catch
-{
-    print(2);
-}
-
-try 
-{
-	var = "x" + 3;
-	print(3);
-} catch 
-{
-	print(4);
-} 

+ 0 - 22
test/try_catch/double_try_catch.cout

@@ -1,22 +0,0 @@
-push 1
-use print(1)
-try(8)
-push var
-push 1
-push 3
-use +(2)
-use =(2)
-catch(10)
-push 2
-use print(1)
-try(19)
-push var
-push "x"
-push 3
-use +(2)
-use =(2)
-push 3
-use print(1)
-catch(21)
-push 4
-use print(1)

+ 0 - 2
test/try_catch/double_try_catch.out

@@ -1,2 +0,0 @@
-1
-4

+ 0 - 45
test/try_catch/double_try_catch.tout

@@ -1,45 +0,0 @@
-(1, LITERAL, "print")
-(1, OPEN_BRACKET)
-(1, NUMBER, 1.0)
-(1, CLOSE_BRACKET)
-(1, SEMICOLON)
-(2, TRY)
-(3, OPEN_CURVED_BRACKET)
-(4, LITERAL, "var")
-(4, SET)
-(4, NUMBER, 1.0)
-(4, ADD)
-(4, NUMBER, 3.0)
-(4, SEMICOLON)
-(5, CLOSE_CURVED_BRACKET)
-(6, CATCH)
-(7, OPEN_CURVED_BRACKET)
-(8, LITERAL, "print")
-(8, OPEN_BRACKET)
-(8, NUMBER, 2.0)
-(8, CLOSE_BRACKET)
-(8, SEMICOLON)
-(9, CLOSE_CURVED_BRACKET)
-(11, TRY)
-(12, OPEN_CURVED_BRACKET)
-(13, LITERAL, "var")
-(13, SET)
-(13, STRING, "x")
-(13, ADD)
-(13, NUMBER, 3.0)
-(13, SEMICOLON)
-(14, LITERAL, "print")
-(14, OPEN_BRACKET)
-(14, NUMBER, 3.0)
-(14, CLOSE_BRACKET)
-(14, SEMICOLON)
-(15, CLOSE_CURVED_BRACKET)
-(15, CATCH)
-(16, OPEN_CURVED_BRACKET)
-(17, LITERAL, "print")
-(17, OPEN_BRACKET)
-(17, NUMBER, 4.0)
-(17, CLOSE_BRACKET)
-(17, SEMICOLON)
-(18, CLOSE_CURVED_BRACKET)
-(19, EOF)

+ 0 - 14
test/try_catch/function2_try_catch

@@ -1,14 +0,0 @@
-print(1 + wusi());
-
-function wusi()
-{
-    try 
-    {
-	    var = "a" + 3;
-	    return 1;
-    }
-    catch
-    {
-        return 2;
-    }
-}

+ 0 - 17
test/try_catch/function2_try_catch.cout

@@ -1,17 +0,0 @@
-push 1
-use wusi(0)
-use +(2)
-use print(1)
-wusi(16)
-try(13)
-push var#L
-push "a"
-push 3
-use +(2)
-use =(2)
-push 1
-return(1)
-catch(15)
-push 2
-return(1)
-return(0)

+ 0 - 1
test/try_catch/function2_try_catch.out

@@ -1 +0,0 @@
-3.0

+ 0 - 34
test/try_catch/function2_try_catch.tout

@@ -1,34 +0,0 @@
-(1, LITERAL, "print")
-(1, OPEN_BRACKET)
-(1, NUMBER, 1.0)
-(1, ADD)
-(1, LITERAL, "wusi")
-(1, OPEN_BRACKET)
-(1, CLOSE_BRACKET)
-(1, CLOSE_BRACKET)
-(1, SEMICOLON)
-(3, FUNCTION)
-(3, LITERAL, "wusi")
-(3, OPEN_BRACKET)
-(3, CLOSE_BRACKET)
-(4, OPEN_CURVED_BRACKET)
-(5, TRY)
-(6, OPEN_CURVED_BRACKET)
-(7, LITERAL, "var")
-(7, SET)
-(7, STRING, "a")
-(7, ADD)
-(7, NUMBER, 3.0)
-(7, SEMICOLON)
-(8, RETURN)
-(8, NUMBER, 1.0)
-(8, SEMICOLON)
-(9, CLOSE_CURVED_BRACKET)
-(10, CATCH)
-(11, OPEN_CURVED_BRACKET)
-(12, RETURN)
-(12, NUMBER, 2.0)
-(12, SEMICOLON)
-(13, CLOSE_CURVED_BRACKET)
-(14, CLOSE_CURVED_BRACKET)
-(15, EOF)

+ 0 - 14
test/try_catch/function_try_catch

@@ -1,14 +0,0 @@
-print(1 + wusi());
-
-function wusi()
-{
-    try 
-    {
-	    var = 1 + 3;
-	    return 1;
-    }
-    catch
-    {
-        return 2;
-    }
-}

+ 0 - 17
test/try_catch/function_try_catch.cout

@@ -1,17 +0,0 @@
-push 1
-use wusi(0)
-use +(2)
-use print(1)
-wusi(16)
-try(13)
-push var#L
-push 1
-push 3
-use +(2)
-use =(2)
-push 1
-return(1)
-catch(15)
-push 2
-return(1)
-return(0)

+ 0 - 1
test/try_catch/function_try_catch.out

@@ -1 +0,0 @@
-2.0

+ 0 - 34
test/try_catch/function_try_catch.tout

@@ -1,34 +0,0 @@
-(1, LITERAL, "print")
-(1, OPEN_BRACKET)
-(1, NUMBER, 1.0)
-(1, ADD)
-(1, LITERAL, "wusi")
-(1, OPEN_BRACKET)
-(1, CLOSE_BRACKET)
-(1, CLOSE_BRACKET)
-(1, SEMICOLON)
-(3, FUNCTION)
-(3, LITERAL, "wusi")
-(3, OPEN_BRACKET)
-(3, CLOSE_BRACKET)
-(4, OPEN_CURVED_BRACKET)
-(5, TRY)
-(6, OPEN_CURVED_BRACKET)
-(7, LITERAL, "var")
-(7, SET)
-(7, NUMBER, 1.0)
-(7, ADD)
-(7, NUMBER, 3.0)
-(7, SEMICOLON)
-(8, RETURN)
-(8, NUMBER, 1.0)
-(8, SEMICOLON)
-(9, CLOSE_CURVED_BRACKET)
-(10, CATCH)
-(11, OPEN_CURVED_BRACKET)
-(12, RETURN)
-(12, NUMBER, 2.0)
-(12, SEMICOLON)
-(13, CLOSE_CURVED_BRACKET)
-(14, CLOSE_CURVED_BRACKET)
-(15, EOF)