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