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

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.50    ! ohara      48:  * $OpenXM: OpenXM_contrib2/asir2000/builtin/strobj.c,v 1.49 2004/07/13 09:10:38 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.18      noro       61: struct TeXSymbol {
                     62:        char *text;
                     63:        char *symbol;
                     64: };
                     65:
1.1       noro       66: extern char *parse_strp;
                     67:
1.50    ! ohara      68: void Psprintf();
1.1       noro       69: void Prtostr(), Pstrtov(), Peval_str();
1.3       noro       70: void Pstrtoascii(), Pasciitostr();
1.5       noro       71: void Pstr_len(), Pstr_chr(), Psub_str();
1.14      noro       72: void Pwrite_to_tb();
                     73: void Ptb_to_string();
                     74: void Pclear_tb();
                     75: void Pstring_to_tb();
                     76: void Pquotetotex_tb();
                     77: void Pquotetotex();
1.24      noro       78: void Pquotetotex_env();
1.47      noro       79: void Pflatten_quote();
1.48      noro       80: void Pquote_to_funargs(),Pfunargs_to_quote();
1.14      noro       81: void fnodetotex_tb(FNODE f,TB tb);
                     82: char *symbol_name(char *name);
1.28      noro       83: char *conv_rule(char *name);
1.38      noro       84: char *conv_subs(char *name);
1.28      noro       85: char *call_convfunc(char *name);
1.14      noro       86: void tb_to_string(TB tb,STRING *rp);
                     87: void fnodenodetotex_tb(NODE n,TB tb);
                     88: void fargstotex_tb(char *opname,FNODE f,TB tb);
1.35      noro       89: int top_is_minus(FNODE f);
1.1       noro       90:
                     91: struct ftab str_tab[] = {
1.50    ! ohara      92:        {"sprintf",Psprintf,-99999999},
1.1       noro       93:        {"rtostr",Prtostr,1},
                     94:        {"strtov",Pstrtov,1},
                     95:        {"eval_str",Peval_str,1},
1.3       noro       96:        {"strtoascii",Pstrtoascii,1},
                     97:        {"asciitostr",Pasciitostr,1},
1.5       noro       98:        {"str_len",Pstr_len,1},
                     99:        {"str_chr",Pstr_chr,3},
                    100:        {"sub_str",Psub_str,3},
1.14      noro      101:        {"write_to_tb",Pwrite_to_tb,2},
                    102:        {"clear_tb",Pclear_tb,1},
                    103:        {"tb_to_string",Ptb_to_string,1},
                    104:        {"string_to_tb",Pstring_to_tb,1},
                    105:        {"quotetotex_tb",Pquotetotex_tb,2},
                    106:        {"quotetotex",Pquotetotex,1},
1.24      noro      107:        {"quotetotex_env",Pquotetotex_env,-99999999},
1.47      noro      108:        {"flatten_quote",Pflatten_quote,2},
1.48      noro      109:        {"quote_to_funargs",Pquote_to_funargs,1},
                    110:        {"funargs_to_quote",Pfunargs_to_quote,1},
1.1       noro      111:        {0,0,0},
                    112: };
1.13      noro      113:
1.14      noro      114: void write_tb(char *s,TB tb)
                    115: {
                    116:        if ( tb->next == tb->size ) {
                    117:                tb->size *= 2;
                    118:                tb->body = (char **)REALLOC(tb->body,tb->size*sizeof(char *));
                    119:        }
                    120:        tb->body[tb->next] = s;
                    121:        tb->next++;
                    122: }
1.13      noro      123:
1.18      noro      124: int register_symbol_table(Obj arg);
                    125: int register_conv_rule(Obj arg);
1.38      noro      126: int register_conv_func(Obj arg);
1.23      noro      127: int register_dp_vars(Obj arg);
1.46      noro      128: int register_dp_vars_origin(Obj arg);
                    129: int register_dp_dvars_origin(Obj arg);
                    130: int register_dp_dvars_prefix(Obj arg);
1.25      noro      131: int register_dp_vars_prefix(Obj arg);
1.31      noro      132: int register_dp_vars_hweyl(Obj arg);
1.27      noro      133: int register_show_lt(Obj arg);
1.40      noro      134: char *objtostr(Obj obj);
1.18      noro      135: static struct TeXSymbol *user_texsymbol;
1.23      noro      136: static char **dp_vars;
1.25      noro      137: static int dp_vars_len;
                    138: static char *dp_vars_prefix;
1.46      noro      139: static char *dp_dvars_prefix;
                    140: static int dp_vars_origin;
                    141: static int dp_dvars_origin;
1.27      noro      142: static int show_lt;
1.26      noro      143: static FUNC convfunc;
1.27      noro      144: static int is_lt;
1.28      noro      145: static int conv_flag;
1.31      noro      146: static int dp_vars_hweyl;
1.28      noro      147:
1.38      noro      148: #define CONV_TABLE (1U<<0)
                    149: #define CONV_SUBS (1U<<1)
                    150: #define CONV_DMODE (1U<<2)
1.18      noro      151:
                    152: static struct {
                    153:        char *name;
                    154:        Obj value;
                    155:        int (*reg)();
                    156: } qtot_env[] = {
                    157:        {"symbol_table",0,register_symbol_table},
                    158:        {"conv_rule",0,register_conv_rule},
1.38      noro      159:        {"conv_func",0,register_conv_func},
1.23      noro      160:        {"dp_vars",0,register_dp_vars},
1.25      noro      161:        {"dp_vars_prefix",0,register_dp_vars_prefix},
1.46      noro      162:        {"dp_dvars_prefix",0,register_dp_dvars_prefix},
                    163:        {"dp_vars_origin",0,register_dp_vars_origin},
                    164:        {"dp_dvars_origin",0,register_dp_dvars_origin},
1.31      noro      165:        {"dp_vars_hweyl",0,register_dp_vars_hweyl},
1.27      noro      166:        {"show_lt",0,register_show_lt},
1.18      noro      167:        {0,0,0},
                    168: };
                    169:
1.20      noro      170: #define PARTIAL "\\partial"
1.28      noro      171:
                    172: char *conv_rule(char *name)
                    173: {
                    174:        char *body,*r;
1.41      noro      175:        int len;
1.28      noro      176:
1.38      noro      177:        if ( convfunc )
                    178:                name = call_convfunc(name);
1.41      noro      179:        if ( conv_flag & CONV_TABLE ) {
                    180:                r = symbol_name(name);
                    181:                if ( r ) return r;
                    182:        }
                    183:        if ( (conv_flag & CONV_DMODE) && *name == 'd' ) {
                    184:                body = conv_rule(name+1);
                    185:                r = MALLOC_ATOMIC((strlen(PARTIAL)+strlen(body)+5)*sizeof(char));
                    186:                if ( !body || !(len=strlen(body)) )
                    187:                        strcpy(r,PARTIAL);
                    188:                else if ( len == 1 )
                    189:                        sprintf(r,"%s_%s",PARTIAL,body);
                    190:                else
                    191:                        sprintf(r,"%s_{%s}",PARTIAL,body);
                    192:                return r;
                    193:        } else
                    194:                return conv_subs(name);
1.28      noro      195: }
                    196:
1.40      noro      197: int _is_delimiter(char c)
                    198: {
                    199:        if ( (c == ' ' || c == '_' || c == ',') ) return 1;
                    200:        else return 0;
                    201: }
                    202:
                    203: int _is_alpha(char c)
                    204: {
                    205:        if ( isdigit(c) || c == '{' || _is_delimiter(c) ) return 0;
                    206:        else return 1;
                    207: }
                    208:
