functions now may be declared with any "var" as a name;

therefore they do not have a "baptism" name.
Changes in debug API to acomodate that.
This commit is contained in:
Roberto Ierusalimschy 1995-10-26 12:21:56 -02:00
parent 39b071f7b1
commit 15d48576ea
8 changed files with 140 additions and 127 deletions

20
func.c
View File

@ -1,7 +1,10 @@
#include <stdio.h> #include <stdio.h>
#include "luadebug.h"
#include "table.h" #include "table.h"
#include "mem.h" #include "mem.h"
#include "func.h" #include "func.h"
#include "opcode.h"
static TFunc *function_root = NULL; static TFunc *function_root = NULL;
@ -57,3 +60,20 @@ Long luaI_funccollector (void)
} }
return counter; return counter;
} }
void lua_funcinfo (lua_Object func, char **filename, int *linedefined)
{
Object *f = luaI_Address(func);
if (f->tag == LUA_T_MARK || f->tag == LUA_T_FUNCTION)
{
*filename = f->value.tf->fileName;
*linedefined = f->value.tf->lineDefined;
}
else if (f->tag == LUA_T_CMARK || f->tag == LUA_T_CFUNCTION)
{
*filename = "(C)";
*linedefined = -1;
}
}

4
func.h
View File

@ -2,6 +2,7 @@
#define func_h #define func_h
#include "types.h" #include "types.h"
#include "lua.h"
/* /*
** Header para funcoes. ** Header para funcoes.
@ -13,12 +14,11 @@ typedef struct TFunc
int size; int size;
Byte *code; Byte *code;
int lineDefined; int lineDefined;
char *name1; /* function or method name (or null if main) */
char *name2; /* object name (or null if not method) */
char *fileName; char *fileName;
} TFunc; } TFunc;
Long luaI_funccollector (void); Long luaI_funccollector (void);
void luaI_insertfunction (TFunc *f); void luaI_insertfunction (TFunc *f);
#endif #endif

39
iolib.c
View File

@ -3,7 +3,7 @@
** Input/output library to LUA ** Input/output library to LUA
*/ */
char *rcs_iolib="$Id: iolib.c,v 1.24 1995/10/17 14:12:45 roberto Exp roberto $"; char *rcs_iolib="$Id: iolib.c,v 1.25 1995/10/23 13:53:48 roberto Exp roberto $";
#include <stdio.h> #include <stdio.h>
#include <ctype.h> #include <ctype.h>
@ -612,20 +612,33 @@ static void print_message (void)
fprintf(stderr, "Active Stack:\n"); fprintf(stderr, "Active Stack:\n");
while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT) while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT)
{ {
char *filename; char *funcname; char *name;
char *objname; int linedefined; int currentline;
lua_funcinfo(func, &filename, &funcname, &objname, &linedefined); fprintf(stderr, "\t");
if (objname == NULL) switch (*getobjname(func, &name))
if (funcname) {
fprintf(stderr, "\t%s", funcname); case 'g':
else fprintf(stderr, "function %s", name);
break;
case 'f':
fprintf(stderr, "fallback %s", name);
break;
default:
{ {
fprintf(stderr, "\tmain of %s\n", filename); char *filename;
continue; int linedefined;
lua_funcinfo(func, &filename, &linedefined);
if (linedefined == 0)
fprintf(stderr, "main of %s", filename);
else if (linedefined < 0)
fprintf(stderr, "%s", filename);
else
fprintf(stderr, "function (%s:%d)", filename, linedefined);
} }
else }
fprintf(stderr, "\t%s:%s", objname, funcname); if ((currentline = lua_currentline(func)) > 0)
fprintf(stderr, "\t(defined in %s)\n", filename); fprintf(stderr, " at line %d", currentline);
fprintf(stderr, "\n");
} }
} }

130
lua.stx
View File

