3 INTERACTIVE
=${INTERACTIVE-yes}
5 source $
(dirname $0)/reader.sh
6 source $
(dirname $0)/printer.sh
7 source $
(dirname $0)/env.sh
8 source $
(dirname $0)/core.sh
10 # READ: read and parse input
12 [ "${1}" ] && r
="${1}" || READLINE
17 if _list?
"${1}"; then
19 [[ "${r}" > 0 ]] && return 0
25 if ! IS_PAIR
"${1}"; then
30 _nth
"${1}" 0; local a0
="${r}"
31 if [[ "${ANON["${a0}"]}" == "unquote" ]]; then
34 elif IS_PAIR
"${a0}"; then
35 _nth
"${a0}" 0; local a00
="${r}"
36 if [[ "${ANON["${a00}"]}" == "splice-unquote" ]]; then
37 _symbol concat
; local a
="${r}"
38 _nth
"${a0}" 1; local b
="${r}"
40 QUASIQUOTE
"${r}"; local c
="${r}"
41 _list
"${a}" "${b}" "${c}"
46 _symbol cons
; local a
="${r}"
47 QUASIQUOTE
"${a0}"; local b
="${r}"
49 QUASIQUOTE
"${r}"; local c
="${r}"
50 _list
"${a}" "${b}" "${c}"
55 if ! _list?
"${1}"; then return 1; fi
56 _nth
"${1}" 0; local a0
="${r}"
57 if _symbol?
"${a0}"; then
58 ENV_FIND
"${2}" "${ANON["${a0}"]}_ismacro_"
67 local ast
="${1}" env
="${2}"
68 while IS_MACRO_CALL
"${ast}" "${env}"; do
69 _nth
"${ast}" 0; local a0
="${r}"
70 ENV_GET
"${env}" "${ANON["${a0}"]}"; local mac="${ANON["${r}"]}"
72 ${mac%%@*} ${ANON["${r}"]}
80 local ast
="${1}" env
="${2}"
81 #_pr_str "${ast}"; echo "EVAL_AST '${ast}:${r} / ${env}'"
82 _obj_type
"${ast}"; local ot
="${r}"
85 local val
="${ANON["${ast}"]}"
86 ENV_GET
"${env}" "${val}"
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}"
107 # EVAL: evaluate the parameter
109 local ast
="${1}" env
="${2}"
112 [[ "${__ERROR}" ]] && return 1
113 #_pr_str "${ast}"; echo "EVAL '${r} / ${env}'"
114 if ! _list?
"${ast}"; then
115 EVAL_AST
"${ast}" "${env}"
120 MACROEXPAND
"${ast}" "${env}"
122 if ! _list?
"${ast}"; then return; fi
123 _nth
"${ast}" 0; local a0
="${r}"
124 _nth
"${ast}" 1; local a1
="${r}"
125 _nth
"${ast}" 2; local a2
="${r}"
126 case "${ANON["${a0}"]}" in
127 def
!) local k
="${ANON["${a1}"]}"
128 #echo "def! ${k} to ${a2} in ${env}"
129 EVAL
"${a2}" "${env}"
130 ENV_SET
"${env}" "${k}" "${r}"
132 let*) ENV
"${env}"; local let_env
="${r}"
133 local let_pairs
=(${ANON["${a1}"]})
135 #echo "let: [${let_pairs[*]}] for ${a2}"
136 while [[ "${let_pairs["${idx}"]}" ]]; do
137 EVAL
"${let_pairs[$(( idx + 1))]}" "${let_env}"
138 ENV_SET
"${let_env}" "${ANON["${let_pairs[${idx}]}"]}" "${r}"
141 EVAL "${a2}" "${let_env}"
151 local k="${ANON["${a1}"]}"
152 EVAL "${a2}" "${env}"
153 ENV_SET "${env}" "${k}" "${r}"
154 ENV_SET "${env}" "${k}_ismacro_
" "yes"
157 MACROEXPAND "${a1}" "${env}"
159 sh*) MACROEXPAND "${a1}" "${env}"
164 output="${output}${line}\n"
165 done < <(eval ${ANON["${r}"]})
169 _slice "${ast}" 1 $(( ${r} - 2 ))
170 EVAL_AST "${r}" "${env}"
171 [[ "${__ERROR}" ]] && r= && return 1
176 if) EVAL "${a1}" "${env}"
177 if [[ "${r}" == "${__false}" || "${r}" == "${__nil}" ]]; then
179 _nth "${ast}" 3; local a3="${r}"
180 if [[ "${a3}" ]]; then
187 # eval true condition
192 fn*) _function "ENV
\"${env}\" \"${a1}\" \"\${@}\"; \
193 EVAL
\"${a2}\" \"\
${r}\"" \
194 "${a2}" "${env}" "${a1}"
196 *) EVAL_AST "${ast}" "${env}"
197 [[ "${__ERROR}" ]] && r= && return 1
199 _first "${el}"; local f="${ANON["${r}"]}"
200 _rest "${el}"; local args="${ANON["${r}"]}"
201 #echo "invoke
: [${f}] ${args}"
202 if [[ "${f//@/ }" != "${f}" ]]; then
205 ENV "${3}" "${4}" ${args}
208 eval ${f%%@*} ${args}
219 if [[ "${__ERROR}" ]]; then
220 _pr_str
"${__ERROR}" yes
228 # REPL: read, eval, print, loop
232 READ
"${1}" ||
return 1
233 EVAL
"${r}" "${REPL_ENV}"
237 # core.sh: defined using bash
238 _fref
() { _function
"${2} \"\${@}\""; ENV_SET "${REPL_ENV}" "${1}" "${r}"; }
239 for n in "${!core_ns[@]}"; do _fref "${n}" "${core_ns["${n}"]}"; done
240 _eval () { EVAL "${1}" "${REPL_ENV}"; }
243 # core.mal: defined using the language itself
244 REP
"(def! not (fn* (a) (if a false true)))"
245 REP
"(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"
246 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)))))))"
247 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))))))))"
249 if [[ "${1}" ]]; then
251 REP
"(load-file \"${1}\")" && echo "${r}"
252 elif [[ -n "${INTERACTIVE}" ]]; then
254 READLINE
"user> " ||
exit "$?"
255 [[ "${r}" ]] && REP "${r}" && echo "${r}"