summaryrefslogtreecommitdiff
path: root/src/func.c
diff options
context:
space:
mode:
authorLua Team <team@lua.org>1996-05-14 12:00:00 +0000
committerrepogen <>1996-05-14 12:00:00 +0000
commit721542976ebc89f2f8d17d19be7e4426570b69be (patch)
tree0c79a45c63aa89d6e4b8ac80931e46d74a72f8cb /src/func.c
parent71754d2f6423fb9b6e87658e58bafc5470d53f65 (diff)
downloadlua-github-2.4.tar.gz
Lua 2.42.4
Diffstat (limited to 'src/func.c')
-rw-r--r--src/func.c83
1 files changed, 80 insertions, 3 deletions
diff --git a/src/func.c b/src/func.c
index 2982f661..21b19a19 100644
--- a/src/func.c
+++ b/src/func.c
@@ -1,4 +1,4 @@
-#include <stdio.h>
+#include <stdlib.h>
#include "luadebug.h"
#include "table.h"
@@ -6,8 +6,26 @@
#include "func.h"
#include "opcode.h"
+
static TFunc *function_root = NULL;
+static LocVar *currvars = NULL;
+static int numcurrvars = 0;
+static int maxcurrvars = 0;
+
+/*
+** Initialize TFunc struct
+*/
+void luaI_initTFunc (TFunc *f)
+{
+ f->next = NULL;
+ f->marked = 0;
+ f->size = 0;
+ f->code = NULL;
+ f->lineDefined = 0;
+ f->fileName = NULL;
+ f->locvars = NULL;
+}
/*
** Insert function in list for GC
@@ -24,9 +42,10 @@ void luaI_insertfunction (TFunc *f)
/*
** Free function
*/
-static void freefunc (TFunc *f)
+void luaI_freefunc (TFunc *f)
{
luaI_free (f->code);
+ luaI_free (f->locvars);
luaI_free (f);
}
@@ -48,7 +67,7 @@ Long luaI_funccollector (void)
function_root = next;
else
prev->next = next;
- freefunc (curr);
+ luaI_freefunc (curr);
++counter;
}
else
@@ -77,3 +96,61 @@ void lua_funcinfo (lua_Object func, char **filename, int *linedefined)
}
}
+/*
+** Stores information to know that variable has been declared in given line
+*/
+void luaI_registerlocalvar (TaggedString *varname, int line)
+{
+ if (numcurrvars >= maxcurrvars)
+ maxcurrvars = growvector(&currvars, maxcurrvars, LocVar, "", MAX_WORD);
+ currvars[numcurrvars].varname = varname;
+ currvars[numcurrvars].line = line;
+ numcurrvars++;
+}
+
+/*
+** Stores information to know that variable has been out of scope in given line
+*/
+void luaI_unregisterlocalvar (int line)
+{
+ luaI_registerlocalvar(NULL, line);
+}
+
+/*
+** Copies "currvars" into a new area and store it in function header.
+** The values (varname = NULL, line = -1) signal the end of vector.
+*/
+void luaI_closelocalvars (TFunc *func)
+{
+ func->locvars = newvector (numcurrvars+1, LocVar);
+ memcpy (func->locvars, currvars, numcurrvars*sizeof(LocVar));
+ func->locvars[numcurrvars].varname = NULL;
+ func->locvars[numcurrvars].line = -1;
+ numcurrvars = 0; /* prepares for next function */
+}
+
+/*
+** Look for n-esim local variable at line "line" in function "func".
+** Returns NULL if not found.
+*/
+char *luaI_getlocalname (TFunc *func, int local_number, int line)
+{
+ int count = 0;
+ char *varname = NULL;
+ LocVar *lv = func->locvars;
+ if (lv == NULL)
+ return NULL;
+ for (; lv->line != -1 && lv->line < line; lv++)
+ {
+ if (lv->varname) /* register */
+ {
+ if (++count == local_number)
+ varname = lv->varname->str;
+ }
+ else /* unregister */
+ if (--count < local_number)
+ varname = NULL;
+ }
+ return varname;
+}
+