jswitch
[org.ibex.core.git] / src / org / ibex / util / Preprocessor.java
index ae66770..4698fdc 100644 (file)
@@ -171,7 +171,8 @@ PROCESS:
                 sinceLastRepeat = null;
                 replace = save;
 
                 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(')');
                 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);
 
                 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];
                 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 */
                 out.print("} "); /* switch */
                 if (Default != null) out.print(" " + Default);
                 out.print(" } while(false);\n"); /* OUTER */
+                if(jswitch) out.print("}");
                 enumSwitch++;
 
             } else {
                 enumSwitch++;
 
             } else {