summaryrefslogtreecommitdiff
path: root/bos-mop-inheritance.sh
blob: 551dd18f3280cfe48cb8db57e55e65733d776154 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
}