|
28 | 28 | import static com.oracle.graal.python.builtins.objects.PNone.NO_VALUE;
|
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 | +import static com.oracle.graal.python.nodes.BuiltinNames.BIN; |
31 | 32 | import static com.oracle.graal.python.nodes.BuiltinNames.CALLABLE;
|
32 | 33 | import static com.oracle.graal.python.nodes.BuiltinNames.CHR;
|
33 | 34 | import static com.oracle.graal.python.nodes.BuiltinNames.COMPILE;
|
|
120 | 121 | import com.oracle.graal.python.nodes.object.GetClassNode;
|
121 | 122 | import com.oracle.graal.python.nodes.subscript.GetItemNode;
|
122 | 123 | import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
|
| 124 | +import com.oracle.graal.python.nodes.util.CastToIntNode; |
123 | 125 | import com.oracle.graal.python.runtime.PythonContext;
|
124 | 126 | import com.oracle.graal.python.runtime.PythonCore;
|
125 | 127 | import com.oracle.graal.python.runtime.PythonOptions;
|
@@ -198,6 +200,54 @@ public Object absObject(Object object,
|
198 | 200 | }
|
199 | 201 | }
|
200 | 202 |
|
| 203 | + // bin(object) |
| 204 | + @Builtin(name = BIN, fixedNumOfArguments = 1) |
| 205 | + @TypeSystemReference(PythonArithmeticTypes.class) |
| 206 | + @GenerateNodeFactory |
| 207 | + public abstract static class BinNode extends PythonUnaryBuiltinNode { |
| 208 | + |
| 209 | + public abstract String executeObject(Object x); |
| 210 | + |
| 211 | + private static String buildString(boolean isNegative, String number) { |
| 212 | + StringBuilder sb = new StringBuilder(); |
| 213 | + if (isNegative) { |
| 214 | + sb.append('-'); |
| 215 | + } |
| 216 | + sb.append("0b"); |
| 217 | + sb.append(number); |
| 218 | + return sb.toString(); |
| 219 | + } |
| 220 | + |
| 221 | + @Specialization |
| 222 | + public String doL(long x) { |
| 223 | + return buildString(x < 0, Long.toBinaryString(Math.abs(x))); |
| 224 | + } |
| 225 | + |
| 226 | + @Specialization |
| 227 | + public String doD(double x) { |
| 228 | + throw raise(TypeError, "'%p' object cannot be interpreted as an integer", x); |
| 229 | + } |
| 230 | + |
| 231 | + @Specialization |
| 232 | + @TruffleBoundary |
| 233 | + public String doPI(PInt x) { |
| 234 | + BigInteger value = x.getValue(); |
| 235 | + return buildString(value.compareTo(BigInteger.ZERO) == -1, value.abs().toString(2)); |
| 236 | + } |
| 237 | + |
| 238 | + @Specialization |
| 239 | + public String doO(Object x, |
| 240 | + @Cached("create()") CastToIntNode toIntNode, |
| 241 | + @Cached("create()") BinNode recursiveNode) { |
| 242 | + Object value = toIntNode.execute(x); |
| 243 | + return recursiveNode.executeObject(value); |
| 244 | + } |
| 245 | + |
| 246 | + protected BinNode create() { |
| 247 | + return BuiltinFunctionsFactory.BinNodeFactory.create(); |
| 248 | + } |
| 249 | + } |
| 250 | + |
201 | 251 | // callable(object)
|
202 | 252 | @Builtin(name = CALLABLE, fixedNumOfArguments = 1)
|
203 | 253 | @GenerateNodeFactory
|
|
0 commit comments