2014-03-25 01:32:24 +04:00
|
|
|
#
|
2014-04-03 07:23:37 +04:00
|
|
|
# mal (Make a Lisp) object types
|
2014-03-25 01:32:24 +04:00
|
|
|
#
|
|
|
|
|
2014-04-03 07:23:37 +04:00
|
|
|
if [ -z "${__mal_types_included__}" ]; then
|
|
|
|
__mal_types_included=true
|
|
|
|
|
2014-03-25 01:32:24 +04:00
|
|
|
declare -A ANON
|
|
|
|
|
|
|
|
__obj_magic=__5bal7
|
2015-03-12 06:22:35 +03:00
|
|
|
__keyw=$(echo -en "\xCA\x9E") # \u029E
|
2014-03-25 01:32:24 +04:00
|
|
|
__obj_hash_code=${__obj_hash_code:-0}
|
|
|
|
|
|
|
|
__new_obj_hash_code () {
|
|
|
|
__obj_hash_code=$(( __obj_hash_code + 1))
|
|
|
|
r="${__obj_hash_code}"
|
|
|
|
}
|
|
|
|
|
|
|
|
__new_obj () {
|
|
|
|
__new_obj_hash_code
|
|
|
|
r="${1}_${r}"
|
|
|
|
}
|
|
|
|
|
|
|
|
__new_obj_like () {
|
|
|
|
__new_obj_hash_code
|
|
|
|
r="${1%_*}_${r}"
|
|
|
|
}
|
|
|
|
|
2014-04-03 07:23:37 +04:00
|
|
|
|
|
|
|
# Errors/Exceptions
|
|
|
|
|
2014-03-25 01:32:24 +04:00
|
|
|
__ERROR=
|
2014-04-03 07:23:37 +04:00
|
|
|
_error() {
|
|
|
|
_string "${1}"
|
|
|
|
__ERROR="${r}"
|
|
|
|
r=
|
|
|
|
}
|
|
|
|
|
2014-03-25 01:32:24 +04:00
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# General functions
|
|
|
|
#
|
|
|
|
|
|
|
|
# Return the type of the object (or "make" if it's not a object
|
|
|
|
_obj_type () {
|
|
|
|
local type="${1:0:4}"
|
|
|
|
r=
|
|
|
|
case "${type}" in
|
|
|
|
symb) r="symbol" ;;
|
|
|
|
list) r="list" ;;
|
|
|
|
numb) r="number" ;;
|
|
|
|
func) r="function" ;;
|
2014-12-19 05:33:49 +03:00
|
|
|
strn)
|
|
|
|
local s="${ANON["${1}"]}"
|
2015-03-15 01:14:32 +03:00
|
|
|
if [[ "${1:0:1}" = "${__keyw}" ]] \
|
|
|
|
|| [[ "${1:0:2}" = "${__keyw}" ]]; then
|
|
|
|
r="keyword"
|
|
|
|
else
|
|
|
|
r="string"
|
|
|
|
fi ;;
|
2014-03-25 01:32:24 +04:00
|
|
|
_nil) r="nil" ;;
|
|
|
|
true) r="true" ;;
|
|
|
|
fals) r="false" ;;
|
|
|
|
vect) r="vector" ;;
|
|
|
|
hmap) r="hash_map" ;;
|
|
|
|
atom) r="atom" ;;
|
|
|
|
undf) r="undefined" ;;
|
|
|
|
*) r="bash" ;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2014-04-03 07:23:37 +04:00
|
|
|
_equal? () {
|
|
|
|
_obj_type "${1}"; local ot1="${r}"
|
|
|
|
_obj_type "${2}"; local ot2="${r}"
|
|
|
|
if [[ "${ot1}" != "${ot2}" ]]; then
|
|
|
|
if ! _sequential? "${1}" || ! _sequential? "${2}"; then
|
|
|
|
return 1
|
|
|
|
fi
|
2014-03-25 01:32:24 +04:00
|
|
|
fi
|
2014-04-03 07:23:37 +04:00
|
|
|
case "${ot1}" in
|
2014-12-19 05:33:49 +03:00
|
|
|
string|symbol|keyword|number)
|
2014-04-03 07:23:37 +04:00
|
|
|
[[ "${ANON["${1}"]}" == "${ANON["${2}"]}" ]] ;;
|
hash-map equality: bash, c, coffee, cs, es6, ...
- hash-map equality support for bash, c, coffee, cs, es6, java, js,
julia, make, php.
- also, add another test to catch another hash-map in-equality: same
keys, different values
2015-11-17 08:28:58 +03:00
|
|
|
list|vector)
|
2014-04-03 07:23:37 +04:00
|
|
|
_count "${1}"; local sz1="${r}"
|
|
|
|
_count "${2}"; local sz2="${r}"
|
|
|
|
[[ "${sz1}" == "${sz2}" ]] || return 1
|
|
|
|
local a1=(${ANON["${1}"]})
|
|
|
|
local a2=(${ANON["${2}"]})
|
|
|
|
for ((i=0;i<${#a1[*]};i++)); do
|
|
|
|
_equal? "${a1[${i}]}" "${a2[${i}]}" || return 1
|
|
|
|
done
|
|
|
|
;;
|
hash-map equality: bash, c, coffee, cs, es6, ...
- hash-map equality support for bash, c, coffee, cs, es6, java, js,
julia, make, php.
- also, add another test to catch another hash-map in-equality: same
keys, different values
2015-11-17 08:28:58 +03:00
|
|
|
hash_map)
|
|
|
|
local hm1="${ANON["${1}"]}"
|
|
|
|
eval local ks1="\${!${hm1}[@]}"
|
|
|
|
local hm2="${ANON["${2}"]}"
|
|
|
|
eval local ks2="\${!${hm2}[@]}"
|
|
|
|
[[ "${#ks1}" == "${#ks2}" ]] || return 1
|
|
|
|
for k in ${ks1}; do
|
|
|
|
eval v1="\${${hm1}[\"${k}\"]}"
|
|
|
|
eval v2="\${${hm2}[\"${k}\"]}"
|
|
|
|
[ "${v1}" ] || return 1
|
|
|
|
[ "${v2}" ] || return 1
|
|
|
|
_equal? "${v1}" "${v2}" || return 1
|
|
|
|
done
|
|
|
|
;;
|
2014-04-03 07:23:37 +04:00
|
|
|
*)
|
|
|
|
[[ "${1}" == "${2}" ]] ;;
|
|
|
|
esac
|
2014-03-25 01:32:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
# Constant atomic values
|
|
|
|
|
|
|
|
__nil=_nil_0
|
|
|
|
__true=true_0
|
|
|
|
__false=fals_0
|
|
|
|
|
|
|
|
_nil? () { [[ ${1} =~ ^_nil_ ]]; }
|
|
|
|
_true? () { [[ ${1} =~ ^true_ ]]; }
|
|
|
|
_false? () { [[ ${1} =~ ^fals_ ]]; }
|
|
|
|
|
|
|
|
|
|
|
|
# Symbols
|
|
|
|
|
2014-04-03 07:23:37 +04:00
|
|
|
_symbol () {
|
2014-03-25 01:32:24 +04:00
|
|
|
__new_obj_hash_code
|
|
|
|
r="symb_${r}"
|
2014-10-10 08:48:47 +04:00
|
|
|
ANON["${r}"]="${1//\*/__STAR__}"
|
2014-03-25 01:32:24 +04:00
|
|
|
}
|
|
|
|
_symbol? () { [[ ${1} =~ ^symb_ ]]; }
|
2014-04-03 07:23:37 +04:00
|
|
|
|
|
|
|
|
2014-12-19 05:33:49 +03:00
|
|
|
# Keywords
|
|
|
|
|
|
|
|
_keyword () {
|
|
|
|
local k="${1}"
|
|
|
|
__new_obj_hash_code
|
|
|
|
r="strn_${r}"
|
2015-03-15 01:14:32 +03:00
|
|
|
if [[ "${1:0:1}" = "${__keyw}" ]] \
|
|
|
|
|| [[ "${1:0:2}" = "${__keyw}" ]]; then
|
|
|
|
true
|
|
|
|
else
|
|
|
|
k="${__keyw}${1}"
|
|
|
|
fi
|
2014-12-19 05:33:49 +03:00
|
|
|
ANON["${r}"]="${k//\*/__STAR__}"
|
|
|
|
}
|
|
|
|
_keyword? () {
|
|
|
|
[[ ${1} =~ ^strn_ ]] || return 1
|
|
|
|
local s="${ANON["${1}"]}"
|
2015-03-15 01:14:32 +03:00
|
|
|
[[ "${s:0:1}" = "${__keyw}" ]] || [[ "${s:0:2}" = "${__keyw}" ]]
|
2014-12-19 05:33:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-03 07:23:37 +04:00
|
|
|
# Numbers
|
|
|
|
|
|
|
|
_number () {
|
|
|
|
__new_obj_hash_code
|
|
|
|
r="numb_${r}"
|
|
|
|
ANON["${r}"]="${1}"
|
2014-03-25 01:32:24 +04:00
|
|
|
}
|
2014-04-03 07:23:37 +04:00
|
|
|
_number? () { [[ ${1} =~ ^numb_ ]]; }
|
2014-03-25 01:32:24 +04:00
|
|
|
|
|
|
|
|
|
|
|
# Strings
|
|
|
|
|
2014-04-03 07:23:37 +04:00
|
|
|
_string () {
|
2014-03-25 01:32:24 +04:00
|
|
|
__new_obj_hash_code
|
|
|
|
r="strn_${r}"
|
2014-10-10 08:48:47 +04:00
|
|
|
ANON["${r}"]="${1//\*/__STAR__}"
|
2014-03-25 01:32:24 +04:00
|
|
|
}
|
|
|
|
_string? () { [[ ${1} =~ ^strn_ ]]; }
|
|
|
|
|
|
|
|
|
2014-04-03 07:23:37 +04:00
|
|
|
# Functions
|
2014-03-25 01:32:24 +04:00
|
|
|
# Return a function object. The first parameter is the
|
|
|
|
# function 'source'.
|
2014-04-03 07:23:37 +04:00
|
|
|
_function () {
|
2014-03-25 01:32:24 +04:00
|
|
|
__new_obj_hash_code
|
|
|
|
eval "function ${__obj_magic}_func_${r} () { ${1%;} ; }"
|
|
|
|
r="func_${r}"
|
|
|
|
if [[ "${2}" ]]; then
|
|
|
|
# Native function
|
|
|
|
ANON["${r}"]="${__obj_magic}_${r}@${2}@${3}@${4}"
|
|
|
|
else
|
|
|
|
# Bash function
|
|
|
|
ANON["${r}"]="${__obj_magic}_${r}"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
_function? () { [[ ${1} =~ ^func_ ]]; }
|
|
|
|
|
|
|
|
|
2014-04-03 07:23:37 +04:00
|
|
|
# Lists
|
|
|
|
|
|
|
|
_list () {
|
|
|
|
__new_obj_hash_code
|
|
|
|
r="list_${r}"
|
|
|
|
ANON["${r}"]="${*}"
|
|
|
|
}
|
|
|
|
_list? () { [[ ${1} =~ ^list_ ]]; }
|
|
|
|
|
|
|
|
|
|
|
|
# Vectors
|
|
|
|
|
|
|
|
_vector () {
|
|
|
|
__new_obj_hash_code
|
|
|
|
r="vector_${r}"
|
|
|
|
ANON["${r}"]="${*}"
|
|
|
|
}
|
|
|
|
_vector? () { [[ ${1} =~ ^vector_ ]]; }
|
|
|
|
|
2020-07-21 19:01:48 +03:00
|
|
|
vec () {
|
|
|
|
__new_obj_hash_code
|
|
|
|
r="vector_$r"
|
|
|
|
ANON["$r"]=${ANON["$1"]}
|
|
|
|
}
|
|
|
|
|
2014-04-03 07:23:37 +04:00
|
|
|
|
2014-03-25 01:32:24 +04:00
|
|
|
# hash maps (associative arrays)
|
|
|
|
|
2014-04-03 07:23:37 +04:00
|
|
|
_hash_map () {
|
2014-03-25 01:32:24 +04:00
|
|
|
__new_obj_hash_code
|
|
|
|
local name="hmap_${r}"
|
|
|
|
local obj="${__obj_magic}_${name}"
|
2014-10-10 08:48:47 +04:00
|
|
|
declare -A -g ${obj}; eval "${obj}=()"
|
2014-03-25 01:32:24 +04:00
|
|
|
ANON["${name}"]="${obj}"
|
|
|
|
|
|
|
|
while [[ "${1}" ]]; do
|
|
|
|
eval ${obj}[\"${ANON["${1}"]}\"]=\"${2}\"
|
|
|
|
shift; shift
|
|
|
|
done
|
|
|
|
|
|
|
|
r="${name}"
|
|
|
|
}
|
|
|
|
_hash_map? () { [[ ${1} =~ ^hmap_ ]]; }
|
|
|
|
|
2014-04-17 08:57:50 +04:00
|
|
|
_contains? () {
|
|
|
|
local obj="${ANON["${1}"]}"
|
|
|
|
eval [[ "\${${obj}[\"${2}\"]+isset}" ]]
|
|
|
|
}
|
|
|
|
|
2014-03-25 01:32:24 +04:00
|
|
|
_copy_hash_map () {
|
|
|
|
local orig_obj="${ANON["${1}"]}"
|
2014-04-03 07:23:37 +04:00
|
|
|
_hash_map
|
2014-03-25 01:32:24 +04:00
|
|
|
local name="${r}"
|
|
|
|
local obj="${ANON["${name}"]}"
|
|
|
|
|
|
|
|
# Copy the existing key/values to the new object
|
|
|
|
local temp=$(typeset -p ${orig_obj})
|
|
|
|
eval ${temp/#declare -A ${orig_obj}=/declare -A -g ${obj}=}
|
|
|
|
r="${name}"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Return same hash map with keys/values added/mutated in place
|
2014-04-03 07:23:37 +04:00
|
|
|
_assoc! () {
|
2014-03-25 01:32:24 +04:00
|
|
|
local obj=${ANON["${1}"]}; shift
|
|
|
|
declare -A -g ${obj}
|
|
|
|
|
|
|
|
# Set the key/values specified
|
|
|
|
while [[ "${1}" ]]; do
|
|
|
|
eval ${obj}[\"${1}\"]=\"${2}\"
|
|
|
|
shift; shift
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
# Return same hash map with keys/values deleted/mutated in place
|
2014-04-03 07:23:37 +04:00
|
|
|
_dissoc! () {
|
2014-03-25 01:32:24 +04:00
|
|
|
local obj=${ANON["${1}"]}; shift
|
|
|
|
declare -A -g ${obj}
|
|
|
|
|
|
|
|
# Delete the key/values specified
|
|
|
|
while [[ "${1}" ]]; do
|
|
|
|
eval unset ${obj}[\"${1}\"]
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-03 07:23:37 +04:00
|
|
|
# Atoms
|
2014-03-25 01:32:24 +04:00
|
|
|
|
2014-04-03 07:23:37 +04:00
|
|
|
_atom() {
|
2014-03-25 01:32:24 +04:00
|
|
|
__new_obj_hash_code
|
|
|
|
r="atom_${r}"
|
|
|
|
ANON["${r}"]="${*}"
|
|
|
|
}
|
|
|
|
_atom? () { [[ ${1} =~ ^atom_ ]]; }
|
|
|
|
|
|
|
|
|
|
|
|
# sequence operations
|
|
|
|
|
|
|
|
_sequential? () {
|
|
|
|
_list? "${1}" || _vector? "${1}"
|
|
|
|
}
|
|
|
|
|
|
|
|
_nth () {
|
|
|
|
local temp=(${ANON["${1}"]})
|
2014-12-19 05:33:49 +03:00
|
|
|
r="${temp[${2}]}"
|
2014-03-25 01:32:24 +04:00
|
|
|
}
|
|
|
|
|
2014-04-17 08:57:50 +04:00
|
|
|
_first () {
|
|
|
|
local temp="${ANON["${1}"]}"
|
|
|
|
r="${temp%% *}"
|
|
|
|
[ "${r}" ] || r="${__nil}"
|
|
|
|
}
|
|
|
|
|
|
|
|
_last () {
|
|
|
|
local temp="${ANON["${1}"]}"
|
|
|
|
r="${temp##* }"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Creates a new vector/list of the everything after but the first
|
|
|
|
# element
|
|
|
|
_rest () {
|
|
|
|
local temp="${ANON["${1}"]}"
|
|
|
|
_list
|
|
|
|
if [[ "${temp#* }" == "${temp}" ]]; then
|
|
|
|
ANON["${r}"]=
|
|
|
|
else
|
|
|
|
ANON["${r}"]="${temp#* }"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-25 01:32:24 +04:00
|
|
|
_empty? () { [[ -z "${ANON["${1}"]}" ]]; }
|
|
|
|
|
2014-04-02 07:50:55 +04:00
|
|
|
# conj that mutates in place (and always appends)
|
2014-04-03 07:23:37 +04:00
|
|
|
_conj! () {
|
2014-03-25 01:32:24 +04:00
|
|
|
local obj="${1}"; shift
|
|
|
|
local obj_data="${ANON["${obj}"]}"
|
|
|
|
ANON["${obj}"]="${obj_data:+${obj_data} }${*}"
|
|
|
|
r="${1}"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_count () {
|
2014-12-19 05:33:49 +03:00
|
|
|
if _nil? "${1}"; then
|
|
|
|
r="0"
|
|
|
|
else
|
|
|
|
local temp=(${ANON["${1}"]})
|
|
|
|
r=${#temp[*]}
|
|
|
|
fi
|
2014-03-25 01:32:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
# Slice a sequence object $1 starting at $2 of length $3
|
|
|
|
_slice () {
|
|
|
|
local temp=(${ANON["${1}"]})
|
|
|
|
__new_obj_like "${1}"
|
|
|
|
ANON["${r}"]="${temp[@]:${2}:${3}}"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Takes a bash function and an list object and invokes the function on
|
|
|
|
# each element of the list, returning a new list (or vector) of the results.
|
|
|
|
_map_with_type () {
|
2014-04-03 07:23:37 +04:00
|
|
|
local constructor="${1}"; shift
|
2014-03-25 01:32:24 +04:00
|
|
|
local f="${1}"; shift
|
|
|
|
local items="${ANON["${1}"]}"; shift
|
2014-04-03 07:23:37 +04:00
|
|
|
eval "${constructor}"; local new_seq="${r}"
|
2014-03-25 01:32:24 +04:00
|
|
|
for v in ${items}; do
|
|
|
|
#echo eval ${f%%@*} "${v}" "${@}"
|
|
|
|
eval ${f%%@*} "${v}" "${@}"
|
|
|
|
[[ "${__ERROR}" ]] && r= && return 1
|
2014-04-03 07:23:37 +04:00
|
|
|
_conj! "${new_seq}" "${r}"
|
2014-03-25 01:32:24 +04:00
|
|
|
done
|
|
|
|
r="${new_seq}"
|
|
|
|
}
|
|
|
|
|
|
|
|
_map () {
|
2014-04-03 07:23:37 +04:00
|
|
|
_map_with_type _list "${@}"
|
2014-03-25 01:32:24 +04:00
|
|
|
}
|
|
|
|
|
2014-04-03 07:23:37 +04:00
|
|
|
fi
|