Subversion Repositories wimsdev

Rev

Rev 3718 | Rev 8185 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3718 Rev 8136
Line 12... Line 12...
12
 *
12
 *
13
 *  You should have received a copy of the GNU General Public License
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
14
 *  along with this program; if not, write to the Free Software
15
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
 */
16
 */
-
 
17
#include "../../Lib/libwims.h"
-
 
18
#include "mathexp.h"
17
 
19
 
18
struct {
20
struct {
19
    char *orig, *reverse;
21
    char *orig, *reverse;
20
} revtab[]={
22
} revtab[]={
21
    {"=",       "!="},
23
    {"=", "!="},
22
    {"!=",      "="},
24
    {"!=", "="},
23
    {"==",      "!="},
25
    {"==", "!="},
24
    {"<>",      "="},
26
    {"<>", "="},
25
    {"<",       ">="},
27
    {"<", ">="},
26
    {">",       "<="},
28
    {">", "<="},
27
    {"<=",      ">"},
29
    {"<=", ">"},
28
    {">=",      "<"},
30
    {">=", "<"},
29
    {"and",     "or"},
31
    {"and", "or"},
30
    {"or",      "and"},
32
    {"or", "and"},
31
};
33
};
32
#define revno (sizeof(revtab)/sizeof(revtab[0]))
34
#define revno (sizeof(revtab)/sizeof(revtab[0]))
33
 
35
 
34
        /* p will contain the new exp, and must has a buffer of MAX_LINELEN+1 */
