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