Annotation of OpenXM_contrib2/asir2018/builtin/print.c, Revision 1.3
1.1 noro 1: /*
2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
3: * All rights reserved.
4: *
5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
6: * non-exclusive and royalty-free license to use, copy, modify and
7: * redistribute, solely for non-commercial and non-profit purposes, the
8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
9: * conditions of this Agreement. For the avoidance of doubt, you acquire
10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
11: * third party developer retains all rights, including but not limited to
12: * copyrights, in and to the SOFTWARE.
13: *
14: * (1) FLL does not grant you a license in any way for commercial
15: * purposes. You may use the SOFTWARE only for non-commercial and
16: * non-profit purposes only, such as academic, research and internal
17: * business use.
18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
19: * international copyright treaties. If you make copies of the SOFTWARE,
20: * with or without modification, as permitted hereunder, you shall affix
21: * to all such copies of the SOFTWARE the above copyright notice.
22: * (3) An explicit reference to this SOFTWARE and its copyright owner
23: * shall be made on your publication or presentation in any form of the
24: * results obtained by use of the SOFTWARE.
25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
27: * for such modification or the source code of the modified part of the
28: * SOFTWARE.
29: *
30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
47: *
1.3 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2018/builtin/print.c,v 1.2 2018/09/28 08:20:27 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;
248: }
249: MKSTR(op,opname); break;
250:
251: case I_AND:
252: MKSTR(op,"&&"); break;
253:
254: case I_OR:
255: MKSTR(op,"||"); break;
256: }
257: n = mknode(4,head,op,a1,a2);
258: MKLIST(*rp,n);
259: break;
260:
261: case I_NARYOP:
262: /* head */
263: MKSTR(head,"n_op");
264: n = (NODE)FA1(f);
265: for ( t0 = 0; n; n = NEXT(n) ) {
266: NEXTNODE(t0,t);
1.3 ! noro 267: fnodetotree((FNODE)BDY(n),vs,&a1);
1.1 noro 268: BDY(t) = (pointer)a1;
269: }
270: MKSTR(op,((ARF)FA0(f))->name);
271: MKNODE(t,op,t0);
272: MKNODE(n,head,t);
273: MKLIST(*rp,n);
274: break;
275:
276: /* ternary operators */
277: case I_CE:
278: MKSTR(head,"t_op");
279: MKSTR(op,"?:");
1.3 ! noro 280: fnodetotree((FNODE)FA0(f),vs,&a1);
! 281: fnodetotree((FNODE)FA1(f),vs,&a2);
! 282: fnodetotree((FNODE)FA2(f),vs,&a3);
1.1 noro 283: n = mknode(5,head,op,a1,a2,a3);
284: MKLIST(*rp,n);
285: break;
286:
287: /* lists */
288: case I_LIST:
289: n = (NODE)FA0(f);
290: for ( t0 = 0; n; n = NEXT(n) ) {
291: NEXTNODE(t0,t);
1.3 ! noro 292: fnodetotree((FNODE)BDY(n),vs,&a1);
1.1 noro 293: BDY(t) = (pointer)a1;
294: }
295: if ( t0 )
296: NEXT(t) = 0;
297: MKSTR(head,"list");
298: MKNODE(n,head,t0);
299: MKLIST(*rp,n);
300: break;
301:
302: /* function */
303: case I_FUNC: case I_FUNC_QARG: case I_CAR: case I_CDR: case I_EV:
304: MKSTR(head,"function");
305: switch ( f->id ) {
306: case I_FUNC: case I_FUNC_QARG:
307: MKSTR(op,((FUNC)FA0(f))->fullname);
1.3 ! noro 308: fnodetotree((FNODE)FA1(f),vs,&a1);
1.1 noro 309: break;
310: case I_CAR:
311: MKSTR(op,"car");
1.3 ! noro 312: fnodetotree((FNODE)FA0(f),vs,&a1);
1.1 noro 313: break;
314: case I_CDR:
315: MKSTR(op,"cdr");
1.3 ! noro 316: fnodetotree((FNODE)FA0(f),vs,&a1);
1.1 noro 317: break;
318: case I_EV:
319: /* exponent vector; should be treated as function call */
320: MKSTR(op,"exponent_vector");
1.3 ! noro 321: fnodetotree(mkfnode(1,I_LIST,FA0(f)),vs,&a1);
1.1 noro 322: break;
323: }
324: t0 = NEXT(BDY(a1)); /* XXX : skip the headers */
325: MKNODE(t,op,t0);
326: MKNODE(n,head,t);
327: MKLIST(*rp,n);
328: break;
329:
330: case I_STR:
331: MKSTR(head,"internal");
332: MKSTR(str,FA0(f));
333: n = mknode(2,head,str);
334: MKLIST(*rp,n);
335: break;
336:
337: case I_FORMULA:
338: MKSTR(head,"internal");
339: n = mknode(2,head,FA0(f));
340: MKLIST(*rp,n);
341: break;
342:
343: case I_PVAR:
344: if ( FA1(f) )
345: error("fnodetotree : not implemented yet");
346: MKSTR(head,"variable");
1.3 ! noro 347: GETPVNAME2(FA0(f),opname,vs);
1.1 noro 348: MKSTR(op,opname);
349: n = mknode(2,head,op);
350: MKLIST(*rp,n);
351: break;
352:
353: default:
354: error("fnodetotree : not implemented yet");
355: }
356: }
357:
358: FNODE eval_pvar_in_fnode(FNODE f)
359: {
360: FNODE a1,a2,a3;
361: pointer r;
362: NODE n,t,t0;
363: QUOTE q;
364:
365: if ( !f )
366: return 0;
367:
368: switch ( f->id ) {
369: /* unary operators */
370: case I_NOT: case I_PAREN: case I_MINUS:
371: a1 = eval_pvar_in_fnode((FNODE)FA0(f));
372: return mkfnode(1,f->id,a1);
373:
374: /* binary operators */
375: case I_AND: case I_OR:
376: a1 = eval_pvar_in_fnode((FNODE)FA0(f));
377: a2 = eval_pvar_in_fnode((FNODE)FA1(f));
378: return mkfnode(3,f->id,a1,a2);
379:
380: case I_BOP: case I_COP: case I_LOP:
381: a1 = eval_pvar_in_fnode((FNODE)FA1(f));
382: a2 = eval_pvar_in_fnode((FNODE)FA2(f));
383: return mkfnode(4,f->id,FA0(f),a1,a2);
384:
385: /* ternary operators */
386: case I_CE:
387: a1 = eval_pvar_in_fnode((FNODE)FA0(f));
388: a2 = eval_pvar_in_fnode((FNODE)FA1(f));
389: a3 = eval_pvar_in_fnode((FNODE)FA2(f));
390: return mkfnode(5,f->id,a1,a2,a3);
391:
392: /* lists */
393: case I_LIST:
394: n = (NODE)FA0(f);
395: for ( t0 = 0; n; n = NEXT(n) ) {
396: NEXTNODE(t0,t);
397: BDY(t) = (pointer)eval_pvar_in_fnode(BDY(n));
398: }
399: if ( t0 )
400: NEXT(t) = 0;
401: return mkfnode(1,f->id,t0);
402:
403: /* function */
404: case I_FUNC:
405: a1 = eval_pvar_in_fnode((FNODE)FA1(f));
406: return mkfnode(2,f->id,FA0(f),a1);
407: break;
408: case I_CAR: case I_CDR:
409: a1 = eval_pvar_in_fnode((FNODE)FA0(f));
410: return mkfnode(1,f->id,a1);
411: case I_EV:
412: /* exponent vector */
413: a1 = eval_pvar_in_fnode(mkfnode(1,I_LIST,FA0(f)));
414: return mkfnode(1,f->id,a1);
415:
416: case I_STR: case I_FORMULA:
417: return f;
418:
419: case I_PVAR: case I_INDEX:
420: case I_POSTSELF: case I_PRESELF:
421: r = eval(f);
422: objtoquote(r,&q);
423: return BDY(q);
424:
425: default:
426: error("eval_pvar_in_fnode : not implemented yet");
427: /* NOTREACHED */
428: return 0;
429: }
430: }
431:
432: FNODE subst_in_fnode(FNODE f,V v,FNODE g)
433: {
434: FNODE a1,a2,a3;
435: DCP dc;
436: V vf;
437: NODE n,t,t0;
438: Obj obj;
439:
440: if ( !f )
441: return 0;
442:
443: switch ( f->id ) {
444: /* unary operators */
445: case I_NOT: case I_PAREN: case I_MINUS:
446: a1 = subst_in_fnode((FNODE)FA0(f),v,g);
447: return mkfnode(1,f->id,a1);
448:
449: /* binary operators */
450: case I_AND: case I_OR:
451: a1 = subst_in_fnode((FNODE)FA0(f),v,g);
452: a2 = subst_in_fnode((FNODE)FA1(f),v,g);
453: return mkfnode(3,f->id,a1,a2);
454:
455: case I_BOP: case I_COP: case I_LOP:
456: a1 = subst_in_fnode((FNODE)FA1(f),v,g);
457: a2 = subst_in_fnode((FNODE)FA2(f),v,g);
458: return mkfnode(4,f->id,FA0(f),a1,a2);
459:
460: /* ternary operators */
461: case I_CE:
462: a1 = subst_in_fnode((FNODE)FA0(f),v,g);
463: a2 = subst_in_fnode((FNODE)FA1(f),v,g);
464: a3 = subst_in_fnode((FNODE)FA2(f),v,g);
465: return mkfnode(5,f->id,a1,a2,a3);
466:
467: /* lists */
468: case I_LIST:
469: n = (NODE)FA0(f);
470: for ( t0 = 0; n; n = NEXT(n) ) {
471: NEXTNODE(t0,t);
472: BDY(t) = (pointer)subst_in_fnode(BDY(n),v,g);
473: }
474: if ( t0 )
475: NEXT(t) = 0;
476: return mkfnode(1,f->id,t0);
477:
478: /* function */
479: case I_FUNC:
480: a1 = subst_in_fnode((FNODE)FA1(f),v,g);
481: return mkfnode(2,f->id,FA0(f),a1);
482: break;
483: case I_CAR: case I_CDR:
484: a1 = subst_in_fnode((FNODE)FA0(f),v,g);
485: return mkfnode(1,f->id,a1);
486: case I_EV:
487: /* exponent vector */
488: a1 = subst_in_fnode(mkfnode(1,I_LIST,FA0(f)),v,g);
489: return mkfnode(1,f->id,a1);
490:
491: case I_STR:
492: return f;
493:
494: case I_FORMULA:
495: obj = (Obj)FA0(f);
496: if ( !obj )
497: return f;
498:
499: switch ( OID(obj) ) {
500: case O_N:
501: return f;
502: case O_P:
503: vf = VR((P)obj);
504: dc = DC((P)obj);
505: if ( vf != v )
506: return f;
507: else if ( UNIQ(DEG(dc)) && UNIQ((Q)COEF(dc)) )
508: return g;
509: else break;
510: default:
511: break;
512: }
513:
514: default:
515: error("subst_in_fnode : not implemented yet");
516: /* NOTREACHED */
517: return 0;
518: }
519: }
520:
521: /* not completed yet */
522:
523: #if 0
524: char *get_attribute(char *key,LIST attr)
525: {}
526:
527: void treetofnode(Obj obj,FNODE *f)
528: {
529: NODE n;
530: LIST attr;
531: char *prop;
532:
533: if ( obj || OID(obj) != O_LIST ) {
534: /* internal object */
535: *f = mkfnode(1,I_FORMULA,obj);
536: } else {
537: /* [attr(list),name(string),args(node)] */
538: n = BDY((LIST)obj);
539: attr = (LIST)BDY(n); n = NEXT(n);
540: prop = get_attribute("asir",attr);
541: if ( !strcmp(prop,"u_op") ) {
542: } else if ( !strcmp(prop,"b_op") ) {
543: } else if ( !strcmp(prop,"t_op") ) {
544: } else if ( !strcmp(prop,"function") ) {
545: }
546: /* default will be set to P_FUNC */
547: }
548: }
549: #endif
550:
551: FUNC user_print_function;
552:
553: void Pset_print_function(NODE arg,pointer *rp)
554: {
555: if ( !arg )
556: user_print_function = 0;
557: else {
558: gen_searchf(BDY((STRING)ARG0(arg)),&user_print_function);
559: }
560: *rp = 0;
561: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>