From b8ee29b22fbaa7a01f2754b4d6dd9af52e02017c Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Thu, 18 Dec 2014 20:33:49 -0600 Subject: All: add keywords. Also, fix nth and count to match cloure. --- bash/types.sh | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) (limited to 'bash/types.sh') 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 -- cgit v1.2.3