[BACK]Return to simple.y CVS log [TXT][DIR] Up to [local] / OpenXM / src / k097

Annotation of OpenXM/src/k097/simple.y, Revision 1.7

1.7     ! takayama    1: /* $OpenXM: OpenXM/src/k097/simple.y,v 1.6 2003/04/30 06:17:57 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
1.6       takayama  151:   : PSFOR '(' ID '=' exp ';'  ID '<' exp ';' ID INCREMENT  ')'
                    152:      {
1.7     ! takayama  153:        pkkan(" 2 -1 roll \n");
1.6       takayama  154:        pkkan("%%PSfor initvalue.\n (integer) data_conversion \n");
1.7     ! takayama  155:        pkkan(" 2 -1 roll \n");
1.1       maekawa   156:        ips($3);
1.6       takayama  157:        ips($7); ips($11);
1.1       maekawa   158:        /* They must be equal id, but it is not checked. */
                    159:        pkkan(" (1).. sub  (integer) data_conversion  1  2 -1 roll \n");
                    160:        pkkan("{ %% for body\n (universalNumber) data_conversion ");
                    161:        pkkan("/"); printObjectSymbol($3); pkkan("  set \n");
                    162:      }
                    163:   ;
                    164: PSfor
                    165:   : PSfor_prefix curryBrace
                    166:      { pkkan("  } for \n"); }
                    167:   ;
                    168:
                    169: while_prefix0
                    170:   : WHILE '('  { pkkan("\n%%while\n{ "); }
                    171:   ;
                    172:
                    173: while_prefix
                    174:   : while_prefix0 exp ')'   { pkkan(" { } {exit} ifelse\n "); }
                    175:   | while_prefix0     ')'   { pkkan("%%no exit condition.\n "); }
                    176:   ;
                    177:
                    178: while
                    179:   :  while_prefix curryBrace {   pkkan("} loop\n"); }
                    180:   ;
                    181:
                    182:
                    183: print
                    184:   : PRINT '(' exp ')' { pkkan(" print\n");}
                    185:   ;
                    186:
                    187: sm1 :
                    188:     SM1 '(' sm1ArgList ')'
                    189:   ;
                    190:
                    191: load :
                    192:     LOAD '(' QUOTE ')'   {   loadFile($3);  }
                    193:   | LOAD '(' ID ')'      {   loadFile($3);  }
                    194:   | LOAD ID              {   loadFile($2);  }
                    195:   | LOAD QUOTE           {   loadFile($2);  }
                    196:   | LOAD '[' QUOTE ']'   {   loadFileWithCpp($3); }
1.4       takayama  197: ;
1.1       maekawa   198: sm1ArgList :
                    199:   | sm1ArgList ',' QUOTE  { pkkan(" "); printObjectSymbol($3); pkkan(" "); }
                    200:   | QUOTE                 { pkkan(" "); printObjectSymbol($1); pkkan(" "); }
                    201:   | sm1ArgList ',' ID     { pkkan(" "); printObjectSymbol($3); pkkan(" "); }
                    202:   | ID                    { pkkan(" "); printObjectSymbol($1); pkkan(" "); }
                    203:   ;
                    204:
                    205: primitive
                    206:   : ID
                    207:    { int tmp0;
                    208:      if ((tmp0 = K00getIncetanceVariable(objectSymbolToString($1))) != -1) {
                    209:        pkkan(" this "); pkkanInteger(tmp0); pkkan(" get ");
                    210:      } else {
                    211:        printObjectSymbol($1); pkkan(" ");
                    212:      }
                    213:    }
                    214:   | QUOTE     { pkkan("("); printObjectSymbol($1); pkkan(") "); }
                    215:   | SINGLEQUOTE
                    216:   | NUMBER    { pkkan("("); printObjectSymbol($1); pkkan(").. ");}
                    217:   | THIS      { pkkan(" this "); }
                    218:   | '(' exp ')'
                    219:   | functionCall
                    220:   | list
                    221:   | print
                    222:   | sm1
                    223:   | load
                    224:   | test
                    225:   | special
                    226:   | set
                    227:   | arrayValue
                    228:   ;
                    229:
                    230: functionCall_prefix
                    231:   :
