Merge branch 'wasamasa-elisp'
[jackhill/mal.git] / bash / step1_read_print.sh
1 #!/usr/bin/env bash
2
3 source $(dirname $0)/reader.sh
4 source $(dirname $0)/printer.sh
5
6 # read
7 READ () {
8 [ "${1}" ] && r="${1}" || READLINE
9 READ_STR "${r}"
10 }
11
12 # eval
13 EVAL () {
14 local ast="${1}"
15 local env="${2}"
16 r=
17 [[ "${__ERROR}" ]] && return 1
18 r="${ast}"
19 }
20
21 # print
22 PRINT () {
23 if [[ "${__ERROR}" ]]; then
24 _pr_str "${__ERROR}" yes
25 r="Error: ${r}"
26 __ERROR=
27 else
28 _pr_str "${1}" yes
29 fi
30 }
31
32 # repl
33 REP () {
34 READ "${1}"
35 EVAL "${r}"
36 PRINT "${r}"
37 }
38
39 # repl loop
40 while true; do
41 READLINE "user> " || exit "$?"
42 [[ "${r}" ]] && REP "${r}" && echo "${r}"
43 done