3 source $
(dirname $0)/reader.sh
4 source $
(dirname $0)/printer.sh
5 source $
(dirname $0)/env.sh
6 source $
(dirname $0)/core.sh
10 [ "${1}" ] && r
="${1}" || READLINE
16 if _sequential?
"${1}"; then
18 [[ "${r}" > 0 ]] && return 0
24 if ! IS_PAIR
"${1}"; then
29 _nth
"${1}" 0; local a0
="${r}"
30 if [[ "${ANON["${a0}"]}" == "unquote" ]]; then
33 elif IS_PAIR
"${a0}"; then
34 _nth
"${a0}" 0; local a00
="${r}"
35 if [[ "${ANON["${a00}"]}" == "splice-unquote" ]]; then
36 _symbol concat
; local a
="${r}"
37 _nth
"${a0}" 1; local b
="${r}"
39 QUASIQUOTE
"${r}"; local c
="${r}"
40 _list
"${a}" "${b}" "${c}"
45 _symbol cons
; local a
="${r}"
46 QUASIQUOTE
"${a0}"; local b
="${r}"
48 QUASIQUOTE
"${r}"; local c
="${r}"
49 _list
"${a}" "${b}" "${c}"
54 if ! _list?
"${1}"; then return 1; fi
55 _nth
"${1}" 0; local a0
="${r}"
56 if _symbol?
"${a0}"; then
57 ENV_FIND
"${2}" "${a0}"
59 ENV_GET
"${2}" "${a0}"
60 [ "${ANON["${r}_ismacro_"]}" ]
68 local ast
="${1}" env
="${2}"
69 while IS_MACRO_CALL
"${ast}" "${env}"; do
70 _nth
"${ast}" 0; local a0
="${r}"
71 ENV_GET
"${env}" "${a0}"; local mac="${ANON["${r}"]}"
73 ${mac%%@*} ${ANON["${r}"]}
81 local ast
="${1}" env
="${2}"
82 #_pr_str "${ast}"; echo "EVAL_AST '${ast}:${r} / ${env}'"
83 _obj_type
"${ast}"; local ot
="${r}"
86 ENV_GET
"${env}" "${ast}"
89 _map_with_type _list EVAL
"${ast}" "${env}" ;;
91 _map_with_type _vector EVAL
"${ast}" "${env}" ;;
93 local res
="" val
="" hm
="${ANON["${ast}"]}"
94 _hash_map
; local new_hm
="${r}"
95 eval local keys
="\${!${hm}[@]}"
96 for key
in ${keys}; do
97 eval val
="\${${hm}[\"${key}\"]}"
98 EVAL
"${val}" "${env}"
99 _assoc
! "${new_hm}" "${key}" "${r}"
108 local ast
="${1}" env
="${2}"
111 [[ "${__ERROR}" ]] && return 1
112 #_pr_str "${ast}"; echo "EVAL '${r} / ${env}'"
113 if ! _list?
"${ast}"; then
114 EVAL_AST
"${ast}" "${env}"
119 MACROEXPAND
"${ast}" "${env}"
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
126 def
!) EVAL
"${a2}" "${env}"
127 [[ "${__ERROR}" ]] && return 1
128 ENV_SET
"${env}" "${a1}" "${r}"
130 let*) ENV
"${env}"; local let_env
="${r}"
131 local let_pairs
=(${ANON["${a1}"]})
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}" "${let_pairs[${idx}]}" "${r}"
152 EVAL
"${a2}" "${env}"
153 [[ "${__ERROR}" ]] && return 1
154 ANON
["${r}_ismacro_"]="yes"
155 ENV_SET
"${env}" "${a1}" "${r}"
158 MACROEXPAND
"${a1}" "${env}"
160 try
*) EVAL
"${a1}" "${env}"
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}"
166 _list
"${a21}"; local binds
="${r}"
167 ENV
"${env}" "${binds}" "${__ERROR}"
170 EVAL
"${a22}" "${try_env}"
171 fi # if no catch* clause, just propagate __ERROR
174 _slice
"${ast}" 1 $
(( ${r} - 2 ))
175 EVAL_AST
"${r}" "${env}"
176 [[ "${__ERROR}" ]] && r
= && return 1
181 if) EVAL
"${a1}" "${env}"
182 [[ "${__ERROR}" ]] && return 1
183 if [[ "${r}" == "${__false}" || "${r}" == "${__nil}" ]]; then
185 _nth
"${ast}" 3; local a3
="${r}"
186 if [[ "${a3}" ]]; then
193 # eval true condition
198 fn
*) _function
"ENV \"${env}\" \"${a1}\" \"\${@}\"; \
199 EVAL \"${a2}\" \"\${r}\"" \
200 "${a2}" "${env}" "${a1}"
202 *) EVAL_AST
"${ast}" "${env}"
203 [[ "${__ERROR}" ]] && r
= && return 1
205 _first
"${el}"; local f="${ANON["${r}"]}"
206 _rest
"${el}"; local args="${ANON["${r}"]}"
207 #echo "invoke: [${f}] ${args}"
208 if [[ "${f//@/ }" != "${f}" ]]; then
211 ENV
"${3}" "${4}" ${args}
214 eval ${f%%@*} ${args}
225 if [[ "${__ERROR}" ]]; then
226 _pr_str "${__ERROR}" yes
239 EVAL "${r}" "${REPL_ENV}"
243 # core.sh: defined using bash
245 _symbol "${1}"; local sym="${r}"
246 _function "${2} \"\
${@}\""
247 ENV_SET "${REPL_ENV}" "${sym}" "${r}"
249 for n in "${!core_ns[@]}"; do _fref "${n}" "${core_ns["${n}"]}"; done
250 _eval () { EVAL "${1}" "${REPL_ENV}"; }
253 for _arg
in "${@:2}"; do _string "${_arg}"; _conj! "${argv}" "${r}"; done
254 _symbol
"__STAR__ARGV__STAR__"
255 ENV_SET
"${REPL_ENV}" "${r}" "${argv}";
257 # core.mal: defined using the language itself
258 REP
"(def! not (fn* (a) (if a false true)))"
259 REP
"(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"
260 REP
"(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)))))))"
261 REP
"(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))))))))"
263 # load/run file from command line (then exit)
264 if [[ "${1}" ]]; then
265 REP
"(load-file \"${1}\")"
271 READLINE
"user> " ||
exit "$?"
272 [[ "${r}" ]] && REP "${r}" && echo "${r}"