2015-02-10 23:59:50 +03:00
|
|
|
#!/usr/bin/env bash
|
2014-03-25 01:32:24 +04:00
|
|
|
|
|
|
|
source $(dirname $0)/reader.sh
|
2014-04-03 07:23:37 +04:00
|
|
|
source $(dirname $0)/printer.sh
|
|
|
|
source $(dirname $0)/env.sh
|
2014-04-17 08:57:50 +04:00
|
|
|
source $(dirname $0)/core.sh
|
2014-03-25 01:32:24 +04:00
|
|
|
|
2014-04-19 22:04:09 +04:00
|
|
|
# read
|
2014-03-25 01:32:24 +04:00
|
|
|
READ () {
|
2014-04-17 08:57:50 +04:00
|
|
|
[ "${1}" ] && r="${1}" || READLINE
|
2014-03-25 01:32:24 +04:00
|
|
|
READ_STR "${r}"
|
|
|
|
}
|
|
|
|
|
2014-04-19 22:04:09 +04:00
|
|
|
# eval
|
2014-03-25 01:32:24 +04:00
|
|
|
IS_PAIR () {
|
2014-04-18 06:49:07 +04:00
|
|
|
if _sequential? "${1}"; then
|
2014-03-25 01:32:24 +04:00
|
|
|
_count "${1}"
|
|
|
|
[[ "${r}" > 0 ]] && return 0
|
|
|
|
fi
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
QUASIQUOTE () {
|
|
|
|
if ! IS_PAIR "${1}"; then
|
2014-04-03 07:23:37 +04:00
|
|
|
_symbol quote
|
|
|
|
_list "${r}" "${1}"
|
2014-03-25 01:32:24 +04:00
|
|
|
return
|
|
|
|
else
|
|
|
|
_nth "${1}" 0; local a0="${r}"
|
|
|
|
if [[ "${ANON["${a0}"]}" == "unquote" ]]; then
|
|
|
|
_nth "${1}" 1
|
|
|
|
return
|
|
|
|
elif IS_PAIR "${a0}"; then
|
|
|
|
_nth "${a0}" 0; local a00="${r}"
|
|
|
|
if [[ "${ANON["${a00}"]}" == "splice-unquote" ]]; then
|
2014-04-03 07:23:37 +04:00
|
|
|
_symbol concat; local a="${r}"
|
2014-03-25 01:32:24 +04:00
|
|
|
_nth "${a0}" 1; local b="${r}"
|
2014-04-17 08:57:50 +04:00
|
|
|
_rest "${1}"
|
2014-03-25 01:32:24 +04:00
|
|
|
QUASIQUOTE "${r}"; local c="${r}"
|
2014-04-03 07:23:37 +04:00
|
|
|
_list "${a}" "${b}" "${c}"
|
2014-03-25 01:32:24 +04:00
|
|
|
return
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
2014-04-03 07:23:37 +04:00
|
|
|
_symbol cons; local a="${r}"
|
2014-03-25 01:32:24 +04:00
|
|
|
QUASIQUOTE "${a0}"; local b="${r}"
|
2014-04-17 08:57:50 +04:00
|
|
|
_rest "${1}"
|
2014-03-25 01:32:24 +04:00
|
|
|
QUASIQUOTE "${r}"; local c="${r}"
|
2014-04-03 07:23:37 +04:00
|
|
|
_list "${a}" "${b}" "${c}"
|
2014-03-25 01:32:24 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
IS_MACRO_CALL () {
|
|
|
|
if ! _list? "${1}"; then return 1; fi
|
|
|
|
_nth "${1}" 0; local a0="${r}"
|
|
|
|
if _symbol? "${a0}"; then
|
2014-12-19 05:33:49 +03:00
|
|
|
ENV_FIND "${2}" "${a0}"
|
2014-03-25 01:32:24 +04:00
|
|
|
if [[ "${r}" ]]; then
|
2014-12-19 05:33:49 +03:00
|
|
|
ENV_GET "${2}" "${a0}"
|
|
|
|
[ "${ANON["${r}_ismacro_"]}" ]
|
|
|
|
return $?
|
2014-03-25 01:32:24 +04:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
MACROEXPAND () {
|
|
|
|
local ast="${1}" env="${2}"
|
|
|
|
while IS_MACRO_CALL "${ast}" "${env}"; do
|
|
|
|
_nth "${ast}" 0; local a0="${r}"
|
2014-12-19 05:33:49 +03:00
|
|
|
ENV_GET "${env}" "${a0}"; local mac="${ANON["${r}"]}"
|
2014-04-17 08:57:50 +04:00
|
|
|
_rest "${ast}"
|
2014-03-25 01:32:24 +04:00
|
|
|
${mac%%@*} ${ANON["${r}"]}
|
|
|
|
ast="${r}"
|
|
|
|
done
|
|
|
|
r="${ast}"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EVAL_AST () {
|
|
|
|
local ast="${1}" env="${2}"
|
|
|
|
#_pr_str "${ast}"; echo "EVAL_AST '${ast}:${r} / ${env}'"
|
|
|
|
_obj_type "${ast}"; local ot="${r}"
|
|
|
|
case "${ot}" in
|
|
|
|
symbol)
|
2014-12-19 05:33:49 +03:00
|
|
|
ENV_GET "${env}" "${ast}"
|
2014-03-25 01:32:24 +04:00
|
|
|
return ;;
|
|
|
|
list)
|
2014-04-03 07:23:37 +04:00
|
|
|
_map_with_type _list EVAL "${ast}" "${env}" ;;
|
2014-03-25 01:32:24 +04:00
|
|
|
vector)
|
2014-04-03 07:23:37 +04:00
|
|
|
_map_with_type _vector EVAL "${ast}" "${env}" ;;
|
2014-03-25 01:32:24 +04:00
|
|
|
hash_map)
|
2015-12-04 01:19:22 +03:00
|
|
|
local res="" key= val="" hm="${ANON["${ast}"]}"
|
2014-04-03 07:23:37 +04:00
|
|
|
_hash_map; local new_hm="${r}"
|
2014-03-25 01:32:24 +04:00
|
|
|
eval local keys="\${!${hm}[@]}"
|
|
|
|
for key in ${keys}; do
|
|
|
|
eval val="\${${hm}[\"${key}\"]}"
|
|
|
|
EVAL "${val}" "${env}"
|
2014-04-03 07:23:37 +04:00
|
|
|
_assoc! "${new_hm}" "${key}" "${r}"
|
2014-03-25 01:32:24 +04:00
|
|
|
done
|
|
|
|
r="${new_hm}" ;;
|
|
|
|
*)
|
|
|
|
r="${ast}" ;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
EVAL () {
|
|
|
|
local ast="${1}" env="${2}"
|
|
|
|
while true; do
|
|
|
|
r=
|
|
|
|
[[ "${__ERROR}" ]] && return 1
|
|
|
|
#_pr_str "${ast}"; echo "EVAL '${r} / ${env}'"
|
|
|
|
if ! _list? "${ast}"; then
|
|
|
|
EVAL_AST "${ast}" "${env}"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
# apply list
|
|
|
|
MACROEXPAND "${ast}" "${env}"
|
|
|
|
ast="${r}"
|
2016-01-22 22:00:11 +03:00
|
|
|
if ! _list? "${ast}"; then
|
|
|
|
EVAL_AST "${ast}" "${env}"
|
|
|
|
return
|
|
|
|
fi
|
2016-03-31 21:46:28 +03:00
|
|
|
_empty? "${ast}" && r="${ast}" && return
|
|
|
|
|
2014-03-25 01:32:24 +04:00
|
|
|
_nth "${ast}" 0; local a0="${r}"
|
|
|
|
_nth "${ast}" 1; local a1="${r}"
|
|
|
|
_nth "${ast}" 2; local a2="${r}"
|
|
|
|
case "${ANON["${a0}"]}" in
|
2014-12-19 05:33:49 +03:00
|
|
|
def!) EVAL "${a2}" "${env}"
|
|
|
|
[[ "${__ERROR}" ]] && return 1
|
|
|
|
ENV_SET "${env}" "${a1}" "${r}"
|
2014-03-25 01:32:24 +04:00
|
|
|
return ;;
|
2017-10-11 22:57:07 +03:00
|
|
|
let__STAR__) ENV "${env}"; local let_env="${r}"
|
2014-03-25 01:32:24 +04:00
|
|
|
local let_pairs=(${ANON["${a1}"]})
|
|
|
|
local idx=0
|
|
|
|
#echo "let: [${let_pairs[*]}] for ${a2}"
|
|
|
|
while [[ "${let_pairs["${idx}"]}" ]]; do
|
|
|
|
EVAL "${let_pairs[$(( idx + 1))]}" "${let_env}"
|
2014-12-19 05:33:49 +03:00
|
|
|
ENV_SET "${let_env}" "${let_pairs[${idx}]}" "${r}"
|
2014-03-25 01:32:24 +04:00
|
|
|
idx=$(( idx + 2))
|
|
|
|
done
|
2014-04-24 06:59:50 +04:00
|
|
|
ast="${a2}"
|
|
|
|
env="${let_env}"
|
|
|
|
# Continue loop
|
|
|
|
;;
|
2014-03-25 01:32:24 +04:00
|
|
|
quote)
|
|
|
|
r="${a1}"
|
|
|
|
return ;;
|
|
|
|
quasiquote)
|
|
|
|
QUASIQUOTE "${a1}"
|
2014-04-24 06:59:50 +04:00
|
|
|
ast="${r}"
|
|
|
|
# Continue loop
|
|
|
|
;;
|
2014-03-25 01:32:24 +04:00
|
|
|
defmacro!)
|
|
|
|
EVAL "${a2}" "${env}"
|
2014-12-19 05:33:49 +03:00
|
|
|
[[ "${__ERROR}" ]] && return 1
|
|
|
|
ANON["${r}_ismacro_"]="yes"
|
|
|
|
ENV_SET "${env}" "${a1}" "${r}"
|
2014-03-25 01:32:24 +04:00
|
|
|
return ;;
|
|
|
|
macroexpand)
|
|
|
|
MACROEXPAND "${a1}" "${env}"
|
|
|
|
return ;;
|
2017-10-11 22:57:07 +03:00
|
|
|
sh__STAR__) EVAL "${a1}" "${env}"
|
2014-03-25 01:32:24 +04:00
|
|
|
local output=""
|
|
|
|
local line=""
|
|
|
|
while read line; do
|
|
|
|
output="${output}${line}\n"
|
|
|
|
done < <(eval ${ANON["${r}"]})
|
2014-04-28 02:58:48 +04:00
|
|
|
_string "${output%\\n}"
|
2014-03-25 01:32:24 +04:00
|
|
|
return ;;
|
2017-10-11 22:57:07 +03:00
|
|
|
try__STAR__) EVAL "${a1}" "${env}"
|
2014-03-25 01:32:24 +04:00
|
|
|
[[ -z "${__ERROR}" ]] && return
|
|
|
|
_nth "${a2}" 0; local a20="${r}"
|
|
|
|
if [ "${ANON["${a20}"]}" == "catch__STAR__" ]; then
|
|
|
|
_nth "${a2}" 1; local a21="${r}"
|
|
|
|
_nth "${a2}" 2; local a22="${r}"
|
2014-04-03 07:23:37 +04:00
|
|
|
_list "${a21}"; local binds="${r}"
|
2014-03-25 01:32:24 +04:00
|
|
|
ENV "${env}" "${binds}" "${__ERROR}"
|
|
|
|
local try_env="${r}"
|
|
|
|
__ERROR=
|
2014-12-19 05:33:49 +03:00
|
|
|
EVAL "${a22}" "${try_env}"
|
2014-03-25 01:32:24 +04:00
|
|
|
fi # if no catch* clause, just propagate __ERROR
|
|
|
|
return ;;
|
|
|
|
do) _count "${ast}"
|
|
|
|
_slice "${ast}" 1 $(( ${r} - 2 ))
|
|
|
|
EVAL_AST "${r}" "${env}"
|
|
|
|
[[ "${__ERROR}" ]] && r= && return 1
|
2014-04-17 08:57:50 +04:00
|
|
|
_last "${ast}"
|
2014-03-25 01:32:24 +04:00
|
|
|
ast="${r}"
|
|
|
|
# Continue loop
|
|
|
|
;;
|
|
|
|
if) EVAL "${a1}" "${env}"
|
2014-12-19 05:33:49 +03:00
|
|
|
[[ "${__ERROR}" ]] && return 1
|
2014-03-25 01:32:24 +04:00
|
|
|
if [[ "${r}" == "${__false}" || "${r}" == "${__nil}" ]]; then
|
|
|
|
# eval false form
|
|
|
|
_nth "${ast}" 3; local a3="${r}"
|
|
|
|
if [[ "${a3}" ]]; then
|
|
|
|
ast="${a3}"
|
|
|
|
else
|
|
|
|
r="${__nil}"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
# eval true condition
|
|
|
|
ast="${a2}"
|
|
|
|
fi
|
|
|
|
# Continue loop
|
|
|
|
;;
|
2017-10-11 22:57:07 +03:00
|
|
|
fn__STAR__) _function "ENV \"${env}\" \"${a1}\" \"\${@}\"; \
|
2014-04-03 07:23:37 +04:00
|
|
|
EVAL \"${a2}\" \"\${r}\"" \
|
|
|
|
"${a2}" "${env}" "${a1}"
|
2014-03-25 01:32:24 +04:00
|
|
|
return ;;
|
|
|
|
*) EVAL_AST "${ast}" "${env}"
|
|
|
|
[[ "${__ERROR}" ]] && r= && return 1
|
|
|
|
local el="${r}"
|
2014-04-17 08:57:50 +04:00
|
|
|
_first "${el}"; local f="${ANON["${r}"]}"
|
|
|
|
_rest "${el}"; local args="${ANON["${r}"]}"
|
2014-03-25 01:32:24 +04:00
|
|
|
#echo "invoke: [${f}] ${args}"
|
|
|
|
if [[ "${f//@/ }" != "${f}" ]]; then
|
|
|
|
set -- ${f//@/ }
|
|
|
|
ast="${2}"
|
|
|
|
ENV "${3}" "${4}" ${args}
|
|
|
|
env="${r}"
|
|
|
|
else
|
|
|
|
eval ${f%%@*} ${args}
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
# Continue loop
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
2014-04-03 07:23:37 +04:00
|
|
|
|
2014-04-19 22:04:09 +04:00
|
|
|
# print
|
2014-03-25 01:32:24 +04:00
|
|
|
PRINT () {
|
|
|
|
if [[ "${__ERROR}" ]]; then
|
|
|
|
_pr_str "${__ERROR}" yes
|
|
|
|
r="Error: ${r}"
|
|
|
|
__ERROR=
|
|
|
|
else
|
|
|
|
_pr_str "${1}" yes
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2014-04-19 22:04:09 +04:00
|
|
|
# repl
|
2014-03-25 01:32:24 +04:00
|
|
|
ENV; REPL_ENV="${r}"
|
|
|
|
REP () {
|
|
|
|
r=
|
2014-05-11 00:45:36 +04:00
|
|
|
READ "${1}"
|
2014-03-25 01:32:24 +04:00
|
|
|
EVAL "${r}" "${REPL_ENV}"
|
|
|
|
PRINT "${r}"
|
|
|
|
}
|
|
|
|
|
2014-04-17 08:57:50 +04:00
|
|
|
# core.sh: defined using bash
|
2014-12-19 05:33:49 +03:00
|
|
|
_fref () {
|
|
|
|
_symbol "${1}"; local sym="${r}"
|
|
|
|
_function "${2} \"\${@}\""
|
|
|
|
ENV_SET "${REPL_ENV}" "${sym}" "${r}"
|
|
|
|
}
|
2014-04-03 07:23:37 +04:00
|
|
|
for n in "${!core_ns[@]}"; do _fref "${n}" "${core_ns["${n}"]}"; done
|
|
|
|
_eval () { EVAL "${1}" "${REPL_ENV}"; }
|
2014-03-25 01:32:24 +04:00
|
|
|
_fref "eval" _eval
|
2014-04-19 22:04:09 +04:00
|
|
|
_list; argv="${r}"
|
|
|
|
for _arg in "${@:2}"; do _string "${_arg}"; _conj! "${argv}" "${r}"; done
|
2014-12-19 05:33:49 +03:00
|
|
|
_symbol "__STAR__ARGV__STAR__"
|
|
|
|
ENV_SET "${REPL_ENV}" "${r}" "${argv}";
|
2014-03-25 01:32:24 +04:00
|
|
|
|
2014-04-17 08:57:50 +04:00
|
|
|
# core.mal: defined using the language itself
|
2014-04-18 06:49:07 +04:00
|
|
|
REP "(def! *host-language* \"bash\")"
|
2014-03-25 01:32:24 +04:00
|
|
|
REP "(def! not (fn* (a) (if a false true)))"
|
2014-04-17 08:57:50 +04:00
|
|
|
REP "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"
|
2014-03-25 01:32:24 +04:00
|
|
|
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)))))))"
|
2016-01-23 08:30:28 +03:00
|
|
|
REP "(def! *gensym-counter* (atom 0))"
|
|
|
|
REP "(def! gensym (fn* [] (symbol (str \"G__\" (swap! *gensym-counter* (fn* [x] (+ 1 x)))))))"
|
|
|
|
REP "(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) (let* (condvar (gensym)) \`(let* (~condvar ~(first xs)) (if ~condvar ~condvar (or ~@(rest xs)))))))))"
|
2014-03-25 01:32:24 +04:00
|
|
|
|
2014-04-19 22:04:09 +04:00
|
|
|
# load/run file from command line (then exit)
|
2014-03-25 01:32:24 +04:00
|
|
|
if [[ "${1}" ]]; then
|
2014-04-19 22:04:09 +04:00
|
|
|
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
|