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

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

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