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