X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fjs%2FParser.java;h=81215fcd28c3be76f56fd410eabd6ba9b4177b43;hb=f3ad8b6cba43f3c5364dc2cd9f1d050c1f48f167;hp=9f62439495f61592b84cdae5c52414484970adca;hpb=7c2cac7d060d75681c2043a05a4cbd7a1b415623;p=org.ibex.js.git
diff --git a/src/org/ibex/js/Parser.java b/src/org/ibex/js/Parser.java
index 9f62439..81215fc 100644
--- a/src/org/ibex/js/Parser.java
+++ b/src/org/ibex/js/Parser.java
@@ -1,8 +1,12 @@
-// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
+// Copyright 2000-2005 the Contributors, as shown in the revision logs.
+// Licensed under the Apache Public Source License 2.0 ("the License").
+// You may not use this file except in compliance with the License.
+
package org.ibex.js;
-import org.ibex.util.*;
import java.io.*;
+import java.util.*;
+import org.ibex.util.*;
/**
* Parses a stream of lexed tokens into a tree of JSFunction's.
@@ -69,16 +73,15 @@ 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 {
- 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);
}
-
// Statics ////////////////////////////////////////////////////////////
static byte[] precedence = new byte[MAX_TOKEN + 1];
@@ -133,8 +136,68 @@ class Parser extends Lexer implements ByteCodes {
precedence[DOT] = precedence[LB] = precedence[LP] = precedence[INC] = precedence[DEC] = 15;
}
-
+ // Local variable management
+ Basket.Array scopeStack = new Basket.Array();
+ static class ScopeInfo {
+ int base;
+ int end;
+ int newScopeInsn;
+ Map mapping = new HashMap();
+ }
+ Map globalCache = new HashMap();
+ 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.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.peek();
+ if(si.mapping.get(name) != null) throw pe("" + name + " already declared in this scope");
+ si.mapping.put(name,JSU.N(si.end++));
+ globalCache.put(name,null);
+ }
+ void scopePush(JSFunction b) {
+ ScopeInfo prev = (ScopeInfo) scopeStack.peek();
+ 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,JSU.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.clear();
+ 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,30 +232,28 @@ 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, 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, JS.B(tok == TRUE)); 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, 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, 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
@@ -202,13 +263,21 @@ class Parser extends Lexer implements ByteCodes {
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: { // negative literal (like "3 * -1")
- consume(NUMBER);
- b.add(parserLine, LITERAL, JS.N(number.doubleValue() * -1));
+ case SUB: case ADD: {
+ if(peekToken() == NUMBER) { // literal
+ consume(NUMBER);
+ b.add(parserLine, LITERAL, JSU.N(number.doubleValue() * (tok == SUB ? -1 : 1)));
+ } else { // unary +/- operator
+ 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, JSU.ZERO); // HACK to force expr into a numeric context
+ b.add(parserLine, SUB);
+ }
break;
}
case LP: { // grouping (not calling)
@@ -219,18 +288,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);
- 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, GET_PRESERVE, Boolean.TRUE);
+ 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 {
+ b.add(parserLine, PUT, null);
+ b.add(parserLine, SWAP, null);
+ b.add(parserLine, POP, null);
+ }
break;
}
case BANG: case BITNOT: case TYPEOF: {
@@ -245,7 +319,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
@@ -258,11 +332,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, JSU.T);
+ } else {
+ b.add(parserLine, CASCADE, JSU.F);
+ }
break;
}
+
case FUNCTION: {
consume(LP);
int numArgs = 0;
@@ -270,27 +358,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.formalArgs.push(varName);
+
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
+ 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 (peekToken() == RP) break;
consume(COMMA);
@@ -299,13 +380,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);
@@ -356,12 +437,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])))
@@ -385,52 +466,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);
+ b.add(parserLine, tok - 1, tok-1==ADD ? JSU.N(2) : null);
+ 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, 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, 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, JSU.ONE);
+ b.add(parserLine, tok == INC ? ADD : SUB, JSU.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, JSU.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);
@@ -467,8 +567,8 @@ class Parser extends Lexer implements ByteCodes {
switch (tok) {
case LP: { // invocation (not grouping)
- int n = parseArgs(b, 1);
- b.add(parserLine, CALL, JS.N(n));
+ int n = parseArgs(b);
+ b.add(parserLine, CALL, JSU.N(n));
break;
}
case BITOR: case BITXOR: case BITAND: case SHEQ: case SHNE: case LSH:
@@ -487,17 +587,17 @@ class Parser extends Lexer implements ByteCodes {
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: {
@@ -507,26 +607,26 @@ 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: {
- 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: {
@@ -545,14 +645,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);
@@ -605,22 +703,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;
}
@@ -629,18 +724,18 @@ class Parser extends Lexer implements ByteCodes {
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: {
@@ -650,12 +745,12 @@ class Parser extends Lexer implements ByteCodes {
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: {
@@ -673,10 +768,10 @@ class Parser extends Lexer implements ByteCodes {
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);
@@ -688,7 +783,7 @@ class Parser extends Lexer implements ByteCodes {
} 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;
}
@@ -700,12 +795,12 @@ class Parser extends Lexer implements ByteCodes {
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;
}
@@ -725,7 +820,7 @@ class Parser extends Lexer implements ByteCodes {
int catchJMPDistance = -1;
if (peekToken() == CATCH) {
- Vec catchEnds = new Vec();
+ Basket.List catchEnds = new Basket.Array();
boolean catchAll = false;
catchJMPDistance = b.size - tryInsn;
@@ -741,7 +836,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);
@@ -774,22 +869,17 @@ 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));
+ 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
}
@@ -797,8 +887,8 @@ class Parser extends Lexer implements ByteCodes {
b.add(parserLine, THROW);
for(int i=0;i= 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.set(size, JSU.N(b.size - size));
+ 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
@@ -912,29 +983,29 @@ 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, 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
- b.add(parserLine, OLDSCOPE); // get our scope back
+ scopePop(b); // get our scope back
}
break;
}
@@ -958,9 +1029,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;
}