[BACK]Return to cio.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / io

Annotation of OpenXM_contrib2/asir2000/io/cio.c, Revision 1.20

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.20    ! noro       47:  * $OpenXM: OpenXM_contrib2/asir2000/io/cio.c,v 1.19 2015/08/14 13:51:55 fujimoto Exp $
1.3       noro       48: */
1.1       noro       49: #include "ca.h"
                     50: #include "parse.h"
                     51: #include "ox.h"
1.19      fujimoto   52: #if !defined(VISUAL) && !defined(__MINGW32__)
1.12      ohara      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: {
1.14      noro       60:   NODE m;
1.13      noro       61:   int nid;
1.1       noro       62:
1.14      noro       63:   if ( !obj )
                     64:     return 1;
                     65:   switch ( OID(obj) ) {
                     66:     case O_MATHCAP: case O_P: case O_R: case O_DP: case O_STR:
                     67:     case O_ERR: case O_USINT: case O_BYTEARRAY: case O_VOID:
                     68:       return 1;
                     69:     case O_N:
1.13      noro       70:       nid = NID((Num)obj);
1.20    ! noro       71:       if ( nid == N_Q || nid == N_R || nid == N_B || nid == N_C )
1.14      noro       72:         return 1;
                     73:       else
                     74:         return 0;
                     75:     case O_LIST:
                     76:       for ( m = BDY((LIST)obj); m; m = NEXT(m) )
                     77:         if ( !valid_as_cmo(BDY(m)) )
                     78:           return 0;
                     79:       return 1;
                     80:     case O_QUOTE:
                     81:       return 1;
                     82:     default:
                     83:       return 0;
                     84:   }
1.1       noro       85: }
                     86:
1.11      noro       87: void write_cmo(FILE *s,Obj obj)
1.1       noro       88: {
1.14      noro       89:   int r;
                     90:   char errmsg[BUFSIZ];
                     91:   LIST l;
                     92:
                     93:   if ( !obj ) {
                     94:     r = CMO_NULL; write_int(s,&r);
                     95:     return;
                     96:   }
                     97:   switch ( OID(obj) ) {
                     98:     case O_N:
                     99:       switch ( NID((Num)obj) ) {
                    100:         case N_Q:
                    101:           write_cmo_q(s,(Q)obj);
                    102:           break;
                    103:         case N_R:
                    104:           write_cmo_real(s,(Real)obj);
                    105:           break;
1.13      noro      106:         case N_B:
1.14      noro      107:           write_cmo_bf(s,(BF)obj);
                    108:           break;
1.20    ! noro      109:         case N_C:
        !           110:           write_cmo_complex(s,(C)obj);
        !           111:           break;
1.14      noro      112:         default:
                    113:           sprintf(errmsg, "write_cmo : number id=%d not implemented.",
                    114:             NID((Num)obj));
                    115:           error(errmsg);
                    116:           break;
                    117:       }
                    118:       break;
                    119:     case O_P:
                    120:       write_cmo_p(s,(P)obj);
                    121:       break;
                    122:     case O_R:
                    123:       write_cmo_r(s,(R)obj);
                    124:       break;
                    125:     case O_DP:
                    126:       write_cmo_dp(s,(DP)obj);
                    127:       break;
                    128:     case O_LIST:
                    129:       write_cmo_list(s,(LIST)obj);
                    130:       break;
                    131:     case O_STR:
                    132:       write_cmo_string(s,(STRING)obj);
                    133:       break;
                    134:     case O_USINT:
                    135:       write_cmo_uint(s,(USINT)obj);
                    136:       break;
                    137:     case O_MATHCAP:
                    138:       write_cmo_mathcap(s,(MATHCAP)obj);
                    139:       break;
                    140:     case O_ERR:
                    141:       write_cmo_error(s,(ERR)obj);
                    142:       break;
                    143:     case O_BYTEARRAY:
                    144:       write_cmo_bytearray(s,(BYTEARRAY)obj);
                    145:       break;
                    146:     case O_VOID:
                    147:       r = ((USINT)obj)->body; write_int(s,&r);
                    148:       break;
                    149:     case O_QUOTE:
                    150:       fnodetotree(BDY((QUOTE)obj),&l);
                    151:       write_cmo_tree(s,l);
                    152:       break;
1.15      noro      153:     case O_MAT:
                    154:       write_cmo_matrix_as_list(s,(MAT)obj);
                    155:       break;
1.14      noro      156:     default:
                    157:       sprintf(errmsg, "write_cmo : id=%d not implemented.",OID(obj));
                    158:       error(errmsg);
                    159:       break;
                    160:   }
1.5       noro      161: }
                    162:
