[BACK]Return to d.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / k097

Annotation of OpenXM/src/k097/d.c, Revision 1.1

1.1     ! maekawa     1: /* simple.c,  1996, 1/1 --- 1/5 */
        !             2: #include <stdio.h>
        !             3: #include <ctype.h>
        !             4: #include <setjmp.h>
        !             5: #include <stdlib.h>
        !             6: #include "d.h"
        !             7: #include "simple.tab.h"
        !             8:
        !             9:
        !            10: jmp_buf KCenvOfParser;
        !            11:
        !            12: int DebugMode = 1;
        !            13: extern int K00_verbose;
        !            14:
        !            15: static FILE *outfile = stdout;
        !            16: int Linenumber = 0;
        !            17: objectp Inop = (objectp) NULL;  /* Input stream */
        !            18: int Saki = 0;   /* Look a head */
        !            19:
        !            20: #define SSIZE 10
        !            21: objectp InopStack[SSIZE];
        !            22: int     LinenumberStack[SSIZE];
        !            23: int     SakiStack[SSIZE];
        !            24: int     Stackp = 0;
        !            25:
        !            26: int Mydebug = 1;
        !            27: int Replace = 0;
        !            28: int Debug2 = 0;
        !            29: /* If you need to check only the lexical analizer,
        !            30:    change main2() ---> main()
        !            31:           main() ----> main2()
        !            32: */
        !            33:
        !            34: int Interactive = 1;
        !            35:
        !            36: main2(int argc, char *argv[]) {
        !            37:   FILE *f;
        !            38:   FILE *outf;
        !            39:   char name[1024];
        !            40:   int n;
        !            41:   int i;
        !            42:   if (argc < 2) {
        !            43:     repl(stdin,stdout);
        !            44:   }else{
        !            45:     for (i=1; i<argc; i++) {
        !            46:       fprintf(stderr,"Input file=%s\n",argv[i]);
        !            47:       f = fopen(argv[i],"r");
        !            48:       if (f == (FILE *)NULL) {
        !            49:        fprintf(stderr,"Error: No file %s\n",argv[i]);
        !            50:        exit(10);
        !            51:       }
        !            52:       strcpy(name,argv[i]);
        !            53:       n = strlen(name);
        !            54:       name[n] = '.'; name[n+1] = 't'; name[n+2]='\0';
        !            55:       outf = fopen(name,"w");
        !            56:       if (f == (FILE *)NULL) {
        !            57:        fprintf(stderr,"Error: failed to open the file %s\n",name);
        !            58:        exit(10);
        !            59:       }
        !            60:       fprintf(stderr,"Output file=%s\n",name);
        !            61:       repl(f,outf);
        !            62:     }
        !            63:   }
        !            64: }
        !            65:
        !            66: repl(FILE *inFile,FILE *outFile) {
        !            67:   int c;
        !            68:   int t;
        !            69:   Inop = newObject_d();
        !            70:   Inop->tag = Sfile;
        !            71:   Inop->lc.file = inFile;
        !            72:   Inop->rc.sbuf = newStringBuf((char *)NULL);
        !            73:
        !            74:   outfile = outFile;
        !            75:   while ( (c = KClex()) != EOF) {
        !            76:     if (Mydebug) {
        !            77:       if ( c > ' ' && c < 0x7f ) {
        !            78:        printf("\nKClex returns --- %x [ %c ].",c,c);
        !            79:       }else{
        !            80:        switch(c) {
        !            81:        case ID:    printf("\nKClex returns --- ID.[%s]",
        !            82:                           KClval->lc.str); break;
        !            83:        case QUOTE: printf("\nKClex returns --- QUOTE. [%s]",
        !            84:                           KClval->lc.str); break;
        !            85:        case SINGLEQUOTE:    printf("\nKClex returns --- SINGLEQUOTE.[%x]",
        !            86:                                    KClval->lc.ival); break;
        !            87:        /*case NUMBER:    printf("\nKClex returns --- NUMBER.[%d]",
        !            88:                               KClval->lc.ival); break;*/
        !            89:        case NUMBER:    printf("\nKClex returns --- NUMBER.[%s]",
        !            90:                               KClval->lc.str); break;
        !            91:
        !            92:        case CLASS:    printf("\nKClex returns --- CLASS."); break;
        !            93:        case SUPER:    printf("\nKClex returns --- SUPER."); break;
        !            94:        case OPERATOR:    printf("\nKClex returns --- OPERATOR."); break;
        !            95:        case FINAL:    printf("\nKClex returns --- FINAL."); break;
        !            96:        case EXTENDS:    printf("\nKClex returns --- EXTENDS."); break;
        !            97:        case INCETANCEVARIABLE:
        !            98:          printf("\nKClex returns --- INCETANCEVARIABLE."); break;
        !            99:        case MODULE:    printf("\nKClex returns --- MODULE."); break;
        !           100:         case SM1:       printf("\nKClex returns --- SM1."); break;
        !           101:        case PRINT: printf("\nKClex returns --- PRINT."); break;
        !           102:        case LOCAL: printf("\nKClex returns --- LOCAL."); break;
        !           103:        case DEF: printf("\nKClex returns --- DEF."); break;
        !           104:        case BREAK: printf("\nKClex returns --- BREAK."); break;
        !           105:        case CASE:    printf("\nKClex returns --- CASE."); break;
        !           106:        case CHAR:    printf("\nKClex returns --- CHAR."); break;
        !           107:        case CONST:    printf("\nKClex returns --- CONST."); break;
        !           108:        case CONTINUE:    printf("\nKClex returns --- CONTINUE."); break;
        !           109:        case DEFAULT:    printf("\nKClex returns --- DEFAULT."); break;
        !           110:        case DO:    printf("\nKClex returns --- DO."); break;
        !           111:        case DOUBLE:    printf("\nKClex returns --- DOUBLE."); break;
        !           112:        case ELSE:    printf("\nKClex returns --- ELSE."); break;
        !           113:        case ENUM:    printf("\nKClex returns --- ENUM."); break;
        !           114:        case EXTERN:    printf("\nKClex returns --- EXTERN."); break;
        !           115:        case FLOAT:    printf("\nKClex returns --- FLOAT."); break;
        !           116:        case FOR:    printf("\nKClex returns --- FOR."); break;
        !           117:        case GOTO:    printf("\nKClex returns --- GOTO."); break;
        !           118:        case IF:    printf("\nKClex returns --- IF."); break;
        !           119:        case INT:    printf("\nKClex returns --- INT."); break;
        !           120:        case LONG:    printf("\nKClex returns --- LONG."); break;
        !           121:        case REGISTER:    printf("\nKClex returns --- REGISTER."); break;
        !           122:        case RETURN:    printf("\nKClex returns --- RETURN."); break;
        !           123:        case SHORT:    printf("\nKClex returns --- SHORT."); break;
        !           124:        case SIGNED:    printf("\nKClex returns --- SIGNED."); break;
        !           125:        case SIZEOF:    printf("\nKClex returns --- SIZEOF."); break;
        !           126:        case STATIC:    printf("\nKClex returns --- STATIC."); break;
        !           127:        case STRUCT:    printf("\nKClex returns --- SWITCH."); break;
        !           128:        case TYPEDEF:    printf("\nKClex returns --- TYPEDEF."); break;
        !           129:        case UNION:    printf("\nKClex returns --- UNION."); break;
        !           130:        case UNSIGNED:    printf("\nKClex returns --- UNSIGNED."); break;
        !           131:        case VOLATILE: printf("\nKClex returns --- VOLATILE."); break;
        !           132:        case VOID: printf("\nKClex returns --- VOID."); break;
        !           133:        case WHILE: printf("\nKClex returns --- WHILE."); break;
        !           134:
        !           135:        case EQUAL: printf("\nKClex returns --- ==."); break;
        !           136:        case LESSEQUAL: printf("\nKClex returns --- <=."); break;
        !           137:        case LEFTSHIFT: printf("\nKClex returns --- <<."); break;
        !           138:        case GREATEREQUAL: printf("\nKClex returns --- >=."); break;
        !           139:        case RIGHTSHIFT: printf("\nKClex returns --- >>."); break;
        !           140:        case AND: printf("\nKClex returns --- &&."); break;
        !           141:        case OR: printf("\nKClex returns --- ||."); break;
        !           142:        case NOTEQUAL: printf("\nKClex returns --- !=."); break;
        !           143:        case PUT: printf("\nKClex returns --- :=."); break;
        !           144:        case INCREMENT: printf("\nKClex returns --- ++."); break;
        !           145:        case DECREMENT: printf("\nKClex returns --- --."); break;
        !           146:        case MEMBER: printf("\nKClex returns --- ->."); break;
        !           147:        case RESIDUEPUT: printf("\nKClex returns --- %=."); break;
        !           148:        case NEGATEPUT: printf("\nKClex returns --- ^=."); break;
        !           149:        case MULTPUT: printf("\nKClex returns --- *=."); break;
        !           150:
        !           151:        default:
        !           152:          printf("\nKClex returns --- %x(%d)[%c].",c,c,c);
        !           153:        }
        !           154:       }
        !           155:       printf(" line=%d.\n",Linenumber);
        !           156:     }
        !           157:   }
        !           158: }
        !           159:
        !           160: /* --------------------------------------------------------- */
        !           161: objectp newObject_d() {
        !           162:   objectp obj;
        !           163:   obj = (objectp) mymalloc(sizeof(struct Object));
        !           164:   if (obj == (objectp) NULL) {
        !           165:     fprintf(stderr,"Error: No more memory in newObject_d().\n");
        !           166:     exit(10);
        !           167:   }
        !           168:   obj->tag = Snull;
        !           169:   return(obj);
        !           170: }
        !           171:
        !           172: char *newString(int size) {
        !           173:   char *str;
        !           174:   if (size <= 0) size=1;
        !           175:   str = (char *)mymalloc(sizeof(char)*size);
        !           176:   if (str == (char *)NULL) {
        !           177:     fprintf(stderr,"Error: No more memory in newObject_d().\n");
        !           178:     exit(10);
        !           179:   }
        !           180:   return(str);
        !           181: }
        !           182:
        !           183: void printObject_d(FILE *fp,objectp op)
        !           184: {
        !           185:   if (op == (objectp) NULL) {
        !           186:     fprintf(fp," <null objectp> ");
        !           187:     return;
        !           188:   }
        !           189:   switch(op->tag) {
        !           190:   case Sinteger:
        !           191:     fprintf(fp,"%d",op->lc.ival);
        !           192:     return;
        !           193:     break;
        !           194:   case Sstring:
        !           195:     fprintf(fp,"%s",op->lc.str);
        !           196:     return;
        !           197:     break;
        !           198:   case CLASSNAME_CONTEXT:
        !           199:     K00fprintContext(fp,K00objectToContext(op));
        !           200:     return;
        !           201:     break;
        !           202:   default:
        !           203:     fprintf(stderr,"printObject_d(): Sorry. Not implemented.");
        !           204:     break;
        !           205:   }
        !           206: }
        !           207:
        !           208: void printObjectSymbol(objectp op)
        !           209: {
        !           210:   static char tmp[1024];
        !           211:   if (op == (objectp) NULL) {
        !           212:     printf(" <null objectp> ");
        !           213:     return;
        !           214:   }
        !           215:   switch(op->tag) {
        !           216:   case Sinteger:
        !           217:     sprintf(tmp,"%d",op->lc.ival);
        !           218:     pkkan(tmp);
        !           219:     return; break;
        !           220:   case Sstring:
        !           221:     pkkan(op->lc.str);
        !           222:     return; break;
        !           223:   default:
        !           224:     fprintf(stderr,"(printObjectSymbol(): Sorry. Not implemented.)");
        !           225:     break;
        !           226:   }
        !           227: }
        !           228:
        !           229: void printTens(objectp op)
        !           230: {
        !           231:   static char tmp[1024];
        !           232:   int n;
        !           233:   int i;
        !           234:   if (op == (objectp) NULL) {
        !           235:     printf(" <null objectp> ");
        !           236:     return;
        !           237:   }
        !           238:   switch(op->tag) {
        !           239:   case Sstring:
        !           240:     n = strlen(op->lc.str);
        !           241:     if (n > 1022) {
        !           242:       fprintf(stderr,"printTens(): Too long string. ");
        !           243:       return;
        !           244:     }
        !           245:     tmp[0] = '1';
        !           246:     for (i=1; i<=n; i++) tmp[i] = '0';
        !           247:     tmp[n+1] = 0;
        !           248:     pkkan(tmp);
        !           249:     return; break;
        !           250:   default:
        !           251:     fprintf(stderr,"(printTens(): Sorry. Not implemented.)");
        !           252:     break;
        !           253:   }
        !           254: }
        !           255:
        !           256: char *objectSymbolToString(objectp op)
        !           257: {
        !           258:   static char tmp[1024];
        !           259:   char *r = (char *)NULL;
        !           260:   if (op == (objectp) NULL) {
        !           261:     printf(" <null objectp> ");
        !           262:     return (r);
        !           263:   }
        !           264:   switch(op->tag) {
        !           265:   case Sinteger:
        !           266:     sprintf(tmp,"%d",op->lc.ival);
        !           267:     r = (char *)GC_malloc(sizeof(char)*(strlen(tmp)+2));
        !           268:     strcpy(r,tmp);
        !           269:     break;
        !           270:   case Sstring:
        !           271:     r = (char *)GC_malloc(sizeof(char)*(strlen(op->lc.str)+2));
        !           272:     strcpy(r,op->lc.str);
        !           273:     break;
        !           274:   default:
        !           275:     fprintf(stderr,"(objectSymbolToString(): Sorry. Not implemented.)");
        !           276:     break;
        !           277:   }
        !           278:   return(r);
        !           279: }
        !           280:
        !           281: objectp ooAdd(objectp a,objectp b)
        !           282: {
        !           283:   objectp r;
        !           284:   if (a == (objectp)NULL || b == (objectp)NULL) return((objectp) NULL);
        !           285:   if (a->tag == Sinteger && b->tag == Sinteger) {
        !           286:     r = newObject_d();
        !           287:     r->tag = Sinteger;
        !           288:     r->lc.ival = a->lc.ival + b->lc.ival;
        !           289:     return(r);
        !           290:   }else{
        !           291:     fprintf(stderr,"ooAdd(): Sorry. Not implemented.");
        !           292:     return((objectp)NULL);
        !           293:   }
        !           294: }
        !           295:
        !           296: objectp ooMult(objectp a,objectp b)
        !           297: {
        !           298:   objectp r;
        !           299:   if (a == (objectp)NULL || b == (objectp)NULL) return((objectp) NULL);
        !           300:   if (a->tag == Sinteger && b->tag == Sinteger) {
        !           301:     r = newObject_d();
        !           302:     r->tag = Sinteger;
        !           303:     r->lc.ival = a->lc.ival * b->lc.ival;
        !           304:     return(r);
        !           305:   }else{
        !           306:     fprintf(stderr,"ooMult(): Sorry. Not implemented.");
        !           307:     return((objectp)NULL);
        !           308:   }
        !           309: }
        !           310:
        !           311: /* ------------------------------------------- */
        !           312:
        !           313: int KClex() {
        !           314:   extern int Saki; /* Saki-yomi */
        !           315:   int d;
        !           316:   int state = 0;
        !           317:   int i;
        !           318:   static char nametmp[1024];
        !           319:   char *name = nametmp;
        !           320:   char *str;
        !           321:   KClval = (objectp) NULL;
        !           322:   while (1) {
        !           323:     switch( state ) {
        !           324:     case 0:
        !           325:       /* printf(" <%x> ",Saki); */
        !           326:       if (Saki == EOF) {
        !           327:        return(Saki);
        !           328:       }
        !           329:       if (Saki <= ' ') {
        !           330:        if (Saki == '\n') ++Linenumber;
        !           331:        if (Replace) putchar0(Saki);
        !           332:        Saki = fsgetc(Inop); break;
        !           333:       }
        !           334:       if ( Saki == '\"' ) {
        !           335:        str = readstring();
        !           336:        /* if (Mydebug) printf("[string: %s]",str); */
        !           337:
        !           338:        KClval = newObject_d();
        !           339:        KClval->tag = Sstring;
        !           340:        KClval->lc.str = newString(strlen(str)+1);
        !           341:        strcpy(KClval->lc.str,str);
        !           342:
        !           343:        Saki = fsgetc(Inop);
        !           344:        return(QUOTE);
        !           345:        break;
        !           346:       }
        !           347:       if (Saki == '\'' ) {
        !           348:        d = readchar();
        !           349:        Saki = fsgetc(Inop);
        !           350:
        !           351:        KClval = newObject_d();
        !           352:        KClval->tag = Sinteger;
        !           353:        KClval->lc.ival = d;
        !           354:
        !           355:        return(SINGLEQUOTE);
        !           356:       }
        !           357:       /* single */
        !           358:       if ( Saki == '(' || Saki == ')' || Saki == ';' ||
        !           359:           Saki == '{' || Saki == '}' || Saki == ',' ||
        !           360:           Saki == '[' || Saki == ']' || Saki == '~' ||
        !           361:           Saki == '?' || Saki == '.') {
        !           362:        d = Saki; Saki = fsgetc(Inop);
        !           363:        if (Replace) putchar0(d);
        !           364:        return(d);
        !           365:       }
        !           366:
        !           367:       /* single or combination =, == */
        !           368:       if ( Saki == '=') {
        !           369:        state = 51; Saki = fsgetc(Inop); break;
        !           370:       } else if ( Saki == '<' ) {
        !           371:        state = 52; Saki = fsgetc(Inop); break;
        !           372:       } else if ( Saki == '>' ) {
        !           373:        state = 53; Saki = fsgetc(Inop); break;
        !           374:       } else if ( Saki == '/' ) {
        !           375:        state = 54; Saki = fsgetc(Inop); break;
        !           376:       } else if ( Saki == '&' ) {
        !           377:        state = 55; Saki = fsgetc(Inop); break;
        !           378:       } else if ( Saki == '|' ) {
        !           379:        state = 56; Saki = fsgetc(Inop); break;
        !           380:       } else if ( Saki == '!' ) {
        !           381:        state = 57; Saki = fsgetc(Inop); break;
        !           382:       } else if ( Saki == ':' ) {
        !           383:        state = 58; Saki = fsgetc(Inop); break;
        !           384:       } else if ( Saki == '+' ) {
        !           385:        state = 59; Saki = fsgetc(Inop); break;
        !           386:       } else if ( Saki == '-' ) {
        !           387:        state = 60; Saki = fsgetc(Inop); break;
        !           388:       } else if ( Saki == '%' ) {
        !           389:        state = 61; Saki = fsgetc(Inop); break;
        !           390:       } else if ( Saki == '^' ) {
        !           391:        state = 62; Saki = fsgetc(Inop); break;
        !           392:       } else if ( Saki == '*' ) {
        !           393:        state = 63; Saki = fsgetc(Inop); break;
        !           394:       } else ;
        !           395:
        !           396:
        !           397:       /* else : Identifier or function names. */
        !           398:       name[0] = Saki; i=1; name[i] = '\0';
        !           399:       Saki = fsgetc(Inop);
        !           400:       if (isdigit(name[0])) {
        !           401:        /*while (isdigit(Saki) || isalpha(Saki) || Saki=='.') */
        !           402:        while (isdigit(Saki) || isalpha(Saki)) {
        !           403:          name[i++] = Saki; name[i] = '\0';
        !           404:          Saki = fsgetc(Inop);
        !           405:        }
        !           406:       }else{
        !           407:        while (isdigit(Saki) || isalpha(Saki) || (Saki=='_') ||
        !           408:               ( Saki>= 256)) {
        !           409:          name[i++] = Saki; name[i] = '\0';
        !           410:          Saki = fsgetc(Inop);
        !           411:        }
        !           412:       }
        !           413:       /*if (Mydebug) printf("identifier string=[%s]",name);*/
        !           414:       if (isdigit(name[0])) {
        !           415:         /****************************
        !           416:          /**case : machine integer.
        !           417:        KClval = newObject_d();
        !           418:        KClval->tag = Sinteger;
        !           419:        sscanf(name,"%d",&(KClval->lc.ival));*************/
        !           420:        /* Other cases.  */
        !           421:        KClval = newObject_d();
        !           422:        KClval->tag = Sstring;
        !           423:        KClval->lc.str = newString(strlen(name)+1);
        !           424:        strcpy(KClval->lc.str,name);
        !           425:        return(NUMBER);
        !           426:        break;
        !           427:       } /* else : Identifier case.*/
        !           428:
        !           429:       if (d = isReserved(name)) {
        !           430:        if (Replace) printf0(name);
        !           431:        return(d);
        !           432:       } else {
        !           433:        if (Replace) {
        !           434:          if (shouldReplace(name))
        !           435:            printf1(name); /* do your own replacement in printf1*/
        !           436:          else
        !           437:            printf0(name);
        !           438:        }
        !           439:        KClval = newObject_d();
        !           440:        KClval->tag = Sstring;
        !           441:        KClval->lc.str = newString(strlen(name)+1);
        !           442:        strcpy(KClval->lc.str,name);
        !           443:        return(ID);
        !           444:       }
        !           445:       break;
        !           446:
        !           447:     case 51:
        !           448:       if (Replace) putchar0('=');
        !           449:       if ( Saki == '=') {
        !           450:        if (Replace) putchar0('=');
        !           451:        Saki = fsgetc(Inop);state = 0;return(EQUAL); /* == */
        !           452:       }else{
        !           453:        state = 0;return('=');
        !           454:       }
        !           455:       break;
        !           456:     case 52: /* 52 --- 60 tmporary return values */
        !           457:       if (Replace) putchar0('<');
        !           458:       if ( Saki == '=') {
        !           459:        if (Replace) putchar0('=');
        !           460:        Saki = fsgetc(Inop);state = 0;return(LESSEQUAL); /* <= */
        !           461:       } else if ( Saki == '<') {
        !           462:        if (Replace) putchar0('<');
        !           463:        Saki = fsgetc(Inop);state = 0;return(LEFTSHIFT); /* << */
        !           464:       }else{
        !           465:        state = 0;return('<');
        !           466:       }
        !           467:       break;
        !           468:     case 53:
        !           469:       if (Replace) putchar0('>');
        !           470:       if ( Saki == '=') {
        !           471:        if (Replace) putchar0('=');
        !           472:        Saki = fsgetc(Inop);state = 0;return(GREATEREQUAL); /* >= */
        !           473:       } else if ( Saki == '>') {
        !           474:        if (Replace) putchar0('>');
        !           475:        Saki = fsgetc(Inop);state = 0;return(RIGHTSHIFT); /* >> */
        !           476:       }else{
        !           477:        state = 0;return('>');
        !           478:       }
        !           479:       break;
        !           480:     case 54:
        !           481:       if ( Saki == '*') {
        !           482:        readcomment();
        !           483:        Saki = fsgetc(Inop);state = 0;break; /* clike-comment */
        !           484:       }else{
        !           485:        if (Replace) putchar0('/');
        !           486:        state = 0;return('/');
        !           487:       }
        !           488:       break;
        !           489:     case 55:
        !           490:       if (Replace) putchar0('&');
        !           491:       if ( Saki == '&') {
        !           492:        if (Replace) putchar0('&');
        !           493:        Saki = fsgetc(Inop);state = 0;return(AND); /* && */
        !           494:       }else{
        !           495:        state = 0;return('&');
        !           496:       }
        !           497:       break;
        !           498:     case 56:
        !           499:       if (Replace) putchar0('|');
        !           500:       if ( Saki == '|') {
        !           501:        if (Replace) putchar0('|');
        !           502:        Saki = fsgetc(Inop);state = 0;return(OR); /* || */
        !           503:       }else{
        !           504:        state = 0;return('|');
        !           505:       }
        !           506:       break;
        !           507:     case 57:
        !           508:       if (Replace) putchar0('!');
        !           509:       if ( Saki == '=') {
        !           510:        if (Replace) putchar0('=');
        !           511:        Saki = fsgetc(Inop);state = 0;return(NOTEQUAL); /* != */
        !           512:       }else{
        !           513:        state = 0;return('!');
        !           514:       }
        !           515:       break;
        !           516:     case 58:
        !           517:       if (Replace) putchar0(':');
        !           518:       if ( Saki == '=') {
        !           519:        if (Replace) putchar0('=');
        !           520:        Saki = fsgetc(Inop);state = 0;return(PUT); /* := */
        !           521:       }else{
        !           522:        state = 0;return(':');
        !           523:       }
        !           524:       break;
        !           525:     case 59:
        !           526:       if (Replace) putchar0('+');
        !           527:       if ( Saki == '+') {
        !           528:        if (Replace) putchar0('+');
        !           529:        Saki = fsgetc(Inop);state = 0;return(INCREMENT); /* ++ */
        !           530:       }else{
        !           531:        state = 0;return('+');
        !           532:       }
        !           533:       break;
        !           534:     case 60:
        !           535:       if (Replace) putchar0('-');
        !           536:       if ( Saki == '-') {
        !           537:        if (Replace) putchar0('-');
        !           538:        Saki = fsgetc(Inop);state = 0;return(DECREMENT); /* -- */
        !           539:       }else if (Saki == '>') {
        !           540:        if (Replace) putchar0('>');
        !           541:        Saki = fsgetc(Inop);state = 0;return(MEMBER); /* -> */
        !           542:       }else{
        !           543:        state = 0;return('-');
        !           544:       }
        !           545:       break;
        !           546:     case 61:
        !           547:       if (Replace) putchar0('%');
        !           548:       if ( Saki == '=') {
        !           549:        if (Replace) putchar0('=');
        !           550:        Saki = fsgetc(Inop);state = 0;return(RESIDUEPUT); /* %= */
        !           551:       }else{
        !           552:        state = 0;return('%');
        !           553:       }
        !           554:       break;
        !           555:     case 62:
        !           556:       if (Replace) putchar0('^');
        !           557:       if ( Saki == '=') {
        !           558:        if (Replace) putchar0('=');
        !           559:        Saki = fsgetc(Inop);state = 0;return(NEGATEPUT); /* ^= */
        !           560:       }else{
        !           561:        state = 0;return('^');
        !           562:       }
        !           563:       break;
        !           564:     case 63:
        !           565:       if (Replace) putchar0('*');
        !           566:       if ( Saki == '=') {
        !           567:        if (Replace) putchar0('=');
        !           568:        Saki = fsgetc(Inop);state = 0;return(MULTPUT); /* *= */
        !           569:       }else{
        !           570:        state = 0;return('*');
        !           571:       }
        !           572:       break;
        !           573:
        !           574:
        !           575:     default:
        !           576:       fprintf(stderr,"%d: Error in KClex().\n",Linenumber);
        !           577:     }
        !           578:   }
        !           579:
        !           580: }
        !           581:
        !           582: KCerror(char *s)   /* You need this function. Otherwise, you get core. */
        !           583: {
        !           584:   K00recoverFromError();
        !           585:   fprintf(stderr,"\nSyntax Error in the line %d:%s\n",Linenumber,s);
        !           586:   showStringBuff(Inop); return ;
        !           587:   longjmp(KCenvOfParser,2);
        !           588:   exit(1);
        !           589: }
        !           590:
        !           591: readcomment() {
        !           592:   int c;
        !           593:   while (1) {
        !           594:     c = fsgetc(Inop);
        !           595:     if (c == EOF) {
        !           596:       fprintf(stderr,"%d: Unexpected end of file in a comment.\n",Linenumber);
        !           597:       fsungetc(c,Inop); /* should change */
        !           598:       return;
        !           599:     }
        !           600:     if (c == '*') {
        !           601:       c = fsgetc(Inop);
        !           602:       if (c == '/') return;
        !           603:     }
        !           604:   }
        !           605: }
        !           606:
        !           607:
        !           608: char *readstring() {
        !           609:   static char strtmp[1024]; /* temporary */
        !           610:   static int size = 1024;
        !           611:   static char *str = strtmp;
        !           612:   int i=0;
        !           613:   int c;
        !           614:   static char *strwork;
        !           615:
        !           616:
        !           617:   if (Replace) putchar0('\"'); /* output " */
        !           618:   while (1) {
        !           619:     c = fsgetc(Inop);
        !           620:     if (Replace) putchar0(c);
        !           621:     if (c == '\"') {
        !           622:       str[i] = '\0';
        !           623:       return(str);
        !           624:     }
        !           625:     if (c == EOF) {
        !           626:       fprintf(stderr,"%d: Unexpected end of file in the string.\n",Linenumber);
        !           627:       fsungetc(c,Inop); /* should change */
        !           628:       str[i]= '\0';
        !           629:       return(str);
        !           630:     }
        !           631:     if (c == '\\') {
        !           632:       c = fsgetc(Inop);
        !           633:       if (Replace) {
        !           634:        putchar0(c);
        !           635:       }
        !           636:       if (c == EOF) {
        !           637:        fprintf(stderr,"%d: Unexpected end of file in the escape sequence.\n",Linenumber);
        !           638:        fsungetc(c,Inop); /* should change */
        !           639:        str[i]= '\0';
        !           640:        return(str);
        !           641:       }
        !           642:     }
        !           643:
        !           644:     str[i++] = c;
        !           645:     if (i >= size-10) {
        !           646:       size = size*2;
        !           647:       strwork = newString(size);
        !           648:       strcpy(strwork,str);
        !           649:       str = strwork;
        !           650:     }
        !           651:   }
        !           652: }
        !           653:
        !           654:
        !           655: readchar() {
        !           656:   int c;
        !           657:   if (Replace) putchar0('\'');
        !           658:   c = fsgetc(Inop); /* 'c.'   '\.c' */
        !           659:   if (Replace) putchar0(c);
        !           660:   if ( c == '\\') {
        !           661:     c = fsgetc(Inop); /* '\c.' */
        !           662:     if (Replace) putchar0(c);
        !           663:     if (c == EOF) {
        !           664:       fprintf(stderr,"%d: Unexpected end of file in the escape sequence.\n",Linenumber);
        !           665:       fsungetc(c,Inop); /* should change */
        !           666:       return(-1);
        !           667:     }
        !           668:     if (fsgetc(Inop) != '\'') {
        !           669:       fprintf(stderr,"%d: Error in single quote string (escape seq)\n",Linenumber);
        !           670:       return(c);
        !           671:     }
        !           672:     if (Replace) putchar0('\'');
        !           673:     return(c);
        !           674:   }
        !           675:
        !           676:   if (fsgetc(Inop) != '\'') {
        !           677:     fprintf(stderr,"%d: Error in single quote string\n",Linenumber);
        !           678:   }
        !           679:   if (Replace) putchar0('\'');
        !           680:   return(c);
        !           681: }
        !           682:
        !           683: putchar0(c)
        !           684: int c;
        !           685: {
        !           686:   if (c > 0) fputc(c,outfile);
        !           687: }
        !           688:
        !           689: printf0(s)
        !           690: char *s;
        !           691: {
        !           692:   int i = 0;
        !           693:   while (s[i] != '\0') putchar0(s[i++]);
        !           694: }
        !           695:
        !           696: printf1(s)
        !           697: char *s;
        !           698: {
        !           699:   int i = 0;
        !           700:   /* putchar0('K'); */   /* do your own replacement */
        !           701:   while (s[i] != '\0') putchar0(s[i++]);
        !           702: }
        !           703:
        !           704: isReserved(s)
        !           705: char *s;
        !           706: {
        !           707:   char *r[] = {"auto","break","case","char","const","continue",
        !           708:                 "default","do","double","else","enum","extern",
        !           709:                 "float","for","goto","if","int","long","register",
        !           710:                 "return","short","signed","sizeof","static","struct",
        !           711:                 "switch","typedef","union","unsigned","volatile",
        !           712:                 "void","while",
        !           713:               "print","module","local","def","sm1","load","Test","special",
        !           714:               "class","super","operator","final","extends",
        !           715:               "incetanceVariable","this","new","startOfThisClass",
        !           716:                "sizeOfThisClass","PSfor","OutputPrompt"};
        !           717:
        !           718:   int  val[]= {AUTO, BREAK, CASE, CHAR, CONST, CONTINUE, DEFAULT, DO, DOUBLE,
        !           719:                 ELSE, ENUM,
        !           720:                 EXTERN, FLOAT, FOR, GOTO, IF, INT, LONG, REGISTER,
        !           721:                 RETURN, SHORT, SIGNED, SIZEOF, STATIC, STRUCT, SWITCH,
        !           722:                 TYPEDEF, UNION,
        !           723:                 UNSIGNED, VOLATILE, VOID, WHILE,
        !           724:               PRINT,MODULE,LOCAL,DEF,SM1,LOAD,TEST,SPECIAL,
        !           725:               CLASS,SUPER,OPERATOR,FINAL,EXTENDS,INCETANCEVARIABLE,
        !           726:               THIS, NEW, STARTOFTHISCLASS, SIZEOFTHISCLASS,PSFOR,PROMPT};
        !           727:   int  n = 52; /* Length of the list above */
        !           728:   /* You have to change simple.y, too. */
        !           729:
        !           730:   int i;
        !           731:   for (i=0; i<n; i++) {
        !           732:     if (strcmp(r[i],s) == 0) {
        !           733:       if (Debug2) printf("\nReserved word: %s ---\n",s);
        !           734:       return(val[i]);
        !           735:     }
        !           736:   }
        !           737:   return(0);
        !           738:
        !           739: }
        !           740:
        !           741: shouldReplace(s)
        !           742: char *s;
        !           743: {
        !           744:   char *r[] = {"dummy"};
        !           745:   int n = 1;
        !           746:   int i;
        !           747:   for (i=0; i<n; i++) {
        !           748:     if (strcmp(r[i],s) == 0) {
        !           749:       /* printf("\n--- %s ---\n",s); */
        !           750:       return(1);
        !           751:     }
        !           752:   }
        !           753:   return(1); /* change to 0. */
        !           754: }
        !           755:
        !           756: /* --------------------------- protection of symbols ------------------ */
        !           757: #include "Stable/sm1symbol.h"
        !           758: int ips(objectp op) {
        !           759:   return(isProtectedSymbol(op->lc.str));
        !           760: }
        !           761: int isProtectedSymbol(char *s)
        !           762: {
        !           763:
        !           764:   static char **sn = SymbolTableOfsm1;
        !           765:   int size = SizeOfSymbolTableOfsm1;
        !           766:   int start = 0;
        !           767:   int end = size-1;
        !           768:   int r;
        !           769:
        !           770:   while (1) {
        !           771:     /* binary search */
        !           772:     if (start>end) break;
        !           773:     r = strcmp(s,sn[start+(end-start)/2]);
        !           774:     if (r == 0) {
        !           775:       fprintf(stderr,"Warning: Protected symbol %s is used as user variable.\n",s);
        !           776:       return(0);
        !           777:     }
        !           778:     if (r > 0) {
        !           779:       start = start + (end-start)/2 + 1;
        !           780:     } else if ( r < 0) {
        !           781:       end = start + (end-start)/2-1;
        !           782:     }
        !           783:   }
        !           784:   return(0);
        !           785: }
        !           786:
        !           787: /* ------------------ read from file ------------------- */
        !           788: struct stringBuf *newStringBuf(char *initstr)
        !           789: {
        !           790: #define INITSIZE  1024
        !           791:   struct stringBuf *stringBuf;
        !           792:   int limit;
        !           793:   stringBuf = mymalloc(sizeof(struct stringBuf));
        !           794:   if (stringBuf == (struct stringBuf *)NULL) {
        !           795:     fprintf(stderr,"No memory\n"); exit(10);
        !           796:   }
        !           797:   if (initstr == (char *)NULL) limit = INITSIZE;
        !           798:   else limit = strlen(initstr)+11;
        !           799:   stringBuf->str = mymalloc(sizeof(char)*limit);
        !           800:   stringBuf->ptr = 0;
        !           801:   stringBuf->limit = limit;
        !           802:   if (stringBuf->str == (char *)NULL) {
        !           803:     fprintf(stderr,"No memory\n"); exit(10);
        !           804:   }
        !           805:   if (initstr != (char *)NULL) {
        !           806:     strcpy(stringBuf->str,initstr);
        !           807:   } else (stringBuf->str)[0] = '\0';
        !           808:   return(stringBuf);
        !           809: }
        !           810:
        !           811: void doublingStringBuf(struct stringBuf *obuf)
        !           812: {
        !           813:   char *t;
        !           814:   t = mymalloc(sizeof(char)*(obuf->limit)*2);
        !           815:   if (t == (char *)NULL) {
        !           816:     fprintf(stderr,"No memory\n"); exit(10);
        !           817:   }
        !           818:   strcpy(t,obuf->str);
        !           819:   obuf->str = t;
        !           820:   obuf->limit *=  2;
        !           821: }
        !           822:
        !           823: void readlineFromFile(FILE *fp,struct stringBuf *obuf)
        !           824: {
        !           825:   int c;
        !           826:   obuf->ptr = 0; (obuf->str)[obuf->ptr]='\0';
        !           827:   while (1) {
        !           828:     c = fgetc(fp);
        !           829:     /* for debug.
        !           830:     if (c<' ') printf(" %x(%x) ",c,(int) fp); else printf(" <%c>(%x) ",c,(int) fp); fflush(NULL);
        !           831:     if (c == EOF) {
        !           832:       printf("Stackp=%d ",Stackp);
        !           833:       printf("File=%x\n",(int) fp); fflush(NULL);
        !           834:     }
        !           835:     */
        !           836:     if (c == EOF) {
        !           837:       (obuf->str)[obuf->ptr]='\0';
        !           838:       obuf->ptr = 0;
        !           839:       return;
        !           840:     }
        !           841:     (obuf->str)[(obuf->ptr)++] = c;
        !           842:     (obuf->str)[obuf->ptr] = '\0';
        !           843:     if (c == '\n') {
        !           844:       obuf->ptr = 0;
        !           845:       return;
        !           846:     }
        !           847:     if (obuf->ptr > obuf->limit - 5) doublingStringBuf(obuf);
        !           848:   }
        !           849: }
        !           850:
        !           851: static int popFile() {
        !           852:   if (Inop->tag == Sfile) {
        !           853:     if (Inop->lc.file != stdin) {
        !           854:       fclose(Inop->lc.file);
        !           855:       /* sendKan(10);  output In[n]= */
        !           856:     }
        !           857:   }
        !           858:   /* fprintf(stderr,"popFile(), Stackp=%d\n",Stackp); fflush(NULL);  */
        !           859:   if (Stackp <= 1) return(EOF);
        !           860:   Stackp--;
        !           861:   Linenumber = LinenumberStack[Stackp];
        !           862:   Inop = InopStack[Stackp];
        !           863:   if (Inop == (objectp) NULL) {
        !           864:     fprintf(stderr,"popFile: Inop==NULL\n"); fflush(stdout); /* was fflush(NULL); */
        !           865:     return(EOF);
        !           866:   }
        !           867:   if (Inop->tag == Sfile) {
        !           868:     if (Inop->lc.file != stdin) {
        !           869:       Interactive = 0;
        !           870:     }else{
        !           871:       Interactive = 1;
        !           872:     }
        !           873:   }
        !           874:   /* Saki = SakiStack[Stackp]; */
        !           875:   Saki = '\n';
        !           876:   return(Saki);
        !           877: }
        !           878:
        !           879: int fsgetc(objectp op) {
        !           880:   struct stringBuf *obuf;
        !           881:   char *str;
        !           882:   int ptr;
        !           883:   if (op->tag == Sfile) {
        !           884:     /* return(fgetc(op->lc.file));  non-buffered reading */
        !           885:     obuf = op->rc.sbuf;
        !           886:     str = obuf->str;
        !           887:     ptr = obuf->ptr;
        !           888:     if (str[ptr] == '\0') {
        !           889:       readlineFromFile(op->lc.file,obuf);
        !           890:       str = obuf->str;
        !           891:       ptr = obuf->ptr;
        !           892:     }
        !           893:     if (str[ptr] == '\0') {
        !           894:       return(popFile());
        !           895:     } else return( str[(obuf->ptr)++] );
        !           896:   }else if (op->tag == Slist) {
        !           897:     obuf = op->lc.sbuf;
        !           898:     str = obuf->str;
        !           899:     ptr = obuf->ptr;
        !           900:     if (str[ptr] == '\0') {
        !           901:       return(popFile());
        !           902:     } else return(str[(obuf->ptr)++]);
        !           903:   }else {
        !           904:     fprintf(stderr,"unknown tag?\n");
        !           905:   }
        !           906:   return(EOF);
        !           907: }
        !           908:
        !           909: int fsungetc(int c,objectp op) {
        !           910:   fprintf(stderr,"Sorry. fsungetc has not yet implemented.\n");
        !           911: }
        !           912:
        !           913: void clearInop() {
        !           914:   Saki = '\0';
        !           915:   Inop = (objectp) NULL;
        !           916:   Stackp = 0;
        !           917: }
        !           918:
        !           919: static void pushOld() {
        !           920:   LinenumberStack[Stackp]=Linenumber;
        !           921:   InopStack[Stackp] = Inop;
        !           922:   SakiStack[Stackp] = Saki;
        !           923:   /* fprintf(stderr,"pushOld, Stackp=%d, fp=%x\n",Stackp,(int)(Inop->lc.file)); fflush(NULL);  */
        !           924:   Stackp++;
        !           925:   if (Stackp >= SSIZE) {
        !           926:     fprintf(stderr,"Stackp overflow.\n");
        !           927:     exit(10);
        !           928:   }
        !           929: }
        !           930:
        !           931: void parseAfile(FILE *fp) {
        !           932:   int c;
        !           933:
        !           934:   pushOld();
        !           935:
        !           936:   Linenumber = 0;
        !           937:   Inop = newObject_d();
        !           938:   Inop->tag = Sfile;
        !           939:   Inop->lc.file = fp;
        !           940:   Inop->rc.sbuf = newStringBuf((char *)NULL);
        !           941:   if (fp != stdin) {
        !           942:     Interactive = 0;
        !           943:   }
        !           944:
        !           945: }
        !           946:
        !           947: void parseAstring(char *s)
        !           948: {
        !           949:   int c;
        !           950:   int prevLineNumber;
        !           951:   objectp prevInop;
        !           952:   int prevSaki;
        !           953:
        !           954:   pushOld();
        !           955:
        !           956:   Linenumber = 0;
        !           957:   Inop = newObject_d();
        !           958:   Inop->tag = Slist;
        !           959:   Inop->lc.sbuf = newStringBuf(s);
        !           960:
        !           961: }
        !           962:
        !           963: void loadFile(objectp op)
        !           964: {
        !           965:   FILE *fp;
        !           966:   char fname[1024];
        !           967:   if (op->tag != Sstring) {
        !           968:     fprintf(stderr,"File name must be given as an argment of load.\n");
        !           969:     return;
        !           970:   }
        !           971:   if (strlen(op->lc.str) > 1000) {
        !           972:     fprintf(stderr,"Too long file name.\n");
        !           973:     return;
        !           974:   }
        !           975:   fp = fopen(op->lc.str,"r");
        !           976:   if (fp == (FILE *)NULL) {
        !           977:     strcpy(fname,getLOAD_K_PATH());
        !           978:     strcat(fname,op->lc.str);
        !           979:     fp = fopen(fname,"r");
        !           980:     if (fp == (FILE *)NULL) {
        !           981:       strcpy(fname,LOAD_K_PATH);
        !           982:       strcat(fname,op->lc.str);
        !           983:       fp = fopen(fname,"r");
        !           984:       if (fp == (FILE *)NULL) {
        !           985:        fprintf(stderr,"Cannot open the file <<%s>> for loading in the current directory nor the library directories %s and %s.\n",op->lc.str,getLOAD_K_PATH(),LOAD_K_PATH);
        !           986:        return;
        !           987:       }
        !           988:     }
        !           989:   }
        !           990:   if (K00_verbose) fprintf(stderr,"Reading the file <<%s>>...  ",op->lc.str);
        !           991:   parseAfile(fp);
        !           992:   if (K00_verbose) fprintf(stderr,"\nClosed the file <<%s>>.\n",op->lc.str);
        !           993: }
        !           994:
        !           995: void loadFileWithCpp(objectp op)
        !           996: {
        !           997:   FILE *fp;
        !           998:   char fname[1024];
        !           999:   objectp ob;
        !          1000:   if (op->tag != Sstring) {
        !          1001:     fprintf(stderr,"File name must be given as an argment of load.\n");
        !          1002:     return;
        !          1003:   }
        !          1004:   if (strlen(op->lc.str) > 900) {
        !          1005:     fprintf(stderr,"Too long file name.\n");
        !          1006:     return;
        !          1007:   }
        !          1008:   system("/bin/rm -f k00.cppload.tmp");
        !          1009:   strcpy(fname,"/lib/cpp -P -lang-c++ <");
        !          1010:   strcat(fname,op->lc.str);
        !          1011:   strcat(fname,"  >k00.cppload.tmp");
        !          1012:   system(fname);
        !          1013:   ob = newObject_d();
        !          1014:   ob->tag = Sstring;
        !          1015:   ob->lc.str = "k00.cppload.tmp";
        !          1016:   loadFile(ob);
        !          1017: }
        !          1018:
        !          1019: void showStringBuff(objectp op)
        !          1020: {
        !          1021:   struct stringBuf *sb;
        !          1022:   int i;
        !          1023:   int start;
        !          1024:   int ptr;
        !          1025:   if (op->tag == Sfile) {
        !          1026:     sb = op->rc.sbuf;
        !          1027:   }else if (op->tag == Slist) {
        !          1028:     sb = op->lc.sbuf;
        !          1029:   }else fprintf(stderr,"Unknown tag.\n");
        !          1030:   ptr = sb->ptr;
        !          1031:   if (K00_verbose) {
        !          1032:     fprintf(stderr,"stringBuff ptr = %d, ",ptr);
        !          1033:     fprintf(stderr,"sb[ptr] = %x,%d",(sb->str)[ptr],(sb->str)[ptr]);
        !          1034:     fprintf(stderr,"Saki(yomi) = %x \n",Saki,Saki);
        !          1035:   }
        !          1036:   if (ptr == 0 && Saki == -1) {
        !          1037:     fprintf(stderr," ; was expected.\n");
        !          1038:   }
        !          1039:   start = (ptr-20<0?0:ptr-20);
        !          1040:   fprintf(stderr,"The error occured when the system is reading the line: ");
        !          1041:   for (i=start; i<ptr+20 && (sb->str)[i] >= ' '; i++) {
        !          1042:     fprintf(stderr,"%c",(sb->str)[i]);
        !          1043:     if (i==ptr-1) fprintf(stderr,"<<error>>  \n");
        !          1044:   }
        !          1045:   fprintf(stderr,"\n");
        !          1046: }
        !          1047:
        !          1048: char *getLOAD_K_PATH() {
        !          1049:   char *p;
        !          1050:   char *p2;
        !          1051:   p = getenv("LOAD_K_PATH");
        !          1052:   if (p == NULL) {
        !          1053:     return("/usr/local/lib/kxx97/yacc/");
        !          1054:   }else{
        !          1055:     if (strlen(p) == 0) return(p);
        !          1056:     if (p[strlen(p)-1] == '/') return(p);
        !          1057:     /* Add / */
        !          1058:     p2 = (char *) malloc(sizeof(char)*(strlen(p)+3));
        !          1059:     if (p2 == NULL) { fprintf(stderr,"No more memory.\n"); exit(10); }
        !          1060:     strcpy(p2,p); strcat(p2,"/");
        !          1061:     return(p2);
        !          1062:   }
        !          1063: }
        !          1064:

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