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