import java.io.*;
/**
- * Parses a stream of lexed tokens into a tree of CompiledFunction's.
+ * Parses a stream of lexed tokens into a tree of CompiledFunctionImpl's.
*
* There are three kinds of things we parse: blocks, statements, and
* expressions.
/** for debugging */
public static void main(String[] s) throws Exception {
- CompiledFunction block = new CompiledFunction("stdin", 0, new InputStreamReader(System.in), null);
+ CompiledFunctionImpl block = new JS.CompiledFunction("stdin", 0, new InputStreamReader(System.in), null);
if (block == null) return;
System.out.println(block);
}
static boolean[] isRightAssociative = new boolean[MAX_TOKEN + 1];
static {
isRightAssociative[ASSIGN] =
- isRightAssociative[ASSIGN_BITOR] =
- isRightAssociative[ASSIGN_BITXOR] =
- isRightAssociative[ASSIGN_BITAND] =
- isRightAssociative[ASSIGN_LSH] =
- isRightAssociative[ASSIGN_RSH] =
- isRightAssociative[ASSIGN_URSH] =
- isRightAssociative[ASSIGN_ADD] =
- isRightAssociative[ASSIGN_SUB] =
- isRightAssociative[ASSIGN_MUL] =
- isRightAssociative[ASSIGN_DIV] =
- isRightAssociative[ASSIGN_MOD] = true;
+ isRightAssociative[ASSIGN_BITOR] =
+ isRightAssociative[ASSIGN_BITXOR] =
+ isRightAssociative[ASSIGN_BITAND] =
+ isRightAssociative[ASSIGN_LSH] =
+ isRightAssociative[ASSIGN_RSH] =
+ isRightAssociative[ASSIGN_URSH] =
+ isRightAssociative[ASSIGN_ADD] =
+ isRightAssociative[ASSIGN_SUB] =
+ isRightAssociative[ASSIGN_MUL] =
+ isRightAssociative[ASSIGN_DIV] =
+ isRightAssociative[ASSIGN_MOD] = true;
precedence[ASSIGN] =
- precedence[ASSIGN_BITOR] =
- precedence[ASSIGN_BITXOR] =
- precedence[ASSIGN_BITAND] =
- precedence[ASSIGN_LSH] =
- precedence[ASSIGN_RSH] =
- precedence[ASSIGN_URSH] =
- precedence[ASSIGN_ADD] =
- precedence[ASSIGN_SUB] =
- precedence[ASSIGN_MUL] =
- precedence[ASSIGN_DIV] =
- precedence[ASSIGN_MOD] = 1;
+ precedence[ASSIGN_BITOR] =
+ precedence[ASSIGN_BITXOR] =
+ precedence[ASSIGN_BITAND] =
+ precedence[ASSIGN_LSH] =
+ precedence[ASSIGN_RSH] =
+ precedence[ASSIGN_URSH] =
+ precedence[ASSIGN_ADD] =
+ precedence[ASSIGN_SUB] =
+ precedence[ASSIGN_MUL] =
+ precedence[ASSIGN_DIV] =
+ precedence[ASSIGN_MOD] = 1;
precedence[HOOK] = 2;
precedence[COMMA] = 3;
precedence[OR] = precedence[AND] = 4;
* bytecodes for that expression to <tt>appendTo</tt>; the
* appended bytecodes MUST grow the stack by exactly one element.
*/
- private void startExpr(CompiledFunction appendTo, int minPrecedence) throws IOException {
- int saveParserLine = parserLine;
- _startExpr(appendTo, minPrecedence);
- parserLine = saveParserLine;
+ private void startExpr(CompiledFunctionImpl appendTo, int minPrecedence) throws IOException {
+ int saveParserLine = parserLine;
+ _startExpr(appendTo, minPrecedence);
+ parserLine = saveParserLine;
}
- private void _startExpr(CompiledFunction appendTo, int minPrecedence) throws IOException {
+ private void _startExpr(CompiledFunctionImpl appendTo, int minPrecedence) throws IOException {
int tok = getToken();
- CompiledFunction b = appendTo;
+ CompiledFunctionImpl b = appendTo;
switch (tok) {
case -1: throw new ParserException("expected expression");
}
case INC: case DEC: { // prefix (not postfix)
startExpr(b, precedence[tok]);
- if (b.get(b.size() - 1) != GET)
- throw new ParserException("prefixed increment/decrement can only be performed on a valid assignment target");
+ if (b.get(b.size() - 1) != GET)
+ throw new ParserException("prefixed increment/decrement can only be performed on a valid assignment target");
b.set(b.size() - 1, tok, new Boolean(true));
break;
}
break;
}
case NAME: {
- b.add(parserLine, TOPSCOPE);
- b.add(parserLine, LITERAL, string);
- continueExprAfterAssignable(b);
- break;
+ b.add(parserLine, TOPSCOPE);
+ b.add(parserLine, LITERAL, string);
+ continueExprAfterAssignable(b);
+ break;
}
case FUNCTION: {
consume(LP);
int numArgs = 0;
- CompiledFunction b2 = new CompiledFunction(sourceName, parserLine, null);
+ CompiledFunctionImpl b2 = new JS.CompiledFunction(sourceName, parserLine, null, null);
b.add(parserLine, NEWFUNCTION, b2);
// function prelude; arguments array is already on the stack
* expression that modifies the assignable. This method always
* decreases the stack depth by exactly one element.
*/
- private void continueExprAfterAssignable(CompiledFunction b) throws IOException {
- int saveParserLine = parserLine;
- _continueExprAfterAssignable(b);
- parserLine = saveParserLine;
+ private void continueExprAfterAssignable(CompiledFunctionImpl b) throws IOException {
+ int saveParserLine = parserLine;
+ _continueExprAfterAssignable(b);
+ parserLine = saveParserLine;
}
- private void _continueExprAfterAssignable(CompiledFunction b) throws IOException {
+ private void _continueExprAfterAssignable(CompiledFunctionImpl b) throws IOException {
if (b == null) throw new Error("got null b; this should never happen");
int tok = getToken();
- switch(tok) {
+ 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: {
- b.add(parserLine, GET_PRESERVE);
+ b.add(parserLine, GET_PRESERVE);
startExpr(b, -1);
b.add(parserLine, tok - 1);
b.add(parserLine, PUT);
break;
}
case INC: case DEC: { // postfix
- b.add(parserLine, GET_PRESERVE);
- b.add(parserLine, LITERAL, new Integer(1));
- b.add(parserLine, tok == INC ? ADD : SUB);
- b.add(parserLine, PUT);
- b.add(parserLine, SWAP);
- b.add(parserLine, POP);
- b.add(parserLine, LITERAL, new Integer(1));
- b.add(parserLine, tok == INC ? SUB : ADD);
- break;
- }
- case ASSIGN: {
+ b.add(parserLine, GET_PRESERVE);
+ b.add(parserLine, LITERAL, new Integer(1));
+ b.add(parserLine, tok == INC ? ADD : SUB);
+ b.add(parserLine, PUT);
+ b.add(parserLine, SWAP);
+ b.add(parserLine, POP);
+ b.add(parserLine, LITERAL, new Integer(1));
+ b.add(parserLine, tok == INC ? SUB : ADD);
+ break;
+ }
+ case ASSIGN: {
startExpr(b, -1);
- b.add(parserLine, PUT);
- b.add(parserLine, SWAP);
- b.add(parserLine, POP);
- break;
- }
- default: {
- pushBackToken();
- b.add(parserLine, GET);
- return;
- }
- }
+ b.add(parserLine, PUT);
+ b.add(parserLine, SWAP);
+ b.add(parserLine, POP);
+ break;
+ }
+ default: {
+ pushBackToken();
+ b.add(parserLine, GET);
+ return;
+ }
+ }
}
* If any bytecodes are appended, they will not alter the stack
* depth.
*/
- private void continueExpr(CompiledFunction b, int minPrecedence) throws IOException {
- int saveParserLine = parserLine;
- _continueExpr(b, minPrecedence);
- parserLine = saveParserLine;
+ private void continueExpr(CompiledFunctionImpl b, int minPrecedence) throws IOException {
+ int saveParserLine = parserLine;
+ _continueExpr(b, minPrecedence);
+ parserLine = saveParserLine;
}
- private void _continueExpr(CompiledFunction b, int minPrecedence) throws IOException {
+ private void _continueExpr(CompiledFunctionImpl b, int minPrecedence) throws IOException {
if (b == null) throw new Error("got null b; this should never happen");
int tok = getToken();
if (tok == -1) return;
startExpr(b, precedence[tok]); // otherwise check the second value
b.add(parserLine, JMP, new Integer(2)); // leave the second value on the stack and jump to the end
b.add(parserLine, LITERAL, tok == AND ?
- new Boolean(false) : new Boolean(true)); // target of the short-circuit jump is here
+ new Boolean(false) : new Boolean(true)); // target of the short-circuit jump is here
b.set(size - 1, new Integer(b.size() - size)); // write the target of the short-circuit jump
break;
}
case DOT: {
consume(NAME);
- b.add(parserLine, LITERAL, string);
- continueExprAfterAssignable(b);
- break;
+ b.add(parserLine, LITERAL, string);
+ continueExprAfterAssignable(b);
+ break;
}
case LB: { // subscripting (not array constructor)
startExpr(b, -1);
consume(RB);
- continueExprAfterAssignable(b);
- break;
+ continueExprAfterAssignable(b);
+ break;
}
case HOOK: {
b.add(parserLine, JF, new Integer(0)); // jump to the if-false expression
}
/** Parse a block of statements which must be surrounded by LC..RC. */
- void parseBlock(CompiledFunction b) throws IOException { parseBlock(b, null); }
- void parseBlock(CompiledFunction b, String label) throws IOException {
- int saveParserLine = parserLine;
- _parseBlock(b, label);
- parserLine = saveParserLine;
+ void parseBlock(CompiledFunctionImpl b) throws IOException { parseBlock(b, null); }
+ void parseBlock(CompiledFunctionImpl b, String label) throws IOException {
+ int saveParserLine = parserLine;
+ _parseBlock(b, label);
+ parserLine = saveParserLine;
}
- void _parseBlock(CompiledFunction b, String label) throws IOException {
+ void _parseBlock(CompiledFunctionImpl b, String label) throws IOException {
if (peekToken() == -1) return;
else if (peekToken() != LC) parseStatement(b, null);
else {
}
/** Parse a single statement, consuming the RC or SEMI which terminates it. */
- void parseStatement(CompiledFunction b, String label) throws IOException {
- int saveParserLine = parserLine;
- _parseStatement(b, label);
- parserLine = saveParserLine;
+ void parseStatement(CompiledFunctionImpl b, String label) throws IOException {
+ int saveParserLine = parserLine;
+ _parseStatement(b, label);
+ parserLine = saveParserLine;
}
- void _parseStatement(CompiledFunction b, String label) throws IOException {
+ void _parseStatement(CompiledFunctionImpl b, String label) throws IOException {
int tok = peekToken();
if (tok == -1) return;
switch(tok = getToken()) {
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
+ 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);
b.add(parserLine, NEWSCOPE); // grab a fresh scope
parseStatement(b, null); // initializer
- CompiledFunction e2 = // we need to put the incrementor before the test
- new CompiledFunction(sourceName, parserLine, null); // so we save the test here
+ 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
case LC: { // blocks are statements too
pushBackToken();
- b.add(parserLine, NEWSCOPE);
+ b.add(parserLine, NEWSCOPE);
parseBlock(b, label);
- b.add(parserLine, OLDSCOPE);
+ b.add(parserLine, OLDSCOPE);
break;
}