diff options
author | dakkar <dakkar@thenautilus.net> | 2023-07-22 12:35:39 +0100 |
---|---|---|
committer | dakkar <dakkar@thenautilus.net> | 2023-07-22 12:35:39 +0100 |
commit | 6b3f9cb909df162345d9f5a001abeb0d3df31ce9 (patch) | |
tree | 32d89ba43faaea0ebfe69f9aaa8f002c7b9e57bd | |
parent | move tests to actual test programs (diff) | |
download | bash-object-system-6b3f9cb909df162345d9f5a001abeb0d3df31ce9.tar.gz bash-object-system-6b3f9cb909df162345d9f5a001abeb0d3df31ce9.tar.bz2 bash-object-system-6b3f9cb909df162345d9f5a001abeb0d3df31ce9.zip |
`*-into` always takes the destination as `$1`
-rw-r--r-- | bos-dispatch.sh | 4 | ||||
-rw-r--r-- | bos-mop-inheritance.sh | 14 | ||||
-rw-r--r-- | bos-mop.sh | 21 | ||||
-rw-r--r-- | bos-namespaces.sh | 24 | ||||
-rw-r--r-- | bos-object-id.sh | 12 | ||||
-rw-r--r-- | bos-sugar.sh | 12 | ||||
-rw-r--r-- | meta-stuff.rst.txt | 4 |
7 files changed, 47 insertions, 44 deletions
diff --git a/bos-dispatch.sh b/bos-dispatch.sh index 8489765..8306378 100644 --- a/bos-dispatch.sh +++ b/bos-dispatch.sh @@ -28,13 +28,13 @@ function bos-dispatch/invoke() { if declare -F "$qualified_method" >/dev/null; then local self - bos-object-id/pack-self-into "$class" "$self_id" self + bos-object-id/pack-self-into self "$class" "$self_id" "$qualified_method" "$@" return $? fi local metaclass_ref - bos-namespaces/store-scalar-for-into meta "$class" metaclass_ref + bos-namespaces/store-scalar-for-into metaclass_ref meta "$class" local -n metaclass_object="$metaclass_ref" $metaclass_object invoke "$class" "$self_id" "$method" "$@" diff --git a/bos-mop-inheritance.sh b/bos-mop-inheritance.sh index 551dd18..cc3d604 100644 --- a/bos-mop-inheritance.sh +++ b/bos-mop-inheritance.sh @@ -5,11 +5,11 @@ 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 isa "$1" "$2" + bos-namespaces/store-array-for-into "$1" isa "$2" } function bos/mop/inheritance/set-superclasses-for() { - local isa_name; $self isa-for-into "$1" isa_name + local isa_name; $self isa-for-into isa_name "$1" local -n isa="$isa_name" shift @@ -19,9 +19,9 @@ function bos/mop/inheritance/set-superclasses-for() { } function bos/mop/inheritance/get-superclasses-for-into() { - local isa_name; $self isa-for-into "$1" isa_name + local -n dest="$1" + local isa_name; $self isa-for-into isa_name "$2" local -n isa="$isa_name" - local -n dest="$2" dest=( "${isa[@]}" ) @@ -32,19 +32,19 @@ function bos/mop/inheritance/get-superclasses-for-into() { # 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 "$class" mro_name + 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 "$class" nextclasses + $self get-superclasses-for-into nextclasses "$class" while [[ "${#nextclasses}" -gt 0 ]]; do mro+=( "${nextclasses[0]}" ) class="${nextclasses[0]}" - $self get-superclasses-for-into "$class" nextclasses + $self get-superclasses-for-into nextclasses "$class" done return 0 @@ -7,21 +7,21 @@ declare -a bos_5fbos_2fmop_2fbase_5fmro=( "bos/mop/base" ) # these are instance methods of the base metaclass function bos/mop/base/mro-for-into() { - bos-namespaces/store-array-for-into mro "$1" "$2" + bos-namespaces/store-array-for-into "$1" mro "$2" } # 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" # the class that started the method dispatch (the class of $self, # usually) - local class="$1" + local class="$2" # what class invoked next/method (if we're doing next/method) - local start_from_class="$2" + local start_from_class="$3" # the method to look for - local method="$3" - local -n bos_mop_base_find_method_result="$4" + local method="$4" - local mro_store; $self mro-for-into "$class" mro_store + local mro_store; $self mro-for-into mro_store "$class" local -n mro="$mro_store" # minimal MRO: the class itself @@ -78,11 +78,12 @@ function bos/mop/base/invoke() { # local; this is a probably-bad-but-works workaround: use a # different variable name in different recursive calls local ffsname="to_invoke_${#FUNCNAME[*]}" - $self find-method-into "$class" "$start_from_class" "$method" "$ffsname" + $self find-method-into "$ffsname" "$class" "$start_from_class" "$method" # TODO: make the fields accessible! - bos-object-id/pack-self-into "$class" "$object_id" self + local self + bos-object-id/pack-self-into self "$class" "$object_id" # call it eval "\"\$$ffsname\" \"\$@\"" @@ -93,11 +94,11 @@ 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 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 "$class" "$object_id" bos_mop_base_create_object_result + bos-object-id/pack-self-into bos_mop_base_create_object_result "$class" "$object_id" # TODO invoke BUILD/BUILDALL? probably in a different metaclass, though diff --git a/bos-namespaces.sh b/bos-namespaces.sh index a208e02..0cb794a 100644 --- a/bos-namespaces.sh +++ b/bos-namespaces.sh @@ -2,8 +2,8 @@ ## namespace handling function bos-namespaces/encode-into() { - local string="$1" - local -n result="$2" + local -n result="$1" + local string="$2" local idx char tmp result='' @@ -18,22 +18,22 @@ function bos-namespaces/encode-into() { } function bos-namespaces/store-scalar-for-into() { - local name; bos-namespaces/encode-into "bos_${2}_${1}" name - local -n result="$3" + local -n result="$1" + local name; bos-namespaces/encode-into name "bos_${3}_${2}" declare -g "$name" result="$name" } function bos-namespaces/store-array-for-into() { - local name; bos-namespaces/encode-into "bos_${2}_${1}" name - local -n result="$3" + local -n result="$1" + local name; bos-namespaces/encode-into name "bos_${3}_${2}" declare -ga "$name" result="$name" } function bos-namespaces/store-dict-for-into() { - local name; bos-namespaces/encode-into "bos_${2}_${1}" name - local -n result="$3" + local -n result="$1" + local name; bos-namespaces/encode-into name "bos_${3}_${2}" declare -gA "$name" result="$name" } @@ -41,7 +41,7 @@ function bos-namespaces/store-dict-for-into() { # save all currently-visible functions in an associative array function bos-namespaces/start() { local namespace="$1";shift - local ns_store; bos-namespaces/store-dict-for-into saved_funcs "$namespace" ns_store + local ns_store; bos-namespaces/store-dict-for-into ns_store saved_funcs "$namespace" local -n saved_funcs="$ns_store" local _ funcname @@ -55,9 +55,9 @@ function bos-namespaces/start() { # diff all currently-visible function against the saved ones, list the # ones that differ function bos-namespaces/list-new-funcs-into() { - local namespace="$1";shift local -n result="$1" - local ns_store; bos-namespaces/store-dict-for-into saved_funcs "$namespace" ns_store + local namespace="$2" + local ns_store; bos-namespaces/store-dict-for-into ns_store saved_funcs "$namespace" local -n saved_funcs="$ns_store" local _ funcname new_function @@ -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-dict-for-into saved_funcs "$namespace" ns_store + local ns_store; bos-namespaces/store-dict-for-into ns_store saved_funcs "$namespace" local -n saved_funcs="$ns_store" local _ funcname new_function diff --git a/bos-object-id.sh b/bos-object-id.sh index ac7a7c8..47dfd6d 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 bos_object_id_class="$1" - local bos_object_id_self_id="$2" - local -n bos_object_id_result="$3" + local -n bos_object_id_result="$1" + local bos_object_id_class="$2" + local bos_object_id_self_id="$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 bos_object_id_self="$1" - local -n bos_object_id_class="$2" - local -n bos_object_id_self_id="$3" + local -n bos_object_id_class="$1" + local -n bos_object_id_self_id="$2" + local bos_object_id_self="$3" local _ 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 a199ed9..2da789a 100644 --- a/bos-sugar.sh +++ b/bos-sugar.sh @@ -25,7 +25,7 @@ function bos-sugar/current-class-into() { 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 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' } @@ -42,7 +42,7 @@ function bos-sugar/class-open() { 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 metaclass_ref; bos-namespaces/store-scalar-for-into metaclass_ref meta "$fq_class" local -n metaclass_object="$metaclass_ref" $metaclass_object set-superclasses-for "$fq_class" "$@" } @@ -53,9 +53,9 @@ function bos-sugar/class-open() { # this will get renamed into the class function new-into() { - local class _; bos-object/unpack-self-into "$self" class _ + local class _; bos-object/unpack-self-into class _ "$self" local -n new_into_result="$1"; shift - local metaclass_ref; bos-namespaces/store-scalar-for-into meta "$class" metaclass_ref + local metaclass_ref; bos-namespaces/store-scalar-for-into metaclass_ref meta "$class" local -n metaclass_object="$metaclass_ref" local new_object @@ -71,10 +71,10 @@ 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 metaclass_ref; bos-namespaces/store-scalar-for-into metaclass_ref meta "$fq_class" local -n metaclass_object="$metaclass_ref" - local -a methods_list; bos-namespaces/list-new-funcs-into "$fq_class" methods_list + local -a methods_list; bos-namespaces/list-new-funcs-into methods_list "$fq_class" bos-namespaces/qualify-funcs "$fq_class" "${methods_list[@]}" $metaclass_object make-mro-for "$fq_class" diff --git a/meta-stuff.rst.txt b/meta-stuff.rst.txt index 96c33a9..2a990e8 100644 --- a/meta-stuff.rst.txt +++ b/meta-stuff.rst.txt @@ -1,9 +1,11 @@ -next bits… +done bits * fix all the ``*-into`` functions/methods to take the destination variable name as 1st argument (otherwise optional arguments become a mess) +next bits… + * expose namespace open/close, and ``namespace $name; do …; done`` - use them inside ``class_open`` / ``class_close`` |