diff options
author | dakkar <dakkar@thenautilus.net> | 2023-07-22 15:57:30 +0100 |
---|---|---|
committer | dakkar <dakkar@thenautilus.net> | 2023-07-22 15:57:30 +0100 |
commit | 81a723086f99f8270563dbab28f6a70866dde02d (patch) | |
tree | 4b382a1098a33195121d8db3f2bf294ef3743cec | |
parent | factor namespace open/close (diff) | |
download | bash-object-system-81a723086f99f8270563dbab28f6a70866dde02d.tar.gz bash-object-system-81a723086f99f8270563dbab28f6a70866dde02d.tar.bz2 bash-object-system-81a723086f99f8270563dbab28f6a70866dde02d.zip |
single meta class
-rw-r--r-- | bos-meta-class.sh (renamed from bos-mop.sh) | 68 | ||||
-rw-r--r-- | bos-mop-inheritance.sh | 51 | ||||
-rw-r--r-- | bos-sugar.sh | 2 | ||||
-rw-r--r-- | meta-stuff.rst.txt | 8 | ||||
-rw-r--r-- | t/testlib.sh | 3 |
5 files changed, 63 insertions, 69 deletions
diff --git a/bos-mop.sh b/bos-meta-class.sh index b4eddc3..4d701b5 100644 --- a/bos-mop.sh +++ b/bos-meta-class.sh @@ -1,18 +1,64 @@ #!bash # this encoding must match bos-namespaces/encode-into -bos_5fbos_2fmop_2fbase_5fmeta="bos-dipatch/invoke bos/mop/base 0" -declare -a bos_5fbos_2fmop_2fbase_5fmro=( "bos/mop/base" ) +bos_5fbos_2fmeta_2fclass_5fmeta="bos-dipatch/invoke bos/meta/class 0" +declare -a bos_5fbos_2fmeta_2fclass_5fmro=( "bos/meta/class" ) # these are instance methods of the base metaclass -function bos/mop/base/mro-for-into() { +function bos/meta/class/isa-for-into() { + bos-namespaces/store-array-for-into "$1" isa "$2" +} + +function bos/meta/class/set-superclasses-for() { + local isa_name; $self isa-for-into isa_name "$1" + local -n isa="$isa_name" + shift + + isa=( "$@" ) + + return 0 +} + +function bos/meta/class/get-superclasses-for-into() { + local -n dest="$1" + local isa_name; $self isa-for-into isa_name "$2" + local -n isa="$isa_name" + + dest=( "${isa[@]}" ) + + return 0 +} + +function bos/meta/class/mro-for-into() { bos-namespaces/store-array-for-into "$1" mro "$2" } +# given a class and an array, sets the array to the list of clasess to +# look into when resolving a method call for the given class, in order +function bos/meta/class/make-mro-for() { + local class="$1" + local mro_name; $self mro-for-into mro_name "$class" + local -n mro="$mro_name" + + # TODO: use C3 and support multiple inheritance + + mro=( "$class" ) + local -a nextclasses + $self get-superclasses-for-into nextclasses "$class" + + while [[ "${#nextclasses}" -gt 0 ]]; do + mro+=( "${nextclasses[0]}" ) + class="${nextclasses[0]}" + $self get-superclasses-for-into nextclasses "$class" + done + + return 0 +} + # find the first class (in MRO) that defines a given method -function bos/mop/base/find-method-into() { - local -n bos_mop_base_find_method_result="$1" +function bos/meta/class/find-method-into() { + local -n bos_meta_class_find_method_result="$1" # the class that started the method dispatch (the class of $self, # usually) local class="$2" @@ -45,7 +91,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 - bos_mop_base_find_method_result="${full_name}" + bos_meta_class_find_method_result="${full_name}" return 0 fi done @@ -56,7 +102,7 @@ function bos/mop/base/find-method-into() { } # the method dispatcher -function bos/mop/base/invoke() { +function bos/meta/class/invoke() { local class="$1";shift local object_id="$1";shift local method="$1";shift @@ -90,17 +136,17 @@ function bos/mop/base/invoke() { return $? } -function bos/mop/base/create-object-into() { - local -n bos_mop_base_create_object_result="$1";shift +function bos/meta/class/create-object-into() { + local -n bos_meta_class_create_object_result="$1";shift local class="$1";shift local object_id_store; bos-namespaces/store-scalar-for-into object_id_store object_id "$class" local -n object_id="$object_id_store" (( ++object_id )) - bos-object-id/pack-self-into bos_mop_base_create_object_result "$class" "$object_id" + bos-object-id/pack-self-into bos_meta_class_create_object_result "$class" "$object_id" - # TODO invoke BUILD/BUILDALL? probably in a different metaclass, though + # TODO invoke BUILD/BUILDALL? return 0 } diff --git a/bos-mop-inheritance.sh b/bos-mop-inheritance.sh deleted file mode 100644 index cc3d604..0000000 --- a/bos-mop-inheritance.sh +++ /dev/null @@ -1,51 +0,0 @@ -#!bash - -# this encoding must match bos-namespaces/encode-into -bos_5fbos_2fmop_2finheritance_5fmeta="bos-dispatch/invoke bos/mop/base 0" -declare -a bos_5fbos_2fmop_2finheritance_5fmro=( "bos/mop/inheritance" "bos/mop/base" ) - -function bos/mop/inheritance/isa-for-into() { - bos-namespaces/store-array-for-into "$1" isa "$2" -} - -function bos/mop/inheritance/set-superclasses-for() { - local isa_name; $self isa-for-into isa_name "$1" - local -n isa="$isa_name" - shift - - isa=( "$@" ) - - return 0 -} - -function bos/mop/inheritance/get-superclasses-for-into() { - local -n dest="$1" - local isa_name; $self isa-for-into isa_name "$2" - local -n isa="$isa_name" - - dest=( "${isa[@]}" ) - - return 0 -} - -# given a class and an array, sets the array to the list of clasess to -# look into when resolving a method call for the given class, in order -function bos/mop/inheritance/make-mro-for() { - local class="$1" - local mro_name; $self mro-for-into mro_name "$class" - local -n mro="$mro_name" - - # TODO: use C3 and support multiple inheritance - - mro=( "$class" ) - local -a nextclasses - $self get-superclasses-for-into nextclasses "$class" - - while [[ "${#nextclasses}" -gt 0 ]]; do - mro+=( "${nextclasses[0]}" ) - class="${nextclasses[0]}" - $self get-superclasses-for-into nextclasses "$class" - done - - return 0 -} diff --git a/bos-sugar.sh b/bos-sugar.sh index f211f97..f7e790a 100644 --- a/bos-sugar.sh +++ b/bos-sugar.sh @@ -35,7 +35,7 @@ function bos-sugar/set-metaclass-for() { local class="$1" local metaclass_ref; bos-namespaces/store-scalar-for-into metaclass_ref meta "$class" local -n metaclass_object="$metaclass_ref" - metaclass_object='bos-dispatch/invoke bos/mop/inheritance 0' + metaclass_object='bos-dispatch/invoke bos/meta/class 0' } function bos-sugar/class-open() { diff --git a/meta-stuff.rst.txt b/meta-stuff.rst.txt index c53fb7d..46fabf5 100644 --- a/meta-stuff.rst.txt +++ b/meta-stuff.rst.txt @@ -8,13 +8,13 @@ done bits - use them inside ``class_open`` / ``class_close`` +* ``bos/mop/base`` + ``bos/mop/inheritance`` become ``bos/meta/class`` + next bits… * formalise that ``bos-dispatch/invoke $class 0 $method $args`` is a class method invocation -* ``bos/mop/base`` + ``bos/mop/inheritance`` become ``bos/meta/class`` - * add ``bos/meta/attribute`` - store values in ``bos-namespaces/store-*-for-into attribute @@ -38,7 +38,7 @@ next bits… - when creating a class, create a metaclass instance and store it - the store must be pre-populated with metaclass instances for: - + ``bos/mop/class`` → ``bos-dispatch/invoke bos/mop/class 1`` - + ``bos/mop/attribute`` → ``bos-dispatch/invoke bos/mop/class 2`` + + ``bos/meta/class`` → ``bos-dispatch/invoke bos/meta/class 1`` + + ``bos/meta/attribute`` → ``bos-dispatch/invoke bos/meta/class 2`` and those instances' attributes must be manually stored diff --git a/t/testlib.sh b/t/testlib.sh index 6da46f5..00b82af 100644 --- a/t/testlib.sh +++ b/t/testlib.sh @@ -4,9 +4,8 @@ PS4='[${#FUNCNAME[*]}] ${BASH_SOURCE[0]}:${LINENO} (${FUNCNAME[0]}) +' . bos-namespaces.sh . bos-object-id.sh -. bos-mop.sh +. bos-meta-class.sh . bos-dispatch.sh -. bos-mop-inheritance.sh . bos-sugar.sh . minitap.sh |