[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.13

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

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