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