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
="" 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 _nth
"${ast}" 0; local a0
="${r}"
55 _nth
"${ast}" 1; local a1
="${r}"
56 _nth
"${ast}" 2; local a2
="${r}"
57 case "${ANON["${a0}"]}" in
58 def
!) EVAL
"${a2}" "${env}"
59 [[ "${__ERROR}" ]] && return 1
60 ENV_SET
"${env}" "${a1}" "${r}"
62 let*) ENV
"${env}"; local let_env
="${r}"
63 local let_pairs
=(${ANON["${a1}"]})
65 #echo "let: [${let_pairs[*]}] for ${a2}"
66 while [[ "${let_pairs["${idx}"]}" ]]; do
67 EVAL
"${let_pairs[$(( idx + 1))]}" "${let_env}"
68 ENV_SET
"${let_env}" "${let_pairs[${idx}]}" "${r}"
71 EVAL
"${a2}" "${let_env}"
74 EVAL_AST
"${r}" "${env}"
75 [[ "${__ERROR}" ]] && r
= && return 1
78 if) EVAL
"${a1}" "${env}"
79 [[ "${__ERROR}" ]] && return 1
80 if [[ "${r}" == "${__false}" || "${r}" == "${__nil}" ]]; then
82 _nth
"${ast}" 3; local a3
="${r}"
83 if [[ "${a3}" ]]; then
93 fn
*) _function
"ENV \"${env}\" \"${a1}\" \"\${@}\"; \
94 EVAL \"${a2}\" \"\${r}\""
96 *) EVAL_AST
"${ast}" "${env}"
97 [[ "${__ERROR}" ]] && r
= && return 1
99 _first
"${el}"; local f="${ANON["${r}"]}"
100 _rest
"${el}"; local args="${ANON["${r}"]}"
101 #echo "invoke: ${f} ${args}"
109 if [[ "${__ERROR}" ]]; then
110 _pr_str
"${__ERROR}" yes
123 EVAL
"${r}" "${REPL_ENV}"
127 # core.sh: defined using bash
129 _symbol
"${1}"; local sym
="${r}"
130 _function
"${2} \"\${@}\""
131 ENV_SET
"${REPL_ENV}" "${sym}" "${r}"
133 for n
in "${!core_ns[@]}"; do _fref "${n}" "${core_ns["${n}"]}"; done
135 # core.mal: defined using the language itself
136 REP
"(def! not (fn* (a) (if a false true)))"
140 READLINE
"user> " ||
exit "$?"
141 [[ "${r}" ]] && REP "${r}" && echo "${r}"