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