Annotation of OpenXM_contrib2/asir2000/builtin/print.c, Revision 1.18
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.18 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/builtin/print.c,v 1.17 2004/05/14 09:20:56 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.14 noro 214: MKLIST(*rp,n);
1.4 noro 215: break;
1.7 noro 216:
217: /* ternary operators */
1.4 noro 218: case I_CE:
1.7 noro 219: MKSTR(head,"t_op");
220: MKSTR(op,"?:");
1.4 noro 221: fnodetotree((FNODE)FA0(f),&a1);
222: fnodetotree((FNODE)FA1(f),&a2);
1.7 noro 223: fnodetotree((FNODE)FA2(f),&a3);
224: n = mknode(5,head,op,a1,a2,a3);
1.14 noro 225: MKLIST(*rp,n);
1.4 noro 226: break;
1.7 noro 227:
228: /* lists */
1.8 noro 229: case I_LIST:
1.4 noro 230: n = (NODE)FA0(f);
231: for ( t0 = 0; n; n = NEXT(n) ) {
232: NEXTNODE(t0,t);
1.14 noro 233: fnodetotree((FNODE)BDY(n),&a1);
234: BDY(t) = (pointer)a1;
1.4 noro 235: }
236: if ( t0 )
237: NEXT(t) = 0;
1.8 noro 238: MKSTR(head,"list");
1.7 noro 239: MKNODE(n,head,t0);
1.14 noro 240: MKLIST(*rp,n);
1.4 noro 241: break;
1.7 noro 242:
243: /* function */
1.8 noro 244: case I_FUNC: case I_CAR: case I_CDR: case I_EV:
1.7 noro 245: MKSTR(head,"function");
246: switch ( f->id ) {
247: case I_FUNC:
1.18 ! noro 248: MKSTR(op,((FUNC)FA0(f))->fullname);
1.14 noro 249: fnodetotree((FNODE)FA1(f),&a1);
1.7 noro 250: break;
251: case I_CAR:
252: MKSTR(op,"car");
1.14 noro 253: fnodetotree((FNODE)FA0(f),&a1);
1.7 noro 254: break;
255: case I_CDR:
256: MKSTR(op,"cdr");
1.14 noro 257: fnodetotree((FNODE)FA0(f),&a1);
1.7 noro 258: break;
1.8 noro 259: case I_EV:
260: /* exponent vector; should be treated as function call */
261: MKSTR(op,"exponent_vector");
1.14 noro 262: fnodetotree(mkfnode(1,I_LIST,FA0(f)),&a1);
1.8 noro 263: break;
1.7 noro 264: }
1.14 noro 265: t0 = NEXT(BDY(a1)); /* XXX : skip the headers */
1.7 noro 266: MKNODE(t,op,t0);
267: MKNODE(n,head,t);
1.14 noro 268: MKLIST(*rp,n);
1.4 noro 269: break;
1.7 noro 270:
271: case I_STR:
272: MKSTR(head,"internal");
273: MKSTR(str,FA0(f));
274: n = mknode(2,head,str);
1.14 noro 275: MKLIST(*rp,n);
1.4 noro 276: break;
1.7 noro 277:
278: case I_FORMULA:
279: MKSTR(head,"internal");
280: n = mknode(2,head,FA0(f));
1.14 noro 281: MKLIST(*rp,n);
1.4 noro 282: break;
1.9 noro 283:
284: case I_PVAR:
1.10 noro 285: if ( FA1(f) )
286: error("fnodetotree : not implemented yet");
1.9 noro 287: MKSTR(head,"variable");
288: GETPVNAME(FA0(f),opname);
289: MKSTR(op,opname);
290: n = mknode(2,head,op);
1.14 noro 291: MKLIST(*rp,n);
1.9 noro 292: break;
293:
1.4 noro 294: default:
295: error("fnodetotree : not implemented yet");
1.9 noro 296: }
297: }
298:
1.14 noro 299: FNODE eval_pvar_in_fnode(FNODE f)
1.9 noro 300: {
301: FNODE a1,a2,a3;
302: pointer r;
303: NODE n,t,t0;
1.10 noro 304: QUOTE q;
1.9 noro 305:
306: if ( !f )
307: return 0;
308:
309: switch ( f->id ) {
310: /* unary operators */
1.13 noro 311: case I_NOT: case I_PAREN: case I_MINUS:
1.9 noro 312: a1 = eval_pvar_in_fnode((FNODE)FA0(f));
313: return mkfnode(1,f->id,a1);
314:
315: /* binary operators */
316: case I_AND: case I_OR:
317: a1 = eval_pvar_in_fnode((FNODE)FA0(f));
318: a2 = eval_pvar_in_fnode((FNODE)FA1(f));
319: return mkfnode(3,f->id,a1,a2);
320:
321: case I_BOP: case I_COP: case I_LOP:
322: a1 = eval_pvar_in_fnode((FNODE)FA1(f));
323: a2 = eval_pvar_in_fnode((FNODE)FA2(f));
324: return mkfnode(4,f->id,FA0(f),a1,a2);
325:
326: /* ternary operators */
327: case I_CE:
328: a1 = eval_pvar_in_fnode((FNODE)FA0(f));
329: a2 = eval_pvar_in_fnode((FNODE)FA1(f));
330: a3 = eval_pvar_in_fnode((FNODE)FA2(f));
331: return mkfnode(5,f->id,a1,a2,a3);
332:
333: /* lists */
334: case I_LIST:
335: n = (NODE)FA0(f);
336: for ( t0 = 0; n; n = NEXT(n) ) {
337: NEXTNODE(t0,t);
338: BDY(t) = (pointer)eval_pvar_in_fnode(BDY(n));
339: }
340: if ( t0 )
341: NEXT(t) = 0;
342: return mkfnode(1,f->id,t0);
343:
344: /* function */
345: case I_FUNC:
346: a1 = eval_pvar_in_fnode((FNODE)FA1(f));
347: return mkfnode(2,f->id,FA0(f),a1);
348: break;
349: case I_CAR: case I_CDR:
350: a1 = eval_pvar_in_fnode((FNODE)FA0(f));
351: return mkfnode(1,f->id,a1);
352: case I_EV:
353: /* exponent vector */
354: a1 = eval_pvar_in_fnode(mkfnode(1,I_LIST,FA0(f)));
355: return mkfnode(1,f->id,a1);
356:
357: case I_STR: case I_FORMULA:
358: return f;
359:
360: case I_PVAR: case I_INDEX:
361: case I_POSTSELF: case I_PRESELF:
362: r = eval(f);
1.10 noro 363: objtoquote(r,&q);
364: return BDY(q);
1.9 noro 365:
366: default:
367: error("eval_pvar_in_fnode : not implemented yet");
1.14 noro 368: /* NOTREACHED */
369: return 0;
1.12 noro 370: }
371: }
372:
1.14 noro 373: FNODE subst_in_fnode(FNODE f,V v,FNODE g)
1.12 noro 374: {
375: FNODE a1,a2,a3;
376: DCP dc;
377: V vf;
378: NODE n,t,t0;
379: Obj obj;
380:
381: if ( !f )
382: return 0;
383:
384: switch ( f->id ) {
385: /* unary operators */
1.13 noro 386: case I_NOT: case I_PAREN: case I_MINUS:
1.12 noro 387: a1 = subst_in_fnode((FNODE)FA0(f),v,g);
388: return mkfnode(1,f->id,a1);
389:
390: /* binary operators */
391: case I_AND: case I_OR:
392: a1 = subst_in_fnode((FNODE)FA0(f),v,g);
393: a2 = subst_in_fnode((FNODE)FA1(f),v,g);
394: return mkfnode(3,f->id,a1,a2);
395:
396: case I_BOP: case I_COP: case I_LOP:
397: a1 = subst_in_fnode((FNODE)FA1(f),v,g);
398: a2 = subst_in_fnode((FNODE)FA2(f),v,g);
399: return mkfnode(4,f->id,FA0(f),a1,a2);
400:
401: /* ternary operators */
402: case I_CE:
403: a1 = subst_in_fnode((FNODE)FA0(f),v,g);
404: a2 = subst_in_fnode((FNODE)FA1(f),v,g);
405: a3 = subst_in_fnode((FNODE)FA2(f),v,g);
406: return mkfnode(5,f->id,a1,a2,a3);
407:
408: /* lists */
409: case I_LIST:
410: n = (NODE)FA0(f);
411: for ( t0 = 0; n; n = NEXT(n) ) {
412: NEXTNODE(t0,t);
413: BDY(t) = (pointer)subst_in_fnode(BDY(n),v,g);
414: }
415: if ( t0 )
416: NEXT(t) = 0;
417: return mkfnode(1,f->id,t0);
418:
419: /* function */
420: case I_FUNC:
421: a1 = subst_in_fnode((FNODE)FA1(f),v,g);
422: return mkfnode(2,f->id,FA0(f),a1);
423: break;
424: case I_CAR: case I_CDR:
425: a1 = subst_in_fnode((FNODE)FA0(f),v,g);
426: return mkfnode(1,f->id,a1);
427: case I_EV:
428: /* exponent vector */
429: a1 = subst_in_fnode(mkfnode(1,I_LIST,FA0(f)),v,g);
430: return mkfnode(1,f->id,a1);
431:
432: case I_STR:
433: return f;
434:
435: case I_FORMULA:
436: obj = (Obj)FA0(f);
437: if ( !obj )
438: return f;
439:
440: switch ( OID(obj) ) {
441: case O_N:
442: return f;
443: case O_P:
444: vf = VR((P)obj);
445: dc = DC((P)obj);
446: if ( vf != v )
447: return f;
448: else if ( UNIQ(DEG(dc)) && UNIQ((Q)COEF(dc)) )
449: return g;
450: else break;
451: default:
452: break;
453: }
454:
455: default:
456: error("subst_in_fnode : not implemented yet");
1.14 noro 457: /* NOTREACHED */
458: return 0;
1.4 noro 459: }
1.1 noro 460: }
1.8 noro 461:
1.14 noro 462: /* not completed yet */
463:
464: #if 0
465: char *get_attribute(char *key,LIST attr)
1.8 noro 466: {}
467:
1.14 noro 468: void treetofnode(Obj obj,FNODE *f)
1.8 noro 469: {
470: NODE n;
471: LIST attr;
472: char *prop;
473:
474: if ( obj || OID(obj) != O_LIST ) {
475: /* internal object */
476: *f = mkfnode(1,I_FORMULA,obj);
477: } else {
478: /* [attr(list),name(string),args(node)] */
479: n = BDY((LIST)obj);
480: attr = (LIST)BDY(n); n = NEXT(n);
481: prop = get_attribute("asir",attr);
482: if ( !strcmp(prop,"u_op") ) {
483: } else if ( !strcmp(prop,"b_op") ) {
484: } else if ( !strcmp(prop,"t_op") ) {
485: } else if ( !strcmp(prop,"function") ) {
486: }
487: /* default will be set to P_FUNC */
488: }
489: }
1.14 noro 490: #endif
1.8 noro 491:
1.11 noro 492: FUNC user_print_function;
493:
1.14 noro 494: void Pset_print_function(NODE arg,pointer *rp)
1.11 noro 495: {
496: if ( !arg )
497: user_print_function = 0;
498: else {
499: gen_searchf(BDY((STRING)ARG0(arg)),&user_print_function);
500: }
501: *rp = 0;
502: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>