aboutsummaryrefslogtreecommitdiff
path: root/bash
diff options
context:
space:
mode:
Diffstat (limited to 'bash')
-rwxr-xr-xbash/step1_read_print.sh21
-rwxr-xr-xbash/step2_eval.sh22
-rwxr-xr-xbash/step3_env.sh21
-rwxr-xr-xbash/step4_if_fn_do.sh21
-rwxr-xr-xbash/step5_tco.sh21
-rwxr-xr-xbash/step6_file.sh31
-rwxr-xr-xbash/step7_quote.sh31
-rwxr-xr-xbash/step8_macros.sh31
-rwxr-xr-xbash/step9_interop.sh31
-rwxr-xr-xbash/stepA_more.sh34
10 files changed, 132 insertions, 132 deletions
diff --git a/bash/step1_read_print.sh b/bash/step1_read_print.sh
index ca852ed..e7ca283 100755
--- a/bash/step1_read_print.sh
+++ b/bash/step1_read_print.sh
@@ -1,17 +1,15 @@
#!/bin/bash
-INTERACTIVE=${INTERACTIVE-yes}
-
source $(dirname $0)/reader.sh
source $(dirname $0)/printer.sh
-# READ: read and parse input
+# read
READ () {
[ "${1}" ] && r="${1}" || READLINE
READ_STR "${r}"
}
-# EVAL: just return the input
+# eval
EVAL () {
local ast="${1}"
local env="${2}"
@@ -20,7 +18,7 @@ EVAL () {
r="${ast}"
}
-# PRINT:
+# print
PRINT () {
if [[ "${__ERROR}" ]]; then
_pr_str "${__ERROR}" yes
@@ -31,16 +29,15 @@ PRINT () {
fi
}
-# REPL: read, eval, print, loop
+# repl
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
+# repl loop
+while true; do
+ READLINE "user> " || exit "$?"
+ [[ "${r}" ]] && REP "${r}" && echo "${r}"
+done
diff --git a/bash/step2_eval.sh b/bash/step2_eval.sh
index 4572223..224f181 100755
--- a/bash/step2_eval.sh
+++ b/bash/step2_eval.sh
@@ -1,16 +1,15 @@
#!/bin/bash
-INTERACTIVE=${INTERACTIVE-yes}
-
source $(dirname $0)/reader.sh
source $(dirname $0)/printer.sh
-# READ: read and parse input
+# read
READ () {
[ "${1}" ] && r="${1}" || READLINE
READ_STR "${r}"
}
+# eval
EVAL_AST () {
local ast="${1}" env="${2}"
#_pr_str "${ast}"; echo "EVAL_AST '${ast}:${r} / ${env}'"
@@ -39,7 +38,6 @@ EVAL_AST () {
esac
}
-# EVAL: evaluate the parameter
EVAL () {
local ast="${1}" env="${2}"
r=
@@ -61,7 +59,7 @@ EVAL () {
eval ${f} ${args}
}
-# PRINT:
+# print
PRINT () {
if [[ "${__ERROR}" ]]; then
_pr_str "${__ERROR}" yes
@@ -72,9 +70,10 @@ PRINT () {
fi
}
-# REPL: read, eval, print, loop
+# repl
declare -A REPL_ENV
REP () {
+ r=
READ "${1}" || return 1
EVAL "${r}" REPL_ENV
PRINT "${r}"
@@ -90,9 +89,8 @@ REPL_ENV["-"]=minus
REPL_ENV["__STAR__"]=multiply
REPL_ENV["/"]=divide
-if [[ -n "${INTERACTIVE}" ]]; then
- while true; do
- READLINE "user> " || exit "$?"
- [[ "${r}" ]] && REP "${r}" && echo "${r}"
- done
-fi
+# repl loop
+while true; do
+ READLINE "user> " || exit "$?"
+ [[ "${r}" ]] && REP "${r}" && echo "${r}"
+done
diff --git a/bash/step3_env.sh b/bash/step3_env.sh
index f7a9e42..c81805a 100755
--- a/bash/step3_env.sh
+++ b/bash/step3_env.sh
@@ -1,17 +1,16 @@
#!/bin/bash
-INTERACTIVE=${INTERACTIVE-yes}
-
source $(dirname $0)/reader.sh
source $(dirname $0)/printer.sh
source $(dirname $0)/env.sh
-# READ: read and parse input
+# read
READ () {
[ "${1}" ] && r="${1}" || READLINE
READ_STR "${r}"
}
+# eval
EVAL_AST () {
local ast="${1}" env="${2}"
#_pr_str "${ast}"; echo "EVAL_AST '${ast}:${r} / ${env}'"
@@ -40,7 +39,6 @@ EVAL_AST () {
esac
}
-# EVAL: evaluate the parameter
EVAL () {
local ast="${1}" env="${2}"
r=
@@ -84,7 +82,7 @@ EVAL () {
esac
}
-# PRINT:
+# print
PRINT () {
if [[ "${__ERROR}" ]]; then
_pr_str "${__ERROR}" yes
@@ -95,7 +93,7 @@ PRINT () {
fi
}
-# REPL: read, eval, print, loop
+# repl
ENV; REPL_ENV="${r}"
REP () {
r=
@@ -114,9 +112,8 @@ ENV_SET "${REPL_ENV}" "-" minus
ENV_SET "${REPL_ENV}" "__STAR__" multiply
ENV_SET "${REPL_ENV}" "/" divide
-if [[ -n "${INTERACTIVE}" ]]; then
- while true; do
- READLINE "user> " || exit "$?"
- [[ "${r}" ]] && REP "${r}" && echo "${r}"
- done
-fi
+# repl loop
+while true; do
+ READLINE "user> " || exit "$?"
+ [[ "${r}" ]] && REP "${r}" && echo "${r}"
+done
diff --git a/bash/step4_if_fn_do.sh b/bash/step4_if_fn_do.sh
index a1b27ed..07bf3bf 100755
--- a/bash/step4_if_fn_do.sh
+++ b/bash/step4_if_fn_do.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
EVAL_AST () {
local ast="${1}" env="${2}"
#_pr_str "${ast}"; echo "EVAL_AST '${ast}:${r} / ${env}'"
@@ -41,7 +40,6 @@ EVAL_AST () {
esac
}
-# EVAL: evaluate the parameter
EVAL () {
local ast="${1}" env="${2}"
r=
@@ -107,7 +105,7 @@ EVAL () {
esac
}
-# PRINT:
+# print
PRINT () {
if [[ "${__ERROR}" ]]; then
_pr_str "${__ERROR}" yes
@@ -118,7 +116,7 @@ PRINT () {
fi
}
-# REPL: read, eval, print, loop
+# repl
ENV; REPL_ENV="${r}"
REP () {
r=
@@ -134,9 +132,8 @@ for n in "${!core_ns[@]}"; do _fref "${n}" "${core_ns["${n}"]}"; done
# core.mal: defined using the language itself
REP "(def! not (fn* (a) (if a false true)))"
-if [[ -n "${INTERACTIVE}" ]]; then
- while true; do
- READLINE "user> " || exit "$?"
- [[ "${r}" ]] && REP "${r}" && echo "${r}"
- done
-fi
+# repl loop
+while true; do
+ READLINE "user> " || exit "$?"
+ [[ "${r}" ]] && REP "${r}" && echo "${r}"
+done
diff --git a/bash/step5_tco.sh b/bash/step5_tco.sh
index 62446a7..b57c1f7 100755
--- a/bash/step5_tco.sh
+++ b/bash/step5_tco.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
EVAL_AST () {
local ast="${1}" env="${2}"
#_pr_str "${ast}"; echo "EVAL_AST '${ast}:${r} / ${env}'"
@@ -41,7 +40,6 @@ EVAL_AST () {
esac
}
-# EVAL: evaluate the parameter
EVAL () {
local ast="${1}" env="${2}"
while true; do
@@ -124,7 +122,7 @@ EVAL () {
done
}
-# PRINT:
+# print
PRINT () {
if [[ "${__ERROR}" ]]; then
_pr_str "${__ERROR}" yes
@@ -135,7 +133,7 @@ PRINT () {
fi
}
-# REPL: read, eval, print, loop
+# repl
ENV; REPL_ENV="${r}"
REP () {
r=
@@ -151,9 +149,8 @@ for n in "${!core_ns[@]}"; do _fref "${n}" "${core_ns["${n}"]}"; done
# core.mal: defined using the language itself
REP "(def! not (fn* (a) (if a false true)))"
-if [[ -n "${INTERACTIVE}" ]]; then
- while true; do
- READLINE "user> " || exit "$?"
- [[ "${r}" ]] && REP "${r}" && echo "${r}"
- done
-fi
+# repl loop
+while true; do
+ READLINE "user> " || exit "$?"
+ [[ "${r}" ]] && REP "${r}" && echo "${r}"
+done
diff --git a/bash/step6_file.sh b/bash/step6_file.sh
index b2a3dc8..5f26335 100755
--- a/bash/step6_file.sh
+++ b/bash/step6_file.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
EVAL_AST () {
local ast="${1}" env="${2}"
#_pr_str "${ast}"; echo "EVAL_AST '${ast}:${r} / ${env}'"
@@ -41,7 +40,6 @@ EVAL_AST () {
esac
}
-# EVAL: evaluate the parameter
EVAL () {
local ast="${1}" env="${2}"
while true; do
@@ -124,7 +122,7 @@ EVAL () {
done
}
-# PRINT:
+# print
PRINT () {
if [[ "${__ERROR}" ]]; then
_pr_str "${__ERROR}" yes
@@ -135,7 +133,7 @@ PRINT () {
fi
}
-# REPL: read, eval, print, loop
+# repl
ENV; REPL_ENV="${r}"
REP () {
r=
@@ -149,17 +147,22 @@ _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)))"
REP "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"
+# 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
diff --git a/bash/step7_quote.sh b/bash/step7_quote.sh
index 14721ba..f1a91ca 100755
--- a/bash/step7_quote.sh
+++ b/bash/step7_quote.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}"
@@ -79,7 +78,6 @@ EVAL_AST () {
esac
}
-# EVAL: evaluate the parameter
EVAL () {
local ast="${1}" env="${2}"
while true; do
@@ -169,7 +167,7 @@ EVAL () {
done
}
-# PRINT:
+# print
PRINT () {
if [[ "${__ERROR}" ]]; then
_pr_str "${__ERROR}" yes
@@ -180,7 +178,7 @@ PRINT () {
fi
}
-# REPL: read, eval, print, loop
+# repl
ENV; REPL_ENV="${r}"
REP () {
r=
@@ -194,17 +192,22 @@ _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)))"
REP "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"
+# 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
diff --git a/bash/step8_macros.sh b/bash/step8_macros.sh
index e311489..7826bf2 100755
--- a/bash/step8_macros.sh
+++ b/bash/step8_macros.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
@@ -205,7 +203,7 @@ EVAL () {
done
}
-# PRINT:
+# print
PRINT () {
if [[ "${__ERROR}" ]]; then
_pr_str "${__ERROR}" yes
@@ -216,7 +214,7 @@ PRINT () {
fi
}
-# REPL: read, eval, print, loop
+# repl
ENV; REPL_ENV="${r}"
REP () {
r=
@@ -230,6 +228,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)))"
@@ -237,12 +238,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
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
diff --git a/bash/stepA_more.sh b/bash/stepA_more.sh
index 8a8366a..88f2d4f 100755
--- a/bash/stepA_more.sh
+++ b/bash/stepA_more.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
@@ -229,7 +227,7 @@ EVAL () {
done
}
-# PRINT:
+# print
PRINT () {
if [[ "${__ERROR}" ]]; then
_pr_str "${__ERROR}" yes
@@ -240,7 +238,7 @@ PRINT () {
fi
}
-# REPL: read, eval, print, loop
+# repl
ENV; REPL_ENV="${r}"
REP () {
r=
@@ -254,8 +252,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
-_time_ms () { local ms=$(date +%s%3N); _number "${ms}"; }
-_fref "time-ms" _time_ms
+_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! *host-language* \"bash\")"
@@ -264,12 +263,15 @@ 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
+REP "(println (str \"Mal [\" *host-language* \"]\"))"
+while true; do
+ READLINE "user> " || exit "$?"
+ [[ "${r}" ]] && REP "${r}" && echo "${r}"
+done