/** for debugging */
public static void main(String[] s) throws IOException {
- JS block = JS.fromReader("stdin", 0, new InputStreamReader(System.in));
+ JS block = JSU.fromReader("stdin", 0, new InputStreamReader(System.in));
if (block == null) return;
System.out.println(block);
}
}
// Local variable management
- List scopeStack = new ArrayList();
+ Basket.Array scopeStack = new Basket.Array();
static class ScopeInfo {
int base;
int end;
JS scopeKey(String name) {
if(globalCache.get(name) != null) return null;
for(int i=scopeStack.size()-1;i>=0;i--) {
- JS key = (JS)((ScopeInfo) scopeStack.elementAt(i)).mapping.get(name);
+ JS key = (JS)((ScopeInfo) scopeStack.get(i)).mapping.get(name);
if(key != null) return key;
}
globalCache.put(name,Boolean.TRUE);
return null;
}
void scopeDeclare(String name) throws IOException {
- ScopeInfo si = (ScopeInfo) scopeStack.lastElement();
+ ScopeInfo si = (ScopeInfo) scopeStack.peek();
if(si.mapping.get(name) != null) throw pe("" + name + " already declared in this scope");
- si.mapping.put(name,JS.N(si.end++));
+ si.mapping.put(name,JSU.N(si.end++));
globalCache.put(name,null);
}
void scopePush(JSFunction b) {
- ScopeInfo prev = (ScopeInfo) scopeStack.lastElement();
+ ScopeInfo prev = (ScopeInfo) scopeStack.peek();
ScopeInfo si = new ScopeInfo();
si.base = prev.end;
si.end = si.base;
void scopePop(JSFunction b) {
ScopeInfo si = (ScopeInfo) scopeStack.pop();
b.add(parserLine, OLDSCOPE);
- b.set(si.newScopeInsn,JS.N((si.base<<16)|((si.end-si.base)<<0)));
+ b.set(si.newScopeInsn,JSU.N((si.base<<16)|((si.end-si.base)<<0)));
}
JSFunction ret = new JSFunction(sourceName, firstLine, null);
if (sourceCode == null) return ret;
Parser p = new Parser(sourceCode, sourceName, firstLine);
- p.scopeStack.setSize(0);
+ p.scopeStack.clear();
p.scopeStack.push(new ScopeInfo());
p.scopePush(ret);
while(true) {
case -1: throw pe("expected expression");
// all of these simply push values onto the stack
- case NUMBER: b.add(parserLine, LITERAL, JS.N(number)); break;
+ case NUMBER: b.add(parserLine, LITERAL, JSU.N(number)); break;
case STRING: b.add(parserLine, LITERAL, JSString.intern(string)); break;
case NULL: b.add(parserLine, LITERAL, null); break;
- case TRUE: case FALSE: b.add(parserLine, LITERAL, tok == TRUE ? JS.T : JS.F); break;
+ case TRUE: case FALSE: b.add(parserLine, LITERAL, tok == TRUE ? JSU.T : JSU.F); break;
// (.foo) syntax
case DOT: {
consume(NAME);
b.add(parserLine, GLOBALSCOPE);
- b.add(parserLine, GET, JS.S("",true));
- b.add(parserLine, LITERAL, JS.S(string,true));
+ b.add(parserLine, GET, JSU.S("",true));
+ b.add(parserLine, LITERAL, JSU.S(string,true));
continueExprAfterAssignable(b,minPrecedence,null);
break;
}
case LB: {
- b.add(parserLine, ARRAY, JS.ZERO); // push an array onto the stack
+ b.add(parserLine, ARRAY, JSU.ZERO); // push an array onto the stack
int size0 = b.size;
int i = 0;
if (peekToken() != RB)
while(true) { // iterate over the initialization values
- b.add(parserLine, LITERAL, JS.N(i++)); // push the index in the array to place it into
+ b.add(parserLine, LITERAL, JSU.N(i++)); // push the index in the array to place it into
if (peekToken() == COMMA || peekToken() == RB)
b.add(parserLine, LITERAL, null); // for stuff like [1,,2,]
else
if (peekToken() == RB) break;
consume(COMMA);
}
- b.set(size0 - 1, JS.N(i)); // back at the ARRAY instruction, write the size of the array
+ b.set(size0 - 1, JSU.N(i)); // back at the ARRAY instruction, write the size of the array
consume(RB);
break;
}
case SUB: case ADD: {
if(peekToken() == NUMBER) { // literal
consume(NUMBER);
- b.add(parserLine, LITERAL, JS.N(number.doubleValue() * (tok == SUB ? -1 : 1)));
+ b.add(parserLine, LITERAL, JSU.N(number.doubleValue() * (tok == SUB ? -1 : 1)));
} else { // unary +/- operator
- if(tok == SUB) b.add(parserLine, LITERAL, JS.ZERO);
+ if(tok == SUB) b.add(parserLine, LITERAL, JSU.ZERO);
// BITNOT has the same precedence as the unary +/- operators
startExpr(b,precedence[BITNOT]);
- if(tok == ADD) b.add(parserLine, LITERAL, JS.ZERO); // HACK to force expr into a numeric context
+ if(tok == ADD) b.add(parserLine, LITERAL, JSU.ZERO); // HACK to force expr into a numeric context
b.add(parserLine, SUB);
}
break;
else if(!sg)
throw pe("prefixed increment/decrement can only be performed on a valid assignment target");
if(!sg) b.add(parserLine, GET_PRESERVE, Boolean.TRUE);
- b.add(parserLine, LITERAL, JS.N(1));
- b.add(parserLine, tok == INC ? ADD : SUB, JS.N(2));
+ b.add(parserLine, LITERAL, JSU.N(1));
+ b.add(parserLine, tok == INC ? ADD : SUB, JSU.N(2));
if(sg) {
b.add(parserLine, SCOPEPUT, b.getArg(prev));
} else {
if(peekToken() == ASSIGN) {
consume(ASSIGN);
startExpr(b, precedence[ASSIGN]);
- b.add(parserLine, CASCADE, JS.T);
+ b.add(parserLine, CASCADE, JSU.T);
} else {
- b.add(parserLine, CASCADE, JS.F);
+ b.add(parserLine, CASCADE, JSU.F);
}
break;
}
consume(NAME); // a named argument
b2.add(parserLine, DUP); // dup the args array
- b2.add(parserLine, GET, JS.N(numArgs - 1)); // retrieve it from the arguments array
+ b2.add(parserLine, GET, JSU.N(numArgs - 1)); // retrieve it from the arguments array
scopeDeclare(string);
b2.add(parserLine, SCOPEPUT, scopeKey(string));
b2.add(parserLine, POP);
if (tok != ADD_TRAP && tok != DEL_TRAP) {
// tok-1 is always s/^ASSIGN_// (0 is BITOR, 1 is ASSIGN_BITOR, etc)
- b.add(parserLine, tok - 1, tok-1==ADD ? JS.N(2) : null);
+ b.add(parserLine, tok - 1, tok-1==ADD ? JSU.N(2) : null);
if(varKey == null) {
b.add(parserLine, PUT);
b.add(parserLine, SWAP);
case INC: case DEC: { // postfix
if(varKey == null) {
b.add(parserLine, GET_PRESERVE, Boolean.TRUE);
- b.add(parserLine, LITERAL, JS.N(1));
- b.add(parserLine, tok == INC ? ADD : SUB, JS.N(2));
+ b.add(parserLine, LITERAL, JSU.N(1));
+ b.add(parserLine, tok == INC ? ADD : SUB, JSU.N(2));
b.add(parserLine, PUT, null);
b.add(parserLine, SWAP, null);
b.add(parserLine, POP, null);
- b.add(parserLine, LITERAL, JS.N(1));
- b.add(parserLine, tok == INC ? SUB : ADD, JS.N(2)); // undo what we just did, since this is postfix
+ b.add(parserLine, LITERAL, JSU.N(1));
+ b.add(parserLine, tok == INC ? SUB : ADD, JSU.N(2)); // undo what we just did, since this is postfix
} else {
b.add(parserLine, SCOPEGET, varKey);
b.add(parserLine, DUP);
- b.add(parserLine, LITERAL, JS.ONE);
- b.add(parserLine, tok == INC ? ADD : SUB, JS.N(2));
+ b.add(parserLine, LITERAL, JSU.ONE);
+ b.add(parserLine, tok == INC ? ADD : SUB, JSU.N(2));
b.add(parserLine, SCOPEPUT, varKey);
}
break;
// return JS.METHOD
b.add(parserLine, varKey == null ? GET_PRESERVE : SCOPEGET, varKey);
int n = parseArgs(b);
- b.add(parserLine, varKey == null ? CALLMETHOD : CALL, JS.N(n));
+ b.add(parserLine, varKey == null ? CALLMETHOD : CALL, JSU.N(n));
break;
}
default: {
switch (tok) {
case LP: { // invocation (not grouping)
int n = parseArgs(b);
- b.add(parserLine, CALL, JS.N(n));
+ b.add(parserLine, CALL, JSU.N(n));
break;
}
case BITOR: case BITXOR: case BITAND: case SHEQ: case SHNE: case LSH:
nextTok = getToken();
} while(nextTok == tok);
pushBackToken();
- b.add(parserLine, tok, JS.N(count));
+ b.add(parserLine, tok, JSU.N(count));
break;
}
case OR: case AND: {
- b.add(parserLine, tok == AND ? JSFunction.JF : JSFunction.JT, JS.ZERO); // test to see if we can short-circuit
+ b.add(parserLine, tok == AND ? JSFunction.JF : JSFunction.JT, JSU.ZERO); // test to see if we can short-circuit
int size = b.size;
startExpr(b, precedence[tok]); // otherwise check the second value
- b.add(parserLine, JMP, JS.N(2)); // leave the second value on the stack and jump to the end
+ b.add(parserLine, JMP, JSU.N(2)); // leave the second value on the stack and jump to the end
b.add(parserLine, LITERAL, tok == AND ?
- JS.B(false) : JS.B(true)); // target of the short-circuit jump is here
- b.set(size - 1, JS.N(b.size - size)); // write the target of the short-circuit jump
+ JSU.B(false) : JSU.B(true)); // target of the short-circuit jump is here
+ b.set(size - 1, JSU.N(b.size - size)); // write the target of the short-circuit jump
break;
}
case DOT: {
break;
}
case HOOK: {
- b.add(parserLine, JF, JS.ZERO); // jump to the if-false expression
+ b.add(parserLine, JF, JSU.ZERO); // jump to the if-false expression
int size = b.size;
startExpr(b, minPrecedence); // write the if-true expression
- b.add(parserLine, JMP, JS.ZERO); // if true, jump *over* the if-false expression
- b.set(size - 1, JS.N(b.size - size + 1)); // now we know where the target of the jump is
+ b.add(parserLine, JMP, JSU.ZERO); // if true, jump *over* the if-false expression
+ b.set(size - 1, JSU.N(b.size - size + 1)); // now we know where the target of the jump is
consume(COLON);
size = b.size;
startExpr(b, minPrecedence); // write the if-false expression
- b.set(size - 1, JS.N(b.size - size + 1)); // this is the end; jump to here
+ b.set(size - 1, JSU.N(b.size - size + 1)); // this is the end; jump to here
break;
}
case COMMA: {
startExpr(b, -1);
consume(RP);
- b.add(parserLine, JF, JS.ZERO); // if false, jump to the else-block
+ b.add(parserLine, JF, JSU.ZERO); // if false, jump to the else-block
int size = b.size;
parseStatement(b, null);
if (peekToken() == ELSE) {
consume(ELSE);
- b.add(parserLine, JMP, JS.ZERO); // if we took the true-block, jump over the else-block
- b.set(size - 1, JS.N(b.size - size + 1));
+ b.add(parserLine, JMP, JSU.ZERO); // if we took the true-block, jump over the else-block
+ b.set(size - 1, JSU.N(b.size - size + 1));
size = b.size;
parseStatement(b, null);
}
- b.set(size - 1, JS.N(b.size - size + 1)); // regardless of which branch we took, b[size] needs to point here
+ b.set(size - 1, JSU.N(b.size - size + 1)); // regardless of which branch we took, b[size] needs to point here
break;
}
case WHILE: {
int size = b.size;
b.add(parserLine, POP); // discard the first-iteration indicator
startExpr(b, -1);
- b.add(parserLine, JT, JS.N(2)); // if the while() clause is true, jump over the BREAK
+ b.add(parserLine, JT, JSU.N(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, JS.N(b.size - size + 1)); // end of the loop
+ b.set(size - 1, JSU.N(b.size - size + 1)); // end of the loop
break;
}
case SWITCH: {
startExpr(b, -1);
consume(COLON);
b.add(parserLine, EQ); // check if we should do this case-block
- b.add(parserLine, JF, JS.ZERO); // if not, jump to the next one
+ b.add(parserLine, JF, JSU.ZERO); // 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, JS.N(1 + b.size - size));
+ b.set(size - 1, JSU.N(1 + b.size - size));
} else if (peekToken() == DEFAULT) {
consume(DEFAULT);
consume(COLON);
} else {
throw pe("expected CASE, DEFAULT, or RC; got " + codeToString[peekToken()]);
}
- b.set(size0 - 1, JS.N(b.size - size0 + 1)); // end of the loop
+ b.set(size0 - 1, JSU.N(b.size - size0 + 1)); // end of the loop
break;
}
consume(WHILE);
consume(LP);
startExpr(b, -1);
- b.add(parserLine, JT, JS.N(2)); // check the while() clause; jump over the BREAK if true
+ b.add(parserLine, JT, JSU.N(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, JS.N(b.size - size + 1)); // end of the loop; write this location to the LOOP instruction
+ b.set(size - 1, JSU.N(b.size - size + 1)); // end of the loop; write this location to the LOOP instruction
break;
}
int catchJMPDistance = -1;
if (peekToken() == CATCH) {
- Vec catchEnds = new Vec();
+ Basket.List catchEnds = new Basket.Array();
boolean catchAll = false;
catchJMPDistance = b.size - tryInsn;
scopePop(b);
b.add(parserLine, JMP);
- catchEnds.addElement(new Integer(b.size-1));
+ catchEnds.add(new Integer(b.size-1));
- for(int i=0; i<3; i++) if (writebacks[i] != -1) b.set(writebacks[i], JS.N(b.size-writebacks[i]));
+ for(int i=0; i<3; i++) if (writebacks[i] != -1) b.set(writebacks[i], JSU.N(b.size-writebacks[i]));
b.add(parserLine, POP); // pop the element thats on the stack from the compare
}
b.add(parserLine, THROW);
for(int i=0;i<catchEnds.size();i++) {
- int n = ((Integer)catchEnds.elementAt(i)).intValue();
- b.set(n, JS.N(b.size-n));
+ int n = ((Integer)catchEnds.get(i)).intValue();
+ b.set(n, JSU.N(b.size-n));
}
// pop the try and catch markers
}
// jump here if no exception was thrown
- b.set(successJMPInsn, JS.N(b.size - successJMPInsn));
+ b.set(successJMPInsn, JSU.N(b.size - successJMPInsn));
int finallyJMPDistance = -1;
if (peekToken() == FINALLY) {
b.add(parserLine,SWAP); // get the keys enumeration object on top
b.add(parserLine,DUP);
- b.add(parserLine,GET,JS.S("hasMoreElements"));
+ b.add(parserLine,GET,JSU.S("hasMoreElements"));
int size2 = b.size;
b.add(parserLine,JT);
b.add(parserLine,SWAP);
b.add(parserLine,BREAK);
- b.set(size2, JS.N(b.size - size2));
+ b.set(size2, JSU.N(b.size - size2));
b.add(parserLine,DUP);
- b.add(parserLine,GET,JS.S("nextElement"));
+ b.add(parserLine,GET,JSU.S("nextElement"));
scopePush(b);
scopePop(b);
b.add(parserLine, CONTINUE);
// jump here on break
- b.set(size, JS.N(b.size - size));
+ b.set(size, JSU.N(b.size - size));
b.add(parserLine, POP);
} else {
if (peekToken() != SEMI)
startExpr(e2, -1);
else
- e2.add(parserLine, JSFunction.LITERAL, JS.T); // handle the for(foo;;foo) case
+ e2.add(parserLine, JSFunction.LITERAL, JSU.T); // 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, JS.ZERO); // if we're on the first iteration, jump over the incrementor
+ b.add(parserLine, JT, JSU.ZERO); // 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, JS.N(b.size - size + 1));
+ b.set(size - 1, JSU.N(b.size - size + 1));
consume(RP);
b.paste(e2); // ok, *now* test if we're done yet
- b.add(parserLine, JT, JS.N(2)); // break out if we don't meet the test
+ b.add(parserLine, JT, JSU.N(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, JS.N(b.size - size2 + 1)); // end of the loop
+ b.set(size2 - 1, JSU.N(b.size - size2 + 1)); // end of the loop
scopePop(b); // get our scope back
}