diff options
| author | Joel Martin <github@martintribe.org> | 2014-12-18 20:33:49 -0600 |
|---|---|---|
| committer | Joel Martin <github@martintribe.org> | 2015-01-09 16:16:50 -0600 |
| commit | b8ee29b22fbaa7a01f2754b4d6dd9af52e02017c (patch) | |
| tree | f4d977ed220e9a3f665cfbf4f68770a81e4c2095 /bash/types.sh | |
| parent | aaba249304b184e12e2445ab22d66df1f39a51a5 (diff) | |
| download | mal-b8ee29b22fbaa7a01f2754b4d6dd9af52e02017c.tar.gz mal-b8ee29b22fbaa7a01f2754b4d6dd9af52e02017c.zip | |
All: add keywords.
Also, fix nth and count to match cloure.
Diffstat (limited to 'bash/types.sh')
| -rw-r--r-- | bash/types.sh | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/bash/types.sh b/bash/types.sh index 5cdc14a..4c2c824 100644 --- a/bash/types.sh +++ b/bash/types.sh @@ -8,6 +8,7 @@ __mal_types_included=true declare -A ANON __obj_magic=__5bal7 +__keyw=$(echo -en "\u029e") __obj_hash_code=${__obj_hash_code:-0} __new_obj_hash_code () { @@ -50,7 +51,9 @@ _obj_type () { list) r="list" ;; numb) r="number" ;; func) r="function" ;; - strn) r="string" ;; + strn) + local s="${ANON["${1}"]}" + [[ "${s:0:1}" = "${__keyw}" ]] && r="keyword" || r="string" ;; _nil) r="nil" ;; true) r="true" ;; fals) r="false" ;; @@ -71,7 +74,7 @@ _equal? () { fi fi case "${ot1}" in - string|symbol|number) + string|symbol|keyword|number) [[ "${ANON["${1}"]}" == "${ANON["${2}"]}" ]] ;; list|vector|hash_map) _count "${1}"; local sz1="${r}" @@ -109,6 +112,22 @@ _symbol () { _symbol? () { [[ ${1} =~ ^symb_ ]]; } +# Keywords + +_keyword () { + local k="${1}" + __new_obj_hash_code + r="strn_${r}" + [[ "${1:1:1}" = "${__keyw}" ]] || k="${__keyw}${1}" + ANON["${r}"]="${k//\*/__STAR__}" +} +_keyword? () { + [[ ${1} =~ ^strn_ ]] || return 1 + local s="${ANON["${1}"]}" + [[ "${s:0:1}" = "${__keyw}" ]] +} + + # Numbers _number () { @@ -245,7 +264,7 @@ _sequential? () { _nth () { local temp=(${ANON["${1}"]}) - r=${temp[${2}]} + r="${temp[${2}]}" } _first () { @@ -285,8 +304,12 @@ _conj! () { _count () { - local temp=(${ANON["${1}"]}) - r=${#temp[*]} + if _nil? "${1}"; then + r="0" + else + local temp=(${ANON["${1}"]}) + r=${#temp[*]} + fi } # Slice a sequence object $1 starting at $2 of length $3 |
