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

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 <stdlib.h>
@@ -54,6 +54,14 @@ static int nfields=0;
/* 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)
{
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)
{
if (*initcode == basepc)
yyerror("local variable outside function body");
if (nlocalvar+n < MAXLOCALS)
localvar[nlocalvar+n] = name;
else
@@ -249,6 +259,20 @@ static void savemain (void)
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)
{
if (nlocalvar == 0)
@@ -300,23 +324,31 @@ static void adjust_mult_assign (int vars, Long exps, int 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)
{
if (varbuffer[i] > 0) /* global var */
{
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);
}
}
if (varbuffer[i] != 0) /* global or local var */
storesinglevar(varbuffer[i]);
else /* indexed var */
{
int j;
@@ -352,14 +384,6 @@ static void codeIf (Long thenAdd, Long elseAdd)
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.
@@ -419,8 +443,8 @@ void lua_parse (TFunc *tf)
%type <vInt> fieldlist, localdeclist, decinit
%type <vInt> ffieldlist, ffieldlist1, semicolonpart
%type <vInt> lfieldlist, lfieldlist1
%type <vInt> functiontoken
%type <vLong> var, singlevar
%type <vInt> parlist
%type <vLong> var, singlevar, funcname
%type <pFunc> body
%left AND OR
@@ -438,59 +462,29 @@ void lua_parse (TFunc *tf)
functionlist : /* empty */
| functionlist globalstat
| functionlist function
| functionlist method
;
globalstat : stat sc
| setdebug
;
function : functiontoken NAME body
function : FUNCTION funcname body
{
code_byte(PUSHFUNCTION);
code_code($3);
code_byte(STOREGLOBAL);
code_word(luaI_findsymbol($2));
$3->lineDefined = $1;
$3->name1 = $2->ts.str;
$3->name2 = NULL;
$3->fileName = lua_parsedfile;
storesinglevar($2);
}
;
method : functiontoken NAME methkind NAME body
{
/* 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
funcname : var { $$ =$1; init_func(); }
| varexp ':' NAME
{
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;
$$ = lua_linenumber;
code_byte(PUSHSTRING);
code_word(luaI_findconstant($3));
$$ = 0; /* indexed variable */
init_func();
add_localvar(luaI_findsymbolbyname("self"));
}
;
methkind : ':' { add_localvar(luaI_findsymbolbyname("self")); }
| '.' /* no self */
;
body : '(' parlist ')' block END
@@ -499,6 +493,8 @@ body : '(' parlist ')' block END
$$ = new(TFunc);
$$->size = pc;
$$->code = newvector(pc, Byte);
$$->fileName = lua_parsedfile;
$$->lineDefined = $2;
memcpy($$->code, basepc, pc*sizeof(Byte));
/* save func values */
funcCode = basepc; maxcode=maxcurr;
@@ -674,8 +670,8 @@ exprlist1 : expr { if ($1 != 0) $$ = $1; else $$ = -1; }
}
;
parlist : /* empty */ { lua_codeadjust(0); }
| parlist1 { lua_codeadjust(0); }
parlist : /* empty */ { lua_codeadjust(0); $$ = lua_linenumber; }
| parlist1 { lua_codeadjust(0); $$ = lua_linenumber; }
;
parlist1 : NAME