2003/06/07 09:37:13
[org.ibex.core.git] / src / org / xwt / js / Parser.java
index 67cb0a1..cc55164 100644 (file)
@@ -1,4 +1,4 @@
-// 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.*;
@@ -19,7 +19,8 @@ public class Parser extends Lexer implements OpCodes {
     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);
+           ForthBlock block = new ForthBlock(0, "stdin");
+           p.parseStatement(false, block);
            if (block == null) return;
            System.out.println(block);
            if (p.peekToken() == -1) return;
@@ -32,8 +33,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;
@@ -57,219 +59,237 @@ public class Parser extends Lexer implements OpCodes {
 
     // 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 ? "EOL" : codeToString[op]));
     }
 
-    /** parses the largest possible expression */
-    public ForthBlock parseMaximalForthBlock() throws IOException { return parseMaximalForthBlock(null, -1); }
-    public ForthBlock parseMaximalForthBlock(ForthBlock prefix, int minPrecedence) throws IOException {
-       while(true) {
-           if (peekToken() == -1) break;
-           ForthBlock save = prefix;
-           prefix = parseSingleForthBlock(prefix, minPrecedence);
-           if (save == prefix) break;
-           if (prefix == null) throw new ParserException("parseSingleForthBlock() returned null");
-       }
-       return prefix;
-    }
-
-    public ForthBlock parseSingleForthBlock(ForthBlock prefix, int minPrecedence) throws IOException {
-       ForthBlock e1 = null, e2 = null, e3 = null, head = null, tail = null, ret = null;
+    /** append the largest expression beginning with prefix containing no operators of precedence below <tt>minPrecedence</tt> */
+    public ForthBlock startExpr() throws IOException { return startExpr(-1); }
+    public ForthBlock startExpr(int minPrecedence) throws IOException { return startExpr(minPrecedence, new ForthBlock(line, sourceName)); }
+    public ForthBlock startExpr(int minPrecedence, ForthBlock appendTo) throws IOException {
 
-       int tok = peekToken();
-       if (minPrecedence > 0 && tok < precedence.length && precedence[tok] != 0 &&
-           (isRightAssociative[tok] ? (precedence[tok] < minPrecedence) : (precedence[tok] <= minPrecedence)))
-           return prefix;
-
-       getToken();
+       int tok = getToken();
        int curLine = line;
+       if (tok == -1) return null;
+       if (minPrecedence > 0 && precedence[tok] != 0)
+           if (precedence[tok] < minPrecedence || (precedence[tok] == minPrecedence && !isRightAssociative[tok]))
+               { pushBackToken(); return null; }
 
-       switch (tok) {
+       ForthBlock b = appendTo;
 
-       case VAR: {
-           if (prefix != null) { pushBackToken(); return prefix; }
-           ForthBlock b = new ForthBlock(curLine, sourceName);
-           b.add(OpCodes.THIS);
+       switch (tok) {
+       case NUMBER: return continueExpr(b.add(ForthBlock.LITERAL, number), minPrecedence);
+       case STRING: return continueExpr(b.add(ForthBlock.LITERAL, string), minPrecedence);
+       case THIS: return continueExpr(b.add(THIS, null), minPrecedence);
+       case NULL: return continueExpr(b.add(ForthBlock.LITERAL, null), minPrecedence);
+       case TRUE: case FALSE: return continueExpr(b.add(ForthBlock.LITERAL, new Boolean(tok == TRUE)), minPrecedence);
+       case LB: {
+           b.add(b.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, parseMaximalForthBlock());
-                   b.add(b.PUT);
-                   b.add(b.POP);
-               }
-               if (peekToken() != COMMA) break;
+               ForthBlock e = startExpr();
+               if (e == null && peekToken() == RB) { consume(RB); return continueExpr(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 continueExpr(b, minPrecedence); }
                consume(COMMA);
            }
-           return parseSingleForthBlock(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, parseMaximalForthBlock());
+       case SUB: {
+           consume(NUMBER);
+           return continueExpr(b.add(ForthBlock.LITERAL, new Double(number.doubleValue() * -1)), minPrecedence);
+       }
+       case LP: {
+           b.add(EXPR, startExpr());
            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 parseSingleForthBlock(b.add(b.LITERAL, null), minPrecedence);
-           consume(ELSE);
-           b.add(b.EXPR, parseStatement(false));
-           return parseSingleForthBlock(b, minPrecedence);
+           return continueExpr(b, minPrecedence);
        }
-
-           // 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, parseMaximalForthBlock(null, precedence[tok]));
-               b.add(tok);
+       case INC: case DEC: {
+           // prefix
+           ForthBlock subexp = startExpr(precedence[tok]);
+           subexp.set(subexp.size() - 1, tok, new Boolean(true));
+           b.add(b.EXPR, subexp);
+           return continueExpr(b, minPrecedence);
+       }
+       case BANG: case BITNOT: case INSTANCEOF: case TYPEOF: {
+           b.add(b.EXPR, startExpr(precedence[tok]));
+           b.add(tok);
+           return continueExpr(b, minPrecedence);
+       }
+       case LC: {
+           b.add(b.OBJECT, null);
+           if (peekToken() == RC) { consume(RC); return continueExpr(b, minPrecedence); }
+           while(true) {
+               if (peekToken() != NAME && peekToken() != STRING) throw new Error("expected NAME or STRING");
+               getToken();
+               b.add(b.LITERAL, string);
+               consume(COLON);
+               b.add(b.EXPR, startExpr());
                b.add(b.PUT);
-               b.add(b.SWAP);
                b.add(b.POP);
-               return parseSingleForthBlock(b, minPrecedence);
+               if (peekToken() == RC) { consume(RC); return continueExpr(b, minPrecedence); }
+               consume(COMMA);
+               if (peekToken() == RC) { consume(RC); return continueExpr(b, minPrecedence); }
            }
-
-       case INC: case DEC:
-           if (prefix == null) {
-               // prefix
-               ForthBlock b = (ForthBlock)parseMaximalForthBlock(null, precedence[tok]);
-               b.set(b.size() - 1, tok, new Boolean(true));
-               return parseSingleForthBlock(b, minPrecedence);
+       }
+       case NAME: {
+           String name = string;
+           if (peekToken() == ASSIGN) {
+               consume(ASSIGN);
+               b.add(THIS);
+               b.add(ForthBlock.LITERAL, name);
+               b.add(ForthBlock.EXPR, startExpr(minPrecedence));
+               b.add(ForthBlock.PUT);
+               b.add(ForthBlock.SWAP);
+               b.add(ForthBlock.POP);
            } else {
-               // postfix
-               ForthBlock b = (ForthBlock)prefix;
-               b.set(b.size() - 1, tok, new Boolean(false));
-               return parseSingleForthBlock(b, minPrecedence);
+               b.add(THIS);
+               b.add(ForthBlock.LITERAL, name);
+               b.add(ForthBlock.GET);
            }
+           return continueExpr(b, minPrecedence);
+       }
+       case Tokens.FUNCTION: {
+           consume(LP);
+           int numArgs = 0;
+           ForthBlock b2 = new ForthBlock(curLine, sourceName);
+           b2.add(THIS);
+           b2.add(b.SWAP);
+           b2.add(b.LITERAL, "arguments");
+           b2.add(b.LITERAL, "arguments");
+           b2.add(b.DECLARE);
+           b2.add(b.SWAP);
+           b2.add(b.PUT);
+           b2.add(b.SWAP);
+           b2.add(b.POP);
+           if (peekToken() == RP) consume(RP);
+           else while(true) {
+               if (peekToken() == COMMA) {
+                   // FIXME: pop an item off the stack here?
+                   consume(COMMA);
+               } else {
+                   consume(NAME);
 
-       case LP:
-           if (prefix == null) {  // grouping
-               ForthBlock b = new ForthBlock(curLine, sourceName, ForthBlock.EXPR, parseMaximalForthBlock());
-               consume(RP);
-               return parseSingleForthBlock(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, parseMaximalForthBlock());
-                   i++;
-                   if (peekToken() == RP) break;
+                   // declare the name
+                   b2.add(b.LITERAL, string);
+                   b2.add(b.DECLARE);
+
+                   // retrieve it from the arguments array
+                   b2.add(b.LITERAL, new Integer(numArgs));
+                   b2.add(b.GET_PRESERVE);
+                   b2.add(b.SWAP);
+                   b2.add(b.POP);
+
+                   // put it to the current scope
+                   b2.add(THIS);
+                   b2.add(b.SWAP);
+                   b2.add(b.LITERAL, string);
+                   b2.add(b.SWAP);
+                   b2.add(b.PUT);
+
+                   // clean the stack
+                   b2.add(b.POP);
+                   b2.add(b.POP);
+
+                   if (peekToken() == RP) { consume(RP); break; }
                    consume(COMMA);
                }
-               consume(RP);
-               b.add(b.CALL, new Integer(i));
-               return parseSingleForthBlock(b, minPrecedence);
+               numArgs++;
            }
+           // pop off the arguments array
+           b2.add(b.POP);
+           parseStatement(true, b2);
+           b2.add(b.LITERAL, null);
+           b2.add(RETURN);
+           return continueExpr(b.add(OpCodes.FUNCTION, b2), minPrecedence);
+       }
+       default: pushBackToken(); return null;
+       }
+    }
 
-       case BANG: case BITNOT: case INSTANCEOF: case TYPEOF: {
-           if (prefix != null) { pushBackToken(); return prefix; }
-           ForthBlock b = new ForthBlock(curLine, sourceName);
-           b.add(b.EXPR, parseMaximalForthBlock(null, precedence[tok]));
-           b.add(tok);
-           return parseSingleForthBlock(b, minPrecedence);
+
+    /** return the largest expression beginning with prefix containing no operators of precedence below <tt>minPrecedence</tt> */
+    public ForthBlock continueExpr(ForthBlock prefix, int minPrecedence) throws IOException {
+       return continueExpr(prefix, minPrecedence, new ForthBlock(line, sourceName));
+    }
+    public ForthBlock continueExpr(ForthBlock prefix, int minPrecedence, ForthBlock appendTo) throws IOException {
+       int tok = getToken();
+       int curLine = line;
+       if (tok == -1) return prefix;
+       if (minPrecedence > 0 && precedence[tok] != 0)
+           if (precedence[tok] < minPrecedence || (precedence[tok] == minPrecedence && !isRightAssociative[tok]))
+               { pushBackToken(); return prefix; }
+
+       if (prefix == null) throw new Error("got null prefix");
+
+       ForthBlock b = appendTo;
+
+       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: {
+           b.add(b.EXPR, prefix);
+           prefix.set(prefix.size() - 1, b.GET_PRESERVE, new Boolean(true));
+           prefix.add(prefix.EXPR, startExpr(precedence[tok - 1]));
+           prefix.add(tok - 1);
+           prefix.add(b.PUT);
+           prefix.add(b.SWAP);
+           prefix.add(b.POP);
+           return continueExpr(b, minPrecedence);
+       }
+
+       case INC: case DEC: {
+           // postfix
+           prefix.set(prefix.size() - 1, tok, new Boolean(false));
+           b.add(b.EXPR, prefix);
+           return continueExpr(b, minPrecedence);
+       }
+
+       case LP: {
+           // invocation
+           int i = 0;
+           b.add(b.EXPR, prefix);
+           while(peekToken() != RP) {
+               b.add(b.EXPR, startExpr());
+               i++;
+               if (peekToken() == RP) break;
+               consume(COMMA);
+           }
+           consume(RP);
+           b.add(b.CALL, new Integer(i));
+           return continueExpr(b, minPrecedence);
        }
 
-       case SUB:
-           if (prefix == null && peekToken() == NUMBER) {
-               getToken();
-               return parseSingleForthBlock(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);
+       case GT: case GE: case EQ: case NE: case LT: case LE: case SUB: {
            b.add(b.EXPR, prefix);
-           b.add(b.EXPR, parseMaximalForthBlock(null, precedence[tok]));
+           b.add(b.EXPR, startExpr(precedence[tok]));
            b.add(tok);
-           return parseSingleForthBlock(b, minPrecedence);
+           return continueExpr(b, minPrecedence);
        }
 
-           // 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, parseMaximalForthBlock(null, precedence[tok]));
-           return parseSingleForthBlock(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 parseSingleForthBlock(new ForthBlock(curLine, sourceName, ForthBlock.LITERAL, number), minPrecedence);
-
-       case STRING:
-           if (prefix != null) { pushBackToken(); return prefix; }
-           return parseSingleForthBlock(new ForthBlock(curLine, sourceName, ForthBlock.LITERAL, string), minPrecedence);
-
-       case NULL: case TRUE: case FALSE: case NOP:
-           if (prefix != null) { pushBackToken(); return prefix; }
-           return parseSingleForthBlock(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 parseSingleForthBlock(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, parseMaximalForthBlock(null, minPrecedence));
-               b.add(ForthBlock.PUT);
-               b.add(ForthBlock.SWAP);
-               b.add(ForthBlock.POP);
-               return parseSingleForthBlock(b, minPrecedence);
-           } else {
-               b.add(OpCodes.THIS);
-               b.add(ForthBlock.LITERAL, name);
-               b.add(ForthBlock.GET);
-               return parseSingleForthBlock(parseMaximalForthBlock(b, minPrecedence), minPrecedence);
-           }
+           b.add(b.EXPR, startExpr(precedence[tok]));
+           return continueExpr(b, 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 = parseMaximalForthBlock();
+               ForthBlock val = startExpr();
                b.add(b.LITERAL, target);
                b.add(b.EXPR, val);
                b.add(b.PUT);
@@ -279,219 +299,221 @@ public class Parser extends Lexer implements OpCodes {
                b.add(b.LITERAL, target);
                b.add(b.GET);
            }
-           return parseSingleForthBlock(b, minPrecedence);
+           return continueExpr(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 = parseMaximalForthBlock();
-                   if (e == null && peekToken() == RB) { consume(RB); return parseSingleForthBlock(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 parseSingleForthBlock(b, minPrecedence); }
-                   consume(COMMA);
-               }
-           } else {
-               b.add(b.EXPR, prefix);
-               b.add(b.EXPR, parseMaximalForthBlock());
-               consume(RB);
-               if (peekToken() == ASSIGN) {
-                   consume(ASSIGN);
-                   b.add(b.EXPR, parseMaximalForthBlock());
-                   b.add(b.PUT);
-                   b.add(b.SWAP);
-                   b.add(b.POP);
-               } else {
-                   b.add(b.GET);
-               }
-               return parseSingleForthBlock(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 parseSingleForthBlock(b, minPrecedence); }
-           while(true) {
-               if (peekToken() != NAME && peekToken() != STRING) throw new Error("expected NAME or STRING");
-               getToken();
-               b.add(b.LITERAL, string);
-               consume(COLON);
-               b.add(b.EXPR, parseMaximalForthBlock());
+           b.add(b.EXPR, prefix);
+           b.add(b.EXPR, startExpr());
+           consume(RB);
+           if (peekToken() == ASSIGN) {
+               consume(ASSIGN);
+               b.add(b.EXPR, startExpr());
                b.add(b.PUT);
+               b.add(b.SWAP);
                b.add(b.POP);
-               if (peekToken() == RC) { consume(RC); return parseSingleForthBlock(b, minPrecedence); }
-               consume(COMMA);
-               if (peekToken() == RC) { consume(RC); return parseSingleForthBlock(b, minPrecedence); }
+           } else {
+               b.add(b.GET);
            }
+           return continueExpr(b, minPrecedence);
        }
-           
+
        case HOOK: {
-           ForthBlock b = new ForthBlock(curLine, sourceName);
            b.add(b.EXPR, prefix);
            b.add(b.JF, new Integer(3));
-           b.add(b.EXPR, parseMaximalForthBlock());
+           b.add(b.EXPR, startExpr());
            b.add(b.JMP, new Integer(2));
            consume(COLON);
-           b.add(b.EXPR, parseMaximalForthBlock());
-           return parseSingleForthBlock(b, minPrecedence);
+           b.add(b.EXPR, startExpr());
+           return continueExpr(b, minPrecedence);
        }
            
-       case Tokens.FUNCTION: {
-           if (prefix != null) { pushBackToken(); return prefix; }
-           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);
-           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);
-
-                   // 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);
+           
+       default: pushBackToken(); return prefix;
+       }
+    }
+    
+    /** a block is either a single statement or a list of statements surrounded by curly braces; all expressions are also statements */
+    public ForthBlock parseStatement() throws IOException {
+       ForthBlock ret = new ForthBlock(line, sourceName);
+       ret.add(ret.LITERAL, null);
+       parseStatement(false, ret);
+       if (ret.size() == 1) return null;
+       return ret;
+    }
 
-                   // 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);
+    public void parseStatement(boolean requireBraces) throws IOException {
+       parseStatement(requireBraces, new ForthBlock(line, sourceName));
+    }
+    public void parseStatement(boolean requireBraces, ForthBlock 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()) {
 
-                   // clean the stack
-                   b.add(b.POP);
-                   b.add(b.POP);
+           case THROW: case RETURN: case ASSERT: {
+               getToken();
+               if (tok == RETURN && peekToken() == SEMI) b.add(b.LITERAL, null);
+               else b.add(b.EXPR, startExpr());
+               consume(SEMI);
+               b.add(tok);
+               break;
+           }
 
-                   if (peekToken() == RP) { consume(RP); break; }
+           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(THIS);                               // push the current scope
+               while(true) {
+                   consume(NAME);
+                   String name = string;
+                   b.add(b.LITERAL, name);                // push the name to be declared
+                   b.add(b.DECLARE);                      // declare it
+                   if (peekToken() == ASSIGN) {           // if there is an '=' after the variable name
+                       b.add(b.LITERAL, name);            // put the var name back on the stack
+                       consume(ASSIGN);
+                       b.add(b.EXPR, startExpr());
+                       b.add(b.PUT);
+                       b.add(b.POP);
+                   }
+                   if (peekToken() != COMMA) break;
                    consume(COMMA);
                }
-               numArgs++;
+               b.add(b.POP);
+               if (peekToken() == SEMI) consume(SEMI);
+               break;
+           }
+               
+           case IF: {
+               consume(IF);
+               consume(LP);
+               b.add(b.EXPR, startExpr());
+               consume(RP);
+               b.add(b.JF, new Integer(4));
+               b.add(b.EXPR, parseStatement());
+               b.add(b.POP);
+               b.add(b.JMP, new Integer(3));
+               if (peekToken() == ELSE) {
+                   consume(ELSE);
+                   b.add(b.EXPR, parseStatement());
+                   b.add(b.POP);
+               } else {
+                   b.add(b.JMP, new Integer(1));  // nop
+                   b.add(b.JMP, new Integer(1));  // nop
+               }
+               break;
            }
-           // pop off the arguments array
-           b.add(b.POP);
-           parseStatement(true, b);
-           return parseSingleForthBlock(new ForthBlock(curLine, sourceName, OpCodes.FUNCTION, b), minPrecedence);
-       }
-           
-       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, parseMaximalForthBlock());
-           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 parseSingleForthBlock(r, minPrecedence);
-       }
+           case WHILE: {
+               consume(WHILE);
+               consume(LP);
+               ForthBlock loop = new ForthBlock(curLine, sourceName);
+               b.add(loop.LOOP, loop);
+               
+               loop.add(loop.POP);
+               loop.add(loop.EXPR, startExpr());
+               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);
+               break;
+           }
 
-       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, parseMaximalForthBlock());
-           consume(RP);
-           consume(LC);
-           while(true) {
-               ForthBlock caseForthBlock;
-               tok = getToken();
-               if (tok == CASE) {
-                   loop.add(loop.DUP);
-                   loop.add(loop.EXPR, parseMaximalForthBlock());
-                   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 parseSingleForthBlock(r, minPrecedence);
+           case SWITCH: {
+               consume(SWITCH);
+               consume(LP);
+               ForthBlock loop = new ForthBlock(curLine, sourceName);
+               b.add(loop.LOOP, loop);
+               loop.add(loop.EXPR, startExpr());
+               consume(RP);
+               consume(LC);
+               while(true) {
+                   ForthBlock caseForthBlock;
+                   tok = getToken();
+                   if (tok == CASE) {
+                       loop.add(loop.DUP);
+                       loop.add(loop.EXPR, startExpr());
+                       loop.add(EQ);
+                       loop.add(loop.JF, new Integer(2));
+                   } else if (tok != DEFAULT) throw new ParserException("expected CASE or DEFAULT");
+                   consume(COLON);
+                   ForthBlock b2 = new ForthBlock(curLine, sourceName);
+                   ForthBlock e1 = null;
+                   while(peekToken() != CASE && peekToken() != DEFAULT && peekToken() != RC) {
+                       if ((e1 = parseStatement()) == null) break;
+                       b2.add(b.EXPR, e1);
+                       b2.add(b.POP);
+                   }
+                   loop.add(loop.EXPR, b2);
+                   if (peekToken() == RC) {
+                       consume(RC);
+                       loop.add(BREAK);
+                       break;
+                   }
                }
+               break;
            }
-       }
-           
-       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, parseMaximalForthBlock());
-           loop.add(loop.JT, new Integer(2));
-           loop.add(Lexer.BREAK);
-           loop.add(Lexer.CONTINUE);
-           consume(RP);
-           consume(SEMI);
-           return parseSingleForthBlock(r, minPrecedence);
-       }
-
-       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 DO: {
+               consume(DO);
+               ForthBlock loop = new ForthBlock(curLine, sourceName);
+               b.add(loop.LOOP, loop);
+               
+               parseStatement(false, loop);
+               consume(WHILE);
+               consume(LP);
+               loop.add(loop.EXPR, startExpr());
+               loop.add(loop.JT, new Integer(2));
+               loop.add(Lexer.BREAK);
+               loop.add(Lexer.CONTINUE);
+               consume(RP);
+               consume(SEMI);
+               break;
            }
-           if (tok == FINALLY) getToken();
+               
+           case TRY: {
+               // FIXME: don't just ignore this!
+               // 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
+               }
 
-           return parseSingleForthBlock(tryBlock, minPrecedence);
-       }
+               if (peekToken() == FINALLY) {
+                   consume(FINALLY);
+                   parseStatement(false, b);
+               }
+               break;
+           }
 
        case FOR: {
-           if (prefix != null) { pushBackToken(); return prefix; }
-           if (getToken() != LP) throw new ParserException("expected left paren");
+           consume(FOR);
+           consume(LP);
 
            tok = getToken();
            if (tok == VAR) tok = getToken();
@@ -499,11 +521,10 @@ public class Parser extends Lexer implements OpCodes {
            boolean forIn = peekToken() == IN;
            pushBackToken(tok, varName);
 
-           ForthBlock b = new ForthBlock(curLine, sourceName);
            if (forIn) {
                consume(NAME);
                consume(IN);
-               b.add(b.EXPR, parseMaximalForthBlock());
+               b.add(b.EXPR, startExpr());
                b.add(b.PUSHKEYS);
                b.add(b.LITERAL, "length");
                b.add(b.GET);
@@ -521,23 +542,24 @@ public class Parser extends Lexer implements OpCodes {
                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 parseSingleForthBlock(b, minPrecedence);
+               b2.add(b.EXPR, parseStatement());
+               //b2.add(b.LITERAL, null);
+               break;
                
            } else {
                ForthBlock b2 = new ForthBlock(curLine, sourceName);
                b.add(b.SCOPE, b2);
+               b.add(b.POP);
 
-               e1 = parseMaximalForthBlock();
+               ForthBlock e1 = startExpr();
                if (e1 == null) e1 = new ForthBlock(curLine, sourceName, b.LITERAL, null);
 
                b2.add(b.EXPR, e1);
                b2.add(b.POP);
                consume(SEMI);
-               e2 = parseMaximalForthBlock();
+               ForthBlock e2 = startExpr();
                consume(SEMI);
-               e3 = parseMaximalForthBlock();
+               ForthBlock e3 = startExpr();
                consume(RP);
 
                if (e2 == null) e2 = new ForthBlock(curLine, sourceName, b.LITERAL, null);
@@ -553,88 +575,38 @@ public class Parser extends Lexer implements OpCodes {
                b3.add(b.JT, new Integer(2));
                b3.add(BREAK);
                parseStatement(false, b3);
-               return parseSingleForthBlock(b, minPrecedence);
+               b3.add(BREAK);
+               break;
            }
        }
            
-       default:
-           pushBackToken();
-           return prefix;
-       }
-    }
-    
-    /** 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;
-       int tok = peekToken();
-       if (tok == -1) return null;
-       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, parseMaximalForthBlock());
-               consume(SEMI);
-               r.add(tok);
-               smt = r;
-               break;
-           }
-
-           case BREAK: case CONTINUE: {
-               getToken();
-               if (peekToken() == NAME) consume(NAME);
-               smt = new ForthBlock(curLine, sourceName, 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;
-               
            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(ForthBlock.LABEL, string);
                    break;
                } else {
                    pushBackToken(NAME, name);
                    // fall through to default case
                }
            }
-
-           case -1:
-           default:
-               smt = parseMaximalForthBlock();
-               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: {
+               ForthBlock ret = startExpr();
+               if (ret == null) return;
+               b.add(b.EXPR, ret);
+               b.add(b.POP);
+               if (peekToken() == SEMI) consume(SEMI);
                break;
            }
-
-           if (!braced) return smt;
-           block.add(block.EXPR, smt);
-           block.add(block.POP);
+           }
+           
+           if (!braced) return;
        }
     }