36
/* p will contain the new exp, and must has a buffer of MAX_LINELEN+1 */
35
void _not(char *p)
37
void _not(char *p)
36
{
38
{
37
    char buf1[MAX_LINELEN+1], bufr[MAX_LINELEN+1];
39
    char buf1[MAX_LINELEN+1], bufr[MAX_LINELEN+1];
38
    char buft[16];
40
    char buft[16];
39
    int commas[MAX_COMMAS], cmcnt;
41
    int commas[MAX_COMMAS], cmcnt;
40
    char *pp;
42
    char *pp;
41
    int i, l;
43
    int i, l;
42
   
44
 
43
    snprintf(buf1,sizeof(buf1),"%s",find_word_start(p));
45
    snprintf(buf1,sizeof(buf1),"%s",find_word_start(p));
44
    retype:
46
    retype:
45
    if(buf1[0]==0) {*p=0; return;}      /* empty */
47
    if(buf1[0]==0) {*p=0; return;} /* empty */
46
    strip_trailing_spaces(buf1);
48
    strip_trailing_spaces(buf1);
47
    switch(_type(buf1,commas,&cmcnt)) {
49
    switch(_type(buf1,commas,&cmcnt)) {
48
        case exp_paren: {
50
     case exp_paren: {
49
            if(buf1[0]=='(' && find_matching(buf1+1,')')==buf1+strlen(buf1)-1) {
51
         if(buf1[0]=='(' && find_matching(buf1+1,')')==buf1+strlen(buf1)-1) {
50
                buf1[strlen(buf1)-1]=0; ovlstrcpy(buf1,find_word_start(buf1+1));
52
           buf1[strlen(buf1)-1]=0; ovlstrcpy(buf1,find_word_start(buf1+1));
51
                goto retype;
53
           goto retype;
52
            }
54
         }
53
            snprintf(p,MAX_LINELEN,"not %s",buf1); return;
55
         snprintf(p,MAX_LINELEN,"not %s",buf1); return;
54
        }
56
     }
55
        case exp_not: {
57
     case exp_not: {
56
            if(strncasecmp(buf1,"not",3)!=0) error("Syntax error.");
58
         if(strncasecmp(buf1,"not",3)!=0) error("Syntax error.");
57
            pp=find_word_start(buf1+3);
59
         pp=find_word_start(buf1+3);
58
            if(*pp=='(' && find_matching(pp+1,')')==pp+strlen(pp)-1) {
60
         if(*pp=='(' && find_matching(pp+1,')')==pp+strlen(pp)-1) {
59
                pp++; *(pp+strlen(pp)-1)=0;
61
          pp++; *(pp+strlen(pp)-1)=0;
60
            }
62
         }
61
            snprintf(p,MAX_LINELEN,"%s",pp); return;
63
         snprintf(p,MAX_LINELEN,"%s",pp); return;
62
        }
64
     }
63
        case exp_ineq:
65
     case exp_ineq:
64
        case exp_eq: {
66
     case exp_eq: {
65
            if(cmcnt!=2 || commas[1]-commas[0]>4) error("Syntax error.");
67
         if(cmcnt!=2 || commas[1]-commas[0]>4) error("Syntax error.");
66
            memmove(buft,buf1+commas[0],commas[1]-commas[0]);
68
         memmove(buft,buf1+commas[0],commas[1]-commas[0]);
67
            buft[commas[1]-commas[0]]=0;
69
         buft[commas[1]-commas[0]]=0;
68
            for(i=0;i<revno && strcmp(buft,revtab[i].orig)!=0;i++);
70
         for(i=0;i<revno && strcmp(buft,revtab[i].orig)!=0;i++);
69
            if(i>=revno) error("Software bug: bad sign.");
71
         if(i>=revno) error("Software bug: bad sign.");
70
            string_modify(buf1,buf1+commas[0],buf1+commas[1],revtab[i].reverse);
72
         string_modify(buf1,buf1+commas[0],buf1+commas[1],revtab[i].reverse);
71
            snprintf(p,MAX_LINELEN,"%s",buf1);
73
         snprintf(p,MAX_LINELEN,"%s",buf1);
72
            return;
74
         return;
73
        }
75
     }
74
        case exp_and: {
76
     case exp_and: {
75
            if(cmcnt<2 || (cmcnt&1)!=0) error("Syntax error.");
77
         if(cmcnt<2 || (cmcnt&1)!=0) error("Syntax error.");
76
            commas[cmcnt]=strlen(buf1);
78
         commas[cmcnt]=strlen(buf1);
77
            memmove(bufr,buf1,commas[0]); bufr[commas[0]]=0;
79
         memmove(bufr,buf1,commas[0]); bufr[commas[0]]=0;
78
            _not(bufr); snprintf(p,MAX_LINELEN,"%s",bufr); l=strlen(p);
80
         _not(bufr); snprintf(p,MAX_LINELEN,"%s",bufr); l=strlen(p);
79
            for(i=1;i<=cmcnt/2;i++) {
81
         for(i=1;i<=cmcnt/2;i++) {
80
                memmove(bufr,buf1+commas[2*i-1],commas[2*i]-commas[2*i-1]);
82
          memmove(bufr,buf1+commas[2*i-1],commas[2*i]-commas[2*i-1]);
81
                bufr[commas[2*i]-commas[2*i-1]]=0;
83
          bufr[commas[2*i]-commas[2*i-1]]=0;
82
                _not(bufr);
84
          _not(bufr);
83
                snprintf(p+l,MAX_LINELEN-l," or %s",bufr); l=strlen(p);
85
          snprintf(p+l,MAX_LINELEN-l," or %s",bufr); l=strlen(p);
84
            }
86
         }
85
            return;
87
         return;
86
        }
88
     }
87
        case exp_or: {
89
     case exp_or: {
88
            int commas2[MAX_COMMAS], cmcnt2;
90
         int commas2[MAX_COMMAS], cmcnt2;
89
            if(cmcnt<2 || (cmcnt&1)!=0) error("Syntax error.");
91
         if(cmcnt<2 || (cmcnt&1)!=0) error("Syntax error.");
90
            commas[cmcnt]=strlen(buf1);
92
         commas[cmcnt]=strlen(buf1);
91
            memmove(bufr,buf1,commas[0]); bufr[commas[0]]=0;
93
         memmove(bufr,buf1,commas[0]); bufr[commas[0]]=0;
92
            _not(bufr);
94
         _not(bufr);
93
            if(_type(bufr,commas2,&cmcnt2)!=exp_or)
95
         if(_type(bufr,commas2,&cmcnt2)!=exp_or)
94
              snprintf(p,MAX_LINELEN,"%s",bufr);
96
           snprintf(p,MAX_LINELEN,"%s",bufr);
95
            else snprintf(p,MAX_LINELEN,"(%s)",bufr);
97
         else snprintf(p,MAX_LINELEN,"(%s)",bufr);
96
            l=strlen(p);
98
         l=strlen(p);
97
            for(i=1;i<=cmcnt/2;i++) {
99
         for(i=1;i<=cmcnt/2;i++) {
98
                memmove(bufr,buf1+commas[2*i-1],commas[2*i]-commas[2*i-1]);
100
          memmove(bufr,buf1+commas[2*i-1],commas[2*i]-commas[2*i-1]);
99
                bufr[commas[2*i]-commas[2*i-1]]=0;
101
          bufr[commas[2*i]-commas[2*i-1]]=0;
100
                _not(bufr);
102
          _not(bufr);
101
                if(_type(bufr,commas2,&cmcnt2)!=exp_or)
103
          if(_type(bufr,commas2,&cmcnt2)!=exp_or)
102
                  snprintf(p+l,MAX_LINELEN-l," and %s",bufr);
104
            snprintf(p+l,MAX_LINELEN-l," and %s",bufr);
103
                else snprintf(p+l,MAX_LINELEN-l," and (%s)",bufr);
105
          else snprintf(p+l,MAX_LINELEN-l," and (%s)",bufr);
104
                l=strlen(p);
106
          l=strlen(p);
105
            }
107
         }
106
            return;
108
         return;
107
        }
109
     }
108
        case exp_imply: {
110
     case exp_imply: {
-
 
111
 
109
           
112
 
110
           
113
     }
111
        }
-
 
112
        case exp_quantifier: {
114
     case exp_quantifier: {
-
 
115
 
113
           
116
 
114
           
117
     }
115
        }
-
 
116
        default: {
118
     default: {
117
            snprintf(p,MAX_LINELEN,"not(%s)",buf1); return;
119
         snprintf(p,MAX_LINELEN,"not(%s)",buf1); return;
118
        }
120
     }
119
    }
121
    }
120
}
122
}
121
 
