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