renamed Script to JSU
[org.ibex.js.git] / src / org / ibex / js / Parser.java
index 6b3f640..81215fc 100644 (file)
@@ -77,7 +77,7 @@ class Parser extends Lexer implements ByteCodes {
 
     /** for debugging */
     public static void main(String[] s) throws IOException {
-        JS block = Script.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);
     }
@@ -157,7 +157,7 @@ class Parser extends Lexer implements ByteCodes {
     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,Script.N(si.end++));
+        si.mapping.put(name,JSU.N(si.end++));
         globalCache.put(name,null);
     }
     void scopePush(JSFunction b) {
@@ -172,7 +172,7 @@ class Parser extends Lexer implements ByteCodes {
     void scopePop(JSFunction b) {
         ScopeInfo si = (ScopeInfo) scopeStack.pop();
         b.add(parserLine, OLDSCOPE);
-        b.set(si.newScopeInsn,Script.N((si.base<<16)|((si.end-si.base)<<0))); 
+        b.set(si.newScopeInsn,JSU.N((si.base<<16)|((si.end-si.base)<<0))); 
     }
     
     
@@ -232,28 +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, Script.N(number)); 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, tok == TRUE ? Script.T : Script.F); 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, GLOBALSCOPE);
-            b.add(parserLine, GET, Script.S("",true));
-            b.add(parserLine, LITERAL, Script.S(string,true));
+            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, Script.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, Script.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
@@ -263,19 +263,19 @@ class Parser extends Lexer implements ByteCodes {
                     if (peekToken() == RB) break;
                     consume(COMMA);
                 }
