[BACK]Return to subst.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / builtin

Annotation of OpenXM_contrib2/asir2000/builtin/subst.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/subst.c,v 1.12 2018/03/27 06:29:19 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[] = {
1.13    ! noro       57:   {"subst",Psubst,-99999999},
        !            58:   {"substr2np",Psubstr2np,-3},
        !            59:   {"subst_quote",Psubst_quote,-99999999},
        !            60:   {"psubst",Ppsubst,-99999999},
        !            61:   {"substf",Psubstf,-99999999},
        !            62:   {0,0,0},
1.1       noro       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: {
1.13    ! noro      158:   Obj a,b,t;
        !           159:   LIST l;
        !           160:   V v;
        !           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;
        !           169:   VL lastvl,vl,tvl,prev,cur;
        !           170:
        !           171:   if ( !arg ) {
        !           172:     *rp = 0; return;
        !           173:   }
        !           174:   a = (Obj)ARG0(arg);
        !           175:   if ( !a ) {
        !           176:     *rp = 0;
        !           177:     return;
        !           178:   }
        !           179:   lastvl = LASTCO;
        !           180:   switch ( OID(a) ) {
        !           181:     case O_N: case O_P: case O_R:
        !           182:       reductr(CO,(Obj)ARG0(arg),&a);
        !           183:       arg = NEXT(arg);
        !           184:       if ( arg && (l = (LIST)ARG0(arg)) && OID(l) == O_LIST )
        !           185:         arg = BDY(l);
        !           186:       while ( arg ) {
        !           187:         if ( !BDY(arg) || OID((Obj)BDY(arg)) != O_P )
        !           188:           error("subst : invalid argument");
        !           189:         v = VR((P)BDY(arg)); arg = NEXT(arg);
        !           190:         if ( !arg )
        !           191:           error("subst : invalid argument");
        !           192:         asir_assert(ARG0(arg),O_R,"subst");
        !           193:         reductr(CO,(Obj)BDY(arg),&b); arg = NEXT(arg);
        !           194:     /*    b = (Obj)BDY(arg); arg = NEXT(arg); */
        !           195:         substr(CO,0,a,v,b,&t); a = t;
        !           196:       }
        !           197:       *rp = a;
        !           198:       break;
        !           199:     case O_LIST:
        !           200:       n0 = 0;
        !           201:       for ( nd = BDY((LIST)a); nd; nd = NEXT(nd) ) {
        !           202:         NEXTNODE(n0,n);
        !           203:         arg0.body = (pointer)BDY(nd);
        !           204:         arg0.next = NEXT(arg);
        !           205:         Psubst(&arg0,&b);
        !           206:         BDY(n) = (pointer)b;
        !           207:       }
        !           208:       if ( n0 )
        !           209:         NEXT(n) = 0;
        !           210:       MKLIST(l,n0);
        !           211:       *rp = (Obj)l;
        !           212:       break;
        !           213:     case O_VECT:
        !           214:       len = ((VECT)a)->len;
        !           215:       MKVECT(vect,len);
        !           216:       for ( i = 0; i < len; i++ ) {
        !           217:         arg0.body = (pointer)BDY((VECT)a)[i];
        !           218:         arg0.next = NEXT(arg);
        !           219:         Psubst(&arg0,&b);
        !           220:         BDY(vect)[i] = (pointer)b;
        !           221:       }
        !           222:       *rp = (Obj)vect;
        !           223:       break;
        !           224:     case O_MAT:
        !           225:       row = ((MAT)a)->row;
        !           226:       col = ((MAT)a)->col;
        !           227:       MKMAT(mat,row,col);
        !           228:       for ( i = 0; i < row; i++ )
        !           229:         for ( j = 0; j < col; j++ ) {
        !           230:           arg0.body = (pointer)BDY((MAT)a)[i][j];
        !           231:           arg0.next = NEXT(arg);
        !           232:           Psubst(&arg0,&b);
        !           233:           BDY(mat)[i][j] = (pointer)b;
        !           234:         }
        !           235:       *rp = (Obj)mat;
        !           236:       break;
        !           237:     case O_DP:
        !           238:       mp0 = 0;
        !           239:       for ( m = BDY((DP)a); m; m = NEXT(m) ) {
        !           240:         arg0.body = (pointer)C(m);
        !           241:         arg0.next = NEXT(arg);
        !           242:         Psubst(&arg0,&b);
        !           243:         if ( b ) {
        !           244:           NEXTMP(mp0,mp);
        !           245:           C(mp) = (P)b;
        !           246:           mp->dl = m->dl;
        !           247:         }
        !           248:       }
        !           249:       if ( mp0 ) {
        !           250:         MKDP(NV((DP)a),mp0,d);
        !           251:         d->sugar = ((DP)a)->sugar;
        !           252:         *rp = (Obj)d;
        !           253:       } else
        !           254:         *rp = 0;
        !           255:
        !           256:       break;
        !           257:     default:
        !           258:       error("subst : invalid argument");
        !           259:   }
        !           260:   if ( lastvl != LASTCO ) {
        !           261:     get_vars_recursive(*rp,&vl);
        !           262:     prev = lastvl; cur = NEXT(prev);
        !           263:     while ( cur ) {
        !           264:       v = cur->v;
        !           265:       for ( tvl = vl; tvl && tvl->v != v; tvl = NEXT(tvl) );
        !           266:       if ( !tvl ) NEXT(prev) = NEXT(cur);
        !           267:       else prev = cur;
        !           268:       cur = NEXT(cur);
        !           269:     }
        !           270:     update_LASTCO();
        !           271:   }
1.4       noro      272: }
                    273:
                    274: FNODE subst_in_fnode();
                    275:
                    276: void Psubst_quote(arg,rp)
                    277: NODE arg;
                    278: QUOTE *rp;
                    279: {
1.13    ! noro      280:   QUOTE h;
        !           281:   FNODE fn;
        !           282:   Obj g;
        !           283:   LIST l;
        !           284:   V v;
        !           285:
        !           286:   if ( !arg ) {
        !           287:     *rp = 0; return;
        !           288:   }
        !           289:   asir_assert(ARG0(arg),O_QUOTE,"subst_quote");
        !           290:   fn = BDY((QUOTE)ARG0(arg)); arg = NEXT(arg);
        !           291:   if ( arg && (l = (LIST)ARG0(arg)) && OID(l) == O_LIST )
        !           292:     arg = BDY(l);
        !           293:   while ( arg ) {
        !           294:     asir_assert(BDY(arg),O_P,"subst_quote");
        !           295:     v = VR((P)BDY(arg)); arg = NEXT(arg);
        !           296:     if ( !arg )
        !           297:       error("subst_quote : invalid argument");
        !           298:     g = (Obj)ARG0(arg); arg = NEXT(arg);
        !           299:     if ( !g || OID(g) != O_QUOTE )
        !           300:       objtoquote(g,&h);
        !           301:     else
        !           302:       h = (QUOTE)g;
        !           303:     fn = subst_in_fnode(fn,v,BDY(h));
        !           304:   }
        !           305:   MKQUOTE(*rp,fn);
1.1       noro      306: }
                    307:
                    308: void Ppsubst(arg,rp)
                    309: NODE arg;
                    310: Obj *rp;
                    311: {
1.13    ! noro      312:   Obj a,b,t;
        !           313:   LIST l;
        !           314:   V v;
        !           315:
        !           316:   if ( !arg ) {
        !           317:     *rp = 0; return;
        !           318:   }
        !           319:   asir_assert(ARG0(arg),O_R,"psubst");
        !           320:   reductr(CO,(Obj)ARG0(arg),&a);
        !           321: /*  a = (Obj)ARG0(arg); */
        !           322:   arg = NEXT(arg);
        !           323:   if ( arg && (l = (LIST)ARG0(arg)) && OID(l) == O_LIST )
        !           324:     arg = BDY(l);
        !           325:   while ( arg ) {
        !           326:     asir_assert(BDY(arg),O_P,"psubst");
        !           327:     v = VR((P)BDY(arg)); arg = NEXT(arg);
        !           328:     if ( !arg )
        !           329:       error("psubst : invalid argument");
        !           330:     asir_assert(ARG0(arg),O_R,"psubst");
        !           331:     reductr(CO,(Obj)BDY(arg),&b); arg = NEXT(arg);
        !           332: /*    b = (Obj)BDY(arg); arg = NEXT(arg); */
        !           333:     substr(CO,1,a,v,b,&t); a = t;
        !           334:   }
        !           335:   *rp = a;
1.1       noro      336: }
                    337:
                    338: void Psubstf(arg,rp)
                    339: NODE arg;
                    340: Obj *rp;
                    341: {
1.13    ! noro      342:   Obj a,t;
        !           343:   LIST l;
        !           344:   V v,f;
        !           345:
        !           346:   if ( !arg ) {
        !           347:     *rp = 0; return;
        !           348:   }
        !           349:   asir_assert(ARG0(arg),O_R,"substf");
        !           350:   reductr(CO,(Obj)ARG0(arg),&a);
        !           351: /*  a = (Obj)ARG0(arg); */
        !           352:   arg = NEXT(arg);
        !           353:   if ( arg && (l = (LIST)ARG0(arg)) && OID(l) == O_LIST )
        !           354:     arg = BDY(l);
        !           355:   while ( arg ) {
        !           356:     asir_assert(BDY(arg),O_P,"substf");
        !           357:     v = VR((P)BDY(arg)); arg = NEXT(arg);
        !           358:     if ( !arg || (int)v->attr != V_SR )
        !           359:       error("substf : invalid argument");
        !           360:     f = VR((P)BDY(arg)); arg = NEXT(arg);
        !           361:     if ( (int)f->attr != V_SR )
        !           362:       error("substf : invalid argument\n");
        !           363:     substfr(CO,a,((FUNC)v->priv)->f.puref,((FUNC)f->priv)->f.puref,&t);
        !           364:     a = t;
        !           365:   }
        !           366:   *rp = a;
1.1       noro      367: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>