jswitch
authorbrian <brian@brianweb.net>
Wed, 7 Jul 2004 08:09:29 +0000 (08:09 +0000)
committerbrian <brian@brianweb.net>
Wed, 7 Jul 2004 08:09:29 +0000 (08:09 +0000)
darcs-hash:20040707080929-24bed-a4aa577e662c83ab732f93fb81f7be3c583fbd98.gz

src/org/ibex/js/Interpreter.java
src/org/ibex/js/JS.java
src/org/ibex/js/JSArray.java
src/org/ibex/js/JSDate.java
src/org/ibex/js/JSMath.java
src/org/ibex/js/JSPrimitive.java
src/org/ibex/js/JSRegexp.java
src/org/ibex/js/JSScope.java
src/org/ibex/util/Preprocessor.java

index 6f163fa..3b4dd28 100644 (file)
@@ -614,13 +614,11 @@ class Interpreter implements ByteCodes, Tokens {
             super(parent); this.t = t;
         }
         public JS get(JS key) throws JSExn {
-            if(JS.isString(key)) {
-                //#switch(JS.toString(key))
-                case "trapee": return t.target;
-                case "callee": return t.f;
-                case "trapname": return t.key;
-                //#end
-            }
+            //#jswitch(key)
+            case "trapee": return t.target;
+            case "callee": return t.f;
+            case "trapname": return t.key;
+            //#end
             return super.get(key);
         }
     }
@@ -651,7 +649,7 @@ class Interpreter implements ByteCodes, Tokens {
                     default: return n>= 0 && n < nargs ? rest[n-3] : null;
                 }
             }
-            //#switch(JS.toString(key))
+            //#jswitch(key)
             case "callee": return callee;
             case "length": return JS.N(nargs);
             //#end
index ad13a40..65ba3f6 100644 (file)
@@ -74,13 +74,7 @@ public abstract class JS {
         public final void replaceNode(int index, Object o) { bt().replaceNode(index,o); }
         public final int indexNode(Object o) { return bt().indexNode(o); }
     }
-    
-    // FEATURE: JS.StringKeys
-    /* public static StringKeys extends JS {
-        public JS get(JS key) { return JS.isString(key) ? get(JS.toString(key) : null; }
-        ...
-    */
-    
+        
     JS _unclone() { return this; }
     
     public interface Cloneable { }