-            b.set(size0 - 1, Script.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: case ADD: {
             if(peekToken() == NUMBER) {   // literal
                 consume(NUMBER);
-                b.add(parserLine, LITERAL, Script.N(number.doubleValue() * (tok == SUB ? -1 : 1)));
+                b.add(parserLine, LITERAL, JSU.N(number.doubleValue() * (tok == SUB ? -1 : 1)));
             } else { // unary +/- operator
-                if(tok == SUB) b.add(parserLine, LITERAL, Script.ZERO);
+                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, Script.ZERO); // HACK to force expr into a numeric context
+                if(tok == ADD) b.add(parserLine, LITERAL, JSU.ZERO); // HACK to force expr into a numeric context
                 b.add(parserLine, SUB);
             }
             break;
@@ -296,8 +296,8 @@ class Parser extends Lexer implements ByteCodes {
             else if(!sg)
                 throw pe("prefixed increment/decrement can only be performed on a valid assignment target");
             if(!sg) b.add(parserLine, GET_PRESERVE, Boolean.TRUE);
-            b.add(parserLine, LITERAL, Script.N(1));
-            b.add(parserLine, tok == INC ? ADD : SUB, Script.N(2));
+            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 {
@@ -344,9 +344,9 @@ class Parser extends Lexer implements ByteCodes {
             if(peekToken() == ASSIGN) {
                 consume(ASSIGN);
                 startExpr(b, precedence[ASSIGN]);
-                b.add(parserLine, CASCADE, Script.T);
+                b.add(parserLine, CASCADE, JSU.T);
             } else {
-                b.add(parserLine, CASCADE, Script.F);
+                b.add(parserLine, CASCADE, JSU.F);
             }
             break;
         }
@@ -368,7 +368,7 @@ class Parser extends Lexer implements ByteCodes {
                     consume(NAME);                                        // a named argument
                     
                     b2.add(parserLine, DUP);                              // dup the args array 
-                    b2.add(parserLine, GET, Script.N(numArgs - 1));   // retrieve it from the arguments array
+                    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);
@@ -473,7 +473,7 @@ class Parser extends Lexer implements ByteCodes {
             
             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 ? Script.N(2) : null);
+                b.add(parserLine, tok - 1, tok-1==ADD ? JSU.N(2) : null);
                 if(varKey == null) {
                     b.add(parserLine, PUT);
                     b.add(parserLine, SWAP);
@@ -490,18 +490,18 @@ class Parser extends Lexer implements ByteCodes {
         case INC: case DEC: { // postfix
             if(varKey == null) {
                 b.add(parserLine, GET_PRESERVE, Boolean.TRUE);
-                b.add(parserLine, LITERAL, Script.N(1));
-                b.add(parserLine, tok == INC ? ADD : SUB, Script.N(2));
+                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, Script.N(1));
-                b.add(parserLine, tok == INC ? SUB : ADD, Script.N(2));   // undo what we just did, since this is postfix
+                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, Script.ONE);
-                b.add(parserLine, tok == INC ? ADD : SUB, Script.N(2));
+                b.add(parserLine, LITERAL, JSU.ONE);
+                b.add(parserLine, tok == INC ? ADD : SUB, JSU.N(2));
                 b.add(parserLine, SCOPEPUT, varKey);
             }
             break;
@@ -523,7 +523,7 @@ class Parser extends Lexer implements ByteCodes {
             // return JS.METHOD
             b.add(parserLine, varKey == null ? GET_PRESERVE : SCOPEGET, varKey);
             int n = parseArgs(b);
-            b.add(parserLine, varKey == null ? CALLMETHOD : CALL, Script.N(n));
+            b.add(parserLine, varKey == null ? CALLMETHOD : CALL, JSU.N(n));
             break;
         }
         default: {
@@ -568,7 +568,7 @@ class Parser extends Lexer implements ByteCodes {
         switch (tok) {
         case LP: {  // invocation (not grouping)
             int n = parseArgs(b);
-            b.add(parserLine, CALL, Script.N(n));
+            b.add(parserLine, CALL, JSU.N(n));
             break;
         }
         case BITOR: case BITXOR: case BITAND: case SHEQ: case SHNE: case LSH:
@@ -587,17 +587,17 @@ class Parser extends Lexer implements ByteCodes {
                 nextTok = getToken();
             } while(nextTok == tok);
             pushBackToken();
-            b.add(parserLine, tok, Script.N(count));
+            b.add(parserLine, tok, JSU.N(count));
             break;
         }
         case OR: case AND: {
-            b.add(parserLine, tok == AND ? JSFunction.JF : JSFunction.JT, Script.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, Script.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 ?
-                  Script.B(false) : Script.B(true));                     // target of the short-circuit jump is here
-            b.set(size - 1, Script.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: {
@@ -618,15 +618,15 @@ class Parser extends Lexer implements ByteCodes {
             break;
         }
         case HOOK: {
-            b.add(parserLine, JF, Script.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, Script.ZERO);               // if true, jump *over* the if-false expression     
-            b.set(size - 1, Script.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, Script.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: {
@@ -724,18 +724,18 @@ class Parser extends Lexer implements ByteCodes {
             startExpr(b, -1);
             consume(RP);
             
-            b.add(parserLine, JF, Script.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, Script.ZERO);               // if we took the true-block, jump over the else-block
-                b.set(size - 1, Script.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, Script.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: {
@@ -745,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, Script.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, Script.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: {
@@ -768,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, Script.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, Script.N(1 + b.size - size));
+                    b.set(size - 1, JSU.N(1 + b.size - size));
                 } else if (peekToken() == DEFAULT) {
                     consume(DEFAULT);
                     consume(COLON);
@@ -783,7 +783,7 @@ class Parser extends Lexer implements ByteCodes {
                 } else {
                     throw pe("expected CASE, DEFAULT, or RC; got " + codeToString[peekToken()]);
                 }
-            b.set(size0 - 1, Script.N(b.size - size0 + 1));      // end of the loop
+            b.set(size0 - 1, JSU.N(b.size - size0 + 1));      // end of the loop
             break;
         }
             
@@ -795,12 +795,12 @@ class Parser extends Lexer implements ByteCodes {
             consume(WHILE);
             consume(LP);
             startExpr(b, -1);
-            b.add(parserLine, JT, Script.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, Script.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;
         }
             
@@ -879,7 +879,7 @@ class Parser extends Lexer implements ByteCodes {
                     b.add(parserLine, JMP);
                     catchEnds.add(new Integer(b.size-1));
                     
-                    for(int i=0; i<3; i++) if (writebacks[i] != -1) b.set(writebacks[i], Script.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
                 }
                 
@@ -888,7 +888,7 @@ class Parser extends Lexer implements ByteCodes {
                 
                 for(int i=0;i<catchEnds.size();i++) {
                     int n = ((Integer)catchEnds.get(i)).intValue();
-                    b.set(n, Script.N(b.size-n));
+                    b.set(n, JSU.N(b.size-n));
                 }
                 
                 // pop the try and catch markers
@@ -897,7 +897,7 @@ class Parser extends Lexer implements ByteCodes {
             }
                         
             // jump here if no exception was thrown
-            b.set(successJMPInsn, Script.N(b.size - successJMPInsn)); 
+            b.set(successJMPInsn, JSU.N(b.size - successJMPInsn)); 
                         
             int finallyJMPDistance = -1;
             if (peekToken() == FINALLY) {
@@ -938,14 +938,14 @@ class Parser extends Lexer implements ByteCodes {
                 
                 b.add(parserLine,SWAP); // get the keys enumeration object on top
                 b.add(parserLine,DUP);
-                b.add(parserLine,GET,Script.S("hasMoreElements"));
+                b.add(parserLine,GET,JSU.S("hasMoreElements"));
                 int size2 = b.size;
                 b.add(parserLine,JT);
                 b.add(parserLine,SWAP);
                 b.add(parserLine,BREAK);
-                b.set(size2, Script.N(b.size - size2));
+                b.set(size2, JSU.N(b.size - size2));
                 b.add(parserLine,DUP);
-                b.add(parserLine,GET,Script.S("nextElement"));
+                b.add(parserLine,GET,JSU.S("nextElement"));
 
                 scopePush(b);
                 
@@ -970,7 +970,7 @@ class Parser extends Lexer implements ByteCodes {
                 scopePop(b);
                 b.add(parserLine, CONTINUE);
                 // jump here on break
-                b.set(size, Script.N(b.size - size));
+                b.set(size, JSU.N(b.size - size));
                 
                 b.add(parserLine, POP);
             } else {
@@ -983,27 +983,27 @@ class Parser extends Lexer implements ByteCodes {
                 if (peekToken() != SEMI)
                     startExpr(e2, -1);
                 else
-                    e2.add(parserLine, JSFunction.LITERAL, Script.T);         // 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, Script.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, Script.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, Script.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, Script.N(b.size - size2 + 1));     // end of the loop
+                b.set(size2 - 1, JSU.N(b.size - size2 + 1));     // end of the loop
                     
                 scopePop(b);                            // get our scope back
             }