Browse Source

functions do not push their return value if not needed, if state has
become a stack to fix function calls with if in an if

Kajetan Johannes Hammerle 5 years ago
parent
commit
9a600301db

+ 9 - 5
src/me/hammerle/snuviscript/code/Script.java

@@ -35,7 +35,7 @@ public final class Script
     private final Stack<HashMap<String, Variable>> localVars = new Stack<>();   
     private final HashMap<String, Integer> functions = new HashMap<>();
     
-    private boolean ifState = true;
+    private Stack<Boolean> ifState = new Stack<>();
     private Stack<Integer> stackElements = new Stack<>();
     private int errorLine = -1;
     private Stack<String> inFunction = new Stack<>();
@@ -58,6 +58,7 @@ public final class Script
     
     public Script(ScriptManager sm, Consumer<Script> onStart, Consumer<Script> onTerm, String name, String... path)
     {
+        ifState.push(true);
         this.id = idCounter++;
         this.name = name;
         this.sm = sm;
@@ -250,12 +251,13 @@ public final class Script
     
     public void setIfState(boolean state)
     {
-        ifState = state;
+        ifState.pop();
+        ifState.push(state);
     }
     
     public boolean getIfState()
     {
-        return ifState;
+        return ifState.peek();
     }
     
     public void setErrorLine(int line)
@@ -290,6 +292,7 @@ public final class Script
             lvars.put(args[i], v);
         }
         
+        ifState.push(true);
         localVars.push(lvars);
         returnStack.push(lineIndex);
         lineIndex = sub;
@@ -299,16 +302,17 @@ public final class Script
     
     public void handleReturn(ReturnWrapper wrapper)
     {
+        lineIndex = returnStack.pop();
         if(returnVarPop.pop())
         {
+            ifState.pop();
             inFunction.pop();
             localVars.pop();
-            if(wrapper != null)
+            if(wrapper != null && !code[lineIndex].shouldNotReturnValue())
             {
                 dataStack.add(wrapper);
             }
         }
-        lineIndex = returnStack.pop();
     }
     
     public Variable getOrAddLocalVariable(String name)

+ 5 - 1
src/me/hammerle/snuviscript/code/ScriptManager.java

@@ -115,7 +115,11 @@ public class ScriptManager
         {
             return false;
         }
-        scripts.values().forEach(sc -> sc.onTerm());
+        scripts.values().forEach(sc -> 
+        {
+            sc.term();
+            sc.onTerm();
+        });
         scripts.clear();
         loadedEvents.values().forEach(list -> list.clear());
         return true;

+ 8 - 0
test/functions/functions17

@@ -0,0 +1,8 @@
+print("hmm");
+
+function getWusi()
+{
+    return 1;
+}
+
+getwusi();

+ 7 - 0
test/functions/functions17.cout

@@ -0,0 +1,7 @@
+push "hmm"
+use print(1)
+getwusi(5)
+push 1
+return(1)
+return(0)
+use getwusi(0)

+ 1 - 0
test/functions/functions17.out

@@ -0,0 +1 @@
+hmm

+ 19 - 0
test/functions/functions17.tout

@@ -0,0 +1,19 @@
+(1, LITERAL, "print")
+(1, OPEN_BRACKET)
+(1, STRING, "hmm")
+(1, CLOSE_BRACKET)
+(1, SEMICOLON)
+(3, FUNCTION)
+(3, LITERAL, "getWusi")
+(3, OPEN_BRACKET)
+(3, CLOSE_BRACKET)
+(4, OPEN_CURVED_BRACKET)
+(5, RETURN)
+(5, NUMBER, 1.0)
+(5, SEMICOLON)
+(6, CLOSE_CURVED_BRACKET)
+(8, LITERAL, "getwusi")
+(8, OPEN_BRACKET)
+(8, CLOSE_BRACKET)
+(8, SEMICOLON)
+(9, EOF)

+ 30 - 0
test/if_elseif_else/if13

@@ -0,0 +1,30 @@
+if(getFalse())
+{
+    print("a");
+}
+elseif(getTrue())
+{
+    print("b");
+}
+
+if(getTrue())
+{
+    print("c");
+}
+elseif(getTrue())
+{
+    print("d");
+}
+
+function getFalse()
+{
+    return false;
+}
+
+function getTrue()
+{
+    if(3 > 3)
+    {
+    }
+    return true;
+}

