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", |
31 | {"and", "or"}, |
30 | {"or", |
32 | {"or", "and"}, |
31 | }; |
33 | }; |
32 | #define revno (sizeof(revtab)/sizeof(revtab[0])) |
34 | #define revno (sizeof(revtab)/sizeof(revtab[0])) |
33 | 35 | ||
34 |
|
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;} |
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 |
|
50 | case exp_paren: { |
49 |
|
51 | if(buf1[0]=='(' && find_matching(buf1+1,')')==buf1+strlen(buf1)-1) { |
50 |
|
52 | buf1[strlen(buf1)-1]=0; ovlstrcpy(buf1,find_word_start(buf1+1)); |
51 |
|
53 | goto retype; |
52 |
|
54 | } |
53 |
|
55 | snprintf(p,MAX_LINELEN,"not %s",buf1); return; |
54 |
|
56 | } |
55 |
|
57 | case exp_not: { |
56 |
|
58 | if(strncasecmp(buf1,"not",3)!=0) error("Syntax error."); |
57 |
|
59 | pp=find_word_start(buf1+3); |
58 |
|
60 | if(*pp=='(' && find_matching(pp+1,')')==pp+strlen(pp)-1) { |
59 |
|
61 | pp++; *(pp+strlen(pp)-1)=0; |
60 |
|
62 | } |
61 |
|
63 | snprintf(p,MAX_LINELEN,"%s",pp); return; |
62 |
|
64 | } |
63 |
|
65 | case exp_ineq: |
64 |
|
66 | case exp_eq: { |
65 |
|
67 | if(cmcnt!=2 || commas[1]-commas[0]>4) error("Syntax error."); |
66 |
|
68 | memmove(buft,buf1+commas[0],commas[1]-commas[0]); |
67 |
|
69 | buft[commas[1]-commas[0]]=0; |
68 |
|
70 | for(i=0;i<revno && strcmp(buft,revtab[i].orig)!=0;i++); |
69 |
|
71 | if(i>=revno) error("Software bug: bad sign."); |
70 |
|
72 | string_modify(buf1,buf1+commas[0],buf1+commas[1],revtab[i].reverse); |
71 |
|
73 | snprintf(p,MAX_LINELEN,"%s",buf1); |
72 |
|
74 | return; |
73 |
|
75 | } |
74 |
|
76 | case exp_and: { |
75 |
|
77 | if(cmcnt<2 || (cmcnt&1)!=0) error("Syntax error."); |
76 |
|
78 | commas[cmcnt]=strlen(buf1); |
77 |
|
79 | memmove(bufr,buf1,commas[0]); bufr[commas[0]]=0; |
78 |
|
80 | _not(bufr); snprintf(p,MAX_LINELEN,"%s",bufr); l=strlen(p); |
79 |
|
81 | for(i=1;i<=cmcnt/2;i++) { |
80 |
|
82 | memmove(bufr,buf1+commas[2*i-1],commas[2*i]-commas[2*i-1]); |
81 |
|
83 | bufr[commas[2*i]-commas[2*i-1]]=0; |
82 |
|
84 | _not(bufr); |
83 |
|
85 | snprintf(p+l,MAX_LINELEN-l," or %s",bufr); l=strlen(p); |
84 |
|
86 | } |
85 |
|
87 | return; |
86 |
|
88 | } |
87 |
|
89 | case exp_or: { |
88 |
|
90 | int commas2[MAX_COMMAS], cmcnt2; |
89 |
|
91 | if(cmcnt<2 || (cmcnt&1)!=0) error("Syntax error."); |
90 |
|
92 | commas[cmcnt]=strlen(buf1); |
91 |
|
93 | memmove(bufr,buf1,commas[0]); bufr[commas[0]]=0; |
92 |
|
94 | _not(bufr); |
93 |
|
95 | if(_type(bufr,commas2,&cmcnt2)!=exp_or) |
94 |
|
96 | snprintf(p,MAX_LINELEN,"%s",bufr); |
95 |
|
97 | else snprintf(p,MAX_LINELEN,"(%s)",bufr); |
96 |
|
98 | l=strlen(p); |
97 |
|
99 | for(i=1;i<=cmcnt/2;i++) { |
98 |
|
100 | memmove(bufr,buf1+commas[2*i-1],commas[2*i]-commas[2*i-1]); |
99 |
|
101 | bufr[commas[2*i]-commas[2*i-1]]=0; |
100 |
|
102 | _not(bufr); |
101 |
|
103 | if(_type(bufr,commas2,&cmcnt2)!=exp_or) |
102 |
|
104 | snprintf(p+l,MAX_LINELEN-l," and %s",bufr); |
103 |
|
105 | else snprintf(p+l,MAX_LINELEN-l," and (%s)",bufr); |
104 |
|
106 | l=strlen(p); |
105 |
|
107 | } |
106 |
|
108 | return; |
107 |
|
109 | } |
108 |
|
110 | case exp_imply: { |
- | 111 | ||
109 | 112 | ||
110 | 113 | } |
|
111 | } |
- | |
112 |
|
114 | case exp_quantifier: { |
- | 115 | ||
113 | 116 | ||
114 | 117 | } |
|
115 | } |
- | |
116 |
|
118 | default: { |
117 |
|
119 | snprintf(p,MAX_LINELEN,"not(%s)",buf1); return; |
118 |
|
120 | } |
119 | } |
121 | } |
120 | } |
122 | } |
121 | 123 | ||
122 |
|
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 |
|
132 | thisobjline=i; p=find_word_start(objline[i]); |
131 |
|
133 | linelogdir=0; ld=""; |
132 |
|
134 | if(*p=='>') { |
133 |
|
135 | if(logdir<0) continue; |
134 |
|
136 | ld=">"; p=find_word_start(p+1); linelogdir=1; |
135 |
|
137 | } |
136 |
|
138 | else if(*p=='<') { |
137 |
|
139 | if(logdir>0) continue; |
138 |
|
140 | ld="<"; p=find_word_start(p+1); linelogdir=-1; |
139 |
|
141 | } |
140 |
|
142 | thislinelen=strlen(p); if(thislinelen<=0) continue; |
141 |
|
143 | snprintf(nbuf,sizeof(nbuf),"%s",p); |
142 |
|
144 | _not(nbuf); printf("%s %s\n",ld, nbuf); |
143 | } |
145 | } |
144 | - | ||
145 | } |
146 | } |
146 | - |