diff options
Diffstat (limited to 'bos-sugar.sh')
-rw-r--r-- | bos-sugar.sh | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/bos-sugar.sh b/bos-sugar.sh new file mode 100644 index 0000000..e211b5e --- /dev/null +++ b/bos-sugar.sh @@ -0,0 +1,76 @@ +#!bash + +declare -a bos_sugar_block_toggle + +function bos-sugar/block() { + local on_open="$1"; shift + local on_close="$1"; shift + local depth="${#FUNCNAME[*]}" + if [[ -z "${bos_sugar_block_toggle[$depth]}" ]]; then + bos_sugar_block_toggle=( ["$depth"]=1 ) + "$on_open" "$@" + else + unset "bos_sugar_block_toggle[$depth]" + "$on_close" "$@" + fi +} + +declare -a bos_sugar_class_stack + +function bos-sugar/current-class-into() { + local -n result="$1" + local IFS=/ + result="${bos_sugar_class_stack[*]}" +} + +function bos-sugar/set-metaclass-for-current-class() { + local class; bos-sugar/current-class-into class + local metaclass_ref; bos-namespaces/store-scalar-for-into meta "$class" metaclass_ref + local -n metaclass_object="$metaclass_ref" + metaclass_object='bos-dispatch/invoke bos/mop/inheritance 0' +} + +function bos-sugar/class-open() { + if [[ "${#*}" -gt 1 ]]; then + >&2 echo "'class \$class_name; do …; done', not 'class $*'" + return 1 + fi + local class="$1" + bos_sugar_class_stack+=( "$class" ) + bos-sugar/set-metaclass-for-current-class + local fq_class; bos-sugar/current-class-into fq_class + + function extends() { + local fq_class; bos-sugar/current-class-into fq_class + local metaclass_ref; bos-namespaces/store-scalar-for-into meta "$fq_class" metaclass_ref + local -n metaclass_object="$metaclass_ref" + $metaclass_object set-superclasses-for "$fq_class" "$@" + } + + bos-namespaces/start "$fq_class" + + return 0 +} + + + +function bos-sugar/class-close() { + local class="$1" + + local fq_class; bos-sugar/current-class-into fq_class + local metaclass_ref; bos-namespaces/store-scalar-for-into meta "$fq_class" metaclass_ref + local -n metaclass_object="$metaclass_ref" + + local -a methods_list; bos-namespaces/list-new-funcs-into "$fq_class" methods_list + bos-namespaces/qualify-funcs "$fq_class" "${methods_list[@]}" + + $metaclass_object make-mro-for "$fq_class" + + unset "bos_sugar_class_stack[-1]" + + return 1 +} + +shopt -s expand_aliases + +alias class='while bos-sugar/block bos-sugar/class-open bos-sugar/class-close' |