* condition text clean up
@ 1995-06-05 16:02 P.Stephenson
0 siblings, 0 replies; only message in thread
From: P.Stephenson @ 1995-06-05 16:02 UTC (permalink / raw)
To: Zsh hackers list
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.
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~1995-06-05 16:11 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1995-06-05 16:02 condition text clean up P.Stephenson
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).