@@ -120,7 +114,7 @@ public abstract class JS {
         public final JS nextElement() throws JSExn { return !done ? _nextElement() : parent != null ? parent.nextElement() : null; }
         
         public JS get(JS key) throws JSExn {
-            //#switch(JS.toString(key))
+            //#jswitch(key)
             case "hasMoreElements": return B(hasMoreElements());
             case "nextElement": return nextElement();
             //#end
index 8aa5325..d0cd550 100644 (file)
@@ -24,40 +24,38 @@ public class JSArray extends JS.BT {
     }*/
     
     public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
-        if(isString(method)) {
-            //#switch(JS.toString(method))
-            case "pop": {
-                int oldSize = size();
-                if(oldSize == 0) return null;
-                return removeElementAt(oldSize-1);
-            }
-            case "reverse": return reverse();
-            case "toString": return join(",");
-            case "shift":
-                if(length() == 0) return null;
-                return removeElementAt(0);
-            case "join":
-                return join(nargs == 0 ? "," : JS.toString(a0));
-            case "sort":
-                return sort(nargs < 1 ? null : a0);
-            case "slice":
-                int start = toInt(nargs < 1 ? null : a0);
-                int end = nargs < 2 ? length() : toInt(a1);
-                return slice(start, end);
-            case "push": {
-                int oldSize = size();
-                for(int i=0; i<nargs; i++) insertElementAt(i==0?a0:i==1?a1:i==2?a2:rest[i-3],oldSize+i);
-                return N(oldSize + nargs);
-            }
-            case "unshift":
-                for(int i=0; i<nargs; i++) insertElementAt(i==0?a0:i==1?a1:i==2?a2:rest[i-3],i);
-                return N(size());
-            case "splice":
-                JSArray array = new JSArray();
-                for(int i=0; i<nargs; i++) array.addElement(i==0?a0:i==1?a1:i==2?a2:rest[i-3]);
-                return splice(array);
-            //#end
+        //#jswitch(method)
+        case "pop": {
+            int oldSize = size();
+            if(oldSize == 0) return null;
+            return removeElementAt(oldSize-1);
         }
+        case "reverse": return reverse();
+        case "toString": return join(",");
+        case "shift":
+            if(length() == 0) return null;
+            return removeElementAt(0);
+        case "join":
+            return join(nargs == 0 ? "," : JS.toString(a0));
+        case "sort":
+            return sort(nargs < 1 ? null : a0);
+        case "slice":
+            int start = toInt(nargs < 1 ? null : a0);
+            int end = nargs < 2 ? length() : toInt(a1);
+            return slice(start, end);
+        case "push": {
+            int oldSize = size();
+            for(int i=0; i<nargs; i++) insertElementAt(i==0?a0:i==1?a1:i==2?a2:rest[i-3],oldSize+i);
+            return N(oldSize + nargs);
+        }
+        case "unshift":
+            for(int i=0; i<nargs; i++) insertElementAt(i==0?a0:i==1?a1:i==2?a2:rest[i-3],i);
+            return N(size());
+        case "splice":
+            JSArray array = new JSArray();
+            for(int i=0; i<nargs; i++) array.addElement(i==0?a0:i==1?a1:i==2?a2:rest[i-3]);
+            return splice(array);
+        //#end
         return super.callMethod(method, a0, a1, a2, rest, nargs);
     }
         
@@ -67,21 +65,19 @@ public class JSArray extends JS.BT {
             if (i < 0 || i >= size()) return null;
             return elementAt(i);
         }
-        if(isString(key)) {
-            //#switch(JS.toString(key))
-            case "pop": return METHOD;
-            case "reverse": return METHOD;
-            case "toString": return METHOD;
-            case "shift": return METHOD;
-            case "join": return METHOD;
-            case "sort": return METHOD;
-            case "slice": return METHOD;
-            case "push": return METHOD;
-            case "unshift": return METHOD;
-            case "splice": return METHOD;
-            case "length": return N(size());
-            //#end
-        }
+        //#jswitch(key)
+        case "pop": return METHOD;
+        case "reverse": return METHOD;
+        case "toString": return METHOD;
+        case "shift": return METHOD;
+        case "join": return METHOD;
+        case "sort": return METHOD;
+        case "slice": return METHOD;
+        case "push": return METHOD;
+        case "unshift": return METHOD;
+        case "splice": return METHOD;
+        case "length": return N(size());
+        //#end
         return super.get(key);
     }
 
index 600b59d..144e868 100644 (file)
@@ -57,12 +57,10 @@ public class JSDate extends JS {
 
     String coerceToString() { return date_format(date, FORMATSPEC_FULL); }
 
-    public JS callMethod(JS method_, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
-        if(!JS.isString(method_)) return super.callMethod(method_, a0, a1, a2, rest, nargs);
-        String method = JS.toString(method_);
+    public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
         switch(nargs) {
             case 0: {
-                //#switch(method)
+                //#jswitch(method)
                 case "toString": return JS.S(date_format(date, FORMATSPEC_FULL));
                 case "toTimeString": return JS.S(date_format(date, FORMATSPEC_TIME));
                 case "toDateString": return JS.S(date_format(date, FORMATSPEC_DATE));
@@ -91,10 +89,10 @@ public class JSDate extends JS {
                 case "getUTCMilliseconds": return N(msFromTime(date));
                 case "getTimezoneOffset": return N(getTimezoneOffset(date));
                 //#end
-                return super.callMethod(method_, a0, a1, a2, rest, nargs);
+                return super.callMethod(method, a0, a1, a2, rest, nargs);
             }
             case 1: {
-                //#switch(method)
+                //#jswitch(method)
                 case "setTime": return N(this.setTime(toDouble(a0)));
                 case "setYear": return N(this.setYear(toDouble(a0)));
                 //#end
@@ -103,7 +101,7 @@ public class JSDate extends JS {
             default: {
                 JS[] args = new JS[nargs];
                 for(int i=0; i<nargs; i++) args[i] = i==0 ? a0 : i==1 ? a1 : i==2 ? a2 : rest[i-3];
-                //#switch(method)
+                //#jswitch(method)
                 case "setMilliseconds": return N(this.makeTime(args, 1, true));
                 case "setUTCMilliseconds": return N(this.makeTime(args, 1, false));
                 case "setSeconds": return N(this.makeTime(args, 2, true));
@@ -121,13 +119,11 @@ public class JSDate extends JS {
                 //#end
             }
         }
-        return super.callMethod(method_, a0, a1, a2, rest, nargs);
+        return super.callMethod(method, a0, a1, a2, rest, nargs);
     }
 
-    public JS get(JS key_) throws JSExn {
-        if(!JS.isString(key_)) return super.get(key_);
-        String key = JS.toString(key_);
-        //#switch(key)
+    public JS get(JS key) throws JSExn {
+        //#jswitch(key)
         case "toString": return METHOD;
         case "toTimeString": return METHOD;
         case "toDateString": return METHOD;
@@ -172,7 +168,7 @@ public class JSDate extends JS {
         case "setFullYear": return METHOD;
         case "setUTCFullYear": return METHOD;
         //#end
-        return super.get(key_);
+        return super.get(key);
     }
 
     /* ECMA helper functions */
index 835eefa..816c3e8 100644 (file)
@@ -16,18 +16,16 @@ public class JSMath extends JS {
     private static final JS SQRT1_2 = JS.N(1/java.lang.Math.sqrt(2));
     private static final JS SQRT2   = JS.N(java.lang.Math.sqrt(2));
 
-    public JS callMethod(JS method_, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
-        if(!JS.isString(method_)) return super.callMethod(method_,a0,a1,a2,rest,nargs);
-        String method = JS.toString(method_);
+    public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
         switch(nargs) {
             case 0: {
-                //#switch(method)
+                //#jswitch(method)
                 case "random": return JS.N(java.lang.Math.random());
                 //#end
                 break;
             }
             case 1: {
-                //#switch(method)
+                //#jswitch(method)
                 case "ceil": return JS.N((long)java.lang.Math.ceil(toDouble(a0)));
                 case "floor": return JS.N((long)java.lang.Math.floor(toDouble(a0)));
                 case "round": return JS.N((long)java.lang.Math.round(toDouble(a0)));
@@ -45,7 +43,7 @@ public class JSMath extends JS {
                 break;
             }
             case 2: {
-                //#switch(method)
+                //#jswitch(method)
                 case "min": return JS.N(java.lang.Math.min(toDouble(a0), toDouble(a1)));
                 case "max": return JS.N(java.lang.Math.max(toDouble(a0), toDouble(a1)));
                 case "pow": return JS.N(java.lang.Math.pow(toDouble(a0), toDouble(a1)));
@@ -54,13 +52,11 @@ public class JSMath extends JS {
                 break;
             }
         }
-        return super.callMethod(method_, a0, a1, a2, rest, nargs);
+        return super.callMethod(method, a0, a1, a2, rest, nargs);
     }
 
-    public JS get(JS key_) throws JSExn {
-        if(!JS.isString(key_)) return super.get(key_);
-        String key = JS.toString(key_);
-        //#switch(key)
+    public JS get(JS key) throws JSExn {
+        //#jswitch(key)
         case "E": return E;
         case "LN10": return LN10;
         case "LN2": return LN2;
@@ -88,6 +84,6 @@ public class JSMath extends JS {
         case "log": return METHOD;
         case "random": return METHOD;
         //#end
-        return super.get(key_);
+        return super.get(key);
     }
 }
index 1b574ea..904a587 100644 (file)
@@ -1,11 +1,8 @@
 package org.ibex.js;
 
 class JSPrimitive extends JS {
-    public JS callMethod(JS method_, JS arg0, JS arg1, JS arg2, JS[] rest, int alength) throws JSExn {
-        if(!JS.isString(method_)) return super.callMethod(method_,arg0,arg1,arg2,rest,alength);
-        String method = JS.toString(method_);
-        
-        //#switch(method)
+    public JS callMethod(JS method, JS arg0, JS arg1, JS arg2, JS[] rest, int alength) throws JSExn {
+        //#jswitch(method)
         case "toFixed": throw new JSExn("toFixed() not implemented");
         case "toExponential": throw new JSExn("toExponential() not implemented");
         case "toPrecision": throw new JSExn("toPrecision() not implemented");
@@ -15,7 +12,7 @@ class JSPrimitive extends JS {
         String s = coerceToString();
         int slength = s.length();
             
-        //#switch(method)
+        //#jswitch(method)
         case "substring": {
             int a = alength >= 1 ? JS.toInt(arg0) : 0;
             int b = alength >= 2 ? JS.toInt(arg1) : slength;
@@ -82,13 +79,11 @@ class JSPrimitive extends JS {
             return JS.S(s.substring(a,b));
         }
         //#end
-        return super.callMethod(method_,arg0,arg1,arg2,rest,alength);
+        return super.callMethod(method,arg0,arg1,arg2,rest,alength);
     }
     
-    public JS get(JS key_) throws JSExn {
-        if(!JS.isString(key_)) return super.get(key_);
-        String key = JS.toString(key_);
-        //#switch(key)
+    public JS get(JS key) throws JSExn {
+        //#jswitch(key)
         case "length": return JS.N(JS.toString(this).length());
         case "substring": return METHOD;
         case "charAt": return METHOD;
@@ -109,6 +104,6 @@ class JSPrimitive extends JS {
         case "toExponential": return METHOD;
         case "toFixed": return METHOD;
         //#end
-        return super.get(key_);
+        return super.get(key);
     }
 }
index e083902..00a6f40 100644 (file)
@@ -40,12 +40,10 @@ public class JSRegexp extends JS {
         }
     }
 
-    public JS callMethod(JS method_, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
-        if(!JS.isString(method_)) return super.callMethod(method_, a0, a1, a2, rest, nargs);
-        String method = JS.toString(method_);
+    public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
         switch(nargs) {
             case 1: {
-                //#switch(method)
+                //#jswitch(method)
                 case "exec": {
                     String s = JS.toString(a0);
                     int start = global ? lastIndex : 0;
@@ -70,19 +68,17 @@ public class JSRegexp extends JS {
                 break;
             }
             case 2: {
-                //#switch(method)
+                //#jswitch(method)
                 case "stringReplace": return stringReplace(a0, a1,a2);
                 //#end
                 break;
             }
         }
-        return super.callMethod(method_, a0, a1, a2, rest, nargs);
+        return super.callMethod(method, a0, a1, a2, rest, nargs);
     }
     
-    public JS get(JS key_) throws JSExn {
-        if(!JS.isString(key_)) return super.get(key_);
-        String key = JS.toString(key_);
-        //#switch(key)
+    public JS get(JS key) throws JSExn {
+        //#jswitch(key)
         case "exec": return METHOD;
         case "test": return METHOD;
         case "toString": return METHOD;
@@ -92,7 +88,7 @@ public class JSRegexp extends JS {
         case "ignoreCase": return B(flags & RE.REG_ICASE);
         case "multiline": return B(flags & RE.REG_MULTILINE);
         //#end
-        return super.get(key_);
+        return super.get(key);
     }
     
     public void put(JS key, JS value) throws JSExn {
index f7d3d6d..5036220 100644 (file)
@@ -50,8 +50,7 @@ public class JSScope extends JS.BT {
         }
         
         public JS _get(JS key) throws JSExn {
-            if(!JS.isString(key)) return super.get(key);
-            //#switch(JS.toString(key))
+            //#jswitch(key)
             case "NaN": return NaN;
             case "Infinity": return POSITIVE_INFINITY;
             case "undefined": return null;
@@ -71,8 +70,7 @@ public class JSScope extends JS.BT {
         }
 
         public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
-            if(!JS.isString(method)) return super.callMethod(method, a0, a1, a2, rest, nargs);
-            //#switch(JS.toString(method))
+            //#jswitch(method)
             case "parseInt": return parseInt(a0, N(0));
             case "parseFloat": return parseFloat(a0);
             case "isNaN": { double d = toDouble(a0); return d == d ? F : T; }
index ae66770..4698fdc 100644 (file)
@@ -171,7 +171,8 @@ PROCESS:
                 sinceLastRepeat = null;
                 replace = save;
 
-            } else if (trimmed.startsWith("//#switch")) {
+            } else if (trimmed.startsWith("//#switch") || trimmed.startsWith("//#jswitch")) {
+                boolean jswitch =  trimmed.startsWith("//#jswitch");
                 int expStart = trimmed.indexOf('(') +1;
                 if (expStart < 1) { err.add(new Error("expected ( in #switch")); continue PROCESS; }
                 int expEnd = trimmed.lastIndexOf(')');
@@ -179,7 +180,13 @@ PROCESS:
                 if (expEnd - expStart <= 1) { err.add(new Error("badly formed #switch statement")); continue PROCESS; }
                 String expr = trimmed.substring(expStart, expEnd);
 
-                out.print("final String ccSwitch"+enumSwitch+" = (String)("+expr+");  ");
+                if(jswitch) {
+                    out.print("final org.ibex.js.JS ccSwitchExpr"+enumSwitch+" = " + expr + ";");
+                    out.print("if(org.ibex.js.JS.isString(ccSwitchExpr"+enumSwitch+")) {");
+                    out.print("final String ccSwitch"+enumSwitch+" = org.ibex.js.JS.toString(ccSwitchExpr"+enumSwitch+");");
+                } else {
+                    out.print("final String ccSwitch"+enumSwitch+" = "+expr+";  ");
+                }
                 out.print("SUCCESS:do { switch(ccSwitch"+enumSwitch+".length()) {\n");
 
                 Hashtable[] byLength = new Hashtable[255];
@@ -225,6 +232,7 @@ PROCESS:
                 out.print("} "); /* switch */
                 if (Default != null) out.print(" " + Default);
                 out.print(" } while(false);\n"); /* OUTER */
+                if(jswitch) out.print("}");
                 enumSwitch++;
 
             } else {