[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.10

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.10    ! noro       48:  * $OpenXM: OpenXM_contrib2/asir2000/builtin/subst.c,v 1.9 2010/01/31 03:25:54 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: {
                     71:        Obj a;
                     72:        P nm,dn,p;
                     73:        R r;
                     74:        VL vl,tvl;
1.10    ! noro       75:        int nv,i,ac;
        !            76:        NODE slist,t,ps,u,vlist;
1.8       noro       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));
1.10    ! noro       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_N,"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;
        !           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.8       noro      118:
                    119:        switch ( OID(a) ) {
                    120:                case O_P:
                    121:                        substpp(vl,(P)a,vvect,svect,nv,&nm); *rp = (Obj)nm;
                    122:                        return;
                    123:                case O_R:
                    124:                        substpp(vl,(P)NM((R)a),vvect,svect,nv,&nm);
                    125:                        substpp(vl,(P)DN((R)a),vvect,svect,nv,&dn);
                    126:                        if ( !dn )
                    127:                                error("substr2np: division by 0");
1.9       noro      128:                        else if ( !nm )
                    129:                                *rp = 0;
1.8       noro      130:                        else if ( NUM(dn) ) {
                    131:                                divsp(vl,nm,dn,&p);
                    132:                                *rp = (Obj)p;
                    133:                        } else {
                    134:                                MKRAT(nm,dn,0,r);
                    135:                                *rp = (Obj)r;
                    136:                        }
                    137:                        return;
                    138:                default:
                    139:                        error("substr2np: invalid argument");
                    140:        }
                    141: }
                    142:
