diff options
| author | Joel Martin <github@martintribe.org> | 2014-04-19 13:04:09 -0500 |
|---|---|---|
| committer | Joel Martin <github@martintribe.org> | 2014-04-19 13:04:09 -0500 |
| commit | 86b689f3d7111a9fa13da389a30f3dfdf877d1a4 (patch) | |
| tree | d72b065f9f987e291f892ceee5a8640363bfd9df /bash/step9_interop.sh | |
| parent | 718887c3019c49fc807bc18fbd5feb975ec03c85 (diff) | |
| download | mal-86b689f3d7111a9fa13da389a30f3dfdf877d1a4.tar.gz mal-86b689f3d7111a9fa13da389a30f3dfdf877d1a4.zip | |
All: *ARGV* and *host-language*. Misc syncing/fixes.
Diffstat (limited to 'bash/step9_interop.sh')
| -rwxr-xr-x | bash/step9_interop.sh | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/bash/step9_interop.sh b/bash/step9_interop.sh index 0491f7f..04d3413 100755 --- a/bash/step9_interop.sh +++ b/bash/step9_interop.sh @@ -1,18 +1,17 @@ #!/bin/bash -INTERACTIVE=${INTERACTIVE-yes} - source $(dirname $0)/reader.sh source $(dirname $0)/printer.sh source $(dirname $0)/env.sh source $(dirname $0)/core.sh -# READ: read and parse input +# read READ () { [ "${1}" ] && r="${1}" || READLINE READ_STR "${r}" } +# eval IS_PAIR () { if _sequential? "${1}"; then _count "${1}" @@ -104,7 +103,6 @@ EVAL_AST () { esac } -# EVAL: evaluate the parameter EVAL () { local ast="${1}" env="${2}" while true; do @@ -214,7 +212,7 @@ EVAL () { done } -# PRINT: +# print PRINT () { if [[ "${__ERROR}" ]]; then _pr_str "${__ERROR}" yes @@ -225,7 +223,7 @@ PRINT () { fi } -# REPL: read, eval, print, loop +# repl ENV; REPL_ENV="${r}" REP () { r= @@ -239,6 +237,9 @@ _fref () { _function "${2} \"\${@}\""; ENV_SET "${REPL_ENV}" "${1}" "${r}"; } for n in "${!core_ns[@]}"; do _fref "${n}" "${core_ns["${n}"]}"; done _eval () { EVAL "${1}" "${REPL_ENV}"; } _fref "eval" _eval +_list; argv="${r}" +for _arg in "${@:2}"; do _string "${_arg}"; _conj! "${argv}" "${r}"; done +ENV_SET "${REPL_ENV}" "__STAR__ARGV__STAR__" "${argv}"; # core.mal: defined using the language itself REP "(def! not (fn* (a) (if a false true)))" @@ -246,12 +247,14 @@ REP "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")) REP "(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))" REP "(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) \`(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))" +# load/run file from command line (then exit) if [[ "${1}" ]]; then - echo "${@}" - REP "(load-file \"${1}\")" && echo "${r}" -elif [[ -n "${INTERACTIVE}" ]]; then - while true; do - READLINE "user> " || exit "$?" - [[ "${r}" ]] && REP "${r}" && echo "${r}" - done -fi + REP "(load-file \"${1}\")" + exit 0 +fi + +# repl loop +while true; do + READLINE "user> " || exit "$?" + [[ "${r}" ]] && REP "${r}" && echo "${r}" +done |
