Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Typeset with array



On Tue, 23 Jun 2015 13:25:22 -0700
Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> On Jun 23,  5:47pm, Peter Stephenson wrote:
> }
> } I think this is now basically working.  Any more comments, or should I
> } roll this out and see what happens?
> 
> I'd say go for it.

OK, here's one more test that's been bugging me (but does pass), then
I'll rebase onto the end of master and do any remaining tweaks there.

> 
> } +	    /*
> } +	     * Careful here: this must be the typeset case,
> } +	     * but we need to tell the lexer not to look
> } +	     * for assignments until we've finished the
> } +	     * present one.
> } +	     */
>
> This has me trying to think of ways to implement the ksh ([key]=value)
> syntax.
> 
>     typeset -a varname=([k1]=v1 [k2]=v2)

One possibility is to hook into ENVARRAY handling in parse.c at two
places. par_nl_worldist() is a key to this, though there's one
irrelevant case which is arguments for "for".  I don't know if the
answer is to get the lexer to detect k1 and v1 as tokens or to get the
parser to see if it fits that form a word at a time.  Then it needs to
go into wordcode in a special form --- though a specially tagged list of
an even number of items is good enough for this.

However, You could get away with detecting the form until the list is
expanded: that's now in two different places, addvars and execcmd, but
could easily be made common --- put something in front of the
ecgetlist() that retrieves the array.  As long as you detect it before
attempting to glob, to avoid NO_MATCH behaviour, it ought to work.  This
is easier as there are no wordcode changes and I can't see any obvious
gotchas.  I'm not sure what expansions apply: do k* get expanded at all?
Presumably v* get single word expansion?

pws

diff --git a/Test/B02typeset.ztst b/Test/B02typeset.ztst
index 48d1653..4afb189 100644
--- a/Test/B02typeset.ztst
+++ b/Test/B02typeset.ztst
@@ -595,7 +595,7 @@
      enable -r typeset
      disable typeset
      print reserved
-     eval $fn;fn
+     eval $fn; fn
   )
 0:reserved word and builtin interfaces
 >reserved
@@ -607,3 +607,47 @@
 >reserved
 >one word=two
 >
+
+  fn() {
+    emulate -L zsh
+    setopt typeset_silent
+    local k
+    typeset -A hash=(k1 v1 k2 v2)
+    typeset foo=word array=(more than one word)
+    for k in ${(ko)hash}; do
+      print $k $hash[$k]
+    done
+    print -l $foo $array
+    typeset -A hash
+    typeset foo array
+    for k in ${(ko)hash}; do
+      print $k $hash[$k]
+    done
+    print -l $foo $array
+    typeset hash=(k3 v3 k4 v4) array=(odd number here)
+    for k in ${(ko)hash}; do
+      print $k $hash[$k]
+    done
+    print -l $array
+  }
+  fn
+0:typeset preserves existing variable types
+>k1 v1
+>k2 v2
+>word
+>more
+>than
+>one
+>word
+>k1 v1
+>k2 v2
+>word
+>more
+>than
+>one
+>word
+>k3 v3
+>k4 v4
+>odd
+>number
+>here



Messages sorted by: Reverse Date, Date, Thread, Author