From 8cb5cda46cf3aef847ae3926dc53a5e5f87fe261 Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Wed, 16 Apr 2014 23:57:50 -0500 Subject: All: move some fns to core. Major cleanup. - Don't import/require core until step4. - Define cond/or macros from step8 --- bash/step3_env.sh | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'bash/step3_env.sh') diff --git a/bash/step3_env.sh b/bash/step3_env.sh index 28106ee..f7a9e42 100755 --- a/bash/step3_env.sh +++ b/bash/step3_env.sh @@ -4,12 +4,11 @@ INTERACTIVE=${INTERACTIVE-yes} source $(dirname $0)/reader.sh source $(dirname $0)/printer.sh -source $(dirname $0)/core.sh source $(dirname $0)/env.sh # READ: read and parse input READ () { - READLINE + [ "${1}" ] && r="${1}" || READLINE READ_STR "${r}" } @@ -77,8 +76,8 @@ EVAL () { *) EVAL_AST "${ast}" "${env}" [[ "${__ERROR}" ]] && r= && 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} return ;; @@ -100,16 +99,20 @@ PRINT () { ENV; REPL_ENV="${r}" REP () { r= - READ_STR "${1}" - EVAL "${r}" ${REPL_ENV} + READ "${1}" || return 1 + EVAL "${r}" "${REPL_ENV}" PRINT "${r}" } -_ref () { ENV_SET "${REPL_ENV}" "${1}" "${2}"; } -_ref "+" num_plus -_ref "-" num_minus -_ref "__STAR__" num_multiply -_ref "/" 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}"; } + +ENV_SET "${REPL_ENV}" "+" plus +ENV_SET "${REPL_ENV}" "-" minus +ENV_SET "${REPL_ENV}" "__STAR__" multiply +ENV_SET "${REPL_ENV}" "/" divide if [[ -n "${INTERACTIVE}" ]]; then while true; do -- cgit v1.2.3