1.11      noro      163: int cmo_tag(Obj obj,int *tag)
1.5       noro      164: {
1.14      noro      165:   if ( !valid_as_cmo(obj) )
                    166:     return 0;
                    167:   if ( !obj ) {
                    168:     *tag = CMO_NULL;
                    169:     return 1;
                    170:   }
                    171:   switch ( OID(obj) ) {
                    172:     case O_N:
                    173:       switch ( NID((Num)obj) ) {
                    174:         case N_Q:
                    175:           *tag = DN((Q)obj) ? CMO_QQ : CMO_ZZ; break;
                    176:         case N_R:
                    177:           *tag = CMO_IEEE_DOUBLE_FLOAT; break;
1.13      noro      178:         case N_B:
1.14      noro      179:           *tag = CMO_BIGFLOAT; break;
1.20    ! noro      180:         case N_C:
        !           181:           *tag = CMO_COMPLEX; break;
1.14      noro      182:         default:
                    183:           return 0;
                    184:       }
                    185:       break;
                    186:     case O_P:
                    187:       *tag = CMO_RECURSIVE_POLYNOMIAL; break;
                    188:     case O_R:
                    189:       *tag = CMO_RATIONAL; break;
                    190:     case O_DP:
                    191:       *tag = CMO_DISTRIBUTED_POLYNOMIAL; break;
                    192:     case O_LIST:
                    193:       *tag = CMO_LIST; break;
                    194:     case O_STR:
                    195:       *tag = CMO_STRING; break;
                    196:     case O_USINT:
                    197:       *tag = CMO_INT32; break;
                    198:     case O_MATHCAP:
                    199:       *tag = CMO_MATHCAP; break;
                    200:     case O_ERR:
                    201:       *tag = CMO_ERROR2; break;
                    202:     case O_QUOTE:
                    203:       *tag = CMO_TREE; break; break;
                    204:     default:
                    205:       return 0;
                    206:   }
                    207:   return 1;
1.1       noro      208: }
                    209:
1.11      noro      210: void write_cmo_mathcap(FILE *s,MATHCAP mc)
1.1       noro      211: {
1.14      noro      212:   unsigned int r;
1.1       noro      213:
1.14      noro      214:   r = CMO_MATHCAP; write_int(s,&r);
                    215:   write_cmo(s,(Obj)BDY(mc));
1.1       noro      216: }
                    217:
1.11      noro      218: void write_cmo_uint(FILE *s,USINT ui)
1.1       noro      219: {
1.14      noro      220:   unsigned int r;
1.1       noro      221:
1.14      noro      222:   r = CMO_INT32; write_int(s,&r);
                    223:   r = ui->body; write_int(s,&r);
1.1       noro      224: }
                    225:
1.11      noro      226: void write_cmo_q(FILE *s,Q q)
1.1       noro      227: {
1.14      noro      228:   int r;
1.1       noro      229:
1.14      noro      230:   if ( q && DN(q) ) {
                    231:     r = CMO_QQ; write_int(s,&r);
                    232:     write_cmo_zz(s,SGN(q),NM(q));
                    233:     write_cmo_zz(s,1,DN(q));
                    234:   } else {
                    235:     r = CMO_ZZ; write_int(s,&r);
                    236:     write_cmo_zz(s,SGN(q),NM(q));
                    237:   }
1.1       noro      238: }
                    239:
1.11      noro      240: void write_cmo_real(FILE *s,Real real)
1.2       noro      241: {
1.14      noro      242:   unsigned int r;
                    243:   double dbl;
1.2       noro      244:
1.14      noro      245:   r = CMO_IEEE_DOUBLE_FLOAT; write_int(s,&r);
                    246:   dbl = real->body; write_double(s,&dbl);
1.2       noro      247: }
                    248:
1.13      noro      249: void write_cmo_bf(FILE *s,BF bf)
                    250: {
1.14      noro      251:   unsigned int r,u,l;
                    252:   int len,t,i;
                    253:   L exp;
                    254:   UL *ptr;
1.13      noro      255:
1.14      noro      256:   r = CMO_BIGFLOAT; write_int(s,&r);
1.13      noro      257:   write_int(s,&MPFR_SIGN(bf->body));
1.17      noro      258:   write_int(s,(unsigned int *)&MPFR_PREC(bf->body));
1.13      noro      259:   exp = MPFR_EXP(bf->body);
                    260:   write_int64(s,&exp);
1.14      noro      261:   len = MPFR_LIMB_SIZE(bf->body);
                    262: #if SIZEOF_LONG == 4
1.13      noro      263:   write_int(s,&len);
1.20    ! noro      264:   write_intarray(s,(int *)MPFR_MANT(bf->body),len);
1.13      noro      265: #else /* SIZEOF_LONG == 8 */
1.18      noro      266:   t = (MPFR_PREC(bf->body)+31)/32;
1.14      noro      267:   write_int(s,&t);
1.18      noro      268:   write_longarray(s,MPFR_MANT(bf->body),t);
1.13      noro      269: #endif
                    270: }
                    271:
