Annotation of OpenXM_contrib2/asir2018/builtin/print.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 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2018/builtin/print.c,v 1.3 2020/02/22 06:23:35 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;
167: NODE n,t,t0;
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:
332: case I_STR:
333: MKSTR(head,"internal");
334: MKSTR(str,FA0(f));
335: n = mknode(2,head,str);
336: MKLIST(*rp,n);
337: break;
338:
339: case I_FORMULA:
340: MKSTR(head,"internal");
341: n = mknode(2,head,FA0(f));
342: MKLIST(*rp,n);
343: break;
344:
345: case I_PVAR:
346: if ( FA1(f) )
347: error("fnodetotree : not implemented yet");
348: MKSTR(head,"variable");
1.3 noro 349: GETPVNAME2(FA0(f),opname,vs);
1.1 noro 350: MKSTR(op,opname);
351: n = mknode(2,head,op);
352: MKLIST(*rp,n);
353: break;
354:
355: default:
356: error("fnodetotree : not implemented yet");
357: }
358: }
359:
360: FNODE eval_pvar_in_fnode(FNODE f)
361: {
362: FNODE a1,a2,a3;
363: pointer r;
364: NODE n,t,t0;
365: QUOTE q;
366:
367: if ( !f )
368: return 0;
369:
370: switch ( f->id ) {
371: /* unary operators */
372: case I_NOT: case I_PAREN: case I_MINUS:
373: a1 = eval_pvar_in_fnode((FNODE)FA0(f));
374: return mkfnode(1,f->id,a1);
375:
376: /* binary operators */
377: case I_AND: case I_OR:
378: a1 = eval_pvar_in_fnode((FNODE)FA0(f));
379: a2 = eval_pvar_in_fnode((FNODE)FA1(f));
380: return mkfnode(3,f->id,a1,a2);
381:
382: case I_BOP: case I_COP: case I_LOP:
383: a1 = eval_pvar_in_fnode((FNODE)FA1(f));
384: a2 = eval_pvar_in_fnode((FNODE)FA2(f));
385: return mkfnode(4,f->id,FA0(f),a1,a2);
386:
387: /* ternary operators */
388: case I_CE:
389: a1 = eval_pvar_in_fnode((FNODE)FA0(f));
390: a2 = eval_pvar_in_fnode((FNODE)FA1(f));
391: a3 = eval_pvar_in_fnode((FNODE)FA2(f));
392: return mkfnode(5,f->id,a1,a2,a3);
393:
394: /* lists */
395: case I_LIST:
396: n = (NODE)FA0(f);
397: for ( t0 = 0; n; n = NEXT(n) ) {
398: NEXTNODE(t0,t);
399: BDY(t) = (pointer)eval_pvar_in_fnode(BDY(n));
400: }
401: if ( t0 )
402: NEXT(t) = 0;
403: return mkfnode(1,f->id,t0);
404:
405: /* function */
406: case I_FUNC:
407: a1 = eval_pvar_in_fnode((FNODE)FA1(f));
408: return mkfnode(2,f->id,FA0(f),a1);
409: break;
410: case I_CAR: case I_CDR:
411: a1 = eval_pvar_in_fnode((FNODE)FA0(f));
412: return mkfnode(1,f->id,a1);
413: case I_EV:
414: /* exponent vector */
415: a1 = eval_pvar_in_fnode(mkfnode(1,I_LIST,FA0(f)));
416: return mkfnode(1,f->id,a1);
417:
418: case I_STR: case I_FORMULA:
419: return f;
420:
421: case I_PVAR: case I_INDEX:
422: case I_POSTSELF: case I_PRESELF:
423: r = eval(f);
424: objtoquote(r,&q);
425: return BDY(q);
426:
427: default:
428: error("eval_pvar_in_fnode : not implemented yet");
429: /* NOTREACHED */
430: return 0;
431: }
432: }
433:
434: FNODE subst_in_fnode(FNODE f,V v,FNODE g)
435: {
436: FNODE a1,a2,a3;
437: DCP dc;
438: V vf;
439: NODE n,t,t0;
440: Obj obj;
441:
442: if ( !f )
443: return 0;
444:
445: switch ( f->id ) {
446: /* unary operators */
447: case I_NOT: case I_PAREN: case I_MINUS:
448: a1 = subst_in_fnode((FNODE)FA0(f),v,g);
449: return mkfnode(1,f->id,a1);
450:
451: /* binary operators */
452: case I_AND: case I_OR:
453: a1 = subst_in_fnode((FNODE)FA0(f),v,g);
454: a2 = subst_in_fnode((FNODE)FA1(f),v,g);
455: return mkfnode(3,f->id,a1,a2);
456:
457: case I_BOP: case I_COP: case I_LOP:
458: a1 = subst_in_fnode((FNODE)FA1(f),v,g);
459: a2 = subst_in_fnode((FNODE)FA2(f),v,g);
460: return mkfnode(4,f->id,FA0(f),a1,a2);
461:
462: /* ternary operators */
463: case I_CE:
464: a1 = subst_in_fnode((FNODE)FA0(f),v,g);
465: a2 = subst_in_fnode((FNODE)FA1(f),v,g);
466: a3 = subst_in_fnode((FNODE)FA2(f),v,g);
467: return mkfnode(5,f->id,a1,a2,a3);
468:
469: /* lists */
470: case I_LIST:
471: n = (NODE)FA0(f);
472: for ( t0 = 0; n; n = NEXT(n) ) {
473: NEXTNODE(t0,t);
474: BDY(t) = (pointer)subst_in_fnode(BDY(n),v,g);
475: }
476: if ( t0 )
477: NEXT(t) = 0;
478: return mkfnode(1,f->id,t0);
479:
480: /* function */
481: case I_FUNC:
482: a1 = subst_in_fnode((FNODE)FA1(f),v,g);
483: return mkfnode(2,f->id,FA0(f),a1);
484: break;
485: case I_CAR: case I_CDR:
486: a1 = subst_in_fnode((FNODE)FA0(f),v,g);
487: return mkfnode(1,f->id,a1);
488: case I_EV:
489: /* exponent vector */
490: a1 = subst_in_fnode(mkfnode(1,I_LIST,FA0(f)),v,g);
491: return mkfnode(1,f->id,a1);
492:
493: case I_STR:
494: return f;
495:
496: case I_FORMULA:
497: obj = (Obj)FA0(f);
498: if ( !obj )
499: return f;
500:
501: switch ( OID(obj) ) {
502: case O_N:
503: return f;
504: case O_P:
505: vf = VR((P)obj);
506: dc = DC((P)obj);
507: if ( vf != v )
508: return f;
509: else if ( UNIQ(DEG(dc)) && UNIQ((Q)COEF(dc)) )
510: return g;
511: else break;
512: default:
513: break;
514: }
515:
516: default:
517: error("subst_in_fnode : not implemented yet");
518: /* NOTREACHED */
519: return 0;
520: }
521: }
522:
523: /* not completed yet */
524:
525: #if 0
526: char *get_attribute(char *key,LIST attr)
527: {}
528:
529: void treetofnode(Obj obj,FNODE *f)
530: {
531: NODE n;
532: LIST attr;
533: char *prop;
534:
535: if ( obj || OID(obj) != O_LIST ) {
536: /* internal object */
537: *f = mkfnode(1,I_FORMULA,obj);
538: } else {
539: /* [attr(list),name(string),args(node)] */
540: n = BDY((LIST)obj);
541: attr = (LIST)BDY(n); n = NEXT(n);
542: prop = get_attribute("asir",attr);
543: if ( !strcmp(prop,"u_op") ) {
544: } else if ( !strcmp(prop,"b_op") ) {
545: } else if ( !strcmp(prop,"t_op") ) {
546: } else if ( !strcmp(prop,"function") ) {
547: }
548: /* default will be set to P_FUNC */
549: }
550: }
551: #endif
552:
553: FUNC user_print_function;
554:
555: void Pset_print_function(NODE arg,pointer *rp)
556: {
557: if ( !arg )
558: user_print_function = 0;
559: else {
560: gen_searchf(BDY((STRING)ARG0(arg)),&user_print_function);
561: }
562: *rp = 0;
563: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>