2003/06/08 23:48:03
[org.ibex.core.git] / src / org / xwt / js / Parser.java
index 82da503..73b3590 100644 (file)
@@ -1,20 +1,27 @@
-// Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
+// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
 package org.xwt.js;
 
 import org.xwt.util.*;
 import java.io.*;
 
-/** parses a stream of lexed tokens into a tree of Expr's */
-public class Parser extends Lexer {
+/** Parses a stream of lexed tokens into a tree of ByteCodeBlock's */
+class Parser extends Lexer implements ByteCodes {
+
 
     // Constructors //////////////////////////////////////////////////////
 
-    public Parser(Reader r) throws IOException { super(r); }
+    public Parser(Reader r, String sourceName, int line) throws IOException {
+       super(r);
+       this.sourceName = sourceName;
+       this.line = line;
+    }
 
+    /** for debugging */
     public static void main(String[] s) throws Exception {
-       Parser p = new Parser(new InputStreamReader(System.in));
+       Parser p = new Parser(new InputStreamReader(System.in), "stdin", 0);
        while(true) {
-           Expr block = p.parseBlock(false);
+           ByteCodeBlock block = new ByteCodeBlock(0, "stdin");
+           p.parseStatement(false, block);
            if (block == null) return;
            System.out.println(block);
            if (p.peekToken() == -1) return;
@@ -25,329 +32,603 @@ public class Parser extends Lexer {
     // Statics ////////////////////////////////////////////////////////////
 
     static byte[] precedence = new byte[MAX_TOKEN + 1];
+    static boolean[] isRightAssociative = new boolean[MAX_TOKEN + 1];
     static {
-       precedence[COMMA] = 1;
-       precedence[ASSIGN] = 2;
-       precedence[GT] = precedence[GE] = 3;
+       isRightAssociative[ASSIGN] = true;
+
+       precedence[ASSIGN] = 1;
+       precedence[HOOK] = 2;
+       precedence[COMMA] = 3;
        precedence[OR] = precedence[AND] = 4;
-       precedence[BITOR] = 5;
-       precedence[BITXOR] = 6;
-       precedence[BITAND] = 7;
-       precedence[EQ] = precedence[NE] = 8;
-       precedence[LT] = precedence[LE] = 9;
-       precedence[SHEQ] = precedence[SHNE] = 10;
-       precedence[LSH] = precedence[RSH] = precedence[URSH] = 11;
-       precedence[ADD] = precedence[SUB] = 12;
-       precedence[MUL] = precedence[DIV] = precedence[MOD] = 13;
-       precedence[BITNOT] = precedence[INSTANCEOF] = 14;
-       precedence[INC] = precedence[DEC] = 15;
-       precedence[LP] = 16;
-       precedence[DOT] = 17;
+       precedence[GT] = precedence[GE] = 5;
+       precedence[BITOR] = 6;
+       precedence[BITXOR] = 7;
+       precedence[BITAND] = 8;
+       precedence[EQ] = precedence[NE] = 9;
+       precedence[LT] = precedence[LE] = 10;
+       precedence[SHEQ] = precedence[SHNE] = 11;
+       precedence[LSH] = precedence[RSH] = precedence[URSH] = 12;
+       precedence[ADD] = precedence[SUB] = 13;
+       precedence[MUL] = precedence[DIV] = precedence[MOD] = 14;
+       precedence[BITNOT] =  15;
+       precedence[INC] = precedence[DEC] = 16;
+       precedence[LP] = 17;
+       precedence[LB] = 18;
+       precedence[DOT] = 19;
     }
 
 
     // Parsing Logic /////////////////////////////////////////////////////////
 
-    /** a block is either a single statement or a list of statements surrounded by curly braces; all expressions are also statements */
-    public Expr parseBlock(boolean requireBraces) throws IOException {
-       Expr ret = null;
-       int tok = peekToken();
-       boolean braced = tok == LC;
-       if (requireBraces && !braced) throw new Error("expected {");
-       if (braced) getToken();
-       Expr head = null;
-       Expr tail = null;
-       OUTER: while(true) {
-           Expr smt;
-           switch(tok = peekToken()) {
-           case -1: break OUTER;
-           case LC: smt = parseBlock(true); break;
-           case THROW: case RETURN: case ASSERT:
-               getToken();
-               smt = new Expr(tok, parseMaximalExpr());
-               if (getToken() != SEMI) throw new Error("expected ;");
-               break;
-           case GOTO: case BREAK: case CONTINUE:
-               getToken();
-               if (getToken() == NAME)
-                   smt = new Expr(tok, new Expr(string));
-               else if (tok == GOTO)
-                   throw new Error("goto must be followed by a label");
-               else
-                   smt = new Expr(tok);
-               if (getToken() != SEMI) throw new Error("expected ;");
-               break;
+    private ByteCodeBlock newbb(int line) { return new ByteCodeBlock(line, sourceName); }
 
-           case RC:
-               if (braced) getToken();
-               break OUTER;
+    /** 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 ? "EOL" : codeToString[op]));
+    }
 
-           case SEMI:
+    /** append the largest expression beginning with prefix containing no operators of precedence below <tt>minPrecedence</tt> */
+    public ByteCodeBlock startExpr() throws IOException { return startExpr(-1); }
+    public void startExpr(ByteCodeBlock block) throws IOException { startExpr(-1, block); }
+    public ByteCodeBlock startExpr(int minPrecedence) throws IOException {
+       ByteCodeBlock ret = new ByteCodeBlock(line, sourceName);
+       startExpr(minPrecedence, ret);
+       return ret.size() == 0 ? null : ret;
+    }
+
+    public void startExpr(int minPrecedence, ByteCodeBlock appendTo) throws IOException {
+       int tok = getToken();
+       int curLine = line;
+       if (tok == -1) return;
+       if (minPrecedence > 0 && precedence[tok] != 0)
+           if (precedence[tok] < minPrecedence || (precedence[tok] == minPrecedence && !isRightAssociative[tok]))
+               { pushBackToken(); return; }
+
+       ByteCodeBlock b = appendTo;
+
+       switch (tok) {
+       case NUMBER: continueExpr(b.add(ByteCodeBlock.LITERAL, number), minPrecedence); return;
+       case STRING: continueExpr(b.add(ByteCodeBlock.LITERAL, string), minPrecedence); return;
+       case THIS: continueExpr(b.add(TOPSCOPE, null), minPrecedence); return;
+       case NULL: continueExpr(b.add(ByteCodeBlock.LITERAL, null), minPrecedence); return;
+       case TRUE: case FALSE: continueExpr(b.add(ByteCodeBlock.LITERAL, new Boolean(tok == TRUE)), minPrecedence); return;
+       case LB: {
+           b.add(ARRAY, new Integer(0));
+           int i = 0;
+           while(true) {
+               int size = b.size();
+               startExpr(b);
+               if (size == b.size())
+                   if (peekToken() == RB) { consume(RB); continueExpr(b, minPrecedence); return; }
+               b.add(LITERAL, new Integer(i++));
+               if (size == b.size()) b.add(LITERAL, null);
+               b.add(PUT);
+               b.add(POP);
+               if (peekToken() == RB) { consume(RB); continueExpr(b, minPrecedence); return; }
+               consume(COMMA);
+           }
+       }
+       case SUB: {
+           consume(NUMBER);
+           continueExpr(b.add(ByteCodeBlock.LITERAL, new Double(number.doubleValue() * -1)), minPrecedence);
+           return;
+       }
+       case LP: {
+           startExpr(b);
+           consume(RP);
+           continueExpr(b, minPrecedence);
+           return;
+       }
+       case INC: case DEC: {
+           // prefix
+           startExpr(precedence[tok], b);
+           b.set(b.size() - 1, tok, new Boolean(true));
+           continueExpr(b, minPrecedence);
+           return;
+       }
+       case BANG: case BITNOT: case TYPEOF: {
+           startExpr(precedence[tok], b);
+           b.add(tok);
+           continueExpr(b, minPrecedence);
+           return;
+       }
+       case LC: {
+           b.add(OBJECT, null);
+           if (peekToken() == RC) { consume(RC); continueExpr(b, minPrecedence); return; }
+           while(true) {
+               if (peekToken() != NAME && peekToken() != STRING) throw new Error("expected NAME or STRING");
                getToken();
-               if (!braced) break OUTER;
-               continue;
-
-           default:
-               smt = parseMaximalExpr();
-               if (smt == null) {
-                   if (head == null) throw new Error("empty statement list; next token is " + codeToString[peekToken()]);
-                   break OUTER;
-               }
-               break;
+               b.add(LITERAL, string);
+               consume(COLON);
+               startExpr(b);
+               b.add(PUT);
+               b.add(POP);
+               if (peekToken() == RC) { consume(RC); continueExpr(b, minPrecedence); return; }
+               consume(COMMA);
+               if (peekToken() == RC) { consume(RC); continueExpr(b, minPrecedence); return; }
            }
-           if (head == null) head = tail = smt; else tail = (tail.next = smt);
        }
-       return new Expr(LC, head);
-    }
-
-    /** throws an error if the next token is not <tt>code</tt> */
-    public void expect(int code) throws IOException {
-       int got = peekToken();
-       if (got != code)
-           throw new Error("expected " + codeToString[got] + ", got " + (got == -1 ? "EOL" : codeToString[got]));
-    }
+       case NAME: {
+           String name = string;
+           if (peekToken() == ASSIGN) {
+               consume(ASSIGN);
+               b.add(TOPSCOPE);
+               b.add(ByteCodeBlock.LITERAL, name);
+               startExpr(minPrecedence, b);
+               b.add(ByteCodeBlock.PUT);
+               b.add(ByteCodeBlock.SWAP);
+               b.add(ByteCodeBlock.POP);
+           } else {
+               b.add(TOPSCOPE);
+               b.add(ByteCodeBlock.LITERAL, name);
+               b.add(ByteCodeBlock.GET);
+           }
+           continueExpr(b, minPrecedence);
+           return;
+       }
+       case FUNCTION: {
+           consume(LP);
+           int numArgs = 0;
+           ByteCodeBlock b2 = newbb(curLine);
+           b2.add(TOPSCOPE);
+           b2.add(SWAP);
+           b2.add(LITERAL, "arguments");
+           b2.add(LITERAL, "arguments");
+           b2.add(DECLARE);
+           b2.add(SWAP);
+           b2.add(PUT);
+           b2.add(SWAP);
+           b2.add(POP);
+           if (peekToken() == RP) consume(RP);
+           else while(true) {
+               if (peekToken() == COMMA) {
+                   consume(COMMA);
 
-    /** parses the largest possible expression */
-    public Expr parseMaximalExpr() throws IOException { return parseMaximalExpr(null, -1); }
-    public Expr parseMaximalExpr(Expr prefix, int minPrecedence) throws IOException {
-       Expr save = null;
-       while(true) {
-           save = prefix;
-           if (peekToken() == -1) break;
-           prefix = parseSingleExpr(prefix, minPrecedence);
-           if (save == prefix) break;
-           if (prefix == null) throw new Error("parseSingleExpr_() returned null");
+               } else {
+                   consume(NAME);
+
+                   // declare the name
+                   b2.add(LITERAL, string);
+                   b2.add(DECLARE);
+
+                   // retrieve it from the arguments array
+                   b2.add(LITERAL, new Integer(numArgs));
+                   b2.add(GET_PRESERVE);
+                   b2.add(SWAP);
+                   b2.add(POP);
+
+                   // put it to the current scope
+                   b2.add(TOPSCOPE);
+                   b2.add(SWAP);
+                   b2.add(LITERAL, string);
+                   b2.add(SWAP);
+                   b2.add(PUT);
+
+                   // clean the stack
+                   b2.add(POP);
+                   b2.add(POP);
+
+                   if (peekToken() == RP) { consume(RP); break; }
+                   consume(COMMA);
+               }
+               numArgs++;
+           }
+           // pop off the arguments array
+           b2.add(POP);
+           parseStatement(true, b2);
+           b2.add(LITERAL, null);
+           b2.add(RETURN);
+           continueExpr(b.add(NEWFUNCTION, b2), minPrecedence);
+           return;
+       }
+       default: pushBackToken(); return;
        }
-       return prefix;
     }
 
-    /** parses the smallest possible complete expression */
-    public Expr parseSingleExpr() throws IOException { return parseSingleExpr(null, 0); }
+    public void continueExpr(ByteCodeBlock prefix, int minPrecedence) throws IOException {
+       int tok = getToken();
+       int curLine = line;
+       if (tok == -1) return;
+       if (minPrecedence > 0 && precedence[tok] != 0)
+           if (precedence[tok] < minPrecedence || (precedence[tok] == minPrecedence && !isRightAssociative[tok]))
+               { pushBackToken(); return; }
 
-    /** parses the smallest possible complete expression beginning with prefix and only using operators with at least minPrecedence */
-    public Expr parseSingleExpr(Expr prefix, int minPrecedence) throws IOException {
-       Expr e1 = null, e2 = null, e3 = null, head = null, tail = null, ret = null;
-
-       int tok = peekToken();
-       if (minPrecedence > 0 && tok < precedence.length && precedence[tok] != 0 && precedence[tok] <= minPrecedence) return prefix;
-       getToken();
+       if (prefix == null) throw new Error("got null prefix");
+       ByteCodeBlock b = prefix;
 
-       // these case arms match the precedence of operators; each arm is a precedence level.
        switch (tok) {
 
-       case WITH: throw new Error("XWT does not allow the WITH keyword");
-       case VOID: case RESERVED: throw new Error("reserved word that you shouldn't be using");
-       case NAME: if (prefix != null) { pushBackToken(); return prefix; } else return parseMaximalExpr(new Expr(NAME, string), minPrecedence);
-       case STRING: if (prefix != null) { pushBackToken(); return prefix; } else return new Expr(string);
-       case NUMBER: if (prefix != null) { pushBackToken(); return prefix; } else return new Expr(number);
-       case NULL: case TRUE: case FALSE: case NOP: if (prefix != null) { pushBackToken(); return prefix; } else return new Expr(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(tok - 1);
+           prefix.add(PUT);
+           prefix.add(SWAP);
+           prefix.add(POP);
+           continueExpr(b, minPrecedence);
+           return;
+       }
+
+       case INC: case DEC: {
+           // postfix
+           b.set(b.size() - 1, tok, new Boolean(false));
+           continueExpr(b, minPrecedence);
+           return;
+       }
 
-        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:
-           return new Expr(ASSIGN, prefix, new Expr(tok - 1, prefix, parseMaximalExpr(null, precedence[ASSIGN])));
+       case LP: {
+           // invocation
+           int i = 0;
+           while(peekToken() != RP) {
+               startExpr(b);
+               i++;
+               if (peekToken() == RP) break;
+               consume(COMMA);
+           }
+           consume(RP);
+           b.add(CALL, new Integer(i));
+           continueExpr(b, minPrecedence);
+           return;
+       }
 
         case BITOR: case BITXOR: case BITAND: case SHEQ: case SHNE: case LSH:
-       case RSH: case URSH: case ADD: case SUB: case MUL: case DIV: case MOD:
-       case COMMA: case ASSIGN: case GT: case GE: case OR: case AND:
-       case EQ: case NE: case LT: case LE: case DOT:
-           return new Expr(tok, prefix, parseMaximalExpr(null, precedence[tok]));
-
-       case BITNOT: case INSTANCEOF:
-           if (prefix != null) throw new Error("didn't expect non-null prefix!");
-           return new Expr(tok, parseMaximalExpr(null, precedence[tok]));
-
-       case INC: case DEC:
-           if (prefix == null) {
-               // prefix
-               return new Expr(tok, parseMaximalExpr(null, precedence[tok]));
+       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);
+           b.add(tok);
+           continueExpr(b, minPrecedence);
+           return;
+       }
+
+       case OR: case AND: {
+           b.add(tok == AND ? b.JF : b.JT, new Integer(0));
+           int size = b.size();
+           startExpr(precedence[tok], b);
+           b.set(size - 1, new Integer(b.size() - size + 2));
+           b.add(JMP, new Integer(2));
+           b.add(LITERAL, tok == AND ? new Boolean(false) : new Boolean(true));
+           continueExpr(b, minPrecedence);
+           return;
+       }
+
+       case DOT: {
+           consume(NAME);
+           String target = string;
+           if (peekToken() == ASSIGN) {
+               consume(ASSIGN);
+               b.add(LITERAL, target);
+               startExpr(b);
+               b.add(PUT);
+               b.add(SWAP);
+               b.add(POP);
            } else {
-               // postfix
-               return new Expr(tok, null, prefix);
+               b.add(LITERAL, target);
+               b.add(GET);
            }
+           continueExpr(b, minPrecedence);
+           return;
+       }
 
-       case LP:
-           if (prefix == null) {  // grouping
-               Expr r = parseMaximalExpr();
-               expect(RP); getToken();
-               return r;
-
-           } else {  // invocation
-               while(peekToken() != RP) {
-                   Expr e = parseMaximalExpr(null, precedence[COMMA]);
-                   if (head == null) head = tail = e; else tail = tail.next = e;
-                   tok = getToken();
-                   if (tok == RP) { pushBackToken(); break; }
-                   if (tok != COMMA) throw new Error("expected comma or right paren, got " + codeToString[tok]);
-               }
+       case LB: {
+           startExpr(b);
+           consume(RB);
+           if (peekToken() == ASSIGN) {
+               consume(ASSIGN);
+               startExpr(b);
+               b.add(PUT);
+               b.add(SWAP);
+               b.add(POP);
+           } else {
+               b.add(GET);
+           }
+           continueExpr(b, minPrecedence);
+           return;
+       }
+
+       case HOOK: {
+           b.add(JF, new Integer(0));
+           int size = b.size();
+           startExpr(b);
+           b.set(size - 1, new Integer(b.size() - size + 2));
+           b.add(JMP, new Integer(0));
+           consume(COLON);
+           size = b.size();
+           startExpr(b);
+           b.set(size - 1, new Integer(b.size() - size + 1));
+           continueExpr(b, minPrecedence);
+           return;
+       }
+           
+       default: { pushBackToken(); return; }
+       }
+    }
+    
+    /** a block is either a single statement or a list of statements surrounded by curly braces; all expressions are also statements */
+    public ByteCodeBlock parseStatement() throws IOException {
+       ByteCodeBlock ret = new ByteCodeBlock(line, sourceName);
+       ret.add(ret.LITERAL, null);
+       parseStatement(false, ret);
+       if (ret.size() == 1) return null;
+       return ret;
+    }
+
+    public void parseStatement(boolean requireBraces) throws IOException {
+       parseStatement(requireBraces, new ByteCodeBlock(line, sourceName));
+    }
+    public void parseStatement(boolean requireBraces, ByteCodeBlock b) throws IOException {
+       int tok = peekToken();
+       if (tok == -1) return;
+       boolean braced = tok == LC;
+       if (requireBraces && !braced) throw new ParserException("expected {, got " + codeToString[tok]);
+       if (braced) consume(LC);
+       int curLine = line;
+       while(true) {
+           switch(tok = peekToken()) {
+
+           case THROW: case RETURN: case ASSERT: {
                getToken();
-               return new Expr(LP, prefix, head);
+               if (tok == RETURN && peekToken() == SEMI) b.add(LITERAL, null);
+               else startExpr(b);
+               consume(SEMI);
+               b.add(tok);
+               break;
            }
 
-       case LB:
-           if (prefix != null) {
-               // subscripting
-               e1 = parseMaximalExpr();
-               if (getToken() != RB) throw new Error("expected a right brace");
-               return new Expr(LB, prefix, e1);
-           } else {
-               // array ctor
-               tok = getToken();
+           case BREAK: case CONTINUE: {
+               getToken();
+               if (peekToken() == NAME) consume(NAME);
+               b.add(tok, string);
+               consume(SEMI);
+               break;
+           }
+               
+           case SEMI:
+               consume(SEMI);
+               if (!braced) return;
+               break;
+               
+           case VAR: {
+               consume(VAR);
+               b.add(TOPSCOPE);                               // push the current scope
                while(true) {
-                   if (tok == RB) return new Expr(LB, prefix, head);
-                   if (head == null) head = tail = parseMaximalExpr(null, precedence[COMMA]);
-                   else tail = tail.next = parseMaximalExpr(null, precedence[COMMA]);
-                   tok = getToken();
-                   if (tok != COMMA && tok != RP) throw new Error("expected right bracket or comma");
+                   consume(NAME);
+                   String name = string;
+                   b.add(LITERAL, name);                // push the name to be declared
+                   b.add(DECLARE);                      // declare it
+                   if (peekToken() == ASSIGN) {           // if there is an '=' after the variable name
+                       b.add(LITERAL, name);            // put the var name back on the stack
+                       consume(ASSIGN);
+                       startExpr(b);
+                       b.add(PUT);
+                       b.add(POP);
+                   }
+                   if (peekToken() != COMMA) break;
+                   consume(COMMA);
                }
+               b.add(POP);
+               if (peekToken() == SEMI) consume(SEMI);
+               break;
            }
-           
-       case LC:
-           if (prefix != null) throw new Error("didn't expect non-null prefix");
-           tok = getToken();
-           while(true) {
-               if (tok == RC) return new Expr(LC, head);
-               if (tok != NAME) throw new Error("expecting name");
-               expect(NAME); getToken();
-               Expr name = new Expr(NAME, string);
-               if (tok != COLON) throw new Error("expecting colon");           
-               e1 = new Expr(COLON, name, parseMaximalExpr(null, precedence[COMMA]));
-               if (head == null) head = tail = e1; else tail = tail.next = e1;
-               tok = getToken();
-               if (tok != COMMA && tok != RP) throw new Error("expected right curly or comma");
+               
+           case IF: {
+               consume(IF);
+               consume(LP);
+               startExpr(b);
+               consume(RP);
+
+               b.add(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(JMP, new Integer(0));
+                   size = b.size();
+                   parseStatement(false, b);
+               }
+               b.set(size - 1, new Integer(1 + b.size() - size));
+               break;
            }
-           
-       case HOOK:
-           e2 = parseMaximalExpr();
-           if (getToken() != COLON) throw new Error("expected colon to close ?: expression");
-           e3 = parseMaximalExpr();
-           return new Expr(HOOK, prefix, new Expr(ELSE, e2, e3));
-           
-       case SWITCH: {
-           if (prefix != null) throw new Error("didn't expect non-null prefix");
-           if (getToken() != LP) throw new Error("expected left paren");
-           Expr switchExpr = parseMaximalExpr();
-           if (getToken() != RP) throw new Error("expected left paren");
-           if (getToken() != LC) throw new Error("expected left brace");
-           Expr firstExpr = null;
-           Expr lastExpr = null;
-           while(true) {
-               if (getToken() != CASE) throw new Error("expected CASE");
-               Expr caseExpr = parseMaximalExpr();
-               if (getToken() != COLON) throw new Error("expected COLON");
-               Expr e = new Expr(CASE, caseExpr, parseBlock(false));
-               if (lastExpr == null) firstExpr = e;
-               else lastExpr.next = e;
-               lastExpr = e;
-               if (getToken() == RC) return new Expr(SWITCH, switchExpr, firstExpr);
+
+           case WHILE: {
+               consume(WHILE);
+               consume(LP);
+               ByteCodeBlock loop = newbb(curLine);
+               b.add(LOOP, loop);
+               
+               loop.add(POP);
+               startExpr(loop);
+               loop.add(JT, new Integer(2));
+               loop.add(BREAK);
+               consume(RP);
+               parseStatement(false, loop);
+               
+               // if we fall out of the end, definately continue
+               loop.add(CONTINUE);
+               break;
            }
-       }
-           
-       case FUNCTION: {
-           if (prefix != null) throw new Error("didn't expect non-null prefix");
-           if (getToken() != LP) throw new Error("function keyword must be followed by a left paren");
-           Expr formalArgs = null, cur = null;
-           tok = getToken();
-           while(tok != RP) {
-               if (tok != NAME) throw new Error("expected a variable name");
-               if (cur == null) { formalArgs = cur = new Expr(string); }
-               else { cur.next = new Expr(NAME, string); cur = cur.next; }
-               tok = getToken();
-               if (tok == RP) break;
-               if (tok != COMMA) throw new Error("function argument list must consist of alternating NAMEs and COMMAs");
-               tok = getToken();
+
+           case SWITCH: {
+               consume(SWITCH);
+               consume(LP);
+               ByteCodeBlock loop = newbb(curLine);
+               b.add(LOOP, loop);
+               startExpr(loop);
+               consume(RP);
+               consume(LC);
+               while(true)
+                   if (peekToken() == CASE) {
+                       consume(CASE);
+                       loop.add(DUP);
+                       startExpr(loop);
+                       consume(COLON);
+                       loop.add(EQ);
+                       loop.add(JF, new Integer(0));
+                       int size = loop.size();
+                       while(peekToken() != CASE && peekToken() != DEFAULT && peekToken() != RC) {
+                           int size2 = loop.size();
+                           parseStatement(false, loop);
+                           if (size2 == loop.size()) break;
+                       }
+                       loop.set(size - 1, new Integer(1 + loop.size() - size));
+                   } else if (peekToken() == DEFAULT) {
+                       consume(DEFAULT);
+                       consume(COLON);
+                       while(peekToken() != CASE && peekToken() != DEFAULT && peekToken() != RC) {
+                           int size2 = loop.size();
+                           parseStatement(false, loop);
+                           if (size2 == loop.size()) break;
+                       }
+                   } else if (peekToken() == RC) {
+                       consume(RC);
+                       loop.add(BREAK);
+                       break;
+                   } else {
+                       throw new ParserException("expected CASE, DEFAULT, or RC; got " + codeToString[peekToken()]);
+                   }
+               break;
            }
-           return new Expr(FUNCTION, formalArgs, parseBlock(true));
-       }
-           
-       case VAR:
-           if (prefix != null) throw new Error("didn't expect non-null prefix");
-           while(true) {
-               if (getToken() != NAME) throw new Error("variable declarations must start with a variable name");
-               Expr name = new Expr(NAME, string);
-               Expr initVal = null;
-               tok = peekToken();
-               Expr e = null;
-               if (tok == ASSIGN) {
+               
+           case DO: {
+               consume(DO);
+               ByteCodeBlock loop = newbb(curLine);
+               b.add(LOOP, loop);
+               
+               parseStatement(false, loop);
+               consume(WHILE);
+               consume(LP);
+               startExpr(loop);
+               loop.add(JT, new Integer(2));
+               loop.add(BREAK);
+               loop.add(CONTINUE);
+               consume(RP);
+               consume(SEMI);
+               break;
+           }
+               
+           case TRY: {
+               // We deliberately allow you to omit braces in catch{}/finally{} if they are single statements...
+               consume(TRY);
+               parseStatement(true, b);
+               
+               if (peekToken() == CATCH) {
                    getToken();
-                   initVal = parseMaximalExpr(null, precedence[COMMA]);
-                   tok = peekToken();
-                   e = new Expr(ASSIGN, name, initVal);
-               } else {
-                   e = new Expr(NAME, name);
+                   consume(LP);
+                   consume(NAME);
+                   consume(RP);
+                   parseStatement();   // just discard the catchblock
                }
-               if (head == null) head = tail = e; else tail = tail.next = e;
-               if (tok != COMMA) break;
-               getToken();
-           }
-           return new Expr(VAR, head);
 
-       case TRY: {
-           // We deliberately allow you to omit braces in catch{}/finally{} if they are single statements...
-           if (prefix != null) throw new Error("didn't expect non-null prefix");
-           Expr tryBlock = parseBlock(true);
-           while ((tok = peekToken()) == CATCH || tok == FINALLY) {
-               getToken();
-               if (getToken() != LP) throw new Error("expected (");
-               if (getToken() != NAME) throw new Error("expected name");
-               Expr name = new Expr(NAME, string);
-               if (getToken() != RP) throw new Error("expected )");
-               e1 = new Expr(tok, name, parseBlock(false));
-               if (head == null) head = tail = e1; else tail = tail.next = e1;
+               if (peekToken() == FINALLY) {
+                   consume(FINALLY);
+                   parseStatement(false, b);
+               }
+               break;
            }
-           if (head == null) throw new Error("try without catch or finally");
-           return new Expr(TRY, tryBlock, head);
-       }
 
-       case IF: case WHILE: {
-           if (prefix != null) throw new Error("didn't expect non-null prefix");
-           if (getToken() != LP) throw new Error("expected left paren");
-           Expr parenExpr = parseMaximalExpr();
-           int t;
-           if ((t = getToken()) != RP) throw new Error("expected right paren, but got " + codeToString[t]);
-           Expr firstBlock = parseBlock(false);
-           if (tok == IF && peekToken() == ELSE) {
-               getToken();
-               return new Expr(tok, parenExpr, new Expr(ELSE, firstBlock, parseBlock(false)));
-           }
-           return new Expr(tok, parenExpr, firstBlock);
-       }
+       case FOR: {
+           consume(FOR);
+           consume(LP);
 
-       case IN: return prefix;
-       case FOR:
-           if (prefix != null) throw new Error("didn't expect non-null prefix");
-           if (getToken() != LP) throw new Error("expected left paren");
-           e1 = parseMaximalExpr(null, -1);
-           if (e1.code == NAME && peekToken() == IN) {
-               getToken();
-               e2 = parseMaximalExpr(null, -1);
-               if (getToken() != RP) throw new Error("expected right paren");
-               return new Expr(FOR, new Expr(IN, e1, e2), parseBlock(false));
+           tok = getToken();
+           if (tok == VAR) tok = getToken();
+           String varName = string;
+           boolean forIn = peekToken() == IN;
+           pushBackToken(tok, varName);
+
+           if (forIn) {
+               consume(NAME);
+               consume(IN);
+               startExpr(b);
+               b.add(PUSHKEYS);
+               b.add(LITERAL, "length");
+               b.add(GET);
+               consume(RP);
+               ByteCodeBlock b2 = newbb(curLine);
+               b.add(SCOPE, b2);
+               b2.add(LITERAL, new Integer(1));
+               b2.add(SUB);
+               b2.add(DUP);
+               b2.add(LITERAL, new Integer(0));
+               b2.add(LT);
+               b2.add(JT, new Integer(7));
+               b2.add(GET_PRESERVE);
+               b2.add(LITERAL, varName);
+               b2.add(LITERAL, varName);
+               b2.add(DECLARE);
+               b2.add(PUT);
+               parseStatement(false, b2);
+               break;
                
            } else {
-               if (getToken() != SEMI) throw new Error("expected ;");
-               e2 = parseMaximalExpr(null, -1);
-               if (getToken() != SEMI) throw new Error("expected ;");
-               e3 = parseMaximalExpr(null, -1);
-               if (getToken() != RP) throw new Error("expected right paren");
-               return new Expr(LC, e1, new Expr(WHILE, e2, new Expr(LC, parseBlock(false), e3)));
+               ByteCodeBlock b2 = newbb(curLine);
+               b.add(SCOPE, b2);
+               b.add(POP);
+
+               int size = b2.size();
+               startExpr(b2);
+               if (b2.size() - size > 0) b2.add(POP);
+               consume(SEMI);
+               ByteCodeBlock e2 = startExpr();
+               consume(SEMI);
+               if (e2 == null) e2 = newbb(curLine).add(b.LITERAL, null);
+
+               ByteCodeBlock b3 = newbb(curLine);
+               b2.add(LOOP, b3);
+               b2.add(LITERAL, null);
+
+               b3.add(JT, new Integer(0));
+               size = b3.size();
+               startExpr(b3);
+               consume(RP);
+               if (b3.size() - size > 0) b3.add(POP);
+               b3.set(size - 1, new Integer(b3.size() - size + 1));
+
+               b3.paste(e2);
+               b3.add(JT, new Integer(2));
+               b3.add(BREAK);
+               parseStatement(false, b3);
+               b3.add(BREAK);
+               break;
            }
-           
-       case DO: {
-           if (prefix != null) throw new Error("didn't expect non-null prefix");
-           Expr firstBlock = parseBlock(false);
-           if (getToken() != WHILE) throw new Error("expecting WHILE");
-           if (getToken() != LP) throw new Error("expected left paren");
-           Expr whileExpr = parseMaximalExpr();
-           if (getToken() != RP) throw new Error("expected right paren");
-           if (getToken() != SEMI) throw new Error("semicolon");
-           return new Expr(DO, firstBlock, whileExpr);
        }
            
-       default:
-           pushBackToken();
-           return prefix;
+           case NAME: {
+               consume(NAME);
+               String name = string;
+               if (peekToken() == COLON) {
+                   consume(COLON);
+                   b.add(ByteCodeBlock.LABEL, string);
+                   break;
+               } else {
+                   pushBackToken(NAME, name);
+                   // fall through to default case
+               }
+           }
+            // fall through
+           case RC:
+               if (tok == RC && braced) { consume(RC); return; }
+            // fall through
+           default: {
+               int size = b.size();
+               startExpr(b);
+               if (size == b.size()) return;
+               b.add(POP);
+               if (peekToken() == SEMI) consume(SEMI);
+               break;
+           }
+           }
+           
+           if (!braced) return;
        }
     }
     
+    class ParserException extends RuntimeException {
+       public ParserException(String s) { super(sourceName + ":" + line + " " + s); }
+    }
+    
 }