prepare for later
authorAnotherTest <ali.mpfard@gmail.com>
Sun, 5 Jan 2020 15:08:15 +0000 (18:38 +0330)
committerAnotherTest <ali.mpfard@gmail.com>
Tue, 7 Jan 2020 20:35:33 +0000 (00:05 +0330)
jq/interp.jq
jq/step2_eval.jq

dissimilarity index 69%
index 5164280..0fa2f3a 100644 (file)
@@ -1,24 +1,26 @@
-include "utils";
-
-def arg_check(args):
-    if .inputs != (args|length) then
-        jqmal_error("Invalid number of arguments (expected \(.inputs) got \(args|length): \(args))")
-    else
-        .
-    end;
-
-
-def interpret(arguments; env):
-    arg_check(arguments) | (
-        select(.function == "number_add") |
-        arguments | map(.value) | .[0] + .[1] | wrap("number")
-    ) // (
-        select(.function == "number_sub") |
-        arguments | map(.value) | .[0] - .[1] | wrap("number")
-    ) // (
-        select(.function == "number_mul") |
-        arguments | map(.value) | .[0] * .[1] | wrap("number")
-    ) // (
-        select(.function == "number_div") |
-        arguments | map(.value) | .[0] / .[1] | wrap("number")
-    ) // jqmal_error("Unknown function \(.function)");
\ No newline at end of file
+include "utils";
+
+def arg_check(args):
+    if .inputs != (args|length) then
+        jqmal_error("Invalid number of arguments (expected \(.inputs) got \(args|length): \(args))")
+    else
+        .
+    end;
+
+
+def interpret(arguments; env):
+    select(.kind == "fn") | (
+        arg_check(arguments) | (
+            select(.function == "number_add") |
+            arguments | map(.value) | .[0] + .[1] | wrap("number")
+        ) // (
+            select(.function == "number_sub") |
+            arguments | map(.value) | .[0] - .[1] | wrap("number")
+        ) // (
+            select(.function == "number_mul") |
+            arguments | map(.value) | .[0] * .[1] | wrap("number")
+        ) // (
+            select(.function == "number_div") |
+            arguments | map(.value) | .[0] / .[1] | wrap("number")
+        ) // jqmal_error("Unknown native function \(.function)");
+    ) // jqmal_error("Unsupported function kind \(.kind)")
\ No newline at end of file
index b54cd6f..6df358f 100644 (file)
@@ -73,18 +73,22 @@ def replEnv:
         parent: null,
         environment: {
             "+": {
+                kind: "fn", # native function
                 inputs: 2,
                 function: "number_add"
             },
             "-": {
+                kind: "fn", # native function
                 inputs: 2,
                 function: "number_sub"
             },
             "*": {
+                kind: "fn", # native function
                 inputs: 2,
                 function: "number_mul"
             },
             "/": {
+                kind: "fn", # native function
                 inputs: 2,
                 function: "number_div"
             },