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 | |
| parent | 718887c3019c49fc807bc18fbd5feb975ec03c85 (diff) | |
| download | mal-86b689f3d7111a9fa13da389a30f3dfdf877d1a4.tar.gz mal-86b689f3d7111a9fa13da389a30f3dfdf877d1a4.zip | |
All: *ARGV* and *host-language*. Misc syncing/fixes.
126 files changed, 833 insertions, 533 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 diff --git a/c/step1_read_print.c b/c/step1_read_print.c index d28439e..264528d 100644 --- a/c/step1_read_print.c +++ b/c/step1_read_print.c @@ -64,7 +64,7 @@ int main() // Set the initial prompt snprintf(prompt, sizeof(prompt), "user> "); - // REPL loop + // repl loop for(;;) { exp = RE(NULL, prompt, NULL); if (mal_error && strcmp("EOF", mal_error->val.string) == 0) { diff --git a/c/step2_eval.c b/c/step2_eval.c index 5d24ff0..39b2789 100644 --- a/c/step2_eval.c +++ b/c/step2_eval.c @@ -134,7 +134,7 @@ int main() snprintf(prompt, sizeof(prompt), "user> "); init_repl_env(); - // REPL loop + // repl loop for(;;) { exp = RE(repl_env, prompt, NULL); if (mal_error && strcmp("EOF", mal_error->val.string) == 0) { diff --git a/c/step3_env.c b/c/step3_env.c index 7c36b38..2f41bc0 100644 --- a/c/step3_env.c +++ b/c/step3_env.c @@ -159,7 +159,7 @@ int main() snprintf(prompt, sizeof(prompt), "user> "); init_repl_env(); - // REPL loop + // repl loop for(;;) { exp = RE(repl_env, prompt, NULL); if (mal_error && strcmp("EOF", mal_error->val.string) == 0) { diff --git a/c/step4_if_fn_do.c b/c/step4_if_fn_do.c index 87e1241..84fb760 100644 --- a/c/step4_if_fn_do.c +++ b/c/step4_if_fn_do.c @@ -178,7 +178,7 @@ void init_repl_env() { // core.c: defined using C int i; - for(i=0; i< (sizeof(core_ns) / sizeof(core_ns[0])); i++) { + for(i=0; i < (sizeof(core_ns) / sizeof(core_ns[0])); i++) { env_set(repl_env, core_ns[i].name, malval_new_function(core_ns[i].func, core_ns[i].arg_cnt)); } @@ -197,7 +197,7 @@ int main() snprintf(prompt, sizeof(prompt), "user> "); init_repl_env(); - // REPL loop + // repl loop for(;;) { exp = RE(repl_env, prompt, NULL); if (mal_error && strcmp("EOF", mal_error->val.string) == 0) { diff --git a/c/step5_tco.c b/c/step5_tco.c index 6938e47..3a46bd2 100644 --- a/c/step5_tco.c +++ b/c/step5_tco.c @@ -187,7 +187,7 @@ void init_repl_env() { // core.c: defined using C int i; - for(i=0; i< (sizeof(core_ns) / sizeof(core_ns[0])); i++) { + for(i=0; i < (sizeof(core_ns) / sizeof(core_ns[0])); i++) { env_set(repl_env, core_ns[i].name, malval_new_function(core_ns[i].func, core_ns[i].arg_cnt)); } @@ -206,7 +206,7 @@ int main() snprintf(prompt, sizeof(prompt), "user> "); init_repl_env(); - // REPL loop + // repl loop for(;;) { exp = RE(repl_env, prompt, NULL); if (mal_error && strcmp("EOF", mal_error->val.string) == 0) { diff --git a/c/step6_file.c b/c/step6_file.c index ae48693..d73acfe 100644 --- a/c/step6_file.c +++ b/c/step6_file.c @@ -182,12 +182,12 @@ MalVal *RE(Env *env, char *prompt, char *str) { // Setup the initial REPL environment Env *repl_env; -void init_repl_env() { +void init_repl_env(int argc, char *argv[]) { repl_env = new_env(NULL, NULL, NULL); // core.c: defined using C int i; - for(i=0; i< (sizeof(core_ns) / sizeof(core_ns[0])); i++) { + for(i=0; i < (sizeof(core_ns) / sizeof(core_ns[0])); i++) { env_set(repl_env, core_ns[i].name, malval_new_function(core_ns[i].func, core_ns[i].arg_cnt)); } @@ -195,6 +195,13 @@ void init_repl_env() { env_set(repl_env, "eval", malval_new_function((void*(*)(void *))do_eval, 1)); + MalVal *_argv = _listX(0); + for (i=2; i < argc; i++) { + MalVal *arg = malval_new_string(argv[i]); + g_array_append_val(_argv->val.array, arg); + } + env_set(repl_env, "*ARGV*", _argv); + // core.mal: defined using the language itself RE(repl_env, "", "(def! not (fn* (a) (if a false true)))"); RE(repl_env, "", @@ -209,7 +216,7 @@ int main(int argc, char *argv[]) // Set the initial prompt and environment snprintf(prompt, sizeof(prompt), "user> "); - init_repl_env(); + init_repl_env(argc, argv); if (argc > 1) { char *cmd = g_strdup_printf("(load-file \"%s\")", argv[1]); @@ -217,7 +224,7 @@ int main(int argc, char *argv[]) return 0; } - // REPL loop + // repl loop for(;;) { exp = RE(repl_env, prompt, NULL); if (mal_error && strcmp("EOF", mal_error->val.string) == 0) { diff --git a/c/step7_quote.c b/c/step7_quote.c index ac17955..b66bde6 100644 --- a/c/step7_quote.c +++ b/c/step7_quote.c @@ -218,12 +218,12 @@ MalVal *RE(Env *env, char *prompt, char *str) { // Setup the initial REPL environment Env *repl_env; -void init_repl_env() { +void init_repl_env(int argc, char *argv[]) { repl_env = new_env(NULL, NULL, NULL); // core.c: defined using C int i; - for(i=0; i< (sizeof(core_ns) / sizeof(core_ns[0])); i++) { + for(i=0; i < (sizeof(core_ns) / sizeof(core_ns[0])); i++) { env_set(repl_env, core_ns[i].name, malval_new_function(core_ns[i].func, core_ns[i].arg_cnt)); } @@ -231,6 +231,13 @@ void init_repl_env() { env_set(repl_env, "eval", malval_new_function((void*(*)(void *))do_eval, 1)); + MalVal *_argv = _listX(0); + for (i=2; i < argc; i++) { + MalVal *arg = malval_new_string(argv[i]); + g_array_append_val(_argv->val.array, arg); + } + env_set(repl_env, "*ARGV*", _argv); + // core.mal: defined using the language itself RE(repl_env, "", "(def! not (fn* (a) (if a false true)))"); RE(repl_env, "", @@ -245,7 +252,7 @@ int main(int argc, char *argv[]) // Set the initial prompt and environment snprintf(prompt, sizeof(prompt), "user> "); - init_repl_env(); + init_repl_env(argc, argv); if (argc > 1) { char *cmd = g_strdup_printf("(load-file \"%s\")", argv[1]); @@ -253,7 +260,7 @@ int main(int argc, char *argv[]) return 0; } - // REPL loop + // repl loop for(;;) { exp = RE(repl_env, prompt, NULL); if (mal_error && strcmp("EOF", mal_error->val.string) == 0) { diff --git a/c/step8_macros.c b/c/step8_macros.c index 93c83fa..e7aebf2 100644 --- a/c/step8_macros.c +++ b/c/step8_macros.c @@ -257,12 +257,12 @@ MalVal *RE(Env *env, char *prompt, char *str) { // Setup the initial REPL environment Env *repl_env; -void init_repl_env() { +void init_repl_env(int argc, char *argv[]) { repl_env = new_env(NULL, NULL, NULL); // core.c: defined using C int i; - for(i=0; i< (sizeof(core_ns) / sizeof(core_ns[0])); i++) { + for(i=0; i < (sizeof(core_ns) / sizeof(core_ns[0])); i++) { env_set(repl_env, core_ns[i].name, malval_new_function(core_ns[i].func, core_ns[i].arg_cnt)); } @@ -270,6 +270,13 @@ void init_repl_env() { env_set(repl_env, "eval", malval_new_function((void*(*)(void *))do_eval, 1)); + MalVal *_argv = _listX(0); + for (i=2; i < argc; i++) { + MalVal *arg = malval_new_string(argv[i]); + g_array_append_val(_argv->val.array, arg); + } + env_set(repl_env, "*ARGV*", _argv); + // core.mal: defined using the language itself RE(repl_env, "", "(def! not (fn* (a) (if a false true)))"); RE(repl_env, "", @@ -286,7 +293,7 @@ int main(int argc, char *argv[]) // Set the initial prompt and environment snprintf(prompt, sizeof(prompt), "user> "); - init_repl_env(); + init_repl_env(argc, argv); if (argc > 1) { char *cmd = g_strdup_printf("(load-file \"%s\")", argv[1]); @@ -294,7 +301,7 @@ int main(int argc, char *argv[]) return 0; } - // REPL loop + // repl loop for(;;) { exp = RE(repl_env, prompt, NULL); if (mal_error && strcmp("EOF", mal_error->val.string) == 0) { diff --git a/c/step9_interop.c b/c/step9_interop.c index 743fb22..f248c55 100644 --- a/c/step9_interop.c +++ b/c/step9_interop.c @@ -263,12 +263,12 @@ MalVal *RE(Env *env, char *prompt, char *str) { // Setup the initial REPL environment Env *repl_env; -void init_repl_env() { +void init_repl_env(int argc, char *argv[]) { repl_env = new_env(NULL, NULL, NULL); // core.c: defined using C int i; - for(i=0; i< (sizeof(core_ns) / sizeof(core_ns[0])); i++) { + for(i=0; i < (sizeof(core_ns) / sizeof(core_ns[0])); i++) { env_set(repl_env, core_ns[i].name, malval_new_function(core_ns[i].func, core_ns[i].arg_cnt)); } @@ -276,6 +276,13 @@ void init_repl_env() { env_set(repl_env, "eval", malval_new_function((void*(*)(void *))do_eval, 1)); + MalVal *_argv = _listX(0); + for (i=2; i < argc; i++) { + MalVal *arg = malval_new_string(argv[i]); + g_array_append_val(_argv->val.array, arg); + } + env_set(repl_env, "*ARGV*", _argv); + // core.mal: defined using the language itself RE(repl_env, "", "(def! not (fn* (a) (if a false true)))"); RE(repl_env, "", @@ -292,7 +299,7 @@ int main(int argc, char *argv[]) // Set the initial prompt and environment snprintf(prompt, sizeof(prompt), "user> "); - init_repl_env(); + init_repl_env(argc, argv); if (argc > 1) { char *cmd = g_strdup_printf("(load-file \"%s\")", argv[1]); @@ -300,7 +307,7 @@ int main(int argc, char *argv[]) return 0; } - // REPL loop + // repl loop for(;;) { exp = RE(repl_env, prompt, NULL); if (mal_error && strcmp("EOF", mal_error->val.string) == 0) { diff --git a/c/stepA_more.c b/c/stepA_more.c index 8547607..de49568 100644 --- a/c/stepA_more.c +++ b/c/stepA_more.c @@ -284,12 +284,12 @@ MalVal *RE(Env *env, char *prompt, char *str) { // Setup the initial REPL environment Env *repl_env; -void init_repl_env() { +void init_repl_env(int argc, char *argv[]) { repl_env = new_env(NULL, NULL, NULL); // core.c: defined using C int i; - for(i=0; i< (sizeof(core_ns) / sizeof(core_ns[0])); i++) { + for(i=0; i < (sizeof(core_ns) / sizeof(core_ns[0])); i++) { env_set(repl_env, core_ns[i].name, malval_new_function(core_ns[i].func, core_ns[i].arg_cnt)); } @@ -297,6 +297,13 @@ void init_repl_env() { env_set(repl_env, "eval", malval_new_function((void*(*)(void *))do_eval, 1)); + MalVal *_argv = _listX(0); + for (i=2; i < argc; i++) { + MalVal *arg = malval_new_string(argv[i]); + g_array_append_val(_argv->val.array, arg); + } + env_set(repl_env, "*ARGV*", _argv); + // core.mal: defined using the language itself RE(repl_env, "", "(def! *host-language* \"c\")"); RE(repl_env, "", "(def! not (fn* (a) (if a false true)))"); @@ -314,7 +321,7 @@ int main(int argc, char *argv[]) // Set the initial prompt and environment snprintf(prompt, sizeof(prompt), "user> "); - init_repl_env(); + init_repl_env(argc, argv); if (argc > 1) { char *cmd = g_strdup_printf("(load-file \"%s\")", argv[1]); @@ -322,7 +329,8 @@ int main(int argc, char *argv[]) return 0; } - // REPL loop + // repl loop + RE(repl_env, "", "(println (str \"Mal [\" *host-language* \"]\"))"); for(;;) { exp = RE(repl_env, prompt, NULL); if (mal_error && strcmp("EOF", mal_error->val.string) == 0) { diff --git a/clojure/src/step0_repl.clj b/clojure/src/step0_repl.clj index f6201dd..7a1a443 100644 --- a/clojure/src/step0_repl.clj +++ b/clojure/src/step0_repl.clj @@ -16,10 +16,12 @@ ;; repl (defn rep [strng] (PRINT (EVAL (READ strng), {}))) +;; repl loop +(defn repl-loop [] + (let [line (readline/readline "user> ")] + (when line + (println (rep line)) + (recur)))) (defn -main [& args] - (loop [] - (let [line (readline/readline "user> ")] - (when line - (println (rep line)) - (recur))))) + (repl-loop)) diff --git a/clojure/src/step1_read_print.clj b/clojure/src/step1_read_print.clj index f15131a..21b297d 100644 --- a/clojure/src/step1_read_print.clj +++ b/clojure/src/step1_read_print.clj @@ -21,13 +21,16 @@ [strng] (PRINT (EVAL (READ strng) {}))) +;; repl loop +(defn repl-loop [] + (let [line (readline/readline "user> ")] + (when line + (when-not (re-seq #"^\s*$|^\s*;.*$" line) ; blank/comment + (try + (println (rep line)) + (catch Throwable e + (clojure.repl/pst e)))) + (recur)))) + (defn -main [& args] - (loop [] - (let [line (readline/readline "user> ")] - (when line - (when-not (re-seq #"^\s*$|^\s*;.*$" line) ; blank/comment - (try - (println (rep line)) - (catch Throwable e - (clojure.repl/pst e)))) - (recur))))) + (repl-loop)) diff --git a/clojure/src/step2_eval.clj b/clojure/src/step2_eval.clj index c34a1ae..96946af 100644 --- a/clojure/src/step2_eval.clj +++ b/clojure/src/step2_eval.clj @@ -48,13 +48,16 @@ [strng] (PRINT (EVAL (READ strng) repl-env))) +;; repl loop +(defn repl-loop [] + (let [line (readline/readline "user> ")] + (when line + (when-not (re-seq #"^\s*$|^\s*;.*$" line) ; blank/comment + (try + (println (rep line)) + (catch Throwable e + (clojure.repl/pst e)))) + (recur)))) + (defn -main [& args] - (loop [] - (let [line (readline/readline "user> ")] - (when line - (when-not (re-seq #"^\s*$|^\s*;.*$" line) ; blank/comment - (try - (println (rep line)) - (catch Throwable e - (clojure.repl/pst e)))) - (recur))))) + (repl-loop)) diff --git a/clojure/src/step3_env.clj b/clojure/src/step3_env.clj index cb634ff..4d31260 100644 --- a/clojure/src/step3_env.clj +++ b/clojure/src/step3_env.clj @@ -62,14 +62,16 @@ (env/env-set repl-env '* *) (env/env-set repl-env '/ /) +;; repl loop +(defn repl-loop [] + (let [line (readline/readline "user> ")] + (when line + (when-not (re-seq #"^\s*$|^\s*;.*$" line) ; blank/comment + (try + (println (rep line)) + (catch Throwable e + (clojure.repl/pst e)))) + (recur)))) (defn -main [& args] - (loop [] - (let [line (readline/readline "user> ")] - (when line - (when-not (re-seq #"^\s*$|^\s*;.*$" line) ; blank/comment - (try - (println (rep line)) - (catch Throwable e - (clojure.repl/pst e)))) - (recur))))) + (repl-loop)) diff --git a/clojure/src/step4_if_fn_do.clj b/clojure/src/step4_if_fn_do.clj index 49c8b03..c125b04 100644 --- a/clojure/src/step4_if_fn_do.clj +++ b/clojure/src/step4_if_fn_do.clj @@ -75,18 +75,20 @@ ;; core.clj: defined using Clojure (doseq [[k v] core/core_ns] (env/env-set repl-env k v)) -(env/env-set repl-env 'eval (fn [ast] (EVAL ast repl-env))) ;; core.mal: defined using the language itself (rep "(def! not (fn* [a] (if a false true)))") +;; repl loop +(defn repl-loop [] + (let [line (readline/readline "user> ")] + (when line + (when-not (re-seq #"^\s*$|^\s*;.*$" line) ; blank/comment + (try + (println (rep line)) + (catch Throwable e + (clojure.repl/pst e)))) + (recur)))) + (defn -main [& args] - (loop [] - (let [line (readline/readline "user> ")] - (when line - (when-not (re-seq #"^\s*$|^\s*;.*$" line) ; blank/comment - (try - (println (rep line)) - (catch Throwable e - (clojure.repl/pst e)))) - (recur))))) + (repl-loop)) diff --git a/clojure/src/step5_tco.clj b/clojure/src/step5_tco.clj index 69e09bf..da47669 100644 --- a/clojure/src/step5_tco.clj +++ b/clojure/src/step5_tco.clj @@ -85,18 +85,20 @@ ;; core.clj: defined using Clojure (doseq [[k v] core/core_ns] (env/env-set repl-env k v)) -(env/env-set repl-env 'eval (fn [ast] (EVAL ast repl-env))) ;; core.mal: defined using the language itself (rep "(def! not (fn* [a] (if a false true)))") +;; repl loop +(defn repl-loop [] + (let [line (readline/readline "user> ")] + (when line + (when-not (re-seq #"^\s*$|^\s*;.*$" line) ; blank/comment + (try + (println (rep line)) + (catch Throwable e + (clojure.repl/pst e)))) + (recur)))) + (defn -main [& args] - (loop [] - (let [line (readline/readline "user> ")] - (when line - (when-not (re-seq #"^\s*$|^\s*;.*$" line) ; blank/comment - (try - (println (rep line)) - (catch Throwable e - (clojure.repl/pst e)))) - (recur))))) + (repl-loop)) diff --git a/clojure/src/step6_file.clj b/clojure/src/step6_file.clj index 2aeb1be..5d080b9 100644 --- a/clojure/src/step6_file.clj +++ b/clojure/src/step6_file.clj @@ -86,20 +86,25 @@ ;; core.clj: defined using Clojure (doseq [[k v] core/core_ns] (env/env-set repl-env k v)) (env/env-set repl-env 'eval (fn [ast] (EVAL ast repl-env))) +(env/env-set repl-env '*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) \")\")))))") +;; repl loop +(defn repl-loop [] + (let [line (readline/readline "user> ")] + (when line + (when-not (re-seq #"^\s*$|^\s*;.*$" line) ; blank/comment + (try + (println (rep line)) + (catch Throwable e + (clojure.repl/pst e)))) + (recur)))) + (defn -main [& args] + (env/env-set repl-env '*ARGV* (rest args)) (if args (rep (str "(load-file \"" (first args) "\")")) - (loop [] - (let [line (readline/readline "user> ")] - (when line - (when-not (re-seq #"^\s*$|^\s*;.*$" line) ; blank/comment - (try - (println (rep line)) - (catch Throwable e - (clojure.repl/pst e)))) - (recur)))))) + (repl-loop))) diff --git a/clojure/src/step7_quote.clj b/clojure/src/step7_quote.clj index 774c61f..ffcca24 100644 --- a/clojure/src/step7_quote.clj +++ b/clojure/src/step7_quote.clj @@ -109,20 +109,25 @@ ;; core.clj: defined using Clojure (doseq [[k v] core/core_ns] (env/env-set repl-env k v)) (env/env-set repl-env 'eval (fn [ast] (EVAL ast repl-env))) +(env/env-set repl-env '*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) \")\")))))") +;; repl loop +(defn repl-loop [] + (let [line (readline/readline "user> ")] + (when line + (when-not (re-seq #"^\s*$|^\s*;.*$" line) ; blank/comment + (try + (println (rep line)) + (catch Throwable e + (clojure.repl/pst e)))) + (recur)))) + (defn -main [& args] + (env/env-set repl-env '*ARGV* (rest args)) (if args (rep (str "(load-file \"" (first args) "\")")) - (loop [] - (let [line (readline/readline "user> ")] - (when line - (when-not (re-seq #"^\s*$|^\s*;.*$" line) ; blank/comment - (try - (println (rep line)) - (catch Throwable e - (clojure.repl/pst e)))) - (recur)))))) + (repl-loop))) diff --git a/clojure/src/step8_macros.clj b/clojure/src/step8_macros.clj index 723be17..6f51415 100644 --- a/clojure/src/step8_macros.clj +++ b/clojure/src/step8_macros.clj @@ -135,6 +135,7 @@ ;; core.clj: defined using Clojure (doseq [[k v] core/core_ns] (env/env-set repl-env k v)) (env/env-set repl-env 'eval (fn [ast] (EVAL ast repl-env))) +(env/env-set repl-env '*ARGV* ()) ;; core.mal: defined using the language itself (rep "(def! not (fn* [a] (if a false true)))") @@ -142,15 +143,19 @@ (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))))))))") +;; repl loop +(defn repl-loop [] + (let [line (readline/readline "user> ")] + (when line + (when-not (re-seq #"^\s*$|^\s*;.*$" line) ; blank/comment + (try + (println (rep line)) + (catch Throwable e + (clojure.repl/pst e)))) + (recur)))) + (defn -main [& args] + (env/env-set repl-env '*ARGV* (rest args)) (if args (rep (str "(load-file \"" (first args) "\")")) - (loop [] - (let [line (readline/readline "user> ")] - (when line - (when-not (re-seq #"^\s*$|^\s*;.*$" line) ; blank/comment - (try - (println (rep line)) - (catch Throwable e - (clojure.repl/pst e)))) - (recur)))))) + (repl-loop))) diff --git a/clojure/src/step9_interop.clj b/clojure/src/step9_interop.clj index 40f2b5c..677599c 100644 --- a/clojure/src/step9_interop.clj +++ b/clojure/src/step9_interop.clj @@ -138,6 +138,7 @@ ;; core.clj: defined using Clojure (doseq [[k v] core/core_ns] (env/env-set repl-env k v)) (env/env-set repl-env 'eval (fn [ast] (EVAL ast repl-env))) +(env/env-set repl-env '*ARGV* ()) ;; core.mal: defined using the language itself (rep "(def! not (fn* [a] (if a false true)))") @@ -145,15 +146,19 @@ (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))))))))") +;; repl loop +(defn repl-loop [] + (let [line (readline/readline "user> ")] + (when line + (when-not (re-seq #"^\s*$|^\s*;.*$" line) ; blank/comment + (try + (println (rep line)) + (catch Throwable e + (clojure.repl/pst e)))) + (recur)))) + (defn -main [& args] + (env/env-set repl-env '*ARGV* (rest args)) (if args (rep (str "(load-file \"" (first args) "\")")) - (loop [] - (let [line (readline/readline "user> ")] - (when line - (when-not (re-seq #"^\s*$|^\s*;.*$" line) ; blank/comment - (try - (println (rep line)) - (catch Throwable e - (clojure.repl/pst e)))) - (recur)))))) + (repl-loop))) diff --git a/clojure/src/stepA_more.clj b/clojure/src/stepA_more.clj index 9739760..e233089 100644 --- a/clojure/src/stepA_more.clj +++ b/clojure/src/stepA_more.clj @@ -152,6 +152,7 @@ ;; core.clj: defined using Clojure (doseq [[k v] core/core_ns] (env/env-set repl-env k v)) (env/env-set repl-env 'eval (fn [ast] (EVAL ast repl-env))) +(env/env-set repl-env '*ARGV* ()) ;; core.mal: defined using the language itself (rep "(def! *host-language* \"clojure\")") @@ -160,15 +161,21 @@ (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))))))))") +;; repl loop +(defn repl-loop [] + (let [line (readline/readline "user> ")] + (when line + (when-not (re-seq #"^\s*$|^\s*;.*$" line) ; blank/comment + (try + (println (rep line)) + (catch Throwable e + (clojure.repl/pst e)))) + (recur)))) + (defn -main [& args] + (env/env-set repl-env '*ARGV* (rest args)) (if args (rep (str "(load-file \"" (first args) "\")")) - (loop [] - (let [line (readline/readline "user> ")] - (when line - (when-not (re-seq #"^\s*$|^\s*;.*$" line) ; blank/comment - (try - (println (rep line)) - (catch Throwable e - (clojure.repl/pst e)))) - (recur)))))) + (do + (rep "(println (str \"Mal [\" *host-language* \"]\"))") + (repl-loop)))) diff --git a/cs/Makefile b/cs/Makefile index f8aefcc..b7eb023 100644 --- a/cs/Makefile +++ b/cs/Makefile @@ -34,7 +34,7 @@ mal.dll: $(LIB_SRCS) mcs $(FLAGS) -r:mal.dll $< clean: - rm -f *.dll *.exe *.mbd + rm -f *.dll *.exe *.mdb .PHONY: stats tests $(TESTS) diff --git a/cs/step0_repl.cs b/cs/step0_repl.cs index de7a6de..616e154 100644 --- a/cs/step0_repl.cs +++ b/cs/step0_repl.cs @@ -19,7 +19,7 @@ namespace Mal { return exp; } - // REPL + // repl static string RE(string env, string str) { return EVAL(READ(str), env); } diff --git a/cs/step1_read_print.cs b/cs/step1_read_print.cs index 62cd7ad..1b427c8 100644 --- a/cs/step1_read_print.cs +++ b/cs/step1_read_print.cs @@ -20,7 +20,7 @@ namespace Mal { return printer._pr_str(exp, true); } - // REPL + // repl static MalVal RE(string env, string str) { return EVAL(READ(str), env); } diff --git a/cs/step2_eval.cs b/cs/step2_eval.cs index dc467de..1a95a81 100644 --- a/cs/step2_eval.cs +++ b/cs/step2_eval.cs @@ -69,7 +69,7 @@ namespace Mal { return printer._pr_str(exp, true); } - // REPL + // repl static MalVal RE(Dictionary<string, MalVal> env, string str) { return EVAL(READ(str), env); } diff --git a/cs/step3_env.cs b/cs/step3_env.cs index ba859eb..2be7992 100644 --- a/cs/step3_env.cs +++ b/cs/step3_env.cs @@ -92,7 +92,7 @@ namespace Mal { return printer._pr_str(exp, true); } - // REPL + // repl static MalVal RE(Env env, string str) { return EVAL(READ(str), env); } @@ -119,6 +119,8 @@ namespace Mal { if (args.Length > 0 && args[0] == "--raw") { Mal.readline.mode = Mal.readline.Mode.Raw; } + + // repl loop while (true) { string line; try { diff --git a/cs/step4_if_fn_do.cs b/cs/step4_if_fn_do.cs index 2383c31..659fc18 100644 --- a/cs/step4_if_fn_do.cs +++ b/cs/step4_if_fn_do.cs @@ -116,7 +116,7 @@ namespace Mal { return printer._pr_str(exp, true); } - // REPL + // repl static MalVal RE(Env env, string str) { return EVAL(READ(str), env); } @@ -136,6 +136,8 @@ namespace Mal { if (args.Length > 0 && args[0] == "--raw") { Mal.readline.mode = Mal.readline.Mode.Raw; } + + // repl loop while (true) { string line; try { diff --git a/cs/step5_tco.cs b/cs/step5_tco.cs index b04d16e..95df860 100644 --- a/cs/step5_tco.cs +++ b/cs/step5_tco.cs @@ -128,7 +128,7 @@ namespace Mal { return printer._pr_str(exp, true); } - // REPL + // repl static MalVal RE(Env env, string str) { return EVAL(READ(str), env); } @@ -148,6 +148,8 @@ namespace Mal { if (args.Length > 0 && args[0] == "--raw") { Mal.readline.mode = Mal.readline.Mode.Raw; } + + // repl loop while (true) { string line; try { diff --git a/cs/step6_file.cs b/cs/step6_file.cs index cb2157e..db1a7b2 100644 --- a/cs/step6_file.cs +++ b/cs/step6_file.cs @@ -4,6 +4,7 @@ using System.Collections; using System.Collections.Generic; using Mal; using MalVal = Mal.types.MalVal; +using MalString = Mal.types.MalString; using MalSymbol = Mal.types.MalSymbol; using MalInteger = Mal.types.MalInteger; using MalList = Mal.types.MalList; @@ -128,7 +129,7 @@ namespace Mal { return printer._pr_str(exp, true); } - // REPL + // repl static MalVal RE(Env env, string str) { return EVAL(READ(str), env); } @@ -142,6 +143,11 @@ namespace Mal { repl_env.set(entry.Key, entry.Value); } repl_env.set("eval", new MalFunction(a => EVAL(a[0], repl_env))); + MalList _argv = new MalList(); + for (int i=1; i < args.Length; i++) { + _argv.conj_BANG(new MalString(args[i])); + } + repl_env.set("*ARGV*", _argv); // core.mal: defined using the language itself RE(repl_env, "(def! not (fn* (a) (if a false true)))"); @@ -153,11 +159,11 @@ namespace Mal { fileIdx = 1; } if (args.Length > fileIdx) { - for(int i=fileIdx; i<args.Length; i++) { - RE(repl_env, "(load-file \"" + args[i] + "\")"); - } + RE(repl_env, "(load-file \"" + args[fileIdx] + "\")"); return; } + + // repl loop while (true) { string line; try { diff --git a/cs/step7_quote.cs b/cs/step7_quote.cs index 6f08abb..d803ac2 100644 --- a/cs/step7_quote.cs +++ b/cs/step7_quote.cs @@ -160,7 +160,7 @@ namespace Mal { return printer._pr_str(exp, true); } - // REPL + // repl static MalVal RE(Env env, string str) { return EVAL(READ(str), env); } @@ -174,6 +174,11 @@ namespace Mal { repl_env.set(entry.Key, entry.Value); } repl_env.set("eval", new MalFunction(a => EVAL(a[0], repl_env))); + MalList _argv = new MalList(); + for (int i=1; i < args.Length; i++) { + _argv.conj_BANG(new MalString(args[i])); + } + repl_env.set("*ARGV*", _argv); // core.mal: defined using the language itself RE(repl_env, "(def! not (fn* (a) (if a false true)))"); @@ -185,11 +190,11 @@ namespace Mal { fileIdx = 1; } if (args.Length > fileIdx) { - for(int i=fileIdx; i<args.Length; i++) { - RE(repl_env, "(load-file \"" + args[i] + "\")"); - } + RE(repl_env, "(load-file \"" + args[fileIdx] + "\")"); return; } + + // repl loop while (true) { string line; try { diff --git a/cs/step8_macros.cs b/cs/step8_macros.cs index caa8f0a..af98fe9 100644 --- a/cs/step8_macros.cs +++ b/cs/step8_macros.cs @@ -197,7 +197,7 @@ namespace Mal { return printer._pr_str(exp, true); } - // REPL + // repl static MalVal RE(Env env, string str) { return EVAL(READ(str), env); } @@ -211,6 +211,11 @@ namespace Mal { repl_env.set(entry.Key, entry.Value); } repl_env.set("eval", new MalFunction(a => EVAL(a[0], repl_env))); + MalList _argv = new MalList(); + for (int i=1; i < args.Length; i++) { + _argv.conj_BANG(new MalString(args[i])); + } + repl_env.set("*ARGV*", _argv); // core.mal: defined using the language itself RE(repl_env, "(def! not (fn* (a) (if a false true)))"); @@ -224,11 +229,11 @@ namespace Mal { fileIdx = 1; } if (args.Length > fileIdx) { - for(int i=fileIdx; i<args.Length; i++) { - RE(repl_env, "(load-file \"" + args[i] + "\")"); - } + RE(repl_env, "(load-file \"" + args[fileIdx] + "\")"); return; } + + // repl loop while (true) { string line; try { diff --git a/cs/stepA_more.cs b/cs/stepA_more.cs index dbbf53d..4fa8387 100644 --- a/cs/stepA_more.cs +++ b/cs/stepA_more.cs @@ -218,7 +218,7 @@ namespace Mal { return printer._pr_str(exp, true); } - // REPL + // repl static MalVal RE(Env env, string str) { return EVAL(READ(str), env); } @@ -232,6 +232,11 @@ namespace Mal { repl_env.set(entry.Key, entry.Value); } repl_env.set("eval", new MalFunction(a => EVAL(a[0], repl_env))); + MalList _argv = new MalList(); + for (int i=1; i < args.Length; i++) { + _argv.conj_BANG(new MalString(args[i])); + } + repl_env.set("*ARGV*", _argv); // core.mal: defined using the language itself RE(repl_env, "(def! *host-language* \"c#\")"); @@ -246,11 +251,12 @@ namespace Mal { fileIdx = 1; } if (args.Length > fileIdx) { - for(int i=fileIdx; i<args.Length; i++) { - RE(repl_env, "(load-file \"" + args[i] + "\")"); - } + RE(repl_env, "(load-file \"" + args[fileIdx] + "\")"); return; } + + // repl loop + RE(repl_env, "(println (str \"Mal [\" *host-language* \"]\"))"); while (true) { string line; try { @@ -39,7 +39,6 @@ Make: Mal: - line numbers in errors - - step6_file: command line arguments - step5_tco - step9_interop @@ -49,6 +48,7 @@ Postscript: - negative numbers Python: + - error: python ../python/stepA_more.py ../mal/stepA_more.mal ../mal/stepA_more.mal Ruby: diff --git a/java/src/main/java/mal/step0_repl.java b/java/src/main/java/mal/step0_repl.java index 9095aca..060f43e 100644 --- a/java/src/main/java/mal/step0_repl.java +++ b/java/src/main/java/mal/step0_repl.java @@ -20,7 +20,7 @@ public class step0_repl { return exp; } - // REPL + // repl public static String RE(String env, String str) { return EVAL(READ(str), env); } diff --git a/java/src/main/java/mal/step1_read_print.java b/java/src/main/java/mal/step1_read_print.java index 3f7e3fb..2c029aa 100644 --- a/java/src/main/java/mal/step1_read_print.java +++ b/java/src/main/java/mal/step1_read_print.java @@ -23,7 +23,7 @@ public class step1_read_print { return printer._pr_str(exp, true); } - // REPL + // repl public static MalVal RE(String env, String str) throws MalThrowable { return EVAL(READ(str), env); } diff --git a/java/src/main/java/mal/step2_eval.java b/java/src/main/java/mal/step2_eval.java index cc2b785..ee8ea26 100644 --- a/java/src/main/java/mal/step2_eval.java +++ b/java/src/main/java/mal/step2_eval.java @@ -72,7 +72,7 @@ public class step2_eval { return printer._pr_str(exp, true); } - // REPL + // repl public static MalVal RE(HashMap env, String str) throws MalThrowable { return EVAL(READ(str), env); } diff --git a/java/src/main/java/mal/step3_env.java b/java/src/main/java/mal/step3_env.java index 297453c..0bf5c16 100644 --- a/java/src/main/java/mal/step3_env.java +++ b/java/src/main/java/mal/step3_env.java @@ -92,7 +92,7 @@ public class step3_env { return printer._pr_str(exp, true); } - // REPL + // repl public static MalVal RE(Env env, String str) throws MalThrowable { return EVAL(READ(str), env); } diff --git a/java/src/main/java/mal/step4_if_fn_do.java b/java/src/main/java/mal/step4_if_fn_do.java index 6e7997a..9a66542 100644 --- a/java/src/main/java/mal/step4_if_fn_do.java +++ b/java/src/main/java/mal/step4_if_fn_do.java @@ -118,7 +118,7 @@ public class step4_if_fn_do { return printer._pr_str(exp, true); } - // REPL + // repl public static MalVal RE(Env env, String str) throws MalThrowable { return EVAL(READ(str), env); } diff --git a/java/src/main/java/mal/step5_tco.java b/java/src/main/java/mal/step5_tco.java index ad15d74..dd1dc89 100644 --- a/java/src/main/java/mal/step5_tco.java +++ b/java/src/main/java/mal/step5_tco.java @@ -129,7 +129,7 @@ public class step5_tco { return printer._pr_str(exp, true); } - // REPL + // repl public static MalVal RE(Env env, String str) throws MalThrowable { return EVAL(READ(str), env); } diff --git a/java/src/main/java/mal/step6_file.java b/java/src/main/java/mal/step6_file.java index 446ffa0..4bf0f0e 100644 --- a/java/src/main/java/mal/step6_file.java +++ b/java/src/main/java/mal/step6_file.java @@ -129,7 +129,7 @@ public class step6_file { return printer._pr_str(exp, true); } - // REPL + // repl public static MalVal RE(Env env, String str) throws MalThrowable { return EVAL(READ(str), env); } @@ -148,6 +148,12 @@ public class step6_file { return EVAL(args.nth(0), repl_env); } }); + MalList _argv = new MalList(); + for (Integer i=1; i < args.length; i++) { + _argv.conj_BANG(new MalString(args[i])); + } + repl_env.set("*ARGV*", _argv); + // core.mal: defined using the language itself RE(repl_env, "(def! not (fn* (a) (if a false true)))"); @@ -159,9 +165,7 @@ public class step6_file { fileIdx = 1; } if (args.length > fileIdx) { - for(Integer i=fileIdx; i<args.length; i++) { - RE(repl_env, "(load-file \"" + args[i] + "\")"); - } + RE(repl_env, "(load-file \"" + args[fileIdx] + "\")"); return; } while (true) { diff --git a/java/src/main/java/mal/step7_quote.java b/java/src/main/java/mal/step7_quote.java index 2f5a638..da0781b 100644 --- a/java/src/main/java/mal/step7_quote.java +++ b/java/src/main/java/mal/step7_quote.java @@ -160,7 +160,7 @@ public class step7_quote { return printer._pr_str(exp, true); } - // REPL + // repl public static MalVal RE(Env env, String str) throws MalThrowable { return EVAL(READ(str), env); } @@ -179,6 +179,12 @@ public class step7_quote { return EVAL(args.nth(0), repl_env); } }); + MalList _argv = new MalList(); + for (Integer i=1; i < args.length; i++) { + _argv.conj_BANG(new MalString(args[i])); + } + repl_env.set("*ARGV*", _argv); + // core.mal: defined using the language itself RE(repl_env, "(def! not (fn* (a) (if a false true)))"); @@ -190,9 +196,7 @@ public class step7_quote { fileIdx = 1; } if (args.length > fileIdx) { - for(Integer i=fileIdx; i<args.length; i++) { - RE(repl_env, "(load-file \"" + args[i] + "\")"); - } + RE(repl_env, "(load-file \"" + args[fileIdx] + "\")"); return; } while (true) { diff --git a/java/src/main/java/mal/step8_macros.java b/java/src/main/java/mal/step8_macros.java index 914c7d3..c4cb1de 100644 --- a/java/src/main/java/mal/step8_macros.java +++ b/java/src/main/java/mal/step8_macros.java @@ -198,7 +198,7 @@ public class step8_macros { return printer._pr_str(exp, true); } - // REPL + // repl public static MalVal RE(Env env, String str) throws MalThrowable { return EVAL(READ(str), env); } @@ -217,6 +217,12 @@ public class step8_macros { return EVAL(args.nth(0), repl_env); } }); + MalList _argv = new MalList(); + for (Integer i=1; i < args.length; i++) { + _argv.conj_BANG(new MalString(args[i])); + } + repl_env.set("*ARGV*", _argv); + // core.mal: defined using the language itself RE(repl_env, "(def! not (fn* (a) (if a false true)))"); @@ -230,9 +236,7 @@ public class step8_macros { fileIdx = 1; } if (args.length > fileIdx) { - for(Integer i=fileIdx; i<args.length; i++) { - RE(repl_env, "(load-file \"" + args[i] + "\")"); - } + RE(repl_env, "(load-file \"" + args[fileIdx] + "\")"); return; } while (true) { diff --git a/java/src/main/java/mal/stepA_more.java b/java/src/main/java/mal/stepA_more.java index 17bdb16..20dee3f 100644 --- a/java/src/main/java/mal/stepA_more.java +++ b/java/src/main/java/mal/stepA_more.java @@ -224,7 +224,7 @@ public class stepA_more { return printer._pr_str(exp, true); } - // REPL + // repl public static MalVal RE(Env env, String str) throws MalThrowable { return EVAL(READ(str), env); } @@ -243,6 +243,12 @@ public class stepA_more { return EVAL(args.nth(0), repl_env); } }); + MalList _argv = new MalList(); + for (Integer i=1; i < args.length; i++) { + _argv.conj_BANG(new MalString(args[i])); + } + repl_env.set("*ARGV*", _argv); + // core.mal: defined using the language itself RE(repl_env, "(def! *host-language* \"java\")"); @@ -257,11 +263,12 @@ public class stepA_more { fileIdx = 1; } if (args.length > fileIdx) { - for(Integer i=fileIdx; i<args.length; i++) { - RE(repl_env, "(load-file \"" + args[i] + "\")"); - } + RE(repl_env, "(load-file \"" + args[fileIdx] + "\")"); return; } + + // repl loop + RE(repl_env, "(println (str \"Mal [\" *host-language* \"]\"))"); while (true) { String line; try { diff --git a/js/step0_repl.js b/js/step0_repl.js index 1d2bbfb..c13be2d 100644 --- a/js/step0_repl.js +++ b/js/step0_repl.js @@ -20,6 +20,7 @@ function PRINT(exp) { // repl var rep = function(str) { return PRINT(EVAL(READ(str), {})); }; +// repl loop if (typeof require === 'undefined') { // Asynchronous browser mode readline.rlwrap(function(line) { return rep(line); }, diff --git a/js/step1_read_print.js b/js/step1_read_print.js index ef9b6bc..1ab162f 100644 --- a/js/step1_read_print.js +++ b/js/step1_read_print.js @@ -24,6 +24,7 @@ function PRINT(exp) { var re = function(str) { return EVAL(READ(str), {}); }; var rep = function(str) { return PRINT(EVAL(READ(str), {})); }; +// repl loop if (typeof require === 'undefined') { // Asynchronous browser mode readline.rlwrap(function(line) { return rep(line); }, diff --git a/js/step2_eval.js b/js/step2_eval.js index adcefe5..72b1f9c 100644 --- a/js/step2_eval.js +++ b/js/step2_eval.js @@ -32,7 +32,7 @@ function eval_ast(ast, env) { } function _EVAL(ast, env) { - //printer.println("EVAL:", types._pr_str(ast, true)); + //printer.println("EVAL:", printer._pr_str(ast, true)); if (!types._list_Q(ast)) { return eval_ast(ast, env); } @@ -61,6 +61,7 @@ repl_env['-'] = function(a,b){return a-b;}; repl_env['*'] = function(a,b){return a*b;}; repl_env['/'] = function(a,b){return a/b;}; +// repl loop if (typeof require === 'undefined') { // Asynchronous browser mode readline.rlwrap(function(line) { return rep(line); }, diff --git a/js/step3_env.js b/js/step3_env.js index 7dbefc4..b65824f 100644 --- a/js/step3_env.js +++ b/js/step3_env.js @@ -33,7 +33,7 @@ function eval_ast(ast, env) { } function _EVAL(ast, env) { - //printer.println("EVAL:", types._pr_str(ast, true)); + //printer.println("EVAL:", printer._pr_str(ast, true)); if (!types._list_Q(ast)) { return eval_ast(ast, env); } @@ -75,6 +75,7 @@ repl_env.set('-', function(a,b){return a-b;}); repl_env.set('*', function(a,b){return a*b;}); repl_env.set('/', function(a,b){return a/b;}); +// repl loop if (typeof require === 'undefined') { // Asynchronous browser mode readline.rlwrap(function(line) { return rep(line); }, diff --git a/js/step4_if_fn_do.js b/js/step4_if_fn_do.js index 7d679a9..a0bb548 100644 --- a/js/step4_if_fn_do.js +++ b/js/step4_if_fn_do.js @@ -34,7 +34,7 @@ function eval_ast(ast, env) { } function _EVAL(ast, env) { - //printer.println("EVAL:", types._pr_str(ast, true)); + //printer.println("EVAL:", printer._pr_str(ast, true)); if (!types._list_Q(ast)) { return eval_ast(ast, env); } @@ -87,11 +87,11 @@ var rep = function(str) { return PRINT(EVAL(READ(str), repl_env)); }; // core.js: defined using javascript for (var n in core.ns) { repl_env.set(n, core.ns[n]); } -repl_env.set('eval', function(ast) { return EVAL(ast, repl_env); }); // core.mal: defined using the language itself rep("(def! not (fn* (a) (if a false true)))"); +// repl loop if (typeof require === 'undefined') { // Asynchronous browser mode readline.rlwrap(function(line) { return rep(line); }, diff --git a/js/step5_tco.js b/js/step5_tco.js index 320807c..9335aa8 100644 --- a/js/step5_tco.js +++ b/js/step5_tco.js @@ -36,7 +36,7 @@ function eval_ast(ast, env) { function _EVAL(ast, env) { while (true) { - //printer.println("EVAL:", types._pr_str(ast, true)); + //printer.println("EVAL:", printer._pr_str(ast, true)); if (!types._list_Q(ast)) { return eval_ast(ast, env); } @@ -96,11 +96,11 @@ var rep = function(str) { return PRINT(EVAL(READ(str), repl_env)); }; // core.js: defined using javascript for (var n in core.ns) { repl_env.set(n, core.ns[n]); } -repl_env.set('eval', function(ast) { return EVAL(ast, repl_env); }); // core.mal: defined using the language itself rep("(def! not (fn* (a) (if a false true)))"); +// repl loop if (typeof require === 'undefined') { // Asynchronous browser mode readline.rlwrap(function(line) { return rep(line); }, diff --git a/js/step6_file.js b/js/step6_file.js index aed1825..85de49e 100644 --- a/js/step6_file.js +++ b/js/step6_file.js @@ -36,7 +36,7 @@ function eval_ast(ast, env) { function _EVAL(ast, env) { while (true) { - //printer.println("EVAL:", types._pr_str(ast, true)); + //printer.println("EVAL:", printer._pr_str(ast, true)); if (!types._list_Q(ast)) { return eval_ast(ast, env); } @@ -97,16 +97,20 @@ var rep = function(str) { return PRINT(EVAL(READ(str), repl_env)); }; // core.js: defined using javascript for (var n in core.ns) { repl_env.set(n, core.ns[n]); } repl_env.set('eval', function(ast) { return EVAL(ast, repl_env); }); +repl_env.set('*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) \")\")))))"); if (typeof process !== 'undefined' && process.argv.length > 2) { - for (var i=2; i < process.argv.length; i++) { - rep('(load-file "' + process.argv[i] + '")'); - } -} else if (typeof require === 'undefined') { + repl_env.set('*ARGV*', process.argv.slice(3)); + rep('(load-file "' + process.argv[2] + '")'); + process.exit(0); +} + +// repl loop +if (typeof require === 'undefined') { // Asynchronous browser mode readline.rlwrap(function(line) { return rep(line); }, function(exc) { diff --git a/js/step7_quote.js b/js/step7_quote.js index a7f4535..8676d07 100644 --- a/js/step7_quote.js +++ b/js/step7_quote.js @@ -56,7 +56,7 @@ function eval_ast(ast, env) { function _EVAL(ast, env) { while (true) { - //printer.println("EVAL:", types._pr_str(ast, true)); + //printer.println("EVAL:", printer._pr_str(ast, true)); if (!types._list_Q(ast)) { return eval_ast(ast, env); } @@ -121,16 +121,20 @@ var rep = function(str) { return PRINT(EVAL(READ(str), repl_env)); }; // core.js: defined using javascript for (var n in core.ns) { repl_env.set(n, core.ns[n]); } repl_env.set('eval', function(ast) { return EVAL(ast, repl_env); }); +repl_env.set('*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) \")\")))))"); if (typeof process !== 'undefined' && process.argv.length > 2) { - for (var i=2; i < process.argv.length; i++) { - rep('(load-file "' + process.argv[i] + '")'); - } -} else if (typeof require === 'undefined') { + repl_env.set('*ARGV*', process.argv.slice(3)); + rep('(load-file "' + process.argv[2] + '")'); + process.exit(0); +} + +// repl loop +if (typeof require === 'undefined') { // Asynchronous browser mode readline.rlwrap(function(line) { return rep(line); }, function(exc) { diff --git a/js/step8_macros.js b/js/step8_macros.js index 1268a4d..dca7beb 100644 --- a/js/step8_macros.js +++ b/js/step8_macros.js @@ -71,7 +71,7 @@ function eval_ast(ast, env) { function _EVAL(ast, env) { while (true) { - //printer.println("EVAL:", types._pr_str(ast, true)); + //printer.println("EVAL:", printer._pr_str(ast, true)); if (!types._list_Q(ast)) { return eval_ast(ast, env); } @@ -145,6 +145,7 @@ var rep = function(str) { return PRINT(EVAL(READ(str), repl_env)); }; // core.js: defined using javascript for (var n in core.ns) { repl_env.set(n, core.ns[n]); } repl_env.set('eval', function(ast) { return EVAL(ast, repl_env); }); +repl_env.set('*ARGV*', []); // core.mal: defined using the language itself rep("(def! not (fn* (a) (if a false true)))"); @@ -153,10 +154,13 @@ rep("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if ( 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))))))))"); if (typeof process !== 'undefined' && process.argv.length > 2) { - for (var i=2; i < process.argv.length; i++) { - rep('(load-file "' + process.argv[i] + '")'); - } -} else if (typeof require === 'undefined') { + repl_env.set('*ARGV*', process.argv.slice(3)); + rep('(load-file "' + process.argv[2] + '")'); + process.exit(0); +} + +// repl loop +if (typeof require === 'undefined') { // Asynchronous browser mode readline.rlwrap(function(line) { return rep(line); }, function(exc) { diff --git a/js/step9_interop.js b/js/step9_interop.js index 1fd07d4..e95b4ca 100644 --- a/js/step9_interop.js +++ b/js/step9_interop.js @@ -71,7 +71,7 @@ function eval_ast(ast, env) { function _EVAL(ast, env) { while (true) { - //printer.println("EVAL:", types._pr_str(ast, true)); + //printer.println("EVAL:", printer._pr_str(ast, true)); if (!types._list_Q(ast)) { return eval_ast(ast, env); } @@ -151,6 +151,7 @@ var rep = function(str) { return PRINT(EVAL(READ(str), repl_env)); }; // core.js: defined using javascript for (var n in core.ns) { repl_env.set(n, core.ns[n]); } repl_env.set('eval', function(ast) { return EVAL(ast, repl_env); }); +repl_env.set('*ARGV*', []); // core.mal: defined using the language itself rep("(def! not (fn* (a) (if a false true)))"); @@ -159,10 +160,13 @@ rep("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if ( 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))))))))"); if (typeof process !== 'undefined' && process.argv.length > 2) { - for (var i=2; i < process.argv.length; i++) { - rep('(load-file "' + process.argv[i] + '")'); - } -} else if (typeof require === 'undefined') { + repl_env.set('*ARGV*', process.argv.slice(3)); + rep('(load-file "' + process.argv[2] + '")'); + process.exit(0); +} + +// repl loop +if (typeof require === 'undefined') { // Asynchronous browser mode readline.rlwrap(function(line) { return rep(line); }, function(exc) { diff --git a/js/stepA_more.js b/js/stepA_more.js index a2035dc..58840eb 100644 --- a/js/stepA_more.js +++ b/js/stepA_more.js @@ -71,7 +71,7 @@ function eval_ast(ast, env) { function _EVAL(ast, env) { while (true) { - //printer.println("EVAL:", types._pr_str(ast, true)); + //printer.println("EVAL:", printer._pr_str(ast, true)); if (!types._list_Q(ast)) { return eval_ast(ast, env); } @@ -162,6 +162,7 @@ var rep = function(str) { return PRINT(EVAL(READ(str), repl_env)); }; // core.js: defined using javascript for (var n in core.ns) { repl_env.set(n, core.ns[n]); } repl_env.set('eval', function(ast) { return EVAL(ast, repl_env); }); +repl_env.set('*ARGV*', []); // core.mal: defined using the language itself rep("(def! *host-language* \"javascript\")") @@ -171,10 +172,14 @@ rep("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if ( 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))))))))"); if (typeof process !== 'undefined' && process.argv.length > 2) { - for (var i=2; i < process.argv.length; i++) { - rep('(load-file "' + process.argv[i] + '")'); - } -} else if (typeof require === 'undefined') { + repl_env.set('*ARGV*', process.argv.slice(3)); + rep('(load-file "' + process.argv[2] + '")'); + process.exit(0); +} + +// repl loop +rep("(println (str \"Mal [\" *host-language* \"]\"))"); +if (typeof require === 'undefined') { // Asynchronous browser mode readline.rlwrap(function(line) { return rep(line); }, function(exc) { diff --git a/make/step1_read_print.mk b/make/step1_read_print.mk index a5fe2d2..f695a7e 100644 --- a/make/step1_read_print.mk +++ b/make/step1_read_print.mk @@ -28,5 +28,5 @@ endef REP = $(call PRINT,$(strip $(call EVAL,$(strip $(call READ,$(1))),$(REPL_ENV)))) REPL = $(info $(call REP,$(call READLINE,"user> ")))$(if $(READLINE_EOF),,$(call REPL)) -# Call the read-eval-print loop +# repl loop $(if $(strip $(INTERACTIVE)),$(call REPL)) diff --git a/make/step2_eval.mk b/make/step2_eval.mk index 96cd98f..34b1dab 100644 --- a/make/step2_eval.mk +++ b/make/step2_eval.mk @@ -69,5 +69,5 @@ $(call do,$(call _assoc!,$(REPL_ENV),-,number_subtract)) $(call do,$(call _assoc!,$(REPL_ENV),*,number_multiply)) $(call do,$(call _assoc!,$(REPL_ENV),/,number_divide)) -# Call the read-eval-print loop +# repl loop $(if $(strip $(INTERACTIVE)),$(call REPL)) diff --git a/make/step3_env.mk b/make/step3_env.mk index acc038d..b548874 100644 --- a/make/step3_env.mk +++ b/make/step3_env.mk @@ -91,5 +91,5 @@ REPL_ENV := $(call ENV_SET,$(REPL_ENV),-,number_subtract) REPL_ENV := $(call ENV_SET,$(REPL_ENV),*,number_multiply) REPL_ENV := $(call ENV_SET,$(REPL_ENV),/,number_divide) -# Call the read-eval-print loop +# repl loop $(if $(strip $(INTERACTIVE)),$(call REPL)) diff --git a/make/step4_if_fn_do.mk b/make/step4_if_fn_do.mk index 13dcf1c..47f7fe4 100644 --- a/make/step4_if_fn_do.mk +++ b/make/step4_if_fn_do.mk @@ -109,5 +109,5 @@ $(call _import_core,$(core_ns)) # core.mal: defined in terms of the language itself $(call do,$(call REP, (def! not (fn* (a) (if a false true))) )) -# Call the read-eval-print loop +# repl loop $(if $(strip $(INTERACTIVE)),$(call REPL)) diff --git a/make/step6_file.mk b/make/step6_file.mk index 494cbb2..f779d12 100644 --- a/make/step6_file.mk +++ b/make/step6_file.mk @@ -106,6 +106,8 @@ _fref = $(eval REPL_ENV := $(call ENV_SET,$(REPL_ENV),$(1),$(call _function,$$(c _import_core = $(if $(strip $(1)),$(call _fref,$(word 1,$(1)),$(word 2,$(1)))$(call _import_core,$(wordlist 3,$(words $(1)),$(1))),) $(call _import_core,$(core_ns)) REPL_ENV := $(call ENV_SET,$(REPL_ENV),eval,$(call _function,$$(call EVAL,$$(1),$$(REPL_ENV)))) +_argv := $(call _list) +REPL_ENV := $(call ENV_SET,$(REPL_ENV),*ARGV*,$(_argv)) # core.mal: defined in terms of the language itself $(call do,$(call REP, (def! not (fn* (a) (if a false true))) )) @@ -113,11 +115,13 @@ $(call do,$(call REP, (def! load-file (fn* (f) (eval (read-string (str "(do " (s # Load and eval any files specified on the command line $(if $(MAKECMDGOALS),\ - $(foreach file,$(MAKECMDGOALS),$(call do,$(call REP, (load-file "$(file)") )))\ + $(foreach arg,$(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS)),\ + $(call do,$(call _conj!,$(_argv),$(call _string,$(arg)))))\ + $(call do,$(call REP, (load-file "$(word 1,$(MAKECMDGOALS))") )) \ $(eval INTERACTIVE :=),) .PHONY: none $(MAKECMDGOALS) none $(MAKECMDGOALS): @true -# Call the read-eval-print loop +# repl loop $(if $(strip $(INTERACTIVE)),$(call REPL)) diff --git a/make/step7_quote.mk b/make/step7_quote.mk index 072e407..c734ad1 100644 --- a/make/step7_quote.mk +++ b/make/step7_quote.mk @@ -123,6 +123,8 @@ _fref = $(eval REPL_ENV := $(call ENV_SET,$(REPL_ENV),$(1),$(call _function,$$(c _import_core = $(if $(strip $(1)),$(call _fref,$(word 1,$(1)),$(word 2,$(1)))$(call _import_core,$(wordlist 3,$(words $(1)),$(1))),) $(call _import_core,$(core_ns)) REPL_ENV := $(call ENV_SET,$(REPL_ENV),eval,$(call _function,$$(call EVAL,$$(1),$$(REPL_ENV)))) +_argv := $(call _list) +REPL_ENV := $(call ENV_SET,$(REPL_ENV),*ARGV*,$(_argv)) # core.mal: defined in terms of the language itself $(call do,$(call REP, (def! not (fn* (a) (if a false true))) )) @@ -130,11 +132,13 @@ $(call do,$(call REP, (def! load-file (fn* (f) (eval (read-string (str "(do " (s # Load and eval any files specified on the command line $(if $(MAKECMDGOALS),\ - $(foreach file,$(MAKECMDGOALS),$(call do,$(call REP, (load-file "$(file)") )))\ + $(foreach arg,$(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS)),\ + $(call do,$(call _conj!,$(_argv),$(call _string,$(arg)))))\ + $(call do,$(call REP, (load-file "$(word 1,$(MAKECMDGOALS))") )) \ $(eval INTERACTIVE :=),) .PHONY: none $(MAKECMDGOALS) none $(MAKECMDGOALS): @true -# Call the read-eval-print loop +# repl loop $(if $(strip $(INTERACTIVE)),$(call REPL)) diff --git a/make/step8_macros.mk b/make/step8_macros.mk index f4234d2..5a0007c 100644 --- a/make/step8_macros.mk +++ b/make/step8_macros.mk @@ -146,6 +146,8 @@ _fref = $(eval REPL_ENV := $(call ENV_SET,$(REPL_ENV),$(1),$(call _function,$$(c _import_core = $(if $(strip $(1)),$(call _fref,$(word 1,$(1)),$(word 2,$(1)))$(call _import_core,$(wordlist 3,$(words $(1)),$(1))),) $(call _import_core,$(core_ns)) REPL_ENV := $(call ENV_SET,$(REPL_ENV),eval,$(call _function,$$(call EVAL,$$(1),$$(REPL_ENV)))) +_argv := $(call _list) +REPL_ENV := $(call ENV_SET,$(REPL_ENV),*ARGV*,$(_argv)) # core.mal: defined in terms of the language itself $(call do,$(call REP, (def! not (fn* (a) (if a false true))) )) @@ -155,11 +157,13 @@ $(call do,$(call REP, (defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (co # Load and eval any files specified on the command line $(if $(MAKECMDGOALS),\ - $(foreach file,$(MAKECMDGOALS),$(call do,$(call REP, (load-file "$(file)") )))\ + $(foreach arg,$(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS)),\ + $(call do,$(call _conj!,$(_argv),$(call _string,$(arg)))))\ + $(call do,$(call REP, (load-file "$(word 1,$(MAKECMDGOALS))") )) \ $(eval INTERACTIVE :=),) .PHONY: none $(MAKECMDGOALS) none $(MAKECMDGOALS): @true -# Call the read-eval-print loop +# repl loop $(if $(strip $(INTERACTIVE)),$(call REPL)) diff --git a/make/step9_interop.mk b/make/step9_interop.mk index 2dfe7e0..07ad5a3 100644 --- a/make/step9_interop.mk +++ b/make/step9_interop.mk @@ -150,6 +150,8 @@ _fref = $(eval REPL_ENV := $(call ENV_SET,$(REPL_ENV),$(1),$(call _function,$$(c _import_core = $(if $(strip $(1)),$(call _fref,$(word 1,$(1)),$(word 2,$(1)))$(call _import_core,$(wordlist 3,$(words $(1)),$(1))),) $(call _import_core,$(core_ns)) REPL_ENV := $(call ENV_SET,$(REPL_ENV),eval,$(call _function,$$(call EVAL,$$(1),$$(REPL_ENV)))) +_argv := $(call _list) +REPL_ENV := $(call ENV_SET,$(REPL_ENV),*ARGV*,$(_argv)) # core.mal: defined in terms of the language itself $(call do,$(call REP, (def! not (fn* (a) (if a false true))) )) @@ -159,11 +161,13 @@ $(call do,$(call REP, (defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (co # Load and eval any files specified on the command line $(if $(MAKECMDGOALS),\ - $(foreach file,$(MAKECMDGOALS),$(call do,$(call REP, (load-file "$(file)") )))\ + $(foreach arg,$(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS)),\ + $(call do,$(call _conj!,$(_argv),$(call _string,$(arg)))))\ + $(call do,$(call REP, (load-file "$(word 1,$(MAKECMDGOALS))") )) \ $(eval INTERACTIVE :=),) .PHONY: none $(MAKECMDGOALS) none $(MAKECMDGOALS): @true -# Call the read-eval-print loop +# repl loop $(if $(strip $(INTERACTIVE)),$(call REPL)) diff --git a/make/stepA_more.mk b/make/stepA_more.mk index 8b92178..dc07b01 100644 --- a/make/stepA_more.mk +++ b/make/stepA_more.mk @@ -165,6 +165,8 @@ _fref = $(eval REPL_ENV := $(call ENV_SET,$(REPL_ENV),$(1),$(call _function,$$(c _import_core = $(if $(strip $(1)),$(call _fref,$(word 1,$(1)),$(word 2,$(1)))$(call _import_core,$(wordlist 3,$(words $(1)),$(1))),) $(call _import_core,$(core_ns)) REPL_ENV := $(call ENV_SET,$(REPL_ENV),eval,$(call _function,$$(call EVAL,$$(1),$$(REPL_ENV)))) +_argv := $(call _list) +REPL_ENV := $(call ENV_SET,$(REPL_ENV),*ARGV*,$(_argv)) # core.mal: defined in terms of the language itself $(call do,$(call REP, (def! *host-language* "make") )) @@ -175,11 +177,15 @@ $(call do,$(call REP, (defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (co # Load and eval any files specified on the command line $(if $(MAKECMDGOALS),\ - $(foreach file,$(MAKECMDGOALS),$(call do,$(call REP, (load-file "$(file)") )))\ + $(foreach arg,$(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS)),\ + $(call do,$(call _conj!,$(_argv),$(call _string,$(arg)))))\ + $(call do,$(call REP, (load-file "$(word 1,$(MAKECMDGOALS))") )) \ $(eval INTERACTIVE :=),) .PHONY: none $(MAKECMDGOALS) none $(MAKECMDGOALS): @true -# Call the read-eval-print loop -$(if $(strip $(INTERACTIVE)),$(call REPL)) +# repl loop +$(if $(strip $(INTERACTIVE)),\ + $(call do,$(call REP, (println (str "Mal [" *host-language* "]")) )) \ + $(call REPL)) diff --git a/mal/step1_read_print.mal b/mal/step1_read_print.mal index c24fca8..991a745 100644 --- a/mal/step1_read_print.mal +++ b/mal/step1_read_print.mal @@ -13,7 +13,8 @@ (def! rep (fn* [strng] (PRINT (EVAL (READ strng) {})))) -(def! -main (fn* [] +;; repl loop +(def! repl-loop (fn* [] (let* [line (readline "mal-user> ")] (if line (do @@ -22,5 +23,8 @@ (println (rep line)) (catch* exc (println "Uncaught exception:" exc)))) - (-main)))))) + (repl-loop)))))) + +(def! -main (fn* [& args] + (repl-loop))) (-main) diff --git a/mal/step2_eval.mal b/mal/step2_eval.mal index b27ec91..9c5f2eb 100644 --- a/mal/step2_eval.mal +++ b/mal/step2_eval.mal @@ -45,15 +45,18 @@ (def! rep (fn* [strng] (PRINT (EVAL (READ strng) repl-env)))) -(def! -main (fn* [] +;; repl loop +(def! repl-loop (fn* [] (let* [line (readline "mal-user> ")] (if line (do (if (not (= "" line)) (try* - (let* [res (rep line)] - (println res)) + (println (rep line)) (catch* exc (println "Uncaught exception:" exc)))) - (-main)))))) + (repl-loop)))))) + +(def! -main (fn* [& args] + (repl-loop))) (-main) diff --git a/mal/step3_env.mal b/mal/step3_env.mal index c812f17..6677b04 100644 --- a/mal/step3_env.mal +++ b/mal/step3_env.mal @@ -65,15 +65,18 @@ (env-set repl-env "*" *) (env-set repl-env "/" /) -(def! -main (fn* [] +;; repl loop +(def! repl-loop (fn* [] (let* [line (readline "mal-user> ")] (if line (do (if (not (= "" line)) (try* - (let* [res (rep line)] - (println res)) + (println (rep line)) (catch* exc (println "Uncaught exception:" exc)))) - (-main)))))) + (repl-loop)))))) + +(def! -main (fn* [& args] + (repl-loop))) (-main) diff --git a/mal/step4_if_fn_do.mal b/mal/step4_if_fn_do.mal index aa48112..85a9109 100644 --- a/mal/step4_if_fn_do.mal +++ b/mal/step4_if_fn_do.mal @@ -83,15 +83,18 @@ ;; core.mal: defined using the new language itself (rep "(def! not (fn* [a] (if a false true)))") -(def! -main (fn* [] +;; repl loop +(def! repl-loop (fn* [] (let* [line (readline "mal-user> ")] (if line (do (if (not (= "" line)) (try* - (let* [res (rep line)] - (println res)) + (println (rep line)) (catch* exc (println "Uncaught exception:" exc)))) - (-main)))))) + (repl-loop)))))) + +(def! -main (fn* [& args] + (repl-loop))) (-main) diff --git a/mal/step6_file.mal b/mal/step6_file.mal index 905f9fd..b36820d 100644 --- a/mal/step6_file.mal +++ b/mal/step6_file.mal @@ -80,20 +80,26 @@ ;; core.mal: defined directly using mal (map (fn* [data] (env-set repl-env (nth data 0) (nth data 1))) core_ns) (env-set repl-env 'eval (fn* [ast] (EVAL ast repl-env))) +(env-set repl-env '*ARGV* (rest *ARGV*)) ;; core.mal: defined using the new language itself (rep "(def! not (fn* [a] (if a false true)))") (rep "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))") -(def! -main (fn* [] +;; repl loop +(def! repl-loop (fn* [] (let* [line (readline "mal-user> ")] (if line (do (if (not (= "" line)) (try* - (let* [res (rep line)] - (println res)) + (println (rep line)) (catch* exc (println "Uncaught exception:" exc)))) - (-main)))))) -(-main) + (repl-loop)))))) + +(def! -main (fn* [& args] + (if (> (count args) 0) + (rep (str "(load-file \"" (first args) "\")")) + (repl-loop)))) +(apply -main *ARGV*) diff --git a/mal/step7_quote.mal b/mal/step7_quote.mal index c95db5d..042443c 100644 --- a/mal/step7_quote.mal +++ b/mal/step7_quote.mal @@ -108,20 +108,26 @@ ;; core.mal: defined directly using mal (map (fn* [data] (env-set repl-env (nth data 0) (nth data 1))) core_ns) (env-set repl-env 'eval (fn* [ast] (EVAL ast repl-env))) +(env-set repl-env '*ARGV* (rest *ARGV*)) ;; core.mal: defined using the new language itself (rep "(def! not (fn* [a] (if a false true)))") (rep "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))") -(def! -main (fn* [] +;; repl loop +(def! repl-loop (fn* [] (let* [line (readline "mal-user> ")] (if line (do (if (not (= "" line)) (try* - (let* [res (rep line)] - (println res)) + (println (rep line)) (catch* exc (println "Uncaught exception:" exc)))) - (-main)))))) -(-main) + (repl-loop)))))) + +(def! -main (fn* [& args] + (if (> (count args) 0) + (rep (str "(load-file \"" (first args) "\")")) + (repl-loop)))) +(apply -main *ARGV*) diff --git a/mal/step8_macros.mal b/mal/step8_macros.mal index feec227..e5e308c 100644 --- a/mal/step8_macros.mal +++ b/mal/step8_macros.mal @@ -140,6 +140,7 @@ ;; core.mal: defined directly using mal (map (fn* [data] (env-set repl-env (nth data 0) (nth data 1))) core_ns) (env-set repl-env 'eval (fn* [ast] (EVAL ast repl-env))) +(env-set repl-env '*ARGV* (rest *ARGV*)) ;; core.mal: defined using the new language itself (rep "(def! not (fn* [a] (if a false true)))") @@ -147,15 +148,20 @@ (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))))))))") -(def! -main (fn* [] +;; repl loop +(def! repl-loop (fn* [] (let* [line (readline "mal-user> ")] (if line (do (if (not (= "" line)) (try* - (let* [res (rep line)] - (println res)) + (println (rep line)) (catch* exc (println "Uncaught exception:" exc)))) - (-main)))))) -(-main) + (repl-loop)))))) + +(def! -main (fn* [& args] + (if (> (count args) 0) + (rep (str "(load-file \"" (first args) "\")")) + (repl-loop)))) +(apply -main *ARGV*) diff --git a/mal/stepA_more.mal b/mal/stepA_more.mal index 1af0d25..3e2a258 100644 --- a/mal/stepA_more.mal +++ b/mal/stepA_more.mal @@ -151,6 +151,7 @@ ;; core.mal: defined directly using mal (map (fn* [data] (env-set repl-env (nth data 0) (nth data 1))) core_ns) (env-set repl-env 'eval (fn* [ast] (EVAL ast repl-env))) +(env-set repl-env '*ARGV* (rest *ARGV*)) ;; core.mal: defined using the new language itself (rep (str "(def! *host-language* \"" *host-language* "-mal\")")) @@ -159,15 +160,22 @@ (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))))))))") -(def! -main (fn* [] +;; repl loop +(def! repl-loop (fn* [] (let* [line (readline "mal-user> ")] (if line (do (if (not (= "" line)) (try* - (let* [res (rep line)] - (println res)) + (println (rep line)) (catch* exc (println "Uncaught exception:" exc)))) - (-main)))))) -(-main) + (repl-loop)))))) + +(def! -main (fn* [& args] + (if (> (count args) 0) + (rep (str "(load-file \"" (first args) "\")")) + (do + (rep "(println (str \"Mal [\" *host-language* \"]\"))") + (repl-loop))))) +(apply -main *ARGV*) diff --git a/php/readline.php b/php/readline.php index d4b3ad7..5e33501 100644 --- a/php/readline.php +++ b/php/readline.php @@ -31,4 +31,4 @@ function mal_readline($prompt) { return $line; } -?> +?> diff --git a/php/step0_repl.php b/php/step0_repl.php index 64b086b..eecb052 100644 --- a/php/step0_repl.php +++ b/php/step0_repl.php @@ -22,6 +22,7 @@ function rep($str) { return MAL_PRINT(MAL_EVAL(READ($str), array())); } +// repl loop do { $line = mal_readline("user> "); if ($line === NULL) { break; } @@ -30,4 +31,4 @@ do { } } while (true); -?> +?> diff --git a/php/step1_read_print.php b/php/step1_read_print.php index 808ea09..c9c5267 100644 --- a/php/step1_read_print.php +++ b/php/step1_read_print.php @@ -25,6 +25,7 @@ function rep($str) { return MAL_PRINT(MAL_EVAL(READ($str), array())); } +// repl loop do { try { $line = mal_readline("user> "); @@ -40,4 +41,4 @@ do { } } while (true); -?> +?> diff --git a/php/step2_eval.php b/php/step2_eval.php index 1cec7f7..b9d2e8e 100644 --- a/php/step2_eval.php +++ b/php/step2_eval.php @@ -61,6 +61,7 @@ $repl_env['-'] = function ($a, $b) { return intval($a - $b,10); }; $repl_env['*'] = function ($a, $b) { return intval($a * $b,10); }; $repl_env['/'] = function ($a, $b) { return intval($a / $b,10); }; +// repl loop do { try { $line = mal_readline("user> "); @@ -76,4 +77,4 @@ do { } } while (true); -?> +?> diff --git a/php/step3_env.php b/php/step3_env.php index 3c46b04..6585fe7 100644 --- a/php/step3_env.php +++ b/php/step3_env.php @@ -78,6 +78,7 @@ $repl_env->set('-', function ($a, $b) { return intval($a - $b,10); }); $repl_env->set('*', function ($a, $b) { return intval($a * $b,10); }); $repl_env->set('/', function ($a, $b) { return intval($a / $b,10); }); +// repl loop do { try { $line = mal_readline("user> "); @@ -93,4 +94,4 @@ do { } } while (true); -?> +?> diff --git a/php/step4_if_fn_do.php b/php/step4_if_fn_do.php index 83734b1..1e88c6a 100644 --- a/php/step4_if_fn_do.php +++ b/php/step4_if_fn_do.php @@ -99,6 +99,7 @@ foreach ($core_ns as $k=>$v) { // core.mal: defined using the language itself rep("(def! not (fn* (a) (if a false true)))"); +// repl loop do { try { $line = mal_readline("user> "); @@ -114,4 +115,4 @@ do { } } while (true); -?> +?> diff --git a/php/step5_tco.php b/php/step5_tco.php index 31e2980..5ae29a7 100644 --- a/php/step5_tco.php +++ b/php/step5_tco.php @@ -108,6 +108,7 @@ foreach ($core_ns as $k=>$v) { // core.mal: defined using the language itself rep("(def! not (fn* (a) (if a false true)))"); +// repl loop do { try { $line = mal_readline("user> "); @@ -123,4 +124,4 @@ do { } } while (true); -?> +?> diff --git a/php/step6_file.php b/php/step6_file.php index 0a632c0..b7cdcc3 100644 --- a/php/step6_file.php +++ b/php/step6_file.php @@ -107,18 +107,22 @@ foreach ($core_ns as $k=>$v) { $repl_env->set('eval', _function(function($ast) { global $repl_env; return MAL_EVAL($ast, $repl_env); })); +$_argv = _list(); +for ($i=2; $i < count($argv); $i++) { + $_argv->append($argv[$i]); +} +$repl_env->set('*ARGV*', $_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) \")\")))))"); if (count($argv) > 1) { - for ($i=1; $i < count($argv); $i++) { - rep('(load-file "' . $argv[$i] . '")'); - } + rep('(load-file "' . $argv[1] . '")'); exit(0); } +// repl loop do { try { $line = mal_readline("user> "); @@ -134,4 +138,4 @@ do { } } while (true); -?> +?> diff --git a/php/step7_quote.php b/php/step7_quote.php index 8296c1a..fd1c922 100644 --- a/php/step7_quote.php +++ b/php/step7_quote.php @@ -130,18 +130,22 @@ foreach ($core_ns as $k=>$v) { $repl_env->set('eval', _function(function($ast) { global $repl_env; return MAL_EVAL($ast, $repl_env); })); +$_argv = _list(); +for ($i=2; $i < count($argv); $i++) { + $_argv->append($argv[$i]); +} +$repl_env->set('*ARGV*', $_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) \")\")))))"); if (count($argv) > 1) { - for ($i=1; $i < count($argv); $i++) { - rep('(load-file "' . $argv[$i] . '")'); - } + rep('(load-file "' . $argv[1] . '")'); exit(0); } +// repl loop do { try { $line = mal_readline("user> "); @@ -157,4 +161,4 @@ do { } } while (true); -?> +?> diff --git a/php/step8_macros.php b/php/step8_macros.php index 4723ffd..b33111d 100644 --- a/php/step8_macros.php +++ b/php/step8_macros.php @@ -155,6 +155,11 @@ foreach ($core_ns as $k=>$v) { $repl_env->set('eval', _function(function($ast) { global $repl_env; return MAL_EVAL($ast, $repl_env); })); +$_argv = _list(); +for ($i=2; $i < count($argv); $i++) { + $_argv->append($argv[$i]); +} +$repl_env->set('*ARGV*', $_argv); // core.mal: defined using the language itself rep("(def! not (fn* (a) (if a false true)))"); @@ -163,12 +168,11 @@ rep("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if ( 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))))))))"); if (count($argv) > 1) { - for ($i=1; $i < count($argv); $i++) { - rep('(load-file "' . $argv[$i] . '")'); - } + rep('(load-file "' . $argv[1] . '")'); exit(0); } +// repl loop do { try { $line = mal_readline("user> "); @@ -184,4 +188,4 @@ do { } } while (true); -?> +?> diff --git a/php/step9_interop.php b/php/step9_interop.php index a46864c..f35244c 100644 --- a/php/step9_interop.php +++ b/php/step9_interop.php @@ -157,6 +157,11 @@ foreach ($core_ns as $k=>$v) { $repl_env->set('eval', _function(function($ast) { global $repl_env; return MAL_EVAL($ast, $repl_env); })); +$_argv = _list(); +for ($i=2; $i < count($argv); $i++) { + $_argv->append($argv[$i]); +} +$repl_env->set('*ARGV*', $_argv); // core.mal: defined using the language itself rep("(def! not (fn* (a) (if a false true)))"); @@ -165,12 +170,11 @@ rep("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if ( 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))))))))"); if (count($argv) > 1) { - for ($i=1; $i < count($argv); $i++) { - rep('(load-file "' . $argv[$i] . '")'); - } + rep('(load-file "' . $argv[1] . '")'); exit(0); } +// repl loop do { try { $line = mal_readline("user> "); @@ -186,4 +190,4 @@ do { } } while (true); -?> +?> diff --git a/php/stepA_more.php b/php/stepA_more.php index 2eae095..0f4c8ae 100644 --- a/php/stepA_more.php +++ b/php/stepA_more.php @@ -175,6 +175,11 @@ foreach ($core_ns as $k=>$v) { $repl_env->set('eval', _function(function($ast) { global $repl_env; return MAL_EVAL($ast, $repl_env); })); +$_argv = _list(); +for ($i=2; $i < count($argv); $i++) { + $_argv->append($argv[$i]); +} +$repl_env->set('*ARGV*', $_argv); // core.mal: defined using the language itself rep("(def! *host-language* \"php\")"); @@ -184,12 +189,12 @@ rep("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if ( 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))))))))"); if (count($argv) > 1) { - for ($i=1; $i < count($argv); $i++) { - rep('(load-file "' . $argv[$i] . '")'); - } + rep('(load-file "' . $argv[1] . '")'); exit(0); } +// repl loop +rep("(println (str \"Mal [\" *host-language* \"]\"))"); do { try { $line = mal_readline("user> "); @@ -205,4 +210,4 @@ do { } } while (true); -?> +?> diff --git a/ps/step0_repl.ps b/ps/step0_repl.ps index 046e5a1..d26844c 100644 --- a/ps/step0_repl.ps +++ b/ps/step0_repl.ps @@ -28,7 +28,8 @@ % repl /REP { READ (stub env) EVAL PRINT } def -{ % loop +% repl loop +{ %loop (user> ) _readline not { exit } if % exit if EOF diff --git a/ps/step1_read_print.ps b/ps/step1_read_print.ps index b4c6275..476c917 100644 --- a/ps/step1_read_print.ps +++ b/ps/step1_read_print.ps @@ -29,7 +29,8 @@ end } def % repl /REP { READ (stub env) EVAL PRINT } def -{ % loop +% repl loop +{ %loop (user> ) _readline not { exit } if % exit if EOF diff --git a/ps/step2_eval.ps b/ps/step2_eval.ps index d5c956b..551c637 100644 --- a/ps/step2_eval.ps +++ b/ps/step2_eval.ps @@ -71,7 +71,8 @@ end } def /REP { READ repl_env EVAL PRINT } def -{ % loop +% repl loop +{ %loop (user> ) _readline not { exit } if % exit if EOF diff --git a/ps/step3_env.ps b/ps/step3_env.ps index 42eda2c..92dc26e 100644 --- a/ps/step3_env.ps +++ b/ps/step3_env.ps @@ -88,7 +88,8 @@ end } def (*) { dup 0 _nth exch 1 _nth mul } _ref (/) { dup 0 _nth exch 1 _nth idiv } _ref -{ % loop +% repl loop +{ %loop (user> ) _readline not { exit } if % exit if EOF diff --git a/ps/step4_if_fn_do.ps b/ps/step4_if_fn_do.ps index b01c594..9e628b6 100644 --- a/ps/step4_if_fn_do.ps +++ b/ps/step4_if_fn_do.ps @@ -113,13 +113,14 @@ end } def /REP { READ repl_env EVAL PRINT } def % core.ps: defined using postscript -/_ref { _function repl_env 3 1 roll env_set pop } def -core_ns { _ref } forall +/_ref { repl_env 3 1 roll env_set pop } def +core_ns { _function _ref } forall % core.mal: defined using the language itself (\(def! not \(fn* \(a\) \(if a false true\)\)\)) RE pop -{ % loop +% repl loop +{ %loop (user> ) _readline not { exit } if % exit if EOF diff --git a/ps/step5_tco.ps b/ps/step5_tco.ps index 50bcdbf..c1698d6 100644 --- a/ps/step5_tco.ps +++ b/ps/step5_tco.ps @@ -123,13 +123,14 @@ end } def /REP { READ repl_env EVAL PRINT } def % core.ps: defined using postscript -/_ref { _function repl_env 3 1 roll env_set pop } def -core_ns { _ref } forall +/_ref { repl_env 3 1 roll env_set pop } def +core_ns { _function _ref } forall % core.mal: defined using the language itself (\(def! not \(fn* \(a\) \(if a false true\)\)\)) RE pop -{ % loop +% repl loop +{ %loop (user> ) _readline not { exit } if % exit if EOF diff --git a/ps/step6_file.ps b/ps/step6_file.ps index faa7101..00598be 100644 --- a/ps/step6_file.ps +++ b/ps/step6_file.ps @@ -123,9 +123,10 @@ end } def /REP { READ repl_env EVAL PRINT } def % core.ps: defined using postscript -/_ref { _function repl_env 3 1 roll env_set pop } def -core_ns { _ref } forall -(eval) { 0 _nth repl_env EVAL } _ref +/_ref { repl_env 3 1 roll env_set pop } def +core_ns { _function _ref } forall +(eval) { 0 _nth repl_env EVAL } _function _ref +(*ARGV*) [ ] _list_from_array _ref % core.mal: defined using the language itself (\(def! not \(fn* \(a\) \(if a false true\)\)\)) RE pop @@ -133,13 +134,16 @@ core_ns { _ref } forall userdict /ARGUMENTS known { %if command line arguments ARGUMENTS length 0 gt { %if more than 0 arguments - ARGUMENTS { - (\(load-file ") exch ("\)) concatenate concatenate RE pop - } forall + (*ARGV*) ARGUMENTS 1 ARGUMENTS length 1 sub getinterval + _list_from_array _ref + ARGUMENTS 0 get + (\(load-file ") exch ("\)) concatenate concatenate RE pop quit } if } if -{ % loop + +% repl loop +{ %loop (user> ) _readline not { exit } if % exit if EOF diff --git a/ps/step7_quote.ps b/ps/step7_quote.ps index 93bf464..10c8089 100644 --- a/ps/step7_quote.ps +++ b/ps/step7_quote.ps @@ -155,9 +155,10 @@ end } def /REP { READ repl_env EVAL PRINT } def % core.ps: defined using postscript -/_ref { _function repl_env 3 1 roll env_set pop } def -core_ns { _ref } forall -(eval) { 0 _nth repl_env EVAL } _ref +/_ref { repl_env 3 1 roll env_set pop } def +core_ns { _function _ref } forall +(eval) { 0 _nth repl_env EVAL } _function _ref +(*ARGV*) [ ] _list_from_array _ref % core.mal: defined using the language itself (\(def! not \(fn* \(a\) \(if a false true\)\)\)) RE pop @@ -165,13 +166,16 @@ core_ns { _ref } forall userdict /ARGUMENTS known { %if command line arguments ARGUMENTS length 0 gt { %if more than 0 arguments - ARGUMENTS { - (\(load-file ") exch ("\)) concatenate concatenate RE pop - } forall + (*ARGV*) ARGUMENTS 1 ARGUMENTS length 1 sub getinterval + _list_from_array _ref + ARGUMENTS 0 get + (\(load-file ") exch ("\)) concatenate concatenate RE pop quit } if } if -{ % loop + +% repl loop +{ %loop (user> ) _readline not { exit } if % exit if EOF diff --git a/ps/step8_macros.ps b/ps/step8_macros.ps index 814b62b..74cf50f 100644 --- a/ps/step8_macros.ps +++ b/ps/step8_macros.ps @@ -197,9 +197,10 @@ end } def /REP { READ repl_env EVAL PRINT } def % core.ps: defined using postscript -/_ref { _function repl_env 3 1 roll env_set pop } def -core_ns { _ref } forall -(eval) { 0 _nth repl_env EVAL } _ref +/_ref { repl_env 3 1 roll env_set pop } def +core_ns { _function _ref } forall +(eval) { 0 _nth repl_env EVAL } _function _ref +(*ARGV*) [ ] _list_from_array _ref % core.mal: defined using the language itself (\(def! not \(fn* \(a\) \(if a false true\)\)\)) RE pop @@ -209,13 +210,16 @@ core_ns { _ref } forall userdict /ARGUMENTS known { %if command line arguments ARGUMENTS length 0 gt { %if more than 0 arguments - ARGUMENTS { - (\(load-file ") exch ("\)) concatenate concatenate RE pop - } forall + (*ARGV*) ARGUMENTS 1 ARGUMENTS length 1 sub getinterval + _list_from_array _ref + ARGUMENTS 0 get + (\(load-file ") exch ("\)) concatenate concatenate RE pop quit } if } if -{ % loop + +% repl loop +{ %loop (user> ) _readline not { exit } if % exit if EOF diff --git a/ps/step9_interop.ps b/ps/step9_interop.ps index e8d837f..4f324d5 100644 --- a/ps/step9_interop.ps +++ b/ps/step9_interop.ps @@ -211,9 +211,10 @@ end } def /REP { READ repl_env EVAL PRINT } def % core.ps: defined using postscript -/_ref { _function repl_env 3 1 roll env_set pop } def -core_ns { _ref } forall -(eval) { 0 _nth repl_env EVAL } _ref +/_ref { repl_env 3 1 roll env_set pop } def +core_ns { _function _ref } forall +(eval) { 0 _nth repl_env EVAL } _function _ref +(*ARGV*) [ ] _list_from_array _ref % core.mal: defined using the language itself (\(def! not \(fn* \(a\) \(if a false true\)\)\)) RE pop @@ -223,13 +224,16 @@ core_ns { _ref } forall userdict /ARGUMENTS known { %if command line arguments ARGUMENTS length 0 gt { %if more than 0 arguments - ARGUMENTS { - (\(load-file ") exch ("\)) concatenate concatenate RE pop - } forall + (*ARGV*) ARGUMENTS 1 ARGUMENTS length 1 sub getinterval + _list_from_array _ref + ARGUMENTS 0 get + (\(load-file ") exch ("\)) concatenate concatenate RE pop quit } if } if -{ % loop + +% repl loop +{ %loop (user> ) _readline not { exit } if % exit if EOF diff --git a/ps/stepA_more.ps b/ps/stepA_more.ps index 73403b0..744a092 100644 --- a/ps/stepA_more.ps +++ b/ps/stepA_more.ps @@ -247,9 +247,10 @@ end } def /REP { READ repl_env EVAL PRINT } def % core.ps: defined using postscript -/_ref { _function repl_env 3 1 roll env_set pop } def -core_ns { _ref } forall -(eval) { 0 _nth repl_env EVAL } _ref +/_ref { repl_env 3 1 roll env_set pop } def +core_ns { _function _ref } forall +(eval) { 0 _nth repl_env EVAL } _function _ref +(*ARGV*) [ ] _list_from_array _ref % core.mal: defined using the language itself (\(def! *host-language* "postscript"\)) RE pop @@ -260,13 +261,17 @@ core_ns { _ref } forall userdict /ARGUMENTS known { %if command line arguments ARGUMENTS length 0 gt { %if more than 0 arguments - ARGUMENTS { - (\(load-file ") exch ("\)) concatenate concatenate RE pop - } forall + (*ARGV*) ARGUMENTS 1 ARGUMENTS length 1 sub getinterval + _list_from_array _ref + ARGUMENTS 0 get + (\(load-file ") exch ("\)) concatenate concatenate RE pop quit } if } if -{ % loop + +% repl loop +(\(println \(str "Mal [" *host-language* "]"\)\)) RE pop +{ %loop (user> ) _readline not { exit } if % exit if EOF diff --git a/python/step0_repl.py b/python/step0_repl.py index 8d42c33..bb4d6bf 100644 --- a/python/step0_repl.py +++ b/python/step0_repl.py @@ -22,6 +22,7 @@ def PRINT(exp): def REP(str): return PRINT(EVAL(READ(str), {})) +# repl loop while True: try: line = mal_readline.readline("user> ") @@ -29,4 +30,4 @@ while True: if line == "": continue print(REP(line)) except Exception as e: - print "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2])) + print("".join(traceback.format_exception(*sys.exc_info()))) diff --git a/python/step1_read_print.py b/python/step1_read_print.py index 7c5b7d8..3c2e4ac 100644 --- a/python/step1_read_print.py +++ b/python/step1_read_print.py @@ -9,7 +9,7 @@ def READ(str): # eval def EVAL(ast, env): - #print("EVAL %s" % ast) + #print("EVAL %s" % printer._pr_str(ast)) return ast def PRINT(exp): @@ -19,6 +19,7 @@ def PRINT(exp): def REP(str): return PRINT(EVAL(READ(str), {})) +# repl loop while True: try: line = mal_readline.readline("user> ") diff --git a/python/step2_eval.py b/python/step2_eval.py index a5061dc..8af09b1 100644 --- a/python/step2_eval.py +++ b/python/step2_eval.py @@ -28,7 +28,7 @@ def eval_ast(ast, env): return ast # primitive value, return unchanged def EVAL(ast, env): - #print("EVAL %s" % ast) + #print("EVAL %s" % printer._pr_str(ast)) if not types._list_Q(ast): return eval_ast(ast, env) @@ -50,6 +50,7 @@ repl_env['-'] = lambda a,b: a-b repl_env['*'] = lambda a,b: a*b repl_env['/'] = lambda a,b: int(a/b) +# repl loop while True: try: line = mal_readline.readline("user> ") diff --git a/python/step3_env.py b/python/step3_env.py index 372adc4..4565011 100644 --- a/python/step3_env.py +++ b/python/step3_env.py @@ -26,7 +26,7 @@ def eval_ast(ast, env): return ast # primitive value, return unchanged def EVAL(ast, env): - #print("EVAL %s" % ast) + #print("EVAL %s" % printer._pr_str(ast)) if not types._list_Q(ast): return eval_ast(ast, env) @@ -63,6 +63,7 @@ repl_env.set('-', lambda a,b: a-b) repl_env.set('*', lambda a,b: a*b) repl_env.set('/', lambda a,b: int(a/b)) +# repl loop while True: try: line = mal_readline.readline("user> ") diff --git a/python/step4_if_fn_do.py b/python/step4_if_fn_do.py index a06d10b..e99cfcf 100644 --- a/python/step4_if_fn_do.py +++ b/python/step4_if_fn_do.py @@ -27,7 +27,7 @@ def eval_ast(ast, env): return ast # primitive value, return unchanged def EVAL(ast, env): - #print("EVAL %s" % ast) + #print("EVAL %s" % printer._pr_str(ast)) if not types._list_Q(ast): return eval_ast(ast, env) @@ -79,6 +79,7 @@ for k, v in core.ns.items(): repl_env.set(k, v) # core.mal: defined using the language itself REP("(def! not (fn* (a) (if a false true)))") +# repl loop while True: try: line = mal_readline.readline("user> ") diff --git a/python/step5_tco.py b/python/step5_tco.py index 7982073..72457e9 100644 --- a/python/step5_tco.py +++ b/python/step5_tco.py @@ -28,7 +28,7 @@ def eval_ast(ast, env): def EVAL(ast, env): while True: - #print("EVAL %s" % ast) + #print("EVAL %s" % printer._pr_str(ast)) if not types._list_Q(ast): return eval_ast(ast, env) @@ -86,6 +86,7 @@ for k, v in core.ns.items(): repl_env.set(k, v) # core.mal: defined using the language itself REP("(def! not (fn* (a) (if a false true)))") +# repl loop while True: try: line = mal_readline.readline("user> ") diff --git a/python/step6_file.py b/python/step6_file.py index ba2d355..764eb53 100644 --- a/python/step6_file.py +++ b/python/step6_file.py @@ -28,7 +28,7 @@ def eval_ast(ast, env): def EVAL(ast, env): while True: - #print("EVAL %s" % ast) + #print("EVAL %s" % printer._pr_str(ast)) if not types._list_Q(ast): return eval_ast(ast, env) @@ -83,6 +83,7 @@ def REP(str): # core.py: defined using python for k, v in core.ns.items(): repl_env.set(k, v) repl_env.set('eval', lambda ast: EVAL(ast, repl_env)) +repl_env.set('*ARGV*', types._list(*sys.argv[2:])) # core.mal: defined using the language itself REP("(def! not (fn* (a) (if a false true)))") @@ -90,13 +91,15 @@ REP("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")) if len(sys.argv) >= 2: REP('(load-file "' + sys.argv[1] + '")') -else: - while True: - try: - line = mal_readline.readline("user> ") - if line == None: break - if line == "": continue - print(REP(line)) - except reader.Blank: continue - except Exception as e: - print("".join(traceback.format_exception(*sys.exc_info()))) + sys.exit(0) + +# repl loop +while True: + try: + line = mal_readline.readline("user> ") + if line == None: break + if line == "": continue + print(REP(line)) + except reader.Blank: continue + except Exception as e: + print("".join(traceback.format_exception(*sys.exc_info()))) diff --git a/python/step7_quote.py b/python/step7_quote.py index aefa421..1002613 100644 --- a/python/step7_quote.py +++ b/python/step7_quote.py @@ -46,7 +46,7 @@ def eval_ast(ast, env): def EVAL(ast, env): while True: - #print("EVAL %s" % ast) + #print("EVAL %s" % printer._pr_str(ast)) if not types._list_Q(ast): return eval_ast(ast, env) @@ -105,6 +105,7 @@ def REP(str): # core.py: defined using python for k, v in core.ns.items(): repl_env.set(k, v) repl_env.set('eval', lambda ast: EVAL(ast, repl_env)) +repl_env.set('*ARGV*', types._list(*sys.argv[2:])) # core.mal: defined using the language itself REP("(def! not (fn* (a) (if a false true)))") @@ -112,13 +113,15 @@ REP("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")) if len(sys.argv) >= 2: REP('(load-file "' + sys.argv[1] + '")') -else: - while True: - try: - line = mal_readline.readline("user> ") - if line == None: break - if line == "": continue - print(REP(line)) - except reader.Blank: continue - except Exception as e: - print("".join(traceback.format_exception(*sys.exc_info()))) + sys.exit(0) + +# repl loop +while True: + try: + line = mal_readline.readline("user> ") + if line == None: break + if line == "": continue + print(REP(line)) + except reader.Blank: continue + except Exception as e: + print("".join(traceback.format_exception(*sys.exc_info()))) diff --git a/python/step8_macros.py b/python/step8_macros.py index 90aedf3..28b68fb 100644 --- a/python/step8_macros.py +++ b/python/step8_macros.py @@ -58,7 +58,7 @@ def eval_ast(ast, env): def EVAL(ast, env): while True: - #print("EVAL %s" % ast) + #print("EVAL %s" % printer._pr_str(ast)) if not types._list_Q(ast): return eval_ast(ast, env) @@ -125,6 +125,7 @@ def REP(str): # core.py: defined using python for k, v in core.ns.items(): repl_env.set(k, v) repl_env.set('eval', lambda ast: EVAL(ast, repl_env)) +repl_env.set('*ARGV*', types._list(*sys.argv[2:])) # core.mal: defined using the language itself REP("(def! not (fn* (a) (if a false true)))") @@ -134,13 +135,15 @@ REP("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first x if len(sys.argv) >= 2: REP('(load-file "' + sys.argv[1] + '")') -else: - while True: - try: - line = mal_readline.readline("user> ") - if line == None: break - if line == "": continue - print(REP(line)) - except reader.Blank: continue - except Exception as e: - print("".join(traceback.format_exception(*sys.exc_info()))) + sys.exit(0) + +# repl loop +while True: + try: + line = mal_readline.readline("user> ") + if line == None: break + if line == "": continue + print(REP(line)) + except reader.Blank: continue + except Exception as e: + print("".join(traceback.format_exception(*sys.exc_info()))) diff --git a/python/step9_interop.py b/python/step9_interop.py index eae7837..2f7c5e0 100644 --- a/python/step9_interop.py +++ b/python/step9_interop.py @@ -58,7 +58,7 @@ def eval_ast(ast, env): def EVAL(ast, env): while True: - #print("EVAL %s" % ast) + #print("EVAL %s" % printer._pr_str(ast)) if not types._list_Q(ast): return eval_ast(ast, env) @@ -89,7 +89,10 @@ def EVAL(ast, env): elif 'macroexpand' == a0: return macroexpand(ast[1], env) elif "py!*" == a0: - exec compile(ast[1], '', 'single') in globals() + if sys.version_info[0] >= 3: + exec(compile(ast[1], '', 'single'), globals()) + else: + exec(compile(ast[1], '', 'single') in globals()) return None elif "py*" == a0: return eval(ast[1]) @@ -134,6 +137,7 @@ def REP(str): # core.py: defined using python for k, v in core.ns.items(): repl_env.set(k, v) repl_env.set('eval', lambda ast: EVAL(ast, repl_env)) +repl_env.set('*ARGV*', types._list(*sys.argv[2:])) # core.mal: defined using the language itself REP("(def! not (fn* (a) (if a false true)))") @@ -143,13 +147,15 @@ REP("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first x if len(sys.argv) >= 2: REP('(load-file "' + sys.argv[1] + '")') -else: - while True: - try: - line = mal_readline.readline("user> ") - if line == None: break - if line == "": continue - print(REP(line)) - except reader.Blank: continue - except Exception as e: - print("".join(traceback.format_exception(*sys.exc_info()))) + sys.exit(0) + +# repl loop +while True: + try: + line = mal_readline.readline("user> ") + if line == None: break + if line == "": continue + print(REP(line)) + except reader.Blank: continue + except Exception as e: + print("".join(traceback.format_exception(*sys.exc_info()))) diff --git a/python/stepA_more.py b/python/stepA_more.py index e4bc768..b9d8152 100644 --- a/python/stepA_more.py +++ b/python/stepA_more.py @@ -148,6 +148,7 @@ def REP(str): # core.py: defined using python for k, v in core.ns.items(): repl_env.set(k, v) repl_env.set('eval', lambda ast: EVAL(ast, repl_env)) +repl_env.set('*ARGV*', types._list(*sys.argv[2:])) # core.mal: defined using the language itself REP("(def! *host-language* \"python\")") @@ -158,13 +159,16 @@ REP("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first x if len(sys.argv) >= 2: REP('(load-file "' + sys.argv[1] + '")') -else: - while True: - try: - line = mal_readline.readline("user> ") - if line == None: break - if line == "": continue - print(REP(line)) - except reader.Blank: continue - except Exception as e: - print("".join(traceback.format_exception(*sys.exc_info()))) + sys.exit(0) + +# repl loop +REP("(println (str \"Mal [\" *host-language* \"]\"))") +while True: + try: + line = mal_readline.readline("user> ") + if line == None: break + if line == "": continue + print(REP(line)) + except reader.Blank: continue + except Exception as e: + print("".join(traceback.format_exception(*sys.exc_info()))) diff --git a/ruby/step0_repl.rb b/ruby/step0_repl.rb index 21d534a..9c03cfa 100644 --- a/ruby/step0_repl.rb +++ b/ruby/step0_repl.rb @@ -21,6 +21,7 @@ def REP(str) return PRINT(EVAL(READ(str), {})) end +# repl loop while line = _readline("user> ") puts REP(line) end diff --git a/ruby/step1_read_print.rb b/ruby/step1_read_print.rb index 617323e..ded992a 100644 --- a/ruby/step1_read_print.rb +++ b/ruby/step1_read_print.rb @@ -24,10 +24,12 @@ def REP(str) return PRINT(EVAL(READ(str), {})) end +# repl loop while line = _readline("user> ") begin puts REP(line) rescue Exception => e - puts "Error: %{e}" + puts "Error: #{e}" + puts "\t#{e.backtrace.join("\n\t")}" end end diff --git a/ruby/step2_eval.rb b/ruby/step2_eval.rb index 1454d8c..50a135d 100644 --- a/ruby/step2_eval.rb +++ b/ruby/step2_eval.rb @@ -55,6 +55,7 @@ repl_env[:-] = lambda {|a,b| a - b} repl_env[:*] = lambda {|a,b| a * b} repl_env[:/] = lambda {|a,b| a / b} +# repl loop while line = _readline("user> ") begin puts REP[line] diff --git a/ruby/step3_env.rb b/ruby/step3_env.rb index 88811f0..17126c5 100644 --- a/ruby/step3_env.rb +++ b/ruby/step3_env.rb @@ -67,6 +67,7 @@ repl_env.set(:-, lambda {|a,b| a - b}) repl_env.set(:*, lambda {|a,b| a * b}) repl_env.set(:/, lambda {|a,b| a / b}) +# repl loop while line = _readline("user> ") begin puts REP[line] diff --git a/ruby/step4_if_fn_do.rb b/ruby/step4_if_fn_do.rb index 4ae0d86..a93463b 100644 --- a/ruby/step4_if_fn_do.rb +++ b/ruby/step4_if_fn_do.rb @@ -81,11 +81,11 @@ REP = lambda {|str| PRINT(EVAL(READ(str), repl_env)) } # core.rb: defined using ruby $core_ns.each do |k,v| repl_env.set(k,v) end -repl_env.set(:eval, lambda {|ast| EVAL(ast, repl_env)}) # core.mal: defined using the language itself RE["(def! not (fn* (a) (if a false true)))"] +# repl loop while line = _readline("user> ") begin puts REP[line] diff --git a/ruby/step5_tco.rb b/ruby/step5_tco.rb index e2fe5e7..6be04ae 100644 --- a/ruby/step5_tco.rb +++ b/ruby/step5_tco.rb @@ -90,11 +90,11 @@ REP = lambda {|str| PRINT(EVAL(READ(str), repl_env)) } # core.rb: defined using ruby $core_ns.each do |k,v| repl_env.set(k,v) end -repl_env.set(:eval, lambda {|ast| EVAL(ast, repl_env)}) # core.mal: defined using the language itself RE["(def! not (fn* (a) (if a false true)))"] +# repl loop while line = _readline("user> ") begin puts REP[line] diff --git a/ruby/step6_file.rb b/ruby/step6_file.rb index 45de088..191febe 100644 --- a/ruby/step6_file.rb +++ b/ruby/step6_file.rb @@ -91,17 +91,18 @@ REP = lambda {|str| PRINT(EVAL(READ(str), repl_env)) } # core.rb: defined using ruby $core_ns.each do |k,v| repl_env.set(k,v) end repl_env.set(:eval, lambda {|ast| EVAL(ast, repl_env)}) +repl_env.set(:"*ARGV*", List.new(ARGV.slice(1,ARGV.length) || [])) # core.mal: defined using the language itself RE["(def! not (fn* (a) (if a false true)))"] RE["(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"] if ARGV.size > 0 - ARGV.each {|f| - RE["(load-file \"" + f + "\")"] - } + RE["(load-file \"" + ARGV[0] + "\")"] exit 0 end + +# repl loop while line = _readline("user> ") begin puts REP[line] diff --git a/ruby/step7_quote.rb b/ruby/step7_quote.rb index e91058b..18b7db3 100644 --- a/ruby/step7_quote.rb +++ b/ruby/step7_quote.rb @@ -111,17 +111,18 @@ REP = lambda {|str| PRINT(EVAL(READ(str), repl_env)) } # core.rb: defined using ruby $core_ns.each do |k,v| repl_env.set(k,v) end repl_env.set(:eval, lambda {|ast| EVAL(ast, repl_env)}) +repl_env.set(:"*ARGV*", List.new(ARGV.slice(1,ARGV.length) || [])) # core.mal: defined using the language itself RE["(def! not (fn* (a) (if a false true)))"] RE["(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"] if ARGV.size > 0 - ARGV.each {|f| - RE["(load-file \"" + f + "\")"] - } + RE["(load-file \"" + ARGV[0] + "\")"] exit 0 end + +# repl loop while line = _readline("user> ") begin puts REP[line] diff --git a/ruby/step8_macros.rb b/ruby/step8_macros.rb index dd02c4f..3c99cce 100644 --- a/ruby/step8_macros.rb +++ b/ruby/step8_macros.rb @@ -136,6 +136,7 @@ REP = lambda {|str| PRINT(EVAL(READ(str), repl_env)) } # core.rb: defined using ruby $core_ns.each do |k,v| repl_env.set(k,v) end repl_env.set(:eval, lambda {|ast| EVAL(ast, repl_env)}) +repl_env.set(:"*ARGV*", List.new(ARGV.slice(1,ARGV.length) || [])) # core.mal: defined using the language itself RE["(def! not (fn* (a) (if a false true)))"] @@ -144,11 +145,11 @@ RE["(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> RE["(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))))))))"] if ARGV.size > 0 - ARGV.each {|f| - RE["(load-file \"" + f + "\")"] - } + RE["(load-file \"" + ARGV[0] + "\")"] exit 0 end + +# repl loop while line = _readline("user> ") begin puts REP[line] diff --git a/ruby/step9_interop.rb b/ruby/step9_interop.rb index 2c56de4..507114e 100644 --- a/ruby/step9_interop.rb +++ b/ruby/step9_interop.rb @@ -138,6 +138,7 @@ REP = lambda {|str| PRINT(EVAL(READ(str), repl_env)) } # core.rb: defined using ruby $core_ns.each do |k,v| repl_env.set(k,v) end repl_env.set(:eval, lambda {|ast| EVAL(ast, repl_env)}) +repl_env.set(:"*ARGV*", List.new(ARGV.slice(1,ARGV.length) || [])) # core.mal: defined using the language itself RE["(def! not (fn* (a) (if a false true)))"] @@ -146,11 +147,11 @@ RE["(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> RE["(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))))))))"] if ARGV.size > 0 - ARGV.each {|f| - RE["(load-file \"" + f + "\")"] - } + RE["(load-file \"" + ARGV[0] + "\")"] exit 0 end + +# repl loop while line = _readline("user> ") begin puts REP[line] diff --git a/ruby/stepA_more.rb b/ruby/stepA_more.rb index 697caa3..101dee3 100644 --- a/ruby/stepA_more.rb +++ b/ruby/stepA_more.rb @@ -153,6 +153,7 @@ REP = lambda {|str| PRINT(EVAL(READ(str), repl_env)) } # core.rb: defined using ruby $core_ns.each do |k,v| repl_env.set(k,v) end repl_env.set(:eval, lambda {|ast| EVAL(ast, repl_env)}) +repl_env.set(:"*ARGV*", List.new(ARGV.slice(1,ARGV.length) || [])) # core.mal: defined using the language itself RE["(def! *host-language* \"ruby\")"] @@ -162,11 +163,12 @@ RE["(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> RE["(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))))))))"] if ARGV.size > 0 - ARGV.each {|f| - RE["(load-file \"" + f + "\")"] - } + RE["(load-file \"" + ARGV[0] + "\")"] exit 0 end + +# repl loop +RE["(println (str \"Mal [\" *host-language* \"]\"))"] while line = _readline("user> ") begin puts REP[line] |
