X-Git-Url: http://git.megacz.com/?p=org.ibex.core.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fjs%2FParser.java;h=6293107d006d5900c69564d1dc98b9ad26e550f7;hp=38d15ea73b0ca00341d6a7979b3b1fe1a96f4334;hb=3f8aa5300e178e8975b0edc896a5a9d303e7bdf3;hpb=9886bf61ea5fbbc8eec3969ca21d72a4dce69afe diff --git a/src/org/ibex/js/Parser.java b/src/org/ibex/js/Parser.java index 38d15ea..6293107 100644 --- a/src/org/ibex/js/Parser.java +++ b/src/org/ibex/js/Parser.java @@ -69,7 +69,7 @@ class Parser extends Lexer implements ByteCodes { // Constructors ////////////////////////////////////////////////////// - public Parser(Reader r, String sourceName, int line) throws IOException { super(r, sourceName, line); } + private Parser(Reader r, String sourceName, int line) throws IOException { super(r, sourceName, line); } /** for debugging */ public static void main(String[] s) throws IOException { @@ -78,7 +78,6 @@ class Parser extends Lexer implements ByteCodes { System.out.println(block); } - // Statics //////////////////////////////////////////////////////////// static byte[] precedence = new byte[MAX_TOKEN + 1]; @@ -133,8 +132,68 @@ class Parser extends Lexer implements ByteCodes { precedence[DOT] = precedence[LB] = precedence[LP] = precedence[INC] = precedence[DEC] = 15; } - + // Local variable management + Vec scopeStack = new Vec(); + static class ScopeInfo { + int base; + int end; + int newScopeInsn; + Hash mapping = new Hash(); + } + Hash globalCache = new Hash(); + 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); + if(key != null) return key; + } + globalCache.put(name,Boolean.TRUE); + return null; + } + void scopeDeclare(String name) throws IOException { + ScopeInfo si = (ScopeInfo) scopeStack.lastElement(); + if(si.mapping.get(name) != null) throw pe("" + name + " already declared in this scope"); + si.mapping.put(name,JS.N(si.end++)); + globalCache.put(name,null); + } + void scopePush(JSFunction b) { + ScopeInfo prev = (ScopeInfo) scopeStack.lastElement(); + ScopeInfo si = new ScopeInfo(); + si.base = prev.end; + si.end = si.base; + si.newScopeInsn = b.size; + scopeStack.push(si); + b.add(parserLine, NEWSCOPE); + } + 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))); + } + + // Parsing Logic ///////////////////////////////////////////////////////// + + /** parse and compile a function */ + public static JSFunction fromReader(String sourceName, int firstLine, Reader sourceCode) throws IOException { + 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.push(new ScopeInfo()); + p.scopePush(ret); + while(true) { + //int s = ret.size; + if(p.peekToken() == -1) break; // FIXME: Check this logic one more time + p.parseStatement(ret, null); + //if (s == ret.size) break; + } + p.scopePop(ret); + if(p.scopeStack.size() != 1) throw new Error("scopeStack height mismatch"); + ret.add(-1, LITERAL, null); + ret.add(-1, RETURN); + return ret; + } /** gets a token and throws an exception if it is not code */ private void consume(int code) throws IOException { @@ -169,20 +228,18 @@ class Parser extends Lexer implements ByteCodes { case -1: throw pe("expected expression"); // all of these simply push values onto the stack - case NUMBER: b.add(parserLine, LITERAL, number); break; - case STRING: b.add(parserLine, LITERAL, string); break; + case NUMBER: b.add(parserLine, LITERAL, JS.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, JS.B(tok == TRUE)); break; + case TRUE: case FALSE: b.add(parserLine, LITERAL, tok == TRUE ? JS.T : JS.F); break; // (.foo) syntax case DOT: { consume(NAME); - b.add(parserLine, TOPSCOPE); - b.add(parserLine, LITERAL, ""); - b.add(parserLine, GET); - b.add(parserLine, LITERAL, string); - b.add(parserLine, GET); - continueExpr(b, minPrecedence); + b.add(parserLine, GLOBALSCOPE); + b.add(parserLine, GET, JS.S("",true)); + b.add(parserLine, LITERAL, JS.S(string,true)); + continueExprAfterAssignable(b,minPrecedence,null); break; } @@ -227,18 +284,23 @@ class Parser extends Lexer implements ByteCodes { case INC: case DEC: { // prefix (not postfix) startExpr(b, precedence[tok]); int prev = b.size - 1; + boolean sg = b.get(prev) == SCOPEGET; if (b.get(prev) == GET && b.getArg(prev) != null) b.set(prev, LITERAL, b.getArg(prev)); else if(b.get(prev) == GET) b.pop(); - else + else if(!sg) throw pe("prefixed increment/decrement can only be performed on a valid assignment target"); - b.add(parserLine, GET_PRESERVE, Boolean.TRUE); + 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, PUT, null); - b.add(parserLine, SWAP, null); - b.add(parserLine, POP, null); + if(sg) { + b.add(parserLine, SCOPEPUT, b.getArg(prev)); + } else { + b.add(parserLine, PUT, null); + b.add(parserLine, SWAP, null); + b.add(parserLine, POP, null); + } break; } case BANG: case BITNOT: case TYPEOF: { @@ -253,7 +315,7 @@ class Parser extends Lexer implements ByteCodes { if (peekToken() != NAME && peekToken() != STRING) throw pe("expected NAME or STRING"); getToken(); - b.add(parserLine, LITERAL, string); // grab the key + b.add(parserLine, LITERAL, JSString.intern(string)); // grab the key consume(COLON); startExpr(b, NO_COMMA); // grab the value b.add(parserLine, PUT); // put the value into the object @@ -266,11 +328,25 @@ class Parser extends Lexer implements ByteCodes { break; } case NAME: { - b.add(parserLine, TOPSCOPE); - b.add(parserLine, LITERAL, string); - continueExprAfterAssignable(b,minPrecedence); + JS varKey = scopeKey(string); + if(varKey == null) { + b.add(parserLine, GLOBALSCOPE); + b.add(parserLine, LITERAL, JSString.intern(string)); + } + continueExprAfterAssignable(b,minPrecedence,varKey); break; } + case CASCADE: { + if(peekToken() == ASSIGN) { + consume(ASSIGN); + startExpr(b, precedence[ASSIGN]); + b.add(parserLine, CASCADE, JS.T); + } else { + b.add(parserLine, CASCADE, JS.F); + } + break; + } + case FUNCTION: { consume(LP); int numArgs = 0; @@ -278,27 +354,20 @@ class Parser extends Lexer implements ByteCodes { b.add(parserLine, NEWFUNCTION, b2); // function prelude; arguments array is already on the stack - b2.add(parserLine, TOPSCOPE); - b2.add(parserLine, SWAP); - b2.add(parserLine, DECLARE, "arguments"); // declare arguments (equivalent to 'var arguments;') - b2.add(parserLine, SWAP); // set this.arguments and leave the value on the stack - b2.add(parserLine, PUT); + scopePush(b2); + scopeDeclare("arguments"); + b2.add(parserLine, SCOPEPUT,scopeKey("arguments")); while(peekToken() != RP) { // run through the list of argument names numArgs++; if (peekToken() == NAME) { consume(NAME); // a named argument - String varName = string; 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, TOPSCOPE); - b2.add(parserLine, SWAP); - b2.add(parserLine, DECLARE, varName); // declare the name - b2.add(parserLine, SWAP); - b2.add(parserLine, PUT); - b2.add(parserLine, POP); // pop the value - b2.add(parserLine, POP); // pop the scope + scopeDeclare(string); + b2.add(parserLine, SCOPEPUT, scopeKey(string)); + b2.add(parserLine, POP); } if (peekToken() == RP) break; consume(COMMA); @@ -307,13 +376,13 @@ class Parser extends Lexer implements ByteCodes { b2.numFormalArgs = numArgs; b2.add(parserLine, POP); // pop off the arguments array - b2.add(parserLine, POP); // pop off TOPSCOPE if(peekToken() != LC) throw pe("JSFunctions must have a block surrounded by curly brackets"); parseBlock(b2, null); // the function body - + + scopePop(b2); b2.add(parserLine, LITERAL, null); // in case we "fall out the bottom", return NULL b2.add(parserLine, RETURN); @@ -364,12 +433,12 @@ class Parser extends Lexer implements ByteCodes { * expression that modifies the assignable. This method always * decreases the stack depth by exactly one element. */ - private void continueExprAfterAssignable(JSFunction b,int minPrecedence) throws IOException { + private void continueExprAfterAssignable(JSFunction b,int minPrecedence, JS varKey) throws IOException { int saveParserLine = parserLine; - _continueExprAfterAssignable(b,minPrecedence); + _continueExprAfterAssignable(b,minPrecedence,varKey); parserLine = saveParserLine; } - private void _continueExprAfterAssignable(JSFunction b,int minPrecedence) throws IOException { + private void _continueExprAfterAssignable(JSFunction b,int minPrecedence, JS varKey) throws IOException { if (b == null) throw new Error("got null b; this should never happen"); int tok = getToken(); if (minPrecedence != -1 && (precedence[tok] < minPrecedence || (precedence[tok] == minPrecedence && !isRightAssociative[tok]))) @@ -393,52 +462,71 @@ class Parser extends Lexer implements ByteCodes { */ 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); + if (tok != ADD_TRAP && tok != DEL_TRAP) + b.add(parserLine, varKey == null ? GET_PRESERVE : SCOPEGET, varKey); startExpr(b, precedence[tok]); 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, PUT); - b.add(parserLine, SWAP); - b.add(parserLine, POP); + if(varKey == null) { + b.add(parserLine, PUT); + b.add(parserLine, SWAP); + b.add(parserLine, POP); + } else { + b.add(parserLine, SCOPEPUT, varKey); + } } else { + if(varKey != null) throw pe("cannot place traps on local variables"); b.add(parserLine, tok); } break; } case INC: case DEC: { // postfix - 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, 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 + 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, 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 + } 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, SCOPEPUT, varKey); + } break; } case ASSIGN: { startExpr(b, precedence[tok]); - b.add(parserLine, PUT); - b.add(parserLine, SWAP); - b.add(parserLine, POP); + if(varKey == null) { + b.add(parserLine, PUT); + b.add(parserLine, SWAP); + b.add(parserLine, POP); + } else { + b.add(parserLine, SCOPEPUT, varKey); + } break; } case LP: { - // Method calls are implemented by doing a GET_PRESERVE // first. If the object supports method calls, it will // return JS.METHOD - int n = parseArgs(b, 2); - b.add(parserLine, GET_PRESERVE); - b.add(parserLine, CALLMETHOD, JS.N(n)); + b.add(parserLine, varKey == null ? GET_PRESERVE : SCOPEGET, varKey); + int n = parseArgs(b); + b.add(parserLine, varKey == null ? CALLMETHOD : CALL, JS.N(n)); break; } default: { pushBackToken(); - if(b.get(b.size-1) == LITERAL && b.getArg(b.size-1) != null) + if(varKey != null) + b.add(parserLine, SCOPEGET, varKey); + else if(b.get(b.size-1) == LITERAL && b.getArg(b.size-1) != null) b.set(b.size-1,GET,b.getArg(b.size-1)); else b.add(parserLine, GET); @@ -475,7 +563,7 @@ class Parser extends Lexer implements ByteCodes { switch (tok) { case LP: { // invocation (not grouping) - int n = parseArgs(b, 1); + int n = parseArgs(b); b.add(parserLine, CALL, JS.N(n)); break; } @@ -515,14 +603,14 @@ class Parser extends Lexer implements ByteCodes { } else { consume(NAME); } - b.add(parserLine, LITERAL, string); - continueExprAfterAssignable(b,minPrecedence); + b.add(parserLine, LITERAL, JSString.intern(string)); + continueExprAfterAssignable(b,minPrecedence,null); break; } case LB: { // subscripting (not array constructor) startExpr(b, -1); consume(RB); - continueExprAfterAssignable(b,minPrecedence); + continueExprAfterAssignable(b,minPrecedence,null); break; } case HOOK: { @@ -553,14 +641,12 @@ class Parser extends Lexer implements ByteCodes { } // parse a set of comma separated function arguments, assume LP has already been consumed - // if swap is true, (because the function is already on the stack) we will SWAP after each argument to keep it on top - private int parseArgs(JSFunction b, int pushdown) throws IOException { + private int parseArgs(JSFunction b) throws IOException { int i = 0; while(peekToken() != RP) { i++; if (peekToken() != COMMA) { startExpr(b, NO_COMMA); - b.add(parserLine, SWAP, JS.N(pushdown)); if (peekToken() == RP) break; } consume(COMMA); @@ -613,22 +699,19 @@ class Parser extends Lexer implements ByteCodes { break; } case VAR: { - b.add(parserLine, TOPSCOPE); // push the current scope while(true) { consume(NAME); - b.add(parserLine, DECLARE, string); // declare it + String var = string; + scopeDeclare(var); if (peekToken() == ASSIGN) { // if there is an '=' after the variable name consume(ASSIGN); startExpr(b, NO_COMMA); - b.add(parserLine, PUT); // assign it + b.add(parserLine, SCOPEPUT, scopeKey(var)); // assign it b.add(parserLine, POP); // clean the stack - } else { - b.add(parserLine, POP); // pop the string pushed by declare - } + } if (peekToken() != COMMA) break; consume(COMMA); } - b.add(parserLine, POP); // pop off the topscope if ((mostRecentlyReadToken != RC || peekToken() == SEMI) && peekToken() != -1 && mostRecentlyReadToken != SEMI) consume(SEMI); break; } @@ -749,7 +832,7 @@ class Parser extends Lexer implements ByteCodes { // extended Ibex catch block: catch(e faultCode "foo.bar.baz") consume(NAME); b.add(parserLine, DUP); - b.add(parserLine, LITERAL, string); + b.add(parserLine, LITERAL, JSString.intern(string)); b.add(parserLine, GET); b.add(parserLine, DUP); b.add(parserLine, LITERAL, null); @@ -782,17 +865,12 @@ class Parser extends Lexer implements ByteCodes { } consume(RP); // the exception is on top of the stack; put it to the chosen name - b.add(parserLine, NEWSCOPE); - b.add(parserLine, TOPSCOPE); - b.add(parserLine, SWAP); - b.add(parserLine, LITERAL,exceptionVar); - b.add(parserLine, DECLARE); - b.add(parserLine, SWAP); - b.add(parserLine, PUT); - b.add(parserLine, POP); + scopePush(b); + scopeDeclare(exceptionVar); + b.add(parserLine, SCOPEPUT, scopeKey(exceptionVar)); b.add(parserLine, POP); parseBlock(b, null); - b.add(parserLine, OLDSCOPE); + scopePop(b); b.add(parserLine, JMP); catchEnds.addElement(new Integer(b.size-1)); @@ -849,70 +927,51 @@ class Parser extends Lexer implements ByteCodes { consume(RP); 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); - // Stack is now: index, keys, obj, LoopMarker - b.add(parserLine, LITERAL, JS.ZERO); - b.add(parserLine, LT); - // Stack is now index<0, index, keys, obj, LoopMarker, ... + b.add(parserLine,SWAP); // get the keys enumeration object on top + b.add(parserLine,DUP); + b.add(parserLine,GET,JS.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.add(parserLine,DUP); + b.add(parserLine,GET,JS.S("nextElement")); + + scopePush(b); - 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); + if(hadVar) scopeDeclare(varName); + JS varKey = scopeKey(varName); - b.add(parserLine, NEWSCOPE); - if(hadVar) { - b.add(parserLine, DECLARE, varName); - b.add(parserLine, POP); + if(varKey == null) { + b.add(parserLine,GLOBALSCOPE); + b.add(parserLine,SWAP); + b.add(parserLine, LITERAL, JSString.intern(varName)); + b.add(parserLine,SWAP); + b.add(parserLine,PUT); + b.add(parserLine,POP); + } else { + b.add(parserLine, SCOPEPUT, varKey); } - - // 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)); + b.add(parserLine,POP); // pop the put'ed value + b.add(parserLine,SWAP); // put CallMarker back into place parseStatement(b, null); - b.add(parserLine, OLDSCOPE); + scopePop(b); 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 - + b.add(parserLine, POP); } else { if (hadVar) pushBackToken(VAR, null); // yeah, this actually matters - b.add(parserLine, NEWSCOPE); // grab a fresh scope + scopePush(b); // grab a fresh scope parseStatement(b, null); // initializer JSFunction e2 = // we need to put the incrementor before the test @@ -920,7 +979,7 @@ class Parser extends Lexer implements ByteCodes { if (peekToken() != SEMI) startExpr(e2, -1); else - e2.add(parserLine, JSFunction.LITERAL, Boolean.TRUE); // handle the for(foo;;foo) case + e2.add(parserLine, JSFunction.LITERAL, JS.T); // handle the for(foo;;foo) case consume(SEMI); if (label != null) b.add(parserLine, LABEL, label); b.add(parserLine, LOOP); @@ -942,7 +1001,7 @@ class Parser extends Lexer implements ByteCodes { 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.add(parserLine, OLDSCOPE); // get our scope back + scopePop(b); // get our scope back } break; } @@ -966,9 +1025,9 @@ class Parser extends Lexer implements ByteCodes { case LC: { // blocks are statements too pushBackToken(); - b.add(parserLine, NEWSCOPE); + scopePush(b); parseBlock(b, label); - b.add(parserLine, OLDSCOPE); + scopePop(b); break; }