summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGianni Ceccarelli <gianni.ceccarelli@broadbean.com>2023-07-14 16:15:00 +0100
committerGianni Ceccarelli <gianni.ceccarelli@broadbean.com>2023-07-14 16:15:00 +0100
commit9fe99a59594a83ae2c0960ed8c2ee6959eab062e (patch)
tree06af594d07c1dd1cb46ba6fc57a38cc387cb7f3f
parentinheritance via MOP (diff)
downloadbash-object-system-9fe99a59594a83ae2c0960ed8c2ee6959eab062e.tar.gz
bash-object-system-9fe99a59594a83ae2c0960ed8c2ee6959eab062e.tar.bz2
bash-object-system-9fe99a59594a83ae2c0960ed8c2ee6959eab062e.zip
some sugaring
-rw-r--r--bos-namespaces.sh2
-rw-r--r--bos-sugar.sh76
-rw-r--r--test.sh23
3 files changed, 90 insertions, 11 deletions
diff --git a/bos-namespaces.sh b/bos-namespaces.sh
index fd371c3..a208e02 100644
--- a/bos-namespaces.sh
+++ b/bos-namespaces.sh
@@ -75,7 +75,7 @@ function bos-namespaces/list-new-funcs-into() {
# saved function of the same name, if it exists
function bos-namespaces/qualify-funcs() {
local namespace="$1";shift
- local ns_store; bos-namespaces/store-for-into saved_funcs "$namespace" ns_store
+ local ns_store; bos-namespaces/store-dict-for-into saved_funcs "$namespace" ns_store
local -n saved_funcs="$ns_store"
local _ funcname new_function
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'
diff --git a/test.sh b/test.sh
index 4a3e02c..058fe07 100644
--- a/test.sh
+++ b/test.sh
@@ -7,21 +7,24 @@ PS4='[${#FUNCNAME[*]}] ${BASH_SOURCE[0]}:${LINENO} (${FUNCNAME[0]}) +'
. bos-mop.sh
. bos-dispatch.sh
. bos-mop-inheritance.sh
+. bos-sugar.sh
-bos_5fA_5fmeta="bos-dispatch/invoke bos/mop/inheritance 0"
+class A; do
-function A/thing() {
- echo "<$self> A/thing ($*)"
-}
+ function thing() {
+ echo "<$self> A/thing ($*)"
+ }
+done
-bos_5fB_5fmeta="bos-dispatch/invoke bos/mop/inheritance 0"
-$bos_5fB_5fmeta set-superclasses-for B A
-$bos_5fB_5fmeta make-mro-for B
+class B; do
+ extends A
-function B/other() {
- echo "<$self> B/other ($*)"
-}
+ function other() {
+ echo "<$self> B/other ($*)"
+ }
+
+done
bos-object-id/pack-self-into A 0 objA
bos-object-id/pack-self-into B 0 objB