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