=================================================================== RCS file: /home/cvs/OpenXM_contrib2/asir2000/parse/eval.c,v retrieving revision 1.10 retrieving revision 1.30 diff -u -p -r1.10 -r1.30 --- OpenXM_contrib2/asir2000/parse/eval.c 2001/08/21 01:39:39 1.10 +++ OpenXM_contrib2/asir2000/parse/eval.c 2003/11/08 01:12:03 1.30 @@ -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/parse/eval.c,v 1.9 2001/08/20 09:50:34 noro Exp $ + * $OpenXM: OpenXM_contrib2/asir2000/parse/eval.c,v 1.29 2003/05/30 00:47:24 noro Exp $ */ #include #include "ca.h" @@ -54,22 +54,18 @@ #include "parse.h" #include #include -#if PARI +#if defined(PARI) #include "genpari.h" #endif -extern jmp_buf timer_env; +extern JMP_BUF timer_env; int f_break,f_return,f_continue; int evalstatline; int recv_intr; +int show_crossref; -pointer bevalf(), evalmapf(), evall(); -pointer eval_rec_mapf(), beval_rec_mapf(); -Obj getopt_from_cpvs(); - -pointer eval(f) -FNODE f; +pointer eval(FNODE f) { LIST t; STRING str; @@ -78,12 +74,16 @@ FNODE f; NODE tn,ind; R u; DP dp; - int pv,c; + unsigned int pv; + int c; FNODE f1; UP2 up2; UP up; + UM um; + Obj obj; GF2N gf2n; GFPN gfpn; + GFSN gfsn; #if defined(VISUAL) if ( recv_intr ) { @@ -103,6 +103,11 @@ FNODE f; case I_PAREN: val = eval((FNODE)(FA0(f))); break; + case I_MINUS: + a1 = eval((FNODE)(FA0(f))); + arf_chsgn((Obj)a1,&obj); + val = (pointer)obj; + break; case I_BOP: a1 = eval((FNODE)FA1(f)); a2 = eval((FNODE)FA2(f)); (*((ARF)FA0(f))->fp)(CO,a1,a2,&val); @@ -179,7 +184,7 @@ FNODE f; expired = (Obj)eval((FNODE)FA2(f)); set_timer(interval); savepvs(); - if ( !setjmp(timer_env) ) + if ( !SETJMP(timer_env) ) val = eval((FNODE)FA1(f)); else { val = (pointer)expired; @@ -192,7 +197,7 @@ FNODE f; case I_PRESELF: f1 = (FNODE)FA1(f); if ( ID(f1) == I_PVAR ) { - pv = (int)FA0(f1); ind = (NODE)FA1(f1); GETPV(pv,a); + pv = (unsigned int)FA0(f1); ind = (NODE)FA1(f1); GETPV(pv,a); if ( !ind ) { (*((ARF)FA0(f))->fp)(CO,a,ONE,&val); ASSPV(pv,val); } else if ( a ) { @@ -205,7 +210,7 @@ FNODE f; case I_POSTSELF: f1 = (FNODE)FA1(f); if ( ID(f1) == I_PVAR ) { - pv = (int)FA0(f1); ind = (NODE)FA1(f1); GETPV(pv,val); + pv = (unsigned int)FA0(f1); ind = (NODE)FA1(f1); GETPV(pv,val); if ( !ind ) { (*((ARF)FA0(f))->fp)(CO,val,ONE,&u); ASSPV(pv,u); } else if ( val ) { @@ -217,7 +222,7 @@ FNODE f; error("-- : not implemented yet"); break; case I_PVAR: - pv = (int)FA0(f); ind = (NODE)FA1(f); GETPV(pv,a); + pv = (unsigned int)FA0(f); ind = (NODE)FA1(f); GETPV(pv,a); if ( !ind ) val = a; else { @@ -227,7 +232,7 @@ FNODE f; case I_ASSPVAR: f1 = (FNODE)FA0(f); if ( ID(f1) == I_PVAR ) { - pv = (int)FA0(f1); ind = (NODE)FA1(f1); + pv = (unsigned int)FA0(f1); ind = (NODE)FA1(f1); if ( !ind ) { val = eval((FNODE)FA1(f)); ASSPV(pv,val); } else { @@ -244,6 +249,8 @@ FNODE f; a = eval((FNODE)FA0(f1)); ind = (NODE)FA1(f1); evalnodebody(ind,&tn); putarray(a,tn,val = eval((FNODE)FA1(f))); + } else { + error("eval : invalid assignment"); } break; case I_ANS: @@ -259,12 +266,20 @@ FNODE f; break; case I_GFPNGEN: up = UPALLOC(1); - up->d=1; - up->c[0] = 0; - up->c[1] = (Num)ONELM; + DEG(up)=1; + COEF(up)[0] = 0; + COEF(up)[1] = (Num)ONELM; MKGFPN(up,gfpn); val = (pointer)gfpn; break; + case I_GFSNGEN: + um = UMALLOC(1); + DEG(um) = 1; + COEF(um)[0] = 0; + COEF(um)[1] = _onesf(); + MKGFSN(um,gfsn); + val = (pointer)gfsn; + break; case I_STR: MKSTR(str,FA0(f)); val = (pointer)str; break; case I_FORMULA: @@ -316,8 +331,7 @@ FNODE f; return ( val ); } -pointer evalstat(f) -SNODE f; +pointer evalstat(SNODE f) { pointer val = 0,t,s,s1; P u; @@ -338,13 +352,13 @@ SNODE f; case S_BP: if ( !nextbp && (!FA1(f) || eval((FNODE)FA1(f))) ) { if ( (FNODE)FA2(f) ) { -#if PARI +#if defined(PARI) pari_outfile = stderr; #endif asir_out = stderr; printexpr(CO,eval((FNODE)FA2(f))); putc('\n',asir_out); fflush(asir_out); -#if PARI +#if defined(PARI) pari_outfile = stdout; #endif asir_out = stdout; @@ -422,8 +436,7 @@ SNODE f; return ( val ); } -pointer evalnode(node) -NODE node; +pointer evalnode(NODE node) { NODE tn; pointer val; @@ -437,17 +450,17 @@ NODE node; extern FUNC cur_binf; extern NODE PVSS; -pointer evalf(f,a,opt) -FUNC f; -FNODE a; -FNODE opt; +pointer evalf(FUNC f,FNODE a,FNODE opt) { LIST args; + OPTLIST optlist; pointer val; int i,n,level; - NODE tn,sn,opts; - VS pvs; + NODE tn,sn,opts,opt1,dmy; + VS pvs,prev_mpvs; char errbuf[BUFSIZ]; + static unsigned int stack_size; + static void *stack_base; if ( f->id == A_UNDEF ) { sprintf(errbuf,"evalf : %s undefined",NAME(f)); @@ -462,11 +475,30 @@ FNODE opt; } switch ( f->id ) { case A_BIN: + if ( opt ) { + opts = BDY((LIST)eval(opt)); + /* opts = ["opt1",arg1],... */ + opt1 = BDY((LIST)BDY(opts)); + if ( !strcmp(BDY((STRING)BDY(opt1)),"option_list") ) { + /* + * the special option specification: + * option_list=[["o1","a1"],...] + */ + asir_assert(BDY(NEXT(opt1)),O_LIST,"evalf"); + opts = BDY((LIST)BDY(NEXT(opt1))); + } + } else + opts = 0; if ( !n ) { cur_binf = f; (*f->f.binf)(&val); } else { args = (LIST)eval(a); + if ( opts ) { + NEWOPTLIST(optlist); + BDY(optlist) = opts; + appendtonode(BDY(args),(pointer)optlist,&dmy); + } cur_binf = f; (*f->f.binf)(args?BDY(args):0,&val); } @@ -479,10 +511,32 @@ FNODE opt; cur_binf = 0; break; case A_USR: + /* stack check */ +#if !defined(VISUAL) && !defined(__CYGWIN__) + if ( !stack_size ) { + struct rlimit rl; + getrlimit(RLIMIT_STACK,&rl); + stack_size = rl.rlim_cur; + } + if ( !stack_base ) + stack_base = (void *)GC_get_stack_base(); + if ( (stack_base - (void *)&args) +0x100000 > stack_size ) + error("stack overflow"); +#endif args = (LIST)eval(a); - if ( opt ) + if ( opt ) { opts = BDY((LIST)eval(opt)); - else + /* opts = ["opt1",arg1],... */ + opt1 = BDY((LIST)BDY(opts)); + if ( !strcmp(BDY((STRING)BDY(opt1)),"option_list") ) { + /* + * the special option specification: + * option_list=[["o1","a1"],...] + */ + asir_assert(BDY(NEXT(opt1)),O_LIST,"evalf"); + opts = BDY((LIST)BDY(NEXT(opt1))); + } + } else opts = 0; pvs = f->f.usrf->pvs; if ( PVSS ) { @@ -505,7 +559,13 @@ FNODE opt; for ( tn = f->f.usrf->args, sn = BDY(args); sn; tn = NEXT(tn), sn = NEXT(sn) ) ASSPV((int)FA0((FNODE)BDY(tn)),BDY(sn)); - val = evalstat((SNODE)BDY(f->f.usrf)); + if ( f->f.usrf->module ) { + prev_mpvs = MPVS; + MPVS = f->f.usrf->module->pvs; + val = evalstat((SNODE)BDY(f->f.usrf)); + MPVS = prev_mpvs; + } else + val = evalstat((SNODE)BDY(f->f.usrf)); f_return = f_break = f_continue = 0; poppvs(); break; case A_PURE: @@ -520,9 +580,7 @@ FNODE opt; return val; } -pointer evalmapf(f,a) -FUNC f; -FNODE a; +pointer evalmapf(FUNC f,FNODE a) { LIST args; NODE node,rest,t,n,r,r0; @@ -572,9 +630,7 @@ FNODE a; return val; } -pointer eval_rec_mapf(f,a) -FUNC f; -FNODE a; +pointer eval_rec_mapf(FUNC f,FNODE a) { LIST args; @@ -582,11 +638,8 @@ FNODE a; return beval_rec_mapf(f,BDY(args)); } -pointer beval_rec_mapf(f,node) -FUNC f; -NODE node; +pointer beval_rec_mapf(FUNC f,NODE node) { - LIST args; NODE rest,t,n,r,r0; Obj head; VECT v,rv; @@ -635,16 +688,12 @@ NODE node; return val; } -pointer bevalf(f,a) -FUNC f; -NODE a; +pointer bevalf(FUNC f,NODE a) { pointer val; int i,n; NODE tn,sn; VS pvs; - struct oLIST list; - struct oFNODE fnode; char errbuf[BUFSIZ]; if ( f->id == A_UNDEF ) { @@ -706,8 +755,7 @@ NODE a; return val; } -pointer evalif(f,a) -FNODE f,a; +pointer evalif(FNODE f,FNODE a) { Obj g; @@ -716,19 +764,18 @@ FNODE f,a; return evalf((FUNC)VR((P)g)->priv,a,0); else { error("invalid function pointer"); + /* NOTREACHED */ + return (pointer)-1; } } -pointer evalpf(pf,args) -PF pf; -NODE args; +pointer evalpf(PF pf,NODE args) { Obj s,s1; int i; NODE node; PFINS ins; PFAD ad; - char errbuf[BUFSIZ]; if ( !pf->body ) { ins = (PFINS)CALLOC(1,sizeof(PF)+pf->argc*sizeof(struct oPFAD)); @@ -747,9 +794,7 @@ NODE args; return (pointer)s; } -void evalnodebody(sn,dnp) -NODE sn; -NODE *dnp; +void evalnodebody(NODE sn,NODE *dnp) { NODE n,n0,tn; int line; @@ -767,11 +812,66 @@ NODE *dnp; NEXT(n) = 0; *dnp = n0; } -void searchf(fn,name,r) -NODE fn; -char *name; -FUNC *r; +MODULE searchmodule(char *name) { + MODULE mod; + NODE m; + + for ( m = MODULE_LIST; m; m = NEXT(m) ) { + mod = (MODULE)BDY(m); + if ( !strcmp(mod->name,name) ) + return mod; + } + return 0; +} +/* + * xxx.yyy() is searched in the flist + * of the module xxx. + * yyy() is searched in the global flist. + */ + +void searchuf(char *name,FUNC *r) +{ + MODULE mod; + char *name0,*dot; + + if ( dot = strchr(name,'.') ) { + name0 = (char *)ALLOCA(strlen(name)+1); + strcpy(name0,name); + dot = strchr(name0,'.'); + *dot = 0; + mod = searchmodule(name0); + if ( mod ) + searchf(mod->usrf_list,dot+1,r); + } else + searchf(usrf,name,r); +} + +void gen_searchf(char *name,FUNC *r) +{ + FUNC val = 0; + int global = 0; + if ( *name == ':' ) { + global = 1; + name += 2; + } + if ( CUR_MODULE && !global ) + searchf(CUR_MODULE->usrf_list,name,&val); + if ( !val ) + searchf(sysf,name,&val); + if ( !val ) + searchf(ubinf,name,&val); + if ( !val ) + searchpf(name,&val); + if ( !val ) + searchuf(name,&val); + if ( !val ) + appenduf(name,&val); + *r = val; +} + +void searchf(NODE fn,char *name,FUNC *r) +{ NODE tn; for ( tn = fn; @@ -783,46 +883,96 @@ FUNC *r; *r = 0; } -void appenduf(name,r) -char *name; -FUNC *r; +MODULE mkmodule(char *); + +void appenduf(char *name,FUNC *r) { NODE tn; FUNC f; + int len; + MODULE mod; + char *modname,*fname,*dot; f=(FUNC)MALLOC(sizeof(struct oFUNC)); - f->name = name; f->id = A_UNDEF; f->argc = 0; f->f.binf = 0; - MKNODE(tn,f,usrf); usrf = tn; + f->id = A_UNDEF; f->argc = 0; f->f.binf = 0; + if ( dot = strchr(name,'.') ) { + /* undefined function in a module */ + len = dot-name; + modname = (char *)MALLOC_ATOMIC(len+1); + strncpy(modname,name,len); modname[len] = 0; + fname = (char *)MALLOC_ATOMIC(strlen(name)-len+1); + strcpy(fname,dot+1); + f->name = fname; + f->fullname = name; + mod = searchmodule(modname); + if ( !mod ) + mod = mkmodule(modname); + MKNODE(tn,f,mod->usrf_list); mod->usrf_list = tn; + } else { + f->name = name; + f->fullname = name; + MKNODE(tn,f,usrf); usrf = tn; + } *r = f; } -void mkparif(name,r) -char *name; -FUNC *r; +void appenduf_local(char *name,FUNC *r) { + NODE tn; FUNC f; + MODULE mod; + for ( tn = CUR_MODULE->usrf_list; tn; tn = NEXT(tn) ) + if ( !strcmp(((FUNC)BDY(tn))->name,name) ) + break; + if ( tn ) + return; + + f=(FUNC)MALLOC(sizeof(struct oFUNC)); + f->id = A_UNDEF; f->argc = 0; f->f.binf = 0; + f->name = name; + f->fullname = + (char *)MALLOC_ATOMIC(strlen(CUR_MODULE->name)+strlen(name)+1); + sprintf(f->fullname,"%s.%s",CUR_MODULE->name,name); + MKNODE(tn,f,CUR_MODULE->usrf_list); CUR_MODULE->usrf_list = tn; + *r = f; +} + +void appenduflist(NODE n) +{ + NODE tn; + FUNC f; + + for ( tn = n; tn; tn = NEXT(tn) ) + appenduf_local((char *)BDY(tn),&f); +} + +void mkparif(char *name,FUNC *r) +{ + FUNC f; + *r = f =(FUNC)MALLOC(sizeof(struct oFUNC)); f->name = name; f->id = A_PARI; f->argc = 0; f->f.binf = 0; + f->fullname = name; } -void mkuf(name,fname,args,body,startl,endl,desc) -char *name,*fname; -NODE args; -SNODE body; -int startl,endl; -char *desc; +void mkuf(char *name,char *fname,NODE args,SNODE body,int startl,int endl,char *desc,MODULE module) { FUNC f; USRF t; - NODE sn,tn; + NODE usrf_list,sn,tn; FNODE fn; + char *longname; int argc; - searchf(sysf,name,&f); - if ( f ) { - fprintf(stderr,"def : builtin function %s() cannot be redefined.\n",name); - CPVS = GPVS; return; + if ( *name == ':' ) + name += 2; + if ( !module ) { + searchf(sysf,name,&f); + if ( f ) { + fprintf(stderr,"def : builtin function %s() cannot be redefined.\n",name); + CPVS = GPVS; return; + } } for ( argc = 0, sn = args; sn; argc++, sn = NEXT(sn) ) { fn = (FNODE)BDY(sn); @@ -831,24 +981,37 @@ char *desc; CPVS = GPVS; return; } } - for ( sn = usrf; sn && strcmp(NAME((FUNC)BDY(sn)),name); sn = NEXT(sn) ); + usrf_list = module ? module->usrf_list : usrf; + for ( sn = usrf_list; sn && strcmp(NAME((FUNC)BDY(sn)),name); sn = NEXT(sn) ); if ( sn ) f = (FUNC)BDY(sn); else { f=(FUNC)MALLOC(sizeof(struct oFUNC)); f->name = name; - MKNODE(tn,f,usrf); usrf = tn; + MKNODE(tn,f,usrf_list); usrf_list = tn; + if ( module ) { + f->fullname = + (char *)MALLOC_ATOMIC(strlen(f->name)+strlen(module->name)+1); + sprintf(f->fullname,"%s.%s",module->name,f->name); + module->usrf_list = usrf_list; + } else { + f->fullname = f->name; + usrf = usrf_list; + } } - if ( Verbose && f->id != A_UNDEF ) - fprintf(stderr,"Warning : %s() redefined.\n",name); -/* else - fprintf(stderr,"%s() defined.\n",name); */ + if ( Verbose && f->id != A_UNDEF ) { + if ( module ) + fprintf(stderr,"Warning : %s.%s() redefined.\n",module->name,name); + else + fprintf(stderr,"Warning : %s() redefined.\n",name); + } t=(USRF)MALLOC(sizeof(struct oUSRF)); t->args=args; BDY(t)=body; t->pvs = CPVS; t->fname = fname; - t->startl = startl; t->endl = endl; t->vol = asir_infile->vol; + t->startl = startl; t->endl = endl; t->module = module; t->desc = desc; f->id = A_USR; f->argc = argc; f->f.usrf = t; CPVS = GPVS; + CUR_FUNC = 0; clearbp(f); } @@ -858,19 +1021,65 @@ char *desc; CVS->opt = BDY([[key,value],[key,value],...]) */ -Obj getopt_from_cpvs(key) -char *key; +Obj getopt_from_cpvs(char *key) { NODE opts,opt; - Obj value; + LIST r; extern Obj VOIDobj; opts = CPVS->opt; - for ( ; opts; opts = NEXT(opts) ) { - opt = BDY((LIST)BDY(opts)); - if ( !strcmp(key,BDY((STRING)BDY(opt))) ) - return (Obj)BDY(NEXT(opt)); + if ( !key ) { + MKLIST(r,opts); + return (Obj)r; + } else { + for ( ; opts; opts = NEXT(opts) ) { + asir_assert(BDY(opts),O_LIST,"getopt_from_cvps"); + opt = BDY((LIST)BDY(opts)); + if ( !strcmp(key,BDY((STRING)BDY(opt))) ) + return (Obj)BDY(NEXT(opt)); + } + return VOIDobj; } - return VOIDobj; +} + +MODULE mkmodule(char *name) +{ + MODULE mod; + NODE m; + int len; + VS mpvs; + + for ( m = MODULE_LIST; m; m = NEXT(m) ) { + mod = (MODULE)m->body; + if ( !strcmp(mod->name,name) ) + break; + } + if ( m ) + return mod; + else { + mod = (MODULE)MALLOC(sizeof(struct oMODULE)); + len = strlen(name); + mod->name = (char *)MALLOC_ATOMIC(len+1); + strcpy(mod->name,name); + mod->pvs = mpvs = (VS)MALLOC(sizeof(struct oVS)); + reallocarray((char **)&mpvs->va,(int *)&mpvs->asize, + (int *)&mpvs->n,(int)sizeof(struct oPV)); + mod->usrf_list = 0; + MKNODE(m,mod,MODULE_LIST); + MODULE_LIST = m; + return mod; + } +} + +void print_crossref(FUNC f) +{ + FUNC r; + if ( show_crossref && CUR_FUNC ) { + searchuf(f->fullname,&r); + if (r != NULL) { + fprintf(asir_out,"%s() at line %d in %s()\n", + f->fullname, asir_infile->ln, CUR_FUNC); + } + } }