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