123
 
122
        /* Logic reverse an expression */
124
/* Logic reverse an expression */
123
void req_not(void)
125
void req_not(void)
124
{
126
{
125
    int i;
127
    int i;
126
    char *p, *ld;
128
    char *p, *ld;
127
    char nbuf[MAX_LINELEN+1];
129
    char nbuf[MAX_LINELEN+1];
128
    if(objlinecnt<2) return;
130
    if(objlinecnt<2) return;
129
    for(i=1;i<objlinecnt;i++) {
131
    for(i=1;i<objlinecnt;i++) {
130
        thisobjline=i; p=find_word_start(objline[i]);
132
     thisobjline=i; p=find_word_start(objline[i]);
131
        linelogdir=0; ld="";
133
     linelogdir=0; ld="";
132
        if(*p=='>') {
134
     if(*p=='>') {
133
            if(logdir<0) continue;
135
         if(logdir<0) continue;
134
            ld=">"; p=find_word_start(p+1); linelogdir=1;
136
         ld=">"; p=find_word_start(p+1); linelogdir=1;
135
        }
137
     }
136
        else if(*p=='<') {
138
     else if(*p=='<') {
137
            if(logdir>0) continue;
139
         if(logdir>0) continue;
138
            ld="<"; p=find_word_start(p+1); linelogdir=-1;
140
         ld="<"; p=find_word_start(p+1); linelogdir=-1;
139
        }
141
     }
140
        thislinelen=strlen(p); if(thislinelen<=0) continue;
142
     thislinelen=strlen(p); if(thislinelen<=0) continue;
141
        snprintf(nbuf,sizeof(nbuf),"%s",p);
143
     snprintf(nbuf,sizeof(nbuf),"%s",p);
142
        _not(nbuf); printf("%s %s\n",ld, nbuf);
144
     _not(nbuf); printf("%s %s\n",ld, nbuf);
143
    }
145
    }
144
 
-
 
145
}
146
}
146
 
-