1.11      noro      272: void write_cmo_zz(FILE *s,int sgn,N n)
1.1       noro      273: {
1.14      noro      274:   int l,bytes;
1.1       noro      275:
                    276: #if 1
1.14      noro      277:   l = PL(n);
                    278:   bytes = sgn*l;
                    279:   write_int(s,&bytes);
                    280:   write_intarray(s,BD(n),l);
1.1       noro      281: #else
1.14      noro      282:   l = PL(n); b = (unsigned int *)BD(n);
                    283:   bytes = sgn*4*l;
                    284:   write_int(s,&bytes);
                    285:   for ( i = l-1; i >= 0; i-- ) {
                    286:     t = b[i];
                    287:     c = t>>24; write_char(s,&c);
                    288:     c = (t>>16)&0xff; write_char(s,&c);
                    289:     c = (t>>8)&0xff; write_char(s,&c);
                    290:     c = t&0xff; write_char(s,&c);
                    291:   }
1.1       noro      292: #endif
                    293: }
                    294:
1.11      noro      295: void write_cmo_p(FILE *s,P p)
1.1       noro      296: {
1.14      noro      297:   int r,i;
                    298:   VL t,vl;
                    299:   char *namestr;
                    300:   STRING name;
                    301:
                    302:   r = CMO_RECURSIVE_POLYNOMIAL; write_int(s,&r);
                    303:   get_vars((Obj)p,&vl);
                    304:
                    305:   /* indeterminate list */
                    306:   r = CMO_LIST; write_int(s,&r);
                    307:   for ( t = vl, i = 0; t; t = NEXT(t), i++ );
                    308:   write_int(s,&i);
                    309:   r = CMO_INDETERMINATE;
                    310:   for ( t = vl; t; t = NEXT(t) ) {
                    311:     write_int(s,&r);
                    312: /*    localname_to_cmoname(NAME(t->v),&namestr); */
                    313:     namestr = NAME(t->v);
                    314:     MKSTR(name,namestr);
                    315:     write_cmo(s,(Obj)name);
                    316:   }
1.1       noro      317:
1.14      noro      318:   /* body */
                    319:   write_cmo_upoly(s,vl,p);
1.1       noro      320: }
                    321:
1.11      noro      322: void write_cmo_upoly(FILE *s,VL vl,P p)
1.1       noro      323: {
1.14      noro      324:   int r,i;
                    325:   V v;
                    326:   DCP dc,dct;
                    327:   VL vlt;
                    328:
                    329:   if ( NUM(p) )
                    330:     write_cmo(s,(Obj)p);
                    331:   else {
                    332:     r = CMO_UNIVARIATE_POLYNOMIAL; write_int(s,&r);
                    333:     v = VR(p);
                    334:     dc = DC(p);
                    335:     for ( i = 0, dct = dc; dct; dct = NEXT(dct), i++ );
                    336:     write_int(s,&i);
                    337:     for ( i = 0, vlt = vl; vlt->v != v; vlt = NEXT(vlt), i++ );
                    338:     write_int(s,&i);
                    339:     for ( dct = dc; dct; dct = NEXT(dct) ) {
                    340:       i = QTOS(DEG(dct)); write_int(s,&i);
                    341:       write_cmo_upoly(s,vl,COEF(dct));
                    342:     }
                    343:   }
1.1       noro      344: }
                    345:
1.11      noro      346: void write_cmo_r(FILE *s,R f)
1.1       noro      347: {
1.14      noro      348:   int r;
1.1       noro      349:
1.14      noro      350:   r = CMO_RATIONAL; write_int(s,&r);
                    351:   write_cmo(s,(Obj)NM(f));
                    352:   write_cmo(s,(Obj)DN(f));
1.1       noro      353: }
                    354:
1.20    ! noro      355: void write_cmo_complex(FILE *s,C f)
        !           356: {
        !           357:   int r;
        !           358:
        !           359:   r = CMO_COMPLEX; write_int(s,&r);
        !           360:   write_cmo(s,(Obj)f->r);
        !           361:   write_cmo(s,(Obj)f->i);
        !           362: }
        !           363:
1.11      noro      364: void write_cmo_dp(FILE *s,DP dp)
1.1       noro      365: {
1.14      noro      366:   int i,n,nv,r;
                    367:   MP m;
1.1       noro      368:
1.14      noro      369:   for ( n = 0, m = BDY(dp); m; m = NEXT(m), n++ );
                    370:   r = CMO_DISTRIBUTED_POLYNOMIAL; write_int(s,&r);
                    371:   r = n; write_int(s,&r);
                    372:   r = CMO_DMS_GENERIC; write_int(s,&r);
                    373:   nv = dp->nv;
                    374:   for ( i = 0, m = BDY(dp); i < n; i++, m = NEXT(m) )
                    375:     write_cmo_monomial(s,m,nv);
1.1       noro      376: }
                    377:
1.11      noro      378: void write_cmo_monomial(FILE *s,MP m,int n)
1.1       noro      379: {
1.14      noro      380:   int i,r;
                    381:   int *p;
1.1       noro      382:
1.14      noro      383:   r = CMO_MONOMIAL32; write_int(s,&r);
                    384:   write_int(s,&n);
                    385:   for ( i = 0, p = m->dl->d; i < n; i++ ) {
                    386:     write_int(s,p++);
                    387:   }
                    388:   write_cmo_q(s,(Q)m->c);
1.1       noro      389: }
                    390:
