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