1.5       takayama  232:    ID '('  { pkkan("this [ %% function args \n"); $$ = $1; }
1.1       maekawa   233:   ;
                    234:
                    235: /* function call. cf. Schreiner 19p */
                    236: /* If you want to have a strong binding s.t.
                    237:    ID[1,2], then you need to define a new non-terminal
                    238:    such as primitive. ????*/
                    239: functionCall
                    240:   :
                    241:     functionCall_prefix  argList  ')'
                    242:        {pkkan("] {");printObjectSymbol($1);pkkan("} sendmsg2 \n");}
                    243:   | functionCall_prefix           ')'
                    244:        {pkkan("] {");printObjectSymbol($1);pkkan("} sendmsg2 \n");}
                    245:
                    246:   ;
                    247: idList
                    248:   : idList ',' ID   { pkkan("/");printObjectSymbol($3); pkkan(" "); ips($3);}
                    249:   | ID              { pkkan("/");printObjectSymbol($1); pkkan(" "); ips($1);}
                    250:   ;
                    251:
                    252: functionHead
                    253:   : DEF ID
                    254:         { pkkan("/"); printObjectSymbol($2); pkkan(" {\n"); ips($2);
                    255:           pkdebug("In function : ", objectSymbolToString($2),
                    256:                  " of class ",K00getCurrentContextName());
                    257:          pkkan(" /Arglist set /Argthis set /FunctionValue [ ] def\n [/this ");
                    258:          $$ = $2;}
                    259:   ;
                    260:
                    261: voidfunctionHead
                    262:   : DEF VOID ID
                    263:         { pkkan("/"); printObjectSymbol($3); pkkan(" {\n"); ips($3);
                    264:           pkdebug("In function : ", objectSymbolToString($3),
                    265:                  " of class ",K00getCurrentContextName());
                    266:          pkkan(" /Arglist set /Argthis set /FunctionValue [ ] def\n [/this ");
                    267:          $$ = $2;}
                    268:   ;
                    269:
                    270: functionArg
                    271:   : '(' idList ')'  '{'
                    272:      {pkkan(" ] /ArgNames set ArgNames pushVariables [ %%function body\n");
                    273:       pkkan(" [Argthis] Arglist join ArgNames mapset\n");}
                    274:   | '('        ')'  '{'
                    275:      {pkkan(" ] /ArgNames set ArgNames pushVariables [ %%function body\n");
                    276:       pkkan(" [Argthis]  ArgNames mapset\n"); }
                    277:   ;
                    278:
                    279: functionBody
                    280:   :
                    281:     declaration extern_declaration statements '}'
                    282:           { pkkan("/ExitPoint ]pop popVariables %%pop the local variables\n"); }
                    283:   |  extern_declaration statements '}'
                    284:   ;
                    285:
                    286:
                    287: functionDefinition
                    288:   : functionHead functionArg functionBody
                    289:         {pkkan("/ExitPoint ]pop popVariables %%pop argValues\n");
                    290:         pkdebug2();
                    291:          pkkan("FunctionValue } def\n%%end of function\n\n"); }
                    292:   | operatorfunctionHead functionArg functionBody
                    293:         {pkkan("/ExitPoint ]pop popVariables %%pop argValues\n");
                    294:         pkdebug2();
                    295:          pkkan("FunctionValue } def\n%%end of function\n\n"); }
                    296:   | voidfunctionHead functionArg functionBody
                    297:         {pkkan("/ExitPoint ]pop popVariables %%pop argValues\n");
                    298:         pkdebug2();
                    299:          pkkan("} def\n%%end of function\n\n"); }
                    300:   ;
                    301:
                    302: declaration_prefix
                    303:   : LOCAL { pkkan("[ %%start of local variables\n"); }
                    304:   ;
                    305:
                    306: declaration
                    307:   : declaration_prefix  idList ';'
                    308:        { pkkan("] pushVariables [ %%local variables\n"); }
                    309:   ;
                    310:
                    311: extern_idList
                    312:   : extern_idList ',' ID
                    313:            {     ;}
                    314:   | ID     {     ;}
                    315:   ;
                    316:
                    317: extern_declaration_prefix
                    318:   : EXTERN {   ; }
                    319:   ;
                    320:
                    321: extern_declaration
                    322:   : extern_declaration_prefix  extern_idList ';'
                    323:        {  ; }
                    324:   |
                    325:   ;
                    326:
                    327: arrayValue :
                    328:    array    { pkkan(" Get\n"); }
                    329:   ;
                    330: array :
                    331:    array_prefix arrayIndex ']' {pkkan(" ] "); }
                    332:   ;
                    333: arrayIndex :
                    334:     exp ',' arrayIndex
                    335:   | exp
                    336:   ;
                    337: array_prefix :
                    338:     ID '['
                    339:      { int tmp0;
                    340:        if ((tmp0 = K00getIncetanceVariable(objectSymbolToString($1))) != -1) {
                    341:          pkkan(" this "); pkkanInteger(tmp0); pkkan(" get [");
                    342:        } else {
                    343:          printObjectSymbol($1); pkkan(" [");
                    344:        }
                    345:      }
                    346:   ;
                    347:
                    348:
                    349: id_set
                    350:   : ID             { $$ = $1;}
                    351:   ;
                    352: set
                    353:   : id_set '=' exp
                    354:     { int tmp0;
                    355:       if ((tmp0 = K00getIncetanceVariable(objectSymbolToString($1))) != -1) {
                    356:        pkkan(" this "); pkkanInteger(tmp0);
                    357:        pkkan(" 3 -1 roll   put\n");
                    358:       }else {
                    359:        pkkan("/"); printObjectSymbol($1); pkkan(" "); ips($1); pkkan(" set\n");
                    360:       }
                    361:     }
                    362:   | array '=' exp  { pkkan(" Put\n"); }
                    363:   | THIS '=' exp   { pkkan(" /this set \n"); }
                    364:   ;
                    365:
                    366: exp
                    367:   : primitive
                    368:   | new
                    369:   | '!' exp      { pkkan(" not\n"); }
                    370:   | '~' exp
                    371:   | '-' exp      { pkkan(" (0)..  2 1 roll {sub} sendmsg \n"); }
                    372:   | exp '+' exp  { pkkan(" {add} sendmsg2 \n"); }
                    373:   | exp '-' exp  { pkkan(" {sub} sendmsg2 \n"); }
                    374:   | exp '*' exp  { pkkan(" {mul} sendmsg2 \n"); }
                    375:   | exp '/' exp  { pkkan(" {div} sendmsg2 \n"); }
                    376:   | exp '%' exp
                    377:   | exp '^' exp  { pkkan(" power\n"); }
                    378:   | exp '&' exp
                    379:   | exp '|' exp
                    380:   | exp EQUAL exp { pkkan(" eq\n"); }
                    381:   | exp '<' exp { pkkan(" lt\n"); }
                    382:   | exp '>' exp { pkkan(" gt\n"); }
                    383:   | exp LESSEQUAL exp { pkkan(" lessThanOrEqual\n"); }
                    384:   | exp LEFTSHIFT exp
                    385:   | exp GREATEREQUAL exp { pkkan(" greaterThanOrEqual\n"); }
                    386:   | exp RIGHTSHIFT exp
                    387:   | exp AND exp { pkkan(" and\n"); }
                    388:   | exp OR exp  { pkkan(" or\n"); }
                    389:   | exp NOTEQUAL exp  { pkkan(" eq not\n"); }
                    390:   | exp PUT exp
                    391:   | id_set INCREMENT { pkkan("/");
                    392:                       printObjectSymbol($1); ips($1);
                    393:                       pkkan(" "); printObjectSymbol($1);
                    394:                       pkkan(" (1).. {add} sendmsg2 "); pkkan("def\n"); }
                    395:   | id_set DECREMENT { pkkan("/");
                    396:                       printObjectSymbol($1); ips($1);
                    397:                       pkkan(" "); printObjectSymbol($1);
                    398:                       pkkan(" (1).. {sub} sendmsg2 "); pkkan("def\n"); }
                    399:   | exp MEMBER exp
                    400:   | exp '.' member_functionCall {  pkkan(" sendmsg2 \n"); }
                    401:   | exp '.' ID   { /* Not implemented yet. */ }
                    402:   | super '.' member_functionCall {  pkkan(" supmsg2 \n"); }
                    403:   | NUMBER '.' NUMBER {  pkkan("("); printObjectSymbol($1);
                    404:                         pkkan(")..  (double) dc ");
                    405:                         pkkan("("); printObjectSymbol($3); pkkan(")..  ");
                    406:                          pkkan("("); printTens($3); pkkan(").. ");
                    407:                         pkkan(" div (double) dc  add\n"); }
                    408:   | exp RESIDUEPUT exp
                    409:   | exp NEGATEPUT exp
                    410:   | exp MULTPUT exp
                    411:   | SIZEOFTHISCLASS
                    412:     {
                    413:       int tmp0;
                    414:       tmp0 = K00getIncetanceVariable("K00sizeof");
                    415:       pkkan(" ("); pkkanInteger(tmp0); pkkan(").. ");
                    416:     }
                    417:
                    418:   | STARTOFTHISCLASS
                    419:     {
                    420:       int tmp0;
                    421:       tmp0 = K00getIncetanceVariable("K00start");
                    422:       pkkan(" ("); pkkanInteger(tmp0); pkkan(").. ");
                    423:     }
                    424:   ;
                    425:
                    426: test : TEST '(' exp ')'
                    427:           { /* It is not used now. */
                    428:            sendKan(1);
                    429:            Sm1obj = KSpop();
                    430:            if (Sm1obj.tag != Sdollar) {
                    431:              fprintf(stderr," Argument of test must be a string.\n");
                    432:            }
                    433:            testNewFunction((struct Object *)&Sm1obj);
                    434:          }
                    435:      ;
                    436:
                    437: special : SPECIAL '(' list ')'
                    438:       {
                    439:           fprintf(stderr," special is used to extend the function. \n");
                    440:       }
                    441:       ;
                    442:
                    443:
                    444: member_functionCall
                    445:   :
                    446:     member_functionCall_prefix  argList  ')'
                    447:        {pkkan("] {");printObjectSymbol($1);pkkan("}  ");}
                    448:   | member_functionCall_prefix           ')'
                    449:        {pkkan("] {");printObjectSymbol($1);pkkan("}  ");}
                    450:
                    451:   ;
                    452: member_functionCall_prefix
                    453:   :