1.11      noro      391: void write_cmo_list(FILE *s,LIST list)
1.1       noro      392: {
1.14      noro      393:   NODE m;
                    394:   int i,n,r;
1.1       noro      395:
1.14      noro      396:   for ( n = 0, m = BDY(list); m; m = NEXT(m), n++ );
                    397:   r = CMO_LIST; write_int(s,&r);
                    398:   write_int(s,&n);
                    399:   for ( i = 0, m = BDY(list); i < n; i++, m = NEXT(m) )
                    400:     write_cmo(s,BDY(m));
1.1       noro      401: }
                    402:
1.11      noro      403: void write_cmo_string(FILE *s,STRING str)
1.1       noro      404: {
1.14      noro      405:   int r;
1.1       noro      406:
1.14      noro      407:   r = CMO_STRING; write_int(s,&r);
                    408:   savestr(s,BDY(str));
1.1       noro      409: }
                    410:
1.11      noro      411: void write_cmo_bytearray(FILE *s,BYTEARRAY array)
1.6       noro      412: {
1.14      noro      413:   int r;
1.6       noro      414:
1.14      noro      415:   r = CMO_DATUM; write_int(s,&r);
                    416:   write_int(s,&array->len);
                    417:   write_string(s,array->body,array->len);
1.6       noro      418: }
                    419:
1.11      noro      420: void write_cmo_error(FILE *s,ERR e)
1.1       noro      421: {
1.14      noro      422:   int r;
1.1       noro      423:
1.14      noro      424:   r = CMO_ERROR2; write_int(s,&r);
                    425:   write_cmo(s,BDY(e));
1.1       noro      426: }
                    427:
1.7       noro      428: /* XXX */
                    429:
1.10      noro      430: /*
                    431:  * BDY(l) = treenode
                    432:  * treenode = [property,(name,)arglist]
                    433:  * arglist = list of treenode
                    434:  */
                    435:
1.11      noro      436: void write_cmo_tree(FILE *s,LIST l)
1.7       noro      437: {
1.14      noro      438:   NODE n;
                    439:   int r;
                    440:   STRING prop,name,key;
                    441:
                    442:   /* (CMO_TREE (CMO_LIST,n,key1,attr1,...,keyn,attn),(CMO_LIST,m,arg1,...,argm)) */
                    443:   n = BDY(l);
                    444:   prop = (STRING)BDY(n);  n = NEXT(n);
                    445:   if ( !strcmp(BDY(prop),"internal") ) {
                    446:     write_cmo(s,(Obj)BDY(n));
                    447:   } else {
                    448:     if ( strcmp(BDY(prop),"list") ) {
                    449:       r = CMO_TREE; write_int(s,&r);
                    450:       name = (STRING)BDY(n);
                    451:       n = NEXT(n);
                    452:       /* function name */
                    453:       write_cmo(s,(Obj)name);
                    454:
                    455:       /* attribute list */
                    456:       r = CMO_LIST; write_int(s,&r);
                    457:       r = 2; write_int(s,&r);
                    458:       MKSTR(key,"asir");
                    459:       write_cmo(s,(Obj)key);
                    460:       write_cmo(s,(Obj)prop);
                    461:     }
                    462:
                    463:     /* argument list */
                    464:     r = CMO_LIST; write_int(s,&r);
                    465:     /* len = number of arguments */
                    466:     r = length(n); write_int(s,&r);
                    467:     while ( n ) {
                    468:       write_cmo_tree(s,BDY(n));
                    469:       n = NEXT(n);
                    470:     }
                    471:   }
1.7       noro      472: }
                    473:
1.15      noro      474: void write_cmo_matrix_as_list(FILE *s,MAT a)
                    475: {
                    476:   int i,j,r,row,col;
                    477:
                    478:   /* CMO_LIST row (CMO_LIST col a[0][0] ... a[0][col-1]) ... (CMO_LIST col a[row-1][0] ... a[row-1][col-1] */
                    479:   row = a->row; col = a->col;
                    480:   r = CMO_LIST;
                    481:   write_int(s,&r);
                    482:   write_int(s,&row);
                    483:   for ( i = 0; i < row; i++ ) {
                    484:     write_int(s,&r);
                    485:     write_int(s,&col);
                    486:     for ( j = 0; j < col; j++ )
                    487:       write_cmo(s,a->body[i][j]);
                    488:   }
                    489: }
                    490:
