Hi!

----

Here comes another testcase for typeset -m (based on the object stack type):
-- snip --
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"
        }

        # 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
{
        integer -S nested=0

        compound c
        objstack_t c.ost

        # push some objects
        compound foo=( integer val=5 )
        c.ost.pushobj foo

        # create, fill and push another stack object
        compound sc
        objstack_t sc.s
        compound c1=( integer a=1 )
        compound c2=( integer a=2 )
        sc.s.pushobj c1
        sc.s.pushobj c2
        c.ost.pushobj sc.s

        compound dummy
        c.ost.popobj dummy.s

#        print -v dummy

        if (( nested < 5 )) ; then
                (( nested++ ))
                main
                (( nested-- ))
        fi

        return 0
}

set -o nounset
main
main
-- snip --
... AFAIK the testcase should print nothing but
ast-ksh.2013-05-03+today(=15.3.2013) patches for typeset -m results
in:
-- snip --
stk.sh[87]: main[73]: c.ost.popobj[34]: typeset: dummy.s: no parent
stk.sh[87]: main[79]: main[73]: c.ost.popobj[34]: typeset: dummy.s: no parent
stk.sh[87]: main[79]: main[79]: main[73]: c.ost.popobj[34]: typeset:
dummy.s: no parent
stk.sh[87]: main[79]: main[79]: main[79]: main[73]: c.ost.popobj[34]:
typeset: dummy.s: no parent
stk.sh[87]: main[79]: main[79]: main[79]: main[79]: main[73]:
c.ost.popobj[34]: typeset: dummy.s: no parent
stk.sh[87]: main[79]: main[79]: main[79]: main[79]: main[79]:
main[73]: c.ost.popobj[34]: typeset: dummy.s: no parent
stk.sh[88]: main[73]: c.ost.popobj[34]: typeset: dummy.s: no parent
stk.sh[88]: main[79]: main[73]: c.ost.popobj[34]: typeset: dummy.s: no parent
stk.sh[88]: main[79]: main[79]: main[73]: c.ost.popobj[34]: typeset:
dummy.s: no parent
stk.sh[88]: main[79]: main[79]: main[79]: main[73]: c.ost.popobj[34]:
typeset: dummy.s: no parent
stk.sh[88]: main[79]: main[79]: main[79]: main[79]: main[73]:
c.ost.popobj[34]: typeset: dummy.s: no parent
stk.sh[88]: main[79]: main[79]: main[79]: main[79]: main[79]:
main[73]: c.ost.popobj[34]: typeset: dummy.s: no parent
-- snip --
(output is duplicated because "main" is called twice (intentionally to
see what will happen then))

----

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