table.c 7.9 KB

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