1.5       takayama  454:     ID '('  { pkkan(" [ %% function args \n"); $$ = $1; }
1.1       maekawa   455:   ;
                    456:
                    457:
                    458: class_definition
                    459:   :
                    460:     class_definition_prefix incetance_variables globalstatements '}'
                    461:       { pkkan(" PrimitiveContextp setcontext ");
                    462:        /* debug */ K00foo1();
                    463:         K00toPrimitiveClass();
                    464:       }
                    465:   ;
                    466:
                    467: class_definition_prefix
                    468:   :
                    469:     CLASS ID EXTENDS ID '{'
                    470:       { ips($2);
                    471:        pkkan("[ $") ; printObjectSymbol($2); pkkan("$ ");
                    472:        printObjectSymbol($4); pkkan(" 0 get  newcontext ] /");
                    473:        printObjectSymbol($2);  pkkan(" set \n");
                    474:        printObjectSymbol($2); pkkan(" 0 get setcontext \n");
                    475:
                    476:        if (K00declareClass(objectSymbolToString($2),
                    477:                            objectSymbolToString($4)) == -1) {
                    478:          /* error */
                    479:          KCerror("Super class has not been defined or Invalid class name.");
                    480:        }else{
                    481:          K00putIncetanceVariable(IRESET," ");
                    482:        }
                    483:       }
