Annotation of OpenXM_contrib2/asir2000/builtin/subst.c, Revision 1.11
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.11 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/builtin/subst.c,v 1.10 2015/09/24 04:43:13 noro Exp $
1.2 noro 49: */
1.1 noro 50: #include "ca.h"
51: #include "parse.h"
52:
1.4 noro 53: void Psubst(), Ppsubst(), Psubstf(), Psubst_quote();
1.8 noro 54: void Psubstr2np();
1.1 noro 55:
56: struct ftab subst_tab[] = {
57: {"subst",Psubst,-99999999},
1.10 noro 58: {"substr2np",Psubstr2np,-3},
1.4 noro 59: {"subst_quote",Psubst_quote,-99999999},
1.1 noro 60: {"psubst",Ppsubst,-99999999},
61: {"substf",Psubstf,-99999999},
62: {0,0,0},
63: };
64:
1.8 noro 65: extern Obj VOIDobj;
66:
1.10 noro 67: /* substr2np(P,[[v,a],...]) or substr2np(P,[v1,...],[a1,...]) */
68:
1.8 noro 69: void Psubstr2np(NODE arg,Obj *rp)
70: {
1.11 ! noro 71: Obj a,b;
! 72: P nm,dn,p,q;
! 73: R r;
! 74: VL vl,tvl;
! 75: int nv,i,ac,j;
! 76: NODE slist,t,ps,u,vlist;
! 77: P s;
! 78: P *svect;
! 79: V v;
! 80: V *vvect;
! 81:
! 82: a = (Obj)ARG0(arg);
! 83: if ( !a || NUM(a) ) {
! 84: *rp = a;
! 85: return;
! 86: }
! 87: asir_assert(ARG0(arg),O_R,"substr2np");
! 88: asir_assert(ARG1(arg),O_LIST,"substr2np");
! 89: get_vars(a,&vl);
! 90: for ( i = 0, tvl = vl; tvl; tvl = NEXT(tvl), i++ );
! 91: nv = i;
! 92: vvect = (V *)MALLOC((nv)*sizeof(V));
! 93: for ( i = 0, tvl = vl; tvl; tvl = NEXT(tvl), i++ ) vvect[i] = tvl->v;
! 94: svect = (P *)MALLOC((nv)*sizeof(P));
! 95: ac = argc(arg);
! 96: if ( ac == 2 ) {
! 97: slist = BDY((LIST)ARG1(arg));
! 98: for ( i = 0; i < nv; i++ ) svect[i] = (P)VOIDobj;
! 99: for ( t = slist; t; t = NEXT(t) ) {
! 100: ps = BDY((LIST)BDY(t)); p = (P)BDY(ps); s = (P)BDY(NEXT(ps));
! 101: asir_assert(p,O_P,"substr2np"); asir_assert(s,O_P,"substr2np");
! 102: v = VR(p);
! 103: for ( i = 0; i < nv; i++ ) if ( vvect[i] == v ) break;
! 104: svect[i] = s;
! 105: }
! 106: } else if ( ac == 3 ) {
! 107: asir_assert(ARG2(arg),O_LIST,"substr2np");
! 108: vlist = BDY((LIST)ARG1(arg));
! 109: slist = BDY((LIST)ARG2(arg));
! 110: for ( i = 0; i < nv; i++ ) svect[i] = (P)VOIDobj;
1.10 noro 111: for ( u = vlist, t = slist; u && t; u = NEXT(u), t = NEXT(t) ) {
112: v = VR((P)BDY(u));
113: for ( i = 0; i < nv; i++ ) if ( vvect[i] == v ) break;
114: svect[i] = (P)BDY(t);
115: }
116: } else
117: error("substr2np : argument mismatch");
1.11 ! noro 118: for ( i = 0; i < nv; i++ ) {
! 119: if ( (int)(vvect[i]->attr) == V_PF ) {
! 120: MKV(vvect[i],p);
! 121: for ( j = 0; j < nv; j++ )
! 122: if ( j != i ) {
! 123: substr(CO,0,(Obj)p,vvect[j],(Obj)svect[j],&b); p = (P)b;
! 124: }
! 125: if ( OID(svect[i]) == O_VOID ) svect[i] = p;
! 126: else if ( arf_comp(CO,(Obj)p,(Obj)svect[i]) )
! 127: error("substr2np : inconsistent values for substitution");
! 128: }
! 129: }
! 130: switch ( OID(a) ) {
! 131: case O_P:
! 132: substpp(CO,(P)a,vvect,svect,nv,&nm); *rp = (Obj)nm;
! 133: return;
! 134: case O_R:
! 135: substpp(CO,(P)NM((R)a),vvect,svect,nv,&nm);
! 136: substpp(CO,(P)DN((R)a),vvect,svect,nv,&dn);
! 137: if ( !dn )
! 138: error("substr2np: division by 0");
! 139: else if ( !nm )
! 140: *rp = 0;
! 141: else if ( NUM(dn) ) {
! 142: divsp(CO,nm,dn,&p);
! 143: *rp = (Obj)p;
! 144: } else {
! 145: MKRAT(nm,dn,0,r);
! 146: *rp = (Obj)r;
! 147: }
! 148: return;
! 149: default:
! 150: error("substr2np: invalid argument");
! 151: }
1.8 noro 152: }
153:
1.1 noro 154: void Psubst(arg,rp)
155: NODE arg;
156: Obj *rp;
157: {
158: Obj a,b,t;
159: LIST l;
160: V v;
1.6 noro 161: int row,col,len;
162: VECT vect;
163: MAT mat;
164: int i,j;
165: NODE n0,n,nd;
166: struct oNODE arg0;
167: MP m,mp,mp0;
168: DP d;
1.1 noro 169:
170: if ( !arg ) {
171: *rp = 0; return;
172: }
1.6 noro 173: a = (Obj)ARG0(arg);
174: if ( !a ) {
175: *rp = 0;
176: return;
177: }
178: switch ( OID(a) ) {
179: case O_N: case O_P: case O_R:
180: reductr(CO,(Obj)ARG0(arg),&a);
181: arg = NEXT(arg);
182: if ( arg && (l = (LIST)ARG0(arg)) && OID(l) == O_LIST )
183: arg = BDY(l);
184: while ( arg ) {
1.7 noro 185: if ( !BDY(arg) || OID((Obj)BDY(arg)) != O_P )
186: error("subst : invalid argument");
1.6 noro 187: v = VR((P)BDY(arg)); arg = NEXT(arg);
188: if ( !arg )
189: error("subst : invalid argument");
190: asir_assert(ARG0(arg),O_R,"subst");
191: reductr(CO,(Obj)BDY(arg),&b); arg = NEXT(arg);
192: /* b = (Obj)BDY(arg); arg = NEXT(arg); */
193: substr(CO,0,a,v,b,&t); a = t;
194: }
195: *rp = a;
196: break;
197: case O_LIST:
198: n0 = 0;
199: for ( nd = BDY((LIST)a); nd; nd = NEXT(nd) ) {
200: NEXTNODE(n0,n);
201: arg0.body = (pointer)BDY(nd);
202: arg0.next = NEXT(arg);
203: Psubst(&arg0,&b);
204: BDY(n) = (pointer)b;
205: }
206: if ( n0 )
207: NEXT(n) = 0;
208: MKLIST(l,n0);
209: *rp = (Obj)l;
210: break;
211: case O_VECT:
212: len = ((VECT)a)->len;
213: MKVECT(vect,len);
214: for ( i = 0; i < len; i++ ) {
215: arg0.body = (pointer)BDY((VECT)a)[i];
216: arg0.next = NEXT(arg);
217: Psubst(&arg0,&b);
218: BDY(vect)[i] = (pointer)b;
219: }
220: *rp = (Obj)vect;
221: break;
222: case O_MAT:
223: row = ((MAT)a)->row;
224: col = ((MAT)a)->col;
225: MKMAT(mat,row,col);
226: for ( i = 0; i < row; i++ )
227: for ( j = 0; j < col; j++ ) {
228: arg0.body = (pointer)BDY((MAT)a)[i][j];
229: arg0.next = NEXT(arg);
230: Psubst(&arg0,&b);
231: BDY(mat)[i][j] = (pointer)b;
232: }
233: *rp = (Obj)mat;
234: break;
235: case O_DP:
236: mp0 = 0;
237: for ( m = BDY((DP)a); m; m = NEXT(m) ) {
238: arg0.body = (pointer)C(m);
239: arg0.next = NEXT(arg);
240: Psubst(&arg0,&b);
241: if ( b ) {
242: NEXTMP(mp0,mp);
243: C(mp) = (P)b;
244: mp->dl = m->dl;
245: }
246: }
247: if ( mp0 ) {
248: MKDP(NV((DP)a),mp0,d);
249: d->sugar = ((DP)a)->sugar;
250: *rp = (Obj)d;
251: } else
252: *rp = 0;
253:
254: break;
255: default:
1.8 noro 256: error("subst : invalid argument");
1.1 noro 257: }
1.4 noro 258: }
259:
260: FNODE subst_in_fnode();
261:
262: void Psubst_quote(arg,rp)
263: NODE arg;
264: QUOTE *rp;
265: {
1.5 noro 266: QUOTE h;
1.4 noro 267: FNODE fn;
268: Obj g;
269: LIST l;
270: V v;
271:
272: if ( !arg ) {
273: *rp = 0; return;
274: }
275: asir_assert(ARG0(arg),O_QUOTE,"subst_quote");
276: fn = BDY((QUOTE)ARG0(arg)); arg = NEXT(arg);
277: if ( arg && (l = (LIST)ARG0(arg)) && OID(l) == O_LIST )
278: arg = BDY(l);
279: while ( arg ) {
280: asir_assert(BDY(arg),O_P,"subst_quote");
281: v = VR((P)BDY(arg)); arg = NEXT(arg);
282: if ( !arg )
283: error("subst_quote : invalid argument");
284: g = (Obj)ARG0(arg); arg = NEXT(arg);
285: if ( !g || OID(g) != O_QUOTE )
286: objtoquote(g,&h);
287: else
288: h = (QUOTE)g;
289: fn = subst_in_fnode(fn,v,BDY(h));
290: }
291: MKQUOTE(*rp,fn);
1.1 noro 292: }
293:
294: void Ppsubst(arg,rp)
295: NODE arg;
296: Obj *rp;
297: {
298: Obj a,b,t;
299: LIST l;
300: V v;
301:
302: if ( !arg ) {
303: *rp = 0; return;
304: }
305: asir_assert(ARG0(arg),O_R,"psubst");
306: reductr(CO,(Obj)ARG0(arg),&a);
307: /* a = (Obj)ARG0(arg); */
308: arg = NEXT(arg);
309: if ( arg && (l = (LIST)ARG0(arg)) && OID(l) == O_LIST )
310: arg = BDY(l);
311: while ( arg ) {
312: asir_assert(BDY(arg),O_P,"psubst");
313: v = VR((P)BDY(arg)); arg = NEXT(arg);
314: if ( !arg )
315: error("psubst : invalid argument");
316: asir_assert(ARG0(arg),O_R,"psubst");
317: reductr(CO,(Obj)BDY(arg),&b); arg = NEXT(arg);
318: /* b = (Obj)BDY(arg); arg = NEXT(arg); */
319: substr(CO,1,a,v,b,&t); a = t;
320: }
321: *rp = a;
322: }
323:
324: void Psubstf(arg,rp)
325: NODE arg;
326: Obj *rp;
327: {
328: Obj a,t;
329: LIST l;
330: V v,f;
331:
332: if ( !arg ) {
333: *rp = 0; return;
334: }
335: asir_assert(ARG0(arg),O_R,"substf");
336: reductr(CO,(Obj)ARG0(arg),&a);
337: /* a = (Obj)ARG0(arg); */
338: arg = NEXT(arg);
339: if ( arg && (l = (LIST)ARG0(arg)) && OID(l) == O_LIST )
340: arg = BDY(l);
341: while ( arg ) {
342: asir_assert(BDY(arg),O_P,"substf");
343: v = VR((P)BDY(arg)); arg = NEXT(arg);
344: if ( !arg || (int)v->attr != V_SR )
345: error("substf : invalid argument");
346: f = VR((P)BDY(arg)); arg = NEXT(arg);
347: if ( (int)f->attr != V_SR )
348: error("substf : invalid argument\n");
349: substfr(CO,a,((FUNC)v->priv)->f.puref,((FUNC)f->priv)->f.puref,&t);
350: a = t;
351: }
352: *rp = a;
353: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>