1.38      noro      209: char *conv_subs(char *name)
1.19      noro      210: {
1.29      noro      211:        int i,j,k,len,clen,slen,start,level;
1.41      noro      212:        char *buf,*head,*r,*h,*brace,*buf_conv;
1.28      noro      213:        char **subs;
                    214:
1.41      noro      215:        if ( !name || !(len=strlen(name)) ) return "";
                    216:        if ( !(conv_flag&CONV_SUBS) ) return name;
1.28      noro      217:        subs = (char **)ALLOCA(len*sizeof(char* ));
1.32      noro      218:        for ( i = 0, j = 0, start = i; ; j++ ) {
1.40      noro      219:                while ( (i < len) && _is_delimiter(name[i]) ) i++;
1.32      noro      220:                start = i;
1.28      noro      221:                if ( i == len ) break;
1.29      noro      222:                if ( name[i] == '{' ) {
                    223:                        for ( level = 1, i++; i < len && level; i++ ) {
                    224:                                if ( name[i] == '{' ) level++;
                    225:                                else if ( name[i] == '}' ) level--;
                    226:                        }
1.32      noro      227:                        slen = i-start;
1.37      noro      228:                        if ( slen >= 3 ) {
                    229:                                brace = (char *)ALLOCA((slen+1)*sizeof(char));
                    230:                                strncpy(brace,name+start+1,slen-2);
                    231:                                brace[slen-2] = 0;
1.38      noro      232:                                buf = conv_subs(brace);
1.37      noro      233:                                subs[j] = (char *)ALLOCA((strlen(buf)+3)*sizeof(char));
1.38      noro      234:                                if ( strlen(buf) == 1 )
                    235:                                        strcpy(subs[j],buf);
                    236:                                else
                    237:                                        sprintf(subs[j],"{%s}",buf);
1.37      noro      238:                        } else
                    239:                                subs[j] = "{}";
1.32      noro      240:                } else {
                    241:                        if ( isdigit(name[i]) )
                    242:                                while ( i < len && isdigit(name[i]) ) i++;
                    243:                        else
1.40      noro      244:                                while ( i < len && _is_alpha(name[i]) ) i++;
1.32      noro      245:                        slen = i-start;
                    246:                        buf = (char *)ALLOCA((slen+1)*sizeof(char));
                    247:                        strncpy(buf,name+start,slen); buf[slen] = 0;
1.41      noro      248:                        buf_conv = symbol_name(buf);
                    249:                        subs[j] = buf_conv?buf_conv:buf;
1.32      noro      250:                }
1.28      noro      251:        }
1.32      noro      252:        for ( k = 0, clen = 0; k < j; k++ ) clen += strlen(subs[k]);
                    253:        /* {subs(0)}_{{subs(1)},...,{subs(j-1)}} => {}:j+1 _:1 ,:j-2 */
                    254:        h = r = MALLOC_ATOMIC((clen+(j+1)*2+1+(j-2)+1)*sizeof(char));
                    255:        if ( j == 1 )
                    256:                sprintf(h,"{%s}",subs[0]);
1.28      noro      257:        else {
1.38      noro      258:                sprintf(h,"{%s}_{%s",subs[0],subs[1]);
1.28      noro      259:                h += strlen(h);
1.32      noro      260:                for ( k = 2; k < j; k++ ) {
1.38      noro      261:                        sprintf(h,",%s",subs[k]);
1.28      noro      262:                        h += strlen(h);
1.19      noro      263:                }
1.28      noro      264:                strcpy(h,"}");
1.20      noro      265:        }
                    266:        return r;
1.19      noro      267: }
                    268:
1.26      noro      269: char *call_convfunc(char *name)
                    270: {
                    271:        STRING str,r;
                    272:        NODE arg;
                    273:
                    274:        MKSTR(str,name);
                    275:        arg = mknode(1,str);
                    276:        r = (STRING)bevalf(convfunc,arg);
                    277:        if ( !r || OID(r) != O_STR )
                    278:                error("call_convfunc : invalid result");
                    279:        return BDY(r);
                    280: }
                    281:
1.18      noro      282: int register_symbol_table(Obj arg)
                    283: {
                    284:        NODE n,t;
                    285:        Obj b;
                    286:        STRING a0,a1;
                    287:        struct TeXSymbol *uts;
                    288:        int i,len;
                    289:
                    290:        /* check */
                    291:        if ( !arg ) {
                    292:                user_texsymbol = 0;
                    293:                return 1;
                    294:        }
                    295:        if ( OID(arg) != O_LIST ) return 0;
                    296:
                    297:        n = BDY((LIST)arg);
                    298:        len = length(n);
                    299:        uts = (struct TeXSymbol *)MALLOC((len+1)*sizeof(struct TeXSymbol));
                    300:        for ( i = 0; n; n = NEXT(n), i++ ) {
                    301:                b = (Obj)BDY(n);
                    302:                if ( !b || OID(b) != O_LIST ) return 0;
                    303:                t = BDY((LIST)b);
                    304:                if ( !t || !NEXT(t) ) return 0;
                    305:                a0 = (STRING)BDY(t);
                    306:                a1 = (STRING)BDY(NEXT(t));
1.23      noro      307:                if ( !a0 ) return 0;
                    308:                if ( OID(a0) == O_STR )
                    309:                        uts[i].text = BDY(a0);
                    310:                else if ( OID(a0) == O_P )
                    311:                        uts[i].text = NAME(VR((P)a0));
                    312:                else
                    313:                        return 0;
                    314:                if ( !a1 ) return 0;
                    315:                if ( OID(a1) == O_STR )
                    316:                        uts[i].symbol = BDY(a1);
                    317:                else if ( OID(a1) == O_P )
                    318:                        uts[i].symbol = NAME(VR((P)a1));
                    319:                else
                    320:                        return 0;
1.18      noro      321:        }
                    322:        uts[i].text = 0;
                    323:        uts[i].symbol = 0;
                    324:        user_texsymbol = uts;
                    325:        return 1;
                    326: }
                    327:
1.46      noro      328: int register_dp_vars_origin(Obj arg)
                    329: {
                    330:        if ( INT(arg) ) {
                    331:                dp_vars_origin = QTOS((Q)arg);
                    332:                return 1;
                    333:        } else return 0;
                    334: }
                    335:
                    336: int register_dp_dvars_origin(Obj arg)
                    337: {
                    338:        if ( INT(arg) ) {
                    339:                dp_dvars_origin = QTOS((Q)arg);
                    340:                return 1;
                    341:        } else return 0;
                    342: }
                    343:
1.31      noro      344: int register_dp_vars_hweyl(Obj arg)
                    345: {
                    346:        if ( INT(arg) ) {
                    347:                dp_vars_hweyl = QTOS((Q)arg);
                    348:                return 1;
                    349:        } else return 0;
                    350: }
                    351:
1.27      noro      352: int register_show_lt(Obj arg)
                    353: {
                    354:        if ( INT(arg) ) {
                    355:                show_lt = QTOS((Q)arg);
                    356:                return 1;
                    357:        } else return 0;
                    358: }
1.26      noro      359:
1.18      noro      360: int register_conv_rule(Obj arg)
                    361: {
1.19      noro      362:        if ( INT(arg) ) {
1.28      noro      363:                conv_flag = QTOS((Q)arg);
                    364:                convfunc = 0;
                    365:                return 1;
1.38      noro      366:        } else return 0;
                    367: }
                    368:
                    369: int register_conv_func(Obj arg)
                    370: {
1.43      noro      371:        if ( !arg ) {
                    372:                convfunc = 0;
                    373:                return 1;
                    374:        } else if ( OID(arg) == O_P && (int)(VR((P)arg))->attr == V_SR ) {
1.26      noro      375:                convfunc = (FUNC)(VR((P)arg)->priv);
                    376:                /* f must be a function which takes single argument */
                    377:                return 1;
1.19      noro      378:        } else return 0;
1.18      noro      379: }
                    380:
1.23      noro      381: int register_dp_vars(Obj arg)
                    382: {
                    383:        int l,i;
                    384:        char **r;
                    385:        NODE n;
                    386:        STRING a;
                    387:
                    388:        if ( !arg ) {
                    389:                dp_vars = 0;
                    390:                dp_vars_len = 0;
1.25      noro      391:                return 1;
1.23      noro      392:        } else if ( OID(arg) != O_LIST )
                    393:                return 0;
                    394:        else {
                    395:                n = BDY((LIST)arg);
                    396:                l = length(n);
                    397:                r = (char **)MALLOC_ATOMIC(l*sizeof(char *));
                    398:                for ( i = 0; i < l; i++, n = NEXT(n) ) {
                    399:                        a = (STRING)BDY(n);
                    400:                        if ( !a ) return 0;
                    401:                        if ( OID(a) == O_STR )
                    402:                                r[i] = BDY(a);
                    403:                        else if ( OID(a) == O_P )
                    404:                                r[i] = NAME(VR((P)a));
                    405:                        else
                    406:                                return 0;
                    407:                }
                    408:                dp_vars = r;
                    409:                dp_vars_len = l;
                    410:                return 1;
                    411:        }
                    412: }
                    413:
1.25      noro      414: int register_dp_vars_prefix(Obj arg)
                    415: {
                    416:        if ( !arg ) {
                    417:                dp_vars_prefix = 0;
                    418:                return 1;
                    419:        } else if ( OID(arg) == O_STR ) {
                    420:                dp_vars_prefix = BDY((STRING)arg);
                    421:                return 1;
                    422:        } else if ( OID(arg) == O_P ) {
                    423:                dp_vars_prefix = NAME(VR((P)arg));
                    424:                return 1;
                    425:        } else return 0;
                    426: }
                    427:
1.46      noro      428: int register_dp_dvars_prefix(Obj arg)
                    429: {
                    430:        if ( !arg ) {
                    431:                dp_dvars_prefix = 0;
                    432:                return 1;
                    433:        } else if ( OID(arg) == O_STR ) {
                    434:                dp_dvars_prefix = BDY((STRING)arg);
                    435:                return 1;
                    436:        } else if ( OID(arg) == O_P ) {
                    437:                dp_dvars_prefix = NAME(VR((P)arg));
                    438:                return 1;
                    439:        } else return 0;
                    440: }
                    441:
1.24      noro      442: void Pquotetotex_env(NODE arg,Obj *rp)
1.18      noro      443: {
                    444:        int ac,i;
                    445:        char *name;
                    446:        NODE n,n0;
                    447:        STRING s;
                    448:        LIST l;
                    449:
                    450:        ac = argc(arg);
                    451:        if ( !ac ) {
                    452:                n0 = 0;
                    453:                for ( i = 0; qtot_env[i].name; i++ ) {
                    454:                        NEXTNODE(n0,n); MKSTR(s,qtot_env[i].name); BDY(n) = (pointer)s;
                    455:                        NEXTNODE(n0,n); BDY(n) = (Q)qtot_env[i].value;
                    456:                }
                    457:                NEXT(n) = 0;
                    458:                MKLIST(l,n0);
                    459:                *rp = (Obj)l;
1.34      noro      460:        } else if ( ac == 1 && !ARG0(arg) ) {
                    461:                /* set to default */
1.43      noro      462:                for ( i = 0; qtot_env[i].name; i++ ) {
                    463:                        (qtot_env[i].reg)(0);
1.34      noro      464:                        qtot_env[i].value = 0;
1.43      noro      465:                }
1.34      noro      466:                *rp = 0;
1.18      noro      467:        } else if ( ac == 1 || ac == 2 ) {
1.24      noro      468:                asir_assert(ARG0(arg),O_STR,"quotetotex_env");
1.18      noro      469:                name = BDY((STRING)ARG0(arg));
                    470:                for ( i = 0; qtot_env[i].name; i++ )
                    471:                        if ( !strcmp(qtot_env[i].name,name) ) {
                    472:                                if ( ac == 2 ) {
                    473:                                        if ( (qtot_env[i].reg)((Obj)ARG1(arg)) )
                    474:                                                qtot_env[i].value = (Obj)ARG1(arg);
                    475:                                        else
1.24      noro      476:                                                error("quotetotex_env : invalid argument");
1.18      noro      477:                                }
                    478:                                *rp = qtot_env[i].value;
                    479:                                return;
                    480:                        }
                    481:                *rp = 0;
                    482:        } else
                    483:                *rp = 0;
                    484: }
                    485:
1.14      noro      486: void Pwrite_to_tb(NODE arg,Q *rp)
1.13      noro      487: {
                    488:        int i;
1.16      noro      489:        Obj obj;
                    490:        TB tb;
1.13      noro      491:
1.14      noro      492:        asir_assert(ARG1(arg),O_TB,"write_to_tb");
1.16      noro      493:        obj = ARG0(arg);
                    494:        if ( !obj )
                    495:                write_tb("",ARG1(arg));
                    496:        else if ( OID(obj) == O_STR )
                    497:                write_tb(BDY((STRING)obj),ARG1(arg));
                    498:        else if ( OID(obj) == O_TB ) {
                    499:                tb = (TB)obj;
                    500:                for ( i = 0; i < tb->next; i++ )
                    501:                        write_tb(tb->body[i],ARG1(arg));
                    502:        }
1.14      noro      503:        *rp = 0;
1.13      noro      504: }
                    505:
1.14      noro      506: void Pquotetotex(NODE arg,STRING *rp)
1.13      noro      507: {
1.14      noro      508:        TB tb;
1.13      noro      509:
1.14      noro      510:        NEWTB(tb);
1.27      noro      511:        /* XXX for DP */
                    512:        is_lt = 1;
1.14      noro      513:        fnodetotex_tb(BDY((QUOTE)ARG0(arg)),tb);
                    514:        tb_to_string(tb,rp);
1.13      noro      515: }
                    516:
1.14      noro      517: void Pquotetotex_tb(NODE arg,Q *rp)
1.13      noro      518: {
                    519:        int i;
1.14      noro      520:        TB tb;
1.13      noro      521:
1.14      noro      522:        asir_assert(ARG1(arg),O_TB,"quotetotex_tb");
1.27      noro      523:        /* XXX for DP */
                    524:        is_lt = 1;
1.14      noro      525:        fnodetotex_tb(BDY((QUOTE)ARG0(arg)),ARG1(arg));
1.13      noro      526:        *rp = 0;
                    527: }
                    528:
1.14      noro      529: void Pstring_to_tb(NODE arg,TB *rp)
                    530: {
                    531:        TB tb;
                    532:
                    533:        asir_assert(ARG0(arg),O_STR,"string_to_tb");
                    534:        NEWTB(tb);
                    535:        tb->body[0] = BDY((STRING)ARG0(arg));
                    536:        tb->next++;
                    537:        *rp = tb;
                    538: }
                    539:
                    540: void Ptb_to_string(NODE arg,STRING *rp)
                    541: {
                    542:        TB tb;
                    543:
                    544:        asir_assert(ARG0(arg),O_TB,"tb_to_string");
                    545:        tb = (TB)ARG0(arg);
                    546:        tb_to_string(tb,rp);
                    547: }
                    548:
                    549: void tb_to_string(TB tb,STRING *rp)
