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

Annotation of OpenXM_contrib2/asir2000/builtin/strobj.c, Revision 1.17

1.6       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.7       noro       26:  * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.6       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.
                     47:  *
1.17    ! noro       48:  * $OpenXM: OpenXM_contrib2/asir2000/builtin/strobj.c,v 1.16 2004/03/04 03:31:28 noro Exp $
1.6       noro       49: */
1.1       noro       50: #include "ca.h"
                     51: #include "parse.h"
                     52: #include "ctype.h"
1.10      ohara      53: #if defined(PARI)
1.1       noro       54: #include "genpari.h"
1.11      saito      55: #  if !(PARI_VERSION_CODE > 131588)
1.1       noro       56: extern jmp_buf environnement;
1.11      saito      57: #  endif
1.1       noro       58: #endif
1.5       noro       59: #include <string.h>
                     60:
1.1       noro       61: extern char *parse_strp;
                     62:
                     63: void Prtostr(), Pstrtov(), Peval_str();
1.3       noro       64: void Pstrtoascii(), Pasciitostr();
1.5       noro       65: void Pstr_len(), Pstr_chr(), Psub_str();
1.14      noro       66: void Pwrite_to_tb();
                     67: void Ptb_to_string();
                     68: void Pclear_tb();
                     69: void Pstring_to_tb();
                     70: void Pquotetotex_tb();
                     71: void Pquotetotex();
                     72: void fnodetotex_tb(FNODE f,TB tb);
                     73: char *symbol_name(char *name);
                     74: void tb_to_string(TB tb,STRING *rp);
                     75: void fnodenodetotex_tb(NODE n,TB tb);
                     76: void fargstotex_tb(char *opname,FNODE f,TB tb);
1.1       noro       77:
                     78: struct ftab str_tab[] = {
                     79:        {"rtostr",Prtostr,1},
                     80:        {"strtov",Pstrtov,1},
                     81:        {"eval_str",Peval_str,1},
1.3       noro       82:        {"strtoascii",Pstrtoascii,1},
                     83:        {"asciitostr",Pasciitostr,1},
1.5       noro       84:        {"str_len",Pstr_len,1},
                     85:        {"str_chr",Pstr_chr,3},
                     86:        {"sub_str",Psub_str,3},
1.14      noro       87:        {"write_to_tb",Pwrite_to_tb,2},
                     88:        {"clear_tb",Pclear_tb,1},
                     89:        {"tb_to_string",Ptb_to_string,1},
                     90:        {"string_to_tb",Pstring_to_tb,1},
                     91:        {"quotetotex_tb",Pquotetotex_tb,2},
                     92:        {"quotetotex",Pquotetotex,1},
1.1       noro       93:        {0,0,0},
                     94: };
1.13      noro       95:
1.14      noro       96: void write_tb(char *s,TB tb)
                     97: {
                     98:        if ( tb->next == tb->size ) {
                     99:                tb->size *= 2;
                    100:                tb->body = (char **)REALLOC(tb->body,tb->size*sizeof(char *));
                    101:        }
                    102:        tb->body[tb->next] = s;
                    103:        tb->next++;
                    104: }
1.13      noro      105:
1.14      noro      106: void Pwrite_to_tb(NODE arg,Q *rp)
1.13      noro      107: {
                    108:        int i;
1.16      noro      109:        Obj obj;
                    110:        TB tb;
1.13      noro      111:
1.14      noro      112:        asir_assert(ARG1(arg),O_TB,"write_to_tb");
1.16      noro      113:        obj = ARG0(arg);
                    114:        if ( !obj )
                    115:                write_tb("",ARG1(arg));
                    116:        else if ( OID(obj) == O_STR )
                    117:                write_tb(BDY((STRING)obj),ARG1(arg));
                    118:        else if ( OID(obj) == O_TB ) {
                    119:                tb = (TB)obj;
                    120:                for ( i = 0; i < tb->next; i++ )
                    121:                        write_tb(tb->body[i],ARG1(arg));
                    122:        }
1.14      noro      123:        *rp = 0;
1.13      noro      124: }
                    125:
