# LuaInteger

a guest
Jan 17th, 2016
122
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. local names = {
2.   [10] = "A",
3.   [11] = "B",
4.   [12] = "C",
5.   [13] = "D",
6.   [14] = "E",
7.   [15] = "F",
8.   [16] = "G",
9.   [17] = "H",
10.   [18] = "I",
11.   [19] = "J",
12.   [20] = "K",
13.   [21] = "L",
14.   [22] = "M",
15.   [23] = "N",
16.   [24] = "O",
17.   [25] = "P",
18.   [26] = "Q",
19.   [27] = "R",
20.   [28] = "S",
21.   [29] = "T",
22.   [30] = "U",
23.   [31] = "V",
24.   [32] = "W",
25.   [33] = "X",
26.   [34] = "Y",
27.   [35] = "Z",
28.   A = 10,
29.   B = 11,
30.   C = 12,
31.   D = 13,
32.   E = 14,
33.   F = 15,
34.   G = 16,
35.   H = 17,
36.   I = 18,
37.   J = 19,
38.   K = 20,
39.   L = 21,
40.   M = 22,
41.   N = 23,
42.   O = 24,
43.   P = 25,
44.   Q = 26,
45.   R = 27,
46.   S = 28,
47.   T = 29,
48.   U = 30,
49.   V = 31,
50.   W = 32,
51.   X = 33,
52.   Y = 34,
53.   Z = 35
54. }
55. local toDec
56. toDec = function(input, base)
57.   if base > 11 then
58.     input = string.upper(input)
59.   end
60.   local sum = 0
61.   local num = string.len(input)
62.   for i = 1, num do
63.     local number = string.sub(input, -i, -i)
64.     if names[number] then
65.       sum = sum + names[number] * base ^ (i - 1)
66.     else
67.       sum = sum + tonumber(number * base ^ (i - 1))
68.     end
69.   end
70.   return sum
71. end
72. local fromDec
73. fromDec = function(input, base)
74.   local hexstr = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
75.   local s = ''
76.   while input > 0 do
77.     local mod = math.fmod(input, base)
78.     s = string.sub(hexstr, mod + 1, mod + 1) .. s
79.     input = math.floor(input / base)
80.   end
81.   if s == '' then
82.     s = '0'
83.   end
84.   return s
85. end
86. local CustomInteger
87. do
88.   local _class_0
89.   local _base_0 = {
90.     to = function(self, base)
91.       if base then
92.         if base == 10 then
94.         else
95.           local dec = toDec(self.int, self.base)
96.           return fromDec(dec, base)
97.         end
98.       end
99.     end,
100.     __tostring = function(self)
101.       return self.int
102.     end,
104.       local left = type(a) == "number" and a or a:to(10)
105.       local right = type(b) == "number" and b or b:to(10)
106.       return left + right
107.     end,
108.     __sub = function(a, b)
109.       local left = type(a) == "number" and a or a:to(10)
110.       local right = type(b) == "number" and b or b:to(10)
111.       return left - right
112.     end,
113.     __mul = function(a, b)
114.       local left = type(a) == "number" and a or a:to(10)
115.       local right = type(b) == "number" and b or b:to(10)
116.       return left * right
117.     end,
118.     __div = function(a, b)
119.       local left = type(a) == "number" and a or a:to(10)
120.       local right = type(b) == "number" and b or b:to(10)
121.       return left / right
122.     end,
123.     __mod = function(a, b)
124.       local left = type(a) == "number" and a or a:to(10)
125.       local right = type(b) == "number" and b or b:to(10)
126.       return left % right
127.     end,
128.     __unm = function(a)
129.       local left = type(a) == "number" and a or a:to(10)
130.       return -left
131.     end,
132.     __pow = function(a, b)
133.       local left = type(a) == "number" and a or a:to(10)
134.       local right = type(b) == "number" and b or b:to(10)
135.       return left ^ right
136.     end,
137.     __concat = function(a, b)
138.       local left = a
139.       local right = b
141.     end,
142.     __eq = function(a, b)
143.       local left = type(a) == "number" and a or a:to(10)
144.       local right = type(b) == "number" and b or b:to(10)
146.     end,
147.     __lt = function(a, b)
148.       local left = type(a) == "number" and a or a:to(10)
149.       local right = type(b) == "number" and b or b:to(10)
151.     end,
152.     _le = function(a, b)
153.       local left = type(a) == "number" and a or a:to(10)
154.       local right = type(b) == "number" and b or b:to(10)
156.     end,
157.     __inherited = function(self, cls)
159.       cls.__base.__sub = self.__sub
160.       cls.__base.__mul = self.__mul
161.       cls.__base.__div = self.__div
162.       cls.__base.__mod = self.__mod
163.       cls.__base.__unm = self.__unm
164.       cls.__base.__pow = self.__pow
165.       cls.__base.__eq = self.__eq
166.       cls.__base.__lt = self.__lt
167.       cls.__base.__le = self.__le
168.       cls.__base.__concat = self.__concat
169.       cls.__base.__tostring = self.__tostring
170.     end
171.   }
172.   _base_0.__index = _base_0
173.   _class_0 = setmetatable({
174.     __init = function(self, int, base)
175.       self.int = type(int) == "string" and int or "0"
176.       self.base = base
177.     end,
178.     __base = _base_0,
179.     __name = "CustomInteger"
180.   }, {
181.     __index = _base_0,
182.     __call = function(cls, ...)
183.       local _self_0 = setmetatable({}, _base_0)
184.       cls.__init(_self_0, ...)
185.       return _self_0
186.     end
187.   })
188.   _base_0.__class = _class_0
189.   CustomInteger = _class_0
190. end
191. local HexInteger
192. do
193.   local _class_0
194.   local _parent_0 = CustomInteger
195.   local _base_0 = { }
196.   _base_0.__index = _base_0
197.   setmetatable(_base_0, _parent_0.__base)
198.   _class_0 = setmetatable({
199.     __init = function(self, int)
200.       return _class_0.__parent.__init(self, int, 16)
201.     end,
202.     __base = _base_0,
203.     __name = "HexInteger",
204.     __parent = _parent_0
205.   }, {
206.     __index = function(cls, name)
207.       local val = rawget(_base_0, name)
208.       if val == nil then
209.         local parent = rawget(cls, "__parent")
210.         if parent then
211.           return parent[name]
212.         end
213.       else
214.         return val
215.       end
216.     end,
217.     __call = function(cls, ...)
218.       local _self_0 = setmetatable({}, _base_0)
219.       cls.__init(_self_0, ...)
220.       return _self_0
221.     end
222.   })
223.   _base_0.__class = _class_0
224.   if _parent_0.__inherited then
225.     _parent_0.__inherited(_parent_0, _class_0)
226.   end
227.   HexInteger = _class_0
228. end
229. local OctInteger
230. do
231.   local _class_0
232.   local _parent_0 = CustomInteger
233.   local _base_0 = { }
234.   _base_0.__index = _base_0
235.   setmetatable(_base_0, _parent_0.__base)
236.   _class_0 = setmetatable({
237.     __init = function(self, int)
238.       return _class_0.__parent.__init(self, int, 8)
239.     end,
240.     __base = _base_0,
241.     __name = "OctInteger",
242.     __parent = _parent_0
243.   }, {
244.     __index = function(cls, name)
245.       local val = rawget(_base_0, name)
246.       if val == nil then
247.         local parent = rawget(cls, "__parent")
248.         if parent then
249.           return parent[name]
250.         end
251.       else
252.         return val
253.       end
254.     end,
255.     __call = function(cls, ...)
256.       local _self_0 = setmetatable({}, _base_0)
257.       cls.__init(_self_0, ...)
258.       return _self_0
259.     end
260.   })
261.   _base_0.__class = _class_0
262.   if _parent_0.__inherited then
263.     _parent_0.__inherited(_parent_0, _class_0)
264.   end
265.   OctInteger = _class_0
266. end
267. local BinInteger
268. do
269.   local _class_0
270.   local _parent_0 = CustomInteger
271.   local _base_0 = { }
272.   _base_0.__index = _base_0
273.   setmetatable(_base_0, _parent_0.__base)
274.   _class_0 = setmetatable({
275.     __init = function(self, int)
276.       return _class_0.__parent.__init(self, int, 2)
277.     end,
278.     __base = _base_0,
279.     __name = "BinInteger",
280.     __parent = _parent_0
281.   }, {
282.     __index = function(cls, name)
283.       local val = rawget(_base_0, name)
284.       if val == nil then
285.         local parent = rawget(cls, "__parent")
286.         if parent then
287.           return parent[name]
288.         end
289.       else
290.         return val
291.       end
292.     end,
293.     __call = function(cls, ...)
294.       local _self_0 = setmetatable({}, _base_0)
295.       cls.__init(_self_0, ...)
296.       return _self_0
297.     end
298.   })
299.   _base_0.__class = _class_0
300.   if _parent_0.__inherited then
301.     _parent_0.__inherited(_parent_0, _class_0)
302.   end
303.   BinInteger = _class_0
304. end
305. local DecInteger
306. do
307.   local _class_0
308.   local _parent_0 = CustomInteger
309.   local _base_0 = { }
310.   _base_0.__index = _base_0
311.   setmetatable(_base_0, _parent_0.__base)
312.   _class_0 = setmetatable({
313.     __init = function(self, int)
314.       return _class_0.__parent.__init(self, int, 10)
315.     end,
316.     __base = _base_0,
317.     __name = "DecInteger",
318.     __parent = _parent_0
319.   }, {
320.     __index = function(cls, name)
321.       local val = rawget(_base_0, name)
322.       if val == nil then
323.         local parent = rawget(cls, "__parent")
324.         if parent then
325.           return parent[name]
326.         end
327.       else
328.         return val
329.       end
330.     end,
331.     __call = function(cls, ...)
332.       local _self_0 = setmetatable({}, _base_0)
333.       cls.__init(_self_0, ...)
334.       return _self_0
335.     end
336.   })
337.   _base_0.__class = _class_0
338.   if _parent_0.__inherited then
339.     _parent_0.__inherited(_parent_0, _class_0)
340.   end
341.   DecInteger = _class_0
342. end
343. return ({
344.   toDec = toDec,
345.   fromDec = fromDec,
346.   CustomInteger = CustomInteger,
347.   HexInteger = HexInteger,
348.   OctInteger = OctInteger,
349.   BinInteger = BinInteger,
350.   DecInteger = DecInteger
351. })
RAW Paste Data