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