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

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.3     ! noro       47:  * $OpenXM: OpenXM_contrib2/asir2018/io/cpexpr.c,v 1.2 2018/10/01 06:06:02 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: {
                    255:   if ( hex_output )
1.2       noro      256:     total_length += mpz_sizeinbase(n,16)+2;
1.1       noro      257:   else
                    258:     total_length += mpz_sizeinbase(n,10);
                    259: }
                    260:
                    261: void PRINTZ(Z n)
                    262: {
                    263:   if ( !n )
                    264:     PUTS("0");
                    265:   else
                    266:     PRINTMPZ(BDY(n));
                    267: }
                    268:
                    269: void PRINTSTR(STRING str)
                    270: {
                    271:   char *p;
                    272:
                    273:   for ( p = BDY(str); *p; p++ )
                    274:     if ( *p == '"' )
                    275:       PUTS("\"");
                    276:     else {
                    277:       total_length += 1;
                    278:     }
                    279: }
                    280:
                    281: void PRINTDP(VL vl,DP d)
                    282: {
                    283:   int n,i;
                    284:   MP m;
                    285:   DL dl;
                    286:
                    287:   for ( n = d->nv, m = BDY(d); m; m = NEXT(m) ) {
                    288:     PUTS("("); PRINTEXPR(vl,(pointer)m->c); PUTS(")*<<");
                    289:     for ( i = 0, dl = m->dl; i < n-1; i++ ) {
                    290:       total_length += 11;
                    291:     }
                    292:     total_length += 10;
                    293:     PUTS(">>");
                    294:     if ( NEXT(m) )
                    295:       PUTS("+");
                    296:   }
                    297: }
                    298:
                    299: void PRINTDPM(VL vl,DPM d)
                    300: {
                    301:   int n,i;
                    302:   DMM m;
                    303:   DL dl;
                    304:
                    305:   for ( n = d->nv, m = BDY(d); m; m = NEXT(m) ) {
                    306:     PUTS("("); PRINTEXPR(vl,(pointer)m->c); PUTS(")*<<");
                    307:     for ( i = 0, dl = m->dl; i < n-1; i++ ) {
                    308:       total_length += 11;
                    309:     }
                    310:     total_length += 10;
                    311:     total_length += 11; /* for ':pos' */
                    312:     PUTS(">>");
                    313:     if ( NEXT(m) )
                    314:       PUTS("+");
                    315:   }
                    316: }
                    317:
                    318:
                    319: void PRINTUI(VL vl,USINT u)
                    320: {
                    321:   total_length += 10;
                    322: }
                    323:
                    324: void PRINTGFMMAT(VL vl,GFMMAT mat)
                    325: {
                    326:   int row,col,i,j;
                    327:   unsigned int **b;
                    328:
                    329:   row = mat->row;
                    330:   col = mat->col;
                    331:   b = mat->body;
                    332:   for ( i = 0; i < row; i++ ) {
                    333:     PUTS("[");
                    334:     for ( j = 0; j < col; j++ ) {
                    335:       total_length += 10; /* XXX */
                    336:     }
                    337:     PUTS("]\n");
                    338:   }
                    339: }
                    340:
                    341: void PRINTBYTEARRAY(VL vl,BYTEARRAY array)
                    342: {
                    343:   /* |xx xx ... xx| */
                    344:   total_length += 1+3*array->len;
                    345: }
                    346:
                    347: extern int print_quote;
                    348:
                    349: void PRINTQUOTE(VL vl,QUOTE quote)
                    350: {
                    351:   LIST list;
                    352:
                    353:   if ( print_quote == 2 ) {
                    354:     PRINTFNODE(BDY(quote),0);
                    355:   } else if ( print_quote == 1 ) {
                    356:     /* XXX */
                    357:     fnodetotree(BDY(quote),&list);
                    358:     PRINTEXPR(vl,(Obj)list);
                    359:   } else {
                    360:     /* <...quoted...> */
                    361:     total_length += 20;
                    362:   }
                    363: }
                    364:
                    365: void PRINTQUOTEARG(VL vl,QUOTEARG quote)
                    366: {
                    367:   /* XXX */
                    368:   /* <...quoted...> */
                    369:   total_length += 20;
                    370: }
                    371:
                    372: void PRINTSYMBOL(SYMBOL sym)
                    373: {
                    374:   total_length += strlen(sym->name);
                    375: }
                    376:
                    377: void PRINTTB(VL vl,TB p)
                    378: {
                    379:   int i;
                    380:
                    381:   for ( i = 0; i < p->next; i++ ) {
                    382:     total_length += strlen(p->body[i]);
                    383:   }
                    384: }
                    385:
                    386: void PRINTUP2(UP2 p)
                    387: {
                    388:   int d,i;
                    389:
                    390:   if ( !p ) {
                    391:     PUTS("0");
                    392:   } else if ( hex_output ) {
                    393:     total_length += p->w*8;
                    394:   } else {
                    395:     d = degup2(p);
                    396:     PUTS("(");
                    397:     if ( !d ) {
                    398:       PUTS("1");
                    399:     } else if ( d == 1 ) {
                    400:       PUTS("@");
                    401:     } else {
                    402:       PRINTHAT;
                    403:       total_length += 11;
                    404:     }
                    405:     for ( i = d-1; i >= 0; i-- ) {
                    406:       if ( p->b[i/BSH] & (1<<(i%BSH)) )
                    407:         if ( !i ) {
                    408:           PUTS("+1");
                    409:         } else if ( i == 1 ) {
                    410:           PUTS("+@");
                    411:         } else {
                    412:           PRINTHAT;
                    413:           total_length += 12;
                    414:         }
                    415:     }
                    416:     PUTS(")");
                    417:   }
                    418: }
                    419:
                    420: void PRINTQOP(VL vl,F f)
                    421: {
                    422:   char *op;
                    423:
                    424:   op = FOP(f)==AL_EX?"ex":"all";
                    425:   PUTS(op); PUTS(NAME(FQVR(f)));
                    426:   total_length += 2;
                    427:   PRINTEXPR(vl,(Obj)FQMAT(f)); PUTS(")");
                    428: }
                    429:
                    430: void PRINTUP(UP n)
                    431: {
                    432:   int i,d;
                    433:
                    434:   if ( !n )
                    435:     PUTS("0");
                    436:   else if ( !n->d )
                    437:     PRINTNUM(n->c[0]);
                    438:   else {
                    439:     d = n->d;
                    440:     PUTS("(");
                    441:     if ( !d ) {
                    442:       PRINTNUM(n->c[d]);
                    443:     } else if ( d == 1 ) {
                    444:       PRINTNUM(n->c[d]);
                    445:       PUTS("*@p");
                    446:     } else {
                    447:       PRINTNUM(n->c[d]);
                    448:       PRINTHAT;
                    449:       total_length += 13;
                    450:     }
                    451:     for ( i = d-1; i >= 0; i-- ) {
                    452:       if ( n->c[i] ) {
                    453:         PUTS("+("); PRINTNUM(n->c[i]); PUTS(")");
                    454:         if ( i >= 2 ) {
                    455:           PRINTHAT;
                    456:           total_length += 13;
                    457:         } else if ( i == 1 )
                    458:           PUTS("*@p");
                    459:       }
                    460:     }
                    461:     PUTS(")");
                    462:   }
                    463: }
                    464:
                    465: void PRINTUM(UM n)
                    466: {
                    467:   int i,d;
                    468:
                    469:   if ( !n )
                    470:     PUTS("0");
                    471:   else if ( !n->d )
                    472:     PRINTSF(n->c[0]);
                    473:   else {
                    474:     d = n->d;
                    475:     PUTS("(");
                    476:     if ( !d ) {
                    477:       PRINTSF(n->c[d]);
                    478:     } else if ( d == 1 ) {
                    479:       PRINTSF(n->c[d]);
                    480:       PUTS("*@s");
                    481:     } else {
                    482:       PRINTSF(n->c[d]);
                    483:       PUTS("*@s"); PRINTHAT; total_length += 13;
                    484:     }
                    485:     for ( i = d-1; i >= 0; i-- ) {
                    486:       if ( n->c[i] ) {
                    487:         PUTS("+("); PRINTSF(n->c[i]); PUTS(")");
                    488:         if ( i >= 2 ) {
                    489:           PUTS("*@s"); PRINTHAT; total_length += 13;
                    490:         } else if ( i == 1 )
                    491:           PUTS("*@s");
                    492:       }
                    493:     }
                    494:     PUTS(")");
                    495:   }
                    496: }
                    497:
                    498: void PRINTNBP(VL vl,NBP p)
                    499: {
                    500:   NODE t;
                    501:   NBM m;
                    502:   int d,i;
                    503:   unsigned int *b;
                    504:   if ( !p ) PUTS("0");
                    505:   else {
                    506:     for ( t = BDY(p); t; t = NEXT(t) ) {
                    507:       m = (NBM)BDY(t);
                    508:       PRINTEXPR(vl,(Obj)m->c);
                    509:       d = m->d;
                    510:       b = m->b;
                    511:       if ( d )
                    512:         for ( i = 0; i < d; i++ ) {
                    513:           if ( NBM_GET(b,i) ) PUTS("x");
                    514:           else PUTS("y");
                    515:         }
                    516:       else PUTS("1");
                    517:       if ( NEXT(t) ) PUTS("+");
                    518:     }
                    519:   }
                    520: }
                    521:
                    522: void PRINTSF(unsigned int i)
                    523: {
                    524:   if ( !i ) {
                    525:     PUTS("0");
                    526:   } else
                    527:     total_length += 15;
                    528:
                    529: #ifndef CPRINT
                    530: #define CPRINT
                    531: #endif
                    532: }

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