1.14      noro      126: void Pquotetotex(NODE arg,STRING *rp)
1.13      noro      127: {
1.14      noro      128:        TB tb;
1.13      noro      129:
1.14      noro      130:        NEWTB(tb);
                    131:        fnodetotex_tb(BDY((QUOTE)ARG0(arg)),tb);
                    132:        tb_to_string(tb,rp);
1.13      noro      133: }
                    134:
1.14      noro      135: void Pquotetotex_tb(NODE arg,Q *rp)
1.13      noro      136: {
                    137:        int i;
1.14      noro      138:        TB tb;
1.13      noro      139:
1.14      noro      140:        asir_assert(ARG1(arg),O_TB,"quotetotex_tb");
                    141:        fnodetotex_tb(BDY((QUOTE)ARG0(arg)),ARG1(arg));
1.13      noro      142:        *rp = 0;
                    143: }
                    144:
1.14      noro      145: void Pstring_to_tb(NODE arg,TB *rp)
                    146: {
                    147:        TB tb;
                    148:
                    149:        asir_assert(ARG0(arg),O_STR,"string_to_tb");
                    150:        NEWTB(tb);
                    151:        tb->body[0] = BDY((STRING)ARG0(arg));
                    152:        tb->next++;
                    153:        *rp = tb;
                    154: }
                    155:
                    156: void Ptb_to_string(NODE arg,STRING *rp)
                    157: {
                    158:        TB tb;
                    159:
                    160:        asir_assert(ARG0(arg),O_TB,"tb_to_string");
                    161:        tb = (TB)ARG0(arg);
                    162:        tb_to_string(tb,rp);
                    163: }
                    164:
                    165: void tb_to_string(TB tb,STRING *rp)
1.13      noro      166: {
1.14      noro      167:        int j,len;
1.13      noro      168:        char *all,*p,*q;
                    169:
1.14      noro      170:        for ( j = 0, len = 0; j < tb->next; j++ )
                    171:                len += strlen(tb->body[j]);
                    172:        all = (char *)MALLOC_ATOMIC((len+1)*sizeof(char));
                    173:        for ( j = 0, p = all; j < tb->next; j++ )
                    174:                for ( q = tb->body[j]; *q; *p++ = *q++ );
                    175:        *p = 0;
                    176:        MKSTR(*rp,all);
                    177: }
                    178:
                    179: void Pclear_tb(NODE arg,Q *rp)
                    180: {
                    181:        TB tb;
                    182:        int j;
                    183:
                    184:        asir_assert(ARG0(arg),O_TB,"clear_tb");
                    185:        tb = (TB)ARG0(arg);
                    186:        for ( j = 0; j < tb->next; j++ )
                    187:                tb->body[j] = 0;
                    188:        tb->next = 0;
                    189:        *rp = 0;
1.13      noro      190: }
1.5       noro      191:
                    192: void Pstr_len(arg,rp)
                    193: NODE arg;
                    194: Q *rp;
                    195: {
1.16      noro      196:        Obj obj;
                    197:        TB tb;
                    198:        int r,i;
1.5       noro      199:
1.16      noro      200:        obj = (Obj)ARG0(arg);
                    201:        if ( !obj || (OID(obj) != O_STR && OID(obj) != O_TB) )
                    202:                error("str_len : invalid argument");
                    203:        if ( OID(obj) == O_STR)
                    204:                r = strlen(BDY((STRING)obj));
                    205:        else if ( OID(obj) == O_TB ) {
                    206:                tb = (TB)obj;
                    207:                for ( r = i = 0; i < tb->next; i++ )
                    208:                        r += strlen(tb->body[i]);
                    209:        }
1.5       noro      210:        STOQ(r,*rp);
                    211: }
                    212:
                    213: void Pstr_chr(arg,rp)
                    214: NODE arg;
                    215: Q *rp;
                    216: {
                    217:        STRING str,terminator;
                    218:        Q start;
                    219:        char *p,*ind;
                    220:        int chr,spos,r;
                    221:
                    222:        str = (STRING)ARG0(arg);
                    223:        start = (Q)ARG1(arg);
                    224:        terminator = (STRING)ARG2(arg);
                    225:        asir_assert(str,O_STR,"str_chr");
                    226:        asir_assert(start,O_N,"str_chr");
                    227:        asir_assert(terminator,O_STR,"str_chr");
                    228:        p = BDY(str);
                    229:        spos = QTOS(start);
                    230:        chr = BDY(terminator)[0];
1.8       noro      231:        if ( spos > (int)strlen(p) )
1.5       noro      232:                r = -1;
                    233:        else {
                    234:                ind = strchr(p+spos,chr);
                    235:                if ( ind )
                    236:                        r = ind-p;
                    237:                else
                    238:                        r = -1;
                    239:        }
                    240:        STOQ(r,*rp);
                    241: }
                    242:
                    243: void Psub_str(arg,rp)
                    244: NODE arg;
                    245: STRING *rp;
                    246: {
                    247:        STRING str;
                    248:        Q head,tail;
                    249:        char *p,*r;
                    250:        int spos,epos,len;
                    251:
                    252:        str = (STRING)ARG0(arg);
                    253:        head = (Q)ARG1(arg);
                    254:        tail = (Q)ARG2(arg);
                    255:        asir_assert(str,O_STR,"sub_str");
                    256:        asir_assert(head,O_N,"sub_str");
                    257:        asir_assert(tail,O_N,"sub_str");
                    258:        p = BDY(str);
                    259:        spos = QTOS(head);
                    260:        epos = QTOS(tail);
                    261:        len = strlen(p);
                    262:        if ( (spos >= len) || (epos < spos) ) {
                    263:                *rp = 0; return;
                    264:        }
                    265:        if ( epos >= len )
                    266:                epos = len-1;
                    267:        len = epos-spos+1;
                    268:        r = (char *)MALLOC(len+1);
                    269:        strncpy(r,p+spos,len);
                    270:        r[len] = 0;
                    271:        MKSTR(*rp,r);
                    272: }
