- int tok = peekToken();
- if (tok == -1) return;
- switch(tok = getToken()) {
-
- case THROW: case ASSERT: case RETURN: {
- if (tok == RETURN && peekToken() == SEMI) b.add(line, LITERAL, null);
- else startExpr(b, -1);
- b.add(line, tok);
- consume(SEMI);
- break;
- }
-
- case BREAK: case CONTINUE: {
- if (peekToken() == NAME) consume(NAME);
- b.add(line, tok, string);
- consume(SEMI);
- break;
- }
-
- case VAR: {
- b.add(line, TOPSCOPE); // push the current scope
- while(true) {
- consume(NAME);
- String name = string;
- b.add(line, LITERAL, name); // push the name to be declared
- b.add(line, DECLARE); // declare it
- if (peekToken() == ASSIGN) { // if there is an '=' after the variable name
- b.add(line, LITERAL, name); // put the var name back on the stack
- consume(ASSIGN);
- startExpr(b, -1);
- b.add(line, PUT);
- b.add(line, POP);
- }
- if (peekToken() != COMMA) break;
- consume(COMMA);
- }
- b.add(line, POP);
- if ((mostRecentlyReadToken != RC || peekToken() == SEMI) && peekToken() != -1) consume(SEMI);
- break;
- }
-
- case IF: {
- consume(LP);
- startExpr(b, -1);
- consume(RP);
-
- b.add(line, JF, new Integer(0));
- int size = b.size();
- parseStatement(b, null);
-
- if (peekToken() == ELSE) {
- consume(ELSE);
- b.set(size - 1, new Integer(2 + b.size() - size));
- b.add(line, JMP, new Integer(0));
- size = b.size();
- parseStatement(b, null);
- }
- b.set(size - 1, new Integer(1 + b.size() - size));
- break;
- }
-
- case WHILE: {
- consume(LP);
- if (label != null) b.add(line, LABEL, label);
- b.add(line, LOOP);
- int size = b.size();
- b.add(line, POP);
- startExpr(b, -1);
- b.add(line, JT, new Integer(2));
- b.add(line, BREAK);
- consume(RP);
- parseStatement(b, null);
- b.add(line, 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(line, LABEL, label);
- b.add(line, LOOP);
- int size0 = b.size();
- startExpr(b, -1);
- consume(RP);
- consume(LC);
- while(true)
- if (peekToken() == CASE) {
- consume(CASE);
- b.add(line, DUP);
- startExpr(b, -1);
- consume(COLON);
- b.add(line, EQ);
- b.add(line, JF, new Integer(0));
- int size = b.size();
- while(peekToken() != CASE && peekToken() != DEFAULT && peekToken() != RC) {
- int size2 = b.size();
- parseStatement(b, null);
- if (size2 == b.size()) break;
- }
- 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) {
- int size2 = b.size();
- parseStatement(b, null);
- if (size2 == b.size()) break;
- }
- } else if (peekToken() == RC) {
- consume(RC);
- b.add(line, BREAK);
- break;
- } else {
- throw new ParserException("expected CASE, DEFAULT, or RC; got " + codeToString[peekToken()]);
- }
- b.add(line, BREAK);
- b.set(size0 - 1, new Integer(b.size() - size0 + 1)); // end of the loop
- break;
- }
-
- case DO: {
- if (label != null) b.add(line, LABEL, label);
- b.add(line, LOOP);
- int size = b.size();
- parseStatement(b, null);
- consume(WHILE);
- consume(LP);
- startExpr(b, -1);
- b.add(line, JT, new Integer(2));
- b.add(line, BREAK);
- b.add(line, CONTINUE);
- consume(RP);
- consume(SEMI);
- b.set(size - 1, new Integer(b.size() - size + 1)); // end of the loop
- break;
- }
-
- case TRY: {
- // We deliberately allow you to omit braces in catch{}/finally{} if they are single statements...
- b.add(line, TRY);
- int size = b.size();
- parseBlock(b, null);
- b.add(line, POP); // pop the TryMarker
- b.add(line, 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);
- // FIXME, we need an extra scope here
- b.add(line, TOPSCOPE); // the exception is on top of the stack; put it to the variable
- b.add(line, SWAP);
- b.add(line, LITERAL);
- b.add(line, SWAP);
- b.add(line, PUT);
- b.add(line, POP);
- b.add(line, POP);
- parseStatement(b, null);
- }
-
- b.set(size2 - 1, new Integer(b.size() - size2 + 1)); // jump here if no exception was thrown
-
- // FIXME: not implemented correctly
- if (peekToken() == FINALLY) {
- consume(FINALLY);
- parseStatement(b, null);
- }
- break;
- }
-
- case 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) {
- // FIXME: break needs to work in here
- consume(NAME);
- consume(IN);
- startExpr(b, -1);
- b.add(line, PUSHKEYS);
- b.add(line, LITERAL, "length");
- b.add(line, GET);
- consume(RP);
- CompiledFunction b2 = new CompiledFunction(sourceName, line, null);
-
- b.add(line, NEWSCOPE);
-
- b.add(line, LITERAL, new Integer(1));
- b.add(line, SUB);
- b.add(line, DUP);
- b.add(line, LITERAL, new Integer(0));
- b.add(line, LT);
- b.add(line, JT, new Integer(7));
- b.add(line, GET_PRESERVE);
- b.add(line, LITERAL, varName);
- b.add(line, LITERAL, varName);
- b.add(line, DECLARE);
- b.add(line, PUT);
- parseStatement(b, null);
-
- b.add(line, OLDSCOPE);
-
- break;
-
- } else {
- if (hadVar) pushBackToken(VAR, null);
- b.add(line, NEWSCOPE);
-
- parseStatement(b, null);
- CompiledFunction e2 = new CompiledFunction(sourceName, line, null);
- if (peekToken() != SEMI) {
- startExpr(e2, -1);
- } else {
- e2.add(line, b.LITERAL, Boolean.TRUE);
- }
- consume(SEMI);
- if (label != null) b.add(line, LABEL, label);
- b.add(line, LOOP);
- int size2 = b.size();
-
- b.add(line, JT, new Integer(0));
- int size = b.size();
- if (peekToken() != RP) {
- startExpr(b, -1);
- b.add(line, POP);
- }
- b.set(size - 1, new Integer(b.size() - size + 1));
- consume(RP);
-
- b.paste(e2);
- b.add(line, JT, new Integer(2));
- b.add(line, BREAK);
- parseStatement(b, null);
- b.add(line, CONTINUE);
- b.set(size2 - 1, new Integer(b.size() - size2 + 1)); // end of the loop
-
- b.add(line, OLDSCOPE);
- break;
- }
- }
-
- case NAME: {
- String possiblyTheLabel = string;
- if (peekToken() == COLON) {
- consume(COLON);
- label = possiblyTheLabel;
- parseStatement(b, label);
- break;
- } else {
- pushBackToken(NAME, possiblyTheLabel);
- startExpr(b, -1);
- b.add(line, POP);
- if ((mostRecentlyReadToken != RC || peekToken() == SEMI) && peekToken() != -1) consume(SEMI);
- break;
- }
- }
-
- case SEMI: return;
- case LC: {
- pushBackToken();
- parseBlock(b, label);
- break;
- }
-
- default: {
- pushBackToken();
- startExpr(b, -1);
- b.add(line, POP);
- if ((mostRecentlyReadToken != RC || peekToken() == SEMI) && peekToken() != -1) consume(SEMI);
- break;
- }
- }
+ int tok = peekToken();
+ if (tok == -1) return;
+ switch(tok = getToken()) {
+
+ case THROW: case ASSERT: case RETURN: {
+ if (tok == RETURN && peekToken() == SEMI)
+ b.add(line, LITERAL, null);
+ else
+ startExpr(b, -1);
+ b.add(line, tok);
+ consume(SEMI);
+ break;
+ }
+ case BREAK: case CONTINUE: {
+ if (peekToken() == NAME) consume(NAME);
+ b.add(line, tok, string);
+ consume(SEMI);
+ break;
+ }
+ case VAR: {
+ b.add(line, TOPSCOPE); // push the current scope
+ while(true) {
+ consume(NAME);
+ String name = string;
+ b.add(line, LITERAL, name); // push the name to be declared
+ b.add(line, DECLARE); // declare it
+ if (peekToken() == ASSIGN) { // if there is an '=' after the variable name
+ b.add(line, LITERAL, name); // put the var name back on the stack
+ consume(ASSIGN);
+ startExpr(b, -1);
+ b.add(line, PUT); // assign it
+ b.add(line, POP); // clean the stack
+ }
+ if (peekToken() != COMMA) break;
+ consume(COMMA);
+ }
+ b.add(line, 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(line, 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(line, 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(line, LABEL, label);
+ b.add(line, LOOP);
+ int size = b.size();
+ b.add(line, POP); // discard the first-iteration indicator
+ startExpr(b, -1);
+ b.add(line, JT, new Integer(2)); // if the while() clause is true, jump over the BREAK
+ b.add(line, BREAK);
+ consume(RP);
+ parseStatement(b, null);
+ b.add(line, 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(line, LABEL, label);
+ b.add(line, 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(line, DUP); // duplicate the switch() value; we'll consume one copy
+ startExpr(b, -1);
+ consume(COLON);
+ b.add(line, EQ); // check if we should do this case-block
+ b.add(line, 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(line, 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(line, LABEL, label);
+ b.add(line, LOOP);
+ int size = b.size();
+ parseStatement(b, null);
+ consume(WHILE);
+ consume(LP);
+ startExpr(b, -1);
+ b.add(line, JT, new Integer(2)); // check the while() clause; jump over the BREAK if true
+ b.add(line, BREAK);
+ b.add(line, 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(line, TRY);
+ int size = b.size();
+ parseStatement(b, null); // parse the expression to be TRYed
+ b.add(line, POP); // pop the TryMarker
+ b.add(line, 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);
+ // FIXME, we need an extra scope here
+ b.add(line, TOPSCOPE); // the exception is on top of the stack; put it to the chosen name
+ b.add(line, SWAP);
+ b.add(line, LITERAL);
+ b.add(line, SWAP);
+ b.add(line, PUT);
+ b.add(line, POP);
+ b.add(line, 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(line, NEWSCOPE);
+ b.add(line, LITERAL, varName); // declare the new variable
+ b.add(line, DECLARE);
+
+ b.add(line, LOOP); // we actually only add this to ensure that BREAK works
+ b.add(line, POP); // discard the first-iteration indicator
+ int size = b.size();
+ consume(NAME);
+ consume(IN);
+ startExpr(b, -1);
+ b.add(line, PUSHKEYS); // push the keys as an array; check the length
+ b.add(line, LITERAL, "length");
+ b.add(line, GET);
+ consume(RP);
+
+ b.add(line, LITERAL, new Integer(1)); // decrement the length
+ b.add(line, SUB);
+ b.add(line, DUP);
+ b.add(line, LITERAL, new Integer(0)); // see if we've exhausted all the elements
+ b.add(line, LT);
+ b.add(line, JF, new Integer(2));
+ b.add(line, BREAK); // if we have, then BREAK
+ b.add(line, GET_PRESERVE); // get the key out of the keys array
+ b.add(line, LITERAL, varName);
+ b.add(line, PUT); // write it to this[varName]
+ parseStatement(b, null); // do some stuff
+ b.add(line, CONTINUE); // continue if we fall out the bottom
+
+ b.set(size - 1, new Integer(b.size() - size + 1)); // BREAK to here
+ b.add(line, OLDSCOPE); // restore the scope
+
+ } else {
+ if (hadVar) pushBackToken(VAR, null); // yeah, this actually matters
+ b.add(line, NEWSCOPE); // grab a fresh scope
+
+ parseStatement(b, null); // initializer
+ CompiledFunction e2 = // we need to put the incrementor before the test
+ new CompiledFunction(sourceName, line, null); // so we save the test here
+ if (peekToken() != SEMI)
+ startExpr(e2, -1);
+ else
+ e2.add(line, b.LITERAL, Boolean.TRUE); // handle the for(foo;;foo) case
+ consume(SEMI);
+ if (label != null) b.add(line, LABEL, label);
+ b.add(line, LOOP);
+ int size2 = b.size();
+
+ b.add(line, 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(line, 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(line, JT, new Integer(2)); // break out if we don't meet the test
+ b.add(line, BREAK);
+ parseStatement(b, null);
+ b.add(line, CONTINUE); // if we fall out the bottom, CONTINUE
+ b.set(size2 - 1, new Integer(b.size() - size2 + 1)); // end of the loop
+
+ b.add(line, 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(line, 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();
+ parseBlock(b, label);
+ break;
+ }
+
+ default: { // hope that it's an expression
+ pushBackToken();
+ startExpr(b, -1);
+ b.add(line, POP);
+ if ((mostRecentlyReadToken != RC || peekToken() == SEMI) && peekToken() != -1) consume(SEMI);
+ break;
+ }
+ }