diff options
author | Gianni Ceccarelli <gianni.ceccarelli@broadbean.com> | 2023-07-14 16:36:10 +0100 |
---|---|---|
committer | Gianni Ceccarelli <gianni.ceccarelli@broadbean.com> | 2023-07-14 16:36:10 +0100 |
commit | 86c37a00dd9ec82c261e3c9f7997051aabaa228b (patch) | |
tree | b682b6e94d62e293aefa353008b33f85b57c6ed3 | |
parent | some sugaring (diff) | |
download | bash-object-system-86c37a00dd9ec82c261e3c9f7997051aabaa228b.tar.gz bash-object-system-86c37a00dd9ec82c261e3c9f7997051aabaa228b.tar.bz2 bash-object-system-86c37a00dd9ec82c261e3c9f7997051aabaa228b.zip |
constructors & class methods!
"class methods" are not actually a thing, we declare a function for
each class, that invokes on the class name and object-id=0 (object-id
start from 1 for real objects)
-rw-r--r-- | bos-mop.sh | 19 | ||||
-rw-r--r-- | bos-object-id.sh | 16 | ||||
-rw-r--r-- | bos-sugar.sh | 21 | ||||
-rw-r--r-- | test.sh | 4 |
4 files changed, 44 insertions, 16 deletions
@@ -19,7 +19,7 @@ function bos/mop/base/find-method-into() { local start_from_class="$2" # the method to look for local method="$3" - local -n result="$4" + local -n bos_mop_base_find_method_result="$4" local mro_store; $self mro-for-into "$class" mro_store local -n mro="$mro_store" @@ -45,7 +45,7 @@ function bos/mop/base/find-method-into() { for (( ; idx < "${#mro[@]}" ; ++idx )); do local full_name="${mro[$idx]}/${method}" if declare -F "$full_name" >/dev/null; then - result="${full_name}" + bos_mop_base_find_method_result="${full_name}" return 0 fi done @@ -88,3 +88,18 @@ function bos/mop/base/invoke() { eval "\"\$$ffsname\" \"\$@\"" return $? } + +function bos/mop/base/create-object-into() { + local -n bos_mop_base_create_object_result="$1";shift + local class="$1";shift + + local object_id_store; bos-namespaces/store-scalar-for-into object_id "$class" object_id_store + local -n object_id="$object_id_store" + (( ++object_id )) + + bos-object-id/pack-self-into "$class" "$object_id" bos_mop_base_create_object_result + + # TODO invoke BUILD/BUILDALL? probably in a different metaclass, though + + return 0 +} diff --git a/bos-object-id.sh b/bos-object-id.sh index 2026506..ac7a7c8 100644 --- a/bos-object-id.sh +++ b/bos-object-id.sh @@ -7,17 +7,17 @@ # id, so that `$my_obj the-method 1 2` is equivalent to `bos_invoke # TheClass $the_obj_id the-method 1 2` function bos-object-id/pack-self-into() { - local class="$1" - local self_id="$2" - local -n result="$3" - result="bos-dispatch/invoke $class $self_id" + local bos_object_id_class="$1" + local bos_object_id_self_id="$2" + local -n bos_object_id_result="$3" + bos_object_id_result="bos-dispatch/invoke $bos_object_id_class $bos_object_id_self_id" } # get class and object id from a $self string function bos-object/unpack-self-into() { - local self="$1" - local -n class="$2" - local -n id="$2" + local bos_object_id_self="$1" + local -n bos_object_id_class="$2" + local -n bos_object_id_self_id="$3" local _ - read -r _ class id <<<"$self" + read -r _ bos_object_id_class bos_object_id_self_id <<<"$bos_object_id_self" } diff --git a/bos-sugar.sh b/bos-sugar.sh index e211b5e..a199ed9 100644 --- a/bos-sugar.sh +++ b/bos-sugar.sh @@ -18,9 +18,9 @@ function bos-sugar/block() { declare -a bos_sugar_class_stack function bos-sugar/current-class-into() { - local -n result="$1" + local -n bos_sugar_current_class_result="$1" local IFS=/ - result="${bos_sugar_class_stack[*]}" + bos_sugar_current_class_result="${bos_sugar_class_stack[*]}" } function bos-sugar/set-metaclass-for-current-class() { @@ -47,12 +47,25 @@ function bos-sugar/class-open() { $metaclass_object set-superclasses-for "$fq_class" "$@" } + eval "function $fq_class { bos-dispatch/invoke \"$fq_class\" 0 \"\$@\"; }" + bos-namespaces/start "$fq_class" - return 0 -} + # this will get renamed into the class + function new-into() { + local class _; bos-object/unpack-self-into "$self" class _ + local -n new_into_result="$1"; shift + local metaclass_ref; bos-namespaces/store-scalar-for-into meta "$class" metaclass_ref + local -n metaclass_object="$metaclass_ref" + local new_object + $metaclass_object create-object-into new_object "$class" + new_into_result="$new_object" + } + + return 0 +} function bos-sugar/class-close() { local class="$1" @@ -26,8 +26,8 @@ class B; do done -bos-object-id/pack-self-into A 0 objA -bos-object-id/pack-self-into B 0 objB +A new-into objA +B new-into objB $objA thing 1 2 3 $objB thing 4 5 6 |