1.3       noro      273:
                    274: void Pstrtoascii(arg,rp)
                    275: NODE arg;
                    276: LIST *rp;
                    277: {
                    278:        STRING str;
                    279:        unsigned char *p;
                    280:        int len,i;
                    281:        NODE n,n1;
                    282:        Q q;
                    283:
                    284:        str = (STRING)ARG0(arg);
                    285:        asir_assert(str,O_STR,"strtoascii");
                    286:        p = BDY(str);
                    287:        len = strlen(p);
                    288:        for ( i = len-1, n = 0; i >= 0; i-- ) {
                    289:                UTOQ((unsigned int)p[i],q);
                    290:                MKNODE(n1,q,n);
                    291:                n = n1;
                    292:        }
                    293:        MKLIST(*rp,n);
                    294: }
                    295:
                    296: void Pasciitostr(arg,rp)
                    297: NODE arg;
                    298: STRING *rp;
                    299: {
                    300:        LIST list;
                    301:        unsigned char *p;
                    302:        int len,i,j;
                    303:        NODE n;
                    304:        Q q;
                    305:
                    306:        list = (LIST)ARG0(arg);
                    307:        asir_assert(list,O_LIST,"asciitostr");
                    308:        n = BDY(list);
                    309:        len = length(n);
                    310:        p = MALLOC_ATOMIC(len+1);
                    311:        for ( i = 0; i < len; i++, n = NEXT(n) ) {
                    312:                q = (Q)BDY(n);
                    313:                asir_assert(q,O_N,"asciitostr");
                    314:                j = QTOS(q);
1.4       noro      315:                if ( j >= 256 || j <= 0 )
1.3       noro      316:                        error("asciitostr : argument out of range");
                    317:                p[i] = j;
                    318:        }
                    319:        p[i] = 0;
                    320:        MKSTR(*rp,(char *)p);
                    321: }
