summaryrefslogtreecommitdiff
path: root/src/LuaBigInt.cc
blob: 67294d9d63c824180388d473cacc5bfbe453f8b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "LuaBigInt.h"

using namespace Balau;

enum BigInt_functions_t {
    BIGINT_CONSTRUCTOR,
};

enum BigInt_methods_t {
    BIGINT_SET,
    BIGINT_TOSTRING,

    BIGINT_ADD,
    BIGINT_SUB,
    BIGINT_MUL,
    BIGINT_DIV,
    BIGINT_MOD,
};

struct lua_functypes_t BigInt_functions[] = {
    { BIGINT_CONSTRUCTOR,     NULL,             0, 2, { BLUA_STRING | BLUA_NUMBER, BLUA_NUMBER } },
    { -1, 0, 0, 0, 0 },
};

struct lua_functypes_t BigInt_methods[] = {
    { BIGINT_SET,             "set",            1, 2, { BLUA_STRING | BLUA_NUMBER, BLUA_NUMBER } },
    { BIGINT_TOSTRING,        "tostring",       0, 1, { BLUA_NUMBER } },

    { BIGINT_ADD,             "add",            1, 1, { BLUA_OBJECT } },
    { BIGINT_SUB,             "sub",            1, 1, { BLUA_OBJECT } },
    { BIGINT_MUL,             "mul",            1, 1, { BLUA_OBJECT } },
    { BIGINT_DIV,             "div",            1, 1, { BLUA_OBJECT } },
    { BIGINT_MOD,             "mod",            1, 1, { BLUA_OBJECT } },

    { -1, 0, 0, 0, 0 },
};

struct sLua_BigInt {
    static int BigInt_proceed_static(Lua & L, int n, int caller);
    static int BigInt_proceed(Lua & L, int n, BigInt * obj, int caller);
};

int sLua_BigInt::BigInt_proceed_static(Lua & L, int n, int caller) {
    int r = 0;
    String s;
    int radix = 10;

    switch (caller) {
    case BIGINT_CONSTRUCTOR:
        {
            BigInt * a = new BigInt();
            if (L.type() == LUA_TSTRING) {
                s = L.tostring(1);
                if (n == 2)
                    radix = L.tonumber(-1);
                a->set(s, radix);
            } else {
                lua_Number f = L.tonumber();
                a->set(f);
            }
            LuaBigIntFactory o(a);
            o.pushDestruct(L);
            r = 1;
            break;
        }
    }

    return r;
}

int sLua_BigInt::BigInt_proceed(Lua & L, int n, BigInt * a, int caller) {
    int r = 0;
    BigInt * b, * c, * m;
    String s;
    int radix = 10;

    switch (caller) {
    case BIGINT_SET:
        if (L.type() == LUA_TSTRING) {
            s = L.tostring(2);
            if (n == 3)
                radix = L.tonumber(-1);
            a->set(s, radix);
        } else {
            lua_Number f = L.tonumber();
            a->set(f);
        }
        r = 0;
        break;
    case BIGINT_TOSTRING:
        if (n == 3)
            radix = L.tonumber(-1);
        L.push(a->toString(radix));
        r = 1;
        break;
    case BIGINT_ADD:
        b = L.recast<BigInt>();
        {
            LuaBigIntFactory cf(c = new BigInt());
            cf.pushDestruct(L);
        }
        *c = *a + *b;
        r = 1;
        break;
    case BIGINT_SUB:
        b = L.recast<BigInt>();
        {
            LuaBigIntFactory cf(c = new BigInt());
            cf.pushDestruct(L);
        }
        *c = *a - *b;
        r = 1;
        break;
    case BIGINT_MUL:
        b = L.recast<BigInt>();
        {
            LuaBigIntFactory cf(c = new BigInt());
            cf.pushDestruct(L);
        }
        *c = *a * *b;
        r = 1;
        break;
    case BIGINT_DIV:
        b = L.recast<BigInt>();
        {
            LuaBigIntFactory cf(c = new BigInt());
            cf.pushDestruct(L);
        }
        *c = *a / *b;
        r = 1;
        break;
    case BIGINT_MOD:
        b = L.recast<BigInt>();
        {
            LuaBigIntFactory cf(c = new BigInt());
            cf.pushDestruct(L);
        }
        *c = *a % *b;
        r = 1;
        break;
    }

    return r;
}

#define METAMETHOD_WRAPPER(op) \
L.push("__" #op); \
L.load("" \
"return function(bigint) return function(a) \n" \
"    return a:" #op "() \n" \
"end end\n" \
""); \
L.copy(-5); \
L.pcall(1); \
L.settable() \

#define METAMETHOD_WRAPPER2(op) \
L.push("__" #op); \
L.load("" \
"return function(bigint) return function(a, b) \n" \
"    if type(a) ~= 'table' or a.__type ~= bigint then \n" \
"        a = bigint.new(a) \n" \
"    end \n" \
"    if type(b) ~= 'table' or b.__type ~= bigint then \n" \
"        b = bigint.new(b) \n" \
"    end \n" \
"    return a:" #op "(b) \n" \
"end end\n" \
""); \
L.copy(-5); \
L.pcall(1); \
L.settable() \

void LuaBigIntFactory::pushStatics(Lua & L) {
    CHECK_FUNCTIONS(BigInt);
    CHECK_METHODS(BigInt);
    PUSH_CLASS(BigInt);
    PUSH_CONSTRUCTOR(BigInt, BIGINT_CONSTRUCTOR);
    L.push("BIGINT_METAS");
    L.newtable();
    METAMETHOD_WRAPPER2(add);
    METAMETHOD_WRAPPER2(sub);
    METAMETHOD_WRAPPER2(div);
    METAMETHOD_WRAPPER2(mul);
    METAMETHOD_WRAPPER2(mod);
    METAMETHOD_WRAPPER(tostring);
    L.settable(LUA_REGISTRYINDEX);
    PUSH_CLASS_DONE();
}

void LuaBigIntFactory::pushObjectAndMembers(Lua & L) {
    pushObj(L, m_obj, "BigInt");

    L.push("BIGINT_METAS");
    L.gettable(LUA_REGISTRYINDEX);
    L.setmetatable();

    PUSH_METHOD(BigInt, BIGINT_SET);
    PUSH_METHOD(BigInt, BIGINT_TOSTRING);

    PUSH_METHOD(BigInt, BIGINT_ADD);
    PUSH_METHOD(BigInt, BIGINT_SUB);
    PUSH_METHOD(BigInt, BIGINT_MUL);
    PUSH_METHOD(BigInt, BIGINT_DIV);
    PUSH_METHOD(BigInt, BIGINT_MOD);
}

void Balau::registerLuaBigInt(Lua & L) {
    LuaBigIntFactory::pushStatics(L);
}