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 JM |
15 | EVAL_AST () { |
16 | local ast="${1}" env="${2}" | |
17 | #_pr_str "${ast}"; echo "EVAL_AST '${ast}:${r} / ${env}'" | |
18 | _obj_type "${ast}"; local ot="${r}" | |
19 | case "${ot}" in | |
20 | symbol) | |
b8ee29b2 | 21 | ENV_GET "${env}" "${ast}" |
31690700 JM |
22 | return ;; |
23 | list) | |
ea81a808 | 24 | _map_with_type _list EVAL "${ast}" "${env}" ;; |
31690700 | 25 | vector) |
ea81a808 | 26 | _map_with_type _vector EVAL "${ast}" "${env}" ;; |
31690700 | 27 | hash_map) |
33d33bb3 | 28 | local res="" key= val="" hm="${ANON["${ast}"]}" |
ea81a808 | 29 | _hash_map; local new_hm="${r}" |
31690700 JM |
30 | eval local keys="\${!${hm}[@]}" |
31 | for key in ${keys}; do | |
32 | eval val="\${${hm}[\"${key}\"]}" | |
33 | EVAL "${val}" "${env}" | |
ea81a808 | 34 | _assoc! "${new_hm}" "${key}" "${r}" |
31690700 JM |
35 | done |
36 | r="${new_hm}" ;; | |
37 | *) | |
38 | r="${ast}" ;; | |
39 | esac | |
40 | } | |
41 | ||
31690700 JM |
42 | EVAL () { |
43 | local ast="${1}" env="${2}" | |
44 | while true; do | |
45 | r= | |
46 | [[ "${__ERROR}" ]] && return 1 | |
47 | #_pr_str "${ast}"; echo "EVAL '${r} / ${env}'" | |
48 | _obj_type "${ast}"; local ot="${r}" | |
49 | if [[ "${ot}" != "list" ]]; then | |
50 | EVAL_AST "${ast}" "${env}" | |
51 | return | |
52 | fi | |
53 | ||
54 | # apply list | |
5bbc7a1f JM |
55 | _empty? "${ast}" && r="${ast}" && return |
56 | ||
31690700 JM |
57 | _nth "${ast}" 0; local a0="${r}" |
58 | _nth "${ast}" 1; local a1="${r}" | |
59 | _nth "${ast}" 2; local a2="${r}" | |
60 | case "${ANON["${a0}"]}" in | |
b8ee29b2 JM |
61 | def!) EVAL "${a2}" "${env}" |
62 | [[ "${__ERROR}" ]] && return 1 | |
63 | ENV_SET "${env}" "${a1}" "${r}" | |
31690700 | 64 | return ;; |
5bbc7a1f | 65 | let__STAR__) ENV "${env}"; local let_env="${r}" |
31690700 JM |
66 | local let_pairs=(${ANON["${a1}"]}) |
67 | local idx=0 | |
68 | #echo "let: [${let_pairs[*]}] for ${a2}" | |
69 | while [[ "${let_pairs["${idx}"]}" ]]; do | |
70 | EVAL "${let_pairs[$(( idx + 1))]}" "${let_env}" | |
b8ee29b2 | 71 | ENV_SET "${let_env}" "${let_pairs[${idx}]}" "${r}" |
31690700 JM |
72 | idx=$(( idx + 2)) |
73 | done | |
6301e0b6 JM |
74 | ast="${a2}" |
75 | env="${let_env}" | |
76 | # Continue loop | |
77 | ;; | |
31690700 JM |
78 | do) _count "${ast}" |
79 | _slice "${ast}" 1 $(( ${r} - 2 )) | |
80 | EVAL_AST "${r}" "${env}" | |
81 | [[ "${__ERROR}" ]] && r= && return 1 | |
8cb5cda4 | 82 | _last "${ast}" |
31690700 JM |
83 | ast="${r}" |
84 | # Continue loop | |
85 | ;; | |
86 | if) EVAL "${a1}" "${env}" | |
b8ee29b2 | 87 | [[ "${__ERROR}" ]] && return 1 |
31690700 JM |
88 | if [[ "${r}" == "${__false}" || "${r}" == "${__nil}" ]]; then |
89 | # eval false form | |
90 | _nth "${ast}" 3; local a3="${r}" | |
91 | if [[ "${a3}" ]]; then | |
92 | ast="${a3}" | |
93 | else | |
94 | r="${__nil}" | |
95 | return | |
96 | fi | |
97 | else | |
98 | # eval true condition | |
99 | ast="${a2}" | |
100 | fi | |
101 | # Continue loop | |
102 | ;; | |
5bbc7a1f | 103 | fn__STAR__) _function "ENV \"${env}\" \"${a1}\" \"\${@}\"; \ |
ea81a808 JM |
104 | EVAL \"${a2}\" \"\${r}\"" \ |
105 | "${a2}" "${env}" "${a1}" | |
31690700 JM |
106 | return ;; |
107 | *) EVAL_AST "${ast}" "${env}" | |
108 | [[ "${__ERROR}" ]] && r= && return 1 | |
109 | local el="${r}" | |
8cb5cda4 JM |
110 | _first "${el}"; local f="${ANON["${r}"]}" |
111 | _rest "${el}"; local args="${ANON["${r}"]}" | |
31690700 JM |
112 | #echo "invoke: [${f}] ${args}" |
113 | if [[ "${f//@/ }" != "${f}" ]]; then | |
114 | set -- ${f//@/ } | |
115 | ast="${2}" | |
116 | ENV "${3}" "${4}" ${args} | |
117 | env="${r}" | |
118 | else | |
119 | eval ${f%%@*} ${args} | |
120 | return | |
121 | fi | |
122 | # Continue loop | |
123 | ;; | |
124 | esac | |
125 | done | |
126 | } | |
127 | ||
86b689f3 | 128 | |
31690700 JM |
129 | PRINT () { |
130 | if [[ "${__ERROR}" ]]; then | |
131 | _pr_str "${__ERROR}" yes | |
132 | r="Error: ${r}" | |
133 | __ERROR= | |
134 | else | |
135 | _pr_str "${1}" yes | |
136 | fi | |
137 | } | |
138 | ||
86b689f3 | 139 | # repl |
31690700 JM |
140 | ENV; REPL_ENV="${r}" |
141 | REP () { | |
142 | r= | |
70aff0c1 | 143 | READ "${1}" |
8cb5cda4 | 144 | EVAL "${r}" "${REPL_ENV}" |
31690700 JM |
145 | PRINT "${r}" |
146 | } | |
147 | ||
8cb5cda4 | 148 | # core.sh: defined using bash |
b8ee29b2 JM |
149 | _fref () { |
150 | _symbol "${1}"; local sym="${r}" | |
151 | _function "${2} \"\${@}\"" | |
152 | ENV_SET "${REPL_ENV}" "${sym}" "${r}" | |
153 | } | |
ea81a808 | 154 | for n in "${!core_ns[@]}"; do _fref "${n}" "${core_ns["${n}"]}"; done |
31690700 JM |
155 | _eval () { EVAL "${1}" "${REPL_ENV}"; } |
156 | _fref "eval" _eval | |
86b689f3 JM |
157 | _list; argv="${r}" |
158 | for _arg in "${@:2}"; do _string "${_arg}"; _conj! "${argv}" "${r}"; done | |
b8ee29b2 JM |
159 | _symbol "__STAR__ARGV__STAR__" |
160 | ENV_SET "${REPL_ENV}" "${r}" "${argv}"; | |
31690700 | 161 | |
8cb5cda4 | 162 | # core.mal: defined using the language itself |
31690700 | 163 | REP "(def! not (fn* (a) (if a false true)))" |
e6d41de4 | 164 | REP "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))" |
31690700 | 165 | |
86b689f3 | 166 | # load/run file from command line (then exit) |
31690700 | 167 | if [[ "${1}" ]]; then |
86b689f3 JM |
168 | REP "(load-file \"${1}\")" |
169 | exit 0 | |
170 | fi | |
171 | ||
172 | # repl loop | |
173 | while true; do | |
174 | READLINE "user> " || exit "$?" | |
175 | [[ "${r}" ]] && REP "${r}" && echo "${r}" | |
176 | done |