diff graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/expression/DSLExpressionResolver.java @ 20169:65d29fa81397

DSL: add support for hex, oct and binary integer literals.
author Chris Seaton <chris.seaton@oracle.com>
date Sun, 05 Apr 2015 19:51:46 +0100
parents ef292a5bb79d
children 953c813b8e7a
line wrap: on
line diff
--- a/graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/expression/DSLExpressionResolver.java	Sun Apr 05 09:45:58 2015 +0200
+++ b/graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/expression/DSLExpressionResolver.java	Sun Apr 05 19:51:46 2015 +0100
@@ -226,7 +226,25 @@
     public void visitIntLiteral(IntLiteral binary) {
         try {
             binary.setResolvedType(context.getType(int.class));
-            binary.setResolvedValueInt(Integer.parseInt(binary.getLiteral()));
+
+            final int base;
+            final String literal;
+
+            if (binary.getLiteral().startsWith("0x")) {
+                base = 16;
+                literal = binary.getLiteral().substring(2);
+            } else if (binary.getLiteral().startsWith("0b")) {
+                base = 2;
+                literal = binary.getLiteral().substring(2);
+            } else if (binary.getLiteral().startsWith("0")) {
+                base = 8;
+                literal = binary.getLiteral();
+            } else {
+                base = 10;
+                literal = binary.getLiteral();
+            }
+
+            binary.setResolvedValueInt(Integer.parseInt(literal, base));
         } catch (NumberFormatException e) {
             throw new InvalidExpressionException(String.format("Type mismatch: cannot convert from String '%s' to int", binary.getLiteral()));
         }