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