Merge pull request #15 from joelpickup/master
[jackhill/mal.git] / bash / step9_try.sh
CommitLineData
7838e339 1#!/usr/bin/env bash
31690700 2
31690700 3source $(dirname $0)/reader.sh
ea81a808 4source $(dirname $0)/printer.sh
ea81a808 5source $(dirname $0)/env.sh
8cb5cda4 6source $(dirname $0)/core.sh
31690700 7
86b689f3 8# read
31690700 9READ () {
8cb5cda4 10 [ "${1}" ] && r="${1}" || READLINE
31690700
JM
11 READ_STR "${r}"
12}
13
86b689f3 14# eval
31690700 15IS_PAIR () {
db4c329a 16 if _sequential? "${1}"; then
31690700
JM
17 _count "${1}"
18 [[ "${r}" > 0 ]] && return 0
19 fi
20 return 1
21}
22
23QUASIQUOTE () {
24 if ! IS_PAIR "${1}"; then
ea81a808
JM
25 _symbol quote
26 _list "${r}" "${1}"
31690700
JM
27 return
28 else
29 _nth "${1}" 0; local a0="${r}"
30 if [[ "${ANON["${a0}"]}" == "unquote" ]]; then
31 _nth "${1}" 1
32 return
33 elif IS_PAIR "${a0}"; then
34 _nth "${a0}" 0; local a00="${r}"
35 if [[ "${ANON["${a00}"]}" == "splice-unquote" ]]; then
ea81a808 36 _symbol concat; local a="${r}"
31690700 37 _nth "${a0}" 1; local b="${r}"
8cb5cda4 38 _rest "${1}"
31690700 39 QUASIQUOTE "${r}"; local c="${r}"
ea81a808 40 _list "${a}" "${b}" "${c}"
31690700
JM
41 return
42 fi
43 fi
44 fi
ea81a808 45 _symbol cons; local a="${r}"
31690700 46 QUASIQUOTE "${a0}"; local b="${r}"
8cb5cda4 47 _rest "${1}"
31690700 48 QUASIQUOTE "${r}"; local c="${r}"
ea81a808 49 _list "${a}" "${b}" "${c}"
31690700
JM
50 return
51}
52
53IS_MACRO_CALL () {
54 if ! _list? "${1}"; then return 1; fi
55 _nth "${1}" 0; local a0="${r}"
56 if _symbol? "${a0}"; then
b8ee29b2 57 ENV_FIND "${2}" "${a0}"
31690700 58 if [[ "${r}" ]]; then
b8ee29b2
JM
59 ENV_GET "${2}" "${a0}"
60 [ "${ANON["${r}_ismacro_"]}" ]
61 return $?
31690700
JM
62 fi
63 fi
64 return 1
65}
66
67MACROEXPAND () {
68 local ast="${1}" env="${2}"
69 while IS_MACRO_CALL "${ast}" "${env}"; do
70 _nth "${ast}" 0; local a0="${r}"
b8ee29b2 71 ENV_GET "${env}" "${a0}"; local mac="${ANON["${r}"]}"
8cb5cda4 72 _rest "${ast}"
31690700
JM
73 ${mac%%@*} ${ANON["${r}"]}
74 ast="${r}"
75 done
76 r="${ast}"
77}
78
79
80EVAL_AST () {
81 local ast="${1}" env="${2}"
82 #_pr_str "${ast}"; echo "EVAL_AST '${ast}:${r} / ${env}'"
83 _obj_type "${ast}"; local ot="${r}"
84 case "${ot}" in
85 symbol)
b8ee29b2 86 ENV_GET "${env}" "${ast}"
31690700
JM
87 return ;;
88 list)
ea81a808 89 _map_with_type _list EVAL "${ast}" "${env}" ;;
31690700 90 vector)
ea81a808 91 _map_with_type _vector EVAL "${ast}" "${env}" ;;
31690700
JM
92 hash_map)
93 local res="" val="" hm="${ANON["${ast}"]}"
ea81a808 94 _hash_map; local new_hm="${r}"
31690700
JM
95 eval local keys="\${!${hm}[@]}"
96 for key in ${keys}; do
97 eval val="\${${hm}[\"${key}\"]}"
98 EVAL "${val}" "${env}"
ea81a808 99 _assoc! "${new_hm}" "${key}" "${r}"
31690700
JM
100 done
101 r="${new_hm}" ;;
102 *)
103 r="${ast}" ;;
104 esac
105}
106
31690700
JM
107EVAL () {
108 local ast="${1}" env="${2}"
109 while true; do
110 r=
111 [[ "${__ERROR}" ]] && return 1
112 #_pr_str "${ast}"; echo "EVAL '${r} / ${env}'"
113 if ! _list? "${ast}"; then
114 EVAL_AST "${ast}" "${env}"
115 return
116 fi
117
118 # apply list
119 MACROEXPAND "${ast}" "${env}"
120 ast="${r}"
121 if ! _list? "${ast}"; then return; fi
122 _nth "${ast}" 0; local a0="${r}"
123 _nth "${ast}" 1; local a1="${r}"
124 _nth "${ast}" 2; local a2="${r}"
125 case "${ANON["${a0}"]}" in
b8ee29b2
JM
126 def!) EVAL "${a2}" "${env}"
127 [[ "${__ERROR}" ]] && return 1
128 ENV_SET "${env}" "${a1}" "${r}"
31690700
JM
129 return ;;
130 let*) ENV "${env}"; local let_env="${r}"
131 local let_pairs=(${ANON["${a1}"]})
132 local idx=0
133 #echo "let: [${let_pairs[*]}] for ${a2}"
134 while [[ "${let_pairs["${idx}"]}" ]]; do
135 EVAL "${let_pairs[$(( idx + 1))]}" "${let_env}"
b8ee29b2 136 ENV_SET "${let_env}" "${let_pairs[${idx}]}" "${r}"
31690700
JM
137 idx=$(( idx + 2))
138 done
6301e0b6
JM
139 ast="${a2}"
140 env="${let_env}"
141 # Continue loop
142 ;;
31690700
JM
143 quote)
144 r="${a1}"
145 return ;;
146 quasiquote)
147 QUASIQUOTE "${a1}"
6301e0b6
JM
148 ast="${r}"
149 # Continue loop
150 ;;
31690700 151 defmacro!)
31690700 152 EVAL "${a2}" "${env}"
b8ee29b2
JM
153 [[ "${__ERROR}" ]] && return 1
154 ANON["${r}_ismacro_"]="yes"
155 ENV_SET "${env}" "${a1}" "${r}"
31690700
JM
156 return ;;
157 macroexpand)
158 MACROEXPAND "${a1}" "${env}"
159 return ;;
b8ee29b2 160 try*) EVAL "${a1}" "${env}"
31690700
JM
161 [[ -z "${__ERROR}" ]] && return
162 _nth "${a2}" 0; local a20="${r}"
163 if [ "${ANON["${a20}"]}" == "catch__STAR__" ]; then
164 _nth "${a2}" 1; local a21="${r}"
165 _nth "${a2}" 2; local a22="${r}"
ea81a808 166 _list "${a21}"; local binds="${r}"
31690700
JM
167 ENV "${env}" "${binds}" "${__ERROR}"
168 local try_env="${r}"
169 __ERROR=
b8ee29b2 170 EVAL "${a22}" "${try_env}"
31690700
JM
171 fi # if no catch* clause, just propagate __ERROR
172 return ;;
173 do) _count "${ast}"
174 _slice "${ast}" 1 $(( ${r} - 2 ))
175 EVAL_AST "${r}" "${env}"
176 [[ "${__ERROR}" ]] && r= && return 1
8cb5cda4 177 _last "${ast}"
31690700
JM
178 ast="${r}"
179 # Continue loop
180 ;;
181 if) EVAL "${a1}" "${env}"
b8ee29b2 182 [[ "${__ERROR}" ]] && return 1
31690700
JM
183 if [[ "${r}" == "${__false}" || "${r}" == "${__nil}" ]]; then
184 # eval false form
185 _nth "${ast}" 3; local a3="${r}"
186 if [[ "${a3}" ]]; then
187 ast="${a3}"
188 else
189 r="${__nil}"
190 return
191 fi
192 else
193 # eval true condition
194 ast="${a2}"
195 fi
196 # Continue loop
197 ;;
ea81a808
JM
198 fn*) _function "ENV \"${env}\" \"${a1}\" \"\${@}\"; \
199 EVAL \"${a2}\" \"\${r}\"" \
200 "${a2}" "${env}" "${a1}"
31690700
JM
201 return ;;
202 *) EVAL_AST "${ast}" "${env}"
203 [[ "${__ERROR}" ]] && r= && return 1
204 local el="${r}"
8cb5cda4
JM
205 _first "${el}"; local f="${ANON["${r}"]}"
206 _rest "${el}"; local args="${ANON["${r}"]}"
31690700
JM
207 #echo "invoke: [${f}] ${args}"
208 if [[ "${f//@/ }" != "${f}" ]]; then
209 set -- ${f//@/ }
210 ast="${2}"
211 ENV "${3}" "${4}" ${args}
212 env="${r}"
213 else
214 eval ${f%%@*} ${args}
215 return
216 fi
217 # Continue loop
218 ;;
219 esac
220 done
221}
ea81a808 222
86b689f3 223# print
31690700
JM
224PRINT () {
225 if [[ "${__ERROR}" ]]; then
226 _pr_str "${__ERROR}" yes
227 r="Error: ${r}"
228 __ERROR=
229 else
230 _pr_str "${1}" yes
231 fi
232}
233
86b689f3 234# repl
31690700
JM
235ENV; REPL_ENV="${r}"
236REP () {
237 r=
70aff0c1 238 READ "${1}"
31690700
JM
239 EVAL "${r}" "${REPL_ENV}"
240 PRINT "${r}"
241}
242
8cb5cda4 243# core.sh: defined using bash
b8ee29b2
JM
244_fref () {
245 _symbol "${1}"; local sym="${r}"
246 _function "${2} \"\${@}\""
247 ENV_SET "${REPL_ENV}" "${sym}" "${r}"
248}
ea81a808 249for n in "${!core_ns[@]}"; do _fref "${n}" "${core_ns["${n}"]}"; done
ea81a808 250_eval () { EVAL "${1}" "${REPL_ENV}"; }
31690700 251_fref "eval" _eval
86b689f3
JM
252_list; argv="${r}"
253for _arg in "${@:2}"; do _string "${_arg}"; _conj! "${argv}" "${r}"; done
b8ee29b2
JM
254_symbol "__STAR__ARGV__STAR__"
255ENV_SET "${REPL_ENV}" "${r}" "${argv}";
31690700 256
8cb5cda4 257# core.mal: defined using the language itself
31690700 258REP "(def! not (fn* (a) (if a false true)))"
8cb5cda4 259REP "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"
31690700
JM
260REP "(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))"
261REP "(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) \`(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))"
31690700 262
86b689f3 263# load/run file from command line (then exit)
31690700 264if [[ "${1}" ]]; then
86b689f3
JM
265 REP "(load-file \"${1}\")"
266 exit 0
267fi
268
269# repl loop
86b689f3
JM
270while true; do
271 READLINE "user> " || exit "$?"
272 [[ "${r}" ]] && REP "${r}" && echo "${r}"
273done