lobject.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. ** $Id: lobject.c,v 1.31 2000/02/17 18:30:36 roberto Exp roberto $
  3. ** Some generic functions over Lua objects
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <ctype.h>
  7. #include <stdlib.h>
  8. #define LUA_REENTRANT
  9. #include "lobject.h"
  10. #include "lua.h"
  11. const char *const luaO_typenames[] = { /* ORDER LUA_T */
  12. "userdata", "number", "string", "table", "function", "function", "nil",
  13. "function", "function", "function", "function", "function", "function",
  14. "line", NULL
  15. };
  16. const TObject luaO_nilobject = {LUA_T_NIL, {NULL}};
  17. /*
  18. ** returns smaller power of 2 larger than `n' (minimum is MINPOWER2)
  19. */
  20. unsigned long luaO_power2 (unsigned long n) {
  21. unsigned long p = MINPOWER2;
  22. while (p<=n) p<<=1;
  23. return p;
  24. }
  25. int luaO_equalval (const TObject *t1, const TObject *t2) {
  26. switch (ttype(t1)) {
  27. case LUA_T_NIL:
  28. return 1;
  29. case LUA_T_NUMBER:
  30. return nvalue(t1) == nvalue(t2);
  31. case LUA_T_STRING: case LUA_T_USERDATA:
  32. return svalue(t1) == svalue(t2);
  33. case LUA_T_ARRAY:
  34. return avalue(t1) == avalue(t2);
  35. case LUA_T_LPROTO:
  36. return tfvalue(t1) == tfvalue(t2);
  37. case LUA_T_CPROTO:
  38. return fvalue(t1) == fvalue(t2);
  39. case LUA_T_CCLOSURE: case LUA_T_LCLOSURE:
  40. return t1->value.cl == t2->value.cl;
  41. default:
  42. LUA_INTERNALERROR(L, "invalid type");
  43. return 0; /* UNREACHABLE */
  44. }
  45. }
  46. static double expten (unsigned int e) {
  47. double exp = 10.0;
  48. double res = 1.0;
  49. for (; e; e>>=1) {
  50. if (e & 1) res *= exp;
  51. exp *= exp;
  52. }
  53. return res;
  54. }
  55. int luaO_str2d (const char *s, real *result) { /* LUA_NUMBER */
  56. double a = 0.0;
  57. int point = 0; /* number of decimal digits */
  58. int sig;
  59. while (isspace((unsigned char)*s)) s++;
  60. sig = 0;
  61. switch (*s) {
  62. case '-': sig = 1; /* go through */
  63. case '+': s++;
  64. }
  65. if (! (isdigit((unsigned char)*s) ||
  66. (*s == '.' && isdigit((unsigned char)*(s+1)))))
  67. return 0; /* not (at least one digit before or after the point) */
  68. while (isdigit((unsigned char)*s))
  69. a = 10.0*a + (*(s++)-'0');
  70. if (*s == '.') {
  71. s++;
  72. while (isdigit((unsigned char)*s)) {
  73. a = 10.0*a + (*(s++)-'0');
  74. point++;
  75. }
  76. }
  77. if (sig) a = -a;
  78. if (*s == 'e' || *s == 'E') {
  79. int e = 0;
  80. s++;
  81. sig = 0;
  82. switch (*s) {
  83. case '-': sig = 1; /* go through */
  84. case '+': s++;
  85. }
  86. if (!isdigit((unsigned char)*s)) return 0; /* no digit in the exponent? */
  87. do {
  88. e = 10*e + (*(s++)-'0');
  89. } while (isdigit((unsigned char)*s));
  90. if (sig) e = -e;
  91. point -= e;
  92. }
  93. while (isspace((unsigned char)*s)) s++;
  94. if (*s != '\0') return 0; /* invalid trailing characters? */
  95. if (point != 0) {
  96. if (point > 0) a /= expten(point);
  97. else a *= expten(-point);
  98. }
  99. *result = a;
  100. return 1;
  101. }