2003/06/09 06:38:36
[org.ibex.core.git] / src / org / xwt / js / Parser.java
index ced2039..f594eee 100644 (file)
@@ -1,25 +1,23 @@
-// 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 ForthBlock's */
-public class Parser extends Lexer implements OpCodes {
+/** Parses a stream of lexed tokens into a tree of ByteCodeBlock's */
+class Parser extends Lexer implements ByteCodes {
+
 
     // Constructors //////////////////////////////////////////////////////
 
-    public Parser(Reader r, String sourceName, int line) throws IOException {
-       super(r);
-       this.sourceName = sourceName;
-       this.line = line;
-    }
+    public Parser(Reader r, String sourceName, int line) throws IOException { super(r, sourceName, line); }
 
     /** for debugging */
     public static void main(String[] s) throws Exception {
        Parser p = new Parser(new InputStreamReader(System.in), "stdin", 0);
        while(true) {
-           ForthBlock block = p.parseStatement(false);
+           ByteCodeBlock block = new ByteCodeBlock(0, "stdin");
+           p.parseStatement(false, block);
            if (block == null) return;
            System.out.println(block);
            if (p.peekToken() == -1) return;
@@ -32,8 +30,9 @@ public class Parser extends Lexer implements OpCodes {
     static byte[] precedence = new byte[MAX_TOKEN + 1];
     static boolean[] isRightAssociative = new boolean[MAX_TOKEN + 1];
     static {
-       precedence[ASSIGN] = 1;
        isRightAssociative[ASSIGN] = true;
+
+       precedence[ASSIGN] = 1;
        precedence[HOOK] = 2;
        precedence[COMMA] = 3;
        precedence[OR] = precedence[AND] = 4;
@@ -47,7 +46,7 @@ public class Parser extends Lexer implements OpCodes {
        precedence[LSH] = precedence[RSH] = precedence[URSH] = 12;
        precedence[ADD] = precedence[SUB] = 13;
        precedence[MUL] = precedence[DIV] = precedence[MOD] = 14;
-       precedence[BITNOT] = precedence[INSTANCEOF] = 15;
+       precedence[BITNOT] =  15;
        precedence[INC] = precedence[DEC] = 16;
        precedence[LP] = 17;
        precedence[LB] = 18;
@@ -57,324 +56,154 @@ public class Parser extends Lexer implements OpCodes {
 
     // Parsing Logic /////////////////////////////////////////////////////////
 
+    private ByteCodeBlock newbb(int line) { return new ByteCodeBlock(line, sourceName); }
+
+    /** 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]));
+           throw new ParserException("expected " + codeToString[code] + ", got " + (op == -1 ? "EOF" : codeToString[op]));
     }
 
-    /** parses the largest possible expression */
-    public ForthBlock parseExpr() throws IOException { return parseExpr(null, -1); }
-    public ForthBlock parseExpr(ForthBlock prefix, int minPrecedence) throws IOException {
-       ForthBlock e1 = null, e2 = null, e3 = null, head = null, tail = null, ret = null;
-
-       int tok = peekToken();
-       if (minPrecedence > 0 && tok < precedence.length && precedence[tok] != 0 &&
-           (isRightAssociative[tok] ? (precedence[tok] < minPrecedence) : (precedence[tok] <= minPrecedence)))
-           return prefix;
+    /** 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;
+    }
 
-       getToken();
+    public void startExpr(int minPrecedence, ByteCodeBlock appendTo) throws IOException {
+       int tok = getToken();
        int curLine = line;
+       if (tok == -1) return;
 
-       switch (tok) {
+       ByteCodeBlock b = appendTo;
 
-       case VAR: {
-           if (prefix != null) { pushBackToken(); return prefix; }
-           ForthBlock b = new ForthBlock(curLine, sourceName);
-           b.add(OpCodes.THIS);
+       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) {
-               consume(NAME);
-               String name = string;
-               b.add(b.LITERAL, name);
-               b.add(b.DECLARE);
-               if (peekToken() == ASSIGN) {
-                   b.add(b.LITERAL, name);
-                   consume(ASSIGN);
-                   b.add(b.EXPR, parseExpr());
-                   b.add(b.PUT);
-                   b.add(b.POP);
-               }
-               if (peekToken() != COMMA) break;
+               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);
            }
-           return parseExpr(b, minPrecedence);
        }
-
-       case IN: pushBackToken(); return prefix;
-
-       case IF: {
-           if (prefix != null) { pushBackToken(); return prefix; }
-           ForthBlock b = new ForthBlock(curLine, sourceName);
-           consume(LP);
-           b.add(b.EXPR, parseExpr());
+       case SUB: {
+           consume(NUMBER);
+           b.add(ByteCodeBlock.LITERAL, new Double(number.doubleValue() * -1));
+           continueExpr(b, minPrecedence);
+           return;
+       }
+       case LP: {
+           startExpr(b);
            consume(RP);
-           b.add(b.JF, new Integer(3));
-           b.add(b.EXPR, parseStatement(false));
-           b.add(b.JMP, new Integer(2));
-           if (peekToken() != ELSE) return parseExpr(b.add(b.LITERAL, null), minPrecedence);
-           consume(ELSE);
-           b.add(b.EXPR, parseStatement(false));
-           return parseExpr(b, minPrecedence);
+           continueExpr(b, minPrecedence);
+           return;
        }
-
-           // FIXME: ugly hack!!
-        case ASSIGN_BITOR: if (tok == ASSIGN_BITOR) tok = BITOR;
-       case ASSIGN_BITXOR: if (tok == ASSIGN_BITXOR) tok = BITXOR;
-       case ASSIGN_BITAND: if (tok == ASSIGN_BITAND) tok = BITAND;
-       case ASSIGN_LSH: if (tok == ASSIGN_LSH) tok = LSH;
-       case ASSIGN_RSH: if (tok == ASSIGN_RSH) tok = RSH;
-       case ASSIGN_URSH: if (tok == ASSIGN_URSH) tok = URSH;
-       case ASSIGN_ADD: if (tok == ASSIGN_ADD) tok = ADD;
-       case ASSIGN_SUB: if (tok == ASSIGN_SUB) tok = SUB;
-       case ASSIGN_MUL: if (tok == ASSIGN_MUL) tok = MUL;
-       case ASSIGN_DIV: if (tok == ASSIGN_DIV) tok = DIV;
-       case ASSIGN_MOD: if (tok == ASSIGN_MOD) tok = MOD;
-           {
-               ForthBlock b = (ForthBlock)prefix;
-               b.set(b.size() - 1, b.GET_PRESERVE, new Boolean(true));
-               b.add(b.EXPR, parseExpr(null, precedence[tok]));
-               b.add(tok);
-               b.add(b.PUT);
-               b.add(b.SWAP);
-               b.add(b.POP);
-               return parseExpr(b, minPrecedence);
-           }
-
-       case INC: case DEC:
-           if (prefix == null) {
-               // prefix
-               ForthBlock b = (ForthBlock)parseExpr(null, precedence[tok]);
-               b.set(b.size() - 1, tok, new Boolean(true));
-               return parseExpr(b, minPrecedence);
-           } else {
-               // postfix
-               ForthBlock b = (ForthBlock)prefix;
-               b.set(b.size() - 1, tok, new Boolean(false));
-               return parseExpr(b, minPrecedence);
-           }
-
-       case LP:
-           if (prefix == null) {  // grouping
-               ForthBlock b = new ForthBlock(curLine, sourceName, ForthBlock.EXPR, parseExpr());
-               consume(RP);
-               return parseExpr(b, minPrecedence);
-
-           } else {  // invocation
-               ForthBlock b = new ForthBlock(curLine, sourceName);
-               int i = 0;
-               b.add(b.EXPR, prefix);
-               while(peekToken() != RP) {
-                   b.add(b.EXPR, parseExpr());
-                   i++;
-                   if (peekToken() == RP) break;
-                   consume(COMMA);
-               }
-               consume(RP);
-               b.add(b.CALL, new Integer(i));
-               return parseExpr(b, minPrecedence);
-           }
-
-       case BANG: case BITNOT: case INSTANCEOF: case TYPEOF: {
-           if (prefix != null) { pushBackToken(); return prefix; }
-           ForthBlock b = new ForthBlock(curLine, sourceName);
-           b.add(b.EXPR, parseExpr(null, precedence[tok]));
-           b.add(tok);
-           return parseExpr(b, minPrecedence);
+       case INC: case DEC: {
+           // prefix
+           startExpr(precedence[tok], b);
+           b.set(b.size() - 1, tok, new Boolean(true));
+           continueExpr(b, minPrecedence);
+           return;
        }
-
-       case SUB:
-           if (prefix == null && peekToken() == NUMBER) {
-               getToken();
-               return parseExpr(new ForthBlock(curLine, sourceName, ForthBlock.LITERAL, new Double(number.doubleValue() * -1)), minPrecedence);
-           } // else fall through
-        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: {
-           if (prefix == null) throw new ParserException("the " + codeToString[tok] + " token cannot start an expression");
-           ForthBlock b = new ForthBlock(curLine, sourceName);
-           b.add(b.EXPR, prefix);
-           b.add(b.EXPR, parseExpr(null, precedence[tok]));
+       case BANG: case BITNOT: case TYPEOF: {
+           startExpr(precedence[tok], b);
            b.add(tok);
-           return parseExpr(b, minPrecedence);
+           continueExpr(b, minPrecedence);
+           return;
        }
-
-           // includes short-circuit logic
-       case OR: case AND: {
-           if (prefix == null) throw new ParserException("the " + codeToString[tok] + " token cannot start an expression");
-           ForthBlock b = new ForthBlock(curLine, sourceName);
-           b.add(b.LITERAL, tok == AND ? new Boolean(false) : new Boolean(true));
-           b.add(b.EXPR, prefix);
-           b.add(tok == AND ? b.JF : b.JT, new Integer(3));
-           b.add(b.POP);
-           b.add(b.EXPR, parseExpr(null, precedence[tok]));
-           return parseExpr(b, minPrecedence);
-       }
-
-       case WITH: throw new ParserException("XWT does not allow the WITH keyword");
-       case VOID: case RESERVED: throw new ParserException("reserved word that you shouldn't be using");
-
-       case NUMBER:
-           if (prefix != null) { pushBackToken(); return prefix; }
-           return parseExpr(new ForthBlock(curLine, sourceName, ForthBlock.LITERAL, number), minPrecedence);
-
-       case STRING:
-           if (prefix != null) { pushBackToken(); return prefix; }
-           return parseExpr(new ForthBlock(curLine, sourceName, ForthBlock.LITERAL, string), minPrecedence);
-
-       case NULL: case TRUE: case FALSE: case NOP:
-           if (prefix != null) { pushBackToken(); return prefix; }
-           return parseExpr(new ForthBlock(curLine, sourceName, ForthBlock.LITERAL, (tok == NULL || tok == NOP) ? null : new Boolean(tok == TRUE)), minPrecedence);
-
-       case COMMA: pushBackToken(); return prefix;
-
-       case Tokens.THIS:
-           if (prefix != null) { pushBackToken(); return prefix; } 
-           return parseExpr(new ForthBlock(curLine, sourceName, OpCodes.THIS, null), minPrecedence);
-
-       case NAME: {
-           if (prefix != null) { pushBackToken(); return prefix; }
-           String name = string;
-           ForthBlock b = new ForthBlock(curLine, sourceName);
-           if (peekToken() == ASSIGN) {
-               consume(ASSIGN);
-               b.add(OpCodes.THIS);
-               b.add(ForthBlock.LITERAL, name);
-               b.add(ForthBlock.EXPR, parseExpr(null, minPrecedence));
-               b.add(ForthBlock.PUT);
-               b.add(ForthBlock.SWAP);
-               b.add(ForthBlock.POP);
-               return parseExpr(b, minPrecedence);
-           } else {
-               b.add(OpCodes.THIS);
-               b.add(ForthBlock.LITERAL, name);
-               b.add(ForthBlock.GET);
-               return parseExpr(parseExpr(b, minPrecedence), minPrecedence);
-           }
-       }
-
-       case DOT: {
-           consume(NAME);
-           String target = string;
-           ForthBlock b = new ForthBlock(curLine, sourceName);
-           b.add(b.EXPR, prefix);
-           if (peekToken() == ASSIGN) {
-               consume(ASSIGN);
-               ForthBlock val = parseExpr();
-               b.add(b.LITERAL, target);
-               b.add(b.EXPR, val);
-               b.add(b.PUT);
-               b.add(b.SWAP);
-               b.add(b.POP);
-           } else {
-               b.add(b.LITERAL, target);
-               b.add(b.GET);
-           }
-           return parseExpr(b, minPrecedence);
-       }
-
-       case LB: {
-           ForthBlock b = new ForthBlock(curLine, sourceName);
-           if (prefix == null) {
-               b.add(b.ARRAY, new Integer(0));
-               int i = 0;
-               while(true) {
-                   ForthBlock e = parseExpr();
-                   if (e == null && peekToken() == RB) { consume(RB); return parseExpr(b, minPrecedence); }
-                   b.add(b.LITERAL, new Integer(i++));
-                   if (e == null) b.add(b.LITERAL, null);
-                   else b.add(b.EXPR, e);
-                   b.add(b.PUT);
-                   b.add(b.POP);
-                   if (peekToken() == RB) { consume(RB); return parseExpr(b, minPrecedence); }
-                   consume(COMMA);
-               }
-           } else {
-               b.add(b.EXPR, prefix);
-               b.add(b.EXPR, parseExpr());
-               consume(RB);
-               if (peekToken() == ASSIGN) {
-                   consume(ASSIGN);
-                   b.add(b.EXPR, parseExpr());
-                   b.add(b.PUT);
-                   b.add(b.SWAP);
-                   b.add(b.POP);
-               } else {
-                   b.add(b.GET);
-               }
-               return parseExpr(b, minPrecedence);
-           }
-       }
-
        case LC: {
-           if (prefix != null) { pushBackToken(); return prefix; }
-           ForthBlock b = new ForthBlock(curLine, sourceName);
-           b.add(b.OBJECT, null);
-           if (peekToken() == RC) { consume(RC); return parseExpr(b, minPrecedence); }
-           while(true) {
+           b.add(OBJECT, null);
+           if (peekToken() != RC) while(true) {
                if (peekToken() != NAME && peekToken() != STRING) throw new Error("expected NAME or STRING");
                getToken();
-               b.add(b.LITERAL, string);
+               b.add(LITERAL, string);
                consume(COLON);
-               b.add(b.EXPR, parseExpr());
-               b.add(b.PUT);
-               b.add(b.POP);
-               if (peekToken() == RC) { consume(RC); return parseExpr(b, minPrecedence); }
+               startExpr(b);
+               b.add(PUT);
+               b.add(POP);
+               if (peekToken() == RC) break;
                consume(COMMA);
-               if (peekToken() == RC) { consume(RC); return parseExpr(b, minPrecedence); }
+               if (peekToken() == RC) break;
            }
+           consume(RC);
+           continueExpr(b, minPrecedence);
+           return;
        }
-           
-       case HOOK: {
-           ForthBlock b = new ForthBlock(curLine, sourceName);
-           b.add(b.EXPR, prefix);
-           b.add(b.JF, new Integer(3));
-           b.add(b.EXPR, parseExpr());
-           b.add(b.JMP, new Integer(2));
-           consume(COLON);
-           b.add(b.EXPR, parseExpr());
-           return parseExpr(b, minPrecedence);
+       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 Tokens.FUNCTION: {
-           if (prefix != null) { pushBackToken(); return prefix; }
+       case FUNCTION: {
            consume(LP);
-           ForthBlock b = new ForthBlock(curLine, sourceName);
            int numArgs = 0;
-           b.add(OpCodes.THIS);
-           b.add(b.SWAP);
-           b.add(b.LITERAL, "arguments");
-           b.add(b.LITERAL, "arguments");
-           b.add(b.DECLARE);
-           b.add(b.SWAP);
-           b.add(b.PUT);
-           b.add(b.SWAP);
-           b.add(b.POP);
+           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);
+
                } else {
                    consume(NAME);
 
                    // declare the name
-                   b.add(b.LITERAL, string);
-                   b.add(b.DECLARE);
+                   b2.add(LITERAL, string);
+                   b2.add(DECLARE);
 
                    // retrieve it from the arguments array
-                   b.add(b.LITERAL, new Integer(numArgs));
-                   b.add(b.GET_PRESERVE);
-                   b.add(b.SWAP);
-                   b.add(b.POP);
+                   b2.add(LITERAL, new Integer(numArgs));
+                   b2.add(GET_PRESERVE);
+                   b2.add(SWAP);
+                   b2.add(POP);
 
                    // put it to the current scope
-                   b.add(OpCodes.THIS);
-                   b.add(b.SWAP);
-                   b.add(b.LITERAL, string);
-                   b.add(b.SWAP);
-                   b.add(b.PUT);
+                   b2.add(TOPSCOPE);
+                   b2.add(SWAP);
+                   b2.add(LITERAL, string);
+                   b2.add(SWAP);
+                   b2.add(PUT);
 
                    // clean the stack
-                   b.add(b.POP);
-                   b.add(b.POP);
+                   b2.add(POP);
+                   b2.add(POP);
 
                    if (peekToken() == RP) { consume(RP); break; }
                    consume(COMMA);
@@ -382,248 +211,414 @@ public class Parser extends Lexer implements OpCodes {
                numArgs++;
            }
            // pop off the arguments array
-           b.add(b.POP);
-           parseStatement(true, b);
-           return parseExpr(new ForthBlock(curLine, sourceName, OpCodes.FUNCTION, b), minPrecedence);
+           b2.add(POP);
+           parseStatement(true, b2);
+           b2.add(LITERAL, null);
+           b2.add(RETURN);
+           continueExpr(b.add(NEWFUNCTION, b2), minPrecedence);
+           return;
        }
-           
-       case WHILE: {
-           if (prefix != null) { pushBackToken(); return prefix; }
-           consume(LP);
-           ForthBlock r = new ForthBlock(curLine, sourceName);
-           ForthBlock loop = new ForthBlock(curLine, sourceName);
-           r.add(loop.LOOP, loop);
-           r.add(r.LITERAL, null);
-
-           loop.add(loop.EXPR, parseExpr());
-           loop.add(loop.JT, new Integer(2));
-           loop.add(Lexer.BREAK);
-           consume(RP);
-           parseStatement(false, loop);
-
-           // if we fall out of the end, definately continue
-           loop.add(CONTINUE);
-           return parseExpr(r, minPrecedence);
+       default: pushBackToken(); return;
        }
+    }
 
-       case SWITCH: {
-           if (prefix != null) { pushBackToken(); return prefix; }
-           consume(LP);
-           ForthBlock r = new ForthBlock(curLine, sourceName);
-           ForthBlock loop = new ForthBlock(curLine, sourceName);
-           r.add(loop.LOOP, loop);
-           r.add(r.LITERAL, null);
-           loop.add(loop.EXPR, parseExpr());
-           consume(RP);
-           consume(LC);
-           while(true) {
-               ForthBlock caseForthBlock;
-               tok = getToken();
-               if (tok == CASE) {
-                   loop.add(loop.DUP);
-                   loop.add(loop.EXPR, parseExpr());
-                   loop.add(EQ);
-                   loop.add(loop.JF, new Integer(2));
-               } else if (tok != DEFAULT) throw new ParserException("expected CASE or DEFAULT");
-               consume(COLON);
-               ForthBlock b = new ForthBlock(curLine, sourceName);
-               while(peekToken() != CASE && peekToken() != DEFAULT && peekToken() != RC) {
-                   if ((e1 = parseStatement(false)) == null) break;
-                   b.add(b.EXPR, e1);
-               }
-               loop.add(loop.EXPR, b);
-               if (peekToken() == RC) {
-                   consume(RC);
-                   r.add(BREAK);
-                   return parseExpr(r, minPrecedence);
-               }
-           }
+    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; }
+
+       if (prefix == null) throw new Error("got null prefix");
+       ByteCodeBlock b = prefix;
+
+       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(tok - 1);
+           prefix.add(PUT);
+           prefix.add(SWAP);
+           prefix.add(POP);
+           continueExpr(b, minPrecedence);
+           return;
        }
-           
-       case DO: {
-           if (prefix != null) { pushBackToken(); return prefix; }
-           ForthBlock r = new ForthBlock(curLine, sourceName);
-           ForthBlock loop = new ForthBlock(curLine, sourceName);
-           r.add(loop.LOOP, loop);
-           r.add(r.LITERAL, null);
-
-           parseStatement(false, loop);
-           consume(WHILE);
-           consume(LP);
-           loop.add(loop.EXPR, parseExpr());
-           loop.add(loop.JT, new Integer(2));
-           loop.add(Lexer.BREAK);
-           loop.add(Lexer.CONTINUE);
-           consume(RP);
-           consume(SEMI);
-           return parseExpr(r, minPrecedence);
+
+       case INC: case DEC: {
+           // postfix
+           b.set(b.size() - 1, tok, new Boolean(false));
+           continueExpr(b, minPrecedence);
+           return;
        }
 
-       case TRY: {
-           // FIXME: don't just ignore this!
-           // We deliberately allow you to omit braces in catch{}/finally{} if they are single statements...
-           if (prefix != null) { pushBackToken(); return prefix; }
-           ForthBlock tryBlock = parseStatement(true);
-           
-           tok = peekToken();
-           if (tok == CATCH) {
-               getToken();
-               if (getToken() != LP) throw new ParserException("expected (");
-               if (getToken() != NAME) throw new ParserException("expected name");
-               if (getToken() != RP) throw new ParserException("expected )");
-               tok = peekToken();
+       case LP: {
+           // invocation
+           int i = 0;
+           while(peekToken() != RP) {
+               i++;
+               startExpr(b);
+               if (peekToken() == RP) break;
+               consume(COMMA);
            }
-           if (tok == FINALLY) getToken();
-
-           return parseExpr(tryBlock, minPrecedence);
+           consume(RP);
+           b.add(CALL, new Integer(i));
+           continueExpr(b, minPrecedence);
+           return;
        }
 
-       case FOR: {
-           if (prefix != null) { pushBackToken(); return prefix; }
-           if (getToken() != LP) throw new ParserException("expected left paren");
+        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);
+           b.add(tok);
+           continueExpr(b, minPrecedence);
+           return;
+       }
 
-           tok = getToken();
-           if (tok == VAR) tok = getToken();
-           String varName = string;
-           boolean forIn = peekToken() == IN;
-           pushBackToken(tok, varName);
+       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;
+       }
 
-           ForthBlock b = new ForthBlock(curLine, sourceName);
-           if (forIn) {
-               consume(NAME);
-               consume(IN);
-               b.add(b.EXPR, parseExpr());
-               b.add(b.PUSHKEYS);
-               b.add(b.LITERAL, "length");
-               b.add(b.GET);
-               consume(RP);
-               ForthBlock b2 = new ForthBlock(curLine, sourceName);
-               b.add(b.SCOPE, b2);
-               b2.add(b.LITERAL, new Integer(1));
-               b2.add(SUB);
-               b2.add(b.DUP);
-               b2.add(b.LITERAL, new Integer(0));
-               b2.add(LT);
-               b2.add(b.JT, new Integer(7));
-               b2.add(b.GET_PRESERVE);
-               b2.add(b.LITERAL, varName);
-               b2.add(b.LITERAL, varName);
-               b2.add(b.DECLARE);
-               b2.add(b.PUT);
-               b2.add(b.EXPR, parseStatement(false));
-               b2.add(b.LITERAL, null);
-               return parseExpr(b, minPrecedence);
-               
+       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 {
-               ForthBlock b2 = new ForthBlock(curLine, sourceName);
-               b.add(b.SCOPE, b2);
-
-               e1 = parseExpr();
-               if (e1 == null) e1 = new ForthBlock(curLine, sourceName, b.LITERAL, null);
-
-               b2.add(b.EXPR, e1);
-               b2.add(b.POP);
-               consume(SEMI);
-               e2 = parseExpr();
-               consume(SEMI);
-               e3 = parseExpr();
-               consume(RP);
+               b.add(LITERAL, target);
+               b.add(GET);
+           }
+           continueExpr(b, minPrecedence);
+           return;
+       }
 
-               if (e2 == null) e2 = new ForthBlock(curLine, sourceName, b.LITERAL, null);
-               if (e3 == null) e3 = new ForthBlock(curLine, sourceName, b.LITERAL, null);
-
-               ForthBlock b3 = new ForthBlock(curLine, sourceName);
-               b2.add(b.LOOP, b3);
-               b2.add(b.LITERAL, null);
-               b3.add(b.JT, new Integer(3));
-               b3.add(b.EXPR, e3);
-               b3.add(b.POP);
-               b3.add(b.EXPR, e2);
-               b3.add(b.JT, new Integer(2));
-               b3.add(BREAK);
-               parseStatement(false, b3);
-               return parseExpr(b, minPrecedence);
+       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 prefix;
+       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 ForthBlock parseStatement(boolean requireBraces) throws IOException { return parseStatement(requireBraces, null); }
-    public ForthBlock parseStatement(boolean requireBraces, ForthBlock b) throws IOException {
-       ForthBlock smt = null;
+    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 null;
+       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;
-       ForthBlock ret = new ForthBlock(curLine, sourceName);
-       ForthBlock block = b == null ? new ForthBlock(curLine, sourceName) : b;
-       block.add(ret.LITERAL, Boolean.TRUE);
-       ret.add(block.SCOPE, block);
        while(true) {
            switch(tok = peekToken()) {
 
-           case LC: smt = parseStatement(true); break;
-           case GOTO: throw new ParserException("goto not supported");
-
            case THROW: case RETURN: case ASSERT: {
                getToken();
-               ForthBlock r = new ForthBlock(curLine, sourceName);
-               if (tok == RETURN && peekToken() == SEMI) r.add(b.LITERAL, null);
-               else r.add(b.EXPR, parseExpr());
+               if (tok == RETURN && peekToken() == SEMI) b.add(LITERAL, null);
+               else startExpr(b);
                consume(SEMI);
-               r.add(tok);
-               smt = r;
+               b.add(tok);
                break;
            }
 
            case BREAK: case CONTINUE: {
                getToken();
                if (peekToken() == NAME) consume(NAME);
-               smt = new ForthBlock(curLine, sourceName, tok, string);
+               b.add(tok, string);
                consume(SEMI);
                break;
            }
                
-           case RC:
-               if (braced) consume(RC);
-               return block.size() == 0 ? null : ret;
-               
            case SEMI:
                consume(SEMI);
-               if (!braced) return block.size() == 0 ? null : ret;
-               continue;
+               if (!braced) return;
+               break;
+               
+           case VAR: {
+               consume(VAR);
+               b.add(TOPSCOPE);                               // push the current scope
+               while(true) {
+                   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 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 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 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;
+           }
+               
+           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();
+                   consume(LP);
+                   consume(NAME);
+                   consume(RP);
+                   parseStatement();   // just discard the catchblock
+               }
+
+               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) {
+               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 (hadVar) pushBackToken(VAR, null);
+               ByteCodeBlock b2 = newbb(curLine);
+               b.add(SCOPE, b2);
+               b.add(POP);
+
+               int size = b2.size();
+               parseStatement(false, b2);
+               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(CONTINUE);
+               break;
+           }
+       }
+           
            case NAME: {
-               String name = string;
                consume(NAME);
+               String name = string;
                if (peekToken() == COLON) {
                    consume(COLON);
-                   smt = new ForthBlock(curLine, sourceName, ForthBlock.LABEL, string);
+                   b.add(ByteCodeBlock.LABEL, string);
                    break;
                } else {
                    pushBackToken(NAME, name);
                    // fall through to default case
                }
            }
-
-           case -1:
-           default:
-               smt = parseExpr();
-               if (smt == null) return block.size() == 0 ? null : ret;
-               if (peekToken() == SEMI) getToken();
+            // 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 smt;
-           block.add(block.EXPR, smt);
-           block.add(block.POP);
+           }
+           
+           if (!braced) return;
        }
     }