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}"
45 [[ "${__ERROR}" ]] && return 1
46 #_pr_str "${ast}"; echo "EVAL '${r} / ${env}'"
47 _obj_type
"${ast}"; local ot
="${r}"
48 if [[ "${ot}" != "list" ]]; then
49 EVAL_AST
"${ast}" "${env}"
54 _empty?
"${ast}" && r
="${ast}" && return
56 _nth
"${ast}" 0; local a0
="${r}"
57 _nth
"${ast}" 1; local a1
="${r}"
58 _nth
"${ast}" 2; local a2
="${r}"
59 case "${ANON["${a0}"]}" in
60 def
!) EVAL
"${a2}" "${env}"
61 [[ "${__ERROR}" ]] && return 1
62 ENV_SET
"${env}" "${a1}" "${r}"
64 let__STAR__
) ENV
"${env}"; local let_env
="${r}"
65 local let_pairs
=(${ANON["${a1}"]})
67 #echo "let: [${let_pairs[*]}] for ${a2}"
68 while [[ "${let_pairs["${idx}"]}" ]]; do
69 EVAL
"${let_pairs[$(( idx + 1))]}" "${let_env}"
70 ENV_SET
"${let_env}" "${let_pairs[${idx}]}" "${r}"
73 EVAL
"${a2}" "${let_env}"
76 EVAL_AST
"${r}" "${env}"
77 [[ "${__ERROR}" ]] && r
= && return 1
80 if) EVAL
"${a1}" "${env}"
81 [[ "${__ERROR}" ]] && return 1
82 if [[ "${r}" == "${__false}" || "${r}" == "${__nil}" ]]; then
84 _nth
"${ast}" 3; local a3
="${r}"
85 if [[ "${a3}" ]]; then
95 fn__STAR__
) _function
"ENV \"${env}\" \"${a1}\" \"\${@}\"; \
96 EVAL \"${a2}\" \"\${r}\""
98 *) EVAL_AST
"${ast}" "${env}"
99 [[ "${__ERROR}" ]] && r
= && return 1
101 _first
"${el}"; local f="${ANON["${r}"]}"
102 _rest
"${el}"; local args="${ANON["${r}"]}"
103 #echo "invoke: ${f} ${args}"
111 if [[ "${__ERROR}" ]]; then
112 _pr_str
"${__ERROR}" yes
125 EVAL
"${r}" "${REPL_ENV}"
129 # core.sh: defined using bash
131 _symbol
"${1}"; local sym
="${r}"
132 _function
"${2} \"\${@}\""
133 ENV_SET
"${REPL_ENV}" "${sym}" "${r}"
135 for n
in "${!core_ns[@]}"; do _fref "${n}" "${core_ns["${n}"]}"; done
137 # core.mal: defined using the language itself
138 REP
"(def! not (fn* (a) (if a false true)))"
142 READLINE
"user> " ||
exit "$?"
143 [[ "${r}" ]] && REP "${r}" && echo "${r}"