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