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 | IS_MACRO_CALL () { | |
54 | if ! _list? "${1}"; then return 1; fi | |
55 | _nth "${1}" 0; local a0="${r}" | |
56 | if _symbol? "${a0}"; then | |
b8ee29b2 | 57 | ENV_FIND "${2}" "${a0}" |
31690700 | 58 | if [[ "${r}" ]]; then |
b8ee29b2 JM |
59 | ENV_GET "${2}" "${a0}" |
60 | [ "${ANON["${r}_ismacro_"]}" ] | |
61 | return $? | |
31690700 JM |
62 | fi |
63 | fi | |
64 | return 1 | |
65 | } | |
66 | ||
67 | MACROEXPAND () { | |
68 | local ast="${1}" env="${2}" | |
69 | while IS_MACRO_CALL "${ast}" "${env}"; do | |
70 | _nth "${ast}" 0; local a0="${r}" | |
b8ee29b2 | 71 | ENV_GET "${env}" "${a0}"; local mac="${ANON["${r}"]}" |
8cb5cda4 | 72 | _rest "${ast}" |
31690700 JM |
73 | ${mac%%@*} ${ANON["${r}"]} |
74 | ast="${r}" | |
75 | done | |
76 | r="${ast}" | |
77 | } | |
78 | ||
79 | ||
80 | EVAL_AST () { | |
81 | local ast="${1}" env="${2}" | |
82 | #_pr_str "${ast}"; echo "EVAL_AST '${ast}:${r} / ${env}'" | |
83 | _obj_type "${ast}"; local ot="${r}" | |
84 | case "${ot}" in | |
85 | symbol) | |
b8ee29b2 | 86 | ENV_GET "${env}" "${ast}" |
31690700 JM |
87 | return ;; |
88 | list) | |
ea81a808 | 89 | _map_with_type _list EVAL "${ast}" "${env}" ;; |
31690700 | 90 | vector) |
ea81a808 | 91 | _map_with_type _vector EVAL "${ast}" "${env}" ;; |
31690700 JM |
92 | hash_map) |
93 | local res="" val="" hm="${ANON["${ast}"]}" | |
ea81a808 | 94 | _hash_map; local new_hm="${r}" |
31690700 JM |
95 | eval local keys="\${!${hm}[@]}" |
96 | for key in ${keys}; do | |
97 | eval val="\${${hm}[\"${key}\"]}" | |
98 | EVAL "${val}" "${env}" | |
ea81a808 | 99 | _assoc! "${new_hm}" "${key}" "${r}" |
31690700 JM |
100 | done |
101 | r="${new_hm}" ;; | |
102 | *) | |
103 | r="${ast}" ;; | |
104 | esac | |
105 | } | |
106 | ||
31690700 JM |
107 | EVAL () { |
108 | local ast="${1}" env="${2}" | |
109 | while true; do | |
110 | r= | |
111 | [[ "${__ERROR}" ]] && return 1 | |
112 | #_pr_str "${ast}"; echo "EVAL '${r} / ${env}'" | |
113 | if ! _list? "${ast}"; then | |
114 | EVAL_AST "${ast}" "${env}" | |
115 | return | |
116 | fi | |
117 | ||
118 | # apply list | |
119 | MACROEXPAND "${ast}" "${env}" | |
120 | ast="${r}" | |
121 | if ! _list? "${ast}"; then return; fi | |
122 | _nth "${ast}" 0; local a0="${r}" | |
123 | _nth "${ast}" 1; local a1="${r}" | |
124 | _nth "${ast}" 2; local a2="${r}" | |
125 | case "${ANON["${a0}"]}" in | |
b8ee29b2 JM |
126 | def!) EVAL "${a2}" "${env}" |
127 | [[ "${__ERROR}" ]] && return 1 | |
128 | ENV_SET "${env}" "${a1}" "${r}" | |
31690700 JM |
129 | return ;; |
130 | let*) ENV "${env}"; local let_env="${r}" | |
131 | local let_pairs=(${ANON["${a1}"]}) | |
132 | local idx=0 | |
133 | #echo "let: [${let_pairs[*]}] for ${a2}" | |
134 | while [[ "${let_pairs["${idx}"]}" ]]; do | |
135 | EVAL "${let_pairs[$(( idx + 1))]}" "${let_env}" | |
b8ee29b2 | 136 | ENV_SET "${let_env}" "${let_pairs[${idx}]}" "${r}" |
31690700 JM |
137 | idx=$(( idx + 2)) |
138 | done | |
6301e0b6 JM |
139 | ast="${a2}" |
140 | env="${let_env}" | |
141 | # Continue loop | |
142 | ;; | |
31690700 JM |
143 | quote) |
144 | r="${a1}" | |
145 | return ;; | |
146 | quasiquote) | |
147 | QUASIQUOTE "${a1}" | |
6301e0b6 JM |
148 | ast="${r}" |
149 | # Continue loop | |
150 | ;; | |
31690700 | 151 | defmacro!) |
31690700 | 152 | EVAL "${a2}" "${env}" |
b8ee29b2 JM |
153 | [[ "${__ERROR}" ]] && return 1 |
154 | ANON["${r}_ismacro_"]="yes" | |
155 | ENV_SET "${env}" "${a1}" "${r}" | |
31690700 JM |
156 | return ;; |
157 | macroexpand) | |
158 | MACROEXPAND "${a1}" "${env}" | |
159 | return ;; | |
160 | do) _count "${ast}" | |
161 | _slice "${ast}" 1 $(( ${r} - 2 )) | |
162 | EVAL_AST "${r}" "${env}" | |
163 | [[ "${__ERROR}" ]] && r= && return 1 | |
8cb5cda4 | 164 | _last "${ast}" |
31690700 JM |
165 | ast="${r}" |
166 | # Continue loop | |
167 | ;; | |
168 | if) EVAL "${a1}" "${env}" | |
b8ee29b2 | 169 | [[ "${__ERROR}" ]] && return 1 |
31690700 JM |
170 | if [[ "${r}" == "${__false}" || "${r}" == "${__nil}" ]]; then |
171 | # eval false form | |
172 | _nth "${ast}" 3; local a3="${r}" | |
173 | if [[ "${a3}" ]]; then | |
174 | ast="${a3}" | |
175 | else | |
176 | r="${__nil}" | |
177 | return | |
178 | fi | |
179 | else | |
180 | # eval true condition | |
181 | ast="${a2}" | |
182 | fi | |
183 | # Continue loop | |
184 | ;; | |
ea81a808 JM |
185 | fn*) _function "ENV \"${env}\" \"${a1}\" \"\${@}\"; \ |
186 | EVAL \"${a2}\" \"\${r}\"" \ | |
187 | "${a2}" "${env}" "${a1}" | |
31690700 JM |
188 | return ;; |
189 | *) EVAL_AST "${ast}" "${env}" | |
190 | [[ "${__ERROR}" ]] && r= && return 1 | |
191 | local el="${r}" | |
8cb5cda4 JM |
192 | _first "${el}"; local f="${ANON["${r}"]}" |
193 | _rest "${el}"; local args="${ANON["${r}"]}" | |
31690700 JM |
194 | #echo "invoke: [${f}] ${args}" |
195 | if [[ "${f//@/ }" != "${f}" ]]; then | |
196 | set -- ${f//@/ } | |
197 | ast="${2}" | |
198 | ENV "${3}" "${4}" ${args} | |
199 | env="${r}" | |
200 | else | |
201 | eval ${f%%@*} ${args} | |
202 | return | |
203 | fi | |
204 | # Continue loop | |
205 | ;; | |
206 | esac | |
207 | done | |
208 | } | |
ea81a808 | 209 | |
86b689f3 | 210 | |
31690700 JM |
211 | PRINT () { |
212 | if [[ "${__ERROR}" ]]; then | |
213 | _pr_str "${__ERROR}" yes | |
214 | r="Error: ${r}" | |
215 | __ERROR= | |
216 | else | |
217 | _pr_str "${1}" yes | |
218 | fi | |
219 | } | |
220 | ||
86b689f3 | 221 | # repl |
31690700 JM |
222 | ENV; REPL_ENV="${r}" |
223 | REP () { | |
224 | r= | |
70aff0c1 | 225 | READ "${1}" |
31690700 JM |
226 | EVAL "${r}" "${REPL_ENV}" |
227 | PRINT "${r}" | |
228 | } | |
229 | ||
8cb5cda4 | 230 | # core.sh: defined using bash |
b8ee29b2 JM |
231 | _fref () { |
232 | _symbol "${1}"; local sym="${r}" | |
233 | _function "${2} \"\${@}\"" | |
234 | ENV_SET "${REPL_ENV}" "${sym}" "${r}" | |
235 | } | |
ea81a808 | 236 | for n in "${!core_ns[@]}"; do _fref "${n}" "${core_ns["${n}"]}"; done |
ea81a808 | 237 | _eval () { EVAL "${1}" "${REPL_ENV}"; } |
31690700 | 238 | _fref "eval" _eval |
86b689f3 JM |
239 | _list; argv="${r}" |
240 | for _arg in "${@:2}"; do _string "${_arg}"; _conj! "${argv}" "${r}"; done | |
b8ee29b2 JM |
241 | _symbol "__STAR__ARGV__STAR__" |
242 | ENV_SET "${REPL_ENV}" "${r}" "${argv}"; | |
31690700 | 243 | |
8cb5cda4 | 244 | # core.mal: defined using the language itself |
31690700 | 245 | REP "(def! not (fn* (a) (if a false true)))" |
1617910a | 246 | REP "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))" |
8cb5cda4 JM |
247 | REP "(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))" |
248 | REP "(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) \`(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))" | |
31690700 | 249 | |
86b689f3 | 250 | # load/run file from command line (then exit) |
31690700 | 251 | if [[ "${1}" ]]; then |
86b689f3 JM |
252 | REP "(load-file \"${1}\")" |
253 | exit 0 | |
254 | fi | |
255 | ||
256 | # repl loop | |
257 | while true; do | |
258 | READLINE "user> " || exit "$?" | |
259 | [[ "${r}" ]] && REP "${r}" && echo "${r}" | |
260 | done |