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

Re: N=5 echo $[N-1] why doesn't it work as expected



On Fri, 17 Jul 2009 11:01:21 +0100
Peter Stephenson <pws@xxxxxxx> wrote:
> % N=5 fn() { echo $N; }
> % which fn
> fn not found

This makes it consistent with other non-trivial structures (and the
documentation), disallowing assignments in front.

Of course, the whole point of anonymous functions is that you have
ordinary local variables, which are much more flexible:

% () { local N=8; print I\'m in an anonymous function with N=$N; }
I'm in an anonymous function with N=8

Index: Src/parse.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/parse.c,v
retrieving revision 1.80
diff -u -r1.80 parse.c
--- Src/parse.c	6 Jul 2009 20:44:29 -0000	1.80
+++ Src/parse.c	17 Jul 2009 20:22:47 -0000
@@ -1546,7 +1546,7 @@
 par_simple(int *complex, int nr)
 {
     int oecused = ecused, isnull = 1, r, argc = 0, p, isfunc = 0, sr = 0;
-    int c = *complex, nrediradd;
+    int c = *complex, nrediradd, assignments = 0;
 
     r = ecused;
     for (;;) {
@@ -1586,6 +1586,7 @@
 	    ecstr(name);
 	    ecstr(str);
 	    isnull = 0;
+	    assignments = 1;
 	} else if (tok == ENVARRAY) {
 	    int oldcmdpos = incmdpos, n, type2;
 
@@ -1606,6 +1607,7 @@
 		YYERROR(oecused);
 	    incmdpos = oldcmdpos;
 	    isnull = 0;
+	    assignments = 1;
 	} else
 	    break;
 	zshlex();
@@ -1667,8 +1669,12 @@
 	    zlong oldlineno = lineno;
 	    int onp, so, oecssub = ecssub;
 
+	    /* Error if too many function definitions at once */
 	    if (!isset(MULTIFUNCDEF) && argc > 1)
 		YYERROR(oecused);
+	    /* Error if preceding assignments */
+	    if (assignments)
+		YYERROR(oecused);
 
 	    *complex = c;
 	    lineno = 0;


-- 
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/



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