2020-01-09 19:07:24 +03:00
|
|
|
def _debug(ex):
|
|
|
|
. as $top
|
|
|
|
| ex
|
|
|
|
| debug
|
|
|
|
| $top;
|
|
|
|
|
|
|
|
def _print:
|
2020-01-10 17:13:51 +03:00
|
|
|
tostring;
|
2020-01-09 19:07:24 +03:00
|
|
|
|
2020-01-05 18:05:49 +03:00
|
|
|
def nwise(n):
|
|
|
|
def _nwise:
|
|
|
|
if length <= n then
|
|
|
|
.
|
|
|
|
else
|
|
|
|
.[0:n], (.[n:] | _nwise)
|
|
|
|
end;
|
|
|
|
_nwise;
|
|
|
|
|
2020-01-08 19:34:12 +03:00
|
|
|
def abs(x):
|
|
|
|
if x < 0 then 0 - x else x end;
|
|
|
|
|
2020-01-05 18:05:49 +03:00
|
|
|
def jqmal_error(e):
|
2020-01-08 19:34:12 +03:00
|
|
|
error("JqMAL Exception :: " + e);
|
2020-01-05 18:05:49 +03:00
|
|
|
|
|
|
|
def is_jqmal_error:
|
2020-01-08 19:34:12 +03:00
|
|
|
startswith("JqMAL Exception :: ");
|
2020-01-05 18:05:49 +03:00
|
|
|
|
|
|
|
def wrap(kind):
|
|
|
|
{
|
|
|
|
kind: kind,
|
|
|
|
value: .
|
2020-01-06 02:40:03 +03:00
|
|
|
};
|
|
|
|
|
2020-01-06 23:55:22 +03:00
|
|
|
def wrap2(kind; opts):
|
|
|
|
opts + {
|
|
|
|
kind: kind,
|
|
|
|
value: .
|
|
|
|
};
|
|
|
|
|
2020-01-07 19:43:49 +03:00
|
|
|
|
2020-01-08 17:36:57 +03:00
|
|
|
def find_free_references(keys):
|
|
|
|
def _refs:
|
|
|
|
if . == null then [] else
|
|
|
|
. as $dot
|
|
|
|
| if .kind == "symbol" then
|
|
|
|
if keys | contains([$dot.value]) then [] else [$dot.value] end
|
|
|
|
else if "list" == $dot.kind then
|
2024-08-26 05:43:34 +03:00
|
|
|
if $dot.value|length == 0 then
|
|
|
|
[]
|
|
|
|
else
|
|
|
|
# if - scan args
|
|
|
|
# def! - scan body
|
|
|
|
# let* - add keys sequentially, scan body
|
|
|
|
# fn* - add keys, scan body
|
|
|
|
# quote - []
|
|
|
|
# quasiquote - ???
|
|
|
|
$dot.value[0] as $head
|
|
|
|
| if $head.kind == "symbol" then
|
|
|
|
(
|
|
|
|
select($head.value == "if") | $dot.value[1:] | map(_refs) | reduce .[] as $x ([]; . + $x)
|
|
|
|
) // (
|
|
|
|
select($head.value == "def!") | $dot.value[2] | _refs
|
|
|
|
) // (
|
|
|
|
select($head.value == "let*") | $dot.value[2] | find_free_references(($dot.value[1].value as $value | ([ range(0; $value|length; 2) ] | map(select(. % 2 == 0) | $value[.].value))) + keys)
|
|
|
|
) // (
|
|
|
|
select($head.value == "fn*") | $dot.value[2] | find_free_references(($dot.value[1].value | map(.value)) + keys)
|
|
|
|
) // (
|
|
|
|
select($head.value == "quote") | []
|
|
|
|
) // (
|
|
|
|
select($head.value == "quasiquote") | []
|
|
|
|
) // ($dot.value | map(_refs) | reduce .[] as $x ([]; . + $x))
|
|
|
|
else
|
|
|
|
[ $dot.values[1:][] | _refs ]
|
|
|
|
end
|
|
|
|
end
|
2020-01-08 17:36:57 +03:00
|
|
|
else if "vector" == $dot.kind then
|
|
|
|
($dot.value | map(_refs) | reduce .[] as $x ([]; . + $x))
|
|
|
|
else if "hashmap" == $dot.kind then
|
2020-01-09 19:07:24 +03:00
|
|
|
([$dot.value | to_entries[] | ({kind: .value.kkind, value: .key}, .value.value) ] | map(_refs) | reduce .[] as $x ([]; . + $x))
|
2020-01-08 17:36:57 +03:00
|
|
|
else
|
|
|
|
[]
|
|
|
|
end end end end
|
|
|
|
end;
|
|
|
|
_refs | unique;
|
|
|
|
|
2020-01-07 19:43:49 +03:00
|
|
|
def tomal:
|
|
|
|
(
|
|
|
|
select(type == "array") | (
|
|
|
|
map(tomal) | wrap("list")
|
|
|
|
)
|
|
|
|
) // (
|
|
|
|
select(type == "string") | (
|
|
|
|
if startswith("sym/") then
|
|
|
|
.[4:] | wrap("symbol")
|
|
|
|
else
|
|
|
|
wrap("string")
|
|
|
|
end
|
|
|
|
)
|
|
|
|
) // (
|
|
|
|
select(type == "number") | (
|
|
|
|
wrap("number")
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2020-01-06 19:26:48 +03:00
|
|
|
def _extern(options):
|
|
|
|
{command: .}
|
|
|
|
| debug
|
2020-01-10 17:13:51 +03:00
|
|
|
| if (options.nowait | not) then
|
2020-01-06 19:26:48 +03:00
|
|
|
input | fromjson
|
|
|
|
else
|
|
|
|
null
|
2020-01-12 14:39:52 +03:00
|
|
|
end;
|
2020-01-06 19:26:48 +03:00
|
|
|
|
|
|
|
def issue_extern(cmd; options):
|
|
|
|
{cmd: cmd, args: .}
|
|
|
|
| _extern(options);
|
|
|
|
|
|
|
|
def issue_extern(cmd):
|
|
|
|
issue_extern(cmd; {});
|
|
|
|
|
2020-01-08 17:36:57 +03:00
|
|
|
def _readline:
|
2020-01-10 17:13:51 +03:00
|
|
|
[.]
|
2020-01-08 17:36:57 +03:00
|
|
|
| issue_extern("readline"; {nowait: false})
|
|
|
|
;
|
|
|
|
|
2020-01-09 19:07:24 +03:00
|
|
|
def __readline(prompt):
|
|
|
|
. as $top
|
|
|
|
| prompt
|
|
|
|
| _readline;
|
|
|
|
|
|
|
|
def __readline:
|
|
|
|
__readline(.);
|
|
|
|
|
2020-01-10 17:13:51 +03:00
|
|
|
def _display:
|
|
|
|
tostring | .+"\n" | debug;
|
|
|
|
|
2020-01-06 19:26:48 +03:00
|
|
|
def _write_to_file(name):
|
|
|
|
. as $value
|
2020-01-06 23:55:22 +03:00
|
|
|
| [(name|tojson), (.|tojson), (false|tojson)]
|
2020-01-06 19:26:48 +03:00
|
|
|
| issue_extern("fwrite"; {nowait: true})
|
2020-01-06 23:55:22 +03:00
|
|
|
| $value;
|
|
|
|
|
|
|
|
def _append_to_file(name):
|
|
|
|
. as $value
|
|
|
|
| [(name|tojson), (.|tojson), (true|tojson)]
|
|
|
|
| issue_extern("fwrite"; {nowait: true})
|
|
|
|
| $value;
|
|
|
|
|
2020-01-10 17:13:51 +03:00
|
|
|
def _halt:
|
|
|
|
[]
|
|
|
|
| issue_extern("halt"; {nowait: true})
|
|
|
|
| halt;
|
|
|
|
|
2020-01-06 23:55:22 +03:00
|
|
|
def trap:
|
2020-01-12 14:39:52 +03:00
|
|
|
_write_to_file("trap_reason.json") | jqmal_error("trap");
|