|
@@ -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 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 */
|
|
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 <vLong> var, singlevar
|
|
|
|
|
|
+%type <vInt> parlist
|
|
|
|
+%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);
|
|
|
|
- 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
|
|
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); }
|
|
|
|
- | parlist1 { lua_codeadjust(0); }
|
|
|
|
|
|
+parlist : /* empty */ { lua_codeadjust(0); $$ = lua_linenumber; }
|
|
|
|
+ | parlist1 { lua_codeadjust(0); $$ = lua_linenumber; }
|
|
;
|
|
;
|
|
|
|
|
|
parlist1 : NAME
|
|
parlist1 : NAME
|