Skip to content

Commit

Permalink
All: *ARGV* and *host-language*. Misc syncing/fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
kanaka committed Apr 19, 2014
1 parent 718887c commit 86b689f
Show file tree
Hide file tree
Showing 126 changed files with 761 additions and 488 deletions.
21 changes: 9 additions & 12 deletions bash/step1_read_print.sh
Original file line number Diff line number Diff line change
@@ -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}"
Expand All @@ -20,7 +18,7 @@ EVAL () {
r="${ast}"
}

# PRINT:
# print
PRINT () {
if [[ "${__ERROR}" ]]; then
_pr_str "${__ERROR}" yes
Expand All @@ -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
22 changes: 10 additions & 12 deletions bash/step2_eval.sh
Original file line number Diff line number Diff line change
@@ -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}'"
Expand Down Expand Up @@ -39,7 +38,6 @@ EVAL_AST () {
esac
}

# EVAL: evaluate the parameter
EVAL () {
local ast="${1}" env="${2}"
r=
Expand All @@ -61,7 +59,7 @@ EVAL () {
eval ${f} ${args}
}

# PRINT:
# print
PRINT () {
if [[ "${__ERROR}" ]]; then
_pr_str "${__ERROR}" yes
Expand All @@ -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}"
Expand All @@ -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
21 changes: 9 additions & 12 deletions bash/step3_env.sh
Original file line number Diff line number Diff line change
@@ -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}'"
Expand Down Expand Up @@ -40,7 +39,6 @@ EVAL_AST () {
esac
}

# EVAL: evaluate the parameter
EVAL () {
local ast="${1}" env="${2}"
r=
Expand Down Expand Up @@ -84,7 +82,7 @@ EVAL () {
esac
}
# PRINT:
# print
PRINT () {
if [[ "${__ERROR}" ]]; then
_pr_str "${__ERROR}" yes
Expand All @@ -95,7 +93,7 @@ PRINT () {
fi
}
# REPL: read, eval, print, loop
# repl
ENV; REPL_ENV="${r}"
REP () {
r=
Expand All @@ -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
21 changes: 9 additions & 12 deletions bash/step4_if_fn_do.sh
Original file line number Diff line number Diff line change
@@ -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}'"
Expand Down Expand Up @@ -41,7 +40,6 @@ EVAL_AST () {
esac
}

# EVAL: evaluate the parameter
EVAL () {
local ast="${1}" env="${2}"
r=
Expand Down Expand Up @@ -107,7 +105,7 @@ EVAL () {
esac
}
# PRINT:
# print
PRINT () {
if [[ "${__ERROR}" ]]; then
_pr_str "${__ERROR}" yes
Expand All @@ -118,7 +116,7 @@ PRINT () {
fi
}
# REPL: read, eval, print, loop
# repl
ENV; REPL_ENV="${r}"
REP () {
r=
Expand All @@ -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
21 changes: 9 additions & 12 deletions bash/step5_tco.sh
Original file line number Diff line number Diff line change
@@ -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}'"
Expand Down Expand Up @@ -41,7 +40,6 @@ EVAL_AST () {
esac
}

# EVAL: evaluate the parameter
EVAL () {
local ast="${1}" env="${2}"
while true; do
Expand Down Expand Up @@ -124,7 +122,7 @@ EVAL () {
done
}
# PRINT:
# print
PRINT () {
if [[ "${__ERROR}" ]]; then
_pr_str "${__ERROR}" yes
Expand All @@ -135,7 +133,7 @@ PRINT () {
fi
}
# REPL: read, eval, print, loop
# repl
ENV; REPL_ENV="${r}"
REP () {
r=
Expand All @@ -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
31 changes: 17 additions & 14 deletions bash/step6_file.sh
Original file line number Diff line number Diff line change
@@ -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}'"
Expand Down Expand Up @@ -41,7 +40,6 @@ EVAL_AST () {
esac
}

# EVAL: evaluate the parameter
EVAL () {
local ast="${1}" env="${2}"
while true; do
Expand Down Expand Up @@ -124,7 +122,7 @@ EVAL () {
done
}
# PRINT:
# print
PRINT () {
if [[ "${__ERROR}" ]]; then
_pr_str "${__ERROR}" yes
Expand All @@ -135,7 +133,7 @@ PRINT () {
fi
}
# REPL: read, eval, print, loop
# repl
ENV; REPL_ENV="${r}"
REP () {
r=
Expand All @@ -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
Loading

0 comments on commit 86b689f

Please sign in to comment.