table.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. ** table.c
  3. ** Module to control static tables
  4. */
  5. char *rcs_table="$Id: $";
  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, eliminate its delimeters (" or '), search it at
  104. ** constant table and return its index. If not found, allocate at end of
  105. ** the table, checking oveflow and return 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_findenclosedconstant (char *s)
  114. {
  115. int i, j, l=strlen(s);
  116. char *c = calloc (l, sizeof(char)); /* make a copy */
  117. c++; /* create mark space */
  118. /* introduce scape characters */
  119. for (i=1,j=0; i<l-1; i++)
  120. {
  121. if (s[i] == '\\')
  122. {
  123. switch (s[i+1])
  124. {
  125. case 'n': c[j++] = '\n'; i++; break;
  126. case 't': c[j++] = '\t'; i++; break;
  127. case 'r': c[j++] = '\r'; i++; break;
  128. default : c[j++] = '\\'; break;
  129. }
  130. }
  131. else
  132. c[j++] = s[i];
  133. }
  134. c[j++] = 0;
  135. for (i=0; i<lua_nconstant; i++)
  136. if (streq(c,lua_constant[i]))
  137. {
  138. free (c-1);
  139. return i;
  140. }
  141. if (lua_nconstant >= MAXCONSTANT-1)
  142. {
  143. lua_error ("lua: constant string table overflow");
  144. return -1;
  145. }
  146. lua_constant[lua_nconstant++] = c;
  147. return (lua_nconstant-1);
  148. }
  149. /*
  150. ** Given a constant string, search it at constant table and return its index.
  151. ** If not found, allocate at end of the table, checking oveflow and return
  152. ** its index.
  153. **
  154. ** For each allocation, the function allocate a extra char to be used to
  155. ** mark used string (it's necessary to deal with constant and string
  156. ** uniformily). The function store at the table the second position allocated,
  157. ** that represents the beginning of the real string. On error, return -1.
  158. **
  159. */
  160. int lua_findconstant (char *s)
  161. {
  162. int i;
  163. for (i=0; i<lua_nconstant; i++)
  164. if (streq(s,lua_constant[i]))
  165. return i;
  166. if (lua_nconstant >= MAXCONSTANT-1)
  167. {
  168. lua_error ("lua: constant string table overflow");
  169. return -1;
  170. }
  171. {
  172. char *c = calloc(strlen(s)+2,sizeof(char));
  173. c++; /* create mark space */
  174. lua_constant[lua_nconstant++] = strcpy(c,s);
  175. }
  176. return (lua_nconstant-1);
  177. }
  178. /*
  179. ** Mark an object if it is a string or a unmarked array.
  180. */
  181. void lua_markobject (Object *o)
  182. {
  183. if (tag(o) == T_STRING)
  184. lua_markstring (svalue(o)) = 1;
  185. else if (tag(o) == T_ARRAY && markarray(avalue(o)) == 0)
  186. lua_hashmark (avalue(o));
  187. }
  188. /*
  189. ** Mark all strings and arrays used by any object stored at symbol table.
  190. */
  191. static void lua_marktable (void)
  192. {
  193. int i;
  194. for (i=0; i<lua_ntable; i++)
  195. lua_markobject (&s_object(i));
  196. }
  197. /*
  198. ** Simulate a garbage colection. When string table or array table overflows,
  199. ** this function check if all allocated strings and arrays are in use. If
  200. ** there are unused ones, pack (compress) the tables.
  201. */
  202. static void lua_pack (void)
  203. {
  204. lua_markstack ();
  205. lua_marktable ();
  206. { /* pack string */
  207. int i, j;
  208. for (i=j=0; i<lua_nstring; i++)
  209. if (lua_markstring(lua_string[i]) == 1)
  210. {
  211. lua_string[j++] = lua_string[i];
  212. lua_markstring(lua_string[i]) = 0;
  213. }
  214. else
  215. {
  216. free (lua_string[i]-1);
  217. }
  218. lua_nstring = j;
  219. }
  220. { /* pack array */
  221. int i, j;
  222. for (i=j=0; i<lua_narray; i++)
  223. if (markarray(lua_array[i]) == 1)
  224. {
  225. lua_array[j++] = lua_array[i];
  226. markarray(lua_array[i]) = 0;
  227. }
  228. else
  229. {
  230. lua_hashdelete (lua_array[i]);
  231. }
  232. lua_narray = j;
  233. }
  234. }
  235. /*
  236. ** Allocate a new string at string table. The given string is already
  237. ** allocated with mark space and the function puts it at the end of the
  238. ** table, checking overflow, and returns its own pointer, or NULL on error.
  239. */
  240. char *lua_createstring (char *s)
  241. {
  242. if (s == NULL) return NULL;
  243. if (lua_nstring >= MAXSTRING-1)
  244. {
  245. lua_pack ();
  246. if (lua_nstring >= MAXSTRING-1)
  247. {
  248. lua_error ("string table overflow");
  249. return NULL;
  250. }
  251. }
  252. lua_string[lua_nstring++] = s;
  253. return s;
  254. }
  255. /*
  256. ** Allocate a new array, already created, at array table. The function puts
  257. ** it at the end of the table, checking overflow, and returns its own pointer,
  258. ** or NULL on error.
  259. */
  260. void *lua_createarray (void *a)
  261. {
  262. if (a == NULL) return NULL;
  263. if (lua_narray >= MAXARRAY-1)
  264. {
  265. lua_pack ();
  266. if (lua_narray >= MAXARRAY-1)
  267. {
  268. lua_error ("indexed table overflow");
  269. return NULL;
  270. }
  271. }
  272. lua_array[lua_narray++] = a;
  273. return a;
  274. }
  275. /*
  276. ** Add a file name at file table, checking overflow. This function also set
  277. ** the external variable "lua_filename" with the function filename set.
  278. ** Return 0 on success or 1 on error.
  279. */
  280. int lua_addfile (char *fn)
  281. {
  282. if (lua_nfile >= MAXFILE-1)
  283. {
  284. lua_error ("too many files");
  285. return 1;
  286. }
  287. if ((lua_file[lua_nfile++] = strdup (fn)) == NULL)
  288. {
  289. lua_error ("not enough memory");
  290. return 1;
  291. }
  292. return 0;
  293. }
  294. /*
  295. ** Delete a file from file stack
  296. */
  297. int lua_delfile (void)
  298. {
  299. lua_nfile--;
  300. return 1;
  301. }
  302. /*
  303. ** Return the last file name set.
  304. */
  305. char *lua_filename (void)
  306. {
  307. return lua_file[lua_nfile-1];
  308. }
  309. /*
  310. ** Internal function: return next global variable
  311. */
  312. void lua_nextvar (void)
  313. {
  314. int index;
  315. Object *o = lua_getparam (1);
  316. if (o == NULL)
  317. { lua_error ("too few arguments to function `nextvar'"); return; }
  318. if (lua_getparam (2) != NULL)
  319. { lua_error ("too many arguments to function `nextvar'"); return; }
  320. if (tag(o) == T_NIL)
  321. {
  322. index = 0;
  323. }
  324. else if (tag(o) != T_STRING)
  325. {
  326. lua_error ("incorrect argument to function `nextvar'");
  327. return;
  328. }
  329. else
  330. {
  331. for (index=0; index<lua_ntable; index++)
  332. if (streq(s_name(index),svalue(o))) break;
  333. if (index == lua_ntable)
  334. {
  335. lua_error ("name not found in function `nextvar'");
  336. return;
  337. }
  338. index++;
  339. while (index < lua_ntable && tag(&s_object(index)) == T_NIL) index++;
  340. if (index == lua_ntable)
  341. {
  342. lua_pushnil();
  343. lua_pushnil();
  344. return;
  345. }
  346. }
  347. {
  348. Object name;
  349. tag(&name) = T_STRING;
  350. svalue(&name) = lua_createstring(lua_strdup(s_name(index)));
  351. if (lua_pushobject (&name)) return;
  352. if (lua_pushobject (&s_object(index))) return;
  353. }
  354. }