Annotation of OpenXM/src/util/oxlistlocalf.c, Revision 1.1
1.1 ! takayama 1: /* $OpenXM$ */
! 2: #include <stdio.h>
! 3: #include <ctype.h>
! 4: #include <stdlib.h>
! 5: #include <sys/types.h>
! 6: #include <sys/stat.h>
! 7: #include "oxlistlocalf.h"
! 8:
! 9: objectp KClval;
! 10: int Replace = 0;
! 11: int Linenumber = 0;
! 12: int Saki = 0;
! 13: int Debug2 = 0;
! 14: FILE *Inop = NULL;
! 15:
! 16: char *readstring(void);
! 17:
! 18: void *mymalloc(int n) { return malloc(n); }
! 19:
! 20: objectp newObject_d() {
! 21: objectp obj;
! 22: obj = (objectp) mymalloc(sizeof(struct Object));
! 23: if (obj == (objectp) NULL) {
! 24: fprintf(stderr,"Error: No more memory in newObject_d().\n");
! 25: exit(10);
! 26: }
! 27: obj->tag = Snull; obj->attr = NULL;
! 28: return(obj);
! 29: }
! 30:
! 31: char *newString(int size) {
! 32: char *str;
! 33: if (size <= 0) size=1;
! 34: str = (char *)mymalloc(sizeof(char)*size);
! 35: if (str == (char *)NULL) {
! 36: fprintf(stderr,"Error: No more memory in newObject_d().\n");
! 37: exit(10);
! 38: }
! 39: return(str);
! 40: }
! 41:
! 42: void printObject_d(FILE *fp,objectp op)
! 43: {
! 44: if (op == (objectp) NULL) {
! 45: fprintf(fp," <null objectp> ");
! 46: return;
! 47: }
! 48: switch(op->tag) {
! 49: case Sinteger:
! 50: fprintf(fp,"%d",op->lc.ival);
! 51: return;
! 52: break;
! 53: case Sstring:
! 54: fprintf(fp,"%s",op->lc.str);
! 55: return;
! 56: break;
! 57: default:
! 58: fprintf(stderr,"printObject_d(): Sorry. Not implemented.");
! 59: break;
! 60: }
! 61: }
! 62:
! 63: void printObjectSymbol(objectp op)
! 64: {
! 65: static char tmp[1024];
! 66: if (op == (objectp) NULL) {
! 67: printf(" <null objectp> ");
! 68: return;
! 69: }
! 70: switch(op->tag) {
! 71: case Sinteger:
! 72: printf("%d",op->lc.ival);
! 73: return; break;
! 74: case Sstring:
! 75: printf("%s",op->lc.str);
! 76: return; break;
! 77: default:
! 78: fprintf(stderr,"(printObjectSymbol(): Sorry. Not implemented.)");
! 79: break;
! 80: }
! 81: }
! 82:
! 83: int KClex() {
! 84: extern int Saki; /* Saki-yomi */
! 85: int d;
! 86: int state = 0;
! 87: int i;
! 88: static char nametmp[1024];
! 89: char *name = nametmp;
! 90: char *str;
! 91: KClval = (objectp) NULL;
! 92: while (1) {
! 93: switch( state ) {
! 94: case 0:
! 95: /* printf(" <%x> ",Saki); */
! 96: if (Saki == EOF) {
! 97: return(Saki);
! 98: }
! 99: if (Saki <= ' ') {
! 100: if (Saki == '\n') ++Linenumber;
! 101: if (Replace) putchar0(Saki);
! 102: Saki = fsgetc(Inop); break;
! 103: }
! 104: if ( Saki == '\"' ) {
! 105: str = readstring();
! 106: /* if (Mydebug) printf("[string: %s]",str); */
! 107:
! 108: KClval = newObject_d();
! 109: KClval->tag = Sstring;
! 110: KClval->lc.str = newString(strlen(str)+1);
! 111: strcpy(KClval->lc.str,str);
! 112:
! 113: Saki = fsgetc(Inop);
! 114: return(QUOTE);
! 115: break;
! 116: }
! 117: if (Saki == '\'' ) {
! 118: d = readchar();
! 119: Saki = fsgetc(Inop);
! 120:
! 121: KClval = newObject_d();
! 122: KClval->tag = Sinteger;
! 123: KClval->lc.ival = d;
! 124:
! 125: return(SINGLEQUOTE);
! 126: }
! 127: /* single */
! 128: if ( Saki == '(' || Saki == ')' || Saki == ';' ||
! 129: Saki == '{' || Saki == '}' || Saki == ',' ||
! 130: Saki == '[' || Saki == ']' || Saki == '~' ||
! 131: Saki == '?' || Saki == '.' || Saki == '$') {
! 132: d = Saki; Saki = fsgetc(Inop);
! 133: if (Replace) putchar0(d);
! 134: return(d);
! 135: }
! 136:
! 137: /* single or combination =, == */
! 138: if ( Saki == '=') {
! 139: state = 51; Saki = fsgetc(Inop); break;
! 140: } else if ( Saki == '<' ) {
! 141: state = 52; Saki = fsgetc(Inop); break;
! 142: } else if ( Saki == '>' ) {
! 143: state = 53; Saki = fsgetc(Inop); break;
! 144: } else if ( Saki == '/' ) {
! 145: state = 54; Saki = fsgetc(Inop); break;
! 146: } else if ( Saki == '&' ) {
! 147: state = 55; Saki = fsgetc(Inop); break;
! 148: } else if ( Saki == '|' ) {
! 149: state = 56; Saki = fsgetc(Inop); break;
! 150: } else if ( Saki == '!' ) {
! 151: state = 57; Saki = fsgetc(Inop); break;
! 152: } else if ( Saki == ':' ) {
! 153: state = 58; Saki = fsgetc(Inop); break;
! 154: } else if ( Saki == '+' ) {
! 155: state = 59; Saki = fsgetc(Inop); break;
! 156: } else if ( Saki == '-' ) {
! 157: state = 60; Saki = fsgetc(Inop); break;
! 158: } else if ( Saki == '%' ) {
! 159: state = 61; Saki = fsgetc(Inop); break;
! 160: } else if ( Saki == '^' ) {
! 161: state = 62; Saki = fsgetc(Inop); break;
! 162: } else if ( Saki == '*' ) {
! 163: state = 63; Saki = fsgetc(Inop); break;
! 164: } else ;
! 165:
! 166:
! 167: /* else : Identifier or function names. */
! 168: name[0] = Saki; i=1; name[i] = '\0';
! 169: Saki = fsgetc(Inop);
! 170: if (isdigit(name[0])) {
! 171: /*while (isdigit(Saki) || isalpha(Saki) || Saki=='.') */
! 172: while (isdigit(Saki) || isalpha(Saki)) {
! 173: name[i++] = Saki; name[i] = '\0';
! 174: Saki = fsgetc(Inop);
! 175: }
! 176: }else{
! 177: while (isdigit(Saki) || isalpha(Saki) || (Saki=='_') ||
! 178: ( Saki>= 256)) {
! 179: name[i++] = Saki; name[i] = '\0';
! 180: Saki = fsgetc(Inop);
! 181: }
! 182: }
! 183: /*if (Mydebug) printf("identifier string=[%s]",name);*/
! 184: if (isdigit(name[0])) {
! 185: /****************************
! 186: /**case : machine integer.
! 187: KClval = newObject_d();
! 188: KClval->tag = Sinteger;
! 189: sscanf(name,"%d",&(KClval->lc.ival));*************/
! 190: /* Other cases. */
! 191: KClval = newObject_d();
! 192: KClval->tag = Sstring;
! 193: KClval->lc.str = newString(strlen(name)+1);
! 194: strcpy(KClval->lc.str,name);
! 195: return(NUMBER);
! 196: break;
! 197: } /* else : Identifier case.*/
! 198:
! 199: if (d = isReserved(name)) {
! 200: if (Replace) printf0(name);
! 201: return(d);
! 202: } else {
! 203: if (Replace) {
! 204: if (shouldReplace(name))
! 205: printf1(name); /* do your own replacement in printf1*/
! 206: else
! 207: printf0(name);
! 208: }
! 209: KClval = newObject_d();
! 210: KClval->tag = Sstring;
! 211: KClval->lc.str = newString(strlen(name)+1);
! 212: strcpy(KClval->lc.str,name);
! 213: return(ID);
! 214: }
! 215: break;
! 216:
! 217: case 51:
! 218: if (Replace) putchar0('=');
! 219: if ( Saki == '=') {
! 220: if (Replace) putchar0('=');
! 221: Saki = fsgetc(Inop);state = 0;return(EQUAL); /* == */
! 222: }else{
! 223: state = 0;return('=');
! 224: }
! 225: break;
! 226: case 52: /* 52 --- 60 tmporary return values */
! 227: if (Replace) putchar0('<');
! 228: if ( Saki == '=') {
! 229: if (Replace) putchar0('=');
! 230: Saki = fsgetc(Inop);state = 0;return(LESSEQUAL); /* <= */
! 231: } else if ( Saki == '<') {
! 232: if (Replace) putchar0('<');
! 233: Saki = fsgetc(Inop);state = 0;return(LEFTSHIFT); /* << */
! 234: }else{
! 235: state = 0;return('<');
! 236: }
! 237: break;
! 238: case 53:
! 239: if (Replace) putchar0('>');
! 240: if ( Saki == '=') {
! 241: if (Replace) putchar0('=');
! 242: Saki = fsgetc(Inop);state = 0;return(GREATEREQUAL); /* >= */
! 243: } else if ( Saki == '>') {
! 244: if (Replace) putchar0('>');
! 245: Saki = fsgetc(Inop);state = 0;return(RIGHTSHIFT); /* >> */
! 246: }else{
! 247: state = 0;return('>');
! 248: }
! 249: break;
! 250: case 54:
! 251: if ( Saki == '*') {
! 252: readcomment();
! 253: Saki = fsgetc(Inop);state = 0;break; /* clike-comment */
! 254: }else if (Saki == '/') {
! 255: readcomment2();
! 256: Saki = fsgetc(Inop);state = 0;break; /* comment by // */
! 257: }else {
! 258: if (Replace) putchar0('/');
! 259: state = 0;return('/');
! 260: }
! 261: break;
! 262: case 55:
! 263: if (Replace) putchar0('&');
! 264: if ( Saki == '&') {
! 265: if (Replace) putchar0('&');
! 266: Saki = fsgetc(Inop);state = 0;return(AND); /* && */
! 267: }else{
! 268: state = 0;return('&');
! 269: }
! 270: break;
! 271: case 56:
! 272: if (Replace) putchar0('|');
! 273: if ( Saki == '|') {
! 274: if (Replace) putchar0('|');
! 275: Saki = fsgetc(Inop);state = 0;return(OR); /* || */
! 276: }else{
! 277: state = 0;return('|');
! 278: }
! 279: break;
! 280: case 57:
! 281: if (Replace) putchar0('!');
! 282: if ( Saki == '=') {
! 283: if (Replace) putchar0('=');
! 284: Saki = fsgetc(Inop);state = 0;return(NOTEQUAL); /* != */
! 285: }else{
! 286: state = 0;return('!');
! 287: }
! 288: break;
! 289: case 58:
! 290: if (Replace) putchar0(':');
! 291: if ( Saki == '=') {
! 292: if (Replace) putchar0('=');
! 293: Saki = fsgetc(Inop);state = 0;return(PUT); /* := */
! 294: }else{
! 295: state = 0;return(':');
! 296: }
! 297: break;
! 298: case 59:
! 299: if (Replace) putchar0('+');
! 300: if ( Saki == '+') {
! 301: if (Replace) putchar0('+');
! 302: Saki = fsgetc(Inop);state = 0;return(INCREMENT); /* ++ */
! 303: }else{
! 304: state = 0;return('+');
! 305: }
! 306: break;
! 307: case 60:
! 308: if (Replace) putchar0('-');
! 309: if ( Saki == '-') {
! 310: if (Replace) putchar0('-');
! 311: Saki = fsgetc(Inop);state = 0;return(DECREMENT); /* -- */
! 312: }else if (Saki == '>') {
! 313: if (Replace) putchar0('>');
! 314: Saki = fsgetc(Inop);state = 0;return(MEMBER); /* -> */
! 315: }else{
! 316: state = 0;return('-');
! 317: }
! 318: break;
! 319: case 61:
! 320: if (Replace) putchar0('%');
! 321: if ( Saki == '=') {
! 322: if (Replace) putchar0('=');
! 323: Saki = fsgetc(Inop);state = 0;return(RESIDUEPUT); /* %= */
! 324: }else{
! 325: state = 0;return('%');
! 326: }
! 327: break;
! 328: case 62:
! 329: if (Replace) putchar0('^');
! 330: if ( Saki == '=') {
! 331: if (Replace) putchar0('=');
! 332: Saki = fsgetc(Inop);state = 0;return(NEGATEPUT); /* ^= */
! 333: }else{
! 334: state = 0;return('^');
! 335: }
! 336: break;
! 337: case 63:
! 338: if (Replace) putchar0('*');
! 339: if ( Saki == '=') {
! 340: if (Replace) putchar0('=');
! 341: Saki = fsgetc(Inop);state = 0;return(MULTPUT); /* *= */
! 342: }else{
! 343: state = 0;return('*');
! 344: }
! 345: break;
! 346:
! 347:
! 348: default:
! 349: fprintf(stderr,"%d: Error in KClex().\n",Linenumber);
! 350: }
! 351: }
! 352:
! 353: }
! 354:
! 355:
! 356: readcomment() {
! 357: int c;
! 358: while (1) {
! 359: c = fsgetc(Inop);
! 360: if (c == EOF) {
! 361: fprintf(stderr,"%d: Unexpected end of file in a comment.\n",Linenumber);
! 362: return 0;
! 363: }
! 364: if (c == '*') {
! 365: c = fsgetc(Inop);
! 366: if (c == '/') return 0;
! 367: }
! 368: }
! 369: }
! 370:
! 371: readcomment2() {
! 372: int c;
! 373: while (1) {
! 374: c = fsgetc(Inop);
! 375: if (c == EOF) {
! 376: fprintf(stderr,"%d: Unexpected end of file in a comment.\n",Linenumber);
! 377: return 0;
! 378: }
! 379: if (c == '\n') {
! 380: return 0;
! 381: }
! 382: }
! 383: }
! 384:
! 385:
! 386: char *readstring() {
! 387: static char strtmp[1024]; /* temporary */
! 388: static int size = 1024;
! 389: static char *str = strtmp;
! 390: int i=0;
! 391: int c;
! 392: static char *strwork;
! 393:
! 394:
! 395: if (Replace) putchar0('\"'); /* output " */
! 396: while (1) {
! 397: c = fsgetc(Inop);
! 398: if (Replace) putchar0(c);
! 399: if (c == '\"') {
! 400: str[i] = '\0';
! 401: return(str);
! 402: }
! 403: if (c == EOF) {
! 404: fprintf(stderr,"%d: Unexpected end of file in the string.\n",Linenumber);
! 405: str[i]= '\0';
! 406: return(str);
! 407: }
! 408: if (c == '\\') {
! 409: c = fsgetc(Inop);
! 410: if (Replace) {
! 411: putchar0(c);
! 412: }
! 413: if (c == EOF) {
! 414: fprintf(stderr,"%d: Unexpected end of file in the escape sequence.\n",Linenumber);
! 415: str[i]= '\0';
! 416: return(str);
! 417: }
! 418: }
! 419:
! 420: str[i++] = c;
! 421: if (i >= size-10) {
! 422: size = size*2;
! 423: strwork = newString(size);
! 424: strcpy(strwork,str);
! 425: str = strwork;
! 426: }
! 427: }
! 428: }
! 429:
! 430:
! 431: readchar() {
! 432: int c;
! 433: if (Replace) putchar0('\'');
! 434: c = fsgetc(Inop); /* 'c.' '\.c' */
! 435: if (Replace) putchar0(c);
! 436: if ( c == '\\') {
! 437: c = fsgetc(Inop); /* '\c.' */
! 438: if (Replace) putchar0(c);
! 439: if (c == EOF) {
! 440: fprintf(stderr,"%d: Unexpected end of file in the escape sequence.\n",Linenumber);
! 441: return(-1);
! 442: }
! 443: if (fsgetc(Inop) != '\'') {
! 444: fprintf(stderr,"%d: Error in single quote string (escape seq)\n",Linenumber);
! 445: return(c);
! 446: }
! 447: if (Replace) putchar0('\'');
! 448: return(c);
! 449: }
! 450:
! 451: if (fsgetc(Inop) != '\'') {
! 452: fprintf(stderr,"%d: Error in single quote string\n",Linenumber);
! 453: }
! 454: if (Replace) putchar0('\'');
! 455: return(c);
! 456: }
! 457:
! 458: putchar0(c)
! 459: int c;
! 460: {
! 461: if (c > 0) putchar(c);
! 462: }
! 463:
! 464: printf0(s)
! 465: char *s;
! 466: {
! 467: int i = 0;
! 468: while (s[i] != '\0') putchar0(s[i++]);
! 469: }
! 470:
! 471: printf1(s)
! 472: char *s;
! 473: {
! 474: int i = 0;
! 475: /* putchar0('K'); */ /* do your own replacement */
! 476: while (s[i] != '\0') putchar0(s[i++]);
! 477: }
! 478:
! 479: isReserved(s)
! 480: char *s;
! 481: {
! 482: char *r[] = {"auto","break","case","char","const","continue",
! 483: "default","do","double","else","enum","extern",
! 484: "float","for","goto","if","int","long","register",
! 485: "return","short","signed","sizeof","static","struct",
! 486: "switch","typedef","union","unsigned","volatile",
! 487: "void","while",
! 488: "print","module","local","def","sm1","load","Test","special",
! 489: "class","super","operator","final","extends",
! 490: "incetanceVariable","this","new","startOfThisClass",
! 491: "sizeOfThisClass","PSfor","OutputPrompt"};
! 492:
! 493: int val[]= {AUTO, BREAK, CASE, CHAR, CONST, CONTINUE, DEFAULT, DO, DOUBLE,
! 494: ELSE, ENUM,
! 495: EXTERN, FLOAT, FOR, GOTO, IF, INT, LONG, REGISTER,
! 496: RETURN, SHORT, SIGNED, SIZEOF, STATIC, STRUCT, SWITCH,
! 497: TYPEDEF, UNION,
! 498: UNSIGNED, VOLATILE, VOID, WHILE,
! 499: PRINT,MODULE,LOCAL,DEF,SM1,LOAD,TEST,SPECIAL,
! 500: CLASS,SUPER,OPERATOR,FINAL,EXTENDS,INCETANCEVARIABLE,
! 501: THIS, NEW, STARTOFTHISCLASS, SIZEOFTHISCLASS,PSFOR,PROMPT};
! 502: int n = 52; /* Length of the list above */
! 503: /* You have to change simple.y, too. */
! 504:
! 505: int i;
! 506: for (i=0; i<n; i++) {
! 507: if (strcmp(r[i],s) == 0) {
! 508: if (Debug2) printf("\nReserved word: %s ---\n",s);
! 509: return(val[i]);
! 510: }
! 511: }
! 512: return(0);
! 513:
! 514: }
! 515:
! 516: shouldReplace(s)
! 517: char *s;
! 518: {
! 519: char *r[] = {"dummy"};
! 520: int n = 1;
! 521: int i;
! 522: for (i=0; i<n; i++) {
! 523: if (strcmp(r[i],s) == 0) {
! 524: /* printf("\n--- %s ---\n",s); */
! 525: return(1);
! 526: }
! 527: }
! 528: return(1); /* change to 0. */
! 529: }
! 530:
! 531:
! 532:
! 533: int fsgetc(FILE *fp) {
! 534: int c;
! 535: return(fgetc(fp));
! 536: }
! 537:
! 538:
! 539: int main(int argc,char *argv[]) {
! 540: int c;
! 541: if (argc <= 1) Inop = stdin;
! 542: else {
! 543: Inop = fopen(argv[1],"r");
! 544: if (Inop == NULL) {
! 545: fprintf(stderr,"File %s is not found.\n",argv[1]); return(-1);
! 546: }
! 547: }
! 548: while ((c=KClex()) != EOF) {
! 549: if (c == DEF) {
! 550: c=KClex();
! 551: if (c != ID) {
! 552: fprintf(stderr,"ID (identifier) is expected, but the token type is %d\n",c);
! 553: }else {
! 554: /* printf("ID=%s",(KClval->lc).str); */
! 555: printf("localf %s$\n",(KClval->lc).str);
! 556: }
! 557: }
! 558: }
! 559: return(0);
! 560: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>