new js api
[org.ibex.core.git] / src / org / ibex / js / Parser.java
index f36eda4..c6c9d9e 100644 (file)
@@ -72,7 +72,7 @@ class Parser extends Lexer implements ByteCodes {
     public Parser(Reader r, String sourceName, int line) throws IOException { super(r, sourceName, line); }
 
     /** for debugging */
-    public static void main(String[] s) throws Exception {
+    public static void main(String[] s) throws IOException {
         JS block = JS.fromReader("stdin", 0, new InputStreamReader(System.in));
         if (block == null) return;
         System.out.println(block);
@@ -169,18 +169,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, LITERAL, JSString.intern(""));
             b.add(parserLine, GET);
-            b.add(parserLine, LITERAL, string);
+            b.add(parserLine, LITERAL, JSString.intern(string));
             b.add(parserLine, GET);
             continueExpr(b, minPrecedence);
             break;
@@ -192,7 +192,6 @@ class Parser extends Lexer implements ByteCodes {
             int i = 0;
             if (peekToken() != RB)
                 while(true) {                                               // iterate over the initialization values
-                    int size = b.size;
                     b.add(parserLine, LITERAL, JS.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,]
@@ -207,9 +206,17 @@ class Parser extends Lexer implements ByteCodes {
             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, JS.N(number.doubleValue() * (tok == SUB ? -1 : 1)));
+            } else { // unary +/- operator
+                if(tok == SUB) b.add(parserLine, LITERAL, JS.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
+                b.add(parserLine, SUB);
+            }
             break;
         }
         case LP: {  // grouping (not calling)
@@ -246,7 +253,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
@@ -260,7 +267,7 @@ class Parser extends Lexer implements ByteCodes {
         }
         case NAME: {
             b.add(parserLine, TOPSCOPE);
-            b.add(parserLine, LITERAL, string);
+            b.add(parserLine, LITERAL, JSString.intern(string));
             continueExprAfterAssignable(b,minPrecedence);
             break;
         }
@@ -273,7 +280,7 @@ class Parser extends Lexer implements ByteCodes {
             // 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, DECLARE, JSString.intern("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);
 
@@ -281,7 +288,7 @@ class Parser extends Lexer implements ByteCodes {
                 numArgs++;
                 if (peekToken() == NAME) {
                     consume(NAME);                                        // a named argument
-                    String varName = string;
+                    JS varName = JSString.intern(string);
                     
                     b2.add(parserLine, DUP);                              // dup the args array 
                     b2.add(parserLine, GET, JS.N(numArgs - 1));   // retrieve it from the arguments array
@@ -318,7 +325,7 @@ class Parser extends Lexer implements ByteCodes {
         // attempt to continue the expression
         continueExpr(b, minPrecedence);
     }
-
+    /*
     private Grammar parseGrammar(Grammar g) throws IOException {
         int tok = getToken();
         if (g != null)
@@ -349,7 +356,7 @@ class Parser extends Lexer implements ByteCodes {
         if (g == null) return parseGrammar(g0);
         return parseGrammar(new Grammar.Juxtaposition(g, g0));
     }
-
+    */
     /**
      *  Assuming that a complete assignable (lvalue) has just been
      *  parsed and the object and key are on the stack,
@@ -369,7 +376,7 @@ class Parser extends Lexer implements ByteCodes {
             // force the default case
             tok = -1;
         switch(tok) {
-
+            /*
         case GRAMMAR: {
             b.add(parserLine, GET_PRESERVE);
             Grammar g = parseGrammar(null);
@@ -383,15 +390,13 @@ class Parser extends Lexer implements ByteCodes {
             b.add(parserLine, PUT);
             break;
         }
-
+            */
         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);
             
             startExpr(b,  precedence[tok]);
             
-            int size = b.size;
-            
             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);
@@ -411,7 +416,7 @@ class Parser extends Lexer implements ByteCodes {
             b.add(parserLine, SWAP, null);
             b.add(parserLine, POP, null);
             b.add(parserLine, LITERAL, JS.N(1));
-            b.add(parserLine, tok == INC ? SUB : ADD, null);   // undo what we just did, since this is postfix
+            b.add(parserLine, tok == INC ? SUB : ADD, JS.N(2));   // undo what we just did, since this is postfix
             break;
         }
         case ASSIGN: {
@@ -494,7 +499,7 @@ class Parser extends Lexer implements ByteCodes {
             break;
         }
         case OR: case AND: {
-            b.add(parserLine, tok == AND ? b.JF : b.JT, JS.ZERO);       // test to see if we can short-circuit
+            b.add(parserLine, tok == AND ? JSFunction.JF : JSFunction.JT, JS.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
@@ -510,7 +515,7 @@ class Parser extends Lexer implements ByteCodes {
             } else {
                 consume(NAME);
             }
-            b.add(parserLine, LITERAL, string);
+            b.add(parserLine, LITERAL, JSString.intern(string));
             continueExprAfterAssignable(b,minPrecedence);
             break;
         }
@@ -611,7 +616,7 @@ class Parser extends Lexer implements ByteCodes {
             b.add(parserLine, TOPSCOPE);                         // push the current scope
             while(true) {
                 consume(NAME);
-                b.add(parserLine, DECLARE, string);               // declare it
+                b.add(parserLine, DECLARE, JSString.intern(string)); // declare it
                 if (peekToken() == ASSIGN) {                     // if there is an '=' after the variable name
                     consume(ASSIGN);
                     startExpr(b, NO_COMMA);
@@ -743,9 +748,8 @@ class Parser extends Lexer implements ByteCodes {
                     if (peekToken() != RP) {
                         // extended Ibex catch block: catch(e faultCode "foo.bar.baz")
                         consume(NAME);
-                        String propName = string;
                         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);
@@ -781,7 +785,7 @@ class Parser extends Lexer implements ByteCodes {
                     b.add(parserLine, NEWSCOPE);
                     b.add(parserLine, TOPSCOPE);
                     b.add(parserLine, SWAP);
-                    b.add(parserLine, LITERAL,exceptionVar);
+                    b.add(parserLine, LITERAL,JSString.intern(exceptionVar));
                     b.add(parserLine, DECLARE);
                     b.add(parserLine, SWAP);
                     b.add(parserLine, PUT);
@@ -839,37 +843,73 @@ class Parser extends Lexer implements ByteCodes {
             pushBackToken(tok, varName);
             
             if (forIn) {
-                b.add(parserLine, NEWSCOPE);                             // for-loops always create new scopes
-                b.add(parserLine, LITERAL, varName);                     // declare the new variable
-                b.add(parserLine, DECLARE);
-                    
-                b.add(parserLine, LOOP);                                 // we actually only add this to ensure that BREAK works
-                b.add(parserLine, POP);                                  // discard the first-iteration indicator
-                int size = b.size;
                 consume(NAME);
                 consume(IN);
-                startExpr(b, -1);
-                b.add(parserLine, PUSHKEYS);                             // push the keys as an array; check the length
-                b.add(parserLine, LITERAL, "length");
-                b.add(parserLine, GET);
+                startExpr(b,-1);
                 consume(RP);
-                    
-                b.add(parserLine, LITERAL, JS.N(1));              // decrement the length
+                
+                b.add(parserLine, PUSHKEYS);
+                b.add(parserLine, DUP); 
+                b.add(parserLine, LITERAL, JSString.intern("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);
-                b.add(parserLine, LITERAL, JS.ZERO);              // see if we've exhausted all the elements
+                // Stack is now: index, keys, obj, LoopMarker
+                b.add(parserLine, LITERAL, JS.ZERO);
                 b.add(parserLine, LT);
-                b.add(parserLine, JF, JS.N(2));
-                b.add(parserLine, BREAK);                                // if we have, then BREAK
-                b.add(parserLine, GET_PRESERVE);                         // get the key out of the keys array
-                b.add(parserLine, LITERAL, varName);
-                b.add(parserLine, PUT);                                  // write it to this[varName]                          
-                parseStatement(b, null);                                 // do some stuff
-                b.add(parserLine, CONTINUE);                             // continue if we fall out the bottom
-
-                b.set(size - 1, JS.N(b.size - size + 1));       // BREAK to here
-                b.add(parserLine, OLDSCOPE);                             // restore the scope
-                    
+                // Stack is now index<0, index, keys, obj, LoopMarker, ...
+                
+                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);
+                
+                b.add(parserLine, NEWSCOPE);
+                if(hadVar) {
+                    b.add(parserLine, DECLARE, JSString.intern(varName));
+                    b.add(parserLine, POP);
+                }
+                
+                // 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, JSString.intern(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));
+                
+                parseStatement(b, null);
+                
+                b.add(parserLine, OLDSCOPE);
+                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
+                
             } else {
                 if (hadVar) pushBackToken(VAR, null);                    // yeah, this actually matters
                 b.add(parserLine, NEWSCOPE);                             // grab a fresh scope
@@ -880,7 +920,7 @@ class Parser extends Lexer implements ByteCodes {
                 if (peekToken() != SEMI)
                     startExpr(e2, -1);
                 else
-                    e2.add(parserLine, b.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);
@@ -944,7 +984,7 @@ class Parser extends Lexer implements ByteCodes {
 
 
     // ParserException //////////////////////////////////////////////////////////////////////
-    private IOException pe(String s) { return new IOException(sourceName + ":" + parserLine + " " + s); }
+    private IOException pe(String s) { return new IOException(sourceName + ":" + line + " " + s); }
     
 }