@ -1,6 +1,6 @@
%{ %{
char *rcs_luastx = "$Id: lua.stx,v 3.22 1995/10/25 13:05:51 roberto Exp roberto $"; char *rcs_luastx = "$Id: lua.stx,v 3.23 1995/10/25 14:33:25 roberto Exp roberto $";
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -54,6 +54,14 @@ static int nfields=0;
/* Internal functions */ /* Internal functions */
static void yyerror (char *s)
{
static char msg[256];
sprintf (msg,"%s near \"%s\" at line %d in file `%s'",
s, lua_lasttext (), lua_linenumber, lua_parsedfile);
lua_error (msg);
}
static void code_byte (Byte c) static void code_byte (Byte c)
{ {
if (pc>maxcurr-2) /* 1 byte free to code HALT of main code */ if (pc>maxcurr-2) /* 1 byte free to code HALT of main code */
@ -148,6 +156,8 @@ static void add_localvar (Word name)
static void store_localvar (Word name, int n) static void store_localvar (Word name, int n)
{ {
if (*initcode == basepc)
yyerror("local variable outside function body");
if (nlocalvar+n < MAXLOCALS) if (nlocalvar+n < MAXLOCALS)
localvar[nlocalvar+n] = name; localvar[nlocalvar+n] = name;
else else
@ -249,6 +259,20 @@ static void savemain (void)
maincode=pc; *initcode=basepc; maxmain=maxcurr; maincode=pc; *initcode=basepc; maxmain=maxcurr;
} }
static void init_func (void)
{
if (funcCode == NULL) /* first function */
{
funcCode = newvector(CODE_BLOCK, Byte);
maxcode = CODE_BLOCK;
}
savemain(); /* save main values */
/* set func values */
pc=0; basepc=funcCode; maxcurr=maxcode;
nlocalvar = 0;
luaI_codedebugline(lua_linenumber);
}
static void codereturn (void) static void codereturn (void)
{ {
if (nlocalvar == 0) if (nlocalvar == 0)
@ -300,23 +324,31 @@ static void adjust_mult_assign (int vars, Long exps, int temps)
lua_codeadjust(temps); lua_codeadjust(temps);
} }
static void storesinglevar (Long v)
{
if (v > 0) /* global var */
{
code_byte(STOREGLOBAL);
code_word(v-1);
}
else if (v < 0) /* local var */
{
int number = (-v) - 1;
if (number < 10) code_byte(STORELOCAL0 + number);
else
{
code_byte(STORELOCAL);
code_byte(number);
}
}
else
code_byte(STOREINDEXED0);
}
static void lua_codestore (int i) static void lua_codestore (int i)
{ {
if (varbuffer[i] > 0) /* global var */ if (varbuffer[i] != 0) /* global or local var */
{ storesinglevar(varbuffer[i]);
code_byte(STOREGLOBAL);
code_word(varbuffer[i]-1);
}
else if (varbuffer[i] < 0) /* local var */
{
int number = (-varbuffer[i]) - 1;
if (number < 10) code_byte(STORELOCAL0 + number);
else
{
code_byte(STORELOCAL);
code_byte(number);
}
}
else /* indexed var */ else /* indexed var */
{ {
int j; int j;
@ -352,14 +384,6 @@ static void codeIf (Long thenAdd, Long elseAdd)
code_word_at(basepc+thenAdd+1,elseinit-(thenAdd+sizeof(Word)+1)); code_word_at(basepc+thenAdd+1,elseinit-(thenAdd+sizeof(Word)+1));
} }
static void yyerror (char *s)
{
static char msg[256];
sprintf (msg,"%s near \"%s\" at line %d in file `%s'",
s, lua_lasttext (), lua_linenumber, lua_parsedfile);
lua_error (msg);
}
/* /*
** Parse LUA code. ** Parse LUA code.
@ -419,8 +443,8 @@ void lua_parse (TFunc *tf)
%type <vInt> fieldlist, localdeclist, decinit %type <vInt> fieldlist, localdeclist, decinit
%type <vInt> ffieldlist, ffieldlist1, semicolonpart %type <vInt> ffieldlist, ffieldlist1, semicolonpart
%type <vInt> lfieldlist, lfieldlist1 %type <vInt> lfieldlist, lfieldlist1
%type <vInt> functiontoken %type <vInt> parlist
%type <vLong> var, singlevar %type <vLong> var, singlevar, funcname
%type <pFunc> body %type <pFunc> body
%left AND OR %left AND OR
@ -438,59 +462,29 @@ void lua_parse (TFunc *tf)
functionlist : /* empty */ functionlist : /* empty */
| functionlist globalstat | functionlist globalstat
| functionlist function | functionlist function
| functionlist method
; ;
globalstat : stat sc globalstat : stat sc
| setdebug | setdebug
; ;
function : functiontoken NAME body function : FUNCTION funcname body
{ {
code_byte(PUSHFUNCTION); code_byte(PUSHFUNCTION);
code_code($3); code_code($3);
code_byte(STOREGLOBAL); storesinglevar($2);
code_word(luaI_findsymbol($2));
$3->lineDefined = $1;
$3->name1 = $2->ts.str;
$3->name2 = NULL;
$3->fileName = lua_parsedfile;
} }
; ;
method : functiontoken NAME methkind NAME body funcname : var { $$ =$1; init_func(); }
{ | varexp ':' NAME
/* assign function to table field */
lua_pushvar(luaI_findsymbol($2)+1);
code_byte(PUSHSTRING);
code_word(luaI_findconstant($4));
code_byte(PUSHFUNCTION);
code_code($5);
code_byte(STOREINDEXED0);
$5->lineDefined = $1;
$5->name1 = $4->ts.str;
$5->name2 = $2->ts.str;
$5->fileName = lua_parsedfile;
}
;
functiontoken : FUNCTION
{ {
if (funcCode == NULL) /* first function */ code_byte(PUSHSTRING);
{ code_word(luaI_findconstant($3));
funcCode = newvector(CODE_BLOCK, Byte); $$ = 0; /* indexed variable */
maxcode = CODE_BLOCK; init_func();
} add_localvar(luaI_findsymbolbyname("self"));
savemain(); /* save main values */
/* set func values */
pc=0; basepc=funcCode; maxcurr=maxcode;
nlocalvar=0;
$$ = lua_linenumber;
} }
;
methkind : ':' { add_localvar(luaI_findsymbolbyname("self")); }
| '.' /* no self */
; ;
body : '(' parlist ')' block END body : '(' parlist ')' block END
@ -499,6 +493,8 @@ body : '(' parlist ')' block END
$$ = new(TFunc); $$ = new(TFunc);
$$->size = pc; $$->size = pc;
$$->code = newvector(pc, Byte); $$->code = newvector(pc, Byte);
$$->fileName = lua_parsedfile;
$$->lineDefined = $2;
memcpy($$->code, basepc, pc*sizeof(Byte)); memcpy($$->code, basepc, pc*sizeof(Byte));
/* save func values */ /* save func values */
funcCode = basepc; maxcode=maxcurr; funcCode = basepc; maxcode=maxcurr;
@ -674,8 +670,8 @@ exprlist1 : expr { if ($1 != 0) $$ = $1; else $$ = -1; }
} }
; ;
parlist : /* empty */ { lua_codeadjust(0); } parlist : /* empty */ { lua_codeadjust(0); $$ = lua_linenumber; }
| parlist1 { lua_codeadjust(0); } | parlist1 { lua_codeadjust(0); $$ = lua_linenumber; }
; ;
parlist1 : NAME parlist1 : NAME

View File

@ -2,7 +2,7 @@
** LUA - Linguagem para Usuarios de Aplicacao ** LUA - Linguagem para Usuarios de Aplicacao
** Grupo de Tecnologia em Computacao Grafica ** Grupo de Tecnologia em Computacao Grafica
** TeCGraf - PUC-Rio ** TeCGraf - PUC-Rio
** $Id: $ ** $Id: luadebug.h,v 1.1 1995/10/17 14:12:45 roberto Exp roberto $
*/ */
@ -12,8 +12,9 @@
#include "lua.h" #include "lua.h"
lua_Object lua_stackedfunction(int level); lua_Object lua_stackedfunction(int level);
void lua_funcinfo (lua_Object func, char **filename, char **funcname, void lua_funcinfo (lua_Object func, char **filename, int *linedefined);
char **objname, int *linedefined); int lua_currentline (lua_Object func);
char *getobjname (lua_Object o, char **name);
#endif #endif

View File

@ -3,7 +3,7 @@
** TecCGraf - PUC-Rio ** TecCGraf - PUC-Rio
*/ */
char *rcs_opcode="$Id: opcode.c,v 3.46 1995/10/17 14:30:05 roberto Exp roberto $"; char *rcs_opcode="$Id: opcode.c,v 3.47 1995/10/25 13:05:51 roberto Exp roberto $";
#include <setjmp.h> #include <setjmp.h>
#include <stdlib.h> #include <stdlib.h>
@ -355,17 +355,13 @@ lua_Object lua_stackedfunction (int level)
} }
void lua_funcinfo (lua_Object func, char **filename, char **funcname, int lua_currentline (lua_Object func)
char **objname, int *line)
{ {
Object *f = Address(func); Object *f = Address(func);
luaI_funcInfo(f, filename, funcname, objname, line); return (f+1 < top && (f+1)->tag == LUA_T_LINE) ? (f+1)->value.i : -1;
*line = (f+1 < top && (f+1)->tag == LUA_T_LINE) ?
(f+1)->value.i : -1;
} }
/* /*
** Execute a protected call. Assumes that function is at CBase and ** Execute a protected call. Assumes that function is at CBase and
** parameters are on top of it. Leave nResults on the stack. ** parameters are on top of it. Leave nResults on the stack.
@ -406,7 +402,6 @@ static int do_protectedmain (void)
stack[CBase].tag = LUA_T_FUNCTION; stack[CBase].tag = LUA_T_FUNCTION;
stack[CBase].value.tf = &tf; stack[CBase].value.tf = &tf;
tf.lineDefined = 0; tf.lineDefined = 0;
tf.name1 = tf.name2 = NULL;
tf.fileName = lua_parsedfile; tf.fileName = lua_parsedfile;
tf.code = NULL; tf.code = NULL;
if (setjmp(myErrorJmp) == 0) if (setjmp(myErrorJmp) == 0)

52
table.c
View File

@ -3,9 +3,9 @@
** Module to control static tables ** Module to control static tables
*/ */
char *rcs_table="$Id: table.c,v 2.35 1995/10/17 11:58:41 roberto Exp roberto $"; char *rcs_table="$Id: table.c,v 2.36 1995/10/23 13:53:48 roberto Exp roberto $";
#include <string.h> /*#include <string.h>*/
#include "mem.h" #include "mem.h"
#include "opcode.h" #include "opcode.h"
@ -15,6 +15,7 @@ char *rcs_table="$Id: table.c,v 2.35 1995/10/17 11:58:41 roberto Exp roberto $";
#include "inout.h" #include "inout.h"
#include "lua.h" #include "lua.h"
#include "fallback.h" #include "fallback.h"
#include "luadebug.h"
#define BUFFER_BLOCK 256 #define BUFFER_BLOCK 256
@ -254,39 +255,28 @@ static void getglobal (void)
} }
static lua_CFunction cfunc = NULL; static Object *functofind;
static int checkfunc (Object *o) static int checkfunc (Object *o)
{ {
return ((o->tag == LUA_T_CMARK || o->tag == LUA_T_CFUNCTION) && if (o->tag == LUA_T_FUNCTION)
o->value.f == cfunc); return
((functofind->tag == LUA_T_FUNCTION || functofind->tag == LUA_T_MARK)
&& (functofind->value.tf == o->value.tf));
if (o->tag == LUA_T_CFUNCTION)
return
((functofind->tag == LUA_T_CFUNCTION || functofind->tag == LUA_T_CMARK)
&& (functofind->value.f == o->value.f));
return 0;
} }
void luaI_funcInfo (struct Object *func, char **filename, char **funcname, char *getobjname (lua_Object o, char **name)
char **objname, int *linedefined) { /* try to find a name for given function */
{ functofind = luaI_Address(o);
if (func->tag == LUA_T_MARK || func->tag == LUA_T_FUNCTION) if ((*name = lua_travsymbol(checkfunc)) != NULL)
{ return "global";
TFunc *f = func->value.tf; else if ((*name = luaI_travfallbacks(checkfunc)) != NULL)
*filename = f->fileName; return "fallback";
*funcname = f->name1; else return "";
*objname = f->name2;
*linedefined = f->lineDefined;
}
else if (func->tag == LUA_T_CMARK || func->tag == LUA_T_CFUNCTION)
{
/* temporario: */
cfunc = func->value.f;
*filename = "(C)";
*linedefined = 0;
*funcname = lua_travsymbol(checkfunc);
if (*funcname)
*objname = 0;
else
{
*funcname = luaI_travfallbacks(checkfunc);
*objname = "(FB)";
}
}
} }

View File

@ -1,7 +1,7 @@
/* /*
** Module to control static tables ** Module to control static tables
** TeCGraf - PUC-Rio ** TeCGraf - PUC-Rio
** $Id: table.h,v 2.11 1995/10/13 15:16:25 roberto Exp roberto $ ** $Id: table.h,v 2.12 1995/10/17 11:58:41 roberto Exp roberto $
*/ */
#ifndef table_h #ifndef table_h
@ -25,7 +25,5 @@ Word luaI_findconstantbyname (char *name);
int lua_markobject (Object *o); int lua_markobject (Object *o);
void lua_pack (void); void lua_pack (void);
void luaI_funcInfo (Object *func, char **filename, char **funcname,
char **objname, int *linedefined);
#endif #endif