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

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.69    ! noro       48:  * $OpenXM: OpenXM_contrib2/asir2000/builtin/strobj.c,v 1.68 2005/10/05 07:38:08 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.69    ! noro       80:
        !            81: void Pquote_is_integer(),Pquote_is_rational(),Pquote_is_number();
        !            82: void Pquote_is_dependent(),Pquote_is_function();
        !            83:
1.52      noro       84: void Pquote_to_funargs(),Pfunargs_to_quote(),Pget_function_name();
1.68      noro       85: void Pquote_unify(),Pget_quote_id(),Pquote_match_rewrite();
1.64      noro       86: void Pquote_to_nary(),Pquote_to_bin();
1.58      ohara      87: void do_assign(NODE arg);
1.14      noro       88: void fnodetotex_tb(FNODE f,TB tb);
                     89: char *symbol_name(char *name);
1.28      noro       90: char *conv_rule(char *name);
1.38      noro       91: char *conv_subs(char *name);
1.28      noro       92: char *call_convfunc(char *name);
1.14      noro       93: void tb_to_string(TB tb,STRING *rp);
                     94: void fnodenodetotex_tb(NODE n,TB tb);
                     95: void fargstotex_tb(char *opname,FNODE f,TB tb);
1.35      noro       96: int top_is_minus(FNODE f);
1.56      noro       97: int quote_unify(Obj f,Obj pat,NODE *rp);
1.1       noro       98:
                     99: struct ftab str_tab[] = {
1.50      ohara     100:        {"sprintf",Psprintf,-99999999},
1.1       noro      101:        {"rtostr",Prtostr,1},
                    102:        {"strtov",Pstrtov,1},
                    103:        {"eval_str",Peval_str,1},
1.3       noro      104:        {"strtoascii",Pstrtoascii,1},
                    105:        {"asciitostr",Pasciitostr,1},
1.5       noro      106:        {"str_len",Pstr_len,1},
                    107:        {"str_chr",Pstr_chr,3},
                    108:        {"sub_str",Psub_str,3},
1.14      noro      109:        {"write_to_tb",Pwrite_to_tb,2},
                    110:        {"clear_tb",Pclear_tb,1},
                    111:        {"tb_to_string",Ptb_to_string,1},
                    112:        {"string_to_tb",Pstring_to_tb,1},
1.63      noro      113:        {"get_quote_id",Pget_quote_id,1},
1.69    ! noro      114:
        !           115:        {"quote_is_number",Pquote_is_number,1},
        !           116:        {"quote_is_rational",Pquote_is_rational,1},
        !           117:        {"quote_is_integer",Pquote_is_integer,1},
        !           118:        {"quote_is_function",Pquote_is_function,1},
        !           119:        {"quote_is_dependent",Pquote_is_dependent,2},
        !           120:
1.64      noro      121:        {"quote_to_nary",Pquote_to_nary,1},
                    122:        {"quote_to_bin",Pquote_to_bin,2},
1.60      noro      123:        {"quotetotex_tb",Pquotetotex_tb,2},
                    124:        {"quotetotex",Pquotetotex,1},
                    125:        {"quotetotex_env",Pquotetotex_env,-99999999},
1.65      noro      126:        {"flatten_quote",Pflatten_quote,-2},
1.60      noro      127:        {"quote_to_funargs",Pquote_to_funargs,1},
1.62      noro      128:        {"quote_unify",Pquote_unify,2},
1.68      noro      129:        {"quote_match_rewrite",Pquote_match_rewrite,-4},
1.48      noro      130:        {"funargs_to_quote",Pfunargs_to_quote,1},
1.52      noro      131:        {"get_function_name",Pget_function_name,1},
1.1       noro      132:        {0,0,0},
                    133: };
1.13      noro      134:
1.14      noro      135: void write_tb(char *s,TB tb)
                    136: {
                    137:        if ( tb->next == tb->size ) {
                    138:                tb->size *= 2;
                    139:                tb->body = (char **)REALLOC(tb->body,tb->size*sizeof(char *));
                    140:        }
                    141:        tb->body[tb->next] = s;
                    142:        tb->next++;
                    143: }
1.13      noro      144:
1.18      noro      145: int register_symbol_table(Obj arg);
                    146: int register_conv_rule(Obj arg);
1.38      noro      147: int register_conv_func(Obj arg);
1.23      noro      148: int register_dp_vars(Obj arg);
1.46      noro      149: int register_dp_vars_origin(Obj arg);
                    150: int register_dp_dvars_origin(Obj arg);
                    151: int register_dp_dvars_prefix(Obj arg);
1.25      noro      152: int register_dp_vars_prefix(Obj arg);
1.31      noro      153: int register_dp_vars_hweyl(Obj arg);
1.27      noro      154: int register_show_lt(Obj arg);
1.40      noro      155: char *objtostr(Obj obj);
1.18      noro      156: static struct TeXSymbol *user_texsymbol;
1.23      noro      157: static char **dp_vars;
1.25      noro      158: static int dp_vars_len;
                    159: static char *dp_vars_prefix;
1.46      noro      160: static char *dp_dvars_prefix;
                    161: static int dp_vars_origin;
                    162: static int dp_dvars_origin;
1.27      noro      163: static int show_lt;
1.26      noro      164: static FUNC convfunc;
1.27      noro      165: static int is_lt;
1.28      noro      166: static int conv_flag;
1.31      noro      167: static int dp_vars_hweyl;
1.28      noro      168:
1.38      noro      169: #define CONV_TABLE (1U<<0)
                    170: #define CONV_SUBS (1U<<1)
                    171: #define CONV_DMODE (1U<<2)
1.18      noro      172:
                    173: static struct {
                    174:        char *name;
                    175:        Obj value;
                    176:        int (*reg)();
                    177: } qtot_env[] = {
                    178:        {"symbol_table",0,register_symbol_table},
                    179:        {"conv_rule",0,register_conv_rule},
1.38      noro      180:        {"conv_func",0,register_conv_func},
1.23      noro      181:        {"dp_vars",0,register_dp_vars},
1.25      noro      182:        {"dp_vars_prefix",0,register_dp_vars_prefix},
1.46      noro      183:        {"dp_dvars_prefix",0,register_dp_dvars_prefix},
                    184:        {"dp_vars_origin",0,register_dp_vars_origin},
                    185:        {"dp_dvars_origin",0,register_dp_dvars_origin},
1.31      noro      186:        {"dp_vars_hweyl",0,register_dp_vars_hweyl},
1.27      noro      187:        {"show_lt",0,register_show_lt},
1.18      noro      188:        {0,0,0},
                    189: };
                    190:
1.20      noro      191: #define PARTIAL "\\partial"
1.28      noro      192:
                    193: char *conv_rule(char *name)
                    194: {
                    195:        char *body,*r;
1.41      noro      196:        int len;
1.28      noro      197:
1.38      noro      198:        if ( convfunc )
                    199:                name = call_convfunc(name);
1.41      noro      200:        if ( conv_flag & CONV_TABLE ) {
                    201:                r = symbol_name(name);
                    202:                if ( r ) return r;
                    203:        }
                    204:        if ( (conv_flag & CONV_DMODE) && *name == 'd' ) {
                    205:                body = conv_rule(name+1);
                    206:                r = MALLOC_ATOMIC((strlen(PARTIAL)+strlen(body)+5)*sizeof(char));
                    207:                if ( !body || !(len=strlen(body)) )
                    208:                        strcpy(r,PARTIAL);
                    209:                else if ( len == 1 )
                    210:                        sprintf(r,"%s_%s",PARTIAL,body);
                    211:                else
                    212:                        sprintf(r,"%s_{%s}",PARTIAL,body);
                    213:                return r;
                    214:        } else
                    215:                return conv_subs(name);
1.28      noro      216: }
                    217:
1.40      noro      218: int _is_delimiter(char c)
                    219: {
                    220:        if ( (c == ' ' || c == '_' || c == ',') ) return 1;
                    221:        else return 0;
                    222: }
                    223:
                    224: int _is_alpha(char c)
                    225: {
                    226:        if ( isdigit(c) || c == '{' || _is_delimiter(c) ) return 0;
                    227:        else return 1;
                    228: }
                    229:
1.38      noro      230: char *conv_subs(char *name)
1.19      noro      231: {
1.29      noro      232:        int i,j,k,len,clen,slen,start,level;
1.41      noro      233:        char *buf,*head,*r,*h,*brace,*buf_conv;
1.28      noro      234:        char **subs;
                    235:
1.41      noro      236:        if ( !name || !(len=strlen(name)) ) return "";
                    237:        if ( !(conv_flag&CONV_SUBS) ) return name;
1.28      noro      238:        subs = (char **)ALLOCA(len*sizeof(char* ));
1.32      noro      239:        for ( i = 0, j = 0, start = i; ; j++ ) {
1.40      noro      240:                while ( (i < len) && _is_delimiter(name[i]) ) i++;
1.32      noro      241:                start = i;
1.28      noro      242:                if ( i == len ) break;
1.29      noro      243:                if ( name[i] == '{' ) {
                    244:                        for ( level = 1, i++; i < len && level; i++ ) {
                    245:                                if ( name[i] == '{' ) level++;
                    246:                                else if ( name[i] == '}' ) level--;
                    247:                        }
1.32      noro      248:                        slen = i-start;
1.37      noro      249:                        if ( slen >= 3 ) {
                    250:                                brace = (char *)ALLOCA((slen+1)*sizeof(char));
                    251:                                strncpy(brace,name+start+1,slen-2);
                    252:                                brace[slen-2] = 0;
1.38      noro      253:                                buf = conv_subs(brace);
1.37      noro      254:                                subs[j] = (char *)ALLOCA((strlen(buf)+3)*sizeof(char));
1.38      noro      255:                                if ( strlen(buf) == 1 )
                    256:                                        strcpy(subs[j],buf);
                    257:                                else
                    258:                                        sprintf(subs[j],"{%s}",buf);
1.37      noro      259:                        } else
                    260:                                subs[j] = "{}";
1.32      noro      261:                } else {
                    262:                        if ( isdigit(name[i]) )
                    263:                                while ( i < len && isdigit(name[i]) ) i++;
                    264:                        else
1.40      noro      265:                                while ( i < len && _is_alpha(name[i]) ) i++;
1.32      noro      266:                        slen = i-start;
                    267:                        buf = (char *)ALLOCA((slen+1)*sizeof(char));
                    268:                        strncpy(buf,name+start,slen); buf[slen] = 0;
1.41      noro      269:                        buf_conv = symbol_name(buf);
                    270:                        subs[j] = buf_conv?buf_conv:buf;
1.32      noro      271:                }
1.28      noro      272:        }
1.32      noro      273:        for ( k = 0, clen = 0; k < j; k++ ) clen += strlen(subs[k]);
                    274:        /* {subs(0)}_{{subs(1)},...,{subs(j-1)}} => {}:j+1 _:1 ,:j-2 */
                    275:        h = r = MALLOC_ATOMIC((clen+(j+1)*2+1+(j-2)+1)*sizeof(char));
                    276:        if ( j == 1 )
                    277:                sprintf(h,"{%s}",subs[0]);
1.28      noro      278:        else {
1.38      noro      279:                sprintf(h,"{%s}_{%s",subs[0],subs[1]);
1.28      noro      280:                h += strlen(h);
1.32      noro      281:                for ( k = 2; k < j; k++ ) {
1.38      noro      282:                        sprintf(h,",%s",subs[k]);
1.28      noro      283:                        h += strlen(h);
1.19      noro      284:                }
1.28      noro      285:                strcpy(h,"}");
1.20      noro      286:        }
                    287:        return r;
1.19      noro      288: }
                    289:
