+// 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 {
+/** Parses a stream of lexed tokens into a tree of ByteCodeBlock's */
+class Parser extends Lexer implements ByteCodes {
- public static void main(String[] s) throws Exception { System.out.println(new Parser(new InputStreamReader(System.in)).parseExpr()); }
- 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; }
- }
-
- /** 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();
- }
+ public Parser(Reader r, String sourceName, int line) throws IOException { super(r, sourceName, line); }
- 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) {
+ ByteCodeBlock block = new ByteCodeBlock(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] = 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) {
+ private ByteCodeBlock newbb(int line) { return new ByteCodeBlock(line, sourceName); }
- 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());
+ /** 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 ? "EOF" : codeToString[op]));
+ }
- 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();
+ /** append the largest expression beginning with prefix containing no operators of precedence below <tt>minPrecedence</tt> */
+ public ByteCodeBlock startExpr() throws IOException { return startExpr(-1); }
+ public void startExpr(ByteCodeBlock block) throws IOException { startExpr(-1, block); }
+ public ByteCodeBlock startExpr(int minPrecedence) throws IOException {
+ ByteCodeBlock ret = new ByteCodeBlock(line, sourceName);
+ startExpr(minPrecedence, ret);
+ return ret.size() == 0 ? null : ret;
+ }
+
+ public void startExpr(int minPrecedence, ByteCodeBlock appendTo) throws IOException {
+ int tok = getToken();
+ int curLine = line;
+ if (tok == -1) return;
+
+ ByteCodeBlock b = appendTo;
+
+ switch (tok) {
+ case NUMBER: continueExpr(b.add(ByteCodeBlock.LITERAL, number), minPrecedence); return;
+ case STRING: continueExpr(b.add(ByteCodeBlock.LITERAL, string), minPrecedence); return;
+ case THIS: continueExpr(b.add(TOPSCOPE, null), minPrecedence); return;
+ case NULL: continueExpr(b.add(ByteCodeBlock.LITERAL, null), minPrecedence); return;
+ case TRUE: case FALSE: continueExpr(b.add(ByteCodeBlock.LITERAL, new Boolean(tok == TRUE)), minPrecedence); return;
+ case LB: {
+ b.add(ARRAY, new Integer(0));
+ int i = 0;
while(true) {
- 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");
+ int size = b.size();
+ startExpr(b);
+ if (size == b.size())
+ if (peekToken() == RB) { consume(RB); continueExpr(b, minPrecedence); return; }
+ b.add(LITERAL, new Integer(i++));
+ if (size == b.size()) b.add(LITERAL, null);
+ b.add(PUT);
+ b.add(POP);
+ if (peekToken() == RB) { consume(RB); continueExpr(b, minPrecedence); return; }
+ consume(COMMA);
}
-
- case 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;
- 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);
+ }
+ case SUB: {
+ consume(NUMBER);
+ b.add(ByteCodeBlock.LITERAL, new Double(number.doubleValue() * -1));
+ continueExpr(b, minPrecedence);
+ return;
+ }
+ case LP: {
+ startExpr(b);
+ consume(RP);
+ continueExpr(b, minPrecedence);
+ return;
+ }
+ case INC: case DEC: {
+ // prefix
+ startExpr(precedence[tok], b);
+ b.set(b.size() - 1, tok, new Boolean(true));
+ continueExpr(b, minPrecedence);
+ return;
+ }
+ case BANG: case BITNOT: case TYPEOF: {
+ startExpr(precedence[tok], b);
+ b.add(tok);
+ continueExpr(b, minPrecedence);
+ return;
+ }
+ case LC: {
+ b.add(OBJECT, null);
+ if (peekToken() != RC) while(true) {
+ if (peekToken() != NAME && peekToken() != STRING) throw new Error("expected NAME or STRING");
+ getToken();
+ b.add(LITERAL, string);
+ consume(COLON);
+ startExpr(b);
+ b.add(PUT);
+ b.add(POP);
+ if (peekToken() == RC) break;
+ consume(COMMA);
+ if (peekToken() == RC) break;
}
+ consume(RC);
+ continueExpr(b, minPrecedence);
+ return;
}
-
- 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(TOPSCOPE);
+ b.add(ByteCodeBlock.LITERAL, name);
+ startExpr(minPrecedence, b);
+ b.add(ByteCodeBlock.PUT);
+ b.add(ByteCodeBlock.SWAP);
+ b.add(ByteCodeBlock.POP);
+ } else {
+ b.add(TOPSCOPE);
+ b.add(ByteCodeBlock.LITERAL, name);
+ b.add(ByteCodeBlock.GET);
}
- return new Expr(tok, formalArgs, parseBlock(true));
+ continueExpr(b, minPrecedence);
+ return;
}
-
- case STRING:
- return new Expr(string);
+ case FUNCTION: {
+ consume(LP);
+ int numArgs = 0;
+ ByteCodeBlock b2 = newbb(curLine);
+ b2.add(TOPSCOPE);
+ b2.add(SWAP);
+ b2.add(LITERAL, "arguments");
+ b2.add(LITERAL, "arguments");
+ b2.add(DECLARE);
+ b2.add(SWAP);
+ b2.add(PUT);
+ b2.add(SWAP);
+ b2.add(POP);
+ if (peekToken() == RP) consume(RP);
+ else while(true) {
+ if (peekToken() == COMMA) {
+ consume(COMMA);
- case NUMBER:
- return new Expr(number);
+ } else {
+ consume(NAME);
- 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();
+ // declare the name
+ b2.add(LITERAL, string);
+ b2.add(DECLARE);
+
+ // retrieve it from the arguments array
+ b2.add(LITERAL, new Integer(numArgs));
+ b2.add(GET_PRESERVE);
+ b2.add(SWAP);
+ b2.add(POP);
+
+ // put it to the current scope
+ b2.add(TOPSCOPE);
+ b2.add(SWAP);
+ b2.add(LITERAL, string);
+ b2.add(SWAP);
+ b2.add(PUT);
+
+ // clean the stack
+ b2.add(POP);
+ b2.add(POP);
+
+ if (peekToken() == RP) { consume(RP); break; }
+ consume(COMMA);
}
- 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(POP);
+ parseStatement(true, b2);
+ b2.add(LITERAL, null);
+ b2.add(RETURN);
+ continueExpr(b.add(NEWFUNCTION, b2), minPrecedence);
+ return;
}
-
- 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;
+ }
+ }
+
+ public void continueExpr(ByteCodeBlock prefix, int minPrecedence) throws IOException {
+ int tok = getToken();
+ int curLine = line;
+ if (tok == -1) return;
+ if (minPrecedence > 0 && precedence[tok] != 0)
+ if (precedence[tok] < minPrecedence || (precedence[tok] == minPrecedence && !isRightAssociative[tok]))
+ { pushBackToken(); return; }
+
+ if (prefix == null) throw new Error("got null prefix");
+ ByteCodeBlock b = prefix;
+
+ switch (tok) {
+
+ case ASSIGN_BITOR: case ASSIGN_BITXOR: case ASSIGN_BITAND: case ASSIGN_LSH: case ASSIGN_RSH: case ASSIGN_URSH:
+ case ASSIGN_ADD: case ASSIGN_SUB: case ASSIGN_MUL: case ASSIGN_DIV: case ASSIGN_MOD: {
+ prefix.set(prefix.size() - 1, b.GET_PRESERVE, new Boolean(true));
+ startExpr(precedence[tok - 1], b);
+ prefix.add(tok - 1);
+ prefix.add(PUT);
+ prefix.add(SWAP);
+ prefix.add(POP);
+ continueExpr(b, minPrecedence);
+ return;
}
- case 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);
+ case INC: case DEC: {
+ // postfix
+ b.set(b.size() - 1, tok, new Boolean(false));
+ continueExpr(b, minPrecedence);
+ return;
+ }
+
+ case LP: {
+ // invocation
+ int i = 0;
+ while(peekToken() != RP) {
+ i++;
+ startExpr(b);
+ if (peekToken() == RP) break;
+ consume(COMMA);
+ }
+ consume(RP);
+ b.add(CALL, new Integer(i));
+ continueExpr(b, minPrecedence);
+ return;
+ }
+
+ case BITOR: case BITXOR: case BITAND: case SHEQ: case SHNE: case LSH:
+ case RSH: case URSH: case ADD: case MUL: case DIV: case MOD:
+ case GT: case GE: case EQ: case NE: case LT: case LE: case SUB: {
+ startExpr(precedence[tok], b);
+ b.add(tok);
+ continueExpr(b, minPrecedence);
+ return;
+ }
+
+ case OR: case AND: {
+ b.add(tok == AND ? b.JF : b.JT, new Integer(0));
+ int size = b.size();
+ startExpr(precedence[tok], b);
+ b.set(size - 1, new Integer(b.size() - size + 2));
+ b.add(JMP, new Integer(2));
+ b.add(LITERAL, tok == AND ? new Boolean(false) : new Boolean(true));
+ continueExpr(b, minPrecedence);
+ return;
+ }
+
+ case DOT: {
+ consume(NAME);
+ String target = string;
+ if (peekToken() == ASSIGN) {
+ consume(ASSIGN);
+ b.add(LITERAL, target);
+ startExpr(b);
+ b.add(PUT);
+ b.add(SWAP);
+ b.add(POP);
+ } else {
+ b.add(LITERAL, target);
+ b.add(GET);
+ }
+ continueExpr(b, minPrecedence);
+ return;
+ }
+
+ case LB: {
+ startExpr(b);
+ consume(RB);
+ if (peekToken() == ASSIGN) {
+ consume(ASSIGN);
+ startExpr(b);
+ b.add(PUT);
+ b.add(SWAP);
+ b.add(POP);
+ } else {
+ b.add(GET);
+ }
+ continueExpr(b, minPrecedence);
+ return;
+ }
+
+ case HOOK: {
+ b.add(JF, new Integer(0));
+ int size = b.size();
+ startExpr(b);
+ b.set(size - 1, new Integer(b.size() - size + 2));
+ b.add(JMP, new Integer(0));
+ consume(COLON);
+ size = b.size();
+ startExpr(b);
+ b.set(size - 1, new Integer(b.size() - size + 1));
+ continueExpr(b, minPrecedence);
+ return;
}
- case VOID: case RESERVED:
- throw new Error("reserved word that you shouldn't be using");
+ default: { pushBackToken(); return; }
+ }
+ }
+
+ /** a block is either a single statement or a list of statements surrounded by curly braces; all expressions are also statements */
+ public ByteCodeBlock parseStatement() throws IOException {
+ ByteCodeBlock ret = new ByteCodeBlock(line, sourceName);
+ ret.add(ret.LITERAL, null);
+ parseStatement(false, ret);
+ if (ret.size() == 1) return null;
+ return ret;
+ }
- case WITH:
- throw new Error("WITH not yet implemented"); // FIXME
+ public void parseStatement(boolean requireBraces) throws IOException {
+ parseStatement(requireBraces, new ByteCodeBlock(line, sourceName));
+ }
+ public void parseStatement(boolean requireBraces, ByteCodeBlock b) throws IOException {
+ int tok = peekToken();
+ if (tok == -1) return;
+ boolean braced = tok == LC;
+ if (requireBraces && !braced) throw new ParserException("expected {, got " + codeToString[tok]);
+ if (braced) consume(LC);
+ int curLine = line;
+ while(true) {
+ switch(tok = peekToken()) {
+
+ case THROW: case RETURN: case ASSERT: {
+ getToken();
+ if (tok == RETURN && peekToken() == SEMI) b.add(LITERAL, null);
+ else startExpr(b);
+ consume(SEMI);
+ b.add(tok);
+ break;
+ }
- default:
- pushBackToken();
- return null;
+ case BREAK: case CONTINUE: {
+ getToken();
+ if (peekToken() == NAME) consume(NAME);
+ b.add(tok, string);
+ consume(SEMI);
+ break;
+ }
+
+ case SEMI:
+ consume(SEMI);
+ if (!braced) return;
+ break;
+
+ case VAR: {
+ consume(VAR);
+ b.add(TOPSCOPE); // push the current scope
+ while(true) {
+ consume(NAME);
+ String name = string;
+ b.add(LITERAL, name); // push the name to be declared
+ b.add(DECLARE); // declare it
+ if (peekToken() == ASSIGN) { // if there is an '=' after the variable name
+ b.add(LITERAL, name); // put the var name back on the stack
+ consume(ASSIGN);
+ startExpr(b);
+ b.add(PUT);
+ b.add(POP);
+ }
+ if (peekToken() != COMMA) break;
+ consume(COMMA);
+ }
+ b.add(POP);
+ if (peekToken() == SEMI) consume(SEMI);
+ break;
+ }
+
+ case IF: {
+ consume(IF);
+ consume(LP);
+ startExpr(b);
+ consume(RP);
+
+ b.add(JF, new Integer(0));
+ int size = b.size();
+ parseStatement(false, b);
+
+ if (peekToken() == ELSE) {
+ consume(ELSE);
+ b.set(size - 1, new Integer(2 + b.size() - size));
+ b.add(JMP, new Integer(0));
+ size = b.size();
+ parseStatement(false, b);
+ }
+ b.set(size - 1, new Integer(1 + b.size() - size));
+ break;
+ }
+
+ case WHILE: {
+ consume(WHILE);
+ consume(LP);
+ ByteCodeBlock loop = newbb(curLine);
+ b.add(LOOP, loop);
+
+ loop.add(POP);
+ startExpr(loop);
+ loop.add(JT, new Integer(2));
+ loop.add(BREAK);
+ consume(RP);
+ parseStatement(false, loop);
+
+ // if we fall out of the end, definately continue
+ loop.add(CONTINUE);
+ break;
+ }
+
+ case SWITCH: {
+ consume(SWITCH);
+ consume(LP);
+ ByteCodeBlock loop = newbb(curLine);
+ b.add(LOOP, loop);
+ startExpr(loop);
+ consume(RP);
+ consume(LC);
+ while(true)
+ if (peekToken() == CASE) {
+ consume(CASE);
+ loop.add(DUP);
+ startExpr(loop);
+ consume(COLON);
+ loop.add(EQ);
+ loop.add(JF, new Integer(0));
+ int size = loop.size();
+ while(peekToken() != CASE && peekToken() != DEFAULT && peekToken() != RC) {
+ int size2 = loop.size();
+ parseStatement(false, loop);
+ if (size2 == loop.size()) break;
+ }
+ loop.set(size - 1, new Integer(1 + loop.size() - size));
+ } else if (peekToken() == DEFAULT) {
+ consume(DEFAULT);
+ consume(COLON);
+ while(peekToken() != CASE && peekToken() != DEFAULT && peekToken() != RC) {
+ int size2 = loop.size();
+ parseStatement(false, loop);
+ if (size2 == loop.size()) break;
+ }
+ } else if (peekToken() == RC) {
+ consume(RC);
+ loop.add(BREAK);
+ break;
+ } else {
+ throw new ParserException("expected CASE, DEFAULT, or RC; got " + codeToString[peekToken()]);
+ }
+ break;
+ }
+
+ case DO: {
+ consume(DO);
+ ByteCodeBlock loop = newbb(curLine);
+ b.add(LOOP, loop);
+
+ parseStatement(false, loop);
+ consume(WHILE);
+ consume(LP);
+ startExpr(loop);
+ loop.add(JT, new Integer(2));
+ loop.add(BREAK);
+ loop.add(CONTINUE);
+ consume(RP);
+ consume(SEMI);
+ break;
+ }
+
+ case TRY: {
+ // We deliberately allow you to omit braces in catch{}/finally{} if they are single statements...
+ consume(TRY);
+ parseStatement(true, b);
+
+ if (peekToken() == CATCH) {
+ getToken();
+ consume(LP);
+ consume(NAME);
+ consume(RP);
+ parseStatement(); // just discard the catchblock
+ }
+
+ if (peekToken() == FINALLY) {
+ consume(FINALLY);
+ parseStatement(false, b);
+ }
+ break;
+ }
+
+ case FOR: {
+ consume(FOR);
+ consume(LP);
+
+ tok = getToken();
+ boolean hadVar = false;
+ if (tok == VAR) { hadVar = true; tok = getToken(); }
+ String varName = string;
+ boolean forIn = peekToken() == IN;
+ pushBackToken(tok, varName);
+
+ if (forIn) {
+ consume(NAME);
+ consume(IN);
+ startExpr(b);
+ b.add(PUSHKEYS);
+ b.add(LITERAL, "length");
+ b.add(GET);
+ consume(RP);
+ ByteCodeBlock b2 = newbb(curLine);
+ b.add(SCOPE, b2);
+ b2.add(LITERAL, new Integer(1));
+ b2.add(SUB);
+ b2.add(DUP);
+ b2.add(LITERAL, new Integer(0));
+ b2.add(LT);
+ b2.add(JT, new Integer(7));
+ b2.add(GET_PRESERVE);
+ b2.add(LITERAL, varName);
+ b2.add(LITERAL, varName);
+ b2.add(DECLARE);
+ b2.add(PUT);
+ parseStatement(false, b2);
+ break;
+
+ } else {
+ if (hadVar) pushBackToken(VAR, null);
+ ByteCodeBlock b2 = newbb(curLine);
+ b.add(SCOPE, b2);
+ b.add(POP);
+
+ int size = b2.size();
+ parseStatement(false, b2);
+ ByteCodeBlock e2 = startExpr();
+ consume(SEMI);
+ if (e2 == null) e2 = newbb(curLine).add(b.LITERAL, null);
+
+ ByteCodeBlock b3 = newbb(curLine);
+ b2.add(LOOP, b3);
+ b2.add(LITERAL, null);
+
+ b3.add(JT, new Integer(0));
+ size = b3.size();
+ startExpr(b3);
+ consume(RP);
+ if (b3.size() - size > 0) b3.add(POP);
+ b3.set(size - 1, new Integer(b3.size() - size + 1));
+
+ b3.paste(e2);
+ b3.add(JT, new Integer(2));
+ b3.add(BREAK);
+ parseStatement(false, b3);
+ b3.add(CONTINUE);
+ break;
+ }
}
+
+ case NAME: {
+ consume(NAME);
+ String name = string;
+ if (peekToken() == COLON) {
+ consume(COLON);
+ b.add(ByteCodeBlock.LABEL, string);
+ break;
+ } else {
+ pushBackToken(NAME, name);
+ // fall through to default case
+ }
+ }
+ // fall through
+ case RC:
+ if (tok == RC && braced) { consume(RC); return; }
+ // fall through
+ default: {
+ int size = b.size();
+ startExpr(b);
+ if (size == b.size()) return;
+ b.add(POP);
+ if (peekToken() == SEMI) consume(SEMI);
+ break;
+ }
+ }
+
+ if (!braced) return;
+ }
+ }
+
+ class ParserException extends RuntimeException {
+ public ParserException(String s) { super(sourceName + ":" + line + " " + s); }
}
}