2014-04-03 07:23:37 +04:00
|
|
|
#
|
|
|
|
# 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 () {
|
2014-12-19 05:33:49 +03:00
|
|
|
if _contains? "${1}" "${ANON["${2}"]}"; then
|
2014-04-03 07:23:37 +04:00
|
|
|
r="${1}"
|
|
|
|
else
|
|
|
|
local obj="${ANON["${1}"]}"
|
2020-06-13 20:57:13 +03:00
|
|
|
eval 'local outer=${'${obj}'["__outer__"]}'
|
2014-04-03 07:23:37 +04:00
|
|
|
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}"
|
2014-12-19 05:33:49 +03:00
|
|
|
local key="${ANON["${2}"]}"
|
2014-04-03 07:23:37 +04:00
|
|
|
if [[ "${r}" ]]; then
|
|
|
|
local obj="${ANON["${env}"]}"
|
2020-06-13 20:57:13 +03:00
|
|
|
eval 'r=${'${obj}'["'${key}'"]}'
|
2014-04-03 07:23:37 +04:00
|
|
|
else
|
2014-12-19 05:33:49 +03:00
|
|
|
_error "'${key}' not found"
|
2014-04-03 07:23:37 +04:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
ENV_SET () {
|
2014-12-19 05:33:49 +03:00
|
|
|
local key="${ANON["${2}"]}"
|
|
|
|
_assoc! "${1}" "${key}" "${3}"
|
2014-04-03 07:23:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
fi
|