On Tue, May 21, 2013 at 3:51 AM, Roland Mainz <[email protected]> wrote:
> Hi!
>
> ----
>
> Attached (as "astksh20130503_typeset_copy_operator001.diff.txt") is a
> small patch which provides a type-independent copy-by-name operator.

Why are typeset options required to move/copy objects? Don't namerefs
on positional parameters create reference variables (`&arg'/`ref arg'
from C++/C#) that refer directly to an object right? If not ksh should
really have an explicit dereference operator instead.

`typeset -c' conflicts with Bash's (undocumented) "capcase" feature.
Also ksh fails on most of your examples if wrapped in a namespace.

typeset -T ObjStack=(
        compound -a nodes
        integer i=-1

        function push {
                nameref obj=$1 i=_.i
                typeset -m '_.nodes[++i].data=obj'
                printf '%#B, %d: %#B\n' .sh.fun $i _.nodes
        }

        function pop {
                nameref obj=$1 i=_.i
                printf '%#B, %d: %#B\n' .sh.fun $i _.nodes
                typeset -m obj=_.nodes[i--].data
        }
)

function main {
        compound foo=(integer val=5) bar=(integer val=6)
        typeset -A bok=([a]=11 [b]=22 [c]=33)
        typeset -a baz=(2 3 4) baz2=([2][2]=4 [2][4]=8 [3][2]=6 [3][4]=12)

        ObjStack o

        typeset x
        for x in foo bar bok baz baz2; do
                print -C x
                o.push "$x"
        done

        compound popc
        while ((o.i + 1)); do
                o.pop popc.store
                typeset -p popc
                print -C popc
        done
}

main

...output isn't quite what I expected (e.g. i=2 gets skipped when popping):

.sh.type.ObjStack.push, 0: (())
.sh.type.ObjStack.push, 1: (() ())
.sh.type.ObjStack.push, 2: (() () ())
.sh.type.ObjStack.push, 3: (() () () ())
.sh.type.ObjStack.push, 4: (() () () () ())
.sh.type.ObjStack.pop, 4: (() () () () ())
typeset -C popc=(typeset -a store=(typeset -a [2]=([2]=4 [4]=8)
typeset -a [3]=([2]=6 [4]=12) );)
(typeset -a store=(typeset -a [2]=([2]=4 [4]=8) typeset -a [3]=([2]=6
[4]=12) );)
.sh.type.ObjStack.pop, 3: (() () () () ())
typeset -C popc=(typeset -A store=([a]=11 [b]=22 [c]=33);)
(typeset -A store=([a]=11 [b]=22 [c]=33);)
.sh.type.ObjStack.pop, 1: (() () () () ())
typeset -C popc=(store=(typeset -l -i val=6))
(store=(typeset -l -i val=6))
.sh.type.ObjStack.pop, 0: (() () () () ())
typeset -C popc=(store=(typeset -l -i val=5))
(store=(typeset -l -i val=5)
_______________________________________________
ast-developers mailing list
[email protected]
http://lists.research.att.com/mailman/listinfo/ast-developers

Reply via email to