=================================================================== RCS file: /home/cvs/OpenXM_contrib2/asir2000/builtin/strobj.c,v retrieving revision 1.49 retrieving revision 1.64 diff -u -p -r1.49 -r1.64 --- OpenXM_contrib2/asir2000/builtin/strobj.c 2004/07/13 09:10:38 1.49 +++ OpenXM_contrib2/asir2000/builtin/strobj.c 2005/09/29 08:55:26 1.64 @@ -45,7 +45,7 @@ * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE, * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE. * - * $OpenXM: OpenXM_contrib2/asir2000/builtin/strobj.c,v 1.48 2004/07/13 07:59:53 noro Exp $ + * $OpenXM: OpenXM_contrib2/asir2000/builtin/strobj.c,v 1.63 2005/09/28 08:08:34 noro Exp $ */ #include "ca.h" #include "parse.h" @@ -65,6 +65,7 @@ struct TeXSymbol { extern char *parse_strp; +void Psprintf(); void Prtostr(), Pstrtov(), Peval_str(); void Pstrtoascii(), Pasciitostr(); void Pstr_len(), Pstr_chr(), Psub_str(); @@ -76,7 +77,10 @@ void Pquotetotex_tb(); void Pquotetotex(); void Pquotetotex_env(); void Pflatten_quote(); -void Pquote_to_funargs(),Pfunargs_to_quote(); +void Pquote_to_funargs(),Pfunargs_to_quote(),Pget_function_name(); +void Pquote_unify(),Pget_quote_id(); +void Pquote_to_nary(),Pquote_to_bin(); +void do_assign(NODE arg); void fnodetotex_tb(FNODE f,TB tb); char *symbol_name(char *name); char *conv_rule(char *name); @@ -86,8 +90,10 @@ void tb_to_string(TB tb,STRING *rp); void fnodenodetotex_tb(NODE n,TB tb); void fargstotex_tb(char *opname,FNODE f,TB tb); int top_is_minus(FNODE f); +int quote_unify(Obj f,Obj pat,NODE *rp); struct ftab str_tab[] = { + {"sprintf",Psprintf,-99999999}, {"rtostr",Prtostr,1}, {"strtov",Pstrtov,1}, {"eval_str",Peval_str,1}, @@ -100,12 +106,17 @@ struct ftab str_tab[] = { {"clear_tb",Pclear_tb,1}, {"tb_to_string",Ptb_to_string,1}, {"string_to_tb",Pstring_to_tb,1}, + {"get_quote_id",Pget_quote_id,1}, + {"quote_to_nary",Pquote_to_nary,1}, + {"quote_to_bin",Pquote_to_bin,2}, {"quotetotex_tb",Pquotetotex_tb,2}, {"quotetotex",Pquotetotex,1}, {"quotetotex_env",Pquotetotex_env,-99999999}, {"flatten_quote",Pflatten_quote,2}, {"quote_to_funargs",Pquote_to_funargs,1}, + {"quote_unify",Pquote_unify,2}, {"funargs_to_quote",Pfunargs_to_quote,1}, + {"get_function_name",Pget_function_name,1}, {0,0,0}, }; @@ -501,6 +512,208 @@ void Pwrite_to_tb(NODE arg,Q *rp) *rp = 0; } +FNODE partial_eval(FNODE), quote_to_nary(FNODE), quote_to_bin(FNODE,int); + +void Pquote_to_nary(NODE arg,QUOTE *rp) +{ + FNODE f; + + f = quote_to_nary(BDY((QUOTE)ARG0(arg))); + MKQUOTE(*rp,f); +} + +void Pquote_to_bin(NODE arg,QUOTE *rp) +{ + FNODE f; + int direction; + + direction = QTOS((Q)ARG1(arg)); + f = quote_to_bin(BDY((QUOTE)ARG0(arg)),direction); + + MKQUOTE(*rp,f); +} + +void Pquote_unify(NODE arg,Q *rp) +{ + FNODE f,g; + QUOTE q; + NODE r; + int ret; + +#if 0 + g = partial_eval(BDY(((QUOTE)ARG0(arg)))); + MKQUOTE(q,g); + ret = quote_unify((Obj)q,(Obj)ARG1(arg),&r); +#else + ret = quote_unify((Obj)ARG0(arg),(Obj)ARG1(arg),&r); +#endif + if ( ret ) { + do_assign(r); + *rp = ONE; + } else + *rp = 0; +} + +void do_assign(NODE arg) +{ + NODE t,pair; + int pv; + + QUOTE value; + + for ( t = arg; t; t = NEXT(t) ) { + pair = BDY((LIST)BDY(t)); + pv = (int)FA0((FNODE)BDY((QUOTE)BDY(pair))); + value = (QUOTE)(BDY(NEXT(pair))); + ASSPV(pv,value); + } +} + +/* +/* consistency check and merge + */ + +int merge_matching_node(NODE n,NODE a,NODE *rp) +{ + NODE ta,ba,tn,bn; + QUOTE pa,va,pn,vn; + + if ( !n ) { + *rp = a; + return 1; + } + for ( ta = a; ta; ta = NEXT(ta) ) { + ba = BDY((LIST)BDY(ta)); + if ( !ba ) continue; + pa = (QUOTE)BDY(ba); va = (QUOTE)BDY(NEXT(ba)); + for ( tn = n; tn; tn = NEXT(tn) ) { + bn = BDY((LIST)BDY(tn)); + if ( !bn ) continue; + pn = (QUOTE)BDY(bn); vn = (QUOTE)BDY(NEXT(bn)); + if ( !compquote(CO,pa,pn) ) { + if ( !compquote(CO,va,vn) ) break; + else return 0; + } + } + if ( !tn ) { + MKNODE(tn,(pointer)BDY(ta),n); + n = tn; + } + } + *rp = n; + return 1; +} + +int quote_unify_node(NODE f,NODE pat,NODE *rp) { + NODE r,a,tf,tp,r1; + int ret; + + if ( length(f) != length(pat) ) return 0; + r = 0; + for ( tf = f, tp = pat; tf; tf = NEXT(tf), tp = NEXT(tp) ) { + ret = quote_unify((Obj)BDY(tf),(Obj)BDY(tp),&a); + if ( !ret ) return 0; + ret = merge_matching_node(r,a,&r1); + if ( !ret ) return 0; + else r = r1; + } + *rp = r; + return 1; +} + +void get_quote_id_arg(QUOTE f,int *id,NODE *r) +{ + LIST fa; + NODE arg,fab; + + arg = mknode(1,f); Pquote_to_funargs(arg,&fa); fab = BDY((LIST)fa); + *id = QTOS((Q)BDY(fab)); *r = NEXT(fab); +} + +/* *rp : [[quote(A),quote(1)],...] */ + +int quote_unify(Obj f, Obj pat, NODE *rp) +{ + NODE tf,tp,head,body; + NODE parg,farg,r; + LIST fa,l; + int pid,id; + FUNC ff,pf; + int ret; + QUOTE q; + FNODE g; + + if ( OID(pat) == O_LIST ) { + if ( OID(f) == O_LIST ) + return quote_unify_node(BDY((LIST)f),BDY((LIST)pat),rp); + else + return 0; + } else if ( OID(pat) == O_QUOTE ) { + if ( OID(f) != O_QUOTE ) return 0; + get_quote_id_arg((QUOTE)pat,&pid,&parg); + get_quote_id_arg((QUOTE)f,&id,&farg); + switch ( pid ) { + case I_FORMULA: + if ( compquote(CO,f,pat) ) + return 0; + else { + *rp = 0; + return 1; + } + break; + case I_LIST: + return quote_unify_node(BDY((LIST)BDY(farg)), + BDY((LIST)BDY(parg)),rp); + case I_PVAR: + /* [[pat,f]] */ + r = mknode(2,pat,f); MKLIST(l,r); + *rp = mknode(1,l); + return 1; + case I_IFUNC: + /* F(X,Y,...) = ... */ + if ( id == I_FUNC ) { + r = mknode(2,BDY(parg),BDY(farg)); MKLIST(l,r); + head = mknode(1,l); + ret = quote_unify(BDY(NEXT(farg)), + BDY(NEXT(parg)),&body); + if ( !ret ) return 0; + else return merge_matching_node(head,body,rp); + } else + return 0; + + case I_NARYOP: + case I_BOP: + /* X+Y = ... */ + if ( compqa(CO,BDY(farg),BDY(parg)) ) return 0; + + /* XXX converting to I_BOP */ + if ( ((FNODE)BDY((QUOTE)pat))->id == I_NARYOP ) { + g = quote_to_bin(BDY((QUOTE)pat),1); + MKQUOTE(q,g); + get_quote_id_arg((QUOTE)q,&pid,&parg); + } + if ( ((FNODE)BDY((QUOTE)f))->id == I_NARYOP ) { + g = quote_to_bin(BDY((QUOTE)f),1); + MKQUOTE(q,g); + get_quote_id_arg((QUOTE)q,&id,&farg); + } + return quote_unify_node(NEXT(farg),NEXT(parg),rp); + break; + + case I_FUNC: + /* f(...) = ... */ + if ( compqa(CO,BDY(farg),BDY(parg)) ) return 0; + return quote_unify_node(NEXT(farg),NEXT(parg),rp); + break; + default: + if ( pid == id ) + return quote_unify_node(farg,parg,rp); + else + return 0; + } + } +} + void Pquotetotex(NODE arg,STRING *rp) { TB tb; @@ -842,6 +1055,24 @@ char *symbol_name(char *name) return 0; } +void Pget_function_name(NODE arg,STRING *rp) +{ + QUOTEARG qa; + ARF f; + char *opname; + + qa = (QUOTEARG)BDY(arg); + if ( !qa || OID(qa) != O_QUOTEARG || qa->type != A_arf ) + *rp = 0; + else { + f = (ARF)BDY(qa); + opname = f->name; + MKSTR(*rp,opname); + } +} + +FNODE strip_paren(FNODE); + void fnodetotex_tb(FNODE f,TB tb) { NODE n,t,t0; @@ -914,7 +1145,7 @@ void fnodetotex_tb(FNODE f,TB tb) } else if ( !strcmp(opname,"^") ) { fnodetotex_tb((FNODE)FA1(f),tb); write_tb("^{",tb); - fnodetotex_tb((FNODE)FA2(f),tb); + fnodetotex_tb(strip_paren((FNODE)FA2(f)),tb); write_tb("} ",tb); } else if ( !strcmp(opname,"%") ) { fnodetotex_tb((FNODE)FA1(f),tb); @@ -1200,6 +1431,42 @@ char *objtostr(Obj obj) return r; } +void Psprintf(NODE arg,STRING *rp) +{ + STRING string; + char *s,*t,*r; + int argc,n,len; + NODE node; + + string = (STRING)ARG0(arg); + asir_assert(string,O_STR,"sprintf"); + s = BDY(string); + for(n = 0, t = s; *t; t++) { + if (*t=='%' && *(t+1)=='a') { + n++; + } + } + for(node = NEXT(arg), argc = 0, len = strlen(s); node; node = NEXT(node), argc++) { + len += estimate_length(CO,BDY(node)); + } + if (argc < n) { + error("sprintf: invalid argument"); + } + r = (char *)MALLOC_ATOMIC(len); + for(node = NEXT(arg), t = r; *s; s++) { + if (*s=='%' && *(s+1)=='a') { + strcpy(t,objtostr(BDY(node))); + node = NEXT(node); + t = strchr(t,0); + s++; + }else { + *t++ = *s; + } + } + *t = 0; + MKSTR(*rp,r); +} + void fnodenodetotex_tb(NODE n,TB tb) { for ( ; n; n = NEXT(n) ) { @@ -1298,6 +1565,18 @@ void Pflatten_quote(NODE arg,Obj *rp) MKQUOTE(q,f); *rp = (Obj)q; } +} + +void Pget_quote_id(NODE arg,Q *rp) +{ + FNODE f; + QUOTE q; + + q = (QUOTE)ARG0(arg); + if ( !q || OID(q) != O_QUOTE ) + error("get_quote_id : invalid argument"); + f = BDY(q); + STOQ((int)f->id,*rp); } void Pquote_to_funargs(NODE arg,LIST *rp)