Annotation of OpenXM_contrib2/asir2000/io/cio.c, Revision 1.12
1.3 noro 1: /*
2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
3: * All rights reserved.
4: *
5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
6: * non-exclusive and royalty-free license to use, copy, modify and
7: * redistribute, solely for non-commercial and non-profit purposes, the
8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
9: * conditions of this Agreement. For the avoidance of doubt, you acquire
10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
11: * third party developer retains all rights, including but not limited to
12: * copyrights, in and to the SOFTWARE.
13: *
14: * (1) FLL does not grant you a license in any way for commercial
15: * purposes. You may use the SOFTWARE only for non-commercial and
16: * non-profit purposes only, such as academic, research and internal
17: * business use.
18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
19: * international copyright treaties. If you make copies of the SOFTWARE,
20: * with or without modification, as permitted hereunder, you shall affix
21: * to all such copies of the SOFTWARE the above copyright notice.
22: * (3) An explicit reference to this SOFTWARE and its copyright owner
23: * shall be made on your publication or presentation in any form of the
24: * results obtained by use of the SOFTWARE.
25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.4 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.3 noro 27: * for such modification or the source code of the modified part of the
28: * SOFTWARE.
29: *
30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
1.12 ! ohara 47: * $OpenXM: OpenXM_contrib2/asir2000/io/cio.c,v 1.11 2001/10/09 01:36:20 noro Exp $
1.3 noro 48: */
1.1 noro 49: #include "ca.h"
50: #include "parse.h"
51: #include "ox.h"
1.12 ! ohara 52: #if !defined(VISUAL)
! 53: #include <ctype.h>
! 54: #endif
1.1 noro 55:
56: #define ISIZ sizeof(int)
57:
1.11 noro 58: int valid_as_cmo(Obj obj)
1.1 noro 59: {
60: NODE m;
61:
62: if ( !obj )
63: return 1;
64: switch ( OID(obj) ) {
65: case O_MATHCAP: case O_P: case O_R: case O_DP: case O_STR:
1.6 noro 66: case O_ERR: case O_USINT: case O_BYTEARRAY: case O_VOID:
1.1 noro 67: return 1;
68: case O_N:
1.2 noro 69: if ( NID((Num)obj) == N_Q || NID((Num)obj) == N_R )
1.1 noro 70: return 1;
71: else
72: return 0;
73: case O_LIST:
74: for ( m = BDY((LIST)obj); m; m = NEXT(m) )
75: if ( !valid_as_cmo(BDY(m)) )
76: return 0;
77: return 1;
1.7 noro 78: case O_QUOTE:
79: return 1;
1.1 noro 80: default:
81: return 0;
82: }
83: }
84:
1.11 noro 85: void write_cmo(FILE *s,Obj obj)
1.1 noro 86: {
87: int r;
88: char errmsg[BUFSIZ];
1.10 noro 89: LIST l;
1.1 noro 90:
91: if ( !obj ) {
92: r = CMO_NULL; write_int(s,&r);
93: return;
94: }
95: switch ( OID(obj) ) {
96: case O_N:
1.2 noro 97: switch ( NID((Num)obj) ) {
98: case N_Q:
99: write_cmo_q(s,(Q)obj);
100: break;
101: case N_R:
102: write_cmo_real(s,(Real)obj);
103: break;
104: default:
105: sprintf(errmsg, "write_cmo : number id=%d not implemented.",
106: NID((Num)obj));
107: error(errmsg);
108: break;
109: }
1.1 noro 110: break;
111: case O_P:
1.11 noro 112: write_cmo_p(s,(P)obj);
1.1 noro 113: break;
114: case O_R:
1.11 noro 115: write_cmo_r(s,(R)obj);
1.1 noro 116: break;
117: case O_DP:
1.11 noro 118: write_cmo_dp(s,(DP)obj);
1.1 noro 119: break;
120: case O_LIST:
1.11 noro 121: write_cmo_list(s,(LIST)obj);
1.1 noro 122: break;
123: case O_STR:
1.11 noro 124: write_cmo_string(s,(STRING)obj);
1.1 noro 125: break;
126: case O_USINT:
1.11 noro 127: write_cmo_uint(s,(USINT)obj);
1.1 noro 128: break;
129: case O_MATHCAP:
1.11 noro 130: write_cmo_mathcap(s,(MATHCAP)obj);
1.1 noro 131: break;
132: case O_ERR:
1.11 noro 133: write_cmo_error(s,(ERR)obj);
1.1 noro 134: break;
1.6 noro 135: case O_BYTEARRAY:
1.11 noro 136: write_cmo_bytearray(s,(BYTEARRAY)obj);
1.6 noro 137: break;
1.1 noro 138: case O_VOID:
139: r = ((USINT)obj)->body; write_int(s,&r);
140: break;
1.7 noro 141: case O_QUOTE:
1.10 noro 142: fnodetotree(BDY((QUOTE)obj),&l);
143: write_cmo_tree(s,l);
1.7 noro 144: break;
1.1 noro 145: default:
146: sprintf(errmsg, "write_cmo : id=%d not implemented.",OID(obj));
147: error(errmsg);
148: break;
149: }
1.5 noro 150: }
151:
1.11 noro 152: int cmo_tag(Obj obj,int *tag)
1.5 noro 153: {
154: if ( !valid_as_cmo(obj) )
155: return 0;
156: if ( !obj ) {
157: *tag = CMO_NULL;
158: return 1;
159: }
160: switch ( OID(obj) ) {
161: case O_N:
162: switch ( NID((Num)obj) ) {
163: case N_Q:
164: *tag = DN((Q)obj) ? CMO_QQ : CMO_ZZ; break;
165: case N_R:
166: *tag = CMO_IEEE_DOUBLE_FLOAT; break;
167: default:
168: return 0;
169: }
170: break;
171: case O_P:
172: *tag = CMO_RECURSIVE_POLYNOMIAL; break;
173: case O_R:
174: *tag = CMO_RATIONAL; break;
175: case O_DP:
176: *tag = CMO_DISTRIBUTED_POLYNOMIAL; break;
177: case O_LIST:
178: *tag = CMO_LIST; break;
179: case O_STR:
180: *tag = CMO_STRING; break;
181: case O_USINT:
182: *tag = CMO_INT32; break;
183: case O_MATHCAP:
184: *tag = CMO_MATHCAP; break;
185: case O_ERR:
186: *tag = CMO_ERROR2; break;
1.7 noro 187: case O_QUOTE:
188: *tag = CMO_TREE; break; break;
1.5 noro 189: default:
190: return 0;
191: }
192: return 1;
1.1 noro 193: }
194:
1.11 noro 195: void write_cmo_mathcap(FILE *s,MATHCAP mc)
1.1 noro 196: {
197: unsigned int r;
198:
199: r = CMO_MATHCAP; write_int(s,&r);
1.11 noro 200: write_cmo(s,(Obj)BDY(mc));
1.1 noro 201: }
202:
1.11 noro 203: void write_cmo_uint(FILE *s,USINT ui)
1.1 noro 204: {
205: unsigned int r;
206:
207: r = CMO_INT32; write_int(s,&r);
208: r = ui->body; write_int(s,&r);
209: }
210:
1.11 noro 211: void write_cmo_q(FILE *s,Q q)
1.1 noro 212: {
213: int r;
214:
215: if ( q && DN(q) ) {
216: r = CMO_QQ; write_int(s,&r);
217: write_cmo_zz(s,SGN(q),NM(q));
218: write_cmo_zz(s,1,DN(q));
219: } else {
220: r = CMO_ZZ; write_int(s,&r);
221: write_cmo_zz(s,SGN(q),NM(q));
222: }
223: }
224:
1.11 noro 225: void write_cmo_real(FILE *s,Real real)
1.2 noro 226: {
227: unsigned int r;
228: double dbl;
229:
230: r = CMO_IEEE_DOUBLE_FLOAT; write_int(s,&r);
231: dbl = real->body; write_double(s,&dbl);
232: }
233:
1.11 noro 234: void write_cmo_zz(FILE *s,int sgn,N n)
1.1 noro 235: {
1.11 noro 236: int l,bytes;
1.1 noro 237:
238: #if 1
239: l = PL(n);
240: bytes = sgn*l;
241: write_int(s,&bytes);
242: write_intarray(s,BD(n),l);
243: #else
244: l = PL(n); b = (unsigned int *)BD(n);
245: bytes = sgn*4*l;
246: write_int(s,&bytes);
247: for ( i = l-1; i >= 0; i-- ) {
248: t = b[i];
249: c = t>>24; write_char(s,&c);
250: c = (t>>16)&0xff; write_char(s,&c);
251: c = (t>>8)&0xff; write_char(s,&c);
252: c = t&0xff; write_char(s,&c);
253: }
254: #endif
255: }
256:
1.11 noro 257: void write_cmo_p(FILE *s,P p)
1.1 noro 258: {
259: int r,i;
260: VL t,vl;
261: char *namestr;
262: STRING name;
263:
264: r = CMO_RECURSIVE_POLYNOMIAL; write_int(s,&r);
265: get_vars((Obj)p,&vl);
266:
267: /* indeterminate list */
268: r = CMO_LIST; write_int(s,&r);
269: for ( t = vl, i = 0; t; t = NEXT(t), i++ );
270: write_int(s,&i);
271: r = CMO_INDETERMINATE;
272: for ( t = vl; t; t = NEXT(t) ) {
273: write_int(s,&r);
274: /* localname_to_cmoname(NAME(t->v),&namestr); */
275: namestr = NAME(t->v);
276: MKSTR(name,namestr);
1.11 noro 277: write_cmo(s,(Obj)name);
1.1 noro 278: }
279:
280: /* body */
281: write_cmo_upoly(s,vl,p);
282: }
283:
1.11 noro 284: void write_cmo_upoly(FILE *s,VL vl,P p)
1.1 noro 285: {
286: int r,i;
287: V v;
288: DCP dc,dct;
289: VL vlt;
290:
291: if ( NUM(p) )
1.11 noro 292: write_cmo(s,(Obj)p);
1.1 noro 293: else {
294: r = CMO_UNIVARIATE_POLYNOMIAL; write_int(s,&r);
295: v = VR(p);
296: dc = DC(p);
297: for ( i = 0, dct = dc; dct; dct = NEXT(dct), i++ );
298: write_int(s,&i);
299: for ( i = 0, vlt = vl; vlt->v != v; vlt = NEXT(vlt), i++ );
300: write_int(s,&i);
301: for ( dct = dc; dct; dct = NEXT(dct) ) {
302: i = QTOS(DEG(dct)); write_int(s,&i);
303: write_cmo_upoly(s,vl,COEF(dct));
304: }
305: }
306: }
307:
1.11 noro 308: void write_cmo_r(FILE *s,R f)
1.1 noro 309: {
310: int r;
311:
312: r = CMO_RATIONAL; write_int(s,&r);
1.11 noro 313: write_cmo(s,(Obj)NM(f));
314: write_cmo(s,(Obj)DN(f));
1.1 noro 315: }
316:
1.11 noro 317: void write_cmo_dp(FILE *s,DP dp)
1.1 noro 318: {
319: int i,n,nv,r;
320: MP m;
321:
322: for ( n = 0, m = BDY(dp); m; m = NEXT(m), n++ );
323: r = CMO_DISTRIBUTED_POLYNOMIAL; write_int(s,&r);
324: r = n; write_int(s,&r);
325: r = CMO_DMS_GENERIC; write_int(s,&r);
326: nv = dp->nv;
327: for ( i = 0, m = BDY(dp); i < n; i++, m = NEXT(m) )
328: write_cmo_monomial(s,m,nv);
329: }
330:
1.11 noro 331: void write_cmo_monomial(FILE *s,MP m,int n)
1.1 noro 332: {
333: int i,r;
334: int *p;
335:
336: r = CMO_MONOMIAL32; write_int(s,&r);
337: write_int(s,&n);
338: for ( i = 0, p = m->dl->d; i < n; i++ ) {
339: write_int(s,p++);
340: }
1.11 noro 341: write_cmo_q(s,(Q)m->c);
1.1 noro 342: }
343:
1.11 noro 344: void write_cmo_list(FILE *s,LIST list)
1.1 noro 345: {
346: NODE m;
347: int i,n,r;
348:
349: for ( n = 0, m = BDY(list); m; m = NEXT(m), n++ );
350: r = CMO_LIST; write_int(s,&r);
351: write_int(s,&n);
352: for ( i = 0, m = BDY(list); i < n; i++, m = NEXT(m) )
353: write_cmo(s,BDY(m));
354: }
355:
1.11 noro 356: void write_cmo_string(FILE *s,STRING str)
1.1 noro 357: {
358: int r;
359:
360: r = CMO_STRING; write_int(s,&r);
361: savestr(s,BDY(str));
362: }
363:
1.11 noro 364: void write_cmo_bytearray(FILE *s,BYTEARRAY array)
1.6 noro 365: {
366: int r;
367:
368: r = CMO_DATUM; write_int(s,&r);
369: write_int(s,&array->len);
370: write_string(s,array->body,array->len);
371: }
372:
1.11 noro 373: void write_cmo_error(FILE *s,ERR e)
1.1 noro 374: {
375: int r;
376:
377: r = CMO_ERROR2; write_int(s,&r);
378: write_cmo(s,BDY(e));
379: }
380:
1.7 noro 381: /* XXX */
382:
1.10 noro 383: /*
384: * BDY(l) = treenode
385: * treenode = [property,(name,)arglist]
386: * arglist = list of treenode
387: */
388:
1.11 noro 389: void write_cmo_tree(FILE *s,LIST l)
1.7 noro 390: {
1.10 noro 391: NODE n;
392: int r;
393: STRING prop,name,key;
1.7 noro 394:
1.10 noro 395: /* (CMO_TREE (CMO_LIST,n,key1,attr1,...,keyn,attn),(CMO_LIST,m,arg1,...,argm)) */
396: n = BDY(l);
397: prop = (STRING)BDY(n); n = NEXT(n);
398: if ( !strcmp(BDY(prop),"internal") ) {
399: write_cmo(s,(Obj)BDY(n));
400: } else {
401: if ( strcmp(BDY(prop),"list") ) {
402: r = CMO_TREE; write_int(s,&r);
403: name = (STRING)BDY(n);
404: n = NEXT(n);
405: /* function name */
1.11 noro 406: write_cmo(s,(Obj)name);
1.8 noro 407:
1.10 noro 408: /* attribute list */
1.7 noro 409: r = CMO_LIST; write_int(s,&r);
1.10 noro 410: r = 2; write_int(s,&r);
411: MKSTR(key,"asir");
1.11 noro 412: write_cmo(s,(Obj)key);
413: write_cmo(s,(Obj)prop);
1.10 noro 414: }
415:
416: /* argument list */
417: r = CMO_LIST; write_int(s,&r);
418: /* len = number of arguments */
419: r = length(n); write_int(s,&r);
420: while ( n ) {
421: write_cmo_tree(s,BDY(n));
422: n = NEXT(n);
423: }
1.7 noro 424: }
425: }
426:
1.11 noro 427: void read_cmo(FILE *s,Obj *rp)
1.1 noro 428: {
429: int id;
1.11 noro 430: int sgn,dummy;
431: Q q;
1.1 noro 432: N nm,dn;
433: P p,pnm,pdn;
1.2 noro 434: Real real;
435: double dbl;
1.1 noro 436: STRING str;
437: USINT t;
438: DP dp;
439: Obj obj;
440: ERR e;
441: MATHCAP mc;
1.6 noro 442: BYTEARRAY array;
1.10 noro 443: LIST list;
1.1 noro 444:
445: read_int(s,&id);
446: switch ( id ) {
447: /* level 0 objects */
448: case CMO_NULL:
449: *rp = 0;
450: break;
451: case CMO_INT32:
1.11 noro 452: read_cmo_uint(s,&t); *rp = (Obj)t;
1.1 noro 453: break;
454: case CMO_DATUM:
1.6 noro 455: loadbytearray(s,&array); *rp = (Obj)array;
456: break;
1.1 noro 457: case CMO_STRING:
458: loadstring(s,&str); *rp = (Obj)str;
459: break;
460: case CMO_MATHCAP:
461: read_cmo(s,&obj); MKMATHCAP(mc,(LIST)obj);
462: *rp = (Obj)mc;
463: break;
464: case CMO_ERROR:
465: MKERR(e,0); *rp = (Obj)e;
466: break;
467: case CMO_ERROR2:
468: read_cmo(s,&obj); MKERR(e,obj); *rp = (Obj)e;
469: break;
470: /* level 1 objects */
471: case CMO_LIST:
472: read_cmo_list(s,rp);
473: break;
474: case CMO_MONOMIAL32:
1.11 noro 475: read_cmo_monomial(s,&dp); *rp = (Obj)dp;
1.1 noro 476: break;
477: case CMO_ZZ:
478: read_cmo_zz(s,&sgn,&nm);
479: NTOQ(nm,sgn,q); *rp = (Obj)q;
480: break;
481: case CMO_QQ:
482: read_cmo_zz(s,&sgn,&nm);
483: read_cmo_zz(s,&dummy,&dn);
484: NDTOQ(nm,dn,sgn,q); *rp = (Obj)q;
1.2 noro 485: break;
486: case CMO_IEEE_DOUBLE_FLOAT:
487: read_double(s,&dbl); MKReal(dbl,real); *rp = (Obj)real;
1.1 noro 488: break;
489: case CMO_DISTRIBUTED_POLYNOMIAL:
490: read_cmo_dp(s,&dp); *rp = (Obj)dp;
491: break;
492: case CMO_RECURSIVE_POLYNOMIAL:
493: read_cmo_p(s,&p); *rp = (Obj)p;
494: break;
495: case CMO_UNIVARIATE_POLYNOMIAL:
496: read_cmo_upoly(s,&p); *rp = (Obj)p;
497: break;
498: case CMO_INDETERMINATE:
1.11 noro 499: read_cmo(s,rp);
1.1 noro 500: break;
501: case CMO_RATIONAL:
1.11 noro 502: read_cmo(s,&obj); pnm = (P)obj;
503: read_cmo(s,&obj); pdn = (P)obj;
1.1 noro 504: divr(CO,(Obj)pnm,(Obj)pdn,rp);
505: break;
506: case CMO_ZERO:
507: *rp = 0;
508: break;
509: case CMO_DMS_OF_N_VARIABLES:
510: read_cmo(s,rp);
511: break;
512: case CMO_RING_BY_NAME:
513: read_cmo(s,rp);
514: break;
1.7 noro 515: case CMO_TREE:
1.10 noro 516: read_cmo_tree_as_list(s,&list);
517: #if 0
518: treetofnode(list,&fn);
1.8 noro 519: MKQUOTE(quote,fn);
520: *rp = (Obj)quote;
1.10 noro 521: #else
1.11 noro 522: *rp = (Obj)list;
1.10 noro 523: #endif
1.7 noro 524: break;
1.1 noro 525: default:
526: MKUSINT(t,id);
527: t->id = O_VOID;
528: *rp = (Obj)t;
529: break;
530: }
531: }
532:
1.11 noro 533: void read_cmo_uint(FILE *s,USINT *rp)
1.1 noro 534: {
535: unsigned int body;
536:
537: read_int(s,&body);
538: MKUSINT(*rp,body);
539: }
540:
1.11 noro 541: void read_cmo_zz(FILE *s,int *sgn,N *rp)
1.1 noro 542: {
1.11 noro 543: int l;
1.1 noro 544: N n;
545:
546: read_int(s,&l);
547: if ( l == 0 ) {
548: *sgn = 0;
549: *rp = 0;
550: return;
551: }
552: if ( l < 0 ) {
553: *sgn = -1; l = -l;
554: } else
555: *sgn = 1;
556: #if 1
557: *rp = n = NALLOC(l); PL(n) = l;
558: read_intarray(s,BD(n),l);
559: #else
560: words = (l+3)/4;
561: *rp = n = NALLOC(words); PL(n) = words; b = BD(n);
562: h = 0;
563: switch ( l % 4 ) {
564: case 0:
565: read_char(s,&c); h = c;
566: case 3:
567: read_char(s,&c); h = (h<<8)|c;
568: case 2:
569: read_char(s,&c); h = (h<<8)|c;
570: case 1:
571: read_char(s,&c); h = (h<<8)|c;
572: }
573: b[words-1] = h;
574: for ( i = words-2; i >= 0; i-- ) {
575: read_char(s,&c); h = c;
576: read_char(s,&c); h = (h<<8)|c;
577: read_char(s,&c); h = (h<<8)|c;
578: read_char(s,&c); h = (h<<8)|c;
579: b[i] = h;
580: }
581: #endif
582: }
583:
1.11 noro 584: void read_cmo_list(FILE *s,Obj *rp)
1.1 noro 585: {
586: int len;
587: Obj *w;
588: int i;
589: NODE n0,n1;
590: LIST list;
591:
592: read_int(s,&len);
593: w = (Obj *)ALLOCA(len*sizeof(Obj));
594: for ( i = 0; i < len; i++ )
595: read_cmo(s,&w[i]);
596: for ( i = len-1, n0 = 0; i >= 0; i-- ) {
597: MKNODE(n1,w[i],n0); n0 = n1;
598: }
599: MKLIST(list,n0);
600: *rp = (Obj)list;
601: }
602:
1.11 noro 603: void read_cmo_dp(FILE *s,DP *rp)
1.1 noro 604: {
605: int len;
606: int i;
607: MP mp0,mp;
608: int nv,d;
609: DP dp;
610: Obj obj;
611:
612: read_int(s,&len);
613: /* skip the ring definition */
614: read_cmo(s,&obj);
615: for ( mp0 = 0, i = 0, d = 0; i < len; i++ ) {
1.11 noro 616: read_cmo(s,&obj); dp = (DP)obj;
1.1 noro 617: if ( !mp0 ) {
618: nv = dp->nv;
619: mp0 = dp->body;
620: mp = mp0;
621: } else {
622: NEXT(mp) = dp->body;
623: mp = NEXT(mp);
624: }
625: d = MAX(d,dp->sugar);
626: }
627: MKDP(nv,mp0,dp);
628: dp->sugar = d; *rp = dp;
629: }
630:
1.11 noro 631: void read_cmo_monomial(FILE *s,DP *rp)
1.1 noro 632: {
1.11 noro 633: Obj obj;
1.1 noro 634: MP m;
635: DP dp;
636: int i,sugar,n;
637: DL dl;
638:
639: read_int(s,&n);
640: NEWMP(m); NEWDL(dl,n); m->dl = dl;
641: read_intarray(s,dl->d,n);
642: for ( sugar = 0, i = 0; i < n; i++ )
643: sugar += dl->d[i];
644: dl->td = sugar;
1.11 noro 645: read_cmo(s,&obj); m->c = (P)obj;
1.1 noro 646: NEXT(m) = 0; MKDP(n,m,dp); dp->sugar = sugar; *rp = dp;
647: }
648:
649: static V *remote_vtab;
650:
1.11 noro 651: void read_cmo_p(FILE *s,P *rp)
1.1 noro 652: {
1.11 noro 653: Obj obj;
1.1 noro 654: LIST vlist;
655: int nv,i;
656: V *vtab;
657: V v1,v2;
658: NODE t;
659: P v,p;
660: VL tvl,rvl;
661: char *name;
662:
1.11 noro 663: read_cmo(s,&obj); vlist = (LIST)obj;
1.1 noro 664: nv = length(BDY(vlist));
665: vtab = (V *)ALLOCA(nv*sizeof(V));
666: for ( i = 0, t = BDY(vlist); i < nv; t = NEXT(t), i++ ) {
667: /* cmoname_to_localname(BDY((STRING)BDY(t)),&name); */
668: name = BDY((STRING)BDY(t));
669: makevar(name,&v); vtab[i] = VR(v);
670: }
671: remote_vtab = vtab;
1.11 noro 672: read_cmo(s,&obj); p = (P)obj;
1.1 noro 673: for ( i = 0; i < nv-1; i++ ) {
674: v1 = vtab[i]; v2 = vtab[i+1];
675: for ( tvl = CO; tvl->v != v1 && tvl->v != v2; tvl = NEXT(tvl) );
676: if ( tvl->v == v2 )
677: break;
678: }
679: if ( i < nv-1 ) {
680: for ( i = nv-1, rvl = 0; i >= 0; i-- ) {
681: NEWVL(tvl); tvl->v = vtab[i]; NEXT(tvl) = rvl; rvl = tvl;
682: }
683: reorderp(CO,rvl,p,rp);
684: } else
685: *rp = p;
686: }
687:
1.11 noro 688: void read_cmo_upoly(FILE *s,P *rp)
1.1 noro 689: {
690: int n,ind,i,d;
1.11 noro 691: Obj obj;
1.1 noro 692: P c;
693: Q q;
694: DCP dc0,dc;
695:
696: read_int(s,&n);
697: read_int(s,&ind);
698: for ( i = 0, dc0 = 0; i < n; i++ ) {
699: read_int(s,&d);
1.11 noro 700: read_cmo(s,&obj); c = (P)obj;
1.1 noro 701: if ( c ) {
702: if ( OID(c) == O_USINT ) {
703: UTOQ(((USINT)c)->body,q); c = (P)q;
704: }
705: NEXTDC(dc0,dc);
706: STOQ(d,q);
707: dc->c = c; dc->d = q;
708: }
709: }
710: if ( dc0 )
711: NEXT(dc) = 0;
712: MKP(remote_vtab[ind],dc0,*rp);
1.7 noro 713: }
714:
715: /* XXX */
716:
717: extern struct oARF arf[];
718:
719: struct operator_tab {
720: char *name;
721: fid id;
722: ARF arf;
723: cid cid;
724: };
725:
726: static struct operator_tab optab[] = {
727: {"+",I_BOP,&arf[0],0}, /* XXX */
728: {"-",I_BOP,&arf[1],0},
729: {"*",I_BOP,&arf[2],0},
730: {"/",I_BOP,&arf[3],0},
731: {"%",I_BOP,&arf[4],0},
732: {"^",I_BOP,&arf[5],0},
733: {"==",I_COP,0,C_EQ},
734: {"!=",I_COP,0,C_NE},
735: {"<",I_COP,0,C_LT},
736: {"<=",I_COP,0,C_LE},
737: {">",I_COP,0,C_GT},
738: {">=",I_COP,0,C_GE},
739: {"&&",I_AND,0,0},
740: {"||",I_OR,0,0},
741: {"!",I_NOT,0,0},
742: };
743:
744: static int optab_len = sizeof(optab)/sizeof(struct operator_tab);
745:
1.10 noro 746: #if 0
747: /* old code */
1.11 noro 748: void read_cmo_tree(s,rp)
1.7 noro 749: FILE *s;
1.8 noro 750: FNODE *rp;
1.7 noro 751: {
752: int r,i,n;
753: char *opname;
754: STRING name,cd;
755: int op;
1.8 noro 756: pointer *arg;
1.7 noro 757: QUOTE quote;
758: FNODE fn;
1.8 noro 759: NODE t,t1;
1.7 noro 760: fid id;
1.8 noro 761: Obj expr;
762: FUNC func;
1.7 noro 763:
764: read_cmo(s,&name);
1.10 noro 765: read_cmo(s,&attr);
1.7 noro 766: for ( i = 0; i < optab_len; i++ )
767: if ( !strcmp(optab[i].name,BDY(name)) )
768: break;
769: if ( i == optab_len ) {
770: /* may be a function name */
1.8 noro 771: n = read_cmo_tree_arg(s,&arg);
772: for ( i = n-1, t = 0; i >= 0; i-- ) {
773: MKNODE(t1,arg[i],t); t = t1;
774: }
775: searchf(sysf,BDY(name),&func);
776: if ( !func )
777: searchf(ubinf,BDY(name),&func);
778: if ( !func )
779: searchpf(BDY(name),&func);
780: if ( !func )
781: searchf(usrf,BDY(name),&func);
782: if ( !func )
783: appenduf(BDY(name),&func);
784: *rp = mkfnode(2,I_FUNC,func,mkfnode(1,I_LIST,t));
1.7 noro 785: } else {
786: opname = optab[i].name;
787: id = optab[i].id;
788: switch ( id ) {
789: case I_BOP:
1.8 noro 790: read_cmo_tree_arg(s,&arg);
791: *rp = mkfnode(3,I_BOP,optab[i].arf,arg[0],arg[1]);
792: return;
1.7 noro 793: case I_COP:
1.8 noro 794: read_cmo_tree_arg(s,&arg);
795: *rp = mkfnode(3,I_COP,optab[i].cid,arg[0],arg[0]);
796: return;
1.7 noro 797: case I_AND:
1.8 noro 798: read_cmo_tree_arg(s,&arg);
799: *rp = mkfnode(2,I_AND,arg[0],arg[1]);
800: return;
1.7 noro 801: case I_OR:
1.8 noro 802: read_cmo_tree_arg(s,&arg);
803: *rp = mkfnode(2,I_OR,arg[0],arg[1]);
804: return;
1.7 noro 805: case I_NOT:
1.8 noro 806: read_cmo_tree_arg(s,&arg);
807: *rp = mkfnode(1,I_OR,arg[0]);
808: return;
1.7 noro 809: }
810: }
1.8 noro 811: }
812:
813: int read_cmo_tree_arg(s,argp)
814: FILE *s;
815: pointer **argp;
816: {
817: int id,n,i;
818: pointer *ap;
819: Obj t;
820:
821: read_int(s,&id); /* id = CMO_LIST */
822: read_int(s,&n); /* n = the number of args */
823: *argp = ap = (pointer *) MALLOC(n*sizeof(pointer));
824: for ( i = 0; i < n; i++ ) {
825: read_cmo(s,&t);
826: if ( !t || (OID(t) != O_QUOTE) )
827: ap[i] = mkfnode(1,I_FORMULA,t);
828: else
829: ap[i] = BDY((QUOTE)t);
830: }
831: return n;
1.1 noro 832: }
1.10 noro 833: #else
1.11 noro 834: void read_cmo_tree_as_list(FILE *s,LIST *rp)
1.10 noro 835: {
1.11 noro 836: Obj obj;
1.10 noro 837: STRING name;
838: LIST attr,args;
839: NODE t0,t1;
840:
1.11 noro 841: read_cmo(s,&obj); name = (STRING)obj;
842: read_cmo(s,&obj); attr = (LIST)obj;
843: read_cmo(s,&obj); args = (LIST)obj;
1.10 noro 844: MKNODE(t1,name,BDY(args));
845: MKNODE(t0,attr,t1);
846: MKLIST(*rp,t0);
847: }
848: #endif
1.1 noro 849:
1.11 noro 850: void localname_to_cmoname(char *a,char **b)
1.1 noro 851: {
852: int l;
853: char *t;
854:
855: l = strlen(a);
856: if ( l >= 2 && a[0] == '@' && isupper(a[1]) ) {
857: t = *b = (char *)MALLOC_ATOMIC(l);
858: strcpy(t,a+1);
859: } else {
860: t = *b = (char *)MALLOC_ATOMIC(l+1);
861: strcpy(t,a);
862: }
863: }
864:
1.11 noro 865: void cmoname_to_localname(char *a,char **b)
1.1 noro 866: {
867: int l;
868: char *t;
869:
870: l = strlen(a);
871: if ( isupper(a[0]) ) {
872: t = *b = (char *)MALLOC_ATOMIC(l+2);
873: strcpy(t+1,a);
874: t[0] = '@';
875: } else {
876: t = *b = (char *)MALLOC_ATOMIC(l+1);
877: strcpy(t,a);
878: }
879: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>