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