lobject.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. ** $Id: lobject.c,v 1.34 2000/03/27 20:10:21 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", "line"
  14. };
  15. const TObject luaO_nilobject = {TAG_NIL, {NULL}};
  16. /*
  17. ** returns smaller power of 2 larger than `n' (minimum is MINPOWER2)
  18. */
  19. unsigned long luaO_power2 (unsigned long n) {
  20. unsigned long p = MINPOWER2;
  21. while (p<=n) p<<=1;
  22. return p;
  23. }
  24. int luaO_equalval (const TObject *t1, const TObject *t2) {
  25. switch (ttype(t1)) {
  26. case TAG_NUMBER:
  27. return nvalue(t1) == nvalue(t2);
  28. case TAG_STRING: case TAG_USERDATA:
  29. return svalue(t1) == svalue(t2);
  30. case TAG_TABLE:
  31. return avalue(t1) == avalue(t2);
  32. case TAG_CCLOSURE: case TAG_LCLOSURE:
  33. return clvalue(t1) == clvalue(t2);
  34. case TAG_NIL:
  35. return 1;
  36. default:
  37. LUA_INTERNALERROR(L, "invalid type");
  38. return 0; /* UNREACHABLE */
  39. }
  40. }
  41. static double expten (unsigned int e) {
  42. double exp = 10.0;
  43. double res = 1.0;
  44. for (; e; e>>=1) {
  45. if (e & 1) res *= exp;
  46. exp *= exp;
  47. }
  48. return res;
  49. }
  50. int luaO_str2d (const char *s, Number *result) { /* LUA_NUMBER */
  51. double a = 0.0;
  52. int point = 0; /* number of decimal digits */
  53. int sig;
  54. while (isspace((unsigned char)*s)) s++;
  55. sig = 0;
  56. switch (*s) {
  57. case '-': sig = 1; /* go through */
  58. case '+': s++;
  59. }
  60. if (! (isdigit((unsigned char)*s) ||
  61. (*s == '.' && isdigit((unsigned char)*(s+1)))))
  62. return 0; /* not (at least one digit before or after the point) */
  63. while (isdigit((unsigned char)*s))
  64. a = 10.0*a + (*(s++)-'0');
  65. if (*s == '.') {
  66. s++;
  67. while (isdigit((unsigned char)*s)) {
  68. a = 10.0*a + (*(s++)-'0');
  69. point++;
  70. }
  71. }
  72. if (sig) a = -a;
  73. if (*s == 'e' || *s == 'E') {
  74. int e = 0;
  75. s++;
  76. sig = 0;
  77. switch (*s) {
  78. case '-': sig = 1; /* go through */
  79. case '+': s++;
  80. }
  81. if (!isdigit((unsigned char)*s)) return 0; /* no digit in the exponent? */
  82. do {
  83. e = 10*e + (*(s++)-'0');
  84. } while (isdigit((unsigned char)*s));
  85. if (sig) e = -e;
  86. point -= e;
  87. }
  88. while (isspace((unsigned char)*s)) s++;
  89. if (*s != '\0') return 0; /* invalid trailing characters? */
  90. if (point != 0) {
  91. if (point > 0) a /= expten(point);
  92. else a *= expten(-point);
  93. }
  94. *result = a;
  95. return 1;
  96. }