Commit | Line | Data |
---|---|---|
7838e339 | 1 | #!/usr/bin/env bash |
31690700 | 2 | |
31690700 | 3 | source $(dirname $0)/reader.sh |
ea81a808 | 4 | source $(dirname $0)/printer.sh |
ea81a808 | 5 | source $(dirname $0)/env.sh |
8cb5cda4 | 6 | source $(dirname $0)/core.sh |
31690700 | 7 | |
86b689f3 | 8 | # read |
31690700 | 9 | READ () { |
8cb5cda4 | 10 | [ "${1}" ] && r="${1}" || READLINE |
31690700 JM |
11 | READ_STR "${r}" |
12 | } | |
13 | ||
86b689f3 | 14 | # eval |
31690700 | 15 | IS_PAIR () { |
db4c329a | 16 | if _sequential? "${1}"; then |
31690700 JM |
17 | _count "${1}" |
18 | [[ "${r}" > 0 ]] && return 0 | |
19 | fi | |
20 | return 1 | |
21 | } | |
22 | ||
23 | QUASIQUOTE () { | |
24 | if ! IS_PAIR "${1}"; then | |
ea81a808 JM |
25 | _symbol quote |
26 | _list "${r}" "${1}" | |
31690700 JM |
27 | return |
28 | else | |
29 | _nth "${1}" 0; local a0="${r}" | |
30 | if [[ "${ANON["${a0}"]}" == "unquote" ]]; then | |
31 | _nth "${1}" 1 | |
32 | return | |
33 | elif IS_PAIR "${a0}"; then | |
34 | _nth "${a0}" 0; local a00="${r}" | |
35 | if [[ "${ANON["${a00}"]}" == "splice-unquote" ]]; then | |
ea81a808 | 36 | _symbol concat; local a="${r}" |
31690700 | 37 | _nth "${a0}" 1; local b="${r}" |
8cb5cda4 | 38 | _rest "${1}" |
31690700 | 39 | QUASIQUOTE "${r}"; local c="${r}" |
ea81a808 | 40 | _list "${a}" "${b}" "${c}" |
31690700 JM |
41 | return |
42 | fi | |
43 | fi | |
44 | fi | |
ea81a808 | 45 | _symbol cons; local a="${r}" |
31690700 | 46 | QUASIQUOTE "${a0}"; local b="${r}" |
8cb5cda4 | 47 | _rest "${1}" |
31690700 | 48 | QUASIQUOTE "${r}"; local c="${r}" |
ea81a808 | 49 | _list "${a}" "${b}" "${c}" |
31690700 JM |
50 | return |
51 | } | |
52 | ||
53 | EVAL_AST () { | |
54 | local ast="${1}" env="${2}" | |
55 | #_pr_str "${ast}"; echo "EVAL_AST '${ast}:${r} / ${env}'" | |
56 | _obj_type "${ast}"; local ot="${r}" | |
57 | case "${ot}" in | |
58 | symbol) | |
b8ee29b2 | 59 | ENV_GET "${env}" "${ast}" |
31690700 JM |
60 | return ;; |
61 | list) | |
ea81a808 | 62 | _map_with_type _list EVAL "${ast}" "${env}" ;; |
31690700 | 63 | vector) |
ea81a808 | 64 | _map_with_type _vector EVAL "${ast}" "${env}" ;; |
31690700 JM |
65 | hash_map) |
66 | local res="" val="" hm="${ANON["${ast}"]}" | |
ea81a808 | 67 | _hash_map; local new_hm="${r}" |
31690700 JM |
68 | eval local keys="\${!${hm}[@]}" |
69 | for key in ${keys}; do | |
70 | eval val="\${${hm}[\"${key}\"]}" | |
71 | EVAL "${val}" "${env}" | |
ea81a808 | 72 | _assoc! "${new_hm}" "${key}" "${r}" |
31690700 JM |
73 | done |
74 | r="${new_hm}" ;; | |
75 | *) | |
76 | r="${ast}" ;; | |
77 | esac | |
78 | } | |
79 | ||
31690700 JM |
80 | EVAL () { |
81 | local ast="${1}" env="${2}" | |
82 | while true; do | |
83 | r= | |
84 | [[ "${__ERROR}" ]] && return 1 | |
85 | #_pr_str "${ast}"; echo "EVAL '${r} / ${env}'" | |
b8ee29b2 | 86 | if ! _list? "${ast}"; then |
31690700 JM |
87 | EVAL_AST "${ast}" "${env}" |
88 | return | |
89 | fi | |
90 | ||
91 | # apply list | |
92 | _nth "${ast}" 0; local a0="${r}" | |
93 | _nth "${ast}" 1; local a1="${r}" | |
94 | _nth "${ast}" 2; local a2="${r}" | |
95 | case "${ANON["${a0}"]}" in | |
b8ee29b2 JM |
96 | def!) EVAL "${a2}" "${env}" |
97 | [[ "${__ERROR}" ]] && return 1 | |
98 | ENV_SET "${env}" "${a1}" "${r}" | |
31690700 JM |
99 | return ;; |
100 | let*) ENV "${env}"; local let_env="${r}" | |
101 | local let_pairs=(${ANON["${a1}"]}) | |
102 | local idx=0 | |
103 | #echo "let: [${let_pairs[*]}] for ${a2}" | |
104 | while [[ "${let_pairs["${idx}"]}" ]]; do | |
105 | EVAL "${let_pairs[$(( idx + 1))]}" "${let_env}" | |
b8ee29b2 | 106 | ENV_SET "${let_env}" "${let_pairs[${idx}]}" "${r}" |
31690700 JM |
107 | idx=$(( idx + 2)) |
108 | done | |
6301e0b6 JM |
109 | ast="${a2}" |
110 | env="${let_env}" | |
111 | # Continue loop | |
112 | ;; | |
31690700 JM |
113 | quote) |
114 | r="${a1}" | |
115 | return ;; | |
116 | quasiquote) | |
117 | QUASIQUOTE "${a1}" | |
6301e0b6 JM |
118 | ast="${r}" |
119 | # Continue loop | |
120 | ;; | |
31690700 JM |
121 | do) _count "${ast}" |
122 | _slice "${ast}" 1 $(( ${r} - 2 )) | |
123 | EVAL_AST "${r}" "${env}" | |
124 | [[ "${__ERROR}" ]] && r= && return 1 | |
8cb5cda4 | 125 | _last "${ast}" |
31690700 JM |
126 | ast="${r}" |
127 | # Continue loop | |
128 | ;; | |
129 | if) EVAL "${a1}" "${env}" | |
b8ee29b2 | 130 | [[ "${__ERROR}" ]] && return 1 |
31690700 JM |
131 | if [[ "${r}" == "${__false}" || "${r}" == "${__nil}" ]]; then |
132 | # eval false form | |
133 | _nth "${ast}" 3; local a3="${r}" | |
134 | if [[ "${a3}" ]]; then | |
135 | ast="${a3}" | |
136 | else | |
137 | r="${__nil}" | |
138 | return | |
139 | fi | |
140 | else | |
141 | # eval true condition | |
142 | ast="${a2}" | |
143 | fi | |
144 | # Continue loop | |
145 | ;; | |
ea81a808 JM |
146 | fn*) _function "ENV \"${env}\" \"${a1}\" \"\${@}\"; \ |
147 | EVAL \"${a2}\" \"\${r}\"" \ | |
148 | "${a2}" "${env}" "${a1}" | |
31690700 JM |
149 | return ;; |
150 | *) EVAL_AST "${ast}" "${env}" | |
151 | [[ "${__ERROR}" ]] && r= && return 1 | |
152 | local el="${r}" | |
8cb5cda4 JM |
153 | _first "${el}"; local f="${ANON["${r}"]}" |
154 | _rest "${el}"; local args="${ANON["${r}"]}" | |
31690700 JM |
155 | #echo "invoke: [${f}] ${args}" |
156 | if [[ "${f//@/ }" != "${f}" ]]; then | |
157 | set -- ${f//@/ } | |
158 | ast="${2}" | |
159 | ENV "${3}" "${4}" ${args} | |
160 | env="${r}" | |
161 | else | |
162 | eval ${f%%@*} ${args} | |
163 | return | |
164 | fi | |
165 | # Continue loop | |
166 | ;; | |
167 | esac | |
168 | done | |
169 | } | |
170 | ||
86b689f3 | 171 | |
31690700 JM |
172 | PRINT () { |
173 | if [[ "${__ERROR}" ]]; then | |
174 | _pr_str "${__ERROR}" yes | |
175 | r="Error: ${r}" | |
176 | __ERROR= | |
177 | else | |
178 | _pr_str "${1}" yes | |
179 | fi | |
180 | } | |
181 | ||
86b689f3 | 182 | # repl |
31690700 JM |
183 | ENV; REPL_ENV="${r}" |
184 | REP () { | |
185 | r= | |
70aff0c1 | 186 | READ "${1}" |
ea81a808 | 187 | EVAL "${r}" "${REPL_ENV}" |
31690700 JM |
188 | PRINT "${r}" |
189 | } | |
190 | ||
8cb5cda4 | 191 | # core.sh: defined using bash |
b8ee29b2 JM |
192 | _fref () { |
193 | _symbol "${1}"; local sym="${r}" | |
194 | _function "${2} \"\${@}\"" | |
195 | ENV_SET "${REPL_ENV}" "${sym}" "${r}" | |
196 | } | |
ea81a808 | 197 | for n in "${!core_ns[@]}"; do _fref "${n}" "${core_ns["${n}"]}"; done |
31690700 JM |
198 | _eval () { EVAL "${1}" "${REPL_ENV}"; } |
199 | _fref "eval" _eval | |
86b689f3 JM |
200 | _list; argv="${r}" |
201 | for _arg in "${@:2}"; do _string "${_arg}"; _conj! "${argv}" "${r}"; done | |
b8ee29b2 JM |
202 | _symbol "__STAR__ARGV__STAR__" |
203 | ENV_SET "${REPL_ENV}" "${r}" "${argv}"; | |
31690700 | 204 | |
8cb5cda4 | 205 | # core.mal: defined using the language itself |
31690700 | 206 | REP "(def! not (fn* (a) (if a false true)))" |
1617910a | 207 | REP "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))" |
31690700 | 208 | |
86b689f3 | 209 | # load/run file from command line (then exit) |
31690700 | 210 | if [[ "${1}" ]]; then |
86b689f3 JM |
211 | REP "(load-file \"${1}\")" |
212 | exit 0 | |
213 | fi | |
214 | ||
215 | # repl loop | |
216 | while true; do | |
217 | READLINE "user> " || exit "$?" | |
218 | [[ "${r}" ]] && REP "${r}" && echo "${r}" | |
219 | done |