diff options
Diffstat (limited to 'src/lua.stx')
-rw-r--r-- | src/lua.stx | 320 |
1 files changed, 47 insertions, 273 deletions
diff --git a/src/lua.stx b/src/lua.stx index 99e96179..54c5a38c 100644 --- a/src/lua.stx +++ b/src/lua.stx @@ -1,12 +1,13 @@ %{ -char *rcs_luastx = "$Id: lua.stx,v 3.25 1995/10/26 17:02:50 roberto Exp $"; +char *rcs_luastx = "$Id: lua.stx,v 3.36 1996/03/21 16:31:32 roberto Exp $"; #include <stdio.h> #include <stdlib.h> -#include <string.h> +#include "luadebug.h" #include "mem.h" +#include "lex.h" #include "opcode.h" #include "hash.h" #include "inout.h" @@ -30,7 +31,7 @@ int yyparse (void); #endif static int maxcode; static int maxmain; -static Long maxcurr; /* to allow maxcurr *= 2 without overflow */ +static int maxcurr; static Byte *funcCode = NULL; static Byte **initcode; static Byte *basepc; @@ -44,35 +45,32 @@ static Long varbuffer[MAXVAR]; /* variables in an assignment list; static int nvarbuffer=0; /* number of variables at a list */ #define MAXLOCALS 32 -static Word localvar[MAXLOCALS]; /* store local variable names */ +static TaggedString *localvar[MAXLOCALS]; /* store local variable names */ static int nlocalvar=0; /* number of local variables */ #define MAXFIELDS FIELDS_PER_FLUSH*2 static Word fields[MAXFIELDS]; /* fieldnames to be flushed */ static int nfields=0; +int lua_debug = 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); + char msg[256]; + char *token = lua_lasttext(); + if (token[0] == 0) + token = "<eof>"; + sprintf (msg,"%s; last token read: \"%s\" at line %d in file `%s'", + s, token, 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 */ - { - if (maxcurr >= MAX_INT) - lua_error("code size overflow"); - maxcurr *= 2; - if (maxcurr >= MAX_INT) - maxcurr = MAX_INT; - basepc = growvector(basepc, maxcurr, Byte); - } + maxcurr = growvector(&basepc, maxcurr, Byte, codeEM, MAX_INT); basepc[pc++] = c; } @@ -104,10 +102,12 @@ static void code_code (TFunc *tf) code_byte(code.m.c4); } -static void code_word_at (Byte *p, Word n) +static void code_word_at (Byte *p, int n) { CodeWord code; - code.w = n; + if ((Word)n != n) + yyerror("block too big"); + code.w = (Word)n; *p++ = code.m.c1; *p++ = code.m.c2; } @@ -117,7 +117,7 @@ static void push_field (Word name) if (nfields < MAXFIELDS) fields[nfields++] = name; else - lua_error ("too many fields in nested constructors"); + yyerror ("too many fields in nested constructors"); } static void flush_record (int n) @@ -142,24 +142,24 @@ static void flush_list (int m, int n) code_byte(m); } else - lua_error ("list constructor too long"); + yyerror ("list constructor too long"); code_byte(n); } -static void add_localvar (Word name) +static void store_localvar (TaggedString *name, int n) { - if (nlocalvar < MAXLOCALS) - localvar[nlocalvar++] = name; + if (nlocalvar+n < MAXLOCALS) + localvar[nlocalvar+n] = name; else - lua_error ("too many local variables"); + yyerror ("too many local variables"); + if (lua_debug) + luaI_registerlocalvar(name, lua_linenumber); } -static void store_localvar (Word name, int n) +static void add_localvar (TaggedString *name) { - if (nlocalvar+n < MAXLOCALS) - localvar[nlocalvar+n] = name; - else - lua_error ("too many local variables"); + store_localvar(name, 0); + nlocalvar++; } static void add_varbuffer (Long var) @@ -167,7 +167,7 @@ static void add_varbuffer (Long var) if (nvarbuffer < MAXVAR) varbuffer[nvarbuffer++] = var; else - lua_error ("variable buffer overflow"); + yyerror ("variable buffer overflow"); } static void code_number (float f) @@ -197,7 +197,7 @@ static void code_number (float f) /* ** Search a local name and if find return its index. If do not find return -1 */ -static int lua_localname (Word n) +static int lua_localname (TaggedString *n) { int i; for (i=nlocalvar-1; i >= 0; i--) @@ -388,7 +388,6 @@ static void codeIf (Long thenAdd, Long elseAdd) */ void lua_parse (TFunc *tf) { - lua_debug = 0; initcode = &(tf->code); *initcode = newvector(CODE_BLOCK, Byte); maincode = 0; @@ -416,7 +415,7 @@ void lua_parse (TFunc *tf) Word vWord; Long vLong; TFunc *pFunc; - TreeNode *pNode; + TaggedString *pTStr; } %start functionlist @@ -429,7 +428,7 @@ void lua_parse (TFunc *tf) %token FUNCTION %token <vFloat> NUMBER %token <vWord> STRING -%token <pNode> NAME +%token <pTStr> NAME %token <vInt> DEBUG %type <vLong> PrepJump @@ -481,7 +480,7 @@ funcname : var { $$ =$1; init_func(); } code_word(luaI_findconstant($3)); $$ = 0; /* indexed variable */ init_func(); - add_localvar(luaI_findsymbolbyname("self")); + add_localvar(luaI_createfixedstring("self")); } ; @@ -489,11 +488,14 @@ body : '(' parlist ')' block END { codereturn(); $$ = new(TFunc); + luaI_initTFunc($$); $$->size = pc; $$->code = newvector(pc, Byte); $$->fileName = lua_parsedfile; $$->lineDefined = $2; memcpy($$->code, basepc, pc*sizeof(Byte)); + if (lua_debug) + luaI_closelocalvars($$); /* save func values */ funcCode = basepc; maxcode=maxcurr; #if LISTING @@ -554,7 +556,11 @@ block : {$<vInt>$ = nlocalvar;} statlist ret { if (nlocalvar != $<vInt>1) { - nlocalvar = $<vInt>1; + if (lua_debug) + for (; nlocalvar > $<vInt>1; nlocalvar--) + luaI_unregisterlocalvar(lua_linenumber); + else + nlocalvar = $<vInt>1; lua_codeadjust (0); } } @@ -672,14 +678,8 @@ parlist : /* empty */ { lua_codeadjust(0); $$ = lua_linenumber; } | parlist1 { lua_codeadjust(0); $$ = lua_linenumber; } ; -parlist1 : NAME - { - add_localvar(luaI_findsymbol($1)); - } - | parlist1 ',' NAME - { - add_localvar(luaI_findsymbol($3)); - } +parlist1 : NAME { add_localvar($1); } + | parlist1 ',' NAME { add_localvar($3); } ; fieldlist : lfieldlist @@ -759,10 +759,9 @@ var : singlevar { $$ = $1; } singlevar : NAME { - Word s = luaI_findsymbol($1); - int local = lua_localname (s); + int local = lua_localname($1); if (local == -1) /* global var */ - $$ = s + 1; /* return positive value */ + $$ = luaI_findsymbol($1)+1; /* return positive value */ else $$ = -(local+1); /* return negative value */ } @@ -771,10 +770,10 @@ singlevar : NAME varexp : var { lua_pushvar($1); } ; -localdeclist : NAME {store_localvar(luaI_findsymbol($1), 0); $$ = 1;} +localdeclist : NAME {store_localvar($1, 0); $$ = 1;} | localdeclist ',' NAME { - store_localvar(luaI_findsymbol($3), $1); + store_localvar($3, $1); $$ = $1+1; } ; @@ -787,228 +786,3 @@ setdebug : DEBUG { lua_debug = $1; } ; %% - -#if LISTING - -static void PrintCode (Byte *code, Byte *end) -{ - Byte *p = code; - printf ("\n\nCODE\n"); - while (p != end) - { - switch ((OpCode)*p) - { - case PUSHNIL: printf ("%d PUSHNIL\n", (p++)-code); break; - case PUSH0: case PUSH1: case PUSH2: - printf ("%d PUSH%c\n", p-code, *p-PUSH0+'0'); - p++; - break; - case PUSHBYTE: - printf ("%d PUSHBYTE %d\n", p-code, *(++p)); - p++; - break; - case PUSHWORD: - { - CodeWord c; - int n = p-code; - p++; - get_word(c,p); - printf ("%d PUSHWORD %d\n", n, c.w); - } - break; - case PUSHFLOAT: - { - CodeFloat c; - int n = p-code; - p++; - get_float(c,p); - printf ("%d PUSHFLOAT %f\n", n, c.f); - } - break; - case PUSHSTRING: - { - CodeWord c; - int n = p-code; - p++; - get_word(c,p); - printf ("%d PUSHSTRING %d\n", n, c.w); - } - break; - case PUSHFUNCTION: - { - CodeCode c; - int n = p-code; - p++; - get_code(c,p); - printf ("%d PUSHFUNCTION %p\n", n, c.tf); - } - break; - - case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2: case PUSHLOCAL3: - case PUSHLOCAL4: case PUSHLOCAL5: case PUSHLOCAL6: case PUSHLOCAL7: - case PUSHLOCAL8: case PUSHLOCAL9: - printf ("%d PUSHLOCAL%c\n", p-code, *p-PUSHLOCAL0+'0'); - p++; - break; - case PUSHLOCAL: printf ("%d PUSHLOCAL %d\n", p-code, *(++p)); - p++; - break; - case PUSHGLOBAL: - { - CodeWord c; - int n = p-code; - p++; - get_word(c,p); - printf ("%d PUSHGLOBAL %d\n", n, c.w); - } - break; - case PUSHINDEXED: printf ("%d PUSHINDEXED\n", (p++)-code); break; - case STORELOCAL0: case STORELOCAL1: case STORELOCAL2: case STORELOCAL3: - case STORELOCAL4: case STORELOCAL5: case STORELOCAL6: case STORELOCAL7: - case STORELOCAL8: case STORELOCAL9: - printf ("%d STORELOCAL%c\n", p-code, *p-STORELOCAL0+'0'); - p++; - break; - case STORELOCAL: - printf ("%d STORELOCAL %d\n", p-code, *(++p)); - p++; - break; - case STOREGLOBAL: - { - CodeWord c; - int n = p-code; - p++; - get_word(c,p); - printf ("%d STOREGLOBAL %d\n", n, c.w); - } - break; - case PUSHSELF: - { - CodeWord c; - int n = p-code; - p++; - get_word(c,p); - printf ("%d PUSHSELF %d\n", n, c.w); - } - break; - case STOREINDEXED0: printf ("%d STOREINDEXED0\n", (p++)-code); break; - case STOREINDEXED: printf ("%d STOREINDEXED %d\n", p-code, *(++p)); - p++; - break; - case STORELIST0: - printf("%d STORELIST0 %d\n", p-code, *(++p)); - p++; - break; - case STORELIST: - printf("%d STORELIST %d %d\n", p-code, *(p+1), *(p+2)); - p+=3; - break; - case STORERECORD: - printf("%d STORERECORD %d\n", p-code, *(++p)); - p += *p*sizeof(Word) + 1; - break; - case ADJUST0: printf ("%d ADJUST0\n", (p++)-code); break; - case ADJUST: - printf ("%d ADJUST %d\n", p-code, *(++p)); - p++; - break; - case CREATEARRAY: - { - CodeWord c; - int n = p-code; - p++; - get_word(c,p); - printf ("%d CREATEARRAY %d\n", n, c.w); - break; - } - case EQOP: printf ("%d EQOP\n", (p++)-code); break; - case LTOP: printf ("%d LTOP\n", (p++)-code); break; - case LEOP: printf ("%d LEOP\n", (p++)-code); break; - case ADDOP: printf ("%d ADDOP\n", (p++)-code); break; - case SUBOP: printf ("%d SUBOP\n", (p++)-code); break; - case MULTOP: printf ("%d MULTOP\n", (p++)-code); break; - case DIVOP: printf ("%d DIVOP\n", (p++)-code); break; - case POWOP: printf ("%d POWOP\n", (p++)-code); break; - case CONCOP: printf ("%d CONCOP\n", (p++)-code); break; - case MINUSOP: printf ("%d MINUSOP\n", (p++)-code); break; - case NOTOP: printf ("%d NOTOP\n", (p++)-code); break; - case ONTJMP: - { - CodeWord c; - int n = p-code; - p++; - get_word(c,p); - printf ("%d ONTJMP %d\n", n, c.w); - } - break; - case ONFJMP: - { - CodeWord c; - int n = p-code; - p++; - get_word(c,p); - printf ("%d ONFJMP %d\n", n, c.w); - } - break; - case JMP: - { - CodeWord c; - int n = p-code; - p++; - get_word(c,p); - printf ("%d JMP %d\n", n, c.w); - } - break; - case UPJMP: - { - CodeWord c; - int n = p-code; - p++; - get_word(c,p); - printf ("%d UPJMP %d\n", n, c.w); - } - break; - case IFFJMP: - { - CodeWord c; - int n = p-code; - p++; - get_word(c,p); - printf ("%d IFFJMP %d\n", n, c.w); - } - break; - case IFFUPJMP: - { - CodeWord c; - int n = p-code; - p++; - get_word(c,p); - printf ("%d IFFUPJMP %d\n", n, c.w); - } - break; - case POP: printf ("%d POP\n", (p++)-code); break; - case CALLFUNC: - printf ("%d CALLFUNC %d %d\n", p-code, *(p+1), *(p+2)); - p+=3; - break; - case RETCODE0: printf ("%d RETCODE0\n", (p++)-code); break; - case RETCODE: - printf ("%d RETCODE %d\n", p-code, *(++p)); - p++; - break; - case SETLINE: - { - CodeWord c; - int n = p-code; - p++; - get_word(c,p); - printf ("%d SETLINE %d\n", n, c.w); - } - break; - - default: printf ("%d Cannot happen: code %d\n", (p++)-code, *(p-1)); break; - } - } -} -#endif - |