Annotation of OpenXM_contrib2/asir2018/builtin/print.c, Revision 1.6
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.6 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2018/builtin/print.c,v 1.5 2021/03/11 03:41:13 noro Exp $
1.1 noro 49: */
50: #include "ca.h"
51: #include "parse.h"
52:
53: void Psprintf(NODE,STRING *);
54:
55: void Pprintf();
56: void Pprint();
57: void Pquotetolist();
58: void Pobjtoquote();
59: void Peval_variables_in_quote();
60: void Pset_print_function();
61:
62: struct ftab print_tab[] = {
63: {"printf",Pprintf,-99999999},
64: {"print",Pprint,-2},
65: {"objtoquote",Pobjtoquote,1},
66: {"quotetolist",Pquotetolist,1},
67: {"eval_variables_in_quote",Peval_variables_in_quote,1},
68: {"set_print_function",Pset_print_function,-1},
69: {0,0,0},
70: };
71:
72: extern int I_am_server;
73:
74: int wfep_mode;
75:
76: void Pprintf(NODE arg,pointer *rp)
77: {
78: STRING s;
79: if ( arg ) {
80: Psprintf(arg,&s);
81: /* engine for wfep */
82: if ( wfep_mode ) {
83: print_to_wfep((Obj)s);
84: }else {
85: printexpr(CO,(Obj)s);
86: fflush(asir_out);
87: }
88: }
89: *rp = 0;
90: return;
91: }
92:
93: void Pprint(NODE arg,pointer *rp)
94: {
95: Obj obj;
96: STRING nl;
97: Q opt;
98:
99: /* engine for wfep */
100: if ( wfep_mode ) {
101: if ( arg ) {
102: print_to_wfep((Obj)ARG0(arg));
103: if ( !NEXT(arg) || ARG1(arg) ) {
104: MKSTR(nl,"\r\n");
105: print_to_wfep((Obj)nl);
106: }
107: }
108: *rp = 0;
109: return;
110: }
111: if ( arg ) {
112: obj = (Obj)ARG0(arg);
113: if ( NEXT(arg) ) {
114: opt = (Q)ARG1(arg);
115: if ( INT(opt) ) {
116: printexpr(CO,obj);
1.2 noro 117: switch ( ZTOS(opt) ) {
1.1 noro 118: case 0:
119: break;
120: case 2:
121: fflush(asir_out); break;
122: break;
123: case 1: default:
124: putc('\n',asir_out); break;
125: }
126: } else
127: error("print : invalid argument");
128: } else {
129: printexpr(CO,obj);
130: putc('\n',asir_out);
131: }
132: }
133: /* XXX : if ox_asir, we have to fflush always */
134: if ( I_am_server )
135: fflush(asir_out);
136: *rp = 0;
137: }
138:
139: void Pobjtoquote(NODE arg,QUOTE *rp)
140: {
141: objtoquote(ARG0(arg),rp);
142: }
143:
144: void Pquotetolist(NODE arg,LIST *rp)
145: {
1.3 noro 146: QUOTE q;
147:
1.1 noro 148: asir_assert(ARG0(arg),O_QUOTE,"quotetolist");
1.3 noro 149: q = (QUOTE)ARG0(arg);
150: fnodetotree((FNODE)BDY(q),q->pvs,rp);
1.1 noro 151: }
152:
153: void Peval_variables_in_quote(NODE arg,QUOTE *rp)
154: {
155: FNODE fn;
156:
157: asir_assert(ARG0(arg),O_QUOTE,"eval_variables_in_quote");
158: fn = eval_pvar_in_fnode((FNODE)BDY((QUOTE)(ARG0(arg))));
159: MKQUOTE(*rp,fn);
160: }
161:
162: /* fnode -> [tag,name,arg0,arg1,...] */
163:
1.3 noro 164: void fnodetotree(FNODE f,VS vs,LIST *rp)
1.1 noro 165: {
166: LIST a1,a2,a3;
1.5 noro 167: NODE n,t,t0,t1;
1.1 noro 168: STRING head,op,str;
169: char *opname;
170:
171: if ( !f ) {
172: MKSTR(head,"internal");
173: n = mknode(2,head,NULLP);
174: MKLIST(*rp,n);
175: return;
176: }
177: switch ( f->id ) {
178: /* unary operators */
179: case I_NOT: case I_PAREN: case I_MINUS:
180: MKSTR(head,"u_op");
181: switch ( f->id ) {
182: case I_NOT:
183: MKSTR(op,"!");
184: break;
185: case I_PAREN:
186: MKSTR(op,"()");
187: break;
188: case I_MINUS:
189: MKSTR(op,"-");
190: break;
191: }
1.3 noro 192: fnodetotree((FNODE)FA0(f),vs,&a1);
1.1 noro 193: n = mknode(3,head,op,a1);
194: MKLIST(*rp,n);
195: break;
196:
197: /* binary operators */
198: case I_BOP: case I_COP: case I_LOP: case I_AND: case I_OR:
199: /* head */
200: MKSTR(head,"b_op");
201:
202: /* arg list */
203: switch ( f->id ) {
204: case I_AND: case I_OR:
1.3 noro 205: fnodetotree((FNODE)FA0(f),vs,&a1);
206: fnodetotree((FNODE)FA1(f),vs,&a2);
1.1 noro 207: break;
208: default:
1.3 noro 209: fnodetotree((FNODE)FA1(f),vs,&a1);
210: fnodetotree((FNODE)FA2(f),vs,&a2);
1.1 noro 211: break;
212: }
213:
214: /* op */
215: switch ( f->id ) {
216: case I_BOP:
217: MKSTR(op,((ARF)FA0(f))->name); break;
218:
219: case I_COP:
220: switch( (cid)FA0(f) ) {
221: case C_EQ: opname = "=="; break;
222: case C_NE: opname = "!="; break;
223: case C_GT: opname = ">"; break;
224: case C_LT: opname = "<"; break;
225: case C_GE: opname = ">="; break;
226: case C_LE: opname = "<="; break;
227: }
228: MKSTR(op,opname); break;
229:
230: case I_LOP:
231: switch( (lid)FA0(f) ) {
232: case L_EQ: opname = "@=="; break;
233: case L_NE: opname = "@!="; break;
234: case L_GT: opname = "@>"; break;
235: case L_LT: opname = "@<"; break;
236: case L_GE: opname = "@>="; break;
237: case L_LE: opname = "@<="; break;
238: case L_AND: opname = "@&&"; break;
239: case L_OR: opname = "@||"; break;
240:
241: case L_NOT: opname = "@!";
242: /* XXX : L_NOT is a unary operator */
243: MKSTR(head,"u_op");
244: MKSTR(op,opname);
245: n = mknode(3,head,op,a1);
246: MKLIST(*rp,n);
247: return;
1.4 noro 248: default:
249: return;
1.1 noro 250: }
251: MKSTR(op,opname); break;
252:
253: case I_AND:
254: MKSTR(op,"&&"); break;
255:
256: case I_OR:
257: MKSTR(op,"||"); break;
258: }
259: n = mknode(4,head,op,a1,a2);
260: MKLIST(*rp,n);
261: break;
262:
263: case I_NARYOP:
264: /* head */
265: MKSTR(head,"n_op");
266: n = (NODE)FA1(f);
267: for ( t0 = 0; n; n = NEXT(n) ) {
268: NEXTNODE(t0,t);
1.3 noro 269: fnodetotree((FNODE)BDY(n),vs,&a1);
1.1 noro 270: BDY(t) = (pointer)a1;
271: }
272: MKSTR(op,((ARF)FA0(f))->name);
273: MKNODE(t,op,t0);
274: MKNODE(n,head,t);
275: MKLIST(*rp,n);
276: break;
277:
278: /* ternary operators */
279: case I_CE:
280: MKSTR(head,"t_op");
281: MKSTR(op,"?:");
1.3 noro 282: fnodetotree((FNODE)FA0(f),vs,&a1);
283: fnodetotree((FNODE)FA1(f),vs,&a2);
284: fnodetotree((FNODE)FA2(f),vs,&a3);
1.1 noro 285: n = mknode(5,head,op,a1,a2,a3);
286: MKLIST(*rp,n);
287: break;
288:
289: /* lists */
290: case I_LIST:
291: n = (NODE)FA0(f);
292: for ( t0 = 0; n; n = NEXT(n) ) {
293: NEXTNODE(t0,t);
1.3 noro 294: fnodetotree((FNODE)BDY(n),vs,&a1);
1.1 noro 295: BDY(t) = (pointer)a1;
296: }
297: if ( t0 )
298: NEXT(t) = 0;
299: MKSTR(head,"list");
300: MKNODE(n,head,t0);
301: MKLIST(*rp,n);
302: break;
303:
304: /* function */
305: case I_FUNC: case I_FUNC_QARG: case I_CAR: case I_CDR: case I_EV:
306: MKSTR(head,"function");
307: switch ( f->id ) {
308: case I_FUNC: case I_FUNC_QARG:
309: MKSTR(op,((FUNC)FA0(f))->fullname);
1.3 noro 310: fnodetotree((FNODE)FA1(f),vs,&a1);
1.1 noro 311: break;
312: case I_CAR:
313: MKSTR(op,"car");
1.3 noro 314: fnodetotree((FNODE)FA0(f),vs,&a1);
1.1 noro 315: break;
316: case I_CDR:
317: MKSTR(op,"cdr");
1.3 noro 318: fnodetotree((FNODE)FA0(f),vs,&a1);
1.1 noro 319: break;
320: case I_EV:
321: /* exponent vector; should be treated as function call */
322: MKSTR(op,"exponent_vector");
1.3 noro 323: fnodetotree(mkfnode(1,I_LIST,FA0(f)),vs,&a1);
1.1 noro 324: break;
325: }
326: t0 = NEXT(BDY(a1)); /* XXX : skip the headers */
327: MKNODE(t,op,t0);
328: MKNODE(n,head,t);
329: MKLIST(*rp,n);
330: break;
331:
1.5 noro 332: /* partial derivative of function */
333: case I_PFDERIV:
334: MKSTR(head,"derivative");
335: MKSTR(op,((FUNC)FA0(f))->fullname);
336: fnodetotree((FNODE)FA1(f),vs,&a1);
337: fnodetotree((FNODE)FA2(f),vs,&a2);
338: n = mknode(4,head,op,a1,a2);
339: MKLIST(*rp,n);
340: break;
341:
1.1 noro 342: case I_STR:
343: MKSTR(head,"internal");
344: MKSTR(str,FA0(f));
345: n = mknode(2,head,str);
346: MKLIST(*rp,n);
347: break;
348:
349: case I_FORMULA:
350: MKSTR(head,"internal");
351: n = mknode(2,head,FA0(f));
352: MKLIST(*rp,n);
353: break;
354:
355: case I_PVAR:
356: if ( FA1(f) )
357: error("fnodetotree : not implemented yet");
358: MKSTR(head,"variable");
1.3 noro 359: GETPVNAME2(FA0(f),opname,vs);
1.1 noro 360: MKSTR(op,opname);
361: n = mknode(2,head,op);
362: MKLIST(*rp,n);
363: break;
364:
365: default:
366: error("fnodetotree : not implemented yet");
367: }
368: }
369:
370: FNODE eval_pvar_in_fnode(FNODE f)
371: {
372: FNODE a1,a2,a3;
373: pointer r;
374: NODE n,t,t0;
375: QUOTE q;
376:
377: if ( !f )
378: return 0;
379:
380: switch ( f->id ) {
381: /* unary operators */
382: case I_NOT: case I_PAREN: case I_MINUS:
383: a1 = eval_pvar_in_fnode((FNODE)FA0(f));
384: return mkfnode(1,f->id,a1);
385:
386: /* binary operators */
387: case I_AND: case I_OR:
388: a1 = eval_pvar_in_fnode((FNODE)FA0(f));
389: a2 = eval_pvar_in_fnode((FNODE)FA1(f));
390: return mkfnode(3,f->id,a1,a2);
391:
392: case I_BOP: case I_COP: case I_LOP:
393: a1 = eval_pvar_in_fnode((FNODE)FA1(f));
394: a2 = eval_pvar_in_fnode((FNODE)FA2(f));
395: return mkfnode(4,f->id,FA0(f),a1,a2);
396:
397: /* ternary operators */
398: case I_CE:
399: a1 = eval_pvar_in_fnode((FNODE)FA0(f));
400: a2 = eval_pvar_in_fnode((FNODE)FA1(f));
401: a3 = eval_pvar_in_fnode((FNODE)FA2(f));
402: return mkfnode(5,f->id,a1,a2,a3);
403:
404: /* lists */
405: case I_LIST:
406: n = (NODE)FA0(f);
407: for ( t0 = 0; n; n = NEXT(n) ) {
408: NEXTNODE(t0,t);
409: BDY(t) = (pointer)eval_pvar_in_fnode(BDY(n));
410: }
411: if ( t0 )
412: NEXT(t) = 0;
413: return mkfnode(1,f->id,t0);
414:
415: /* function */
416: case I_FUNC:
417: a1 = eval_pvar_in_fnode((FNODE)FA1(f));
418: return mkfnode(2,f->id,FA0(f),a1);
419: break;
420: case I_CAR: case I_CDR:
421: a1 = eval_pvar_in_fnode((FNODE)FA0(f));
422: return mkfnode(1,f->id,a1);
423: case I_EV:
424: /* exponent vector */
425: a1 = eval_pvar_in_fnode(mkfnode(1,I_LIST,FA0(f)));
426: return mkfnode(1,f->id,a1);
427:
428: case I_STR: case I_FORMULA:
429: return f;
430:
431: case I_PVAR: case I_INDEX:
432: case I_POSTSELF: case I_PRESELF:
433: r = eval(f);
434: objtoquote(r,&q);
435: return BDY(q);
436:
437: default:
438: error("eval_pvar_in_fnode : not implemented yet");
439: /* NOTREACHED */
440: return 0;
441: }
442: }
443:
444: FNODE subst_in_fnode(FNODE f,V v,FNODE g)
445: {
446: FNODE a1,a2,a3;
447: DCP dc;
448: V vf;
449: NODE n,t,t0;
450: Obj obj;
451:
452: if ( !f )
453: return 0;
454:
455: switch ( f->id ) {
456: /* unary operators */
457: case I_NOT: case I_PAREN: case I_MINUS:
458: a1 = subst_in_fnode((FNODE)FA0(f),v,g);
459: return mkfnode(1,f->id,a1);
460:
461: /* binary operators */
462: case I_AND: case I_OR:
463: a1 = subst_in_fnode((FNODE)FA0(f),v,g);
464: a2 = subst_in_fnode((FNODE)FA1(f),v,g);
465: return mkfnode(3,f->id,a1,a2);
466:
467: case I_BOP: case I_COP: case I_LOP:
468: a1 = subst_in_fnode((FNODE)FA1(f),v,g);
469: a2 = subst_in_fnode((FNODE)FA2(f),v,g);
470: return mkfnode(4,f->id,FA0(f),a1,a2);
471:
472: /* ternary operators */
473: case I_CE:
474: a1 = subst_in_fnode((FNODE)FA0(f),v,g);
475: a2 = subst_in_fnode((FNODE)FA1(f),v,g);
476: a3 = subst_in_fnode((FNODE)FA2(f),v,g);
477: return mkfnode(5,f->id,a1,a2,a3);
478:
479: /* lists */
480: case I_LIST:
481: n = (NODE)FA0(f);
482: for ( t0 = 0; n; n = NEXT(n) ) {
483: NEXTNODE(t0,t);
484: BDY(t) = (pointer)subst_in_fnode(BDY(n),v,g);
485: }
486: if ( t0 )
487: NEXT(t) = 0;
488: return mkfnode(1,f->id,t0);
489:
490: /* function */
491: case I_FUNC:
492: a1 = subst_in_fnode((FNODE)FA1(f),v,g);
493: return mkfnode(2,f->id,FA0(f),a1);
494: break;
1.6 ! noro 495: /* derivative */
! 496: case I_PFDERIV:
! 497: a1 = subst_in_fnode((FNODE)FA1(f),v,g);
! 498: return mkfnode(3,f->id,FA0(f),a1,FA2(f));
! 499: break;
1.1 noro 500: case I_CAR: case I_CDR:
501: a1 = subst_in_fnode((FNODE)FA0(f),v,g);
502: return mkfnode(1,f->id,a1);
503: case I_EV:
504: /* exponent vector */
505: a1 = subst_in_fnode(mkfnode(1,I_LIST,FA0(f)),v,g);
506: return mkfnode(1,f->id,a1);
507:
508: case I_STR:
509: return f;
510:
511: case I_FORMULA:
512: obj = (Obj)FA0(f);
513: if ( !obj )
514: return f;
515:
516: switch ( OID(obj) ) {
517: case O_N:
518: return f;
519: case O_P:
520: vf = VR((P)obj);
521: dc = DC((P)obj);
522: if ( vf != v )
523: return f;
524: else if ( UNIQ(DEG(dc)) && UNIQ((Q)COEF(dc)) )
525: return g;
526: else break;
527: default:
528: break;
529: }
530:
531: default:
532: error("subst_in_fnode : not implemented yet");
533: /* NOTREACHED */
534: return 0;
535: }
536: }
537:
538: /* not completed yet */
539:
540: #if 0
541: char *get_attribute(char *key,LIST attr)
542: {}
543:
544: void treetofnode(Obj obj,FNODE *f)
545: {
546: NODE n;
547: LIST attr;
548: char *prop;
549:
550: if ( obj || OID(obj) != O_LIST ) {
551: /* internal object */
552: *f = mkfnode(1,I_FORMULA,obj);
553: } else {
554: /* [attr(list),name(string),args(node)] */
555: n = BDY((LIST)obj);
556: attr = (LIST)BDY(n); n = NEXT(n);
557: prop = get_attribute("asir",attr);
558: if ( !strcmp(prop,"u_op") ) {
559: } else if ( !strcmp(prop,"b_op") ) {
560: } else if ( !strcmp(prop,"t_op") ) {
561: } else if ( !strcmp(prop,"function") ) {
562: }
563: /* default will be set to P_FUNC */
564: }
565: }
566: #endif
567:
568: FUNC user_print_function;
569:
570: void Pset_print_function(NODE arg,pointer *rp)
571: {
572: if ( !arg )
573: user_print_function = 0;
574: else {
575: gen_searchf(BDY((STRING)ARG0(arg)),&user_print_function);
576: }
577: *rp = 0;
578: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>