1.13      noro      550: {
1.14      noro      551:        int j,len;
1.13      noro      552:        char *all,*p,*q;
                    553:
1.14      noro      554:        for ( j = 0, len = 0; j < tb->next; j++ )
                    555:                len += strlen(tb->body[j]);
                    556:        all = (char *)MALLOC_ATOMIC((len+1)*sizeof(char));
                    557:        for ( j = 0, p = all; j < tb->next; j++ )
                    558:                for ( q = tb->body[j]; *q; *p++ = *q++ );
                    559:        *p = 0;
                    560:        MKSTR(*rp,all);
                    561: }
                    562:
                    563: void Pclear_tb(NODE arg,Q *rp)
                    564: {
                    565:        TB tb;
                    566:        int j;
                    567:
                    568:        asir_assert(ARG0(arg),O_TB,"clear_tb");
                    569:        tb = (TB)ARG0(arg);
                    570:        for ( j = 0; j < tb->next; j++ )
                    571:                tb->body[j] = 0;
                    572:        tb->next = 0;
                    573:        *rp = 0;
1.13      noro      574: }
1.5       noro      575:
                    576: void Pstr_len(arg,rp)
                    577: NODE arg;
                    578: Q *rp;
                    579: {
1.16      noro      580:        Obj obj;
                    581:        TB tb;
                    582:        int r,i;
1.5       noro      583:
1.16      noro      584:        obj = (Obj)ARG0(arg);
                    585:        if ( !obj || (OID(obj) != O_STR && OID(obj) != O_TB) )
                    586:                error("str_len : invalid argument");
                    587:        if ( OID(obj) == O_STR)
                    588:                r = strlen(BDY((STRING)obj));
                    589:        else if ( OID(obj) == O_TB ) {
                    590:                tb = (TB)obj;
                    591:                for ( r = i = 0; i < tb->next; i++ )
                    592:                        r += strlen(tb->body[i]);
                    593:        }
1.5       noro      594:        STOQ(r,*rp);
                    595: }
                    596:
                    597: void Pstr_chr(arg,rp)
                    598: NODE arg;
                    599: Q *rp;
                    600: {
                    601:        STRING str,terminator;
                    602:        Q start;
                    603:        char *p,*ind;
                    604:        int chr,spos,r;
                    605:
                    606:        str = (STRING)ARG0(arg);
                    607:        start = (Q)ARG1(arg);
                    608:        terminator = (STRING)ARG2(arg);
                    609:        asir_assert(str,O_STR,"str_chr");
                    610:        asir_assert(start,O_N,"str_chr");
                    611:        asir_assert(terminator,O_STR,"str_chr");
                    612:        p = BDY(str);
                    613:        spos = QTOS(start);
                    614:        chr = BDY(terminator)[0];
1.8       noro      615:        if ( spos > (int)strlen(p) )
1.5       noro      616:                r = -1;
                    617:        else {
                    618:                ind = strchr(p+spos,chr);
                    619:                if ( ind )
                    620:                        r = ind-p;
                    621:                else
                    622:                        r = -1;
                    623:        }
                    624:        STOQ(r,*rp);
                    625: }
                    626:
                    627: void Psub_str(arg,rp)
                    628: NODE arg;
                    629: STRING *rp;
                    630: {
                    631:        STRING str;
                    632:        Q head,tail;
                    633:        char *p,*r;
                    634:        int spos,epos,len;
                    635:
                    636:        str = (STRING)ARG0(arg);
                    637:        head = (Q)ARG1(arg);
                    638:        tail = (Q)ARG2(arg);
                    639:        asir_assert(str,O_STR,"sub_str");
                    640:        asir_assert(head,O_N,"sub_str");
                    641:        asir_assert(tail,O_N,"sub_str");
                    642:        p = BDY(str);
                    643:        spos = QTOS(head);
                    644:        epos = QTOS(tail);
                    645:        len = strlen(p);
                    646:        if ( (spos >= len) || (epos < spos) ) {
                    647:                *rp = 0; return;
                    648:        }
                    649:        if ( epos >= len )
                    650:                epos = len-1;
                    651:        len = epos-spos+1;
                    652:        r = (char *)MALLOC(len+1);
                    653:        strncpy(r,p+spos,len);
                    654:        r[len] = 0;
                    655:        MKSTR(*rp,r);
                    656: }
1.3       noro      657:
                    658: void Pstrtoascii(arg,rp)
                    659: NODE arg;
                    660: LIST *rp;
                    661: {
                    662:        STRING str;
                    663:        unsigned char *p;
                    664:        int len,i;
                    665:        NODE n,n1;
                    666:        Q q;
                    667:
                    668:        str = (STRING)ARG0(arg);
                    669:        asir_assert(str,O_STR,"strtoascii");
                    670:        p = BDY(str);
                    671:        len = strlen(p);
                    672:        for ( i = len-1, n = 0; i >= 0; i-- ) {
                    673:                UTOQ((unsigned int)p[i],q);
                    674:                MKNODE(n1,q,n);
                    675:                n = n1;
                    676:        }
                    677:        MKLIST(*rp,n);
                    678: }
                    679:
                    680: void Pasciitostr(arg,rp)
                    681: NODE arg;
                    682: STRING *rp;
                    683: {
                    684:        LIST list;
                    685:        unsigned char *p;
                    686:        int len,i,j;
                    687:        NODE n;
                    688:        Q q;
                    689:
                    690:        list = (LIST)ARG0(arg);
                    691:        asir_assert(list,O_LIST,"asciitostr");
                    692:        n = BDY(list);
                    693:        len = length(n);
                    694:        p = MALLOC_ATOMIC(len+1);
                    695:        for ( i = 0; i < len; i++, n = NEXT(n) ) {
                    696:                q = (Q)BDY(n);
                    697:                asir_assert(q,O_N,"asciitostr");
                    698:                j = QTOS(q);
1.4       noro      699:                if ( j >= 256 || j <= 0 )
1.3       noro      700:                        error("asciitostr : argument out of range");
                    701:                p[i] = j;
                    702:        }
                    703:        p[i] = 0;
                    704:        MKSTR(*rp,(char *)p);
                    705: }
1.1       noro      706:
                    707: void Peval_str(arg,rp)
                    708: NODE arg;
                    709: Obj *rp;
                    710: {
                    711:        FNODE fnode;
                    712:        char *cmd;
1.10      ohara     713: #if defined(PARI)
1.8       noro      714:        void recover(int);
                    715:
1.1       noro      716:        recover(0);
1.11      saito     717: #  if !(PARI_VERSION_CODE > 131588)
1.1       noro      718:        if ( setjmp(environnement) ) {
                    719:                avma = top; recover(1);
                    720:                resetenv("");
                    721:        }
1.11      saito     722: #  endif
1.1       noro      723: #endif
                    724:        cmd = BDY((STRING)ARG0(arg));
1.9       noro      725:        exprparse_create_var(0,cmd,&fnode);
1.1       noro      726:        *rp = eval(fnode);
                    727: }
                    728:
                    729: void Prtostr(arg,rp)
                    730: NODE arg;
                    731: STRING *rp;
                    732: {
                    733:        char *b;
                    734:        int len;
                    735:
1.2       noro      736:        len = estimate_length(CO,ARG0(arg));
1.12      noro      737:        b = (char *)MALLOC_ATOMIC(len+1);
1.1       noro      738:        soutput_init(b);
                    739:        sprintexpr(CO,ARG0(arg));
                    740:        MKSTR(*rp,b);
                    741: }
                    742:
                    743: void Pstrtov(arg,rp)
                    744: NODE arg;
                    745: P *rp;
                    746: {
1.8       noro      747:        char *p;
1.1       noro      748:
                    749:        p = BDY((STRING)ARG0(arg));
                    750: #if 0
                    751:        if ( !islower(*p) )
                    752:                *rp = 0;
                    753:        else {
                    754:                for ( t = p+1; t && (isalnum(*t) || *t == '_'); t++ );
                    755:                if ( *t )
                    756:                        *rp = 0;
                    757:                else
                    758:                        makevar(p,rp);
                    759:        }
                    760: #else
                    761:        makevar(p,rp);
                    762: #endif
1.14      noro      763: }
                    764:
1.15      noro      765: static struct TeXSymbol texsymbol[] = {
                    766:  {"sin","\\sin"},
                    767:  {"cos","\\cos"},
                    768:  {"tan","\\tan"},
                    769:  {"sinh","\\sinh"},
                    770:  {"cosh","\\cosh"},
                    771:  {"tanh","\\tanh"},
                    772:  {"exp","\\exp"},
                    773:  {"log","\\log"},
                    774:
                    775: /* Greek Letters (lower case) */
                    776:  {"alpha","\\alpha"},
                    777:  {"beta","\\beta"},
                    778:  {"gamma","\\gamma"},
                    779:  {"delta","\\delta"},
                    780:  {"epsilon","\\epsilon"},
                    781:  {"varepsilon","\\varepsilon"},
                    782:  {"zeta","\\zeta"},
                    783:  {"eta","\\eta"},
                    784:  {"theta","\\theta"},
                    785:  {"vartheta","\\vartheta"},
                    786:  {"iota","\\iota"},
                    787:  {"kappa","\\kappa"},
                    788:  {"lambda","\\lambda"},
                    789:  {"mu","\\mu"},
                    790:  {"nu","\\nu"},
                    791:  {"xi","\\xi"},
                    792:  {"pi","\\pi"},
                    793:  {"varpi","\\varpi"},
                    794:  {"rho","\\rho"},
                    795:  {"sigma","\\sigma"},
                    796:  {"varsigma","\\varsigma"},
                    797:  {"tau","\\tau"},
                    798:  {"upsilon","\\upsilon"},
                    799:  {"phi","\\phi"},
                    800:  {"varphi","\\varphi"},
                    801:  {"chi","\\chi"},
                    802:  {"omega","\\omega"},
                    803:
                    804: /* Greek Letters, (upper case) */
                    805:  {"ggamma","\\Gamma"},
                    806:  {"ddelta","\\Delta"},
                    807:  {"ttheta","\\Theta"},
                    808:  {"llambda","\\Lambda"},
                    809:  {"xxi","\\Xi"},
                    810:  {"ppi","\\Pi"},
                    811:  {"ssigma","\\Sigma"},
                    812:  {"uupsilon","\\Upsilon"},
                    813:  {"pphi","\\Phi"},
                    814:  {"ppsi","\\Psi"},
                    815:  {"oomega","\\Omega"},
                    816:
                    817:  /* Our own mathematical functions */
                    818:  {"algebra_tensor","\\otimes"},
                    819:  {"base_where","{\\rm \\ where \\ }"},
                    820:  /* Mathematical constants */
                    821:  {"c_pi","\\pi"},
                    822:  {"c_i","\\sqrt{-1}"},
                    823:
                    824:  /* Temporary  */
                    825:  {0,0}
                    826: };
                    827:
1.14      noro      828: char *symbol_name(char *name)
                    829: {
1.15      noro      830:        int i;
                    831:
1.41      noro      832:        if ( !name || strlen(name) == 0 )
                    833:                return "";
1.38      noro      834:        if ( !(conv_flag & CONV_TABLE) )
                    835:                return name;
                    836:
1.18      noro      837:        if ( user_texsymbol )
                    838:                for ( i = 0; user_texsymbol[i].text; i++ )
                    839:                        if ( !strcmp(user_texsymbol[i].text,name) )
                    840:                                return user_texsymbol[i].symbol;
1.15      noro      841:        for ( i = 0; texsymbol[i].text; i++ )
                    842:                if ( !strcmp(texsymbol[i].text,name) )
                    843:                        return texsymbol[i].symbol;
1.41      noro      844:        return 0;
1.14      noro      845: }
                    846:
                    847: void fnodetotex_tb(FNODE f,TB tb)
                    848: {
                    849:        NODE n,t,t0;
1.38      noro      850:        char vname[BUFSIZ],prefix[BUFSIZ];
                    851:        char *opname,*vname_conv,*prefix_conv;
1.14      noro      852:        Obj obj;
1.46      noro      853:        int i,len,allzero,elen,elen2,si;
1.40      noro      854:        C cplx;
                    855:        char *r;
1.17      noro      856:        FNODE fi,f2;
1.14      noro      857:
                    858:        write_tb(" ",tb);
                    859:        if ( !f ) {
                    860:                write_tb("0",tb);
                    861:                return;
                    862:        }
                    863:        switch ( f->id ) {
                    864:                /* unary operators */
1.23      noro      865:                case I_NOT:
                    866:                        write_tb("\\neg (",tb);
                    867:                        fnodetotex_tb((FNODE)FA0(f),tb);
                    868:                        write_tb(")",tb);
                    869:                        break;
                    870:                case I_PAREN:
                    871:                        write_tb("(",tb);
                    872:                        fnodetotex_tb((FNODE)FA0(f),tb);
                    873:                        write_tb(")",tb);
                    874:                        break;
                    875:                case I_MINUS:
                    876:                        write_tb("-",tb);
                    877:                        fnodetotex_tb((FNODE)FA0(f),tb);
                    878:                        break;
                    879:
                    880:                /* binary operators */
                    881:                /* arg list */
                    882:                /* I_AND, I_OR => FA0(f), FA1(f) */
                    883:                /* otherwise   => FA1(f), FA2(f) */
                    884:                case I_BOP:
                    885:                        opname = ((ARF)FA0(f))->name;
                    886:                        if ( !strcmp(opname,"+") ) {
                    887:                                fnodetotex_tb((FNODE)FA1(f),tb);
1.35      noro      888:                                if ( !top_is_minus((FNODE)FA2(f)) ) write_tb(opname,tb);
1.23      noro      889:                                fnodetotex_tb((FNODE)FA2(f),tb);
                    890:                        } else if ( !strcmp(opname,"-") ) {
                    891:                                if ( FA1(f) ) fnodetotex_tb((FNODE)FA1(f),tb);
                    892:                                write_tb(opname,tb);
                    893:                                fnodetotex_tb((FNODE)FA2(f),tb);
                    894:                        } else if ( !strcmp(opname,"*") ) {
                    895:                                fnodetotex_tb((FNODE)FA1(f),tb);
                    896:                                write_tb(" ",tb);
                    897:                                /* XXX special care for DP */
                    898:                                f2 = (FNODE)FA2(f);
                    899:                                if ( f2->id == I_EV ) {
                    900:                                        n = (NODE)FA0(f2);
                    901:                                        for ( i = 0; n; n = NEXT(n), i++ ) {
                    902:                                                fi = (FNODE)BDY(n);
                    903:                                                if ( fi->id != I_FORMULA || FA0(fi) )
                    904:                                                        break;
                    905:                                        }
                    906:                                        if ( n )
                    907:                                                fnodetotex_tb((FNODE)FA2(f),tb);
                    908:                                } else
                    909:                                        fnodetotex_tb((FNODE)FA2(f),tb);
                    910:                        } else if ( !strcmp(opname,"/") ) {
                    911:                                write_tb("\\frac{",tb);
                    912:                                fnodetotex_tb((FNODE)FA1(f),tb);
                    913:                                write_tb("} {",tb);
                    914:                                fnodetotex_tb((FNODE)FA2(f),tb);
                    915:                                write_tb("}",tb);
                    916:                        } else if ( !strcmp(opname,"^") ) {
                    917:                                fnodetotex_tb((FNODE)FA1(f),tb);
                    918:                                write_tb("^{",tb);
                    919:                                fnodetotex_tb((FNODE)FA2(f),tb);
                    920:                                write_tb("} ",tb);
                    921:                        } else if ( !strcmp(opname,"%") ) {
                    922:                                fnodetotex_tb((FNODE)FA1(f),tb);
                    923:                                write_tb(" {\\rm mod}\\, ",tb);
                    924:                                fnodetotex_tb((FNODE)FA2(f),tb);
                    925:                        } else
                    926:                                error("invalid binary operator");
                    927:                        break;
                    928:
                    929:                case I_COP:
                    930:                        switch( (cid)FA0(f) ) {
                    931:                                case C_EQ:
                    932:                                        fnodetotex_tb((FNODE)FA1(f),tb);
                    933:                                        write_tb(" = ",tb);
                    934:                                        fnodetotex_tb((FNODE)FA2(f),tb);
                    935:                                        break;
                    936:                                case C_NE:
                    937:                                        fnodetotex_tb((FNODE)FA1(f),tb);
                    938:                                        write_tb(" \\neq ",tb);
                    939:                                        fnodetotex_tb((FNODE)FA2(f),tb);
                    940:                                        break;
                    941:                                case C_GT:
                    942:                                        fnodetotex_tb((FNODE)FA1(f),tb);
1.45      noro      943:                                        write_tb(" > ",tb);
1.23      noro      944:                                        fnodetotex_tb((FNODE)FA2(f),tb);
                    945:                                        break;
                    946:                                case C_LT:
                    947:                                        fnodetotex_tb((FNODE)FA1(f),tb);
1.45      noro      948:                                        write_tb(" < ",tb);
1.23      noro      949:                                        fnodetotex_tb((FNODE)FA2(f),tb);
1.14      noro      950:                                        break;
1.23      noro      951:                                case C_GE:
                    952:                                        fnodetotex_tb((FNODE)FA1(f),tb);
                    953:                                        write_tb(" \\geq ",tb);
                    954:                                        fnodetotex_tb((FNODE)FA2(f),tb);
1.14      noro      955:                                        break;
1.23      noro      956:                                case C_LE:
                    957:                                        fnodetotex_tb((FNODE)FA1(f),tb);
                    958:                                        write_tb(" \\leq ",tb);
                    959:                                        fnodetotex_tb((FNODE)FA2(f),tb);
1.14      noro      960:                                        break;
                    961:                        }
                    962:                        break;
                    963:
1.23      noro      964:                case I_LOP:
                    965:                        switch( (lid)FA0(f) ) {
                    966:                                case L_EQ:
                    967:                                        fnodetotex_tb((FNODE)FA1(f),tb);
                    968:                                        write_tb(" = ",tb);
                    969:                                        fnodetotex_tb((FNODE)FA2(f),tb);
                    970:                                        break;
                    971:                                case L_NE:
                    972:                                        fnodetotex_tb((FNODE)FA1(f),tb);
                    973:                                        write_tb(" \\neq ",tb);
                    974:                                        fnodetotex_tb((FNODE)FA2(f),tb);
                    975:                                        break;
                    976:                                case L_GT:
                    977:                                        fnodetotex_tb((FNODE)FA1(f),tb);
1.45      noro      978:                                        write_tb(" > ",tb);
1.23      noro      979:                                        fnodetotex_tb((FNODE)FA2(f),tb);
                    980:                                        break;
                    981:                                case L_LT:
                    982:                                        fnodetotex_tb((FNODE)FA1(f),tb);
1.45      noro      983:                                        write_tb(" < ",tb);
1.23      noro      984:                                        fnodetotex_tb((FNODE)FA2(f),tb);
                    985:                                        break;
                    986:                                case L_GE:
                    987:                                        fnodetotex_tb((FNODE)FA1(f),tb);
                    988:                                        write_tb(" \\geq ",tb);
                    989:                                        fnodetotex_tb((FNODE)FA2(f),tb);
1.14      noro      990:                                        break;
1.23      noro      991:                                case L_LE:
                    992:                                        fnodetotex_tb((FNODE)FA1(f),tb);
                    993:                                        write_tb(" \\leq ",tb);
                    994:                                        fnodetotex_tb((FNODE)FA2(f),tb);
1.14      noro      995:                                        break;
1.23      noro      996:                                case L_AND:
                    997:                                        fnodetotex_tb((FNODE)FA1(f),tb);
1.14      noro      998:                                        write_tb(" {\\rm \\ and\\ } ",tb);
1.23      noro      999:                                        fnodetotex_tb((FNODE)FA2(f),tb);
                   1000:                                        break;
                   1001:                                case L_OR:
1.14      noro     1002:                                        fnodetotex_tb((FNODE)FA1(f),tb);
1.23      noro     1003:                                        write_tb(" {\\rm \\ or\\ } ",tb);
                   1004:                                        fnodetotex_tb((FNODE)FA2(f),tb);
1.14      noro     1005:                                        break;
1.23      noro     1006:                                case L_NOT:
                   1007:                                        /* XXX : L_NOT is a unary operator */
                   1008:                                        write_tb("\\neg (",tb);
1.14      noro     1009:                                        fnodetotex_tb((FNODE)FA1(f),tb);
1.23      noro     1010:                                        write_tb(")",tb);
                   1011:                                        return;
1.14      noro     1012:                        }
                   1013:                        break;
                   1014:
1.23      noro     1015:                case I_AND:
                   1016:                        fnodetotex_tb((FNODE)FA0(f),tb);
                   1017:                        write_tb(" {\\rm \\ and\\ } ",tb);
                   1018:                        fnodetotex_tb((FNODE)FA1(f),tb);
                   1019:                        break;
                   1020:
                   1021:                case I_OR:
                   1022:                        fnodetotex_tb((FNODE)FA0(f),tb);
                   1023:                        write_tb(" {\\rm \\ or\\ } ",tb);
                   1024:                        fnodetotex_tb((FNODE)FA1(f),tb);
                   1025:                        break;
                   1026:
1.14      noro     1027:                /* ternary operators */
                   1028:                case I_CE:
                   1029:                        error("fnodetotex_tb : not implemented yet");
                   1030:                        break;
                   1031:
                   1032:                /* lists */
                   1033:                case I_LIST:
                   1034:                        write_tb(" [ ",tb);
                   1035:                        n = (NODE)FA0(f);
                   1036:                        fnodenodetotex_tb(n,tb);
                   1037:                        write_tb("]",tb);
                   1038:                        break;
                   1039:
                   1040:                /* function */
1.23      noro     1041:                case I_FUNC:
1.40      noro     1042:                        if ( !strcmp(((FUNC)FA0(f))->name,"@pi") )
                   1043:                                write_tb("\\pi",tb);
                   1044:                        else if ( !strcmp(((FUNC)FA0(f))->name,"@e") )
                   1045:                                write_tb("e",tb);
                   1046:                        else {
                   1047:                                opname = conv_rule(((FUNC)FA0(f))->name);
                   1048:                                write_tb(opname,tb);
                   1049:                                write_tb("(",tb);
                   1050:                                fargstotex_tb(opname,FA1(f),tb);
                   1051:                                write_tb(")",tb);
                   1052:                        }
1.23      noro     1053:                        break;
                   1054:
                   1055:                /* XXX */
                   1056:                case I_CAR:
1.28      noro     1057:                        opname = conv_rule("car");
1.23      noro     1058:                        write_tb(opname,tb);
                   1059:                        write_tb("(",tb);
                   1060:                        fargstotex_tb(opname,FA0(f),tb);
                   1061:                        write_tb(")",tb);
                   1062:                        break;
                   1063:
                   1064:                case I_CDR:
1.28      noro     1065:                        opname = conv_rule("cdr");
1.23      noro     1066:                        write_tb(opname,tb);
                   1067:                        write_tb("(",tb);
                   1068:                        fargstotex_tb(opname,FA0(f),tb);
                   1069:                        write_tb(")",tb);
                   1070:                        break;
                   1071:
                   1072:                /* exponent vector */
                   1073:                case I_EV:
                   1074:                        n = (NODE)FA0(f);
1.31      noro     1075:                        if ( dp_vars_hweyl ) {
                   1076:                                elen = length(n);
                   1077:                                elen2 = elen>>1;
                   1078:                                elen = elen2<<1;
                   1079:                        }
1.23      noro     1080:                        allzero = 1;
1.27      noro     1081:                        if ( show_lt && is_lt )
                   1082:                                write_tb("\\underline{",tb);
1.23      noro     1083:                        for ( t0 = 0, i = 0; n; n = NEXT(n), i++ ) {
                   1084:                                fi = (FNODE)BDY(n);
                   1085:                                if ( fi->id == I_FORMULA && !FA0(fi) ) continue;
                   1086:                                allzero = 0;
1.38      noro     1087:                                if ( dp_vars && i < dp_vars_len ) {
                   1088:                                        strcpy(vname,dp_vars[i]);
                   1089:                                        vname_conv = conv_rule(vname);
                   1090:                                } else {
                   1091:                                        if ( dp_vars_hweyl ) {
1.44      noro     1092:                                                if ( i < elen2 ) {
1.38      noro     1093:                                                        strcpy(prefix,dp_vars_prefix?dp_vars_prefix:"x");
1.44      noro     1094:                                                        prefix_conv = conv_rule(prefix);
                   1095:                                                        vname_conv = (char *)ALLOCA(strlen(prefix_conv)+50);
1.46      noro     1096:                                                        si = i+dp_vars_origin;
                   1097:                                                        sprintf(vname_conv,(si>=0&&si<10)?"%s_%d":"%s_{%d}",
                   1098:                                                                prefix_conv,si);
1.44      noro     1099:                                                } else if ( i < elen ) {
1.46      noro     1100:                                                        strcpy(prefix,
                   1101:                                                                dp_dvars_prefix?dp_dvars_prefix:"\\partial");
1.44      noro     1102:                                                        prefix_conv = conv_rule(prefix);
                   1103:                                                        vname_conv = (char *)ALLOCA(strlen(prefix_conv)+50);
1.46      noro     1104:                                                        si = i+dp_dvars_origin-elen2;
                   1105:                                                        sprintf(vname_conv,(si>=0&&si<10)?"%s_%d":"%s_{%d}",
                   1106:                                                                prefix_conv,si);
1.44      noro     1107:                                                } else {
1.38      noro     1108:                                                        strcpy(prefix,"h");
1.44      noro     1109:                                                        vname_conv = conv_rule(prefix);
                   1110:                                                }
                   1111:                                        } else {
1.38      noro     1112:                                                strcpy(prefix,dp_vars_prefix?dp_vars_prefix:"x");
1.44      noro     1113:                                                prefix_conv = conv_rule(prefix);
                   1114:                                                vname_conv = (char *)ALLOCA(strlen(prefix_conv)+50);
1.46      noro     1115:                                                si = i+dp_vars_origin;
                   1116:                                                sprintf(vname_conv,(si>=0&&si<10)?"%s_%d":"%s_{%d}",
                   1117:                                                        prefix_conv,si);
1.44      noro     1118:                                        }
1.38      noro     1119:                                }
1.23      noro     1120:                                if ( fi->id == I_FORMULA && UNIQ(FA0(fi)) ) {
                   1121:                                        len = strlen(vname_conv);
                   1122:                                        opname = MALLOC_ATOMIC(len+2);
                   1123:                                        sprintf(opname,"%s ",vname_conv);
1.14      noro     1124:                                        write_tb(opname,tb);
1.23      noro     1125:                                } else {
                   1126:                                        len = strlen(vname_conv);
                   1127:                                        /* 2: ^{ */
                   1128:                                        opname = MALLOC_ATOMIC(len+1+2);
                   1129:                                        sprintf(opname,"%s^{",vname_conv);
1.14      noro     1130:                                        write_tb(opname,tb);
1.23      noro     1131:                                        fnodetotex_tb((FNODE)BDY(n),tb);
                   1132:                                        write_tb("} ",tb);
                   1133:                                }
1.14      noro     1134:                        }
1.23      noro     1135:                        /* XXX */
                   1136:                        if ( allzero )
                   1137:                                write_tb(" 1 ",tb);
1.27      noro     1138:                        if ( show_lt && is_lt ) {
                   1139:                                write_tb("}",tb);
                   1140:                                is_lt = 0;
                   1141:                        }
1.14      noro     1142:                        break;
                   1143:
1.23      noro     1144:                /* string */
1.14      noro     1145:                case I_STR:
                   1146:                        write_tb((char *)FA0(f),tb);
                   1147:                        break;
                   1148:
1.23      noro     1149:                /* internal object */
1.14      noro     1150:                case I_FORMULA:
                   1151:                        obj = (Obj)FA0(f);
1.40      noro     1152:                        if ( !obj )
1.42      noro     1153:                                write_tb("0",tb);
1.40      noro     1154:                        else if ( OID(obj) == O_N && NID(obj) == N_C ) {
                   1155:                                cplx = (C)obj;
                   1156:                                write_tb("(",tb);
                   1157:                                if ( cplx->r ) {
                   1158:                                        r = objtostr((Obj)cplx->r); write_tb(r,tb);
                   1159:                                }
                   1160:                                if ( cplx->i ) {
                   1161:                                        if ( cplx->r && compnum(0,cplx->i,0) > 0 ) {
                   1162:                                                write_tb("+",tb);
                   1163:                                                if ( !UNIQ(cplx->i) ) {
                   1164:                                                        r = objtostr((Obj)cplx->i); write_tb(r,tb);
                   1165:                                                }
                   1166:                                        } else if ( MUNIQ(cplx->i) )
                   1167:                                                write_tb("-",tb);
                   1168:                                        else if ( !UNIQ(cplx->i) ) {
                   1169:                                                r = objtostr((Obj)cplx->i); write_tb(r,tb);
                   1170:                                        }
                   1171:                                        write_tb("\\sqrt{-1}",tb);
                   1172:                                }
                   1173:                                write_tb(")",tb);
                   1174:                        } else if ( OID(obj) == O_P )
                   1175:                                write_tb(conv_rule(VR((P)obj)->name),tb);
                   1176:                        else
                   1177:                                write_tb(objtostr(obj),tb);
1.14      noro     1178:                        break;
                   1179:
1.23      noro     1180:                /* program variable */
1.14      noro     1181:                case I_PVAR:
                   1182:                        if ( FA1(f) )
                   1183:                                error("fnodetotex_tb : not implemented yet");
                   1184:                        GETPVNAME(FA0(f),opname);
                   1185:                        write_tb(opname,tb);
                   1186:                        break;
                   1187:
                   1188:                default:
                   1189:                        error("fnodetotex_tb : not implemented yet");
                   1190:        }
1.40      noro     1191: }
                   1192:
                   1193: char *objtostr(Obj obj)
                   1194: {
                   1195:        int len;
                   1196:        char *r;
                   1197:
                   1198:        len = estimate_length(CO,obj);
                   1199:        r = (char *)MALLOC_ATOMIC(len+1);
                   1200:        soutput_init(r);
                   1201:        sprintexpr(CO,obj);
                   1202:        return r;
1.50    ! ohara    1203: }
        !          1204:
        !          1205: void Psprintf(NODE arg,STRING *rp)
        !          1206: {
        !          1207:     STRING string;
        !          1208:     char *s,*t,*r;
        !          1209:     int argc,n,len;
        !          1210:     NODE node;
        !          1211:
        !          1212:     string = (STRING)ARG0(arg);
        !          1213:     asir_assert(string,O_STR,"sprintf");
        !          1214:     s = BDY(string);
        !          1215:     for(n = 0, t = s; *t; t++) {
        !          1216:         if (*t=='%' && *(t+1)=='a') {
        !          1217:             n++;
        !          1218:         }
        !          1219:     }
        !          1220:     for(node = NEXT(arg), argc = 0, len = strlen(s); node; node = NEXT(node), argc++) {
        !          1221:         len += estimate_length(CO,BDY(node));
        !          1222:     }
        !          1223:     if (argc < n) {
        !          1224:         error("sprintf: invalid argument");
        !          1225:     }
        !          1226:     r = (char *)MALLOC_ATOMIC(len);
        !          1227:     for(node = NEXT(arg), t = r; *s; s++) {
        !          1228:         if (*s=='%' && *(s+1)=='a') {
        !          1229:             strcpy(t,objtostr(BDY(node)));
        !          1230:             node = NEXT(node);
        !          1231:             t = strchr(t,0);
        !          1232:             s++;
        !          1233:         }else {
        !          1234:             *t++ = *s;
        !          1235:         }
        !          1236:     }
        !          1237:     *t = 0;
        !          1238:     MKSTR(*rp,r);
1.14      noro     1239: }
                   1240:
                   1241: void fnodenodetotex_tb(NODE n,TB tb)
                   1242: {
                   1243:        for ( ; n; n = NEXT(n) ) {
1.27      noro     1244:                is_lt = 1;
1.14      noro     1245:                fnodetotex_tb((FNODE)BDY(n),tb);
                   1246:                if ( NEXT(n) ) write_tb(", ",tb);
                   1247:        }
                   1248: }
                   1249:
                   1250: void fargstotex_tb(char *name,FNODE f,TB tb)
                   1251: {
                   1252:        NODE n;
                   1253:
                   1254:        if ( !strcmp(name,"matrix") ) {
                   1255:                error("fargstotex_tb : not implemented yet");
                   1256:        } else if ( !strcmp(name,"vector") ) {
                   1257:                error("fargstotex_tb : not implemented yet");
                   1258:        } else {
                   1259:                if ( f->id == I_LIST ) {
                   1260:                        n = (NODE)FA0(f);
                   1261:                        fnodenodetotex_tb(n,tb);
                   1262:                } else
                   1263:                        fnodetotex_tb(f,tb);
1.35      noro     1264:        }
                   1265: }
                   1266:
                   1267: int top_is_minus(FNODE f)
                   1268: {
                   1269:        char *opname;
                   1270:        int len;
                   1271:        Obj obj;
                   1272:
                   1273:        if ( !f )
                   1274:                return 0;
                   1275:        switch ( f->id ) {
                   1276:                case I_MINUS:
                   1277:                        return 1;
                   1278:                case I_BOP:
                   1279:                        opname = ((ARF)FA0(f))->name;
                   1280:                        switch ( opname[0] ) {
                   1281:                                case '+': case '*': case '/': case '^': case '%':
                   1282:                                        return top_is_minus((FNODE)FA1(f));
                   1283:                                case '-':
                   1284:                                        if ( FA1(f) )
                   1285:                                                return top_is_minus((FNODE)FA1(f));
                   1286:                                        else
                   1287:                                                return 1;
                   1288:                                default:
                   1289:                                        return 0;
                   1290:                        }
                   1291:                        break;
                   1292:                case I_COP:
                   1293:                        return top_is_minus((FNODE)FA1(f));
                   1294:                case I_LOP:
                   1295:                        if ( (lid)FA0(f) == L_NOT ) return 0;
                   1296:                        else return top_is_minus((FNODE)FA1(f));
                   1297:                case I_AND: case I_OR:
                   1298:                        return top_is_minus((FNODE)FA0(f));
                   1299:                case I_FORMULA:
                   1300:                        obj = (Obj)FA0(f);
1.36      noro     1301:                        if ( !obj )
                   1302:                                return 0;
                   1303:                        else {
                   1304:                                switch ( OID(obj) ) {
                   1305:                                        case O_N:
                   1306:                                                return mmono((P)obj);
                   1307:                                        case O_P:
                   1308:                                                /* must be a variable */
                   1309:                                                opname = conv_rule(VR((P)obj)->name);
                   1310:                                                return opname[0]=='-';
                   1311:                                        default:
                   1312:                                                /* ??? */
                   1313:                                                len = estimate_length(CO,obj);
                   1314:                                                opname = (char *)MALLOC_ATOMIC(len+1);
                   1315:                                                soutput_init(opname);
                   1316:                                                sprintexpr(CO,obj);
                   1317:                                                return opname[0]=='-';
                   1318:                                }
1.35      noro     1319:                        }
                   1320:                default:
                   1321:                        return 0;
1.14      noro     1322:        }
1.47      noro     1323: }
                   1324:
                   1325: FNODE flatten_fnode(FNODE,char *);
                   1326:
