Commit c95d763e authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge pull request #115 from chrisrammy/master

Implement tuple.__add__, dict.__contains__ and builtin id() function
parents d5a1e6b9 1ea29920
......@@ -180,6 +180,12 @@ extern "C" Box* sum(Box* container, Box* initial) {
return cur;
}
extern "C" Box* id(Box* arg) {
i64 addr = (i64)(arg) ^ 0xdeadbeef00000003;
return boxInt(addr);
}
Box* open(Box* arg1, Box* arg2) {
assert(arg2);
......@@ -566,6 +572,8 @@ void setupBuiltins() {
builtins_module->giveAttr("sum",
new BoxedFunction(boxRTFunction((void*)sum, UNKNOWN, 2, 1, false, false), { boxInt(0) }));
id_obj = new BoxedFunction(boxRTFunction((void*)id, BOXED_INT, 1));
builtins_module->giveAttr("id", id_obj);
chr_obj = new BoxedFunction(boxRTFunction((void*)chr, STR, 1));
builtins_module->giveAttr("chr", chr_obj);
ord_obj = new BoxedFunction(boxRTFunction((void*)ord, BOXED_INT, 1));
......
......@@ -146,6 +146,11 @@ Box* dictSetdefault(BoxedDict* self, Box* k, Box* v) {
return v;
}
Box* dictContains(BoxedDict* self, Box* k) {
assert(self->cls == dict_cls);
return boxBool(self->d.count(k) != 0);
}
extern "C" Box* dictNew(Box* _cls) {
if (!isSubclass(_cls->cls, type_cls))
raiseExcHelper(TypeError, "dict.__new__(X): X is not a type object (%s)", getTypeName(_cls)->c_str());
......@@ -202,6 +207,7 @@ void setupDict() {
dict_cls->giveAttr("__getitem__", new BoxedFunction(boxRTFunction((void*)dictGetitem, UNKNOWN, 2)));
dict_cls->giveAttr("__setitem__", new BoxedFunction(boxRTFunction((void*)dictSetitem, NONE, 3)));
dict_cls->giveAttr("__contains__", new BoxedFunction(boxRTFunction((void*)dictContains, BOXED_BOOL, 2)));
dict_cls->freeze();
......
......@@ -56,6 +56,18 @@ Box* tupleGetitem(BoxedTuple* self, Box* slice) {
}
}
Box* tupleAdd(BoxedTuple* self, Box* rhs) {
if (rhs->cls != tuple_cls) {
return NotImplemented;
}
BoxedTuple* _rhs = static_cast<BoxedTuple*>(rhs);
BoxedTuple::GCVector velts;
velts.insert(velts.end(), self->elts.begin(), self->elts.end());
velts.insert(velts.end(), _rhs->elts.begin(), _rhs->elts.end());
return new BoxedTuple(std::move(velts));
}
Box* tupleLen(BoxedTuple* t) {
assert(t->cls == tuple_cls);
return boxInt(t->elts.size());
......@@ -223,6 +235,7 @@ void setupTuple() {
tuple_cls->giveAttr("__len__", new BoxedFunction(boxRTFunction((void*)tupleLen, BOXED_INT, 1)));
tuple_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)tupleRepr, STR, 1)));
tuple_cls->giveAttr("__str__", tuple_cls->getattr("__repr__"));
tuple_cls->giveAttr("__add__", new BoxedFunction(boxRTFunction((void*)tupleAdd, BOXED_TUPLE, 2)));
tuple_cls->freeze();
......
......@@ -405,6 +405,8 @@ extern "C" BoxedString* functionRepr(BoxedFunction* v) {
return boxStrConstant("<built-in function max>");
if (v == open_obj)
return boxStrConstant("<built-in function open>");
if (v == id_obj)
return boxStrConstant("<built-in function id>");
if (v == chr_obj)
return boxStrConstant("<built-in function chr>");
if (v == ord_obj)
......@@ -422,6 +424,7 @@ Box* abs_obj = NULL;
Box* min_obj = NULL;
Box* max_obj = NULL;
Box* open_obj = NULL;
Box* id_obj = NULL;
Box* chr_obj = NULL;
Box* ord_obj = NULL;
Box* trap_obj = NULL;
......
......@@ -81,8 +81,8 @@ extern const ObjectFlavor object_flavor, type_flavor, bool_flavor, int_flavor, f
}
extern "C" { extern Box* None, *NotImplemented, *True, *False; }
extern "C" {
extern Box* repr_obj, *len_obj, *hash_obj, *range_obj, *abs_obj, *min_obj, *max_obj, *open_obj, *chr_obj, *ord_obj,
*trap_obj;
extern Box* repr_obj, *len_obj, *hash_obj, *range_obj, *abs_obj, *min_obj, *max_obj, *open_obj, *id_obj, *chr_obj,
*ord_obj, *trap_obj;
} // these are only needed for functionRepr, which is hacky
extern "C" { extern BoxedModule* sys_module, *builtins_module, *math_module, *time_module, *thread_module; }
......
......@@ -46,3 +46,20 @@ print dict()
d = dict()
d[1] = 2
print d
print 1 in {}
print 1 in {1:1}
print 'a' in {}
print 'a' in {'a': 1}
print 'a' in dict()
try:
# attempting to print the following result causes the test
# to fail (bad output), but runtime is ok. otherwise it
# throws a TypeError exception.
# print 'a' in dict(a=1)
'a' in dict(a=1)
except TypeError, e:
# throws <function ...> doesn't take keyword arguments
pass
print d
x = 'Hi'
y = 1
z = (42, 7)
f = (2,) + z
print id(x) == id(x)
print id(y) != id(x)
print id(z) == id(z)
print id(f) != id(z)
......@@ -70,3 +70,9 @@ t((T(1),1), (T(2),))
t((T(1),), (T(2),1))
print ("hello", "world", ["test"])
# __add__
print () + ()
print (1, 2, 3) + ()
print () + (1, 2, 3)
print (1, 2) + (2, 3)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment