2003/06/07 08:21:30
[org.ibex.core.git] / src / org / xwt / js / Parser.java
index 0535d50..5a170ba 100644 (file)
@@ -1,20 +1,26 @@
-// Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
+// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
 package org.xwt.js;
 
 import org.xwt.util.*;
 import java.io.*;
 
-/** parses a stream of lexed tokens into a tree of Expr's */
-public class Parser extends Lexer {
+/** parses a stream of lexed tokens into ForthBlock's */
+public class Parser extends Lexer implements OpCodes {
 
     // Constructors //////////////////////////////////////////////////////
 
-    public Parser(Reader r) throws IOException { super(r); }
+    public Parser(Reader r, String sourceName, int line) throws IOException {
+       super(r);
+       this.sourceName = sourceName;
+       this.line = line;
+    }
 
+    /** for debugging */
     public static void main(String[] s) throws Exception {
-       Parser p = new Parser(new InputStreamReader(System.in));
+       Parser p = new Parser(new InputStreamReader(System.in), "stdin", 0);
        while(true) {
-           Expr block = p.parseBlock(false);
+           ForthBlock block = new ForthBlock(0, "stdin");
+           p.parseStatement(false, block);
            if (block == null) return;
            System.out.println(block);
            if (p.peekToken() == -1) return;
@@ -25,358 +31,635 @@ public class Parser extends Lexer {
     // Statics ////////////////////////////////////////////////////////////
 
     static byte[] precedence = new byte[MAX_TOKEN + 1];
+    static boolean[] isRightAssociative = new boolean[MAX_TOKEN + 1];
+    static boolean[] mustStartExpression = new boolean[MAX_TOKEN + 1];
+    static boolean[] cannotStartExpression = new boolean[MAX_TOKEN + 1];
     static {
-       precedence[COMMA] = 1;
-       precedence[ASSIGN] = 2;
-       precedence[GT] = precedence[GE] = 3;
+
+       mustStartExpression[VAR] = true;
+       mustStartExpression[IF] = true;
+       mustStartExpression[BANG] = true;
+       mustStartExpression[BITNOT] = true;
+       mustStartExpression[INSTANCEOF] = true;
+       mustStartExpression[TYPEOF] = true;
+       mustStartExpression[NUMBER] = true;
+       mustStartExpression[STRING] = true;
+       mustStartExpression[NULL] = true;
+       mustStartExpression[TRUE] = true;
+       mustStartExpression[FALSE] = true;
+       mustStartExpression[Tokens.FUNCTION] = true;
+       mustStartExpression[NAME] = true;
+       mustStartExpression[LC] = true;
+       mustStartExpression[THIS] = true;
+       
+       cannotStartExpression[OR] = true;
+       cannotStartExpression[AND] = true;
+       cannotStartExpression[BITOR] = true;
+       cannotStartExpression[BITXOR] = true;
+       cannotStartExpression[BITAND] = true;
+       cannotStartExpression[SHEQ] = true;
+       cannotStartExpression[SHNE] = true;
+       cannotStartExpression[LSH] = true;
+       cannotStartExpression[RSH] = true;
+       cannotStartExpression[URSH] = true;
+       cannotStartExpression[ADD] = true;
+       cannotStartExpression[MUL] = true;
+       cannotStartExpression[DIV] = true;
+       cannotStartExpression[MOD] = true;
+       cannotStartExpression[GT] = true;
+       cannotStartExpression[GE] = true;
+       cannotStartExpression[EQ] = true;
+       cannotStartExpression[NE] = true;
+       cannotStartExpression[LT] = true;
+       cannotStartExpression[LE] = true;
+       cannotStartExpression[DOT] = true;
+       cannotStartExpression[HOOK] = true;
+       cannotStartExpression[ASSIGN_BITOR] = true;
+       cannotStartExpression[ASSIGN_BITXOR] = true;
+       cannotStartExpression[ASSIGN_BITAND] = true;
+       cannotStartExpression[ASSIGN_LSH] = true;
+       cannotStartExpression[ASSIGN_RSH] = true;
+       cannotStartExpression[ASSIGN_URSH] = true;
+       cannotStartExpression[ASSIGN_ADD] = true;
+       cannotStartExpression[ASSIGN_SUB] = true;
+       cannotStartExpression[ASSIGN_MUL] = true;
+       cannotStartExpression[ASSIGN_DIV] = true;
+       cannotStartExpression[ASSIGN_MOD] = true;
+
+       isRightAssociative[ASSIGN] = true;
+
+       precedence[ASSIGN] = 1;
+       precedence[HOOK] = 2;
+       precedence[COMMA] = 3;
        precedence[OR] = precedence[AND] = 4;
-       precedence[BITOR] = 5;
-       precedence[BITXOR] = 6;
-       precedence[BITAND] = 7;
-       precedence[EQ] = precedence[NE] = 8;
-       precedence[LT] = precedence[LE] = 9;
-       precedence[SHEQ] = precedence[SHNE] = 10;
-       precedence[LSH] = precedence[RSH] = precedence[URSH] = 11;
-       precedence[ADD] = precedence[SUB] = 12;
-       precedence[MUL] = precedence[DIV] = precedence[MOD] = 13;
-       precedence[BITNOT] = precedence[INSTANCEOF] = 14;
-       precedence[INC] = precedence[DEC] = 15;
-       precedence[LP] = 16;
-       precedence[DOT] = 17;
+       precedence[GT] = precedence[GE] = 5;
+       precedence[BITOR] = 6;
+       precedence[BITXOR] = 7;
+       precedence[BITAND] = 8;
+       precedence[EQ] = precedence[NE] = 9;
+       precedence[LT] = precedence[LE] = 10;
+       precedence[SHEQ] = precedence[SHNE] = 11;
+       precedence[LSH] = precedence[RSH] = precedence[URSH] = 12;
+       precedence[ADD] = precedence[SUB] = 13;
+       precedence[MUL] = precedence[DIV] = precedence[MOD] = 14;
+       precedence[BITNOT] = precedence[INSTANCEOF] = 15;
+       precedence[INC] = precedence[DEC] = 16;
+       precedence[LP] = 17;
+       precedence[LB] = 18;
+       precedence[DOT] = 19;
     }
 
 
-    // Useful Types /////////////////////////////////////////////////////////
-
-    /** sorta like gcc trees */
-    public static class Expr {
-
-       int code = -1;
-
-       Expr left = null;
-       Expr right = null;
-       Expr next = null;   // if this expr is part of a list
+    // Parsing Logic /////////////////////////////////////////////////////////
 
-       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 == NUMBER) ret += " " + number;
-           else if (string != null) ret += " \"" + string + "\"";
-           ret += "\n";
-           if (left != null) ret += left.toString(indent + 2);
-           if (right != null) ret += right.toString(indent + 2);
-           if (next != null) ret += next.toString(indent);
-           return ret;
-       }
-
-       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); }
-       public Expr(int code, String s) { this.code = code; string = s; }
-       public Expr(int code, Expr left) { this(code, left, null); }
-       public Expr(int code, Expr left, Expr right) { this.code = code; this.left = left; this.right = right; }
+    /** 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]));
     }
-    
-    /** a block is either a single statement or a list of statements surrounded by curly braces; all expressions are also statements */
-    public Expr parseBlock(boolean requireBraces) throws IOException {
-       Expr ret = null;
-       int tok = peekToken();
-       boolean braced = tok == LC;
-       if (requireBraces && !braced) throw new Error("expected {");
-       if (braced) getToken();
-       Expr head = null;
-       Expr tail = null;
-       OUTER: while(true) {
-           Expr smt;
-           switch(tok = peekToken()) {
-           case -1: break OUTER;
-           case LC: smt = parseBlock(true); break;
-           case THROW: case RETURN: case ASSERT:
-               getToken();
-               smt = new Expr(tok, parseMaximalExpr());
-               if (getToken() != SEMI) throw new Error("expected ;");
-               break;
-           case GOTO: case BREAK: case CONTINUE:
-               getToken();
-               if (getToken() == NAME)
-                   smt = new Expr(tok, new Expr(string));
-               else if (tok == GOTO)
-                   throw new Error("goto must be followed by a label");
-               else
-                   smt = new Expr(tok);
-               if (getToken() != SEMI) throw new Error("expected ;");
-               break;
 
-           case RC:
-               if (braced) getToken();
-               break OUTER;
+    /** parses the largest possible expression */
+    public ForthBlock parseExpr() throws IOException { return parseExpr(null, -1); }
 
-           case SEMI:
-               getToken();
-               if (!braced) break OUTER;
-               continue;
-
-           default:
-               smt = parseMaximalExpr();
-               if (smt == null) {
-                   if (head == null) throw new Error("empty statement list");
-                   break OUTER;
-               }
-               break;
-           }
-           if (head == null) head = tail = smt; else tail = (tail.next = smt);
-       }
-       return new Expr(LC, head);
+    /** return the largest expression beginning with prefix containing no operators of precedence below <tt>minPrecedence</tt> */
+    public ForthBlock parseExpr(ForthBlock prefix, int minPrecedence) throws IOException {
+       return parseExpr(prefix, minPrecedence, new ForthBlock(line, sourceName));
     }
 
-    /** throws an error if the next token is not <tt>code</tt> */
-    public void expect(int code) throws IOException {
-       int got = peekToken();
-       if (got != code)
-           throw new Error("expected " + codeToString[got] + ", got " + (got == -1 ? "EOL" : codeToString[got]));
-    }
+    /** append the largest expression beginning with prefix containing no operators of precedence below <tt>minPrecedence</tt> */
+    public ForthBlock parseExpr(ForthBlock prefix, int minPrecedence, ForthBlock appendTo) throws IOException {
 
-    /** parses the largest possible expression */
-    public Expr parseMaximalExpr() throws IOException { return parseMaximalExpr(null, -1); }
-    public Expr parseMaximalExpr(Expr prefix, int minPrecedence) throws IOException {
-       Expr save = null;
-       do {
-           save = prefix;
-           if (peekToken() == -1) break;
-           prefix = parseSingleExpr(prefix, minPrecedence);
-           if (prefix == null) throw new Error("parseSingleExpr_() returned null");
-       } while (save != prefix);
-       return prefix;
-    }
-
-    /** parses the smallest possible complete expression */
-    public Expr parseSingleExpr() throws IOException { return parseSingleExpr(null, 0); }
+       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; }
 
-    /** parses the smallest possible complete expression beginning with prefix and only using operators with at least minPrecedence */
-    public Expr parseSingleExpr(Expr prefix, int minPrecedence) throws IOException {
-       Expr e1 = null, e2 = null, e3 = null, head = null, tail = null, ret = null;
+       if (prefix != null && mustStartExpression[tok]) { pushBackToken(); return prefix; }
+       if (prefix == null && cannotStartExpression[tok]) { pushBackToken(); return prefix; }
 
-       int tok = peekToken();
-       if (minPrecedence > 0 && tok < precedence.length && precedence[tok] != 0 && precedence[tok] <= minPrecedence) return prefix;
-       getToken();
+       ForthBlock b = appendTo;
 
-       // these case arms match the precedence of operators; each arm is a precedence level.
        switch (tok) {
 
-       case WITH: throw new Error("XWT does not allow the WITH keyword");
-       case VOID: case RESERVED: throw new Error("reserved word that you shouldn't be using");
-       case NAME: if (prefix != null) { pushBackToken(); return prefix; } else return parseMaximalExpr(new Expr(NAME, string), minPrecedence);
-       case STRING: if (prefix != null) { pushBackToken(); return prefix; } else return new Expr(string);
-       case NUMBER: if (prefix != null) { pushBackToken(); return prefix; } else return new Expr(number);
-       case NULL: case TRUE: case FALSE: case NOP: if (prefix != null) { pushBackToken(); return prefix; } else return new Expr(tok);
-
-       case 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: case DOT:
-           return new Expr(tok, prefix, parseMaximalExpr(null, precedence[tok]));
-
-       case BITNOT: case INSTANCEOF:
-           if (prefix != null) throw new Error("didn't expect non-null prefix!");
-           return new Expr(tok, parseMaximalExpr(null, precedence[tok]));
+       case NUMBER: return parseExpr(b.add(ForthBlock.LITERAL, number), minPrecedence);
+       case STRING: return parseExpr(b.add(ForthBlock.LITERAL, string), minPrecedence);
+       case THIS: return parseExpr(b.add(THIS, null), minPrecedence);
+       case NULL: return parseExpr(b.add(ForthBlock.LITERAL, null), minPrecedence);
+       case TRUE: case FALSE: return parseExpr(b.add(ForthBlock.LITERAL, new Boolean(tok == TRUE)), minPrecedence);
+
+        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, parseExpr(null, precedence[tok - 1]));
+           prefix.add(tok - 1);
+           prefix.add(b.PUT);
+           prefix.add(b.SWAP);
+           prefix.add(b.POP);
+           return parseExpr(b, minPrecedence);
+       }
 
        case INC: case DEC:
            if (prefix == null) {
                // prefix
-               return new Expr(tok, parseMaximalExpr(null, precedence[tok]));
+               ForthBlock subexp = parseExpr(null, precedence[tok]);
+               subexp.set(subexp.size() - 1, tok, new Boolean(true));
+               b.add(b.EXPR, subexp);
+               return parseExpr(b, minPrecedence);
            } else {
                // postfix
-               return new Expr(tok, null, prefix);
+               prefix.set(prefix.size() - 1, tok, new Boolean(false));
+               b.add(b.EXPR, prefix);
+               return parseExpr(b, minPrecedence);
            }
 
        case LP:
            if (prefix == null) {  // grouping
-               Expr r = parseMaximalExpr();
-               expect(RP);
-               return r;
+               b.add(EXPR, parseExpr());
+               consume(RP);
+               return parseExpr(b, minPrecedence);
 
            } else {  // invocation
+               int i = 0;
+               b.add(b.EXPR, prefix);
                while(peekToken() != RP) {
-                   Expr e = parseMaximalExpr(null, precedence[COMMA]);
-                   if (head == null) head = tail = e; else tail = tail.next = e;
-                   tok = getToken();
-                   if (tok == RP) { pushBackToken(); break; }
-                   if (tok != COMMA) throw new Error("expected comma or right paren, got " + codeToString[tok]);
+                   b.add(b.EXPR, parseExpr());
+                   i++;
+                   if (peekToken() == RP) break;
+                   consume(COMMA);
                }
-               getToken();
-               return new Expr(LP, prefix, head);
+               consume(RP);
+               b.add(b.CALL, new Integer(i));
+               return parseExpr(b, minPrecedence);
            }
 
-       case LB:
-           if (prefix != null) {
-               // subscripting
-               e1 = parseSingleExpr();
-               if (getToken() != RB) throw new Error("expected a right brace");
-               return new Expr(LB, prefix, e1);
+       case BANG: case BITNOT: case INSTANCEOF: case TYPEOF: {
+           b.add(b.EXPR, parseExpr(null, precedence[tok]));
+           b.add(tok);
+           return parseExpr(b, minPrecedence);
+       }
+
+       case SUB:
+           if (prefix == null) {
+               consume(NUMBER);
+               return parseExpr(b.add(ForthBlock.LITERAL, new Double(number.doubleValue() * -1)), minPrecedence);
+           }
+           // 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: {
+           b.add(b.EXPR, prefix);
+           b.add(b.EXPR, parseExpr(null, precedence[tok]));
+           b.add(tok);
+           return parseExpr(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, parseExpr(null, precedence[tok]));
+           return parseExpr(b, minPrecedence);
+       }
+
+       case NAME: {
+           String name = string;
+           if (peekToken() == ASSIGN) {
+               consume(ASSIGN);
+               b.add(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);
            } else {
-               // array ctor
-               tok = getToken();
+               b.add(THIS);
+               b.add(ForthBlock.LITERAL, name);
+               b.add(ForthBlock.GET);
+           }
+           return parseExpr(b, minPrecedence);
+       }
+
+       case DOT: {
+           consume(NAME);
+           String target = string;
+           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: {
+           if (prefix == null) {
+               b.add(b.ARRAY, new Integer(0));
+               int i = 0;
                while(true) {
-                   if (tok == RB) return new Expr(LB, prefix, head);
-                   if (head == null) head = tail = parseSingleExpr(); else tail = tail.next = parseSingleExpr();
-                   tok = getToken();
-                   if (tok != COMMA && tok != RP) throw new Error("expected right bracket or comma");
+                   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) throw new Error("didn't expect non-null prefix");
-           tok = getToken();
-           while(true) {
-               if (tok == RC) return new Expr(LC, head);
-               if (tok != NAME) throw new Error("expecting name");
-               Expr name = parseSingleExpr();
-               if (tok != COLON) throw new Error("expecting colon");           
-               e1 = new Expr(COLON, name, parseSingleExpr());
-               if (head == null) head = tail = e1; else tail = tail.next = e1;
-               tok = getToken();
-               if (tok != COMMA && tok != RP) throw new Error("expected right curly or comma");
-           }
-           
-       case HOOK:
-           e2 = parseSingleExpr();
-           if (getToken() != COLON) throw new Error("expected colon to close ?: expression");
-           e3 = parseSingleExpr();
-           e2.next = e3;
-           return new Expr(HOOK, prefix, e2);
-           
-       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 = parseSingleExpr();
-           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 LC: {
+           b.add(b.OBJECT, null);
+           if (peekToken() == RC) { consume(RC); return parseExpr(b, minPrecedence); }
            while(true) {
-               if (getToken() != CASE) throw new Error("expected CASE");
-               Expr caseExpr = parseSingleExpr();
-               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, parseExpr());
+               b.add(b.PUT);
+               b.add(b.POP);
+               if (peekToken() == RC) { consume(RC); return parseExpr(b, minPrecedence); }
+               consume(COMMA);
+               if (peekToken() == RC) { consume(RC); return parseExpr(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(NAME, string); cur = cur.next; }
-               tok = getToken();
-               if (tok == RP) break;
-               if (tok != COMMA) throw new Error("function argument list must consist of alternating NAMEs and COMMAs");
-               tok = getToken();
-           }
-           return new Expr(FUNCTION, formalArgs, parseBlock(true));
+       case HOOK: {
+           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 VAR:
-           if (prefix != null) throw new Error("didn't expect non-null prefix");
-           while(true) {
-               if (getToken() != NAME) throw new Error("variable declarations must start with a variable name");
-               Expr name = new Expr(NAME, string);
-               Expr initVal = null;
-               tok = peekToken();
-               Expr e = null;
-               if (tok == ASSIGN) {
-                   getToken();
-                   initVal = parseSingleExpr();
-                   tok = peekToken();
-                   e = new Expr(ASSIGN, name, initVal);
+       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 {
-                   e = new Expr(NAME, name);
+                   consume(NAME);
+
+                   // 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);
                }
-               if (head == null) head = tail = e; else tail = tail.next = e;
-               if (tok != COMMA) break;
-               getToken();
+               numArgs++;
            }
-           return new Expr(VAR, head);
+           // pop off the arguments array
+           b2.add(b.POP);
+           parseStatement(true, b2);
+           b2.add(b.LITERAL, null);
+           b2.add(RETURN);
+           return parseExpr(b.add(OpCodes.FUNCTION, b2), minPrecedence);
+       }
+           
+       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 TRY: {
-           // We deliberately allow you to omit braces in catch{}/finally{} if they are single statements...
-           if (prefix != null) throw new Error("didn't expect non-null prefix");
-           Expr tryBlock = parseBlock(true);
-           while ((tok = peekToken()) == CATCH || tok == FINALLY) {
+           case THROW: case RETURN: case ASSERT: {
                getToken();
-               if (getToken() != LP) throw new Error("expected (");
-               if (getToken() != NAME) throw new Error("expected name");
-               Expr name = new Expr(NAME, string);
-               if (getToken() != RP) throw new Error("expected )");
-               e1 = new Expr(tok, name, parseBlock(false));
-               if (head == null) head = tail = e1; else tail = tail.next = e1;
+               if (tok == RETURN && peekToken() == SEMI) b.add(b.LITERAL, null);
+               else b.add(b.EXPR, parseExpr());
+               consume(SEMI);
+               b.add(tok);
+               break;
            }
-           if (head == null) throw new Error("try without catch or finally");
-           return new Expr(TRY, tryBlock, head);
-       }
 
-       case IF: case WHILE: {
-           if (prefix != null) throw new Error("didn't expect non-null prefix");
-           if (getToken() != LP) throw new Error("expected left paren");
-           Expr parenExpr = parseMaximalExpr(null, -1);
-           int t;
-           if ((t = getToken()) != RP) throw new Error("expected right paren, but got " + codeToString[t]);
-           Expr firstBlock = parseBlock(false);
-           if (tok == IF && peekToken() == ELSE) {
+           case BREAK: case CONTINUE: {
                getToken();
-               firstBlock.next = parseBlock(false);
-               return new Expr(tok, parenExpr, firstBlock);
+               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, parseExpr());
+                       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, parseExpr());
+               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;
            }
-           return new Expr(tok, parenExpr, firstBlock);
-       }
 
-       case IN: return prefix;
-       case FOR:
-           if (prefix != null) throw new Error("didn't expect non-null prefix");
-           if (getToken() != LP) throw new Error("expected left paren");
-           e1 = parseMaximalExpr(null, -1);
-           if (e1.code == NAME && peekToken() == IN) {
-               getToken();
-               e2 = parseMaximalExpr(null, -1);
-               if (getToken() != RP) throw new Error("expected right paren");
-               return new Expr(FOR, new Expr(IN, e1, e2), parseBlock(false));
+           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, 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);
+               break;
+           }
+
+           case SWITCH: {
+               consume(SWITCH);
+               consume(LP);
+               ForthBlock loop = new ForthBlock(curLine, sourceName);
+               b.add(loop.LOOP, loop);
+               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 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, parseExpr());
+               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);
+
+           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, 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());
+               //b2.add(b.LITERAL, null);
+               break;
                
            } else {
-               if (getToken() != SEMI) throw new Error("expected ;");
-               e2 = parseMaximalExpr(null, -1);
-               if (getToken() != SEMI) throw new Error("expected ;");
-               e3 = parseMaximalExpr(null, -1);
-               if (getToken() != RP) throw new Error("expected right paren");
-               return new Expr(LC, e1, new Expr(WHILE, e2, new Expr(LC, parseBlock(false), e3)));
+               ForthBlock b2 = new ForthBlock(curLine, sourceName);
+               b.add(b.SCOPE, b2);
+               b.add(b.POP);
+
+               ForthBlock e1 = parseExpr();
+               if (e1 == null) e1 = new ForthBlock(curLine, sourceName, b.LITERAL, null);
+
+               b2.add(b.EXPR, e1);
+               b2.add(b.POP);
+               consume(SEMI);
+               ForthBlock e2 = parseExpr();
+               consume(SEMI);
+               ForthBlock e3 = parseExpr();
+               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 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 = parseSingleExpr();
-           if (getToken() != RP) throw new Error("expected right paren");
-           if (getToken() != SEMI) throw new Error("semicolon");
-           return new Expr(DO, firstBlock, whileExpr);
        }
            
-       default:
-           pushBackToken();
-           return prefix;
+           case NAME: {
+               consume(NAME);
+               String name = string;
+               if (peekToken() == COLON) {
+                   consume(COLON);
+                   b.add(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 = parseExpr();
+               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); }
+    }
+    
 }