From 15d48576ea737d51e579f101a870e37f62b81f22 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 26 Oct 1995 12:21:56 -0200 Subject: 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. --- lua.stx | 124 +++++++++++++++++++++++++++++++--------------------------------- 1 file changed, 60 insertions(+), 64 deletions(-) (limited to 'lua.stx') diff --git a/lua.stx b/lua.stx index 1c9df40b..a2ee9d23 100644 --- a/lua.stx +++ b/lua.stx @@ -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 #include @@ -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 lua_codestore (int i) +static void storesinglevar (Long v) { - if (varbuffer[i] > 0) /* global var */ + if (v > 0) /* global var */ { - code_byte(STOREGLOBAL); - code_word(varbuffer[i]-1); + code_byte(STOREGLOBAL); + code_word(v-1); } - else if (varbuffer[i] < 0) /* local var */ + else if (v < 0) /* local var */ { - int number = (-varbuffer[i]) - 1; - if (number < 10) code_byte(STORELOCAL0 + number); - else - { - code_byte(STORELOCAL); - code_byte(number); - } + 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 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 fieldlist, localdeclist, decinit %type ffieldlist, ffieldlist1, semicolonpart %type lfieldlist, lfieldlist1 -%type functiontoken -%type var, singlevar +%type parlist +%type var, singlevar, funcname %type 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 -- cgit v1.2.3-55-g6feb