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