zsh-workers
 help / color / mirror / code / Atom feed
From: P.Stephenson@swansea.ac.uk
To: zsh-workers@math.gatech.edu (Zsh hackers list)
Subject: condition text clean up
Date: Mon, 05 Jun 95 17:02:55 +0100	[thread overview]
Message-ID: <12964.9506051602@pyro.swan.ac.uk> (raw)

While I'm waiting for some simulations to run, here's a minor
smartening up for the code you get back from conditions when zsh
disgorges some previously parsed code.  The main change is in the
parenthesising.

% fn () { [[ -t 0 && -t 1 && -t 2 || -t 3 || -t 4 && -t 5 || -t 6 ]] ; }
% which fn

Before:

fn () {
        [[ ( (  -t 0 && (  -t 1 &&  -t 2 ) ) || (  -t 3 || ( (  -t 4 &&  -t 5 ) ||  -t 6 ) ) ) ]]
}

After:

fn () {
        [[ ( -t 0 && -t 1 && -t 2 ) || -t 3 || ( -t 4 && -t 5 ) || -t 6 ]]
}

As you will see, I have retained some parentheses around && code which
are strictly unnecessary since that has a higher precedence than ||
anyway, just for clarity.  They are trivial to remove if that is
deemed preferable.

If you're interested, it's still not the same as what ksh does:  that
seems to remember all the parentheses whether they're necessary or
not.  I think this way's neater.

*** Src/text.c.cond	Wed May 31 05:10:41 1995
--- Src/text.c	Mon Jun  5 16:56:59 1995
***************
*** 158,164 ****
  gettext2(struct node *n)
  {
      Cmd nn;
-     Cond nm;
  
      if (!n || ((List) n) == &dummy_list)
  	return;
--- 158,163 ----
***************
*** 293,344 ****
  	getredirs(nn);
  	break;
      case N_COND:
! 	nm = _Cond(n);
! 	switch (nm->type) {
! 	case COND_NOT:
! 	    taddstr("! ");
! 	    gt2(nm->left);
! 	    break;
! 	case COND_AND:
! 	    taddstr("( ");
! 	    gt2(nm->left);
! 	    taddstr(" && ");
! 	    gt2(nm->right);
! 	    taddstr(" )");
! 	    break;
! 	case COND_OR:
! 	    taddstr("( ");
! 	    gt2(nm->left);
! 	    taddstr(" || ");
! 	    gt2(nm->right);
! 	    taddstr(" )");
! 	    break;
! 	default:
! 	    {
! 		static char *c1[] =
! 		{
! 		    " = ", " != ", " < ", " > ", " -nt ", " -ot ", " -ef ", " -eq ",
! 		    " -ne ", " -lt ", " -gt ", " -le ", " -ge "
! 		};
! 
! 		if (nm->right)
! 		    taddstr(nm->left);
! 		if (nm->type <= COND_GE)
! 		    taddstr(c1[nm->type - COND_STREQ]);
! 		else {
! 		    char c2[5];
! 
! 		    c2[0] = ' ';
! 		    c2[1] = '-';
! 		    c2[2] = nm->type;
! 		    c2[3] = ' ';
! 		    c2[4] = '\0';
! 		    taddstr(c2);
! 		}
! 		taddstr((nm->right) ? nm->right : nm->left);
! 	    }
! 	    break;
! 	}
  	break;
      case N_CASE:
  	{
--- 292,298 ----
  	getredirs(nn);
  	break;
      case N_COND:
! 	getcond(_Cond(n), 0);
  	break;
      case N_CASE:
  	{
***************
*** 407,412 ****
--- 361,427 ----
  	taddstr("done");
  	break;
      }
+ }
+ 
+ /* 
+  * Print a condition bracketed by [[ ... ]].
+  * With addpar non-zero, parenthesise the subexpression.
+  */
+ 
+ /**/
+ void
+ getcond(Cond nm, int addpar)
+ {
+     static char *c1[] =
+     {
+ 	"=", "!=", "<", ">", "-nt", "-ot", "-ef", "-eq",
+ 	"-ne", "-lt", "-gt", "-le", "-ge"
+     };
+ 
+     if (addpar)
+ 	taddstr("( ");
+     switch (nm->type) {
+     case COND_NOT:
+ 	taddstr("! ");
+ 	getcond(nm->left, _Cond(nm->left)->type <= COND_OR);
+ 	break;
+     case COND_AND:
+ 	getcond(nm->left, _Cond(nm->left)->type == COND_OR);
+ 	taddstr(" && ");
+ 	getcond(nm->right, _Cond(nm->right)->type == COND_OR);
+ 	break;
+     case COND_OR:
+ 	/*
+ 	 * This is deliberately over-generous with parentheses:
+ 	 * in fact omitting them gives correct precedence.
+ 	 */
+ 	getcond(nm->left, _Cond(nm->left)->type == COND_AND);
+ 	taddstr(" || ");
+ 	getcond(nm->right, _Cond(nm->right)->type == COND_AND);
+ 	break;
+     default:
+ 	if (nm->type <= COND_GE) {
+ 	    /* Binary test: `a = b' etc. */
+ 	    taddstr(nm->left);
+ 	    taddstr(" ");
+ 	    taddstr(c1[nm->type - COND_STREQ]);
+ 	    taddstr(" ");
+ 	    taddstr(nm->right);
+ 	} else {
+ 	    /* Unary test: `-f foo' etc. */ 
+ 	    char c2[4];
+ 
+ 	    c2[0] = '-';
+ 	    c2[1] = nm->type;
+ 	    c2[2] = ' ';
+ 	    c2[3] = '\0';
+ 	    taddstr(c2);
+ 	    taddstr(nm->left);
+ 	}
+ 	break;
+     }
+     if (addpar)
+ 	taddstr(" )");
  }
  
  /**/

-- 
Peter Stephenson <P.Stephenson@swansea.ac.uk>  Tel: +44 1792 205678 extn. 4461
WWW:  http://python.swan.ac.uk/~pypeters/      Fax: +44 1792 295324
Department of Physics, University of Wales, Swansea,
Singleton Park, Swansea, SA2 8PP, U.K.


                 reply	other threads:[~1995-06-05 16:11 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=12964.9506051602@pyro.swan.ac.uk \
    --to=p.stephenson@swansea.ac.uk \
    --cc=zsh-workers@math.gatech.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).