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