MudkipTheEpic

FakeNumber

May 31st, 2013
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.51 KB | None | 0 0
  1. local meta={
  2. isNum=true;
  3. __add=function(a,b)
  4. local aVal,bVal
  5. local isANum, isBNum = (type(a)=="number"), (type(b)=="number")
  6. isANum=isANum or (type(a)=="table" and getmetatable(a)["isNum"])
  7. isBNum=isBNum or (type(b)=="table" and getmetatable(b)["isNum"])
  8. if not isANum or not isBNum then
  9. error("attempt to perform arithmetic __add on "..(isANum and "number" or type(a)).." and "..(isBNum and "number" or type(b)),2)
  10. return nil
  11. end
  12. if type(a)~= "number" then aVal=a["value"] else aVal=a end
  13. if type(b)~= "number" then bVal=b["value"] else bVal=b end
  14. return aVal+bVal
  15. end;
  16. __sub=function(a,b)
  17. local aVal,bVal
  18. local isANum, isBNum = (type(a)=="number"), (type(b)=="number")
  19. isANum=isANum or (type(a)=="table" and getmetatable(a)["isNum"])
  20. isBNum=isBNum or (type(b)=="table" and getmetatable(b)["isNum"])
  21. if not isANum or not isBNum then
  22. error("attempt to perform arithmetic __sub on "..(isANum and "number" or type(a)).." and "..(isBNum and "number" or type(b)),2)
  23. return nil
  24. end
  25. if type(a)~= "number" then aVal=a["value"] else aVal=a end
  26. if type(b)~= "number" then bVal=b["value"] else bVal=b end
  27. return aVal-bVal
  28. end;
  29. __mul=function(a,b)
  30. local aVal,bVal
  31. local isANum, isBNum = (type(a)=="number"), (type(b)=="number")
  32. isANum=isANum or (type(a)=="table" and getmetatable(a)["isNum"])
  33. isBNum=isBNum or (type(b)=="table" and getmetatable(b)["isNum"])
  34. if not isANum or not isBNum then
  35. error("attempt to perform arithmetic __mul on "..(isANum and "number" or type(a)).." and "..(isBNum and "number" or type(b)),2)
  36. return nil
  37. end
  38. if type(a)~= "number" then aVal=a["value"] else aVal=a end
  39. if type(b)~= "number" then bVal=b["value"] else bVal=b end
  40. return aVal*bVal
  41. end;
  42. __div=function(a,b)
  43. local aVal,bVal
  44. local isANum, isBNum = (type(a)=="number"), (type(b)=="number")
  45. isANum=isANum or (type(a)=="table" and getmetatable(a)["isNum"])
  46. isBNum=isBNum or (type(b)=="table" and getmetatable(b)["isNum"])
  47. if not isANum or not isBNum then
  48. error("attempt to perform arithmetic __div on "..(isANum and "number" or type(a)).." and "..(isBNum and "number" or type(b)),2)
  49. return nil
  50. end
  51. if type(a)~= "number" then aVal=a["value"] else aVal=a end
  52. if type(b)~= "number" then bVal=b["value"] else bVal=b end
  53. return aVal/bVal
  54. end;
  55. __mod=function(a,b)
  56. local aVal,bVal
  57. local isANum, isBNum = (type(a)=="number"), (type(b)=="number")
  58. isANum=isANum or (type(a)=="table" and getmetatable(a)["isNum"])
  59. isBNum=isBNum or (type(b)=="table" and getmetatable(b)["isNum"])
  60. if not isANum or not isBNum then
  61. error("attempt to perform arithmetic __mod on "..(isANum and "number" or type(a)).." and "..(isBNum and "number" or type(b)),2)
  62. return nil
  63. end
  64. if type(a)~= "number" then aVal=a["value"] else aVal=a end
  65. if type(b)~= "number" then bVal=b["value"] else bVal=b end
  66. return aVal%bVal
  67. end;
  68. __pow=function(a,b)
  69. local aVal,bVal
  70. local isANum, isBNum = (type(a)=="number"), (type(b)=="number")
  71. isANum=isANum or (type(a)=="table" and getmetatable(a)["isNum"])
  72. isBNum=isBNum or (type(b)=="table" and getmetatable(b)["isNum"])
  73. if not isANum or not isBNum then
  74. error("attempt to perform arithmetic __pow on "..(isANum and "number" or type(a)).." and "..(isBNum and "number" or type(b)),2)
  75. return nil
  76. end
  77. if type(a)~= "number" then aVal=a["value"] else aVal=a end
  78. if type(b)~= "number" then bVal=b["value"] else bVal=b end
  79. return aVal^bVal
  80. end;
  81. __unm=function(a)
  82. local aVal
  83. local isANum = (type(a)=="number")
  84. isANum=isANum or (type(a)=="table" and getmetatable(a)["isNum"])
  85. if not isANum then
  86. error("attempt to perform arithmetic on "..(isANum and "number" or type(a)),2)
  87. return nil
  88. end
  89. if type(a)~= "number" then aVal=a["value"] else aVal=a end
  90. return -aVal
  91. end;
  92. __concat=function(a,b)
  93. local aVal,bVal
  94. local isANum, isBNum = (type(a)=="number" or type(a)=="string"), (type(b)=="number" or type(b)=="string")
  95. isANum=isANum or (type(a)=="table" and getmetatable(a)["isNum"])
  96. isBNum=isBNum or (type(b)=="table" and getmetatable(b)["isNum"])
  97. if not isANum or not isBNum then
  98. error("attempt to concatenate "..((isANum and type(a)=="table" and "number") or type(a)).." and "..((isBNum and type(b)=="table" and "number") or type(b)),2)
  99. return nil
  100. end
  101. if type(a)== "table" then aVal=a["value"] else aVal=a end
  102. if type(b)== "table" then bVal=b["value"] else bVal=b end
  103. return aVal..bVal
  104. end;
  105. __len=function(a)
  106. local aVal
  107. local isANum = (type(a)=="number")
  108. isANum=isANum or (type(a)=="table" and getmetatable(a)["isNum"])
  109. if isANum or not (type(a)=="string" or type(a)=="table") then
  110. error("attempt to get length of "..(isANum and "number" or type(a)),2)
  111. return nil
  112. end
  113. return #a
  114. end;
  115. __eq=function(a,b)
  116. local aVal,bVal
  117. local isANum, isBNum = nil, nil
  118. isANum=isANum or (type(a)=="table" and getmetatable(a)["isNum"])
  119. isBNum=isBNum or (type(b)=="table" and getmetatable(b)["isNum"])
  120. if isANum then aVal=a["value"] else aVal=a end
  121. if isBNum then bVal=b["value"] else bVal=b end
  122. return aVal==bVal
  123. end;
  124. __lt=function(a,b)
  125. local aVal,bVal
  126. local isANum, isBNum = (type(a)=="number"), (type(b)=="number")
  127. isANum=isANum or (type(a)=="table" and getmetatable(a)["isNum"])
  128. isBNum=isBNum or (type(b)=="table" and getmetatable(b)["isNum"])
  129. if not isANum or not isBNum then
  130. error("attempt to perform arithmetic __lt on "..(isANum and "number" or type(a)).." and "..(isBNum and "number" or type(b)),2)
  131. return nil
  132. end
  133. if type(a)~= "number" then aVal=a["value"] else aVal=a end
  134. if type(b)~= "number" then bVal=b["value"] else bVal=b end
  135. return aVal<bVal
  136. end;
  137. __le=function(a,b)
  138. local aVal,bVal
  139. local isANum, isBNum = (type(a)=="number"), (type(b)=="number")
  140. isANum=isANum or (type(a)=="table" and getmetatable(a)["isNum"])
  141. isBNum=isBNum or (type(b)=="table" and getmetatable(b)["isNum"])
  142. if not isANum or not isBNum then
  143. error("attempt to perform arithmetic __le on "..(isANum and "number" or type(a)).." and "..(isBNum and "number" or type(b)),2)
  144. return nil
  145. end
  146. if type(a)~= "number" then aVal=a["value"] else aVal=a end
  147. if type(b)~= "number" then bVal=b["value"] else bVal=b end
  148. return aVal<=bVal
  149. end;
  150. __index=function() error("attempt to index ?: a number value",2) end;
  151. __newindex=function() error("attempt to index ?: a number value",2) end;
  152. __call=function() error("attempt to call number",2) end;
  153. }
  154. function newFakeNum(value)
  155. local t={value=value}
  156. return setmetatable(t,meta)
  157. end
Advertisement
Add Comment
Please, Sign In to add comment