1.48      noro     1327: void Pflatten_quote(NODE arg,Obj *rp)
1.47      noro     1328: {
                   1329:        FNODE f;
                   1330:        QUOTE q;
                   1331:
1.48      noro     1332:        if ( !ARG0(arg) || OID((Obj)ARG0(arg)) != O_QUOTE )
                   1333:                *rp = (Obj)ARG0(arg);
                   1334:        else {
                   1335:                f = flatten_fnode(BDY((QUOTE)ARG0(arg)),BDY((STRING)ARG1(arg)));
                   1336:                MKQUOTE(q,f);
                   1337:                *rp = (Obj)q;
                   1338:        }
                   1339: }
                   1340:
                   1341: void Pquote_to_funargs(NODE arg,LIST *rp)
                   1342: {
                   1343:        fid_spec_p spec;
                   1344:        QUOTE q;
                   1345:        QUOTEARG qa;
                   1346:        FNODE f;
                   1347:        STRING s;
                   1348:        QUOTE r;
                   1349:        int i;
                   1350:        Q id,a;
1.49      noro     1351:        LIST l;
                   1352:        NODE t0,t,w,u,u0;
1.48      noro     1353:
                   1354:        q = (QUOTE)ARG0(arg);
                   1355:        if ( !q || OID(q) != O_QUOTE )
                   1356:                error("quote_to_funargs : invalid argument");
                   1357:        f = BDY(q);
                   1358:        if ( !f ) {
                   1359:                MKLIST(*rp,0);
                   1360:                return;
                   1361:        }
                   1362:        get_fid_spec(f->id,&spec);
                   1363:        if ( !spec )
                   1364:                error("quote_to_funargs : not supported yet");
                   1365:        t0 = 0;
                   1366:        STOQ((int)f->id,id);
                   1367:        NEXTNODE(t0,t);
                   1368:        BDY(t) = (pointer)id;
                   1369:        for ( i = 0; spec->type[i] != A_end; i++ ) {
                   1370:                NEXTNODE(t0,t);
                   1371:                switch ( spec->type[i] ) {
                   1372:                        case A_fnode:
                   1373:                                MKQUOTE(r,(FNODE)f->arg[i]);
                   1374:                                BDY(t) = (pointer)r;
                   1375:                                break;
                   1376:                        case A_int:
                   1377:                                STOQ((int)f->arg[i],a);
                   1378:                                BDY(t) = (pointer)a;
                   1379:                                break;
                   1380:                        case A_str:
                   1381:                                MKSTR(s,(char *)f->arg[i]);
                   1382:                                BDY(t) = (pointer)s;
                   1383:                                break;
                   1384:                        case A_internal:
                   1385:                                BDY(t) = (pointer)f->arg[i];
                   1386:                                break;
1.49      noro     1387:                        case A_node:
                   1388:                                w = (NODE)f->arg[i];
                   1389:                                for ( u0 = 0; w; w = NEXT(w) ){
                   1390:                                        NEXTNODE(u0,u);
                   1391:                                        MKQUOTE(r,(FNODE)BDY(w));
                   1392:                                        BDY(u) = (pointer)r;
                   1393:                                }
                   1394:                                if ( u0 ) NEXT(u) = 0;
                   1395:                                MKLIST(l,u0);
                   1396:                                BDY(t) = (pointer)l;
                   1397:                                break;
1.48      noro     1398:                        default:
                   1399:                                MKQUOTEARG(qa,spec->type[i],f->arg[i]);
                   1400:                                BDY(t) = (pointer)qa;
                   1401:                                break;
                   1402:                }
                   1403:        }
                   1404:        if ( t0 ) NEXT(t) = 0;
                   1405:        MKLIST(*rp,t0);
                   1406: }
                   1407:
                   1408: void Pfunargs_to_quote(NODE arg,QUOTE *rp)
                   1409: {
                   1410:        fid_spec_p spec;
                   1411:        QUOTE q;
                   1412:        QUOTEARG qa;
                   1413:        FNODE f;
                   1414:        STRING s;
1.49      noro     1415:        QUOTE r,b;
1.48      noro     1416:        int i;
                   1417:        LIST l;
                   1418:        fid id;
                   1419:        Obj a;
1.49      noro     1420:        NODE t0,t,u0,u,w;
1.48      noro     1421:
                   1422:        l = (LIST)ARG0(arg);
                   1423:        if ( !l || OID(l) != O_LIST || !(t=BDY(l)) )
                   1424:                error("funargs_to_quote : invalid argument");
                   1425:        t = BDY(l);
                   1426:        id = (fid)QTOS((Q)BDY(t)); t = NEXT(t);
                   1427:        get_fid_spec(id,&spec);
                   1428:        if ( !spec )
                   1429:                error("funargs_to_quote : not supported yet");
                   1430:        for ( i = 0; spec->type[i] != A_end; i++ );
                   1431:        NEWFNODE(f,i);
                   1432:        f->id = id;
                   1433:        for ( i = 0; spec->type[i] != A_end; i++, t = NEXT(t) ) {
                   1434:                if ( !t )
                   1435:                        error("funargs_to_quote : argument mismatch");
                   1436:                a = (Obj)BDY(t);
                   1437:                switch ( spec->type[i] ) {
                   1438:                        case A_fnode:
                   1439:                                if ( !a || OID(a) != O_QUOTE )
                   1440:                                        error("funargs_to_quote : invalid argument");
                   1441:                                f->arg[i] = BDY((QUOTE)a);
                   1442:                                break;
                   1443:                        case A_int:
                   1444:                                if ( !INT(a) )
                   1445:                                        error("funargs_to_quote : invalid argument");
                   1446:                                f->arg[i] = (pointer)QTOS((Q)a);
                   1447:                                break;
                   1448:                        case A_str:
                   1449:                                if ( !a || OID(a) != O_STR )
                   1450:                                        error("funargs_to_quote : invalid argument");
                   1451:                                f->arg[i] = (pointer)BDY((STRING)a);
                   1452:                                break;
                   1453:                        case A_internal:
                   1454:                                f->arg[i] = (pointer)a;
1.49      noro     1455:                                break;
                   1456:                        case A_node:
                   1457:                                if ( !a || OID(a) != O_LIST )
                   1458:                                        error("funargs_to_quote : invalid argument");
                   1459:                                u0 = 0;
                   1460:                                for ( w = BDY((LIST)a); w; w = NEXT(w) ) {
                   1461:                                        NEXTNODE(u0,u);
                   1462:                                        b = (QUOTE)BDY(w);
                   1463:                                        if ( !b || OID(b) != O_QUOTE )
                   1464:                                                error("funargs_to_quote : invalid argument");
                   1465:                                        BDY(u) = BDY(b);
                   1466:                                }
                   1467:                                if ( u0 ) NEXT(u) = 0;
                   1468:                                f->arg[i] = (pointer)u0;
1.48      noro     1469:                                break;
                   1470:                        default:
                   1471:                                if ( !a || OID(a) != O_QUOTEARG ||
                   1472:                                        ((QUOTEARG)a)->type != spec->type[i] )
                   1473:                                        error("funargs_to_quote : invalid argument");
                   1474:                                f->arg[i] = BDY((QUOTEARG)a);
                   1475:                                break;
                   1476:                }
                   1477:        }
                   1478:        MKQUOTE(*rp,f);
1.1       noro     1479: }

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