1.26      noro      290: char *call_convfunc(char *name)
                    291: {
                    292:        STRING str,r;
                    293:        NODE arg;
                    294:
                    295:        MKSTR(str,name);
                    296:        arg = mknode(1,str);
                    297:        r = (STRING)bevalf(convfunc,arg);
                    298:        if ( !r || OID(r) != O_STR )
                    299:                error("call_convfunc : invalid result");
                    300:        return BDY(r);
                    301: }
                    302:
1.18      noro      303: int register_symbol_table(Obj arg)
                    304: {
                    305:        NODE n,t;
                    306:        Obj b;
                    307:        STRING a0,a1;
                    308:        struct TeXSymbol *uts;
                    309:        int i,len;
                    310:
                    311:        /* check */
                    312:        if ( !arg ) {
                    313:                user_texsymbol = 0;
                    314:                return 1;
                    315:        }
                    316:        if ( OID(arg) != O_LIST ) return 0;
                    317:
                    318:        n = BDY((LIST)arg);
                    319:        len = length(n);
                    320:        uts = (struct TeXSymbol *)MALLOC((len+1)*sizeof(struct TeXSymbol));
                    321:        for ( i = 0; n; n = NEXT(n), i++ ) {
                    322:                b = (Obj)BDY(n);
                    323:                if ( !b || OID(b) != O_LIST ) return 0;
                    324:                t = BDY((LIST)b);
                    325:                if ( !t || !NEXT(t) ) return 0;
                    326:                a0 = (STRING)BDY(t);
                    327:                a1 = (STRING)BDY(NEXT(t));
1.23      noro      328:                if ( !a0 ) return 0;
                    329:                if ( OID(a0) == O_STR )
                    330:                        uts[i].text = BDY(a0);
                    331:                else if ( OID(a0) == O_P )
                    332:                        uts[i].text = NAME(VR((P)a0));
                    333:                else
                    334:                        return 0;
                    335:                if ( !a1 ) return 0;
                    336:                if ( OID(a1) == O_STR )
                    337:                        uts[i].symbol = BDY(a1);
                    338:                else if ( OID(a1) == O_P )
                    339:                        uts[i].symbol = NAME(VR((P)a1));
                    340:                else
                    341:                        return 0;
1.18      noro      342:        }
                    343:        uts[i].text = 0;
                    344:        uts[i].symbol = 0;
                    345:        user_texsymbol = uts;
                    346:        return 1;
                    347: }
                    348:
1.46      noro      349: int register_dp_vars_origin(Obj arg)
                    350: {
                    351:        if ( INT(arg) ) {
                    352:                dp_vars_origin = QTOS((Q)arg);
                    353:                return 1;
                    354:        } else return 0;
                    355: }
                    356:
                    357: int register_dp_dvars_origin(Obj arg)
                    358: {
                    359:        if ( INT(arg) ) {
                    360:                dp_dvars_origin = QTOS((Q)arg);
                    361:                return 1;
                    362:        } else return 0;
                    363: }
                    364:
1.31      noro      365: int register_dp_vars_hweyl(Obj arg)
                    366: {
                    367:        if ( INT(arg) ) {
                    368:                dp_vars_hweyl = QTOS((Q)arg);
                    369:                return 1;
                    370:        } else return 0;
                    371: }
                    372:
1.27      noro      373: int register_show_lt(Obj arg)
                    374: {
                    375:        if ( INT(arg) ) {
                    376:                show_lt = QTOS((Q)arg);
                    377:                return 1;
                    378:        } else return 0;
                    379: }
1.26      noro      380:
1.18      noro      381: int register_conv_rule(Obj arg)
                    382: {
1.19      noro      383:        if ( INT(arg) ) {
1.28      noro      384:                conv_flag = QTOS((Q)arg);
                    385:                convfunc = 0;
                    386:                return 1;
1.38      noro      387:        } else return 0;
                    388: }
                    389:
                    390: int register_conv_func(Obj arg)
                    391: {
1.43      noro      392:        if ( !arg ) {
                    393:                convfunc = 0;
                    394:                return 1;
                    395:        } else if ( OID(arg) == O_P && (int)(VR((P)arg))->attr == V_SR ) {
1.26      noro      396:                convfunc = (FUNC)(VR((P)arg)->priv);
                    397:                /* f must be a function which takes single argument */
                    398:                return 1;
1.19      noro      399:        } else return 0;
1.18      noro      400: }
                    401:
1.23      noro      402: int register_dp_vars(Obj arg)
                    403: {
                    404:        int l,i;
                    405:        char **r;
                    406:        NODE n;
                    407:        STRING a;
                    408:
                    409:        if ( !arg ) {
                    410:                dp_vars = 0;
                    411:                dp_vars_len = 0;
1.25      noro      412:                return 1;
1.23      noro      413:        } else if ( OID(arg) != O_LIST )
                    414:                return 0;
                    415:        else {
                    416:                n = BDY((LIST)arg);
                    417:                l = length(n);
                    418:                r = (char **)MALLOC_ATOMIC(l*sizeof(char *));
                    419:                for ( i = 0; i < l; i++, n = NEXT(n) ) {
                    420:                        a = (STRING)BDY(n);
                    421:                        if ( !a ) return 0;
                    422:                        if ( OID(a) == O_STR )
                    423:                                r[i] = BDY(a);
                    424:                        else if ( OID(a) == O_P )
                    425:                                r[i] = NAME(VR((P)a));
                    426:                        else
                    427:                                return 0;
                    428:                }
                    429:                dp_vars = r;
                    430:                dp_vars_len = l;
                    431:                return 1;
                    432:        }
                    433: }
                    434:
1.25      noro      435: int register_dp_vars_prefix(Obj arg)
                    436: {
                    437:        if ( !arg ) {
                    438:                dp_vars_prefix = 0;
                    439:                return 1;
                    440:        } else if ( OID(arg) == O_STR ) {
                    441:                dp_vars_prefix = BDY((STRING)arg);
                    442:                return 1;
                    443:        } else if ( OID(arg) == O_P ) {
                    444:                dp_vars_prefix = NAME(VR((P)arg));
                    445:                return 1;
                    446:        } else return 0;
                    447: }
                    448:
1.46      noro      449: int register_dp_dvars_prefix(Obj arg)
                    450: {
                    451:        if ( !arg ) {
                    452:                dp_dvars_prefix = 0;
                    453:                return 1;
                    454:        } else if ( OID(arg) == O_STR ) {
                    455:                dp_dvars_prefix = BDY((STRING)arg);
                    456:                return 1;
                    457:        } else if ( OID(arg) == O_P ) {
                    458:                dp_dvars_prefix = NAME(VR((P)arg));
                    459:                return 1;
                    460:        } else return 0;
                    461: }
                    462:
1.24      noro      463: void Pquotetotex_env(NODE arg,Obj *rp)
1.18      noro      464: {
                    465:        int ac,i;
                    466:        char *name;
                    467:        NODE n,n0;
                    468:        STRING s;
                    469:        LIST l;
                    470:
                    471:        ac = argc(arg);
                    472:        if ( !ac ) {
                    473:                n0 = 0;
                    474:                for ( i = 0; qtot_env[i].name; i++ ) {
                    475:                        NEXTNODE(n0,n); MKSTR(s,qtot_env[i].name); BDY(n) = (pointer)s;
                    476:                        NEXTNODE(n0,n); BDY(n) = (Q)qtot_env[i].value;
                    477:                }
                    478:                NEXT(n) = 0;
                    479:                MKLIST(l,n0);
                    480:                *rp = (Obj)l;
1.34      noro      481:        } else if ( ac == 1 && !ARG0(arg) ) {
                    482:                /* set to default */
1.43      noro      483:                for ( i = 0; qtot_env[i].name; i++ ) {
                    484:                        (qtot_env[i].reg)(0);
1.34      noro      485:                        qtot_env[i].value = 0;
1.43      noro      486:                }
1.34      noro      487:                *rp = 0;
1.18      noro      488:        } else if ( ac == 1 || ac == 2 ) {
1.24      noro      489:                asir_assert(ARG0(arg),O_STR,"quotetotex_env");
1.18      noro      490:                name = BDY((STRING)ARG0(arg));
                    491:                for ( i = 0; qtot_env[i].name; i++ )
                    492:                        if ( !strcmp(qtot_env[i].name,name) ) {
                    493:                                if ( ac == 2 ) {
                    494:                                        if ( (qtot_env[i].reg)((Obj)ARG1(arg)) )
                    495:                                                qtot_env[i].value = (Obj)ARG1(arg);
                    496:                                        else
1.24      noro      497:                                                error("quotetotex_env : invalid argument");
1.18      noro      498:                                }
                    499:                                *rp = qtot_env[i].value;
                    500:                                return;
                    501:                        }
                    502:                *rp = 0;
                    503:        } else
                    504:                *rp = 0;
                    505: }
                    506:
1.14      noro      507: void Pwrite_to_tb(NODE arg,Q *rp)
1.13      noro      508: {
                    509:        int i;
1.16      noro      510:        Obj obj;
                    511:        TB tb;
1.13      noro      512:
1.14      noro      513:        asir_assert(ARG1(arg),O_TB,"write_to_tb");
1.16      noro      514:        obj = ARG0(arg);
                    515:        if ( !obj )
                    516:                write_tb("",ARG1(arg));
                    517:        else if ( OID(obj) == O_STR )
                    518:                write_tb(BDY((STRING)obj),ARG1(arg));
                    519:        else if ( OID(obj) == O_TB ) {
                    520:                tb = (TB)obj;
                    521:                for ( i = 0; i < tb->next; i++ )
                    522:                        write_tb(tb->body[i],ARG1(arg));
                    523:        }
1.14      noro      524:        *rp = 0;
1.53      noro      525: }
                    526:
1.64      noro      527: FNODE partial_eval(FNODE), quote_to_nary(FNODE), quote_to_bin(FNODE,int);
1.63      noro      528:
1.64      noro      529: void Pquote_to_nary(NODE arg,QUOTE *rp)
1.63      noro      530: {
                    531:        FNODE f;
                    532:
1.64      noro      533:        f = quote_to_nary(BDY((QUOTE)ARG0(arg)));
1.63      noro      534:        MKQUOTE(*rp,f);
                    535: }
                    536:
1.64      noro      537: void Pquote_to_bin(NODE arg,QUOTE *rp)
1.63      noro      538: {
                    539:        FNODE f;
                    540:        int direction;
                    541:
                    542:        direction = QTOS((Q)ARG1(arg));
1.64      noro      543:        f = quote_to_bin(BDY((QUOTE)ARG0(arg)),direction);
1.63      noro      544:
                    545:        MKQUOTE(*rp,f);
                    546: }
