home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 116.lha / SmallTalk / Sources / PARSER.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-20  |  20.8 KB  |  858 lines

  1. /*
  2.     Little Smalltalk, version 2
  3.     Written by Tim Budd, Oregon State University, July 1987
  4.  
  5.     Method parser - parses the textual description of a method,
  6.     generating bytecodes and literals.
  7.  
  8.     This parser is based around a simple minded recursive descent
  9.     parser.
  10.     It is used both by the module that builds the initial virtual image,
  11.     and by a primitive when invoked from a running Smalltalk system.
  12.  
  13.     The latter case could, if the bytecode interpreter were fast enough,
  14.     be replaced by a parser written in Smalltalk.  This would be preferable,
  15.     but not if it slowed down the system too terribly.
  16.  
  17.     To use the parser the routine setInstanceVariables must first be
  18.     called with a class object.  This places the appropriate instance
  19.     variables into the memory buffers, so that references to them
  20.     can be correctly encoded.
  21.  
  22.     As this is recursive descent, you should read it SDRAWKCAB !
  23.         (from bottom to top)
  24. */
  25. # include <stdio.h>
  26. # include "env.h"
  27. # include "memory.h"
  28. # include "names.h"
  29. # include "interp.h"
  30. # include "lex.h"
  31. # ifdef STRING
  32. # include <string.h>
  33. # endif
  34. # ifdef STRINGS
  35. # include <strings.h>
  36. # endif
  37.  
  38.         /* all of the following limits could be increased (up to
  39.             256) without any trouble.  They are kept low 
  40.             to keep memory utilization down */
  41.  
  42. # define codeLimit 256        /* maximum number of bytecodes permitted */
  43. # define literalLimit 32    /* maximum number of literals permitted */
  44. # define temporaryLimit 16    /* maximum number of temporaries permitted */
  45. # define argumentLimit 16    /* maximum number of arguments permitted */
  46. # define instanceLimit 16    /* maximum number of instance vars permitted */
  47. # define methodLimit 32        /* maximum number of methods permitted */
  48.  
  49. extern object binSyms[];
  50. extern object keySyms[];
  51. extern char *unStrs[], *binStrs[], *keyStrs[];
  52.  
  53. static boolean parseok;            /* parse still ok? */
  54. static int codeTop;            /* top position filled in code array */
  55. static byte codeArray[codeLimit];    /* bytecode array */
  56. static int literalTop;            /*  ... etc. */
  57. static object literalArray[literalLimit];
  58. static int temporaryTop;
  59. static char *temporaryName[temporaryLimit];
  60. static int argumentTop;
  61. static char *argumentName[argumentLimit];
  62. static int instanceTop;
  63. static char *instanceName[instanceLimit];
  64.  
  65. static int maxTemporary;        /* highest temporary see so far */
  66. static char selector[80];        /* message selector */
  67.  
  68. static boolean inBlock;            /* true if compiling a block */
  69. static boolean optimizedBlock;        /* true if compiling optimized block */
  70.  
  71. setInstanceVariables(aClass)
  72. object aClass;
  73. {    int i, limit;
  74.     object vars;
  75.  
  76.     if (aClass == nilobj)
  77.         instanceTop = 0;
  78.     else {
  79.         setInstanceVariables(basicAt(aClass, superClassInClass));
  80.         vars = basicAt(aClass, variablesInClass);
  81.         if (vars != nilobj) {
  82.             limit = objectSize(vars);
  83.             for (i = 1; i <= limit; i++)
  84.                 instanceName[++instanceTop] = charPtr(basicAt(vars, i));
  85.             }
  86.         }
  87. }
  88.  
  89. compilWarn(str1, str2)
  90. char *str1, *str2;
  91. {
  92.     ignore fprintf(stderr,"compiler warning: %s %s\n", str1, str2);
  93. }
  94.  
  95. compilError(str1, str2)
  96. char *str1, *str2;
  97. {
  98.     ignore fprintf(stderr,"compiler error: %s %s\n", str1, str2);
  99.     parseok = false;
  100. }
  101.  
  102. static object newChar(value)
  103. int value;
  104. {    object newobj;
  105.  
  106.     newobj = allocObject(1);
  107.     basicAtPut(newobj, 1, newInteger(value));
  108.     setClass(newobj, globalSymbol("Char"));
  109.     return(newobj);
  110. }
  111.  
  112. static object newByteArray(size)
  113. int size;
  114. {    object newobj;
  115.  
  116.     newobj = allocByte(size);
  117.     setClass(newobj, globalSymbol("ByteArray"));
  118.     return(newobj);
  119. }
  120.  
  121. static genCode(value)
  122. int value;
  123. {
  124.     if (codeTop >= codeLimit)
  125.         compilError("too many bytecode instructions in method","");
  126.     else
  127.         codeArray[codeTop++] = value;
  128. }
  129.  
  130. static genInstruction(high, low)
  131. int high, low;
  132. {
  133.     if (low >= 16) {
  134.         genInstruction(0, high);
  135.         genCode(low);
  136.         }
  137.     else
  138.         genCode(high * 16 + low);
  139. }
  140.  
  141. static int genLiteral(aLiteral)
  142. object aLiteral;
  143. {
  144.     if (literalTop >= literalLimit)
  145.         compilError("too many literals in method","");
  146.     else {
  147.         literalArray[++literalTop] = aLiteral;
  148.         incr(aLiteral);
  149.         }
  150.     return(literalTop - 1);
  151. }
  152.  
  153. static char *glbsyms[] = {"nil", "true", "false", "smalltalk", "globalNames",
  154. 0 };
  155.  
  156. static boolean nameTerm(name)
  157. char *name;
  158. {    int i;
  159.     boolean done = false;
  160.     boolean isSuper = false;
  161.     object newterm;
  162.  
  163.     /* it might be self or super */
  164.     if (streq(name, "self") || streq(name, "super")) {
  165.         genInstruction(PushArgument, 0);
  166.         done = true;
  167.         if (streq(name,"super")) isSuper = true;
  168.         }
  169.  
  170.     /* or it might be a temporary */
  171.     if (! done)
  172.         for (i = 1; (! done) && ( i <= temporaryTop ) ; i++)
  173.             if (streq(name, temporaryName[i])) {
  174.                 genInstruction(PushTemporary, i-1);
  175.                 done = true;
  176.                 }
  177.  
  178.     /* or it might be an argument */
  179.     if (! done)
  180.         for (i = 1; (! done) && (i <= argumentTop ) ; i++)
  181.             if (streq(name, argumentName[i])) {
  182.                 genInstruction(PushArgument, i);
  183.                 done = true;
  184.                 }
  185.  
  186.     /* or it might be an instance variable */
  187.     if (! done)
  188.         for (i = 1; (! done) && (i <= instanceTop); i++) {
  189.             if (streq(name, instanceName[i])) {
  190.                 genInstruction(PushInstance, i-1);
  191.                 done = true;
  192.                 }
  193.             }
  194.  
  195.     /* or it might be a global constant */
  196.     if (! done)
  197.         for (i = 0; (! done) && glbsyms[i]; i++)
  198.             if (streq(name, glbsyms[i])) {
  199.                 genInstruction(PushConstant, i+4);
  200.                 done = true;
  201.                 }
  202.  
  203.     /* not anything else, it must be a global */
  204.     /* see if we know of it first */
  205.     if (! done) { 
  206.         newterm = globalSymbol(name);
  207.         if (newterm != nilobj) {
  208.             genInstruction(PushLiteral, genLiteral(newterm));
  209.             done = true;
  210.             }
  211.         }
  212.  
  213.     /* otherwise, must look it up at run time */
  214.     if (! done) {
  215.         genInstruction(PushGlobal, genLiteral(newSymbol(name)));
  216.         }
  217.  
  218.     return(isSuper);
  219. }
  220.  
  221. static int parseArray()
  222. {    int i, size, base;
  223.     object newLit, obj;
  224.  
  225.     base = literalTop;
  226.     ignore nextToken();
  227.     while (parseok && (token != closing)) {
  228.         switch(token) {
  229.             case arraybegin:
  230.                 ignore parseArray();
  231.                 break;
  232.  
  233.             case intconst:
  234.                 ignore genLiteral(newInteger(tokenInteger));
  235.                 ignore nextToken();
  236.                 break;
  237.  
  238.             case floatconst:
  239.                 ignore genLiteral(newFloat(tokenFloat));
  240.                 ignore nextToken();
  241.                 break;
  242.  
  243.             case nameconst: case namecolon: case symconst:
  244.                 ignore genLiteral(newSymbol(tokenString));
  245.                 ignore nextToken();
  246.                 break;
  247.  
  248.             case binary:
  249.                 if (streq(tokenString, "(")) {
  250.                     ignore parseArray();
  251.                     }
  252.                 else {
  253.                     ignore genLiteral(newSymbol(tokenString));
  254.                     ignore nextToken();
  255.                     }
  256.                 break;
  257.  
  258.             case charconst:
  259.                 ignore genLiteral(newChar(
  260.                     newInteger(tokenInteger)));
  261.                 ignore nextToken();
  262.                 break;
  263.  
  264.             case strconst:
  265.                 ignore genLiteral(newStString(tokenString));
  266.                 ignore nextToken();
  267.                 break;
  268.  
  269.             default:
  270.                 compilError("illegal text in literal array",
  271.                     tokenString);
  272.                 ignore nextToken();
  273.                 break;
  274.         }
  275.     }
  276.  
  277.     if (parseok)
  278.         if (! streq(tokenString, ")"))
  279.             compilError("array not terminated by right parenthesis",
  280.                 tokenString);
  281.         else
  282.             ignore nextToken();
  283.     size = literalTop - base;
  284.     newLit = newArray(size);
  285.     for (i = size; i >= 1; i--) {
  286.         obj = literalArray[literalTop];
  287.         basicAtPut(newLit, i, obj);
  288.         decr(obj);
  289.         literalArray[literalTop] = nilobj;
  290.         literalTop = literalTop - 1;
  291.         }
  292.     return(genLiteral(newLit));
  293. }
  294.  
  295. static boolean term()
  296. {    boolean superTerm = false;    /* true if term is pseudo var super */
  297.  
  298.     if (token == nameconst) {
  299.         superTerm = nameTerm(tokenString);
  300.         ignore nextToken();
  301.         }
  302.     else if (token == intconst) {
  303.         if ((tokenInteger >= 0) && (tokenInteger <= 2))
  304.             genInstruction(PushConstant, tokenInteger);
  305.         else
  306.             genInstruction(PushLiteral, 
  307.                 genLiteral(newInteger(tokenInteger)));
  308.         ignore nextToken();
  309.         }
  310.     else if (token == floatconst) {
  311.         genInstruction(PushLiteral, genLiteral(newFloat(tokenFloat)));
  312.         ignore nextToken();
  313.         }
  314.     else if ((token == binary) && streq(tokenString, "-")) {
  315.         ignore nextToken();
  316.         if (token == intconst) {
  317.             if (tokenInteger == 1)
  318.                 genInstruction(PushConstant, 3);
  319.             else
  320.                 genInstruction(PushLiteral, 
  321.                     genLiteral(newInteger( - tokenInteger)));
  322.             }
  323.         else if (token == floatconst) {
  324.             genInstruction(PushLiteral,
  325.                 genLiteral(newFloat(-tokenFloat)));
  326.             }
  327.         else
  328.             compilError("negation not followed",
  329.                 "by number");
  330.         ignore nextToken();
  331.         }
  332.     else if (token == charconst) {
  333.         genInstruction(PushLiteral,
  334.             genLiteral(newChar(tokenInteger)));
  335.         ignore nextToken();
  336.         }
  337.     else if (token == symconst) {
  338.         genInstruction(PushLiteral,
  339.             genLiteral(newSymbol(tokenString)));
  340.         ignore nextToken();
  341.         }
  342.     else if (token == strconst) {
  343.         genInstruction(PushLiteral,
  344.             genLiteral(newStString(tokenString)));
  345.         ignore nextToken();
  346.         }
  347.     else if (token == arraybegin) {
  348.         genInstruction(PushLiteral, parseArray());
  349.         }
  350.     else if ((token == binary) && streq(tokenString, "(")) {
  351.         ignore nextToken();
  352.         expression();
  353.         if (parseok)
  354.             if ((token != closing) || ! streq(tokenString, ")"))
  355.                 compilError("Missing Right Parenthesis","");
  356.             else
  357.                 ignore nextToken();
  358.         }
  359.     else if ((token == binary) && streq(tokenString, "<"))
  360.         parsePrimitive();
  361.     else if ((token == binary) && streq(tokenString, "["))
  362.         block();
  363.     else
  364.         compilError("invalid expression start", tokenString);
  365.  
  366.     return(superTerm);
  367. }
  368.  
  369. static parsePrimitive()
  370. {    int primitiveNumber, argumentCount;
  371.  
  372.     if (nextToken() != intconst)
  373.         compilError("primitive number missing","");
  374.     primitiveNumber = tokenInteger;
  375.     ignore nextToken();
  376.     argumentCount = 0;
  377.     while (parseok && ! ((token == binary) && streq(tokenString, ">"))) {
  378.         ignore term();
  379.         argumentCount++;
  380.         }
  381.     genInstruction(DoPrimitive, argumentCount);
  382.     genCode(primitiveNumber);
  383.     ignore nextToken();
  384. }
  385.  
  386. static genMessage(toSuper, argumentCount, messagesym)
  387. boolean toSuper;
  388. int argumentCount;
  389. object messagesym;
  390. {
  391.     if (toSuper) {
  392.         genInstruction(DoSpecial, SendToSuper);
  393.         genCode(argumentCount);
  394.         }
  395.     else
  396.         genInstruction(SendMessage, argumentCount);
  397.     genCode(genLiteral(messagesym));
  398. }
  399.  
  400. static boolean unaryContinuation(superReceiver)
  401. boolean superReceiver;
  402. {    int i;
  403.     boolean sent;
  404.     object messagesym;
  405.  
  406.     while (parseok && (token == nameconst)) {
  407.         /* first check to see if it could be a temp by mistake */
  408.         for (i=1; i < temporaryTop; i++)
  409.             if (streq(tokenString, temporaryName[i]))
  410.                 compilWarn("message same as temporary:",
  411.                     tokenString);
  412.         for (i=1; i < argumentTop; i++)
  413.             if (streq(tokenString, argumentName[i]))
  414.                 compilWarn("message same as argument:",
  415.                     tokenString);
  416.         /* the next generates too many spurious messages */
  417.         /* for (i=1; i < instanceTop; i++)
  418.             if (streq(tokenString, instanceName[i]))
  419.                 compilWarn("message same as instance",
  420.                     tokenString); */
  421.  
  422.         sent = false;
  423.         messagesym = newSymbol(tokenString);
  424.         /* check for built in messages */
  425.         if (! superReceiver)
  426.             for (i = 0; (! sent) && unStrs[i] ; i++)
  427.                 if (streq(tokenString, unStrs[i])) {
  428.                     genInstruction(SendUnary, i);
  429.                     sent = true;
  430.                     }
  431.         if (! sent) {
  432.             genMessage(superReceiver, 0, messagesym);
  433.             }
  434.         /* once a message is sent to super, reciever is not super */
  435.         superReceiver = false;
  436.         ignore nextToken();
  437.         }
  438.     return(superReceiver);
  439. }
  440.  
  441. static boolean binaryContinuation(superReceiver)
  442. boolean superReceiver;
  443. {    int i;
  444.     boolean sent, superTerm;
  445.     object messagesym;
  446.  
  447.     superReceiver = unaryContinuation(superReceiver);
  448.     while (parseok && (token == binary)) {
  449.         messagesym = newSymbol(tokenString);
  450.         ignore nextToken();
  451.         superTerm = term();
  452.         ignore unaryContinuation(superTerm);
  453.         sent = false;
  454.         /* check for built in messages */
  455.         if (! superReceiver) {
  456.             for (i = 0; (! sent) && binStrs[i]; i++)
  457.                 if (messagesym == binSyms[i]) {
  458.                     genInstruction(SendBinary, i);
  459.                     sent = true;
  460.                     }
  461.  
  462.             }
  463.         if (! sent) {
  464.             genMessage(superReceiver, 1, messagesym);
  465.             }
  466.         superReceiver = false;
  467.         }
  468.     return(superReceiver);
  469. }
  470.  
  471. static int optimizeBlock(instruction, dopop)
  472. int instruction;
  473. boolean dopop;
  474. {    int location;
  475.     boolean saveOB;
  476.  
  477.     genInstruction(DoSpecial, instruction);
  478.     location = codeTop;
  479.     genCode(0);
  480.     if (dopop)
  481.         genInstruction(DoSpecial, PopTop);
  482.     ignore nextToken();
  483.     if (streq(tokenString, "[")) {
  484.         ignore nextToken();
  485.         saveOB = optimizedBlock;
  486.         optimizedBlock = true;
  487.         body();
  488.         optimizedBlock = saveOB;
  489.         if (! streq(tokenString, "]"))
  490.             compilError("missing close","after block");
  491.         ignore nextToken();
  492.         }
  493.     else {
  494.         ignore binaryContinuation(term());
  495.         genInstruction(SendUnary, 3 /* value command */);
  496.         }
  497.     codeArray[location] = codeTop;
  498.     return(location);
  499. }
  500.  
  501. static boolean keyContinuation(superReceiver)
  502. boolean superReceiver;
  503. {    int i, j, argumentCount;
  504.     boolean sent, superTerm;
  505.     object messagesym;
  506.     char pattern[80];
  507.  
  508.     superReceiver = binaryContinuation(superReceiver);
  509.     if (token == namecolon) {
  510.         if (streq(tokenString, "ifTrue:")) {
  511.             i = optimizeBlock(BranchIfFalse, false);
  512.             if (streq(tokenString, "ifFalse:")) {
  513.                 codeArray[i] = codeTop + 3;
  514.                 ignore optimizeBlock(Branch, true);
  515.                 }
  516.             }
  517.         else if (streq(tokenString, "ifFalse:")) {
  518.             i = optimizeBlock(BranchIfTrue, false);
  519.             if (streq(tokenString, "ifTrue:")) {
  520.                 codeArray[i] = codeTop + 3;
  521.                 ignore optimizeBlock(Branch, true);
  522.                 }
  523.             }
  524.         else if (streq(tokenString, "whileTrue:")) {
  525.             j = codeTop;
  526.             genInstruction(DoSpecial, Duplicate);
  527.             genInstruction(SendUnary, 3 /* value command */);
  528.             i = optimizeBlock(BranchIfFalse, false);
  529.             genInstruction(DoSpecial, PopTop);
  530.             genInstruction(DoSpecial, Branch);
  531.             genCode(j);
  532.             codeArray[i] = codeTop;
  533.             genInstruction(DoSpecial, PopTop);
  534.             }
  535.         else if (streq(tokenString, "and:"))
  536.             ignore optimizeBlock(AndBranch, false);
  537.         else if (streq(tokenString, "or:"))
  538.             ignore optimizeBlock(OrBranch, false);
  539.         else {
  540.             pattern[0] = '\0';
  541.             argumentCount = 0;
  542.             while (parseok && (token == namecolon)) {
  543.                 ignore strcat(pattern, tokenString);
  544.                 argumentCount++;
  545.                 ignore nextToken();
  546.                 superTerm = term();
  547.                 ignore binaryContinuation(superTerm);
  548.                 }
  549.             sent = false;
  550.  
  551.             /* check for predefined messages */
  552.             messagesym = newSymbol(pattern);
  553.             if (! superReceiver) {
  554.                 for (i = 0; (! sent) && binStrs[i]; i++)
  555.                     if (messagesym == binSyms[i]) {
  556.                         sent = true;
  557.                         genInstruction(SendBinary, i);
  558.                         }
  559.  
  560.                 for (i = 0; (! sent) && keyStrs[i]; i++)
  561.                     if (messagesym == keySyms[i]) {
  562.                         genInstruction(SendKeyword, i);
  563.                         sent = true;
  564.                         }
  565.                 }
  566.  
  567.             if (! sent) {
  568.                 genMessage(superReceiver, argumentCount, messagesym);
  569.                 }
  570.             }
  571.         superReceiver = false;
  572.         }
  573.     return(superReceiver);
  574. }
  575.  
  576. static continuation(superReceiver)
  577. boolean superReceiver;
  578. {
  579.     superReceiver = keyContinuation(superReceiver);
  580.  
  581.     while (parseok && (token == closing) && streq(tokenString, ";")) {
  582.         genInstruction(DoSpecial, Duplicate);
  583.         ignore nextToken();
  584.         ignore keyContinuation(superReceiver);
  585.         genInstruction(DoSpecial, PopTop);
  586.         }
  587. }
  588.  
  589. static expression()
  590. {    boolean superTerm;
  591.  
  592.     superTerm = term();
  593.     if (parseok)
  594.         continuation(superTerm);
  595. }
  596.  
  597. static assignment(name)
  598. char *name;
  599. {    int i;
  600.     boolean done;
  601.  
  602.     done = false;
  603.  
  604.     /* it might be a temporary */
  605.     for (i = 1; (! done) && (i <= temporaryTop); i++)
  606.         if (streq(name, temporaryName[i])) {
  607.             genInstruction(PopTemporary, i-1);
  608.             done = true;
  609.             }
  610.  
  611.     /* or it might be an instance variable */
  612.     for (i = 1; (! done) && (i <= instanceTop); i++)
  613.         if (streq(name, instanceName[i])) {
  614.             genInstruction(PopInstance, i-1);
  615.             done = true;
  616.             }
  617.  
  618.     if (! done)
  619.         compilError("assignment to unknown name", name);
  620. }
  621.  
  622. static statement()
  623. {    char assignname[80];
  624.     boolean superReceiver = false;
  625.  
  626.     if ((token == binary) && streq(tokenString, "^")) {
  627.         ignore nextToken();
  628.         expression();
  629.         if (inBlock)
  630.             genInstruction(DoSpecial, BlockReturn);
  631.         else
  632.             genInstruction(DoSpecial, StackReturn);
  633.         }
  634.     else if (token == nameconst) {    /* possible assignment */
  635.         ignore strcpy(assignname, tokenString);
  636.         ignore nextToken();
  637.         if ((token == binary) && streq(tokenString, "<-")) {
  638.             ignore nextToken();
  639.             expression();
  640.             if (inBlock || optimizedBlock)
  641.                 if ((token == closing) && streq(tokenString,"]"))
  642.                     genInstruction(DoSpecial, Duplicate);
  643.             assignment(assignname);
  644.             if (inBlock && (token == closing) &&
  645.                 streq(tokenString, "]"))
  646.                 genInstruction(DoSpecial, StackReturn);
  647.             }
  648.         else {        /* not an assignment after all */
  649.             superReceiver = nameTerm(assignname);
  650.             continuation(superReceiver);
  651.             if ((token == closing) && streq(tokenString, "]")) {
  652.                 if (inBlock && ! optimizedBlock)
  653.                     genInstruction(DoSpecial, StackReturn);
  654.                 }
  655.             else
  656.                 genInstruction(DoSpecial, PopTop);
  657.             }
  658.         }
  659.     else {
  660.         expression();
  661.         if ((token == closing) && streq(tokenString, "]")) {
  662.             if (inBlock && ! optimizedBlock)
  663.                 genInstruction(DoSpecial, StackReturn);
  664.             }
  665.         else
  666.             genInstruction(DoSpecial, PopTop);
  667.         }
  668. }
  669.  
  670. static body()
  671. {
  672.     if (inBlock || optimizedBlock)
  673.         if ((token == closing) && streq(tokenString, "]")) {
  674.             genInstruction(PushConstant, 4);
  675.             if (! optimizedBlock)
  676.                 genInstruction(DoSpecial, StackReturn);
  677.             return;
  678.             }
  679.  
  680.     while(parseok) {
  681.         statement();
  682.         if (token == closing)
  683.             if (streq(tokenString,".")) {
  684.                 ignore nextToken();
  685.                 if (token == inputend)
  686.                     break;
  687.                 }
  688.             else
  689.                 break;
  690.         else
  691.             if (token == inputend)
  692.                 break;
  693.         else {
  694.             compilError("invalid statement ending; token is ",
  695.                 tokenString);
  696.             }
  697.         }
  698. }
  699.  
  700. static block()
  701. {    int saveTemporary, argumentCount, fixLocation;
  702.     boolean saveInBlock, saveOB;
  703.     object tempsym;
  704.  
  705.     saveTemporary = temporaryTop;
  706.     argumentCount = 0;
  707.     ignore nextToken();
  708.     if ((token == binary) && streq(tokenString, ":")) {
  709.         while (parseok && (token == binary) && streq(tokenString,":")) {
  710.             if (nextToken() != nameconst)
  711.                 compilError("name must follow colon",
  712.                     "in block argument list");
  713.                 if (++temporaryTop > maxTemporary)
  714.                 maxTemporary = temporaryTop;
  715.             argumentCount++;
  716.             if (temporaryTop > temporaryLimit)
  717.                 compilError("too many temporaries in method","");
  718.             else {
  719.                 tempsym = newSymbol(tokenString);
  720.                 temporaryName[temporaryTop] = charPtr(tempsym);
  721.                 }
  722.             ignore nextToken();
  723.             }
  724.         if ((token != binary) || ! streq(tokenString, "|"))
  725.             compilError("block argument list must be terminated",
  726.                     "by |");
  727.         ignore nextToken();
  728.         }
  729.     genInstruction(CreateBlock, argumentCount);
  730.     if (argumentCount != 0){
  731.         genCode(saveTemporary + 1);
  732.         }
  733.     fixLocation = codeTop;
  734.     genCode(0);
  735.     saveInBlock = inBlock;
  736.     saveOB = optimizedBlock;
  737.     inBlock = true;
  738.     optimizedBlock = false;
  739.     body();
  740.     if ((token == closing) && streq(tokenString, "]"))
  741.         ignore nextToken();
  742.     else
  743.         compilError("block not terminated by ]","");
  744.     codeArray[fixLocation] = codeTop;
  745.     inBlock = saveInBlock;
  746.     optimizedBlock = saveOB;
  747.     temporaryTop = saveTemporary;
  748. }
  749.  
  750. static temporaries()
  751. {    object tempsym;
  752.  
  753.     temporaryTop = 0;
  754.     if ((token == binary) && streq(tokenString, "|")) {
  755.         ignore nextToken();
  756.         while (token == nameconst) {
  757.             if (++temporaryTop > maxTemporary)
  758.                 maxTemporary = temporaryTop;
  759.             if (temporaryTop > temporaryLimit)
  760.                 compilError("too many temporaries in method","");
  761.             else {
  762.                 tempsym = newSymbol(tokenString);
  763.                 temporaryName[temporaryTop] = charPtr(tempsym);
  764.                 }
  765.             ignore nextToken();
  766.             }
  767.         if ((token != binary) || ! streq(tokenString, "|"))
  768.             compilError("temporary list not terminated by bar","");
  769.         else
  770.             ignore nextToken();
  771.         }
  772. }
  773.  
  774. static messagePattern()
  775. {    object argsym;
  776.  
  777.     argumentTop = 0;
  778.     ignore strcpy(selector, tokenString);
  779.     if (token == nameconst)        /* unary message pattern */
  780.         ignore nextToken();
  781.     else if (token == binary) {    /* binary message pattern */
  782.         ignore nextToken();
  783.         if (token != nameconst) 
  784.             compilError("binary message pattern not followed by name",selector);
  785.         argsym = newSymbol(tokenString);
  786.         argumentName[++argumentTop] = charPtr(argsym);
  787.         ignore nextToken();
  788.         }
  789.     else if (token == namecolon) {    /* keyword message pattern */
  790.         selector[0] = '\0';
  791.         while (parseok && (token == namecolon)) {
  792.             ignore strcat(selector, tokenString);
  793.             ignore nextToken();
  794.             if (token != nameconst)
  795.                 compilError("keyword message pattern",
  796.                     "not followed by a name");
  797.             if (++argumentTop > argumentLimit)
  798.                 compilError("too many arguments in method","");
  799.             argsym = newSymbol(tokenString);
  800.             argumentName[argumentTop] = charPtr(argsym);
  801.             ignore nextToken();
  802.             }
  803.         }
  804.     else
  805.         compilError("illegal message selector", tokenString);
  806. }
  807.  
  808. boolean parse(method, text)
  809. object method;
  810. char *text;
  811. {    int i;
  812.     object bytecodes, theLiterals;
  813.     byte *bp;
  814.  
  815.     lexinit(text);
  816.     parseok = true;
  817.     codeTop = 0;
  818.     literalTop = temporaryTop = argumentTop =0;
  819.     maxTemporary = 0;
  820.     inBlock = optimizedBlock = false;
  821.  
  822.     messagePattern();
  823.     if (parseok)
  824.         temporaries();
  825.     if (parseok)
  826.         body();
  827.     if (parseok)
  828.         genInstruction(DoSpecial, SelfReturn);
  829.  
  830.     if (! parseok)
  831.         basicAtPut(method, bytecodesInMethod, nilobj);
  832.     else {
  833.         bytecodes = newByteArray(codeTop);
  834.         bp = bytePtr(bytecodes);
  835.         for (i = 0; i < codeTop; i++) {
  836.             bp[i] = codeArray[i];
  837.             }
  838.         basicAtPut(method, messageInMethod, newSymbol(selector));
  839.         basicAtPut(method, bytecodesInMethod, bytecodes);
  840.         if (literalTop > 0) {
  841.             theLiterals = newArray(literalTop);
  842.             for (i = 1; i <= literalTop; i++) {
  843.                 basicAtPut(theLiterals, i, literalArray[i]);
  844.                 decr(literalArray[i]);
  845.                 }
  846.             basicAtPut(method, literalsInMethod, theLiterals);
  847.             }
  848.         else
  849.             basicAtPut(method, literalsInMethod, nilobj);
  850.         basicAtPut(method, stackSizeInMethod, newInteger(6));
  851.         basicAtPut(method, temporarySizeInMethod,
  852.             newInteger(1 + maxTemporary));
  853.         basicAtPut(method, textInMethod, newStString(text));
  854.         return(true);
  855.         }
  856.     return(false);
  857. }
  858.