All: *ARGV* and *host-language*. Misc syncing/fixes.
[jackhill/mal.git] / bash / stepA_more.sh
CommitLineData
31690700
JM
1#!/bin/bash
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
57 ENV_FIND "${2}" "${ANON["${a0}"]}_ismacro_"
58 if [[ "${r}" ]]; then
59 return 0
60 fi
61 fi
62 return 1
63}
64
65MACROEXPAND () {
66 local ast="${1}" env="${2}"
67 while IS_MACRO_CALL "${ast}" "${env}"; do
68 _nth "${ast}" 0; local a0="${r}"
69 ENV_GET "${env}" "${ANON["${a0}"]}"; local mac="${ANON["${r}"]}"
8cb5cda4 70 _rest "${ast}"
31690700
JM
71 ${mac%%@*} ${ANON["${r}"]}
72 ast="${r}"
73 done
74 r="${ast}"
75}
76
77
78EVAL_AST () {
79 local ast="${1}" env="${2}"
80 #_pr_str "${ast}"; echo "EVAL_AST '${ast}:${r} / ${env}'"
81 _obj_type "${ast}"; local ot="${r}"
82 case "${ot}" in
83 symbol)
84 local val="${ANON["${ast}"]}"
85 ENV_GET "${env}" "${val}"
86 return ;;
87 list)
ea81a808 88 _map_with_type _list EVAL "${ast}" "${env}" ;;
31690700 89 vector)
ea81a808 90 _map_with_type _vector EVAL "${ast}" "${env}" ;;
31690700
JM
91 hash_map)
92 local res="" val="" hm="${ANON["${ast}"]}"
ea81a808 93 _hash_map; local new_hm="${r}"
31690700
JM
94 eval local keys="\${!${hm}[@]}"
95 for key in ${keys}; do
96 eval val="\${${hm}[\"${key}\"]}"
97 EVAL "${val}" "${env}"
ea81a808 98 _assoc! "${new_hm}" "${key}" "${r}"
31690700
JM
99 done
100 r="${new_hm}" ;;
101 *)
102 r="${ast}" ;;
103 esac
104}
105
31690700
JM
106EVAL () {
107 local ast="${1}" env="${2}"
108 while true; do
109 r=
110 [[ "${__ERROR}" ]] && return 1
111 #_pr_str "${ast}"; echo "EVAL '${r} / ${env}'"
112 if ! _list? "${ast}"; then
113 EVAL_AST "${ast}" "${env}"
114 return
115 fi
116
117 # apply list
118 MACROEXPAND "${ast}" "${env}"
119 ast="${r}"
120 if ! _list? "${ast}"; then return; fi
121 _nth "${ast}" 0; local a0="${r}"
122 _nth "${ast}" 1; local a1="${r}"
123 _nth "${ast}" 2; local a2="${r}"
124 case "${ANON["${a0}"]}" in
125 def!) local k="${ANON["${a1}"]}"
126 #echo "def! ${k} to ${a2} in ${env}"
127 EVAL "${a2}" "${env}"
128 ENV_SET "${env}" "${k}" "${r}"
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}"
136 ENV_SET "${let_env}" "${ANON["${let_pairs[${idx}]}"]}" "${r}"
137 idx=$(( idx + 2))
138 done
139 EVAL "${a2}" "${let_env}"
140 return ;;
141 quote)
142 r="${a1}"
143 return ;;
144 quasiquote)
145 QUASIQUOTE "${a1}"
146 EVAL "${r}" "${env}"
147 return ;;
148 defmacro!)
149 local k="${ANON["${a1}"]}"
150 EVAL "${a2}" "${env}"
151 ENV_SET "${env}" "${k}" "${r}"
152 ENV_SET "${env}" "${k}_ismacro_" "yes"
153 return ;;
154 macroexpand)
155 MACROEXPAND "${a1}" "${env}"
156 return ;;
157 sh*) MACROEXPAND "${a1}" "${env}"
158 EVAL "${r}" "${env}"
159 local output=""
160 local line=""
161 while read line; do
162 output="${output}${line}\n"
163 done < <(eval ${ANON["${r}"]})
ea81a808 164 _string "${output}"
31690700
JM
165 return ;;
166 try*) MACROEXPAND "${a1}" "${env}"
167 EVAL "${r}" "${env}"
168 [[ -z "${__ERROR}" ]] && return
169 _nth "${a2}" 0; local a20="${r}"
170 if [ "${ANON["${a20}"]}" == "catch__STAR__" ]; then
171 _nth "${a2}" 1; local a21="${r}"
172 _nth "${a2}" 2; local a22="${r}"
ea81a808 173 _list "${a21}"; local binds="${r}"
31690700
JM
174 ENV "${env}" "${binds}" "${__ERROR}"
175 local try_env="${r}"
176 __ERROR=
177 MACROEXPAND "${a22}" "${try_env}"
178 EVAL "${r}" "${try_env}"
179 fi # if no catch* clause, just propagate __ERROR
180 return ;;
181 do) _count "${ast}"
182 _slice "${ast}" 1 $(( ${r} - 2 ))
183 EVAL_AST "${r}" "${env}"
184 [[ "${__ERROR}" ]] && r= && return 1
8cb5cda4 185 _last "${ast}"
31690700
JM
186 ast="${r}"
187 # Continue loop
188 ;;
189 if) EVAL "${a1}" "${env}"
190 if [[ "${r}" == "${__false}" || "${r}" == "${__nil}" ]]; then
191 # eval false form
192 _nth "${ast}" 3; local a3="${r}"
193 if [[ "${a3}" ]]; then
194 ast="${a3}"
195 else
196 r="${__nil}"
197 return
198 fi
199 else
200 # eval true condition
201 ast="${a2}"
202 fi
203 # Continue loop
204 ;;
ea81a808
JM
205 fn*) _function "ENV \"${env}\" \"${a1}\" \"\${@}\"; \
206 EVAL \"${a2}\" \"\${r}\"" \
207 "${a2}" "${env}" "${a1}"
31690700
JM
208 return ;;
209 *) EVAL_AST "${ast}" "${env}"
210 [[ "${__ERROR}" ]] && r= && return 1
211 local el="${r}"
8cb5cda4
JM
212 _first "${el}"; local f="${ANON["${r}"]}"
213 _rest "${el}"; local args="${ANON["${r}"]}"
31690700
JM
214 #echo "invoke: [${f}] ${args}"
215 if [[ "${f//@/ }" != "${f}" ]]; then
216 set -- ${f//@/ }
217 ast="${2}"
218 ENV "${3}" "${4}" ${args}
219 env="${r}"
220 else
221 eval ${f%%@*} ${args}
222 return
223 fi
224 # Continue loop
225 ;;
226 esac
227 done
228}
ea81a808 229
86b689f3 230# print
31690700
JM
231PRINT () {
232 if [[ "${__ERROR}" ]]; then
233 _pr_str "${__ERROR}" yes
234 r="Error: ${r}"
235 __ERROR=
236 else
237 _pr_str "${1}" yes
238 fi
239}
240
86b689f3 241# repl
31690700
JM
242ENV; REPL_ENV="${r}"
243REP () {
244 r=
8cb5cda4 245 READ "${1}" || return 1
31690700
JM
246 EVAL "${r}" "${REPL_ENV}"
247 PRINT "${r}"
248}
249
8cb5cda4 250# core.sh: defined using bash
ea81a808 251_fref () { _function "${2} \"\${@}\""; ENV_SET "${REPL_ENV}" "${1}" "${r}"; }
ea81a808 252for n in "${!core_ns[@]}"; do _fref "${n}" "${core_ns["${n}"]}"; done
ea81a808 253_eval () { EVAL "${1}" "${REPL_ENV}"; }
31690700 254_fref "eval" _eval
86b689f3
JM
255_list; argv="${r}"
256for _arg in "${@:2}"; do _string "${_arg}"; _conj! "${argv}" "${r}"; done
257ENV_SET "${REPL_ENV}" "__STAR__ARGV__STAR__" "${argv}";
31690700 258
8cb5cda4 259# core.mal: defined using the language itself
db4c329a 260REP "(def! *host-language* \"bash\")"
31690700 261REP "(def! not (fn* (a) (if a false true)))"
8cb5cda4 262REP "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"
31690700
JM
263REP "(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)))))))"
264REP "(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 265
86b689f3 266# load/run file from command line (then exit)
31690700 267if [[ "${1}" ]]; then
86b689f3
JM
268 REP "(load-file \"${1}\")"
269 exit 0
270fi
271
272# repl loop
273REP "(println (str \"Mal [\" *host-language* \"]\"))"
274while true; do
275 READLINE "user> " || exit "$?"
276 [[ "${r}" ]] && REP "${r}" && echo "${r}"
277done