1.1       noro      322:
                    323: void Peval_str(arg,rp)
                    324: NODE arg;
                    325: Obj *rp;
                    326: {
                    327:        FNODE fnode;
                    328:        char *cmd;
1.10      ohara     329: #if defined(PARI)
1.8       noro      330:        void recover(int);
                    331:
1.1       noro      332:        recover(0);
1.11      saito     333: #  if !(PARI_VERSION_CODE > 131588)
1.1       noro      334:        if ( setjmp(environnement) ) {
                    335:                avma = top; recover(1);
                    336:                resetenv("");
                    337:        }
1.11      saito     338: #  endif
1.1       noro      339: #endif
                    340:        cmd = BDY((STRING)ARG0(arg));
1.9       noro      341:        exprparse_create_var(0,cmd,&fnode);
1.1       noro      342:        *rp = eval(fnode);
                    343: }
                    344:
                    345: void Prtostr(arg,rp)
                    346: NODE arg;
                    347: STRING *rp;
                    348: {
                    349:        char *b;
                    350:        int len;
                    351:
1.2       noro      352:        len = estimate_length(CO,ARG0(arg));
1.12      noro      353:        b = (char *)MALLOC_ATOMIC(len+1);
1.1       noro      354:        soutput_init(b);
                    355:        sprintexpr(CO,ARG0(arg));
                    356:        MKSTR(*rp,b);
                    357: }
                    358:
                    359: void Pstrtov(arg,rp)
                    360: NODE arg;
                    361: P *rp;
                    362: {
1.8       noro      363:        char *p;
1.1       noro      364:
                    365:        p = BDY((STRING)ARG0(arg));
                    366: #if 0
                    367:        if ( !islower(*p) )
                    368:                *rp = 0;
                    369:        else {
                    370:                for ( t = p+1; t && (isalnum(*t) || *t == '_'); t++ );
                    371:                if ( *t )
                    372:                        *rp = 0;
                    373:                else
                    374:                        makevar(p,rp);
                    375:        }
                    376: #else
                    377:        makevar(p,rp);
                    378: #endif
1.14      noro      379: }
                    380:
1.15      noro      381: struct TeXSymbol {
                    382:        char *text;
                    383:        char *symbol;
                    384: };
                    385:
                    386: static struct TeXSymbol texsymbol[] = {
                    387:  {"sin","\\sin"},
                    388:  {"cos","\\cos"},
                    389:  {"tan","\\tan"},
                    390:  {"sinh","\\sinh"},
                    391:  {"cosh","\\cosh"},
                    392:  {"tanh","\\tanh"},
                    393:  {"exp","\\exp"},
                    394:  {"log","\\log"},
                    395:
                    396: /* Greek Letters (lower case) */
                    397:  {"alpha","\\alpha"},
                    398:  {"beta","\\beta"},
                    399:  {"gamma","\\gamma"},
                    400:  {"delta","\\delta"},
                    401:  {"epsilon","\\epsilon"},
                    402:  {"varepsilon","\\varepsilon"},
                    403:  {"zeta","\\zeta"},
                    404:  {"eta","\\eta"},
                    405:  {"theta","\\theta"},
                    406:  {"vartheta","\\vartheta"},
                    407:  {"iota","\\iota"},
                    408:  {"kappa","\\kappa"},
                    409:  {"lambda","\\lambda"},
                    410:  {"mu","\\mu"},
                    411:  {"nu","\\nu"},
                    412:  {"xi","\\xi"},
                    413:  {"pi","\\pi"},
                    414:  {"varpi","\\varpi"},
                    415:  {"rho","\\rho"},
                    416:  {"sigma","\\sigma"},
                    417:  {"varsigma","\\varsigma"},
                    418:  {"tau","\\tau"},
                    419:  {"upsilon","\\upsilon"},
                    420:  {"phi","\\phi"},
                    421:  {"varphi","\\varphi"},
                    422:  {"chi","\\chi"},
                    423:  {"omega","\\omega"},
                    424:
                    425: /* Greek Letters, (upper case) */
                    426:  {"ggamma","\\Gamma"},
                    427:  {"ddelta","\\Delta"},
                    428:  {"ttheta","\\Theta"},
                    429:  {"llambda","\\Lambda"},
                    430:  {"xxi","\\Xi"},
                    431:  {"ppi","\\Pi"},
                    432:  {"ssigma","\\Sigma"},
                    433:  {"uupsilon","\\Upsilon"},
                    434:  {"pphi","\\Phi"},
                    435:  {"ppsi","\\Psi"},
                    436:  {"oomega","\\Omega"},
                    437:
                    438:  /* Our own mathematical functions */
                    439:  {"algebra_tensor","\\otimes"},
                    440:  {"base_where","{\\rm \\ where \\ }"},
                    441:  /* Mathematical constants */
                    442:  {"c_pi","\\pi"},
                    443:  {"c_i","\\sqrt{-1}"},
                    444:
                    445:  /* Temporary  */
                    446:  {"dx","\\partial"},
                    447:  {0,0}
                    448: };
                    449:
