3 ** Module to control static tables
17 #define streq(s1,s2) (strcmp(s1,s2)==0)
22 static Symbol tablebuffer
[MAXSYMBOL
] = {
23 {"type",{T_CFUNCTION
,{lua_type
}}},
24 {"tonumber",{T_CFUNCTION
,{lua_obj2number
}}},
25 {"next",{T_CFUNCTION
,{lua_next
}}},
26 {"nextvar",{T_CFUNCTION
,{lua_nextvar
}}},
27 {"print",{T_CFUNCTION
,{lua_print
}}}
29 Symbol
*lua_table
=tablebuffer
;
33 #define MAXCONSTANT 256
35 static char *constantbuffer
[MAXCONSTANT
] = {"mark","nil","number",
37 "function","cfunction"
39 char **lua_constant
= constantbuffer
;
40 Word lua_nconstant
=T_CFUNCTION
+1;
45 static char *stringbuffer
[MAXSTRING
];
46 char **lua_string
= stringbuffer
;
52 static Hash
*arraybuffer
[MAXARRAY
];
53 Hash
**lua_array
= arraybuffer
;
57 char *lua_file
[MAXFILE
];
62 ** Given a name, search it at symbol table and return its index. If not
63 ** found, allocate at end of table, checking oveflow and return its index.
64 ** On error, return -1.
66 int lua_findsymbol (char *s
)
69 for (i
=0; i
<lua_ntable
; i
++)
70 if (streq(s
,s_name(i
)))
72 if (lua_ntable
>= MAXSYMBOL
-1)
74 lua_error ("symbol table overflow");
77 s_name(lua_ntable
) = strdup(s
);
78 if (s_name(lua_ntable
) == NULL
)
80 lua_error ("not enough memory");
83 s_tag(lua_ntable
++) = T_NIL
;
85 return (lua_ntable
-1);
89 ** Given a constant string, eliminate its delimeters (" or '), search it at
90 ** constant table and return its index. If not found, allocate at end of
91 ** the table, checking oveflow and return its index.
93 ** For each allocation, the function allocate a extra char to be used to
94 ** mark used string (it's necessary to deal with constant and string
95 ** uniformily). The function store at the table the second position allocated,
96 ** that represents the beginning of the real string. On error, return -1.
99 int lua_findenclosedconstant (char *s
)
101 int i
, j
, l
=strlen(s
);
102 char *c
= calloc (l
, sizeof(char)); /* make a copy */
104 c
++; /* create mark space */
106 /* introduce scape characters */
107 for (i
=1,j
=0; i
<l
-1; i
++)
113 case 'n': c
[j
++] = '\n'; break;
114 case 't': c
[j
++] = '\t'; break;
115 case 'r': c
[j
++] = '\r'; break;
116 default : c
[j
++] = '\\'; c
[j
++] = c
[i
]; break;
124 for (i
=0; i
<lua_nconstant
; i
++)
125 if (streq(c
,lua_constant
[i
]))
130 if (lua_nconstant
>= MAXCONSTANT
-1)
132 lua_error ("lua: constant string table overflow");
135 lua_constant
[lua_nconstant
++] = c
;
136 return (lua_nconstant
-1);
140 ** Given a constant string, search it at constant table and return its index.
141 ** If not found, allocate at end of the table, checking oveflow and return
144 ** For each allocation, the function allocate a extra char to be used to
145 ** mark used string (it's necessary to deal with constant and string
146 ** uniformily). The function store at the table the second position allocated,
147 ** that represents the beginning of the real string. On error, return -1.
150 int lua_findconstant (char *s
)
153 for (i
=0; i
<lua_nconstant
; i
++)
154 if (streq(s
,lua_constant
[i
]))
156 if (lua_nconstant
>= MAXCONSTANT
-1)
158 lua_error ("lua: constant string table overflow");
162 char *c
= calloc(strlen(s
)+2,sizeof(char));
163 c
++; /* create mark space */
164 lua_constant
[lua_nconstant
++] = strcpy(c
,s
);
166 return (lua_nconstant
-1);
171 ** Mark an object if it is a string or a unmarked array.
173 void lua_markobject (Object
*o
)
175 if (tag(o
) == T_STRING
)
176 lua_markstring (svalue(o
)) = 1;
177 else if (tag(o
) == T_ARRAY
&& markarray(avalue(o
)) == 0)
178 lua_hashmark (avalue(o
));
182 ** Mark all strings and arrays used by any object stored at symbol table.
184 static void lua_marktable (void)
187 for (i
=0; i
<lua_ntable
; i
++)
188 lua_markobject (&s_object(i
));
192 ** Simulate a garbage colection. When string table or array table overflows,
193 ** this function check if all allocated strings and arrays are in use. If
194 ** there are unused ones, pack (compress) the tables.
196 static void lua_pack (void)
203 for (i
=j
=0; i
<lua_nstring
; i
++)
204 if (lua_markstring(lua_string
[i
]) == 1)
206 lua_string
[j
++] = lua_string
[i
];
207 lua_markstring(lua_string
[i
]) = 0;
211 free (lua_string
[i
]-1);
218 for (i
=j
=0; i
<lua_narray
; i
++)
219 if (markarray(lua_array
[i
]) == 1)
221 lua_array
[j
++] = lua_array
[i
];
222 markarray(lua_array
[i
]) = 0;
226 lua_hashdelete (lua_array
[i
]);
233 ** Allocate a new string at string table. The given string is already
234 ** allocated with mark space and the function puts it at the end of the
235 ** table, checking overflow, and returns its own pointer, or NULL on error.
237 char *lua_createstring (char *s
)
239 if (s
== NULL
) return NULL
;
241 if (lua_nstring
>= MAXSTRING
-1)
244 if (lua_nstring
>= MAXSTRING
-1)
246 lua_error ("string table overflow");
250 lua_string
[lua_nstring
++] = s
;
255 ** Allocate a new array, already created, at array table. The function puts
256 ** it at the end of the table, checking overflow, and returns its own pointer,
259 void *lua_createarray (void *a
)
261 if (a
== NULL
) return NULL
;
263 if (lua_narray
>= MAXARRAY
-1)
266 if (lua_narray
>= MAXARRAY
-1)
268 lua_error ("indexed table overflow");
272 lua_array
[lua_narray
++] = a
;
278 ** Add a file name at file table, checking overflow. This function also set
279 ** the external variable "lua_filename" with the function filename set.
280 ** Return 0 on success or 1 on error.
282 int lua_addfile (char *fn
)
284 if (lua_nfile
>= MAXFILE
-1)
286 lua_error ("too many files");
289 if ((lua_file
[lua_nfile
++] = strdup (fn
)) == NULL
)
291 lua_error ("not enough memory");
298 ** Return the last file name set.
300 char *lua_filename (void)
302 return lua_file
[lua_nfile
-1];
306 ** Internal function: return next global variable
308 void lua_nextvar (void)
311 Object
*o
= lua_getparam (1);
313 { lua_error ("too few arguments to function `nextvar'"); return; }
314 if (lua_getparam (2) != NULL
)
315 { lua_error ("too many arguments to function `nextvar'"); return; }
320 else if (tag(o
) != T_STRING
)
322 lua_error ("incorrect argument to function `nextvar'");
327 for (index
=0; index
<lua_ntable
; index
++)
328 if (streq(s_name(index
),svalue(o
))) break;
329 if (index
== lua_ntable
)
331 lua_error ("name not found in function `nextvar'");
335 while (index
< lua_ntable
-1 && tag(&s_object(index
)) == T_NIL
) index
++;
337 if (index
== lua_ntable
-1)
346 tag(&name
) = T_STRING
;
347 svalue(&name
) = lua_createstring(lua_strdup(s_name(index
)));
348 if (lua_pushobject (&name
)) return;
349 if (lua_pushobject (&s_object(index
))) return;