Commit 866e37ac authored by Kevin Modzelewski's avatar Kevin Modzelewski

Last few things to get go.py working

With a quick modification to get around the "import os.path" issue,
we are able to run go.py!
parent 053b5c54
...@@ -535,6 +535,8 @@ Box* floatNew(BoxedClass* cls, Box* a) { ...@@ -535,6 +535,8 @@ Box* floatNew(BoxedClass* cls, Box* a) {
if (a->cls == float_cls) { if (a->cls == float_cls) {
return a; return a;
} else if (a->cls == int_cls) {
return boxFloat(static_cast<BoxedInt*>(a)->n);
} else if (a->cls == str_cls) { } else if (a->cls == str_cls) {
const std::string& s = static_cast<BoxedString*>(a)->s; const std::string& s = static_cast<BoxedString*>(a)->s;
if (s == "nan") if (s == "nan")
......
...@@ -286,6 +286,33 @@ extern "C" Box* longAnd(BoxedLong* v1, Box* _v2) { ...@@ -286,6 +286,33 @@ extern "C" Box* longAnd(BoxedLong* v1, Box* _v2) {
return NotImplemented; return NotImplemented;
} }
extern "C" Box* longXor(BoxedLong* v1, Box* _v2) {
if (!isSubclass(v1->cls, long_cls))
raiseExcHelper(TypeError, "descriptor '__xor__' requires a 'long' object but received a '%s'",
getTypeName(v1)->c_str());
if (isSubclass(_v2->cls, long_cls)) {
BoxedLong* v2 = static_cast<BoxedLong*>(_v2);
BoxedLong* r = new BoxedLong(long_cls);
mpz_init(r->n);
mpz_xor(r->n, v1->n, v2->n);
return r;
} else if (isSubclass(_v2->cls, int_cls)) {
BoxedInt* v2_int = static_cast<BoxedInt*>(_v2);
BoxedLong* r = new BoxedLong(long_cls);
mpz_init(r->n);
mpz_t v2_long;
mpz_init(v2_long);
if (v2_int->n >= 0)
mpz_init_set_ui(v2_long, v2_int->n);
else
mpz_init_set_si(v2_long, v2_int->n);
mpz_xor(r->n, v1->n, v2_long);
return r;
}
return NotImplemented;
}
// TODO reduce duplication between these 6 functions, and add double support // TODO reduce duplication between these 6 functions, and add double support
Box* longGt(BoxedLong* v1, Box* _v2) { Box* longGt(BoxedLong* v1, Box* _v2) {
if (!isSubclass(v1->cls, long_cls)) if (!isSubclass(v1->cls, long_cls))
...@@ -635,6 +662,17 @@ Box* longNonzero(BoxedLong* self) { ...@@ -635,6 +662,17 @@ Box* longNonzero(BoxedLong* self) {
return True; return True;
} }
Box* longHash(BoxedLong* self) {
if (!isSubclass(self->cls, long_cls))
raiseExcHelper(TypeError, "descriptor '__pow__' requires a 'long' object but received a '%s'",
getTypeName(self)->c_str());
// Not sure if this is a good hash function or not;
// simple, but only includes top bits:
double d = mpz_get_d(self->n);
return boxInt(*reinterpret_cast<int64_t*>(&d));
}
void setupLong() { void setupLong() {
long_cls->giveAttr("__name__", boxStrConstant("long")); long_cls->giveAttr("__name__", boxStrConstant("long"));
...@@ -657,6 +695,8 @@ void setupLong() { ...@@ -657,6 +695,8 @@ void setupLong() {
long_cls->giveAttr("__and__", new BoxedFunction(boxRTFunction((void*)longAnd, UNKNOWN, 2))); long_cls->giveAttr("__and__", new BoxedFunction(boxRTFunction((void*)longAnd, UNKNOWN, 2)));
long_cls->giveAttr("__rand__", long_cls->getattr("__and__")); long_cls->giveAttr("__rand__", long_cls->getattr("__and__"));
long_cls->giveAttr("__xor__", new BoxedFunction(boxRTFunction((void*)longXor, UNKNOWN, 2)));
long_cls->giveAttr("__rxor__", long_cls->getattr("__xor__"));
long_cls->giveAttr("__gt__", new BoxedFunction(boxRTFunction((void*)longGt, UNKNOWN, 2))); long_cls->giveAttr("__gt__", new BoxedFunction(boxRTFunction((void*)longGt, UNKNOWN, 2)));
long_cls->giveAttr("__ge__", new BoxedFunction(boxRTFunction((void*)longGe, UNKNOWN, 2))); long_cls->giveAttr("__ge__", new BoxedFunction(boxRTFunction((void*)longGe, UNKNOWN, 2)));
...@@ -672,6 +712,7 @@ void setupLong() { ...@@ -672,6 +712,7 @@ void setupLong() {
long_cls->giveAttr("__str__", new BoxedFunction(boxRTFunction((void*)longStr, STR, 1))); long_cls->giveAttr("__str__", new BoxedFunction(boxRTFunction((void*)longStr, STR, 1)));
long_cls->giveAttr("__nonzero__", new BoxedFunction(boxRTFunction((void*)longNonzero, BOXED_BOOL, 1))); long_cls->giveAttr("__nonzero__", new BoxedFunction(boxRTFunction((void*)longNonzero, BOXED_BOOL, 1)));
long_cls->giveAttr("__hash__", new BoxedFunction(boxRTFunction((void*)longHash, BOXED_INT, 1)));
long_cls->freeze(); long_cls->freeze();
} }
......
...@@ -193,6 +193,12 @@ Box* setAdd(BoxedSet* self, Box* v) { ...@@ -193,6 +193,12 @@ Box* setAdd(BoxedSet* self, Box* v) {
return None; return None;
} }
Box* setClear(BoxedSet* self, Box* v) {
assert(self->cls == set_cls);
self->s.clear();
return None;
}
Box* setContains(BoxedSet* self, Box* v) { Box* setContains(BoxedSet* self, Box* v) {
assert(self->cls == set_cls || self->cls == frozenset_cls); assert(self->cls == set_cls || self->cls == frozenset_cls);
return boxBool(self->s.count(v) != 0); return boxBool(self->s.count(v) != 0);
...@@ -274,6 +280,8 @@ void setupSet() { ...@@ -274,6 +280,8 @@ void setupSet() {
set_cls->giveAttr("add", new BoxedFunction(boxRTFunction((void*)setAdd, NONE, 2))); set_cls->giveAttr("add", new BoxedFunction(boxRTFunction((void*)setAdd, NONE, 2)));
set_cls->giveAttr("clear", new BoxedFunction(boxRTFunction((void*)setClear, NONE, 1)));
set_cls->freeze(); set_cls->freeze();
frozenset_cls->freeze(); frozenset_cls->freeze();
} }
......
...@@ -2,4 +2,6 @@ print 123542598.12938712938192831293812983 ...@@ -2,4 +2,6 @@ print 123542598.12938712938192831293812983
f = 1.0 f = 1.0
print f print f
print float(2)
# print f - 2 # print f - 2
...@@ -45,3 +45,6 @@ except ValueError, e: ...@@ -45,3 +45,6 @@ except ValueError, e:
print long("100", 16) print long("100", 16)
print long("100", 10) print long("100", 10)
print long("100", 26) print long("100", 26)
print type(hash(1L))
print hash(1L) == hash(2L)
...@@ -54,3 +54,8 @@ print set() | set() ...@@ -54,3 +54,8 @@ print set() | set()
for i in xrange(8): for i in xrange(8):
print i, i in set(range(2, 5)) print i, i in set(range(2, 5))
print i, i in frozenset(range(2, 5)) print i, i in frozenset(range(2, 5))
s = set(range(5))
print len(s)
s.clear()
print s
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