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 | 65 | hash_map) |
33d33bb3 | 66 | local res="" key= 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 | |
5bbc7a1f JM |
92 | _empty? "${ast}" && r="${ast}" && return |
93 | ||
31690700 JM |
94 | _nth "${ast}" 0; local a0="${r}" |
95 | _nth "${ast}" 1; local a1="${r}" | |
96 | _nth "${ast}" 2; local a2="${r}" | |
97 | case "${ANON["${a0}"]}" in | |
b8ee29b2 JM |
98 | def!) EVAL "${a2}" "${env}" |
99 | [[ "${__ERROR}" ]] && return 1 | |
100 | ENV_SET "${env}" "${a1}" "${r}" | |
31690700 | 101 | return ;; |
5bbc7a1f | 102 | let__STAR__) ENV "${env}"; local let_env="${r}" |
31690700 JM |
103 | local let_pairs=(${ANON["${a1}"]}) |
104 | local idx=0 | |
105 | #echo "let: [${let_pairs[*]}] for ${a2}" | |
106 | while [[ "${let_pairs["${idx}"]}" ]]; do | |
107 | EVAL "${let_pairs[$(( idx + 1))]}" "${let_env}" | |
b8ee29b2 | 108 | ENV_SET "${let_env}" "${let_pairs[${idx}]}" "${r}" |
31690700 JM |
109 | idx=$(( idx + 2)) |
110 | done | |
6301e0b6 JM |
111 | ast="${a2}" |
112 | env="${let_env}" | |
113 | # Continue loop | |
114 | ;; | |
31690700 JM |
115 | quote) |
116 | r="${a1}" | |
117 | return ;; | |
118 | quasiquote) | |
119 | QUASIQUOTE "${a1}" | |
6301e0b6 JM |
120 | ast="${r}" |
121 | # Continue loop | |
122 | ;; | |
31690700 JM |
123 | do) _count "${ast}" |
124 | _slice "${ast}" 1 $(( ${r} - 2 )) | |
125 | EVAL_AST "${r}" "${env}" | |
126 | [[ "${__ERROR}" ]] && r= && return 1 | |
8cb5cda4 | 127 | _last "${ast}" |
31690700 JM |
128 | ast="${r}" |
129 | # Continue loop | |
130 | ;; | |
131 | if) EVAL "${a1}" "${env}" | |
b8ee29b2 | 132 | [[ "${__ERROR}" ]] && return 1 |
31690700 JM |
133 | if [[ "${r}" == "${__false}" || "${r}" == "${__nil}" ]]; then |
134 | # eval false form | |
135 | _nth "${ast}" 3; local a3="${r}" | |
136 | if [[ "${a3}" ]]; then | |
137 | ast="${a3}" | |
138 | else | |
139 | r="${__nil}" | |
140 | return | |
141 | fi | |
142 | else | |
143 | # eval true condition | |
144 | ast="${a2}" | |
145 | fi | |
146 | # Continue loop | |
147 | ;; | |
5bbc7a1f | 148 | fn__STAR__) _function "ENV \"${env}\" \"${a1}\" \"\${@}\"; \ |
ea81a808 JM |
149 | EVAL \"${a2}\" \"\${r}\"" \ |
150 | "${a2}" "${env}" "${a1}" | |
31690700 JM |
151 | return ;; |
152 | *) EVAL_AST "${ast}" "${env}" | |
153 | [[ "${__ERROR}" ]] && r= && return 1 | |
154 | local el="${r}" | |
8cb5cda4 JM |
155 | _first "${el}"; local f="${ANON["${r}"]}" |
156 | _rest "${el}"; local args="${ANON["${r}"]}" | |
31690700 JM |
157 | #echo "invoke: [${f}] ${args}" |
158 | if [[ "${f//@/ }" != "${f}" ]]; then | |
159 | set -- ${f//@/ } | |
160 | ast="${2}" | |
161 | ENV "${3}" "${4}" ${args} | |
162 | env="${r}" | |
163 | else | |
164 | eval ${f%%@*} ${args} | |
165 | return | |
166 | fi | |
167 | # Continue loop | |
168 | ;; | |
169 | esac | |
170 | done | |
171 | } | |
172 | ||
86b689f3 | 173 | |
31690700 JM |
174 | PRINT () { |
175 | if [[ "${__ERROR}" ]]; then | |
176 | _pr_str "${__ERROR}" yes | |
177 | r="Error: ${r}" | |
178 | __ERROR= | |
179 | else | |
180 | _pr_str "${1}" yes | |
181 | fi | |
182 | } | |
183 | ||
86b689f3 | 184 | # repl |
31690700 JM |
185 | ENV; REPL_ENV="${r}" |
186 | REP () { | |
187 | r= | |
70aff0c1 | 188 | READ "${1}" |
ea81a808 | 189 | EVAL "${r}" "${REPL_ENV}" |
31690700 JM |
190 | PRINT "${r}" |
191 | } | |
192 | ||
8cb5cda4 | 193 | # core.sh: defined using bash |
b8ee29b2 JM |
194 | _fref () { |
195 | _symbol "${1}"; local sym="${r}" | |
196 | _function "${2} \"\${@}\"" | |
197 | ENV_SET "${REPL_ENV}" "${sym}" "${r}" | |
198 | } | |
ea81a808 | 199 | for n in "${!core_ns[@]}"; do _fref "${n}" "${core_ns["${n}"]}"; done |
31690700 JM |
200 | _eval () { EVAL "${1}" "${REPL_ENV}"; } |
201 | _fref "eval" _eval | |
86b689f3 JM |
202 | _list; argv="${r}" |
203 | for _arg in "${@:2}"; do _string "${_arg}"; _conj! "${argv}" "${r}"; done | |
b8ee29b2 JM |
204 | _symbol "__STAR__ARGV__STAR__" |
205 | ENV_SET "${REPL_ENV}" "${r}" "${argv}"; | |
31690700 | 206 | |
8cb5cda4 | 207 | # core.mal: defined using the language itself |
31690700 | 208 | REP "(def! not (fn* (a) (if a false true)))" |
e6d41de4 | 209 | REP "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))" |
31690700 | 210 | |
86b689f3 | 211 | # load/run file from command line (then exit) |
31690700 | 212 | if [[ "${1}" ]]; then |
86b689f3 JM |
213 | REP "(load-file \"${1}\")" |
214 | exit 0 | |
215 | fi | |
216 | ||
217 | # repl loop | |
218 | while true; do | |
219 | READLINE "user> " || exit "$?" | |
220 | [[ "${r}" ]] && REP "${r}" && echo "${r}" | |
221 | done |