1.61      noro      547:
1.69    ! noro      548: void Pquote_is_number(NODE arg,Q *rp)
        !           549: {
        !           550:        QUOTE q;
        !           551:        int ret;
        !           552:
        !           553:        q = (QUOTE)ARG0(arg);
        !           554:        asir_assert(q,O_QUOTE,"quote_is_number");
        !           555:        ret = fnode_is_number(BDY(q));
        !           556:        STOQ(ret,*rp);
        !           557: }
        !           558:
        !           559: void Pquote_is_rational(NODE arg,Q *rp)
        !           560: {
        !           561:        QUOTE q;
        !           562:        int ret;
        !           563:
        !           564:        q = (QUOTE)ARG0(arg);
        !           565:        asir_assert(q,O_QUOTE,"quote_is_rational");
        !           566:        ret = fnode_is_rational(BDY(q));
        !           567:        STOQ(ret,*rp);
        !           568: }
        !           569:
        !           570: void Pquote_is_integer(NODE arg,Q *rp)
        !           571: {
        !           572:        QUOTE q;
        !           573:        int ret;
        !           574:
        !           575:        q = (QUOTE)ARG0(arg);
        !           576:        asir_assert(q,O_QUOTE,"quote_is_integer");
        !           577:        ret = fnode_is_integer(BDY(q));
        !           578:        STOQ(ret,*rp);
        !           579: }
        !           580:
        !           581: void Pquote_is_function(NODE arg,Q *rp)
        !           582: {
        !           583:        QUOTE q;
        !           584:        int ret;
        !           585:
        !           586:        q = (QUOTE)ARG0(arg);
        !           587:        asir_assert(q,O_QUOTE,"quote_is_function");
        !           588:        if ( q->id == I_FUNC || q->id == I_IFUNC )
        !           589:                ret = 1;
        !           590:        else
        !           591:                ret = 0;
        !           592:        STOQ(ret,*rp);
        !           593: }
        !           594:
        !           595: void Pquote_is_dependent(NODE arg,Q *rp)
        !           596: {
        !           597:        P x;
        !           598:        QUOTE q,v;
        !           599:        int ret;
        !           600:        V var;
        !           601:
        !           602:        q = (QUOTE)ARG0(arg);
        !           603:        v = (QUOTE)ARG1(arg);
        !           604:        asir_assert(q,O_QUOTE,"quote_is_dependent");
        !           605:        asir_assert(v,O_QUOTE,"quote_is_dependent");
        !           606:        x = (P)eval(BDY(v));
        !           607:        if ( !x || OID(x) != O_P )
        !           608:                *rp = 0;
        !           609:        var = VR(x);
        !           610:        ret = fnode_is_dependent(BDY(q),var);
        !           611:        STOQ(ret,*rp);
        !           612: }
        !           613:
        !           614:
1.57      noro      615: void Pquote_unify(NODE arg,Q *rp)
1.53      noro      616: {
1.61      noro      617:        FNODE f,g;
1.65      noro      618:        Obj obj;
1.61      noro      619:        QUOTE q;
1.53      noro      620:        NODE r;
1.56      noro      621:        int ret;
1.53      noro      622:
1.62      noro      623: #if 0
1.61      noro      624:        g = partial_eval(BDY(((QUOTE)ARG0(arg))));
                    625:        MKQUOTE(q,g);
                    626:        ret = quote_unify((Obj)q,(Obj)ARG1(arg),&r);
1.62      noro      627: #else
1.65      noro      628:        obj = (Obj)ARG0(arg);
                    629:        ret = quote_unify(obj,(Obj)ARG1(arg),&r);
1.62      noro      630: #endif
1.57      noro      631:        if ( ret ) {
                    632:                do_assign(r);
                    633:                *rp = ONE;
                    634:        } else
1.56      noro      635:                *rp = 0;
1.68      noro      636: }
                    637:
                    638: FNODE rewrite_fnode(FNODE,NODE);
                    639:
                    640: extern Obj VOIDobj;
                    641:
                    642: void Pquote_match_rewrite(NODE arg,Obj *rp)
                    643: {
                    644:        FNODE f,g,h,c,value;
                    645:        Obj obj;
                    646:        QUOTE q;
                    647:        NODE r,t,s,s0,pair;
                    648:        int ret,ind,ac;
                    649:
                    650:        obj = (Obj)ARG0(arg);
                    651:        ret = quote_unify(obj,(Obj)ARG1(arg),&r);
                    652:        if ( ret ) {
                    653:                for ( t = r, s0 = 0; t; t = NEXT(t) ) {
                    654:                        NEXTNODE(s0,s);
                    655:                        pair = BDY((LIST)BDY(t));
                    656:                        ind = (int)FA0((FNODE)BDY((QUOTE)BDY(pair)));
                    657:                        value = mkfnode(1,I_FORMULA,BDY(NEXT(pair)));
                    658:                        BDY(s) = mknode(2,ind,value);
                    659:                }
                    660:                if ( s0 ) NEXT(s) = 0;
                    661:                switch ( ac = argc(arg) ) {
                    662:                        case 3:
                    663:                                h = rewrite_fnode(BDY((QUOTE)ARG2(arg)),s0);
                    664:                                *rp = eval(h);
                    665:                                break;
                    666:                        case 4:
                    667:                                c = rewrite_fnode(BDY((QUOTE)ARG2(arg)),s0);
                    668:                                if ( eval(c) ) {
                    669:                                        h = rewrite_fnode(BDY((QUOTE)ARG3(arg)),s0);
                    670:                                        *rp = eval(h);
                    671:                                } else
                    672:                                        *rp = VOIDobj;
                    673:                                break;
                    674:                        default:
                    675:                                error("quote_match_rewrite : invalid argument");
                    676:                }
                    677:        } else
                    678:                *rp = VOIDobj;
1.56      noro      679: }
                    680:
                    681: void do_assign(NODE arg)
                    682: {
                    683:        NODE t,pair;
                    684:        int pv;
                    685:
                    686:        QUOTE value;
                    687:
                    688:        for ( t = arg; t; t = NEXT(t) ) {
                    689:                pair = BDY((LIST)BDY(t));
                    690:                pv = (int)FA0((FNODE)BDY((QUOTE)BDY(pair)));
                    691:                value = (QUOTE)(BDY(NEXT(pair)));
                    692:                ASSPV(pv,value);
                    693:        }
1.53      noro      694: }
                    695:
                    696: /*
1.56      noro      697: /* consistency check and merge
                    698:  */
1.53      noro      699:
1.56      noro      700: int merge_matching_node(NODE n,NODE a,NODE *rp)
1.53      noro      701: {
                    702:        NODE ta,ba,tn,bn;
                    703:        QUOTE pa,va,pn,vn;
                    704:
1.56      noro      705:        if ( !n ) {
                    706:                *rp = a;
                    707:                return 1;
                    708:        }
1.53      noro      709:        for ( ta = a; ta; ta = NEXT(ta) ) {
                    710:                ba = BDY((LIST)BDY(ta));
1.55      noro      711:                if ( !ba ) continue;
1.53      noro      712:                pa = (QUOTE)BDY(ba); va = (QUOTE)BDY(NEXT(ba));
                    713:                for ( tn = n; tn; tn = NEXT(tn) ) {
                    714:                        bn = BDY((LIST)BDY(tn));
1.55      noro      715:                        if ( !bn ) continue;
1.53      noro      716:                        pn = (QUOTE)BDY(bn); vn = (QUOTE)BDY(NEXT(bn));
1.55      noro      717:                        if ( !compquote(CO,pa,pn) ) {
                    718:                                if ( !compquote(CO,va,vn) ) break;
                    719:                                else return 0;
                    720:                        }
1.53      noro      721:                }
                    722:                if ( !tn ) {
                    723:                        MKNODE(tn,(pointer)BDY(ta),n);
                    724:                        n = tn;
                    725:                }
                    726:        }
1.56      noro      727:        *rp = n;
                    728:        return 1;
1.53      noro      729: }
                    730:
1.56      noro      731: int quote_unify_node(NODE f,NODE pat,NODE *rp) {
                    732:        NODE r,a,tf,tp,r1;
                    733:        int ret;
1.53      noro      734:
                    735:        if ( length(f) != length(pat) ) return 0;
                    736:        r = 0;
                    737:        for ( tf = f, tp = pat; tf; tf = NEXT(tf), tp = NEXT(tp) ) {
1.56      noro      738:                ret = quote_unify((Obj)BDY(tf),(Obj)BDY(tp),&a);
                    739:                if ( !ret ) return 0;
                    740:                ret = merge_matching_node(r,a,&r1);
                    741:                if ( !ret ) return 0;
                    742:                else r = r1;
1.53      noro      743:        }
1.56      noro      744:        *rp = r;
                    745:        return 1;
1.53      noro      746: }
                    747:
1.66      noro      748: /* f = [a,b,c,...] pat = [X,Y,...] rpat matches the rest of f */
                    749:
                    750: int quote_unify_cons(NODE f,NODE pat,Obj rpat,NODE *rp) {
                    751:        QUOTE q;
                    752:        Q id;
                    753:        FNODE fn;
                    754:        NODE r,a,tf,tp,r1,arg;
                    755:        int ret;
                    756:        LIST list,alist;
                    757:
                    758:        /* matching of the head part */
                    759:        if ( length(f) < length(pat) ) return 0;
                    760:        r = 0;
                    761:        for ( tf = f, tp = pat; tp; tf = NEXT(tf), tp = NEXT(tp) ) {
                    762:                ret = quote_unify((Obj)BDY(tf),(Obj)BDY(tp),&a);
                    763:                if ( !ret ) return 0;
                    764:                ret = merge_matching_node(r,a,&r1);
                    765:                if ( !ret ) return 0;
                    766:                else r = r1;
                    767:        }
                    768:        /* matching of the rest */
                    769:        MKLIST(list,tf);
                    770:        STOQ(I_LIST,id); a = mknode(2,id,list);
                    771:        MKLIST(alist,a);
                    772:        arg = mknode(1,alist);
                    773:        Pfunargs_to_quote(arg,&q);
                    774:        ret = quote_unify((Obj)q,rpat,&a);
                    775:        if ( !ret ) return 0;
                    776:        ret = merge_matching_node(r,a,&r1);
                    777:        if ( !ret ) return 0;
                    778:        *rp = r1;
                    779:        return 1;
                    780: }
                    781:
1.53      noro      782: void get_quote_id_arg(QUOTE f,int *id,NODE *r)
                    783: {
                    784:        LIST fa;
                    785:        NODE arg,fab;
                    786:
                    787:        arg = mknode(1,f); Pquote_to_funargs(arg,&fa); fab = BDY((LIST)fa);
                    788:        *id = QTOS((Q)BDY(fab)); *r = NEXT(fab);
                    789: }
                    790:
