|
29 | 29 | import static com.oracle.graal.python.builtins.objects.PNotImplemented.NOT_IMPLEMENTED;
|
30 | 30 | import static com.oracle.graal.python.nodes.BuiltinNames.ABS;
|
31 | 31 | import static com.oracle.graal.python.nodes.BuiltinNames.BIN;
|
| 32 | +import static com.oracle.graal.python.nodes.BuiltinNames.BREAKPOINT; |
| 33 | +import static com.oracle.graal.python.nodes.BuiltinNames.BREAKPOINTHOOK; |
32 | 34 | import static com.oracle.graal.python.nodes.BuiltinNames.CALLABLE;
|
33 | 35 | import static com.oracle.graal.python.nodes.BuiltinNames.CHR;
|
34 | 36 | import static com.oracle.graal.python.nodes.BuiltinNames.COMPILE;
|
|
39 | 41 | import static com.oracle.graal.python.nodes.BuiltinNames.EXEC;
|
40 | 42 | import static com.oracle.graal.python.nodes.BuiltinNames.GETATTR;
|
41 | 43 | import static com.oracle.graal.python.nodes.BuiltinNames.HASH;
|
| 44 | +import static com.oracle.graal.python.nodes.BuiltinNames.HEX; |
42 | 45 | import static com.oracle.graal.python.nodes.BuiltinNames.ID;
|
43 | 46 | import static com.oracle.graal.python.nodes.BuiltinNames.ISINSTANCE;
|
44 | 47 | import static com.oracle.graal.python.nodes.BuiltinNames.ISSUBCLASS;
|
|
55 | 58 | import static com.oracle.graal.python.nodes.BuiltinNames.ROUND;
|
56 | 59 | import static com.oracle.graal.python.nodes.BuiltinNames.SETATTR;
|
57 | 60 | import static com.oracle.graal.python.nodes.BuiltinNames.SUM;
|
58 |
| -import static com.oracle.graal.python.nodes.BuiltinNames.BREAKPOINT; |
59 |
| -import static com.oracle.graal.python.nodes.BuiltinNames.BREAKPOINTHOOK; |
60 | 61 | import static com.oracle.graal.python.nodes.BuiltinNames.__BUILTIN__;
|
61 | 62 | import static com.oracle.graal.python.nodes.BuiltinNames.__DEBUG__;
|
62 | 63 | import static com.oracle.graal.python.nodes.BuiltinNames.__DUMP_TRUFFLE_AST__;
|
|
170 | 171 | import com.oracle.truffle.api.CompilerDirectives;
|
171 | 172 | import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
|
172 | 173 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
|
173 |
| -import com.oracle.truffle.api.debug.Debugger; |
174 | 174 | import com.oracle.truffle.api.Truffle;
|
| 175 | +import com.oracle.truffle.api.debug.Debugger; |
175 | 176 | import com.oracle.truffle.api.dsl.Cached;
|
176 | 177 | import com.oracle.truffle.api.dsl.Fallback;
|
177 | 178 | import com.oracle.truffle.api.dsl.GenerateNodeFactory;
|
@@ -351,6 +352,60 @@ protected static OctNode create() {
|
351 | 352 | }
|
352 | 353 | }
|
353 | 354 |
|
| 355 | + // hex(object) |
| 356 | + @Builtin(name = HEX, fixedNumOfPositionalArgs = 1) |
| 357 | + @TypeSystemReference(PythonArithmeticTypes.class) |
| 358 | + @GenerateNodeFactory |
| 359 | + public abstract static class HexNode extends PythonUnaryBuiltinNode { |
| 360 | + |
| 361 | + public abstract String executeObject(Object x); |
| 362 | + |
| 363 | + @TruffleBoundary |
| 364 | + private static String buildString(boolean isNegative, String number) { |
| 365 | + StringBuilder sb = new StringBuilder(); |
| 366 | + if (isNegative) { |
| 367 | + sb.append('-'); |
| 368 | + } |
| 369 | + sb.append("0x"); |
| 370 | + sb.append(number); |
| 371 | + return sb.toString(); |
| 372 | + } |
| 373 | + |
| 374 | + @Specialization |
| 375 | + public String doL(long x) { |
| 376 | + return buildString(x < 0, longToHexString(x)); |
| 377 | + } |
| 378 | + |
| 379 | + @TruffleBoundary |
| 380 | + private static String longToHexString(long x) { |
| 381 | + return Long.toHexString(Math.abs(x)); |
| 382 | + } |
| 383 | + |
| 384 | + @Specialization |
| 385 | + public String doD(double x) { |
| 386 | + throw raise(TypeError, "'%p' object cannot be interpreted as an integer", x); |
| 387 | + } |
| 388 | + |
| 389 | + @Specialization |
| 390 | + @TruffleBoundary |
| 391 | + public String doPI(PInt x) { |
| 392 | + BigInteger value = x.getValue(); |
| 393 | + return buildString(value.compareTo(BigInteger.ZERO) == -1, value.abs().toString(8)); |
| 394 | + } |
| 395 | + |
| 396 | + @Specialization |
| 397 | + public String doO(Object x, |
| 398 | + @Cached("create()") CastToIntegerFromIndexNode toIntNode, |
| 399 | + @Cached("create()") OctNode recursiveNode) { |
| 400 | + Object value = toIntNode.execute(x); |
| 401 | + return recursiveNode.executeObject(value); |
| 402 | + } |
| 403 | + |
| 404 | + protected static OctNode create() { |
| 405 | + return BuiltinFunctionsFactory.OctNodeFactory.create(); |
| 406 | + } |
| 407 | + } |
| 408 | + |
354 | 409 | // callable(object)
|
355 | 410 | @Builtin(name = CALLABLE, fixedNumOfPositionalArgs = 1)
|
356 | 411 | @GenerateNodeFactory
|
|
0 commit comments