diff --git a/inout.c b/inout.c index 9a54f4fc..a7dc7413 100644 --- a/inout.c +++ b/inout.c @@ -5,7 +5,7 @@ ** Also provides some predefined lua functions. */ -char *rcs_inout="$Id: inout.c,v 2.9 1994/11/08 20:06:15 roberto Exp roberto $"; +char *rcs_inout="$Id: inout.c,v 2.10 1994/11/09 18:09:22 roberto Exp roberto $"; #include #include @@ -150,12 +150,12 @@ void lua_reportbug (char *s) { sprintf (strchr(msg,0), "\n\tin statement begining at line %d in function \"%s\" of file \"%s\"", - lua_debugline, lua_varname(funcstack[nfuncstack-1].function), + lua_debugline, lua_constant[funcstack[nfuncstack-1].function], funcstack[nfuncstack-1].file); sprintf (strchr(msg,0), "\n\tactive stack\n"); for (i=nfuncstack-1; i>=0; i--) sprintf (strchr(msg,0), "\t-> function \"%s\" of file \"%s\"\n", - lua_varname(funcstack[i].function), + lua_constant[funcstack[i].function], funcstack[i].file); } else diff --git a/lex.c b/lex.c index 7aad1d0d..d0075a53 100644 --- a/lex.c +++ b/lex.c @@ -1,4 +1,4 @@ -char *rcs_lex = "$Id: lex.c,v 2.9 1994/11/03 17:09:20 roberto Exp roberto $"; +char *rcs_lex = "$Id: lex.c,v 2.10 1994/11/13 14:39:04 roberto Exp roberto $"; #include @@ -7,6 +7,8 @@ char *rcs_lex = "$Id: lex.c,v 2.9 1994/11/03 17:09:20 roberto Exp roberto $"; #include #include +#include "tree.h" +#include "table.h" #include "opcode.h" #include "inout.h" #include "y.tab.h" @@ -19,9 +21,8 @@ char *rcs_lex = "$Id: lex.c,v 2.9 1994/11/03 17:09:20 roberto Exp roberto $"; #define save_and_next() { save(current); next(); } static int current; -static char yytext[2][256]; +static char yytext[256]; static char *yytextLast; -static int currentText = 0; static Input input; @@ -34,7 +35,7 @@ void lua_setinput (Input fn) char *lua_lasttext (void) { *yytextLast = 0; - return yytext[currentText]; + return yytext; } @@ -87,10 +88,9 @@ static int findReserved (char *name) int yylex (void) { float a; - currentText = !currentText; while (1) { - yytextLast = yytext[currentText]; + yytextLast = yytext; #if 0 fprintf(stderr,"'%c' %d\n",current,current); #endif @@ -110,12 +110,12 @@ int yylex (void) while (isalnum(current) || current == '_') save_and_next(); *yytextLast = 0; - if (lua_strcmp(yytext[currentText], "debug") == 0) + if (lua_strcmp(yytext, "debug") == 0) { yylval.vInt = 1; return DEBUG; } - else if (lua_strcmp(yytext[currentText], "nodebug") == 0) + else if (lua_strcmp(yytext, "nodebug") == 0) { yylval.vInt = 0; return DEBUG; @@ -179,7 +179,7 @@ int yylex (void) } next(); /* skip the delimiter */ *yytextLast = 0; - yylval.pChar = yytext[currentText]; + yylval.vWord = luaI_findconstant(lua_constcreate(yytext)); return STRING; } @@ -200,9 +200,9 @@ int yylex (void) int res; do { save_and_next(); } while (isalnum(current) || current == '_'); *yytextLast = 0; - res = findReserved(yytext[currentText]); + res = findReserved(yytext); if (res) return res; - yylval.pChar = yytext[currentText]; + yylval.pNode = lua_constcreate(yytext); return NAME; } @@ -266,7 +266,7 @@ fraction: default: /* also end of file */ { save_and_next(); - return yytext[currentText][0]; + return yytext[0]; } } } diff --git a/lua.stx b/lua.stx index 177e60ed..1b365942 100644 --- a/lua.stx +++ b/lua.stx @@ -1,6 +1,6 @@ %{ -char *rcs_luastx = "$Id: lua.stx,v 3.4 1994/11/09 18:07:38 roberto Exp roberto $"; +char *rcs_luastx = "$Id: lua.stx,v 3.5 1994/11/13 14:54:18 roberto Exp roberto $"; #include #include @@ -9,6 +9,7 @@ char *rcs_luastx = "$Id: lua.stx,v 3.4 1994/11/09 18:07:38 roberto Exp roberto $ #include "opcode.h" #include "hash.h" #include "inout.h" +#include "tree.h" #include "table.h" #include "lua.h" @@ -40,6 +41,7 @@ static int nlocalvar=0; /* number of local variables */ static Word fields[MAXFIELDS]; /* fieldnames to be flushed */ static int nfields=0; + /* Internal functions */ static void code_byte (Byte c) @@ -164,17 +166,6 @@ static void code_number (float f) } } -static void init_function (void) -{ - if (funcCode == NULL) /* first function */ - { - funcCode = (Byte *) calloc(CODE_BLOCK, sizeof(Byte)); - if (funcCode == NULL) - lua_error("not enough memory"); - maxcode = CODE_BLOCK; - } -} - %} @@ -187,6 +178,7 @@ static void init_function (void) Word vWord; Long vLong; Byte *pByte; + TreeNode *pNode; } %start functionlist @@ -198,8 +190,8 @@ static void init_function (void) %token LOCAL %token FUNCTION %token NUMBER -%token STRING -%token NAME +%token STRING +%token NAME %token DEBUG %type PrepJump @@ -208,7 +200,7 @@ static void init_function (void) %type ffieldlist1 %type lfieldlist1 %type var, singlevar - +%type body %left AND OR %left EQ NE '>' '<' LE GE @@ -239,84 +231,54 @@ functionlist : /* empty */ function : FUNCTION NAME { - init_function(); - pc=0; basepc=funcCode; maxcurr=maxcode; - nlocalvar=0; - $$ = lua_findsymbol($2); - } - '(' parlist ')' - { - if (lua_debug) - { - code_byte(SETFUNCTION); - code_code((Byte *)strdup(lua_file[lua_nfile-1])); - code_word($3); - } - lua_codeadjust (0); + init_function($2); } - block - END + body { - codereturn(); - s_tag($3) = LUA_T_FUNCTION; - s_bvalue($3) = calloc (pc, sizeof(Byte)); - if (s_bvalue($3) == NULL) - lua_error("not enough memory"); - memcpy (s_bvalue($3), basepc, pc*sizeof(Byte)); - funcCode = basepc; maxcode=maxcurr; + Word func = luaI_findsymbol($2); + s_tag(func) = LUA_T_FUNCTION; + s_bvalue(func) = $4; #if LISTING PrintCode(funcCode,funcCode+pc); #endif } ; -method : FUNCTION NAME { $$ = lua_findsymbol($2); } ':' NAME +method : FUNCTION NAME ':' NAME { - init_function(); - pc=0; basepc=funcCode; maxcurr=maxcode; - nlocalvar=0; - localvar[nlocalvar]=lua_findsymbol("self"); /* self param. */ + init_function($4); + localvar[nlocalvar]=luaI_findsymbolbyname("self"); add_nlocalvar(1); - $$ = lua_findconstant($5); - } - '(' parlist ')' - { - if (lua_debug) - { - code_byte(SETFUNCTION); - code_code((Byte *)strdup(lua_file[lua_nfile-1])); - code_word($6); - } - lua_codeadjust (0); } - block - END + body { - Byte *b; - codereturn(); - b = calloc (pc, sizeof(Byte)); - if (b == NULL) - lua_error("not enough memory"); - memcpy (b, basepc, pc*sizeof(Byte)); - funcCode = basepc; maxcode=maxcurr; #if LISTING PrintCode(funcCode,funcCode+pc); #endif /* assign function to table field */ pc=maincode; basepc=*initcode; maxcurr=maxmain; nlocalvar=0; - - lua_pushvar($3+1); + lua_pushvar(luaI_findsymbol($2)+1); code_byte(PUSHSTRING); - code_word($6); + code_word(luaI_findconstant($4)); code_byte(PUSHFUNCTION); - code_code(b); + code_code($6); code_byte(STOREINDEXED0); - maincode=pc; *initcode=basepc; maxmain=maxcurr; } ; +body : '(' parlist ')' block END + { + codereturn(); + $$ = calloc (pc, sizeof(Byte)); + if ($$ == NULL) + lua_error("not enough memory"); + memcpy ($$, basepc, pc*sizeof(Byte)); + funcCode = basepc; maxcode=maxcurr; + } + ; + statlist : /* empty */ | statlist stat sc ; @@ -454,7 +416,7 @@ expr : '(' expr ')' { $$ = $2; } | STRING { code_byte(PUSHSTRING); - code_word(lua_findconstant($1)); + code_word($1); $$ = 1; } | NIL {code_byte(PUSHNIL); $$ = 1; } @@ -493,7 +455,7 @@ funcvalue : varexp { $$ = 0; } | varexp ':' NAME { code_byte(PUSHSTRING); - code_word(lua_findconstant($3)); + code_word(luaI_findconstant($3)); code_byte(PUSHSELF); $$ = 1; } @@ -516,18 +478,18 @@ exprlist1 : expr { if ($1 == 0) $$ = -1; else $$ = 1; } } ; -parlist : /* empty */ - | parlist1 +parlist : /* empty */ { lua_codeadjust(0); } + | parlist1 { lua_codeadjust(0); } ; parlist1 : NAME { - localvar[nlocalvar]=lua_findsymbol($1); + localvar[nlocalvar]=luaI_findsymbol($1); add_nlocalvar(1); } | parlist1 ',' NAME { - localvar[nlocalvar]=lua_findsymbol($3); + localvar[nlocalvar]=luaI_findsymbol($3); add_nlocalvar(1); } ; @@ -555,9 +517,9 @@ ffieldlist1 : ffield {$$=1;} } ; -ffield : NAME {$$ = lua_findconstant($1);} '=' expr1 +ffield : NAME '=' expr1 { - push_field($2); + push_field(luaI_findconstant($1)); } ; @@ -591,14 +553,14 @@ var : singlevar { $$ = $1; } | varexp '.' NAME { code_byte(PUSHSTRING); - code_word(lua_findconstant($3)); + code_word(luaI_findconstant($3)); $$ = 0; /* indexed variable */ } ; singlevar : NAME { - Word s = lua_findsymbol($1); + Word s = luaI_findsymbol($1); int local = lua_localname (s); if (local == -1) /* global var */ $$ = s + 1; /* return positive value */ @@ -610,10 +572,10 @@ singlevar : NAME varexp : var { lua_pushvar($1); } ; -localdeclist : NAME {localvar[nlocalvar]=lua_findsymbol($1); $$ = 1;} +localdeclist : NAME {localvar[nlocalvar]=luaI_findsymbol($1); $$ = 1;} | localdeclist ',' NAME { - localvar[nlocalvar+$1]=lua_findsymbol($3); + localvar[nlocalvar+$1]=luaI_findsymbol($3); $$ = $1+1; } ; @@ -676,6 +638,25 @@ static void lua_codeadjust (int n) } } +static void init_function (TreeNode *func) +{ + if (funcCode == NULL) /* first function */ + { + funcCode = (Byte *) calloc(CODE_BLOCK, sizeof(Byte)); + if (funcCode == NULL) + lua_error("not enough memory"); + maxcode = CODE_BLOCK; + } + pc=0; basepc=funcCode; maxcurr=maxcode; + nlocalvar=0; + if (lua_debug) + { + code_byte(SETFUNCTION); + code_code((Byte *)strdup(lua_file[lua_nfile-1])); + code_word(luaI_findconstant(func)); + } +} + static void codereturn (void) { if (lua_debug) code_byte(RESET); diff --git a/table.c b/table.c index 8047b14a..20ab6046 100644 --- a/table.c +++ b/table.c @@ -3,7 +3,7 @@ ** Module to control static tables */ -char *rcs_table="$Id: table.c,v 2.15 1994/11/10 20:41:37 roberto Exp roberto $"; +char *rcs_table="$Id: table.c,v 2.16 1994/11/11 14:00:08 roberto Exp roberto $"; #include #include @@ -51,23 +51,23 @@ static void lua_initsymbol (void) lua_table = (Symbol *) calloc(lua_maxsymbol, sizeof(Symbol)); if (lua_table == NULL) lua_error ("symbol table: not enough memory"); - n = lua_findsymbol("next"); + n = luaI_findsymbolbyname("next"); s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_next; - n = lua_findsymbol("nextvar"); + n = luaI_findsymbolbyname("nextvar"); s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_nextvar; - n = lua_findsymbol("type"); + n = luaI_findsymbolbyname("type"); s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_type; - n = lua_findsymbol("tonumber"); + n = luaI_findsymbolbyname("tonumber"); s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_obj2number; - n = lua_findsymbol("print"); + n = luaI_findsymbolbyname("print"); s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_print; - n = lua_findsymbol("dofile"); + n = luaI_findsymbolbyname("dofile"); s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_internaldofile; - n = lua_findsymbol("dostring"); + n = luaI_findsymbolbyname("dostring"); s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_internaldostring; - n = lua_findsymbol("setfallback"); + n = luaI_findsymbolbyname("setfallback"); s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_setfallback; - n = lua_findsymbol("error"); + n = luaI_findsymbolbyname("error"); s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_error; } @@ -89,13 +89,11 @@ void lua_initconstant (void) ** found, allocate it. ** On error, return -1. */ -int lua_findsymbol (char *s) +int luaI_findsymbol (TreeNode *t) { - char *n; if (lua_table == NULL) lua_initsymbol(); - n = lua_varcreate(s); - if (indexstring(n) == UNMARKED_STRING) + if (t->varindex == UNMARKED_STRING) { if (lua_ntable == lua_maxsymbol) { @@ -106,11 +104,17 @@ int lua_findsymbol (char *s) if (lua_table == NULL) lua_error ("symbol table: not enough memory"); } - indexstring(n) = lua_ntable; + t->varindex = lua_ntable; s_tag(lua_ntable) = LUA_T_NIL; lua_ntable++; } - return indexstring(n); + return t->varindex; +} + + +int luaI_findsymbolbyname (char *name) +{ + return luaI_findsymbol(lua_constcreate(name)); } @@ -119,13 +123,11 @@ int lua_findsymbol (char *s) ** found, allocate it. ** On error, return -1. */ -int lua_findconstant (char *s) +int luaI_findconstant (TreeNode *t) { - char *n; if (lua_constant == NULL) lua_initconstant(); - n = lua_constcreate(s); - if (indexstring(n) == UNMARKED_STRING) + if (t->constindex == UNMARKED_STRING) { if (lua_nconstant == lua_maxconstant) { @@ -136,11 +138,11 @@ int lua_findconstant (char *s) if (lua_constant == NULL) lua_error ("constant table: not enough memory"); } - indexstring(n) = lua_nconstant; - lua_constant[lua_nconstant] = n; + t->constindex = lua_nconstant; + lua_constant[lua_nconstant] = t->str; lua_nconstant++; } - return indexstring(n); + return t->constindex; } diff --git a/table.h b/table.h index a885f201..423ba3dd 100644 --- a/table.h +++ b/table.h @@ -1,12 +1,14 @@ /* ** Module to control static tables ** TeCGraf - PUC-Rio -** $Id: table.h,v 2.3 1994/10/17 19:03:23 celes Exp roberto $ +** $Id: table.h,v 2.4 1994/11/03 21:48:36 roberto Exp roberto $ */ #ifndef table_h #define table_h +#include "tree.h" + extern Symbol *lua_table; extern char **lua_constant; @@ -19,8 +21,9 @@ extern Word lua_recovered; void lua_initconstant (void); -int lua_findsymbol (char *s); -int lua_findconstant (char *s); +int luaI_findsymbolbyname (char *name); +int luaI_findsymbol (TreeNode *t); +int luaI_findconstant (TreeNode *t); void lua_travsymbol (void (*fn)(Object *)); void lua_markobject (Object *o); void lua_pack (void); diff --git a/tree.c b/tree.c index ae5a19de..bca29994 100644 --- a/tree.c +++ b/tree.c @@ -3,7 +3,7 @@ ** TecCGraf - PUC-Rio */ -char *rcs_tree="$Id: tree.c,v 1.2 1994/10/18 17:36:11 celes Exp roberto $"; +char *rcs_tree="$Id: tree.c,v 1.3 1994/11/10 20:41:37 roberto Exp roberto $"; #include @@ -17,22 +17,13 @@ char *rcs_tree="$Id: tree.c,v 1.2 1994/10/18 17:36:11 celes Exp roberto $"; #define lua_strcmp(a,b) (a[0]b[0]?(1):strcmp(a,b))) -typedef struct TreeNode -{ - struct TreeNode *right; - struct TreeNode *left; - Word index; - char str[1]; /* \0 byte already reserved */ -} TreeNode; - static TreeNode *string_root = NULL; static TreeNode *constant_root = NULL; -static TreeNode *variable_root = NULL; /* ** Insert a new string/constant/variable at the tree. */ -static char *tree_create (TreeNode **node, char *str, int *created) +static TreeNode *tree_create (TreeNode **node, char *str, int *created) { if (*node == NULL) { @@ -41,9 +32,9 @@ static char *tree_create (TreeNode **node, char *str, int *created) lua_error("not enough memory"); (*node)->left = (*node)->right = NULL; strcpy((*node)->str, str); - (*node)->index = UNMARKED_STRING; + (*node)->varindex = (*node)->constindex = UNMARKED_STRING; *created = 1; - return (*node)->str; + return *node; } else { @@ -53,35 +44,28 @@ static char *tree_create (TreeNode **node, char *str, int *created) else if (c > 0) return tree_create(&(*node)->right, str, created); else - return (*node)->str; + return *node; } } char *lua_strcreate (char *str) { int created=0; - char *s = tree_create(&string_root, str, &created); + TreeNode *t = tree_create(&string_root, str, &created); if (created) { if (lua_nentity == lua_block) lua_pack (); lua_nentity++; } - return s; + return t->str; } -char *lua_constcreate (char *str) +TreeNode *lua_constcreate (char *str) { int created; return tree_create(&constant_root, str, &created); } -char *lua_varcreate (char *str) -{ - int created; - return tree_create(&variable_root, str, &created); -} - - /* ** Free a node of the tree @@ -141,14 +125,14 @@ static TreeNode *lua_travcollector (TreeNode *r) if (r == NULL) return NULL; r->right = lua_travcollector(r->right); r->left = lua_travcollector(r->left); - if (r->index == UNMARKED_STRING) + if (r->constindex == UNMARKED_STRING) { ++lua_recovered; return lua_strfree(r); } else { - r->index = UNMARKED_STRING; + r->constindex = UNMARKED_STRING; return r; } } @@ -168,18 +152,6 @@ void lua_strcollector (void) */ static TreeNode *tree_next (TreeNode *node, char *str) { -#if 0 - if (node == NULL) return NULL; - if (str == NULL || lua_strcmp(str, node->str) < 0) - { - TreeNode *result = tree_next(node->left, str); - return result == NULL ? node : result; - } - else - { - return tree_next(node->right, str); - } -#else if (node == NULL) return NULL; else if (str == NULL) return node; else @@ -195,30 +167,11 @@ static TreeNode *tree_next (TreeNode *node, char *str) else return tree_next(node->right, str); } -#endif } char *lua_varnext (char *n) { - TreeNode *result = tree_next(variable_root, n); + TreeNode *result = tree_next(constant_root, n); return result != NULL ? result->str : NULL; } - -/* -** Given an id, find the string with exaustive search -*/ -static char *tree_name (TreeNode *node, Word index) -{ - if (node == NULL) return NULL; - if (node->index == index) return node->str; - else - { - char *result = tree_name(node->left, index); - return result != NULL ? result : tree_name(node->right, index); - } -} -char *lua_varname (Word index) -{ - return tree_name(variable_root, index); -} diff --git a/tree.h b/tree.h index b1099e4e..0e7c27c9 100644 --- a/tree.h +++ b/tree.h @@ -1,7 +1,7 @@ /* ** tree.h ** TecCGraf - PUC-Rio -** $Id: $ +** $Id: tree.h,v 1.1 1994/07/19 21:24:17 celes Exp roberto $ */ #ifndef tree_h @@ -14,14 +14,23 @@ #define MARKED_STRING 0xFFFE #define MAX_WORD 0xFFFD + +typedef struct TreeNode +{ + struct TreeNode *right; + struct TreeNode *left; + Word varindex; /* if this is a symbol */ + Word constindex; /* if this is a constant; also used for garbage collection */ + char str[1]; /* \0 byte already reserved */ +} TreeNode; + + #define indexstring(s) (*(((Word *)s)-1)) char *lua_strcreate (char *str); -char *lua_constcreate (char *str); -char *lua_varcreate (char *str); +TreeNode *lua_constcreate (char *str); void lua_strcollector (void); char *lua_varnext (char *n); -char *lua_varname (Word index); #endif