00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "def.h"
00012
00013
00014 OP *single_op = NULL;
00015 OP *double_op = NULL;
00016 OP *name_op = NULL;
00017 OP *type_op = NULL;
00018
00019 #define OP_BYTES 1024
00020 static char *op_mem = NULL;
00021 static int free_byte = OP_BYTES;
00022
00023
00024 #define ALIGN 4
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 OP *
00040 op_new(pl)
00041 int pl;
00042 {
00043 char *malloc();
00044 int asize;
00045 OP *op;
00046
00047 asize = sizeof(OP) + pl;
00048 if ((asize % ALIGN) != 0) asize += ALIGN - (asize % ALIGN);
00049 #ifdef DEBUG
00050 printf("allocating operator node of %d bytes\n", asize);
00051 fflush(stdout);
00052 #endif
00053
00054 if (asize > OP_BYTES - free_byte) {
00055 char *old_op_mem = op_mem;
00056 op_mem = malloc(OP_BYTES);
00057 if (!op_mem) error("out of memory");
00058 ((char **) op_mem)[0] = old_op_mem;
00059 free_byte = sizeof(char *);
00060 # ifdef DEBUG
00061 printf("allocated operator node memory\n");
00062 # endif
00063 }
00064 op = (OP *) (op_mem + free_byte);
00065 free_byte += asize;
00066 op->length = (unsigned char) pl;
00067 op->eval = 0;
00068 op->hash = (RULE *) NULL;
00069 op->super = (OP *) NULL;
00070 op->other = (OP *) NULL;
00071 return op;
00072 }
00073
00074
00075
00076
00077
00078
00079
00080
00081 void
00082 op_mem_free()
00083 {
00084 char *next_op_mem;
00085 void rule_free();
00086 void free();
00087 int asize;
00088
00089 register int fpos;
00090 register RULE *rr;
00091
00092 while (op_mem) {
00093 next_op_mem = *((char **) op_mem);
00094 fpos = sizeof(char *);
00095 while (fpos<free_byte) {
00096 rr = ((OP *)(op_mem+fpos))->hash;
00097 while(rr) {
00098 rule_free(rr);
00099 rr = rr->next;
00100 }
00101 asize = sizeof(OP) + ((OP *)(op_mem+fpos))->length;
00102 if ((asize % ALIGN) != 0) asize += ALIGN - (asize % ALIGN);
00103 fpos += asize;
00104 }
00105 free(op_mem);
00106 op_mem = next_op_mem;
00107 }
00108 free_byte = OP_BYTES;
00109 single_op = NULL;
00110 double_op = NULL;
00111 name_op = NULL;
00112 type_op = NULL;
00113 }
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 void
00135 op_put(oplist, op)
00136 OP **oplist;
00137 OP *op;
00138 {
00139 int val;
00140 register OP *curr_op = *oplist;
00141 register OP *prev_op = NULL;
00142 char *arity_name();
00143
00144
00145
00146 if (curr_op == NULL) {
00147 op->next = NULL;
00148 *oplist = op;
00149 return;
00150 }
00151
00152
00153 while (curr_op) {
00154
00155
00156 val = strcmp(curr_op->pname, op->pname);
00157
00158
00159
00160
00161
00162
00163
00164 if (val == 0) {
00165 curr_op->other = op;
00166 op->other = curr_op;
00167 if (op->arity & BINARY) {
00168 val = 1;
00169 if (!(curr_op->arity & UNARY)) {
00170 fprintf(stderr, "existing operator: %s", curr_op->pname);
00171 fprintf(stderr, ", arity: %s\n", arity_name(curr_op->arity));
00172 fprintf(stderr, "input operator: %s", op->pname);
00173 fprintf(stderr, ", arity: %s\n", arity_name(op->arity));
00174 fprintf(stderr, "duplicate operator is binary\n");
00175 fprintf(stderr, "current operator must be unary\n");
00176 error("invalid duplicate operators");
00177 }
00178 }
00179 else if (op->arity & UNARY) {
00180
00181 if (!(curr_op->arity & BINARY)) {
00182 fprintf(stderr, "existing operator: %s", curr_op->pname);
00183 fprintf(stderr, ", arity: %s\n", arity_name(curr_op->arity));
00184 fprintf(stderr, "input operator: %s", op->pname);
00185 fprintf(stderr, ", arity: %s\n", arity_name(op->arity));
00186 fprintf(stderr, "duplicate operator is unary\n");
00187 fprintf(stderr, "current operator must be binary\n");
00188 error("invalid duplicate operators");
00189 }
00190 }
00191 else {
00192 fprintf(stderr, "existing operator: %s", curr_op->pname);
00193 fprintf(stderr, ", arity: %s\n", arity_name(curr_op->arity));
00194 fprintf(stderr, "input operator: %s", op->pname);
00195 fprintf(stderr, ", arity: %s\n", arity_name(op->arity));
00196 fprintf(stderr, "operators with the same name may only be");
00197 fprintf(stderr, " unary and binary (one of each).\n");
00198 error("invalid duplicate operators");
00199 }
00200 }
00201
00202 if (val > 0) {
00203 op->next = curr_op;
00204 if (prev_op)
00205 prev_op->next = op;
00206 else
00207 *oplist = op;
00208 return;
00209 }
00210
00211 prev_op = curr_op;
00212 curr_op = curr_op->next;
00213 }
00214
00215
00216 prev_op->next = op;
00217 op->next = NULL;
00218 }
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236 char *
00237 arity_name(arity)
00238 short arity;
00239 {
00240 static char buf[80];
00241
00242 switch(arity) {
00243 case NONASSOC: return("binary nonassociative");
00244 case LEFT: return("binary left associative");
00245 case RIGHT: return("binary right associative");
00246 case PREFIX: return("unary prefix");
00247 case POSTFIX: return("unary postfix");
00248 case OUTFIX1: return("unary outfix1");
00249 case OUTFIX2: return("unary outfix2");
00250 case NULLARY: return("nullary");
00251 case UNARY: return("unary");
00252 case BINARY: return("binary");
00253 case OP_NAME: return("name or type");
00254 case OP_NUM: return("number");
00255 case OP_STR: return("string");
00256 default: sprintf(buf, "unknown arity/associativity: 0x%x", arity);
00257 return(buf);
00258 }
00259 }
00260
00261
00262
00263
00264
00265
00266 void
00267 op_list_print(op)
00268 OP *op;
00269 {
00270 while (op) {
00271 fprintf(stderr, "operator: %s, ", op->pname);
00272 fprintf(stderr, "arity: %s, ", arity_name(op->arity));
00273 if (op->arity != OUTFIX1 && op->arity != OUTFIX2 &&
00274 op->arity != NULLARY)
00275 fprintf(stderr, "precedence: %ld\n", op->precedence);
00276 else fprintf(stderr, "\n");
00277 if (op->other) {
00278 fprintf(stderr, " linked to node whose operator is: %s ",
00279 op->other->pname);
00280 fprintf(stderr, "and whose arity is: %s\n",
00281 arity_name(op->other->arity));
00282 }
00283 op = op->next;
00284 }
00285 }
00286
00287
00288
00289
00290
00291
00292 void
00293 ops_print()
00294 {
00295 fprintf(stderr, "\f");
00296
00297
00298 fprintf(stderr, "\nsingle special character operators:\n\n");
00299 op_list_print(single_op);
00300
00301
00302 fprintf(stderr, "\ndouble special character operators:\n\n");
00303 op_list_print(double_op);
00304
00305
00306 fprintf(stderr, "\nalphanumeric operators:\n\n");
00307 op_list_print(name_op);
00308
00309
00310 fprintf(stderr, "\ntypes (missing single quotes):\n\n");
00311 op_list_print(type_op);
00312 }