diff options
| author | Joel Martin <github@martintribe.org> | 2014-03-24 16:32:24 -0500 |
|---|---|---|
| committer | Joel Martin <github@martintribe.org> | 2014-03-24 16:32:24 -0500 |
| commit | 3169070063b2cb877200117ebb384269d73bcb93 (patch) | |
| tree | 23de3db1ea5c37afd21a45b6ed7771f56a08c0c4 /bash/step1_read_print.sh | |
| download | mal-3169070063b2cb877200117ebb384269d73bcb93.tar.gz mal-3169070063b2cb877200117ebb384269d73bcb93.zip | |
Current state of mal for Clojure West lighting talk.
Diffstat (limited to 'bash/step1_read_print.sh')
| -rwxr-xr-x | bash/step1_read_print.sh | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/bash/step1_read_print.sh b/bash/step1_read_print.sh new file mode 100755 index 0000000..ba94208 --- /dev/null +++ b/bash/step1_read_print.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +INTERACTIVE=${INTERACTIVE-yes} + +source $(dirname $0)/reader.sh + +# READ: read and parse input +READ () { + [ "${1}" ] && r="${1}" || READLINE + READ_STR "${r}" +} + +# EVAL: just return the input +EVAL () { + local ast="${1}" + local env="${2}" + r= + [[ "${__ERROR}" ]] && return 1 + r="${ast}" +} + +# PRINT: +PRINT () { + if [[ "${__ERROR}" ]]; then + _pr_str "${__ERROR}" yes + r="Error: ${r}" + __ERROR= + else + _pr_str "${1}" yes + fi +} + +# REPL: read, eval, print, loop +REP () { + READ "${1}" || return 1 + EVAL "${r}" + PRINT "${r}" +} + +if [[ -n "${INTERACTIVE}" ]]; then + while true; do + READLINE "user> " || exit "$?" + [[ "${r}" ]] && REP "${r}" && echo "${r}" + done +fi |
