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