Rev 3840 | Rev 8185 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3840 | 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 | - | ||
- | 17 | #include "../../Lib/libwims.h" |
|
- | 18 | #include "mathexp.h" |
|
18 | void error(char *msg) |
19 | void error(char *msg) |
19 | { |
20 | { |
20 | fprintf(stderr,"%s\n",msg); |
21 | fprintf(stderr,"%s\n",msg); |
21 | printf("ERROR\n"); |
22 | printf("ERROR\n"); |
22 | exit(1); |
23 | exit(1); |
23 | } |
24 | } |
24 | 25 | ||
25 | void strcompress(char *src, char *dest) |
26 | void strcompress(char *src, char *dest) |
26 | { |
27 | { |
27 | char *p1, *p2; |
28 | char *p1, *p2; |
28 | char lastnospc, lastspc; |
29 | char lastnospc, lastspc; |
29 | p1=find_word_start(src); p2=dest; |
30 | p1=find_word_start(src); p2=dest; |
30 | lastnospc=lastspc=0; |
31 | lastnospc=lastspc=0; |
31 | for(;*p1 && p2-dest<MAX_LINELEN;p1++) { |
32 | for(;*p1 && p2-dest<MAX_LINELEN;p1++) { |
32 |
|
33 | if(isspace(*p1)) {lastspc=' '; continue;} |
33 |
|
34 | if(lastspc!=0) { |
34 |
|
35 | if((isalnum(lastnospc) || lastnospc=='_') && |
35 |
|
36 | (isalnum(*p1) || *p1=='_')) *p2++=' '; |
36 |
|
37 | } |
37 |
|
38 | lastspc=0; *p2++=*p1; lastnospc=*p1; |
38 | } |
39 | } |
39 | *p2=0; |
40 | *p2=0; |
40 | } |
41 | } |
41 | 42 | ||
42 |
|
43 | /* the type of an expression, with cutpoints */ |
43 | int _type(char *p, int commas[], int *commacnt) |
44 | int _type(char *p, int commas[], int *commacnt) |
44 | { |
45 | { |
45 | int i,l,lvl; |
46 | int i,l,lvl; |
46 | char *p1, *p2, *p3, *p4; |
47 | char *p1, *p2, *p3, *p4; |
47 | char buf[MAX_LINELEN+1]; |
48 | char buf[MAX_LINELEN+1]; |
48 | 49 | ||
49 | lvl=-1; *commacnt=0; |
50 | lvl=-1; *commacnt=0; |
50 | for(p1=find_word_start(p), p2=p1; *p2; p2=find_word_start(p3)) { |
51 | for(p1=find_word_start(p), p2=p1; *p2; p2=find_word_start(p3)) { |
51 |
|
52 | if(*p2=='.' || isdigit(*p2)) { |
52 |
|
53 | if(lvl<exp_number) lvl=exp_number; |
53 |
|
54 | (void)strtod(p2,&p3); continue; |
54 |
|
55 | } |
55 |
|
56 | if(*p2=='(') { |
56 |
|
57 | p3=find_matching(p2+1,')'); |
57 |
|
58 | if(lvl<exp_paren) lvl=exp_paren; |
58 |
|
59 | paren: |
59 |
|
60 | if(p3==NULL) error("Unmatched parentheses"); |
60 |
|
61 | p3++; continue; |
61 |
|
62 | } |
62 |
|
63 | if(*p2=='[') { |
63 |
|
64 | if(lvl<exp_matrix) lvl=exp_matrix; |
64 |
|
65 | p3=find_matching(p2+1,']'); goto paren; |
65 |
|
66 | } |
66 |
|
67 | if(*p2=='{') { |
67 |
|
68 | if(lvl<exp_set) lvl=exp_set; |
68 |
|
69 | p3=find_matching(p2+1,'}'); goto paren; |
69 |
|
70 | } |
70 |
|
71 | if(isalpha(*p2)) { |
71 |
|
72 | for(p3=p2; *p3=='_' || isalnum(*p3); p3++); |
72 |
|
73 | if(p3-p2>=16) goto notdefined; |
73 |
|
74 | memmove(buf,p2,p3-p2); buf[p3-p2]=0; |
74 |
|
75 | for(i=0;i<opalphano && strcmp(buf,opalpha[i].name)!=0; i++); |
75 |
|
76 | if(i<opalphano) { |
76 |
|
77 | l=opalpha[i].lvl; |
77 |
|
78 | if(l>lvl) {*commacnt=0; lvl=l;} |
78 |
|
79 | if(l==lvl && *commacnt<MAX_COMMAS-2) { |
79 |
|
80 | commas[(*commacnt)++]=p2-p; |
80 |
|
81 | commas[(*commacnt)++]=p3-p; |
81 |
|
82 | } |
82 |
|
83 | continue; |
83 |
|
84 | } |
84 |
|
85 | notdefined: p4=find_word_start(p3); |
85 |
|
86 | if(*p4=='(') { |
86 |
|
87 | if(lvl<exp_fn) lvl=exp_fn; |
87 |
|
88 | p3=find_matching(p4+1,')'); |
88 |
|
89 | if(p3==NULL) error("Unmatched parentheses."); |
89 |
|
90 | p4++; memmove(buf,p4,p3-p2); buf[p3-p4]=0; |
90 |
|
91 | p3++; |
91 |
|
92 | } |
92 |
|
93 | else { |
93 |
|
94 | if(lvl<exp_variable) lvl=exp_variable; |
94 |
|
95 | } |
95 |
|
96 | continue; |
96 |
|
97 | } |
97 |
|
98 | for(i=0;i<oppunctno && strncmp(p2,oppunct[i].name,strlen(oppunct[i].name))!=0; i++); |
98 |
|
99 | if(i>=oppunctno) error("Unknown operator."); |
99 |
|
100 | p3=p2+strlen(oppunct[i].name); l=oppunct[i].lvl; |
100 |
|
101 | if(l>lvl) {*commacnt=0; lvl=l;} |
101 |
|
102 | if(l==lvl && *commacnt<MAX_COMMAS-2) { |
102 |
|
103 | commas[(*commacnt)++]=p2-p; |
103 |
|
104 | commas[(*commacnt)++]=p3-p; |
104 |
|
105 | } |
105 | } |
106 | } |
106 | return lvl; |
107 | return lvl; |
107 | } |
108 | } |
108 | 109 | ||
109 | void getregex(char *p) |
110 | void getregex(char *p) |
110 | { |
111 | { |
111 | char *p1, *p2, *p3; |
112 | char *p1, *p2, *p3; |
112 | 113 | ||
113 | p1=find_word_start(p); |
114 | p1=find_word_start(p); |
114 | for(regexcnt=0; regexcnt<MAX_REGEX && *p1!=0; p1=find_word_start(p2)) { |
115 | for(regexcnt=0; regexcnt<MAX_REGEX && *p1!=0; p1=find_word_start(p2)) { |
115 |
|
116 | p2=find_word_end(p1); if(*p2) *p2++=0; |
116 |
|
117 | regexchk[regexcnt].srcreg=p1; |
117 |
|
118 | for(p3=p1; *p3 && (isalnum(*p3) || *p3=='.'); p3++); |
118 |
|
119 | if(*p3==0) { |
119 |
|
120 | regexchk[regexcnt].isvar=1; regexcnt++; |
120 |
|
121 | } |
121 |
|
122 | else { |
122 |
|
123 | regexchk[regexcnt].isvar=0; |
123 |
|
124 | if(regcomp(&(regexchk[regexcnt].cmpreg),p1,REG_EXTENDED|REG_ICASE)==0) |
124 |
|
125 | regexcnt++; |
125 |
|
126 | } |
126 | } |
127 | } |
127 | } |
128 | } |
128 | 129 | ||
129 |
|
130 | /* returns 1 if yes, 0 if no. */ |
130 | int checkregex(char *p) |
131 | int checkregex(char *p) |
131 | { |
132 | { |
132 | int i; |
133 | int i; |
133 | char buf[MAX_LINELEN+1]; |
134 | char buf[MAX_LINELEN+1]; |
134 | regmatch_t matchbuf[100]; |
135 | regmatch_t matchbuf[100]; |
135 | 136 | ||
136 | if(regexcnt<1) return 1; /* nothing to check; always true. */ |
137 | if(regexcnt<1) return 1; /* nothing to check; always true. */ |
137 | strcompress(p,buf); |
138 | strcompress(p,buf); |
138 | for(i=0;i<regexcnt;i++) { /* all regex words are ANDed. */ |
139 | for(i=0;i<regexcnt;i++) { /* all regex words are ANDed. */ |
139 |
|
140 | if(regexchk[i].isvar) { |
140 |
|
141 | if(varchr(buf,regexchk[i].srcreg)==NULL) return 0; |
141 |
|
142 | } |
142 |
|
143 | else if(regexec(&(regexchk[i].cmpreg),buf,80,matchbuf,0)!=0) return 0; |
143 | } |
144 | } |
144 | return 1; |
145 | return 1; |
145 | } |
146 | } |
146 | - |