@@ -5,6 +5,7 @@ use std::io::{self, Write};
55
66use super :: compile;
77use super :: obj:: objbool;
8+ use super :: obj:: objint;
89use super :: obj:: objiter;
910use super :: obj:: objstr;
1011use super :: obj:: objtype;
@@ -74,7 +75,19 @@ fn builtin_any(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
7475}
7576
7677// builtin_ascii
77- // builtin_bin
78+
79+ fn builtin_bin ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
80+ arg_check ! ( vm, args, required = [ ( number, Some ( vm. ctx. int_type( ) ) ) ] ) ;
81+
82+ let n = objint:: get_value ( number) ;
83+ let s = match n. signum ( ) {
84+ -1 => format ! ( "-0b{:b}" , n. abs( ) ) ,
85+ _ => format ! ( "0b{:b}" , n) ,
86+ } ;
87+
88+ Ok ( vm. new_str ( s) )
89+ }
90+
7891// builtin_bool
7992// builtin_breakpoint
8093// builtin_bytearray
@@ -244,7 +257,18 @@ fn builtin_hash(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
244257}
245258
246259// builtin_help
247- // builtin_hex
260+
261+ fn builtin_hex ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
262+ arg_check ! ( vm, args, required = [ ( number, Some ( vm. ctx. int_type( ) ) ) ] ) ;
263+
264+ let n = objint:: get_value ( number) ;
265+ let s = match n. signum ( ) {
266+ -1 => format ! ( "-0x{:x}" , n. abs( ) ) ,
267+ _ => format ! ( "0x{:x}" , n) ,
268+ } ;
269+
270+ Ok ( vm. new_str ( s) )
271+ }
248272
249273fn builtin_id ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
250274 arg_check ! ( vm, args, required = [ ( obj, None ) ] ) ;
@@ -514,6 +538,7 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
514538 dict. insert ( String :: from ( "abs" ) , ctx. new_rustfunc ( builtin_abs) ) ;
515539 dict. insert ( String :: from ( "all" ) , ctx. new_rustfunc ( builtin_all) ) ;
516540 dict. insert ( String :: from ( "any" ) , ctx. new_rustfunc ( builtin_any) ) ;
541+ dict. insert ( String :: from ( "bin" ) , ctx. new_rustfunc ( builtin_bin) ) ;
517542 dict. insert ( String :: from ( "bool" ) , ctx. bool_type ( ) ) ;
518543 dict. insert ( String :: from ( "bytes" ) , ctx. bytes_type ( ) ) ;
519544 dict. insert ( String :: from ( "chr" ) , ctx. new_rustfunc ( builtin_chr) ) ;
@@ -527,6 +552,7 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
527552 dict. insert ( String :: from ( "getattr" ) , ctx. new_rustfunc ( builtin_getattr) ) ;
528553 dict. insert ( String :: from ( "hasattr" ) , ctx. new_rustfunc ( builtin_hasattr) ) ;
529554 dict. insert ( String :: from ( "hash" ) , ctx. new_rustfunc ( builtin_hash) ) ;
555+ dict. insert ( String :: from ( "hex" ) , ctx. new_rustfunc ( builtin_hex) ) ;
530556 dict. insert ( String :: from ( "id" ) , ctx. new_rustfunc ( builtin_id) ) ;
531557 dict. insert ( String :: from ( "int" ) , ctx. int_type ( ) ) ;
532558 dict. insert (
0 commit comments