aboutsummaryrefslogtreecommitdiff
path: root/bash/step1_read_print.sh
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2014-03-24 16:32:24 -0500
committerJoel Martin <github@martintribe.org>2014-03-24 16:32:24 -0500
commit3169070063b2cb877200117ebb384269d73bcb93 (patch)
tree23de3db1ea5c37afd21a45b6ed7771f56a08c0c4 /bash/step1_read_print.sh
downloadmal-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-xbash/step1_read_print.sh45
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