1.11      noro      491: void read_cmo(FILE *s,Obj *rp)
1.1       noro      492: {
1.14      noro      493:   int id;
                    494:   int sgn,dummy;
                    495:   Q q;
                    496:   N nm,dn;
                    497:   P p,pnm,pdn;
                    498:   Real real;
1.20    ! noro      499:   C c;
1.14      noro      500:   double dbl;
                    501:   STRING str;
                    502:   USINT t;
                    503:   DP dp;
                    504:   Obj obj;
                    505:   ERR e;
1.13      noro      506:   BF bf;
1.14      noro      507:   MATHCAP mc;
                    508:   BYTEARRAY array;
                    509:   LIST list;
                    510:
                    511:   read_int(s,&id);
                    512:   switch ( id ) {
                    513:   /* level 0 objects */
                    514:     case CMO_NULL:
                    515:       *rp = 0;
                    516:       break;
                    517:     case CMO_INT32:
                    518:       read_cmo_uint(s,&t); *rp = (Obj)t;
                    519:       break;
                    520:     case CMO_DATUM:
                    521:       loadbytearray(s,&array); *rp = (Obj)array;
                    522:       break;
                    523:     case CMO_STRING:
                    524:       loadstring(s,&str); *rp = (Obj)str;
                    525:       break;
                    526:     case CMO_MATHCAP:
                    527:       read_cmo(s,&obj); MKMATHCAP(mc,(LIST)obj);
                    528:       *rp = (Obj)mc;
                    529:       break;
                    530:     case CMO_ERROR:
                    531:       MKERR(e,0); *rp = (Obj)e;
                    532:       break;
                    533:     case CMO_ERROR2:
                    534:       read_cmo(s,&obj); MKERR(e,obj); *rp = (Obj)e;
                    535:       break;
                    536:   /* level 1 objects */
                    537:     case CMO_LIST:
                    538:       read_cmo_list(s,rp);
                    539:       break;
                    540:     case CMO_MONOMIAL32:
                    541:       read_cmo_monomial(s,&dp); *rp = (Obj)dp;
                    542:       break;
                    543:     case CMO_ZZ:
                    544:       read_cmo_zz(s,&sgn,&nm);
                    545:       NTOQ(nm,sgn,q); *rp = (Obj)q;
                    546:       break;
                    547:     case CMO_QQ:
                    548:       read_cmo_zz(s,&sgn,&nm);
                    549:       read_cmo_zz(s,&dummy,&dn);
                    550:       NDTOQ(nm,dn,sgn,q); *rp = (Obj)q;
                    551:       break;
                    552:     case CMO_IEEE_DOUBLE_FLOAT:
                    553:       read_double(s,&dbl); MKReal(dbl,real); *rp = (Obj)real;
                    554:       break;
                    555:     case CMO_BIGFLOAT:
                    556:       read_cmo_bf(s,&bf); *rp = (Obj)bf;
                    557:       break;
1.20    ! noro      558:     case CMO_COMPLEX:
        !           559:       NEWC(c);
        !           560:       read_cmo(s,(Obj *)&c->r);
        !           561:       read_cmo(s,(Obj *)&c->i);
        !           562:       *rp = (Obj)c;
        !           563:       break;
1.14      noro      564:     case CMO_DISTRIBUTED_POLYNOMIAL:
                    565:       read_cmo_dp(s,&dp); *rp = (Obj)dp;
                    566:       break;
                    567:     case CMO_RECURSIVE_POLYNOMIAL:
                    568:       read_cmo_p(s,&p); *rp = (Obj)p;
                    569:       break;
                    570:     case CMO_UNIVARIATE_POLYNOMIAL:
                    571:       read_cmo_upoly(s,&p); *rp = (Obj)p;
                    572:       break;
                    573:     case CMO_INDETERMINATE:
                    574:       read_cmo(s,rp);
                    575:       break;
                    576:     case CMO_RATIONAL:
                    577:       read_cmo(s,&obj); pnm = (P)obj;
                    578:       read_cmo(s,&obj); pdn = (P)obj;
                    579:       divr(CO,(Obj)pnm,(Obj)pdn,rp);
                    580:       break;
                    581:     case CMO_ZERO:
                    582:       *rp = 0;
                    583:       break;
                    584:     case CMO_DMS_OF_N_VARIABLES:
                    585:       read_cmo(s,rp);
                    586:       break;
                    587:     case CMO_RING_BY_NAME:
                    588:       read_cmo(s,rp);
                    589:       break;
                    590:     case CMO_TREE:
                    591:       read_cmo_tree_as_list(s,&list);
1.10      noro      592: #if 0
1.14      noro      593:       treetofnode(list,&fn);
                    594:       MKQUOTE(quote,fn);
                    595:       *rp = (Obj)quote;
1.10      noro      596: #else
1.14      noro      597:       *rp = (Obj)list;
1.10      noro      598: #endif
1.14      noro      599:       break;
                    600:     default:
                    601:       MKUSINT(t,id);
                    602:       t->id = O_VOID;
                    603:       *rp = (Obj)t;
                    604:       break;
                    605:   }
1.1       noro      606: }
                    607:
1.11      noro      608: void read_cmo_uint(FILE *s,USINT *rp)
1.1       noro      609: {
1.14      noro      610:   unsigned int body;
1.1       noro      611:
1.14      noro      612:   read_int(s,&body);
                    613:   MKUSINT(*rp,body);
1.1       noro      614: }
                    615:
