On Tue, May 21, 2013 at 10:51 AM, Roland Mainz <[email protected]> wrote:
> Attached (as "astksh20130503_typeset_copy_operator001.diff.txt") is a
> small patch which provides a type-independent copy-by-name operator.
>
> Usage example:
> -- snip --
> # 1. copy float by name
> $ ksh -c 'float i=5.5 ; typeset -c j=i ; print $i $j'
> 5.5 5.5
>
> # 2. copy associative array by name
> $ ksh -c 'compound c=( typeset -A i=( [1]=4 [3]=5 [7]=6 ) ) ; typeset
> -c c.j=c.i ; print -v c'
> (
>         typeset -A i=(
>                 [1]=4
>                 [3]=5
>                 [7]=6
>         )
>         typeset -A j=(
>                 [1]=4
>                 [3]=5
>                 [7]=6
>         )
> )
> -- snip --
>
> The idea is to have a _generic_ and type-independent facility to
> _copy_ variables by _name_ (instead of j="$i" ... which is copy by
> value and doesn't work the same way for all objects).

Here is a small demo using the "objstack_t" type:
-- snip --
$ cat stk.sh
function _assert
{
        integer line=$1
        shift
        print -u2 -f "ASSERT line=${line}: %q\n" "$@"
#       exit 1
}
alias _assert='_assert $LINENO'

# stack of objects
typeset -T objstack_t=(
        compound -a st
        integer st_n=0

        # push an object
        # argument is the name of a variable which will
        # be moved into the stack space
        function pushobj
        {
                nameref obj=$1
                typeset -m "_.st[$((_.st_n++))].obj=obj"
        }

        # copy an object into the stack
        # argument is the name of a variable which will
        # be copied into the stack space
        function copyobjtostack
        {
                nameref obj=$1
                typeset -c "_.st[$((_.st_n++))].obj=obj"
        }

        # print absolute variable name of object in head
        function printhead
        {
                printf '%s%s\n' "${!_}" ".st[${_.st_n}].obj"
        }

        # pop an object and return it to the location
        # specified by the variable name passed in
        function popobj
        {
                nameref obj=$1
                typeset -m "obj=_.st[$((--_.st_n))].obj"

                # "obj" should be removed from _.st[_.st_n] by
                # now... complain if there are any "leftovers"
                s="$(typeset -p _.st[_.st_n].obj)"
                [[ "$s" == '' ]] || \
                        _assert "_.st[_.st_n].obj == \"$s\""

                # remove empty array node which was created
                # when "pushobj" moved the obj into the array
                [[ "$(typeset -p _.st[_.st_n])" != '' ]] || \
                        _assert "_.st[_.st_n] is gone"

                unset _.st[_.st_n]
        }
)

function main
{
        compound c
        objstack_t c.ost

        # copy object foo twice on the stack
        compound foo=( integer val=5 )
        c.ost.copyobjtostack foo
        c.ost.copyobjtostack foo

        compound cthulhu
        bool cthulhu.taken_souls=true
        c.ost.copyobjtostack cthulhu
        cthulhu.taken_souls=false
        c.ost.copyobjtostack cthulhu

        print -v c

        return 0
}

set -o nounset
main
$ ~/bin/ksh stk.sh
(
        objstack_t ost=(
                typeset -l -i st_n=4
                st[0]=(
                        obj=(
                                typeset -l -i val=5
                        )
                )
                st[1]=(
                        obj=(
                                typeset -l -i val=5
                        )
                )
                st[2]=(
                        obj=(
                                _Bool taken_souls=true
                        )
                )
                st[3]=(
                        obj=(
                                _Bool taken_souls=false
                        )
                )
        )
)
-- snip --

----

Bye,
Roland

-- 
  __ .  . __
 (o.\ \/ /.o) [email protected]
  \__\/\/__/  MPEG specialist, C&&JAVA&&Sun&&Unix programmer
  /O /==\ O\  TEL +49 641 3992797
 (;O/ \/ \O;)
_______________________________________________
ast-developers mailing list
[email protected]
http://lists.research.att.com/mailman/listinfo/ast-developers

Reply via email to