mirror of
https://github.com/lua/lua
synced 2024-11-22 04:41:23 +03:00
Uso de arvores binarias para armazenar nomes e realocacao dinamica
de tabelas (pilhas, hashtable, globais, codigo, etc.)
This commit is contained in:
parent
1c749a3059
commit
493d718b7f
5
hash.c
5
hash.c
@ -4,7 +4,7 @@
|
||||
** Luiz Henrique de Figueiredo - 17 Aug 90
|
||||
*/
|
||||
|
||||
char *rcs_hash="$Id: hash.c,v 1.2 1994/03/28 15:14:02 celes Exp celes $";
|
||||
char *rcs_hash="$Id: hash.c,v 2.1 1994/04/20 22:07:57 celes Exp celes $";
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
@ -31,9 +31,6 @@ char *rcs_hash="$Id: hash.c,v 1.2 1994/03/28 15:14:02 celes Exp celes $";
|
||||
#define ref_nvalue(n) (nvalue(&(n)->ref))
|
||||
#define ref_svalue(n) (svalue(&(n)->ref))
|
||||
|
||||
#ifndef ARRAYBLOCK
|
||||
#define ARRAYBLOCK 50
|
||||
#endif
|
||||
|
||||
typedef struct ArrayList
|
||||
{
|
||||
|
7
inout.c
7
inout.c
@ -4,7 +4,7 @@
|
||||
** facilities.
|
||||
*/
|
||||
|
||||
char *rcs_inout="$Id: inout.c,v 1.1 1993/12/17 18:41:19 celes Exp roberto $";
|
||||
char *rcs_inout="$Id: inout.c,v 1.2 1993/12/22 21:15:16 roberto Exp celes $";
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@ -13,6 +13,7 @@ char *rcs_inout="$Id: inout.c,v 1.1 1993/12/17 18:41:19 celes Exp roberto $";
|
||||
#include "hash.h"
|
||||
#include "inout.h"
|
||||
#include "table.h"
|
||||
#include "tree.h"
|
||||
|
||||
/* Exported variables */
|
||||
int lua_linenumber;
|
||||
@ -157,12 +158,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, s_name(funcstack[nfuncstack-1].function),
|
||||
lua_debugline, lua_varname(funcstack[nfuncstack-1].function),
|
||||
lua_file[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",
|
||||
s_name(funcstack[i].function),
|
||||
lua_varname(funcstack[i].function),
|
||||
lua_file[funcstack[i].file]);
|
||||
}
|
||||
else
|
||||
|
49
lua.stx
49
lua.stx
@ -1,6 +1,6 @@
|
||||
%{
|
||||
|
||||
char *rcs_luastx = "$Id: lua.stx,v 2.3 1994/04/19 19:06:15 celes Exp celes $";
|
||||
char *rcs_luastx = "$Id: lua.stx,v 2.4 1994/04/20 16:22:21 celes Exp celes $";
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@ -16,17 +16,17 @@ char *rcs_luastx = "$Id: lua.stx,v 2.3 1994/04/19 19:06:15 celes Exp celes $";
|
||||
|
||||
#define LISTING 0
|
||||
|
||||
#ifndef GAPCODE
|
||||
#define GAPCODE 50
|
||||
#ifndef CODE_BLOCK
|
||||
#define CODE_BLOCK 256
|
||||
#endif
|
||||
static Word maxcode;
|
||||
static Word maxmain;
|
||||
static Word maxcurr ;
|
||||
static Long maxcode;
|
||||
static Long maxmain;
|
||||
static Long maxcurr ;
|
||||
static Byte *code = NULL;
|
||||
static Byte *initcode;
|
||||
static Byte *basepc;
|
||||
static Word maincode;
|
||||
static Word pc;
|
||||
static Long maincode;
|
||||
static Long pc;
|
||||
|
||||
#define MAXVAR 32
|
||||
static long varbuffer[MAXVAR]; /* variables in an assignment list;
|
||||
@ -48,7 +48,7 @@ static void code_byte (Byte c)
|
||||
{
|
||||
if (pc>maxcurr-2) /* 1 byte free to code HALT of main code */
|
||||
{
|
||||
maxcurr += GAPCODE;
|
||||
maxcurr *= 2;
|
||||
basepc = (Byte *)realloc(basepc, maxcurr*sizeof(Byte));
|
||||
if (basepc == NULL)
|
||||
{
|
||||
@ -155,7 +155,8 @@ static void incr_nvarbuffer (void)
|
||||
}
|
||||
|
||||
static void code_number (float f)
|
||||
{ Word i = (Word)f;
|
||||
{
|
||||
Word i = (Word)f;
|
||||
if (f == (float)i) /* f has an (short) integer value */
|
||||
{
|
||||
if (i <= 2) code_byte(PUSH0 + i);
|
||||
@ -184,10 +185,10 @@ static void code_number (float f)
|
||||
%union
|
||||
{
|
||||
int vInt;
|
||||
long vLong;
|
||||
float vFloat;
|
||||
char *pChar;
|
||||
Word vWord;
|
||||
Long vLong;
|
||||
Byte *pByte;
|
||||
}
|
||||
|
||||
@ -203,7 +204,7 @@ static void code_number (float f)
|
||||
%token <pChar> NAME
|
||||
%token <vInt> DEBUG
|
||||
|
||||
%type <vWord> PrepJump
|
||||
%type <vLong> PrepJump
|
||||
%type <vInt> expr, exprlist, exprlist1, varlist1, typeconstructor
|
||||
%type <vInt> fieldlist, localdeclist
|
||||
%type <vInt> ffieldlist, ffieldlist1
|
||||
@ -240,13 +241,13 @@ function : FUNCTION NAME
|
||||
{
|
||||
if (code == NULL) /* first function */
|
||||
{
|
||||
code = (Byte *) calloc(GAPCODE, sizeof(Byte));
|
||||
code = (Byte *) calloc(CODE_BLOCK, sizeof(Byte));
|
||||
if (code == NULL)
|
||||
{
|
||||
lua_error("not enough memory");
|
||||
err = 1;
|
||||
}
|
||||
maxcode = GAPCODE;
|
||||
maxcode = CODE_BLOCK;
|
||||
}
|
||||
pc=0; basepc=code; maxcurr=maxcode;
|
||||
nlocalvar=0;
|
||||
@ -301,7 +302,7 @@ sc : /* empty */ | ';' ;
|
||||
stat1 : IF expr1 THEN PrepJump block PrepJump elsepart END
|
||||
{
|
||||
{
|
||||
Word elseinit = $6+sizeof(Word)+1;
|
||||
Long elseinit = $6+sizeof(Word)+1;
|
||||
if (pc - elseinit == 0) /* no else */
|
||||
{
|
||||
pc -= sizeof(Word)+1;
|
||||
@ -317,21 +318,21 @@ stat1 : IF expr1 THEN PrepJump block PrepJump elsepart END
|
||||
}
|
||||
}
|
||||
|
||||
| WHILE {$<vWord>$=pc;} expr1 DO PrepJump block PrepJump END
|
||||
| WHILE {$<vLong>$=pc;} expr1 DO PrepJump block PrepJump END
|
||||
|
||||
{
|
||||
basepc[$5] = IFFJMP;
|
||||
code_word_at(basepc+$5+1, pc - ($5 + sizeof(Word)+1));
|
||||
|
||||
basepc[$7] = UPJMP;
|
||||
code_word_at(basepc+$7+1, pc - ($<vWord>2));
|
||||
code_word_at(basepc+$7+1, pc - ($<vLong>2));
|
||||
}
|
||||
|
||||
| REPEAT {$<vWord>$=pc;} block UNTIL expr1 PrepJump
|
||||
| REPEAT {$<vLong>$=pc;} block UNTIL expr1 PrepJump
|
||||
|
||||
{
|
||||
basepc[$6] = IFFUPJMP;
|
||||
code_word_at(basepc+$6+1, pc - ($<vWord>2));
|
||||
code_word_at(basepc+$6+1, pc - ($<vLong>2));
|
||||
}
|
||||
|
||||
|
||||
@ -357,7 +358,7 @@ elsepart : /* empty */
|
||||
| ELSEIF expr1 THEN PrepJump block PrepJump elsepart
|
||||
{
|
||||
{
|
||||
Word elseinit = $6+sizeof(Word)+1;
|
||||
Long elseinit = $6+sizeof(Word)+1;
|
||||
if (pc - elseinit == 0) /* no else */
|
||||
{
|
||||
pc -= sizeof(Word)+1;
|
||||
@ -459,13 +460,13 @@ expr : '(' expr ')' { $$ = $2; }
|
||||
typeconstructor: '@'
|
||||
{
|
||||
code_byte(PUSHBYTE);
|
||||
$<vWord>$ = pc; code_byte(0);
|
||||
$<vLong>$ = pc; code_byte(0);
|
||||
incr_ntemp();
|
||||
code_byte(CREATEARRAY);
|
||||
}
|
||||
objectname fieldlist
|
||||
{
|
||||
basepc[$<vWord>2] = $4;
|
||||
basepc[$<vLong>2] = $4;
|
||||
if ($3 < 0) /* there is no function to be called */
|
||||
{
|
||||
$$ = 1;
|
||||
@ -725,9 +726,9 @@ int yywrap (void)
|
||||
*/
|
||||
int lua_parse (void)
|
||||
{
|
||||
Byte *init = initcode = (Byte *) calloc(GAPCODE, sizeof(Byte));
|
||||
Byte *init = initcode = (Byte *) calloc(CODE_BLOCK, sizeof(Byte));
|
||||
maincode = 0;
|
||||
maxmain = GAPCODE;
|
||||
maxmain = CODE_BLOCK;
|
||||
if (init == NULL)
|
||||
{
|
||||
lua_error("not enough memory");
|
||||
|
141
opcode.c
141
opcode.c
@ -3,7 +3,7 @@
|
||||
** TecCGraf - PUC-Rio
|
||||
*/
|
||||
|
||||
char *rcs_opcode="$Id: opcode.c,v 1.4 1994/04/13 21:37:20 celes Exp celes $";
|
||||
char *rcs_opcode="$Id: opcode.c,v 2.1 1994/04/20 22:07:57 celes Exp celes $";
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@ -23,11 +23,55 @@ char *rcs_opcode="$Id: opcode.c,v 1.4 1994/04/13 21:37:20 celes Exp celes $";
|
||||
#define tonumber(o) ((tag(o) != T_NUMBER) && (lua_tonumber(o) != 0))
|
||||
#define tostring(o) ((tag(o) != T_STRING) && (lua_tostring(o) != 0))
|
||||
|
||||
#ifndef MAXSTACK
|
||||
#define MAXSTACK 256
|
||||
#endif
|
||||
static Object stack[MAXSTACK] = {{T_MARK, {NULL}}};
|
||||
static Object *top=stack+1, *base=stack+1;
|
||||
|
||||
#define STACK_BUFFER (STACKGAP+128)
|
||||
|
||||
static Word maxstack;
|
||||
static Object *stack=NULL;
|
||||
static Object *top, *base;
|
||||
|
||||
|
||||
/*
|
||||
** Init stack
|
||||
*/
|
||||
static int lua_initstack (void)
|
||||
{
|
||||
maxstack = STACK_BUFFER;
|
||||
stack = (Object *)calloc(maxstack, sizeof(Object));
|
||||
if (stack == NULL)
|
||||
{
|
||||
lua_error("stack - not enough memory");
|
||||
return 1;
|
||||
}
|
||||
tag(stack) = T_MARK;
|
||||
top = base = stack+1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Check stack overflow and, if necessary, realloc vector
|
||||
*/
|
||||
static int lua_checkstack (Word n)
|
||||
{
|
||||
if (stack == NULL)
|
||||
return lua_initstack();
|
||||
if (n > maxstack)
|
||||
{
|
||||
Word t = top-stack;
|
||||
Word b = base-stack;
|
||||
maxstack *= 2;
|
||||
stack = (Object *)realloc(stack, maxstack*sizeof(Object));
|
||||
if (stack == NULL)
|
||||
{
|
||||
lua_error("stack - not enough memory");
|
||||
return 1;
|
||||
}
|
||||
top = stack + t;
|
||||
base = stack + b;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
@ -36,30 +80,14 @@ static Object *top=stack+1, *base=stack+1;
|
||||
*/
|
||||
static char *lua_strconc (char *l, char *r)
|
||||
{
|
||||
char *s = calloc (strlen(l)+strlen(r)+2, sizeof(char));
|
||||
if (s == NULL)
|
||||
static char buffer[1024];
|
||||
int n = strlen(l)+strlen(r)+1;
|
||||
if (n > 1024)
|
||||
{
|
||||
lua_error ("not enough memory");
|
||||
lua_error ("string too large");
|
||||
return NULL;
|
||||
}
|
||||
*s++ = 0; /* create mark space */
|
||||
return strcat(strcpy(s,l),r);
|
||||
}
|
||||
|
||||
/*
|
||||
** Duplicate a string, creating a mark space at the beginning.
|
||||
** Return the new string pointer.
|
||||
*/
|
||||
char *lua_strdup (char *l)
|
||||
{
|
||||
char *s = calloc (strlen(l)+2, sizeof(char));
|
||||
if (s == NULL)
|
||||
{
|
||||
lua_error ("not enough memory");
|
||||
return NULL;
|
||||
}
|
||||
*s++ = 0; /* create mark space */
|
||||
return strcpy(s,l);
|
||||
return strcat(strcpy(buffer,l),r);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -127,7 +155,7 @@ static int lua_tostring (Object *obj)
|
||||
sprintf (s, "%d", (int) nvalue(obj));
|
||||
else
|
||||
sprintf (s, "%g", nvalue(obj));
|
||||
svalue(obj) = lua_createstring(lua_strdup(s));
|
||||
svalue(obj) = lua_createstring(s);
|
||||
if (svalue(obj) == NULL)
|
||||
return 1;
|
||||
tag(obj) = T_STRING;
|
||||
@ -140,7 +168,12 @@ static int lua_tostring (Object *obj)
|
||||
*/
|
||||
int lua_execute (Byte *pc)
|
||||
{
|
||||
Object *oldbase = base;
|
||||
Word oldbase;
|
||||
|
||||
if (stack == NULL)
|
||||
lua_initstack();
|
||||
|
||||
oldbase = base-stack;
|
||||
base = top;
|
||||
while (1)
|
||||
{
|
||||
@ -516,11 +549,8 @@ int lua_execute (Byte *pc)
|
||||
nvalue(b) = (base-stack); /* store base value */
|
||||
base = b+1;
|
||||
pc = newpc;
|
||||
if (MAXSTACK-(base-stack) < STACKGAP)
|
||||
{
|
||||
lua_error ("stack overflow");
|
||||
if (lua_checkstack(STACKGAP+(base-stack)))
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else if (tag(b-1) == T_CFUNCTION)
|
||||
{
|
||||
@ -569,7 +599,7 @@ int lua_execute (Byte *pc)
|
||||
break;
|
||||
|
||||
case HALT:
|
||||
base = oldbase;
|
||||
base = stack+oldbase;
|
||||
return 0; /* success */
|
||||
|
||||
case SETFUNCTION:
|
||||
@ -726,7 +756,7 @@ Object *lua_getfield (Object *object, char *field)
|
||||
{
|
||||
Object ref;
|
||||
tag(&ref) = T_STRING;
|
||||
svalue(&ref) = lua_createstring(lua_strdup(field));
|
||||
svalue(&ref) = lua_createstring(field);
|
||||
return (lua_hashdefine(avalue(object), &ref));
|
||||
}
|
||||
}
|
||||
@ -774,12 +804,9 @@ Object *lua_pop (void)
|
||||
*/
|
||||
int lua_pushnil (void)
|
||||
{
|
||||
if ((top-stack) >= MAXSTACK-1)
|
||||
{
|
||||
lua_error ("stack overflow");
|
||||
if (lua_checkstack(top-stack+1) == 1)
|
||||
return 1;
|
||||
}
|
||||
tag(top) = T_NIL;
|
||||
tag(top++) = T_NIL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -788,11 +815,8 @@ int lua_pushnil (void)
|
||||
*/
|
||||
int lua_pushnumber (real n)
|
||||
{
|
||||
if ((top-stack) >= MAXSTACK-1)
|
||||
{
|
||||
lua_error ("stack overflow");
|
||||
if (lua_checkstack(top-stack+1) == 1)
|
||||
return 1;
|
||||
}
|
||||
tag(top) = T_NUMBER; nvalue(top++) = n;
|
||||
return 0;
|
||||
}
|
||||
@ -802,13 +826,10 @@ int lua_pushnumber (real n)
|
||||
*/
|
||||
int lua_pushstring (char *s)
|
||||
{
|
||||
if ((top-stack) >= MAXSTACK-1)
|
||||
{
|
||||
lua_error ("stack overflow");
|
||||
if (lua_checkstack(top-stack+1) == 1)
|
||||
return 1;
|
||||
}
|
||||
tag(top) = T_STRING;
|
||||
svalue(top++) = lua_createstring(lua_strdup(s));
|
||||
svalue(top++) = lua_createstring(s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -817,11 +838,8 @@ int lua_pushstring (char *s)
|
||||
*/
|
||||
int lua_pushcfunction (lua_CFunction fn)
|
||||
{
|
||||
if ((top-stack) >= MAXSTACK-1)
|
||||
{
|
||||
lua_error ("stack overflow");
|
||||
if (lua_checkstack(top-stack+1) == 1)
|
||||
return 1;
|
||||
}
|
||||
tag(top) = T_CFUNCTION; fvalue(top++) = fn;
|
||||
return 0;
|
||||
}
|
||||
@ -831,11 +849,8 @@ int lua_pushcfunction (lua_CFunction fn)
|
||||
*/
|
||||
int lua_pushuserdata (void *u)
|
||||
{
|
||||
if ((top-stack) >= MAXSTACK-1)
|
||||
{
|
||||
lua_error ("stack overflow");
|
||||
if (lua_checkstack(top-stack+1) == 1)
|
||||
return 1;
|
||||
}
|
||||
tag(top) = T_USERDATA; uvalue(top++) = u;
|
||||
return 0;
|
||||
}
|
||||
@ -845,11 +860,8 @@ int lua_pushuserdata (void *u)
|
||||
*/
|
||||
int lua_pushobject (Object *o)
|
||||
{
|
||||
if ((top-stack) >= MAXSTACK-1)
|
||||
{
|
||||
lua_error ("stack overflow");
|
||||
if (lua_checkstack(top-stack+1) == 1)
|
||||
return 1;
|
||||
}
|
||||
*top++ = *o;
|
||||
return 0;
|
||||
}
|
||||
@ -878,7 +890,7 @@ int lua_storefield (lua_Object object, char *field)
|
||||
{
|
||||
Object ref, *h;
|
||||
tag(&ref) = T_STRING;
|
||||
svalue(&ref) = lua_createstring(lua_strdup(field));
|
||||
svalue(&ref) = lua_createstring(field);
|
||||
h = lua_hashdefine(avalue(object), &ref);
|
||||
if (h == NULL) return 1;
|
||||
if (tag(top-1) == T_MARK) return 1;
|
||||
@ -963,6 +975,9 @@ int lua_isuserdata (Object *object)
|
||||
void lua_type (void)
|
||||
{
|
||||
Object *o = lua_getparam(1);
|
||||
|
||||
if (lua_constant == NULL)
|
||||
lua_initconstant();
|
||||
lua_pushstring (lua_constant[tag(o)]);
|
||||
}
|
||||
|
||||
@ -981,7 +996,7 @@ void lua_obj2number (void)
|
||||
void lua_print (void)
|
||||
{
|
||||
int i=1;
|
||||
void *obj;
|
||||
Object *obj;
|
||||
while ((obj=lua_getparam (i++)) != NULL)
|
||||
{
|
||||
if (lua_isnumber(obj)) printf("%g\n",lua_getnumber (obj));
|
||||
|
6
opcode.h
6
opcode.h
@ -1,6 +1,6 @@
|
||||
/*
|
||||
** TeCGraf - PUC-Rio
|
||||
** $Id: opcode.h,v 1.4 1994/04/13 21:37:20 celes Exp celes $
|
||||
** $Id: opcode.h,v 2.1 1994/04/20 22:07:57 celes Exp celes $
|
||||
*/
|
||||
|
||||
#ifndef opcode_h
|
||||
@ -20,6 +20,8 @@ typedef unsigned char Byte;
|
||||
|
||||
typedef unsigned short Word;
|
||||
|
||||
typedef signed long Long;
|
||||
|
||||
typedef union
|
||||
{
|
||||
struct {char c1; char c2;} m;
|
||||
@ -116,7 +118,6 @@ typedef struct Object
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char *name;
|
||||
Object object;
|
||||
} Symbol;
|
||||
|
||||
@ -130,7 +131,6 @@ typedef struct
|
||||
#define uvalue(o) ((o)->value.u)
|
||||
|
||||
/* Macros to access symbol table */
|
||||
#define s_name(i) (lua_table[i].name)
|
||||
#define s_object(i) (lua_table[i].object)
|
||||
#define s_tag(i) (tag(&s_object(i)))
|
||||
#define s_nvalue(i) (nvalue(&s_object(i)))
|
||||
|
308
table.c
308
table.c
@ -3,7 +3,7 @@
|
||||
** Module to control static tables
|
||||
*/
|
||||
|
||||
char *rcs_table="$Id: table.c,v 1.5 1994/04/13 22:10:21 celes Exp celes $";
|
||||
char *rcs_table="$Id: table.c,v 2.1 1994/04/20 22:07:57 celes Exp celes $";
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@ -11,6 +11,7 @@ char *rcs_table="$Id: table.c,v 1.5 1994/04/13 22:10:21 celes Exp celes $";
|
||||
#include "mm.h"
|
||||
|
||||
#include "opcode.h"
|
||||
#include "tree.h"
|
||||
#include "hash.h"
|
||||
#include "inout.h"
|
||||
#include "table.h"
|
||||
@ -18,144 +19,159 @@ char *rcs_table="$Id: table.c,v 1.5 1994/04/13 22:10:21 celes Exp celes $";
|
||||
|
||||
#define streq(s1,s2) (s1[0]==s2[0]&&strcmp(s1+1,s2+1)==0)
|
||||
|
||||
#ifndef MAXSYMBOL
|
||||
#define MAXSYMBOL 512
|
||||
#endif
|
||||
static Symbol tablebuffer[MAXSYMBOL] = {
|
||||
{"type",{T_CFUNCTION,{lua_type}}},
|
||||
{"tonumber",{T_CFUNCTION,{lua_obj2number}}},
|
||||
{"next",{T_CFUNCTION,{lua_next}}},
|
||||
{"nextvar",{T_CFUNCTION,{lua_nextvar}}},
|
||||
{"print",{T_CFUNCTION,{lua_print}}},
|
||||
{"dofile",{T_CFUNCTION,{lua_internaldofile}}},
|
||||
{"dostring",{T_CFUNCTION,{lua_internaldostring}}}
|
||||
};
|
||||
Symbol *lua_table=tablebuffer;
|
||||
Word lua_ntable=7;
|
||||
#define BUFFER_BLOCK 256
|
||||
|
||||
struct List
|
||||
{
|
||||
Symbol *s;
|
||||
struct List *next;
|
||||
};
|
||||
Symbol *lua_table;
|
||||
static Word lua_ntable = 0;
|
||||
static Word lua_maxsymbol = 0;
|
||||
|
||||
static struct List o6={ tablebuffer+6, 0};
|
||||
static struct List o5={ tablebuffer+5, &o6 };
|
||||
static struct List o4={ tablebuffer+4, &o5 };
|
||||
static struct List o3={ tablebuffer+3, &o4 };
|
||||
static struct List o2={ tablebuffer+2, &o3 };
|
||||
static struct List o1={ tablebuffer+1, &o2 };
|
||||
static struct List o0={ tablebuffer+0, &o1 };
|
||||
static struct List *searchlist=&o0;
|
||||
char **lua_constant;
|
||||
static Word lua_nconstant = 0;
|
||||
static Word lua_maxconstant = 0;
|
||||
|
||||
#ifndef MAXCONSTANT
|
||||
#define MAXCONSTANT 256
|
||||
#endif
|
||||
/* pre-defined constants need garbage collection extra byte */
|
||||
static char tm[] = " mark";
|
||||
static char ti[] = " nil";
|
||||
static char tn[] = " number";
|
||||
static char ts[] = " string";
|
||||
static char tt[] = " table";
|
||||
static char tf[] = " function";
|
||||
static char tc[] = " cfunction";
|
||||
static char tu[] = " userdata";
|
||||
static char *constantbuffer[MAXCONSTANT] = {tm+1, ti+1,
|
||||
tn+1, ts+1,
|
||||
tt+1, tf+1,
|
||||
tc+1, tu+1
|
||||
};
|
||||
char **lua_constant = constantbuffer;
|
||||
Word lua_nconstant=T_USERDATA+1;
|
||||
|
||||
#ifndef MAXSTRING
|
||||
#define MAXSTRING 512
|
||||
#endif
|
||||
static char *stringbuffer[MAXSTRING];
|
||||
char **lua_string = stringbuffer;
|
||||
Word lua_nstring=0;
|
||||
|
||||
#define MAXFILE 20
|
||||
char *lua_file[MAXFILE];
|
||||
int lua_nfile;
|
||||
|
||||
|
||||
#define markstring(s) (*((s)-1))
|
||||
|
||||
|
||||
/* Variables to controll garbage collection */
|
||||
Word lua_block=10; /* to check when garbage collector will be called */
|
||||
#define GARBAGE_BLOCK 256
|
||||
Word lua_block=GARBAGE_BLOCK; /* when garbage collector will be called */
|
||||
Word lua_nentity; /* counter of new entities (strings and arrays) */
|
||||
|
||||
|
||||
/*
|
||||
** Initialise symbol table with internal functions
|
||||
*/
|
||||
static void lua_initsymbol (void)
|
||||
{
|
||||
int n;
|
||||
lua_maxsymbol = BUFFER_BLOCK;
|
||||
lua_table = (Symbol *) calloc(lua_maxsymbol, sizeof(Symbol));
|
||||
if (lua_table == NULL)
|
||||
{
|
||||
lua_error ("symbol table: not enough memory");
|
||||
return;
|
||||
}
|
||||
n = lua_findsymbol("type");
|
||||
s_tag(n) = T_CFUNCTION; s_fvalue(n) = lua_type;
|
||||
n = lua_findsymbol("tonumber");
|
||||
s_tag(n) = T_CFUNCTION; s_fvalue(n) = lua_obj2number;
|
||||
n = lua_findsymbol("next");
|
||||
s_tag(n) = T_CFUNCTION; s_fvalue(n) = lua_next;
|
||||
n = lua_findsymbol("nextvar");
|
||||
s_tag(n) = T_CFUNCTION; s_fvalue(n) = lua_nextvar;
|
||||
n = lua_findsymbol("print");
|
||||
s_tag(n) = T_CFUNCTION; s_fvalue(n) = lua_print;
|
||||
n = lua_findsymbol("dofile");
|
||||
s_tag(n) = T_CFUNCTION; s_fvalue(n) = lua_internaldofile;
|
||||
n = lua_findsymbol("dostring");
|
||||
s_tag(n) = T_CFUNCTION; s_fvalue(n) = lua_internaldostring;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Initialise constant table with pre-defined constants
|
||||
*/
|
||||
void lua_initconstant (void)
|
||||
{
|
||||
lua_maxconstant = BUFFER_BLOCK;
|
||||
lua_constant = (char **) calloc(lua_maxconstant, sizeof(char *));
|
||||
if (lua_constant == NULL)
|
||||
{
|
||||
lua_error ("constant table: not enough memory");
|
||||
return;
|
||||
}
|
||||
lua_findconstant("mark");
|
||||
lua_findconstant("nil");
|
||||
lua_findconstant("number");
|
||||
lua_findconstant("string");
|
||||
lua_findconstant("table");
|
||||
lua_findconstant("function");
|
||||
lua_findconstant("cfunction");
|
||||
lua_findconstant("userdata");
|
||||
}
|
||||
|
||||
/*
|
||||
** Given a name, search it at symbol table and return its index. If not
|
||||
** found, allocate at end of table, checking oveflow and return its index.
|
||||
** found, allocate it.
|
||||
** On error, return -1.
|
||||
*/
|
||||
int lua_findsymbol (char *s)
|
||||
{
|
||||
struct List *l, *p;
|
||||
for (p=NULL, l=searchlist; l!=NULL; p=l, l=l->next)
|
||||
if (streq(s,l->s->name))
|
||||
char *n;
|
||||
if (lua_table == NULL)
|
||||
lua_initsymbol();
|
||||
n = lua_varcreate(s);
|
||||
if (n == NULL)
|
||||
{
|
||||
lua_error ("create symbol: not enough memory");
|
||||
return -1;
|
||||
}
|
||||
if (indexstring(n) == UNMARKED_STRING)
|
||||
{
|
||||
if (lua_ntable == lua_maxsymbol)
|
||||
{
|
||||
if (p!=NULL)
|
||||
lua_maxsymbol *= 2;
|
||||
if (lua_maxsymbol > MAX_WORD)
|
||||
{
|
||||
p->next = l->next;
|
||||
l->next = searchlist;
|
||||
searchlist = l;
|
||||
lua_error("symbol table overflow");
|
||||
return -1;
|
||||
}
|
||||
lua_table = (Symbol *)realloc(lua_table, lua_maxsymbol*sizeof(Symbol));
|
||||
if (lua_table == NULL)
|
||||
{
|
||||
lua_error ("symbol table: not enough memory");
|
||||
return -1;
|
||||
}
|
||||
return (l->s-lua_table);
|
||||
}
|
||||
|
||||
if (lua_ntable >= MAXSYMBOL-1)
|
||||
{
|
||||
lua_error ("symbol table overflow");
|
||||
return -1;
|
||||
indexstring(n) = lua_ntable;
|
||||
s_tag(lua_ntable) = T_NIL;
|
||||
lua_ntable++;
|
||||
}
|
||||
s_name(lua_ntable) = strdup(s);
|
||||
if (s_name(lua_ntable) == NULL)
|
||||
{
|
||||
lua_error ("not enough memory");
|
||||
return -1;
|
||||
}
|
||||
s_tag(lua_ntable) = T_NIL;
|
||||
p = malloc(sizeof(*p));
|
||||
p->s = lua_table+lua_ntable;
|
||||
p->next = searchlist;
|
||||
searchlist = p;
|
||||
|
||||
return lua_ntable++;
|
||||
return indexstring(n);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Given a constant string, search it at constant table and return its index.
|
||||
** If not found, allocate at end of the table, checking oveflow and return
|
||||
** its index.
|
||||
**
|
||||
** For each allocation, the function allocate a extra char to be used to
|
||||
** mark used string (it's necessary to deal with constant and string
|
||||
** uniformily). The function store at the table the second position allocated,
|
||||
** that represents the beginning of the real string. On error, return -1.
|
||||
**
|
||||
** Given a name, search it at constant table and return its index. If not
|
||||
** found, allocate it.
|
||||
** On error, return -1.
|
||||
*/
|
||||
int lua_findconstant (char *s)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<lua_nconstant; i++)
|
||||
if (streq(s,lua_constant[i]))
|
||||
return i;
|
||||
if (lua_nconstant >= MAXCONSTANT-1)
|
||||
char *n;
|
||||
if (lua_constant == NULL)
|
||||
lua_initconstant();
|
||||
n = lua_constcreate(s);
|
||||
if (n == NULL)
|
||||
{
|
||||
lua_error ("lua: constant string table overflow");
|
||||
lua_error ("create constant: not enough memory");
|
||||
return -1;
|
||||
}
|
||||
if (indexstring(n) == UNMARKED_STRING)
|
||||
{
|
||||
char *c = calloc(strlen(s)+2,sizeof(char));
|
||||
c++; /* create mark space */
|
||||
lua_constant[lua_nconstant++] = strcpy(c,s);
|
||||
if (lua_nconstant == lua_maxconstant)
|
||||
{
|
||||
lua_maxconstant *= 2;
|
||||
if (lua_maxconstant > MAX_WORD)
|
||||
{
|
||||
lua_error("constant table overflow");
|
||||
return -1;
|
||||
}
|
||||
lua_constant = (char**)realloc(lua_constant,lua_maxconstant*sizeof(char*));
|
||||
if (lua_constant == NULL)
|
||||
{
|
||||
lua_error ("constant table: not enough memory");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
indexstring(n) = lua_nconstant;
|
||||
lua_constant[lua_nconstant] = n;
|
||||
lua_nconstant++;
|
||||
}
|
||||
return (lua_nconstant-1);
|
||||
return indexstring(n);
|
||||
}
|
||||
|
||||
|
||||
@ -175,10 +191,10 @@ void lua_travsymbol (void (*fn)(Object *))
|
||||
*/
|
||||
void lua_markobject (Object *o)
|
||||
{
|
||||
if (tag(o) == T_STRING)
|
||||
markstring (svalue(o)) = 1;
|
||||
if (tag(o) == T_STRING && indexstring(svalue(o)) == UNMARKED_STRING)
|
||||
indexstring(svalue(o)) = MARKED_STRING;
|
||||
else if (tag(o) == T_ARRAY)
|
||||
lua_hashmark (avalue(o));
|
||||
lua_hashmark (avalue(o));
|
||||
}
|
||||
|
||||
|
||||
@ -194,63 +210,27 @@ void lua_pack (void)
|
||||
/* mark symbol table strings */
|
||||
lua_travsymbol(lua_markobject);
|
||||
|
||||
lua_stringcollector();
|
||||
lua_strcollector();
|
||||
lua_hashcollector();
|
||||
|
||||
lua_nentity = 0; /* reset counter */
|
||||
}
|
||||
|
||||
/*
|
||||
** Garbage collection to atrings.
|
||||
** Delete all unmarked strings
|
||||
*/
|
||||
void lua_stringcollector (void)
|
||||
{
|
||||
int i, j;
|
||||
for (i=j=0; i<lua_nstring; i++)
|
||||
if (markstring(lua_string[i]) == 1)
|
||||
{
|
||||
lua_string[j++] = lua_string[i];
|
||||
markstring(lua_string[i]) = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
free (lua_string[i]-1);
|
||||
}
|
||||
lua_nstring = j;
|
||||
}
|
||||
|
||||
/*
|
||||
** Allocate a new string at string table. The given string is already
|
||||
** allocated with mark space and the function puts it at the end of the
|
||||
** table, checking overflow, and returns its own pointer, or NULL on error.
|
||||
** If the string isn't allocated, allocate a new string at string tree.
|
||||
*/
|
||||
char *lua_createstring (char *s)
|
||||
{
|
||||
int i;
|
||||
if (s == NULL) return NULL;
|
||||
|
||||
for (i=0; i<lua_nstring; i++)
|
||||
if (streq(s,lua_string[i]))
|
||||
{
|
||||
free(s-1);
|
||||
return lua_string[i];
|
||||
}
|
||||
|
||||
if (lua_nentity == lua_block || lua_nstring >= MAXSTRING-1)
|
||||
{
|
||||
if (lua_nentity == lua_block)
|
||||
lua_pack ();
|
||||
if (lua_nstring >= MAXSTRING-1)
|
||||
{
|
||||
lua_error ("string table overflow");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
lua_string[lua_nstring++] = s;
|
||||
lua_nentity++;
|
||||
return s;
|
||||
return lua_strcreate(s);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Add a file name at file table, checking overflow. This function also set
|
||||
** the external variable "lua_filename" with the function filename set.
|
||||
@ -293,7 +273,7 @@ char *lua_filename (void)
|
||||
*/
|
||||
void lua_nextvar (void)
|
||||
{
|
||||
int index;
|
||||
char *varname, *next;
|
||||
Object *o = lua_getparam (1);
|
||||
if (o == NULL)
|
||||
{ lua_error ("too few arguments to function `nextvar'"); return; }
|
||||
@ -301,7 +281,7 @@ void lua_nextvar (void)
|
||||
{ lua_error ("too many arguments to function `nextvar'"); return; }
|
||||
if (tag(o) == T_NIL)
|
||||
{
|
||||
index = 0;
|
||||
varname = 0;
|
||||
}
|
||||
else if (tag(o) != T_STRING)
|
||||
{
|
||||
@ -310,28 +290,20 @@ void lua_nextvar (void)
|
||||
}
|
||||
else
|
||||
{
|
||||
for (index=0; index<lua_ntable; index++)
|
||||
if (streq(s_name(index),svalue(o))) break;
|
||||
if (index == lua_ntable)
|
||||
{
|
||||
lua_error ("name not found in function `nextvar'");
|
||||
return;
|
||||
}
|
||||
index++;
|
||||
while (index < lua_ntable && tag(&s_object(index)) == T_NIL) index++;
|
||||
|
||||
if (index == lua_ntable)
|
||||
{
|
||||
lua_pushnil();
|
||||
lua_pushnil();
|
||||
return;
|
||||
}
|
||||
varname = svalue(o);
|
||||
}
|
||||
next = lua_varnext(varname);
|
||||
if (next == NULL)
|
||||
{
|
||||
lua_pushnil();
|
||||
lua_pushnil();
|
||||
}
|
||||
else
|
||||
{
|
||||
Object name;
|
||||
tag(&name) = T_STRING;
|
||||
svalue(&name) = lua_createstring(lua_strdup(s_name(index)));
|
||||
svalue(&name) = next;
|
||||
if (lua_pushobject (&name)) return;
|
||||
if (lua_pushobject (&s_object(index))) return;
|
||||
if (lua_pushobject (&s_object(indexstring(next)))) return;
|
||||
}
|
||||
}
|
||||
|
14
table.h
14
table.h
@ -1,23 +1,14 @@
|
||||
/*
|
||||
** Module to control static tables
|
||||
** TeCGraf - PUC-Rio
|
||||
** $Id: table.h,v 1.2 1993/12/22 21:15:16 roberto Exp celes $
|
||||
** $Id: table.h,v 2.1 1994/04/20 22:07:57 celes Exp celes $
|
||||
*/
|
||||
|
||||
#ifndef table_h
|
||||
#define table_h
|
||||
|
||||
extern Symbol *lua_table;
|
||||
extern Word lua_ntable;
|
||||
|
||||
extern char **lua_constant;
|
||||
extern Word lua_nconstant;
|
||||
|
||||
extern char **lua_string;
|
||||
extern Word lua_nstring;
|
||||
|
||||
extern Hash **lua_array;
|
||||
extern Word lua_narray;
|
||||
|
||||
extern char *lua_file[];
|
||||
extern int lua_nfile;
|
||||
@ -26,13 +17,12 @@ extern Word lua_block;
|
||||
extern Word lua_nentity;
|
||||
|
||||
|
||||
|
||||
void lua_initconstant (void);
|
||||
int lua_findsymbol (char *s);
|
||||
int lua_findconstant (char *s);
|
||||
void lua_travsymbol (void (*fn)(Object *));
|
||||
void lua_markobject (Object *o);
|
||||
void lua_pack (void);
|
||||
void lua_stringcollector (void);
|
||||
char *lua_createstring (char *s);
|
||||
int lua_addfile (char *fn);
|
||||
int lua_delfile (void);
|
||||
|
Loading…
Reference in New Issue
Block a user