File:
[LON-CAPA] /
capa /
capa51 /
pProj /
capaUnit.c
Revision
1.2:
download - view:
text,
annotated -
select for diffs
Wed Feb 9 22:10:24 2000 UTC (24 years, 6 months ago) by
albertel
Branches:
MAIN
CVS tags:
HEAD
--CHANGES BY ISAAC--
- fixed Unit parsing to allow m/m == 1
- added new function init_array to delete all elements from an array
- fixed parser coded so it can be compiled with bison
- started addition of new functions to compute new random
distributions
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:
733: while( isspace(*u_symb) ) u_symb++;
734: ap = parse_unit_expr(u_symb);
735: Ptopidx=0;
736: postwalk_utree(ap);
737: #ifdef UNIT_DBUG
738: fprintf(ufp,"Ptopidx %d\n",Ptopidx);
739: #endif
740: if( Ptopidx == 1 ) {
741: simplify_unit(Pstack[Ptopidx]);
742:
743: if( (Pstack[Ptopidx]->u_count != 0) ||
744: (Pstack[Ptopidx]->u_count == t->u_count) ) { /* has unit */
745: *scale = units_ratio(Pstack[Ptopidx], t);
746: if( *scale == 0.0 ) {
747: result = UNIT_FAIL;
748: }
749: free_utree(ap);
750: } else {
751: result = UNIT_FAIL;
752: }
753: } else { /* invalid unit representation */
754: result = UNIT_FAIL;
755: }
756: #ifdef UNIT_DBUG
757: fclose(ufp);
758: #endif
759: return (result);
760: }
761:
762: /* ============================================================= */
763: int
764: free_units()
765: {
766: free_utree(UnitTree_p);
767: UnitTree_p=NULL;
768: return 0;
769: }
770:
771: int
772: free_utree(Unit_t *t)
773: {
774: int result=1;
775:
776: if( t == NULL ) return (1);
777: u_postfree(t);
778: t=NULL;
779:
780: return (result);
781: }
782:
783:
784: int
785: u_postfree(Unit_t *t)
786: {
787: int result;
788:
789: if( t == NULL ) return (1);
790:
791: result = u_postfree(U_LEFT(t));
792: if( result ) result = u_postfree(U_RIGHT(t));
793: if( result ) {
794: if( t->u_comment ) {
795: capa_mfree((char *)t->u_comment);
796: }
797: freelist_unit_e(t->u_list);
798: capa_mfree((char *)t);
799: }
800: return (result);
801: }
802:
803:
804: void
805: print_unit_t(Unit_t *t)
806: {
807: Unit_E *ue_p;
808:
809: /* printf(" Unit::[%s,%d]= %g * ", t->u_symbol,t->u_count,t->u_scale); */
810: printf(" Unit::[%s] = %g * ", t->u_symbol, t->u_scale);
811: for(ue_p=t->u_list; ue_p ; ue_p = ue_p->ue_nextp) {
812: /*
813: printf("<%s,%d,%g,%g> ",ue_p->ue_symbol,ue_p->ue_index,ue_p->ue_scale,ue_p->ue_exp);
814: */
815: printf("(%g*%s^%g) ",ue_p->ue_scale,ue_p->ue_symbol,ue_p->ue_exp);
816: }
817: printf("\n");
818:
819: }
820: /* ----------------------------------------------------------- */
821: /* copy the Unit_E linked list from b_p->u_list to a_p->u_list */
822: /* create some Unit_E nodes in a_p->u_list if needed and */
823: /* leave b_p->u_list intact */
824: /* a_p->u_scale is multiplied by pow(b_p->u_scale,exp_scale) */
825: /* ----------------------------------------------------------- */
826: void
827: u_copy_unit(Unit_t *a_p, Unit_t *b_p, double exp_scale)
828: {
829: Unit_E *oe_p, *ne_p, *last_p;
830: int ii;
831: double scale;
832:
833: if( a_p->u_count > 0 ) {
834: for(last_p = a_p->u_list; last_p->ue_nextp; last_p = last_p->ue_nextp) { }
835: } else {
836: a_p->u_list = last_p = NULL;
837: }
838: if( b_p->u_count > 0 ) {
839: oe_p = b_p->u_list;
840: for(ii=0;ii<b_p->u_count;ii++) {
841: ne_p = (Unit_E *) capa_malloc(1, sizeof(Unit_E)); /* *** */
842: ne_p->ue_scale = oe_p->ue_scale;
843: ne_p->ue_exp = oe_p->ue_exp * exp_scale;
844: ne_p->ue_index = oe_p->ue_index;
845: strcpy(ne_p->ue_symbol, oe_p->ue_symbol);
846: oe_p = oe_p->ue_nextp;
847: if( last_p == NULL ) {
848: a_p->u_list = ne_p;
849: } else {
850: last_p->ue_nextp = ne_p;
851: }
852: last_p = ne_p;
853: a_p->u_count++;
854: }
855: scale = pow(b_p->u_scale, exp_scale);
856: a_p->u_scale = a_p->u_scale * scale;
857: /* printf("Found scale=%g=%g\n",a_p->u_scale,b_p->u_scale); */
858: } else {
859: if( b_p->u_type == U_BASE ) {
860: /* *b_p is a base unit, so create a one element unit */
861: ne_p = (Unit_E *) capa_malloc(1, sizeof(Unit_E)); /* *** */
862: ne_p->ue_scale = b_p->u_scale;
863: ne_p->ue_exp = exp_scale;
864: ne_p->ue_index = b_p->u_index;
865: strcpy(ne_p->ue_symbol, b_p->u_symbol);
866: if( last_p == NULL ) {
867: a_p->u_list = ne_p;
868: } else {
869: last_p->ue_nextp = ne_p;
870: }
871: last_p = ne_p;
872: a_p->u_count++;
873: } else if( b_p->u_type == U_DERIVED) {
874: /* derived units but without any units elements (scalar) */
875: /* do nothing, ignore this units */
876: } else if( b_p->u_type == U_CONSTANT ) {
877: scale = pow(b_p->u_scale, exp_scale);
878: a_p->u_scale = a_p->u_scale * scale;
879: } else {
880: printf("This node has no u_e list and Type unknown\n");
881: }
882: }
883: }
884: int
885: u_pm_op(Unit_t *a_p, Unit_t *b_p, int op)
886: {
887: int result=0;
888:
889: if( a_p->u_count > 0 || b_p->u_count > 0 ) {
890: printf(" cannot add or sub units at this moment\n");
891: return result;
892: }
893: if( op == U_OP_PLUS ) {
894: a_p->u_scale = a_p->u_scale + b_p->u_scale;
895: } else {
896: a_p->u_scale = a_p->u_scale - b_p->u_scale;
897: }
898: return 1;
899: }
900:
901: int
902: u_parsepower(char *unit_str)
903: {
904: int exp, ii;
905: char *ch_p, exp_str[16];
906:
907: ch_p = unit_str;
908: while( isspace(*ch_p) ) { ch_p++; }
909: ii=0;
910: while( isdigit(*ch_p) ) {
911: ch_p++;
912: }
913: while( isspace(*ch_p) ) { ch_p++; }
914: if( *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: ii=0;
923: while( isdigit(*ch_p) || *ch_p == '-' || *ch_p == '+' ) {
924: exp_str[ii++] = *ch_p;
925: ch_p++;
926: }
927: exp_str[ii]=0;
928: sscanf(exp_str,"%d", &exp);
929: return (exp);
930: }
931:
932: /* ------------------------------------------- */
933: /* scan a number of the form indicated below from the input buffer */
934: /* 1.234^{2.3} */
935: /* 1e */
936: double
937: s_scan_number(char *buf, int idx, int *r_idx)
938: {
939: double num;
940: float exp;
941: double result;
942: int ii=0;
943: char num_str[QUARTER_K];
944:
945: num_str[ii]=0;
946:
947: if( buf[idx] == '-' ) {
948: num_str[ii++] = '-';
949: idx++;
950: }
951: while( isdigit(buf[idx]) || buf[idx] == '.' ) {
952: num_str[ii++] = buf[idx];
953: idx++;
954: }
955: if( buf[idx] == 'E' || buf[idx] == 'e' ) {
956: if( buf[idx+1] == '-' || isdigit(buf[idx+1]) ) {
957: num_str[ii++] = buf[idx++];
958: num_str[ii++] = buf[idx++];
959: while( isdigit(buf[idx]) ) {
960: num_str[ii++] = buf[idx];
961: idx++;
962: }
963: }
964: }
965: num_str[ii] = 0; /* terminate the str */
966: sscanf(num_str,"%lg", &num);
967: /* printf("Scan number %s got %g\n",num_str, num); fflush(stdout); */
968: result = num;
969: if( buf[idx] == '^' ) {
970: idx++;
971: while( isspace(buf[idx]) ) { idx++; }
972: if( buf[idx] == '{' ) { /* need to scan for a matching right bracket */
973: idx++;
974: }
975: while( isspace(buf[idx]) ) { idx++; }
976: num_str[0]=0;
977: if( isdigit(buf[idx]) || buf[idx] == '+' || buf[idx] == '-' ) {
978: ii=0;
979: while( isdigit(buf[idx]) || buf[idx] == '.' || buf[idx] == '+' || buf[idx] == '-' ) {
980: num_str[ii++] = buf[idx];
981: idx++;
982: }
983: num_str[ii]=0;
984: }
985: while( isspace(buf[idx]) ) { idx++; }
986: if( buf[idx] == '}' ) {
987: idx++;
988: }
989: sscanf(num_str,"%f", &exp);
990: /* printf("Scan exp number %s got %g\n",num_str, exp); fflush(stdout); */
991:
992: result = pow(num, (double)exp);
993: /* printf("{%d^%d}=%g\n",num, exp,result); */
994: }
995: *r_idx = idx;
996: return (result);
997: }
998:
999:
1000: double
1001: s_scan_symbol(char *buf,char *symb_p,int idx, int *r_idx)
1002: {
1003: char num_str[QUARTER_K];
1004: int ii=0;
1005: double r_exp=1.0;
1006:
1007: symb_p[0]=0;
1008: while( isalnum(buf[idx]) || buf[idx] == '_' ) {
1009: symb_p[ii++] = buf[idx];
1010: idx++;
1011: }
1012: symb_p[ii]=0;
1013:
1014: if( buf[idx] == '^' ) { /* look for either left bracket or a number */
1015: idx++;
1016: while( isspace(buf[idx]) ) { idx++; }
1017: if( buf[idx] == '{' ) { /* need to scan for a matching right bracket */
1018: idx++;
1019: }
1020: while( isspace(buf[idx]) ) { idx++; }
1021: if( isdigit(buf[idx]) || buf[idx] == '.' || buf[idx] == '+' || buf[idx] == '-' ) {
1022: ii=0; num_str[ii] = 0;
1023: while( isdigit(buf[idx]) || buf[idx] == '.' || buf[idx] == '+' || buf[idx] == '-' ) {
1024: num_str[ii++] = buf[idx];
1025: idx++;
1026: }
1027: num_str[ii]=0;
1028: }
1029: while( isspace(buf[idx]) ) { idx++; }
1030: if( buf[idx] == '}' ) {
1031: idx++;
1032: }
1033: sscanf(num_str,"%lg", &r_exp); /* power could be of type float */
1034: /* printf("[scan symb with power %s ^ %lg] ",symb_p, r_exp); fflush(stdout); */
1035: }
1036: *r_idx = idx;
1037: return (r_exp);
1038: }
1039:
1040: /* return: err_code 0 parsed ok */
1041: /* 1 symbol is of length 1, not found in the tree */
1042: /* 2 symbol not found in the tree */
1043: /* 3 symbol parsed as prefix symb, but symb not found */
1044: /* 4 symbol length is 0 or negative */
1045: int
1046: s_process_symb(char *symb_str,Unit_t *cu_p,double exp)
1047: {
1048: int len;
1049: Unit_t *au_p;
1050: int c_result;
1051: int ii;
1052: char tmp_str[ANSWER_STRING_LENG];
1053: int err_code = 0;
1054: double d_exp;
1055:
1056: len = strlen(symb_str);
1057: if( len > 0 ) {
1058: au_p = u_find_symb(symb_str, UnitTree_p, &c_result);
1059: if( c_result == 1 ) { /* if found, copy the definition over */
1060: u_copy_unit(cu_p, au_p, exp);
1061: } else {
1062: if( len > 1 ) {
1063: if( PrefixTbl[ (int)symb_str[0] ] != 0 ) { /* prefix is defined */
1064: for(ii=1;ii<len;ii++) {
1065: tmp_str[ii-1] = symb_str[ii];
1066: }
1067: tmp_str[len-1]=0;
1068: au_p = u_find_symb(tmp_str, UnitTree_p, &c_result);
1069: if( c_result == 1 ) {
1070: /* printf("[%s] ", tmp_str); */
1071: u_copy_unit(cu_p, au_p, exp);
1072: d_exp = (double)PrefixTbl[ (int)symb_str[0] ] * exp;
1073: cu_p->u_scale = cu_p->u_scale * pow((double)10.0,d_exp);
1074: } else { /* unit *tmp_str not found */
1075: /*printf("The unit: %s, not defined\n",tmp_str);*/
1076: err_code = 3;
1077: }
1078: } else {
1079: /*printf("<<%s>>", symb_str);*/
1080: err_code = 2;
1081: }
1082: } else {/* len == 1 */
1083: /*printf("The unit: %s, not defined\n",symb_str);*/
1084: err_code = 1;
1085: }
1086: }
1087: } else {
1088: err_code = 4;
1089: }
1090: return (err_code);
1091: }
1092:
1093: Unit_t *
1094: u_parse_unit(char *unit_str)
1095: {
1096: char *ch;
1097: char symb_str[QUARTER_K];
1098: int idx;
1099: double exp_sign;
1100: int s_result;
1101: int not_done;
1102: double s_number, offset;
1103: double tmp_scale, symb_exp, exp;
1104: Unit_t *cu_p;
1105:
1106: gUnitError=0;
1107: ch = unit_str;
1108: cu_p = (Unit_t *) capa_malloc(1, sizeof(Unit_t)); /* *** */
1109: cu_p->u_scale = 1.0;
1110: idx = 0; not_done = 1;
1111: exp_sign = 1.0; exp = 1;
1112: symb_str[0] = 0;
1113:
1114: while( isspace(*ch) ) { ch++; } /* trim leading white spaces */
1115: /* fprintf(stdout,"PARSE |%s|\n", unit_str); */
1116: while( not_done ) {
1117: if( isdigit(ch[idx]) || ch[idx] == '-' ) { /* rule 1: number */
1118: s_number = s_scan_number(ch,idx,&idx);
1119:
1120: tmp_scale = pow(s_number,exp_sign);
1121: /* printf("S=%g,Power(%g,%d)=%g\n",
1122: cu_p->u_scale, s_number,exp_sign, tmp_scale);
1123: */
1124: cu_p->u_scale = cu_p->u_scale * tmp_scale;
1125:
1126: /* printf("[Scale %g=%g^%g] ",tmp_scale,s_number,exp_sign); */
1127: while( isspace(ch[idx]) ) { idx++; }
1128: } else {
1129: if( isalpha(ch[idx]) ) { /* rule 2: unit_symbol ^ exp */
1130: symb_str[0] = 0;
1131: symb_exp = s_scan_symbol(ch,symb_str,idx,&idx);
1132: exp = (double)exp_sign * symb_exp;
1133: /* printf("[scanned %s ^ (%g * %g)] ", symb_str,symb_exp,exp_sign); fflush(stdout); */
1134: s_result = s_process_symb(symb_str,cu_p,exp);
1135: if( s_result > 0 ) {
1136: /* printf("Error processing symbol [%s]\n", symb_str); */
1137: gUnitError = 1;
1138: }
1139: while( isspace(ch[idx]) ) { idx++; }
1140: } else {
1141: if( ch[idx] == '*' || ch[idx] == '/' ) {
1142: if( ch[idx] == '/' ) { /* printf("[/] "); */ exp_sign = -1.0; }
1143: idx++;
1144: while( isspace(ch[idx]) ) { idx++; }
1145: } else {
1146: if( ch[idx] == '+' || ch[idx] == '-' ) {
1147: idx++;
1148: while( isspace(ch[idx]) ) { idx++; }
1149: offset = s_scan_number(ch,idx,&idx);
1150: /* printf("[Offset %g] ",offset); */
1151: } else {
1152: if( ch[idx] == 0 ) { /* end of input string */
1153: not_done = 0;
1154: /* printf("\n"); */
1155: } else {
1156: /* garbage in unit string */
1157: gUnitError = 1;
1158: not_done=0;
1159: }
1160: }
1161: }
1162: }
1163: }
1164: }
1165: simplify_unit(cu_p);
1166: return (cu_p);
1167:
1168: }
1169:
1170: void
1171: u_getunit(FILE *f)
1172: {
1173: register int unit_type;
1174: register int c;
1175: int power, result;
1176: char *name_p, *symbol_p, *comment_p, *unit_p;
1177:
1178: BaseUnitcnt = 0;
1179: free_utree(UnitTree_p);
1180: UnitTree_p = NULL;
1181: c_moveto_unit(f); /* move the file position to << */
1182: do {
1183: c_ignorewhite(f);
1184: c = getc(f); ungetc(c,f);
1185: if( c == '<' ) {
1186: unit_type = c_gettype(f);
1187: }
1188: if( c != EOF ) {
1189: switch(unit_type) {
1190: case U_BASE:
1191: name_p = c_getword(f); symbol_p = c_getword(f);
1192: comment_p = c_getcomment(f);
1193: /*
1194: printf("B Unit: N=%s,S=%s,C=%s\n",name_p,symbol_p,comment_p);
1195: */
1196: result = u_insert_baseunit(name_p,symbol_p,comment_p);
1197: if( result == 1 ) {
1198: printf("The entry %s is duplicated\n",symbol_p);
1199: }
1200: free(name_p); free(symbol_p); free(comment_p);
1201: break;
1202: case U_DERIVED:
1203: name_p = c_getword(f); symbol_p = c_getword(f);
1204: unit_p = c_getstring(f); comment_p = c_getcomment(f);
1205: /*
1206: printf("D Unit: N=%s,S=%s,C=%s,U=%s\n",
1207: name_p,symbol_p,comment_p,unit_p);
1208: */
1209: result = u_insert_derived(name_p,symbol_p,comment_p,unit_p);
1210: if( result == 1 ) {
1211: printf("The entry %s is duplicated\n",symbol_p);
1212: }
1213: /* preorder_utree(UnitTree_p); */
1214: free(name_p); free(symbol_p); free(comment_p); free(unit_p);
1215: break;
1216: case U_PREFIX:
1217: name_p = c_getword(f); symbol_p = c_getword(f);
1218: unit_p = c_getstring(f);
1219: /*
1220: printf("Prefix: N=%s,S=%s,U=%s\n",
1221: name_p,symbol_p,unit_p);
1222: */
1223: power = u_parsepower(unit_p);
1224: PrefixTbl[ (int)(*symbol_p) ] = power;
1225: /* printf(" P[%c]=%d\n",*symbol_p,power); */
1226: free(name_p); free(symbol_p); free(unit_p);
1227: break;
1228: case U_CONSTANT:
1229: symbol_p = c_getword(f); unit_p = c_getstring(f);
1230: comment_p = c_getcomment(f);
1231: /*
1232: printf("Const.: S=%s,C=%s,U=%s\n",
1233: symbol_p,comment_p,unit_p);
1234: */
1235: break;
1236: case U_UNKNOWN:
1237: /* printf("Unknown\n"); */
1238: break;
1239: }
1240: }
1241: } while ( c != EOF );
1242:
1243: }
1244:
1245: /* ----------------------------------------------------------------- */
1246: /* comparing unit symbol names should be case sensitive */
1247: int
1248: comp_unit_symb(a, b) char *a; char *b;
1249: {
1250: return strncmp(a,b,SYMBOL_MAXLEN);
1251: }
1252:
1253:
1254: Unit_t *
1255: u_splay (char *name, Unit_t *t)
1256: {
1257: Unit_t N;
1258: Unit_t *l, *r, *y;
1259:
1260: if (t == NULL) return t;
1261: N.u_left = (Unit_t *)NULL;
1262: N.u_right = (Unit_t *)NULL;
1263: l = r = &N;
1264:
1265: for (;;) {
1266: if ( comp_unit_symb(name,t->u_symbol) < 0 ) {
1267: if (t->u_left == NULL) break;
1268: if ( comp_unit_symb(name, (t->u_left)->u_symbol ) < 0 ) {
1269: y = t->u_left; t->u_left = y->u_right; y->u_right = t; t = y;
1270: if (t->u_left == NULL) break;
1271: }
1272: r->u_left = t; r = t; t = t->u_left;
1273: } else if ( comp_unit_symb(name,t->u_symbol) > 0 ) {
1274: if (t->u_right == NULL) break;
1275: if ( comp_unit_symb(name, (t->u_right)->u_symbol ) > 0 ) {
1276: y = t->u_right; t->u_right = y->u_left; y->u_left = t; t = y;
1277: if (t->u_right == NULL) break;
1278: }
1279: l->u_right = t; l = t; t = t->u_right;
1280: } else {
1281: break;
1282: }
1283: }
1284: l->u_right = t->u_left; r->u_left = t->u_right; t->u_left = N.u_right;
1285: t->u_right = N.u_left;
1286: return t;
1287: }
1288:
1289:
1290:
1291: /* returns: 0 correctly inserted */
1292: /* -1 error */
1293: /* 1 duplicate entry */
1294:
1295: int
1296: u_insert_baseunit(n_p,s_p,c_p) char *n_p, *s_p, *c_p;
1297: {
1298: Unit_t *new_p, *t;
1299: int len;
1300:
1301: new_p = (Unit_t *) capa_malloc(1, sizeof(Unit_t)); /* *** */
1302: if (new_p == NULL) {
1303: printf("Ran out of space\n");
1304: return(-1);
1305: }
1306: strcpy(new_p->u_symbol, s_p);
1307: strcpy(new_p->u_name, n_p);
1308: len = strlen(c_p);
1309: new_p->u_comment = (char *) capa_malloc((len+1), sizeof(char)); /* *** */
1310: strcpy(new_p->u_comment,c_p);
1311: BaseUnitcnt++;
1312: new_p->u_index = BaseUnitcnt;
1313: new_p->u_type = U_BASE;
1314: new_p->u_scale = 1.0;
1315: new_p->u_offset = 0.0;
1316: new_p->u_count = 0;
1317: new_p->u_list = NULL;
1318:
1319: if (UnitTree_p == NULL) { /* a new unit tree */
1320: UnitTree_p = new_p;
1321: return (0);
1322: }
1323: t = u_splay(s_p, UnitTree_p);
1324: if ( comp_unit_symb(s_p,t->u_symbol) < 0 ) {
1325: new_p->u_left = t->u_left; new_p->u_right = t;
1326: t->u_left = NULL;
1327: /* Splay_cnt++; */
1328: UnitTree_p = new_p;
1329: return (0);
1330: } else if ( comp_unit_symb(s_p,t->u_symbol) > 0 ) {
1331: new_p->u_right = t->u_right; new_p->u_left = t;
1332: t->u_right = NULL;
1333: /* Splay_cnt++; */
1334: UnitTree_p = new_p;
1335: return (0);
1336: } else { /* name and t->u_symbol is the same, which means found it */
1337: capa_mfree( (char *)new_p );
1338: UnitTree_p = t;
1339: return (1);
1340: }
1341: }
1342:
1343:
1344: int
1345: u_insert_derived(n_p,s_p,c_p,u_p)char *n_p, *s_p, *c_p, *u_p;
1346: {
1347: Unit_t *new_p, *t;
1348: int c_result, len;
1349:
1350: /* inorder_utree(UnitTree_p); */
1351: t = u_splay(s_p, UnitTree_p);
1352: UnitTree_p = t;
1353: c_result = comp_unit_symb(s_p,t->u_symbol);
1354: if ( c_result == 0 ) {
1355: UnitTree_p = t;
1356: return (1);
1357: }
1358:
1359: /* prepare a new Unit_t */
1360: new_p = u_parse_unit(u_p);
1361: strcpy(new_p->u_symbol,s_p);
1362: strcpy(new_p->u_name, n_p);
1363: new_p->u_type = U_DERIVED;
1364: len = strlen(c_p);
1365: new_p->u_comment = (char *) capa_malloc((len+1), sizeof(char)); /* *** */
1366: strcpy(new_p->u_comment,c_p);
1367:
1368: simplify_unit(new_p);
1369: /*
1370: printf("Derived Unit:%s\n",new_p->u_name);
1371: print_unit_t(new_p);
1372: */
1373: if (c_result < 0 ) {
1374: new_p->u_left = t->u_left; new_p->u_right = t;
1375: t->u_left = NULL;
1376: } else { /* c_result > 0 */
1377: new_p->u_right = t->u_right; new_p->u_left = t;
1378: t->u_right = NULL;
1379: }
1380: UnitTree_p = new_p;
1381:
1382: return (0);
1383:
1384: }
1385:
1386: void
1387: freelist_unit_e(Unit_E *ue_p)
1388: {
1389: Unit_E *curr_p, *next_p;
1390:
1391: if( ue_p != NULL ) {
1392: next_p = ue_p->ue_nextp;
1393: curr_p = ue_p;
1394: if( next_p == NULL ) {
1395: capa_mfree((char *)curr_p);
1396: } else {
1397: for( curr_p = ue_p; next_p; curr_p = next_p, next_p = next_p->ue_nextp) {
1398: capa_mfree((char *)curr_p);
1399: }
1400: capa_mfree((char *)curr_p);
1401: }
1402: }
1403: }
1404: void
1405: simplify_unit(u_p) Unit_t *u_p;
1406: {
1407: Unit_E *eu_p, *prev_p;
1408: int ii, idx;
1409:
1410: /* walk through u_list and replace those u_index = -1 with */
1411: /* a linked list of basic unit. */
1412: /* u_msort_main() the whole u_list */
1413: /* combine those units with same u_index */
1414: for(ii=0;ii<BaseUnitcnt;ii++) {
1415: CScale[ii] = 0.0;
1416: CExp[ii] = 0.0;
1417: }
1418: /*
1419: printf("Before Simplify:: \n");
1420: print_unit_t(u_p);
1421: */
1422: if( u_p->u_count > 0 ) {
1423:
1424: for(eu_p=u_p->u_list; eu_p; eu_p = eu_p->ue_nextp) {
1425: idx = eu_p->ue_index;
1426: if( CScale[idx] == 0.0 ) {
1427: CScale[idx] = 1.0;
1428: strcpy(CSymb[idx],eu_p->ue_symbol);
1429: }
1430: CScale[idx] = CScale[idx] * eu_p->ue_scale;
1431: CExp[idx] = CExp[idx] + eu_p->ue_exp;
1432: }
1433: /* debugging
1434: for(ii=0;ii<BaseUnitcnt;ii++) {
1435: if( CScale[ii] != 0.0 ) {
1436: printf("(%d)%s,S=%g,E=%g\n",ii,CSymb[ii],CScale[ii], CExp[ii]);
1437: }
1438: if( CExp[ii] == 0.0 ) {
1439: printf("(%d)%s,S=%g,Exp=%g\n",ii,CSymb[ii],CScale[ii], CExp[ii]);
1440: }
1441: }
1442: */
1443: freelist_unit_e(u_p->u_list);
1444: prev_p = u_p->u_list = NULL;
1445: u_p->u_count = 0;
1446: for(ii=0;ii<BaseUnitcnt;ii++) {
1447: if( CScale[ii] != 0.0 && CExp[ii] != 0) {
1448: eu_p = (Unit_E *)capa_malloc(1,sizeof(Unit_E)); /* ***************** */
1449: eu_p->ue_scale = 1.0;
1450: eu_p->ue_exp = CExp[ii];
1451: eu_p->ue_index = ii;
1452: strcpy(eu_p->ue_symbol,CSymb[ii]);
1453: if( prev_p == NULL) {
1454: u_p->u_list = prev_p = eu_p;
1455: } else {
1456: prev_p->ue_nextp = eu_p;
1457: prev_p = eu_p;
1458: }
1459: u_p->u_count++;
1460: }
1461: }
1462: }
1463: /*
1464: printf("After Simplify:: \n");
1465: print_unit_t(u_p);
1466: */
1467: }
1468:
1469: /* before comparing two units, make sure they are of basic form */
1470: /* compares if two units are equal */
1471: /* equality returns 1 */
1472:
1473: int is_units_equal(Unit_t *u1_p, Unit_t *u2_p)
1474: {
1475: int result=1;
1476: Unit_E *a_p, *b_p;
1477:
1478: if( (u1_p->u_count == u2_p->u_count) &&
1479: (u1_p->u_scale == u2_p->u_scale) ) {
1480: for(a_p=u1_p->u_list, b_p=u2_p->u_list;
1481: a_p; a_p=a_p->ue_nextp, b_p=b_p->ue_nextp) {
1482: if(a_p->ue_index != b_p->ue_index ||
1483: a_p->ue_scale != b_p->ue_scale ||
1484: a_p->ue_exp != b_p->ue_exp ) {
1485: result=0;
1486: break;
1487: }
1488: }
1489: } else {
1490: result=0;
1491: }
1492: return (result);
1493: }
1494: /* input : both are the simplest units */
1495: /* result: 0.0 means they are not of euquvalent units */
1496: /* the ratio of u1 / u2 */
1497: double units_ratio(Unit_t *u1_p, Unit_t *u2_p)
1498: {
1499: double ratio=1.0;
1500: Unit_E *a_p, *b_p;
1501:
1502: if( (u1_p->u_count == u2_p->u_count) ) {
1503: for(a_p=u1_p->u_list, b_p=u2_p->u_list;
1504: a_p; a_p=a_p->ue_nextp, b_p=b_p->ue_nextp) {
1505: if(a_p->ue_index != b_p->ue_index ||
1506: a_p->ue_scale != b_p->ue_scale ||
1507: a_p->ue_exp != b_p->ue_exp ) {
1508: ratio=0.0;
1509: break;
1510: }
1511: }
1512: } else {
1513: ratio=0.0;
1514: }
1515: if( (ratio != 0.0) && (u2_p->u_scale != 0.0 ) ) {
1516: ratio = u1_p->u_scale / u2_p->u_scale;
1517: }
1518: return (ratio);
1519: }
1520:
1521: /* ------------- The Grammar of Units Parser --------------------
1522:
1523: scan_unit_expr() --> scan_basic_block()
1524: --> scan_basic_block() '+' scan_basic_block()
1525: --> scan_basic_block() '-' scan_basic_block()
1526:
1527: scan_num_expr() --> scan_num_block()
1528: --> scan_num_block() '+' scan_num_block()
1529: --> scan_num_block() '-' scan_num_block()
1530:
1531: scan_basic_block()--> scan_basic_term()
1532: --> scan_basic_term() '*' scan_basic_term()
1533: --> scan_basic_term() ' ' scan_basic_term()
1534: --> scan_basic_term() '/' scan_basic_term()
1535:
1536: scan_num_block() --> scan_num_term()
1537: --> scan_num_term() '*' scan_num_term()
1538: --> scan_num_term() ' ' scan_num_term()
1539: --> scan_num_term() '/' scan_num_term()
1540:
1541:
1542: scan_basic_term() --> scan_unit_item()
1543: --> scan_num_item()
1544: --> '(' scan_basic_block() ')'
1545: --> '{' scan_basic_block() '}'
1546:
1547: scan_num_term() --> scan_num_item()<sp>*
1548: --> '-' scan_num_item()<sp>*
1549: --> '(' scan_num_expr() ')'
1550: --> '{' scan_num_expr() '}'
1551:
1552: scan_unit_item() --> UNIT<sp>*
1553: --> UNIT<sp>* '^' <sp>* scan_num_term()
1554:
1555: scan_num_item() --> FLOAT<sp>*
1556: --> FLOAT<sp>* '^' <sp>* scan_num_term()
1557:
1558: scan_FLOAT() --> [0-9]+([eE][+-]?[0-9]+)*
1559:
1560: p_new_unit() --> [a-Z]+[a-Z0-9_]*
1561:
1562: -----------------------------------------
1563: U.expr := B.block
1564: | B.block '+' B.block
1565: | B.block '-' B.block
1566:
1567: N.expr := N.block
1568: | N.block '+' N.block
1569: | N.block '-' N.block
1570:
1571: To allow for operations like (J/N)^2 or {N/m}^2 (N/J)^3
1572:
1573:
1574: B.block := B.term
1575: | B.term ' ' B.term
1576: | B.term '*' B.term
1577: | B.term '/' B.term
1578:
1579: N.block := N.term
1580: | N.term ' ' N.term
1581: | N.term '*' N.term
1582: | N.term '/' N.term
1583:
1584: B.term := U.item
1585: | N.item
1586: | '(' B.block ')'
1587: | '{' B.block '}'
1588:
1589: | '(' B.block ')' ^ N.term
1590: | '{' B.block '}' ^ N.term
1591:
1592: N.term := N.item
1593: | '-' N.item
1594: | '(' N.expr ')'
1595: | '{' N.expr '}'
1596:
1597: U.item := UNIT
1598: | UNIT '^' N.term
1599:
1600: N.item := FLOAT
1601: | FLOAT '^' N.term
1602:
1603: UNIT := [a-Z]+[a-Z0-9_]*
1604:
1605: FLOAT := [0-9]+([eE][+-]?[0-9]+)*
1606:
1607: ------------------------------------------------------------------- */
1608:
1609: Unit_t *
1610: p_new_op(Unit_t *left_p, int op, Unit_t *right_p)
1611: {
1612: Unit_t *new_p;
1613:
1614: new_p = (Unit_t *) capa_malloc(1, sizeof(Unit_t));
1615: if (new_p == NULL) {
1616: printf("Ran out of space\n");
1617: return(NULL);
1618: }
1619: new_p->u_left = left_p;
1620: new_p->u_right = right_p;
1621: new_p->u_scale = 0.0;
1622: new_p->u_type = op;
1623: new_p->u_offset = 0.0;
1624: new_p->u_count = 0;
1625: new_p->u_list = NULL;
1626:
1627: return (new_p);
1628: }
1629:
1630: Unit_t *
1631: p_new_num(Unit_t *left_p, double num, Unit_t *right_p)
1632: {
1633: Unit_t *new_p;
1634:
1635: new_p = (Unit_t *) capa_malloc(1, sizeof(Unit_t));
1636: if (new_p == NULL) {
1637: printf("Ran out of space\n");
1638: return(NULL);
1639: }
1640:
1641: new_p->u_left = left_p;
1642: new_p->u_right = right_p;
1643: new_p->u_scale = num;
1644: new_p->u_type = U_CONSTANT;
1645: new_p->u_offset = 0.0;
1646: new_p->u_count = 0;
1647: new_p->u_list = NULL;
1648:
1649: return (new_p);
1650: }
1651:
1652: Unit_t *
1653: p_new_unit(Unit_t *left_p, Unit_t *right_p)
1654: {
1655: char symb_str[ANSWER_STRING_LENG];
1656: int ii=0;
1657: int len;
1658: Unit_t *au_p, *cu_p;
1659: int c_result;
1660: char tmp_str[ANSWER_STRING_LENG];
1661: int err_code = 0;
1662: double d_exp;
1663:
1664: symb_str[ii]=0;
1665: while( isspace(Sbuf[Sidx]) ) { Sidx++; }
1666: while( isalnum(Sbuf[Sidx]) || Sbuf[Sidx] == '_' ) {
1667: symb_str[ii++] = Sbuf[Sidx];
1668: Sidx++;
1669: }
1670: symb_str[ii]=0;
1671: /* printf("<U %s>", symb_str); */
1672: cu_p = (Unit_t *) capa_malloc(1, sizeof(Unit_t));
1673: strcpy(cu_p->u_symbol,symb_str);
1674: cu_p->u_left = left_p;
1675: cu_p->u_right = right_p;
1676: cu_p->u_scale = 1.0;
1677: cu_p->u_type = U_DERIVED;
1678: cu_p->u_offset = 0.0;
1679: cu_p->u_count = 0;
1680: cu_p->u_list = NULL;
1681:
1682: len = strlen(symb_str);
1683: if( len > 0 ) {
1684: au_p = u_find_symb(symb_str, UnitTree_p, &c_result);
1685: if( c_result == 1 ) { /* if found, copy the definition over */
1686: u_copy_unit(cu_p, au_p, 1);
1687: } else {
1688: if( len > 1 ) {
1689: if( PrefixTbl[ (int)symb_str[0] ] != 0 ) { /* prefix is defined */
1690: for(ii=1;ii<len;ii++) {
1691: tmp_str[ii-1] = symb_str[ii];
1692: }
1693: tmp_str[len-1]=0;
1694: au_p = u_find_symb(tmp_str, UnitTree_p, &c_result);
1695: if( c_result == 1 ) {
1696: /* printf("[%s] ", tmp_str); */
1697: u_copy_unit(cu_p, au_p, 1);
1698: d_exp = (double)PrefixTbl[ (int)symb_str[0] ];
1699: cu_p->u_scale = cu_p->u_scale * pow((double)10.0,d_exp);
1700: } else { /* unit *tmp_str not found */
1701: /* printf(" not found\n"); */
1702: err_code = 3;
1703: }
1704: } else {
1705: /* printf("<<%s>>", symb_str); */
1706: err_code = 2;
1707: }
1708: } else {/* len == 1 */
1709: /* printf(" not found\n"); */
1710: err_code = 1;
1711: }
1712: }
1713: } else {
1714: err_code = 4;
1715: }
1716:
1717: return (cu_p);
1718: }
1719:
1720: int s_peeknext_op()
1721: {
1722: char *ch;
1723: int sp=0;
1724:
1725: ch = (char *)&Sbuf[Sidx];
1726: while( isspace(*ch) ) { ch++; sp=1; }
1727: if( (*ch == '*') || (*ch == '/') || (*ch == '+') || (*ch == '-') || (*ch == '^')) {
1728: return (*ch);
1729: }
1730: /* what if space is the last thing on the line?*/
1731: if( sp && (*ch != '\0')) return '*';
1732: return (*ch);
1733: }
1734:
1735: int s_getnext_op()
1736: {
1737: char *ch;
1738: int inc = 0, sp=0;
1739:
1740:
1741: /* printf("\n((op"); print_remains(); printf("\n"); */
1742: ch = (char *)&Sbuf[Sidx];
1743: while( isspace(*ch) ) { ch++; inc++; sp=1; }
1744: Sidx = Sidx + inc;
1745: if( (*ch == '*') || (*ch == '/') || (*ch == '+') || (*ch == '-') || (*ch == '^') ) {
1746: Sidx++;
1747: /* print_remains(); printf(" op))"); printf("\n"); */
1748: return (*ch);
1749: }
1750: /* print_remains(); printf(" op))"); printf("\n"); */
1751: /* what if space is the last thing on the line?*/
1752: if( sp && (*ch != '\0')) return '*';
1753: return (*ch);
1754: }
1755:
1756: int
1757: s_getnext()
1758: {
1759: char ch;
1760:
1761: ch = Sbuf[Sidx];
1762: Sidx++;
1763: return (ch);
1764: }
1765:
1766: int
1767: s_peeknext()
1768: {
1769: char ch;
1770:
1771: ch = Sbuf[Sidx];
1772: return (ch);
1773: }
1774:
1775: int
1776: s_peeknextNW() /* peek into the next non-whitespaces character */
1777: {
1778: char *ch;
1779:
1780: ch = (char *)&Sbuf[Sidx];
1781: while( isspace(*ch) ) { ch++; }
1782: return (*ch);
1783: }
1784:
1785: int
1786: s_getnextNW() /* get the next non-whitespaces character */
1787: {
1788: char *ch;
1789:
1790: ch = (char *)&Sbuf[Sidx]; Sidx++;
1791: while( isspace(*ch) ) { ch++; Sidx++; }
1792: return (*ch);
1793: }
1794: /* peek into the next non-whitespaces character
1795: which should be either a multiply or division */
1796: int
1797: s_peekMDWS()
1798: {
1799: char *ch;
1800: int sp=0;
1801:
1802: ch = (char *)&Sbuf[Sidx];
1803: while( isspace(*ch) ) { ch++; sp=1;}
1804: if( (*ch == '*') || (*ch == '/') ) {
1805: return (*ch);
1806: }
1807: if( sp ) return ' ';
1808: ch = (char *)&Sbuf[Sidx];
1809: while( isspace(*ch) ) { ch++; }
1810: return (*ch);
1811: }
1812:
1813: int
1814: s_getnextMDWS()
1815: {
1816: char *ch;
1817: int inc=0, sp=0;
1818:
1819: ch = (char *)&Sbuf[Sidx]; Sidx++;
1820: while( isspace(*ch) ) { ch++; inc++; sp=1; }
1821: Sidx += inc;
1822: if( (*ch == '*') || (*ch == '/') ) {
1823: return (*ch);
1824: }
1825: if( sp ) return ' ';
1826: return (*ch);
1827: }
1828:
1829: double
1830: scan_FLOAT()
1831: {
1832: double num;
1833: int ii=0, len;
1834: char num_str[QUARTER_K];
1835:
1836: num_str[ii]=0;
1837: while( isspace(Sbuf[Sidx]) ) { Sidx++; }
1838: if( Sbuf[Sidx] == '-' ) {
1839: num_str[ii++] = Sbuf[Sidx++];
1840: }
1841: while( isdigit(Sbuf[Sidx]) || Sbuf[Sidx] == '.' ) {
1842: num_str[ii++] = Sbuf[Sidx++];
1843: }
1844: if( Sbuf[Sidx] == 'E' || Sbuf[Sidx] == 'e' ) {
1845: if( Sbuf[Sidx+1] == '-' || isdigit(Sbuf[Sidx+1]) ) {
1846: num_str[ii++] = Sbuf[Sidx++];
1847: num_str[ii++] = Sbuf[Sidx++];
1848: while( isdigit(Sbuf[Sidx]) ) {
1849: num_str[ii++] = Sbuf[Sidx++];
1850: }
1851: }
1852: }
1853: num_str[ii] = 0; /* terminate the str */
1854: len = strlen(num_str);
1855: if(len > 0 ) {
1856: sscanf(num_str,"%lg", &num);
1857: /* printf("<N %s %g>",num_str,num); fflush(stdout); print_remains(); */
1858: } else {
1859: num = 1.0;
1860: }
1861: return (num);
1862: }
1863: /* -----------------------------------------------
1864: N.item := FLOAT
1865: | FLOAT '^' N.term
1866: ----------------------------------------------- */
1867: Unit_t *
1868: scan_num_item()
1869: {
1870: Unit_t *node_p, *exp_p;
1871: double num_const;
1872: char ch;
1873:
1874: num_const = scan_FLOAT();
1875: node_p = p_new_num(NULL, num_const, NULL);
1876: ch = s_peeknext_op();
1877: if( ch == '^' ) {
1878: ch = s_getnext_op();
1879:
1880: exp_p = scan_num_term();
1881: num_const = node_p->u_scale;
1882: if( node_p->u_scale > 0.0 ) {
1883: num_const = pow(node_p->u_scale,exp_p->u_scale);
1884: }
1885: node_p->u_scale = num_const;
1886: capa_mfree((char *)exp_p);
1887: }
1888: return node_p;
1889: }
1890:
1891: /* -----------------------------------------------
1892: U.item := UNIT
1893: | UNIT '^' N.term
1894: ----------------------------------------------- */
1895:
1896: Unit_t *
1897: scan_unit_item()
1898: {
1899: Unit_t *node_p, *exp_p;
1900: char ch;
1901: double num_const;
1902: Unit_E *oe_p;
1903:
1904: node_p = p_new_unit(NULL,NULL);
1905: ch = s_peeknext_op();
1906: if( ch == '^' ) {
1907: ch = s_getnext_op();
1908: exp_p = scan_num_term();
1909: num_const = exp_p->u_scale;
1910: if( node_p->u_count > 0 ) {
1911: oe_p = node_p->u_list;
1912: for(oe_p = node_p->u_list; oe_p; oe_p = oe_p->ue_nextp ) {
1913: oe_p->ue_exp = oe_p->ue_exp * num_const;
1914: }
1915: }
1916: num_const = node_p->u_scale;
1917: if( node_p->u_scale > 0.0 ) {
1918: num_const = pow(node_p->u_scale,exp_p->u_scale);
1919: }
1920: node_p->u_scale = num_const;
1921: capa_mfree((char *)exp_p);
1922: }
1923: return node_p;
1924: }
1925:
1926: void distribute_exp(Unit_t* node_p,Unit_t* exp_p)
1927: {
1928: Unit_E* oe_p;
1929: double num_const;
1930: num_const = exp_p->u_scale; /* should we check if num_const too large or small ? */
1931: if( node_p->u_count > 0 ) {
1932: oe_p = node_p->u_list;
1933: for(oe_p = node_p->u_list; oe_p; oe_p = oe_p->ue_nextp ) {
1934: oe_p->ue_exp = oe_p->ue_exp * num_const;
1935: }
1936: }
1937: num_const = node_p->u_scale;
1938: if( node_p->u_scale > 0.0 ) { /* what if u_scale <= 0.0 ? */
1939: num_const = pow(node_p->u_scale,exp_p->u_scale);
1940: }
1941: node_p->u_scale = num_const;
1942: if (node_p->u_left) distribute_exp(node_p->u_left,exp_p);
1943: if (node_p->u_right) distribute_exp(node_p->u_right,exp_p);
1944: }
1945:
1946: /* ---------------------------------------------------------------
1947: B.term := U.item
1948: | N.item
1949: | '(' B.block ')'
1950: | '{' B.block '}'
1951:
1952: | '(' B.block ')' '^' N.term <== July 6 1998
1953: | '{' B.block '}' '^' N.term
1954:
1955: --------------------------------------------------------------- */
1956: Unit_t *
1957: scan_basic_term()
1958: {
1959: Unit_t *node_p, *exp_p;
1960: int ch, nch;
1961:
1962: ch = s_peeknextNW();
1963: if( ch == '(' || ch == '{' ) {
1964: ch = s_getnextNW(); /* get rid of '(' or '{' */
1965: node_p = scan_basic_block();
1966: nch = s_peeknextNW();
1967: if( nch == ')' || nch == '}' ) { /* should be either ')' or '}' */
1968: if( ((ch == '(' ) && (nch == ')' )) ||
1969: ((ch == '{' ) && (nch == '}' )) ) { /* matching left paren with right paren */
1970:
1971:
1972: } else {
1973: /* printf(" WARN: %c matched by %c\n", ch, nch); */
1974: }
1975: nch = s_getnextNW();
1976: /* ====== Added Jul 6, 1998 ====> */
1977: ch = s_peeknext_op();
1978: if( ch == '^' ) {
1979: ch = s_getnext_op(); /* get rid of '^' char */
1980: exp_p = scan_num_term();
1981: distribute_exp(node_p,exp_p);
1982: capa_mfree((char *)exp_p);
1983: }
1984: /* <== added Jul 6, 1998 == */
1985: } else {
1986: /* printf(" WARN: %c is not matched by %c\n", ch, nch); */
1987: }
1988: } else if( ch >= '0' && ch <= '9' ) {
1989: node_p = scan_num_item();
1990: } else { /* assume a unit symbol */
1991: /* printf("<B.term>"); print_remains(); */
1992: node_p = scan_unit_item();
1993: /* print_remains(); */
1994: }
1995: return node_p;
1996: }
1997: /* --------------------------------------------------
1998: N.term := N.item
1999: | '-' N.item
2000: | '(' N.expr ')'
2001: | '{' N.expr '}'
2002: -------------------------------------------------- */
2003: Unit_t *
2004: scan_num_term()
2005: {
2006: Unit_t *node_p;
2007: char ch, nch;
2008:
2009: ch = s_peeknextNW();
2010: if( ch == '(' || ch == '{' ) {
2011: ch = s_getnextNW();
2012: node_p = scan_num_expr();
2013: nch = s_peeknextNW();
2014: if( nch == ')' || nch == '}' ) { /* should be either ')' or '}' */
2015: if( ((ch == '(' ) && (nch == ')' )) ||
2016: ((ch == '{' ) && (nch == '}' )) ) {
2017:
2018: } else {
2019: /* printf(" WARN: %c matched by %c\n", ch, nch); */
2020: }
2021: nch = s_getnextNW();
2022: } else {
2023: /* printf(" WARN: %c is not matched by %c\n", ch, ch); */
2024: }
2025: } else if( ch == '-' ) {
2026: ch = s_getnextNW();
2027: node_p = scan_num_item();
2028: node_p->u_scale = (-1)*node_p->u_scale;
2029: } else {
2030: if( isdigit(ch) ) {
2031: node_p = scan_num_item();
2032: } else { /* something other than a number */
2033: /*
2034: printf(" ERROR: expect a number: ");
2035: print_remains();
2036: */
2037: node_p = p_new_num(NULL, 0.0, NULL); /* make the unknown item */
2038: }
2039: }
2040: return node_p;
2041: }
2042:
2043: /* --------------------------------------------------
2044: B.block := B.term
2045: | B.term ' ' B.term
2046: | B.term '*' B.term
2047: | B.term '/' B.term
2048: -------------------------------------------------- */
2049: Unit_t *
2050: scan_basic_block()
2051: {
2052: Unit_t *node_p;
2053: char ch;
2054: int op;
2055:
2056: /* printf("<B.block>(before B.term)"); print_remains(); */
2057: node_p = scan_basic_term();
2058: ch = s_peeknext_op();
2059: while ( ch == '*' || ch == '/' ) {
2060: op = ( ch == '/' ? U_OP_DIVIDE : U_OP_TIMES);
2061: ch = s_getnext_op();
2062: /* printf("<B.block>(/ *)"); print_remains(); */
2063: node_p = p_new_op(node_p,op,scan_basic_term());
2064: ch = s_peeknext_op();
2065: }
2066: return node_p;
2067: }
2068: /* --------------------------------------------------
2069: N.block := N.term
2070: | N.term ' ' N.term
2071: | N.term '*' N.term
2072: | N.term '/' N.term
2073: -------------------------------------------------- */
2074: Unit_t *
2075: scan_num_block()
2076: {
2077: Unit_t *node_p, *opand_p;
2078: char ch;
2079: double result;
2080:
2081: node_p = scan_num_term();
2082: ch = s_peeknext_op();
2083: while ( ch == '*' || ch == '/' ) {
2084: s_getnext_op();
2085: opand_p = scan_num_term();
2086: if( ch == '*' ) {
2087: result = node_p->u_scale * opand_p->u_scale;
2088: } else {
2089: result = node_p->u_scale / opand_p->u_scale;
2090: }
2091: node_p->u_scale = result;
2092: capa_mfree((char *)opand_p);
2093: ch = s_peeknext_op();
2094: }
2095: return node_p;
2096: }
2097:
2098: /* ---------------------------------------
2099: U.expr := B.block
2100: | B.block '+' B.block
2101: | B.block '-' B.block
2102: --------------------------------------- */
2103: Unit_t *
2104: scan_unit_expr()
2105: {
2106: Unit_t *node_p;
2107: char ch;
2108: int op;
2109:
2110: /* printf("<U.expr>"); print_remains(); */
2111: node_p = scan_basic_block();
2112: ch = s_peeknext_op();
2113: while ( ch == '+' || ch == '-' ) {
2114: op = ( ch == '+' ? U_OP_PLUS : U_OP_MINUS);
2115: ch = s_getnext_op();
2116: /* printf("<U.expr>(+-)"); print_remains(); */
2117: node_p = p_new_op(node_p,op,scan_basic_block());
2118: ch = s_peeknext_op();
2119: }
2120: return node_p;
2121: }
2122: /* -----------------------------------------
2123: N.expr := N.block
2124: | N.block '+' N.block
2125: | N.block '-' N.block
2126: ----------------------------------------- */
2127: Unit_t *
2128: scan_num_expr()
2129: {
2130: Unit_t *node_p, *opand_p;
2131: char ch;
2132: double result;
2133:
2134: node_p = scan_num_block();
2135: ch = s_peeknext_op();
2136: while ( ch == '+' || ch == '-' ) {
2137: ch = s_getnext_op();
2138: opand_p = scan_num_block();
2139: if( ch == '+' ) {
2140: result = node_p->u_scale + opand_p->u_scale;
2141: } else {
2142: result = node_p->u_scale - opand_p->u_scale;
2143: }
2144: node_p->u_scale = result;
2145: capa_mfree((char *)opand_p);
2146: ch = s_peeknext_op();
2147: }
2148: return node_p;
2149: }
2150:
2151: /* ----------------------------------------------------------------------- */
2152: /* <-- This is the major entry point to parse an units expression ------> */
2153: Unit_t *
2154: parse_unit_expr(char *symb_str)
2155: {
2156: Unit_t *root_p;
2157: int len;
2158:
2159: len = strlen(symb_str);
2160: strcpy(Sbuf,symb_str); /* copy it into the global Sbuf */
2161: Sidx=0;
2162: root_p = scan_unit_expr();
2163: if(Sidx < len-1 ) {
2164: /* printf(" WARN: NOT PARSED:"); print_remains(); */
2165: }
2166: return (root_p);
2167:
2168: }
2169:
2170: void
2171: print_remains()
2172: {
2173: int len, ii;
2174:
2175: len = strlen(Sbuf);
2176: printf("[[");
2177: for(ii=Sidx;ii<len;ii++) {
2178: printf("%c",Sbuf[ii]);
2179: }
2180: printf("]]");
2181:
2182: }
2183:
2184:
2185:
2186: /* =================================================================== */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>