table.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. ** table.c
  3. ** Module to control static tables
  4. */
  5. char *rcs_table="$Id: table.c,v 1.1 1993/12/17 18:41:19 celes Exp roberto $";
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "opcode.h"
  9. #include "hash.h"
  10. #include "inout.h"
  11. #include "table.h"
  12. #include "lua.h"
  13. #define streq(s1,s2) (s1[0]==s2[0]&&strcmp(s1+1,s2+1)==0)
  14. #ifndef MAXSYMBOL
  15. #define MAXSYMBOL 512
  16. #endif
  17. static Symbol tablebuffer[MAXSYMBOL] = {
  18. {"type",{T_CFUNCTION,{lua_type}}},
  19. {"tonumber",{T_CFUNCTION,{lua_obj2number}}},
  20. {"next",{T_CFUNCTION,{lua_next}}},
  21. {"nextvar",{T_CFUNCTION,{lua_nextvar}}},
  22. {"print",{T_CFUNCTION,{lua_print}}},
  23. {"dofile",{T_CFUNCTION,{lua_internaldofile}}},
  24. {"dostring",{T_CFUNCTION,{lua_internaldostring}}}
  25. };
  26. Symbol *lua_table=tablebuffer;
  27. Word lua_ntable=7;
  28. struct List
  29. {
  30. Symbol *s;
  31. struct List *next;
  32. };
  33. static struct List o6={ tablebuffer+6, 0};
  34. static struct List o5={ tablebuffer+5, &o6 };
  35. static struct List o4={ tablebuffer+4, &o5 };
  36. static struct List o3={ tablebuffer+3, &o4 };
  37. static struct List o2={ tablebuffer+2, &o3 };
  38. static struct List o1={ tablebuffer+1, &o2 };
  39. static struct List o0={ tablebuffer+0, &o1 };
  40. static struct List *searchlist=&o0;
  41. #ifndef MAXCONSTANT
  42. #define MAXCONSTANT 256
  43. #endif
  44. static char *constantbuffer[MAXCONSTANT] = {"mark","nil","number",
  45. "string","table",
  46. "function","cfunction"
  47. };
  48. char **lua_constant = constantbuffer;
  49. Word lua_nconstant=T_CFUNCTION+1;
  50. #ifndef MAXSTRING
  51. #define MAXSTRING 512
  52. #endif
  53. static char *stringbuffer[MAXSTRING];
  54. char **lua_string = stringbuffer;
  55. Word lua_nstring=0;
  56. #ifndef MAXARRAY
  57. #define MAXARRAY 512
  58. #endif
  59. static Hash *arraybuffer[MAXARRAY];
  60. Hash **lua_array = arraybuffer;
  61. Word lua_narray=0;
  62. #define MAXFILE 20
  63. char *lua_file[MAXFILE];
  64. int lua_nfile;
  65. /*
  66. ** Given a name, search it at symbol table and return its index. If not
  67. ** found, allocate at end of table, checking oveflow and return its index.
  68. ** On error, return -1.
  69. */
  70. int lua_findsymbol (char *s)
  71. {
  72. struct List *l, *p;
  73. for (p=NULL, l=searchlist; l!=NULL; p=l, l=l->next)
  74. if (streq(s,l->s->name))
  75. {
  76. if (p!=NULL)
  77. {
  78. p->next = l->next;
  79. l->next = searchlist;
  80. searchlist = l;
  81. }
  82. return (l->s-lua_table);
  83. }
  84. if (lua_ntable >= MAXSYMBOL-1)
  85. {
  86. lua_error ("symbol table overflow");
  87. return -1;
  88. }
  89. s_name(lua_ntable) = strdup(s);
  90. if (s_name(lua_ntable) == NULL)
  91. {
  92. lua_error ("not enough memory");
  93. return -1;
  94. }
  95. s_tag(lua_ntable) = T_NIL;
  96. p = malloc(sizeof(*p));
  97. p->s = lua_table+lua_ntable;
  98. p->next = searchlist;
  99. searchlist = p;
  100. return lua_ntable++;
  101. }
  102. /*
  103. ** Given a constant string, search it at constant table and return its index.
  104. ** If not found, allocate at end of the table, checking oveflow and return
  105. ** its index.
  106. **
  107. ** For each allocation, the function allocate a extra char to be used to
  108. ** mark used string (it's necessary to deal with constant and string
  109. ** uniformily). The function store at the table the second position allocated,
  110. ** that represents the beginning of the real string. On error, return -1.
  111. **
  112. */
  113. int lua_findconstant (char *s)
  114. {
  115. int i;
  116. for (i=0; i<lua_nconstant; i++)
  117. if (streq(s,lua_constant[i]))
  118. return i;
  119. if (lua_nconstant >= MAXCONSTANT-1)
  120. {
  121. lua_error ("lua: constant string table overflow");
  122. return -1;
  123. }
  124. {
  125. char *c = calloc(strlen(s)+2,sizeof(char));
  126. c++; /* create mark space */
  127. lua_constant[lua_nconstant++] = strcpy(c,s);
  128. }
  129. return (lua_nconstant-1);
  130. }
  131. /*
  132. ** Mark an object if it is a string or a unmarked array.
  133. */
  134. void lua_markobject (Object *o)
  135. {
  136. if (tag(o) == T_STRING)
  137. lua_markstring (svalue(o)) = 1;
  138. else if (tag(o) == T_ARRAY && markarray(avalue(o)) == 0)
  139. lua_hashmark (avalue(o));
  140. }
  141. /*
  142. ** Mark all strings and arrays used by any object stored at symbol table.
  143. */
  144. static void lua_marktable (void)
  145. {
  146. int i;
  147. for (i=0; i<lua_ntable; i++)
  148. lua_markobject (&s_object(i));
  149. }
  150. /*
  151. ** Simulate a garbage colection. When string table or array table overflows,
  152. ** this function check if all allocated strings and arrays are in use. If
  153. ** there are unused ones, pack (compress) the tables.
  154. */
  155. static void lua_pack (void)
  156. {
  157. lua_markstack ();
  158. lua_marktable ();
  159. { /* pack string */
  160. int i, j;
  161. for (i=j=0; i<lua_nstring; i++)
  162. if (lua_markstring(lua_string[i]) == 1)
  163. {
  164. lua_string[j++] = lua_string[i];
  165. lua_markstring(lua_string[i]) = 0;
  166. }
  167. else
  168. {
  169. free (lua_string[i]-1);
  170. }
  171. lua_nstring = j;
  172. }
  173. { /* pack array */
  174. int i, j;
  175. for (i=j=0; i<lua_narray; i++)
  176. if (markarray(lua_array[i]) == 1)
  177. {
  178. lua_array[j++] = lua_array[i];
  179. markarray(lua_array[i]) = 0;
  180. }
  181. else
  182. {
  183. lua_hashdelete (lua_array[i]);
  184. }
  185. lua_narray = j;
  186. }
  187. }
  188. /*
  189. ** Allocate a new string at string table. The given string is already
  190. ** allocated with mark space and the function puts it at the end of the
  191. ** table, checking overflow, and returns its own pointer, or NULL on error.
  192. */
  193. char *lua_createstring (char *s)
  194. {
  195. if (s == NULL) return NULL;
  196. if (lua_nstring >= MAXSTRING-1)
  197. {
  198. lua_pack ();
  199. if (lua_nstring >= MAXSTRING-1)
  200. {
  201. lua_error ("string table overflow");
  202. return NULL;
  203. }
  204. }
  205. lua_string[lua_nstring++] = s;
  206. return s;
  207. }
  208. /*
  209. ** Allocate a new array, already created, at array table. The function puts
  210. ** it at the end of the table, checking overflow, and returns its own pointer,
  211. ** or NULL on error.
  212. */
  213. void *lua_createarray (void *a)
  214. {
  215. if (a == NULL) return NULL;
  216. if (lua_narray >= MAXARRAY-1)
  217. {
  218. lua_pack ();
  219. if (lua_narray >= MAXARRAY-1)
  220. {
  221. lua_error ("indexed table overflow");
  222. return NULL;
  223. }
  224. }
  225. lua_array[lua_narray++] = a;
  226. return a;
  227. }
  228. /*
  229. ** Add a file name at file table, checking overflow. This function also set
  230. ** the external variable "lua_filename" with the function filename set.
  231. ** Return 0 on success or 1 on error.
  232. */
  233. int lua_addfile (char *fn)
  234. {
  235. if (lua_nfile >= MAXFILE-1)
  236. {
  237. lua_error ("too many files");
  238. return 1;
  239. }
  240. if ((lua_file[lua_nfile++] = strdup (fn)) == NULL)
  241. {
  242. lua_error ("not enough memory");
  243. return 1;
  244. }
  245. return 0;
  246. }
  247. /*
  248. ** Delete a file from file stack
  249. */
  250. int lua_delfile (void)
  251. {
  252. lua_nfile--;
  253. return 1;
  254. }
  255. /*
  256. ** Return the last file name set.
  257. */
  258. char *lua_filename (void)
  259. {
  260. return lua_file[lua_nfile-1];
  261. }
  262. /*
  263. ** Internal function: return next global variable
  264. */
  265. void lua_nextvar (void)
  266. {
  267. int index;
  268. Object *o = lua_getparam (1);
  269. if (o == NULL)
  270. { lua_error ("too few arguments to function `nextvar'"); return; }
  271. if (lua_getparam (2) != NULL)
  272. { lua_error ("too many arguments to function `nextvar'"); return; }
  273. if (tag(o) == T_NIL)
  274. {
  275. index = 0;
  276. }
  277. else if (tag(o) != T_STRING)
  278. {
  279. lua_error ("incorrect argument to function `nextvar'");
  280. return;
  281. }
  282. else
  283. {
  284. for (index=0; index<lua_ntable; index++)
  285. if (streq(s_name(index),svalue(o))) break;
  286. if (index == lua_ntable)
  287. {
  288. lua_error ("name not found in function `nextvar'");
  289. return;
  290. }
  291. index++;
  292. while (index < lua_ntable && tag(&s_object(index)) == T_NIL) index++;
  293. if (index == lua_ntable)
  294. {
  295. lua_pushnil();
  296. lua_pushnil();
  297. return;
  298. }
  299. }
  300. {
  301. Object name;
  302. tag(&name) = T_STRING;
  303. svalue(&name) = lua_createstring(lua_strdup(s_name(index)));
  304. if (lua_pushobject (&name)) return;
  305. if (lua_pushobject (&s_object(index))) return;
  306. }
  307. }