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
="" key
= 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
122 EVAL_AST
"${ast}" "${env}"
125 _empty?
"${ast}" && r
="${ast}" && return
127 _nth
"${ast}" 0; local a0
="${r}"
128 _nth
"${ast}" 1; local a1
="${r}"
129 _nth
"${ast}" 2; local a2
="${r}"
130 case "${ANON["${a0}"]}" in
131 def
!) EVAL
"${a2}" "${env}"
132 [[ "${__ERROR}" ]] && return 1
133 ENV_SET
"${env}" "${a1}" "${r}"
135 let__STAR__
) ENV
"${env}"; local let_env
="${r}"
136 local let_pairs
=(${ANON["${a1}"]})
138 #echo "let: [${let_pairs[*]}] for ${a2}"
139 while [[ "${let_pairs["${idx}"]}" ]]; do
140 EVAL
"${let_pairs[$(( idx + 1))]}" "${let_env}"
141 ENV_SET
"${let_env}" "${let_pairs[${idx}]}" "${r}"
157 EVAL
"${a2}" "${env}"
158 [[ "${__ERROR}" ]] && return 1
159 ANON
["${r}_ismacro_"]="yes"
160 ENV_SET
"${env}" "${a1}" "${r}"
163 MACROEXPAND
"${a1}" "${env}"
166 _slice
"${ast}" 1 $
(( ${r} - 2 ))
167 EVAL_AST
"${r}" "${env}"
168 [[ "${__ERROR}" ]] && r
= && return 1
173 if) EVAL
"${a1}" "${env}"
174 [[ "${__ERROR}" ]] && return 1
175 if [[ "${r}" == "${__false}" || "${r}" == "${__nil}" ]]; then
177 _nth
"${ast}" 3; local a3
="${r}"
178 if [[ "${a3}" ]]; then
185 # eval true condition
190 fn__STAR__
) _function
"ENV \"${env}\" \"${a1}\" \"\${@}\"; \
191 EVAL \"${a2}\" \"\${r}\"" \
192 "${a2}" "${env}" "${a1}"
194 *) EVAL_AST
"${ast}" "${env}"
195 [[ "${__ERROR}" ]] && r
= && return 1
197 _first
"${el}"; local f="${ANON["${r}"]}"
198 _rest
"${el}"; local args="${ANON["${r}"]}"
199 #echo "invoke: [${f}] ${args}"
200 if [[ "${f//@/ }" != "${f}" ]]; then
203 ENV
"${3}" "${4}" ${args}
206 eval ${f%%@*} ${args}
217 if [[ "${__ERROR}" ]]; then
218 _pr_str "${__ERROR}" yes
231 EVAL "${r}" "${REPL_ENV}"
235 # core.sh: defined using bash
237 _symbol "${1}"; local sym="${r}"
238 _function "${2} \"\
${@}\""
239 ENV_SET "${REPL_ENV}" "${sym}" "${r}"
241 for n in "${!core_ns[@]}"; do _fref "${n}" "${core_ns["${n}"]}"; done
242 _eval () { EVAL "${1}" "${REPL_ENV}"; }
245 for _arg
in "${@:2}"; do _string "${_arg}"; _conj! "${argv}" "${r}"; done
246 _symbol
"__STAR__ARGV__STAR__"
247 ENV_SET
"${REPL_ENV}" "${r}" "${argv}";
249 # core.mal: defined using the language itself
250 REP
"(def! not (fn* (a) (if a false true)))"
251 REP
"(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"
252 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)))))))"
253 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))))))))"
255 # load/run file from command line (then exit)
256 if [[ "${1}" ]]; then
257 REP
"(load-file \"${1}\")"
263 READLINE
"user> " ||
exit "$?"
264 [[ "${r}" ]] && REP "${r}" && echo "${r}"