1.11      noro      616: void read_cmo_zz(FILE *s,int *sgn,N *rp)
1.1       noro      617: {
1.14      noro      618:   int l;
                    619:   N n;
1.1       noro      620:
1.14      noro      621:   read_int(s,&l);
                    622:   if ( l == 0 ) {
                    623:     *sgn = 0;
                    624:     *rp = 0;
                    625:     return;
                    626:   }
                    627:   if ( l < 0 ) {
                    628:     *sgn = -1; l = -l;
                    629:   } else
                    630:     *sgn = 1;
1.1       noro      631: #if 1
1.14      noro      632:   *rp = n = NALLOC(l); PL(n) = l;
                    633:   read_intarray(s,BD(n),l);
1.1       noro      634: #else
1.14      noro      635:   words = (l+3)/4;
                    636:   *rp = n = NALLOC(words); PL(n) = words; b = BD(n);
                    637:   h = 0;
                    638:   switch ( l % 4 ) {
                    639:     case 0:
                    640:       read_char(s,&c); h = c;
                    641:     case 3:
                    642:       read_char(s,&c); h = (h<<8)|c;
                    643:     case 2:
                    644:       read_char(s,&c); h = (h<<8)|c;
                    645:     case 1:
                    646:       read_char(s,&c); h = (h<<8)|c;
                    647:   }
                    648:   b[words-1] = h;
                    649:   for ( i = words-2; i >= 0; i-- ) {
                    650:     read_char(s,&c); h = c;
                    651:     read_char(s,&c); h = (h<<8)|c;
                    652:     read_char(s,&c); h = (h<<8)|c;
                    653:     read_char(s,&c); h = (h<<8)|c;
                    654:     b[i] = h;
                    655:   }
1.1       noro      656: #endif
                    657: }
                    658:
1.13      noro      659: void read_cmo_bf(FILE *s,BF *bf)
                    660: {
1.14      noro      661:   BF r;
                    662:   int sgn,prec,len,i;
                    663:   unsigned int u,l;
                    664:   UL *ptr;
                    665:   L exp;
1.13      noro      666:
                    667:   NEWBF(r);
                    668:   read_int(s,&sgn);
                    669:   read_int(s,&prec);
                    670:   read_int64(s,&exp);
                    671:   read_int(s,&len);
                    672:   mpfr_init2(r->body,prec);
                    673:   MPFR_SIGN(r->body) = sgn;
                    674:   MPFR_EXP(r->body) = exp;
                    675: #if SIZEOF_LONG == 4
1.20    ! noro      676:   read_intarray(s,(int *)MPFR_MANT(r->body),len);
1.13      noro      677: #else /* SIZEOF_LONG == 8 */
1.17      noro      678:   read_longarray(s,MPFR_MANT(r->body),len);
1.13      noro      679: #endif
                    680:   *bf = r;
                    681: }
                    682:
1.11      noro      683: void read_cmo_list(FILE *s,Obj *rp)
1.1       noro      684: {
1.14      noro      685:   int len;
                    686:   Obj *w;
                    687:   int i;
                    688:   NODE n0,n1;
                    689:   LIST list;
                    690:
                    691:   read_int(s,&len);
                    692:   w = (Obj *)ALLOCA(len*sizeof(Obj));
                    693:   for ( i = 0; i < len; i++ )
                    694:     read_cmo(s,&w[i]);
                    695:   for ( i = len-1, n0 = 0; i >= 0; i-- ) {
                    696:     MKNODE(n1,w[i],n0); n0 = n1;
                    697:   }
                    698:   MKLIST(list,n0);
                    699:   *rp = (Obj)list;
1.1       noro      700: }
                    701:
1.11      noro      702: void read_cmo_dp(FILE *s,DP *rp)
1.1       noro      703: {
1.14      noro      704:   int len;
                    705:   int i;
                    706:   MP mp0,mp;
                    707:   int nv,d;
                    708:   DP dp;
                    709:   Obj obj;
                    710:
                    711:   read_int(s,&len);
                    712:   /* skip the ring definition */
                    713:   read_cmo(s,&obj);
                    714:   for ( mp0 = 0, i = 0, d = 0; i < len; i++ ) {
                    715:     read_cmo(s,&obj); dp = (DP)obj;
                    716:     if ( !mp0 ) {
                    717:       nv = dp->nv;
                    718:       mp0 = dp->body;
                    719:       mp = mp0;
                    720:     } else {
                    721:       NEXT(mp) = dp->body;
                    722:       mp = NEXT(mp);
                    723:     }
                    724:     d = MAX(d,dp->sugar);
                    725:   }
                    726:   MKDP(nv,mp0,dp);
                    727:   dp->sugar = d; *rp = dp;
1.1       noro      728: }
                    729:
1.11      noro      730: void read_cmo_monomial(FILE *s,DP *rp)
1.1       noro      731: {
1.14      noro      732:   Obj obj;
                    733:   MP m;
                    734:   DP dp;
                    735:   int i,sugar,n;
                    736:   DL dl;
                    737:
                    738:   read_int(s,&n);
                    739:   NEWMP(m); NEWDL(dl,n); m->dl = dl;
                    740:   read_intarray(s,dl->d,n);
                    741:   for ( sugar = 0, i = 0; i < n; i++ )
                    742:     sugar += dl->d[i];
                    743:   dl->td = sugar;
                    744:   read_cmo(s,&obj); m->c = (P)obj;
                    745:   NEXT(m) = 0; MKDP(n,m,dp); dp->sugar = sugar; *rp = dp;
1.1       noro      746: }
                    747:
                    748: static V *remote_vtab;
                    749:
