fix unary +/- operators (bug 232)
[org.ibex.core.git] / src / org / ibex / js / Parser.java
index 51b23b8..38d15ea 100644 (file)
@@ -206,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)