aboutsummaryrefslogtreecommitdiff
path: root/bash/step2_eval.sh
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2014-04-16 23:57:50 -0500
committerJoel Martin <github@martintribe.org>2014-04-16 23:57:50 -0500
commit8cb5cda46cf3aef847ae3926dc53a5e5f87fe261 (patch)
tree13e5b2878f19ee24272ead8a92a9cb84b33ad0e5 /bash/step2_eval.sh
parenta05f7822b10ed4cdd61ed8384299a003baf1c1c6 (diff)
downloadmal-8cb5cda46cf3aef847ae3926dc53a5e5f87fe261.tar.gz
mal-8cb5cda46cf3aef847ae3926dc53a5e5f87fe261.zip
All: move some fns to core. Major cleanup.
- Don't import/require core until step4. - Define cond/or macros from step8
Diffstat (limited to 'bash/step2_eval.sh')
-rwxr-xr-xbash/step2_eval.sh22
1 files changed, 13 insertions, 9 deletions
diff --git a/bash/step2_eval.sh b/bash/step2_eval.sh
index 0f03a79..4572223 100755
--- a/bash/step2_eval.sh
+++ b/bash/step2_eval.sh
@@ -4,11 +4,10 @@ INTERACTIVE=${INTERACTIVE-yes}
source $(dirname $0)/reader.sh
source $(dirname $0)/printer.sh
-source $(dirname $0)/core.sh
# READ: read and parse input
READ () {
- READLINE
+ [ "${1}" ] && r="${1}" || READLINE
READ_STR "${r}"
}
@@ -56,8 +55,8 @@ EVAL () {
EVAL_AST "${ast}" "${env}"
[[ "${__ERROR}" ]] && return 1
local el="${r}"
- first "${el}"; local f="${r}"
- rest "${el}"; local args="${ANON["${r}"]}"
+ _first "${el}"; local f="${r}"
+ _rest "${el}"; local args="${ANON["${r}"]}"
#echo "invoke: ${f} ${args}"
eval ${f} ${args}
}
@@ -76,15 +75,20 @@ PRINT () {
# REPL: read, eval, print, loop
declare -A REPL_ENV
REP () {
- READ_STR "${1}"
+ READ "${1}" || return 1
EVAL "${r}" REPL_ENV
PRINT "${r}"
}
-REPL_ENV["+"]=num_plus
-REPL_ENV["-"]=num_minus
-REPL_ENV["__STAR__"]=num_multiply
-REPL_ENV["/"]=num_divide
+plus () { r=$(( ${ANON["${1}"]} + ${ANON["${2}"]} )); _number "${r}"; }
+minus () { r=$(( ${ANON["${1}"]} - ${ANON["${2}"]} )); _number "${r}"; }
+multiply () { r=$(( ${ANON["${1}"]} * ${ANON["${2}"]} )); _number "${r}"; }
+divide () { r=$(( ${ANON["${1}"]} / ${ANON["${2}"]} )); _number "${r}"; }
+
+REPL_ENV["+"]=plus
+REPL_ENV["-"]=minus
+REPL_ENV["__STAR__"]=multiply
+REPL_ENV["/"]=divide
if [[ -n "${INTERACTIVE}" ]]; then
while true; do