2 # mal (Make a Lisp) environment definition
5 if [ -z "${__mal_env_included__}" ]; then
6 __mal_env_included
=true
8 source $
(dirname $0)/types.sh
10 # Any environment is a hash_map with an __outer__ key that refers to
11 # a parent environment (or nil)
18 _assoc
! "${env}" "__outer__" "${outer}"
20 _assoc
! "${env}" "__outer__" "${__nil}"
24 if [[ "${1}" && "${@}" ]]; then
25 local binds
=(${ANON["${1}"]}); shift
27 while [[ "${binds["${idx}"]}" ]]; do
28 local fp
="${ANON["${binds["${idx}"]}"]}"
29 if [[ "${fp}" == "&" ]]; then
31 fp="${ANON["${binds["${idx}"]}"]}"
33 _assoc
! "${env}" "${fp}" "${r}"
36 _assoc
! "${env}" "${fp}" "${1}"
45 # Find the environment with the key set and return the environment
47 if _contains?
"${1}" "${ANON["${2}"]}"; then
50 local obj
="${ANON["${1}"]}"
51 eval 'local outer=${'${obj}'["__outer__"]}'
52 if [[ "${outer}" && "${outer}" != "${__nil}" ]]; then
53 ENV_FIND
"${outer}" "${2}"
60 # Find the environment with the key set and return the value of the
61 # key in that environment. If no environment contains the key then
64 ENV_FIND
"${1}" "${2}"
66 local key
="${ANON["${2}"]}"
68 local obj
="${ANON["${env}"]}"
69 eval 'r=${'${obj}'["'${key}'"]}'
71 _error
"'${key}' not found"
76 local key
="${ANON["${2}"]}"
77 _assoc
! "${1}" "${key}" "${3}"