1.56      noro      791: /* *rp : [[quote(A),quote(1)],...] */
1.53      noro      792:
1.56      noro      793: int quote_unify(Obj f, Obj pat, NODE *rp)
1.53      noro      794: {
                    795:        NODE tf,tp,head,body;
                    796:        NODE parg,farg,r;
1.66      noro      797:        Obj rpat;
1.53      noro      798:        LIST fa,l;
                    799:        int pid,id;
1.55      noro      800:        FUNC ff,pf;
1.56      noro      801:        int ret;
1.64      noro      802:        QUOTE q;
                    803:        FNODE g;
1.53      noro      804:
1.67      noro      805:        if ( !f )
                    806:                if ( !pat ) {
                    807:                        *rp = 0; return 1;
                    808:                } else
                    809:                        return 0;
                    810:        else if ( OID(pat) == O_LIST ) {
1.53      noro      811:                if ( OID(f) == O_LIST )
1.56      noro      812:                        return quote_unify_node(BDY((LIST)f),BDY((LIST)pat),rp);
1.53      noro      813:                else
                    814:                        return 0;
                    815:        } else if ( OID(pat) == O_QUOTE ) {
1.67      noro      816:                pid = ((FNODE)BDY((QUOTE)pat))->id;
1.53      noro      817:                switch ( pid ) {
1.55      noro      818:                        case I_FORMULA:
                    819:                                if ( compquote(CO,f,pat) )
                    820:                                        return 0;
                    821:                                else {
1.67      noro      822:                                        *rp = 0; return 1;
1.55      noro      823:                                }
                    824:                                break;
1.67      noro      825:
                    826:                        case I_LIST: case I_CONS:
                    827:                                get_quote_id_arg((QUOTE)pat,&pid,&parg);
                    828:                                if ( OID(f) == O_LIST )
                    829:                                        tf = BDY((LIST)f);
                    830:                                else if ( OID(f) == O_QUOTE
                    831:                                        && ((FNODE)BDY((QUOTE)f))->id == pid ) {
                    832:                                        get_quote_id_arg((QUOTE)f,&id,&farg);
                    833:                                        tf = BDY((LIST)BDY(farg));
                    834:                                } else
                    835:                                        return 0;
                    836:
1.66      noro      837:                                tp = BDY((LIST)BDY(parg));
1.67      noro      838:                                if ( pid == I_LIST )
                    839:                                        return quote_unify_node(tf,tp,rp);
                    840:                                else {
                    841:                                        rpat = (Obj)BDY(NEXT(parg));
                    842:                                        return quote_unify_cons(tf,tp,rpat,rp);
                    843:                                }
                    844:
1.53      noro      845:                        case I_PVAR:
                    846:                                /* [[pat,f]] */
                    847:                                r = mknode(2,pat,f); MKLIST(l,r);
1.56      noro      848:                                *rp =  mknode(1,l);
                    849:                                return 1;
1.67      noro      850:
1.53      noro      851:                        case I_IFUNC:
                    852:                                /* F(X,Y,...) = ... */
1.67      noro      853:                                get_quote_id_arg((QUOTE)f,&id,&farg);
                    854:                                get_quote_id_arg((QUOTE)pat,&pid,&parg);
1.53      noro      855:                                if ( id == I_FUNC ) {
1.54      noro      856:                                        r = mknode(2,BDY(parg),BDY(farg)); MKLIST(l,r);
                    857:                                        head = mknode(1,l);
1.56      noro      858:                                        ret = quote_unify(BDY(NEXT(farg)),
                    859:                                                                BDY(NEXT(parg)),&body);
                    860:                                        if ( !ret ) return 0;
                    861:                                        else return merge_matching_node(head,body,rp);
1.53      noro      862:                                } else
                    863:                                        return 0;
1.64      noro      864:
1.67      noro      865:                        case I_NARYOP: case I_BOP: case I_FUNC:
1.64      noro      866:                                /* X+Y = ... */
1.67      noro      867:                                /* f(...) = ... */
                    868:                                if ( OID(f) != O_QUOTE ) return 0;
                    869:                                id = ((FNODE)BDY((QUOTE)f))->id;
                    870:                                if ( pid == I_FUNC )
                    871:                                        ;
                    872:                                else {
                    873:                                        /* XXX converting to I_BOP */
                    874:                                        if ( pid == I_NARYOP ) {
                    875:                                                g = quote_to_bin(BDY((QUOTE)pat),1);
                    876:                                                MKQUOTE(q,g); pat = (Obj)q;
                    877:                                        }
                    878:                                        if ( id == I_NARYOP ) {
                    879:                                                g = quote_to_bin(BDY((QUOTE)f),1);
                    880:                                                MKQUOTE(q,g); f = (Obj)q;
                    881:                                        }
                    882:                                }
                    883:                                get_quote_id_arg((QUOTE)pat,&pid,&parg);
                    884:                                get_quote_id_arg((QUOTE)f,&id,&farg);
1.64      noro      885:                                if ( compqa(CO,BDY(farg),BDY(parg)) ) return 0;
                    886:                                return quote_unify_node(NEXT(farg),NEXT(parg),rp);
                    887:
1.53      noro      888:                        default:
1.67      noro      889:                                if ( OID(f) != O_QUOTE ) return 0;
                    890:                                id = ((FNODE)BDY((QUOTE)f))->id;
                    891:                                if ( id != pid ) return 0;
                    892:                                get_quote_id_arg((QUOTE)pat,&pid,&parg);
                    893:                                get_quote_id_arg((QUOTE)f,&id,&farg);
                    894:                                return quote_unify_node(farg,parg,rp);
1.53      noro      895:                }
                    896:        }
1.13      noro      897: }
                    898:
1.14      noro      899: void Pquotetotex(NODE arg,STRING *rp)
1.13      noro      900: {
1.14      noro      901:        TB tb;
1.13      noro      902:
1.14      noro      903:        NEWTB(tb);
1.27      noro      904:        /* XXX for DP */
                    905:        is_lt = 1;
1.14      noro      906:        fnodetotex_tb(BDY((QUOTE)ARG0(arg)),tb);
                    907:        tb_to_string(tb,rp);
1.13      noro      908: }
                    909:
1.14      noro      910: void Pquotetotex_tb(NODE arg,Q *rp)
1.13      noro      911: {
                    912:        int i;
1.14      noro      913:        TB tb;
1.13      noro      914:
1.14      noro      915:        asir_assert(ARG1(arg),O_TB,"quotetotex_tb");
1.27      noro      916:        /* XXX for DP */
                    917:        is_lt = 1;
1.14      noro      918:        fnodetotex_tb(BDY((QUOTE)ARG0(arg)),ARG1(arg));
1.13      noro      919:        *rp = 0;
                    920: }
                    921:
1.14      noro      922: void Pstring_to_tb(NODE arg,TB *rp)
                    923: {
                    924:        TB tb;
                    925:
                    926:        asir_assert(ARG0(arg),O_STR,"string_to_tb");
                    927:        NEWTB(tb);
                    928:        tb->body[0] = BDY((STRING)ARG0(arg));
                    929:        tb->next++;
                    930:        *rp = tb;
                    931: }
                    932:
                    933: void Ptb_to_string(NODE arg,STRING *rp)
                    934: {
                    935:        TB tb;
                    936:
                    937:        asir_assert(ARG0(arg),O_TB,"tb_to_string");
                    938:        tb = (TB)ARG0(arg);
                    939:        tb_to_string(tb,rp);
                    940: }
                    941:
                    942: void tb_to_string(TB tb,STRING *rp)
1.13      noro      943: {
1.14      noro      944:        int j,len;
1.13      noro      945:        char *all,*p,*q;
                    946:
1.14      noro      947:        for ( j = 0, len = 0; j < tb->next; j++ )
                    948:                len += strlen(tb->body[j]);
                    949:        all = (char *)MALLOC_ATOMIC((len+1)*sizeof(char));
                    950:        for ( j = 0, p = all; j < tb->next; j++ )
                    951:                for ( q = tb->body[j]; *q; *p++ = *q++ );
                    952:        *p = 0;
                    953:        MKSTR(*rp,all);
                    954: }
                    955:
                    956: void Pclear_tb(NODE arg,Q *rp)
                    957: {
                    958:        TB tb;
                    959:        int j;
                    960:
                    961:        asir_assert(ARG0(arg),O_TB,"clear_tb");
                    962:        tb = (TB)ARG0(arg);
                    963:        for ( j = 0; j < tb->next; j++ )
                    964:                tb->body[j] = 0;
                    965:        tb->next = 0;
                    966:        *rp = 0;
1.13      noro      967: }
1.5       noro      968:
                    969: void Pstr_len(arg,rp)
                    970: NODE arg;
                    971: Q *rp;
                    972: {
1.16      noro      973:        Obj obj;
                    974:        TB tb;
                    975:        int r,i;
1.5       noro      976:
1.16      noro      977:        obj = (Obj)ARG0(arg);
                    978:        if ( !obj || (OID(obj) != O_STR && OID(obj) != O_TB) )
                    979:                error("str_len : invalid argument");
                    980:        if ( OID(obj) == O_STR)
                    981:                r = strlen(BDY((STRING)obj));
                    982:        else if ( OID(obj) == O_TB ) {
                    983:                tb = (TB)obj;
                    984:                for ( r = i = 0; i < tb->next; i++ )
                    985:                        r += strlen(tb->body[i]);
                    986:        }
1.5       noro      987:        STOQ(r,*rp);
                    988: }
                    989:
                    990: void Pstr_chr(arg,rp)
                    991: NODE arg;
                    992: Q *rp;
                    993: {
                    994:        STRING str,terminator;
                    995:        Q start;
                    996:        char *p,*ind;
                    997:        int chr,spos,r;
                    998:
                    999:        str = (STRING)ARG0(arg);
                   1000:        start = (Q)ARG1(arg);
                   1001:        terminator = (STRING)ARG2(arg);
                   1002:        asir_assert(str,O_STR,"str_chr");
                   1003:        asir_assert(start,O_N,"str_chr");
                   1004:        asir_assert(terminator,O_STR,"str_chr");
                   1005:        p = BDY(str);
                   1006:        spos = QTOS(start);
                   1007:        chr = BDY(terminator)[0];
1.8       noro     1008:        if ( spos > (int)strlen(p) )
1.5       noro     1009:                r = -1;
                   1010:        else {
                   1011:                ind = strchr(p+spos,chr);
                   1012:                if ( ind )
                   1013:                        r = ind-p;
                   1014:                else
                   1015:                        r = -1;
                   1016:        }
                   1017:        STOQ(r,*rp);
                   1018: }
                   1019:
                   1020: void Psub_str(arg,rp)
                   1021: NODE arg;
                   1022: STRING *rp;
                   1023: {
                   1024:        STRING str;
                   1025:        Q head,tail;
                   1026:        char *p,*r;
                   1027:        int spos,epos,len;
                   1028:
                   1029:        str = (STRING)ARG0(arg);
                   1030:        head = (Q)ARG1(arg);
                   1031:        tail = (Q)ARG2(arg);
                   1032:        asir_assert(str,O_STR,"sub_str");
                   1033:        asir_assert(head,O_N,"sub_str");
                   1034:        asir_assert(tail,O_N,"sub_str");
                   1035:        p = BDY(str);
                   1036:        spos = QTOS(head);
                   1037:        epos = QTOS(tail);
                   1038:        len = strlen(p);
                   1039:        if ( (spos >= len) || (epos < spos) ) {
                   1040:                *rp = 0; return;
                   1041:        }
                   1042:        if ( epos >= len )
                   1043:                epos = len-1;
                   1044:        len = epos-spos+1;
                   1045:        r = (char *)MALLOC(len+1);
                   1046:        strncpy(r,p+spos,len);
                   1047:        r[len] = 0;
                   1048:        MKSTR(*rp,r);
                   1049: }
1.3       noro     1050:
                   1051: void Pstrtoascii(arg,rp)
                   1052: NODE arg;
                   1053: LIST *rp;
                   1054: {
                   1055:        STRING str;
                   1056:        unsigned char *p;
                   1057:        int len,i;
                   1058:        NODE n,n1;
                   1059:        Q q;
                   1060:
                   1061:        str = (STRING)ARG0(arg);
                   1062:        asir_assert(str,O_STR,"strtoascii");
                   1063:        p = BDY(str);
                   1064:        len = strlen(p);
                   1065:        for ( i = len-1, n = 0; i >= 0; i-- ) {
                   1066:                UTOQ((unsigned int)p[i],q);
                   1067:                MKNODE(n1,q,n);
                   1068:                n = n1;
                   1069:        }
                   1070:        MKLIST(*rp,n);
                   1071: }
                   1072:
                   1073: void Pasciitostr(arg,rp)
                   1074: NODE arg;
                   1075: STRING *rp;
                   1076: {
                   1077:        LIST list;
                   1078:        unsigned char *p;
                   1079:        int len,i,j;
                   1080:        NODE n;
                   1081:        Q q;
                   1082:
                   1083:        list = (LIST)ARG0(arg);
                   1084:        asir_assert(list,O_LIST,"asciitostr");
                   1085:        n = BDY(list);
                   1086:        len = length(n);
                   1087:        p = MALLOC_ATOMIC(len+1);
                   1088:        for ( i = 0; i < len; i++, n = NEXT(n) ) {
                   1089:                q = (Q)BDY(n);
                   1090:                asir_assert(q,O_N,"asciitostr");
                   1091:                j = QTOS(q);
1.4       noro     1092:                if ( j >= 256 || j <= 0 )
1.3       noro     1093:                        error("asciitostr : argument out of range");
                   1094:                p[i] = j;
                   1095:        }
                   1096:        p[i] = 0;
                   1097:        MKSTR(*rp,(char *)p);
                   1098: }
