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 _nth
"${ast}" 0; local a0
="${r}"
56 _nth
"${ast}" 1; local a1
="${r}"
57 _nth
"${ast}" 2; local a2
="${r}"
58 case "${ANON["${a0}"]}" in
59 def
!) EVAL
"${a2}" "${env}"
60 [[ "${__ERROR}" ]] && return 1
61 ENV_SET
"${env}" "${a1}" "${r}"
63 let*) ENV
"${env}"; local let_env
="${r}"
64 local let_pairs
=(${ANON["${a1}"]})
66 #echo "let: [${let_pairs[*]}] for ${a2}"
67 while [[ "${let_pairs["${idx}"]}" ]]; do
68 EVAL
"${let_pairs[$(( idx + 1))]}" "${let_env}"
69 ENV_SET
"${let_env}" "${let_pairs[${idx}]}" "${r}"
77 _slice
"${ast}" 1 $
(( ${r} - 2 ))
78 EVAL_AST
"${r}" "${env}"
79 [[ "${__ERROR}" ]] && r
= && return 1
84 if) EVAL
"${a1}" "${env}"
85 [[ "${__ERROR}" ]] && return 1
86 if [[ "${r}" == "${__false}" || "${r}" == "${__nil}" ]]; then
88 _nth
"${ast}" 3; local a3
="${r}"
89 if [[ "${a3}" ]]; then
101 fn
*) _function
"ENV \"${env}\" \"${a1}\" \"\${@}\"; \
102 EVAL \"${a2}\" \"\${r}\"" \
103 "${a2}" "${env}" "${a1}"
105 *) EVAL_AST
"${ast}" "${env}"
106 [[ "${__ERROR}" ]] && r
= && return 1
108 _first
"${el}"; local f="${ANON["${r}"]}"
109 _rest
"${el}"; local args="${ANON["${r}"]}"
110 #echo "invoke: [${f}] ${args}"
111 if [[ "${f//@/ }" != "${f}" ]]; then
114 ENV
"${3}" "${4}" ${args}
117 eval ${f%%@*} ${args}
128 if [[ "${__ERROR}" ]]; then
129 _pr_str "${__ERROR}" yes
142 EVAL "${r}" "${REPL_ENV}"
146 # core.sh: defined using bash
148 _symbol "${1}"; local sym="${r}"
149 _function "${2} \"\
${@}\""
150 ENV_SET "${REPL_ENV}" "${sym}" "${r}"
152 for n in "${!core_ns[@]}"; do _fref "${n}" "${core_ns["${n}"]}"; done
154 # core.mal: defined using the language itself
155 REP "(def
! not
(fn
* (a
) (if a false true
)))"
159 READLINE "user
> " || exit "$?
"
160 [[ "${r}" ]] && REP "${r}" && echo "${r}"