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