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

Annotation of OpenXM_contrib2/asir2018/io/cpexpr.c, Revision 1.7

1.1       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
                     26:  * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
                     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.7     ! noro       47:  * $OpenXM: OpenXM_contrib2/asir2018/io/cpexpr.c,v 1.6 2021/03/25 22:09:25 noro Exp $
1.1       noro       48: */
                     49: #include "ca.h"
                     50: #include "parse.h"
                     51: #include "al.h"
                     52: #include "base.h"
                     53:
                     54: #ifndef CPRINT
                     55: #define CPRINT
                     56: #endif
                     57:
                     58: extern int hex_output,fortran_output,double_output,real_digit;
                     59: extern int hideargs,outputstyle;
                     60: static int total_length;
                     61:
                     62: #define TAIL
                     63: #define PUTS(s) (total_length+=strlen(s))
                     64: #define PRINTZ length_z
                     65: #define PRINTMPZ length_mpz
                     66: #define PRINTBF length_bf
                     67: #define PRINTCPLX length_cplx
                     68: #define PRINTLM length_lm
                     69: #define PRINTUP2 length_up2
                     70: #define PRINTV length_v
                     71: #define PRINTEXPR length_expr
                     72: #define PRINTNUM length_num
                     73: #define PRINTP length_p
                     74: #define PRINTR length_r
                     75: #define PRINTLIST length_list
                     76: #define PRINTVECT length_vect
                     77: #define PRINTMAT length_mat
                     78: #define PRINTSTR length_str
                     79: #define PRINTCOMP length_comp
                     80: #define PRINTDP length_dp
                     81: #define PRINTDPM length_dpm
                     82: #define PRINTUI length_ui
                     83: #define PRINTGF2MAT length_gf2mat
                     84: #define PRINTGFMMAT length_gfmmat
                     85: #define PRINTBYTEARRAY length_bytearray
                     86: #define PRINTQUOTE length_QUOTE
                     87: #define PRINTQUOTEARG length_QUOTEARG
                     88: #define PRINTSYMBOL length_SYMBOL
                     89: #define PRINTRANGE length_RANGE
                     90: #define PRINTTB length_TB
                     91: #define PRINTDPV length_DPV
                     92: #define PRINTNBP length_nbp
                     93: #define PRINTERR length_err
                     94: #define PRINTLF length_lf
                     95: #define PRINTLOP length_lop
                     96: #define PRINTFOP length_fop
                     97: #define PRINTEOP length_eop
                     98: #define PRINTQOP length_qop
                     99: #define PRINTUP length_up
                    100: #define PRINTUM length_um
                    101: #define PRINTSF length_sf
                    102: #define PRINTFARGS length_fargs
                    103: #define PRINTFNODENODE length_fnodenode
                    104: #define PRINTFNODE length_fnode
                    105:
                    106: #include "pexpr_body.c"
                    107:
                    108: /* special functions for estimating length */
                    109:
                    110: static int total_length;
                    111:
                    112: int estimate_length(VL vl,pointer p)
                    113: {
                    114:   total_length = 0;
                    115:   PRINTEXPR(vl,p);
                    116:   return total_length;
                    117: }
                    118:
                    119: void PRINTBF(BF a)
                    120: {
                    121:   char *str;
                    122:   int dprec;
                    123:   char fbuf[BUFSIZ];
                    124:
                    125:   dprec = a->body->_mpfr_prec*0.30103;
                    126:   sprintf(fbuf,"%%.%dR%c",dprec,(double_output==1)?'f':(double_output==2)?'e':'g');
                    127:   mpfr_asprintf(&str,fbuf,a->body);
                    128:   total_length += strlen(str);
                    129:   mpfr_free_str(str);
                    130: }
                    131:
                    132: void PRINTNUM(Num q)
                    133: {
                    134:   DAlg d;
                    135:   DP nm;
                    136:   Z dn,den,num;
                    137:
                    138:   if ( !q ) {
                    139:     PUTS("0");
                    140:     return;
                    141:   }
                    142:   switch ( NID(q) ) {
                    143:     case N_Q:
                    144:       nmq((Q)q,&num); PRINTZ(num);
                    145:       if ( !INT((Q)q) ) {
1.3       noro      146:         PUTS("/"); dnq((Q)q,&den); PRINTZ(den);
1.1       noro      147:       }
                    148:       break;
                    149:     case N_R:
                    150:       if ( double_output )
                    151:         total_length += 400+real_digit; /* XXX */
                    152:       else
                    153:         total_length += 20+real_digit; /* XXX */
                    154:       break;
                    155:     case N_A:
                    156:       PUTS("("); PRINTR(ALG,(R)BDY((Alg)q)); PUTS(")");
                    157:       break;
                    158:     case N_B:
                    159:       PRINTBF((BF)q); break;
                    160:     case N_C:
                    161:       PRINTCPLX((C)q); break;
                    162:     case N_M:
                    163:       total_length += 11; /* XXX */
                    164:       break;
                    165:     case N_LM:
                    166:       PRINTMPZ(BDY((LM)q)); break;
                    167:     case N_GF2N:
                    168:       PRINTUP2(((GF2N)q)->body);
                    169:       break;
                    170:     case N_GFPN:
                    171:       PRINTUP((UP)(((GFPN)q)->body));
                    172:       break;
                    173:     case N_GFS:
                    174:       total_length += 13; /* XXX */
                    175:       break;
                    176:     case N_GFSN:
                    177:       PRINTUM(BDY((GFSN)q));
                    178:       break;
                    179:     case N_DA:
                    180:       d = (DAlg)q;
                    181:       nm = d->nm;
                    182:       dn = d->dn;
                    183:       if ( sgnq((Q)dn) == -1 ) PUTS("-");
                    184:       PUTS("(");
                    185:       PRINTDP(CO,((DAlg)q)->nm);
                    186:       PUTS(")");
                    187:       if ( !UNIQ((Q)dn) ) {
                    188:         PUTS("/");
                    189:         PRINTZ(dn);
                    190:       }
                    191:       break;
                    192:     default:
                    193:       break;
                    194:   }
                    195: }
                    196:
                    197: void PRINTV(VL vl,V v)
                    198: {
                    199:   PF pf;
                    200:   PFAD ad;
                    201:   int i;
                    202:
                    203:   if ( NAME(v) )
                    204:     PUTS(NAME(v));
                    205:   else if ( (vid)v->attr == V_PF ) {
                    206:     pf = ((PFINS)v->priv)->pf; ad = ((PFINS)v->priv)->ad;
                    207:     if ( !strcmp(NAME(pf),"pow") ) {
                    208:       PUTS("(("); PRINTR(vl,(R)ad[0].arg); PUTS(")"); PRINTHAT; PUTS("(");
                    209:       PRINTR(vl,(R)ad[1].arg); PUTS("))");
                    210:     } else if ( !pf->argc )
                    211:       PUTS(NAME(pf));
                    212:     else {
                    213:       if ( hideargs ) {
                    214:         for ( i = 0; i < pf->argc; i++ )
                    215:           if ( ad[i].d )
                    216:             break;
                    217:         if ( i < pf->argc ) {
                    218:           PUTS(NAME(pf));
                    219:           total_length += 11; /* XXX */
                    220:           for ( i = 1; i < pf->argc; i++ ) {
                    221:             total_length += 11; /* XXX */
                    222:           }
                    223:           PUTS("}");
                    224:         } else {
                    225:           PUTS(NAME(pf));
                    226:           total_length += 1; /* XXX */
                    227:         }
                    228:       } else {
                    229:         for ( i = 0; i < pf->argc; i++ )
                    230:           if ( ad[i].d )
                    231:             break;
                    232:         if ( i < pf->argc ) {
                    233:           PUTS(NAME(pf));
                    234:           total_length += 11; /* XXX */
                    235:           for ( i = 1; i < pf->argc; i++ ) {
                    236:             total_length += 11; /* XXX */
                    237:           }
                    238:           PUTS(")(");
                    239:         } else {
                    240:           PUTS(NAME(pf));
                    241:           total_length += 1; /* XXX */
                    242:         }
                    243:         PRINTR(vl,(R)ad[0].arg);
                    244:         for ( i = 1; i < pf->argc; i++ ) {
                    245:           PUTS(","); PRINTR(vl,(R)ad[i].arg);
                    246:         }
                    247:         PUTS(")");
                    248:       }
                    249:     }
                    250:   }
                    251: }
                    252:
                    253: void PRINTMPZ(mpz_t n)
                    254: {
1.7     ! noro      255:   if ( hex_output == 1 )
1.6       noro      256:     total_length += mpz_sizeinbase(n,16)+3;
1.7     ! noro      257:   else if ( hex_output == 2 )
        !           258:     total_length += mpz_sizeinbase(n,2)+3;
1.1       noro      259:   else
1.6       noro      260:     total_length += mpz_sizeinbase(n,10)+1;
1.1       noro      261: }
                    262:
                    263: void PRINTZ(Z n)
                    264: {
                    265:   if ( !n )
                    266:     PUTS("0");
                    267:   else
                    268:     PRINTMPZ(BDY(n));
                    269: }
                    270:
                    271: void PRINTSTR(STRING str)
                    272: {
                    273:   char *p;
                    274:
                    275:   for ( p = BDY(str); *p; p++ )
                    276:     if ( *p == '"' )
                    277:       PUTS("\"");
                    278:     else {
                    279:       total_length += 1;
                    280:     }
                    281: }
                    282:
                    283: void PRINTDP(VL vl,DP d)
                    284: {
                    285:   int n,i;
                    286:   MP m;
                    287:   DL dl;
                    288:
                    289:   for ( n = d->nv, m = BDY(d); m; m = NEXT(m) ) {
                    290:     PUTS("("); PRINTEXPR(vl,(pointer)m->c); PUTS(")*<<");
                    291:     for ( i = 0, dl = m->dl; i < n-1; i++ ) {
                    292:       total_length += 11;
                    293:     }
                    294:     total_length += 10;
                    295:     PUTS(">>");
                    296:     if ( NEXT(m) )
                    297:       PUTS("+");
                    298:   }
                    299: }
                    300:
                    301: void PRINTDPM(VL vl,DPM d)
                    302: {
                    303:   int n,i;
                    304:   DMM m;
                    305:   DL dl;
                    306:
                    307:   for ( n = d->nv, m = BDY(d); m; m = NEXT(m) ) {
                    308:     PUTS("("); PRINTEXPR(vl,(pointer)m->c); PUTS(")*<<");
                    309:     for ( i = 0, dl = m->dl; i < n-1; i++ ) {
                    310:       total_length += 11;
                    311:     }
                    312:     total_length += 10;
                    313:     total_length += 11; /* for ':pos' */
                    314:     PUTS(">>");
                    315:     if ( NEXT(m) )
                    316:       PUTS("+");
                    317:   }
                    318: }
                    319:
                    320:
                    321: void PRINTUI(VL vl,USINT u)
                    322: {
                    323:   total_length += 10;
                    324: }
                    325:
                    326: void PRINTGFMMAT(VL vl,GFMMAT mat)
                    327: {
                    328:   int row,col,i,j;
                    329:   unsigned int **b;
                    330:
                    331:   row = mat->row;
                    332:   col = mat->col;
                    333:   b = mat->body;
                    334:   for ( i = 0; i < row; i++ ) {
                    335:     PUTS("[");
                    336:     for ( j = 0; j < col; j++ ) {
                    337:       total_length += 10; /* XXX */
                    338:     }
                    339:     PUTS("]\n");
                    340:   }
                    341: }
                    342:
                    343: void PRINTBYTEARRAY(VL vl,BYTEARRAY array)
                    344: {
                    345:   /* |xx xx ... xx| */
                    346:   total_length += 1+3*array->len;
                    347: }
                    348:
                    349: extern int print_quote;
                    350:
                    351: void PRINTQUOTE(VL vl,QUOTE quote)
                    352: {
                    353:   LIST list;
                    354:
                    355:   if ( print_quote == 2 ) {
                    356:     PRINTFNODE(BDY(quote),0);
                    357:   } else if ( print_quote == 1 ) {
                    358:     /* XXX */
1.4       noro      359:     fnodetotree(BDY(quote),quote->pvs,&list);
1.1       noro      360:     PRINTEXPR(vl,(Obj)list);
                    361:   } else {
                    362:     /* <...quoted...> */
                    363:     total_length += 20;
                    364:   }
                    365: }
                    366:
                    367: void PRINTQUOTEARG(VL vl,QUOTEARG quote)
                    368: {
                    369:   /* XXX */
                    370:   /* <...quoted...> */
                    371:   total_length += 20;
                    372: }
                    373:
                    374: void PRINTSYMBOL(SYMBOL sym)
                    375: {
                    376:   total_length += strlen(sym->name);
                    377: }
                    378:
                    379: void PRINTTB(VL vl,TB p)
                    380: {
                    381:   int i;
                    382:
                    383:   for ( i = 0; i < p->next; i++ ) {
                    384:     total_length += strlen(p->body[i]);
                    385:   }
                    386: }
                    387:
                    388: void PRINTUP2(UP2 p)
                    389: {
                    390:   int d,i;
                    391:
                    392:   if ( !p ) {
                    393:     PUTS("0");
                    394:   } else if ( hex_output ) {
                    395:     total_length += p->w*8;
                    396:   } else {
                    397:     d = degup2(p);
                    398:     PUTS("(");
                    399:     if ( !d ) {
                    400:       PUTS("1");
                    401:     } else if ( d == 1 ) {
                    402:       PUTS("@");
                    403:     } else {
                    404:       PRINTHAT;
                    405:       total_length += 11;
                    406:     }
                    407:     for ( i = d-1; i >= 0; i-- ) {
1.5       noro      408:       if ( p->b[i/BSH] & (1<<(i%BSH)) ) {
1.1       noro      409:         if ( !i ) {
                    410:           PUTS("+1");
                    411:         } else if ( i == 1 ) {
                    412:           PUTS("+@");
                    413:         } else {
                    414:           PRINTHAT;
                    415:           total_length += 12;
                    416:         }
1.5       noro      417:       }
1.1       noro      418:     }
                    419:     PUTS(")");
                    420:   }
                    421: }
                    422:
                    423: void PRINTQOP(VL vl,F f)
                    424: {
                    425:   char *op;
                    426:
                    427:   op = FOP(f)==AL_EX?"ex":"all";
                    428:   PUTS(op); PUTS(NAME(FQVR(f)));
                    429:   total_length += 2;
                    430:   PRINTEXPR(vl,(Obj)FQMAT(f)); PUTS(")");
                    431: }
                    432:
                    433: void PRINTUP(UP n)
                    434: {
                    435:   int i,d;
                    436:
                    437:   if ( !n )
                    438:     PUTS("0");
                    439:   else if ( !n->d )
                    440:     PRINTNUM(n->c[0]);
                    441:   else {
                    442:     d = n->d;
                    443:     PUTS("(");
                    444:     if ( !d ) {
                    445:       PRINTNUM(n->c[d]);
                    446:     } else if ( d == 1 ) {
                    447:       PRINTNUM(n->c[d]);
                    448:       PUTS("*@p");
                    449:     } else {
                    450:       PRINTNUM(n->c[d]);
                    451:       PRINTHAT;
                    452:       total_length += 13;
                    453:     }
                    454:     for ( i = d-1; i >= 0; i-- ) {
                    455:       if ( n->c[i] ) {
                    456:         PUTS("+("); PRINTNUM(n->c[i]); PUTS(")");
                    457:         if ( i >= 2 ) {
                    458:           PRINTHAT;
                    459:           total_length += 13;
                    460:         } else if ( i == 1 )
                    461:           PUTS("*@p");
                    462:       }
                    463:     }
                    464:     PUTS(")");
                    465:   }
                    466: }
                    467:
                    468: void PRINTUM(UM n)
                    469: {
                    470:   int i,d;
                    471:
                    472:   if ( !n )
                    473:     PUTS("0");
                    474:   else if ( !n->d )
                    475:     PRINTSF(n->c[0]);
                    476:   else {
                    477:     d = n->d;
                    478:     PUTS("(");
                    479:     if ( !d ) {
                    480:       PRINTSF(n->c[d]);
                    481:     } else if ( d == 1 ) {
                    482:       PRINTSF(n->c[d]);
                    483:       PUTS("*@s");
                    484:     } else {
                    485:       PRINTSF(n->c[d]);
                    486:       PUTS("*@s"); PRINTHAT; total_length += 13;
                    487:     }
                    488:     for ( i = d-1; i >= 0; i-- ) {
                    489:       if ( n->c[i] ) {
                    490:         PUTS("+("); PRINTSF(n->c[i]); PUTS(")");
                    491:         if ( i >= 2 ) {
                    492:           PUTS("*@s"); PRINTHAT; total_length += 13;
                    493:         } else if ( i == 1 )
                    494:           PUTS("*@s");
                    495:       }
                    496:     }
                    497:     PUTS(")");
                    498:   }
                    499: }
                    500:
                    501: void PRINTNBP(VL vl,NBP p)
                    502: {
                    503:   NODE t;
                    504:   NBM m;
                    505:   int d,i;
                    506:   unsigned int *b;
                    507:   if ( !p ) PUTS("0");
                    508:   else {
                    509:     for ( t = BDY(p); t; t = NEXT(t) ) {
                    510:       m = (NBM)BDY(t);
                    511:       PRINTEXPR(vl,(Obj)m->c);
                    512:       d = m->d;
                    513:       b = m->b;
                    514:       if ( d )
                    515:         for ( i = 0; i < d; i++ ) {
                    516:           if ( NBM_GET(b,i) ) PUTS("x");
                    517:           else PUTS("y");
                    518:         }
                    519:       else PUTS("1");
                    520:       if ( NEXT(t) ) PUTS("+");
                    521:     }
                    522:   }
                    523: }
                    524:
                    525: void PRINTSF(unsigned int i)
                    526: {
                    527:   if ( !i ) {
                    528:     PUTS("0");
                    529:   } else
                    530:     total_length += 15;
                    531:
                    532: #ifndef CPRINT
                    533: #define CPRINT
                    534: #endif
                    535: }

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