- 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 startExpr(b);
- 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);
- startExpr(b);
- 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);
- startExpr(b);
- consume(RP);
-
- b.add(b.JF, new Integer(0));
- int size = b.size();
- parseStatement(false, b);
-
- if (peekToken() == ELSE) {
- consume(ELSE);
- b.arg[size - 1] = new Integer(2 + b.size() - size);
- b.add(b.JMP, new Integer(3));
- b.add(b.EXPR, parseStatement());
- b.add(b.POP);
- } else {
- b.arg[size - 1] = new Integer(1 + b.size() - size);
- }
- break;
- }
-
- case WHILE: {
- consume(WHILE);
- consume(LP);
- ForthBlock loop = new ForthBlock(curLine, sourceName);
- b.add(loop.LOOP, loop);
-
- loop.add(loop.POP);
- startExpr(loop);
- 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);
- startExpr(loop);
- consume(RP);
- consume(LC);
- while(true)
- if (peekToken() == CASE) {
- consume(CASE);
- loop.add(loop.DUP);
- startExpr(loop);
- consume(COLON);
- loop.add(EQ);
- loop.add(loop.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.arg[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);
- ForthBlock loop = new ForthBlock(curLine, sourceName);
- b.add(loop.LOOP, loop);
-
- parseStatement(false, loop);
- consume(WHILE);
- consume(LP);
- startExpr(loop);
- 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);
- startExpr(b);
- 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());
- break;
-
- } else {
- ForthBlock b2 = new ForthBlock(curLine, sourceName);
- b.add(b.SCOPE, b2);
- b.add(b.POP);
-
- int size = b2.size();
- startExpr(b2);
- if (b2.size() - size > 0) b2.add(b.POP);
- consume(SEMI);
- ForthBlock e2 = startExpr();
- consume(SEMI);
-
- if (e2 == null) e2 = 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(0));
- size = b3.size();
- startExpr(b3);
- consume(RP);
- if (b3.size() - size > 0) b3.add(b.POP);
- b3.arg[size - 1] = new Integer(b3.size() - size + 1);
-
- 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: {
- int size = b.size();
- startExpr(b);
- if (size == b.size()) return;
- b.add(b.POP);
- if (peekToken() == SEMI) consume(SEMI);
- break;
- }
- }
-
- if (!braced) return;
- }
+ void _parseStatement(CompiledFunctionImpl b, String label) throws IOException {
+ int tok = peekToken();
+ if (tok == -1) return;
+ switch(tok = getToken()) {
+
+ case THROW: case ASSERT: case RETURN: {
+ if (tok == RETURN && peekToken() == SEMI)
+ b.add(parserLine, LITERAL, null);
+ else
+ startExpr(b, -1);
+ b.add(parserLine, tok);
+ consume(SEMI);
+ break;
+ }
+ case BREAK: case CONTINUE: {
+ if (peekToken() == NAME) consume(NAME);
+ b.add(parserLine, tok, string);
+ consume(SEMI);
+ break;
+ }
+ case VAR: {
+ b.add(parserLine, TOPSCOPE); // push the current scope
+ while(true) {
+ consume(NAME);
+ String name = string;
+ b.add(parserLine, LITERAL, name); // push the name to be declared
+ b.add(parserLine, DECLARE); // declare it
+ if (peekToken() == ASSIGN) { // if there is an '=' after the variable name
+ b.add(parserLine, LITERAL, name); // put the var name back on the stack
+ consume(ASSIGN);
+ startExpr(b, -1);
+ b.add(parserLine, PUT); // assign it
+ b.add(parserLine, POP); // clean the stack
+ }
+ if (peekToken() != COMMA) break;
+ consume(COMMA);
+ }
+ b.add(parserLine, POP); // pop off the topscope
+ if ((mostRecentlyReadToken != RC || peekToken() == SEMI) && peekToken() != -1) consume(SEMI);
+ break;
+ }
+ case IF: {
+ consume(LP);
+ startExpr(b, -1);
+ consume(RP);
+
+ b.add(parserLine, JF, new Integer(0)); // if false, jump to the else-block
+ int size = b.size();
+ parseStatement(b, null);
+
+ if (peekToken() == ELSE) {
+ consume(ELSE);
+ b.add(parserLine, JMP, new Integer(0)); // if we took the true-block, jump over the else-block
+ b.set(size - 1, new Integer(b.size() - size + 1));
+ size = b.size();
+ parseStatement(b, null);
+ }
+ b.set(size - 1, new Integer(b.size() - size + 1)); // regardless of which branch we took, b[size] needs to point here
+ break;
+ }
+ case WHILE: {
+ consume(LP);
+ if (label != null) b.add(parserLine, LABEL, label);
+ b.add(parserLine, LOOP);
+ int size = b.size();
+ b.add(parserLine, POP); // discard the first-iteration indicator
+ startExpr(b, -1);
+ b.add(parserLine, JT, new Integer(2)); // if the while() clause is true, jump over the BREAK
+ b.add(parserLine, BREAK);
+ consume(RP);
+ parseStatement(b, null);
+ b.add(parserLine, CONTINUE); // if we fall out of the end, definately continue
+ b.set(size - 1, new Integer(b.size() - size + 1)); // end of the loop
+ break;
+ }
+ case SWITCH: {
+ consume(LP);
+ if (label != null) b.add(parserLine, LABEL, label);
+ b.add(parserLine, LOOP);
+ int size0 = b.size();
+ startExpr(b, -1);
+ consume(RP);
+ consume(LC);
+ while(true)
+ if (peekToken() == CASE) { // we compile CASE statements like a bunch of if..else's
+ consume(CASE);
+ b.add(parserLine, DUP); // duplicate the switch() value; we'll consume one copy
+ startExpr(b, -1);
+ consume(COLON);
+ b.add(parserLine, EQ); // check if we should do this case-block
+ b.add(parserLine, JF, new Integer(0)); // if not, jump to the next one
+ int size = b.size();
+ while(peekToken() != CASE && peekToken() != DEFAULT && peekToken() != RC) parseStatement(b, null);
+ b.set(size - 1, new Integer(1 + b.size() - size));
+ } else if (peekToken() == DEFAULT) {
+ consume(DEFAULT);
+ consume(COLON);
+ while(peekToken() != CASE && peekToken() != DEFAULT && peekToken() != RC) parseStatement(b, null);
+ } else if (peekToken() == RC) {
+ consume(RC);
+ b.add(parserLine, BREAK); // break out of the loop if we 'fall through'
+ break;
+ } else {
+ throw new ParserException("expected CASE, DEFAULT, or RC; got " + codeToString[peekToken()]);
+ }
+ b.set(size0 - 1, new Integer(b.size() - size0 + 1)); // end of the loop
+ break;
+ }
+
+ case DO: {
+ if (label != null) b.add(parserLine, LABEL, label);
+ b.add(parserLine, LOOP);
+ int size = b.size();
+ parseStatement(b, null);
+ consume(WHILE);
+ consume(LP);
+ startExpr(b, -1);
+ b.add(parserLine, JT, new Integer(2)); // check the while() clause; jump over the BREAK if true
+ b.add(parserLine, BREAK);
+ b.add(parserLine, CONTINUE);
+ consume(RP);
+ consume(SEMI);
+ b.set(size - 1, new Integer(b.size() - size + 1)); // end of the loop; write this location to the LOOP instruction
+ break;
+ }
+
+ case TRY: {
+ b.add(parserLine, TRY);
+ int size = b.size();
+ parseStatement(b, null); // parse the expression to be TRYed
+ b.add(parserLine, POP); // pop the TryMarker
+ b.add(parserLine, JMP); // jump forward to the end of the catch block
+ int size2 = b.size();
+ b.set(size - 1, new Integer(b.size() - size + 1)); // the TRY argument points at the start of the CATCH block
+
+ if (peekToken() == CATCH) {
+ getToken();
+ consume(LP);
+ consume(NAME);
+ consume(RP);
+ b.add(parserLine, TOPSCOPE); // the exception is on top of the stack; put it to the chosen name
+ b.add(parserLine, SWAP);
+ b.add(parserLine, LITERAL);
+ b.add(parserLine, SWAP);
+ b.add(parserLine, PUT);
+ b.add(parserLine, POP);
+ b.add(parserLine, POP);
+ parseStatement(b, null);
+ }
+
+ // jump here if no exception was thrown
+ b.set(size2 - 1, new Integer(b.size() - size2 + 1));
+
+ // FIXME: not implemented correctly
+ if (peekToken() == FINALLY) {
+ consume(FINALLY);
+ parseStatement(b, null);
+ }
+ break;
+ }
+
+ case FOR: {
+ consume(LP);
+
+ tok = getToken();
+ boolean hadVar = false; // if it's a for..in, we ignore the VAR
+ if (tok == VAR) { hadVar = true; tok = getToken(); }
+ String varName = string;
+ boolean forIn = peekToken() == IN; // determine if this is a for..in loop or not
+ pushBackToken(tok, varName);
+
+ if (forIn) {
+ b.add(parserLine, NEWSCOPE); // for-loops always create new scopes
+ b.add(parserLine, LITERAL, varName); // declare the new variable
+ b.add(parserLine, DECLARE);
+
+ b.add(parserLine, LOOP); // we actually only add this to ensure that BREAK works
+ b.add(parserLine, POP); // discard the first-iteration indicator
+ int size = b.size();
+ consume(NAME);
+ consume(IN);
+ startExpr(b, -1);
+ b.add(parserLine, PUSHKEYS); // push the keys as an array; check the length
+ b.add(parserLine, LITERAL, "length");
+ b.add(parserLine, GET);
+ consume(RP);
+
+ b.add(parserLine, LITERAL, new Integer(1)); // decrement the length
+ b.add(parserLine, SUB);
+ b.add(parserLine, DUP);
+ b.add(parserLine, LITERAL, new Integer(0)); // see if we've exhausted all the elements
+ b.add(parserLine, LT);
+ b.add(parserLine, JF, new Integer(2));
+ b.add(parserLine, BREAK); // if we have, then BREAK
+ b.add(parserLine, GET_PRESERVE); // get the key out of the keys array
+ b.add(parserLine, LITERAL, varName);
+ b.add(parserLine, PUT); // write it to this[varName]
+ parseStatement(b, null); // do some stuff
+ b.add(parserLine, CONTINUE); // continue if we fall out the bottom
+
+ b.set(size - 1, new Integer(b.size() - size + 1)); // BREAK to here
+ b.add(parserLine, OLDSCOPE); // restore the scope
+
+ } else {
+ if (hadVar) pushBackToken(VAR, null); // yeah, this actually matters
+ b.add(parserLine, NEWSCOPE); // grab a fresh scope
+
+ parseStatement(b, null); // initializer
+ CompiledFunctionImpl e2 = // we need to put the incrementor before the test
+ new JS.CompiledFunction(sourceName, parserLine, null, null); // so we save the test here
+ if (peekToken() != SEMI)
+ startExpr(e2, -1);
+ else
+ e2.add(parserLine, b.LITERAL, Boolean.TRUE); // handle the for(foo;;foo) case
+ consume(SEMI);
+ if (label != null) b.add(parserLine, LABEL, label);
+ b.add(parserLine, LOOP);
+ int size2 = b.size();
+
+ b.add(parserLine, JT, new Integer(0)); // if we're on the first iteration, jump over the incrementor
+ int size = b.size();
+ if (peekToken() != RP) { // do the increment thing
+ startExpr(b, -1);
+ b.add(parserLine, POP);
+ }
+ b.set(size - 1, new Integer(b.size() - size + 1));
+ consume(RP);
+
+ b.paste(e2); // ok, *now* test if we're done yet
+ b.add(parserLine, JT, new Integer(2)); // break out if we don't meet the test
+ b.add(parserLine, BREAK);
+ parseStatement(b, null);
+ b.add(parserLine, CONTINUE); // if we fall out the bottom, CONTINUE
+ b.set(size2 - 1, new Integer(b.size() - size2 + 1)); // end of the loop
+
+ b.add(parserLine, OLDSCOPE); // get our scope back
+ }
+ break;
+ }
+
+ case NAME: { // either a label or an identifier; this is the one place we're not LL(1)
+ String possiblyTheLabel = string;
+ if (peekToken() == COLON) { // label
+ consume(COLON);
+ parseStatement(b, possiblyTheLabel);
+ break;
+ } else { // expression
+ pushBackToken(NAME, possiblyTheLabel);
+ startExpr(b, -1);
+ b.add(parserLine, POP);
+ if ((mostRecentlyReadToken != RC || peekToken() == SEMI) && peekToken() != -1) consume(SEMI);
+ break;
+ }
+ }
+
+ case SEMI: return; // yep, the null statement is valid
+
+ case LC: { // blocks are statements too
+ pushBackToken();
+ b.add(parserLine, NEWSCOPE);
+ parseBlock(b, label);
+ b.add(parserLine, OLDSCOPE);
+ break;
+ }
+
+ default: { // hope that it's an expression
+ pushBackToken();
+ startExpr(b, -1);
+ b.add(parserLine, POP);
+ if ((mostRecentlyReadToken != RC || peekToken() == SEMI) && peekToken() != -1) consume(SEMI);
+ break;
+ }
+ }