2003/06/07 09:37:13
[org.ibex.core.git] / src / org / xwt / js / Parser.java
index cd2498b..cc55164 100644 (file)
+// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
 package org.xwt.js;
+
 import org.xwt.util.*;
 import java.io.*;
 
-// FIXME: for..in
-public class Parser extends Lexer {
-
-    public static void main(String[] s) throws Exception { System.out.println(new Parser(new InputStreamReader(System.in)).parseExpr()); }
+/** parses a stream of lexed tokens into ForthBlock's */
+public class Parser extends Lexer implements OpCodes {
 
-    public Parser(Reader r) throws IOException { super(r); }
-    private Parser skipToken() throws IOException { getToken(); return this; }
-    
-    /** sorta like gcc trees */
-    public static class Expr {
-       int code = -1;
-
-       Expr left = null;
-       Expr right = null;
-       Expr extra = null;
-
-       Expr next = null;   // if this expr is part of a list
-
-       String string = null;
-       Number number = null;
-
-       public String toString() { return toString(0); }
-
-       public String toString(int indent) {
-           String ret = "";
-           for(int i=0; i<indent; i++) ret += " ";
-           ret += codeToString[code];
-           if (code == STRING) ret += " \"" + string + "\"";
-           else if (code == NUMBER) ret += " " + number;
-           ret += "\n";
-           if (left != null) ret += left.toString(indent + 2);
-           if (right != null) ret += left.toString(indent + 2);
-           if (extra != null) ret += left.toString(indent + 2);
-           // FIXME: next
-           return ret;
-       }
+    // Constructors //////////////////////////////////////////////////////
 
-       public Expr(String s) { code = STRING; this.string = s; }  // an identifier or label
-       public Expr(Number n) { code = NUMBER; this.number = n; }  // an identifier or label
-       public Expr(int code) { this(code, null, null, null); }
-       public Expr(int code, Expr left) { this(code, left, null, null); }
-       public Expr(int code, Expr left, Expr right) { this(code, left, right, null); }
-       public Expr(int code, Expr left, Expr right, Expr extra) { this.left = left; this.right = right; this.extra = extra; this.code = code; }
+    public Parser(Reader r, String sourceName, int line) throws IOException {
+       super(r);
+       this.sourceName = sourceName;
+       this.line = line;
     }
-    
-    /** parses a single statement */
-    public Expr parseStatement() throws IOException {
-       int tok;
-       Expr ret;
-       switch(tok = peekToken()) {
-
-       case LC:
-           ret = parseBlock(true);
-
-       case THROW: case RETURN: case ASSERT:
-           ret = new Expr(ASSERT, skipToken().parseExpr());
-
-       case GOTO: case BREAK: case CONTINUE:
-           skipToken();
-           if (getToken() == NAME)
-               ret = new Expr(tok, new Expr(string));
-           else if (tok == GOTO)
-               throw new Error("goto must be followed by a label");
-           else
-               ret = new Expr(tok);
-                       
-       default:
-           ret = parseExpr();
-       }
 
-       if (getToken() != SEMI) throw new Error("expected ;");
-       return ret;
+    /** for debugging */
+    public static void main(String[] s) throws Exception {
+       Parser p = new Parser(new InputStreamReader(System.in), "stdin", 0);
+       while(true) {
+           ForthBlock block = new ForthBlock(0, "stdin");
+           p.parseStatement(false, block);
+           if (block == null) return;
+           System.out.println(block);
+           if (p.peekToken() == -1) return;
+       }
     }
 
-    /** 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 {
-       int tok = peekToken();
-       if (requireBraces && tok != LC) throw new Error("expected {");
-       if (tok != LC) return parseStatement();
-       skipToken();
-       Expr head = null;
-       Expr tail = null;
-       while(peekToken() != RC)
-           if (head == null) head = tail = parseStatement(); else tail = tail.next = parseStatement();
-       skipToken();
-       return new Expr(LC, head);
-    }
+
+    // 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[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] = precedence[INSTANCEOF] = 15;
+       precedence[INC] = precedence[DEC] = 16;
+       precedence[LP] = 17;
+       precedence[LB] = 18;
+       precedence[DOT] = 19;
     }
 
-    // called after each parseExpr(); returns null if we can't make the expression any bigger
-    public Expr parseExpr() throws IOException { return parseExpr(null, 0); }
-    public Expr parseExpr(Expr prefix, int minPrecedence) throws IOException {
-       Expr e1 = null;     
-       Expr e2 = null;     
-       Expr e3 = null;     
-       Expr head = null;
-       Expr tail = null;
-       Expr ret = null;
-       int tok = getToken();
 
-       if (minPrecedence != 0 && tok < precedence.length && precedence[tok] != 0 && precedence[tok] < minPrecedence)
-           return null;
+    // Parsing Logic /////////////////////////////////////////////////////////
 
-       // these case arms match the precedence of operators; each arm is a precedence level.
-       switch (tok) {
+    /** 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 COMMA: case ASSIGN: case GT: case GE: case OR: case AND:
-        case BITOR: case BITXOR: case BITAND: case EQ: case NE: case LT:
-        case LE: case SHEQ: case SHNE: case LSH: case RSH: case URSH:
-        case ADD: case SUB: case MUL: case DIV: case MOD:
-           return new Expr(tok, prefix, parseExpr(null, precedence[tok]));
-           
-       case BITNOT: case INSTANCEOF:
-           return new Expr(tok, parseExpr(null, precedence[tok]));
-           
-           // FIXME: this isn't 100% right
-       case INC: case DEC:
-           return new Expr(tok, prefix, (tok == INC || tok == DEC) ? null : parseExpr());
+    /** 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 {
 
-       case LP:
-           while(peekToken() != RP) {
-               if (head == null) head = tail = parseExpr(); else tail = tail.next = parseExpr();
-               tok = getToken();
-               if (tok == RP) break;
-               if (tok != COMMA) throw new Error("expected comma or right paren");
-           }
-           return new Expr(LP, prefix, head);
-
-       case LB:
-           if (prefix != null) {
-               // subscripting
-               e1 = parseExpr();
-               if (getToken() != RB) throw new Error("expected a right brace");
-               return new Expr(LB, prefix, e1);
-           } else {
-               // array ctor
-               tok = getToken();
-               while(true) {
-                   if (tok == RB) return new Expr(LB, prefix, head);
-                   if (head == null) head = tail = parseExpr(); else tail = tail.next = parseExpr();
-                   tok = getToken();
-                   if (tok != COMMA && tok != RP) throw new Error("expected right bracket or comma");
-               }
-           }
-           
-       case LC:
-           if (prefix != null) throw new Error("didn't expect non-null prefix");
-           tok = 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; }
+
+       ForthBlock b = appendTo;
+
+       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) {
-               if (tok == RP) return new Expr(LC, head);
-               if (tok != NAME) throw new Error("expecting name");
-               Expr name = parseExpr();
-               if (tok != COLON) throw new Error("expecting colon");           
-               e1 = new Expr(COLON, name, parseExpr());
-               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");
+               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);
            }
-           
-       case HOOK:
-           e2 = parseExpr();
-           if (getToken() != COLON) throw new Error("expected colon to close ?: expression");
-           e3 = parseExpr();
-           return new Expr(HOOK, prefix, 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 = parseExpr();
-           if (getToken() != RP) throw new Error("expected left paren");
-           if (getToken() != LC) throw new Error("expected left brace");
-           Expr firstExpr = null;
-           Expr lastExpr = null;
+       }
+       case SUB: {
+           consume(NUMBER);
+           return continueExpr(b.add(ForthBlock.LITERAL, new Double(number.doubleValue() * -1)), minPrecedence);
+       }
+       case LP: {
+           b.add(EXPR, startExpr());
+           consume(RP);
+           return continueExpr(b, minPrecedence);
+       }
+       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 (getToken() != CASE) throw new Error("expected CASE");
-               Expr caseExpr = parseExpr();
-               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);
+               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.POP);
+               if (peekToken() == RC) { consume(RC); return continueExpr(b, minPrecedence); }
+               consume(COMMA);
+               if (peekToken() == RC) { consume(RC); return continueExpr(b, minPrecedence); }
            }
        }
-           
-       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(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 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 {
+               b.add(THIS);
+               b.add(ForthBlock.LITERAL, name);
+               b.add(ForthBlock.GET);
            }
-           return new Expr(tok, formalArgs, parseBlock(true));
+           return continueExpr(b, minPrecedence);
        }
-           
-       case STRING:
-           return new Expr(string);
+       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 NUMBER:
-           return new Expr(number);
+                   // declare the name
+                   b2.add(b.LITERAL, string);
+                   b2.add(b.DECLARE);
 
-       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, new Expr(string));
-               Expr initVal = null;
-               tok = peekToken();
-               if (tok == ASSIGN) {
-                   skipToken();
-                   initVal = parseExpr();
-                   tok = peekToken();
+                   // 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);
                }
-               Expr e = new Expr(VAR, name, initVal);
-               if (head == null) head = tail = e; else tail = tail.next = e;
-               if (tok != COMMA) break;
-               skipToken();
+               numArgs++;
            }
-           return new Expr(VAR, head);
-           
-       case NAME:
-           if (prefix != null) throw new Error("didn't expect non-null prefix");
-           return new Expr(string);
-    
-       case TRUE: case FALSE: case NOP:
-           if (prefix != null) throw new Error("didn't expect non-null prefix");
-           return new Expr(tok);
-           
-       case TRY: {
-           // FIXME: 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)
-               if (head == null) head = tail = parseBlock(false); else tail = tail.next = parseBlock(false);
-           if (head == null) throw new Error("try without catch");
-           return new Expr(TRY, tryBlock, head, tok == FINALLY ? skipToken().parseBlock(false) : null);
+           // 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);
        }
-           
-       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 = parseExpr();
-           if (getToken() != RP) throw new Error("expected right paren");
-           Expr firstBlock = parseBlock(false);
-           if (tok == IF && peekToken() == ELSE) return new Expr(tok, parenExpr, firstBlock, skipToken().parseBlock(false));
-           return new Expr(tok, parenExpr, firstBlock);
+       default: pushBackToken(); return null;
        }
+    }
 
-       case FOR:
-           // FIXME: for..in
-           if (prefix != null) throw new Error("didn't expect non-null prefix");
-           if (getToken() != LP) throw new Error("expected left paren");
-           e1 = parseStatement();
-           e2 = parseStatement();
-           e3 = parseStatement();  // FIXME: this guy has to be okay with ending via a )
-           if (getToken() != RP) throw new Error("expected right paren");
-           throw new Error("not yet implemented");
-           //return new Expr(FOR, e1, e2, e3, parseBlock(false));
-           
-       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 = parseExpr();
-           if (getToken() != RP) throw new Error("expected right paren");
-           if (getToken() != SEMI) throw new Error("semicolon");
-           return new Expr(DO, firstBlock, whileExpr);
+
+    /** 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 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: {
+           b.add(b.EXPR, prefix);
+           b.add(b.EXPR, startExpr(precedence[tok]));
+           b.add(tok);
+           return continueExpr(b, minPrecedence);
+       }
+
+       case OR: case AND: {
+           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, startExpr(precedence[tok]));
+           return continueExpr(b, minPrecedence);
+       }
+
+       case DOT: {
+           consume(NAME);
+           String target = string;
+           b.add(b.EXPR, prefix);
+           if (peekToken() == ASSIGN) {
+               consume(ASSIGN);
+               ForthBlock val = startExpr();
+               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 continueExpr(b, minPrecedence);
+       }
+
+       case LB: {
+           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);
+           } else {
+               b.add(b.GET);
+           }
+           return continueExpr(b, minPrecedence);
+       }
+
+       case HOOK: {
+           b.add(b.EXPR, prefix);
+           b.add(b.JF, new Integer(3));
+           b.add(b.EXPR, startExpr());
+           b.add(b.JMP, new Integer(2));
+           consume(COLON);
+           b.add(b.EXPR, startExpr());
+           return continueExpr(b, minPrecedence);
        }
            
-       case VOID: case RESERVED:
-           throw new Error("reserved word that you shouldn't be using");
+           
+       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;
+    }
+
+    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()) {
+
+           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;
+           }
+
+           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);
+               }
+               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;
+           }
+
+           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: {
+               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: {
+               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;
+           }
+               
+           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
+               }
+
+               if (peekToken() == FINALLY) {
+                   consume(FINALLY);
+                   parseStatement(false, b);
+               }
+               break;
+           }
+
+       case FOR: {
+           consume(FOR);
+           consume(LP);
 
-       case WITH:
-           throw new Error("WITH not yet implemented"); // FIXME
+           tok = getToken();
+           if (tok == VAR) tok = getToken();
+           String varName = string;
+           boolean forIn = peekToken() == IN;
+           pushBackToken(tok, varName);
+
+           if (forIn) {
+               consume(NAME);
+               consume(IN);
+               b.add(b.EXPR, startExpr());
+               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());
+               //b2.add(b.LITERAL, null);
+               break;
+               
+           } else {
+               ForthBlock b2 = new ForthBlock(curLine, sourceName);
+               b.add(b.SCOPE, b2);
+               b.add(b.POP);
+
+               ForthBlock e1 = startExpr();
+               if (e1 == null) e1 = new ForthBlock(curLine, sourceName, b.LITERAL, null);
 
-       default:
-           pushBackToken();
-           return null;
+               b2.add(b.EXPR, e1);
+               b2.add(b.POP);
+               consume(SEMI);
+               ForthBlock e2 = startExpr();
+               consume(SEMI);
+               ForthBlock e3 = startExpr();
+               consume(RP);
+
+               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);
+               b3.add(BREAK);
+               break;
+           }
+       }
+           
+           case NAME: {
+               consume(NAME);
+               String name = string;
+               if (peekToken() == COLON) {
+                   consume(COLON);
+                   b.add(ForthBlock.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: {
+               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;
        }
     }
     
+    class ParserException extends RuntimeException {
+       public ParserException(String s) { super(sourceName + ":" + line + " " + s); }
+    }
+    
 }