table.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. ** table.c
  3. ** Module to control static tables
  4. */
  5. char *rcs_table="$Id: table.c,v 2.1 1994/04/20 22:07:57 celes Exp celes $";
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "mm.h"
  9. #include "opcode.h"
  10. #include "tree.h"
  11. #include "hash.h"
  12. #include "inout.h"
  13. #include "table.h"
  14. #include "lua.h"
  15. #define streq(s1,s2) (s1[0]==s2[0]&&strcmp(s1+1,s2+1)==0)
  16. #define BUFFER_BLOCK 256
  17. Symbol *lua_table;
  18. static Word lua_ntable = 0;
  19. static Word lua_maxsymbol = 0;
  20. char **lua_constant;
  21. static Word lua_nconstant = 0;
  22. static Word lua_maxconstant = 0;
  23. #define MAXFILE 20
  24. char *lua_file[MAXFILE];
  25. int lua_nfile;
  26. /* Variables to controll garbage collection */
  27. #define GARBAGE_BLOCK 256
  28. Word lua_block=GARBAGE_BLOCK; /* when garbage collector will be called */
  29. Word lua_nentity; /* counter of new entities (strings and arrays) */
  30. /*
  31. ** Initialise symbol table with internal functions
  32. */
  33. static void lua_initsymbol (void)
  34. {
  35. int n;
  36. lua_maxsymbol = BUFFER_BLOCK;
  37. lua_table = (Symbol *) calloc(lua_maxsymbol, sizeof(Symbol));
  38. if (lua_table == NULL)
  39. {
  40. lua_error ("symbol table: not enough memory");
  41. return;
  42. }
  43. n = lua_findsymbol("type");
  44. s_tag(n) = T_CFUNCTION; s_fvalue(n) = lua_type;
  45. n = lua_findsymbol("tonumber");
  46. s_tag(n) = T_CFUNCTION; s_fvalue(n) = lua_obj2number;
  47. n = lua_findsymbol("next");
  48. s_tag(n) = T_CFUNCTION; s_fvalue(n) = lua_next;
  49. n = lua_findsymbol("nextvar");
  50. s_tag(n) = T_CFUNCTION; s_fvalue(n) = lua_nextvar;
  51. n = lua_findsymbol("print");
  52. s_tag(n) = T_CFUNCTION; s_fvalue(n) = lua_print;
  53. n = lua_findsymbol("dofile");
  54. s_tag(n) = T_CFUNCTION; s_fvalue(n) = lua_internaldofile;
  55. n = lua_findsymbol("dostring");
  56. s_tag(n) = T_CFUNCTION; s_fvalue(n) = lua_internaldostring;
  57. }
  58. /*
  59. ** Initialise constant table with pre-defined constants
  60. */
  61. void lua_initconstant (void)
  62. {
  63. lua_maxconstant = BUFFER_BLOCK;
  64. lua_constant = (char **) calloc(lua_maxconstant, sizeof(char *));
  65. if (lua_constant == NULL)
  66. {
  67. lua_error ("constant table: not enough memory");
  68. return;
  69. }
  70. lua_findconstant("mark");
  71. lua_findconstant("nil");
  72. lua_findconstant("number");
  73. lua_findconstant("string");
  74. lua_findconstant("table");
  75. lua_findconstant("function");
  76. lua_findconstant("cfunction");
  77. lua_findconstant("userdata");
  78. }
  79. /*
  80. ** Given a name, search it at symbol table and return its index. If not
  81. ** found, allocate it.
  82. ** On error, return -1.
  83. */
  84. int lua_findsymbol (char *s)
  85. {
  86. char *n;
  87. if (lua_table == NULL)
  88. lua_initsymbol();
  89. n = lua_varcreate(s);
  90. if (n == NULL)
  91. {
  92. lua_error ("create symbol: not enough memory");
  93. return -1;
  94. }
  95. if (indexstring(n) == UNMARKED_STRING)
  96. {
  97. if (lua_ntable == lua_maxsymbol)
  98. {
  99. lua_maxsymbol *= 2;
  100. if (lua_maxsymbol > MAX_WORD)
  101. {
  102. lua_error("symbol table overflow");
  103. return -1;
  104. }
  105. lua_table = (Symbol *)realloc(lua_table, lua_maxsymbol*sizeof(Symbol));
  106. if (lua_table == NULL)
  107. {
  108. lua_error ("symbol table: not enough memory");
  109. return -1;
  110. }
  111. }
  112. indexstring(n) = lua_ntable;
  113. s_tag(lua_ntable) = T_NIL;
  114. lua_ntable++;
  115. }
  116. return indexstring(n);
  117. }
  118. /*
  119. ** Given a name, search it at constant table and return its index. If not
  120. ** found, allocate it.
  121. ** On error, return -1.
  122. */
  123. int lua_findconstant (char *s)
  124. {
  125. char *n;
  126. if (lua_constant == NULL)
  127. lua_initconstant();
  128. n = lua_constcreate(s);
  129. if (n == NULL)
  130. {
  131. lua_error ("create constant: not enough memory");
  132. return -1;
  133. }
  134. if (indexstring(n) == UNMARKED_STRING)
  135. {
  136. if (lua_nconstant == lua_maxconstant)
  137. {
  138. lua_maxconstant *= 2;
  139. if (lua_maxconstant > MAX_WORD)
  140. {
  141. lua_error("constant table overflow");
  142. return -1;
  143. }
  144. lua_constant = (char**)realloc(lua_constant,lua_maxconstant*sizeof(char*));
  145. if (lua_constant == NULL)
  146. {
  147. lua_error ("constant table: not enough memory");
  148. return -1;
  149. }
  150. }
  151. indexstring(n) = lua_nconstant;
  152. lua_constant[lua_nconstant] = n;
  153. lua_nconstant++;
  154. }
  155. return indexstring(n);
  156. }
  157. /*
  158. ** Traverse symbol table objects
  159. */
  160. void lua_travsymbol (void (*fn)(Object *))
  161. {
  162. int i;
  163. for (i=0; i<lua_ntable; i++)
  164. fn(&s_object(i));
  165. }
  166. /*
  167. ** Mark an object if it is a string or a unmarked array.
  168. */
  169. void lua_markobject (Object *o)
  170. {
  171. if (tag(o) == T_STRING && indexstring(svalue(o)) == UNMARKED_STRING)
  172. indexstring(svalue(o)) = MARKED_STRING;
  173. else if (tag(o) == T_ARRAY)
  174. lua_hashmark (avalue(o));
  175. }
  176. /*
  177. ** Garbage collection.
  178. ** Delete all unused strings and arrays.
  179. */
  180. void lua_pack (void)
  181. {
  182. /* mark stack strings */
  183. lua_travstack(lua_markobject);
  184. /* mark symbol table strings */
  185. lua_travsymbol(lua_markobject);
  186. lua_strcollector();
  187. lua_hashcollector();
  188. lua_nentity = 0; /* reset counter */
  189. }
  190. /*
  191. ** If the string isn't allocated, allocate a new string at string tree.
  192. */
  193. char *lua_createstring (char *s)
  194. {
  195. if (s == NULL) return NULL;
  196. if (lua_nentity == lua_block)
  197. lua_pack ();
  198. lua_nentity++;
  199. return lua_strcreate(s);
  200. }
  201. /*
  202. ** Add a file name at file table, checking overflow. This function also set
  203. ** the external variable "lua_filename" with the function filename set.
  204. ** Return 0 on success or 1 on error.
  205. */
  206. int lua_addfile (char *fn)
  207. {
  208. if (lua_nfile >= MAXFILE-1)
  209. {
  210. lua_error ("too many files");
  211. return 1;
  212. }
  213. if ((lua_file[lua_nfile++] = strdup (fn)) == NULL)
  214. {
  215. lua_error ("not enough memory");
  216. return 1;
  217. }
  218. return 0;
  219. }
  220. /*
  221. ** Delete a file from file stack
  222. */
  223. int lua_delfile (void)
  224. {
  225. lua_nfile--;
  226. return 1;
  227. }
  228. /*
  229. ** Return the last file name set.
  230. */
  231. char *lua_filename (void)
  232. {
  233. return lua_file[lua_nfile-1];
  234. }
  235. /*
  236. ** Internal function: return next global variable
  237. */
  238. void lua_nextvar (void)
  239. {
  240. char *varname, *next;
  241. Object *o = lua_getparam (1);
  242. if (o == NULL)
  243. { lua_error ("too few arguments to function `nextvar'"); return; }
  244. if (lua_getparam (2) != NULL)
  245. { lua_error ("too many arguments to function `nextvar'"); return; }
  246. if (tag(o) == T_NIL)
  247. {
  248. varname = 0;
  249. }
  250. else if (tag(o) != T_STRING)
  251. {
  252. lua_error ("incorrect argument to function `nextvar'");
  253. return;
  254. }
  255. else
  256. {
  257. varname = svalue(o);
  258. }
  259. next = lua_varnext(varname);
  260. if (next == NULL)
  261. {
  262. lua_pushnil();
  263. lua_pushnil();
  264. }
  265. else
  266. {
  267. Object name;
  268. tag(&name) = T_STRING;
  269. svalue(&name) = next;
  270. if (lua_pushobject (&name)) return;
  271. if (lua_pushobject (&s_object(indexstring(next)))) return;
  272. }
  273. }