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 local ast
="${1}" env
="${2}"
17 #_pr_str "${ast}"; echo "EVAL_AST '${ast}:${r} / ${env}'"
18 _obj_type
"${ast}"; local ot
="${r}"
21 ENV_GET
"${env}" "${ast}"
24 _map_with_type _list EVAL
"${ast}" "${env}" ;;
26 _map_with_type _vector EVAL
"${ast}" "${env}" ;;
28 local res
="" key
= val
="" hm
="${ANON["${ast}"]}"
29 _hash_map
; local new_hm
="${r}"
30 eval local keys
="\${!${hm}[@]}"
31 for key
in ${keys}; do
32 eval val
="\${${hm}[\"${key}\"]}"
33 EVAL
"${val}" "${env}"
34 _assoc
! "${new_hm}" "${key}" "${r}"
43 local ast
="${1}" env
="${2}"
46 [[ "${__ERROR}" ]] && return 1
47 #_pr_str "${ast}"; echo "EVAL '${r} / ${env}'"
48 _obj_type
"${ast}"; local ot
="${r}"
49 if [[ "${ot}" != "list" ]]; then
50 EVAL_AST
"${ast}" "${env}"
55 _empty?
"${ast}" && r
="${ast}" && return
57 _nth
"${ast}" 0; local a0
="${r}"
58 _nth
"${ast}" 1; local a1
="${r}"
59 _nth
"${ast}" 2; local a2
="${r}"
60 case "${ANON["${a0}"]}" in
61 def
!) EVAL
"${a2}" "${env}"
62 [[ "${__ERROR}" ]] && return 1
63 ENV_SET
"${env}" "${a1}" "${r}"
65 let__STAR__
) ENV
"${env}"; local let_env
="${r}"
66 local let_pairs
=(${ANON["${a1}"]})
68 #echo "let: [${let_pairs[*]}] for ${a2}"
69 while [[ "${let_pairs["${idx}"]}" ]]; do
70 EVAL
"${let_pairs[$(( idx + 1))]}" "${let_env}"
71 ENV_SET
"${let_env}" "${let_pairs[${idx}]}" "${r}"
79 _slice
"${ast}" 1 $
(( ${r} - 2 ))
80 EVAL_AST
"${r}" "${env}"
81 [[ "${__ERROR}" ]] && r
= && return 1
86 if) EVAL
"${a1}" "${env}"
87 [[ "${__ERROR}" ]] && return 1
88 if [[ "${r}" == "${__false}" || "${r}" == "${__nil}" ]]; then
90 _nth
"${ast}" 3; local a3
="${r}"
91 if [[ "${a3}" ]]; then
103 fn__STAR__
) _function
"ENV \"${env}\" \"${a1}\" \"\${@}\"; \
104 EVAL \"${a2}\" \"\${r}\"" \
105 "${a2}" "${env}" "${a1}"
107 *) EVAL_AST
"${ast}" "${env}"
108 [[ "${__ERROR}" ]] && r
= && return 1
110 _first
"${el}"; local f="${ANON["${r}"]}"
111 _rest
"${el}"; local args="${ANON["${r}"]}"
112 #echo "invoke: [${f}] ${args}"
113 if [[ "${f//@/ }" != "${f}" ]]; then
116 ENV
"${3}" "${4}" ${args}
119 eval ${f%%@*} ${args}
130 if [[ "${__ERROR}" ]]; then
131 _pr_str "${__ERROR}" yes
144 EVAL "${r}" "${REPL_ENV}"
148 # core.sh: defined using bash
150 _symbol "${1}"; local sym="${r}"
151 _function "${2} \"\
${@}\""
152 ENV_SET "${REPL_ENV}" "${sym}" "${r}"
154 for n in "${!core_ns[@]}"; do _fref "${n}" "${core_ns["${n}"]}"; done
155 _eval () { EVAL "${1}" "${REPL_ENV}"; }
158 for _arg
in "${@:2}"; do _string "${_arg}"; _conj! "${argv}" "${r}"; done
159 _symbol
"__STAR__ARGV__STAR__"
160 ENV_SET
"${REPL_ENV}" "${r}" "${argv}";
162 # core.mal: defined using the language itself
163 REP
"(def! not (fn* (a) (if a false true)))"
164 REP
"(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))"
166 # load/run file from command line (then exit)
167 if [[ "${1}" ]]; then
168 REP
"(load-file \"${1}\")"
174 READLINE
"user> " ||
exit "$?"
175 [[ "${r}" ]] && REP "${r}" && echo "${r}"