Browse Source

fix: Return statements can return nothing

Matthias Ladkau 2 years ago
parent
commit
c54bd16647
2 changed files with 39 additions and 2 deletions
  1. 6 2
      interpreter/rt_func.go
  2. 33 0
      interpreter/rt_func_test.go

+ 6 - 2
interpreter/rt_func.go

@@ -51,13 +51,17 @@ func (rt *returnRuntime) Eval(vs parser.Scope, is map[string]interface{}, tid ui
 	if err == nil {
 		var res interface{}
 
-		if res, err = rt.node.Children[0].Runtime.Eval(vs, is, tid); err == nil {
+		if len(rt.node.Children) > 0 {
+			res, err = rt.node.Children[0].Runtime.Eval(vs, is, tid)
+		} else {
+			res = nil
+		}
+		if err == nil {
 			rerr := rt.erp.NewRuntimeError(util.ErrReturn, fmt.Sprintf("Return value: %v", res), rt.node)
 			err = &returnValue{
 				rerr.(*util.RuntimeError),
 				res,
 			}
-
 		}
 	}
 

+ 33 - 0
interpreter/rt_func_test.go

@@ -180,6 +180,39 @@ statements
 	}
 }
 
+func TestEmptyReturn(t *testing.T) {
+	vs := scope.NewScope(scope.GlobalScope)
+
+	res, err := UnitTestEvalAndAST(`
+func myfunc() {
+  return
+  return 1
+}
+result := myfunc()
+`, vs, `
+statements
+  function
+    identifier: myfunc
+    params
+    statements
+      return
+      return
+        number: 1
+  :=
+    identifier: result
+    identifier: myfunc
+      funccall
+`[1:])
+
+	if vsRes := vs.String(); err != nil || res != nil || vsRes != `GlobalScope {
+    myfunc (*interpreter.function) : ecal.function: myfunc (Line 2, Pos 1)
+    result (<nil>) : null
+}` {
+		t.Error("Unexpected result: ", vsRes, res, err)
+		return
+	}
+}
+
 func TestFunctionScoping(t *testing.T) {
 
 	vs := scope.NewScope(scope.GlobalScope)