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