From 6297ec2ae4263c346a9f7cf036055ca8cdb72c78 Mon Sep 17 00:00:00 2001 From: Gianni Ceccarelli Date: Fri, 14 Jul 2023 15:22:38 +0100 Subject: inheritance via MOP *probably*, I'm still not completely certain I've found all the weirdness --- bos-mop-inheritance.sh | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 bos-mop-inheritance.sh (limited to 'bos-mop-inheritance.sh') diff --git a/bos-mop-inheritance.sh b/bos-mop-inheritance.sh new file mode 100644 index 0000000..551dd18 --- /dev/null +++ b/bos-mop-inheritance.sh @@ -0,0 +1,51 @@ +#!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 isa "$1" "$2" +} + +function bos/mop/inheritance/set-superclasses-for() { + local isa_name; $self isa-for-into "$1" isa_name + local -n isa="$isa_name" + shift + + isa=( "$@" ) + + return 0 +} + +function bos/mop/inheritance/get-superclasses-for-into() { + local isa_name; $self isa-for-into "$1" isa_name + local -n isa="$isa_name" + local -n dest="$2" + + 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 "$class" mro_name + local -n mro="$mro_name" + + # TODO: use C3 and support multiple inheritance + + mro=( "$class" ) + local -a nextclasses + $self get-superclasses-for-into "$class" nextclasses + + while [[ "${#nextclasses}" -gt 0 ]]; do + mro+=( "${nextclasses[0]}" ) + class="${nextclasses[0]}" + $self get-superclasses-for-into "$class" nextclasses + done + + return 0 +} -- cgit v1.2.3