Annotation of OpenXM/src/k097/simple.y, Revision 1.5
1.5 ! takayama 1: /* $OpenXM: OpenXM/src/k097/simple.y,v 1.4 2002/11/28 01:25:36 takayama Exp $ */
1.1 maekawa 2: /* simple.y 1996, 1/1 --- 1/6 */
3: /* simple.y.ccc, 1996, 4/1 --- */
4: %{
5: #include <stdio.h>
6: #include "d.h"
7: %}
8:
9:
10: %token ID QUOTE SINGLEQUOTE NUMBER
11:
12: /* You have to change isReserved() [d.c], too */
13: %token CLASS SUPER OPERATOR FINAL EXTENDS INCETANCEVARIABLE
14: %token THIS NEW SIZEOFTHISCLASS STARTOFTHISCLASS
15: %token MODULE PRINT LOCAL DEF SM1 LOAD TEST SPECIAL
16: %token AUTO BREAK CASE CHAR CONST CONTINUE DEFAULT DO DOUBLE ELSE ENUM
17: %token EXTERN FLOAT FOR GOTO IF INT LONG REGISTER
18: %token RETURN SHORT SIGNED SIZEOF STATIC STRUCT SWITCH TYPEDEF UNION
19: %token UNSIGNED VOLATILE VOID WHILE PSFOR PROMPT
20:
21: %right '=' PUT MULTPUT NEGATEPUT RESIDUEPUT
22:
23: %left OR
24: %left AND
25: %left '|'
26: %left '&'
27: %left EQUAL NOTEQUAL
28: %left '<' '>' LESSEQUAL GREATEREQUAL
29: %left LEFTSHIFT RIGHTSHIFT
30: %left '+' '-'
31: %left '*' '/' '%'
32:
33: %left UNARYMINUS '!' INCREMENT DECREMENT '~'
34: %right '^'
35:
36: %left '.'
37: %left MEMBER
38: %%
39:
40: /* Mathematica-like parser */
41: program
42: : globalstatements
43: ;
44:
45: globalstatements
46: :
47: sentence globalstatements
48: | sentence
49: ;
50:
51: sentence /* { ... } {...} : ; does not need. */
52: :
53: for
54: | PSfor
55: | while
56: | if
57: | functionDefinition {sendKan(0); }
58: | exp ';' {sendKan(0); }
59: | exp ':' {pkkan(" [ ] {showln} sendmsg2 \n"); sendKan(0); }
60: | ';' {sendKan(0); }
61: | class_definition {sendKan(0); }
62: | error ';'
63: | PROMPT ';' {sendKan(10); }
64: ;
65:
66: statements
67: :
68: localsentence statements
69: | localsentence
70: ;
71:
72: localsentence
73: :
74: for
75: | PSfor
76: | while
77: | if
78: | exp ';'
79: | return ';'
80: | break ';'
81: | ';'
82: ;
83:
84: argList
85: : exp ',' argList
86: | exp /* It does not cause reduce-reduce conflict
87: between "program"! */
88: ;
89:
90: return :
91: RETURN exp { pkkan(" /FunctionValue set {/ExitPoint goto} exec %%return\n");}
92: | RETURN {pkkan(" {/ExitPoint goto} exec %%return void\n");}
93: ;
94:
95: break : BREAK { pkkan(" exit "); }
1.4 takayama 96: ;
1.1 maekawa 97:
1.4 takayama 98: list_prefix
1.1 maekawa 99: :
100: '[' { pkkan("[ "); }
101: ;
102: list
103: : list_prefix ']' { pkkan(" ] "); }
104: | list_prefix argList ']' { pkkan(" ] "); }
105: ;
106:
107: curryBrace
108: : '{' '}'
109: | '{' statements '}'
110: ;
111:
112: if_prefix
113: : IF '(' exp ')' { pkkan(" %% if-condition\n { %%ifbody\n"); }
114: ;
115: if_body
116: : localsentence { pkkan(" }%%end if if body\n { %%if- else part\n"); }
117: | curryBrace { pkkan(" }%%end if if body\n { %%if- else part\n"); }
118: ;
119: if
120: : if_prefix if_body { pkkan(" } ifelse\n"); }
121: /* The line cause 1 shift/reduce conflict. Look up a book. */
122: | if_prefix if_body ELSE localsentence { pkkan(" } ifelse\n"); }
123: | if_prefix if_body ELSE curryBrace { pkkan(" } ifelse\n"); }
124: ;
125:
126:
127: for_prefix0
128: : FOR '(' exp ';' { pkkan("%%for init.\n%%for\n{ "); }
129: | FOR '(' ';' { pkkan("%%nothing for init.\n%%for\n{ "); }
130: ;
131: for_exit
132: : exp ';' { pkkan(" { } {exit} ifelse\n[ {%%increment\n"); }
133: | ';' { pkkan("%%no exit rule.\n[ {%%increment\n"); }
134: ;
135: for_inc
136: : exp { pkkan("} %%end of increment{A}\n"); }
137: ;
138: /* [{ A } { B } roll 2 1] {exec} map ---> B A */
139: for_prefix
140: : for_prefix0 for_exit for_inc ')'
141: { pkkan("{%%start of B part{B}\n"); }
142: | for_prefix0 for_exit ')'
143: { pkkan(" } %% dummy A\n{%%start of B part{B}\n"); }
144: ;
145: for
146: : for_prefix curryBrace
147: { pkkan("} %% end of B part. {B}\n");
148: pkkan(" 2 1 roll] {exec} map pop\n} loop %%end of for\n"); }
1.4 takayama 149: ;
1.1 maekawa 150: PSfor_prefix
151: : PSFOR '(' ID '=' exp ';'
152: { pkkan("%%PSfor initvalue.\n (integer) data_conversion \n");
153: ips($3);
154: }
155: ID '<' exp ';' ID INCREMENT ')'
156: { ips($7); ips($10);
157: /* They must be equal id, but it is not checked. */
158: pkkan(" (1).. sub (integer) data_conversion 1 2 -1 roll \n");
159: pkkan("{ %% for body\n (universalNumber) data_conversion ");
160: pkkan("/"); printObjectSymbol($3); pkkan(" set \n");
161: }
162: ;
163: PSfor
164: : PSfor_prefix curryBrace
165: { pkkan(" } for \n"); }
166: ;
167:
168: while_prefix0
169: : WHILE '(' { pkkan("\n%%while\n{ "); }
170: ;
171:
172: while_prefix
173: : while_prefix0 exp ')' { pkkan(" { } {exit} ifelse\n "); }
174: | while_prefix0 ')' { pkkan("%%no exit condition.\n "); }
175: ;
176:
177: while
178: : while_prefix curryBrace { pkkan("} loop\n"); }
179: ;
180:
181:
182: print
183: : PRINT '(' exp ')' { pkkan(" print\n");}
184: ;
185:
186: sm1 :
187: SM1 '(' sm1ArgList ')'
188: ;
189:
190: load :
191: LOAD '(' QUOTE ')' { loadFile($3); }
192: | LOAD '(' ID ')' { loadFile($3); }
193: | LOAD ID { loadFile($2); }
194: | LOAD QUOTE { loadFile($2); }
195: | LOAD '[' QUOTE ']' { loadFileWithCpp($3); }
1.4 takayama 196: ;
1.1 maekawa 197: sm1ArgList :
198: | sm1ArgList ',' QUOTE { pkkan(" "); printObjectSymbol($3); pkkan(" "); }
199: | QUOTE { pkkan(" "); printObjectSymbol($1); pkkan(" "); }
200: | sm1ArgList ',' ID { pkkan(" "); printObjectSymbol($3); pkkan(" "); }
201: | ID { pkkan(" "); printObjectSymbol($1); pkkan(" "); }
202: ;
203:
204: primitive
205: : ID
206: { int tmp0;
207: if ((tmp0 = K00getIncetanceVariable(objectSymbolToString($1))) != -1) {
208: pkkan(" this "); pkkanInteger(tmp0); pkkan(" get ");
209: } else {
210: printObjectSymbol($1); pkkan(" ");
211: }
212: }
213: | QUOTE { pkkan("("); printObjectSymbol($1); pkkan(") "); }
214: | SINGLEQUOTE
215: | NUMBER { pkkan("("); printObjectSymbol($1); pkkan(").. ");}
216: | THIS { pkkan(" this "); }
217: | '(' exp ')'
218: | functionCall
219: | list
220: | print
221: | sm1
222: | load
223: | test
224: | special
225: | set
226: | arrayValue
227: ;
228:
229: functionCall_prefix
230: :
1.5 ! takayama 231: ID '(' { pkkan("this [ %% function args \n"); $$ = $1; }
1.1 maekawa 232: ;
233:
234: /* function call. cf. Schreiner 19p */
235: /* If you want to have a strong binding s.t.
236: ID[1,2], then you need to define a new non-terminal
237: such as primitive. ????*/
238: functionCall
239: :
240: functionCall_prefix argList ')'
241: {pkkan("] {");printObjectSymbol($1);pkkan("} sendmsg2 \n");}
242: | functionCall_prefix ')'
243: {pkkan("] {");printObjectSymbol($1);pkkan("} sendmsg2 \n");}
244:
245: ;
246: idList
247: : idList ',' ID { pkkan("/");printObjectSymbol($3); pkkan(" "); ips($3);}
248: | ID { pkkan("/");printObjectSymbol($1); pkkan(" "); ips($1);}
249: ;
250:
251: functionHead
252: : DEF ID
253: { pkkan("/"); printObjectSymbol($2); pkkan(" {\n"); ips($2);
254: pkdebug("In function : ", objectSymbolToString($2),
255: " of class ",K00getCurrentContextName());
256: pkkan(" /Arglist set /Argthis set /FunctionValue [ ] def\n [/this ");
257: $$ = $2;}
258: ;
259:
260: voidfunctionHead
261: : DEF VOID ID
262: { pkkan("/"); printObjectSymbol($3); pkkan(" {\n"); ips($3);
263: pkdebug("In function : ", objectSymbolToString($3),
264: " of class ",K00getCurrentContextName());
265: pkkan(" /Arglist set /Argthis set /FunctionValue [ ] def\n [/this ");
266: $$ = $2;}
267: ;
268:
269: functionArg
270: : '(' idList ')' '{'
271: {pkkan(" ] /ArgNames set ArgNames pushVariables [ %%function body\n");
272: pkkan(" [Argthis] Arglist join ArgNames mapset\n");}
273: | '(' ')' '{'
274: {pkkan(" ] /ArgNames set ArgNames pushVariables [ %%function body\n");
275: pkkan(" [Argthis] ArgNames mapset\n"); }
276: ;
277:
278: functionBody
279: :
280: declaration extern_declaration statements '}'
281: { pkkan("/ExitPoint ]pop popVariables %%pop the local variables\n"); }
282: | extern_declaration statements '}'
283: ;
284:
285:
286: functionDefinition
287: : functionHead functionArg functionBody
288: {pkkan("/ExitPoint ]pop popVariables %%pop argValues\n");
289: pkdebug2();
290: pkkan("FunctionValue } def\n%%end of function\n\n"); }
291: | operatorfunctionHead functionArg functionBody
292: {pkkan("/ExitPoint ]pop popVariables %%pop argValues\n");
293: pkdebug2();
294: pkkan("FunctionValue } def\n%%end of function\n\n"); }
295: | voidfunctionHead functionArg functionBody
296: {pkkan("/ExitPoint ]pop popVariables %%pop argValues\n");
297: pkdebug2();
298: pkkan("} def\n%%end of function\n\n"); }
299: ;
300:
301: declaration_prefix
302: : LOCAL { pkkan("[ %%start of local variables\n"); }
303: ;
304:
305: declaration
306: : declaration_prefix idList ';'
307: { pkkan("] pushVariables [ %%local variables\n"); }
308: ;
309:
310: extern_idList
311: : extern_idList ',' ID
312: { ;}
313: | ID { ;}
314: ;
315:
316: extern_declaration_prefix
317: : EXTERN { ; }
318: ;
319:
320: extern_declaration
321: : extern_declaration_prefix extern_idList ';'
322: { ; }
323: |
324: ;
325:
326: arrayValue :
327: array { pkkan(" Get\n"); }
328: ;
329: array :
330: array_prefix arrayIndex ']' {pkkan(" ] "); }
331: ;
332: arrayIndex :
333: exp ',' arrayIndex
334: | exp
335: ;
336: array_prefix :
337: ID '['
338: { int tmp0;
339: if ((tmp0 = K00getIncetanceVariable(objectSymbolToString($1))) != -1) {
340: pkkan(" this "); pkkanInteger(tmp0); pkkan(" get [");
341: } else {
342: printObjectSymbol($1); pkkan(" [");
343: }
344: }
345: ;
346:
347:
348: id_set
349: : ID { $$ = $1;}
350: ;
351: set
352: : id_set '=' exp
353: { int tmp0;
354: if ((tmp0 = K00getIncetanceVariable(objectSymbolToString($1))) != -1) {
355: pkkan(" this "); pkkanInteger(tmp0);
356: pkkan(" 3 -1 roll put\n");
357: }else {
358: pkkan("/"); printObjectSymbol($1); pkkan(" "); ips($1); pkkan(" set\n");
359: }
360: }
361: | array '=' exp { pkkan(" Put\n"); }
362: | THIS '=' exp { pkkan(" /this set \n"); }
363: ;
364:
365: exp
366: : primitive
367: | new
368: | '!' exp { pkkan(" not\n"); }
369: | '~' exp
370: | '-' exp { pkkan(" (0).. 2 1 roll {sub} sendmsg \n"); }
371: | exp '+' exp { pkkan(" {add} sendmsg2 \n"); }
372: | exp '-' exp { pkkan(" {sub} sendmsg2 \n"); }
373: | exp '*' exp { pkkan(" {mul} sendmsg2 \n"); }
374: | exp '/' exp { pkkan(" {div} sendmsg2 \n"); }
375: | exp '%' exp
376: | exp '^' exp { pkkan(" power\n"); }
377: | exp '&' exp
378: | exp '|' exp
379: | exp EQUAL exp { pkkan(" eq\n"); }
380: | exp '<' exp { pkkan(" lt\n"); }
381: | exp '>' exp { pkkan(" gt\n"); }
382: | exp LESSEQUAL exp { pkkan(" lessThanOrEqual\n"); }
383: | exp LEFTSHIFT exp
384: | exp GREATEREQUAL exp { pkkan(" greaterThanOrEqual\n"); }
385: | exp RIGHTSHIFT exp
386: | exp AND exp { pkkan(" and\n"); }
387: | exp OR exp { pkkan(" or\n"); }
388: | exp NOTEQUAL exp { pkkan(" eq not\n"); }
389: | exp PUT exp
390: | id_set INCREMENT { pkkan("/");
391: printObjectSymbol($1); ips($1);
392: pkkan(" "); printObjectSymbol($1);
393: pkkan(" (1).. {add} sendmsg2 "); pkkan("def\n"); }
394: | id_set DECREMENT { pkkan("/");
395: printObjectSymbol($1); ips($1);
396: pkkan(" "); printObjectSymbol($1);
397: pkkan(" (1).. {sub} sendmsg2 "); pkkan("def\n"); }
398: | exp MEMBER exp
399: | exp '.' member_functionCall { pkkan(" sendmsg2 \n"); }
400: | exp '.' ID { /* Not implemented yet. */ }
401: | super '.' member_functionCall { pkkan(" supmsg2 \n"); }
402: | NUMBER '.' NUMBER { pkkan("("); printObjectSymbol($1);
403: pkkan(").. (double) dc ");
404: pkkan("("); printObjectSymbol($3); pkkan(").. ");
405: pkkan("("); printTens($3); pkkan(").. ");
406: pkkan(" div (double) dc add\n"); }
407: | exp RESIDUEPUT exp
408: | exp NEGATEPUT exp
409: | exp MULTPUT exp
410: | SIZEOFTHISCLASS
411: {
412: int tmp0;
413: tmp0 = K00getIncetanceVariable("K00sizeof");
414: pkkan(" ("); pkkanInteger(tmp0); pkkan(").. ");
415: }
416:
417: | STARTOFTHISCLASS
418: {
419: int tmp0;
420: tmp0 = K00getIncetanceVariable("K00start");
421: pkkan(" ("); pkkanInteger(tmp0); pkkan(").. ");
422: }
423: ;
424:
425: test : TEST '(' exp ')'
426: { /* It is not used now. */
427: sendKan(1);
428: Sm1obj = KSpop();
429: if (Sm1obj.tag != Sdollar) {
430: fprintf(stderr," Argument of test must be a string.\n");
431: }
432: testNewFunction((struct Object *)&Sm1obj);
433: }
434: ;
435:
436: special : SPECIAL '(' list ')'
437: {
438: fprintf(stderr," special is used to extend the function. \n");
439: }
440: ;
441:
442:
443: member_functionCall
444: :
445: member_functionCall_prefix argList ')'
446: {pkkan("] {");printObjectSymbol($1);pkkan("} ");}
447: | member_functionCall_prefix ')'
448: {pkkan("] {");printObjectSymbol($1);pkkan("} ");}
449:
450: ;
451: member_functionCall_prefix
452: :
1.5 ! takayama 453: ID '(' { pkkan(" [ %% function args \n"); $$ = $1; }
1.1 maekawa 454: ;
455:
456:
457: class_definition
458: :
459: class_definition_prefix incetance_variables globalstatements '}'
460: { pkkan(" PrimitiveContextp setcontext ");
461: /* debug */ K00foo1();
462: K00toPrimitiveClass();
463: }
464: ;
465:
466: class_definition_prefix
467: :
468: CLASS ID EXTENDS ID '{'
469: { ips($2);
470: pkkan("[ $") ; printObjectSymbol($2); pkkan("$ ");
471: printObjectSymbol($4); pkkan(" 0 get newcontext ] /");
472: printObjectSymbol($2); pkkan(" set \n");
473: printObjectSymbol($2); pkkan(" 0 get setcontext \n");
474:
475: if (K00declareClass(objectSymbolToString($2),
476: objectSymbolToString($4)) == -1) {
477: /* error */
478: KCerror("Super class has not been defined or Invalid class name.");
479: }else{
480: K00putIncetanceVariable(IRESET," ");
481: }
482: }
1.4 takayama 483: ;
1.1 maekawa 484: incetance_variables
485: : LOCAL incetance_variables_list ';'
1.3 takayama 486: {
487: K00putIncetanceVariable(IEXIT," ");
488: }
489: | LOCAL ';'
1.1 maekawa 490: {
491: K00putIncetanceVariable(IEXIT," ");
492: }
493: ;
494: incetance_variables_list
495: : incetance_variables_list ',' ID
496: {
497: K00putIncetanceVariable(IPUT,objectSymbolToString($3));
498: }
499: | ID
500: {
501: K00putIncetanceVariable(IPUT,objectSymbolToString($1));
502: }
1.4 takayama 503: ;
1.1 maekawa 504: operatorfunctionHead
505: : DEF OPERATOR ID
506: { pkkan("/"); printObjectSymbol($3); pkkan(" {\n"); ips($3);
507: pkdebug("In function : ", objectSymbolToString($3),", of class ",
508: K00getCurrentContextName());
509: pkkan(" /Arglist set /Arglist [Arglist] def ");
510: pkkan(" /Argthis set /FunctionValue [ ] def\n [/this ");
511: $$ = $3;}
512: ;
513:
514: super
515: : SUPER { pkkan(" this "); }
516: ;
517:
518: new
519: : NEW '(' exp ')'
520: { int tmp;
521: tmp = K00getIncetanceVariable("K00sizeof")
522: +K00getIncetanceVariable("K00start");
523: pkkanInteger(tmp);
524: pkkan(" ");
525: pkkan(K00getCurrentContextName());
526: pkkan(" cclass \n");
527: }
528: | NEW '(' ')'
529: { int tmp;
530: pkkan(" PrimitiveObject ");
531: tmp = K00getIncetanceVariable("K00sizeof")
532: +K00getIncetanceVariable("K00start");
533: pkkanInteger(tmp);
534: pkkan(" ");
535: pkkan(K00getCurrentContextName());
536: pkkan(" cclass \n");
537: }
538: ;
539:
540:
541:
542:
543:
544:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>