1.4       takayama  484: ;
1.1       maekawa   485: incetance_variables
                    486:   : LOCAL incetance_variables_list ';'
1.3       takayama  487:       {
                    488:        K00putIncetanceVariable(IEXIT," ");
                    489:       }
                    490:   | LOCAL ';'
1.1       maekawa   491:       {
                    492:        K00putIncetanceVariable(IEXIT," ");
                    493:       }
                    494:   ;
                    495: incetance_variables_list
                    496:   : incetance_variables_list ',' ID
                    497:     {
                    498:       K00putIncetanceVariable(IPUT,objectSymbolToString($3));
                    499:     }
                    500:   | ID
                    501:     {
                    502:       K00putIncetanceVariable(IPUT,objectSymbolToString($1));
                    503:     }
1.4       takayama  504: ;
1.1       maekawa   505: operatorfunctionHead
                    506:   : DEF OPERATOR ID
                    507:         { pkkan("/"); printObjectSymbol($3); pkkan(" {\n"); ips($3);
                    508:           pkdebug("In function : ", objectSymbolToString($3),", of class  ",
                    509:                  K00getCurrentContextName());
                    510:          pkkan(" /Arglist set /Arglist [Arglist] def ");
                    511:           pkkan(" /Argthis set /FunctionValue [ ] def\n [/this ");
                    512:          $$ = $3;}
                    513:   ;
                    514:
                    515: super
                    516:   : SUPER   { pkkan(" this "); }
                    517:   ;
                    518:
                    519: new
                    520:   : NEW '(' exp ')'
                    521:        { int tmp;
                    522:         tmp = K00getIncetanceVariable("K00sizeof")
                    523:           +K00getIncetanceVariable("K00start");
                    524:         pkkanInteger(tmp);
                    525:         pkkan(" ");
                    526:         pkkan(K00getCurrentContextName());
                    527:         pkkan(" cclass \n");
                    528:        }
                    529:   | NEW '(' ')'
                    530:        { int tmp;
                    531:          pkkan(" PrimitiveObject ");
                    532:         tmp = K00getIncetanceVariable("K00sizeof")
                    533:           +K00getIncetanceVariable("K00start");
                    534:         pkkanInteger(tmp);
                    535:         pkkan(" ");
                    536:         pkkan(K00getCurrentContextName());
                    537:         pkkan(" cclass \n");
                    538:        }
                    539:   ;
                    540:
                    541:
                    542:
                    543:
                    544:
                    545:

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>