diff options
author | Nicolas "Pixel" Noble <pixel@nobis-crew.org> | 2013-08-12 18:39:03 +0200 |
---|---|---|
committer | Nicolas "Pixel" Noble <pixel@nobis-crew.org> | 2013-08-12 18:39:03 +0200 |
commit | aed6762619168d1c1f9da4c3feeb2ce28dafc55f (patch) | |
tree | 732857a169d4c6262aa9cdfb5bf41108a8b13a68 | |
parent | b226a83ccbf06b52a61dcffaddc9a9c38096ff87 (diff) |
Making BigInt's constructor smarter, and adding a few last boolean accessors.
-rw-r--r-- | src/LuaBigInt.cc | 45 |
1 files changed, 41 insertions, 4 deletions
diff --git a/src/LuaBigInt.cc b/src/LuaBigInt.cc index 53b9f5e..91823b2 100644 --- a/src/LuaBigInt.cc +++ b/src/LuaBigInt.cc @@ -53,10 +53,15 @@ enum BigInt_methods_t { BIGINT_DO_MODSQR, BIGINT_DO_MODINV, BIGINT_DO_MODPOW, + + BIGINT_ISPRIME, + BIGINT_ISNEG, + BIGINT_ISZERO, + BIGINT_ISPOS, }; struct lua_functypes_t BigInt_functions[] = { - { BIGINT_CONSTRUCTOR, NULL, 0, 2, { BLUA_STRING | BLUA_NUMBER, BLUA_NUMBER } }, + { BIGINT_CONSTRUCTOR, NULL, 0, 2, { BLUA_OBJECT | BLUA_STRING | BLUA_NUMBER, BLUA_NUMBER } }, { -1, 0, 0, 0, 0 }, }; @@ -108,6 +113,11 @@ struct lua_functypes_t BigInt_methods[] = { { BIGINT_DO_MODINV, "do_modinv", 1, 1, { BLUA_OBJECT } }, { BIGINT_DO_MODPOW, "do_modpow", 2, 2, { BLUA_OBJECT, BLUA_OBJECT } }, + { BIGINT_ISPRIME, "isPrime", 0, 0, { } }, + { BIGINT_ISNEG, "isNeg", 0, 0, { } }, + { BIGINT_ISZERO, "isZero", 0, 0, { } }, + { BIGINT_ISPOS, "isPos", 0, 0, { } }, + { -1, 0, 0, 0, 0 }, }; @@ -124,15 +134,26 @@ int sLua_BigInt::BigInt_proceed_static(Lua & L, int n, int caller) { switch (caller) { case BIGINT_CONSTRUCTOR: { - BigInt * a = new BigInt(); + BigInt * a; if (L.type() == LUA_TSTRING) { + a = new BigInt(); s = L.tostring(1); if (n == 2) radix = L.tonumber(-1); a->set(s, radix); + } else if (n == 1) { + if (L.istable()) { + BigInt * b = L.recast<BigInt>(); + a = new BigInt(*b); + } else { + a = new BigInt(); + lua_Number f = L.tonumber(); + a->set(f); + } + } else if (n == 0) { + a = new BigInt(); } else { - lua_Number f = L.tonumber(); - a->set(f); + L.error("Invalid arguments to BigInt:new"); } LuaBigIntFactory o(a); o.pushDestruct(L); @@ -400,6 +421,22 @@ int sLua_BigInt::BigInt_proceed(Lua & L, int n, BigInt * a, int caller) { m = L.recast<BigInt>(-1); a->do_modpow(*b, *m); break; + case BIGINT_ISPRIME: + L.push(a->isPrime()); + r = 1; + break; + case BIGINT_ISNEG: + L.push(*a < 0); + r = 1; + break; + case BIGINT_ISZERO: + L.push(*a == 0); + r = 1; + break; + case BIGINT_ISPOS: + L.push(*a > 0); + r = 1; + break; } return r; |