1.1       noro     1099:
                   1100: void Peval_str(arg,rp)
                   1101: NODE arg;
                   1102: Obj *rp;
                   1103: {
                   1104:        FNODE fnode;
                   1105:        char *cmd;
1.10      ohara    1106: #if defined(PARI)
1.8       noro     1107:        void recover(int);
                   1108:
1.1       noro     1109:        recover(0);
1.11      saito    1110: #  if !(PARI_VERSION_CODE > 131588)
1.1       noro     1111:        if ( setjmp(environnement) ) {
                   1112:                avma = top; recover(1);
                   1113:                resetenv("");
                   1114:        }
1.11      saito    1115: #  endif
1.1       noro     1116: #endif
                   1117:        cmd = BDY((STRING)ARG0(arg));
1.9       noro     1118:        exprparse_create_var(0,cmd,&fnode);
1.1       noro     1119:        *rp = eval(fnode);
                   1120: }
                   1121:
                   1122: void Prtostr(arg,rp)
                   1123: NODE arg;
                   1124: STRING *rp;
                   1125: {
                   1126:        char *b;
                   1127:        int len;
                   1128:
1.2       noro     1129:        len = estimate_length(CO,ARG0(arg));
1.12      noro     1130:        b = (char *)MALLOC_ATOMIC(len+1);
1.1       noro     1131:        soutput_init(b);
                   1132:        sprintexpr(CO,ARG0(arg));
                   1133:        MKSTR(*rp,b);
                   1134: }
                   1135:
                   1136: void Pstrtov(arg,rp)
                   1137: NODE arg;
                   1138: P *rp;
                   1139: {
1.8       noro     1140:        char *p;
1.1       noro     1141:
                   1142:        p = BDY((STRING)ARG0(arg));
                   1143: #if 0
                   1144:        if ( !islower(*p) )
                   1145:                *rp = 0;
                   1146:        else {
                   1147:                for ( t = p+1; t && (isalnum(*t) || *t == '_'); t++ );
                   1148:                if ( *t )
                   1149:                        *rp = 0;
                   1150:                else
                   1151:                        makevar(p,rp);
                   1152:        }
                   1153: #else
                   1154:        makevar(p,rp);
                   1155: #endif
1.14      noro     1156: }
                   1157:
1.15      noro     1158: static struct TeXSymbol texsymbol[] = {
                   1159:  {"sin","\\sin"},
                   1160:  {"cos","\\cos"},
                   1161:  {"tan","\\tan"},
                   1162:  {"sinh","\\sinh"},
                   1163:  {"cosh","\\cosh"},
                   1164:  {"tanh","\\tanh"},
                   1165:  {"exp","\\exp"},
                   1166:  {"log","\\log"},
                   1167:
                   1168: /* Greek Letters (lower case) */
                   1169:  {"alpha","\\alpha"},
                   1170:  {"beta","\\beta"},
                   1171:  {"gamma","\\gamma"},
                   1172:  {"delta","\\delta"},
                   1173:  {"epsilon","\\epsilon"},
                   1174:  {"varepsilon","\\varepsilon"},
                   1175:  {"zeta","\\zeta"},
                   1176:  {"eta","\\eta"},
                   1177:  {"theta","\\theta"},
                   1178:  {"vartheta","\\vartheta"},
                   1179:  {"iota","\\iota"},
                   1180:  {"kappa","\\kappa"},
                   1181:  {"lambda","\\lambda"},
                   1182:  {"mu","\\mu"},
                   1183:  {"nu","\\nu"},
                   1184:  {"xi","\\xi"},
                   1185:  {"pi","\\pi"},
                   1186:  {"varpi","\\varpi"},
                   1187:  {"rho","\\rho"},
                   1188:  {"sigma","\\sigma"},
                   1189:  {"varsigma","\\varsigma"},
                   1190:  {"tau","\\tau"},
                   1191:  {"upsilon","\\upsilon"},
                   1192:  {"phi","\\phi"},
                   1193:  {"varphi","\\varphi"},
                   1194:  {"chi","\\chi"},
                   1195:  {"omega","\\omega"},
                   1196:
                   1197: /* Greek Letters, (upper case) */
                   1198:  {"ggamma","\\Gamma"},
                   1199:  {"ddelta","\\Delta"},
                   1200:  {"ttheta","\\Theta"},
                   1201:  {"llambda","\\Lambda"},
                   1202:  {"xxi","\\Xi"},
                   1203:  {"ppi","\\Pi"},
                   1204:  {"ssigma","\\Sigma"},
                   1205:  {"uupsilon","\\Upsilon"},
                   1206:  {"pphi","\\Phi"},
                   1207:  {"ppsi","\\Psi"},
                   1208:  {"oomega","\\Omega"},
                   1209:
                   1210:  /* Our own mathematical functions */
                   1211:  {"algebra_tensor","\\otimes"},
                   1212:  {"base_where","{\\rm \\ where \\ }"},
                   1213:  /* Mathematical constants */
                   1214:  {"c_pi","\\pi"},
                   1215:  {"c_i","\\sqrt{-1}"},
                   1216:
                   1217:  /* Temporary  */
                   1218:  {0,0}
                   1219: };
                   1220:
1.14      noro     1221: char *symbol_name(char *name)
                   1222: {
1.15      noro     1223:        int i;
                   1224:
1.41      noro     1225:        if ( !name || strlen(name) == 0 )
                   1226:                return "";
1.38      noro     1227:        if ( !(conv_flag & CONV_TABLE) )
                   1228:                return name;
                   1229:
1.18      noro     1230:        if ( user_texsymbol )
                   1231:                for ( i = 0; user_texsymbol[i].text; i++ )
                   1232:                        if ( !strcmp(user_texsymbol[i].text,name) )
                   1233:                                return user_texsymbol[i].symbol;
1.15      noro     1234:        for ( i = 0; texsymbol[i].text; i++ )
                   1235:                if ( !strcmp(texsymbol[i].text,name) )
                   1236:                        return texsymbol[i].symbol;
1.41      noro     1237:        return 0;
1.52      noro     1238: }
                   1239:
                   1240: void Pget_function_name(NODE arg,STRING *rp)
                   1241: {
                   1242:                QUOTEARG qa;
                   1243:                ARF f;
                   1244:                char *opname;
                   1245:
                   1246:                qa = (QUOTEARG)BDY(arg);
                   1247:                if ( !qa || OID(qa) != O_QUOTEARG || qa->type != A_arf )
                   1248:                        *rp = 0;
                   1249:                else {
                   1250:                        f = (ARF)BDY(qa);
                   1251:                        opname = f->name;
                   1252:                        MKSTR(*rp,opname);
                   1253:                }
1.14      noro     1254: }
                   1255:
1.51      noro     1256: FNODE strip_paren(FNODE);
                   1257:
