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