Commit | Line | Data |
---|---|---|
b103f95e A |
1 | def _debug(ex): |
2 | . as $top | |
3 | | ex | |
4 | | debug | |
5 | | $top; | |
6 | ||
7 | def _print: | |
83b974c5 | 8 | tostring; |
b103f95e | 9 | |
9088c0fa A |
10 | def nwise(n): |
11 | def _nwise: | |
12 | if length <= n then | |
13 | . | |
14 | else | |
15 | .[0:n], (.[n:] | _nwise) | |
16 | end; | |
17 | _nwise; | |
18 | ||
4db6de12 A |
19 | def abs(x): |
20 | if x < 0 then 0 - x else x end; | |
21 | ||
9088c0fa | 22 | def jqmal_error(e): |
4db6de12 | 23 | error("JqMAL Exception :: " + e); |
9088c0fa A |
24 | |
25 | def is_jqmal_error: | |
4db6de12 | 26 | startswith("JqMAL Exception :: "); |
9088c0fa A |
27 | |
28 | def wrap(kind): | |
29 | { | |
30 | kind: kind, | |
31 | value: . | |
136fb719 A |
32 | }; |
33 | ||
eedfbb43 A |
34 | def wrap2(kind; opts): |
35 | opts + { | |
36 | kind: kind, | |
37 | value: . | |
38 | }; | |
39 | ||
a451ec51 A |
40 | def isPair: |
41 | if (.kind == "list" or .kind == "vector") then | |
42 | .value | length > 0 | |
43 | else | |
44 | false | |
45 | end; | |
46 | ||
47 | def isPair(x): | |
48 | x | isPair; | |
49 | ||
e9cb5f03 A |
50 | def find_free_references(keys): |
51 | def _refs: | |
52 | if . == null then [] else | |
53 | . as $dot | |
54 | | if .kind == "symbol" then | |
55 | if keys | contains([$dot.value]) then [] else [$dot.value] end | |
56 | else if "list" == $dot.kind then | |
57 | # if - scan args | |
58 | # def! - scan body | |
59 | # let* - add keys sequentially, scan body | |
60 | # fn* - add keys, scan body | |
61 | # quote - [] | |
62 | # quasiquote - ??? | |
63 | $dot.value[0] as $head | |
64 | | if $head.kind == "symbol" then | |
65 | ( | |
66 | select($head.value == "if") | $dot.value[1:] | map(_refs) | reduce .[] as $x ([]; . + $x) | |
67 | ) // ( | |
68 | select($head.value == "def!") | $dot.value[2] | _refs | |
69 | ) // ( | |
fed3ca50 | 70 | 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) |
e9cb5f03 A |
71 | ) // ( |
72 | select($head.value == "fn*") | $dot.value[2] | find_free_references(($dot.value[1].value | map(.value)) + keys) | |
73 | ) // ( | |
74 | select($head.value == "quote") | [] | |
75 | ) // ( | |
76 | select($head.value == "quasiquote") | [] | |
77 | ) // ($dot.value | map(_refs) | reduce .[] as $x ([]; . + $x)) | |
78 | else | |
79 | [ $dot.values[1:][] | _refs ] | |
80 | end | |
81 | else if "vector" == $dot.kind then | |
82 | ($dot.value | map(_refs) | reduce .[] as $x ([]; . + $x)) | |
83 | else if "hashmap" == $dot.kind then | |
b103f95e | 84 | ([$dot.value | to_entries[] | ({kind: .value.kkind, value: .key}, .value.value) ] | map(_refs) | reduce .[] as $x ([]; . + $x)) |
e9cb5f03 A |
85 | else |
86 | [] | |
87 | end end end end | |
88 | end; | |
89 | _refs | unique; | |
90 | ||
a451ec51 A |
91 | def tomal: |
92 | ( | |
93 | select(type == "array") | ( | |
94 | map(tomal) | wrap("list") | |
95 | ) | |
96 | ) // ( | |
97 | select(type == "string") | ( | |
98 | if startswith("sym/") then | |
99 | .[4:] | wrap("symbol") | |
100 | else | |
101 | wrap("string") | |
102 | end | |
103 | ) | |
104 | ) // ( | |
105 | select(type == "number") | ( | |
106 | wrap("number") | |
107 | ) | |
108 | ); | |
109 | ||
7650046a A |
110 | def _extern(options): |
111 | {command: .} | |
112 | | debug | |
83b974c5 | 113 | | if (options.nowait | not) then |
7650046a A |
114 | input | fromjson |
115 | else | |
116 | null | |
597522fa | 117 | end; |
7650046a A |
118 | |
119 | def issue_extern(cmd; options): | |
120 | {cmd: cmd, args: .} | |
7650046a A |
121 | | _extern(options); |
122 | ||
123 | def issue_extern(cmd): | |
124 | issue_extern(cmd; {}); | |
125 | ||
e9cb5f03 | 126 | def _readline: |
83b974c5 | 127 | [.] |
e9cb5f03 A |
128 | | issue_extern("readline"; {nowait: false}) |
129 | ; | |
130 | ||
b103f95e A |
131 | def __readline(prompt): |
132 | . as $top | |
133 | | prompt | |
b103f95e A |
134 | | _readline; |
135 | ||
136 | def __readline: | |
137 | __readline(.); | |
138 | ||
83b974c5 A |
139 | def _display: |
140 | tostring | .+"\n" | debug; | |
141 | ||
7650046a A |
142 | def _write_to_file(name): |
143 | . as $value | |
eedfbb43 | 144 | | [(name|tojson), (.|tojson), (false|tojson)] |
7650046a | 145 | | issue_extern("fwrite"; {nowait: true}) |
eedfbb43 A |
146 | | $value; |
147 | ||
148 | def _append_to_file(name): | |
149 | . as $value | |
150 | | [(name|tojson), (.|tojson), (true|tojson)] | |
151 | | issue_extern("fwrite"; {nowait: true}) | |
152 | | $value; | |
153 | ||
83b974c5 A |
154 | def _halt: |
155 | [] | |
156 | | issue_extern("halt"; {nowait: true}) | |
157 | | halt; | |
158 | ||
eedfbb43 | 159 | def trap: |
597522fa | 160 | _write_to_file("trap_reason.json") | jqmal_error("trap"); |