+ 31 - 0
test/if_elseif_else/if13.cout

@@ -0,0 +1,31 @@
+use getfalse(0)
+if(3)
+push "a"
+use print(1)
+use gettrue(0)
+elseif(7)
+push "b"
+use print(1)
+endif
+use gettrue(0)
+if(12)
+push "c"
+use print(1)
+use gettrue(0)
+elseif(16)
+push "d"
+use print(1)
+endif
+getfalse(21)
+push false
+return(1)
+return(0)
+gettrue(30)
+push 3
+push 3
+use >(2)
+if(26)
+endif
+push true
+return(1)
+return(0)

+ 2 - 0
test/if_elseif_else/if13.out

@@ -0,0 +1,2 @@
+b
+c

+ 79 - 0
test/if_elseif_else/if13.tout

@@ -0,0 +1,79 @@
+(1, IF)
+(1, OPEN_BRACKET)
+(1, LITERAL, "getFalse")
+(1, OPEN_BRACKET)
+(1, CLOSE_BRACKET)
+(1, CLOSE_BRACKET)
+(2, OPEN_CURVED_BRACKET)
+(3, LITERAL, "print")
+(3, OPEN_BRACKET)
+(3, STRING, "a")
+(3, CLOSE_BRACKET)
+(3, SEMICOLON)
+(4, CLOSE_CURVED_BRACKET)
+(5, ELSEIF)
+(5, OPEN_BRACKET)
+(5, LITERAL, "getTrue")
+(5, OPEN_BRACKET)
+(5, CLOSE_BRACKET)
+(5, CLOSE_BRACKET)
+(6, OPEN_CURVED_BRACKET)
+(7, LITERAL, "print")
+(7, OPEN_BRACKET)
+(7, STRING, "b")
+(7, CLOSE_BRACKET)
+(7, SEMICOLON)
+(8, CLOSE_CURVED_BRACKET)
+(10, IF)
+(10, OPEN_BRACKET)
+(10, LITERAL, "getTrue")
+(10, OPEN_BRACKET)
+(10, CLOSE_BRACKET)
+(10, CLOSE_BRACKET)
+(11, OPEN_CURVED_BRACKET)
+(12, LITERAL, "print")
+(12, OPEN_BRACKET)
+(12, STRING, "c")
+(12, CLOSE_BRACKET)
+(12, SEMICOLON)
+(13, CLOSE_CURVED_BRACKET)
+(14, ELSEIF)
+(14, OPEN_BRACKET)
+(14, LITERAL, "getTrue")
+(14, OPEN_BRACKET)
+(14, CLOSE_BRACKET)
+(14, CLOSE_BRACKET)
+(15, OPEN_CURVED_BRACKET)
+(16, LITERAL, "print")
+(16, OPEN_BRACKET)
+(16, STRING, "d")
+(16, CLOSE_BRACKET)
+(16, SEMICOLON)
+(17, CLOSE_CURVED_BRACKET)
+(19, FUNCTION)
+(19, LITERAL, "getFalse")
+(19, OPEN_BRACKET)
+(19, CLOSE_BRACKET)
+(20, OPEN_CURVED_BRACKET)
+(21, RETURN)
+(21, FALSE)
+(21, SEMICOLON)
+(22, CLOSE_CURVED_BRACKET)
+(24, FUNCTION)
+(24, LITERAL, "getTrue")
+(24, OPEN_BRACKET)
+(24, CLOSE_BRACKET)
+(25, OPEN_CURVED_BRACKET)
+(26, IF)
+(26, OPEN_BRACKET)
+(26, NUMBER, 3.0)
+(26, GREATER)
+(26, NUMBER, 3.0)
+(26, CLOSE_BRACKET)
+(27, OPEN_CURVED_BRACKET)
+(28, CLOSE_CURVED_BRACKET)
+(29, RETURN)
+(29, TRUE)
+(29, SEMICOLON)
+(30, CLOSE_CURVED_BRACKET)
+(31, EOF)

+ 1 - 7
test/test.test

@@ -1,7 +1 @@
-print(wusi());
-
-
-function wusi()
-{
-    return null;
-}
+print("WUSI");