1.14      noro      450: char *symbol_name(char *name)
                    451: {
1.15      noro      452:        int i;
                    453:
                    454:        for ( i = 0; texsymbol[i].text; i++ )
                    455:                if ( !strcmp(texsymbol[i].text,name) )
                    456:                        return texsymbol[i].symbol;
1.14      noro      457:        return name;
                    458: }
                    459:
                    460: void fnodetotex_tb(FNODE f,TB tb)
                    461: {
                    462:        NODE n,t,t0;
                    463:        char vname[BUFSIZ];
                    464:        char *opname;
                    465:        Obj obj;
1.15      noro      466:        int i,len,allzero;
1.17    ! noro      467:        FNODE fi,f2;
1.14      noro      468:
                    469:        write_tb(" ",tb);
                    470:        if ( !f ) {
                    471:                write_tb("0",tb);
                    472:                return;
                    473:        }
                    474:        switch ( f->id ) {
                    475:                /* unary operators */
                    476:                case I_NOT: case I_PAREN: case I_MINUS:
                    477:                        switch ( f->id ) {
                    478:                                case I_NOT:
                    479:                                        write_tb("\\neg (",tb);
                    480:                                        fnodetotex_tb((FNODE)FA0(f),tb);
                    481:                                        write_tb(")",tb);
                    482:                                        break;
                    483:                                case I_PAREN:
                    484:                                        write_tb("(",tb);
                    485:                                        fnodetotex_tb((FNODE)FA0(f),tb);
                    486:                                        write_tb(")",tb);
                    487:                                        break;
                    488:                                case I_MINUS:
                    489:                                        write_tb("-",tb);
                    490:                                        fnodetotex_tb((FNODE)FA0(f),tb);
                    491:                                        break;
                    492:                        }
                    493:                        break;
                    494:
                    495:                /* binary operators */
                    496:                case I_BOP: case I_COP: case I_LOP: case I_AND: case I_OR:
                    497:                        /* arg list */
                    498:                        /* I_AND, I_OR => FA0(f), FA1(f) */
                    499:                        /* otherwise   => FA1(f), FA2(f) */
                    500:
                    501:                        /* op */
                    502:                        switch ( f->id ) {
                    503:                                case I_BOP:
                    504:                                        opname = ((ARF)FA0(f))->name;
                    505:                                        if ( !strcmp(opname,"+") ) {
                    506:                                                fnodetotex_tb((FNODE)FA1(f),tb);
                    507:                                                write_tb(opname,tb);
                    508:                                                fnodetotex_tb((FNODE)FA2(f),tb);
                    509:                                        } else if ( !strcmp(opname,"-") ) {
                    510:                                                if ( FA1(f) ) fnodetotex_tb((FNODE)FA1(f),tb);
                    511:                                                write_tb(opname,tb);
                    512:                                                fnodetotex_tb((FNODE)FA2(f),tb);
                    513:                                        } else if ( !strcmp(opname,"*") ) {
                    514:                                                fnodetotex_tb((FNODE)FA1(f),tb);
                    515:                                                write_tb(" ",tb);
1.17    ! noro      516:                                                /* XXX special care for DP */
        !           517:                                                f2 = (FNODE)FA2(f);
        !           518:                                                if ( f2->id == I_EV ) {
        !           519:                                                        n = (NODE)FA0(f2);
        !           520:                                                        for ( i = 0; n; n = NEXT(n), i++ ) {
        !           521:                                                                fi = (FNODE)BDY(n);
        !           522:                                                                if ( fi->id != I_FORMULA || FA0(fi) )
        !           523:                                                                        break;
        !           524:                                                        }
        !           525:                                                        if ( n )
        !           526:                                                                fnodetotex_tb((FNODE)FA2(f),tb);
        !           527:                                                } else
        !           528:                                                        fnodetotex_tb((FNODE)FA2(f),tb);
1.14      noro      529:                                        } else if ( !strcmp(opname,"/") ) {
                    530:                                                write_tb("\\frac{",tb);
                    531:                                                fnodetotex_tb((FNODE)FA1(f),tb);
                    532:                                                write_tb("} {",tb);
                    533:                                                fnodetotex_tb((FNODE)FA2(f),tb);
                    534:                                                write_tb("}",tb);
                    535:                                        } else if ( !strcmp(opname,"^") ) {
                    536:                                                fnodetotex_tb((FNODE)FA1(f),tb);
                    537:                                                write_tb("^{",tb);
                    538:                                                fnodetotex_tb((FNODE)FA2(f),tb);
                    539:                                                write_tb("} ",tb);
                    540:                                        } else if ( !strcmp(opname,"%") ) {
                    541:                                                fnodetotex_tb((FNODE)FA1(f),tb);
                    542:                                                write_tb(" {\\rm mod}\\, ",tb);
                    543:                                                fnodetotex_tb((FNODE)FA2(f),tb);
                    544:                                        } else
                    545:                                                error("invalid binary operator");
                    546:
                    547:                                case I_COP:
                    548:                                        switch( (cid)FA0(f) ) {
                    549:                                                case C_EQ:
                    550:                                                        fnodetotex_tb((FNODE)FA1(f),tb);
                    551:                                                        write_tb(" = ",tb);
                    552:                                                        fnodetotex_tb((FNODE)FA2(f),tb);
                    553:                                                        break;
                    554:                                                case C_NE:
                    555:                                                        fnodetotex_tb((FNODE)FA1(f),tb);
                    556:                                                        write_tb(" \\neq ",tb);
                    557:                                                        fnodetotex_tb((FNODE)FA2(f),tb);
                    558:                                                        break;
                    559:                                                case C_GT:
                    560:                                                        fnodetotex_tb((FNODE)FA1(f),tb);
                    561:                                                        write_tb(" \\gt ",tb);
                    562:                                                        fnodetotex_tb((FNODE)FA2(f),tb);
                    563:                                                        break;
                    564:                                                case C_LT:
                    565:                                                        fnodetotex_tb((FNODE)FA1(f),tb);
                    566:                                                        write_tb(" \\lt ",tb);
                    567:                                                        fnodetotex_tb((FNODE)FA2(f),tb);
                    568:                                                        break;
                    569:                                                case C_GE:
                    570:                                                        fnodetotex_tb((FNODE)FA1(f),tb);
                    571:                                                        write_tb(" \\geq ",tb);
                    572:                                                        fnodetotex_tb((FNODE)FA2(f),tb);
                    573:                                                        break;
                    574:                                                case C_LE:
                    575:                                                        fnodetotex_tb((FNODE)FA1(f),tb);
                    576:                                                        write_tb(" \\leq ",tb);
                    577:                                                        fnodetotex_tb((FNODE)FA2(f),tb);
                    578:                                                        break;
                    579:                                        }
                    580:                                        break;
                    581:
                    582:                                case I_LOP:
                    583:                                        switch( (lid)FA0(f) ) {
                    584:                                                case L_EQ:
                    585:                                                        fnodetotex_tb((FNODE)FA1(f),tb);
                    586:                                                        write_tb(" = ",tb);
                    587:                                                        fnodetotex_tb((FNODE)FA2(f),tb);
                    588:                                                        break;
                    589:                                                case L_NE:
                    590:                                                        fnodetotex_tb((FNODE)FA1(f),tb);
                    591:                                                        write_tb(" \\neq ",tb);
                    592:                                                        fnodetotex_tb((FNODE)FA2(f),tb);
                    593:                                                        break;
                    594:                                                case L_GT:
                    595:                                                        fnodetotex_tb((FNODE)FA1(f),tb);
                    596:                                                        write_tb(" \\gt ",tb);
                    597:                                                        fnodetotex_tb((FNODE)FA2(f),tb);
                    598:                                                        break;
                    599:                                                case L_LT:
                    600:                                                        fnodetotex_tb((FNODE)FA1(f),tb);
                    601:                                                        write_tb(" \\lt ",tb);
                    602:                                                        fnodetotex_tb((FNODE)FA2(f),tb);
                    603:                                                        break;
                    604:                                                case L_GE:
                    605:                                                        fnodetotex_tb((FNODE)FA1(f),tb);
                    606:                                                        write_tb(" \\geq ",tb);
                    607:                                                        fnodetotex_tb((FNODE)FA2(f),tb);
                    608:                                                        break;
                    609:                                                case L_LE:
                    610:                                                        fnodetotex_tb((FNODE)FA1(f),tb);
                    611:                                                        write_tb(" \\leq ",tb);
                    612:                                                        fnodetotex_tb((FNODE)FA2(f),tb);
                    613:                                                        break;
                    614:                                                case L_AND:
                    615:                                                        fnodetotex_tb((FNODE)FA1(f),tb);
                    616:                                                        write_tb(" {\\rm \\ and\\ } ",tb);
                    617:                                                        fnodetotex_tb((FNODE)FA2(f),tb);
                    618:                                                        break;
                    619:                                                case L_OR:
                    620:                                                        fnodetotex_tb((FNODE)FA1(f),tb);
                    621:                                                        write_tb(" {\\rm \\ or\\ } ",tb);
                    622:                                                        fnodetotex_tb((FNODE)FA2(f),tb);
                    623:                                                        break;
                    624:                                                case L_NOT:
                    625:                                                        /* XXX : L_NOT is a unary operator */
                    626:                                                        write_tb("\\neg (",tb);
                    627:                                                        fnodetotex_tb((FNODE)FA1(f),tb);
                    628:                                                        write_tb(")",tb);
                    629:                                                        return;
                    630:                                        }
                    631:                                        break;
                    632:
                    633:                                case I_AND:
                    634:                                        fnodetotex_tb((FNODE)FA0(f),tb);
                    635:                                        write_tb(" {\\rm \\ and\\ } ",tb);
                    636:                                        fnodetotex_tb((FNODE)FA1(f),tb);
                    637:                                        break;
                    638:
                    639:                                case I_OR:
                    640:                                        fnodetotex_tb((FNODE)FA0(f),tb);
                    641:                                        write_tb(" {\\rm \\ or\\ } ",tb);
                    642:                                        fnodetotex_tb((FNODE)FA1(f),tb);
                    643:                                        break;
                    644:                        }
                    645:                        break;
                    646:
                    647:                /* ternary operators */
                    648:                case I_CE:
                    649:                        error("fnodetotex_tb : not implemented yet");
                    650:                        break;
                    651:
                    652:                /* lists */
                    653:                case I_LIST:
                    654:                        write_tb(" [ ",tb);
                    655:                        n = (NODE)FA0(f);
                    656:                        fnodenodetotex_tb(n,tb);
                    657:                        write_tb("]",tb);
                    658:                        break;
                    659:
                    660:                /* function */
                    661:                case I_FUNC: case I_CAR: case I_CDR: case I_EV:
                    662:                        switch ( f->id ) {
                    663:                                case I_FUNC:
                    664:                                        opname = symbol_name(((FUNC)FA0(f))->name);
                    665:                                        write_tb(opname,tb);
                    666:                                        write_tb("(",tb);
                    667:                                        fargstotex_tb(opname,FA1(f),tb);
                    668:                                        write_tb(")",tb);
                    669:                                        break;
                    670:                                case I_CAR:
                    671:                                        opname = symbol_name("car");
                    672:                                        write_tb(opname,tb);
                    673:                                        write_tb("(",tb);
                    674:                                        fargstotex_tb(opname,FA0(f),tb);
                    675:                                        write_tb(")",tb);
                    676:                                        break;
                    677:                                case I_CDR:
                    678:                                        opname = symbol_name("cdr");
                    679:                                        write_tb(opname,tb);
                    680:                                        write_tb("(",tb);
                    681:                                        fargstotex_tb(opname,FA0(f),tb);
                    682:                                        write_tb(")",tb);
                    683:                                        break;
                    684:                                case I_EV:
                    685:                                        n = (NODE)FA0(f);
1.15      noro      686:                                        allzero = 1;
1.14      noro      687:                                        for ( t0 = 0, i = 0; n; n = NEXT(n), i++ ) {
1.15      noro      688:                                                fi = (FNODE)BDY(n);
                    689:                                                if ( fi->id == I_FORMULA && !FA0(fi) ) continue;
                    690:                                                allzero = 0;
                    691:                                                if ( fi->id == I_FORMULA && UNIQ(FA0(fi)) ) {
                    692:                                                        sprintf(vname,"x_{%d}",i);
                    693:                                                        len = strlen(vname);
                    694:                                                        opname = MALLOC_ATOMIC(len+1);
                    695:                                                        strcpy(opname,vname);
                    696:                                                        write_tb(opname,tb);
                    697:                                                } else {
                    698:                                                        sprintf(vname,"x_{%d}^{",i);
                    699:                                                        len = strlen(vname);
                    700:                                                        opname = MALLOC_ATOMIC(len+1);
                    701:                                                        strcpy(opname,vname);
                    702:                                                        write_tb(opname,tb);
                    703:                                                        fnodetotex_tb((FNODE)BDY(n),tb);
                    704:                                                        write_tb("} ",tb);
                    705:                                                }
1.14      noro      706:                                        }
1.15      noro      707:                                        /* XXX */
                    708:                                        if ( allzero )
                    709:                                                write_tb(" 1 ",tb);
1.14      noro      710:                                        break;
                    711:                        }
                    712:                        break;
                    713:
                    714:                case I_STR:
                    715:                        write_tb((char *)FA0(f),tb);
                    716:                        break;
                    717:
                    718:                case I_FORMULA:
                    719:                        obj = (Obj)FA0(f);
                    720:                        if ( obj && OID(obj) == O_P ) {
                    721:                                opname = symbol_name(VR((P)obj)->name);
                    722:                        } else {
                    723:                                len = estimate_length(CO,obj);
                    724:                                opname = (char *)MALLOC_ATOMIC(len+1);
                    725:                                soutput_init(opname);
                    726:                                sprintexpr(CO,obj);
                    727:                        }
                    728:                        write_tb(opname,tb);
                    729:                        break;
                    730:
                    731:                case I_PVAR:
                    732:                        if ( FA1(f) )
                    733:                                error("fnodetotex_tb : not implemented yet");
                    734:                        GETPVNAME(FA0(f),opname);
                    735:                        write_tb(opname,tb);
                    736:                        break;
                    737:
                    738:                default:
                    739:                        error("fnodetotex_tb : not implemented yet");
                    740:        }
                    741: }
                    742:
                    743: void fnodenodetotex_tb(NODE n,TB tb)
                    744: {
                    745:        for ( ; n; n = NEXT(n) ) {
                    746:                fnodetotex_tb((FNODE)BDY(n),tb);
                    747:                if ( NEXT(n) ) write_tb(", ",tb);
                    748:        }
                    749: }
                    750:
                    751: void fargstotex_tb(char *name,FNODE f,TB tb)
                    752: {
                    753:        NODE n;
                    754:
                    755:        if ( !strcmp(name,"matrix") ) {
                    756:                error("fargstotex_tb : not implemented yet");
                    757:        } else if ( !strcmp(name,"vector") ) {
                    758:                error("fargstotex_tb : not implemented yet");
                    759:        } else {
                    760:                if ( f->id == I_LIST ) {
                    761:                        n = (NODE)FA0(f);
                    762:                        fnodenodetotex_tb(n,tb);
                    763:                } else
                    764:                        fnodetotex_tb(f,tb);
                    765:        }
1.1       noro      766: }

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