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

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

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