lobject.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. ** $Id: lobject.c,v 1.35 2000/03/29 20:19:20 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. default:
  35. LUA_ASSERT(L, ttype(t1) == TAG_NIL, "invalid type");
  36. return 1; /* TAG_NIL */
  37. }
  38. }
  39. static double expten (unsigned int e) {
  40. double exp = 10.0;
  41. double res = 1.0;
  42. for (; e; e>>=1) {
  43. if (e & 1) res *= exp;
  44. exp *= exp;
  45. }
  46. return res;
  47. }
  48. int luaO_str2d (const char *s, Number *result) { /* LUA_NUMBER */
  49. double a = 0.0;
  50. int point = 0; /* number of decimal digits */
  51. int sig;
  52. while (isspace((unsigned char)*s)) s++;
  53. sig = 0;
  54. switch (*s) {
  55. case '-': sig = 1; /* go through */
  56. case '+': s++;
  57. }
  58. if (! (isdigit((unsigned char)*s) ||
  59. (*s == '.' && isdigit((unsigned char)*(s+1)))))
  60. return 0; /* not (at least one digit before or after the point) */
  61. while (isdigit((unsigned char)*s))
  62. a = 10.0*a + (*(s++)-'0');
  63. if (*s == '.') {
  64. s++;
  65. while (isdigit((unsigned char)*s)) {
  66. a = 10.0*a + (*(s++)-'0');
  67. point++;
  68. }
  69. }
  70. if (sig) a = -a;
  71. if (*s == 'e' || *s == 'E') {
  72. int e = 0;
  73. s++;
  74. sig = 0;
  75. switch (*s) {
  76. case '-': sig = 1; /* go through */
  77. case '+': s++;
  78. }
  79. if (!isdigit((unsigned char)*s)) return 0; /* no digit in the exponent? */
  80. do {
  81. e = 10*e + (*(s++)-'0');
  82. } while (isdigit((unsigned char)*s));
  83. if (sig) e = -e;
  84. point -= e;
  85. }
  86. while (isspace((unsigned char)*s)) s++;
  87. if (*s != '\0') return 0; /* invalid trailing characters? */
  88. if (point != 0) {
  89. if (point > 0) a /= expten(point);
  90. else a *= expten(-point);
  91. }
  92. *result = a;
  93. return 1;
  94. }