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