// attempt to continue the expression
continueExpr(b, minPrecedence);
}
-
+ /*
private Grammar parseGrammar(Grammar g) throws IOException {
int tok = getToken();
if (g != null)
if (g == null) return parseGrammar(g0);
return parseGrammar(new Grammar.Juxtaposition(g, g0));
}
-
+ */
/**
* Assuming that a complete assignable (lvalue) has just been
* parsed and the object and key are on the stack,
// force the default case
tok = -1;
switch(tok) {
-
+ /*
case GRAMMAR: {
b.add(parserLine, GET_PRESERVE);
Grammar g = parseGrammar(null);
b.add(parserLine, PUT);
break;
}
-
+ */
case ASSIGN_BITOR: case ASSIGN_BITXOR: case ASSIGN_BITAND: case ASSIGN_LSH: case ASSIGN_RSH: case ASSIGN_URSH:
case ASSIGN_MUL: case ASSIGN_DIV: case ASSIGN_MOD: case ASSIGN_ADD: case ASSIGN_SUB: case ADD_TRAP: case DEL_TRAP: {
if (tok != ADD_TRAP && tok != DEL_TRAP) b.add(parserLine, GET_PRESERVE);
b.add(parserLine, SWAP, null);
b.add(parserLine, POP, null);
b.add(parserLine, LITERAL, JS.N(1));
- b.add(parserLine, tok == INC ? SUB : ADD, null); // undo what we just did, since this is postfix
+ b.add(parserLine, tok == INC ? SUB : ADD, JS.N(2)); // undo what we just did, since this is postfix
break;
}
case ASSIGN: {
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);
+ startExpr(b,-1);
consume(RP);
-
- b.add(parserLine, LITERAL, JS.N(1)); // decrement the length
+
+ b.add(parserLine, PUSHKEYS);
+ b.add(parserLine, DUP);
+ b.add(parserLine, LITERAL, "length");
+ b.add(parserLine, GET);
+ // Stack is now: n, keys, obj, ...
+
+ int size = b.size;
+ b.add(parserLine, LOOP);
+ b.add(parserLine, POP);
+ // Stack is now: LoopMarker, n, keys, obj, ...
+ // NOTE: This will break if the interpreter ever becomes more strict
+ // and prevents bytecode from messing with the Markers
+ b.add(parserLine, SWAP, JS.N(3));
+ // Stack is now: Tn, keys, obj, LoopMarker, ...
+
+ b.add(parserLine, LITERAL, JS.N(1));
b.add(parserLine, SUB);
b.add(parserLine, DUP);
- b.add(parserLine, LITERAL, JS.ZERO); // see if we've exhausted all the elements
+ // Stack is now: index, keys, obj, LoopMarker
+ b.add(parserLine, LITERAL, JS.ZERO);
b.add(parserLine, LT);
- b.add(parserLine, JF, JS.N(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, JS.N(b.size - size + 1)); // BREAK to here
- b.add(parserLine, OLDSCOPE); // restore the scope
-
+ // Stack is now index<0, index, keys, obj, LoopMarker, ...
+
+ b.add(parserLine, JF, JS.N(5)); // if we're >= 0 jump 5 down (to NEWSCOPE)
+ // Move the LoopMarker back into place - this is sort of ugly
+ b.add(parserLine, SWAP, JS.N(3));
+ b.add(parserLine, SWAP, JS.N(3));
+ b.add(parserLine, SWAP, JS.N(3));
+ // Stack is now: LoopMarker, -1, keys, obj, ...
+ b.add(parserLine, BREAK);
+
+ b.add(parserLine, NEWSCOPE);
+ if(hadVar) {
+ b.add(parserLine, DECLARE, varName);
+ b.add(parserLine, POP);
+ }
+
+ // Stack is now: index, keys, obj, LoopMarker, ...
+ b.add(parserLine, GET_PRESERVE); // key, index, keys, obj, LoopMarker, ...
+ b.add(parserLine, TOPSCOPE); // scope, key, index, keys, obj, LoopMarker, ...
+ b.add(parserLine, SWAP); // key, scope, index, keys, obj, LoopMarker, ...
+ b.add(parserLine, LITERAL, varName); // varName, key, scope, index, keys, obj, LoopMaker, ...
+ b.add(parserLine, SWAP); // key, varName, scope, index, keys, obj, LoopMarker, ...
+ b.add(parserLine, PUT); // key, scope, index, keys, obj, LoopMarker, ...
+ b.add(parserLine, POP); // scope, index, keys, obj, LoopMarker
+ b.add(parserLine, POP); // index, keys, obj, LoopMarker, ...
+ // Move the LoopMarker back into place - this is sort of ugly
+ b.add(parserLine, SWAP, JS.N(3));
+ b.add(parserLine, SWAP, JS.N(3));
+ b.add(parserLine, SWAP, JS.N(3));
+
+ parseStatement(b, null);
+
+ b.add(parserLine, OLDSCOPE);
+ b.add(parserLine, CONTINUE);
+ // jump here on break
+ b.set(size, JS.N(b.size - size));
+
+ b.add(parserLine, POP); // N
+ b.add(parserLine, POP); // KEYS
+ b.add(parserLine, POP); // OBJ
+
} else {
if (hadVar) pushBackToken(VAR, null); // yeah, this actually matters
b.add(parserLine, NEWSCOPE); // grab a fresh scope
// ParserException //////////////////////////////////////////////////////////////////////
- private IOException pe(String s) { return new IOException(sourceName + ":" + parserLine + " " + s); }
+ private IOException pe(String s) { return new IOException(sourceName + ":" + line + " " + s); }
}