2020-01-05 18:05:49 +03:00
|
|
|
include "utils";
|
2020-01-05 20:49:41 +03:00
|
|
|
include "core";
|
2020-01-05 22:59:12 +03:00
|
|
|
include "env";
|
2020-01-08 17:36:57 +03:00
|
|
|
include "printer";
|
2020-01-05 18:05:49 +03:00
|
|
|
|
|
|
|
def arg_check(args):
|
2020-01-08 19:34:12 +03:00
|
|
|
if .inputs < 0 then
|
|
|
|
if (abs(.inputs) - 1) > (args | length) then
|
2020-01-09 19:07:24 +03:00
|
|
|
jqmal_error("Invalid number of arguments (expected at least \(abs(.inputs) - 1), got \(args|length))")
|
2020-01-08 19:34:12 +03:00
|
|
|
else
|
|
|
|
.
|
|
|
|
end
|
2020-01-05 22:59:12 +03:00
|
|
|
else if .inputs != (args|length) then
|
2020-01-10 09:01:00 +03:00
|
|
|
jqmal_error("Invalid number of arguments (expected \(.inputs), got \(args|length))")
|
2020-01-05 18:05:49 +03:00
|
|
|
else
|
|
|
|
.
|
2020-01-05 22:59:12 +03:00
|
|
|
end end;
|
2020-01-05 18:05:49 +03:00
|
|
|
|
2020-01-06 19:26:48 +03:00
|
|
|
def extractReplEnv(env):
|
|
|
|
env | .replEnv // .;
|
|
|
|
|
|
|
|
def extractEnv(env):
|
|
|
|
env | .currentEnv // .;
|
|
|
|
|
2020-01-14 01:25:39 +03:00
|
|
|
def updateReplEnv(renv):
|
2020-01-06 19:26:48 +03:00
|
|
|
def findpath:
|
|
|
|
if .env.parent then
|
|
|
|
.path += ["parent"] |
|
|
|
|
.env |= .parent |
|
|
|
|
findpath
|
|
|
|
else
|
|
|
|
.path
|
|
|
|
end;
|
2020-01-14 01:25:39 +03:00
|
|
|
({ env: ., path: [] } | findpath) as $path |
|
|
|
|
setpath($path; renv);
|
2020-01-06 19:26:48 +03:00
|
|
|
|
|
|
|
def extractCurrentReplEnv(env):
|
|
|
|
def findpath:
|
|
|
|
if .env.parent then
|
|
|
|
.path += ["parent"] |
|
|
|
|
.env |= .parent |
|
|
|
|
findpath
|
|
|
|
else
|
|
|
|
.path
|
|
|
|
end;
|
|
|
|
if env.currentEnv != null then
|
|
|
|
({ env: env.currentEnv, path: [] } | findpath) as $path |
|
|
|
|
env.currentEnv | getpath($path)
|
|
|
|
else
|
|
|
|
env
|
|
|
|
end;
|
|
|
|
|
2020-01-10 09:01:00 +03:00
|
|
|
def extractAtoms(env):
|
|
|
|
env.atoms // {};
|
2020-01-08 17:36:57 +03:00
|
|
|
|
|
|
|
def addFrees(newEnv; frees):
|
|
|
|
. as $env
|
|
|
|
| reduce frees[] as $free (
|
|
|
|
$env;
|
|
|
|
. as $dot
|
|
|
|
| extractEnv(newEnv) as $env
|
|
|
|
| env_req($env; $free) as $lookup
|
|
|
|
| if $lookup != null then
|
|
|
|
env_set_(.; $free; $lookup)
|
|
|
|
else
|
|
|
|
.
|
|
|
|
end)
|
|
|
|
| . as $env
|
|
|
|
| $env;
|
2020-01-06 23:55:22 +03:00
|
|
|
|
2020-01-05 22:59:12 +03:00
|
|
|
def interpret(arguments; env; _eval):
|
2020-01-06 19:26:48 +03:00
|
|
|
extractReplEnv(env) as $replEnv |
|
2020-01-10 09:01:00 +03:00
|
|
|
extractAtoms(env) as $envAtoms |
|
|
|
|
(if $DEBUG then _debug("INTERP: \(. | pr_str(env))") else . end) |
|
2020-01-06 19:26:48 +03:00
|
|
|
(select(.kind == "fn") |
|
|
|
|
arg_check(arguments) |
|
|
|
|
(select(.function == "eval") |
|
|
|
|
# special function
|
2020-01-14 01:25:39 +03:00
|
|
|
{ expr: arguments[0], env: $replEnv|wrapEnv($replEnv; $envAtoms) }
|
2020-01-06 19:26:48 +03:00
|
|
|
| _eval
|
|
|
|
| .env as $xenv
|
|
|
|
| extractReplEnv($xenv) as $xreplenv
|
|
|
|
| setpath(
|
|
|
|
["env", "currentEnv"];
|
2020-01-14 01:25:39 +03:00
|
|
|
extractEnv(env) | updateReplEnv($xreplenv))
|
2020-01-06 23:55:22 +03:00
|
|
|
) //
|
|
|
|
(select(.function == "reset!") |
|
|
|
|
# env modifying function
|
2020-01-10 09:01:00 +03:00
|
|
|
arguments[0].identity as $id |
|
|
|
|
($envAtoms | setpath([$id]; arguments[1])) as $envAtoms |
|
|
|
|
arguments[1] | addEnv(env | setpath(["atoms"]; $envAtoms))
|
2020-01-06 23:55:22 +03:00
|
|
|
) //
|
|
|
|
(select(.function == "swap!") |
|
|
|
|
# env modifying function
|
2020-01-10 09:01:00 +03:00
|
|
|
arguments[0].identity as $id |
|
|
|
|
$envAtoms[$id] as $initValue |
|
2020-01-06 23:55:22 +03:00
|
|
|
arguments[1] as $function |
|
|
|
|
([$initValue] + arguments[2:]) as $args |
|
|
|
|
($function | interpret($args; env; _eval)) as $newEnvValue |
|
2020-01-10 09:01:00 +03:00
|
|
|
($envAtoms | setpath([$id]; $newEnvValue.expr)) as $envAtoms |
|
|
|
|
$newEnvValue.expr | addEnv(env | setpath(["atoms"]; $envAtoms))
|
|
|
|
) // (select(.function == "atom") |
|
|
|
|
(now|tostring) as $id |
|
|
|
|
{kind: "atom", identity: $id} as $value |
|
|
|
|
($envAtoms | setpath([$id]; arguments[0])) as $envAtoms |
|
|
|
|
$value | addEnv(env | setpath(["atoms"]; $envAtoms))
|
|
|
|
) // (select(.function == "deref") |
|
|
|
|
$envAtoms[arguments[0].identity] | addEnv(env)
|
|
|
|
) //
|
2020-01-08 19:34:12 +03:00
|
|
|
(select(.function == "apply") |
|
|
|
|
# (apply F ...T A) -> (F ...T ...A)
|
|
|
|
arguments as $args
|
|
|
|
| ($args|first) as $F
|
|
|
|
| ($args|last.value) as $A
|
|
|
|
| $args[1:-1] as $T
|
|
|
|
| $F | interpret([$T[], $A[]]; env; _eval)
|
|
|
|
) //
|
|
|
|
(select(.function == "map") |
|
|
|
|
arguments
|
|
|
|
| first as $F
|
|
|
|
| last.value as $L
|
|
|
|
| (reduce $L[] as $elem (
|
2020-01-10 09:01:00 +03:00
|
|
|
{env: env, val: []};
|
|
|
|
. as $dot |
|
|
|
|
($F | interpret([$elem]; $dot.env; _eval)) as $val |
|
|
|
|
{
|
|
|
|
val: (.val + [$val.expr]),
|
|
|
|
env: (.env | setpath(["atoms"]; $val.env.atoms))
|
|
|
|
}
|
2020-01-08 19:34:12 +03:00
|
|
|
)) as $ex
|
2020-01-10 09:01:00 +03:00
|
|
|
| $ex.val | wrap("list") | addEnv($ex.env)
|
2020-01-08 19:34:12 +03:00
|
|
|
) //
|
2020-01-06 19:26:48 +03:00
|
|
|
(core_interp(arguments; env) | addEnv(env))
|
|
|
|
) //
|
2020-01-05 22:59:12 +03:00
|
|
|
(select(.kind == "function") as $fn |
|
|
|
|
# todo: arg_check
|
2020-01-10 09:01:00 +03:00
|
|
|
(.body | pr_str(env)) as $src |
|
2020-01-08 17:36:57 +03:00
|
|
|
# _debug("INTERP " + $src) |
|
|
|
|
# _debug("FREES " + ($fn.free_referencess | tostring)) |
|
|
|
|
env_setfallback(extractEnv(.env | addFrees(env; $fn.free_referencess)); extractEnv(env)) | childEnv($fn.binds; arguments) as $fnEnv |
|
2020-01-06 02:40:03 +03:00
|
|
|
# tell it about its surroundings
|
2020-01-06 10:30:26 +03:00
|
|
|
(reduce $fn.free_referencess[] as $name (
|
2020-01-06 02:40:03 +03:00
|
|
|
$fnEnv;
|
2020-01-06 23:55:22 +03:00
|
|
|
. as $env | try env_set_(
|
2020-01-06 10:30:26 +03:00
|
|
|
.;
|
|
|
|
$name;
|
|
|
|
$name | env_get(env) | . as $xvalue
|
|
|
|
| if $xvalue.kind == "function" then
|
|
|
|
setpath(["free_referencess"]; $fn.free_referencess)
|
|
|
|
else
|
|
|
|
$xvalue
|
|
|
|
end
|
|
|
|
) catch $env)) as $fnEnv |
|
2020-01-06 02:40:03 +03:00
|
|
|
# tell it about itself
|
|
|
|
env_multiset($fnEnv; $fn.names; $fn) as $fnEnv |
|
2020-01-06 19:26:48 +03:00
|
|
|
{
|
|
|
|
env: env_multiset($fnEnv; $fn.names; $fn)
|
2020-01-14 01:25:39 +03:00
|
|
|
| wrapEnv($replEnv; $envAtoms),
|
2020-01-06 19:26:48 +03:00
|
|
|
expr: $fn.body
|
|
|
|
}
|
2020-01-08 17:36:57 +03:00
|
|
|
| . as $dot
|
|
|
|
# | _debug("FNEXEC " + (.expr | pr_str) + " " + (env_req($dot.env; $fn.binds[0]) | pr_str))
|
2020-01-06 23:55:22 +03:00
|
|
|
| _eval
|
2020-01-06 19:26:48 +03:00
|
|
|
| . as $envexp
|
2020-01-06 23:55:22 +03:00
|
|
|
| (extractReplEnv($envexp.env)) as $xreplenv
|
2020-01-06 19:26:48 +03:00
|
|
|
|
|
|
|
|
{
|
|
|
|
expr: .expr,
|
|
|
|
env: extractEnv(env)
|
2020-01-14 01:25:39 +03:00
|
|
|
| updateReplEnv($xreplenv)
|
|
|
|
| wrapEnv($xreplenv; $envexp.env.atoms)
|
2020-01-06 19:26:48 +03:00
|
|
|
}
|
2020-01-08 17:36:57 +03:00
|
|
|
# | . as $dot
|
|
|
|
# | _debug("FNPOST " + (.expr | pr_str) + " " + (env_req($dot.expr.env; $fn.binds[0]) | pr_str))
|
|
|
|
# | _debug("INTERP " + $src + " = " + (.expr|pr_str))
|
2020-01-05 22:59:12 +03:00
|
|
|
) //
|
|
|
|
jqmal_error("Unsupported function kind \(.kind)");
|
2020-01-06 19:26:48 +03:00
|
|
|
|