Annotation of OpenXM_contrib2/asir2018/parse/eval.c, Revision 1.1
1.1 ! 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
! 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
! 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: *
! 48: * $OpenXM$
! 49: */
! 50: #include <ctype.h>
! 51: #include "ca.h"
! 52: #include "al.h"
! 53: #include "base.h"
! 54: #include "parse.h"
! 55: #if defined(GC7)
! 56: #include "gc.h"
! 57: #endif
! 58: #include <sys/types.h>
! 59: #include <sys/stat.h>
! 60:
! 61: extern JMP_BUF timer_env;
! 62: extern FUNC cur_binf;
! 63: extern NODE PVSS;
! 64: extern int evalef;
! 65:
! 66: int f_break,f_return,f_continue;
! 67: int evalstatline;
! 68: int show_crossref;
! 69: int at_root;
! 70: void gen_searchf_searchonly(char *name,FUNC *r,int global);
! 71: LIST eval_arg(FNODE a,unsigned int quote);
! 72:
! 73: pointer eval(FNODE f)
! 74: {
! 75: LIST t;
! 76: STRING str;
! 77: pointer val = 0;
! 78: pointer a,a1,a2;
! 79: NODE tn,tn1,ind,match;
! 80: R u;
! 81: DP dp;
! 82: DPM dpm;
! 83: unsigned int pv;
! 84: int c,ret;
! 85: Obj pos;
! 86: FNODE f1;
! 87: UP2 up2;
! 88: UP up;
! 89: UM um;
! 90: Obj obj;
! 91: GF2N gf2n;
! 92: GFPN gfpn;
! 93: GFSN gfsn;
! 94: RANGE range;
! 95: QUOTE expr,pattern;
! 96: Q q;
! 97:
! 98: #if defined(VISUAL) || defined(__MINGW32__)
! 99: check_intr();
! 100: #endif
! 101: if ( !f )
! 102: return ( 0 );
! 103: switch ( f->id ) {
! 104: case I_PAREN:
! 105: val = eval((FNODE)(FA0(f)));
! 106: break;
! 107: case I_MINUS:
! 108: a1 = eval((FNODE)(FA0(f)));
! 109: arf_chsgn((Obj)a1,&obj);
! 110: val = (pointer)obj;
! 111: break;
! 112: case I_BOP:
! 113: a1 = eval((FNODE)FA1(f)); a2 = eval((FNODE)FA2(f));
! 114: (*((ARF)FA0(f))->fp)(CO,a1,a2,&val);
! 115: break;
! 116: case I_NARYOP:
! 117: tn = (NODE)FA1(f);
! 118: a = eval((FNODE)BDY(tn));
! 119: for ( tn = NEXT(tn); tn; tn = NEXT(tn) ) {
! 120: a1 = eval((FNODE)BDY(tn));
! 121: (*((ARF)FA0(f))->fp)(CO,a,a1,&a2);
! 122: a = a2;
! 123: }
! 124: val = a;
! 125: break;
! 126: case I_COP:
! 127: a1 = eval((FNODE)FA1(f)); a2 = eval((FNODE)FA2(f));
! 128: c = arf_comp(CO,a1,a2);
! 129: switch ( (cid)FA0(f) ) {
! 130: case C_EQ:
! 131: c = (c == 0); break;
! 132: case C_NE:
! 133: c = (c != 0); break;
! 134: case C_GT:
! 135: c = (c > 0); break;
! 136: case C_LT:
! 137: c = (c < 0); break;
! 138: case C_GE:
! 139: c = (c >= 0); break;
! 140: case C_LE:
! 141: c = (c <= 0); break;
! 142: default:
! 143: c = 0; break;
! 144: }
! 145: if ( c )
! 146: val = (pointer)ONE;
! 147: break;
! 148: case I_AND:
! 149: if ( eval((FNODE)FA0(f)) && eval((FNODE)FA1(f)) )
! 150: val = (pointer)ONE;
! 151: break;
! 152: case I_OR:
! 153: if ( eval((FNODE)FA0(f)) || eval((FNODE)FA1(f)) )
! 154: val = (pointer)ONE;
! 155: break;
! 156: case I_NOT:
! 157: if ( eval((FNODE)FA0(f)) )
! 158: val = 0;
! 159: else
! 160: val = (pointer)ONE;
! 161: break;
! 162: case I_LOP:
! 163: a1 = eval((FNODE)FA1(f)); a2 = eval((FNODE)FA2(f));
! 164: val = evall((lid)FA0(f),a1,a2);
! 165: break;
! 166: case I_CE:
! 167: if ( eval((FNODE)FA0(f)) )
! 168: val = eval((FNODE)FA1(f));
! 169: else
! 170: val = eval((FNODE)FA2(f));
! 171: break;
! 172: case I_EV:
! 173: evalnodebody((NODE)FA0(f),&tn); nodetod(tn,&dp); val = (pointer)dp;
! 174: break;
! 175: case I_EVM:
! 176: evalnodebody((NODE)FA0(f),&tn); pos = (Obj)eval((FNODE)FA1(f)); nodetodpm(tn,pos,&dpm); val = (pointer)dpm;
! 177: break;
! 178: case I_FUNC:
! 179: val = evalf((FUNC)FA0(f),(FNODE)FA1(f),0); break;
! 180: case I_FUNC_OPT:
! 181: val = evalf((FUNC)FA0(f),(FNODE)FA1(f),(FNODE)FA2(f)); break;
! 182: case I_FUNC_QARG:
! 183: tn = BDY(eval_arg((FNODE)FA1(f),(unsigned int)0xffffffff));
! 184: val = bevalf((FUNC)FA0(f),tn); break;
! 185: case I_PFDERIV:
! 186: val = evalf_deriv((FUNC)FA0(f),(FNODE)FA1(f),(FNODE)FA2(f)); break;
! 187: case I_MAP:
! 188: val = evalmapf((FUNC)FA0(f),(FNODE)FA1(f)); break;
! 189: case I_RECMAP:
! 190: val = eval_rec_mapf((FUNC)FA0(f),(FNODE)FA1(f)); break;
! 191: case I_IFUNC:
! 192: val = evalif((FNODE)FA0(f),(FNODE)FA1(f),(FNODE)FA2(f)); break;
! 193: #if !defined(VISUAL) && !defined(__MINGW32__)
! 194: case I_TIMER:
! 195: {
! 196: int interval;
! 197: Obj expired;
! 198:
! 199: interval = QTOS((Q)eval((FNODE)FA0(f)));
! 200: expired = (Obj)eval((FNODE)FA2(f));
! 201: set_timer(interval);
! 202: savepvs();
! 203: if ( !SETJMP(timer_env) )
! 204: val = eval((FNODE)FA1(f));
! 205: else {
! 206: val = (pointer)expired;
! 207: restorepvs();
! 208: }
! 209: reset_timer();
! 210: }
! 211: break;
! 212: #endif
! 213: case I_PRESELF:
! 214: f1 = (FNODE)FA1(f);
! 215: if ( ID(f1) == I_PVAR ) {
! 216: pv = (unsigned long)FA0(f1); ind = (NODE)FA1(f1); GETPV(pv,a);
! 217: if ( !ind ) {
! 218: (*((ARF)FA0(f))->fp)(CO,a,ONE,&val); ASSPV(pv,val);
! 219: } else if ( a ) {
! 220: evalnodebody(ind,&tn); getarray(a,tn,(pointer *)&u);
! 221: (*((ARF)FA0(f))->fp)(CO,u,ONE,&val); putarray(a,tn,val);
! 222: }
! 223: } else
! 224: error("++ : not implemented yet");
! 225: break;
! 226: case I_POSTSELF:
! 227: f1 = (FNODE)FA1(f);
! 228: if ( ID(f1) == I_PVAR ) {
! 229: pv = (unsigned long)FA0(f1); ind = (NODE)FA1(f1); GETPV(pv,val);
! 230: if ( !ind ) {
! 231: (*((ARF)FA0(f))->fp)(CO,val,ONE,&u); ASSPV(pv,u);
! 232: } else if ( val ) {
! 233: evalnodebody(ind,&tn); getarray(val,tn,&a);
! 234: (*((ARF)FA0(f))->fp)(CO,a,ONE,&u); putarray(val,tn,(pointer)u);
! 235: val = a;
! 236: }
! 237: } else
! 238: error("-- : not implemented yet");
! 239: break;
! 240: case I_PVAR:
! 241: pv = (unsigned long)FA0(f);
! 242: ind = (NODE)FA1(f);
! 243: GETPV(pv,a);
! 244: if ( !ind )
! 245: val = a;
! 246: else {
! 247: evalnodebody(ind,&tn); getarray(a,tn,&val);
! 248: }
! 249: break;
! 250: case I_ASSPVAR:
! 251: f1 = (FNODE)FA0(f);
! 252: if ( ID(f1) == I_PVAR ) {
! 253: pv = (unsigned long)FA0(f1); ind = (NODE)FA1(f1);
! 254: if ( !ind ) {
! 255: val = eval((FNODE)FA1(f)); ASSPV(pv,val);
! 256: } else {
! 257: GETPV(pv,a);
! 258: evalnodebody(ind,&tn);
! 259: putarray(a,tn,val = eval((FNODE)FA1(f)));
! 260: }
! 261: } else if ( ID(f1) == I_POINT ) {
! 262: /* f1 <-> FA0(f1)->FA1(f1) */
! 263: a = eval(FA0(f1));
! 264: assign_to_member(a,(char *)FA1(f1),val = eval((FNODE)FA1(f)));
! 265: } else if ( ID(f1) == I_INDEX ) {
! 266: /* f1 <-> FA0(f1)[FA1(f1)] */
! 267: a = eval((FNODE)FA0(f1)); ind = (NODE)FA1(f1);
! 268: evalnodebody(ind,&tn);
! 269: putarray(a,tn,val = eval((FNODE)FA1(f)));
! 270: } else {
! 271: error("eval : invalid assignment");
! 272: }
! 273: break;
! 274: case I_ANS:
! 275: if ( (pv =(long)FA0(f)) < (long)APVS->n )
! 276: val = APVS->va[pv].priv;
! 277: break;
! 278: case I_GF2NGEN:
! 279: NEWUP2(up2,1);
! 280: up2->w=1;
! 281: up2->b[0] = 2; /* @ */
! 282: MKGF2N(up2,gf2n);
! 283: val = (pointer)gf2n;
! 284: break;
! 285: case I_GFPNGEN:
! 286: up = UPALLOC(1);
! 287: DEG(up)=1;
! 288: COEF(up)[0] = 0;
! 289: COEF(up)[1] = (Num)ONELM;
! 290: MKGFPN(up,gfpn);
! 291: val = (pointer)gfpn;
! 292: break;
! 293: case I_GFSNGEN:
! 294: um = UMALLOC(1);
! 295: DEG(um) = 1;
! 296: COEF(um)[0] = 0;
! 297: COEF(um)[1] = _onesf();
! 298: MKGFSN(um,gfsn);
! 299: val = (pointer)gfsn;
! 300: break;
! 301: case I_STR:
! 302: MKSTR(str,FA0(f)); val = (pointer)str; break;
! 303: case I_FORMULA:
! 304: val = FA0(f);
! 305: break;
! 306: case I_LIST:
! 307: evalnodebody((NODE)FA0(f),&tn); MKLIST(t,tn); val = (pointer)t; break;
! 308: case I_CONS:
! 309: evalnodebody((NODE)FA0(f),&tn); a2 = eval(FA1(f));
! 310: if ( !a2 || OID(a2) != O_LIST )
! 311: error("cons : invalid argument");
! 312: for ( tn1 = tn; NEXT(tn1); tn1 = NEXT(tn1) );
! 313: NEXT(tn1) = BDY((LIST)a2);
! 314: MKLIST(t,tn); val = (pointer)t;
! 315: break;
! 316: case I_NEWCOMP:
! 317: newstruct((long)FA0(f),(struct oCOMP **)&val); break;
! 318: case I_CAR:
! 319: if ( !(a = eval((FNODE)FA0(f))) || (OID(a) != O_LIST) )
! 320: val = 0;
! 321: else if ( !BDY((LIST)a) )
! 322: val = a;
! 323: else
! 324: val = (pointer)BDY(BDY((LIST)a));
! 325: break;
! 326: case I_CDR:
! 327: if ( !(a = eval((FNODE)FA0(f))) || (OID(a) != O_LIST) )
! 328: val = 0;
! 329: else if ( !BDY((LIST)a) )
! 330: val = a;
! 331: else {
! 332: MKLIST(t,NEXT(BDY((LIST)a))); val = (pointer)t;
! 333: }
! 334: break;
! 335: case I_INDEX:
! 336: a = eval((FNODE)FA0(f)); ind = (NODE)FA1(f);
! 337: evalnodebody(ind,&tn); getarray(a,tn,&val);
! 338: break;
! 339: case I_OPT:
! 340: MKSTR(str,(char *)FA0(f));
! 341: a = (pointer)eval(FA1(f));
! 342: tn = mknode(2,str,a);
! 343: MKLIST(t,tn); val = (pointer)t;
! 344: break;
! 345: case I_GETOPT:
! 346: val = (pointer)getopt_from_cpvs((char *)FA0(f));
! 347: break;
! 348: case I_POINT:
! 349: a = (pointer)eval(FA0(f));
! 350: val = (pointer)memberofstruct(a,(char *)FA1(f));
! 351: break;
! 352: default:
! 353: error("eval : unknown id");
! 354: break;
! 355: }
! 356: return ( val );
! 357: }
! 358:
! 359: NODE fnode_to_nary_node(NODE);
! 360: NODE fnode_to_bin_node(NODE,int);
! 361:
! 362: FNODE fnode_to_nary(FNODE f)
! 363: {
! 364: FNODE a0,a1,a2;
! 365: NODE n,t,t0;
! 366: pointer val;
! 367: char *op;
! 368:
! 369: if ( !f )
! 370: return f;
! 371: switch ( f->id ) {
! 372: case I_NARYOP:
! 373: n = fnode_to_nary_node((NODE)FA1(f));
! 374: return mkfnode(2,I_NARYOP,FA0(f),n);
! 375:
! 376: case I_BOP:
! 377: a1 = fnode_to_nary((FNODE)FA1(f));
! 378: a2 = fnode_to_nary((FNODE)FA2(f));
! 379: op = ((ARF)FA0(f))->name;
! 380: if ( !strcmp(op,"+") || !strcmp(op,"*") ) {
! 381: if ( a1->id == I_NARYOP && !strcmp(op,((ARF)FA0(a1))->name) ) {
! 382: for ( n = (NODE)FA1(a1); NEXT(n); n = NEXT(n) );
! 383: if ( a2->id == I_NARYOP && !strcmp(op,((ARF)FA0(a2))->name) )
! 384: NEXT(n) = (NODE)FA1(a2);
! 385: else
! 386: MKNODE(NEXT(n),a2,0);
! 387: return a1;
! 388: } else if ( a2->id == I_NARYOP && !strcmp(op,((ARF)FA0(a2))->name) ) {
! 389: MKNODE(t,a1,(NODE)FA1(a2));
! 390: return mkfnode(2,I_NARYOP,FA0(f),t);
! 391: } else {
! 392: t = mknode(2,a1,a2);
! 393: return mkfnode(2,I_NARYOP,FA0(f),t);
! 394: }
! 395: } else
! 396: return mkfnode(3,f->id,FA0(f),a1,a2);
! 397:
! 398: case I_NOT: case I_PAREN: case I_MINUS:
! 399: case I_CAR: case I_CDR:
! 400: a0 = fnode_to_nary((FNODE)FA0(f));
! 401: return mkfnode(1,f->id,a0);
! 402:
! 403: case I_COP: case I_LOP:
! 404: a1 = fnode_to_nary((FNODE)FA1(f));
! 405: a2 = fnode_to_nary((FNODE)FA2(f));
! 406: return mkfnode(3,f->id,FA0(f),a1,a2);
! 407:
! 408: case I_AND: case I_OR:
! 409: a0 = fnode_to_nary((FNODE)FA0(f));
! 410: a1 = fnode_to_nary((FNODE)FA1(f));
! 411: return mkfnode(2,f->id,a0,a1);
! 412:
! 413: /* ternary operators */
! 414: case I_CE:
! 415: a0 = fnode_to_nary((FNODE)FA0(f));
! 416: a1 = fnode_to_nary((FNODE)FA1(f));
! 417: a2 = fnode_to_nary((FNODE)FA2(f));
! 418: return mkfnode(3,f->id,a0,a1,a2);
! 419: break;
! 420:
! 421: /* function */
! 422: case I_FUNC:
! 423: a1 = fnode_to_nary((FNODE)FA1(f));
! 424: return mkfnode(2,f->id,FA0(f),a1);
! 425:
! 426: case I_LIST: case I_EV:
! 427: n = fnode_to_nary_node((NODE)FA0(f));
! 428: return mkfnode(1,f->id,n);
! 429:
! 430: case I_STR: case I_FORMULA: case I_PVAR:
! 431: return f;
! 432:
! 433: default:
! 434: error("fnode_to_nary : not implemented yet");
! 435: }
! 436: }
! 437:
! 438: FNODE fnode_to_bin(FNODE f,int dir)
! 439: {
! 440: FNODE a0,a1,a2;
! 441: NODE n,t;
! 442: pointer val;
! 443: ARF fun;
! 444: int len,i;
! 445: FNODE *arg;
! 446:
! 447: if ( !f )
! 448: return f;
! 449: switch ( f->id ) {
! 450: case I_NARYOP:
! 451: fun = (ARF)FA0(f);
! 452: len = length((NODE)FA1(f));
! 453: if ( len==1 ) return BDY((NODE)(FA1(f)));
! 454:
! 455: arg = (FNODE *)ALLOCA(len*sizeof(FNODE));
! 456: for ( i = 0, t = (NODE)FA1(f); i < len; i++, t = NEXT(t) )
! 457: arg[i] = fnode_to_bin((FNODE)BDY(t),dir);
! 458: if ( dir ) {
! 459: a2 = mkfnode(3,I_BOP,fun,arg[len-2],arg[len-1]);
! 460: for ( i = len-3; i >= 0; i-- )
! 461: a2 = mkfnode(3,I_BOP,fun,arg[i],a2);
! 462: } else {
! 463: a2 = mkfnode(3,I_BOP,fun,arg[0],arg[1]);
! 464: for ( i = 2; i < len; i++ )
! 465: a2 = mkfnode(3,I_BOP,fun,a2,arg[i]);
! 466: }
! 467: return a2;
! 468:
! 469: case I_NOT: case I_PAREN: case I_MINUS:
! 470: case I_CAR: case I_CDR:
! 471: a0 = fnode_to_bin((FNODE)FA0(f),dir);
! 472: return mkfnode(1,f->id,a0);
! 473:
! 474: case I_BOP: case I_COP: case I_LOP:
! 475: a1 = fnode_to_bin((FNODE)FA1(f),dir);
! 476: a2 = fnode_to_bin((FNODE)FA2(f),dir);
! 477: return mkfnode(3,f->id,FA0(f),a1,a2);
! 478:
! 479: case I_AND: case I_OR:
! 480: a0 = fnode_to_bin((FNODE)FA0(f),dir);
! 481: a1 = fnode_to_bin((FNODE)FA1(f),dir);
! 482: return mkfnode(2,f->id,a0,a1);
! 483:
! 484: /* ternary operators */
! 485: case I_CE:
! 486: a0 = fnode_to_bin((FNODE)FA0(f),dir);
! 487: a1 = fnode_to_bin((FNODE)FA1(f),dir);
! 488: a2 = fnode_to_bin((FNODE)FA2(f),dir);
! 489: return mkfnode(3,f->id,a0,a1,a2);
! 490: break;
! 491:
! 492: /* function */
! 493: case I_FUNC:
! 494: a1 = fnode_to_bin((FNODE)FA1(f),dir);
! 495: return mkfnode(2,f->id,FA0(f),a1);
! 496:
! 497: case I_LIST: case I_EV:
! 498: n = fnode_to_bin_node((NODE)FA0(f),dir);
! 499: return mkfnode(1,f->id,n);
! 500:
! 501: case I_STR: case I_FORMULA: case I_PVAR:
! 502: return f;
! 503:
! 504: default:
! 505: error("fnode_to_bin : not implemented yet");
! 506: }
! 507: }
! 508:
! 509: NODE partial_eval_node(NODE n);
! 510: FNODE partial_eval(FNODE f);
! 511:
! 512: FNODE partial_eval(FNODE f)
! 513: {
! 514: FNODE a0,a1,a2;
! 515: NODE n;
! 516: Obj obj;
! 517: QUOTE q;
! 518: pointer val;
! 519: FUNC func;
! 520:
! 521: if ( !f )
! 522: return f;
! 523: switch ( f->id ) {
! 524: case I_NOT: case I_PAREN: case I_MINUS:
! 525: case I_CAR: case I_CDR:
! 526: a0 = partial_eval((FNODE)FA0(f));
! 527: return mkfnode(1,f->id,a0);
! 528:
! 529: case I_BOP: case I_COP: case I_LOP:
! 530: a1 = partial_eval((FNODE)FA1(f));
! 531: a2 = partial_eval((FNODE)FA2(f));
! 532: return mkfnode(3,f->id,FA0(f),a1,a2);
! 533:
! 534: case I_NARYOP:
! 535: n = partial_eval_node((NODE)FA1(f));
! 536: return mkfnode(2,f->id,FA0(f),n);
! 537:
! 538: case I_AND: case I_OR:
! 539: a0 = partial_eval((FNODE)FA0(f));
! 540: a1 = partial_eval((FNODE)FA1(f));
! 541: return mkfnode(2,f->id,a0,a1);
! 542:
! 543: /* ternary operators */
! 544: case I_CE:
! 545: a0 = partial_eval((FNODE)FA0(f));
! 546: a1 = partial_eval((FNODE)FA1(f));
! 547: a2 = partial_eval((FNODE)FA2(f));
! 548: return mkfnode(3,f->id,a0,a1,a2);
! 549: break;
! 550:
! 551: /* XXX : function is evaluated with QUOTE args */
! 552: case I_FUNC:
! 553: a1 = partial_eval((FNODE)FA1(f));
! 554: func = (FUNC)FA0(f);
! 555: if ( func->id == A_UNDEF || func->id != A_USR ) {
! 556: a1 = mkfnode(2,I_FUNC,func,a1);
! 557: return a1;
! 558: } else {
! 559: n = BDY(eval_arg(a1,(unsigned int)0xffffffff));
! 560: obj = bevalf(func,n);
! 561: objtoquote(obj,&q);
! 562: return BDY(q);
! 563: }
! 564: break;
! 565:
! 566: case I_LIST: case I_EV:
! 567: n = partial_eval_node((NODE)FA0(f));
! 568: return mkfnode(1,f->id,n);
! 569:
! 570: case I_STR: case I_FORMULA:
! 571: return f;
! 572:
! 573: /* program variable */
! 574: case I_PVAR:
! 575: val = eval(f);
! 576: if ( val && OID((Obj)val) == O_QUOTE )
! 577: return partial_eval((FNODE)BDY((QUOTE)val));
! 578: else
! 579: return mkfnode(1,I_FORMULA,val);
! 580:
! 581: default:
! 582: error("partial_eval : not implemented yet");
! 583: }
! 584: }
! 585:
! 586: NODE partial_eval_node(NODE n)
! 587: {
! 588: NODE r0,r,t;
! 589:
! 590: for ( r0 = 0, t = n; t; t = NEXT(t) ) {
! 591: NEXTNODE(r0,r);
! 592: BDY(r) = partial_eval((FNODE)BDY(t));
! 593: }
! 594: if ( r0 ) NEXT(r) = 0;
! 595: return r0;
! 596: }
! 597:
! 598: NODE rewrite_fnode_node(NODE n,NODE arg,int qarg);
! 599: FNODE rewrite_fnode(FNODE f,NODE arg,int qarg);
! 600:
! 601: FNODE rewrite_fnode(FNODE f,NODE arg,int qarg)
! 602: {
! 603: FNODE a0,a1,a2,value;
! 604: NODE n,t,pair;
! 605: pointer val;
! 606: int pv,ind;
! 607:
! 608: if ( !f )
! 609: return f;
! 610: switch ( f->id ) {
! 611: case I_NOT: case I_PAREN: case I_MINUS:
! 612: case I_CAR: case I_CDR:
! 613: a0 = rewrite_fnode((FNODE)FA0(f),arg,qarg);
! 614: return mkfnode(1,f->id,a0);
! 615:
! 616: case I_BOP: case I_COP: case I_LOP:
! 617: a1 = rewrite_fnode((FNODE)FA1(f),arg,qarg);
! 618: a2 = rewrite_fnode((FNODE)FA2(f),arg,qarg);
! 619: return mkfnode(3,f->id,FA0(f),a1,a2);
! 620:
! 621: case I_AND: case I_OR:
! 622: a0 = rewrite_fnode((FNODE)FA0(f),arg,qarg);
! 623: a1 = rewrite_fnode((FNODE)FA1(f),arg,qarg);
! 624: return mkfnode(2,f->id,a0,a1);
! 625:
! 626: /* ternary operators */
! 627: case I_CE:
! 628: a0 = rewrite_fnode((FNODE)FA0(f),arg,qarg);
! 629: a1 = rewrite_fnode((FNODE)FA1(f),arg,qarg);
! 630: a2 = rewrite_fnode((FNODE)FA2(f),arg,qarg);
! 631: return mkfnode(3,f->id,a0,a1,a2);
! 632: break;
! 633:
! 634: /* nary operators */
! 635: case I_NARYOP:
! 636: n = rewrite_fnode_node((NODE)FA1(f),arg,qarg);
! 637: return mkfnode(2,f->id,FA0(f),n);
! 638:
! 639: /* and function */
! 640: case I_FUNC:
! 641: a1 = rewrite_fnode((FNODE)FA1(f),arg,qarg);
! 642: return mkfnode(2,qarg?I_FUNC_QARG:f->id,FA0(f),a1);
! 643:
! 644: case I_LIST: case I_EV:
! 645: n = rewrite_fnode_node((NODE)FA0(f),arg,qarg);
! 646: return mkfnode(1,f->id,n);
! 647:
! 648: case I_STR: case I_FORMULA:
! 649: return f;
! 650:
! 651: /* program variable */
! 652: case I_PVAR:
! 653: pv = (long)FA0(f);
! 654: for ( t = arg; t; t = NEXT(t) ) {
! 655: pair = (NODE)BDY(t);
! 656: ind = (long)BDY(pair);
! 657: value = (FNODE)BDY(NEXT(pair));
! 658: if ( pv == ind )
! 659: return value;
! 660: }
! 661: return f;
! 662: break;
! 663:
! 664: default:
! 665: error("rewrite_fnode : not implemented yet");
! 666: }
! 667: }
! 668:
! 669: NODE rewrite_fnode_node(NODE n,NODE arg,int qarg)
! 670: {
! 671: NODE r0,r,t;
! 672:
! 673: for ( r0 = 0, t = n; t; t = NEXT(t) ) {
! 674: NEXTNODE(r0,r);
! 675: BDY(r) = rewrite_fnode((FNODE)BDY(t),arg,qarg);
! 676: }
! 677: if ( r0 ) NEXT(r) = 0;
! 678: return r0;
! 679: }
! 680:
! 681: NODE fnode_to_nary_node(NODE n)
! 682: {
! 683: NODE r0,r,t;
! 684:
! 685: for ( r0 = 0, t = n; t; t = NEXT(t) ) {
! 686: NEXTNODE(r0,r);
! 687: BDY(r) = fnode_to_nary((FNODE)BDY(t));
! 688: }
! 689: if ( r0 ) NEXT(r) = 0;
! 690: return r0;
! 691: }
! 692:
! 693: NODE fnode_to_bin_node(NODE n,int dir)
! 694: {
! 695: NODE r0,r,t;
! 696:
! 697: for ( r0 = 0, t = n; t; t = NEXT(t) ) {
! 698: NEXTNODE(r0,r);
! 699: BDY(r) = fnode_to_bin((FNODE)BDY(t),dir);
! 700: }
! 701: if ( r0 ) NEXT(r) = 0;
! 702: return r0;
! 703: }
! 704:
! 705: V searchvar(char *name);
! 706:
! 707: pointer evalstat(SNODE f)
! 708: {
! 709: pointer val = 0,t,s,s1;
! 710: P u;
! 711: NODE tn;
! 712: int i,ac;
! 713: V v;
! 714: V *a;
! 715: char *buf;
! 716: FUNC func;
! 717:
! 718: if ( !f )
! 719: return ( 0 );
! 720: if ( nextbp && nextbplevel <= 0 && f->id != S_CPLX ) {
! 721: nextbp = 0;
! 722: bp(f);
! 723: }
! 724: evalstatline = f->ln;
! 725: if ( !PVSS ) at_root = evalstatline;
! 726:
! 727: switch ( f->id ) {
! 728: case S_BP:
! 729: if ( !nextbp && (!FA1(f) || eval((FNODE)FA1(f))) ) {
! 730: if ( (FNODE)FA2(f) ) {
! 731: asir_out = stderr;
! 732: printexpr(CO,eval((FNODE)FA2(f)));
! 733: putc('\n',asir_out); fflush(asir_out);
! 734: asir_out = stdout;
! 735: } else {
! 736: nextbp = 1; nextbplevel = 0;
! 737: }
! 738: }
! 739: val = evalstat((SNODE)FA0(f));
! 740: break;
! 741: case S_PFDEF:
! 742: ac = argc(FA1(f)); a = (V *)MALLOC(ac*sizeof(V));
! 743: s = eval((FNODE)FA2(f));
! 744: buf = (char *)ALLOCA(BUFSIZ);
! 745: for ( i = 0, tn = (NODE)FA1(f); tn; tn = NEXT(tn), i++ ) {
! 746: t = eval((FNODE)tn->body); sprintf(buf,"_%s",NAME(VR((P)t)));
! 747: makevar(buf,&u); a[i] = VR(u);
! 748: substr(CO,0,(Obj)s,VR((P)t),(Obj)u,(Obj *)&s1); s = s1;
! 749: }
! 750: mkpf((char *)FA0(f),(Obj)s,ac,a,0,0,0,(PF *)&val); val = 0;
! 751: v = searchvar((char *)FA0(f));
! 752: if ( v ) {
! 753: searchpf((char *)FA0(f),&func);
! 754: makesrvar(func,&u);
! 755: }
! 756: break;
! 757: case S_SINGLE:
! 758: val = eval((FNODE)FA0(f)); break;
! 759: case S_CPLX:
! 760: for ( tn = (NODE)FA0(f); tn; tn = NEXT(tn) ) {
! 761: if ( BDY(tn) )
! 762: val = evalstat((SNODE)BDY(tn));
! 763: if ( f_break || f_return || f_continue )
! 764: break;
! 765: }
! 766: break;
! 767: case S_BREAK:
! 768: if ( 1 || GPVS != CPVS )
! 769: f_break = 1;
! 770: break;
! 771: case S_CONTINUE:
! 772: if ( 1 || GPVS != CPVS )
! 773: f_continue = 1;
! 774: break;
! 775: case S_RETURN:
! 776: if ( 1 || GPVS != CPVS ) {
! 777: val = eval((FNODE)FA0(f)); f_return = 1;
! 778: }
! 779: break;
! 780: case S_IFELSE:
! 781: if ( evalnode((NODE)FA1(f)) )
! 782: val = evalstat((SNODE)FA2(f));
! 783: else if ( FA3(f) )
! 784: val = evalstat((SNODE)FA3(f));
! 785: break;
! 786: case S_FOR:
! 787: evalnode((NODE)FA1(f));
! 788: while ( 1 ) {
! 789: if ( !evalnode((NODE)FA2(f)) )
! 790: break;
! 791: val = evalstat((SNODE)FA4(f));
! 792: if ( f_break || f_return )
! 793: break;
! 794: f_continue = 0;
! 795: evalnode((NODE)FA3(f));
! 796: }
! 797: f_break = 0; break;
! 798: case S_DO:
! 799: while ( 1 ) {
! 800: val = evalstat((SNODE)FA1(f));
! 801: if ( f_break || f_return )
! 802: break;
! 803: f_continue = 0;
! 804: if ( !evalnode((NODE)FA2(f)) )
! 805: break;
! 806: }
! 807: f_break = 0; break;
! 808: case S_MODULE:
! 809: CUR_MODULE = (MODULE)FA0(f);
! 810: if ( CUR_MODULE )
! 811: MPVS = CUR_MODULE->pvs;
! 812: else
! 813: MPVS = 0;
! 814: break;
! 815: default:
! 816: error("evalstat : unknown id");
! 817: break;
! 818: }
! 819: return ( val );
! 820: }
! 821:
! 822: pointer evalnode(NODE node)
! 823: {
! 824: NODE tn;
! 825: pointer val;
! 826:
! 827: for ( tn = node, val = 0; tn; tn = NEXT(tn) )
! 828: if ( BDY(tn) )
! 829: val = eval((FNODE)BDY(tn));
! 830: return ( val );
! 831: }
! 832:
! 833:
! 834: LIST eval_arg(FNODE a,unsigned int quote)
! 835: {
! 836: LIST l;
! 837: FNODE fn;
! 838: NODE n,n0,tn;
! 839: QUOTE q;
! 840: int i;
! 841:
! 842: for ( tn = (NODE)FA0(a), n0 = 0, i = 0; tn; tn = NEXT(tn), i++ ) {
! 843: NEXTNODE(n0,n);
! 844: if ( quote & (1<<i) ) {
! 845: fn = (FNODE)(BDY(tn));
! 846: if ( fn->id == I_FORMULA && FA0(fn)
! 847: && OID((Obj)FA0(fn))== O_QUOTE )
! 848: BDY(n) = FA0(fn);
! 849: else {
! 850: MKQUOTE(q,(FNODE)BDY(tn));
! 851: BDY(n) = (pointer)q;
! 852: }
! 853: } else
! 854: BDY(n) = eval((FNODE)BDY(tn));
! 855: }
! 856: if ( n0 ) NEXT(n) = 0;
! 857: MKLIST(l,n0);
! 858: return l;
! 859: }
! 860:
! 861: pointer evalf(FUNC f,FNODE a,FNODE opt)
! 862: {
! 863: LIST args;
! 864: pointer val;
! 865: int i,n,level;
! 866: NODE tn,sn,opts,opt1,dmy;
! 867: VS pvs,prev_mpvs;
! 868: char errbuf[BUFSIZ];
! 869: static unsigned int stack_size;
! 870: static void *stack_base;
! 871: FUNC f1;
! 872:
! 873: if ( f->id == A_UNDEF ) {
! 874: gen_searchf_searchonly(f->fullname,&f1,0);
! 875: if ( f1->id == A_UNDEF ) {
! 876: sprintf(errbuf,"evalf : %s undefined",NAME(f));
! 877: error(errbuf);
! 878: } else
! 879: *f = *f1;
! 880: }
! 881: if ( getsecuremode() && !PVSS && !f->secure ) {
! 882: sprintf(errbuf,"evalf : %s not permitted",NAME(f));
! 883: error(errbuf);
! 884: }
! 885: if ( f->id != A_PARI ) {
! 886: for ( i = 0, tn = a?(NODE)FA0(a):0; tn; i++, tn = NEXT(tn) );
! 887: if ( ((n = f->argc)>= 0 && i != n) || (n < 0 && i > -n) ) {
! 888: sprintf(errbuf,"evalf : argument mismatch in %s()",NAME(f));
! 889: error(errbuf);
! 890: }
! 891: }
! 892: switch ( f->id ) {
! 893: case A_BIN:
! 894: if ( opt ) {
! 895: opts = BDY((LIST)eval(opt));
! 896: /* opts = ["opt1",arg1],... */
! 897: opt1 = BDY((LIST)BDY(opts));
! 898: if ( !strcmp(BDY((STRING)BDY(opt1)),"option_list") ) {
! 899: /*
! 900: * the special option specification:
! 901: * option_list=[["o1","a1"],...]
! 902: */
! 903: asir_assert(BDY(NEXT(opt1)),O_LIST,"evalf");
! 904: opts = BDY((LIST)BDY(NEXT(opt1)));
! 905: }
! 906: } else
! 907: opts = 0;
! 908: if ( !n ) {
! 909: current_option = opts;
! 910: cur_binf = f;
! 911: (*f->f.binf)(&val);
! 912: } else {
! 913: args = (LIST)eval_arg(a,f->quote);
! 914: current_option = opts;
! 915: cur_binf = f;
! 916: (*f->f.binf)(args?BDY(args):0,&val);
! 917: }
! 918: cur_binf = 0;
! 919: break;
! 920: case A_PARI:
! 921: args = (LIST)eval(a);
! 922: cur_binf = f;
! 923: val = evalparif(f,args?BDY(args):0);
! 924: cur_binf = 0;
! 925: break;
! 926: case A_USR:
! 927: /* stack check */
! 928: #if !defined(VISUAL) && !defined(__MINGW32__) && !defined(__CYGWIN__)
! 929: if ( !stack_size ) {
! 930: struct rlimit rl;
! 931: getrlimit(RLIMIT_STACK,&rl);
! 932: stack_size = rl.rlim_cur;
! 933: }
! 934: if ( !stack_base ) {
! 935: #if defined(GC7)
! 936: stack_base = (void *)GC_get_main_stack_base();
! 937: #else
! 938: stack_base = (void *)GC_get_stack_base();
! 939: #endif
! 940: }
! 941: if ( (stack_base - (void *)&args) +0x100000 > stack_size )
! 942: error("stack overflow");
! 943: #endif
! 944: args = (LIST)eval_arg(a,f->quote);
! 945: if ( opt ) {
! 946: opts = BDY((LIST)eval(opt));
! 947: /* opts = ["opt1",arg1],... */
! 948: opt1 = BDY((LIST)BDY(opts));
! 949: if ( !strcmp(BDY((STRING)BDY(opt1)),"option_list") ) {
! 950: /*
! 951: * the special option specification:
! 952: * option_list=[["o1","a1"],...]
! 953: */
! 954: asir_assert(BDY(NEXT(opt1)),O_LIST,"evalf");
! 955: opts = BDY((LIST)BDY(NEXT(opt1)));
! 956: }
! 957: } else
! 958: opts = 0;
! 959: pvs = f->f.usrf->pvs;
! 960: if ( PVSS ) {
! 961: ((VS)BDY(PVSS))->at = evalstatline;
! 962: level = ((VS)BDY(PVSS))->level+1;
! 963: } else
! 964: level = 1;
! 965: MKNODE(tn,pvs,PVSS); PVSS = tn;
! 966: CPVS = (VS)ALLOCA(sizeof(struct oVS)); BDY(PVSS) = (pointer)CPVS;
! 967: CPVS->usrf = f; CPVS->n = CPVS->asize = pvs->n;
! 968: CPVS->level = level;
! 969: CPVS->opt = opts;
! 970: if ( CPVS->n ) {
! 971: CPVS->va = (struct oPV *)ALLOCA(CPVS->n*sizeof(struct oPV));
! 972: bcopy((char *)pvs->va,(char *)CPVS->va,
! 973: (int)(pvs->n*sizeof(struct oPV)));
! 974: }
! 975: if ( nextbp )
! 976: nextbplevel++;
! 977: for ( tn = f->f.usrf->args, sn = BDY(args);
! 978: sn; tn = NEXT(tn), sn = NEXT(sn) )
! 979: ASSPV((long)FA0((FNODE)BDY(tn)),BDY(sn));
! 980: f_return = f_break = f_continue = 0;
! 981: if ( f->f.usrf->module ) {
! 982: prev_mpvs = MPVS;
! 983: MPVS = f->f.usrf->module->pvs;
! 984: val = evalstat((SNODE)BDY(f->f.usrf));
! 985: MPVS = prev_mpvs;
! 986: } else
! 987: val = evalstat((SNODE)BDY(f->f.usrf));
! 988: f_return = f_break = f_continue = 0; poppvs();
! 989: if ( PVSS )
! 990: evalstatline = ((VS)BDY(PVSS))->at;
! 991: break;
! 992: case A_PURE:
! 993: args = (LIST)eval(a);
! 994: val = evalpf(f->f.puref,args?BDY(args):0,0);
! 995: break;
! 996: default:
! 997: sprintf(errbuf,"evalf : %s undefined",NAME(f));
! 998: error(errbuf);
! 999: break;
! 1000: }
! 1001: return val;
! 1002: }
! 1003:
! 1004: pointer evalf_deriv(FUNC f,FNODE a,FNODE deriv)
! 1005: {
! 1006: LIST args,dargs;
! 1007: pointer val;
! 1008: char errbuf[BUFSIZ];
! 1009:
! 1010: switch ( f->id ) {
! 1011: case A_PURE:
! 1012: args = (LIST)eval(a);
! 1013: dargs = (LIST)eval(deriv);
! 1014: val = evalpf(f->f.puref,
! 1015: args?BDY(args):0,dargs?BDY(dargs):0);
! 1016: break;
! 1017: default:
! 1018: sprintf(errbuf,
! 1019: "evalf : %s is not a pure function",NAME(f));
! 1020: error(errbuf);
! 1021: break;
! 1022: }
! 1023: return val;
! 1024: }
! 1025:
! 1026: pointer evalmapf(FUNC f,FNODE a)
! 1027: {
! 1028: LIST args;
! 1029: NODE node,rest,t,n,r,r0;
! 1030: Obj head;
! 1031: VECT v,rv;
! 1032: MAT m,rm;
! 1033: LIST rl;
! 1034: int len,row,col,i,j;
! 1035: pointer val;
! 1036:
! 1037: args = (LIST)eval_arg(a,f->quote);
! 1038: node = BDY(args); head = (Obj)BDY(node); rest = NEXT(node);
! 1039: if ( !head ) {
! 1040: val = bevalf(f,node);
! 1041: return val;
! 1042: }
! 1043: switch ( OID(head) ) {
! 1044: case O_VECT:
! 1045: v = (VECT)head; len = v->len; MKVECT(rv,len);
! 1046: for ( i = 0; i < len; i++ ) {
! 1047: MKNODE(t,BDY(v)[i],rest); BDY(rv)[i] = bevalf(f,t);
! 1048: }
! 1049: val = (pointer)rv;
! 1050: break;
! 1051: case O_MAT:
! 1052: m = (MAT)head; row = m->row; col = m->col; MKMAT(rm,row,col);
! 1053: for ( i = 0; i < row; i++ )
! 1054: for ( j = 0; j < col; j++ ) {
! 1055: MKNODE(t,BDY(m)[i][j],rest); BDY(rm)[i][j] = bevalf(f,t);
! 1056: }
! 1057: val = (pointer)rm;
! 1058: break;
! 1059: case O_LIST:
! 1060: n = BDY((LIST)head);
! 1061: for ( r0 = r = 0; n; n = NEXT(n) ) {
! 1062: NEXTNODE(r0,r); MKNODE(t,BDY(n),rest); BDY(r) = bevalf(f,t);
! 1063: }
! 1064: if ( r0 )
! 1065: NEXT(r) = 0;
! 1066: MKLIST(rl,r0);
! 1067: val = (pointer)rl;
! 1068: break;
! 1069: default:
! 1070: val = bevalf(f,node);
! 1071: break;
! 1072: }
! 1073: return val;
! 1074: }
! 1075:
! 1076: pointer eval_rec_mapf(FUNC f,FNODE a)
! 1077: {
! 1078: LIST args;
! 1079:
! 1080: args = (LIST)eval_arg(a,f->quote);
! 1081: return beval_rec_mapf(f,BDY(args));
! 1082: }
! 1083:
! 1084: pointer beval_rec_mapf(FUNC f,NODE node)
! 1085: {
! 1086: NODE rest,t,n,r,r0;
! 1087: Obj head;
! 1088: VECT v,rv;
! 1089: MAT m,rm;
! 1090: LIST rl;
! 1091: int len,row,col,i,j;
! 1092: pointer val;
! 1093:
! 1094: head = (Obj)BDY(node); rest = NEXT(node);
! 1095: if ( !head ) {
! 1096: val = bevalf(f,node);
! 1097: return val;
! 1098: }
! 1099: switch ( OID(head) ) {
! 1100: case O_VECT:
! 1101: v = (VECT)head; len = v->len; MKVECT(rv,len);
! 1102: for ( i = 0; i < len; i++ ) {
! 1103: MKNODE(t,BDY(v)[i],rest); BDY(rv)[i] = beval_rec_mapf(f,t);
! 1104: }
! 1105: val = (pointer)rv;
! 1106: break;
! 1107: case O_MAT:
! 1108: m = (MAT)head; row = m->row; col = m->col; MKMAT(rm,row,col);
! 1109: for ( i = 0; i < row; i++ )
! 1110: for ( j = 0; j < col; j++ ) {
! 1111: MKNODE(t,BDY(m)[i][j],rest);
! 1112: BDY(rm)[i][j] = beval_rec_mapf(f,t);
! 1113: }
! 1114: val = (pointer)rm;
! 1115: break;
! 1116: case O_LIST:
! 1117: n = BDY((LIST)head);
! 1118: for ( r0 = r = 0; n; n = NEXT(n) ) {
! 1119: NEXTNODE(r0,r); MKNODE(t,BDY(n),rest);
! 1120: BDY(r) = beval_rec_mapf(f,t);
! 1121: }
! 1122: if ( r0 )
! 1123: NEXT(r) = 0;
! 1124: MKLIST(rl,r0);
! 1125: val = (pointer)rl;
! 1126: break;
! 1127: default:
! 1128: val = bevalf(f,node);
! 1129: break;
! 1130: }
! 1131: return val;
! 1132: }
! 1133:
! 1134: pointer bevalf(FUNC f,NODE a)
! 1135: {
! 1136: pointer val;
! 1137: int i,n;
! 1138: NODE tn,sn;
! 1139: VS pvs,prev_mpvs;
! 1140: char errbuf[BUFSIZ];
! 1141:
! 1142: if ( f->id == A_UNDEF ) {
! 1143: sprintf(errbuf,"bevalf : %s undefined",NAME(f));
! 1144: error(errbuf);
! 1145: }
! 1146: if ( getsecuremode() && !PVSS && !f->secure ) {
! 1147: sprintf(errbuf,"bevalf : %s not permitted",NAME(f));
! 1148: error(errbuf);
! 1149: }
! 1150: if ( f->id != A_PARI ) {
! 1151: for ( i = 0, tn = a; tn; i++, tn = NEXT(tn) );
! 1152: if ( ((n = f->argc)>= 0 && i != n) || (n < 0 && i > -n) ) {
! 1153: sprintf(errbuf,"bevalf : argument mismatch in %s()",NAME(f));
! 1154: error(errbuf);
! 1155: }
! 1156: }
! 1157: switch ( f->id ) {
! 1158: case A_BIN:
! 1159: current_option = 0;
! 1160: if ( !n ) {
! 1161: cur_binf = f;
! 1162: (*f->f.binf)(&val);
! 1163: } else {
! 1164: cur_binf = f;
! 1165: (*f->f.binf)(a,&val);
! 1166: }
! 1167: cur_binf = 0;
! 1168: break;
! 1169: case A_PARI:
! 1170: cur_binf = f;
! 1171: val = evalparif(f,a);
! 1172: cur_binf = 0;
! 1173: break;
! 1174: case A_USR:
! 1175: pvs = f->f.usrf->pvs;
! 1176: if ( PVSS )
! 1177: ((VS)BDY(PVSS))->at = evalstatline;
! 1178: MKNODE(tn,pvs,PVSS); PVSS = tn;
! 1179: CPVS = (VS)ALLOCA(sizeof(struct oVS)); BDY(PVSS) = (pointer)CPVS;
! 1180: CPVS->usrf = f; CPVS->n = CPVS->asize = pvs->n;
! 1181: CPVS->opt = 0;
! 1182: if ( CPVS->n ) {
! 1183: CPVS->va = (struct oPV *)ALLOCA(CPVS->n*sizeof(struct oPV));
! 1184: bcopy((char *)pvs->va,(char *)CPVS->va,
! 1185: (int)(pvs->n*sizeof(struct oPV)));
! 1186: }
! 1187: if ( nextbp )
! 1188: nextbplevel++;
! 1189: for ( tn = f->f.usrf->args, sn = a;
! 1190: sn; tn = NEXT(tn), sn = NEXT(sn) )
! 1191: ASSPV((long)FA0((FNODE)BDY(tn)),BDY(sn));
! 1192: f_return = f_break = f_continue = 0;
! 1193: if ( f->f.usrf->module ) {
! 1194: prev_mpvs = MPVS;
! 1195: MPVS = f->f.usrf->module->pvs;
! 1196: val = evalstat((SNODE)BDY(f->f.usrf));
! 1197: MPVS = prev_mpvs;
! 1198: } else
! 1199: val = evalstat((SNODE)BDY(f->f.usrf));
! 1200: f_return = f_break = f_continue = 0; poppvs();
! 1201: break;
! 1202: case A_PURE:
! 1203: val = evalpf(f->f.puref,a,0);
! 1204: break;
! 1205: default:
! 1206: sprintf(errbuf,"bevalf : %s undefined",NAME(f));
! 1207: error(errbuf);
! 1208: break;
! 1209: }
! 1210: return val;
! 1211: }
! 1212:
! 1213: pointer bevalf_with_opts(FUNC f,NODE a,NODE opts)
! 1214: {
! 1215: pointer val;
! 1216: int i,n;
! 1217: NODE tn,sn;
! 1218: VS pvs,prev_mpvs;
! 1219: char errbuf[BUFSIZ];
! 1220:
! 1221: if ( f->id == A_UNDEF ) {
! 1222: sprintf(errbuf,"bevalf : %s undefined",NAME(f));
! 1223: error(errbuf);
! 1224: }
! 1225: if ( getsecuremode() && !PVSS && !f->secure ) {
! 1226: sprintf(errbuf,"bevalf : %s not permitted",NAME(f));
! 1227: error(errbuf);
! 1228: }
! 1229: if ( f->id != A_PARI ) {
! 1230: for ( i = 0, tn = a; tn; i++, tn = NEXT(tn) );
! 1231: if ( ((n = f->argc)>= 0 && i != n) || (n < 0 && i > -n) ) {
! 1232: sprintf(errbuf,"bevalf : argument mismatch in %s()",NAME(f));
! 1233: error(errbuf);
! 1234: }
! 1235: }
! 1236: switch ( f->id ) {
! 1237: case A_BIN:
! 1238: current_option = opts;
! 1239: if ( !n ) {
! 1240: cur_binf = f;
! 1241: (*f->f.binf)(&val);
! 1242: } else {
! 1243: cur_binf = f;
! 1244: (*f->f.binf)(a,&val);
! 1245: }
! 1246: cur_binf = 0;
! 1247: break;
! 1248: case A_PARI:
! 1249: cur_binf = f;
! 1250: val = evalparif(f,a);
! 1251: cur_binf = 0;
! 1252: break;
! 1253: case A_USR:
! 1254: pvs = f->f.usrf->pvs;
! 1255: if ( PVSS )
! 1256: ((VS)BDY(PVSS))->at = evalstatline;
! 1257: MKNODE(tn,pvs,PVSS); PVSS = tn;
! 1258: CPVS = (VS)ALLOCA(sizeof(struct oVS)); BDY(PVSS) = (pointer)CPVS;
! 1259: CPVS->usrf = f; CPVS->n = CPVS->asize = pvs->n;
! 1260: CPVS->opt = opts;
! 1261: if ( CPVS->n ) {
! 1262: CPVS->va = (struct oPV *)ALLOCA(CPVS->n*sizeof(struct oPV));
! 1263: bcopy((char *)pvs->va,(char *)CPVS->va,
! 1264: (int)(pvs->n*sizeof(struct oPV)));
! 1265: }
! 1266: if ( nextbp )
! 1267: nextbplevel++;
! 1268: for ( tn = f->f.usrf->args, sn = a;
! 1269: sn; tn = NEXT(tn), sn = NEXT(sn) )
! 1270: ASSPV((long)FA0((FNODE)BDY(tn)),BDY(sn));
! 1271: f_return = f_break = f_continue = 0;
! 1272: if ( f->f.usrf->module ) {
! 1273: prev_mpvs = MPVS;
! 1274: MPVS = f->f.usrf->module->pvs;
! 1275: val = evalstat((SNODE)BDY(f->f.usrf));
! 1276: MPVS = prev_mpvs;
! 1277: } else
! 1278: val = evalstat((SNODE)BDY(f->f.usrf));
! 1279: f_return = f_break = f_continue = 0; poppvs();
! 1280: break;
! 1281: case A_PURE:
! 1282: val = evalpf(f->f.puref,a,0);
! 1283: break;
! 1284: default:
! 1285: sprintf(errbuf,"bevalf : %s undefined",NAME(f));
! 1286: error(errbuf);
! 1287: break;
! 1288: }
! 1289: return val;
! 1290: }
! 1291:
! 1292: pointer evalif(FNODE f,FNODE a,FNODE opt)
! 1293: {
! 1294: Obj g;
! 1295: QUOTE q;
! 1296: FNODE t;
! 1297: LIST l;
! 1298:
! 1299: g = (Obj)eval(f);
! 1300: if ( g && (OID(g) == O_P) && (VR((P)g)->attr == (pointer)V_SR) )
! 1301: return evalf((FUNC)VR((P)g)->priv,a,opt);
! 1302: else if ( g && OID(g) == O_QUOTEARG && ((QUOTEARG)g)->type == A_func ) {
! 1303: t = mkfnode(2,I_FUNC,((QUOTEARG)g)->body,a);
! 1304: MKQUOTE(q,t);
! 1305: return q;
! 1306: } else {
! 1307: error("invalid function pointer");
! 1308: /* NOTREACHED */
! 1309: return (pointer)-1;
! 1310: }
! 1311: }
! 1312:
! 1313: pointer evalpf(PF pf,NODE args,NODE dargs)
! 1314: {
! 1315: Obj s,s1;
! 1316: int i,di,j;
! 1317: NODE node,dnode;
! 1318: PFINS ins;
! 1319: PFAD ad;
! 1320:
! 1321: if ( !pf->body ) {
! 1322: ins = (PFINS)CALLOC(1,sizeof(PF)+pf->argc*sizeof(struct oPFAD));
! 1323: ins->pf = pf;
! 1324: for ( i = 0, node = args, dnode = dargs, ad = ins->ad;
! 1325: node; i++ ) {
! 1326: ad[i].arg = (Obj)node->body;
! 1327: if ( !dnode ) ad[i].d = 0;
! 1328: else
! 1329: ad[i].d = QTOS((Q)dnode->body);
! 1330: node = NEXT(node);
! 1331: if ( dnode ) dnode = NEXT(dnode);
! 1332: }
! 1333: simplify_ins(ins,&s);
! 1334: } else {
! 1335: s = pf->body;
! 1336: if ( dargs ) {
! 1337: for ( i = 0, dnode = dargs; dnode; dnode = NEXT(dnode), i++ ) {
! 1338: di = QTOS((Q)dnode->body);
! 1339: for ( j = 0; j < di; j++ ) {
! 1340: derivr(CO,s,pf->args[i],&s1); s = s1;
! 1341: }
! 1342: }
! 1343: }
! 1344: for ( i = 0, node = args; node; node = NEXT(node), i++ ) {
! 1345: substr(CO,0,s,pf->args[i],(Obj)node->body,&s1); s = s1;
! 1346: }
! 1347: }
! 1348: return (pointer)s;
! 1349: }
! 1350:
! 1351: void evalnodebody(NODE sn,NODE *dnp)
! 1352: {
! 1353: NODE n,n0,tn;
! 1354: int line;
! 1355:
! 1356: if ( !sn ) {
! 1357: *dnp = 0;
! 1358: return;
! 1359: }
! 1360: line = evalstatline;
! 1361: for ( tn = sn, n0 = 0; tn; tn = NEXT(tn) ) {
! 1362: NEXTNODE(n0,n);
! 1363: BDY(n) = eval((FNODE)BDY(tn));
! 1364: evalstatline = line;
! 1365: }
! 1366: NEXT(n) = 0; *dnp = n0;
! 1367: }
! 1368:
! 1369: MODULE searchmodule(char *name)
! 1370: {
! 1371: MODULE mod;
! 1372: NODE m;
! 1373:
! 1374: for ( m = MODULE_LIST; m; m = NEXT(m) ) {
! 1375: mod = (MODULE)BDY(m);
! 1376: if ( !strcmp(mod->name,name) )
! 1377: return mod;
! 1378: }
! 1379: return 0;
! 1380: }
! 1381: /*
! 1382: * xxx.yyy() is searched in the flist
! 1383: * of the module xxx.
! 1384: * yyy() is searched in the global flist.
! 1385: */
! 1386:
! 1387: void searchuf(char *name,FUNC *r)
! 1388: {
! 1389: MODULE mod;
! 1390: char *name0,*dot;
! 1391:
! 1392: if ( dot = strchr(name,'.') ) {
! 1393: name0 = (char *)ALLOCA(strlen(name)+1);
! 1394: strcpy(name0,name);
! 1395: dot = strchr(name0,'.');
! 1396: *dot = 0;
! 1397: mod = searchmodule(name0);
! 1398: if ( mod )
! 1399: searchf(mod->usrf_list,dot+1,r);
! 1400: } else
! 1401: searchf(usrf,name,r);
! 1402: }
! 1403:
! 1404: void gen_searchf(char *name,FUNC *r)
! 1405: {
! 1406: FUNC val = 0;
! 1407: int global = 0;
! 1408: if ( *name == ':' ) {
! 1409: global = 1;
! 1410: name += 2;
! 1411: }
! 1412: if ( CUR_MODULE && !global )
! 1413: searchf(CUR_MODULE->usrf_list,name,&val);
! 1414: if ( !val )
! 1415: searchf(sysf,name,&val);
! 1416: if ( !val )
! 1417: searchf(ubinf,name,&val);
! 1418: if ( !val )
! 1419: searchpf(name,&val);
! 1420: if ( !val )
! 1421: searchuf(name,&val);
! 1422: if ( !val )
! 1423: appenduf(name,&val);
! 1424: *r = val;
! 1425: }
! 1426:
! 1427: void gen_searchf_searchonly(char *name,FUNC *r,int global)
! 1428: {
! 1429: FUNC val = 0;
! 1430: if ( *name == ':' ) {
! 1431: global = 1;
! 1432: name += 2;
! 1433: }
! 1434: if ( CUR_MODULE && !global )
! 1435: searchf(CUR_MODULE->usrf_list,name,&val);
! 1436: if ( !val )
! 1437: searchf(sysf,name,&val);
! 1438: if ( !val )
! 1439: searchf(ubinf,name,&val);
! 1440: if ( !val )
! 1441: searchpf(name,&val);
! 1442: if ( !val )
! 1443: searchuf(name,&val);
! 1444: *r = val;
! 1445: }
! 1446:
! 1447: void searchf(NODE fn,char *name,FUNC *r)
! 1448: {
! 1449: NODE tn;
! 1450:
! 1451: for ( tn = fn;
! 1452: tn && strcmp(NAME((FUNC)BDY(tn)),name); tn = NEXT(tn) );
! 1453: if ( tn ) {
! 1454: *r = (FUNC)BDY(tn);
! 1455: return;
! 1456: }
! 1457: *r = 0;
! 1458: }
! 1459:
! 1460: MODULE mkmodule(char *);
! 1461:
! 1462: void appenduf(char *name,FUNC *r)
! 1463: {
! 1464: NODE tn;
! 1465: FUNC f;
! 1466: int len;
! 1467: MODULE mod;
! 1468: char *modname,*fname,*dot;
! 1469:
! 1470: f=(FUNC)MALLOC(sizeof(struct oFUNC));
! 1471: f->id = A_UNDEF; f->argc = 0; f->f.binf = 0;
! 1472: if ( dot = strchr(name,'.') ) {
! 1473: /* undefined function in a module */
! 1474: len = dot-name;
! 1475: modname = (char *)MALLOC_ATOMIC(len+1);
! 1476: strncpy(modname,name,len); modname[len] = 0;
! 1477: fname = (char *)MALLOC_ATOMIC(strlen(name)-len+1);
! 1478: strcpy(fname,dot+1);
! 1479: f->name = fname;
! 1480: f->fullname = name;
! 1481: mod = searchmodule(modname);
! 1482: if ( !mod )
! 1483: mod = mkmodule(modname);
! 1484: MKNODE(tn,f,mod->usrf_list); mod->usrf_list = tn;
! 1485: } else {
! 1486: f->name = name;
! 1487: f->fullname = name;
! 1488: MKNODE(tn,f,usrf); usrf = tn;
! 1489: }
! 1490: *r = f;
! 1491: }
! 1492:
! 1493: void appenduf_local(char *name,FUNC *r)
! 1494: {
! 1495: NODE tn;
! 1496: FUNC f;
! 1497: MODULE mod;
! 1498:
! 1499: for ( tn = CUR_MODULE->usrf_list; tn; tn = NEXT(tn) )
! 1500: if ( !strcmp(((FUNC)BDY(tn))->name,name) )
! 1501: break;
! 1502: if ( tn )
! 1503: return;
! 1504:
! 1505: f=(FUNC)MALLOC(sizeof(struct oFUNC));
! 1506: f->id = A_UNDEF; f->argc = 0; f->f.binf = 0;
! 1507: f->name = name;
! 1508: f->fullname =
! 1509: (char *)MALLOC_ATOMIC(strlen(CUR_MODULE->name)+strlen(name)+1);
! 1510: sprintf(f->fullname,"%s.%s",CUR_MODULE->name,name);
! 1511: MKNODE(tn,f,CUR_MODULE->usrf_list); CUR_MODULE->usrf_list = tn;
! 1512: *r = f;
! 1513: }
! 1514:
! 1515: void appenduflist(NODE n)
! 1516: {
! 1517: NODE tn;
! 1518: FUNC f;
! 1519:
! 1520: for ( tn = n; tn; tn = NEXT(tn) )
! 1521: appenduf_local((char *)BDY(tn),&f);
! 1522: }
! 1523:
! 1524: void mkparif(char *name,FUNC *r)
! 1525: {
! 1526: FUNC f;
! 1527:
! 1528: *r = f =(FUNC)MALLOC(sizeof(struct oFUNC));
! 1529: f->name = name; f->id = A_PARI; f->argc = 0; f->f.binf = 0;
! 1530: f->fullname = name;
! 1531: }
! 1532:
! 1533: void mkuf(char *name,char *fname,NODE args,SNODE body,int startl,int endl,char *desc,MODULE module)
! 1534: {
! 1535: FUNC f;
! 1536: USRF t;
! 1537: NODE usrf_list,sn,tn;
! 1538: FNODE fn;
! 1539: char *longname;
! 1540: int argc;
! 1541:
! 1542: if ( getsecuremode() ) {
! 1543: error("defining function is not permitted in the secure mode");
! 1544: }
! 1545: if ( *name == ':' )
! 1546: name += 2;
! 1547: if ( !module ) {
! 1548: searchf(sysf,name,&f);
! 1549: if ( f ) {
! 1550: fprintf(stderr,"def : builtin function %s() cannot be redefined.\n",name);
! 1551: CPVS = GPVS; return;
! 1552: }
! 1553: }
! 1554: for ( argc = 0, sn = args; sn; argc++, sn = NEXT(sn) ) {
! 1555: fn = (FNODE)BDY(sn);
! 1556: if ( !fn || ID(fn) != I_PVAR ) {
! 1557: fprintf(stderr,"illegal argument in %s()\n",name);
! 1558: CPVS = GPVS; return;
! 1559: }
! 1560: }
! 1561: usrf_list = module ? module->usrf_list : usrf;
! 1562: for ( sn = usrf_list; sn && strcmp(NAME((FUNC)BDY(sn)),name); sn = NEXT(sn) );
! 1563: if ( sn )
! 1564: f = (FUNC)BDY(sn);
! 1565: else {
! 1566: f=(FUNC)MALLOC(sizeof(struct oFUNC));
! 1567: f->name = name;
! 1568: MKNODE(tn,f,usrf_list); usrf_list = tn;
! 1569: if ( module ) {
! 1570: f->fullname =
! 1571: (char *)MALLOC_ATOMIC(strlen(f->name)+strlen(module->name)+1);
! 1572: sprintf(f->fullname,"%s.%s",module->name,f->name);
! 1573: module->usrf_list = usrf_list;
! 1574: } else {
! 1575: f->fullname = f->name;
! 1576: usrf = usrf_list;
! 1577: }
! 1578: }
! 1579: if ( Verbose && f->id != A_UNDEF ) {
! 1580: if ( module )
! 1581: fprintf(stderr,"Warning : %s.%s() redefined.\n",module->name,name);
! 1582: else
! 1583: fprintf(stderr,"Warning : %s() redefined.\n",name);
! 1584: }
! 1585: t=(USRF)MALLOC(sizeof(struct oUSRF));
! 1586: t->args=args; BDY(t)=body; t->pvs = CPVS; t->fname = fname;
! 1587: t->startl = startl; t->endl = endl; t->module = module;
! 1588: t->desc = desc;
! 1589: f->id = A_USR; f->argc = argc; f->f.usrf = t;
! 1590: CPVS = GPVS;
! 1591: CUR_FUNC = 0;
! 1592: clearbp(f);
! 1593: }
! 1594:
! 1595: /*
! 1596: retrieve value of an option whose key matches 'key'
! 1597: CVS->opt is a list(node) of key-value pair (list)
! 1598: CVS->opt = BDY([[key,value],[key,value],...])
! 1599: */
! 1600:
! 1601: Obj getopt_from_cpvs(char *key)
! 1602: {
! 1603: NODE opts,opt;
! 1604: LIST r;
! 1605: extern Obj VOIDobj;
! 1606:
! 1607: opts = CPVS->opt;
! 1608: if ( !key ) {
! 1609: MKLIST(r,opts);
! 1610: return (Obj)r;
! 1611: } else {
! 1612: for ( ; opts; opts = NEXT(opts) ) {
! 1613: asir_assert(BDY(opts),O_LIST,"getopt_from_cvps");
! 1614: opt = BDY((LIST)BDY(opts));
! 1615: if ( !strcmp(key,BDY((STRING)BDY(opt))) )
! 1616: return (Obj)BDY(NEXT(opt));
! 1617: }
! 1618: return VOIDobj;
! 1619: }
! 1620:
! 1621: }
! 1622:
! 1623: MODULE mkmodule(char *name)
! 1624: {
! 1625: MODULE mod;
! 1626: NODE m;
! 1627: int len;
! 1628: VS mpvs;
! 1629:
! 1630: for ( m = MODULE_LIST; m; m = NEXT(m) ) {
! 1631: mod = (MODULE)m->body;
! 1632: if ( !strcmp(mod->name,name) )
! 1633: break;
! 1634: }
! 1635: if ( m )
! 1636: return mod;
! 1637: else {
! 1638: mod = (MODULE)MALLOC(sizeof(struct oMODULE));
! 1639: len = strlen(name);
! 1640: mod->name = (char *)MALLOC_ATOMIC(len+1);
! 1641: strcpy(mod->name,name);
! 1642: mod->pvs = mpvs = (VS)MALLOC(sizeof(struct oVS));
! 1643: asir_reallocarray((char **)&mpvs->va,(int *)&mpvs->asize,
! 1644: (int *)&mpvs->n,(int)sizeof(struct oPV));
! 1645: mod->usrf_list = 0;
! 1646: MKNODE(m,mod,MODULE_LIST);
! 1647: MODULE_LIST = m;
! 1648: return mod;
! 1649: }
! 1650: }
! 1651:
! 1652: void print_crossref(FUNC f)
! 1653: {
! 1654: FUNC r;
! 1655: if ( show_crossref && CUR_FUNC ) {
! 1656: searchuf(f->fullname,&r);
! 1657: if (r != NULL) {
! 1658: fprintf(asir_out,"%s() at line %d in %s()\n",
! 1659: f->fullname, asir_infile->ln, CUR_FUNC);
! 1660: }
! 1661: }
! 1662: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>