1.1       noro      143: void Psubst(arg,rp)
                    144: NODE arg;
                    145: Obj *rp;
                    146: {
                    147:        Obj a,b,t;
                    148:        LIST l;
                    149:        V v;
1.6       noro      150:        int row,col,len;
                    151:        VECT vect;
                    152:        MAT mat;
                    153:        int i,j;
                    154:        NODE n0,n,nd;
                    155:        struct oNODE arg0;
                    156:        MP m,mp,mp0;
                    157:        DP d;
1.1       noro      158:
                    159:        if ( !arg ) {
                    160:                *rp = 0; return;
                    161:        }
1.6       noro      162:        a = (Obj)ARG0(arg);
                    163:        if ( !a ) {
                    164:                *rp = 0;
                    165:                return;
                    166:        }
                    167:        switch ( OID(a) ) {
                    168:                case O_N: case O_P: case O_R:
                    169:                        reductr(CO,(Obj)ARG0(arg),&a);
                    170:                        arg = NEXT(arg);
                    171:                        if ( arg && (l = (LIST)ARG0(arg)) && OID(l) == O_LIST )
                    172:                                arg = BDY(l);
                    173:                        while ( arg ) {
1.7       noro      174:                                if ( !BDY(arg) || OID((Obj)BDY(arg)) != O_P )
                    175:                                        error("subst : invalid argument");
1.6       noro      176:                                v = VR((P)BDY(arg)); arg = NEXT(arg);
                    177:                                if ( !arg )
                    178:                                        error("subst : invalid argument");
                    179:                                asir_assert(ARG0(arg),O_R,"subst");
                    180:                                reductr(CO,(Obj)BDY(arg),&b); arg = NEXT(arg);
                    181:                /*              b = (Obj)BDY(arg); arg = NEXT(arg); */
                    182:                                substr(CO,0,a,v,b,&t); a = t;
                    183:                        }
                    184:                        *rp = a;
                    185:                        break;
                    186:                case O_LIST:
                    187:                        n0 = 0;
                    188:                        for ( nd = BDY((LIST)a); nd; nd = NEXT(nd) ) {
                    189:                                NEXTNODE(n0,n);
                    190:                                arg0.body = (pointer)BDY(nd);
                    191:                                arg0.next = NEXT(arg);
                    192:                                Psubst(&arg0,&b);
                    193:                                BDY(n) = (pointer)b;
                    194:                        }
                    195:                        if ( n0 )
                    196:                                NEXT(n) = 0;
                    197:                        MKLIST(l,n0);
                    198:                        *rp = (Obj)l;
                    199:                        break;
                    200:                case O_VECT:
                    201:                        len = ((VECT)a)->len;
                    202:                        MKVECT(vect,len);
                    203:                        for ( i = 0; i < len; i++ ) {
                    204:                                arg0.body = (pointer)BDY((VECT)a)[i];
                    205:                                arg0.next = NEXT(arg);
                    206:                                Psubst(&arg0,&b);
                    207:                                BDY(vect)[i] = (pointer)b;
                    208:                        }
                    209:                        *rp = (Obj)vect;
                    210:                        break;
                    211:                case O_MAT:
                    212:                        row = ((MAT)a)->row;
                    213:                        col = ((MAT)a)->col;
                    214:                        MKMAT(mat,row,col);
                    215:                        for ( i = 0; i < row; i++ )
                    216:                                for ( j = 0; j < col; j++ ) {
                    217:                                        arg0.body = (pointer)BDY((MAT)a)[i][j];
                    218:                                        arg0.next = NEXT(arg);
                    219:                                        Psubst(&arg0,&b);
                    220:                                        BDY(mat)[i][j] = (pointer)b;
                    221:                                }
                    222:                        *rp = (Obj)mat;
                    223:                        break;
                    224:                case O_DP:
                    225:                        mp0 = 0;
                    226:                        for ( m = BDY((DP)a); m; m = NEXT(m) ) {
                    227:                                arg0.body = (pointer)C(m);
                    228:                                arg0.next = NEXT(arg);
                    229:                                Psubst(&arg0,&b);
                    230:                                if ( b ) {
                    231:                                        NEXTMP(mp0,mp);
                    232:                                        C(mp) = (P)b;
                    233:                                        mp->dl = m->dl;
                    234:                                }
                    235:                        }
                    236:                        if ( mp0 ) {
                    237:                                MKDP(NV((DP)a),mp0,d);
                    238:                                d->sugar = ((DP)a)->sugar;
                    239:                                *rp = (Obj)d;
                    240:                        } else
                    241:                                *rp = 0;
                    242:
                    243:                        break;
                    244:                default:
1.8       noro      245:                        error("subst : invalid argument");
1.1       noro      246:        }
1.4       noro      247: }
                    248:
                    249: FNODE subst_in_fnode();
                    250:
                    251: void Psubst_quote(arg,rp)
                    252: NODE arg;
                    253: QUOTE *rp;
                    254: {
1.5       noro      255:        QUOTE h;
1.4       noro      256:        FNODE fn;
                    257:        Obj g;
                    258:        LIST l;
                    259:        V v;
                    260:
                    261:        if ( !arg ) {
                    262:                *rp = 0; return;
                    263:        }
                    264:        asir_assert(ARG0(arg),O_QUOTE,"subst_quote");
                    265:        fn = BDY((QUOTE)ARG0(arg)); arg = NEXT(arg);
                    266:        if ( arg && (l = (LIST)ARG0(arg)) && OID(l) == O_LIST )
                    267:                arg = BDY(l);
                    268:        while ( arg ) {
                    269:                asir_assert(BDY(arg),O_P,"subst_quote");
                    270:                v = VR((P)BDY(arg)); arg = NEXT(arg);
                    271:                if ( !arg )
                    272:                        error("subst_quote : invalid argument");
                    273:                g = (Obj)ARG0(arg); arg = NEXT(arg);
                    274:                if ( !g || OID(g) != O_QUOTE )
                    275:                        objtoquote(g,&h);
                    276:                else
                    277:                        h = (QUOTE)g;
                    278:                fn = subst_in_fnode(fn,v,BDY(h));
                    279:        }
                    280:        MKQUOTE(*rp,fn);
1.1       noro      281: }
                    282:
                    283: void Ppsubst(arg,rp)
                    284: NODE arg;
                    285: Obj *rp;
                    286: {
                    287:        Obj a,b,t;
                    288:        LIST l;
                    289:        V v;
                    290:
                    291:        if ( !arg ) {
                    292:                *rp = 0; return;
                    293:        }
                    294:        asir_assert(ARG0(arg),O_R,"psubst");
                    295:        reductr(CO,(Obj)ARG0(arg),&a);
                    296: /*     a = (Obj)ARG0(arg); */
                    297:        arg = NEXT(arg);
                    298:        if ( arg && (l = (LIST)ARG0(arg)) && OID(l) == O_LIST )
                    299:                arg = BDY(l);
                    300:        while ( arg ) {
                    301:                asir_assert(BDY(arg),O_P,"psubst");
                    302:                v = VR((P)BDY(arg)); arg = NEXT(arg);
                    303:                if ( !arg )
                    304:                        error("psubst : invalid argument");
                    305:                asir_assert(ARG0(arg),O_R,"psubst");
                    306:                reductr(CO,(Obj)BDY(arg),&b); arg = NEXT(arg);
                    307: /*             b = (Obj)BDY(arg); arg = NEXT(arg); */
                    308:                substr(CO,1,a,v,b,&t); a = t;
                    309:        }
                    310:        *rp = a;
                    311: }
                    312:
                    313: void Psubstf(arg,rp)
                    314: NODE arg;
                    315: Obj *rp;
                    316: {
                    317:        Obj a,t;
                    318:        LIST l;
                    319:        V v,f;
                    320:
                    321:        if ( !arg ) {
                    322:                *rp = 0; return;
                    323:        }
                    324:        asir_assert(ARG0(arg),O_R,"substf");
                    325:        reductr(CO,(Obj)ARG0(arg),&a);
                    326: /*     a = (Obj)ARG0(arg); */
                    327:        arg = NEXT(arg);
                    328:        if ( arg && (l = (LIST)ARG0(arg)) && OID(l) == O_LIST )
                    329:                arg = BDY(l);
                    330:        while ( arg ) {
                    331:                asir_assert(BDY(arg),O_P,"substf");
                    332:                v = VR((P)BDY(arg)); arg = NEXT(arg);
                    333:                if ( !arg || (int)v->attr != V_SR )
                    334:                        error("substf : invalid argument");
                    335:                f = VR((P)BDY(arg)); arg = NEXT(arg);
                    336:                if ( (int)f->attr != V_SR )
                    337:                        error("substf : invalid argument\n");
                    338:                substfr(CO,a,((FUNC)v->priv)->f.puref,((FUNC)f->priv)->f.puref,&t);
                    339:                a = t;
                    340:        }
                    341:        *rp = a;
                    342: }

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