1.14      noro     1258: void fnodetotex_tb(FNODE f,TB tb)
                   1259: {
                   1260:        NODE n,t,t0;
1.38      noro     1261:        char vname[BUFSIZ],prefix[BUFSIZ];
                   1262:        char *opname,*vname_conv,*prefix_conv;
1.14      noro     1263:        Obj obj;
1.46      noro     1264:        int i,len,allzero,elen,elen2,si;
1.40      noro     1265:        C cplx;
                   1266:        char *r;
1.17      noro     1267:        FNODE fi,f2;
1.14      noro     1268:
                   1269:        write_tb(" ",tb);
                   1270:        if ( !f ) {
                   1271:                write_tb("0",tb);
                   1272:                return;
                   1273:        }
                   1274:        switch ( f->id ) {
                   1275:                /* unary operators */
1.23      noro     1276:                case I_NOT:
                   1277:                        write_tb("\\neg (",tb);
                   1278:                        fnodetotex_tb((FNODE)FA0(f),tb);
                   1279:                        write_tb(")",tb);
                   1280:                        break;
                   1281:                case I_PAREN:
                   1282:                        write_tb("(",tb);
                   1283:                        fnodetotex_tb((FNODE)FA0(f),tb);
                   1284:                        write_tb(")",tb);
                   1285:                        break;
                   1286:                case I_MINUS:
                   1287:                        write_tb("-",tb);
                   1288:                        fnodetotex_tb((FNODE)FA0(f),tb);
                   1289:                        break;
                   1290:
                   1291:                /* binary operators */
                   1292:                /* arg list */
                   1293:                /* I_AND, I_OR => FA0(f), FA1(f) */
                   1294:                /* otherwise   => FA1(f), FA2(f) */
                   1295:                case I_BOP:
                   1296:                        opname = ((ARF)FA0(f))->name;
                   1297:                        if ( !strcmp(opname,"+") ) {
                   1298:                                fnodetotex_tb((FNODE)FA1(f),tb);
1.35      noro     1299:                                if ( !top_is_minus((FNODE)FA2(f)) ) write_tb(opname,tb);
1.23      noro     1300:                                fnodetotex_tb((FNODE)FA2(f),tb);
                   1301:                        } else if ( !strcmp(opname,"-") ) {
                   1302:                                if ( FA1(f) ) fnodetotex_tb((FNODE)FA1(f),tb);
                   1303:                                write_tb(opname,tb);
                   1304:                                fnodetotex_tb((FNODE)FA2(f),tb);
                   1305:                        } else if ( !strcmp(opname,"*") ) {
                   1306:                                fnodetotex_tb((FNODE)FA1(f),tb);
                   1307:                                write_tb(" ",tb);
                   1308:                                /* XXX special care for DP */
                   1309:                                f2 = (FNODE)FA2(f);
                   1310:                                if ( f2->id == I_EV ) {
                   1311:                                        n = (NODE)FA0(f2);
                   1312:                                        for ( i = 0; n; n = NEXT(n), i++ ) {
                   1313:                                                fi = (FNODE)BDY(n);
                   1314:                                                if ( fi->id != I_FORMULA || FA0(fi) )
                   1315:                                                        break;
                   1316:                                        }
                   1317:                                        if ( n )
                   1318:                                                fnodetotex_tb((FNODE)FA2(f),tb);
                   1319:                                } else
                   1320:                                        fnodetotex_tb((FNODE)FA2(f),tb);
                   1321:                        } else if ( !strcmp(opname,"/") ) {
                   1322:                                write_tb("\\frac{",tb);
                   1323:                                fnodetotex_tb((FNODE)FA1(f),tb);
                   1324:                                write_tb("} {",tb);
                   1325:                                fnodetotex_tb((FNODE)FA2(f),tb);
                   1326:                                write_tb("}",tb);
                   1327:                        } else if ( !strcmp(opname,"^") ) {
                   1328:                                fnodetotex_tb((FNODE)FA1(f),tb);
                   1329:                                write_tb("^{",tb);
1.51      noro     1330:                                fnodetotex_tb(strip_paren((FNODE)FA2(f)),tb);
1.23      noro     1331:                                write_tb("} ",tb);
                   1332:                        } else if ( !strcmp(opname,"%") ) {
                   1333:                                fnodetotex_tb((FNODE)FA1(f),tb);
                   1334:                                write_tb(" {\\rm mod}\\, ",tb);
                   1335:                                fnodetotex_tb((FNODE)FA2(f),tb);
                   1336:                        } else
                   1337:                                error("invalid binary operator");
                   1338:                        break;
                   1339:
                   1340:                case I_COP:
                   1341:                        switch( (cid)FA0(f) ) {
                   1342:                                case C_EQ:
                   1343:                                        fnodetotex_tb((FNODE)FA1(f),tb);
                   1344:                                        write_tb(" = ",tb);
                   1345:                                        fnodetotex_tb((FNODE)FA2(f),tb);
                   1346:                                        break;
                   1347:                                case C_NE:
                   1348:                                        fnodetotex_tb((FNODE)FA1(f),tb);
                   1349:                                        write_tb(" \\neq ",tb);
                   1350:                                        fnodetotex_tb((FNODE)FA2(f),tb);
                   1351:                                        break;
                   1352:                                case C_GT:
                   1353:                                        fnodetotex_tb((FNODE)FA1(f),tb);
1.45      noro     1354:                                        write_tb(" > ",tb);
1.23      noro     1355:                                        fnodetotex_tb((FNODE)FA2(f),tb);
                   1356:                                        break;
                   1357:                                case C_LT:
                   1358:                                        fnodetotex_tb((FNODE)FA1(f),tb);
1.45      noro     1359:                                        write_tb(" < ",tb);
1.23      noro     1360:                                        fnodetotex_tb((FNODE)FA2(f),tb);
1.14      noro     1361:                                        break;
1.23      noro     1362:                                case C_GE:
                   1363:                                        fnodetotex_tb((FNODE)FA1(f),tb);
                   1364:                                        write_tb(" \\geq ",tb);
                   1365:                                        fnodetotex_tb((FNODE)FA2(f),tb);
1.14      noro     1366:                                        break;
1.23      noro     1367:                                case C_LE:
                   1368:                                        fnodetotex_tb((FNODE)FA1(f),tb);
                   1369:                                        write_tb(" \\leq ",tb);
                   1370:                                        fnodetotex_tb((FNODE)FA2(f),tb);
1.14      noro     1371:                                        break;
                   1372:                        }
                   1373:                        break;
                   1374:
1.23      noro     1375:                case I_LOP:
                   1376:                        switch( (lid)FA0(f) ) {
                   1377:                                case L_EQ:
                   1378:                                        fnodetotex_tb((FNODE)FA1(f),tb);
                   1379:                                        write_tb(" = ",tb);
                   1380:                                        fnodetotex_tb((FNODE)FA2(f),tb);
                   1381:                                        break;
                   1382:                                case L_NE:
                   1383:                                        fnodetotex_tb((FNODE)FA1(f),tb);
                   1384:                                        write_tb(" \\neq ",tb);
                   1385:                                        fnodetotex_tb((FNODE)FA2(f),tb);
                   1386:                                        break;
                   1387:                                case L_GT:
                   1388:                                        fnodetotex_tb((FNODE)FA1(f),tb);
1.45      noro     1389:                                        write_tb(" > ",tb);
1.23      noro     1390:                                        fnodetotex_tb((FNODE)FA2(f),tb);
                   1391:                                        break;
                   1392:                                case L_LT:
                   1393:                                        fnodetotex_tb((FNODE)FA1(f),tb);
1.45      noro     1394:                                        write_tb(" < ",tb);
1.23      noro     1395:                                        fnodetotex_tb((FNODE)FA2(f),tb);
                   1396:                                        break;
                   1397:                                case L_GE:
                   1398:                                        fnodetotex_tb((FNODE)FA1(f),tb);
                   1399:                                        write_tb(" \\geq ",tb);
                   1400:                                        fnodetotex_tb((FNODE)FA2(f),tb);
1.14      noro     1401:                                        break;
1.23      noro     1402:                                case L_LE:
                   1403:                                        fnodetotex_tb((FNODE)FA1(f),tb);
                   1404:                                        write_tb(" \\leq ",tb);
                   1405:                                        fnodetotex_tb((FNODE)FA2(f),tb);
1.14      noro     1406:                                        break;
1.23      noro     1407:                                case L_AND:
                   1408:                                        fnodetotex_tb((FNODE)FA1(f),tb);
1.14      noro     1409:                                        write_tb(" {\\rm \\ and\\ } ",tb);
1.23      noro     1410:                                        fnodetotex_tb((FNODE)FA2(f),tb);
                   1411:                                        break;
                   1412:                                case L_OR:
1.14      noro     1413:                                        fnodetotex_tb((FNODE)FA1(f),tb);
1.23      noro     1414:                                        write_tb(" {\\rm \\ or\\ } ",tb);
                   1415:                                        fnodetotex_tb((FNODE)FA2(f),tb);
1.14      noro     1416:                                        break;
1.23      noro     1417:                                case L_NOT:
                   1418:                                        /* XXX : L_NOT is a unary operator */
                   1419:                                        write_tb("\\neg (",tb);
1.14      noro     1420:                                        fnodetotex_tb((FNODE)FA1(f),tb);
1.23      noro     1421:                                        write_tb(")",tb);
                   1422:                                        return;
1.14      noro     1423:                        }
                   1424:                        break;
                   1425:
1.23      noro     1426:                case I_AND:
                   1427:                        fnodetotex_tb((FNODE)FA0(f),tb);
                   1428:                        write_tb(" {\\rm \\ and\\ } ",tb);
                   1429:                        fnodetotex_tb((FNODE)FA1(f),tb);
                   1430:                        break;
                   1431:
                   1432:                case I_OR:
                   1433:                        fnodetotex_tb((FNODE)FA0(f),tb);
                   1434:                        write_tb(" {\\rm \\ or\\ } ",tb);
                   1435:                        fnodetotex_tb((FNODE)FA1(f),tb);
                   1436:                        break;
                   1437:
1.14      noro     1438:                /* ternary operators */
                   1439:                case I_CE:
                   1440:                        error("fnodetotex_tb : not implemented yet");
                   1441:                        break;
                   1442:
                   1443:                /* lists */
                   1444:                case I_LIST:
                   1445:                        write_tb(" [ ",tb);
                   1446:                        n = (NODE)FA0(f);
                   1447:                        fnodenodetotex_tb(n,tb);
                   1448:                        write_tb("]",tb);
                   1449:                        break;
                   1450:
                   1451:                /* function */
1.23      noro     1452:                case I_FUNC:
1.40      noro     1453:                        if ( !strcmp(((FUNC)FA0(f))->name,"@pi") )
                   1454:                                write_tb("\\pi",tb);
                   1455:                        else if ( !strcmp(((FUNC)FA0(f))->name,"@e") )
                   1456:                                write_tb("e",tb);
                   1457:                        else {
                   1458:                                opname = conv_rule(((FUNC)FA0(f))->name);
                   1459:                                write_tb(opname,tb);
                   1460:                                write_tb("(",tb);
                   1461:                                fargstotex_tb(opname,FA1(f),tb);
                   1462:                                write_tb(")",tb);
                   1463:                        }
1.23      noro     1464:                        break;
                   1465:
                   1466:                /* XXX */
                   1467:                case I_CAR:
1.28      noro     1468:                        opname = conv_rule("car");
1.23      noro     1469:                        write_tb(opname,tb);
                   1470:                        write_tb("(",tb);
                   1471:                        fargstotex_tb(opname,FA0(f),tb);
                   1472:                        write_tb(")",tb);
                   1473:                        break;
                   1474:
                   1475:                case I_CDR:
1.28      noro     1476:                        opname = conv_rule("cdr");
1.23      noro     1477:                        write_tb(opname,tb);
                   1478:                        write_tb("(",tb);
                   1479:                        fargstotex_tb(opname,FA0(f),tb);
                   1480:                        write_tb(")",tb);
                   1481:                        break;
                   1482:
                   1483:                /* exponent vector */
                   1484:                case I_EV:
                   1485:                        n = (NODE)FA0(f);
1.31      noro     1486:                        if ( dp_vars_hweyl ) {
                   1487:                                elen = length(n);
                   1488:                                elen2 = elen>>1;
                   1489:                                elen = elen2<<1;
                   1490:                        }
1.23      noro     1491:                        allzero = 1;
1.27      noro     1492:                        if ( show_lt && is_lt )
                   1493:                                write_tb("\\underline{",tb);
1.23      noro     1494:                        for ( t0 = 0, i = 0; n; n = NEXT(n), i++ ) {
                   1495:                                fi = (FNODE)BDY(n);
                   1496:                                if ( fi->id == I_FORMULA && !FA0(fi) ) continue;
                   1497:                                allzero = 0;
1.38      noro     1498:                                if ( dp_vars && i < dp_vars_len ) {
                   1499:                                        strcpy(vname,dp_vars[i]);
                   1500:                                        vname_conv = conv_rule(vname);
                   1501:                                } else {
                   1502:                                        if ( dp_vars_hweyl ) {
1.44      noro     1503:                                                if ( i < elen2 ) {
1.38      noro     1504:                                                        strcpy(prefix,dp_vars_prefix?dp_vars_prefix:"x");
1.44      noro     1505:                                                        prefix_conv = conv_rule(prefix);
                   1506:                                                        vname_conv = (char *)ALLOCA(strlen(prefix_conv)+50);
1.46      noro     1507:                                                        si = i+dp_vars_origin;
                   1508:                                                        sprintf(vname_conv,(si>=0&&si<10)?"%s_%d":"%s_{%d}",
                   1509:                                                                prefix_conv,si);
1.44      noro     1510:                                                } else if ( i < elen ) {
1.46      noro     1511:                                                        strcpy(prefix,
                   1512:                                                                dp_dvars_prefix?dp_dvars_prefix:"\\partial");
1.44      noro     1513:                                                        prefix_conv = conv_rule(prefix);
                   1514:                                                        vname_conv = (char *)ALLOCA(strlen(prefix_conv)+50);
1.46      noro     1515:                                                        si = i+dp_dvars_origin-elen2;
                   1516:                                                        sprintf(vname_conv,(si>=0&&si<10)?"%s_%d":"%s_{%d}",
                   1517:                                                                prefix_conv,si);
1.44      noro     1518:                                                } else {
1.38      noro     1519:                                                        strcpy(prefix,"h");
1.44      noro     1520:                                                        vname_conv = conv_rule(prefix);
                   1521:                                                }
                   1522:                                        } else {
1.38      noro     1523:                                                strcpy(prefix,dp_vars_prefix?dp_vars_prefix:"x");
1.44      noro     1524:                                                prefix_conv = conv_rule(prefix);
                   1525:                                                vname_conv = (char *)ALLOCA(strlen(prefix_conv)+50);
1.46      noro     1526:                                                si = i+dp_vars_origin;
                   1527:                                                sprintf(vname_conv,(si>=0&&si<10)?"%s_%d":"%s_{%d}",
                   1528:                                                        prefix_conv,si);
1.44      noro     1529:                                        }
1.38      noro     1530:                                }
1.23      noro     1531:                                if ( fi->id == I_FORMULA && UNIQ(FA0(fi)) ) {
                   1532:                                        len = strlen(vname_conv);
                   1533:                                        opname = MALLOC_ATOMIC(len+2);
                   1534:                                        sprintf(opname,"%s ",vname_conv);
1.14      noro     1535:                                        write_tb(opname,tb);
1.23      noro     1536:                                } else {
                   1537:                                        len = strlen(vname_conv);
                   1538:                                        /* 2: ^{ */
                   1539:                                        opname = MALLOC_ATOMIC(len+1+2);
                   1540:                                        sprintf(opname,"%s^{",vname_conv);
1.14      noro     1541:                                        write_tb(opname,tb);
1.23      noro     1542:                                        fnodetotex_tb((FNODE)BDY(n),tb);
                   1543:                                        write_tb("} ",tb);
                   1544:                                }
1.14      noro     1545:                        }
1.23      noro     1546:                        /* XXX */
                   1547:                        if ( allzero )
                   1548:                                write_tb(" 1 ",tb);
1.27      noro     1549:                        if ( show_lt && is_lt ) {
                   1550:                                write_tb("}",tb);
                   1551:                                is_lt = 0;
                   1552:                        }
1.14      noro     1553:                        break;
                   1554:
1.23      noro     1555:                /* string */
1.14      noro     1556:                case I_STR:
                   1557:                        write_tb((char *)FA0(f),tb);
                   1558:                        break;
                   1559:
1.23      noro     1560:                /* internal object */
1.14      noro     1561:                case I_FORMULA:
                   1562:                        obj = (Obj)FA0(f);
1.40      noro     1563:                        if ( !obj )
1.42      noro     1564:                                write_tb("0",tb);
1.40      noro     1565:                        else if ( OID(obj) == O_N && NID(obj) == N_C ) {
                   1566:                                cplx = (C)obj;
                   1567:                                write_tb("(",tb);
                   1568:                                if ( cplx->r ) {
                   1569:                                        r = objtostr((Obj)cplx->r); write_tb(r,tb);
                   1570:                                }
                   1571:                                if ( cplx->i ) {
                   1572:                                        if ( cplx->r && compnum(0,cplx->i,0) > 0 ) {
                   1573:                                                write_tb("+",tb);
                   1574:                                                if ( !UNIQ(cplx->i) ) {
                   1575:                                                        r = objtostr((Obj)cplx->i); write_tb(r,tb);
                   1576:                                                }
                   1577:                                        } else if ( MUNIQ(cplx->i) )
                   1578:                                                write_tb("-",tb);
                   1579:                                        else if ( !UNIQ(cplx->i) ) {
                   1580:                                                r = objtostr((Obj)cplx->i); write_tb(r,tb);
                   1581:                                        }
                   1582:                                        write_tb("\\sqrt{-1}",tb);
                   1583:                                }
                   1584:                                write_tb(")",tb);
                   1585:                        } else if ( OID(obj) == O_P )
                   1586:                                write_tb(conv_rule(VR((P)obj)->name),tb);
                   1587:                        else
                   1588:                                write_tb(objtostr(obj),tb);
1.14      noro     1589:                        break;
                   1590:
1.23      noro     1591:                /* program variable */
1.14      noro     1592:                case I_PVAR:
                   1593:                        if ( FA1(f) )
                   1594:                                error("fnodetotex_tb : not implemented yet");
                   1595:                        GETPVNAME(FA0(f),opname);
                   1596:                        write_tb(opname,tb);
                   1597:                        break;
                   1598:
                   1599:                default:
                   1600:                        error("fnodetotex_tb : not implemented yet");
                   1601:        }
1.40      noro     1602: }
                   1603:
                   1604: char *objtostr(Obj obj)
                   1605: {
                   1606:        int len;
                   1607:        char *r;
                   1608:
                   1609:        len = estimate_length(CO,obj);
                   1610:        r = (char *)MALLOC_ATOMIC(len+1);
                   1611:        soutput_init(r);
                   1612:        sprintexpr(CO,obj);
                   1613:        return r;
1.50      ohara    1614: }
                   1615:
                   1616: void Psprintf(NODE arg,STRING *rp)
                   1617: {
                   1618:     STRING string;
                   1619:     char *s,*t,*r;
                   1620:     int argc,n,len;
                   1621:     NODE node;
                   1622:
                   1623:     string = (STRING)ARG0(arg);
                   1624:     asir_assert(string,O_STR,"sprintf");
                   1625:     s = BDY(string);
                   1626:     for(n = 0, t = s; *t; t++) {
                   1627:         if (*t=='%' && *(t+1)=='a') {
                   1628:             n++;
                   1629:         }
                   1630:     }
                   1631:     for(node = NEXT(arg), argc = 0, len = strlen(s); node; node = NEXT(node), argc++) {
                   1632:         len += estimate_length(CO,BDY(node));
                   1633:     }
                   1634:     if (argc < n) {
                   1635:         error("sprintf: invalid argument");
                   1636:     }
                   1637:     r = (char *)MALLOC_ATOMIC(len);
                   1638:     for(node = NEXT(arg), t = r; *s; s++) {
                   1639:         if (*s=='%' && *(s+1)=='a') {
                   1640:             strcpy(t,objtostr(BDY(node)));
                   1641:             node = NEXT(node);
                   1642:             t = strchr(t,0);
                   1643:             s++;
                   1644:         }else {
                   1645:             *t++ = *s;
                   1646:         }
                   1647:     }
                   1648:     *t = 0;
                   1649:     MKSTR(*rp,r);
1.14      noro     1650: }
                   1651:
                   1652: void fnodenodetotex_tb(NODE n,TB tb)
                   1653: {
                   1654:        for ( ; n; n = NEXT(n) ) {
1.27      noro     1655:                is_lt = 1;
1.14      noro     1656:                fnodetotex_tb((FNODE)BDY(n),tb);
                   1657:                if ( NEXT(n) ) write_tb(", ",tb);
                   1658:        }
                   1659: }
                   1660:
                   1661: void fargstotex_tb(char *name,FNODE f,TB tb)
                   1662: {
                   1663:        NODE n;
                   1664:
                   1665:        if ( !strcmp(name,"matrix") ) {
                   1666:                error("fargstotex_tb : not implemented yet");
                   1667:        } else if ( !strcmp(name,"vector") ) {
                   1668:                error("fargstotex_tb : not implemented yet");
                   1669:        } else {
                   1670:                if ( f->id == I_LIST ) {
                   1671:                        n = (NODE)FA0(f);
                   1672:                        fnodenodetotex_tb(n,tb);
                   1673:                } else
                   1674:                        fnodetotex_tb(f,tb);
1.35      noro     1675:        }
                   1676: }
                   1677:
                   1678: int top_is_minus(FNODE f)
                   1679: {
                   1680:        char *opname;
                   1681:        int len;
                   1682:        Obj obj;
                   1683:
                   1684:        if ( !f )
                   1685:                return 0;
                   1686:        switch ( f->id ) {
                   1687:                case I_MINUS:
                   1688:                        return 1;
                   1689:                case I_BOP:
                   1690:                        opname = ((ARF)FA0(f))->name;
                   1691:                        switch ( opname[0] ) {
                   1692:                                case '+': case '*': case '/': case '^': case '%':
                   1693:                                        return top_is_minus((FNODE)FA1(f));
                   1694:                                case '-':
                   1695:                                        if ( FA1(f) )
                   1696:                                                return top_is_minus((FNODE)FA1(f));
                   1697:                                        else
                   1698:                                                return 1;
                   1699:                                default:
                   1700:                                        return 0;
                   1701:                        }
                   1702:                        break;
                   1703:                case I_COP:
                   1704:                        return top_is_minus((FNODE)FA1(f));
                   1705:                case I_LOP:
                   1706:                        if ( (lid)FA0(f) == L_NOT ) return 0;
                   1707:                        else return top_is_minus((FNODE)FA1(f));
                   1708:                case I_AND: case I_OR:
                   1709:                        return top_is_minus((FNODE)FA0(f));
                   1710:                case I_FORMULA:
                   1711:                        obj = (Obj)FA0(f);
1.36      noro     1712:                        if ( !obj )
                   1713:                                return 0;
                   1714:                        else {
                   1715:                                switch ( OID(obj) ) {
                   1716:                                        case O_N:
                   1717:                                                return mmono((P)obj);
                   1718:                                        case O_P:
                   1719:                                                /* must be a variable */
                   1720:                                                opname = conv_rule(VR((P)obj)->name);
                   1721:                                                return opname[0]=='-';
                   1722:                                        default:
                   1723:                                                /* ??? */
                   1724:                                                len = estimate_length(CO,obj);
                   1725:                                                opname = (char *)MALLOC_ATOMIC(len+1);
                   1726:                                                soutput_init(opname);
                   1727:                                                sprintexpr(CO,obj);
                   1728:                                                return opname[0]=='-';
                   1729:                                }
1.35      noro     1730:                        }
                   1731:                default:
                   1732:                        return 0;
1.14      noro     1733:        }
1.47      noro     1734: }
                   1735:
                   1736: FNODE flatten_fnode(FNODE,char *);
                   1737:
