Subversion Repositories wimsdev

Rev

Rev 3718 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*    Copyright (C) 2002-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
  2.  *
  3.  *  This program is free software; you can redistribute it and/or modify
  4.  *  it under the terms of the GNU General Public License as published by
  5.  *  the Free Software Foundation; either version 2 of the License, or
  6.  *  (at your option) any later version.
  7.  *
  8.  *  This program is distributed in the hope that it will be useful,
  9.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.  *  GNU General Public License for more details.
  12.  *
  13.  *  You should have received a copy of the GNU General Public License
  14.  *  along with this program; if not, write to the Free Software
  15.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17.  
  18. struct {
  19.     char *orig, *reverse;
  20. } revtab[]={
  21.     {"=",       "!="},
  22.     {"!=",      "="},
  23.     {"==",      "!="},
  24.     {"<>",      "="},
  25.     {"<",       ">="},
  26.     {">",       "<="},
  27.     {"<=",      ">"},
  28.     {">=",      "<"},
  29.     {"and",     "or"},
  30.     {"or",      "and"},
  31. };
  32. #define revno (sizeof(revtab)/sizeof(revtab[0]))
  33.  
  34.         /* p will contain the new exp, and must has a buffer of MAX_LINELEN+1 */
  35. void _not(char *p)
  36. {
  37.     char buf1[MAX_LINELEN+1], bufr[MAX_LINELEN+1];
  38.     char buft[16];
  39.     int commas[MAX_COMMAS], cmcnt;
  40.     char *pp;
  41.     int i, l;
  42.    
  43.     snprintf(buf1,sizeof(buf1),"%s",find_word_start(p));
  44.     retype:
  45.     if(buf1[0]==0) {*p=0; return;}      /* empty */
  46.     strip_trailing_spaces(buf1);
  47.     switch(_type(buf1,commas,&cmcnt)) {
  48.         case exp_paren: {
  49.             if(buf1[0]=='(' && find_matching(buf1+1,')')==buf1+strlen(buf1)-1) {
  50.                 buf1[strlen(buf1)-1]=0; strcpy(buf1,find_word_start(buf1+1));
  51.                 goto retype;
  52.             }
  53.             snprintf(p,MAX_LINELEN,"not %s",buf1); return;
  54.         }
  55.         case exp_not: {
  56.             if(strncasecmp(buf1,"not",3)!=0) error("Syntax error.");
  57.             pp=find_word_start(buf1+3);
  58.             if(*pp=='(' && find_matching(pp+1,')')==pp+strlen(pp)-1) {
  59.                 pp++; *(pp+strlen(pp)-1)=0;
  60.             }
  61.             snprintf(p,MAX_LINELEN,"%s",pp); return;
  62.         }
  63.         case exp_ineq:
  64.         case exp_eq: {
  65.             if(cmcnt!=2 || commas[1]-commas[0]>4) error("Syntax error.");
  66.             memmove(buft,buf1+commas[0],commas[1]-commas[0]);
  67.             buft[commas[1]-commas[0]]=0;
  68.             for(i=0;i<revno && strcmp(buft,revtab[i].orig)!=0;i++);
  69.             if(i>=revno) error("Software bug: bad sign.");
  70.             string_modify(buf1,buf1+commas[0],buf1+commas[1],revtab[i].reverse);
  71.             snprintf(p,MAX_LINELEN,"%s",buf1);
  72.             return;
  73.         }
  74.         case exp_and: {
  75.             if(cmcnt<2 || (cmcnt&1)!=0) error("Syntax error.");
  76.             commas[cmcnt]=strlen(buf1);
  77.             memmove(bufr,buf1,commas[0]); bufr[commas[0]]=0;
  78.             _not(bufr); snprintf(p,MAX_LINELEN,"%s",bufr); l=strlen(p);
  79.             for(i=1;i<=cmcnt/2;i++) {
  80.                 memmove(bufr,buf1+commas[2*i-1],commas[2*i]-commas[2*i-1]);
  81.                 bufr[commas[2*i]-commas[2*i-1]]=0;
  82.                 _not(bufr);
  83.                 snprintf(p+l,MAX_LINELEN-l," or %s",bufr); l=strlen(p);
  84.             }
  85.             return;
  86.         }
  87.         case exp_or: {
  88.             int commas2[MAX_COMMAS], cmcnt2;
  89.             if(cmcnt<2 || (cmcnt&1)!=0) error("Syntax error.");
  90.             commas[cmcnt]=strlen(buf1);
  91.             memmove(bufr,buf1,commas[0]); bufr[commas[0]]=0;
  92.             _not(bufr);
  93.             if(_type(bufr,commas2,&cmcnt2)!=exp_or)
  94.               snprintf(p,MAX_LINELEN,"%s",bufr);
  95.             else snprintf(p,MAX_LINELEN,"(%s)",bufr);
  96.             l=strlen(p);
  97.             for(i=1;i<=cmcnt/2;i++) {
  98.                 memmove(bufr,buf1+commas[2*i-1],commas[2*i]-commas[2*i-1]);
  99.                 bufr[commas[2*i]-commas[2*i-1]]=0;
  100.                 _not(bufr);
  101.                 if(_type(bufr,commas2,&cmcnt2)!=exp_or)
  102.                   snprintf(p+l,MAX_LINELEN-l," and %s",bufr);
  103.                 else snprintf(p+l,MAX_LINELEN-l," and (%s)",bufr);
  104.                 l=strlen(p);
  105.             }
  106.             return;
  107.         }
  108.         case exp_imply: {
  109.            
  110.            
  111.         }
  112.         case exp_quantifier: {
  113.            
  114.            
  115.         }
  116.         default: {
  117.             snprintf(p,MAX_LINELEN,"not(%s)",buf1); return;
  118.         }
  119.     }
  120. }
  121.  
  122.         /* Logic reverse an expression */
  123. void req_not(void)
  124. {
  125.     int i;
  126.     char *p, *ld;
  127.     char nbuf[MAX_LINELEN+1];
  128.     if(objlinecnt<2) return;
  129.     for(i=1;i<objlinecnt;i++) {
  130.         thisobjline=i; p=find_word_start(objline[i]);
  131.         linelogdir=0; ld="";
  132.         if(*p=='>') {
  133.             if(logdir<0) continue;
  134.             ld=">"; p=find_word_start(p+1); linelogdir=1;
  135.         }
  136.         else if(*p=='<') {
  137.             if(logdir>0) continue;
  138.             ld="<"; p=find_word_start(p+1); linelogdir=-1;
  139.         }
  140.         thislinelen=strlen(p); if(thislinelen<=0) continue;
  141.         snprintf(nbuf,sizeof(nbuf),"%s",p);
  142.         _not(nbuf); printf("%s %s\n",ld, nbuf);
  143.     }
  144.  
  145. }
  146.  
  147.