1.11      noro      750: void read_cmo_p(FILE *s,P *rp)
1.1       noro      751: {
1.14      noro      752:   Obj obj;
                    753:   LIST vlist;
                    754:   int nv,i;
                    755:   V *vtab;
                    756:   V v1,v2;
                    757:   NODE t;
                    758:   P v,p;
                    759:   VL tvl,rvl;
                    760:   char *name;
                    761:
                    762:   read_cmo(s,&obj); vlist = (LIST)obj;
                    763:   nv = length(BDY(vlist));
                    764:   vtab = (V *)ALLOCA(nv*sizeof(V));
                    765:   for ( i = 0, t = BDY(vlist); i < nv; t = NEXT(t), i++ ) {
                    766: /*    cmoname_to_localname(BDY((STRING)BDY(t)),&name); */
                    767:     name = BDY((STRING)BDY(t));
                    768:     makevar(name,&v); vtab[i] = VR(v);
                    769:   }
                    770:   remote_vtab = vtab;
                    771:   read_cmo(s,&obj); p = (P)obj;
                    772:   for ( i = 0; i < nv-1; i++ ) {
                    773:     v1 = vtab[i]; v2 = vtab[i+1];
                    774:     for ( tvl = CO; tvl->v != v1 && tvl->v != v2; tvl = NEXT(tvl) );
                    775:     if ( tvl->v == v2 )
                    776:       break;
                    777:   }
                    778:   if ( i < nv-1 ) {
                    779:      for ( i = nv-1, rvl = 0; i >= 0; i-- ) {
                    780:       NEWVL(tvl); tvl->v = vtab[i]; NEXT(tvl) = rvl; rvl = tvl;
                    781:      }
                    782:      reorderp(CO,rvl,p,rp);
                    783:   } else
                    784:     *rp = p;
1.1       noro      785: }
                    786:
1.11      noro      787: void read_cmo_upoly(FILE *s,P *rp)
1.1       noro      788: {
1.14      noro      789:   int n,ind,i,d;
                    790:   Obj obj;
                    791:   P c;
                    792:   Q q;
                    793:   DCP dc0,dc;
                    794:
                    795:   read_int(s,&n);
                    796:   read_int(s,&ind);
                    797:   for ( i = 0, dc0 = 0; i < n; i++ ) {
                    798:     read_int(s,&d);
                    799:     read_cmo(s,&obj); c = (P)obj;
                    800:     if ( c ) {
                    801:       if ( OID(c) == O_USINT ) {
                    802:         UTOQ(((USINT)c)->body,q); c = (P)q;
                    803:       }
                    804:       NEXTDC(dc0,dc);
                    805:       STOQ(d,q);
                    806:       dc->c = c; dc->d = q;
                    807:     }
                    808:   }
                    809:   if ( dc0 )
                    810:     NEXT(dc) = 0;
                    811:   MKP(remote_vtab[ind],dc0,*rp);
1.7       noro      812: }
                    813:
                    814: /* XXX */
                    815:
                    816: extern struct oARF arf[];
                    817:
                    818: struct operator_tab {
1.14      noro      819:   char *name;
                    820:   fid id;
                    821:   ARF arf;
                    822:   cid cid;
1.7       noro      823: };
                    824:
                    825: static struct operator_tab optab[] = {
1.14      noro      826:   {"+",I_BOP,&arf[0],0}, /* XXX */
                    827:   {"-",I_BOP,&arf[1],0},
                    828:   {"*",I_BOP,&arf[2],0},
                    829:   {"/",I_BOP,&arf[3],0},
                    830:   {"%",I_BOP,&arf[4],0},
                    831:   {"^",I_BOP,&arf[5],0},
                    832:   {"==",I_COP,0,C_EQ},
                    833:   {"!=",I_COP,0,C_NE},
                    834:   {"<",I_COP,0,C_LT},
                    835:   {"<=",I_COP,0,C_LE},
                    836:   {">",I_COP,0,C_GT},
                    837:   {">=",I_COP,0,C_GE},
                    838:   {"&&",I_AND,0,0},
                    839:   {"||",I_OR,0,0},
                    840:   {"!",I_NOT,0,0},
1.7       noro      841: };
                    842:
                    843: static int optab_len = sizeof(optab)/sizeof(struct operator_tab);
                    844:
1.10      noro      845: #if 0
                    846: /* old code */
