Annotation of researchv10no/cmd/sno/sno2.c, revision 1.1

1.1     ! root        1: /*     @(#)sno2.c      1.1     */
        !             2: #include "sno.h"
        !             3: 
        !             4: struct node *
        !             5: compon()
        !             6: {
        !             7:        register struct node *a, *b;
        !             8:        register int c;
        !             9:        static next;
        !            10: 
        !            11:        if (next == 0)
        !            12:                schar = sgetc();
        !            13:        else
        !            14:                next = 0;
        !            15:        if (schar == 0) {
        !            16:                (a=salloc())->typ = 0;
        !            17:                return (a);
        !            18:        }
        !            19:        switch (class (schar->ch)) {
        !            20: 
        !            21:        case 1:
        !            22:                schar->typ = 5;
        !            23:                return (schar);
        !            24: 
        !            25:        case 2:
        !            26:                schar->typ = 16;
        !            27:                return (schar);
        !            28: 
        !            29:        case 3:
        !            30:                a = schar;
        !            31:                for (;;) {
        !            32:                        schar = sgetc();
        !            33:                        if (schar == 0) {
        !            34:                                a->typ = 0;
        !            35:                                return (a);
        !            36:                        }
        !            37:                        if (class (schar->ch) != 3)
        !            38:                                break;
        !            39:                        sfree (schar);
        !            40:                }
        !            41:                next = 1;
        !            42:                a->typ = 7;
        !            43:                return (a);
        !            44: 
        !            45:        case 4:
        !            46:                schar->typ = 8;
        !            47:                return (schar);
        !            48: 
        !            49:        case 5:
        !            50:                schar->typ = 9;
        !            51:                return (schar);
        !            52: 
        !            53:        case 6:
        !            54:                a = schar;
        !            55:                schar = sgetc();
        !            56:                if (class (schar->ch) == 3)
        !            57:                        a->typ = 10;
        !            58:                else
        !            59:                        a->typ = 1;
        !            60:                next = 1;
        !            61:                return (a);
        !            62: 
        !            63:        case 7:
        !            64:                a = schar;
        !            65:                schar = sgetc();
        !            66:                if (class (schar->ch) == 3)
        !            67:                        a->typ = 11;
        !            68:                else
        !            69:                        a->typ = 2;
        !            70:                next = 1;
        !            71:                return (a);
        !            72: 
        !            73:        case 8:
        !            74:                schar->typ = 12;
        !            75:                return (schar);
        !            76: 
        !            77:        case 9:
        !            78:                c = schar->ch;
        !            79:                a = sgetc();
        !            80:                if (a == 0)
        !            81:                        goto lerr;
        !            82:                b = schar;
        !            83:                if (a->ch == c) {
        !            84:                        sfree (schar);
        !            85:                        a->typ = 15;
        !            86:                        a->p1 = 0;
        !            87:                        return (a);
        !            88:                }
        !            89:                b->p1 = a;
        !            90:                for (;;) {
        !            91:                        schar = sgetc();
        !            92:                        if (schar == 0)
        !            93:                        lerr:
        !            94:                                writes ("illegal literal string");
        !            95:                        if (schar->ch == c)
        !            96:                                break;
        !            97:                        a->p1 = schar;
        !            98:                        a = schar;
        !            99:                }
        !           100:                b->p2 = a;
        !           101:                schar->typ = 15;
        !           102:                schar->p1 = b;
        !           103:                return (schar);
        !           104: 
        !           105:        case 10:
        !           106:                schar->typ = 3;
        !           107:                return (schar);
        !           108: 
        !           109:        case 11:
        !           110:                schar->typ = 4;
        !           111:                return (schar);
        !           112: 
        !           113:        }
        !           114:        b = salloc();
        !           115:        b->p1 = a = schar;
        !           116:        schar = sgetc();
        !           117:        while (schar!=0 && !class (schar->ch)) {
        !           118:                a->p1 = schar;
        !           119:                a = schar;
        !           120:                schar = sgetc();
        !           121:        }
        !           122:        b->p2 = a;
        !           123:        next = 1;
        !           124:        a = look (b);
        !           125:        delete (b);
        !           126:        b = salloc();
        !           127:        b->typ = 14;
        !           128:        b->p1 = a;
        !           129:        return (b);
        !           130: }
        !           131: 
        !           132: struct node *
        !           133: nscomp()
        !           134: {
        !           135:        register struct node *c;
        !           136: 
        !           137:        while ((c=compon())->typ == 7)
        !           138:                sfree (c);
        !           139:        return (c);
        !           140: }
        !           141: 
        !           142: struct node *
        !           143: push (stack)
        !           144:        struct node *stack;
        !           145: {
        !           146:        register struct node *a;
        !           147: 
        !           148:        (a=salloc())->p2 = stack;
        !           149:        return (a);
        !           150: }
        !           151: 
        !           152: struct node *
        !           153: pop (stack)
        !           154:        struct node *stack;
        !           155: {
        !           156:        register struct node *a, *s;
        !           157: 
        !           158:        s = stack;
        !           159:        if (s == 0)
        !           160:                writes ("pop");
        !           161:        a = s->p2;
        !           162:        sfree (s);
        !           163:        return (a);
        !           164: }
        !           165: 
        !           166: struct node *
        !           167: expr (start, eof, e)
        !           168:        struct node *start, *e;
        !           169: {
        !           170:        register struct node *stack, *list, *comp, *space;
        !           171:        int operand, op, op1;
        !           172:        struct node *a, *b, *c;
        !           173:        int d;
        !           174: 
        !           175:        list = salloc();
        !           176:        e->p2 = list;
        !           177:        stack = push ((struct node *) NULL);
        !           178:        stack->typ = eof;
        !           179:        operand = 0;
        !           180:        space = start;
        !           181: l1:
        !           182:        if (space) {
        !           183:                comp = space;
        !           184:                space = 0;
        !           185:        } else
        !           186:                comp = compon();
        !           187: 
        !           188: l3:
        !           189:        op = comp->typ;
        !           190:        switch (op) {
        !           191: 
        !           192:        case 7:
        !           193:                space = (struct node *) 1;
        !           194:                sfree (comp);
        !           195:                comp = compon();
        !           196:                goto l3;
        !           197: 
        !           198:        case 10:
        !           199:                if (space == 0) {
        !           200:                        comp->typ = 1;
        !           201:                        goto l3;
        !           202:                }
        !           203: 
        !           204:        case 11:
        !           205:                if (space == 0) {
        !           206:                        comp->typ = 2;
        !           207:                        goto l3;
        !           208:                }
        !           209: 
        !           210:        case 8:
        !           211:        case 9:
        !           212:                if (operand == 0)
        !           213:                        writes ("no operand preceding operator");
        !           214:                operand = 0;
        !           215:                goto l5;
        !           216: 
        !           217:        case 14:
        !           218:        case 15:
        !           219:                if (operand == 0) {
        !           220:                        operand = 1;
        !           221:                        goto l5;
        !           222:                }
        !           223:                if (space == 0)
        !           224:                        goto l7;
        !           225:                goto l4;
        !           226: 
        !           227:        case 12:
        !           228:                if (operand == 0)
        !           229:                        goto l5;
        !           230:                if (space)
        !           231:                        goto l4;
        !           232:        l7:
        !           233:                writes ("illegal juxtaposition of operands");
        !           234: 
        !           235:        case 16:
        !           236:                if (operand == 0)
        !           237:                        goto l5;
        !           238:                if (space)
        !           239:                        goto l4;
        !           240:                b = compon();
        !           241:                op = comp->typ = 13;
        !           242:                if (b->typ == 5) {
        !           243:                        comp->p1 = 0;
        !           244:                        goto l10;
        !           245:                }
        !           246:                comp->p1 = a = salloc();
        !           247:                b = expr (b, 6, a);
        !           248:                while ((d=b->typ) == 4) {
        !           249:                        a->p1 = b;
        !           250:                        a = b;
        !           251:                        b = expr ((struct node *) NULL, 6, a);
        !           252:                }
        !           253:                if (d != 5)
        !           254:                        writes ("error in function");
        !           255:                a->p1 = 0;
        !           256:        l10:
        !           257:                sfree (b);
        !           258:                goto l6;
        !           259: 
        !           260:        l4:
        !           261:                space = comp;
        !           262:                op = 7;
        !           263:                operand = 0;
        !           264:                goto l6;
        !           265:        }
        !           266:        if (operand==0)
        !           267:                writes ("no operand at end of expression");
        !           268: l5:
        !           269:        space = 0;
        !           270: l6:
        !           271:        op1 = stack->typ;
        !           272:        if (op > op1) {
        !           273:                stack = push (stack);
        !           274:                if (op == 16)
        !           275:                        op = 6;
        !           276:                stack->typ = op;
        !           277:                stack->p1 = comp;
        !           278:                goto l1;
        !           279:        }
        !           280:        c = stack->p1;
        !           281:        stack = pop (stack);
        !           282:        if (stack == 0) {
        !           283:                list->typ = 0;
        !           284:                return (comp);
        !           285:        }
        !           286:        if (op1 == 6) {
        !           287:                if (op != 5)
        !           288:                        writes ("too many ('s");
        !           289:                goto l1;
        !           290:        }
        !           291:        if (op1 == 7)
        !           292:                c = salloc();
        !           293:        list->typ = op1;
        !           294:        list->p2 = c->p1;
        !           295:        list->p1 = c;
        !           296:        list = c;
        !           297:        goto l6;
        !           298: }
        !           299: 
        !           300: struct node *
        !           301: match (start, m)
        !           302:        struct node *start, *m;
        !           303: {
        !           304:        register struct node *list, *comp, *term;
        !           305:        struct node *a;
        !           306:        int b, bal;
        !           307: 
        !           308:        term = NULL;
        !           309:        bal = 0;
        !           310:        list = salloc();
        !           311:        m->p2 = list;
        !           312:        comp = start;
        !           313:        if (!comp)
        !           314:                comp = compon();
        !           315:        goto l2;
        !           316: 
        !           317: l3:
        !           318:        list->p1 = a = salloc();
        !           319:        list = a;
        !           320: l2:
        !           321:        switch (comp->typ) {
        !           322:        case 7:
        !           323:                sfree (comp);
        !           324:                comp = compon();
        !           325:                goto l2;
        !           326: 
        !           327:        case 12:
        !           328:        case 14:
        !           329:        case 15:
        !           330:        case 16:
        !           331:                term = 0;
        !           332:                comp = expr (comp, 6, list);
        !           333:                list->typ = 1;
        !           334:                goto l3;
        !           335: 
        !           336:        case 1:
        !           337:                sfree (comp);
        !           338:                comp = compon();
        !           339:                bal = 0;
        !           340:                if (comp->typ == 16) {
        !           341:                        bal = 1;
        !           342:                        sfree (comp);
        !           343:                        comp = compon();
        !           344:                }
        !           345:                a = salloc();
        !           346:                b = comp->typ;
        !           347:                if (b == 2 || b == 5 || b == 10 || b == 1)
        !           348:                        a->p1 = 0;
        !           349:                else {
        !           350:                        comp = expr (comp, 11, a);
        !           351:                        a->p1 = a->p2;
        !           352:                }
        !           353:                if (comp->typ != 2) {
        !           354:                        a->p2 = 0;
        !           355:                } else {
        !           356:                        sfree (comp);
        !           357:                        comp = expr ((struct node *) NULL, 6, a);
        !           358:                }
        !           359:                if (bal) {
        !           360:                        if (comp->typ != 5)
        !           361:                                goto merr;
        !           362:                        sfree (comp);
        !           363:                        comp = compon();
        !           364:                }
        !           365:                b = comp->typ;
        !           366:                if (b != 1 && b != 10)
        !           367:                        goto merr;
        !           368:                list->p2 = a;
        !           369:                list->typ = 2;
        !           370:                a->typ = bal;
        !           371:                sfree (comp);
        !           372:                comp = compon();
        !           373:                if (bal)
        !           374:                        term = 0; else
        !           375:                        term = list;
        !           376:                goto l3;
        !           377:        }
        !           378:        if (term)
        !           379:                term->typ = 3;
        !           380:        list->typ = 0;
        !           381:        return (comp);
        !           382: 
        !           383: merr:
        !           384:        writes ("unrecognized component in match");
        !           385:        return (NULL);
        !           386: }
        !           387: 
        !           388: struct node *
        !           389: compile()
        !           390: {
        !           391:        register struct node *b, *comp;
        !           392:        struct node *m, *r, *l, *xs, *xf, *g, *as;
        !           393:        struct node *aa;
        !           394:        register int a;
        !           395:        int t;
        !           396: 
        !           397:        as = m = l = xs = xf = 0;
        !           398:        t = 0;
        !           399:        comp = compon();
        !           400:        a = comp->typ;
        !           401:        if (a == 14) {
        !           402:                l = comp->p1;
        !           403:                sfree (comp);
        !           404:                comp = compon();
        !           405:                a = comp->typ;
        !           406:        }
        !           407:        if (a != 7)
        !           408:                writes ("no space beginning statement");
        !           409:        sfree (comp);
        !           410:        if (l == lookdef)
        !           411:                goto def;
        !           412:        comp = expr ((struct node *) NULL, 11, r=salloc());
        !           413:        a = comp->typ;
        !           414:        if (a == 0)
        !           415:                goto asmble;
        !           416:        if (a == 2)
        !           417:                goto xfer;
        !           418:        if (a == 3)
        !           419:                goto assig;
        !           420:        m = salloc();
        !           421:        comp = match (comp, m);
        !           422:        a = comp->typ;
        !           423:        if (a == 0)
        !           424:                goto asmble;
        !           425:        if (a == 2)
        !           426:                goto xfer;
        !           427:        if (a == 3)
        !           428:                goto assig;
        !           429:        writes ("unrecognized component in match");
        !           430: 
        !           431: assig:
        !           432:        sfree (comp);
        !           433:        comp = expr ((struct node *) NULL, 6, as=salloc());
        !           434:        a = comp->typ;
        !           435:        if (a == 0)
        !           436:                goto asmble;
        !           437:        if (a == 2)
        !           438:                goto xfer;
        !           439:        writes ("unrecognized component in assignment");
        !           440: 
        !           441: xfer:
        !           442:        sfree (comp);
        !           443:        comp = compon();
        !           444:        a = comp->typ;
        !           445:        if (a == 16)
        !           446:                goto xboth;
        !           447:        if (a == 0) {
        !           448:                if (xs!=0 || xf!=0)
        !           449:                        goto asmble;
        !           450:                goto xerr;
        !           451:        }
        !           452:        if (a != 14)
        !           453:                goto xerr;
        !           454:        b = comp->p1;
        !           455:        sfree (comp);
        !           456:        if (b == looks)
        !           457:                goto xsuc;
        !           458:        if (b == lookf)
        !           459:                goto xfail;
        !           460: 
        !           461: xerr:
        !           462:        writes ("unrecognized component in goto");
        !           463: 
        !           464: xboth:
        !           465:        sfree (comp);
        !           466:        xs = salloc();
        !           467:        xf = salloc();
        !           468:        comp = expr ((struct node *) NULL, 6, xs);
        !           469:        if (comp->typ != 5)
        !           470:                goto xerr;
        !           471:        xf->p2 = xs->p2;
        !           472:        comp = compon();
        !           473:        if (comp->typ != 0)
        !           474:                goto xerr;
        !           475:        goto asmble;
        !           476: 
        !           477: xsuc:
        !           478:        if (xs)
        !           479:                goto xerr;
        !           480:        comp = compon();
        !           481:        if (comp->typ != 16)
        !           482:                goto xerr;
        !           483:        comp = expr ((struct node *) NULL, 6, xs=salloc());
        !           484:        if (comp->typ != 5)
        !           485:                goto xerr;
        !           486:        goto xfer;
        !           487: 
        !           488: xfail:
        !           489:        if (xf)
        !           490:                goto xerr;
        !           491:        comp = compon();
        !           492:        if (comp->typ != 16)
        !           493:                goto xerr;
        !           494:        comp = expr ((struct node *) NULL, 6, xf=salloc());
        !           495:        if (comp->typ != 5)
        !           496:                goto xerr;
        !           497:        goto xfer;
        !           498: 
        !           499: asmble:
        !           500:        if (l) {
        !           501:                if (l->typ)
        !           502:                        writes ("name doubly defined");
        !           503:                l->p2 = comp;
        !           504:                l->typ = 2; /* type label;*/
        !           505:        }
        !           506:        comp->p2 = r;
        !           507:        if (m) {
        !           508:                t++;
        !           509:                r->p1 = m;
        !           510:                r = m;
        !           511:        }
        !           512:        if (as) {
        !           513:                t += 2;
        !           514:                r->p1 = as;
        !           515:                r = as;
        !           516:        }
        !           517:        (g=salloc())->p1 = 0;
        !           518:        if (xs) {
        !           519:                g->p1 = xs->p2;
        !           520:                sfree (xs);
        !           521:        }
        !           522:        g->p2 = 0;
        !           523:        if (xf) {
        !           524:                g->p2 = xf->p2;
        !           525:                sfree (xf);
        !           526:        }
        !           527:        r->p1 = g;
        !           528:        comp->typ = t;
        !           529:        comp->ch = lc;
        !           530:        return (comp);
        !           531: 
        !           532: def:
        !           533:        r = nscomp();
        !           534:        if (r->typ != 14)
        !           535:                goto derr;
        !           536:        l = r->p1;
        !           537:        if (l->typ)
        !           538:                writes ("name doubly defined");
        !           539:        l->typ = 5; /*type function;*/
        !           540:        aa = r;
        !           541:        l->p2 = aa;
        !           542:        r = nscomp();
        !           543:        l = r;
        !           544:        aa->p1 = l;
        !           545:        if (r->typ == 0)
        !           546:                goto d4;
        !           547:        if (r->typ != 16)
        !           548:                goto derr;
        !           549: 
        !           550: d2:
        !           551:        r = nscomp();
        !           552:        if (r->typ != 14)
        !           553:                goto derr;
        !           554:        aa->p2 = r;
        !           555:        r->typ = 0;
        !           556:        aa = r;
        !           557:        r = nscomp();
        !           558:        if (r->typ == 4) {
        !           559:                sfree (r);
        !           560:                goto d2;
        !           561:        }
        !           562:        if (r->typ != 5)
        !           563:                goto derr;
        !           564:        sfree (r);
        !           565:        if ((r=compon())->typ != 0)
        !           566:                goto derr;
        !           567:        sfree (r);
        !           568: 
        !           569: d4:
        !           570:        r = compile();
        !           571:        aa->p2 = 0;
        !           572:        l->p1 = r;
        !           573:        l->p2 = 0;
        !           574:        return (r);
        !           575: 
        !           576: derr:
        !           577:        writes ("illegal component in define");
        !           578:        return (NULL);
        !           579: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.