Skip to content
This repository was archived by the owner on Apr 2, 2020. It is now read-only.

Commit 851d263

Browse files
committed
2 parents 4187a6e + abe6aba commit 851d263

File tree

5 files changed

+70
-2
lines changed

5 files changed

+70
-2
lines changed

tests/snippets/builtin_bin.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
assert bin(0) == '0b0'
2+
assert bin(1) == '0b1'
3+
assert bin(-1) == '-0b1'
4+
assert bin(2**24) == '0b1' + '0' * 24
5+
assert bin(2**24-1) == '0b' + '1' * 24
6+
assert bin(-(2**24)) == '-0b1' + '0' * 24
7+
assert bin(-(2**24-1)) == '-0b' + '1' * 24
8+
9+
# TODO: uncomment these tests when arbitrarily sized ints are supported
10+
# assert bin(2**65) == '0b1' + '0' * 65
11+
# assert bin(2**65-1) == '0b' + '1' * 65
12+
# assert bin(-(2**65)) == '-0b1' + '0' * 65
13+
# assert bin(-(2**65-1)) == '-0b' + '1' * 65

tests/snippets/builtin_hex.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
assert hex(16) == '0x10'
2+
assert hex(-16) == '-0x10'
3+
4+
try:
5+
hex({})
6+
except TypeError:
7+
pass
8+
else:
9+
assert False, "TypeError not raised when ord() is called with a dict"

tests/snippets/floats.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@
1111
assert b > a
1212
assert not a > b
1313
assert not a > c
14+
assert b >= a
15+
assert c >= a
16+
assert not a >= b
17+

vm/src/builtins.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::io::{self, Write};
55

66
use super::compile;
77
use super::obj::objbool;
8+
use super::obj::objint;
89
use super::obj::objiter;
910
use super::obj::objstr;
1011
use 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

249273
fn 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(

vm/src/obj/objfloat.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,21 @@ fn float_gt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
106106
Ok(vm.ctx.new_bool(result))
107107
}
108108

109+
fn float_ge(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
110+
arg_check!(
111+
vm,
112+
args,
113+
required = [
114+
(zelf, Some(vm.ctx.float_type())),
115+
(other, Some(vm.ctx.float_type()))
116+
]
117+
);
118+
let zelf = get_value(zelf);
119+
let other = get_value(other);
120+
let result = zelf >= other;
121+
Ok(vm.ctx.new_bool(result))
122+
}
123+
109124
fn float_abs(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
110125
arg_check!(vm, args, required = [(i, Some(vm.ctx.float_type()))]);
111126
Ok(vm.ctx.new_float(get_value(i).abs()))
@@ -220,6 +235,7 @@ pub fn init(context: &PyContext) {
220235
float_type.set_attr("__lt__", context.new_rustfunc(float_lt));
221236
float_type.set_attr("__le__", context.new_rustfunc(float_le));
222237
float_type.set_attr("__gt__", context.new_rustfunc(float_gt));
238+
float_type.set_attr("__ge__", context.new_rustfunc(float_ge));
223239
float_type.set_attr("__abs__", context.new_rustfunc(float_abs));
224240
float_type.set_attr("__add__", context.new_rustfunc(float_add));
225241
float_type.set_attr("__divmod__", context.new_rustfunc(float_divmod));

0 commit comments

Comments
 (0)