1.48      noro     1738: void Pflatten_quote(NODE arg,Obj *rp)
1.47      noro     1739: {
                   1740:        FNODE f;
                   1741:        QUOTE q;
                   1742:
1.48      noro     1743:        if ( !ARG0(arg) || OID((Obj)ARG0(arg)) != O_QUOTE )
                   1744:                *rp = (Obj)ARG0(arg);
1.65      noro     1745:        else if ( argc(arg) == 1 ) {
                   1746:                f = flatten_fnode(BDY((QUOTE)ARG0(arg)),"+");
                   1747:                f = flatten_fnode(f,"*");
                   1748:                MKQUOTE(q,f);
                   1749:                *rp = (Obj)q;
                   1750:        } else {
1.48      noro     1751:                f = flatten_fnode(BDY((QUOTE)ARG0(arg)),BDY((STRING)ARG1(arg)));
                   1752:                MKQUOTE(q,f);
                   1753:                *rp = (Obj)q;
                   1754:        }
1.63      noro     1755: }
                   1756:
                   1757: void Pget_quote_id(NODE arg,Q *rp)
                   1758: {
                   1759:        FNODE f;
                   1760:        QUOTE q;
                   1761:
                   1762:        q = (QUOTE)ARG0(arg);
                   1763:        if ( !q || OID(q) != O_QUOTE )
                   1764:                error("get_quote_id : invalid argument");
                   1765:        f = BDY(q);
                   1766:        STOQ((int)f->id,*rp);
1.48      noro     1767: }
                   1768:
                   1769: void Pquote_to_funargs(NODE arg,LIST *rp)
                   1770: {
                   1771:        fid_spec_p spec;
                   1772:        QUOTE q;
                   1773:        QUOTEARG qa;
                   1774:        FNODE f;
                   1775:        STRING s;
                   1776:        QUOTE r;
                   1777:        int i;
                   1778:        Q id,a;
1.49      noro     1779:        LIST l;
                   1780:        NODE t0,t,w,u,u0;
1.48      noro     1781:
                   1782:        q = (QUOTE)ARG0(arg);
                   1783:        if ( !q || OID(q) != O_QUOTE )
                   1784:                error("quote_to_funargs : invalid argument");
                   1785:        f = BDY(q);
                   1786:        if ( !f ) {
                   1787:                MKLIST(*rp,0);
                   1788:                return;
                   1789:        }
                   1790:        get_fid_spec(f->id,&spec);
                   1791:        if ( !spec )
                   1792:                error("quote_to_funargs : not supported yet");
                   1793:        t0 = 0;
                   1794:        STOQ((int)f->id,id);
                   1795:        NEXTNODE(t0,t);
                   1796:        BDY(t) = (pointer)id;
                   1797:        for ( i = 0; spec->type[i] != A_end; i++ ) {
                   1798:                NEXTNODE(t0,t);
                   1799:                switch ( spec->type[i] ) {
                   1800:                        case A_fnode:
                   1801:                                MKQUOTE(r,(FNODE)f->arg[i]);
                   1802:                                BDY(t) = (pointer)r;
                   1803:                                break;
                   1804:                        case A_int:
                   1805:                                STOQ((int)f->arg[i],a);
                   1806:                                BDY(t) = (pointer)a;
                   1807:                                break;
                   1808:                        case A_str:
                   1809:                                MKSTR(s,(char *)f->arg[i]);
                   1810:                                BDY(t) = (pointer)s;
                   1811:                                break;
                   1812:                        case A_internal:
                   1813:                                BDY(t) = (pointer)f->arg[i];
                   1814:                                break;
1.49      noro     1815:                        case A_node:
                   1816:                                w = (NODE)f->arg[i];
                   1817:                                for ( u0 = 0; w; w = NEXT(w) ){
                   1818:                                        NEXTNODE(u0,u);
                   1819:                                        MKQUOTE(r,(FNODE)BDY(w));
                   1820:                                        BDY(u) = (pointer)r;
                   1821:                                }
                   1822:                                if ( u0 ) NEXT(u) = 0;
                   1823:                                MKLIST(l,u0);
                   1824:                                BDY(t) = (pointer)l;
                   1825:                                break;
1.48      noro     1826:                        default:
                   1827:                                MKQUOTEARG(qa,spec->type[i],f->arg[i]);
                   1828:                                BDY(t) = (pointer)qa;
                   1829:                                break;
                   1830:                }
                   1831:        }
                   1832:        if ( t0 ) NEXT(t) = 0;
                   1833:        MKLIST(*rp,t0);
                   1834: }
                   1835:
                   1836: void Pfunargs_to_quote(NODE arg,QUOTE *rp)
                   1837: {
                   1838:        fid_spec_p spec;
                   1839:        QUOTE q;
                   1840:        QUOTEARG qa;
                   1841:        FNODE f;
                   1842:        STRING s;
1.49      noro     1843:        QUOTE r,b;
1.48      noro     1844:        int i;
                   1845:        LIST l;
                   1846:        fid id;
                   1847:        Obj a;
1.49      noro     1848:        NODE t0,t,u0,u,w;
1.48      noro     1849:
                   1850:        l = (LIST)ARG0(arg);
                   1851:        if ( !l || OID(l) != O_LIST || !(t=BDY(l)) )
                   1852:                error("funargs_to_quote : invalid argument");
                   1853:        t = BDY(l);
                   1854:        id = (fid)QTOS((Q)BDY(t)); t = NEXT(t);
                   1855:        get_fid_spec(id,&spec);
                   1856:        if ( !spec )
                   1857:                error("funargs_to_quote : not supported yet");
                   1858:        for ( i = 0; spec->type[i] != A_end; i++ );
                   1859:        NEWFNODE(f,i);
                   1860:        f->id = id;
                   1861:        for ( i = 0; spec->type[i] != A_end; i++, t = NEXT(t) ) {
                   1862:                if ( !t )
                   1863:                        error("funargs_to_quote : argument mismatch");
                   1864:                a = (Obj)BDY(t);
                   1865:                switch ( spec->type[i] ) {
                   1866:                        case A_fnode:
                   1867:                                if ( !a || OID(a) != O_QUOTE )
                   1868:                                        error("funargs_to_quote : invalid argument");
                   1869:                                f->arg[i] = BDY((QUOTE)a);
                   1870:                                break;
                   1871:                        case A_int:
                   1872:                                if ( !INT(a) )
                   1873:                                        error("funargs_to_quote : invalid argument");
                   1874:                                f->arg[i] = (pointer)QTOS((Q)a);
                   1875:                                break;
                   1876:                        case A_str:
                   1877:                                if ( !a || OID(a) != O_STR )
                   1878:                                        error("funargs_to_quote : invalid argument");
                   1879:                                f->arg[i] = (pointer)BDY((STRING)a);
                   1880:                                break;
                   1881:                        case A_internal:
                   1882:                                f->arg[i] = (pointer)a;
1.49      noro     1883:                                break;
                   1884:                        case A_node:
                   1885:                                if ( !a || OID(a) != O_LIST )
                   1886:                                        error("funargs_to_quote : invalid argument");
                   1887:                                u0 = 0;
                   1888:                                for ( w = BDY((LIST)a); w; w = NEXT(w) ) {
                   1889:                                        NEXTNODE(u0,u);
                   1890:                                        b = (QUOTE)BDY(w);
                   1891:                                        if ( !b || OID(b) != O_QUOTE )
                   1892:                                                error("funargs_to_quote : invalid argument");
                   1893:                                        BDY(u) = BDY(b);
                   1894:                                }
                   1895:                                if ( u0 ) NEXT(u) = 0;
                   1896:                                f->arg[i] = (pointer)u0;
1.48      noro     1897:                                break;
                   1898:                        default:
                   1899:                                if ( !a || OID(a) != O_QUOTEARG ||
                   1900:                                        ((QUOTEARG)a)->type != spec->type[i] )
                   1901:                                        error("funargs_to_quote : invalid argument");
                   1902:                                f->arg[i] = BDY((QUOTEARG)a);
                   1903:                                break;
                   1904:                }
                   1905:        }
                   1906:        MKQUOTE(*rp,f);
1.69    ! noro     1907: }
        !          1908:
        !          1909: int fnode_is_number(FNODE f)
        !          1910: {
        !          1911:        Obj obj;
        !          1912:
        !          1913:        switch ( f->id ) {
        !          1914:                case I_MINUS: case I_PAREN:
        !          1915:                        return fnode_is_number(FA0(f));
        !          1916:
        !          1917:                case I_FORMULA:
        !          1918:                        obj = FA0(f);
        !          1919:                        if ( !obj ) return 1;
        !          1920:                        else if ( OID(obj) == O_QUOTE )
        !          1921:                                return fnode_is_number(BDY((QUOTE)obj));
        !          1922:                        else if ( NUM(obj) ) return 1;
        !          1923:                        else return 0;
        !          1924:
        !          1925:                case I_BOP:
        !          1926:                        return fnode_is_number(FA1(f)) && fnode_is_number(FA2(f));
        !          1927:
        !          1928:                default:
        !          1929:                        return 0;
        !          1930:        }
        !          1931: }
        !          1932:
        !          1933: int fnode_is_rational(FNODE f)
        !          1934: {
        !          1935:        Obj obj;
        !          1936:
        !          1937:        switch ( f->id ) {
        !          1938:                case I_MINUS: case I_PAREN:
        !          1939:                        return fnode_is_number(FA0(f));
        !          1940:
        !          1941:                case I_FORMULA:
        !          1942:                        obj = FA0(f);
        !          1943:                        if ( !obj ) return 1;
        !          1944:                        else if ( OID(obj) == O_QUOTE )
        !          1945:                                return fnode_is_rational(BDY((QUOTE)obj));
        !          1946:                        else if ( NUM(obj) && RATN(obj) ) return 1;
        !          1947:                        else return 0;
        !          1948:
        !          1949:                case I_BOP:
        !          1950:                        if ( !strcmp(((ARF)FA0(f))->name,"^")  )
        !          1951:                                return fnode_is_rational(FA1(f)) && fnode_is_integer(FA2(f));
        !          1952:                        else
        !          1953:                                return fnode_is_rational(FA1(f)) && fnode_is_rational(FA2(f));
        !          1954:
        !          1955:                default:
        !          1956:                        return 0;
        !          1957:        }
        !          1958: }
        !          1959:
        !          1960: int fnode_is_integer(FNODE f)
        !          1961: {
        !          1962:        Obj obj;
        !          1963:
        !          1964:        switch ( f->id ) {
        !          1965:                case I_MINUS: case I_PAREN:
        !          1966:                        return fnode_is_integer(FA0(f));
        !          1967:
        !          1968:                case I_FORMULA:
        !          1969:                        obj = FA0(f);
        !          1970:                        if ( !obj ) return 1;
        !          1971:                        else if ( OID(obj) == O_QUOTE )
        !          1972:                                return fnode_is_integer(BDY((QUOTE)obj));
        !          1973:                        else if ( INT(obj)) return 1;
        !          1974:                        else return 0;
        !          1975:
        !          1976:                case I_BOP:
        !          1977:                        if ( !strcmp(((ARF)FA0(f))->name,"^")  )
        !          1978:                                return fnode_is_integer(FA1(f))
        !          1979:                                        && fnode_is_nonnegative_integer(FA2(f));
        !          1980:                        else if ( !strcmp(((ARF)FA0(f))->name,"/")  )
        !          1981:                                return fnode_is_integer(FA1(f)) &&
        !          1982:                                        ( fnode_is_one(FA2(f)) || fnode_is_minusone(FA2(f)) );
        !          1983:                        else
        !          1984:                                return fnode_is_integer(FA1(f)) && fnode_is_integer(FA2(f));
        !          1985:
        !          1986:                default:
        !          1987:                        return 0;
        !          1988:        }
        !          1989: }
        !          1990:
        !          1991: int fnode_is_nonnegative_integer(FNODE f)
        !          1992: {
        !          1993:        Q n;
        !          1994:
        !          1995:        n = eval(f);
        !          1996:        if ( !n || (INT(n) && SGN(n) > 0) ) return 1;
        !          1997:        else return 0;
        !          1998: }
        !          1999:
        !          2000: int fnode_is_one(FNODE f)
        !          2001: {
        !          2002:        Q n;
        !          2003:
        !          2004:        n = eval(f);
        !          2005:        if ( UNIQ(n) ) return 1;
        !          2006:        else return 0;
        !          2007: }
        !          2008:
        !          2009: int fnode_is_minusone(FNODE f)
        !          2010: {
        !          2011:        Q n;
        !          2012:
        !          2013:        n = eval(f);
        !          2014:        if ( MUNIQ(n) ) return 1;
        !          2015:        else return 0;
        !          2016: }
        !          2017:
        !          2018: int fnode_is_dependent(FNODE f,V v)
        !          2019: {
        !          2020:        Obj obj;
        !          2021:        FNODE arg;
        !          2022:        NODE t;
        !          2023:
        !          2024:        switch ( f->id ) {
        !          2025:                case I_MINUS: case I_PAREN:
        !          2026:                        return fnode_is_dependent(FA0(f),v);
        !          2027:
        !          2028:                case I_FORMULA:
        !          2029:                        obj = FA0(f);
        !          2030:                        if ( !obj ) return 0;
        !          2031:                        else if ( OID(obj) == O_QUOTE )
        !          2032:                                return fnode_is_dependent(BDY((QUOTE)obj),v);
        !          2033:                        else if ( obj_is_dependent(obj,v) ) return 1;
        !          2034:                        else return 0;
        !          2035:
        !          2036:                case I_BOP:
        !          2037:                        return fnode_is_dependent(FA1(f),v) || fnode_is_dependent(FA2(f),v);
        !          2038:
        !          2039:                case I_FUNC:
        !          2040:                        arg = (FNODE)FA1(f);
        !          2041:                        for ( t = FA0(arg); t; t = NEXT(t) )
        !          2042:                                if ( fnode_is_dependent(BDY(t),v) ) return 1;
        !          2043:                        return 0;
        !          2044:
        !          2045:                default:
        !          2046:                        return 0;
        !          2047:        }
1.1       noro     2048: }

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