blob: ca53c43478a7eff0937f950866443b0e0053cdf1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
#
# mal (Make a Lisp) object types
#
if [ -z "${__mal_core_included__}" ]; then
__mal_core_included=true
source $(dirname $0)/types.sh
source $(dirname $0)/reader.sh
source $(dirname $0)/printer.sh
# Exceptions/Errors
throw() {
__ERROR="${1}"
r=
}
# General functions
obj_type () {
_obj_type "${1}"
_string "${r}"
}
equal? () {
_equal? "${1}" "${2}" && r="${__true}" || r="${__false}"
}
# Scalar functions
nil? () { _nil? "${1}" && r="${__true}" || r="${__false}"; }
true? () { _true? "${1}" && r="${__true}" || r="${__false}"; }
false? () { _false? "${1}" && r="${__true}" || r="${__false}"; }
# Symbol functions
symbol? () { _symbol? "${1}" && r="${__true}" || r="${__false}"; }
# Keyword functions
keyword? () { _keyword? "${1}" && r="${__true}" || r="${__false}"; }
# Number functions
number? () { _number? "${1}" && r="${__true}" || r="${__false}"; }
num_plus () { r=$(( ${ANON["${1}"]} + ${ANON["${2}"]} )); _number "${r}"; }
num_minus () { r=$(( ${ANON["${1}"]} - ${ANON["${2}"]} )); _number "${r}"; }
num_multiply () { r=$(( ${ANON["${1}"]} * ${ANON["${2}"]} )); _number "${r}"; }
num_divide () { r=$(( ${ANON["${1}"]} / ${ANON["${2}"]} )); _number "${r}"; }
_num_bool () { [[ "${1}" = "1" ]] && r="${__true}" || r="${__false}"; }
num_gt () { r=$(( ${ANON["${1}"]} > ${ANON["${2}"]} )); _num_bool "${r}"; }
num_gte () { r=$(( ${ANON["${1}"]} >= ${ANON["${2}"]} )); _num_bool "${r}"; }
num_lt () { r=$(( ${ANON["${1}"]} < ${ANON["${2}"]} )); _num_bool "${r}"; }
num_lte () { r=$(( ${ANON["${1}"]} <= ${ANON["${2}"]} )); _num_bool "${r}"; }
# return number of milliseconds since epoch
time_ms () {
local ms=$(date +%s%3N)
_number "${ms}"
}
# String functions
string? () { _string? "${1}" && r="${__true}" || r="${__false}"; }
pr_str () {
local res=""
for x in "${@}"; do _pr_str "${x}" yes; res="${res} ${r}"; done
_string "${res:1}"
}
str () {
local res=""
for x in "${@}"; do _pr_str "${x}"; res="${res}${r}"; done
_string "${res}"
}
prn () {
local res=""
for x in "${@}"; do _pr_str "${x}" yes; res="${res} ${r}"; done
echo "${res:1}"
r="${__nil}";
}
println () {
local res=""
for x in "${@}"; do _pr_str "${x}"; res="${res} ${r}"; done
res="${res//\\n/$'\n'}"
echo -e "${res:1}"
r="${__nil}";
}
readline () {
READLINE "${ANON["${1}"]}" && _string "${r}" || r="${__nil}"
}
read_string () {
READ_STR "${ANON["${1}"]}"
}
slurp () {
local lines
mapfile lines < "${ANON["${1}"]}"
local text="${lines[*]}"; text=${text//$'\n' /$'\n'}
_string "${text}"
}
# Function functions
function? () { _function? "${1}" && r="${__true}" || r="${__false}"; }
# List functions
list? () { _list? "${1}" && r="${__true}" || r="${__false}"; }
# Vector functions (same as lists for now)
vector? () { _vector? "${1}" && r="${__true}" || r="${__false}"; }
# Hash map (associative array) functions
hash_map? () { _hash_map? "${1}" && r="${__true}" || r="${__false}"; }
# Return new hash map with keys/values updated
assoc () {
if ! _hash_map? "${1}"; then
_error "assoc onto non-hash-map"
return
fi
_copy_hash_map "${1}"; shift
local name="${r}"
local obj=${ANON["${name}"]}
declare -A -g ${obj}
while [[ "${1}" ]]; do
eval ${obj}[\"${ANON["${1}"]}\"]=\"${2}\"
shift; shift
done
r="${name}"
}
dissoc () {
if ! _hash_map? "${1}"; then
_error "dissoc from non-hash-map"
return
fi
_copy_hash_map "${1}"; shift
local name="${r}"
local obj=${ANON["${name}"]}
declare -A -g ${obj}
while [[ "${1}" ]]; do
eval unset ${obj}[\"${ANON["${1}"]}\"]
shift
done
r="${name}"
}
_get () {
_obj_type "${1}"; local ot="${r}"
case "${ot}" in
hash_map)
local obj="${ANON["${1}"]}"
eval r="\${${obj}[\"${2}\"]}" ;;
list|vector)
_nth "${1}" "${2}" ;;
nil)
r="${__nil}" ;;
esac
}
get () {
_get "${1}" "${ANON["${2}"]}"
[[ "${r}" ]] || r="${__nil}"
}
contains? () { _contains? "${1}" "${ANON["${2}"]}" && r="${__true}" || r="${__false}"; }
keys () {
local obj="${ANON["${1}"]}"
local kstrs=
eval local keys="\${!${obj}[@]}"
for k in ${keys}; do
_string "${k}"
kstrs="${kstrs} ${r}"
done
__new_obj_hash_code
r="list_${r}"
ANON["${r}"]="${kstrs:1}"
}
vals () {
local obj="${ANON["${1}"]}"
local kvals=
local val=
eval local keys="\${!${obj}[@]}"
for k in ${keys}; do
eval val="\${${obj}["\${k}"]}"
kvals="${kvals} ${val}"
done
__new_obj_hash_code
r="list_${r}"
ANON["${r}"]="${kvals:1}"
}
# sequence operations
sequential? () {
_sequential? "${1}" && r="${__true}" || r="${__false}"
}
cons () {
_list ${1} ${ANON["${2}"]}
}
concat () {
_list
local acc=""
for item in "${@}"; do
acc="${acc} ${ANON["${item}"]}"
done
ANON["${r}"]="${acc:1}"
}
nth () {
_nth "${1}" "${ANON["${2}"]}"
if [ -z "${r}" ]; then
_error "nth: index out of bounds"
return
fi
}
empty? () { _empty? "${1}" && r="${__true}" || r="${__false}"; }
count () {
_count "${1}"
_number "${r}"
}
conj () {
local obj="${1}"; shift
local obj_data="${ANON["${obj}"]}"
__new_obj_like "${obj}"
if _list? "${obj}"; then
ANON["${r}"]="${obj_data:+${obj_data}}"
for elem in ${@}; do
ANON["${r}"]="${elem} ${ANON["${r}"]}"
done
else
ANON["${r}"]="${obj_data:+${obj_data} }${*}"
fi
}
apply () {
local f="${ANON["${1}"]}"; shift
local items="${@:1:$(( ${#@} -1 ))} ${ANON["${!#}"]}"
eval ${f%%@*} ${items}
}
# Takes a function object and an list object and invokes the function
# on each element of the list, returning a new list of the results.
map () {
local f="${ANON["${1}"]}"; shift
#echo _map "${f}" "${@}"
_map "${f}" "${@}"
}
# Metadata functions
with_meta () {
local obj="${1}"; shift
local meta_data="${1}"; shift
__new_obj_like "${obj}"
ANON["${r}"]="${ANON["${obj}"]}"
local meta_obj="meta_${r#*_}"
ANON["${meta_obj}"]="${meta_data}"
}
meta () {
r="${ANON["meta_${1#*_}"]}"
[[ "${r}" ]] || r="${__nil}"
}
# Atom functions
atom? () { _atom? "${1}" && r="${__true}" || r="${__false}"; }
deref () {
# TODO: double-check atom type
r=${ANON["${1}"]}
}
reset_BANG () {
local atm="${1}"; shift
ANON["${atm}"]="${*}"
r="${*}"
}
swap_BANG () {
local atm="${1}"; shift
local f="${ANON["${1}"]}"; shift
${f%%@*} "${ANON["${atm}"]}" "${@}"
ANON["${atm}"]="${r}"
}
# Namespace of core functions
declare -A core_ns=(
[type]=obj_type
[=]=equal?
[throw]=throw
[nil?]=nil?
[true?]=true?
[false?]=false?
[symbol]=_symbol
[symbol?]=symbol?
[keyword]=_keyword
[keyword?]=keyword?
[pr-str]=pr_str
[str]=str
[prn]=prn
[println]=println
[readline]=readline
[read-string]=read_string
[slurp]=slurp
[<]=num_lt
[<=]=num_lte
[>]=num_gt
[>=]=num_gte
[+]=num_plus
[-]=num_minus
[__STAR__]=num_multiply
[/]=num_divide
[time-ms]=time_ms
[list]=_list
[list?]=list?
[vector]=_vector
[vector?]=vector?
[hash-map]=_hash_map
[map?]=hash_map?
[assoc]=assoc
[dissoc]=dissoc
[get]=get
[contains?]=contains?
[keys]=keys
[vals]=vals
[sequential?]=sequential?
[cons]=cons
[concat]=concat
[nth]=nth
[first]=_first
[rest]=_rest
[empty?]=empty?
[count]=count
[conj]=conj
[apply]=apply
[map]=map
[with-meta]=with_meta
[meta]=meta
[atom]=_atom
[atom?]=atom?
[deref]=deref
[reset!]=reset_BANG
[swap!]=swap_BANG)
fi
|