2003/06/13 09:19:10
authormegacz <megacz@xwt.org>
Fri, 30 Jan 2004 07:01:08 +0000 (07:01 +0000)
committermegacz <megacz@xwt.org>
Fri, 30 Jan 2004 07:01:08 +0000 (07:01 +0000)
darcs-hash:20040130070108-2ba56-2d9ea493efe82e0a8306c39defb4764f66380e1f.gz

src/org/xwt/js/CompiledFunction.java
src/org/xwt/js/Lexer.java
src/org/xwt/js/Parser.java

index 23b3f95..cddaeca 100644 (file)
@@ -55,7 +55,7 @@ public final class CompiledFunction extends JS.Callable implements ByteCodes, To
        try {
            while(true) {
                int s = size();
-               p.parseStatement(false, this);
+               p.parseStatement(this, null);
                if (s == size()) break;
            }
            add(-1, Tokens.RETURN);
index 4752c64..f4c386e 100644 (file)
@@ -17,6 +17,8 @@
  * Contributor(s): Roger Lawrence, Mike McCabe
  */
 
+// FIXME: mark lots of these methods 'final' so they get inlined
+
 package org.xwt.js;
 import java.io.*;
 
@@ -32,6 +34,9 @@ class Lexer implements Tokens {
 
     /** the token that was just parsed */
     protected int op;
+   /** the most recently parsed token, <i>regardless of pushbacks</i> */
+    protected int mostRecentlyReadToken;
 
     /** if the token just parsed was a NUMBER, this is the numeric value */
     protected Number number = null;
@@ -400,7 +405,10 @@ class Lexer implements Tokens {
     public int getToken() throws IOException {
        number = null;
        string = null;
-       if (pushBackDepth == 0) return op = _getToken();
+       if (pushBackDepth == 0) {
+           mostRecentlyReadToken = op;
+           return op = _getToken();
+       }
        pushBackDepth--;
        op = pushBackInts[pushBackDepth];
        if (pushBackObjects[pushBackDepth] != null) {
index fa36d6a..f5ddd42 100644 (file)
@@ -1,10 +1,57 @@
 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
 package org.xwt.js;
 
+// FIXME: line number accuracy
+
 import org.xwt.util.*;
 import java.io.*;
 
-/** Parses a stream of lexed tokens into a tree of CompiledFunction's */
+/**
+ *  Parses a stream of lexed tokens into a tree of CompiledFunction's.
+ *
+
+ *  There are three kinds of things we parse: blocks, statements,
+ *  expressions.  Expressions are a special type of statement that
+ *  evaluates to a value (for example, "break" is not an expression,
+ *  but "3+2" is).  AssignmentTargets are a special kind of expression
+ *  that can be 'put' to (for example, "foo()" is not an
+ *  assignmentTarget, but "foo[7]" is). FIXME.
+
+ *
+ *  Technically it would be a better design for this class to build an
+ *  intermediate parse tree and use that to emit bytecode.  Here's the
+ *  tradeoff:
+ *
+ *  Advantages of building a parse tree:
+ *  - easier to apply optimizations
+ *  - would let us handle more sophisticated languages than JavaScript
+ *
+ *  Advantages of leaving out the parse tree
+ *  - faster compilation
+ *  - less load on the garbage collector
+ *  - much simpler code, easier to understand
+ *  - less error-prone
+ *
+ *  Fortunately JS is such a simple language that we can get away with
+ *  the half-assed approach and still produce a working, complete
+ *  compiler.
+ *
+ *  The bytecode language emitted doesn't really cause any appreciable
+ *  semantic loss, and is itself a parseable language very similar to
+ *  Forth or a postfix variant of LISP.  This means that the bytecode
+ *  can be transformed into a parse tree, which can be manipulated.
+ *  So if we ever want to add an optimizer, it could easily be done by
+ *  producing a parse tree from the bytecode, optimizing that tree,
+ *  and then re-emitting the bytecode.  The parse tree node class
+ *  would also be much simpler since the bytecode language has so few
+ *  operators.
+ *
+ *  Actually, the above paragraph is slightly inaccurate -- there are
+ *  places where we push a value and then perform an arbitrary number
+ *  of operations using it before popping it; this doesn't parse well.
+ *  But these cases are clearly marked and easy to change if we do
+ *  need to move to a parse tree format.
+ */
 class Parser extends Lexer implements ByteCodes {
 
 
@@ -52,231 +99,241 @@ class Parser extends Lexer implements ByteCodes {
     // Parsing Logic /////////////////////////////////////////////////////////
 
     /** gets a token and throws an exception if it is not <tt>code</tt> */
-    public void consume(int code) throws IOException {
-       if (getToken() != code)
-           throw new ParserException("expected " + codeToString[code] + ", got " + (op == -1 ? "EOF" : codeToString[op]));
+    private void consume(int code) throws IOException {
+       if (getToken() != code) throw new ParserException("expected " + codeToString[code] + ", got " + (op == -1 ? "EOF" : codeToString[op]));
     }
 
-    /** append the largest possible expression containing no operators of precedence below <tt>minPrecedence</tt> */
-    public void startExpr(int minPrecedence, CompiledFunction appendTo) throws IOException {
+    /**
+     *  Parse the largest possible expression containing no operators
+     *  of precedence below <tt>minPrecedence</tt> and append the
+     *  bytecodes for that expression to <tt>appendTo</tt>; the
+     *  appended bytecodes MUST grow the stack by exactly one element.
+     */
+    private void startExpr(CompiledFunction appendTo, int minPrecedence) throws IOException {
        int tok = getToken();
        CompiledFunction b = appendTo;
 
        switch (tok) {
-       case -1: return;
-       case NUMBER: continueExpr(b.add(line, CompiledFunction.LITERAL, number), minPrecedence); return;
-       case STRING: continueExpr(b.add(line, CompiledFunction.LITERAL, string), minPrecedence); return;
-       case THIS: continueExpr(b.add(line, TOPSCOPE, null), minPrecedence); return;
-       case NULL: continueExpr(b.add(line, CompiledFunction.LITERAL, null), minPrecedence); return;
-       case TRUE: case FALSE: continueExpr(b.add(line, CompiledFunction.LITERAL, new Boolean(tok == TRUE)), minPrecedence); return;
+       case -1: throw new ParserException("expected expression");
+
+        // all of these simply push values onto the stack
+       case NUMBER: b.add(line, LITERAL, number); break;
+       case STRING: b.add(line, LITERAL, string); break;
+       case THIS: b.add(line, TOPSCOPE, null); break;
+       case NULL: b.add(line, LITERAL, null); break;
+       case TRUE: case FALSE: b.add(line, LITERAL, new Boolean(tok == TRUE)); break;
+
        case LB: {
-           b.add(line, ARRAY, new Integer(0));
+           b.add(line, ARRAY, new Integer(0));                       // push an array onto the stack
+           int size0 = b.size();
            int i = 0;
-           while(true) {
-               int size = b.size();
-               startExpr(-1, b);
-               if (size == b.size())
-                   if (peekToken() == RB) { consume(RB); continueExpr(b, minPrecedence); return; }
-               b.add(line, LITERAL, new Integer(i++));
-               if (size == b.size()) b.add(line, LITERAL, null);
-               b.add(line, PUT);
-               b.add(line, POP);
-               if (peekToken() == RB) { consume(RB); continueExpr(b, minPrecedence); return; }
-               consume(COMMA);
-           }
+           if (peekToken() != RB)
+               while(true) {                                         // iterate over the initialization values
+                   int size = b.size();
+                   if (peekToken() == COMMA || peekToken() == RB)
+                       b.add(line, LITERAL, null);                   // for stuff like [1,,2,]
+                   else
+                       startExpr(b, -1);                             // push the value onto the stack
+                   b.add(line, LITERAL, new Integer(i++));           // push the index in the array to place it into
+                   b.add(line, PUT);                                 // put it into the array
+                   b.add(line, POP);                                 // discard the value remaining on the stack
+                   if (peekToken() == RB) break;
+                   consume(COMMA);
+               }
+           b.set(size0 - 1, new Integer(i));                         // back at the ARRAY instruction, write the size of the array
+           consume(RB);
+           break;
        }
-       case SUB: {
+       case SUB: {  // negative literal (like "3 * -1")
            consume(NUMBER);
-           b.add(line, CompiledFunction.LITERAL, new Double(number.doubleValue() * -1));
-           continueExpr(b, minPrecedence);
-           return;
+           b.add(line, LITERAL, new Double(number.doubleValue() * -1));
+           break;
        }
-       case LP: {
-           startExpr(-1, b);
+       case LP: {  // grouping (not calling)
+           startExpr(b, -1);
            consume(RP);
-           continueExpr(b, minPrecedence);
-           return;
+           break;
        }
-       case INC: case DEC: {
-           // prefix
-           startExpr(precedence[tok], b);
-           b.set(b.size() - 1, tok, new Boolean(true));
-           continueExpr(b, minPrecedence);
-           return;
+       case INC: case DEC: {  // prefix (not postfix)
+           startExpr(b, precedence[tok]);
+           b.set(b.size() - 1, tok, new Boolean(true));    // FIXME, ugly; need startAssignTarget
+           break;
        }
        case BANG: case BITNOT: case TYPEOF: {
-           startExpr(precedence[tok], b);
+           startExpr(b, precedence[tok]);
            b.add(line, tok);
-           continueExpr(b, minPrecedence);
-           return;
+           break;
        }
-       case LC: {
-           b.add(line, OBJECT, null);
-           if (peekToken() != RC) while(true) {
-               if (peekToken() != NAME && peekToken() != STRING) throw new Error("expected NAME or STRING");
-               getToken();
-               b.add(line, LITERAL, string);
-               consume(COLON);
-               startExpr(-1, b);
-               b.add(line, PUT);
-               b.add(line, POP);
-               if (peekToken() == RC) break;
-               consume(COMMA);
-               if (peekToken() == RC) break;
-           }
+       case LC: { // object constructor
+           b.add(line, OBJECT, null);                                           // put an object on the stack
+           if (peekToken() != RC)
+               while(true) {
+                   if (peekToken() != NAME && peekToken() != STRING)
+                       throw new ParserException("expected NAME or STRING");
+                   getToken();
+                   b.add(line, LITERAL, string);                                // grab the key
+                   consume(COLON);
+                   startExpr(b, -1);                                            // grab the value
+                   b.add(line, PUT);                                            // put the value into the object
+                   b.add(line, POP);                                            // discard the remaining value
+                   if (peekToken() == RC) break;
+                   consume(COMMA);
+                   if (peekToken() == RC) break;                                // we permit {,,} -- I'm not sure if ECMA does
+               }
            consume(RC);
-           continueExpr(b, minPrecedence);
-           return;
+           break;
        }
-       case NAME: {
+       case NAME: {    // FIXME; this is an lvalue
            String name = string;
            if (peekToken() == ASSIGN) {
                consume(ASSIGN);
                b.add(line, TOPSCOPE);
-               b.add(line, CompiledFunction.LITERAL, name);
-               startExpr(minPrecedence, b);
-               b.add(line, CompiledFunction.PUT);
-               b.add(line, CompiledFunction.SWAP);
-               b.add(line, CompiledFunction.POP);
+               b.add(line, LITERAL, name);
+               startExpr(b, minPrecedence);
+               b.add(line, PUT);
+               b.add(line, SWAP);
+               b.add(line, POP);
            } else {
                b.add(line, TOPSCOPE);
-               b.add(line, CompiledFunction.LITERAL, name);
-               b.add(line, CompiledFunction.GET);
+               b.add(line, LITERAL, name);
+               b.add(line, GET);
            }
-           continueExpr(b, minPrecedence);
-           return;
+           break;
        }
        case FUNCTION: {
            consume(LP);
            int numArgs = 0;
-           CompiledFunction b2 = new CompiledFunction(sourceName, line, null);
-           b2.add(line, TOPSCOPE);
-           b2.add(line, SWAP);
-           b2.add(line, LITERAL, "arguments");
-           b2.add(line, LITERAL, "arguments");
+           CompiledFunction b2 = new CompiledFunction(sourceName, line, null);    
+           b.add(line, NEWFUNCTION, b2);
+
+           // function prelude; arguments array is already on the stack
+           b2.add(line, TOPSCOPE);                                                // push the scope onto the stack
+           b2.add(line, SWAP);                                                    // swap 'this' and 'arguments'
+
+           b2.add(line, LITERAL, "arguments");                                    // declare arguments (equivalent to 'var arguments;')
            b2.add(line, DECLARE);
+
+           b2.add(line, LITERAL, "arguments");                                    // set this.arguments and leave the value on the stack
            b2.add(line, SWAP);
            b2.add(line, PUT);
            b2.add(line, SWAP);
            b2.add(line, POP);
-           if (peekToken() == RP) consume(RP);
-           else while(true) {
-               if (peekToken() == COMMA) {
-                   consume(COMMA);
-
-               } else {
-                   consume(NAME);
 
-                   // declare the name
-                   b2.add(line, LITERAL, string);
+           while(peekToken() != RP) {                              // run through the list of argument names
+               if (peekToken() == NAME) {
+                   consume(NAME);                                  // a named argument
+                   
+                   b2.add(line, LITERAL, string);                  // declare the name
                    b2.add(line, DECLARE);
-
-                   // retrieve it from the arguments array
-                   b2.add(line, LITERAL, new Integer(numArgs));
+                   
+                   b2.add(line, LITERAL, new Integer(numArgs));    // retrieve it from the arguments array
                    b2.add(line, GET_PRESERVE);
                    b2.add(line, SWAP);
                    b2.add(line, POP);
-
-                   // put it to the current scope
-                   b2.add(line, TOPSCOPE);
+                   
+                   b2.add(line, TOPSCOPE);                         // put it to the current scope
                    b2.add(line, SWAP);
                    b2.add(line, LITERAL, string);
                    b2.add(line, SWAP);
                    b2.add(line, PUT);
-
-                   // clean the stack
-                   b2.add(line, POP);
+                   
+                   b2.add(line, POP);                              // clean the stack
                    b2.add(line, POP);
-
-                   if (peekToken() == RP) { consume(RP); break; }
-                   consume(COMMA);
                }
+               if (peekToken() == RP) break;
+               consume(COMMA);
                numArgs++;
            }
-           // pop off the arguments array
-           b2.add(line, POP);
-           parseStatement(true, b2);
-           b2.add(line, LITERAL, null);
+           consume(RP);
+
+           b2.add(line, POP);                                      // pop off the arguments array
+
+           parseStatement(b2, null);                               // the function body
+
+           b2.add(line, LITERAL, null);                            // in case we "fall out the bottom", return NULL
            b2.add(line, RETURN);
-           continueExpr(b.add(line, NEWFUNCTION, b2), minPrecedence);
-           return;
+
+           break;
        }
-       default: pushBackToken(); return;
+       default: throw new ParserException("expected expression, found " + codeToString[tok] + ", which cannot start an expression");
        }
+
+       // attempt to continue the expression
+       continueExpr(b, minPrecedence);
     }
 
-    public void continueExpr(CompiledFunction prefix, int minPrecedence) throws IOException {
+    /**
+     *  Assuming that a complete expression has just been parsed,
+     *  <tt>continueExpr</tt> will attempt to extend this expression by
+     *  parsing additional tokens and appending additional bytecodes.
+     *
+     *  No operators with precedence less than <tt>minPrecedence</tt>
+     *  will be parsed.
+     *
+     *  If any bytecodes are appended, they will not alter the stack
+     *  depth.
+     */
+    private void continueExpr(CompiledFunction b, int minPrecedence) throws IOException {
+       if (b == null) throw new Error("got null b; this should never happen");
        int tok = getToken();
        if (tok == -1) return;
-       if (minPrecedence > 0 && precedence[tok] != 0)
-           if (precedence[tok] < minPrecedence || (precedence[tok] == minPrecedence && !isRightAssociative[tok]))
-               { pushBackToken(); return; }
-       if (prefix == null) throw new Error("got null prefix");
-       CompiledFunction b = prefix;
+       if (minPrecedence != -1 && (precedence[tok] < minPrecedence || (precedence[tok] == minPrecedence && !isRightAssociative[tok]))) {
+           pushBackToken();
+           return;
+       }
 
        switch (tok) {
-
         case ASSIGN_BITOR: case ASSIGN_BITXOR: case ASSIGN_BITAND: case ASSIGN_LSH: case ASSIGN_RSH: case ASSIGN_URSH:
        case ASSIGN_ADD: case ASSIGN_SUB: case ASSIGN_MUL: case ASSIGN_DIV: case ASSIGN_MOD: {
-           prefix.set(prefix.size() - 1, b.GET_PRESERVE, new Boolean(true));
-           startExpr(precedence[tok - 1], b);
-           prefix.add(line, tok - 1);
-           prefix.add(line, PUT);
-           prefix.add(line, SWAP);
-           prefix.add(line, POP);
-           continueExpr(b, minPrecedence);
-           return;
+           b.set(b.size() - 1, b.GET_PRESERVE, new Boolean(true));  // FIXME should use AssignTarget
+           startExpr(b, precedence[tok - 1]);
+           b.add(line, tok - 1);
+           b.add(line, PUT);
+           b.add(line, SWAP);
+           b.add(line, POP);
+           break;
        }
-
-       case INC: case DEC: {
-           // postfix
-           b.set(b.size() - 1, tok, new Boolean(false));
-           continueExpr(b, minPrecedence);
-           return;
+       case INC: case DEC: { // postfix
+           b.set(b.size() - 1, tok, new Boolean(false));   // FIXME use assignmenttarget
+           break;
        }
-
-       case LP: {
-           // invocation
+       case LP: {  // invocation (not grouping)
            int i = 0;
            while(peekToken() != RP) {
                i++;
-               startExpr(-1, b);
-               if (peekToken() == RP) break;
+               if (peekToken() != COMMA) {
+                   startExpr(b, -1);
+                   if (peekToken() == RP) break;
+               }
                consume(COMMA);
            }
            consume(RP);
            b.add(line, CALL, new Integer(i));
-           continueExpr(b, minPrecedence);
-           return;
+           break;
        }
-
         case BITOR: case BITXOR: case BITAND: case SHEQ: case SHNE: case LSH:
        case RSH: case URSH: case ADD: case MUL: case DIV: case MOD:
        case GT: case GE: case EQ: case NE: case LT: case LE: case SUB: {
-           startExpr(precedence[tok], b);
+           startExpr(b, precedence[tok]);
            b.add(line, tok);
-           continueExpr(b, minPrecedence);
-           return;
+           break;
        }
-
        case OR: case AND: {
-           b.add(line, tok == AND ? b.JF : b.JT, new Integer(0));
+           b.add(line, tok == AND ? b.JF : b.JT, new Integer(0));                     // test to see if we can short-circuit
            int size = b.size();
-           startExpr(precedence[tok], b);
-           b.set(size - 1, new Integer(b.size() - size + 2));
-           b.add(line, JMP, new Integer(2));
-           b.add(line, LITERAL, tok == AND ? new Boolean(false) : new Boolean(true));
-           continueExpr(b, minPrecedence);
-           return;
+           startExpr(b, precedence[tok]);                                             // otherwise check the second value
+           b.add(line, JMP, new Integer(2));                                          // leave the second value on the stack and jump to the end
+           b.add(line, LITERAL, tok == AND ? new Boolean(false) : new Boolean(true)); // target of the short-circuit jump is here
+           b.set(size - 1, new Integer(b.size() - size));                             // write the target of the short-circuit jump
+           break;
        }
-
-       case DOT: {
+       case DOT: {  // FIXME, assigntarget
            consume(NAME);
            String target = string;
            if (peekToken() == ASSIGN) {
                consume(ASSIGN);
                b.add(line, LITERAL, target);
-               startExpr(-1, b);
+               startExpr(b, -1);
                b.add(line, PUT);
                b.add(line, SWAP);
                b.add(line, POP);
@@ -284,359 +341,344 @@ class Parser extends Lexer implements ByteCodes {
                b.add(line, LITERAL, target);
                b.add(line, GET);
            }
-           continueExpr(b, minPrecedence);
-           return;
+           break;
        }
-
-       case LB: {
-           startExpr(-1, b);
+       case LB: { // subscripting (not array constructor)
+           startExpr(b, -1);
            consume(RB);
-           if (peekToken() == ASSIGN) {
+           if (peekToken() == ASSIGN) { // FIXME: assigntarget
                consume(ASSIGN);
-               startExpr(-1, b);
+               startExpr(b, -1);
                b.add(line, PUT);
                b.add(line, SWAP);
                b.add(line, POP);
            } else {
                b.add(line, GET);
            }
-           continueExpr(b, minPrecedence);
-           return;
+           break;
        }
-
        case HOOK: {
-           b.add(line, JF, new Integer(0));
+           b.add(line, JF, new Integer(0));                      // jump to the if-false expression
            int size = b.size();
-           startExpr(-1, b);
-           b.set(size - 1, new Integer(b.size() - size + 2));
-           b.add(line, JMP, new Integer(0));
+           startExpr(b, -1);                                     // write the if-true expression
+           b.add(line, JMP, new Integer(0));                     // if true, jump *over* the if-false expression     
+           b.set(size - 1, new Integer(b.size() - size + 1));    // now we know where the target of the jump is
            consume(COLON);
            size = b.size();
-           startExpr(-1, b);
-           b.set(size - 1, new Integer(b.size() - size + 1));
-           continueExpr(b, minPrecedence);
+           startExpr(b, -1);                                     // write the if-false expression
+           b.set(size - 1, new Integer(b.size() - size + 1));    // this is the end; jump to here
+           break;
+       }
+       default: {
+           pushBackToken();
            return;
        }
-           
-       default: { pushBackToken(); return; }
        }
+
+       continueExpr(b, minPrecedence);                           // try to continue the expression
     }
     
-    /**
-     *  a block is either a single statement or a list of statements surrounded by curly braces;
-     *  all expressions are also statements
-     */
-    public CompiledFunction parseStatement() throws IOException {
-       CompiledFunction ret = new CompiledFunction(sourceName, line, null);
-       ret.add(line, ret.LITERAL, null);
-       parseStatement(false, ret);
-       if (ret.size() == 1) return null;
-       return ret;
+    /** Parse a block of statements which must be surrounded by LC..RC. */
+    void parseBlock(CompiledFunction b) throws IOException { parseBlock(b, null); }
+    void parseBlock(CompiledFunction b, String label) throws IOException {
+       if (peekToken() == -1) return;
+       else if (peekToken() != LC) parseStatement(b, null);
+       else {
+           consume(LC);
+           while(peekToken() != RC && peekToken() != -1) parseStatement(b, null);
+           consume(RC);
+       }
     }
 
-    public void parseStatement(boolean r, CompiledFunction b) throws IOException { parseStatement(r, b, null); }
-    public void parseStatement(boolean requireBraces, CompiledFunction b, String label) throws IOException {
+    /** Parse a single statement, consuming the RC or SEMI which terminates it. */
+    void parseStatement(CompiledFunction b, String label) throws IOException {
        int tok = peekToken();
        if (tok == -1) return;
-       int line = this.line;
-       boolean braced = tok == LC;
-       if (requireBraces && !braced) throw new ParserException("expected {, got " + codeToString[tok]);
-       if (braced) consume(LC);
-       while(true) {
-           switch(tok = peekToken()) {
-
-           case THROW: case RETURN: case ASSERT: {
-               getToken();
-               if (tok == RETURN && peekToken() == SEMI) b.add(line, LITERAL, null);
-               else startExpr(-1, b);
-               consume(SEMI);
-               b.add(line, tok);
-               break;
-           }
-
-           case BREAK: case CONTINUE: {
-               getToken();
-               if (peekToken() == NAME) consume(NAME);
-               b.add(line, tok, string);
-               consume(SEMI);
-               break;
+       switch(tok = getToken()) {
+           
+       case THROW: case ASSERT: case RETURN: {
+           if (tok == RETURN && peekToken() == SEMI) b.add(line, LITERAL, null);
+           else startExpr(b, -1);
+           b.add(line, tok);
+           consume(SEMI);
+           break;
+       }
+           
+       case BREAK: case CONTINUE: {
+           if (peekToken() == NAME) consume(NAME);
+           b.add(line, tok, string);
+           consume(SEMI);
+           break;
+       }
+           
+       case VAR: {
+           b.add(line, TOPSCOPE);                               // push the current scope
+           while(true) {
+               consume(NAME);
+               String name = string;
+               b.add(line, LITERAL, name);                // push the name to be declared
+               b.add(line, DECLARE);                      // declare it
+               if (peekToken() == ASSIGN) {           // if there is an '=' after the variable name
+                   b.add(line, LITERAL, name);            // put the var name back on the stack
+                   consume(ASSIGN);
+                   startExpr(b, -1);
+                   b.add(line, PUT);
+                   b.add(line, POP);
+               }
+               if (peekToken() != COMMA) break;
+               consume(COMMA);
            }
-               
-           case SEMI: {
-               consume(SEMI);
-               if (!braced) return;
-               break;
+           b.add(line, POP);
+           if ((mostRecentlyReadToken != RC || peekToken() == SEMI) && peekToken() != -1) consume(SEMI);
+           break;
+       }
+           
+       case IF: {
+           consume(LP);
+           startExpr(b, -1);
+           consume(RP);
+           
+           b.add(line, JF, new Integer(0));
+           int size = b.size();
+           parseStatement(b, null);
+           
+           if (peekToken() == ELSE) {
+               consume(ELSE);
+               b.set(size - 1, new Integer(2 + b.size() - size));
+               b.add(line, JMP, new Integer(0));
+               size = b.size();
+               parseStatement(b, null);
            }
-               
-           case VAR: {
-               consume(VAR);
-               b.add(line, TOPSCOPE);                               // push the current scope
-               while(true) {
-                   consume(NAME);
-                   String name = string;
-                   b.add(line, LITERAL, name);                // push the name to be declared
-                   b.add(line, DECLARE);                      // declare it
-                   if (peekToken() == ASSIGN) {           // if there is an '=' after the variable name
-                       b.add(line, LITERAL, name);            // put the var name back on the stack
-                       consume(ASSIGN);
-                       startExpr(-1, b);
-                       b.add(line, PUT);
-                       b.add(line, POP);
+           b.set(size - 1, new Integer(1 + b.size() - size));
+           break;
+       }
+           
+       case WHILE: {
+           consume(LP);
+           if (label != null) b.add(line, LABEL, label);
+           b.add(line, LOOP);
+           int size = b.size();
+           b.add(line, POP);
+           startExpr(b, -1);
+           b.add(line, JT, new Integer(2));
+           b.add(line, BREAK);
+           consume(RP);
+           parseStatement(b, null);
+           b.add(line, CONTINUE);                                    // if we fall out of the end, definately continue
+           b.set(size - 1, new Integer(b.size() - size + 1));  // end of the loop
+           break;
+       }
+           
+       case SWITCH: {
+           consume(LP);
+           if (label != null) b.add(line, LABEL, label);
+           b.add(line, LOOP);
+           int size0 = b.size();
+           startExpr(b, -1);
+           consume(RP);
+           consume(LC);
+           while(true)
+               if (peekToken() == CASE) {
+                   consume(CASE);
+                   b.add(line, DUP);
+                   startExpr(b, -1);
+                   consume(COLON);
+                   b.add(line, EQ);
+                   b.add(line, JF, new Integer(0));
+                   int size = b.size();
+                   while(peekToken() != CASE && peekToken() != DEFAULT && peekToken() != RC) {
+                       int size2 = b.size();
+                       parseStatement(b, null);
+                       if (size2 == b.size()) break;
                    }
-                   if (peekToken() != COMMA) break;
-                   consume(COMMA);
+                   b.set(size - 1, new Integer(1 + b.size() - size));
+               } else if (peekToken() == DEFAULT) {
+                   consume(DEFAULT);
+                   consume(COLON);
+                   while(peekToken() != CASE && peekToken() != DEFAULT && peekToken() != RC) {
+                       int size2 = b.size();
+                       parseStatement(b, null);
+                       if (size2 == b.size()) break;
+                   }
+               } else if (peekToken() == RC) {
+                   consume(RC);
+                   b.add(line, BREAK);
+                   break;
+               } else {
+                   throw new ParserException("expected CASE, DEFAULT, or RC; got " + codeToString[peekToken()]);
                }
-               b.add(line, POP);
-               if (peekToken() == SEMI) consume(SEMI);
-               break;
-           }
-               
-           case IF: {
-               consume(IF);
+           b.add(line, BREAK);
+           b.set(size0 - 1, new Integer(b.size() - size0 + 1));      // end of the loop
+           break;
+       }
+           
+       case DO: {
+           if (label != null) b.add(line, LABEL, label);
+           b.add(line, LOOP);
+           int size = b.size();
+           parseStatement(b, null);
+           consume(WHILE);
+           consume(LP);
+           startExpr(b, -1);
+           b.add(line, JT, new Integer(2));
+           b.add(line, BREAK);
+           b.add(line, CONTINUE);
+           consume(RP);
+           consume(SEMI);
+           b.set(size - 1, new Integer(b.size() - size + 1));      // end of the loop
+           break;
+       }
+           
+       case TRY: {
+           // We deliberately allow you to omit braces in catch{}/finally{} if they are single statements...
+           b.add(line, TRY);
+           int size = b.size();
+           parseBlock(b, null);
+           b.add(line, POP);                                 // pop the TryMarker
+           b.add(line, JMP);                                 // jump forward to the end of the catch block
+           int size2 = b.size();
+           b.set(size - 1, new Integer(b.size() - size + 1));// the TRY argument points at the start of the CATCH block
+           
+           if (peekToken() == CATCH) {
+               getToken();
                consume(LP);
-               startExpr(-1, b);
+               consume(NAME);
                consume(RP);
-
-               b.add(line, JF, new Integer(0));
-               int size = b.size();
-               parseStatement(false, b);
-               
-               if (peekToken() == ELSE) {
-                   consume(ELSE);
-                   b.set(size - 1, new Integer(2 + b.size() - size));
-                   b.add(line, JMP, new Integer(0));
-                   size = b.size();
-                   parseStatement(false, b);
-               }
-               b.set(size - 1, new Integer(1 + b.size() - size));
-               break;
-           }
-
-           case WHILE: {
-               consume(WHILE);
-               consume(LP);
-               if (label != null) b.add(line, LABEL, label);
-               b.add(line, LOOP);
-               int size = b.size();
+               // FIXME, we need an extra scope here
+               b.add(line, TOPSCOPE);                        // the exception is on top of the stack; put it to the variable
+               b.add(line, SWAP);
+               b.add(line, LITERAL);
+               b.add(line, SWAP);
+               b.add(line, PUT);
                b.add(line, POP);
-               startExpr(-1, b);
-               b.add(line, JT, new Integer(2));
-               b.add(line, BREAK);
-               consume(RP);
-               parseStatement(false, b);
-               b.add(line, CONTINUE);                                    // if we fall out of the end, definately continue
-               b.set(size - 1, new Integer(b.size() - size + 1));  // end of the loop
-               break;
+               b.add(line, POP);
+               parseStatement(b, null);
            }
-
-           case SWITCH: {
-               consume(SWITCH);
-               consume(LP);
-               if (label != null) b.add(line, LABEL, label);
-               b.add(line, LOOP);
-               int size0 = b.size();
-               startExpr(-1, b);
-               consume(RP);
-               consume(LC);
-               while(true)
-                   if (peekToken() == CASE) {
-                       consume(CASE);
-                       b.add(line, DUP);
-                       startExpr(-1, b);
-                       consume(COLON);
-                       b.add(line, EQ);
-                       b.add(line, JF, new Integer(0));
-                       int size = b.size();
-                       while(peekToken() != CASE && peekToken() != DEFAULT && peekToken() != RC) {
-                           int size2 = b.size();
-                           parseStatement(false, b);
-                           if (size2 == b.size()) break;
-                       }
-                       b.set(size - 1, new Integer(1 + b.size() - size));
-                   } else if (peekToken() == DEFAULT) {
-                       consume(DEFAULT);
-                       consume(COLON);
-                       while(peekToken() != CASE && peekToken() != DEFAULT && peekToken() != RC) {
-                           int size2 = b.size();
-                           parseStatement(false, b);
-                           if (size2 == b.size()) break;
-                       }
-                   } else if (peekToken() == RC) {
-                       consume(RC);
-                       b.add(line, BREAK);
-                       break;
-                   } else {
-                       throw new ParserException("expected CASE, DEFAULT, or RC; got " + codeToString[peekToken()]);
-                   }
-               b.add(line, BREAK);
-               b.set(size0 - 1, new Integer(b.size() - size0 + 1));      // end of the loop
-               break;
+           
+           b.set(size2 - 1, new Integer(b.size() - size2 + 1)); // jump here if no exception was thrown
+           
+           // FIXME: not implemented correctly
+           if (peekToken() == FINALLY) {
+               consume(FINALLY);
+               parseStatement(b, null);
            }
-               
-           case DO: {
-               consume(DO);
-               if (label != null) b.add(line, LABEL, label);
-               b.add(line, LOOP);
-               int size = b.size();
-               parseStatement(false, b);
-               consume(WHILE);
-               consume(LP);
-               startExpr(-1, b);
-               b.add(line, JT, new Integer(2));
-               b.add(line, BREAK);
-               b.add(line, CONTINUE);
+           break;
+       }
+           
+       case FOR: {
+           consume(LP);
+           
+           tok = getToken();
+           boolean hadVar = false;
+           if (tok == VAR) { hadVar = true; tok = getToken(); }
+           String varName = string;
+           boolean forIn = peekToken() == IN;
+           pushBackToken(tok, varName);
+           
+           if (forIn) {
+               // FIXME: break needs to work in here
+               consume(NAME);
+               consume(IN);
+               startExpr(b, -1);
+               b.add(line, PUSHKEYS);
+               b.add(line, LITERAL, "length");
+               b.add(line, GET);
                consume(RP);
-               consume(SEMI);
-               b.set(size - 1, new Integer(b.size() - size + 1));      // end of the loop
-               break;
-           }
-               
-           case TRY: {
-               // We deliberately allow you to omit braces in catch{}/finally{} if they are single statements...
-               consume(TRY);
-               b.add(line, TRY);
-               int size = b.size();
-               parseStatement(true, b);
-               b.add(line, POP);                                 // pop the TryMarker
-               b.add(line, JMP);                                 // jump forward to the end of the catch block
-               int size2 = b.size();
-               b.set(size - 1, new Integer(b.size() - size + 1));// the TRY argument points at the start of the CATCH block
-               
-               if (peekToken() == CATCH) {
-                   getToken();
-                   consume(LP);
-                   consume(NAME);
-                   consume(RP);
-                   // FIXME, we need an extra scope here
-                   b.add(line, TOPSCOPE);                        // the exception is on top of the stack; put it to the variable
-                   b.add(line, SWAP);
-                   b.add(line, LITERAL);
-                   b.add(line, SWAP);
-                   b.add(line, PUT);
-                   b.add(line, POP);
-                   b.add(line, POP);
-                   parseStatement(false, b);
-               }
-
-               b.set(size2 - 1, new Integer(b.size() - size2 + 1)); // jump here if no exception was thrown
-
-                     // FIXME: not implemented correctly
-               if (peekToken() == FINALLY) {
-                   consume(FINALLY);
-                   parseStatement(false, b);
-               }
-               break;
-           }
-
-           case FOR: {
-               consume(FOR);
-               consume(LP);
-               
-               tok = getToken();
-               boolean hadVar = false;
-               if (tok == VAR) { hadVar = true; tok = getToken(); }
-               String varName = string;
-               boolean forIn = peekToken() == IN;
-               pushBackToken(tok, varName);
-               
-               if (forIn) {
-                   // FIXME: break needs to work in here
-                   consume(NAME);
-                   consume(IN);
-                   startExpr(-1, b);
-                   b.add(line, PUSHKEYS);
-                   b.add(line, LITERAL, "length");
-                   b.add(line, GET);
-                   consume(RP);
-                   CompiledFunction b2 = new CompiledFunction(sourceName, line, null);
-                   
-                   b.add(line, NEWSCOPE);
+               CompiledFunction b2 = new CompiledFunction(sourceName, line, null);
                    
-                   b.add(line, LITERAL, new Integer(1));
-                   b.add(line, SUB);
-                   b.add(line, DUP);
-                   b.add(line, LITERAL, new Integer(0));
-                   b.add(line, LT);
-                   b.add(line, JT, new Integer(7));
-                   b.add(line, GET_PRESERVE);
-                   b.add(line, LITERAL, varName);
-                   b.add(line, LITERAL, varName);
-                   b.add(line, DECLARE);
-                   b.add(line, PUT);
-                   parseStatement(false, b);
+               b.add(line, NEWSCOPE);
                    
-                   b.add(line, OLDSCOPE);
+               b.add(line, LITERAL, new Integer(1));
+               b.add(line, SUB);
+               b.add(line, DUP);
+               b.add(line, LITERAL, new Integer(0));
+               b.add(line, LT);
+               b.add(line, JT, new Integer(7));
+               b.add(line, GET_PRESERVE);
+               b.add(line, LITERAL, varName);
+               b.add(line, LITERAL, varName);
+               b.add(line, DECLARE);
+               b.add(line, PUT);
+               parseStatement(b, null);
                    
-                   break;
+               b.add(line, OLDSCOPE);
                    
-               } else {
-                   if (hadVar) pushBackToken(VAR, null);
-                   b.add(line, NEWSCOPE);
+               break;
                    
-                   parseStatement(false, b);
-                   CompiledFunction e2 = new CompiledFunction(sourceName, line, null);
-                   startExpr(-1, e2);
-                   consume(SEMI);
-                   if (e2 == null) e2 = new CompiledFunction(sourceName, line, null).add(line, b.LITERAL, Boolean.TRUE);
+           } else {
+               if (hadVar) pushBackToken(VAR, null);
+               b.add(line, NEWSCOPE);
                    
-                   if (label != null) b.add(line, LABEL, label);
-                   b.add(line, LOOP);
-                   int size2 = b.size();
+               parseStatement(b, null);
+               CompiledFunction e2 = new CompiledFunction(sourceName, line, null);
+               if (peekToken() != SEMI) {
+                   startExpr(e2, -1);
+               } else {
+                   e2.add(line, b.LITERAL, Boolean.TRUE);
+               }
+               consume(SEMI);
+               if (label != null) b.add(line, LABEL, label);
+               b.add(line, LOOP);
+               int size2 = b.size();
                    
-                   b.add(line, JT, new Integer(0));
-                   int size = b.size();
-                   startExpr(-1, b);
-                   if (b.size() > size) b.add(line, POP);
-                   b.set(size - 1, new Integer(b.size() - size + 1));
-                   consume(RP);
+               b.add(line, JT, new Integer(0));
+               int size = b.size();
+               if (peekToken() != RP) {
+                   startExpr(b, -1);
+                   b.add(line, POP);
+               }
+               b.set(size - 1, new Integer(b.size() - size + 1));
+               consume(RP);
                    
-                   b.paste(e2);
-                   b.add(line, JT, new Integer(2));
-                   b.add(line, BREAK);
-                   parseStatement(false, b);
-                   b.add(line, CONTINUE);
-                   b.set(size2 - 1, new Integer(b.size() - size2 + 1));      // end of the loop
+               b.paste(e2);
+               b.add(line, JT, new Integer(2));
+               b.add(line, BREAK);
+               parseStatement(b, null);
+               b.add(line, CONTINUE);
+               b.set(size2 - 1, new Integer(b.size() - size2 + 1));      // end of the loop
                    
-                   b.add(line, OLDSCOPE);
-                   break;
-               }
+               b.add(line, OLDSCOPE);
+               break;
            }
+       }
                
-           case NAME: {
-               consume(NAME);
-               String possiblyTheLabel = string;
-               if (peekToken() == COLON) {
-                   consume(COLON);
-                   label = possiblyTheLabel;
-                   System.out.println("label == " + label);
-                   parseStatement(false, b, label);
-                   break;
-               } else {
-                   pushBackToken(NAME, possiblyTheLabel);
-                   // fall through to default case
-               }
-           }
-            // fall through
-           case RC:
-               if (tok == RC && braced) { consume(RC); return; }
-            // fall through
-           default: {
-               int size = b.size();
-               startExpr(-1, b);
-               if (size == b.size()) return;
+       case NAME: {
+           String possiblyTheLabel = string;
+           if (peekToken() == COLON) {
+               consume(COLON);
+               label = possiblyTheLabel;
+               parseStatement(b, label);
+               break;
+           } else {
+               pushBackToken(NAME, possiblyTheLabel);
+               startExpr(b, -1);
                b.add(line, POP);
-               if (peekToken() == SEMI) consume(SEMI);
+               if ((mostRecentlyReadToken != RC || peekToken() == SEMI) && peekToken() != -1) consume(SEMI);
                break;
            }
-           }
-           
-           if (!braced) return;
+       }
+
+       case SEMI: return;
+       case LC: {
+           pushBackToken();
+           parseBlock(b, label);
+           break;
+       }
+
+       default: {
+           pushBackToken();
+           startExpr(b, -1);
+           b.add(line, POP);
+           if ((mostRecentlyReadToken != RC || peekToken() == SEMI) && peekToken() != -1) consume(SEMI);
+           break;
+       }
        }
     }
+
+
+    // ParserException //////////////////////////////////////////////////////////////////////
     
-    private class ParserException extends RuntimeException {
-       public ParserException(String s) { super(sourceName + ":" + line + " " + s); }
-    }
+    private class ParserException extends IOException { public ParserException(String s) { super(sourceName + ":" + line + " " + s); } }
     
 }