1.11      noro      847: void read_cmo_tree(s,rp)
1.7       noro      848: FILE *s;
1.8       noro      849: FNODE *rp;
1.7       noro      850: {
1.14      noro      851:   int r,i,n;
                    852:   char *opname;
                    853:   STRING name,cd;
                    854:   int op;
                    855:   pointer *arg;
                    856:   QUOTE quote;
                    857:   FNODE fn;
                    858:   NODE t,t1;
                    859:   fid id;
                    860:   Obj expr;
                    861:   FUNC func;
                    862:
                    863:   read_cmo(s,&name);
                    864:   read_cmo(s,&attr);
                    865:   for ( i = 0; i < optab_len; i++ )
                    866:     if ( !strcmp(optab[i].name,BDY(name)) )
                    867:       break;
                    868:   if ( i == optab_len ) {
                    869:     /* may be a function name */
                    870:     n = read_cmo_tree_arg(s,&arg);
                    871:     for ( i = n-1, t = 0; i >= 0; i-- ) {
                    872:       MKNODE(t1,arg[i],t); t = t1;
                    873:     }
                    874:     searchf(sysf,BDY(name),&func);
                    875:     if ( !func )
                    876:       searchf(ubinf,BDY(name),&func);
                    877:     if ( !func )
                    878:       searchpf(BDY(name),&func);
                    879:     if ( !func )
                    880:       searchf(usrf,BDY(name),&func);
                    881:     if ( !func )
                    882:       appenduf(BDY(name),&func);
                    883:     *rp = mkfnode(2,I_FUNC,func,mkfnode(1,I_LIST,t));
                    884:   } else {
                    885:     opname = optab[i].name;
                    886:     id = optab[i].id;
                    887:     switch ( id ) {
                    888:       case I_BOP:
                    889:         read_cmo_tree_arg(s,&arg);
                    890:         *rp = mkfnode(3,I_BOP,optab[i].arf,arg[0],arg[1]);
                    891:         return;
                    892:       case I_COP:
                    893:         read_cmo_tree_arg(s,&arg);
                    894:         *rp = mkfnode(3,I_COP,optab[i].cid,arg[0],arg[0]);
                    895:         return;
                    896:       case I_AND:
                    897:         read_cmo_tree_arg(s,&arg);
                    898:         *rp = mkfnode(2,I_AND,arg[0],arg[1]);
                    899:         return;
                    900:       case I_OR:
                    901:         read_cmo_tree_arg(s,&arg);
                    902:         *rp = mkfnode(2,I_OR,arg[0],arg[1]);
                    903:         return;
                    904:       case I_NOT:
                    905:         read_cmo_tree_arg(s,&arg);
                    906:         *rp = mkfnode(1,I_OR,arg[0]);
                    907:         return;
                    908:     }
                    909:   }
1.8       noro      910: }
                    911:
                    912: int read_cmo_tree_arg(s,argp)
                    913: FILE *s;
                    914: pointer **argp;
                    915: {
1.14      noro      916:   int id,n,i;
                    917:   pointer *ap;
                    918:   Obj t;
                    919:
                    920:   read_int(s,&id); /* id = CMO_LIST */
                    921:   read_int(s,&n); /* n = the number of args */
                    922:   *argp = ap = (pointer *) MALLOC(n*sizeof(pointer));
                    923:   for ( i = 0; i < n; i++ ) {
                    924:     read_cmo(s,&t);
                    925:     if ( !t || (OID(t) != O_QUOTE) )
                    926:       ap[i] = mkfnode(1,I_FORMULA,t);
                    927:     else
                    928:       ap[i] = BDY((QUOTE)t);
                    929:   }
                    930:   return n;
1.1       noro      931: }
1.10      noro      932: #else
1.11      noro      933: void read_cmo_tree_as_list(FILE *s,LIST *rp)
1.10      noro      934: {
1.14      noro      935:   Obj obj;
                    936:   STRING name;
                    937:   LIST attr,args;
                    938:   NODE t0,t1;
                    939:
                    940:   read_cmo(s,&obj); name = (STRING)obj;
                    941:   read_cmo(s,&obj); attr = (LIST)obj;
                    942:   read_cmo(s,&obj); args = (LIST)obj;
                    943:   MKNODE(t1,name,BDY(args));
                    944:   MKNODE(t0,attr,t1);
                    945:   MKLIST(*rp,t0);
1.10      noro      946: }
                    947: #endif
1.1       noro      948:
1.11      noro      949: void localname_to_cmoname(char *a,char **b)
1.1       noro      950: {
1.14      noro      951:   int l;
                    952:   char *t;
1.1       noro      953:
1.14      noro      954:   l = strlen(a);
                    955:   if ( l >= 2 && a[0] == '@' && isupper(a[1]) ) {
                    956:     t = *b = (char *)MALLOC_ATOMIC(l);
                    957:     strcpy(t,a+1);
                    958:   } else {
                    959:     t = *b = (char *)MALLOC_ATOMIC(l+1);
                    960:     strcpy(t,a);
                    961:   }
1.1       noro      962: }
                    963:
1.11      noro      964: void cmoname_to_localname(char *a,char **b)
1.1       noro      965: {
1.14      noro      966:   int l;
                    967:   char *t;
1.1       noro      968:
1.14      noro      969:   l = strlen(a);
                    970:   if ( isupper(a[0]) ) {
                    971:     t = *b = (char *)MALLOC_ATOMIC(l+2);
                    972:     strcpy(t+1,a);
                    973:     t[0] = '@';
                    974:   } else {
                    975:     t = *b = (char *)MALLOC_ATOMIC(l+1);
                    976:     strcpy(t,a);
                    977:   }
1.1       noro      978: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>