mirror of
https://github.com/kanaka/mal.git
synced 2024-09-11 21:57:38 +03:00
OSH is a bash-compatible shell: https://www.oilshell.org/ reader.sh: - Put the constant regex pattern in a string literal. This simplifies it by removing mixed regex vs. shell quoting, and implicit concatenation with $'\n'. This is suggested by the bash manual: https://www.gnu.org/software/bash/manual/bash.html#Conditional-Constructs "Storing the regular expression in a shell variable is often a useful way to avoid problems with quoting characters that are special to the shell." - Initialize __reader_tokens as an array, not a string. https://www.oilshell.org/release/0.8.pre6/doc/known-differences.html#values-are-tagged-with-types-not-cells env.sh: Simplify quoting in 'eval' expressions. This quotes associative array keys, which is required by OSH to avoid dynamic parsing. https://www.oilshell.org/release/0.8.pre6/doc/known-differences.html#strings-vs-bare-words-in-array-indices http://www.oilshell.org/blog/2016/10/20.html core.sh: Quote associative array keys. '<' and '>' are shell operators and OSH doesn't have a special case when inside []. ---- With this change, OSH can run tests just like bash, e.g.: $ osh impls/bash/stepA_mal.sh tests/step4_if_fn_do.mal ---- Test results are the same before and after this change: $ NO_DOCKER=1 ./.travis_test.sh test bash FAILURES: SOFT FAILED TEST (line 295): (f (+ 1 1)) -> ['',true]: Expected : '.*\ntrue' Got : '(f (+ 1 1))\nfalse' TEST RESULTS (for ../tests/stepA_mal.mal): 1: soft failing tests 0: failing tests 106: passing tests 107: total tests
81 lines
1.9 KiB
Bash
81 lines
1.9 KiB
Bash
#
|
|
# mal (Make a Lisp) environment definition
|
|
#
|
|
|
|
if [ -z "${__mal_env_included__}" ]; then
|
|
__mal_env_included=true
|
|
|
|
source $(dirname $0)/types.sh
|
|
|
|
# Any environment is a hash_map with an __outer__ key that refers to
|
|
# a parent environment (or nil)
|
|
ENV () {
|
|
r=
|
|
_hash_map
|
|
local env="${r}"
|
|
if [[ "${1}" ]]; then
|
|
outer="${1}"; shift
|
|
_assoc! "${env}" "__outer__" "${outer}"
|
|
else
|
|
_assoc! "${env}" "__outer__" "${__nil}"
|
|
fi
|
|
r="${env}"
|
|
|
|
if [[ "${1}" && "${@}" ]]; then
|
|
local binds=(${ANON["${1}"]}); shift
|
|
local idx=0
|
|
while [[ "${binds["${idx}"]}" ]]; do
|
|
local fp="${ANON["${binds["${idx}"]}"]}"
|
|
if [[ "${fp}" == "&" ]]; then
|
|
idx=$(( idx + 1 ))
|
|
fp="${ANON["${binds["${idx}"]}"]}"
|
|
_list "${@}"
|
|
_assoc! "${env}" "${fp}" "${r}"
|
|
break
|
|
else
|
|
_assoc! "${env}" "${fp}" "${1}"
|
|
shift
|
|
idx=$(( idx + 1 ))
|
|
fi
|
|
done
|
|
fi
|
|
r="${env}"
|
|
}
|
|
|
|
# Find the environment with the key set and return the environment
|
|
ENV_FIND () {
|
|
if _contains? "${1}" "${ANON["${2}"]}"; then
|
|
r="${1}"
|
|
else
|
|
local obj="${ANON["${1}"]}"
|
|
eval 'local outer=${'${obj}'["__outer__"]}'
|
|
if [[ "${outer}" && "${outer}" != "${__nil}" ]]; then
|
|
ENV_FIND "${outer}" "${2}"
|
|
else
|
|
r=
|
|
fi
|
|
fi
|
|
}
|
|
|
|
# Find the environment with the key set and return the value of the
|
|
# key in that environment. If no environment contains the key then
|
|
# return an error
|
|
ENV_GET () {
|
|
ENV_FIND "${1}" "${2}"
|
|
local env="${r}"
|
|
local key="${ANON["${2}"]}"
|
|
if [[ "${r}" ]]; then
|
|
local obj="${ANON["${env}"]}"
|
|
eval 'r=${'${obj}'["'${key}'"]}'
|
|
else
|
|
_error "'${key}' not found"
|
|
fi
|
|
}
|
|
|
|
ENV_SET () {
|
|
local key="${ANON["${2}"]}"
|
|
_assoc! "${1}" "${key}" "${3}"
|
|
}
|
|
|
|
fi
|