home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnusmalltalk / mst.y < prev    next >
Text File  |  1992-02-15  |  10KB  |  443 lines

  1. /* -*- bison -*- */
  2.  
  3. /***********************************************************************
  4.  *
  5.  * Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
  6.  * Written by Steve Byrne.
  7.  *
  8.  * This file is part of GNU Smalltalk.
  9.  *
  10.  * GNU Smalltalk is free software; you can redistribute it and/or modify it
  11.  * under the terms of the GNU General Public License as published by the Free
  12.  * Software Foundation; either version 1, or (at your option) any later 
  13.  * version.
  14.  * 
  15.  * GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  16.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
  17.  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  18.  * more details.
  19.  * 
  20.  * You should have received a copy of the GNU General Public License along with
  21.  * GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  22.  * Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  23.  *
  24.  ***********************************************************************/
  25.  
  26. %{
  27. #include "mst.h"
  28. #include "mstsym.h"
  29. #include "msttree.h"
  30. #include "mstdict.h"
  31. #include "mstcomp.h"
  32. #include <stdio.h>
  33. #ifdef HAS_ALLOCA_H
  34. #include <alloca.h>
  35. #endif
  36.  
  37. #define YYDEBUG 1
  38.  
  39. extern Boolean        quietExecution;
  40.  
  41. %}
  42.  
  43. %pure_parser
  44.  
  45. %union{
  46.   char        cval;
  47.   double    fval;
  48.   long        ival;
  49.   char        *sval;
  50.   TreeNode    node;
  51. }
  52.  
  53. /* single definite characters */     
  54. %token BANG COLON UPARROW DOT ASSIGN SHARP SEMICOLON
  55. %token OPEN_PAREN CLOSE_PAREN OPEN_BRACKET CLOSE_BRACKET
  56. %token PRIMITIVE_START INTERNAL_TOKEN
  57.  
  58. /* larger lexical items */
  59. %token <sval> IDENTIFIER KEYWORD STRING_LITERAL SYMBOL_KEYWORD BINOP
  60.               VERTICAL_BAR 
  61. %token <ival> INTEGER_LITERAL
  62. %token <fval> FLOATING_LITERAL
  63. %token <cval> CHAR_LITERAL
  64.  
  65. %type <node> method message_pattern variable_name keyword_variable_list
  66.     temporaries variable_names statements non_empty_statements expression
  67.     assigns primary number symbol_constant symbol 
  68.     character_constant string array_constant array
  69.     array_constant_list block opt_block_variables 
  70.     block_variable_list unary_expression binary_expression
  71.     keyword_expression keyword_binary_object_description_list
  72.     cascaded_message_expression semi_message_list
  73.     message_elt simple_expression literal message_expression
  74.     array_constant_elt unary_object_description
  75.     binary_object_description
  76. %type <sval> unary_selector keyword binary_selector
  77. %type <ival> primitive
  78. %%
  79.  
  80. program:
  81.     class_definition_list
  82.     | internal_marker method    { compileMethod($2); }
  83.     | /* EMPTY */
  84.     ;
  85.  
  86. internal_marker:
  87.     INTERNAL_TOKEN            { clearMethodStartPos(); }
  88.  
  89. class_definition_list:
  90.     class_definition
  91.     | class_definition_list class_definition
  92.     ;
  93.  
  94. class_definition:
  95.     class_header method_list BANG    { skipCompilation = false; }
  96.     | class_header  BANG        { skipCompilation = false; }
  97.     | non_empty_statements BANG    { if (!hadError) {
  98.                         executeStatements(nil, $1,
  99.                                 quietExecution); 
  100.                       }
  101.                       hadError = false;
  102.                     }
  103.     | temporaries non_empty_statements BANG    
  104.                     { if (!hadError) {
  105.                         executeStatements($1, $2,
  106.                                 quietExecution); 
  107.                                           }
  108.                       hadError = false;
  109.                                         }
  110.     | error BANG            { hadError = false;
  111.                       yyerrok; }
  112.     ;
  113.  
  114. class_header:
  115.     BANG class_specification BANG     { clearMethodStartPos(); }
  116.     ;
  117.  
  118. class_specification:
  119.     simple_expression    { executeStatements(nil, 
  120.                     makeStatementList($1, nil), true); }
  121.     ;
  122.  
  123. /*
  124. method_list:
  125.     method                 { if (!hadError) {
  126.                         if (skipCompilation) {
  127.                           freeTree($1);
  128.                         } else {
  129.                            compileMethod($1);
  130.                         }
  131.                       } else {
  132.                         hadError = false;
  133.                       }
  134.                     }
  135.         | method_list method        { if (!hadError) {
  136.                         if (skipCompilation) {
  137.                           freeTree($2);
  138.                         } else {
  139.                            compileMethod($2);
  140.                         }
  141.                       } else {
  142.                         hadError = false;
  143.                       }
  144.                     }
  145.     ;
  146.      
  147. method:
  148.     message_pattern statements BANG 
  149.                     { $$ = makeMethod($1, nil, 0, $2); }
  150.     | message_pattern temporaries statements BANG
  151.                     { $$ = makeMethod($1, $2, 0, $3); }
  152.     | message_pattern primitive statements BANG
  153.                     { $$ = makeMethod($1, nil, $2, $3); }
  154.     | message_pattern temporaries primitive statements BANG
  155.         { $$ = makeMethod($1, $2, $3, $4); }
  156.     ;
  157.  
  158. */
  159. method_list:
  160.     method BANG             { if (!hadError) {
  161.                         if (skipCompilation) {
  162.                           freeTree($1);
  163.                         } else {
  164.                           compileMethod($1);
  165.                           clearMethodStartPos();
  166.                         }
  167.                       } else {
  168.                         hadError = false;
  169.                       }
  170.                     }
  171.         | method_list method BANG    { if (!hadError) {
  172.                         if (skipCompilation) {
  173.                           freeTree($2);
  174.                         } else {
  175.                           compileMethod($2);
  176.                           clearMethodStartPos();
  177.                         }
  178.                       } else {
  179.                         hadError = false;
  180.                       }
  181.                     }
  182.     ;
  183.      
  184. method:
  185.     message_pattern statements 
  186.                     { $$ = makeMethod($1, nil, 0, $2); }
  187.     | message_pattern temporaries statements
  188.                     { $$ = makeMethod($1, $2, 0, $3); }
  189.     | message_pattern primitive statements 
  190.                     { $$ = makeMethod($1, nil, $2, $3); }
  191.     | message_pattern temporaries primitive statements
  192.         { $$ = makeMethod($1, $2, $3, $4); }
  193.     ;
  194.  
  195.  
  196. message_pattern:
  197.     unary_selector            { $$ = makeUnaryExpr(nil, $1); }
  198.     | binary_selector variable_name    { $$ = makeBinaryExpr(nil, $1,
  199.                                       $2); }
  200.     | keyword_variable_list        { $$ = makeKeywordExpr(nil, $1); }
  201.     | error                { errorf("Invalid message pattern");
  202.                       hadError = true;
  203.                       yyerrok;
  204.                       $$ = nil; }
  205.     ;
  206.  
  207. unary_selector:
  208.     IDENTIFIER
  209.     ;
  210.  
  211. binary_selector:
  212.     BINOP            /* I don't like this usage of binop */
  213.     | VERTICAL_BAR
  214.     ;
  215.  
  216. variable_name:
  217.     IDENTIFIER            { $$ = makeVariable($1); }
  218.     ;
  219.  
  220. keyword_variable_list:
  221.     keyword variable_name        { $$ = makeKeywordList($1, $2); }
  222.     | keyword_variable_list keyword variable_name
  223.                     { addNode($1, makeKeywordList($2, $3));
  224.                       $$ = $1; }
  225.     ;
  226.  
  227. keyword:
  228.     KEYWORD
  229.     ;
  230.  
  231. primitive:
  232.     PRIMITIVE_START INTEGER_LITERAL BINOP
  233.                     { $$ = $2;
  234.                       if (strcmp($3, ">") != 0) {
  235.                         YYERROR;
  236.                       }
  237.                     }
  238.  
  239. temporaries:
  240.     VERTICAL_BAR VERTICAL_BAR    { $$ = nil; }
  241.     | VERTICAL_BAR variable_names VERTICAL_BAR
  242.                     { $$ = $2; }
  243.     ;
  244.  
  245. variable_names:
  246.     variable_name            { $$ = makeVariableList($1); }
  247.     | variable_names variable_name    { addNode($1, makeVariableList($2));
  248.                       $$ = $1; }
  249.     ;
  250.  
  251. statements:
  252.     /* empty */            { $$ = nil; }
  253.     | non_empty_statements
  254.     ;
  255.  
  256. non_empty_statements:
  257.     UPARROW expression optional_dot    
  258.                 { $$ = makeStatementList(makeReturn($2),
  259.                                    nil); }
  260.     | expression        { $$ = makeStatementList($1, nil); }
  261.     | expression DOT statements
  262.                  /* I don't know if I like this production */
  263.                 { $$ = makeStatementList($1, $3); }
  264.     | error DOT statements  { $$ = $3;
  265.                   yyerrok;
  266.                   errorf("Error in expression");
  267.                   hadError = true;
  268.                 }
  269.     ;
  270.     
  271. optional_dot:
  272.     /* empty */
  273.      | DOT
  274.     ;
  275.  
  276. expression:
  277.     simple_expression
  278.     | assigns simple_expression    { $$ = makeAssign($1, $2); }
  279.     ;
  280.  
  281. assigns:
  282.     variable_name ASSIGN        { $$ = makeVariableList($1); }
  283.     | assigns variable_name ASSIGN
  284.                     { addNode($1, makeVariableList($2));
  285.                       $$ = $1; }
  286.     ;
  287.  
  288. simple_expression:
  289.     primary
  290.     | message_expression
  291.     | cascaded_message_expression
  292.     ;
  293.  
  294. primary:
  295.     variable_name
  296.     | literal
  297.     | block                
  298.     | OPEN_PAREN expression CLOSE_PAREN { $$ = $2; }
  299.     ;
  300.  
  301. literal:
  302.     number
  303.     | symbol_constant
  304.     | character_constant
  305.     | string
  306.     | array_constant
  307.     ;
  308.  
  309. number:
  310.     INTEGER_LITERAL            { $$ = makeIntConstant($1); }
  311.     | FLOATING_LITERAL        { $$ = makeFloatConstant($1); }
  312.     ;
  313.  
  314. symbol_constant:
  315.     SHARP symbol            { $$ = makeSymbolConstant($2); }
  316.     ;
  317.  
  318. symbol:
  319.     IDENTIFIER            { $$ = internIdent($1); }
  320.     | binary_selector        { $$ = internBinOP($1); }
  321.     | SYMBOL_KEYWORD        { $$ = internIdent($1); }
  322.     | KEYWORD            { $$ = internIdent($1); }
  323.     ;
  324.  
  325.  
  326. character_constant:
  327.     CHAR_LITERAL            { $$ = makeCharConstant($1); }
  328.     ;
  329.  
  330. string:
  331.     STRING_LITERAL            { $$ = makeStringConstant($1); }
  332.     ;
  333.  
  334. array_constant:
  335.     SHARP array            { $$ = makeArrayConstant($2); }
  336.     ;
  337.  
  338. array:
  339.     OPEN_PAREN CLOSE_PAREN        { $$ = nil; }
  340.     | OPEN_PAREN array_constant_list CLOSE_PAREN
  341.                     { $$ = $2; }
  342.     ;
  343.  
  344. array_constant_list:
  345.     array_constant_elt        { $$ = makeArrayElt($1); }
  346.     | array_constant_list array_constant_elt
  347.                           { addNode($1, makeArrayElt($2));
  348.                       $$ = $1; }
  349.     ;
  350.  
  351. array_constant_elt:
  352.     number
  353.     | symbol
  354.     | string
  355.     | character_constant
  356.     | array
  357.     ;
  358.  
  359. block:
  360.     OPEN_BRACKET opt_block_variables statements CLOSE_BRACKET
  361.                     { $$ = makeBlock($2, $3); }
  362.     ;
  363.  
  364. opt_block_variables:
  365.     /* empty */            { $$ = nil; }
  366.     | block_variable_list VERTICAL_BAR
  367.     ;
  368.  
  369. /* syntax for blocks with temporaries is just args and vertical bar (if
  370.  * any followed by a standard temporaries declarations */
  371.  
  372. block_variable_list:
  373.     COLON variable_name        { $$ = makeVariableList($2); }
  374.     | block_variable_list COLON variable_name
  375.                         { addNode($1, makeVariableList($3));
  376.                       $$ = $1; }
  377.     ;
  378.  
  379. message_expression:
  380.     unary_expression
  381.     | binary_expression
  382.     | keyword_expression
  383.     ;
  384.  
  385. unary_expression:
  386.     unary_object_description unary_selector { $$ = makeUnaryExpr($1, $2); }
  387.     ;
  388.  
  389. unary_object_description:
  390.     primary
  391.     | unary_expression
  392.     ;
  393.  
  394. binary_expression:
  395.     binary_object_description binary_selector unary_object_description
  396.                     { $$ = makeBinaryExpr($1, $2, $3); }
  397.     ;
  398.  
  399. binary_object_description:
  400.     unary_object_description
  401.     | binary_expression
  402.     ;
  403.  
  404. keyword_expression:
  405.     binary_object_description keyword_binary_object_description_list
  406.                     { $$ = makeKeywordExpr($1, $2); }
  407.      ;
  408.  
  409. keyword_binary_object_description_list:
  410.     keyword binary_object_description
  411.                     { $$ = makeKeywordList($1, $2); }
  412.     | keyword_binary_object_description_list keyword
  413.       binary_object_description    { addNode($1, makeKeywordList($2, $3));
  414.                       $$ = $1; }
  415.     ;
  416.  
  417. cascaded_message_expression:
  418.     message_expression semi_message_list
  419.                        { $$ = makeCascadedMessage($1, $2); }
  420.     ;
  421.  
  422. semi_message_list:
  423.     SEMICOLON message_elt        { $$ = makeMessageList($2); }
  424.     | semi_message_list SEMICOLON message_elt
  425.                     { addNode($1, makeMessageList($3));
  426.                       $$ = $1; }
  427.     ;
  428.  
  429. message_elt:
  430.     unary_selector            { $$ = makeUnaryExpr(nil, $1); }
  431.     | binary_selector unary_object_description
  432.                     { $$ = makeBinaryExpr(nil, $1, $2); }
  433.     | keyword_binary_object_description_list
  434.                     { $$ = makeKeywordExpr(nil, $1); }
  435.     ;
  436.  
  437.  
  438. %%
  439. /*     
  440. ADDITIONAL C CODE
  441. */
  442.  
  443.