Annotation of capa/capa51/pProj/capaUnit.c, revision 1.10
1.5 albertel 1: /* functions to handle the unit parser/comparison engine
2: Copyright (C) 1992-2000 Michigan State University
3:
4: The CAPA system is free software; you can redistribute it and/or
1.7 albertel 5: modify it under the terms of the GNU General Public License as
1.5 albertel 6: published by the Free Software Foundation; either version 2 of the
7: License, or (at your option) any later version.
8:
9: The CAPA system is distributed in the hope that it will be useful,
10: but WITHOUT ANY WARRANTY; without even the implied warranty of
11: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1.7 albertel 12: General Public License for more details.
1.5 albertel 13:
1.7 albertel 14: You should have received a copy of the GNU General Public
1.5 albertel 15: License along with the CAPA system; see the file COPYING. If not,
16: write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1.6 albertel 17: Boston, MA 02111-1307, USA.
18:
19: As a special exception, you have permission to link this program
20: with the TtH/TtM library and distribute executables, as long as you
21: follow the requirements of the GNU GPL in regard to all of the
22: software in the executable aside from TtH/TtM.
23: */
1.1 albertel 24:
25: /* =||>|===================== capaUnit.c =====================|<||= */
26: /* created by Isaac Tsai 1997 */
1.6 albertel 27: /* by Isaac Tsai 1997, 1998, 1999 */
1.1 albertel 28: /* =||>|========================================================|<||= */
29: #include <stdio.h> /* fopen() */
30: #include <stdlib.h>
31: #include <ctype.h> /* isalnum() */
32: #include <string.h>
33: #include <math.h>
1.8 albertel 34: #include <float.h>
1.1 albertel 35:
36: #include "capaParser.h"
37:
38: int PrefixTbl[QUARTER_K];
39: int BaseUnitcnt;
40: double CScale[BASEUNIT_LIMIT];
41: double CExp[BASEUNIT_LIMIT];
42: char CSymb[BASEUNIT_LIMIT][SYMBOL_MAXLEN];
43: Unit_t *UnitTree_p;
44: double MinSquared;
45: Unit_t *MinSquaredUnit_p;
46: Unit_t *InqueryUnit_p;
47: double *TmpAexp, *TmpBexp;
48: Unit_t *EquivUnit[BASEUNIT_LIMIT];
49: double MinValue[BASEUNIT_LIMIT];
50: int EquivUnitCnt;
51: char Sbuf[ONE_K_SIZE];
52: int Sidx;
53: Unit_t *Pstack[ONE_K_SIZE];
54: int Ptopidx;
55: int gUnitError;
56:
57: FILE *ufp;
58:
59: /* ==================================================================== */
60: void c_ignorewhite(FILE *f) /* ignore white spaces from a file stream */
61: {
62: register int c;
63: register int ok;
64:
65: ok = 0;
66: do {
67: do { c = getc(f);
68: } while ( isspace(c) );
69: ungetc(c,f);
70: if (c == '#') {
71: while (getc(f) != '\n');
72: } else ok = 1;
73: } while( ! ok);
74: }
75:
76: int c_getint(FILE *f) /* returns an integer from the file stream */
77: {
78: int c;
79: int value;
80:
81: c_ignorewhite(f);
82: c = fgetc(f);
83: if (!isdigit(c)) {
84: fprintf(stderr,"Error: Expected digit, got %c\n", c);
85: exit(-1);
86: }
87: ungetc(c,f);
88: fscanf(f,"%d", &value);
89: return(value);
90: }
91: int c_getsec_range(FILE *f,int *low,int *high)
92: {
93: int c;
94: int tmp, result;
95:
96: c_ignorewhite(f);
97: c = fgetc(f);
98: if( c == '[' ) { /* specify a range of sections */
99: do { c = getc(f); } while ( isspace(c) );
100: if (!isdigit(c)) {
101: fprintf(stderr,"Error in section range format, expecting a number.\n");
102: result = -1;
103: return (result);
104: }
105: ungetc(c,f);
106: fscanf(f,"%d", low);
107: do { c = getc(f); } while ( isspace(c) );
108: if( c == ',' ) {
109: do { c = getc(f); } while ( isspace(c) );
110: if (!isdigit(c)) {
111: fprintf(stderr,"Error in section range format, expecting a number.\n");
112: result = -1;
113: return (result);
114: }
115: ungetc(c,f);
116: fscanf(f,"%d", high);
117: do { c = getc(f); } while ( isspace(c) );
118: if( c == ']' ) {
119: if( *high < *low ) {
120: tmp= *high; *high = *low; *low =tmp;
121: }
122: if(*low <=0) {
123: *low = 1;
124: }
125: if(*high <=0) {
126: *high =1;
127: }
128: /* printf("Section range=>[%d,%d]\n",*low,*high); */
129: result = 2;
130: }
131: } else { /* no , specified */
132: result = -1;
133: return (result);
134: }
135: } else { /* specify a section only */
136: if (!isdigit(c)) {
137: fprintf(stderr,"Error: Expected digit, got %c\n", c);
138: result = -1;
139: return (result);
140: }
141: ungetc(c,f);
142: fscanf(f,"%d", low);
143: result = 1;
144: }
145: return (result);
146: }
147:
148: double c_getdouble(FILE *f)
149: {
150: int c;
151: double value;
152:
153: c_ignorewhite(f);
154: c = fgetc(f);
155: if (!isdigit(c)) {
156: fprintf(stderr,"Error: Expected digit, got %c\n", c);
157: exit(-1);
158: }
159: ungetc(c,f);
160: fscanf(f,"%lf", &value);
161: return(value);
162: }
163:
164: /* read until encountered an unrecognizable char */
165: /* space, #, anything other than alphanum, {}-^_ */
166: char *c_getword(FILE *f)
167: {
168: register int c;
169: register int idx;
170: char tmp_string[ONE_K];
171: char *new_string;
172:
173: idx = 0;
174: c_ignorewhite(f);
175: do { c = getc(f);
176: tmp_string[idx] = c;
177: idx++;
178: } while (isalnum(c) || c == '{' || c == '}' || c == '-' ||
179: c == '^' || c == '_' );
180: ungetc(c,f); idx--;
181: tmp_string[idx] = 0;
182: new_string = (char *)malloc( (idx+1)*sizeof(char) );
183: strncpy(new_string,tmp_string, (idx+1) );
184:
185: return (new_string);
186: }
187: /* read until encountered a newline, # */
188: char *c_getstring(FILE *f)
189: {
190: register int c;
191: register int idx;
192: char tmp_string[1024];
193: char *new_string;
194:
195: idx = 0;
196: c_ignorewhite(f);
197: do { c = getc(f);
198: tmp_string[idx] = c;
199: idx++;
200: } while (isalnum(c) || c == '{' || c == '}' || c == '-' ||
201: c == '^' || c == ' ' || c == ',' || c == ';' ||
202: c == '.' || c == '(' || c == ')' || c == '=' ||
203: c == '+' || c == '*' || c == '/' );
204: ungetc(c,f); idx--;
205: tmp_string[idx] = 0;
206: c = tmp_string[idx-1];
207: while( c == ' ') { /* get rid of trailing white space */
208: idx--;
209: c = tmp_string[idx-1];
210: }
211: tmp_string[idx] = 0;
212: new_string = (char *)malloc( (idx+1)*sizeof(char) );
213: strncpy(new_string,tmp_string, (idx+1) );
214:
215: return (new_string);
216: }
217: char *c_getcomment(FILE *f)
218: {
219: register int c;
220: register int idx;
221: char tmp_string[ONE_K];
222: char *new_string;
223:
224: idx = 0;
225: while (getc(f) != '#');
226: while ((c = getc(f)) == ' '); ungetc(c,f);
227: do { c = getc(f);
228: tmp_string[idx] = c;
229: idx++;
230: } while ( isprint(c) );
231: /*
232: } while (isalnum(c) || c == '{' || c == '}' || c == '-' ||
233: c == '^' || c == ' ' || c == ',' || c == ';' ||
234: c == '.' || c == '(' || c == ')' || c == '=' );
235: */
236: ungetc(c,f); idx--;
237: tmp_string[idx] = 0;
238: c = tmp_string[idx-1];
239: while( c == ' ') { /* get rid of trailing white space */
240: idx--;
241: c = tmp_string[idx-1];
242: }
243: tmp_string[idx] = 0;
244: new_string = (char *)malloc( (idx+1)*sizeof(char) );
245: strncpy(new_string,tmp_string, (idx+1) );
246:
247: return (new_string);
248: }
249: void c_moveto_unit(FILE *f)
250: {
251: register int c;
252: register int ok;
253:
254: ok = 0;
255: do {
256: do { c = getc(f);
257: } while (c != '<' );
258: c = getc(f);
259: if (c == '<') {
260: ungetc(c,f); ungetc(c,f); ok=1;
261: }
262: } while( ! ok);
263: }
264:
265: int c_gettype(FILE *f)
266: {
267: register int c;
268: register int idx;
269: char tmp_string[ONE_K];
270: char new_string[ONE_K];
271:
272: idx = 0;
273: PRESTART:
274: c_ignorewhite(f);
275: while ((c=getc(f)) != '<') { if ( (char)c==(char)EOF ) return U_UNKNOWN; }
276: c = getc(f);
277: if( c == '<' ) {
278: c_ignorewhite(f);
279: PREEND:
280: do { c = getc(f);
281: tmp_string[idx] = toupper(c);
282: idx++;
283: } while ( c != '>' );
284: c = getc(f);
285: if( c == '>' ) {
286: idx--;
287: tmp_string[idx] = 0;
288: c = tmp_string[idx-1];
289: while( c == ' ') { /* get rid of trailing white space */
290: idx--;
291: c = tmp_string[idx-1];
292: }
293: tmp_string[idx] = 0;
294: strncpy(new_string,tmp_string, (idx+1) );
295: } else {
296: ungetc(c,f);
297: goto PREEND;
298: }
299: } else {
300: goto PRESTART;
301: }
302: if( !strcmp(new_string,"BASE UNIT") ) {
303: return (U_BASE);
304: }
305: if( strcmp(new_string, "DERIVED UNIT") == 0 ) {
306: return (U_DERIVED);
307: }
308: if( strcmp(new_string, "PREFIX") == 0 ) {
309: return (U_PREFIX);
310: }
311: if( strcmp(new_string, "CONSTANTS") == 0 ) {
312: return (U_CONSTANT);
313: }
314: if( strcasecmp(new_string, "DEFAULTS") == 0 ) {
315: return (U_DEFAULT);
316: }
317: return (U_UNKNOWN);
318:
319: }
320:
321: /* =================================================================== */
322: /* =================================================================== */
323: /* returns: 0 success */
324: /* 1 the first units string u1_str could not be reduce to a valid unit */
325: /* 2 the second units string could not be reduced to a valid unit */
326: int
327: u_convert_unit(char *u1_str,char *u2_str,double *ratio)
328: {
329: Unit_t *ap, *bp;
330: int result=0;
331:
332: while( isspace(*u1_str) ) u1_str++;
333: while( isspace(*u2_str) ) u2_str++;
334: bp = parse_unit_expr(u2_str);
335: Ptopidx=0;
336: postwalk_utree(bp);
337: if( Ptopidx == 1 ) {
338: simplify_unit(Pstack[Ptopidx]);
339: bp = Pstack[Ptopidx];
340: /* print_unit_t(bp); */
341: ap = parse_unit_expr(u1_str);
342: Ptopidx=0;
343: postwalk_utree(ap);
344: if( Ptopidx == 1 ) {
345: simplify_unit(Pstack[Ptopidx]);
346: /* print_unit_t(Pstack[Ptopidx]); */
347: if( (Pstack[Ptopidx]->u_count != 0) ||
348: (Pstack[Ptopidx]->u_count == bp->u_count) ) { /* has unit */
349: *ratio = units_ratio(Pstack[Ptopidx], bp);
350: } else {
351: result = 1;
352: }
353: }
354: free_utree(ap);
355: } else {
356: result = 2;
357: }
358: free_utree(bp);
359: return (result);
360: }
361:
362: /* =================================================================== */
363:
364:
365:
366: Unit_t *
367: u_find_symb (char *name, Unit_t *t, int *result)
368: {
369:
370: if (t == NULL) return t;
371:
372: for (;;) {
373: if ( comp_unit_symb(name,t->u_symbol) < 0 ) {
374: if (t->u_left == NULL) {
375: /* printf("L not found\n"); */
376: *result = 0;
377: break;
378: }
379: t = t->u_left;
380: } else if ( comp_unit_symb(name,t->u_symbol) > 0 ) {
381: if (t->u_right == NULL) {
382: /* printf("R not found\n"); */
383: *result = 0;
384: break;
385: }
386: t = t->u_right;
387: } else {
388: *result = 1;
389: break;
390: }
391: }
392: return t;
393: }
394: /* ------------------------------------------------------------- */
395: /* use the input unit_t's element list to locate the min squared */
396: /* error fit of the unit tree */
397: /* report either exact fit or approx */
398:
399: void
400: u_find_name(Unit_t *t)
401: {
402: int ii;
403: Unit_E *eu_p;
404:
405: MinSquared = FLT_MAX;
406: EquivUnitCnt=0;
407: InqueryUnit_p = t;
408: /* printf("INQ[[%s,%s,%d]]\n", U_SYMB(t), U_NAME(t),U_COUNT(t)); */
409: TmpAexp = (double *)capa_malloc(BaseUnitcnt,sizeof(double));
410: TmpBexp = (double *)capa_malloc(BaseUnitcnt,sizeof(double));
411: for(ii=0;ii<BaseUnitcnt;ii++) {
412: TmpAexp[ii] = 0.0;
413: }
414: if( t->u_count > 0 ) {
415: for(eu_p = t->u_list; eu_p; eu_p = eu_p->ue_nextp) {
416: TmpAexp[eu_p->ue_index] = eu_p->ue_exp;
417: /* printf("(%d)^(%g) ",eu_p->ue_index,TmpAexp[eu_p->ue_exp]); */
418: }
419: /* printf("\n"); */
420: }
421: inorder_diff(UnitTree_p);
422: /*capa_mfree((char *)TmpAexp); capa_mfree((char *)TmpBexp);*/
423:
424: }
425:
426: void
427: print_matches(Unit_t *t)
428: {
429: double scale, factor;
430: Unit_t *tmp_p;
431: int ii;
432:
433: scale = t->u_scale;
434: if( MinSquared == 0.0 ) { /* exact match */
435: if( EquivUnitCnt > 0 ) {
436: printf(" Entered unit is equivalent to:\n");
437: for(ii=0;ii<EquivUnitCnt;ii++) {
438: tmp_p = EquivUnit[ii];
439: if( MinSquared == MinValue[ii] ) {
440: if( tmp_p->u_type == U_BASE ) { /* if there is a base unit */
441: MinSquaredUnit_p = tmp_p;
442: }
443: factor = scale / tmp_p->u_scale;
444: printf(" <<%g %s>>", factor,U_SYMB(tmp_p));
445: }
446: }
447: printf("\n");
448:
449: }
450: } else { /* no exact match */
451: if( EquivUnitCnt > 0 ) {
452: printf(" Entered unit is approximated by:\n");
453: for(ii=0;ii<EquivUnitCnt;ii++) {
454: tmp_p = EquivUnit[ii];
455: if( MinSquared == MinValue[ii] ) {
456: printf(" <<%s>> ", U_SYMB(tmp_p) );
457: }
458: }
459: printf("\n");
460: }
461: }
462: }
463:
464: /* ------------------------------------ */
465: double
466: u_squared_diff(Unit_t *a, Unit_t *b)
467: {
468: double result;
469: double squared_diff = 0.0;
470: int ii;
471: Unit_E *eu_p;
472:
473:
474: for(ii=0;ii<BaseUnitcnt;ii++) {
475: TmpAexp[ii] = 0.0;
476: TmpBexp[ii] = 0.0;
477: }
478: if( a->u_count > 0 ) {
479: for(eu_p= a->u_list; eu_p; eu_p = eu_p->ue_nextp) {
480: TmpAexp[eu_p->ue_index] = eu_p->ue_exp;
481: }
482: }
483: if( b->u_count > 0 ) {
484: for(eu_p= b->u_list; eu_p; eu_p = eu_p->ue_nextp) {
485: TmpBexp[eu_p->ue_index] = eu_p->ue_exp;
486: /* printf("Exp[%d]=%g ",ii,TmpBexp[ii]); */
487: }
488: /* printf("\n"); */
489: }
490: for(ii=0;ii<BaseUnitcnt;ii++) {
491: result = TmpAexp[ii] - TmpBexp[ii];
492: squared_diff = squared_diff + result*result;
493: }
494:
495: return (squared_diff);
496: }
497:
498: double
499: u_sq_diff(Unit_t *b)
500: {
501: double result;
502: double squared_diff = 0.0;
503: int ii;
504: Unit_E *eu_p;
505:
506:
507: for(ii=0;ii<BaseUnitcnt;ii++) {
508: TmpBexp[ii] = 0.0;
509: }
510: if( b->u_count > 0 ) {
511: for(eu_p= b->u_list; eu_p; eu_p = eu_p->ue_nextp) {
512: TmpBexp[eu_p->ue_index] = eu_p->ue_exp;
513: /* printf("Exp[%d]=%g ",ii,TmpBexp[ii]); */
514: }
515: /* printf("\n"); */
516: } else if( b->u_type == U_BASE ) {
517: TmpBexp[b->u_index] = 1.0;
518: }
519: for(ii=0;ii<BaseUnitcnt;ii++) {
520: result = TmpAexp[ii] - TmpBexp[ii];
521: squared_diff = squared_diff + result*result;
522: }
523:
524: return (squared_diff);
525:
526: }
527: /* ------------------------------------ */
528:
529: int
530: inorder_diff(node_p) Unit_t *node_p;
531: {
532: int result;
533: double sq_diff=0.0;
534:
535: if( node_p == NULL ) return (1);
536:
537: result = inorder_diff(U_LEFT(node_p));
538: if( result ) {
539: sq_diff = u_sq_diff(node_p);
540: /*
541: printf("DIFF [%s,%s,%d] - [%s,%s,%d] = %g\n",
542: U_SYMB(InqueryUnit_p), U_NAME(InqueryUnit_p),U_COUNT(InqueryUnit_p),
543: U_SYMB(node_p), U_NAME(node_p),U_COUNT(node_p),sq_diff);
544: */
545: if( MinSquared > sq_diff) {
546: MinSquaredUnit_p = node_p;
547: MinSquared = sq_diff;
548: } else if ( MinSquared == sq_diff) {
549: EquivUnit[EquivUnitCnt] = node_p;
550: MinValue[EquivUnitCnt] = sq_diff;
551: EquivUnitCnt++;
552: }
553: }
554: result = inorder_diff(U_RIGHT(node_p));
555:
556: return (result);
557: }
558:
559:
560: int
561: alphaorder_utree(node_p) Unit_t *node_p;
562: {
563: int result;
564:
565: if( node_p == NULL ) return (1);
566:
567: result = alphaorder_utree(U_LEFT(node_p));
568: if( result ) printf(" (%s,%s)\n", U_SYMB(node_p), U_NAME(node_p) );
569: result = alphaorder_utree(U_RIGHT(node_p));
570:
571: return (result);
572: }
573:
574: int
575: w_alphaorder_utree(node_p) Unit_t *node_p;
576: {
577: int result;
578:
579: if( node_p == NULL ) return (1);
580:
581: result = alphaorder_utree(U_LEFT(node_p));
582: if( result ) {
583: printf(" (%s,%s)\n", U_SYMB(node_p), U_NAME(node_p) );
584: }
585: result = alphaorder_utree(U_RIGHT(node_p));
586:
587: return (result);
588: }
589:
590: /* --------------------------------------------------------------------- */
591: void
592: print_unit_tree(int mode)
593: {
594: if( mode == 1 ) {
595: alphaorder_utree(UnitTree_p);
596: } else {
597: w_alphaorder_utree(UnitTree_p);
598: }
599: }
600:
601:
602: int
603: preorder_utree(node_p) Unit_t *node_p;
604: {
605: int result;
606:
607: if( node_p == NULL ) return (1);
608: printf("Preorder=[[%s,%s,%d]]\n", U_SYMB(node_p), U_NAME(node_p),U_COUNT(node_p));
609: result = preorder_utree(U_LEFT(node_p));
610: if( result ) result = preorder_utree(U_RIGHT(node_p));
611: return (result);
612: }
613: int
614: inorder_utree(node_p) Unit_t *node_p;
615: {
616: int result;
617:
618: if( node_p == NULL ) return (1);
619:
620: result = inorder_utree(U_LEFT(node_p));
621: if( result ) printf("INorder=[[%s,%s,%d]]\n",
622: U_SYMB(node_p), U_NAME(node_p),U_COUNT(node_p));
623: result = inorder_utree(U_RIGHT(node_p));
624:
625: return (result);
626: }
627: int
628: postorder_utree(node_p) Unit_t *node_p;
629: {
630: int result;
631:
632: if( node_p == NULL ) return (1);
633:
634: result = postorder_utree(U_LEFT(node_p));
635: if( result ) result = postorder_utree(U_RIGHT(node_p));
636: if( result ) {
637: switch(U_TYPE(node_p)) {
638: case U_DERIVED: print_unit_t(node_p);
639: break;
640: case U_CONSTANT: printf("(%g)",U_SCALE(node_p));
641: break;
642: case U_OP_POWER: printf("^");
643: break;
644: case U_OP_TIMES: printf("*");
645: break;
646: case U_OP_PLUS: printf("+");
647: break;
648: case U_OP_MINUS: printf("-");
649: break;
650: case U_OP_DIVIDE: printf("/");
651: break;
652: default: printf("()");
653: break;
654: }
655: }
656: return (result);
657: }
658:
1.4 albertel 659: /* returns 1 on okay, 2 on error*/
1.1 albertel 660: int
661: postwalk_utree(Unit_t *n_p)
662: {
663: int result;
664:
665: if( n_p == NULL ) return (1);
666:
667: result = postwalk_utree(U_LEFT(n_p));
1.4 albertel 668: if (result !=2) {
669: if( result ) result = postwalk_utree(U_RIGHT(n_p));
670: if (result !=2) {
671: if( result ) {
672: switch(U_TYPE(n_p)) {
673: case U_DERIVED: Ptopidx++; Pstack[Ptopidx] = n_p; /* push into stack */
674: break;
675: case U_CONSTANT: Ptopidx++; Pstack[Ptopidx] = n_p; /* push into stack */
676: break;
677: case U_UNKNOWN: result=2;
678: /*push into stack anyway, try to parse rest of tree */
679: break;
680: case U_OP_POWER: printf("^"); result=2;
681: break;
682: case U_OP_TIMES: process_op(U_OP_TIMES); /* process operator */
683: break;
684: case U_OP_PLUS: printf("+"); result=2;
685: break;
686: case U_OP_MINUS: printf("-"); result=2;
687: break;
688: case U_OP_DIVIDE: process_op(U_OP_DIVIDE); /* process operator */
689: break;
690: default: printf("()"); result=2;
691: break;
692: }
693: }
1.1 albertel 694: }
695: }
696: return (result);
697: }
698:
699: void
700: process_op(int op)
701: {
702: Unit_t *ap, *bp;
703: double exp_scale;
704: int no_error=1;
705:
706: bp = Pstack[Ptopidx--];
707: ap = Pstack[Ptopidx--];
708:
709: switch(op) {
710: case U_OP_TIMES: exp_scale = 1.0; break;
711: case U_OP_DIVIDE: exp_scale = -1.0; break;
712: case U_OP_PLUS:
713: case U_OP_MINUS: no_error = u_pm_op(ap,bp,op);
714: if(no_error) {
715: Ptopidx++;
716: Pstack[Ptopidx] = ap;
717: }
718: break;
719: default: no_error=0;
720: printf("No such op on the parse tree!\n");
721: break;
722: }
723: if(no_error) {
724: u_copy_unit(ap, bp, exp_scale);
725: Ptopidx++;
726: Pstack[Ptopidx] = ap;
727: }
728: }
729:
730: void
731: process_utree(Unit_t *t)
732: {
733: Ptopidx=0;
734: postwalk_utree(t);
735: if( Ptopidx == 1 ) {
736: /* printf("Correctly parsed!\n"); */
737: printf("Unit:%s\n",Sbuf);
738: simplify_unit(Pstack[Ptopidx]);
739: Pstack[Ptopidx]->u_symbol[0]='\0';
740: /*sprintf(Pstack[Ptopidx]->u_symbol,"");*/
741: print_unit_t(Pstack[Ptopidx]);
742: u_find_name(Pstack[Ptopidx]);
743: print_matches(Pstack[Ptopidx]);
744: free_utree(t);
745: }
746: }
747:
748: /* ============================================================== */
749: /* called from capaCommon.c */
750: /* */
751: /* UNIT_FAIL */
752: /* NO_UNIT */
753: /* result: UNIT_OK correct */
754: /* */
755: /* -------------------------------------------------------------- */
756: int check_correct_unit(char *u_symb,Unit_t *t,double *scale)
757: {
758: Unit_t *ap;
759: int result=UNIT_OK;
760:
761: #ifdef UNIT_DBUG
1.4 albertel 762: if ((ufp=fopen("unit.DBUG","a"))==NULL) { fprintf(stderr,"Error: can't open login debug\n"); return UNIT_FAIL; }
1.1 albertel 763: #endif
764:
1.3 albertel 765: while( isspace(*u_symb) ) u_symb++;
766: /* <= change this to search from the end of string */
767: /* or to get rid of all the white spaces */
768:
769:
1.1 albertel 770: ap = parse_unit_expr(u_symb);
771: Ptopidx=0;
1.4 albertel 772:
773: if (postwalk_utree(ap)==1) {
1.1 albertel 774: #ifdef UNIT_DBUG
1.4 albertel 775: fprintf(ufp,"Ptopidx %d\n",Ptopidx);
1.1 albertel 776: #endif
1.4 albertel 777: if( Ptopidx == 1 ) {
778: simplify_unit(Pstack[Ptopidx]);
779:
780: if( (Pstack[Ptopidx]->u_count != 0) ||
781: (Pstack[Ptopidx]->u_count == t->u_count) ) { /* has unit */
782: *scale = units_ratio(Pstack[Ptopidx], t);
783: if( *scale == 0.0 ) {
784: result = UNIT_FAIL;
785: }
786: free_utree(ap);
787: } else {
788: result = UNIT_FAIL;
1.1 albertel 789: }
1.4 albertel 790: } else { /* invalid unit representation */
1.1 albertel 791: result = UNIT_FAIL;
792: }
1.4 albertel 793: } else {
1.1 albertel 794: result = UNIT_FAIL;
795: }
796: #ifdef UNIT_DBUG
797: fclose(ufp);
798: #endif
799: return (result);
800: }
801:
802: /* ============================================================= */
803: int
804: free_units()
805: {
806: free_utree(UnitTree_p);
807: UnitTree_p=NULL;
808: return 0;
809: }
810:
811: int
812: free_utree(Unit_t *t)
813: {
814: int result=1;
815:
816: if( t == NULL ) return (1);
817: u_postfree(t);
818: t=NULL;
819:
820: return (result);
821: }
822:
823:
824: int
825: u_postfree(Unit_t *t)
826: {
827: int result;
828:
829: if( t == NULL ) return (1);
830:
831: result = u_postfree(U_LEFT(t));
832: if( result ) result = u_postfree(U_RIGHT(t));
833: if( result ) {
834: if( t->u_comment ) {
835: capa_mfree((char *)t->u_comment);
836: }
837: freelist_unit_e(t->u_list);
838: capa_mfree((char *)t);
839: }
840: return (result);
841: }
842:
843:
844: void
845: print_unit_t(Unit_t *t)
846: {
847: Unit_E *ue_p;
848:
849: /* printf(" Unit::[%s,%d]= %g * ", t->u_symbol,t->u_count,t->u_scale); */
850: printf(" Unit::[%s] = %g * ", t->u_symbol, t->u_scale);
851: for(ue_p=t->u_list; ue_p ; ue_p = ue_p->ue_nextp) {
852: /*
853: printf("<%s,%d,%g,%g> ",ue_p->ue_symbol,ue_p->ue_index,ue_p->ue_scale,ue_p->ue_exp);
854: */
855: printf("(%g*%s^%g) ",ue_p->ue_scale,ue_p->ue_symbol,ue_p->ue_exp);
856: }
857: printf("\n");
858:
859: }
860: /* ----------------------------------------------------------- */
861: /* copy the Unit_E linked list from b_p->u_list to a_p->u_list */
862: /* create some Unit_E nodes in a_p->u_list if needed and */
863: /* leave b_p->u_list intact */
864: /* a_p->u_scale is multiplied by pow(b_p->u_scale,exp_scale) */
865: /* ----------------------------------------------------------- */
866: void
867: u_copy_unit(Unit_t *a_p, Unit_t *b_p, double exp_scale)
868: {
869: Unit_E *oe_p, *ne_p, *last_p;
870: int ii;
871: double scale;
872:
873: if( a_p->u_count > 0 ) {
874: for(last_p = a_p->u_list; last_p->ue_nextp; last_p = last_p->ue_nextp) { }
875: } else {
876: a_p->u_list = last_p = NULL;
877: }
878: if( b_p->u_count > 0 ) {
879: oe_p = b_p->u_list;
880: for(ii=0;ii<b_p->u_count;ii++) {
881: ne_p = (Unit_E *) capa_malloc(1, sizeof(Unit_E)); /* *** */
882: ne_p->ue_scale = oe_p->ue_scale;
883: ne_p->ue_exp = oe_p->ue_exp * exp_scale;
884: ne_p->ue_index = oe_p->ue_index;
885: strcpy(ne_p->ue_symbol, oe_p->ue_symbol);
886: oe_p = oe_p->ue_nextp;
887: if( last_p == NULL ) {
888: a_p->u_list = ne_p;
889: } else {
890: last_p->ue_nextp = ne_p;
891: }
892: last_p = ne_p;
893: a_p->u_count++;
894: }
895: scale = pow(b_p->u_scale, exp_scale);
896: a_p->u_scale = a_p->u_scale * scale;
897: /* printf("Found scale=%g=%g\n",a_p->u_scale,b_p->u_scale); */
898: } else {
1.2 albertel 899: if( b_p->u_type == U_BASE ) {
1.1 albertel 900: /* *b_p is a base unit, so create a one element unit */
901: ne_p = (Unit_E *) capa_malloc(1, sizeof(Unit_E)); /* *** */
902: ne_p->ue_scale = b_p->u_scale;
903: ne_p->ue_exp = exp_scale;
904: ne_p->ue_index = b_p->u_index;
905: strcpy(ne_p->ue_symbol, b_p->u_symbol);
906: if( last_p == NULL ) {
907: a_p->u_list = ne_p;
908: } else {
909: last_p->ue_nextp = ne_p;
910: }
911: last_p = ne_p;
912: a_p->u_count++;
1.2 albertel 913: } else if( b_p->u_type == U_DERIVED) {
914: /* derived units but without any units elements (scalar) */
1.4 albertel 915: /* do nothing, ignore this units WE REALLY MEAN THIS DON'T DO THE NEXT LINE!*/
916: /*a_p->u_count++;*/
1.10 ! albertel 917: scale = pow(b_p->u_scale, exp_scale);
! 918: a_p->u_scale = a_p->u_scale * scale;
1.1 albertel 919: } else if( b_p->u_type == U_CONSTANT ) {
920: scale = pow(b_p->u_scale, exp_scale);
921: a_p->u_scale = a_p->u_scale * scale;
922: } else {
923: printf("This node has no u_e list and Type unknown\n");
924: }
925: }
926: }
927: int
928: u_pm_op(Unit_t *a_p, Unit_t *b_p, int op)
929: {
930: int result=0;
931:
932: if( a_p->u_count > 0 || b_p->u_count > 0 ) {
933: printf(" cannot add or sub units at this moment\n");
934: return result;
935: }
936: if( op == U_OP_PLUS ) {
937: a_p->u_scale = a_p->u_scale + b_p->u_scale;
938: } else {
939: a_p->u_scale = a_p->u_scale - b_p->u_scale;
940: }
941: return 1;
942: }
943:
944: int
945: u_parsepower(char *unit_str)
946: {
947: int exp, ii;
948: char *ch_p, exp_str[16];
949:
950: ch_p = unit_str;
951: while( isspace(*ch_p) ) { ch_p++; }
952: ii=0;
953: while( isdigit(*ch_p) ) {
954: ch_p++;
955: }
956: while( isspace(*ch_p) ) { ch_p++; }
957: if( *ch_p == '^' ) {
958: ch_p++;
959: }
960: while( isspace(*ch_p) ) { ch_p++; }
961: if( *ch_p == '{' ) {
962: ch_p++;
963: }
964: while( isspace(*ch_p) ) { ch_p++; }
965: ii=0;
966: while( isdigit(*ch_p) || *ch_p == '-' || *ch_p == '+' ) {
967: exp_str[ii++] = *ch_p;
968: ch_p++;
969: }
970: exp_str[ii]=0;
971: sscanf(exp_str,"%d", &exp);
972: return (exp);
973: }
974:
975: /* ------------------------------------------- */
976: /* scan a number of the form indicated below from the input buffer */
977: /* 1.234^{2.3} */
978: /* 1e */
979: double
980: s_scan_number(char *buf, int idx, int *r_idx)
981: {
982: double num;
983: float exp;
984: double result;
985: int ii=0;
986: char num_str[QUARTER_K];
987:
988: num_str[ii]=0;
989:
990: if( buf[idx] == '-' ) {
991: num_str[ii++] = '-';
992: idx++;
993: }
994: while( isdigit(buf[idx]) || buf[idx] == '.' ) {
995: num_str[ii++] = buf[idx];
996: idx++;
997: }
998: if( buf[idx] == 'E' || buf[idx] == 'e' ) {
999: if( buf[idx+1] == '-' || isdigit(buf[idx+1]) ) {
1000: num_str[ii++] = buf[idx++];
1001: num_str[ii++] = buf[idx++];
1002: while( isdigit(buf[idx]) ) {
1003: num_str[ii++] = buf[idx];
1004: idx++;
1005: }
1006: }
1007: }
1008: num_str[ii] = 0; /* terminate the str */
1009: sscanf(num_str,"%lg", &num);
1010: /* printf("Scan number %s got %g\n",num_str, num); fflush(stdout); */
1011: result = num;
1012: if( buf[idx] == '^' ) {
1013: idx++;
1014: while( isspace(buf[idx]) ) { idx++; }
1015: if( buf[idx] == '{' ) { /* need to scan for a matching right bracket */
1016: idx++;
1017: }
1018: while( isspace(buf[idx]) ) { idx++; }
1019: num_str[0]=0;
1020: if( isdigit(buf[idx]) || buf[idx] == '+' || buf[idx] == '-' ) {
1021: ii=0;
1022: while( isdigit(buf[idx]) || buf[idx] == '.' || buf[idx] == '+' || buf[idx] == '-' ) {
1023: num_str[ii++] = buf[idx];
1024: idx++;
1025: }
1026: num_str[ii]=0;
1027: }
1028: while( isspace(buf[idx]) ) { idx++; }
1029: if( buf[idx] == '}' ) {
1030: idx++;
1031: }
1032: sscanf(num_str,"%f", &exp);
1033: /* printf("Scan exp number %s got %g\n",num_str, exp); fflush(stdout); */
1034:
1035: result = pow(num, (double)exp);
1036: /* printf("{%d^%d}=%g\n",num, exp,result); */
1037: }
1038: *r_idx = idx;
1039: return (result);
1040: }
1041:
1042:
1043: double
1044: s_scan_symbol(char *buf,char *symb_p,int idx, int *r_idx)
1045: {
1046: char num_str[QUARTER_K];
1047: int ii=0;
1048: double r_exp=1.0;
1049:
1050: symb_p[0]=0;
1051: while( isalnum(buf[idx]) || buf[idx] == '_' ) {
1052: symb_p[ii++] = buf[idx];
1053: idx++;
1054: }
1055: symb_p[ii]=0;
1056:
1057: if( buf[idx] == '^' ) { /* look for either left bracket or a number */
1058: idx++;
1059: while( isspace(buf[idx]) ) { idx++; }
1060: if( buf[idx] == '{' ) { /* need to scan for a matching right bracket */
1061: idx++;
1062: }
1063: while( isspace(buf[idx]) ) { idx++; }
1064: if( isdigit(buf[idx]) || buf[idx] == '.' || buf[idx] == '+' || buf[idx] == '-' ) {
1065: ii=0; num_str[ii] = 0;
1066: while( isdigit(buf[idx]) || buf[idx] == '.' || buf[idx] == '+' || buf[idx] == '-' ) {
1067: num_str[ii++] = buf[idx];
1068: idx++;
1069: }
1070: num_str[ii]=0;
1071: }
1072: while( isspace(buf[idx]) ) { idx++; }
1073: if( buf[idx] == '}' ) {
1074: idx++;
1075: }
1076: sscanf(num_str,"%lg", &r_exp); /* power could be of type float */
1077: /* printf("[scan symb with power %s ^ %lg] ",symb_p, r_exp); fflush(stdout); */
1078: }
1079: *r_idx = idx;
1080: return (r_exp);
1081: }
1082:
1083: /* return: err_code 0 parsed ok */
1084: /* 1 symbol is of length 1, not found in the tree */
1085: /* 2 symbol not found in the tree */
1086: /* 3 symbol parsed as prefix symb, but symb not found */
1087: /* 4 symbol length is 0 or negative */
1088: int
1089: s_process_symb(char *symb_str,Unit_t *cu_p,double exp)
1090: {
1091: int len;
1092: Unit_t *au_p;
1093: int c_result;
1094: int ii;
1095: char tmp_str[ANSWER_STRING_LENG];
1096: int err_code = 0;
1097: double d_exp;
1098:
1099: len = strlen(symb_str);
1100: if( len > 0 ) {
1101: au_p = u_find_symb(symb_str, UnitTree_p, &c_result);
1102: if( c_result == 1 ) { /* if found, copy the definition over */
1103: u_copy_unit(cu_p, au_p, exp);
1104: } else {
1105: if( len > 1 ) {
1106: if( PrefixTbl[ (int)symb_str[0] ] != 0 ) { /* prefix is defined */
1107: for(ii=1;ii<len;ii++) {
1108: tmp_str[ii-1] = symb_str[ii];
1109: }
1110: tmp_str[len-1]=0;
1111: au_p = u_find_symb(tmp_str, UnitTree_p, &c_result);
1112: if( c_result == 1 ) {
1113: /* printf("[%s] ", tmp_str); */
1114: u_copy_unit(cu_p, au_p, exp);
1115: d_exp = (double)PrefixTbl[ (int)symb_str[0] ] * exp;
1116: cu_p->u_scale = cu_p->u_scale * pow((double)10.0,d_exp);
1117: } else { /* unit *tmp_str not found */
1118: /*printf("The unit: %s, not defined\n",tmp_str);*/
1119: err_code = 3;
1120: }
1121: } else {
1122: /*printf("<<%s>>", symb_str);*/
1123: err_code = 2;
1124: }
1125: } else {/* len == 1 */
1126: /*printf("The unit: %s, not defined\n",symb_str);*/
1127: err_code = 1;
1128: }
1129: }
1130: } else {
1131: err_code = 4;
1132: }
1133: return (err_code);
1134: }
1135:
1136: Unit_t *
1137: u_parse_unit(char *unit_str)
1138: {
1139: char *ch;
1140: char symb_str[QUARTER_K];
1141: int idx;
1142: double exp_sign;
1143: int s_result;
1144: int not_done;
1145: double s_number, offset;
1146: double tmp_scale, symb_exp, exp;
1147: Unit_t *cu_p;
1148:
1149: gUnitError=0;
1150: ch = unit_str;
1151: cu_p = (Unit_t *) capa_malloc(1, sizeof(Unit_t)); /* *** */
1152: cu_p->u_scale = 1.0;
1153: idx = 0; not_done = 1;
1154: exp_sign = 1.0; exp = 1;
1155: symb_str[0] = 0;
1156:
1157: while( isspace(*ch) ) { ch++; } /* trim leading white spaces */
1158: /* fprintf(stdout,"PARSE |%s|\n", unit_str); */
1159: while( not_done ) {
1160: if( isdigit(ch[idx]) || ch[idx] == '-' ) { /* rule 1: number */
1161: s_number = s_scan_number(ch,idx,&idx);
1162:
1163: tmp_scale = pow(s_number,exp_sign);
1164: /* printf("S=%g,Power(%g,%d)=%g\n",
1165: cu_p->u_scale, s_number,exp_sign, tmp_scale);
1166: */
1167: cu_p->u_scale = cu_p->u_scale * tmp_scale;
1168:
1169: /* printf("[Scale %g=%g^%g] ",tmp_scale,s_number,exp_sign); */
1170: while( isspace(ch[idx]) ) { idx++; }
1171: } else {
1172: if( isalpha(ch[idx]) ) { /* rule 2: unit_symbol ^ exp */
1173: symb_str[0] = 0;
1174: symb_exp = s_scan_symbol(ch,symb_str,idx,&idx);
1175: exp = (double)exp_sign * symb_exp;
1176: /* printf("[scanned %s ^ (%g * %g)] ", symb_str,symb_exp,exp_sign); fflush(stdout); */
1177: s_result = s_process_symb(symb_str,cu_p,exp);
1178: if( s_result > 0 ) {
1179: /* printf("Error processing symbol [%s]\n", symb_str); */
1180: gUnitError = 1;
1181: }
1182: while( isspace(ch[idx]) ) { idx++; }
1183: } else {
1184: if( ch[idx] == '*' || ch[idx] == '/' ) {
1185: if( ch[idx] == '/' ) { /* printf("[/] "); */ exp_sign = -1.0; }
1186: idx++;
1187: while( isspace(ch[idx]) ) { idx++; }
1188: } else {
1189: if( ch[idx] == '+' || ch[idx] == '-' ) {
1190: idx++;
1191: while( isspace(ch[idx]) ) { idx++; }
1192: offset = s_scan_number(ch,idx,&idx);
1193: /* printf("[Offset %g] ",offset); */
1194: } else {
1195: if( ch[idx] == 0 ) { /* end of input string */
1196: not_done = 0;
1197: /* printf("\n"); */
1198: } else {
1199: /* garbage in unit string */
1200: gUnitError = 1;
1201: not_done=0;
1202: }
1203: }
1204: }
1205: }
1206: }
1207: }
1208: simplify_unit(cu_p);
1209: return (cu_p);
1210:
1211: }
1212:
1213: void
1214: u_getunit(FILE *f)
1215: {
1216: register int unit_type;
1217: register int c;
1218: int power, result;
1219: char *name_p, *symbol_p, *comment_p, *unit_p;
1220:
1221: BaseUnitcnt = 0;
1222: free_utree(UnitTree_p);
1223: UnitTree_p = NULL;
1224: c_moveto_unit(f); /* move the file position to << */
1225: do {
1226: c_ignorewhite(f);
1227: c = getc(f); ungetc(c,f);
1228: if( c == '<' ) {
1229: unit_type = c_gettype(f);
1230: }
1231: if( c != EOF ) {
1232: switch(unit_type) {
1233: case U_BASE:
1234: name_p = c_getword(f); symbol_p = c_getword(f);
1235: comment_p = c_getcomment(f);
1236: /*
1237: printf("B Unit: N=%s,S=%s,C=%s\n",name_p,symbol_p,comment_p);
1238: */
1239: result = u_insert_baseunit(name_p,symbol_p,comment_p);
1240: if( result == 1 ) {
1241: printf("The entry %s is duplicated\n",symbol_p);
1242: }
1243: free(name_p); free(symbol_p); free(comment_p);
1244: break;
1245: case U_DERIVED:
1246: name_p = c_getword(f); symbol_p = c_getword(f);
1247: unit_p = c_getstring(f); comment_p = c_getcomment(f);
1248: /*
1249: printf("D Unit: N=%s,S=%s,C=%s,U=%s\n",
1250: name_p,symbol_p,comment_p,unit_p);
1251: */
1252: result = u_insert_derived(name_p,symbol_p,comment_p,unit_p);
1253: if( result == 1 ) {
1254: printf("The entry %s is duplicated\n",symbol_p);
1255: }
1256: /* preorder_utree(UnitTree_p); */
1257: free(name_p); free(symbol_p); free(comment_p); free(unit_p);
1258: break;
1259: case U_PREFIX:
1260: name_p = c_getword(f); symbol_p = c_getword(f);
1261: unit_p = c_getstring(f);
1262: /*
1263: printf("Prefix: N=%s,S=%s,U=%s\n",
1264: name_p,symbol_p,unit_p);
1265: */
1266: power = u_parsepower(unit_p);
1267: PrefixTbl[ (int)(*symbol_p) ] = power;
1268: /* printf(" P[%c]=%d\n",*symbol_p,power); */
1269: free(name_p); free(symbol_p); free(unit_p);
1270: break;
1271: case U_CONSTANT:
1272: symbol_p = c_getword(f); unit_p = c_getstring(f);
1273: comment_p = c_getcomment(f);
1274: /*
1275: printf("Const.: S=%s,C=%s,U=%s\n",
1276: symbol_p,comment_p,unit_p);
1277: */
1278: break;
1279: case U_UNKNOWN:
1280: /* printf("Unknown\n"); */
1281: break;
1282: }
1283: }
1284: } while ( c != EOF );
1285:
1286: }
1287:
1288: /* ----------------------------------------------------------------- */
1289: /* comparing unit symbol names should be case sensitive */
1290: int
1291: comp_unit_symb(a, b) char *a; char *b;
1292: {
1293: return strncmp(a,b,SYMBOL_MAXLEN);
1294: }
1295:
1296:
1297: Unit_t *
1298: u_splay (char *name, Unit_t *t)
1299: {
1300: Unit_t N;
1301: Unit_t *l, *r, *y;
1302:
1303: if (t == NULL) return t;
1304: N.u_left = (Unit_t *)NULL;
1305: N.u_right = (Unit_t *)NULL;
1306: l = r = &N;
1307:
1308: for (;;) {
1309: if ( comp_unit_symb(name,t->u_symbol) < 0 ) {
1310: if (t->u_left == NULL) break;
1311: if ( comp_unit_symb(name, (t->u_left)->u_symbol ) < 0 ) {
1312: y = t->u_left; t->u_left = y->u_right; y->u_right = t; t = y;
1313: if (t->u_left == NULL) break;
1314: }
1315: r->u_left = t; r = t; t = t->u_left;
1316: } else if ( comp_unit_symb(name,t->u_symbol) > 0 ) {
1317: if (t->u_right == NULL) break;
1318: if ( comp_unit_symb(name, (t->u_right)->u_symbol ) > 0 ) {
1319: y = t->u_right; t->u_right = y->u_left; y->u_left = t; t = y;
1320: if (t->u_right == NULL) break;
1321: }
1322: l->u_right = t; l = t; t = t->u_right;
1323: } else {
1324: break;
1325: }
1326: }
1327: l->u_right = t->u_left; r->u_left = t->u_right; t->u_left = N.u_right;
1328: t->u_right = N.u_left;
1329: return t;
1330: }
1331:
1332:
1333:
1334: /* returns: 0 correctly inserted */
1335: /* -1 error */
1336: /* 1 duplicate entry */
1337:
1338: int
1339: u_insert_baseunit(n_p,s_p,c_p) char *n_p, *s_p, *c_p;
1340: {
1341: Unit_t *new_p, *t;
1342: int len;
1343:
1344: new_p = (Unit_t *) capa_malloc(1, sizeof(Unit_t)); /* *** */
1345: if (new_p == NULL) {
1346: printf("Ran out of space\n");
1347: return(-1);
1348: }
1349: strcpy(new_p->u_symbol, s_p);
1350: strcpy(new_p->u_name, n_p);
1351: len = strlen(c_p);
1352: new_p->u_comment = (char *) capa_malloc((len+1), sizeof(char)); /* *** */
1353: strcpy(new_p->u_comment,c_p);
1354: BaseUnitcnt++;
1355: new_p->u_index = BaseUnitcnt;
1356: new_p->u_type = U_BASE;
1357: new_p->u_scale = 1.0;
1358: new_p->u_offset = 0.0;
1359: new_p->u_count = 0;
1360: new_p->u_list = NULL;
1361:
1362: if (UnitTree_p == NULL) { /* a new unit tree */
1363: UnitTree_p = new_p;
1364: return (0);
1365: }
1366: t = u_splay(s_p, UnitTree_p);
1367: if ( comp_unit_symb(s_p,t->u_symbol) < 0 ) {
1368: new_p->u_left = t->u_left; new_p->u_right = t;
1369: t->u_left = NULL;
1370: /* Splay_cnt++; */
1371: UnitTree_p = new_p;
1372: return (0);
1373: } else if ( comp_unit_symb(s_p,t->u_symbol) > 0 ) {
1374: new_p->u_right = t->u_right; new_p->u_left = t;
1375: t->u_right = NULL;
1376: /* Splay_cnt++; */
1377: UnitTree_p = new_p;
1378: return (0);
1379: } else { /* name and t->u_symbol is the same, which means found it */
1380: capa_mfree( (char *)new_p );
1381: UnitTree_p = t;
1382: return (1);
1383: }
1384: }
1385:
1386:
1387: int
1388: u_insert_derived(n_p,s_p,c_p,u_p)char *n_p, *s_p, *c_p, *u_p;
1389: {
1390: Unit_t *new_p, *t;
1391: int c_result, len;
1392:
1393: /* inorder_utree(UnitTree_p); */
1394: t = u_splay(s_p, UnitTree_p);
1395: UnitTree_p = t;
1396: c_result = comp_unit_symb(s_p,t->u_symbol);
1397: if ( c_result == 0 ) {
1398: UnitTree_p = t;
1399: return (1);
1400: }
1401:
1402: /* prepare a new Unit_t */
1403: new_p = u_parse_unit(u_p);
1404: strcpy(new_p->u_symbol,s_p);
1405: strcpy(new_p->u_name, n_p);
1406: new_p->u_type = U_DERIVED;
1407: len = strlen(c_p);
1408: new_p->u_comment = (char *) capa_malloc((len+1), sizeof(char)); /* *** */
1409: strcpy(new_p->u_comment,c_p);
1410:
1411: simplify_unit(new_p);
1.10 ! albertel 1412: #ifdef UNIT_DBUG
1.2 albertel 1413: printf("Derived Unit:%s\n",new_p->u_name);
1414: print_unit_t(new_p);
1.10 ! albertel 1415: #endif
1.1 albertel 1416: if (c_result < 0 ) {
1417: new_p->u_left = t->u_left; new_p->u_right = t;
1418: t->u_left = NULL;
1419: } else { /* c_result > 0 */
1420: new_p->u_right = t->u_right; new_p->u_left = t;
1421: t->u_right = NULL;
1422: }
1423: UnitTree_p = new_p;
1424:
1425: return (0);
1426:
1427: }
1428:
1429: void
1430: freelist_unit_e(Unit_E *ue_p)
1431: {
1432: Unit_E *curr_p, *next_p;
1433:
1434: if( ue_p != NULL ) {
1435: next_p = ue_p->ue_nextp;
1436: curr_p = ue_p;
1437: if( next_p == NULL ) {
1438: capa_mfree((char *)curr_p);
1439: } else {
1440: for( curr_p = ue_p; next_p; curr_p = next_p, next_p = next_p->ue_nextp) {
1441: capa_mfree((char *)curr_p);
1442: }
1443: capa_mfree((char *)curr_p);
1444: }
1445: }
1446: }
1447: void
1448: simplify_unit(u_p) Unit_t *u_p;
1449: {
1450: Unit_E *eu_p, *prev_p;
1451: int ii, idx;
1452:
1453: /* walk through u_list and replace those u_index = -1 with */
1454: /* a linked list of basic unit. */
1455: /* u_msort_main() the whole u_list */
1456: /* combine those units with same u_index */
1457: for(ii=0;ii<BaseUnitcnt;ii++) {
1458: CScale[ii] = 0.0;
1459: CExp[ii] = 0.0;
1460: }
1.2 albertel 1461: /*
1462: printf("Before Simplify:: \n");
1463: print_unit_t(u_p);
1464: */
1.1 albertel 1465: if( u_p->u_count > 0 ) {
1466:
1467: for(eu_p=u_p->u_list; eu_p; eu_p = eu_p->ue_nextp) {
1468: idx = eu_p->ue_index;
1469: if( CScale[idx] == 0.0 ) {
1470: CScale[idx] = 1.0;
1471: strcpy(CSymb[idx],eu_p->ue_symbol);
1472: }
1473: CScale[idx] = CScale[idx] * eu_p->ue_scale;
1474: CExp[idx] = CExp[idx] + eu_p->ue_exp;
1475: }
1.2 albertel 1476: /* debugging
1.1 albertel 1477: for(ii=0;ii<BaseUnitcnt;ii++) {
1478: if( CScale[ii] != 0.0 ) {
1479: printf("(%d)%s,S=%g,E=%g\n",ii,CSymb[ii],CScale[ii], CExp[ii]);
1480: }
1.2 albertel 1481: if( CExp[ii] == 0.0 ) {
1482: printf("(%d)%s,S=%g,Exp=%g\n",ii,CSymb[ii],CScale[ii], CExp[ii]);
1483: }
1.1 albertel 1484: }
1485: */
1486: freelist_unit_e(u_p->u_list);
1487: prev_p = u_p->u_list = NULL;
1488: u_p->u_count = 0;
1489: for(ii=0;ii<BaseUnitcnt;ii++) {
1490: if( CScale[ii] != 0.0 && CExp[ii] != 0) {
1491: eu_p = (Unit_E *)capa_malloc(1,sizeof(Unit_E)); /* ***************** */
1492: eu_p->ue_scale = 1.0;
1493: eu_p->ue_exp = CExp[ii];
1494: eu_p->ue_index = ii;
1495: strcpy(eu_p->ue_symbol,CSymb[ii]);
1496: if( prev_p == NULL) {
1497: u_p->u_list = prev_p = eu_p;
1498: } else {
1499: prev_p->ue_nextp = eu_p;
1500: prev_p = eu_p;
1501: }
1502: u_p->u_count++;
1503: }
1504: }
1505: }
1.2 albertel 1506: /*
1507: printf("After Simplify:: \n");
1508: print_unit_t(u_p);
1509: */
1.1 albertel 1510: }
1511:
1512: /* before comparing two units, make sure they are of basic form */
1513: /* compares if two units are equal */
1514: /* equality returns 1 */
1515:
1516: int is_units_equal(Unit_t *u1_p, Unit_t *u2_p)
1517: {
1518: int result=1;
1519: Unit_E *a_p, *b_p;
1520:
1521: if( (u1_p->u_count == u2_p->u_count) &&
1522: (u1_p->u_scale == u2_p->u_scale) ) {
1523: for(a_p=u1_p->u_list, b_p=u2_p->u_list;
1524: a_p; a_p=a_p->ue_nextp, b_p=b_p->ue_nextp) {
1525: if(a_p->ue_index != b_p->ue_index ||
1526: a_p->ue_scale != b_p->ue_scale ||
1527: a_p->ue_exp != b_p->ue_exp ) {
1528: result=0;
1529: break;
1530: }
1531: }
1532: } else {
1533: result=0;
1534: }
1535: return (result);
1536: }
1537: /* input : both are the simplest units */
1538: /* result: 0.0 means they are not of euquvalent units */
1539: /* the ratio of u1 / u2 */
1540: double units_ratio(Unit_t *u1_p, Unit_t *u2_p)
1541: {
1542: double ratio=1.0;
1543: Unit_E *a_p, *b_p;
1544:
1545: if( (u1_p->u_count == u2_p->u_count) ) {
1546: for(a_p=u1_p->u_list, b_p=u2_p->u_list;
1547: a_p; a_p=a_p->ue_nextp, b_p=b_p->ue_nextp) {
1548: if(a_p->ue_index != b_p->ue_index ||
1549: a_p->ue_scale != b_p->ue_scale ||
1550: a_p->ue_exp != b_p->ue_exp ) {
1551: ratio=0.0;
1552: break;
1553: }
1554: }
1555: } else {
1556: ratio=0.0;
1557: }
1558: if( (ratio != 0.0) && (u2_p->u_scale != 0.0 ) ) {
1559: ratio = u1_p->u_scale / u2_p->u_scale;
1560: }
1561: return (ratio);
1562: }
1563:
1564: /* ------------- The Grammar of Units Parser --------------------
1565:
1566: scan_unit_expr() --> scan_basic_block()
1567: --> scan_basic_block() '+' scan_basic_block()
1568: --> scan_basic_block() '-' scan_basic_block()
1569:
1570: scan_num_expr() --> scan_num_block()
1571: --> scan_num_block() '+' scan_num_block()
1572: --> scan_num_block() '-' scan_num_block()
1573:
1574: scan_basic_block()--> scan_basic_term()
1575: --> scan_basic_term() '*' scan_basic_term()
1576: --> scan_basic_term() ' ' scan_basic_term()
1577: --> scan_basic_term() '/' scan_basic_term()
1578:
1579: scan_num_block() --> scan_num_term()
1580: --> scan_num_term() '*' scan_num_term()
1581: --> scan_num_term() ' ' scan_num_term()
1582: --> scan_num_term() '/' scan_num_term()
1583:
1584:
1585: scan_basic_term() --> scan_unit_item()
1586: --> scan_num_item()
1587: --> '(' scan_basic_block() ')'
1588: --> '{' scan_basic_block() '}'
1589:
1590: scan_num_term() --> scan_num_item()<sp>*
1591: --> '-' scan_num_item()<sp>*
1592: --> '(' scan_num_expr() ')'
1593: --> '{' scan_num_expr() '}'
1594:
1595: scan_unit_item() --> UNIT<sp>*
1596: --> UNIT<sp>* '^' <sp>* scan_num_term()
1597:
1598: scan_num_item() --> FLOAT<sp>*
1599: --> FLOAT<sp>* '^' <sp>* scan_num_term()
1600:
1601: scan_FLOAT() --> [0-9]+([eE][+-]?[0-9]+)*
1602:
1603: p_new_unit() --> [a-Z]+[a-Z0-9_]*
1604:
1605: -----------------------------------------
1606: U.expr := B.block
1607: | B.block '+' B.block
1608: | B.block '-' B.block
1609:
1610: N.expr := N.block
1611: | N.block '+' N.block
1612: | N.block '-' N.block
1613:
1614: To allow for operations like (J/N)^2 or {N/m}^2 (N/J)^3
1615:
1616:
1617: B.block := B.term
1618: | B.term ' ' B.term
1619: | B.term '*' B.term
1620: | B.term '/' B.term
1621:
1622: N.block := N.term
1623: | N.term ' ' N.term
1624: | N.term '*' N.term
1625: | N.term '/' N.term
1626:
1627: B.term := U.item
1628: | N.item
1629: | '(' B.block ')'
1630: | '{' B.block '}'
1631:
1632: | '(' B.block ')' ^ N.term
1633: | '{' B.block '}' ^ N.term
1634:
1635: N.term := N.item
1636: | '-' N.item
1637: | '(' N.expr ')'
1638: | '{' N.expr '}'
1639:
1640: U.item := UNIT
1641: | UNIT '^' N.term
1642:
1643: N.item := FLOAT
1644: | FLOAT '^' N.term
1645:
1646: UNIT := [a-Z]+[a-Z0-9_]*
1647:
1648: FLOAT := [0-9]+([eE][+-]?[0-9]+)*
1649:
1650: ------------------------------------------------------------------- */
1651:
1652: Unit_t *
1653: p_new_op(Unit_t *left_p, int op, Unit_t *right_p)
1654: {
1655: Unit_t *new_p;
1656:
1657: new_p = (Unit_t *) capa_malloc(1, sizeof(Unit_t));
1658: if (new_p == NULL) {
1659: printf("Ran out of space\n");
1660: return(NULL);
1661: }
1662: new_p->u_left = left_p;
1663: new_p->u_right = right_p;
1664: new_p->u_scale = 0.0;
1665: new_p->u_type = op;
1666: new_p->u_offset = 0.0;
1667: new_p->u_count = 0;
1668: new_p->u_list = NULL;
1669:
1670: return (new_p);
1671: }
1672:
1673: Unit_t *
1674: p_new_num(Unit_t *left_p, double num, Unit_t *right_p)
1675: {
1676: Unit_t *new_p;
1677:
1678: new_p = (Unit_t *) capa_malloc(1, sizeof(Unit_t));
1679: if (new_p == NULL) {
1680: printf("Ran out of space\n");
1681: return(NULL);
1682: }
1683:
1684: new_p->u_left = left_p;
1685: new_p->u_right = right_p;
1686: new_p->u_scale = num;
1687: new_p->u_type = U_CONSTANT;
1688: new_p->u_offset = 0.0;
1689: new_p->u_count = 0;
1690: new_p->u_list = NULL;
1691:
1692: return (new_p);
1693: }
1694:
1695: Unit_t *
1696: p_new_unit(Unit_t *left_p, Unit_t *right_p)
1697: {
1698: char symb_str[ANSWER_STRING_LENG];
1699: int ii=0;
1700: int len;
1701: Unit_t *au_p, *cu_p;
1702: int c_result;
1703: char tmp_str[ANSWER_STRING_LENG];
1704: int err_code = 0;
1705: double d_exp;
1706:
1707: symb_str[ii]=0;
1708: while( isspace(Sbuf[Sidx]) ) { Sidx++; }
1709: while( isalnum(Sbuf[Sidx]) || Sbuf[Sidx] == '_' ) {
1710: symb_str[ii++] = Sbuf[Sidx];
1711: Sidx++;
1712: }
1713: symb_str[ii]=0;
1714: /* printf("<U %s>", symb_str); */
1715: cu_p = (Unit_t *) capa_malloc(1, sizeof(Unit_t));
1716: strcpy(cu_p->u_symbol,symb_str);
1717: cu_p->u_left = left_p;
1718: cu_p->u_right = right_p;
1719: cu_p->u_scale = 1.0;
1720: cu_p->u_type = U_DERIVED;
1721: cu_p->u_offset = 0.0;
1722: cu_p->u_count = 0;
1723: cu_p->u_list = NULL;
1724:
1725: len = strlen(symb_str);
1726: if( len > 0 ) {
1727: au_p = u_find_symb(symb_str, UnitTree_p, &c_result);
1728: if( c_result == 1 ) { /* if found, copy the definition over */
1729: u_copy_unit(cu_p, au_p, 1);
1730: } else {
1731: if( len > 1 ) {
1732: if( PrefixTbl[ (int)symb_str[0] ] != 0 ) { /* prefix is defined */
1733: for(ii=1;ii<len;ii++) {
1734: tmp_str[ii-1] = symb_str[ii];
1735: }
1736: tmp_str[len-1]=0;
1737: au_p = u_find_symb(tmp_str, UnitTree_p, &c_result);
1738: if( c_result == 1 ) {
1739: /* printf("[%s] ", tmp_str); */
1740: u_copy_unit(cu_p, au_p, 1);
1741: d_exp = (double)PrefixTbl[ (int)symb_str[0] ];
1742: cu_p->u_scale = cu_p->u_scale * pow((double)10.0,d_exp);
1743: } else { /* unit *tmp_str not found */
1744: /* printf(" not found\n"); */
1745: err_code = 3;
1.4 albertel 1746: cu_p->u_type = U_UNKNOWN;
1.1 albertel 1747: }
1.9 albertel 1748: } else { /* symb_str is not in <prefix><units> form */
1.1 albertel 1749: /* printf("<<%s>>", symb_str); */
1750: err_code = 2;
1.4 albertel 1751: cu_p->u_type = U_UNKNOWN;
1.1 albertel 1752: }
1753: } else {/* len == 1 */
1.9 albertel 1754: /* printf(" not found in symbol tree \n"); */
1.1 albertel 1755: err_code = 1;
1.4 albertel 1756: cu_p->u_type = U_UNKNOWN;
1.1 albertel 1757: }
1758: }
1.9 albertel 1759: } else { /* why would we have a length less than zero symb_str ? */
1.1 albertel 1760: err_code = 4;
1761: }
1762:
1763: return (cu_p);
1764: }
1765:
1766: int s_peeknext_op()
1767: {
1768: char *ch;
1769: int sp=0;
1770:
1771: ch = (char *)&Sbuf[Sidx];
1772: while( isspace(*ch) ) { ch++; sp=1; }
1773: if( (*ch == '*') || (*ch == '/') || (*ch == '+') || (*ch == '-') || (*ch == '^')) {
1774: return (*ch);
1775: }
1776: /* what if space is the last thing on the line?*/
1777: if( sp && (*ch != '\0')) return '*';
1778: return (*ch);
1779: }
1780:
1781: int s_getnext_op()
1782: {
1783: char *ch;
1784: int inc = 0, sp=0;
1785:
1786:
1787: /* printf("\n((op"); print_remains(); printf("\n"); */
1788: ch = (char *)&Sbuf[Sidx];
1789: while( isspace(*ch) ) { ch++; inc++; sp=1; }
1790: Sidx = Sidx + inc;
1791: if( (*ch == '*') || (*ch == '/') || (*ch == '+') || (*ch == '-') || (*ch == '^') ) {
1792: Sidx++;
1793: /* print_remains(); printf(" op))"); printf("\n"); */
1794: return (*ch);
1795: }
1796: /* print_remains(); printf(" op))"); printf("\n"); */
1797: /* what if space is the last thing on the line?*/
1798: if( sp && (*ch != '\0')) return '*';
1799: return (*ch);
1800: }
1801:
1802: int
1803: s_getnext()
1804: {
1805: char ch;
1806:
1807: ch = Sbuf[Sidx];
1808: Sidx++;
1809: return (ch);
1810: }
1811:
1812: int
1813: s_peeknext()
1814: {
1815: char ch;
1816:
1817: ch = Sbuf[Sidx];
1818: return (ch);
1819: }
1820:
1821: int
1822: s_peeknextNW() /* peek into the next non-whitespaces character */
1823: {
1824: char *ch;
1825:
1826: ch = (char *)&Sbuf[Sidx];
1827: while( isspace(*ch) ) { ch++; }
1828: return (*ch);
1829: }
1830:
1831: int
1832: s_getnextNW() /* get the next non-whitespaces character */
1833: {
1834: char *ch;
1835:
1836: ch = (char *)&Sbuf[Sidx]; Sidx++;
1837: while( isspace(*ch) ) { ch++; Sidx++; }
1838: return (*ch);
1839: }
1840: /* peek into the next non-whitespaces character
1841: which should be either a multiply or division */
1842: int
1843: s_peekMDWS()
1844: {
1845: char *ch;
1846: int sp=0;
1847:
1848: ch = (char *)&Sbuf[Sidx];
1849: while( isspace(*ch) ) { ch++; sp=1;}
1850: if( (*ch == '*') || (*ch == '/') ) {
1851: return (*ch);
1852: }
1853: if( sp ) return ' ';
1854: ch = (char *)&Sbuf[Sidx];
1855: while( isspace(*ch) ) { ch++; }
1856: return (*ch);
1857: }
1858:
1859: int
1860: s_getnextMDWS()
1861: {
1862: char *ch;
1863: int inc=0, sp=0;
1864:
1865: ch = (char *)&Sbuf[Sidx]; Sidx++;
1866: while( isspace(*ch) ) { ch++; inc++; sp=1; }
1867: Sidx += inc;
1868: if( (*ch == '*') || (*ch == '/') ) {
1869: return (*ch);
1870: }
1871: if( sp ) return ' ';
1872: return (*ch);
1873: }
1874:
1875: double
1876: scan_FLOAT()
1877: {
1878: double num;
1879: int ii=0, len;
1880: char num_str[QUARTER_K];
1881:
1882: num_str[ii]=0;
1883: while( isspace(Sbuf[Sidx]) ) { Sidx++; }
1884: if( Sbuf[Sidx] == '-' ) {
1885: num_str[ii++] = Sbuf[Sidx++];
1886: }
1887: while( isdigit(Sbuf[Sidx]) || Sbuf[Sidx] == '.' ) {
1888: num_str[ii++] = Sbuf[Sidx++];
1889: }
1890: if( Sbuf[Sidx] == 'E' || Sbuf[Sidx] == 'e' ) {
1891: if( Sbuf[Sidx+1] == '-' || isdigit(Sbuf[Sidx+1]) ) {
1892: num_str[ii++] = Sbuf[Sidx++];
1893: num_str[ii++] = Sbuf[Sidx++];
1894: while( isdigit(Sbuf[Sidx]) ) {
1895: num_str[ii++] = Sbuf[Sidx++];
1896: }
1897: }
1898: }
1899: num_str[ii] = 0; /* terminate the str */
1900: len = strlen(num_str);
1901: if(len > 0 ) {
1902: sscanf(num_str,"%lg", &num);
1903: /* printf("<N %s %g>",num_str,num); fflush(stdout); print_remains(); */
1904: } else {
1905: num = 1.0;
1906: }
1907: return (num);
1908: }
1909: /* -----------------------------------------------
1910: N.item := FLOAT
1911: | FLOAT '^' N.term
1912: ----------------------------------------------- */
1913: Unit_t *
1914: scan_num_item()
1915: {
1916: Unit_t *node_p, *exp_p;
1917: double num_const;
1918: char ch;
1919:
1920: num_const = scan_FLOAT();
1921: node_p = p_new_num(NULL, num_const, NULL);
1922: ch = s_peeknext_op();
1923: if( ch == '^' ) {
1924: ch = s_getnext_op();
1925:
1926: exp_p = scan_num_term();
1927: num_const = node_p->u_scale;
1928: if( node_p->u_scale > 0.0 ) {
1929: num_const = pow(node_p->u_scale,exp_p->u_scale);
1930: }
1931: node_p->u_scale = num_const;
1932: capa_mfree((char *)exp_p);
1933: }
1934: return node_p;
1935: }
1936:
1937: /* -----------------------------------------------
1938: U.item := UNIT
1939: | UNIT '^' N.term
1940: ----------------------------------------------- */
1941:
1942: Unit_t *
1943: scan_unit_item()
1944: {
1945: Unit_t *node_p, *exp_p;
1946: char ch;
1947: double num_const;
1948: Unit_E *oe_p;
1949:
1950: node_p = p_new_unit(NULL,NULL);
1951: ch = s_peeknext_op();
1952: if( ch == '^' ) {
1953: ch = s_getnext_op();
1954: exp_p = scan_num_term();
1955: num_const = exp_p->u_scale;
1956: if( node_p->u_count > 0 ) {
1957: oe_p = node_p->u_list;
1958: for(oe_p = node_p->u_list; oe_p; oe_p = oe_p->ue_nextp ) {
1959: oe_p->ue_exp = oe_p->ue_exp * num_const;
1960: }
1961: }
1962: num_const = node_p->u_scale;
1963: if( node_p->u_scale > 0.0 ) {
1964: num_const = pow(node_p->u_scale,exp_p->u_scale);
1965: }
1966: node_p->u_scale = num_const;
1967: capa_mfree((char *)exp_p);
1968: }
1969: return node_p;
1970: }
1971:
1972: void distribute_exp(Unit_t* node_p,Unit_t* exp_p)
1973: {
1974: Unit_E* oe_p;
1975: double num_const;
1976: num_const = exp_p->u_scale; /* should we check if num_const too large or small ? */
1977: if( node_p->u_count > 0 ) {
1978: oe_p = node_p->u_list;
1979: for(oe_p = node_p->u_list; oe_p; oe_p = oe_p->ue_nextp ) {
1980: oe_p->ue_exp = oe_p->ue_exp * num_const;
1981: }
1982: }
1983: num_const = node_p->u_scale;
1984: if( node_p->u_scale > 0.0 ) { /* what if u_scale <= 0.0 ? */
1985: num_const = pow(node_p->u_scale,exp_p->u_scale);
1986: }
1987: node_p->u_scale = num_const;
1988: if (node_p->u_left) distribute_exp(node_p->u_left,exp_p);
1989: if (node_p->u_right) distribute_exp(node_p->u_right,exp_p);
1990: }
1991:
1992: /* ---------------------------------------------------------------
1993: B.term := U.item
1994: | N.item
1995: | '(' B.block ')'
1996: | '{' B.block '}'
1997:
1998: | '(' B.block ')' '^' N.term <== July 6 1998
1999: | '{' B.block '}' '^' N.term
2000:
2001: --------------------------------------------------------------- */
2002: Unit_t *
2003: scan_basic_term()
2004: {
2005: Unit_t *node_p, *exp_p;
2006: int ch, nch;
2007:
2008: ch = s_peeknextNW();
2009: if( ch == '(' || ch == '{' ) {
2010: ch = s_getnextNW(); /* get rid of '(' or '{' */
2011: node_p = scan_basic_block();
2012: nch = s_peeknextNW();
2013: if( nch == ')' || nch == '}' ) { /* should be either ')' or '}' */
2014: if( ((ch == '(' ) && (nch == ')' )) ||
2015: ((ch == '{' ) && (nch == '}' )) ) { /* matching left paren with right paren */
2016:
2017:
2018: } else {
2019: /* printf(" WARN: %c matched by %c\n", ch, nch); */
2020: }
2021: nch = s_getnextNW();
2022: /* ====== Added Jul 6, 1998 ====> */
2023: ch = s_peeknext_op();
2024: if( ch == '^' ) {
2025: ch = s_getnext_op(); /* get rid of '^' char */
2026: exp_p = scan_num_term();
2027: distribute_exp(node_p,exp_p);
2028: capa_mfree((char *)exp_p);
2029: }
2030: /* <== added Jul 6, 1998 == */
2031: } else {
2032: /* printf(" WARN: %c is not matched by %c\n", ch, nch); */
2033: }
2034: } else if( ch >= '0' && ch <= '9' ) {
2035: node_p = scan_num_item();
2036: } else { /* assume a unit symbol */
2037: /* printf("<B.term>"); print_remains(); */
2038: node_p = scan_unit_item();
2039: /* print_remains(); */
2040: }
2041: return node_p;
2042: }
2043: /* --------------------------------------------------
2044: N.term := N.item
2045: | '-' N.item
2046: | '(' N.expr ')'
2047: | '{' N.expr '}'
2048: -------------------------------------------------- */
2049: Unit_t *
2050: scan_num_term()
2051: {
2052: Unit_t *node_p;
2053: char ch, nch;
2054:
2055: ch = s_peeknextNW();
2056: if( ch == '(' || ch == '{' ) {
2057: ch = s_getnextNW();
2058: node_p = scan_num_expr();
2059: nch = s_peeknextNW();
2060: if( nch == ')' || nch == '}' ) { /* should be either ')' or '}' */
2061: if( ((ch == '(' ) && (nch == ')' )) ||
2062: ((ch == '{' ) && (nch == '}' )) ) {
2063:
2064: } else {
2065: /* printf(" WARN: %c matched by %c\n", ch, nch); */
2066: }
2067: nch = s_getnextNW();
2068: } else {
2069: /* printf(" WARN: %c is not matched by %c\n", ch, ch); */
2070: }
2071: } else if( ch == '-' ) {
2072: ch = s_getnextNW();
2073: node_p = scan_num_item();
2074: node_p->u_scale = (-1)*node_p->u_scale;
2075: } else {
2076: if( isdigit(ch) ) {
2077: node_p = scan_num_item();
2078: } else { /* something other than a number */
2079: /*
2080: printf(" ERROR: expect a number: ");
2081: print_remains();
2082: */
2083: node_p = p_new_num(NULL, 0.0, NULL); /* make the unknown item */
2084: }
2085: }
2086: return node_p;
2087: }
2088:
2089: /* --------------------------------------------------
2090: B.block := B.term
2091: | B.term ' ' B.term
2092: | B.term '*' B.term
2093: | B.term '/' B.term
2094: -------------------------------------------------- */
2095: Unit_t *
2096: scan_basic_block()
2097: {
2098: Unit_t *node_p;
2099: char ch;
2100: int op;
2101:
2102: /* printf("<B.block>(before B.term)"); print_remains(); */
2103: node_p = scan_basic_term();
2104: ch = s_peeknext_op();
2105: while ( ch == '*' || ch == '/' ) {
2106: op = ( ch == '/' ? U_OP_DIVIDE : U_OP_TIMES);
2107: ch = s_getnext_op();
2108: /* printf("<B.block>(/ *)"); print_remains(); */
2109: node_p = p_new_op(node_p,op,scan_basic_term());
2110: ch = s_peeknext_op();
2111: }
2112: return node_p;
2113: }
2114: /* --------------------------------------------------
2115: N.block := N.term
2116: | N.term ' ' N.term
2117: | N.term '*' N.term
2118: | N.term '/' N.term
2119: -------------------------------------------------- */
2120: Unit_t *
2121: scan_num_block()
2122: {
2123: Unit_t *node_p, *opand_p;
2124: char ch;
2125: double result;
2126:
2127: node_p = scan_num_term();
2128: ch = s_peeknext_op();
2129: while ( ch == '*' || ch == '/' ) {
2130: s_getnext_op();
2131: opand_p = scan_num_term();
2132: if( ch == '*' ) {
2133: result = node_p->u_scale * opand_p->u_scale;
2134: } else {
2135: result = node_p->u_scale / opand_p->u_scale;
2136: }
2137: node_p->u_scale = result;
2138: capa_mfree((char *)opand_p);
2139: ch = s_peeknext_op();
2140: }
2141: return node_p;
2142: }
2143:
2144: /* ---------------------------------------
2145: U.expr := B.block
2146: | B.block '+' B.block
2147: | B.block '-' B.block
2148: --------------------------------------- */
2149: Unit_t *
2150: scan_unit_expr()
2151: {
2152: Unit_t *node_p;
2153: char ch;
2154: int op;
2155:
2156: /* printf("<U.expr>"); print_remains(); */
2157: node_p = scan_basic_block();
2158: ch = s_peeknext_op();
2159: while ( ch == '+' || ch == '-' ) {
2160: op = ( ch == '+' ? U_OP_PLUS : U_OP_MINUS);
2161: ch = s_getnext_op();
2162: /* printf("<U.expr>(+-)"); print_remains(); */
2163: node_p = p_new_op(node_p,op,scan_basic_block());
2164: ch = s_peeknext_op();
2165: }
2166: return node_p;
2167: }
2168: /* -----------------------------------------
2169: N.expr := N.block
2170: | N.block '+' N.block
2171: | N.block '-' N.block
2172: ----------------------------------------- */
2173: Unit_t *
2174: scan_num_expr()
2175: {
2176: Unit_t *node_p, *opand_p;
2177: char ch;
2178: double result;
2179:
2180: node_p = scan_num_block();
2181: ch = s_peeknext_op();
2182: while ( ch == '+' || ch == '-' ) {
2183: ch = s_getnext_op();
2184: opand_p = scan_num_block();
2185: if( ch == '+' ) {
2186: result = node_p->u_scale + opand_p->u_scale;
2187: } else {
2188: result = node_p->u_scale - opand_p->u_scale;
2189: }
2190: node_p->u_scale = result;
2191: capa_mfree((char *)opand_p);
2192: ch = s_peeknext_op();
2193: }
2194: return node_p;
2195: }
2196:
2197: /* ----------------------------------------------------------------------- */
2198: /* <-- This is the major entry point to parse an units expression ------> */
2199: Unit_t *
2200: parse_unit_expr(char *symb_str)
2201: {
2202: Unit_t *root_p;
2203: int len;
2204:
2205: len = strlen(symb_str);
2206: strcpy(Sbuf,symb_str); /* copy it into the global Sbuf */
2207: Sidx=0;
2208: root_p = scan_unit_expr();
2209: if(Sidx < len-1 ) {
2210: /* printf(" WARN: NOT PARSED:"); print_remains(); */
2211: }
2212: return (root_p);
2213:
2214: }
2215:
2216: void
2217: print_remains()
2218: {
2219: int len, ii;
2220:
2221: len = strlen(Sbuf);
2222: printf("[[");
2223: for(ii=Sidx;ii<len;ii++) {
2224: printf("%c",Sbuf[ii]);
2225: }
2226: printf("]]");
2227:
2228: }
2229:
2230:
2231:
2232: /* =================================================================== */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>