// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
package org.xwt.js;
+// FIXME: line number accuracy
+
import org.xwt.util.*;
import java.io.*;
-/** Parses a stream of lexed tokens into a tree of ByteCodeBlock's */
+/**
+ * Parses a stream of lexed tokens into a tree of CompiledFunction's.
+ *
+
+ * There are three kinds of things we parse: blocks, statements,
+ * expressions. Expressions are a special type of statement that
+ * evaluates to a value (for example, "break" is not an expression,
+ * but "3+2" is). AssignmentTargets are a special kind of expression
+ * that can be 'put' to (for example, "foo()" is not an
+ * assignmentTarget, but "foo[7]" is). FIXME.
+
+ *
+ * Technically it would be a better design for this class to build an
+ * intermediate parse tree and use that to emit bytecode. Here's the
+ * tradeoff:
+ *
+ * Advantages of building a parse tree:
+ * - easier to apply optimizations
+ * - would let us handle more sophisticated languages than JavaScript
+ *
+ * Advantages of leaving out the parse tree
+ * - faster compilation
+ * - less load on the garbage collector
+ * - much simpler code, easier to understand
+ * - less error-prone
+ *
+ * Fortunately JS is such a simple language that we can get away with
+ * the half-assed approach and still produce a working, complete
+ * compiler.
+ *
+ * The bytecode language emitted doesn't really cause any appreciable
+ * semantic loss, and is itself a parseable language very similar to
+ * Forth or a postfix variant of LISP. This means that the bytecode
+ * can be transformed into a parse tree, which can be manipulated.
+ * So if we ever want to add an optimizer, it could easily be done by
+ * producing a parse tree from the bytecode, optimizing that tree,
+ * and then re-emitting the bytecode. The parse tree node class
+ * would also be much simpler since the bytecode language has so few
+ * operators.
+ *
+ * Actually, the above paragraph is slightly inaccurate -- there are
+ * places where we push a value and then perform an arbitrary number
+ * of operations using it before popping it; this doesn't parse well.
+ * But these cases are clearly marked and easy to change if we do
+ * need to move to a parse tree format.
+ */
class Parser extends Lexer implements ByteCodes {
// Constructors //////////////////////////////////////////////////////
- public Parser(Reader r, String sourceName, int line) throws IOException {
- super(r);
- this.sourceName = sourceName;
- this.line = line;
- }
+ public Parser(Reader r, String sourceName, int line) throws IOException { super(r, sourceName, line); }
/** for debugging */
public static void main(String[] s) throws Exception {
- Parser p = new Parser(new InputStreamReader(System.in), "stdin", 0);
- while(true) {
- ByteCodeBlock block = new ByteCodeBlock(0, "stdin");
- p.parseStatement(false, block);
- if (block == null) return;
- System.out.println(block);
- if (p.peekToken() == -1) return;
- }
+ CompiledFunction block = new CompiledFunction("stdin", 0, new InputStreamReader(System.in), null);
+ if (block == null) return;
+ System.out.println(block);
}
precedence[LSH] = precedence[RSH] = precedence[URSH] = 12;
precedence[ADD] = precedence[SUB] = 13;
precedence[MUL] = precedence[DIV] = precedence[MOD] = 14;
- precedence[BITNOT] = precedence[INSTANCEOF] = 15;
+ precedence[BITNOT] = 15;
precedence[INC] = precedence[DEC] = 16;
precedence[LP] = 17;
precedence[LB] = 18;
// Parsing Logic /////////////////////////////////////////////////////////
/** gets a token and throws an exception if it is not <tt>code</tt> */
- public void consume(int code) throws IOException {
- if (getToken() != code)
- throw new ParserException("expected " + codeToString[code] + ", got " + (op == -1 ? "EOL" : codeToString[op]));
+ private void consume(int code) throws IOException {
+ if (getToken() != code) throw new ParserException("expected " + codeToString[code] + ", got " + (op == -1 ? "EOF" : codeToString[op]));
}
- /** append the largest expression beginning with prefix containing no operators of precedence below <tt>minPrecedence</tt> */
- public ByteCodeBlock startExpr() throws IOException { return startExpr(-1); }
- public void startExpr(ByteCodeBlock block) throws IOException { startExpr(-1, block); }
- public ByteCodeBlock startExpr(int minPrecedence) throws IOException {
- ByteCodeBlock ret = new ByteCodeBlock(line, sourceName);
- startExpr(minPrecedence, ret);
- return ret.size() == 0 ? null : ret;
- }
-
- public void startExpr(int minPrecedence, ByteCodeBlock appendTo) throws IOException {
+ /**
+ * Parse the largest possible expression containing no operators
+ * of precedence below <tt>minPrecedence</tt> and append the
+ * 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 tok = getToken();
- int curLine = line;
- if (tok == -1) return;
- if (minPrecedence > 0 && precedence[tok] != 0)
- if (precedence[tok] < minPrecedence || (precedence[tok] == minPrecedence && !isRightAssociative[tok]))
- { pushBackToken(); return; }
-
- ByteCodeBlock b = appendTo;
+ CompiledFunction b = appendTo;
switch (tok) {
- case NUMBER: continueExpr(b.add(ByteCodeBlock.LITERAL, number), minPrecedence); return;
- case STRING: continueExpr(b.add(ByteCodeBlock.LITERAL, string), minPrecedence); return;
- case THIS: continueExpr(b.add(TOPSCOPE, null), minPrecedence); return;
- case NULL: continueExpr(b.add(ByteCodeBlock.LITERAL, null), minPrecedence); return;
- case TRUE: case FALSE: continueExpr(b.add(ByteCodeBlock.LITERAL, new Boolean(tok == TRUE)), minPrecedence); return;
+ case -1: throw new ParserException("expected expression");
+
+ // all of these simply push values onto the stack
+ case NUMBER: b.add(line, LITERAL, number); break;
+ case STRING: b.add(line, LITERAL, string); break;
+ case THIS: b.add(line, TOPSCOPE, null); break;
+ case NULL: b.add(line, LITERAL, null); break;
+ case TRUE: case FALSE: b.add(line, LITERAL, new Boolean(tok == TRUE)); break;
+
case LB: {
- b.add(ARRAY, new Integer(0));
+ b.add(line, ARRAY, new Integer(0)); // push an array onto the stack
+ int size0 = b.size();
int i = 0;
- while(true) {
- int size = b.size();
- startExpr(b);
- if (size == b.size())
- if (peekToken() == RB) { consume(RB); continueExpr(b, minPrecedence); return; }
- b.add(LITERAL, new Integer(i++));
- if (size == b.size()) b.add(LITERAL, null);
- b.add(PUT);
- b.add(POP);
- if (peekToken() == RB) { consume(RB); continueExpr(b, minPrecedence); return; }
- consume(COMMA);
- }
+ if (peekToken() != RB)
+ while(true) { // iterate over the initialization values
+ int size = b.size();
+ if (peekToken() == COMMA || peekToken() == RB)
+ b.add(line, LITERAL, null); // for stuff like [1,,2,]
+ else
+ startExpr(b, -1); // push the value onto the stack
+ b.add(line, LITERAL, new Integer(i++)); // push the index in the array to place it into
+ b.add(line, PUT); // put it into the array
+ b.add(line, POP); // discard the value remaining on the stack
+ if (peekToken() == RB) break;
+ consume(COMMA);
+ }
+ b.set(size0 - 1, new Integer(i)); // back at the ARRAY instruction, write the size of the array
+ consume(RB);
+ break;
}
- case SUB: {
+ case SUB: { // negative literal (like "3 * -1")
consume(NUMBER);
- continueExpr(b.add(ByteCodeBlock.LITERAL, new Double(number.doubleValue() * -1)), minPrecedence);
- return;
+ b.add(line, LITERAL, new Double(number.doubleValue() * -1));
+ break;
}
- case LP: {
- startExpr(b);
+ case LP: { // grouping (not calling)
+ startExpr(b, -1);
consume(RP);
- continueExpr(b, minPrecedence);
- return;
+ break;
}
- case INC: case DEC: {
- // prefix
- startExpr(precedence[tok], b);
- b.set(b.size() - 1, tok, new Boolean(true));
- continueExpr(b, minPrecedence);
- return;
+ case INC: case DEC: { // prefix (not postfix)
+ startExpr(b, precedence[tok]);
+ b.set(b.size() - 1, tok, new Boolean(true)); // FIXME, ugly; need startAssignTarget
+ break;
}
- case BANG: case BITNOT: case INSTANCEOF: case TYPEOF: {
- startExpr(precedence[tok], b);
- b.add(tok);
- continueExpr(b, minPrecedence);
- return;
+ case BANG: case BITNOT: case TYPEOF: {
+ startExpr(b, precedence[tok]);
+ b.add(line, tok);
+ break;
}
- case LC: {
- b.add(OBJECT, null);
- if (peekToken() == RC) { consume(RC); continueExpr(b, minPrecedence); return; }
- while(true) {
- if (peekToken() != NAME && peekToken() != STRING) throw new Error("expected NAME or STRING");
- getToken();
- b.add(LITERAL, string);
- consume(COLON);
- startExpr(b);
- b.add(PUT);
- b.add(POP);
- if (peekToken() == RC) { consume(RC); continueExpr(b, minPrecedence); return; }
- consume(COMMA);
- if (peekToken() == RC) { consume(RC); continueExpr(b, minPrecedence); return; }
- }
+ case LC: { // object constructor
+ b.add(line, OBJECT, null); // put an object on the stack
+ if (peekToken() != RC)
+ while(true) {
+ if (peekToken() != NAME && peekToken() != STRING)
+ throw new ParserException("expected NAME or STRING");
+ getToken();
+ b.add(line, LITERAL, string); // grab the key
+ consume(COLON);
+ startExpr(b, -1); // grab the value
+ b.add(line, PUT); // put the value into the object
+ b.add(line, POP); // discard the remaining value
+ if (peekToken() == RC) break;
+ consume(COMMA);
+ if (peekToken() == RC) break; // we permit {,,} -- I'm not sure if ECMA does
+ }
+ consume(RC);
+ break;
}
- case NAME: {
+ case NAME: { // FIXME; this is an lvalue
String name = string;
if (peekToken() == ASSIGN) {
consume(ASSIGN);
- b.add(TOPSCOPE);
- b.add(ByteCodeBlock.LITERAL, name);
- startExpr(minPrecedence, b);
- b.add(ByteCodeBlock.PUT);
- b.add(ByteCodeBlock.SWAP);
- b.add(ByteCodeBlock.POP);
+ b.add(line, TOPSCOPE);
+ b.add(line, LITERAL, name);
+ startExpr(b, minPrecedence);
+ b.add(line, PUT);
+ b.add(line, SWAP);
+ b.add(line, POP);
} else {
- b.add(TOPSCOPE);
- b.add(ByteCodeBlock.LITERAL, name);
- b.add(ByteCodeBlock.GET);
+ b.add(line, TOPSCOPE);
+ b.add(line, LITERAL, name);
+ b.add(line, GET);
}
- continueExpr(b, minPrecedence);
- return;
+ break;
}
case FUNCTION: {
consume(LP);
int numArgs = 0;
- ByteCodeBlock b2 = new ByteCodeBlock(curLine, sourceName);
- b2.add(TOPSCOPE);
- b2.add(SWAP);
- b2.add(LITERAL, "arguments");
- b2.add(LITERAL, "arguments");
- b2.add(DECLARE);
- b2.add(SWAP);
- b2.add(PUT);
- b2.add(SWAP);
- b2.add(POP);
- if (peekToken() == RP) consume(RP);
- else while(true) {
- if (peekToken() == COMMA) {
- consume(COMMA);
-
- } else {
- consume(NAME);
-
- // declare the name
- b2.add(LITERAL, string);
- b2.add(DECLARE);
-
- // retrieve it from the arguments array
- b2.add(LITERAL, new Integer(numArgs));
- b2.add(GET_PRESERVE);
- b2.add(SWAP);
- b2.add(POP);
-
- // put it to the current scope
- b2.add(TOPSCOPE);
- b2.add(SWAP);
- b2.add(LITERAL, string);
- b2.add(SWAP);
- b2.add(PUT);
-
- // clean the stack
- b2.add(POP);
- b2.add(POP);
-
- if (peekToken() == RP) { consume(RP); break; }
- consume(COMMA);
+ CompiledFunction b2 = new CompiledFunction(sourceName, line, null);
+ b.add(line, NEWFUNCTION, b2);
+
+ // function prelude; arguments array is already on the stack
+ b2.add(line, TOPSCOPE); // push the scope onto the stack
+ b2.add(line, SWAP); // swap 'this' and 'arguments'
+
+ b2.add(line, LITERAL, "arguments"); // declare arguments (equivalent to 'var arguments;')
+ b2.add(line, DECLARE);
+
+ b2.add(line, LITERAL, "arguments"); // set this.arguments and leave the value on the stack
+ b2.add(line, SWAP);
+ b2.add(line, PUT);
+ b2.add(line, SWAP);
+ b2.add(line, POP);
+
+ while(peekToken() != RP) { // run through the list of argument names
+ if (peekToken() == NAME) {
+ consume(NAME); // a named argument
+
+ b2.add(line, LITERAL, string); // declare the name
+ b2.add(line, DECLARE);
+
+ b2.add(line, LITERAL, new Integer(numArgs)); // retrieve it from the arguments array
+ b2.add(line, GET_PRESERVE);
+ b2.add(line, SWAP);
+ b2.add(line, POP);
+
+ b2.add(line, TOPSCOPE); // put it to the current scope
+ b2.add(line, SWAP);
+ b2.add(line, LITERAL, string);
+ b2.add(line, SWAP);
+ b2.add(line, PUT);
+
+ b2.add(line, POP); // clean the stack
+ b2.add(line, POP);
}
+ if (peekToken() == RP) break;
+ consume(COMMA);
numArgs++;
}
- // pop off the arguments array
- b2.add(POP);
- parseStatement(true, b2);
- b2.add(LITERAL, null);
- b2.add(RETURN);
- continueExpr(b.add(NEWFUNCTION, b2), minPrecedence);
- return;
+ consume(RP);
+
+ b2.add(line, POP); // pop off the arguments array
+
+ parseStatement(b2, null); // the function body
+
+ b2.add(line, LITERAL, null); // in case we "fall out the bottom", return NULL
+ b2.add(line, RETURN);
+
+ break;
}
- default: pushBackToken(); return;
+ default: throw new ParserException("expected expression, found " + codeToString[tok] + ", which cannot start an expression");
}
+
+ // attempt to continue the expression
+ continueExpr(b, minPrecedence);
}
- public void continueExpr(ByteCodeBlock prefix, int minPrecedence) throws IOException {
+ /**
+ * Assuming that a complete expression has just been parsed,
+ * <tt>continueExpr</tt> will attempt to extend this expression by
+ * parsing additional tokens and appending additional bytecodes.
+ *
+ * No operators with precedence less than <tt>minPrecedence</tt>
+ * will be parsed.
+ *
+ * If any bytecodes are appended, they will not alter the stack
+ * depth.
+ */
+ private void continueExpr(CompiledFunction b, int minPrecedence) throws IOException {
+ if (b == null) throw new Error("got null b; this should never happen");
int tok = getToken();
- int curLine = line;
if (tok == -1) return;
- if (minPrecedence > 0 && precedence[tok] != 0)
- if (precedence[tok] < minPrecedence || (precedence[tok] == minPrecedence && !isRightAssociative[tok]))
- { pushBackToken(); return; }
-
- if (prefix == null) throw new Error("got null prefix");
- ByteCodeBlock b = prefix;
+ if (minPrecedence != -1 && (precedence[tok] < minPrecedence || (precedence[tok] == minPrecedence && !isRightAssociative[tok]))) {
+ pushBackToken();
+ return;
+ }
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: {
- prefix.set(prefix.size() - 1, b.GET_PRESERVE, new Boolean(true));
- startExpr(precedence[tok - 1], b);
- prefix.add(tok - 1);
- prefix.add(PUT);
- prefix.add(SWAP);
- prefix.add(POP);
- continueExpr(b, minPrecedence);
- return;
+ b.set(b.size() - 1, b.GET_PRESERVE, new Boolean(true)); // FIXME should use AssignTarget
+ startExpr(b, precedence[tok - 1]);
+ b.add(line, tok - 1);
+ b.add(line, PUT);
+ b.add(line, SWAP);
+ b.add(line, POP);
+ break;
}
-
- case INC: case DEC: {
- // postfix
- b.set(b.size() - 1, tok, new Boolean(false));
- continueExpr(b, minPrecedence);
- return;
+ case INC: case DEC: { // postfix
+ b.set(b.size() - 1, tok, new Boolean(false)); // FIXME use assignmenttarget
+ break;
}
-
- case LP: {
- // invocation
+ case LP: { // invocation (not grouping)
int i = 0;
while(peekToken() != RP) {
- startExpr(b);
i++;
- if (peekToken() == RP) break;
+ if (peekToken() != COMMA) {
+ startExpr(b, -1);
+ if (peekToken() == RP) break;
+ }
consume(COMMA);
}
consume(RP);
- b.add(CALL, new Integer(i));
- continueExpr(b, minPrecedence);
- return;
+ b.add(line, CALL, new Integer(i));
+ break;
}
-
case BITOR: case BITXOR: case BITAND: case SHEQ: case SHNE: case LSH:
case RSH: case URSH: case ADD: case MUL: case DIV: case MOD:
case GT: case GE: case EQ: case NE: case LT: case LE: case SUB: {
- startExpr(precedence[tok], b);
- b.add(tok);
- continueExpr(b, minPrecedence);
- return;
+ startExpr(b, precedence[tok]);
+ b.add(line, tok);
+ break;
}
-
case OR: case AND: {
- b.add(tok == AND ? b.JF : b.JT, new Integer(0));
+ b.add(line, tok == AND ? b.JF : b.JT, new Integer(0)); // test to see if we can short-circuit
int size = b.size();
- startExpr(precedence[tok], b);
- b.arg[size - 1] = new Integer(b.size() - size + 2);
- b.add(JMP, new Integer(2));
- b.add(LITERAL, tok == AND ? new Boolean(false) : new Boolean(true));
- continueExpr(b, minPrecedence);
- return;
+ startExpr(b, precedence[tok]); // otherwise check the second value
+ b.add(line, JMP, new Integer(2)); // leave the second value on the stack and jump to the end
+ b.add(line, LITERAL, tok == AND ? 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: {
+ case DOT: { // FIXME, assigntarget
consume(NAME);
String target = string;
if (peekToken() == ASSIGN) {
consume(ASSIGN);
- b.add(LITERAL, target);
- startExpr(b);
- b.add(PUT);
- b.add(SWAP);
- b.add(POP);
+ b.add(line, LITERAL, target);
+ startExpr(b, -1);
+ b.add(line, PUT);
+ b.add(line, SWAP);
+ b.add(line, POP);
} else {
- b.add(LITERAL, target);
- b.add(GET);
+ b.add(line, LITERAL, target);
+ b.add(line, GET);
}
- continueExpr(b, minPrecedence);
- return;
+ break;
}
-
- case LB: {
- startExpr(b);
+ case LB: { // subscripting (not array constructor)
+ startExpr(b, -1);
consume(RB);
- if (peekToken() == ASSIGN) {
+ if (peekToken() == ASSIGN) { // FIXME: assigntarget
consume(ASSIGN);
- startExpr(b);
- b.add(PUT);
- b.add(SWAP);
- b.add(POP);
+ startExpr(b, -1);
+ b.add(line, PUT);
+ b.add(line, SWAP);
+ b.add(line, POP);
} else {
- b.add(GET);
+ b.add(line, GET);
}
- continueExpr(b, minPrecedence);
- return;
+ break;
}
-
case HOOK: {
- b.add(JF, new Integer(0));
+ b.add(line, JF, new Integer(0)); // jump to the if-false expression
int size = b.size();
- startExpr(b);
- b.arg[size - 1] = new Integer(b.size() - size + 2);
- b.add(JMP, new Integer(0));
+ startExpr(b, -1); // write the if-true expression
+ b.add(line, JMP, new Integer(0)); // if true, jump *over* the if-false expression
+ b.set(size - 1, new Integer(b.size() - size + 1)); // now we know where the target of the jump is
consume(COLON);
size = b.size();
- startExpr(b);
- b.arg[size - 1] = new Integer(b.size() - size + 1);
- continueExpr(b, minPrecedence);
+ startExpr(b, -1); // write the if-false expression
+ b.set(size - 1, new Integer(b.size() - size + 1)); // this is the end; jump to here
+ break;
+ }
+ default: {
+ pushBackToken();
return;
}
-
- default: { pushBackToken(); return; }
}
+
+ continueExpr(b, minPrecedence); // try to continue the expression
}
- /** a block is either a single statement or a list of statements surrounded by curly braces; all expressions are also statements */
- public ByteCodeBlock parseStatement() throws IOException {
- ByteCodeBlock ret = new ByteCodeBlock(line, sourceName);
- ret.add(ret.LITERAL, null);
- parseStatement(false, ret);
- if (ret.size() == 1) return null;
- return ret;
+ /** 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 {
+ if (peekToken() == -1) return;
+ else if (peekToken() != LC) parseStatement(b, null);
+ else {
+ consume(LC);
+ while(peekToken() != RC && peekToken() != -1) parseStatement(b, null);
+ consume(RC);
+ }
}
- public void parseStatement(boolean requireBraces) throws IOException {
- parseStatement(requireBraces, new ByteCodeBlock(line, sourceName));
- }
- public void parseStatement(boolean requireBraces, ByteCodeBlock b) throws IOException {
+ /** Parse a single statement, consuming the RC or SEMI which terminates it. */
+ void parseStatement(CompiledFunction b, String label) 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(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(TOPSCOPE); // push the current scope
- while(true) {
- consume(NAME);
- String name = string;
- b.add(LITERAL, name); // push the name to be declared
- b.add(DECLARE); // declare it
- if (peekToken() == ASSIGN) { // if there is an '=' after the variable name
- b.add(LITERAL, name); // put the var name back on the stack
- consume(ASSIGN);
- startExpr(b);
- b.add(PUT);
- b.add(POP);
- }
- if (peekToken() != COMMA) break;
- consume(COMMA);
- }
- b.add(POP);
- if (peekToken() == SEMI) consume(SEMI);
- break;
- }
-
- case IF: {
- consume(IF);
- consume(LP);
- startExpr(b);
- consume(RP);
-
- b.add(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(JMP, new Integer(0));
- size = b.size();
- parseStatement(false, b);
+ 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);
}
- b.arg[size - 1] = new Integer(1 + b.size() - size);
- break;
+ if (peekToken() != COMMA) break;
+ consume(COMMA);
}
-
- case WHILE: {
- consume(WHILE);
- consume(LP);
- ByteCodeBlock loop = new ByteCodeBlock(curLine, sourceName);
- b.add(LOOP, loop);
-
- loop.add(POP);
- startExpr(loop);
- loop.add(JT, new Integer(2));
- loop.add(BREAK);
- consume(RP);
- parseStatement(false, loop);
-
- // if we fall out of the end, definately continue
- loop.add(CONTINUE);
- break;
+ 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);
}
-
- case SWITCH: {
- consume(SWITCH);
- consume(LP);
- ByteCodeBlock loop = new ByteCodeBlock(curLine, sourceName);
- b.add(LOOP, loop);
- startExpr(loop);
- consume(RP);
- consume(LC);
- while(true)
- if (peekToken() == CASE) {
- consume(CASE);
- loop.add(DUP);
- startExpr(loop);
- consume(COLON);
- loop.add(EQ);
- loop.add(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()]);
+ 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;
}
- break;
- }
-
- case DO: {
- consume(DO);
- ByteCodeBlock loop = new ByteCodeBlock(curLine, sourceName);
- b.add(LOOP, loop);
-
- parseStatement(false, loop);
- consume(WHILE);
+ 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);
- startExpr(loop);
- loop.add(JT, new Integer(2));
- loop.add(BREAK);
- loop.add(CONTINUE);
+ consume(NAME);
consume(RP);
- consume(SEMI);
- break;
+ // 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);
}
-
- case TRY: {
- // 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;
+
+ 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(FOR);
consume(LP);
-
+
tok = getToken();
- if (tok == VAR) 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);
- b.add(PUSHKEYS);
- b.add(LITERAL, "length");
- b.add(GET);
+ startExpr(b, -1);
+ b.add(line, PUSHKEYS);
+ b.add(line, LITERAL, "length");
+ b.add(line, GET);
consume(RP);
- ByteCodeBlock b2 = new ByteCodeBlock(curLine, sourceName);
- b.add(SCOPE, b2);
- b2.add(LITERAL, new Integer(1));
- b2.add(SUB);
- b2.add(DUP);
- b2.add(LITERAL, new Integer(0));
- b2.add(LT);
- b2.add(JT, new Integer(7));
- b2.add(GET_PRESERVE);
- b2.add(LITERAL, varName);
- b2.add(LITERAL, varName);
- b2.add(DECLARE);
- b2.add(PUT);
- parseStatement(false, b2);
+ 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 {
- ByteCodeBlock b2 = new ByteCodeBlock(curLine, sourceName);
- b.add(SCOPE, b2);
- b.add(POP);
-
- int size = b2.size();
- startExpr(b2);
- if (b2.size() - size > 0) b2.add(POP);
- consume(SEMI);
- ByteCodeBlock e2 = startExpr();
+ 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 (e2 == null) e2 = new ByteCodeBlock(curLine, sourceName, b.LITERAL, null);
-
- ByteCodeBlock b3 = new ByteCodeBlock(curLine, sourceName);
- b2.add(LOOP, b3);
- b2.add(LITERAL, null);
-
- b3.add(JT, new Integer(0));
- size = b3.size();
- startExpr(b3);
+ 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);
- if (b3.size() - size > 0) b3.add(POP);
- b3.arg[size - 1] = new Integer(b3.size() - size + 1);
-
- b3.paste(e2);
- b3.add(JT, new Integer(2));
- b3.add(BREAK);
- parseStatement(false, b3);
- b3.add(BREAK);
+
+ 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: {
- consume(NAME);
- String name = string;
- if (peekToken() == COLON) {
- consume(COLON);
- b.add(ByteCodeBlock.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(POP);
- if (peekToken() == SEMI) consume(SEMI);
+
+ 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;
}
- }
-
- if (!braced) return;
+ }
+
+ 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;
+ }
}
}
+
+
+ // ParserException //////////////////////////////////////////////////////////////////////
- class ParserException extends RuntimeException {
- public ParserException(String s) { super(sourceName + ":" + line + " " + s); }
- }
+ private class ParserException extends IOException { public ParserException(String s) { super(sourceName + ":" + line + " " + s); } }
}