Annotation of OpenXM_contrib2/asir2018/parse/eval.c, Revision 1.4
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.4 ! kondoh 48: * $OpenXM: OpenXM_contrib2/asir2018/parse/eval.c,v 1.3 2019/08/21 00:37:47 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: }
1.4 ! kondoh 753: #if defined(INTERVAL)
! 754: mkpf((char *)FA0(f),(Obj)s,ac,a,0,0,0,0,(PF *)&val); val = 0;
! 755: #else
1.1 noro 756: mkpf((char *)FA0(f),(Obj)s,ac,a,0,0,0,(PF *)&val); val = 0;
1.4 ! kondoh 757: #endif
1.1 noro 758: v = searchvar((char *)FA0(f));
759: if ( v ) {
760: searchpf((char *)FA0(f),&func);
761: makesrvar(func,&u);
762: }
763: break;
764: case S_SINGLE:
765: val = eval((FNODE)FA0(f)); break;
766: case S_CPLX:
767: for ( tn = (NODE)FA0(f); tn; tn = NEXT(tn) ) {
768: if ( BDY(tn) )
769: val = evalstat((SNODE)BDY(tn));
770: if ( f_break || f_return || f_continue )
771: break;
772: }
773: break;
774: case S_BREAK:
775: if ( 1 || GPVS != CPVS )
776: f_break = 1;
777: break;
778: case S_CONTINUE:
779: if ( 1 || GPVS != CPVS )
780: f_continue = 1;
781: break;
782: case S_RETURN:
783: if ( 1 || GPVS != CPVS ) {
784: val = eval((FNODE)FA0(f)); f_return = 1;
785: }
786: break;
787: case S_IFELSE:
788: if ( evalnode((NODE)FA1(f)) )
789: val = evalstat((SNODE)FA2(f));
790: else if ( FA3(f) )
791: val = evalstat((SNODE)FA3(f));
792: break;
793: case S_FOR:
794: evalnode((NODE)FA1(f));
795: while ( 1 ) {
796: if ( !evalnode((NODE)FA2(f)) )
797: break;
798: val = evalstat((SNODE)FA4(f));
799: if ( f_break || f_return )
800: break;
801: f_continue = 0;
802: evalnode((NODE)FA3(f));
803: }
804: f_break = 0; break;
805: case S_DO:
806: while ( 1 ) {
807: val = evalstat((SNODE)FA1(f));
808: if ( f_break || f_return )
809: break;
810: f_continue = 0;
811: if ( !evalnode((NODE)FA2(f)) )
812: break;
813: }
814: f_break = 0; break;
815: case S_MODULE:
816: CUR_MODULE = (MODULE)FA0(f);
817: if ( CUR_MODULE )
818: MPVS = CUR_MODULE->pvs;
819: else
820: MPVS = 0;
821: break;
822: default:
823: error("evalstat : unknown id");
824: break;
825: }
826: return ( val );
827: }
828:
829: pointer evalnode(NODE node)
830: {
831: NODE tn;
832: pointer val;
833:
834: for ( tn = node, val = 0; tn; tn = NEXT(tn) )
835: if ( BDY(tn) )
836: val = eval((FNODE)BDY(tn));
837: return ( val );
838: }
839:
840:
841: LIST eval_arg(FNODE a,unsigned int quote)
842: {
843: LIST l;
844: FNODE fn;
845: NODE n,n0,tn;
846: QUOTE q;
847: int i;
848:
849: for ( tn = (NODE)FA0(a), n0 = 0, i = 0; tn; tn = NEXT(tn), i++ ) {
850: NEXTNODE(n0,n);
851: if ( quote & (1<<i) ) {
852: fn = (FNODE)(BDY(tn));
853: if ( fn->id == I_FORMULA && FA0(fn)
854: && OID((Obj)FA0(fn))== O_QUOTE )
855: BDY(n) = FA0(fn);
856: else {
857: MKQUOTE(q,(FNODE)BDY(tn));
858: BDY(n) = (pointer)q;
859: }
860: } else
861: BDY(n) = eval((FNODE)BDY(tn));
862: }
863: if ( n0 ) NEXT(n) = 0;
864: MKLIST(l,n0);
865: return l;
866: }
867:
868: pointer evalf(FUNC f,FNODE a,FNODE opt)
869: {
870: LIST args;
871: pointer val;
872: int i,n,level;
873: NODE tn,sn,opts,opt1,dmy;
874: VS pvs,prev_mpvs;
875: char errbuf[BUFSIZ];
876: static unsigned int stack_size;
877: static void *stack_base;
878: FUNC f1;
879:
880: if ( f->id == A_UNDEF ) {
881: gen_searchf_searchonly(f->fullname,&f1,0);
882: if ( f1->id == A_UNDEF ) {
883: sprintf(errbuf,"evalf : %s undefined",NAME(f));
884: error(errbuf);
885: } else
886: *f = *f1;
887: }
888: if ( getsecuremode() && !PVSS && !f->secure ) {
889: sprintf(errbuf,"evalf : %s not permitted",NAME(f));
890: error(errbuf);
891: }
892: if ( f->id != A_PARI ) {
893: for ( i = 0, tn = a?(NODE)FA0(a):0; tn; i++, tn = NEXT(tn) );
894: if ( ((n = f->argc)>= 0 && i != n) || (n < 0 && i > -n) ) {
895: sprintf(errbuf,"evalf : argument mismatch in %s()",NAME(f));
896: error(errbuf);
897: }
898: }
899: switch ( f->id ) {
900: case A_BIN:
901: if ( opt ) {
902: opts = BDY((LIST)eval(opt));
903: /* opts = ["opt1",arg1],... */
904: opt1 = BDY((LIST)BDY(opts));
905: if ( !strcmp(BDY((STRING)BDY(opt1)),"option_list") ) {
906: /*
907: * the special option specification:
908: * option_list=[["o1","a1"],...]
909: */
910: asir_assert(BDY(NEXT(opt1)),O_LIST,"evalf");
911: opts = BDY((LIST)BDY(NEXT(opt1)));
912: }
913: } else
914: opts = 0;
915: if ( !n ) {
916: current_option = opts;
917: cur_binf = f;
918: (*f->f.binf)(&val);
919: } else {
920: args = (LIST)eval_arg(a,f->quote);
921: current_option = opts;
922: cur_binf = f;
923: (*f->f.binf)(args?BDY(args):0,&val);
924: }
925: cur_binf = 0;
926: break;
927: case A_PARI:
928: args = (LIST)eval(a);
929: cur_binf = f;
930: val = evalparif(f,args?BDY(args):0);
931: cur_binf = 0;
932: break;
933: case A_USR:
934: /* stack check */
935: #if !defined(VISUAL) && !defined(__MINGW32__) && !defined(__CYGWIN__)
936: if ( !stack_size ) {
937: struct rlimit rl;
938: getrlimit(RLIMIT_STACK,&rl);
939: stack_size = rl.rlim_cur;
940: }
941: if ( !stack_base ) {
942: #if defined(GC7)
943: stack_base = (void *)GC_get_main_stack_base();
944: #else
945: stack_base = (void *)GC_get_stack_base();
946: #endif
947: }
948: if ( (stack_base - (void *)&args) +0x100000 > stack_size )
949: error("stack overflow");
950: #endif
951: args = (LIST)eval_arg(a,f->quote);
952: if ( opt ) {
953: opts = BDY((LIST)eval(opt));
954: /* opts = ["opt1",arg1],... */
955: opt1 = BDY((LIST)BDY(opts));
956: if ( !strcmp(BDY((STRING)BDY(opt1)),"option_list") ) {
957: /*
958: * the special option specification:
959: * option_list=[["o1","a1"],...]
960: */
961: asir_assert(BDY(NEXT(opt1)),O_LIST,"evalf");
962: opts = BDY((LIST)BDY(NEXT(opt1)));
963: }
964: } else
965: opts = 0;
966: pvs = f->f.usrf->pvs;
967: if ( PVSS ) {
968: ((VS)BDY(PVSS))->at = evalstatline;
969: level = ((VS)BDY(PVSS))->level+1;
970: } else
971: level = 1;
972: MKNODE(tn,pvs,PVSS); PVSS = tn;
973: CPVS = (VS)ALLOCA(sizeof(struct oVS)); BDY(PVSS) = (pointer)CPVS;
974: CPVS->usrf = f; CPVS->n = CPVS->asize = pvs->n;
975: CPVS->level = level;
976: CPVS->opt = opts;
977: if ( CPVS->n ) {
978: CPVS->va = (struct oPV *)ALLOCA(CPVS->n*sizeof(struct oPV));
979: bcopy((char *)pvs->va,(char *)CPVS->va,
980: (int)(pvs->n*sizeof(struct oPV)));
981: }
982: if ( nextbp )
983: nextbplevel++;
984: for ( tn = f->f.usrf->args, sn = BDY(args);
985: sn; tn = NEXT(tn), sn = NEXT(sn) )
986: ASSPV((long)FA0((FNODE)BDY(tn)),BDY(sn));
987: f_return = f_break = f_continue = 0;
988: if ( f->f.usrf->module ) {
989: prev_mpvs = MPVS;
990: MPVS = f->f.usrf->module->pvs;
991: val = evalstat((SNODE)BDY(f->f.usrf));
992: MPVS = prev_mpvs;
993: } else
994: val = evalstat((SNODE)BDY(f->f.usrf));
995: f_return = f_break = f_continue = 0; poppvs();
996: if ( PVSS )
997: evalstatline = ((VS)BDY(PVSS))->at;
998: break;
999: case A_PURE:
1000: args = (LIST)eval(a);
1001: val = evalpf(f->f.puref,args?BDY(args):0,0);
1002: break;
1003: default:
1004: sprintf(errbuf,"evalf : %s undefined",NAME(f));
1005: error(errbuf);
1006: break;
1007: }
1008: return val;
1009: }
1010:
1011: pointer evalf_deriv(FUNC f,FNODE a,FNODE deriv)
1012: {
1013: LIST args,dargs;
1014: pointer val;
1015: char errbuf[BUFSIZ];
1016:
1017: switch ( f->id ) {
1018: case A_PURE:
1019: args = (LIST)eval(a);
1020: dargs = (LIST)eval(deriv);
1021: val = evalpf(f->f.puref,
1022: args?BDY(args):0,dargs?BDY(dargs):0);
1023: break;
1024: default:
1025: sprintf(errbuf,
1026: "evalf : %s is not a pure function",NAME(f));
1027: error(errbuf);
1028: break;
1029: }
1030: return val;
1031: }
1032:
1033: pointer evalmapf(FUNC f,FNODE a)
1034: {
1035: LIST args;
1036: NODE node,rest,t,n,r,r0;
1037: Obj head;
1038: VECT v,rv;
1039: MAT m,rm;
1040: LIST rl;
1041: int len,row,col,i,j;
1042: pointer val;
1043:
1044: args = (LIST)eval_arg(a,f->quote);
1045: node = BDY(args); head = (Obj)BDY(node); rest = NEXT(node);
1046: if ( !head ) {
1047: val = bevalf(f,node);
1048: return val;
1049: }
1050: switch ( OID(head) ) {
1051: case O_VECT:
1052: v = (VECT)head; len = v->len; MKVECT(rv,len);
1053: for ( i = 0; i < len; i++ ) {
1054: MKNODE(t,BDY(v)[i],rest); BDY(rv)[i] = bevalf(f,t);
1055: }
1056: val = (pointer)rv;
1057: break;
1058: case O_MAT:
1059: m = (MAT)head; row = m->row; col = m->col; MKMAT(rm,row,col);
1060: for ( i = 0; i < row; i++ )
1061: for ( j = 0; j < col; j++ ) {
1062: MKNODE(t,BDY(m)[i][j],rest); BDY(rm)[i][j] = bevalf(f,t);
1063: }
1064: val = (pointer)rm;
1065: break;
1066: case O_LIST:
1067: n = BDY((LIST)head);
1068: for ( r0 = r = 0; n; n = NEXT(n) ) {
1069: NEXTNODE(r0,r); MKNODE(t,BDY(n),rest); BDY(r) = bevalf(f,t);
1070: }
1071: if ( r0 )
1072: NEXT(r) = 0;
1073: MKLIST(rl,r0);
1074: val = (pointer)rl;
1075: break;
1076: default:
1077: val = bevalf(f,node);
1078: break;
1079: }
1080: return val;
1081: }
1082:
1083: pointer eval_rec_mapf(FUNC f,FNODE a)
1084: {
1085: LIST args;
1086:
1087: args = (LIST)eval_arg(a,f->quote);
1088: return beval_rec_mapf(f,BDY(args));
1089: }
1090:
1091: pointer beval_rec_mapf(FUNC f,NODE node)
1092: {
1093: NODE rest,t,n,r,r0;
1094: Obj head;
1095: VECT v,rv;
1096: MAT m,rm;
1097: LIST rl;
1098: int len,row,col,i,j;
1099: pointer val;
1100:
1101: head = (Obj)BDY(node); rest = NEXT(node);
1102: if ( !head ) {
1103: val = bevalf(f,node);
1104: return val;
1105: }
1106: switch ( OID(head) ) {
1107: case O_VECT:
1108: v = (VECT)head; len = v->len; MKVECT(rv,len);
1109: for ( i = 0; i < len; i++ ) {
1110: MKNODE(t,BDY(v)[i],rest); BDY(rv)[i] = beval_rec_mapf(f,t);
1111: }
1112: val = (pointer)rv;
1113: break;
1114: case O_MAT:
1115: m = (MAT)head; row = m->row; col = m->col; MKMAT(rm,row,col);
1116: for ( i = 0; i < row; i++ )
1117: for ( j = 0; j < col; j++ ) {
1118: MKNODE(t,BDY(m)[i][j],rest);
1119: BDY(rm)[i][j] = beval_rec_mapf(f,t);
1120: }
1121: val = (pointer)rm;
1122: break;
1123: case O_LIST:
1124: n = BDY((LIST)head);
1125: for ( r0 = r = 0; n; n = NEXT(n) ) {
1126: NEXTNODE(r0,r); MKNODE(t,BDY(n),rest);
1127: BDY(r) = beval_rec_mapf(f,t);
1128: }
1129: if ( r0 )
1130: NEXT(r) = 0;
1131: MKLIST(rl,r0);
1132: val = (pointer)rl;
1133: break;
1134: default:
1135: val = bevalf(f,node);
1136: break;
1137: }
1138: return val;
1139: }
1140:
1141: pointer bevalf(FUNC f,NODE a)
1142: {
1143: pointer val;
1144: int i,n;
1145: NODE tn,sn;
1146: VS pvs,prev_mpvs;
1147: char errbuf[BUFSIZ];
1148:
1149: if ( f->id == A_UNDEF ) {
1150: sprintf(errbuf,"bevalf : %s undefined",NAME(f));
1151: error(errbuf);
1152: }
1153: if ( getsecuremode() && !PVSS && !f->secure ) {
1154: sprintf(errbuf,"bevalf : %s not permitted",NAME(f));
1155: error(errbuf);
1156: }
1157: if ( f->id != A_PARI ) {
1158: for ( i = 0, tn = a; tn; i++, tn = NEXT(tn) );
1159: if ( ((n = f->argc)>= 0 && i != n) || (n < 0 && i > -n) ) {
1160: sprintf(errbuf,"bevalf : argument mismatch in %s()",NAME(f));
1161: error(errbuf);
1162: }
1163: }
1164: switch ( f->id ) {
1165: case A_BIN:
1166: current_option = 0;
1167: if ( !n ) {
1168: cur_binf = f;
1169: (*f->f.binf)(&val);
1170: } else {
1171: cur_binf = f;
1172: (*f->f.binf)(a,&val);
1173: }
1174: cur_binf = 0;
1175: break;
1176: case A_PARI:
1177: cur_binf = f;
1178: val = evalparif(f,a);
1179: cur_binf = 0;
1180: break;
1181: case A_USR:
1182: pvs = f->f.usrf->pvs;
1183: if ( PVSS )
1184: ((VS)BDY(PVSS))->at = evalstatline;
1185: MKNODE(tn,pvs,PVSS); PVSS = tn;
1186: CPVS = (VS)ALLOCA(sizeof(struct oVS)); BDY(PVSS) = (pointer)CPVS;
1187: CPVS->usrf = f; CPVS->n = CPVS->asize = pvs->n;
1188: CPVS->opt = 0;
1189: if ( CPVS->n ) {
1190: CPVS->va = (struct oPV *)ALLOCA(CPVS->n*sizeof(struct oPV));
1191: bcopy((char *)pvs->va,(char *)CPVS->va,
1192: (int)(pvs->n*sizeof(struct oPV)));
1193: }
1194: if ( nextbp )
1195: nextbplevel++;
1196: for ( tn = f->f.usrf->args, sn = a;
1197: sn; tn = NEXT(tn), sn = NEXT(sn) )
1198: ASSPV((long)FA0((FNODE)BDY(tn)),BDY(sn));
1199: f_return = f_break = f_continue = 0;
1200: if ( f->f.usrf->module ) {
1201: prev_mpvs = MPVS;
1202: MPVS = f->f.usrf->module->pvs;
1203: val = evalstat((SNODE)BDY(f->f.usrf));
1204: MPVS = prev_mpvs;
1205: } else
1206: val = evalstat((SNODE)BDY(f->f.usrf));
1207: f_return = f_break = f_continue = 0; poppvs();
1208: break;
1209: case A_PURE:
1210: val = evalpf(f->f.puref,a,0);
1211: break;
1212: default:
1213: sprintf(errbuf,"bevalf : %s undefined",NAME(f));
1214: error(errbuf);
1215: break;
1216: }
1217: return val;
1218: }
1219:
1220: pointer bevalf_with_opts(FUNC f,NODE a,NODE opts)
1221: {
1222: pointer val;
1223: int i,n;
1224: NODE tn,sn;
1225: VS pvs,prev_mpvs;
1226: char errbuf[BUFSIZ];
1227:
1228: if ( f->id == A_UNDEF ) {
1229: sprintf(errbuf,"bevalf : %s undefined",NAME(f));
1230: error(errbuf);
1231: }
1232: if ( getsecuremode() && !PVSS && !f->secure ) {
1233: sprintf(errbuf,"bevalf : %s not permitted",NAME(f));
1234: error(errbuf);
1235: }
1236: if ( f->id != A_PARI ) {
1237: for ( i = 0, tn = a; tn; i++, tn = NEXT(tn) );
1238: if ( ((n = f->argc)>= 0 && i != n) || (n < 0 && i > -n) ) {
1239: sprintf(errbuf,"bevalf : argument mismatch in %s()",NAME(f));
1240: error(errbuf);
1241: }
1242: }
1243: switch ( f->id ) {
1244: case A_BIN:
1245: current_option = opts;
1246: if ( !n ) {
1247: cur_binf = f;
1248: (*f->f.binf)(&val);
1249: } else {
1250: cur_binf = f;
1251: (*f->f.binf)(a,&val);
1252: }
1253: cur_binf = 0;
1254: break;
1255: case A_PARI:
1256: cur_binf = f;
1257: val = evalparif(f,a);
1258: cur_binf = 0;
1259: break;
1260: case A_USR:
1261: pvs = f->f.usrf->pvs;
1262: if ( PVSS )
1263: ((VS)BDY(PVSS))->at = evalstatline;
1264: MKNODE(tn,pvs,PVSS); PVSS = tn;
1265: CPVS = (VS)ALLOCA(sizeof(struct oVS)); BDY(PVSS) = (pointer)CPVS;
1266: CPVS->usrf = f; CPVS->n = CPVS->asize = pvs->n;
1267: CPVS->opt = opts;
1268: if ( CPVS->n ) {
1269: CPVS->va = (struct oPV *)ALLOCA(CPVS->n*sizeof(struct oPV));
1270: bcopy((char *)pvs->va,(char *)CPVS->va,
1271: (int)(pvs->n*sizeof(struct oPV)));
1272: }
1273: if ( nextbp )
1274: nextbplevel++;
1275: for ( tn = f->f.usrf->args, sn = a;
1276: sn; tn = NEXT(tn), sn = NEXT(sn) )
1277: ASSPV((long)FA0((FNODE)BDY(tn)),BDY(sn));
1278: f_return = f_break = f_continue = 0;
1279: if ( f->f.usrf->module ) {
1280: prev_mpvs = MPVS;
1281: MPVS = f->f.usrf->module->pvs;
1282: val = evalstat((SNODE)BDY(f->f.usrf));
1283: MPVS = prev_mpvs;
1284: } else
1285: val = evalstat((SNODE)BDY(f->f.usrf));
1286: f_return = f_break = f_continue = 0; poppvs();
1287: break;
1288: case A_PURE:
1289: val = evalpf(f->f.puref,a,0);
1290: break;
1291: default:
1292: sprintf(errbuf,"bevalf : %s undefined",NAME(f));
1293: error(errbuf);
1294: break;
1295: }
1296: return val;
1297: }
1298:
1299: pointer evalif(FNODE f,FNODE a,FNODE opt)
1300: {
1301: Obj g;
1302: QUOTE q;
1303: FNODE t;
1304: LIST l;
1305:
1306: g = (Obj)eval(f);
1307: if ( g && (OID(g) == O_P) && (VR((P)g)->attr == (pointer)V_SR) )
1308: return evalf((FUNC)VR((P)g)->priv,a,opt);
1309: else if ( g && OID(g) == O_QUOTEARG && ((QUOTEARG)g)->type == A_func ) {
1310: t = mkfnode(2,I_FUNC,((QUOTEARG)g)->body,a);
1311: MKQUOTE(q,t);
1312: return q;
1313: } else {
1314: error("invalid function pointer");
1315: /* NOTREACHED */
1316: return (pointer)-1;
1317: }
1318: }
1319:
1320: pointer evalpf(PF pf,NODE args,NODE dargs)
1321: {
1322: Obj s,s1;
1323: int i,di,j;
1324: NODE node,dnode;
1325: PFINS ins;
1326: PFAD ad;
1327:
1328: if ( !pf->body ) {
1329: ins = (PFINS)CALLOC(1,sizeof(PF)+pf->argc*sizeof(struct oPFAD));
1330: ins->pf = pf;
1331: for ( i = 0, node = args, dnode = dargs, ad = ins->ad;
1332: node; i++ ) {
1333: ad[i].arg = (Obj)node->body;
1334: if ( !dnode ) ad[i].d = 0;
1335: else
1.2 noro 1336: ad[i].d = ZTOS((Q)dnode->body);
1.1 noro 1337: node = NEXT(node);
1338: if ( dnode ) dnode = NEXT(dnode);
1339: }
1340: simplify_ins(ins,&s);
1341: } else {
1342: s = pf->body;
1343: if ( dargs ) {
1344: for ( i = 0, dnode = dargs; dnode; dnode = NEXT(dnode), i++ ) {
1.2 noro 1345: di = ZTOS((Q)dnode->body);
1.1 noro 1346: for ( j = 0; j < di; j++ ) {
1347: derivr(CO,s,pf->args[i],&s1); s = s1;
1348: }
1349: }
1350: }
1351: for ( i = 0, node = args; node; node = NEXT(node), i++ ) {
1352: substr(CO,0,s,pf->args[i],(Obj)node->body,&s1); s = s1;
1353: }
1354: }
1355: return (pointer)s;
1356: }
1357:
1358: void evalnodebody(NODE sn,NODE *dnp)
1359: {
1360: NODE n,n0,tn;
1361: int line;
1362:
1363: if ( !sn ) {
1364: *dnp = 0;
1365: return;
1366: }
1367: line = evalstatline;
1368: for ( tn = sn, n0 = 0; tn; tn = NEXT(tn) ) {
1369: NEXTNODE(n0,n);
1370: BDY(n) = eval((FNODE)BDY(tn));
1371: evalstatline = line;
1372: }
1373: NEXT(n) = 0; *dnp = n0;
1374: }
1375:
1376: MODULE searchmodule(char *name)
1377: {
1378: MODULE mod;
1379: NODE m;
1380:
1381: for ( m = MODULE_LIST; m; m = NEXT(m) ) {
1382: mod = (MODULE)BDY(m);
1383: if ( !strcmp(mod->name,name) )
1384: return mod;
1385: }
1386: return 0;
1387: }
1388: /*
1389: * xxx.yyy() is searched in the flist
1390: * of the module xxx.
1391: * yyy() is searched in the global flist.
1392: */
1393:
1394: void searchuf(char *name,FUNC *r)
1395: {
1396: MODULE mod;
1397: char *name0,*dot;
1398:
1399: if ( dot = strchr(name,'.') ) {
1400: name0 = (char *)ALLOCA(strlen(name)+1);
1401: strcpy(name0,name);
1402: dot = strchr(name0,'.');
1403: *dot = 0;
1404: mod = searchmodule(name0);
1405: if ( mod )
1406: searchf(mod->usrf_list,dot+1,r);
1407: } else
1408: searchf(usrf,name,r);
1409: }
1410:
1411: void gen_searchf(char *name,FUNC *r)
1412: {
1413: FUNC val = 0;
1414: int global = 0;
1415: if ( *name == ':' ) {
1416: global = 1;
1417: name += 2;
1418: }
1419: if ( CUR_MODULE && !global )
1420: searchf(CUR_MODULE->usrf_list,name,&val);
1421: if ( !val )
1422: searchf(sysf,name,&val);
1423: if ( !val )
1424: searchf(ubinf,name,&val);
1425: if ( !val )
1426: searchpf(name,&val);
1427: if ( !val )
1428: searchuf(name,&val);
1429: if ( !val )
1430: appenduf(name,&val);
1431: *r = val;
1432: }
1433:
1434: void gen_searchf_searchonly(char *name,FUNC *r,int global)
1435: {
1436: FUNC val = 0;
1437: if ( *name == ':' ) {
1438: global = 1;
1439: name += 2;
1440: }
1441: if ( CUR_MODULE && !global )
1442: searchf(CUR_MODULE->usrf_list,name,&val);
1443: if ( !val )
1444: searchf(sysf,name,&val);
1445: if ( !val )
1446: searchf(ubinf,name,&val);
1447: if ( !val )
1448: searchpf(name,&val);
1449: if ( !val )
1450: searchuf(name,&val);
1451: *r = val;
1452: }
1453:
1454: void searchf(NODE fn,char *name,FUNC *r)
1455: {
1456: NODE tn;
1457:
1458: for ( tn = fn;
1459: tn && strcmp(NAME((FUNC)BDY(tn)),name); tn = NEXT(tn) );
1460: if ( tn ) {
1461: *r = (FUNC)BDY(tn);
1462: return;
1463: }
1464: *r = 0;
1465: }
1466:
1467: MODULE mkmodule(char *);
1468:
1469: void appenduf(char *name,FUNC *r)
1470: {
1471: NODE tn;
1472: FUNC f;
1473: int len;
1474: MODULE mod;
1475: char *modname,*fname,*dot;
1476:
1477: f=(FUNC)MALLOC(sizeof(struct oFUNC));
1478: f->id = A_UNDEF; f->argc = 0; f->f.binf = 0;
1479: if ( dot = strchr(name,'.') ) {
1480: /* undefined function in a module */
1481: len = dot-name;
1482: modname = (char *)MALLOC_ATOMIC(len+1);
1483: strncpy(modname,name,len); modname[len] = 0;
1484: fname = (char *)MALLOC_ATOMIC(strlen(name)-len+1);
1485: strcpy(fname,dot+1);
1486: f->name = fname;
1487: f->fullname = name;
1488: mod = searchmodule(modname);
1489: if ( !mod )
1490: mod = mkmodule(modname);
1491: MKNODE(tn,f,mod->usrf_list); mod->usrf_list = tn;
1492: } else {
1493: f->name = name;
1494: f->fullname = name;
1495: MKNODE(tn,f,usrf); usrf = tn;
1496: }
1497: *r = f;
1498: }
1499:
1500: void appenduf_local(char *name,FUNC *r)
1501: {
1502: NODE tn;
1503: FUNC f;
1504: MODULE mod;
1505:
1506: for ( tn = CUR_MODULE->usrf_list; tn; tn = NEXT(tn) )
1507: if ( !strcmp(((FUNC)BDY(tn))->name,name) )
1508: break;
1509: if ( tn )
1510: return;
1511:
1512: f=(FUNC)MALLOC(sizeof(struct oFUNC));
1513: f->id = A_UNDEF; f->argc = 0; f->f.binf = 0;
1514: f->name = name;
1515: f->fullname =
1516: (char *)MALLOC_ATOMIC(strlen(CUR_MODULE->name)+strlen(name)+1);
1517: sprintf(f->fullname,"%s.%s",CUR_MODULE->name,name);
1518: MKNODE(tn,f,CUR_MODULE->usrf_list); CUR_MODULE->usrf_list = tn;
1519: *r = f;
1520: }
1521:
1522: void appenduflist(NODE n)
1523: {
1524: NODE tn;
1525: FUNC f;
1526:
1527: for ( tn = n; tn; tn = NEXT(tn) )
1528: appenduf_local((char *)BDY(tn),&f);
1529: }
1530:
1531: void mkparif(char *name,FUNC *r)
1532: {
1533: FUNC f;
1534:
1535: *r = f =(FUNC)MALLOC(sizeof(struct oFUNC));
1536: f->name = name; f->id = A_PARI; f->argc = 0; f->f.binf = 0;
1537: f->fullname = name;
1538: }
1539:
1540: void mkuf(char *name,char *fname,NODE args,SNODE body,int startl,int endl,char *desc,MODULE module)
1541: {
1542: FUNC f;
1543: USRF t;
1544: NODE usrf_list,sn,tn;
1545: FNODE fn;
1546: char *longname;
1547: int argc;
1548:
1549: if ( getsecuremode() ) {
1550: error("defining function is not permitted in the secure mode");
1551: }
1552: if ( *name == ':' )
1553: name += 2;
1554: if ( !module ) {
1555: searchf(sysf,name,&f);
1556: if ( f ) {
1557: fprintf(stderr,"def : builtin function %s() cannot be redefined.\n",name);
1558: CPVS = GPVS; return;
1559: }
1560: }
1561: for ( argc = 0, sn = args; sn; argc++, sn = NEXT(sn) ) {
1562: fn = (FNODE)BDY(sn);
1563: if ( !fn || ID(fn) != I_PVAR ) {
1564: fprintf(stderr,"illegal argument in %s()\n",name);
1565: CPVS = GPVS; return;
1566: }
1567: }
1568: usrf_list = module ? module->usrf_list : usrf;
1569: for ( sn = usrf_list; sn && strcmp(NAME((FUNC)BDY(sn)),name); sn = NEXT(sn) );
1570: if ( sn )
1571: f = (FUNC)BDY(sn);
1572: else {
1573: f=(FUNC)MALLOC(sizeof(struct oFUNC));
1574: f->name = name;
1575: MKNODE(tn,f,usrf_list); usrf_list = tn;
1576: if ( module ) {
1577: f->fullname =
1578: (char *)MALLOC_ATOMIC(strlen(f->name)+strlen(module->name)+1);
1579: sprintf(f->fullname,"%s.%s",module->name,f->name);
1580: module->usrf_list = usrf_list;
1581: } else {
1582: f->fullname = f->name;
1583: usrf = usrf_list;
1584: }
1585: }
1586: if ( Verbose && f->id != A_UNDEF ) {
1587: if ( module )
1588: fprintf(stderr,"Warning : %s.%s() redefined.\n",module->name,name);
1589: else
1590: fprintf(stderr,"Warning : %s() redefined.\n",name);
1591: }
1592: t=(USRF)MALLOC(sizeof(struct oUSRF));
1593: t->args=args; BDY(t)=body; t->pvs = CPVS; t->fname = fname;
1594: t->startl = startl; t->endl = endl; t->module = module;
1595: t->desc = desc;
1596: f->id = A_USR; f->argc = argc; f->f.usrf = t;
1597: CPVS = GPVS;
1598: CUR_FUNC = 0;
1599: clearbp(f);
1600: }
1601:
1602: /*
1603: retrieve value of an option whose key matches 'key'
1604: CVS->opt is a list(node) of key-value pair (list)
1605: CVS->opt = BDY([[key,value],[key,value],...])
1606: */
1607:
1608: Obj getopt_from_cpvs(char *key)
1609: {
1610: NODE opts,opt;
1611: LIST r;
1612: extern Obj VOIDobj;
1613:
1614: opts = CPVS->opt;
1615: if ( !key ) {
1616: MKLIST(r,opts);
1617: return (Obj)r;
1618: } else {
1619: for ( ; opts; opts = NEXT(opts) ) {
1620: asir_assert(BDY(opts),O_LIST,"getopt_from_cvps");
1621: opt = BDY((LIST)BDY(opts));
1622: if ( !strcmp(key,BDY((STRING)BDY(opt))) )
1623: return (Obj)BDY(NEXT(opt));
1624: }
1625: return VOIDobj;
1626: }
1627:
1628: }
1629:
1630: MODULE mkmodule(char *name)
1631: {
1632: MODULE mod;
1633: NODE m;
1634: int len;
1635: VS mpvs;
1636:
1637: for ( m = MODULE_LIST; m; m = NEXT(m) ) {
1638: mod = (MODULE)m->body;
1639: if ( !strcmp(mod->name,name) )
1640: break;
1641: }
1642: if ( m )
1643: return mod;
1644: else {
1645: mod = (MODULE)MALLOC(sizeof(struct oMODULE));
1646: len = strlen(name);
1647: mod->name = (char *)MALLOC_ATOMIC(len+1);
1648: strcpy(mod->name,name);
1649: mod->pvs = mpvs = (VS)MALLOC(sizeof(struct oVS));
1650: asir_reallocarray((char **)&mpvs->va,(int *)&mpvs->asize,
1651: (int *)&mpvs->n,(int)sizeof(struct oPV));
1652: mod->usrf_list = 0;
1653: MKNODE(m,mod,MODULE_LIST);
1654: MODULE_LIST = m;
1655: return mod;
1656: }
1657: }
1658:
1659: void print_crossref(FUNC f)
1660: {
1661: FUNC r;
1662: if ( show_crossref && CUR_FUNC ) {
1663: searchuf(f->fullname,&r);
1664: if (r != NULL) {
1665: fprintf(asir_out,"%s() at line %d in %s()\n",
1666: f->fullname, asir_infile->ln, CUR_FUNC);
1667: }
1668: }
1669: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>