mirror of
https://github.com/lua/lua.git
synced 2026-07-30 18:09:05 +00:00
Compare commits
108 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ce23901f04 | ||
|
|
df1ee1fb1c | ||
|
|
f1d0276684 | ||
|
|
7ecc2ea597 | ||
|
|
7a35f23c16 | ||
|
|
9284742a11 | ||
|
|
9704ff4cb1 | ||
|
|
e3c0ce9a69 | ||
|
|
85b76bcc01 | ||
|
|
a275d9a25b | ||
|
|
7e0be1fbde | ||
|
|
54ba642cc3 | ||
|
|
8826eb7918 | ||
|
|
e701a86385 | ||
|
|
3e1f731826 | ||
|
|
f86c1367db | ||
|
|
58fd8aa851 | ||
|
|
3226ac2da8 | ||
|
|
3e9daa7416 | ||
|
|
7236df875a | ||
|
|
675e608325 | ||
|
|
1dc0e82aeb | ||
|
|
c2eb02aaf6 | ||
|
|
2fee7e42c9 | ||
|
|
281db390e8 | ||
|
|
df8cf53cc9 | ||
|
|
40306b10db | ||
|
|
5eff5d3eac | ||
|
|
8ad8426c43 | ||
|
|
3cab7cd025 | ||
|
|
bb26efbbec | ||
|
|
5c0e5fd36d | ||
|
|
621322a305 | ||
|
|
e33a3b8e0d | ||
|
|
9a6cccb08c | ||
|
|
b58225e93b | ||
|
|
852b919465 | ||
|
|
ef94999647 | ||
|
|
6f30fa98d8 | ||
|
|
74102bd716 | ||
|
|
8d82aa821a | ||
|
|
cec1ffb80b | ||
|
|
870967ca77 | ||
|
|
66fc0f554a | ||
|
|
d6e4c29733 | ||
|
|
3e42969979 | ||
|
|
712ac505e0 | ||
|
|
f935d3397e | ||
|
|
30dd3a2dbc | ||
|
|
b04f88d581 | ||
|
|
b3c10c8c41 | ||
|
|
5c1bd89a1c | ||
|
|
15f3ab09eb | ||
|
|
c7e834f424 | ||
|
|
8c1a9899d4 | ||
|
|
05caf09a36 | ||
|
|
168a865e60 | ||
|
|
15c17c24fa | ||
|
|
45cf24485d | ||
|
|
c56e2b2e30 | ||
|
|
d1608c597e | ||
|
|
0f4903a5d7 | ||
|
|
772f25d3dd | ||
|
|
f1a1eda7c5 | ||
|
|
41259bff31 | ||
|
|
afaa98a666 | ||
|
|
73be918285 | ||
|
|
ca412214cb | ||
|
|
801722825d | ||
|
|
3abc25fa54 | ||
|
|
f4d67761f1 | ||
|
|
369c5fe3c0 | ||
|
|
7918c6cf11 | ||
|
|
826d70fcba | ||
|
|
bbb23048e3 | ||
|
|
5a3a1fe458 | ||
|
|
56fb06b6f5 | ||
|
|
995a9f7188 | ||
|
|
a0ef046ef1 | ||
|
|
5fa51fc426 | ||
|
|
15057aa0a4 | ||
|
|
1431b52e76 | ||
|
|
98fe770cab | ||
|
|
43382ce5a2 | ||
|
|
abfebf1e21 | ||
|
|
b1c02c7f00 | ||
|
|
84df3ac267 | ||
|
|
55a70c9719 | ||
|
|
0d50b87aa4 | ||
|
|
19290a8e92 | ||
|
|
d845963349 | ||
|
|
8dae4657a1 | ||
|
|
ca7be1cfeb | ||
|
|
445872a6e2 | ||
|
|
3681d025ac | ||
|
|
2998049f51 | ||
|
|
24ccc7c038 | ||
|
|
be48c4d91e | ||
|
|
a19f9056f3 | ||
|
|
5b71ab780c | ||
|
|
481bafd581 | ||
|
|
e74b250d71 | ||
|
|
cd54c95ee1 | ||
|
|
bf006eeaf5 | ||
|
|
b2afc410fa | ||
|
|
19cfa32393 | ||
|
|
27ae8432b6 | ||
|
|
415ee250b5 |
40
fallback.c
40
fallback.c
@@ -3,7 +3,7 @@
|
||||
** TecCGraf - PUC-Rio
|
||||
*/
|
||||
|
||||
char *rcs_fallback="$Id: fallback.c,v 1.16 1995/10/17 11:52:38 roberto Exp roberto $";
|
||||
char *rcs_fallback="$Id: fallback.c,v 1.22 1996/03/19 22:28:37 roberto Exp roberto $";
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@@ -36,8 +36,10 @@ struct FB luaI_fallBacks[] = {
|
||||
{"concat", {LUA_T_CFUNCTION, {concatFB}}, 2, 1},
|
||||
{"settable", {LUA_T_CFUNCTION, {gettableFB}}, 3, 0},
|
||||
{"gc", {LUA_T_CFUNCTION, {GDFB}}, 1, 0},
|
||||
{"function", {LUA_T_CFUNCTION, {funcFB}}, -1, -1}
|
||||
{"function", {LUA_T_CFUNCTION, {funcFB}}, -1, -1},
|
||||
/* no fixed number of params or results */
|
||||
{"getglobal", {LUA_T_CFUNCTION, {indexFB}}, 1, 1}
|
||||
/* same default behavior of index FB */
|
||||
};
|
||||
|
||||
#define N_FB (sizeof(luaI_fallBacks)/sizeof(struct FB))
|
||||
@@ -47,7 +49,7 @@ void luaI_setfallback (void)
|
||||
int i;
|
||||
char *name = lua_getstring(lua_getparam(1));
|
||||
lua_Object func = lua_getparam(2);
|
||||
if (name == NULL || !(lua_isfunction(func) || lua_iscfunction(func)))
|
||||
if (name == NULL || !lua_isfunction(func))
|
||||
lua_error("incorrect argument to function `setfallback'");
|
||||
for (i=0; i<N_FB; i++)
|
||||
{
|
||||
@@ -114,14 +116,14 @@ static void funcFB (void)
|
||||
*/
|
||||
|
||||
static Object *lockArray = NULL;
|
||||
static Word lockSize = 0;
|
||||
static int lockSize = 0;
|
||||
|
||||
int luaI_lock (Object *object)
|
||||
{
|
||||
Word i;
|
||||
Word oldSize;
|
||||
int i;
|
||||
int oldSize;
|
||||
if (tag(object) == LUA_T_NIL)
|
||||
return -1;
|
||||
return -1; /* special lock ref for nil */
|
||||
for (i=0; i<lockSize; i++)
|
||||
if (tag(&lockArray[i]) == LUA_T_NIL)
|
||||
{
|
||||
@@ -130,16 +132,7 @@ int luaI_lock (Object *object)
|
||||
}
|
||||
/* no more empty spaces */
|
||||
oldSize = lockSize;
|
||||
if (lockArray == NULL)
|
||||
{
|
||||
lockSize = 10;
|
||||
lockArray = newvector(lockSize, Object);
|
||||
}
|
||||
else
|
||||
{
|
||||
lockSize = 3*oldSize/2 + 5;
|
||||
lockArray = growvector(lockArray, lockSize, Object);
|
||||
}
|
||||
lockSize = growvector(&lockArray, lockSize, Object, lockEM, MAX_WORD);
|
||||
for (i=oldSize; i<lockSize; i++)
|
||||
tag(&lockArray[i]) = LUA_T_NIL;
|
||||
lockArray[oldSize] = *object;
|
||||
@@ -149,19 +142,24 @@ int luaI_lock (Object *object)
|
||||
|
||||
void lua_unlock (int ref)
|
||||
{
|
||||
tag(&lockArray[ref]) = LUA_T_NIL;
|
||||
if (ref >= 0 && ref < lockSize)
|
||||
tag(&lockArray[ref]) = LUA_T_NIL;
|
||||
}
|
||||
|
||||
|
||||
Object *luaI_getlocked (int ref)
|
||||
{
|
||||
return &lockArray[ref];
|
||||
static Object nul = {LUA_T_NIL, {0}};
|
||||
if (ref >= 0 && ref < lockSize)
|
||||
return &lockArray[ref];
|
||||
else
|
||||
return &nul;
|
||||
}
|
||||
|
||||
|
||||
void luaI_travlock (int (*fn)(Object *))
|
||||
{
|
||||
Word i;
|
||||
int i;
|
||||
for (i=0; i<lockSize; i++)
|
||||
fn(&lockArray[i]);
|
||||
}
|
||||
@@ -169,7 +167,7 @@ void luaI_travlock (int (*fn)(Object *))
|
||||
|
||||
char *luaI_travfallbacks (int (*fn)(Object *))
|
||||
{
|
||||
Word i;
|
||||
int i;
|
||||
for (i=0; i<N_FB; i++)
|
||||
if (fn(&luaI_fallBacks[i].function))
|
||||
return luaI_fallBacks[i].kind;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: fallback.h,v 1.9 1995/10/09 13:14:29 roberto Exp roberto $
|
||||
** $Id: fallback.h,v 1.10 1995/10/17 11:52:38 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef fallback_h
|
||||
@@ -23,6 +23,7 @@ extern struct FB {
|
||||
#define FB_SETTABLE 6
|
||||
#define FB_GC 7
|
||||
#define FB_FUNCTION 8
|
||||
#define FB_GETGLOBAL 9
|
||||
|
||||
void luaI_setfallback (void);
|
||||
int luaI_lock (Object *object);
|
||||
|
||||
85
func.c
85
func.c
@@ -1,4 +1,4 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "luadebug.h"
|
||||
#include "table.h"
|
||||
@@ -6,8 +6,26 @@
|
||||
#include "func.h"
|
||||
#include "opcode.h"
|
||||
|
||||
static TFunc *function_root = NULL;
|
||||
|
||||
static TFunc *function_root = NULL;
|
||||
static LocVar *currvars = NULL;
|
||||
static int numcurrvars = 0;
|
||||
static int maxcurrvars = 0;
|
||||
|
||||
|
||||
/*
|
||||
** Initialize TFunc struct
|
||||
*/
|
||||
void luaI_initTFunc (TFunc *f)
|
||||
{
|
||||
f->next = NULL;
|
||||
f->marked = 0;
|
||||
f->size = 0;
|
||||
f->code = NULL;
|
||||
f->lineDefined = 0;
|
||||
f->fileName = NULL;
|
||||
f->locvars = NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
** Insert function in list for GC
|
||||
@@ -24,9 +42,10 @@ void luaI_insertfunction (TFunc *f)
|
||||
/*
|
||||
** Free function
|
||||
*/
|
||||
static void freefunc (TFunc *f)
|
||||
void luaI_freefunc (TFunc *f)
|
||||
{
|
||||
luaI_free (f->code);
|
||||
luaI_free (f->locvars);
|
||||
luaI_free (f);
|
||||
}
|
||||
|
||||
@@ -48,7 +67,7 @@ Long luaI_funccollector (void)
|
||||
function_root = next;
|
||||
else
|
||||
prev->next = next;
|
||||
freefunc (curr);
|
||||
luaI_freefunc (curr);
|
||||
++counter;
|
||||
}
|
||||
else
|
||||
@@ -77,3 +96,61 @@ void lua_funcinfo (lua_Object func, char **filename, int *linedefined)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Stores information to know that variable has been declared in given line
|
||||
*/
|
||||
void luaI_registerlocalvar (TaggedString *varname, int line)
|
||||
{
|
||||
if (numcurrvars >= maxcurrvars)
|
||||
maxcurrvars = growvector(&currvars, maxcurrvars, LocVar, "", MAX_WORD);
|
||||
currvars[numcurrvars].varname = varname;
|
||||
currvars[numcurrvars].line = line;
|
||||
numcurrvars++;
|
||||
}
|
||||
|
||||
/*
|
||||
** Stores information to know that variable has been out of scope in given line
|
||||
*/
|
||||
void luaI_unregisterlocalvar (int line)
|
||||
{
|
||||
luaI_registerlocalvar(NULL, line);
|
||||
}
|
||||
|
||||
/*
|
||||
** Copies "currvars" into a new area and store it in function header.
|
||||
** The values (varname = NULL, line = -1) signal the end of vector.
|
||||
*/
|
||||
void luaI_closelocalvars (TFunc *func)
|
||||
{
|
||||
func->locvars = newvector (numcurrvars+1, LocVar);
|
||||
memcpy (func->locvars, currvars, numcurrvars*sizeof(LocVar));
|
||||
func->locvars[numcurrvars].varname = NULL;
|
||||
func->locvars[numcurrvars].line = -1;
|
||||
numcurrvars = 0; /* prepares for next function */
|
||||
}
|
||||
|
||||
/*
|
||||
** Look for n-esim local variable at line "line" in function "func".
|
||||
** Returns NULL if not found.
|
||||
*/
|
||||
char *luaI_getlocalname (TFunc *func, int local_number, int line)
|
||||
{
|
||||
int count = 0;
|
||||
char *varname = NULL;
|
||||
LocVar *lv = func->locvars;
|
||||
if (lv == NULL)
|
||||
return NULL;
|
||||
for (; lv->line != -1 && lv->line < line; lv++)
|
||||
{
|
||||
if (lv->varname) /* register */
|
||||
{
|
||||
if (++count == local_number)
|
||||
varname = lv->varname->str;
|
||||
}
|
||||
else /* unregister */
|
||||
if (--count < local_number)
|
||||
varname = NULL;
|
||||
}
|
||||
return varname;
|
||||
}
|
||||
|
||||
|
||||
24
func.h
24
func.h
@@ -1,24 +1,44 @@
|
||||
/*
|
||||
** $Id: func.h,v 1.7 1996/03/08 12:04:04 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef func_h
|
||||
#define func_h
|
||||
|
||||
#include "types.h"
|
||||
#include "lua.h"
|
||||
#include "tree.h"
|
||||
|
||||
typedef struct LocVar
|
||||
{
|
||||
TaggedString *varname; /* NULL signals end of scope */
|
||||
int line;
|
||||
} LocVar;
|
||||
|
||||
|
||||
/*
|
||||
** Header para funcoes.
|
||||
** Function Headers
|
||||
*/
|
||||
typedef struct TFunc
|
||||
{
|
||||
struct TFunc *next;
|
||||
char marked;
|
||||
int marked;
|
||||
int size;
|
||||
Byte *code;
|
||||
int lineDefined;
|
||||
char *fileName;
|
||||
LocVar *locvars;
|
||||
} TFunc;
|
||||
|
||||
Long luaI_funccollector (void);
|
||||
void luaI_insertfunction (TFunc *f);
|
||||
|
||||
void luaI_initTFunc (TFunc *f);
|
||||
void luaI_freefunc (TFunc *f);
|
||||
|
||||
void luaI_registerlocalvar (TaggedString *varname, int line);
|
||||
void luaI_unregisterlocalvar (int line);
|
||||
void luaI_closelocalvars (TFunc *func);
|
||||
char *luaI_getlocalname (TFunc *func, int local_number, int line);
|
||||
|
||||
#endif
|
||||
|
||||
29
hash.c
29
hash.c
@@ -3,9 +3,8 @@
|
||||
** hash manager for lua
|
||||
*/
|
||||
|
||||
char *rcs_hash="$Id: hash.c,v 2.25 1995/05/02 18:43:03 roberto Exp $";
|
||||
char *rcs_hash="$Id: hash.c,v 2.28 1996/02/12 18:32:40 roberto Exp roberto $";
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "mem.h"
|
||||
#include "opcode.h"
|
||||
@@ -13,7 +12,6 @@ char *rcs_hash="$Id: hash.c,v 2.25 1995/05/02 18:43:03 roberto Exp $";
|
||||
#include "table.h"
|
||||
#include "lua.h"
|
||||
|
||||
#define streq(s1,s2) (s1 == s2 || (*(s1) == *(s2) && strcmp(s1,s2)==0))
|
||||
|
||||
#define nhash(t) ((t)->nhash)
|
||||
#define nuse(t) ((t)->nuse)
|
||||
@@ -24,19 +22,18 @@ char *rcs_hash="$Id: hash.c,v 2.25 1995/05/02 18:43:03 roberto Exp $";
|
||||
#define val(n) (&(n)->val)
|
||||
|
||||
|
||||
#define REHASH_LIMIT 0.70 /* avoid more than this % full */
|
||||
#define REHASH_LIMIT 0.70 /* avoid more than this % full */
|
||||
|
||||
|
||||
static Hash *listhead = NULL;
|
||||
|
||||
|
||||
|
||||
/* hash dimensions values */
|
||||
static Word dimensions[] =
|
||||
{3, 5, 7, 11, 23, 47, 97, 197, 397, 797, 1597, 3203, 6421,
|
||||
12853, 25717, 51437, 65521, 0}; /* 65521 == last prime < MAX_WORD */
|
||||
|
||||
static Word redimension (Word nhash)
|
||||
Word luaI_redimension (Word nhash)
|
||||
{
|
||||
Word i;
|
||||
for (i=0; dimensions[i]!=0; i++)
|
||||
@@ -58,17 +55,7 @@ static Word hashindex (Hash *t, Object *ref) /* hash function */
|
||||
case LUA_T_NUMBER:
|
||||
return (((Word)nvalue(ref))%nhash(t));
|
||||
case LUA_T_STRING:
|
||||
{
|
||||
unsigned long h = tsvalue(ref)->hash;
|
||||
if (h == 0)
|
||||
{
|
||||
char *name = svalue(ref);
|
||||
while (*name)
|
||||
h = ((h<<5)-h)^(unsigned char)*(name++);
|
||||
tsvalue(ref)->hash = h;
|
||||
}
|
||||
return (Word)h%nhash(t); /* make it a valid index */
|
||||
}
|
||||
return (Word)((tsvalue(ref)->hash)%nhash(t)); /* make it a valid index */
|
||||
case LUA_T_FUNCTION:
|
||||
return (((IntPoint)ref->value.tf)%nhash(t));
|
||||
case LUA_T_CFUNCTION:
|
||||
@@ -80,14 +67,14 @@ static Word hashindex (Hash *t, Object *ref) /* hash function */
|
||||
}
|
||||
}
|
||||
|
||||
Bool lua_equalObj (Object *t1, Object *t2)
|
||||
int lua_equalObj (Object *t1, Object *t2)
|
||||
{
|
||||
if (tag(t1) != tag(t2)) return 0;
|
||||
switch (tag(t1))
|
||||
{
|
||||
case LUA_T_NIL: return 1;
|
||||
case LUA_T_NUMBER: return nvalue(t1) == nvalue(t2);
|
||||
case LUA_T_STRING: return streq(svalue(t1), svalue(t2));
|
||||
case LUA_T_STRING: return svalue(t1) == svalue(t2);
|
||||
case LUA_T_ARRAY: return avalue(t1) == avalue(t2);
|
||||
case LUA_T_FUNCTION: return t1->value.tf == t2->value.tf;
|
||||
case LUA_T_CFUNCTION: return fvalue(t1) == fvalue(t2);
|
||||
@@ -126,7 +113,7 @@ static Node *hashnodecreate (Word nhash)
|
||||
static Hash *hashcreate (Word nhash)
|
||||
{
|
||||
Hash *t = new(Hash);
|
||||
nhash = redimension((Word)((float)nhash/REHASH_LIMIT));
|
||||
nhash = luaI_redimension((Word)((float)nhash/REHASH_LIMIT));
|
||||
nodevector(t) = hashnodecreate(nhash);
|
||||
nhash(t) = nhash;
|
||||
nuse(t) = 0;
|
||||
@@ -237,7 +224,7 @@ static void rehash (Hash *t)
|
||||
Word i;
|
||||
Word nold = nhash(t);
|
||||
Node *vold = nodevector(t);
|
||||
nhash(t) = redimension(nhash(t));
|
||||
nhash(t) = luaI_redimension(nhash(t));
|
||||
nodevector(t) = hashnodecreate(nhash(t));
|
||||
for (i=0; i<nold; i++)
|
||||
{
|
||||
|
||||
7
hash.h
7
hash.h
@@ -1,14 +1,14 @@
|
||||
/*
|
||||
** hash.h
|
||||
** hash manager for lua
|
||||
** Luiz Henrique de Figueiredo - 17 Aug 90
|
||||
** $Id: hash.h,v 2.7 1994/12/20 21:20:36 roberto Exp roberto $
|
||||
** $Id: hash.h,v 2.10 1996/02/12 18:32:40 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef hash_h
|
||||
#define hash_h
|
||||
|
||||
#include "types.h"
|
||||
#include "opcode.h"
|
||||
|
||||
typedef struct node
|
||||
{
|
||||
@@ -26,7 +26,8 @@ typedef struct Hash
|
||||
} Hash;
|
||||
|
||||
|
||||
Bool lua_equalObj (Object *t1, Object *t2);
|
||||
int lua_equalObj (Object *t1, Object *t2);
|
||||
Word luaI_redimension (Word nhash);
|
||||
Hash *lua_createarray (Word nhash);
|
||||
void lua_hashmark (Hash *h);
|
||||
Long lua_hashcollector (void);
|
||||
|
||||
130
inout.c
130
inout.c
@@ -5,31 +5,21 @@
|
||||
** Also provides some predefined lua functions.
|
||||
*/
|
||||
|
||||
char *rcs_inout="$Id: inout.c,v 2.24 1995/10/23 13:54:11 roberto Exp roberto $";
|
||||
char *rcs_inout="$Id: inout.c,v 2.35 1996/03/19 16:50:24 roberto Exp roberto $";
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "mem.h"
|
||||
#include "lex.h"
|
||||
#include "opcode.h"
|
||||
#include "hash.h"
|
||||
#include "inout.h"
|
||||
#include "table.h"
|
||||
#include "tree.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
#ifndef MAXFUNCSTACK
|
||||
#define MAXFUNCSTACK 100
|
||||
#endif
|
||||
|
||||
#define MAXMESSAGE MAXFUNCSTACK*80
|
||||
#include "mem.h"
|
||||
|
||||
|
||||
/* Exported variables */
|
||||
Word lua_linenumber;
|
||||
Bool lua_debug = 0;
|
||||
char *lua_parsedfile;
|
||||
|
||||
|
||||
@@ -54,9 +44,9 @@ static int stringinput (void)
|
||||
|
||||
/*
|
||||
** Function to open a file to be input unit.
|
||||
** Return 0 on success or error message on error.
|
||||
** Return the file.
|
||||
*/
|
||||
char *lua_openfile (char *fn)
|
||||
FILE *lua_openfile (char *fn)
|
||||
{
|
||||
lua_setinput (fileinput);
|
||||
if (fn == NULL)
|
||||
@@ -67,14 +57,10 @@ char *lua_openfile (char *fn)
|
||||
else
|
||||
fp = fopen (fn, "r");
|
||||
if (fp == NULL)
|
||||
{
|
||||
static char buff[255];
|
||||
sprintf(buff, "unable to open file `%.200s'", fn);
|
||||
return buff;
|
||||
}
|
||||
return NULL;
|
||||
lua_linenumber = 1;
|
||||
lua_parsedfile = lua_constcreate(fn)->ts.str;
|
||||
return NULL;
|
||||
lua_parsedfile = luaI_createfixedstring(fn)->str;
|
||||
return fp;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -97,7 +83,7 @@ void lua_openstring (char *s)
|
||||
lua_setinput (stringinput);
|
||||
st = s;
|
||||
lua_linenumber = 1;
|
||||
lua_parsedfile = lua_constcreate("(string)")->ts.str;
|
||||
lua_parsedfile = luaI_createfixedstring("(string)")->str;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -126,33 +112,57 @@ void lua_internaldostring (void)
|
||||
void lua_internaldofile (void)
|
||||
{
|
||||
lua_Object obj = lua_getparam (1);
|
||||
if (lua_isstring(obj) && !lua_dofile(lua_getstring(obj)))
|
||||
char *fname = NULL;
|
||||
if (lua_isstring(obj))
|
||||
fname = lua_getstring(obj);
|
||||
else if (obj != LUA_NOOBJECT)
|
||||
lua_error("invalid argument to function `dofile'");
|
||||
/* else fname = NULL */
|
||||
if (!lua_dofile(fname))
|
||||
lua_pushnumber(1);
|
||||
else
|
||||
lua_pushnil();
|
||||
}
|
||||
|
||||
/*
|
||||
** Internal function: print object values
|
||||
*/
|
||||
void lua_print (void)
|
||||
|
||||
static char *tostring (lua_Object obj)
|
||||
{
|
||||
int i=1;
|
||||
lua_Object obj;
|
||||
while ((obj=lua_getparam (i++)) != LUA_NOOBJECT)
|
||||
{
|
||||
if (lua_isnumber(obj)) printf("%g\n",lua_getnumber(obj));
|
||||
else if (lua_isstring(obj)) printf("%s\n",lua_getstring(obj));
|
||||
else if (lua_isfunction(obj)) printf("function: %p\n",(luaI_Address(obj))->value.tf);
|
||||
else if (lua_iscfunction(obj)) printf("cfunction: %p\n",lua_getcfunction(obj)
|
||||
);
|
||||
else if (lua_isuserdata(obj)) printf("userdata: %p\n",lua_getuserdata(obj));
|
||||
else if (lua_istable(obj)) printf("table: %p\n",avalue(luaI_Address(obj)));
|
||||
else if (lua_isnil(obj)) printf("nil\n");
|
||||
else printf("invalid value to print\n");
|
||||
}
|
||||
char *buff = luaI_buffer(20);
|
||||
if (lua_isstring(obj)) /* get strings and numbers */
|
||||
return lua_getstring(obj);
|
||||
else switch(lua_type(obj))
|
||||
{
|
||||
case LUA_T_FUNCTION:
|
||||
sprintf(buff, "function: %p", (luaI_Address(obj))->value.tf);
|
||||
break;
|
||||
case LUA_T_CFUNCTION:
|
||||
sprintf(buff, "cfunction: %p", lua_getcfunction(obj));
|
||||
break;
|
||||
case LUA_T_ARRAY:
|
||||
sprintf(buff, "table: %p", avalue(luaI_Address(obj)));
|
||||
break;
|
||||
case LUA_T_NIL:
|
||||
sprintf(buff, "nil");
|
||||
break;
|
||||
default:
|
||||
sprintf(buff, "userdata: %p", lua_getuserdata(obj));
|
||||
break;
|
||||
}
|
||||
return buff;
|
||||
}
|
||||
|
||||
void luaI_tostring (void)
|
||||
{
|
||||
lua_pushstring(tostring(lua_getparam(1)));
|
||||
}
|
||||
|
||||
void luaI_print (void)
|
||||
{
|
||||
int i = 1;
|
||||
lua_Object obj;
|
||||
while ((obj = lua_getparam(i++)) != LUA_NOOBJECT)
|
||||
printf("%s\n", tostring(obj));
|
||||
}
|
||||
|
||||
/*
|
||||
** Internal function: return an object type.
|
||||
@@ -196,16 +206,7 @@ void lua_obj2number (void)
|
||||
{
|
||||
lua_Object o = lua_getparam(1);
|
||||
if (lua_isnumber(o))
|
||||
lua_pushobject(o);
|
||||
else if (lua_isstring(o))
|
||||
{
|
||||
char c;
|
||||
float f;
|
||||
if (sscanf(lua_getstring(o),"%f %c",&f,&c) == 1)
|
||||
lua_pushnumber(f);
|
||||
else
|
||||
lua_pushnil();
|
||||
}
|
||||
lua_pushnumber(lua_getnumber(o));
|
||||
else
|
||||
lua_pushnil();
|
||||
}
|
||||
@@ -218,3 +219,28 @@ void luaI_error (void)
|
||||
lua_error(s);
|
||||
}
|
||||
|
||||
void luaI_assert (void)
|
||||
{
|
||||
lua_Object p = lua_getparam(1);
|
||||
if (p == LUA_NOOBJECT || lua_isnil(p))
|
||||
lua_error("assertion failed!");
|
||||
}
|
||||
|
||||
void luaI_setglobal (void)
|
||||
{
|
||||
lua_Object name = lua_getparam(1);
|
||||
lua_Object value = lua_getparam(2);
|
||||
if (!lua_isstring(name))
|
||||
lua_error("incorrect argument to function `setglobal'");
|
||||
lua_pushobject(value);
|
||||
lua_storeglobal(lua_getstring(name));
|
||||
lua_pushobject(value); /* return given value */
|
||||
}
|
||||
|
||||
void luaI_getglobal (void)
|
||||
{
|
||||
lua_Object name = lua_getparam(1);
|
||||
if (!lua_isstring(name))
|
||||
lua_error("incorrect argument to function `getglobal'");
|
||||
lua_pushobject(lua_getglobal(lua_getstring(name)));
|
||||
}
|
||||
|
||||
12
inout.h
12
inout.h
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: inout.h,v 1.9 1995/05/16 17:23:58 roberto Exp roberto $
|
||||
** $Id: inout.h,v 1.14 1996/03/15 13:13:13 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
|
||||
@@ -7,23 +7,27 @@
|
||||
#define inout_h
|
||||
|
||||
#include "types.h"
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
extern Word lua_linenumber;
|
||||
extern Bool lua_debug;
|
||||
extern Word lua_debugline;
|
||||
extern char *lua_parsedfile;
|
||||
|
||||
char *lua_openfile (char *fn);
|
||||
FILE *lua_openfile (char *fn);
|
||||
void lua_closefile (void);
|
||||
void lua_openstring (char *s);
|
||||
void lua_closestring (void);
|
||||
|
||||
void lua_internaldofile (void);
|
||||
void lua_internaldostring (void);
|
||||
void lua_print (void);
|
||||
void luaI_tostring (void);
|
||||
void luaI_print (void);
|
||||
void luaI_type (void);
|
||||
void lua_obj2number (void);
|
||||
void luaI_error (void);
|
||||
void luaI_assert (void);
|
||||
void luaI_setglobal (void);
|
||||
void luaI_getglobal (void);
|
||||
|
||||
#endif
|
||||
|
||||
190
iolib.c
190
iolib.c
@@ -3,7 +3,7 @@
|
||||
** Input/output library to LUA
|
||||
*/
|
||||
|
||||
char *rcs_iolib="$Id: iolib.c,v 1.28 1995/11/10 17:55:48 roberto Exp roberto $";
|
||||
char *rcs_iolib="$Id: iolib.c,v 1.39 1996/03/14 15:55:18 roberto Exp roberto $";
|
||||
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
@@ -12,6 +12,7 @@ char *rcs_iolib="$Id: iolib.c,v 1.28 1995/11/10 17:55:48 roberto Exp roberto $";
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
@@ -29,6 +30,14 @@ int pclose();
|
||||
#endif
|
||||
|
||||
|
||||
static void pushresult (int i)
|
||||
{
|
||||
if (i)
|
||||
lua_pushnumber (1);
|
||||
else
|
||||
lua_pushnil();
|
||||
}
|
||||
|
||||
static void closeread (void)
|
||||
{
|
||||
if (in != stdin)
|
||||
@@ -142,15 +151,14 @@ static char getformat (char *f, int *just, int *m, int *n)
|
||||
int t;
|
||||
switch (*f++)
|
||||
{
|
||||
case 'q': case 'Q':
|
||||
case 's': case 'S':
|
||||
t = 's';
|
||||
case 'i': case 'I':
|
||||
t = tolower(*(f-1));
|
||||
break;
|
||||
case 'f': case 'F': case 'g': case 'G': case 'e': case 'E':
|
||||
t = 'f';
|
||||
break;
|
||||
case 'i': case 'I':
|
||||
t = 'i';
|
||||
break;
|
||||
default:
|
||||
t = 0; /* to avoid compiler warnings */
|
||||
lua_arg_error("read/write (format)");
|
||||
@@ -177,32 +185,6 @@ static char getformat (char *f, int *just, int *m, int *n)
|
||||
}
|
||||
|
||||
|
||||
static char *add_char (int c)
|
||||
{
|
||||
static char *buff = NULL;
|
||||
static int max = 0;
|
||||
static int n = 0;
|
||||
if (n >= max)
|
||||
{
|
||||
if (max == 0)
|
||||
{
|
||||
max = 100;
|
||||
buff = (char *)malloc(max);
|
||||
}
|
||||
else
|
||||
{
|
||||
max *= 2;
|
||||
buff = (char *)realloc(buff, max);
|
||||
}
|
||||
if (buff == NULL)
|
||||
lua_error("memory overflow");
|
||||
}
|
||||
buff[n++] = c;
|
||||
if (c == 0)
|
||||
n = 0; /* prepare for next string */
|
||||
return buff;
|
||||
}
|
||||
|
||||
/*
|
||||
** Read a variable. On error put nil on stack.
|
||||
** LUA interface:
|
||||
@@ -222,23 +204,23 @@ static int read_until_char (int del)
|
||||
{
|
||||
int c;
|
||||
while((c = fgetc(in)) != EOF && c != del)
|
||||
add_char(c);
|
||||
luaI_addchar(c);
|
||||
return c;
|
||||
}
|
||||
|
||||
static int read_until_blank (void)
|
||||
static void read_until_blank (void)
|
||||
{
|
||||
int c;
|
||||
while((c = fgetc(in)) != EOF && !isspace(c))
|
||||
add_char(c);
|
||||
return c;
|
||||
luaI_addchar(c);
|
||||
if (c != EOF) ungetc(c,in);
|
||||
}
|
||||
|
||||
static void read_m (int m)
|
||||
{
|
||||
int c;
|
||||
while (m-- && (c = fgetc(in)) != EOF)
|
||||
add_char(c);
|
||||
luaI_addchar(c);
|
||||
}
|
||||
|
||||
|
||||
@@ -256,21 +238,18 @@ static void read_free (void)
|
||||
{ /* string */
|
||||
c = read_until_char(c);
|
||||
if (c == EOF)
|
||||
{
|
||||
add_char(0); /* to be ready for next time */
|
||||
lua_pushnil();
|
||||
}
|
||||
else
|
||||
lua_pushstring(add_char(0));
|
||||
lua_pushstring(luaI_addchar(0));
|
||||
}
|
||||
else
|
||||
{
|
||||
double d;
|
||||
char dummy;
|
||||
char *s;
|
||||
add_char(c);
|
||||
luaI_addchar(c);
|
||||
read_until_blank();
|
||||
s = add_char(0);
|
||||
s = luaI_addchar(0);
|
||||
if (sscanf(s, "%lf %c", &d, &dummy) == 1)
|
||||
lua_pushnumber(d);
|
||||
else
|
||||
@@ -281,6 +260,7 @@ static void read_free (void)
|
||||
static void io_read (void)
|
||||
{
|
||||
lua_Object o = lua_getparam (1);
|
||||
luaI_addchar(0); /* initialize buffer */
|
||||
if (o == LUA_NOOBJECT) /* free format */
|
||||
read_free();
|
||||
else /* formatted */
|
||||
@@ -289,12 +269,19 @@ static void io_read (void)
|
||||
switch (getformat(lua_check_string(1, "read"), &dummy1, &m, &dummy2))
|
||||
{
|
||||
case 's':
|
||||
{
|
||||
char *s;
|
||||
if (m < 0)
|
||||
read_until_blank();
|
||||
else
|
||||
read_m(m);
|
||||
lua_pushstring(add_char(0));
|
||||
s = luaI_addchar(0);
|
||||
if ((m >= 0 && strlen(s) == m) || (m < 0 && strlen(s) > 0))
|
||||
lua_pushstring(s);
|
||||
else
|
||||
lua_pushnil();
|
||||
break;
|
||||
}
|
||||
|
||||
case 'i': /* can read as float, since it makes no difference to Lua */
|
||||
case 'f':
|
||||
@@ -306,7 +293,7 @@ static void io_read (void)
|
||||
else
|
||||
{
|
||||
read_m(m);
|
||||
result = sscanf(add_char(0), "%lf", &d);
|
||||
result = sscanf(luaI_addchar(0), "%lf", &d);
|
||||
}
|
||||
if (result == 1)
|
||||
lua_pushnumber(d);
|
||||
@@ -314,6 +301,8 @@ static void io_read (void)
|
||||
lua_pushnil();
|
||||
break;
|
||||
}
|
||||
default:
|
||||
lua_arg_error("read (format)");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -324,10 +313,16 @@ static void io_read (void)
|
||||
*/
|
||||
static void io_readuntil (void)
|
||||
{
|
||||
int del = *lua_check_string(1, "readuntil");
|
||||
int c = read_until_char(del);
|
||||
int del, c;
|
||||
lua_Object p = lua_getparam(1);
|
||||
luaI_addchar(0); /* initialize buffer */
|
||||
if (p == LUA_NOOBJECT || lua_isnil(p))
|
||||
del = EOF;
|
||||
else
|
||||
del = *lua_check_string(1, "readuntil");
|
||||
c = read_until_char(del);
|
||||
if (c != EOF) ungetc(c,in);
|
||||
lua_pushstring(add_char(0));
|
||||
lua_pushstring(luaI_addchar(0));
|
||||
}
|
||||
|
||||
|
||||
@@ -384,6 +379,13 @@ static int write_string (char *s, int just, int m)
|
||||
return status;
|
||||
}
|
||||
|
||||
static int write_quoted (int just, int m)
|
||||
{
|
||||
luaI_addchar(0);
|
||||
luaI_addquoted(lua_check_string(1, "write"));
|
||||
return write_string(luaI_addchar(0), just, m);
|
||||
}
|
||||
|
||||
static int write_float (int just, int m, int n)
|
||||
{
|
||||
char buffer[100];
|
||||
@@ -420,15 +422,16 @@ static void io_write (void)
|
||||
if (lua_getparam (2) == LUA_NOOBJECT) /* free format */
|
||||
{
|
||||
lua_Object o1 = lua_getparam(1);
|
||||
if (lua_isnumber(o1))
|
||||
int t = lua_type(o1);
|
||||
if (t == LUA_T_NUMBER)
|
||||
status = fprintf (out, "%g", lua_getnumber(o1)) >= 0;
|
||||
else if (lua_isstring(o1))
|
||||
else if (t == LUA_T_STRING)
|
||||
status = fprintf (out, "%s", lua_getstring(o1)) >= 0;
|
||||
}
|
||||
else /* formated */
|
||||
{
|
||||
int just, m, n;
|
||||
switch (getformat (lua_check_string(2, "write"), &just, &m, &n))
|
||||
switch (getformat(lua_check_string(2, "write"), &just, &m, &n))
|
||||
{
|
||||
case 's':
|
||||
{
|
||||
@@ -439,6 +442,9 @@ static void io_write (void)
|
||||
status = 0;
|
||||
break;
|
||||
}
|
||||
case 'q':
|
||||
status = write_quoted(just, m);
|
||||
break;
|
||||
case 'f':
|
||||
status = write_float(just, m, n);
|
||||
break;
|
||||
@@ -467,59 +473,54 @@ static void io_execute (void)
|
||||
*/
|
||||
static void io_remove (void)
|
||||
{
|
||||
if (remove(lua_check_string(1, "remove")) == 0)
|
||||
lua_pushnumber (1);
|
||||
else
|
||||
lua_pushnil();
|
||||
pushresult(remove(lua_check_string(1, "remove")) == 0);
|
||||
}
|
||||
|
||||
static void io_rename (void)
|
||||
{
|
||||
char *f1 = lua_check_string(1, "rename");
|
||||
char *f2 = lua_check_string(2, "rename");
|
||||
pushresult(rename(f1, f2) == 0);
|
||||
}
|
||||
|
||||
static void io_tmpname (void)
|
||||
{
|
||||
lua_pushstring(tmpnam(NULL));
|
||||
}
|
||||
|
||||
static void io_errorno (void)
|
||||
{
|
||||
/* lua_pushstring(strerror(errno));*/
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** To get a environment variables
|
||||
** To get a environment variable
|
||||
*/
|
||||
static void io_getenv (void)
|
||||
{
|
||||
char *env = getenv(lua_check_string(1, "getenv"));
|
||||
if (env == NULL) lua_pushnil();
|
||||
else lua_pushstring(env);
|
||||
lua_pushstring(env); /* if NULL push nil */
|
||||
}
|
||||
|
||||
/*
|
||||
** Return time: hour, min, sec
|
||||
*/
|
||||
static void io_time (void)
|
||||
{
|
||||
time_t t;
|
||||
struct tm *s;
|
||||
|
||||
time(&t);
|
||||
s = localtime(&t);
|
||||
lua_pushnumber(s->tm_hour);
|
||||
lua_pushnumber(s->tm_min);
|
||||
lua_pushnumber(s->tm_sec);
|
||||
}
|
||||
|
||||
/*
|
||||
** Return date: dd, mm, yyyy
|
||||
** Return user formatted time stamp
|
||||
*/
|
||||
static void io_date (void)
|
||||
{
|
||||
time_t t;
|
||||
struct tm *s;
|
||||
|
||||
time(&t);
|
||||
s = localtime(&t);
|
||||
lua_pushnumber(s->tm_mday);
|
||||
lua_pushnumber(s->tm_mon+1);
|
||||
lua_pushnumber(s->tm_year+1900);
|
||||
}
|
||||
|
||||
/*
|
||||
** Beep
|
||||
*/
|
||||
static void io_beep (void)
|
||||
{
|
||||
printf("\a");
|
||||
struct tm *tm;
|
||||
char *s;
|
||||
char b[BUFSIZ];
|
||||
if (lua_getparam(1) == LUA_NOOBJECT)
|
||||
s = "%c";
|
||||
else
|
||||
s = lua_check_string(1, "date");
|
||||
time(&t); tm = localtime(&t);
|
||||
if (strftime(b,sizeof(b),s,tm))
|
||||
lua_pushstring(b);
|
||||
else
|
||||
lua_error("invalid `date' format");
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -543,7 +544,7 @@ static void io_debug (void)
|
||||
{
|
||||
char buffer[250];
|
||||
fprintf(stderr, "lua_debug> ");
|
||||
if (gets(buffer) == 0) return;
|
||||
if (fgets(buffer, sizeof(buffer), stdin) == 0) return;
|
||||
if (strcmp(buffer, "cont") == 0) return;
|
||||
lua_dostring(buffer);
|
||||
}
|
||||
@@ -560,13 +561,13 @@ void lua_printstack (FILE *f)
|
||||
char *name;
|
||||
int currentline;
|
||||
fprintf(f, "\t");
|
||||
switch (*getobjname(func, &name))
|
||||
switch (*lua_getobjname(func, &name))
|
||||
{
|
||||
case 'g':
|
||||
fprintf(f, "function %s", name);
|
||||
break;
|
||||
case 'f':
|
||||
fprintf(f, "fallback %s", name);
|
||||
fprintf(f, "`%s' fallback", name);
|
||||
break;
|
||||
default:
|
||||
{
|
||||
@@ -582,7 +583,7 @@ void lua_printstack (FILE *f)
|
||||
}
|
||||
}
|
||||
if ((currentline = lua_currentline(func)) > 0)
|
||||
fprintf(f, " at line %d", currentline);
|
||||
fprintf(f, " at line %d", currentline);
|
||||
fprintf(f, "\n");
|
||||
}
|
||||
}
|
||||
@@ -610,10 +611,11 @@ void iolib_open (void)
|
||||
lua_register ("write", io_write);
|
||||
lua_register ("execute", io_execute);
|
||||
lua_register ("remove", io_remove);
|
||||
lua_register ("rename", io_rename);
|
||||
lua_register ("tmpname", io_tmpname);
|
||||
lua_register ("ioerror", io_errorno);
|
||||
lua_register ("getenv", io_getenv);
|
||||
lua_register ("time", io_time);
|
||||
lua_register ("date", io_date);
|
||||
lua_register ("beep", io_beep);
|
||||
lua_register ("exit", io_exit);
|
||||
lua_register ("debug", io_debug);
|
||||
lua_register ("print_stack", errorfb);
|
||||
|
||||
91
lex.c
91
lex.c
@@ -1,23 +1,19 @@
|
||||
char *rcs_lex = "$Id: lex.c,v 2.20 1995/10/25 13:05:51 roberto Exp roberto $";
|
||||
char *rcs_lex = "$Id: lex.c,v 2.31 1996/03/19 16:50:24 roberto Exp roberto $";
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "mem.h"
|
||||
#include "tree.h"
|
||||
#include "table.h"
|
||||
#include "opcode.h"
|
||||
#include "lex.h"
|
||||
#include "inout.h"
|
||||
#include "luadebug.h"
|
||||
#include "parser.h"
|
||||
#include "ugly.h"
|
||||
|
||||
#define MINBUFF 260
|
||||
|
||||
#define lua_strcmp(a,b) (a[0]<b[0]?(-1):(a[0]>b[0]?(1):strcmp(a,b)))
|
||||
|
||||
#define next() { current = input(); }
|
||||
#define save(x) { *yytextLast++ = (x); }
|
||||
#define save_and_next() { save(current); next(); }
|
||||
@@ -47,7 +43,6 @@ char *lua_lasttext (void)
|
||||
}
|
||||
|
||||
|
||||
/* The reserved words must be listed in lexicographic order */
|
||||
static struct
|
||||
{
|
||||
char *name;
|
||||
@@ -74,30 +69,21 @@ static struct
|
||||
#define RESERVEDSIZE (sizeof(reserved)/sizeof(reserved[0]))
|
||||
|
||||
|
||||
static int findReserved (char *name)
|
||||
void luaI_addReserved (void)
|
||||
{
|
||||
int l = 0;
|
||||
int h = RESERVEDSIZE - 1;
|
||||
while (l <= h)
|
||||
int i;
|
||||
for (i=0; i<RESERVEDSIZE; i++)
|
||||
{
|
||||
int m = (l+h)/2;
|
||||
int comp = lua_strcmp(name, reserved[m].name);
|
||||
if (comp < 0)
|
||||
h = m-1;
|
||||
else if (comp == 0)
|
||||
return reserved[m].token;
|
||||
else
|
||||
l = m+1;
|
||||
TaggedString *ts = lua_createstring(reserved[i].name);
|
||||
ts->marked = reserved[i].token; /* reserved word (always > 255) */
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static void growtext (void)
|
||||
{
|
||||
int size = yytextLast - yytext;
|
||||
textsize *= 2;
|
||||
yytext = growvector(yytext, textsize, char);
|
||||
textsize = growvector(&yytext, textsize, char, lexEM, MAX_WORD);
|
||||
yytextLast = yytext + size;
|
||||
}
|
||||
|
||||
@@ -144,9 +130,9 @@ static int read_long_string (void)
|
||||
}
|
||||
|
||||
|
||||
int yylex (void)
|
||||
int luaY_lex (void)
|
||||
{
|
||||
float a;
|
||||
double a;
|
||||
static int linelasttoken = 0;
|
||||
if (lua_debug)
|
||||
luaI_codedebugline(linelasttoken);
|
||||
@@ -154,9 +140,6 @@ int yylex (void)
|
||||
while (1)
|
||||
{
|
||||
yytextLast = yytext;
|
||||
#if 0
|
||||
fprintf(stderr,"'%c' %d\n",current,current);
|
||||
#endif
|
||||
switch (current)
|
||||
{
|
||||
case EOF:
|
||||
@@ -174,22 +157,21 @@ int yylex (void)
|
||||
while (isalnum(current) || current == '_')
|
||||
save_and_next();
|
||||
*yytextLast = 0;
|
||||
if (lua_strcmp(yytext, "debug") == 0)
|
||||
if (strcmp(yytext, "debug") == 0)
|
||||
{
|
||||
yylval.vInt = 1;
|
||||
luaY_lval.vInt = 1;
|
||||
return DEBUG;
|
||||
}
|
||||
else if (lua_strcmp(yytext, "nodebug") == 0)
|
||||
else if (strcmp(yytext, "nodebug") == 0)
|
||||
{
|
||||
yylval.vInt = 0;
|
||||
luaY_lval.vInt = 0;
|
||||
return DEBUG;
|
||||
}
|
||||
return WRONGTOKEN;
|
||||
|
||||
case '-':
|
||||
save_and_next();
|
||||
if (current != '-') return '-'; /* else goes through */
|
||||
case '#':
|
||||
if (current != '-') return '-';
|
||||
do { next(); } while (current != '\n' && current != 0);
|
||||
continue;
|
||||
|
||||
@@ -203,7 +185,7 @@ int yylex (void)
|
||||
return WRONGTOKEN;
|
||||
save_and_next(); /* pass the second ']' */
|
||||
*(yytextLast-2) = 0; /* erases ']]' */
|
||||
yylval.vWord = luaI_findconstantbyname(yytext+2);
|
||||
luaY_lval.vWord = luaI_findconstantbyname(yytext+2);
|
||||
return STRING;
|
||||
}
|
||||
|
||||
@@ -254,6 +236,7 @@ int yylex (void)
|
||||
case 'n': save('\n'); next(); break;
|
||||
case 't': save('\t'); next(); break;
|
||||
case 'r': save('\r'); next(); break;
|
||||
case '\n': lua_linenumber++; /* goes through */
|
||||
default : save(current); next(); break;
|
||||
}
|
||||
break;
|
||||
@@ -263,7 +246,7 @@ int yylex (void)
|
||||
}
|
||||
next(); /* skip the delimiter */
|
||||
*yytextLast = 0;
|
||||
yylval.vWord = luaI_findconstantbyname(yytext);
|
||||
luaY_lval.vWord = luaI_findconstantbyname(yytext);
|
||||
return STRING;
|
||||
}
|
||||
|
||||
@@ -281,12 +264,14 @@ int yylex (void)
|
||||
case 'Z':
|
||||
case '_':
|
||||
{
|
||||
Word res;
|
||||
TaggedString *ts;
|
||||
do { save_and_next(); } while (isalnum(current) || current == '_');
|
||||
*yytextLast = 0;
|
||||
res = findReserved(yytext);
|
||||
if (res) return res;
|
||||
yylval.pNode = lua_constcreate(yytext);
|
||||
ts = lua_createstring(yytext);
|
||||
if (ts->marked > 2)
|
||||
return ts->marked; /* reserved word */
|
||||
luaY_lval.pTStr = ts;
|
||||
ts->marked = 2; /* avoid GC */
|
||||
return NAME;
|
||||
}
|
||||
|
||||
@@ -305,48 +290,32 @@ int yylex (void)
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9':
|
||||
a=0.0;
|
||||
do { a=10*a+current-'0'; save_and_next(); } while (isdigit(current));
|
||||
do { a=10.0*a+(current-'0'); save_and_next(); } while (isdigit(current));
|
||||
if (current == '.') save_and_next();
|
||||
fraction:
|
||||
{ float da=0.1;
|
||||
{ double da=0.1;
|
||||
while (isdigit(current))
|
||||
{a+=(current-'0')*da; da/=10.0; save_and_next()};
|
||||
if (current == 'e' || current == 'E')
|
||||
{
|
||||
int e=0;
|
||||
int neg;
|
||||
float ea;
|
||||
double ea;
|
||||
save_and_next();
|
||||
neg=(current=='-');
|
||||
if (current == '+' || current == '-') save_and_next();
|
||||
if (!isdigit(current)) return WRONGTOKEN;
|
||||
do { e=10*e+current-'0'; save_and_next(); } while (isdigit(current));
|
||||
do { e=10.0*e+(current-'0'); save_and_next(); } while (isdigit(current));
|
||||
for (ea=neg?0.1:10.0; e>0; e>>=1)
|
||||
{
|
||||
if (e & 1) a*=ea;
|
||||
ea*=ea;
|
||||
}
|
||||
}
|
||||
yylval.vFloat = a;
|
||||
luaY_lval.vFloat = a;
|
||||
return NUMBER;
|
||||
}
|
||||
|
||||
case U_and: case U_do: case U_else: case U_elseif: case U_end:
|
||||
case U_function: case U_if: case U_local: case U_nil: case U_not:
|
||||
case U_or: case U_repeat: case U_return: case U_then:
|
||||
case U_until: case U_while:
|
||||
{
|
||||
int old = current;
|
||||
next();
|
||||
return reserved[old-U_and].token;
|
||||
}
|
||||
|
||||
case U_eq: next(); return EQ;
|
||||
case U_le: next(); return LE;
|
||||
case U_ge: next(); return GE;
|
||||
case U_ne: next(); return NE;
|
||||
case U_sc: next(); return CONC;
|
||||
|
||||
default: /* also end of file */
|
||||
{
|
||||
save_and_next();
|
||||
|
||||
19
lex.h
Normal file
19
lex.h
Normal file
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
** lex.h
|
||||
** TecCGraf - PUC-Rio
|
||||
** $Id: lex.h,v 1.1 1996/02/13 17:30:39 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef lex_h
|
||||
#define lex_h
|
||||
|
||||
|
||||
typedef int (*Input) (void);
|
||||
|
||||
void lua_setinput (Input fn);
|
||||
char *lua_lasttext (void);
|
||||
int luaY_lex (void);
|
||||
void luaI_addReserved (void);
|
||||
|
||||
|
||||
#endif
|
||||
4
lua.c
4
lua.c
@@ -3,7 +3,7 @@
|
||||
** Linguagem para Usuarios de Aplicacao
|
||||
*/
|
||||
|
||||
char *rcs_lua="$Id: lua.c,v 1.6 1995/10/23 13:54:11 roberto Exp roberto $";
|
||||
char *rcs_lua="$Id: lua.c,v 1.7 1995/10/31 17:05:35 roberto Exp roberto $";
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@@ -44,7 +44,7 @@ static void manual_input (void)
|
||||
if (isatty(fileno(stdin)))
|
||||
{
|
||||
char buffer[250];
|
||||
while (gets(buffer) != 0)
|
||||
while (fgets(buffer, sizeof(buffer), stdin) != 0)
|
||||
lua_dostring(buffer);
|
||||
}
|
||||
else
|
||||
|
||||
23
lua.h
23
lua.h
@@ -2,14 +2,14 @@
|
||||
** LUA - Linguagem para Usuarios de Aplicacao
|
||||
** Grupo de Tecnologia em Computacao Grafica
|
||||
** TeCGraf - PUC-Rio
|
||||
** $Id: lua.h,v 3.20 1995/10/31 16:41:53 roberto Exp roberto $
|
||||
** $Id: lua.h,v 3.24 1996/03/19 22:28:37 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
|
||||
#ifndef lua_h
|
||||
#define lua_h
|
||||
|
||||
#define LUA_VERSION "Lua 2.2"
|
||||
#define LUA_VERSION "Lua 2.4 (beta)"
|
||||
#define LUA_COPYRIGHT "Copyright (C) 1994, 1995 TeCGraf"
|
||||
#define LUA_AUTHORS "W. Celes, R. Ierusalimschy & L. H. de Figueiredo"
|
||||
|
||||
@@ -52,6 +52,14 @@ void lua_endblock (void);
|
||||
lua_Object lua_getparam (int number);
|
||||
#define lua_getresult(_) lua_getparam(_)
|
||||
|
||||
#define lua_isnil(_) (lua_type(_)==LUA_T_NIL)
|
||||
#define lua_istable(_) (lua_type(_)==LUA_T_ARRAY)
|
||||
#define lua_isuserdata(_) (lua_type(_)>=LUA_T_USERDATA)
|
||||
#define lua_iscfunction(_) (lua_type(_)==LUA_T_CFUNCTION)
|
||||
int lua_isnumber (lua_Object object);
|
||||
int lua_isstring (lua_Object object);
|
||||
int lua_isfunction (lua_Object object);
|
||||
|
||||
float lua_getnumber (lua_Object object);
|
||||
char *lua_getstring (lua_Object object);
|
||||
lua_CFunction lua_getcfunction (lua_Object object);
|
||||
@@ -60,7 +68,6 @@ void *lua_getuserdata (lua_Object object);
|
||||
void lua_pushnil (void);
|
||||
void lua_pushnumber (float n);
|
||||
void lua_pushstring (char *s);
|
||||
void lua_pushliteral (char *s);
|
||||
void lua_pushcfunction (lua_CFunction fn);
|
||||
void lua_pushusertag (void *u, int tag);
|
||||
void lua_pushobject (lua_Object object);
|
||||
@@ -89,16 +96,10 @@ lua_Object lua_createtable (void);
|
||||
|
||||
#define lua_pushuserdata(u) lua_pushusertag(u, LUA_T_USERDATA)
|
||||
|
||||
#define lua_isnil(_) (lua_type(_)==LUA_T_NIL)
|
||||
#define lua_isnumber(_) (lua_type(_)==LUA_T_NUMBER)
|
||||
#define lua_isstring(_) (lua_type(_)==LUA_T_STRING)
|
||||
#define lua_istable(_) (lua_type(_)==LUA_T_ARRAY)
|
||||
#define lua_isfunction(_) (lua_type(_)==LUA_T_FUNCTION)
|
||||
#define lua_iscfunction(_) (lua_type(_)==LUA_T_CFUNCTION)
|
||||
#define lua_isuserdata(_) (lua_type(_)>=LUA_T_USERDATA)
|
||||
|
||||
/* for compatibility with old versions. Avoid using these macros */
|
||||
|
||||
/* for lua 1.1 compatibility. Avoid using these macros */
|
||||
#define lua_pushliteral(o) lua_pushstring(o)
|
||||
|
||||
#define lua_getindexed(o,n) (lua_pushobject(o), lua_pushnumber(n), lua_getsubscript())
|
||||
#define lua_getfield(o,f) (lua_pushobject(o), lua_pushliteral(f), lua_getsubscript())
|
||||
|
||||
322
lua.stx
322
lua.stx
@@ -1,12 +1,13 @@
|
||||
%{
|
||||
|
||||
char *rcs_luastx = "$Id: lua.stx,v 3.24 1995/10/26 14:21:56 roberto Exp roberto $";
|
||||
char *rcs_luastx = "$Id: lua.stx,v 3.35 1996/03/08 12:02:37 roberto Exp roberto $";
|
||||
|
||||
#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)
|
||||
{
|
||||
if (nlocalvar < MAXLOCALS)
|
||||
localvar[nlocalvar++] = name;
|
||||
else
|
||||
lua_error ("too many local variables");
|
||||
}
|
||||
|
||||
static void store_localvar (Word name, int n)
|
||||
static void store_localvar (TaggedString *name, int n)
|
||||
{
|
||||
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 add_localvar (TaggedString *name)
|
||||
{
|
||||
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
|
||||
|
||||
|
||||
21
luadebug.h
21
luadebug.h
@@ -2,7 +2,7 @@
|
||||
** LUA - Linguagem para Usuarios de Aplicacao
|
||||
** Grupo de Tecnologia em Computacao Grafica
|
||||
** TeCGraf - PUC-Rio
|
||||
** $Id: luadebug.h,v 1.1 1995/10/17 14:12:45 roberto Exp roberto $
|
||||
** $Id: luadebug.h,v 1.5 1996/02/08 17:03:20 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
|
||||
@@ -11,10 +11,21 @@
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
lua_Object lua_stackedfunction(int level);
|
||||
void lua_funcinfo (lua_Object func, char **filename, int *linedefined);
|
||||
int lua_currentline (lua_Object func);
|
||||
char *getobjname (lua_Object o, char **name);
|
||||
typedef lua_Object lua_Function;
|
||||
|
||||
typedef void (*lua_LHFunction) (int line);
|
||||
typedef void (*lua_CHFunction) (lua_Function func, char *file, int line);
|
||||
|
||||
lua_Function lua_stackedfunction (int level);
|
||||
void lua_funcinfo (lua_Object func, char **filename, int *linedefined);
|
||||
int lua_currentline (lua_Function func);
|
||||
char *lua_getobjname (lua_Object o, char **name);
|
||||
|
||||
lua_Object lua_getlocal (lua_Function func, int local_number, char **name);
|
||||
int lua_setlocal (lua_Function func, int local_number);
|
||||
|
||||
extern lua_LHFunction lua_linehook;
|
||||
extern lua_CHFunction lua_callhook;
|
||||
extern int lua_debug;
|
||||
|
||||
#endif
|
||||
|
||||
6
lualib.h
6
lualib.h
@@ -2,7 +2,7 @@
|
||||
** Libraries to be used in LUA programs
|
||||
** Grupo de Tecnologia em Computacao Grafica
|
||||
** TeCGraf - PUC-Rio
|
||||
** $Id: lualib.h,v 1.3 1994/12/13 15:59:16 roberto Exp roberto $
|
||||
** $Id: lualib.h,v 1.6 1996/02/09 19:00:23 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef lualib_h
|
||||
@@ -16,7 +16,9 @@ void mathlib_open (void);
|
||||
/* auxiliar functions (private) */
|
||||
void lua_arg_error(char *funcname);
|
||||
char *lua_check_string (int numArg, char *funcname);
|
||||
float lua_check_number (int numArg, char *funcname);
|
||||
double lua_check_number (int numArg, char *funcname);
|
||||
char *luaI_addchar (int c);
|
||||
void luaI_addquoted (char *s);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
46
luamem.c
46
luamem.c
@@ -3,18 +3,27 @@
|
||||
** TecCGraf - PUC-Rio
|
||||
*/
|
||||
|
||||
char *rcs_mem = "$Id: mem.c,v 1.4 1995/01/13 22:11:12 roberto Exp roberto $";
|
||||
char *rcs_mem = "$Id: mem.c,v 1.10 1996/03/21 16:31:32 roberto Exp roberto $";
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "mem.h"
|
||||
#include "lua.h"
|
||||
#include "table.h"
|
||||
|
||||
|
||||
#define mem_error() lua_error(memEM)
|
||||
|
||||
|
||||
void luaI_free (void *block)
|
||||
{
|
||||
*((int *)block) = -1; /* to catch errors */
|
||||
free(block);
|
||||
if (block)
|
||||
{
|
||||
*((int *)block) = -1; /* to catch errors */
|
||||
free(block);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -22,23 +31,40 @@ void *luaI_malloc (unsigned long size)
|
||||
{
|
||||
void *block = malloc((size_t)size);
|
||||
if (block == NULL)
|
||||
lua_error("not enough memory");
|
||||
mem_error();
|
||||
return block;
|
||||
}
|
||||
|
||||
|
||||
void *luaI_realloc (void *oldblock, unsigned long size)
|
||||
{
|
||||
void *block = realloc(oldblock, (size_t)size);
|
||||
void *block = oldblock ? realloc(oldblock, (size_t)size) :
|
||||
malloc((size_t)size);
|
||||
if (block == NULL)
|
||||
lua_error("not enough memory");
|
||||
mem_error();
|
||||
return block;
|
||||
}
|
||||
|
||||
|
||||
char *luaI_strdup (char *str)
|
||||
int luaI_growvector (void **block, unsigned long nelems, int size,
|
||||
char *errormsg, unsigned long limit)
|
||||
{
|
||||
char *newstr = luaI_malloc(strlen(str)+1);
|
||||
strcpy(newstr, str);
|
||||
return newstr;
|
||||
if (nelems >= limit)
|
||||
lua_error(errormsg);
|
||||
nelems = (nelems == 0) ? 20 : nelems*2;
|
||||
if (nelems > limit)
|
||||
nelems = limit;
|
||||
*block = luaI_realloc(*block, nelems*size);
|
||||
return (int) nelems;
|
||||
}
|
||||
|
||||
|
||||
void* luaI_buffer (unsigned long size)
|
||||
{
|
||||
static unsigned long buffsize = 0;
|
||||
static char* buffer = NULL;
|
||||
if (size > buffsize)
|
||||
buffer = luaI_realloc(buffer, buffsize=size);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
22
luamem.h
22
luamem.h
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
** mem.c
|
||||
** memory manager for lua
|
||||
** $Id: mem.h,v 1.1 1994/11/16 17:38:08 roberto Stab roberto $
|
||||
** $Id: mem.h,v 1.5 1996/03/21 16:31:32 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef mem_h
|
||||
@@ -11,15 +11,29 @@
|
||||
#define NULL 0
|
||||
#endif
|
||||
|
||||
|
||||
/* memory error messages */
|
||||
#define codeEM "code size overflow"
|
||||
#define symbolEM "symbol table overflow"
|
||||
#define constantEM "constant table overflow"
|
||||
#define stackEM "stack size overflow"
|
||||
#define lexEM "lex buffer overflow"
|
||||
#define lockEM "lock table overflow"
|
||||
#define tableEM "table overflow"
|
||||
#define memEM "not enough memory"
|
||||
|
||||
|
||||
void luaI_free (void *block);
|
||||
void *luaI_malloc (unsigned long size);
|
||||
void *luaI_realloc (void *oldblock, unsigned long size);
|
||||
|
||||
char *luaI_strdup (char *str);
|
||||
void *luaI_buffer (unsigned long size);
|
||||
int luaI_growvector (void **block, unsigned long nelems, int size,
|
||||
char *errormsg, unsigned long limit);
|
||||
|
||||
#define new(s) ((s *)luaI_malloc(sizeof(s)))
|
||||
#define newvector(n,s) ((s *)luaI_malloc((n)*sizeof(s)))
|
||||
#define growvector(old,n,s) ((s *)luaI_realloc(old,(n)*sizeof(s)))
|
||||
#define growvector(old,n,s,e,l) \
|
||||
(luaI_growvector((void**)old,n,sizeof(s),e,l))
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
47
makefile
47
makefile
@@ -1,4 +1,4 @@
|
||||
# $Id: makefile,v 1.15 1995/10/17 18:16:58 roberto Exp roberto $
|
||||
# $Id: makefile,v 1.21 1996/03/05 15:57:53 roberto Exp roberto $
|
||||
|
||||
#configuration
|
||||
|
||||
@@ -25,7 +25,8 @@ LUAOBJS = \
|
||||
tree.o \
|
||||
fallback.o \
|
||||
mem.o \
|
||||
func.o
|
||||
func.o \
|
||||
undump.o
|
||||
|
||||
LIBOBJS = \
|
||||
iolib.o \
|
||||
@@ -36,7 +37,7 @@ LIBOBJS = \
|
||||
lua : lua.o lua.a lualib.a
|
||||
$(CC) $(CFLAGS) -o $@ lua.o lua.a lualib.a -lm
|
||||
|
||||
lua.a : parser.o $(LUAOBJS)
|
||||
lua.a : $(LUAOBJS)
|
||||
$(AR) $(ARFLAGS) $@ $?
|
||||
ranlib lua.a
|
||||
|
||||
@@ -47,36 +48,44 @@ lualib.a : $(LIBOBJS)
|
||||
liblua.so.1.0 : lua.o
|
||||
ld -o liblua.so.1.0 $(LUAOBJS)
|
||||
|
||||
y.tab.c y.tab.h : lua.stx
|
||||
yacc++ -d lua.stx
|
||||
|
||||
parser.c : lua.stx
|
||||
yacc++ -d lua.stx ; mv -f y.tab.c parser.c ; mv -f y.tab.h parser.h
|
||||
parser.c : y.tab.c
|
||||
sed -e 's/yy/luaY_/g' y.tab.c > parser.c
|
||||
|
||||
parser.h : y.tab.h
|
||||
sed -e 's/yy/luaY_/g' y.tab.h > parser.h
|
||||
|
||||
clear :
|
||||
rcsclean
|
||||
rm -f *.o
|
||||
rm -f parser.c parser.h
|
||||
rm -f parser.c parser.h y.tab.c y.tab.h
|
||||
co lua.h lualib.h luadebug.h
|
||||
|
||||
|
||||
% : RCS/%,v
|
||||
co $@
|
||||
|
||||
|
||||
|
||||
fallback.o : fallback.c mem.h fallback.h opcode.h lua.h types.h tree.h func.h
|
||||
func.o : func.c table.h tree.h types.h opcode.h lua.h func.h mem.h
|
||||
func.o : func.c luadebug.h lua.h table.h tree.h types.h opcode.h func.h mem.h
|
||||
hash.o : hash.c mem.h opcode.h lua.h types.h tree.h func.h hash.h table.h
|
||||
inout.o : inout.c mem.h opcode.h lua.h types.h tree.h func.h hash.h inout.h \
|
||||
table.h
|
||||
iolib.o : iolib.c lua.h lualib.h luadebug.h
|
||||
lex.o : lex.c mem.h tree.h types.h table.h opcode.h lua.h func.h inout.h parser.h \
|
||||
ugly.h
|
||||
inout.o : inout.c lex.h opcode.h lua.h types.h tree.h func.h inout.h table.h \
|
||||
mem.h
|
||||
iolib.o : iolib.c lua.h luadebug.h lualib.h
|
||||
lex.o : lex.c mem.h tree.h types.h table.h opcode.h lua.h func.h lex.h inout.h \
|
||||
luadebug.h parser.h
|
||||
lua.o : lua.c lua.h lualib.h
|
||||
mathlib.o : mathlib.c lualib.h lua.h
|
||||
mem.o : mem.c mem.h lua.h
|
||||
opcode.o : opcode.c mem.h opcode.h lua.h types.h tree.h func.h hash.h inout.h \
|
||||
table.h fallback.h luadebug.h
|
||||
parser.o : parser.c mem.h opcode.h lua.h types.h tree.h func.h hash.h inout.h \
|
||||
table.h
|
||||
mem.o : mem.c mem.h lua.h table.h tree.h types.h opcode.h func.h
|
||||
opcode.o : opcode.c luadebug.h lua.h mem.h opcode.h types.h tree.h func.h hash.h \
|
||||
inout.h table.h fallback.h undump.h
|
||||
parser.o : parser.c luadebug.h lua.h mem.h lex.h opcode.h types.h tree.h func.h \
|
||||
hash.h inout.h table.h
|
||||
strlib.o : strlib.c lua.h lualib.h
|
||||
table.o : table.c mem.h opcode.h lua.h types.h tree.h func.h hash.h table.h \
|
||||
inout.h fallback.h
|
||||
tree.o : tree.c mem.h lua.h tree.h types.h table.h opcode.h func.h
|
||||
inout.h fallback.h luadebug.h
|
||||
tree.o : tree.c mem.h lua.h tree.h types.h lex.h hash.h opcode.h func.h table.h
|
||||
undump.o : undump.c opcode.h lua.h types.h tree.h func.h mem.h table.h undump.h
|
||||
|
||||
582
manual.tex
582
manual.tex
@@ -1,3 +1,5 @@
|
||||
% $Id: manual.tex,v 1.13 1996/03/19 22:39:07 roberto Exp roberto $
|
||||
|
||||
\documentstyle[A4,11pt,bnf]{article}
|
||||
|
||||
\newcommand{\rw}[1]{{\bf #1}}
|
||||
@@ -9,13 +11,15 @@
|
||||
\newcommand{\Index}[1]{#1\index{#1}}
|
||||
\newcommand{\IndexVerb}[1]{{\tt #1}\index{#1}}
|
||||
\newcommand{\Def}[1]{{\em #1}\index{#1}}
|
||||
\newcommand{\Deffunc}[1]{\index{{\tt #1}}}
|
||||
\newcommand{\Deffunc}[1]{\index{#1}}
|
||||
|
||||
\newcommand{\Version}{2.4}
|
||||
|
||||
\makeindex
|
||||
|
||||
\begin{document}
|
||||
|
||||
\title{Reference Manual of the Programming Language Lua 2.2}
|
||||
\title{Reference Manual of the Programming Language Lua \Version}
|
||||
|
||||
\author{%
|
||||
Roberto Ierusalimschy\quad
|
||||
@@ -27,20 +31,19 @@ Waldemar Celes Filho
|
||||
\small\tt roberto,lhf,celes@icad.puc-rio.br
|
||||
\vspace{2.0ex}\\
|
||||
%MCC 08/95 ---
|
||||
Departamento de Inform\'atica --- PUC-Rio
|
||||
\tecgraf\ --- Departamento de Inform\'atica --- PUC-Rio
|
||||
}
|
||||
|
||||
\date{November, 1995}
|
||||
\date{\small \verb$Date: 1996/03/19 22:39:07 $}
|
||||
|
||||
\maketitle
|
||||
|
||||
|
||||
\begin{abstract}
|
||||
\noindent
|
||||
Lua is an extension programming language designed to be used
|
||||
as a configuration language for any program that needs one.
|
||||
This document describes version 2.2 of the Lua programming language and the
|
||||
API that allows interaction between Lua programs and its host C program.
|
||||
This document describes Version \Version\ of the Lua programming language and
|
||||
the API that allows interaction between Lua programs and its host C program.
|
||||
It also presents some examples of using the main features of the system.
|
||||
\end{abstract}
|
||||
|
||||
@@ -53,9 +56,9 @@ It also presents some examples of using the main features of the system.
|
||||
Lua \'e uma linguagem de extens\~ao projetada para ser usada como
|
||||
linguagem de configura\c{c}\~ao em qualquer programa que precise de
|
||||
uma.
|
||||
Este documento descreve a vers\~ao 2.2 da linguagem de programa\c{c}\~ao Lua e a
|
||||
Interface de Programa\c{c}\~ao que permite a intera\c{c}\~ao entre programas Lua
|
||||
e o programa C hospedeiro.
|
||||
Este documento descreve a vers\~ao \Version\ da linguagem de
|
||||
programa\c{c}\~ao Lua e a Interface de Programa\c{c}\~ao que permite
|
||||
a intera\c{c}\~ao entre programas Lua e o programa C hospedeiro.
|
||||
O documento tamb\'em apresenta alguns exemplos de uso das principais
|
||||
ca\-racte\-r\'{\i}sticas do sistema.
|
||||
\end{quotation}
|
||||
@@ -68,9 +71,9 @@ general procedural programming features with data description
|
||||
facilities.
|
||||
It is supposed to be used as a configuration language for any
|
||||
program that needs one.
|
||||
Its main extensions are related to object-oriented facilities,
|
||||
and fallbacks,
|
||||
but it has some other minor contributions.
|
||||
%Its main extensions are related to object-oriented facilities,
|
||||
%and fallbacks,
|
||||
%but it has some other minor contributions.
|
||||
Lua has been designed and implemented by
|
||||
W.~Celes~F., L.~H.~de Figueiredo and R.~Ierusalimschy.
|
||||
|
||||
@@ -90,7 +93,7 @@ and provided as usual with no guarantees.
|
||||
The implementation described in this manual is available
|
||||
by anonymous ftp from
|
||||
\begin{verbatim}
|
||||
ftp.icad.puc-rio.br:/pub/lua/lua-2.2.tar.gz
|
||||
ftp.icad.puc-rio.br:/pub/lua/lua.tar.gz
|
||||
\end{verbatim}
|
||||
or by WWW (World Wide Web) from
|
||||
\begin{verbatim}
|
||||
@@ -98,7 +101,7 @@ or by WWW (World Wide Web) from
|
||||
\end{verbatim}
|
||||
|
||||
|
||||
\section{Environment and Modules}
|
||||
\section{Environment and Chunks}
|
||||
|
||||
All statements in Lua are executed in a \Def{global environment}.
|
||||
This environment, which keeps all global variables and functions,
|
||||
@@ -134,7 +137,11 @@ of new functions%
|
||||
\footnote{Actually, a function definition is an
|
||||
assignment to a global variable; \see{TypesSec}.}.
|
||||
|
||||
|
||||
Chunks may be pre-compiled; see program \IndexVerb{luac} for details.
|
||||
Ascii files with chunk code and their binary pre-compiled forms
|
||||
are interchangeable.
|
||||
Lua automatically detects the file type and acts accordingly.
|
||||
\index{pre-compilation}
|
||||
|
||||
\section{\Index{Types}} \label{TypesSec}
|
||||
|
||||
@@ -229,7 +236,7 @@ double hyphen (\verb'--') and run until the end of the line.
|
||||
and an optional decimal exponent.
|
||||
Examples of valid numerical constants are:
|
||||
\begin{verbatim}
|
||||
4 4. .4 4.57e-3 .3e12
|
||||
4 4.0 0.4 4.57e-3 0.3e12
|
||||
\end{verbatim}
|
||||
|
||||
|
||||
@@ -342,17 +349,14 @@ the syntax for a \Index{return statement} is:
|
||||
\produc{ret}{\rwd{return} explist}
|
||||
\end{Produc}
|
||||
|
||||
\subsubsection{Expressions as Statements} \label{statexp}
|
||||
All expressions with possible side-effects can be
|
||||
executed as statements.
|
||||
These include function calls and table constructors:
|
||||
\subsubsection{Function Calls as Statements} \label{funcstat}
|
||||
Because of possible side-effects,
|
||||
function calls can be executed as statements.
|
||||
\begin{Produc}
|
||||
\produc{stat}{functioncall}
|
||||
\produc{stat}{tableconstructor}
|
||||
\end{Produc}%
|
||||
Eventual returned values are thrown away.
|
||||
Function calls are explained in Section \ref{functioncall};
|
||||
constructors are the subject of Section \ref{tableconstructor}.
|
||||
Function calls are explained in Section \ref{functioncall}.
|
||||
|
||||
\subsubsection{Local Declarations} \label{localvar}
|
||||
\Index{Local variables} can be declared anywhere inside a block.
|
||||
@@ -403,7 +407,8 @@ Lua offers the following \Index{relational operators}:
|
||||
\begin{verbatim}
|
||||
< > <= >= ~= ==
|
||||
\end{verbatim}
|
||||
All return \nil\ as false and 1 as true.
|
||||
All return \nil\ as false and a value different from \nil\
|
||||
(actually the number 1) as true.
|
||||
|
||||
Equality first compares the types of its operands.
|
||||
If they are different, the result is \nil.
|
||||
@@ -411,7 +416,7 @@ Otherwise, their values are compared.
|
||||
Numbers and strings are compared in the usual way.
|
||||
Tables, CFunctions, and functions are compared by reference,
|
||||
that is, two tables are considered equal only if they are the same table.
|
||||
The operator \verb'~=' is exactly the negation of equality (\verb'=').
|
||||
The operator \verb'~=' is exactly the negation of equality (\verb'==').
|
||||
|
||||
The other operators work as follows.
|
||||
If both arguments are numbers, they are compared as such.
|
||||
@@ -443,7 +448,7 @@ Otherwise, the fallback ``concat'' is called; \see{fallback}.
|
||||
from the lower to the higher priority:
|
||||
\begin{verbatim}
|
||||
and or
|
||||
< > <= >= ~= =
|
||||
< > <= >= ~= ==
|
||||
..
|
||||
+ -
|
||||
* /
|
||||
@@ -544,7 +549,7 @@ the parameter list is a single new table.
|
||||
Because a function can return any number of results
|
||||
(\see{return}),
|
||||
the number of results must be adjusted before used.
|
||||
If the function is called as an statement (\see{statexp}),
|
||||
If the function is called as a statement (\see{funcstat}),
|
||||
its return list is adjusted to 0.
|
||||
If the function is called in a place that needs a single value
|
||||
(syntactically denoted by the non-terminal \verb'exp1'),
|
||||
@@ -556,7 +561,7 @@ no adjustment is done.
|
||||
|
||||
\subsection{\Index{Function Definitions}}
|
||||
|
||||
Functions in Lua can be defined anywhere in the global level of a module.
|
||||
Functions in Lua can be defined anywhere in the global level of a chunk.
|
||||
The syntax for function definition is:
|
||||
\begin{Produc}
|
||||
\produc{function}{\rwd{function} var \ter{(} \opt{parlist1} \ter{)}
|
||||
@@ -572,7 +577,7 @@ into the variable \verb'var'.
|
||||
Parameters act as local variables,
|
||||
initialized with the argument values.
|
||||
\begin{Produc}
|
||||
\produc{parlist1}{'name' \rep{\ter{,} name}}
|
||||
\produc{parlist1}{name \rep{\ter{,} name}}
|
||||
\end{Produc}
|
||||
|
||||
Results are returned using the \verb'return' statement (\see{return}).
|
||||
@@ -599,7 +604,7 @@ end
|
||||
\end{verbatim}
|
||||
that is, the function gets an extra formal parameter called \verb'self'.
|
||||
Notice that
|
||||
the variable \verb'v' must be previously initialized with a table value.
|
||||
the variable \verb'v' must have been previously initialized with a table value.
|
||||
|
||||
|
||||
\subsection{Fallbacks} \label{fallback}
|
||||
@@ -645,6 +650,12 @@ not present in a table.
|
||||
It receives as arguments the table and the index.
|
||||
Its return value is the final result of the indexing operation.
|
||||
The default function returns nil.
|
||||
\item[``getglobal'']\index{index getglobal}
|
||||
called when Lua tries to retrieve the value of a global variable
|
||||
which has a nil value (or which has not been initialized).
|
||||
It receives as argument the name of the variable.
|
||||
Its return value is the final result of the expression.
|
||||
The default function returns nil.
|
||||
\item[``gettable'']\index{gettable fallback}
|
||||
called when Lua tries to index a non table value.
|
||||
It receives as arguments the non table value and the index.
|
||||
@@ -696,18 +707,18 @@ and then the corresponding function from the library
|
||||
is terminated returning an error condition.
|
||||
|
||||
The only argument to the error fallback function is a string describing
|
||||
the error and some extra informations,
|
||||
like current line (when the error is at compilation)
|
||||
or current function (when the error is at execution).
|
||||
the error.
|
||||
The standard I/O library redefines this fallback,
|
||||
using the debug facilities (\see{debugI},
|
||||
in order to print some extra informations,
|
||||
like the stack of calls.
|
||||
For more information about an error,
|
||||
the Lua program can include the compilation pragma \verb'$debug'.
|
||||
\index{debug pragma}
|
||||
\index{debug pragma}\label{pragma}
|
||||
This pragma must be written in a line by itself.
|
||||
When an error occurs in a program compiled with this option,
|
||||
the error message includes extra information showing the stack of calls.
|
||||
|
||||
The standard error routine only prints the error message
|
||||
to \verb'stderr'.
|
||||
the error routine is able to print also the lines where the calls
|
||||
(and the error) were made.
|
||||
If needed, it is possible to change the error fallback routine;
|
||||
\see{fallback}.
|
||||
|
||||
@@ -733,7 +744,7 @@ The API functions can be classified in the following categories:
|
||||
All API functions are declared in the file \verb'lua.h'.
|
||||
|
||||
\subsection{Executing Lua Code}
|
||||
A host program can execute Lua programs written in a file or in a string,
|
||||
A host program can execute Lua chunks written in a file or in a string,
|
||||
using the following functions:
|
||||
\Deffunc{lua_dofile}\Deffunc{lua_dostring}
|
||||
\begin{verbatim}
|
||||
@@ -744,11 +755,18 @@ Both functions return an error code:
|
||||
0, in case of success; non zero, in case of errors.
|
||||
The function \verb'lua_dofile', if called with argument NULL (0),
|
||||
executes the ``file'' {\tt stdin}.
|
||||
Function \verb'lua_dofile' is also able to execute pre-compiled chunks.
|
||||
It detects whether the file is text or not,
|
||||
and loads it accordingly (see program \IndexVerb{luac}).
|
||||
|
||||
\subsection{Converting Values between C and Lua} \label{valuesCLua}
|
||||
Because Lua has no static type system,
|
||||
all values passed between Lua and C have type \IndexVerb{lua\_Object},
|
||||
all values passed between Lua and C have type
|
||||
\verb'lua_Object'\Deffunc{lua_Object},
|
||||
which works like an abstract type in C that can hold any Lua value.
|
||||
Values of type \verb'lua_Object' have no meaning outside Lua;
|
||||
for instance,
|
||||
the comparisson of two \verb"lua_Object's" is of no significance.
|
||||
|
||||
Lua has automatic memory management, and garbage collection.
|
||||
Because of that, a \verb'lua_Object' has a limited scope,
|
||||
@@ -777,19 +795,24 @@ the following function is available:
|
||||
\begin{verbatim}
|
||||
int lua_type (lua_Object object);
|
||||
\end{verbatim}
|
||||
plus the following macros:
|
||||
plus the following macros and functions:
|
||||
\Deffunc{lua_isnil}\Deffunc{lua_isnumber}\Deffunc{lua_isstring}
|
||||
\Deffunc{lua_istable}\Deffunc{lua_iscfunction}\Deffunc{lua_isuserdata}
|
||||
\Deffunc{lua_isfunction}
|
||||
\begin{verbatim}
|
||||
int lua_isnil (lua_Object object);
|
||||
int lua_isnumber (lua_Object object);
|
||||
int lua_isstring (lua_Object object);
|
||||
int lua_istable (lua_Object object);
|
||||
int lua_isfunction (lua_Object object);
|
||||
int lua_iscfunction (lua_Object object);
|
||||
int lua_isuserdata (lua_Object object);
|
||||
\end{verbatim}
|
||||
All macros return 1 if the object has the given type,
|
||||
All macros return 1 if the object is compatible with the given type,
|
||||
and 0 otherwise.
|
||||
The function \verb'lua_isnumber' accepts numbers and numerical strings,
|
||||
\verb'lua_isstring' accepts strings and numbers (\see{coercion}),
|
||||
and \verb'lua_isfunction' accepts Lua and C functions.
|
||||
|
||||
The function \verb'lua_type' can be used to distinguish between
|
||||
different kinds of user data; see below.
|
||||
@@ -827,12 +850,12 @@ otherwise, the function returns 0 (the null pointer).
|
||||
|
||||
The reverse process, that is, passing a specific C value to Lua,
|
||||
is done by using the following functions:
|
||||
\Deffunc{lua_pushnumber}\Deffunc{lua_pushstring}\Deffunc{lua_pushliteral}
|
||||
\Deffunc{lua_pushcfunction}\Deffunc{lua_pushusertag}\Deffunc{lua_pushuserdata}
|
||||
\Deffunc{lua_pushnumber}\Deffunc{lua_pushstring}
|
||||
\Deffunc{lua_pushcfunction}\Deffunc{lua_pushusertag}
|
||||
\Deffunc{lua_pushuserdata}
|
||||
\begin{verbatim}
|
||||
void lua_pushnumber (double n);
|
||||
void lua_pushstring (char *s);
|
||||
void lua_pushliteral (char *s);
|
||||
void lua_pushcfunction (lua_CFunction f);
|
||||
void lua_pushusertag (void *u, int tag);
|
||||
\end{verbatim}
|
||||
@@ -845,12 +868,6 @@ convert it to a correspondent \verb'lua_Object',
|
||||
and leave the result on the top of the Lua stack,
|
||||
where it can be assigned to a Lua variable,
|
||||
passed as paramenter to a Lua function, etc (see below). \label{pushing}
|
||||
\verb'lua_pushliteral' is like \verb'lua_pushstring',
|
||||
but also puts the string in the Lua literal table.
|
||||
This avoids the string to be garbage collected,
|
||||
and therefore has a better overall performance.
|
||||
As a rule, when the string to be pushed is a literal,
|
||||
\verb'lua_pushliteral' should be used.
|
||||
|
||||
User data can have different tags,
|
||||
whose semantics are defined by the host program.
|
||||
@@ -875,6 +892,9 @@ one can use the function:
|
||||
\begin{verbatim}
|
||||
lua_Object lua_getglobal (char *varname);
|
||||
\end{verbatim}
|
||||
As in Lua, if the value of the global is \nil,
|
||||
the \verb'"getglobal"' fallback is called.
|
||||
|
||||
To store a value previously pushed onto the stack in a global variable,
|
||||
there is the function:
|
||||
\Deffunc{lua_storeglobal}
|
||||
@@ -894,15 +914,15 @@ As in Lua, if the first object is not a table,
|
||||
or the index is not present in the table,
|
||||
the correspondent fallback is called.
|
||||
|
||||
For compatibility with previous versions of the API,
|
||||
the following macros are supported:
|
||||
\Deffunc{lua_getindexed}\Deffunc{lua_getfield}
|
||||
\begin{verbatim}
|
||||
lua_Object lua_getindexed (lua_Object table, float index);
|
||||
lua_Object lua_getfield (lua_Object table, char *field);
|
||||
\end{verbatim}
|
||||
The first one is used for numeric indices,
|
||||
while the second can be used for any string index.
|
||||
%For compatibility with previous versions of the API,
|
||||
%the following macros are supported:
|
||||
%\Deffunc{lua_getindexed}\Deffunc{lua_getfield}
|
||||
%\begin{verbatim}
|
||||
%lua_Object lua_getindexed (lua_Object table, float index);
|
||||
%lua_Object lua_getfield (lua_Object table, char *field);
|
||||
%\end{verbatim}
|
||||
%The first one is used for numeric indices,
|
||||
%while the second can be used for any string index.
|
||||
|
||||
To store a value in an index,
|
||||
the program must push onto the stack the table, the index,
|
||||
@@ -999,7 +1019,7 @@ The first parameter is the fallback name,
|
||||
and the second a CFunction to be used as the new fallback.
|
||||
This function returns a \verb'lua_Object',
|
||||
which is the old fallback value,
|
||||
or nil on fail (invalid fallback name).
|
||||
or \nil\ on fail (invalid fallback name).
|
||||
This old value can be used for chaining fallbacks.
|
||||
|
||||
An example of C code calling a Lua function is shown in
|
||||
@@ -1036,7 +1056,8 @@ lua_Object lua_getparam (int number);
|
||||
\end{verbatim}
|
||||
where \verb'number' starts with 1 to get the first argument.
|
||||
When called with a number larger than the actual number of arguments,
|
||||
this function returns \IndexVerb{LUA\_NOOBJECT}.
|
||||
this function returns
|
||||
\verb'LUA_NOOBJECT'\Deffunc{LUA_NOOBJECT}.
|
||||
In this way, it is possible to write functions that work with
|
||||
a variable number of parameters.
|
||||
|
||||
@@ -1080,8 +1101,9 @@ it can be unlocked with a call to \verb'lua_unlock'.
|
||||
The set of \Index{predefined functions} in Lua is small but powerful.
|
||||
Most of them provide features that allows some degree of
|
||||
\Index{reflexivity} in the language.
|
||||
Many of these features cannot be simulated with the rest of the
|
||||
Language nor with the standard API.
|
||||
Some of these features cannot be simulated with the rest of the
|
||||
Language nor with the standard Lua API.
|
||||
Others are just helpful interfaces to common API functions.
|
||||
|
||||
The libraries, on the other hand, provide useful routines
|
||||
that are implemented directly through the standard API.
|
||||
@@ -1103,8 +1125,12 @@ declared in \verb-lualib.h-.
|
||||
|
||||
\subsubsection*{{\tt dofile (filename)}}\Deffunc{dofile}
|
||||
This function receives a file name,
|
||||
opens it and executes its contents as a Lua chunk.
|
||||
opens it and executes its contents as a Lua chunk,
|
||||
or as pre-compiled chunks.
|
||||
When called without arguments,
|
||||
it executes the contents of the standard input.
|
||||
It returns 1 if there are no errors, \nil\ otherwise.
|
||||
It issues an error when called with a non string argument.
|
||||
|
||||
\subsubsection*{{\tt dostring (string)}}\Deffunc{dostring}
|
||||
This function executes a given string as a Lua chunk.
|
||||
@@ -1141,6 +1167,10 @@ and its value,
|
||||
or \nil\ if there are no more variables.
|
||||
See Section \ref{exnext} for an example of the use of this function.
|
||||
|
||||
\subsubsection*{{\tt tostring (e)}}\Deffunc{tostring}
|
||||
This function receives an argument of any type and
|
||||
converts it to a string in a reasonable format.
|
||||
|
||||
\subsubsection*{{\tt print (e1, e2, ...)}}\Deffunc{print}
|
||||
This function receives any number of arguments,
|
||||
and prints their values in a reasonable format.
|
||||
@@ -1174,6 +1204,10 @@ This tag can be used to distinguish between user
|
||||
data with different tags,
|
||||
and between C functions and Lua functions.
|
||||
|
||||
\subsubsection*{{\tt assert (v)}}\Deffunc{assert}
|
||||
This function issues an {\em ``assertion failed!''} error
|
||||
when its argument is \nil.
|
||||
|
||||
\subsubsection*{{\tt error (message)}}\Deffunc{error}
|
||||
This function issues an error message and terminates
|
||||
the last called function from the library
|
||||
@@ -1185,6 +1219,7 @@ This function assigns the given value to a global variable.
|
||||
The string \verb'name' does not need to be a syntactically valid variable name.
|
||||
Therefore, this function can set global variables with strange names like
|
||||
\verb'm v 1' or \verb'34'.
|
||||
It returns the value of its second argument.
|
||||
|
||||
\subsubsection*{{\tt getglobal (name)}}\Deffunc{getglobal}
|
||||
This function retrieves the value of a global variable.
|
||||
@@ -1240,11 +1275,27 @@ All other characters are left unchanged.
|
||||
Returns the ascii code of the character \verb's[i]'.
|
||||
If \verb'i' is absent, it is assumed to be 1.
|
||||
|
||||
\subsubsection*{{\tt int2str (\{i\})}}\Deffunc{int2str}
|
||||
Receives 0 or more numbers.
|
||||
Returns a string with length equal to the number of arguments,
|
||||
wherein each character has ascii value equal
|
||||
to its correspondent argument.
|
||||
\subsubsection*{{\tt format (formatstring, e1, e2, \ldots)}}\Deffunc{format}
|
||||
\label{format}
|
||||
This function returns a formated version of its variable number of arguments
|
||||
following the description given in its first argument (which must be a string).
|
||||
The format string follows the same rules as the \verb'printf' family of
|
||||
standard C functions.
|
||||
The only differencies are that the options/modifiers
|
||||
\verb'*', \verb'l', \verb'L', \verb'n', \verb'p',
|
||||
and \verb'h' are not supported,
|
||||
and there is an extra option, \verb'q'.
|
||||
This option formats a string in a form suitable to be safely read
|
||||
back by the Lua interpreter.
|
||||
The string is written between double quotes,
|
||||
and all double quotes, returns and backslashes in the string
|
||||
are correctly escaped when written.
|
||||
|
||||
The options \verb'c', \verb'd', \verb'E', \verb'e', \verb'f',
|
||||
\verb'g' \verb'i', \verb'o', \verb'u', \verb'X', and \verb'x' all
|
||||
expect a number argument,
|
||||
while \verb'q' and \verb's' expects a string.
|
||||
|
||||
|
||||
\subsection{Mathematical Functions} \label{mathlib}
|
||||
|
||||
@@ -1257,9 +1308,10 @@ The library provides the following functions:
|
||||
\Deffunc{atan2}\Deffunc{ceil}\Deffunc{cos}\Deffunc{floor}
|
||||
\Deffunc{log}\Deffunc{log10}\Deffunc{max}\Deffunc{min}
|
||||
\Deffunc{mod}\Deffunc{sin}\Deffunc{sqrt}\Deffunc{tan}
|
||||
\Deffunc{random}\Deffunc{randomseed}
|
||||
\begin{verbatim}
|
||||
abs acos asin atan atan2 ceil cos floor
|
||||
log log10 max min mod sin sqrt tan
|
||||
abs acos asin atan atan2 ceil cos floor log log10
|
||||
max min mod sin sqrt tan random randomseed
|
||||
\end{verbatim}
|
||||
Most of them
|
||||
are only interfaces to the homonymous functions in the C library,
|
||||
@@ -1273,6 +1325,12 @@ Both can be used with an unlimited number of arguments.
|
||||
|
||||
The function \verb'mod' is equivalent to the \verb'%' operator in C.
|
||||
|
||||
The functions \verb'random' and \verb'randomseed' are interfaces to
|
||||
the simple random generator functions \verb'rand' and \verb'srand',
|
||||
provided by ANSI C.
|
||||
The function \verb'random' returns pseudo-random numbers in the range
|
||||
$[0,1)$.
|
||||
|
||||
|
||||
\subsection{I/O Facilities} \label{libio}
|
||||
|
||||
@@ -1299,8 +1357,8 @@ then a \Index{piped input} is open, via function \IndexVerb{popen}.
|
||||
|
||||
This function opens a file named \verb'filename' and sets it as the
|
||||
{\em current} output file.
|
||||
Notice that, if the file already exists, it is completely erased with this
|
||||
operation.
|
||||
Notice that, if the file already exists,
|
||||
it will be {\em completely erased} with this operation.
|
||||
When called without parameters,
|
||||
this function closes the current output file,
|
||||
and restores \verb'stdout' as the current output file.
|
||||
@@ -1322,6 +1380,15 @@ This function returns 2 if the file already exists,
|
||||
|
||||
This function deletes the file with the given name.
|
||||
|
||||
\subsubsection*{{\tt rename (name1, name2)}}\Deffunc{rename}
|
||||
|
||||
This function renames file \verb'name1' to \verb'name2'.
|
||||
|
||||
\subsubsection*{{\tt tmpname ()}}\Deffunc{tmpname}
|
||||
|
||||
This function returns a string with a file name that can safely
|
||||
be used for a temporary file.
|
||||
|
||||
\subsubsection*{{\tt read ([format])}}\Deffunc{read}
|
||||
|
||||
This function returns a value read from the current input.
|
||||
@@ -1350,6 +1417,8 @@ Particularly, the format \verb'"s1"' reads a single character.
|
||||
\subsubsection*{{\tt readuntil (char)}}\Deffunc{readuntil}
|
||||
|
||||
Reads the current input until the first ocurrence of the given character.
|
||||
When called with no parameters,
|
||||
reads until the end of the current input file.
|
||||
Returns the string read.
|
||||
The character itself is not read.
|
||||
|
||||
@@ -1363,7 +1432,13 @@ following characters:
|
||||
\begin{description}
|
||||
\item['s' or 'S'] to write strings;
|
||||
\item['f' or 'F'] to write floats;
|
||||
\item['i' or 'I'] to write integers.
|
||||
\item['i' or 'I'] to write integers;
|
||||
\item['q' or 'Q'] to write quoted strings.
|
||||
This format writes the string in a form suitable to be safely read
|
||||
back by the Lua interpreter.
|
||||
The string is written between double quotes,
|
||||
and all double quotes, returns and backslashes in the string
|
||||
are correctly escaped when written.
|
||||
\end{description}
|
||||
These characters can be followed by
|
||||
\begin{verbatim}
|
||||
@@ -1386,6 +1461,20 @@ This option has no meaning for strings.
|
||||
When called without a format string,
|
||||
this function writes numbers using the \verb'%g' format
|
||||
and strings with \verb'%s'.
|
||||
For better format facilities,
|
||||
the function \verb'format' should be used (\see{format}).
|
||||
|
||||
\subsubsection*{{\tt date ([format])}}\Deffunc{date}
|
||||
|
||||
This function returns a string containing date and time
|
||||
formatted according to the given string \verb'format',
|
||||
following the same rules of the ANSI C function \verb'strftime'.
|
||||
When called without arguments,
|
||||
it returns a reasonable date and time representation.
|
||||
|
||||
This function replaces functions \verb'date' and \verb'time' from
|
||||
previous Lua versions.
|
||||
|
||||
|
||||
% \subsubsection*{{\tt debug ()}}
|
||||
% This function, when called, repeatedly presents a prompt \verb'lua_debug> '
|
||||
@@ -1396,6 +1485,134 @@ and strings with \verb'%s'.
|
||||
% This function then returns and the execution of the program continues.
|
||||
|
||||
|
||||
|
||||
\section{The Debuger Interface} \label{debugI}
|
||||
|
||||
Lua has no built in debuger facilities.
|
||||
Instead, it offers a special interface,
|
||||
by means of functions and {\em hooks},
|
||||
which allows the construction of different
|
||||
kinds of debugers, profiles, and other tools
|
||||
that need ``inside'' information from the interpreter.
|
||||
This interface is declared in the file \verb'luadebug.h'.
|
||||
|
||||
\subsection{Stack and Function Information}
|
||||
|
||||
The main function to get information about the interpreter stack
|
||||
is
|
||||
\begin{verbatim}
|
||||
lua_Function lua_stackedfunction (int level);
|
||||
\end{verbatim}
|
||||
It returns a handle (\verb'lua_Function') to the {\em activation record\/}
|
||||
of the function executing at a given level.
|
||||
Level 0 is the current running function,
|
||||
while level $n+1$ is the function that has called level $n$.
|
||||
When called with a level greater than the stack depth,
|
||||
\verb'lua_stackedfunction' returns \verb'LUA_NOOBJECT'.
|
||||
|
||||
The type \verb'lua_Function' is just another name
|
||||
to \verb'lua_Object'.
|
||||
Although, in this library,
|
||||
a \verb'lua_Function' can be used wherever a \verb'lua_Object' is required,
|
||||
a parameter \verb'lua_Function' accepts only a handle returned by
|
||||
\verb'lua_stackedfunction'.
|
||||
|
||||
Three other functions produce extra information about a function:
|
||||
\begin{verbatim}
|
||||
void lua_funcinfo (lua_Object func, char **filename, int *linedefined);
|
||||
int lua_currentline (lua_Function func);
|
||||
char *lua_getobjname (lua_Object o, char **name);
|
||||
\end{verbatim}
|
||||
\verb'lua_funcinfo' gives the file name and the line where the
|
||||
given function has been defined.
|
||||
If the ``function'' is in fact the main code of a chunk,
|
||||
\verb'linedefined' is 0.
|
||||
If the function is a C function,
|
||||
\verb'linedefined' is -1, and \verb'filename' is \verb'"(C)"'.
|
||||
|
||||
The function \verb'lua_currentline' gives the current line where
|
||||
a given function is executing.
|
||||
It only works if the function has been pre-compiled with debug
|
||||
information (\see{pragma}).
|
||||
When no line information is available, it returns -1.
|
||||
|
||||
Function \verb'lua_getobjname' tries to find a reasonable name for
|
||||
a given function.
|
||||
Because functions in Lua are first class values,
|
||||
they do not have a fixed name.
|
||||
Some functions may be the value of many global variables,
|
||||
while others may be stored only in a table field.
|
||||
Function \verb'lua_getobjname' first checks whether the given
|
||||
function is a fallback.
|
||||
If so, it returns the string \verb'"fallback"',
|
||||
and \verb'name' is set to point to the fallback name.
|
||||
Otherwise, if the given function is the value of a global variable,
|
||||
\verb'lua_getobjname' returns the string \verb'"global"',
|
||||
while \verb'name' points to the variable name.
|
||||
If the given function is neither a fallback nor a global variable,
|
||||
\verb'lua_getobjname' returns the empty string,
|
||||
and \verb'name' is set to \verb'NULL'.
|
||||
|
||||
\subsection{Manipulating Local Variables}
|
||||
|
||||
The following functions allow the manipulation of the
|
||||
local variables of a given activation record.
|
||||
They only work if the function has been pre-compiled with debug
|
||||
information (\see{pragma}).
|
||||
\begin{verbatim}
|
||||
lua_Object lua_getlocal (lua_Function func, int local_number, char **name);
|
||||
int lua_setlocal (lua_Function func, int local_number);
|
||||
\end{verbatim}
|
||||
The first one returns the value of a local variable,
|
||||
and sets \verb'name' to point to the variable name.
|
||||
\verb'local_number' is an index for local variables.
|
||||
The first parameter has index 1, and so on, until the
|
||||
last active local variable.
|
||||
When called with a \verb'local_number' greater than the
|
||||
number of active local variables,
|
||||
or if the activation record has no debug information,
|
||||
\verb'lua_getlocal' returns \verb'LUA_NOOBJECT'.
|
||||
|
||||
The function \verb'lua_setlocal' sets the local variable
|
||||
\verb'local_number' to the value previously pushed on the stack
|
||||
(\see{valuesCLua}).
|
||||
If the function succeeds it returns 1.
|
||||
If \verb'local_number' is greater than the number
|
||||
of active local variables,
|
||||
or if the activation record has no debug information,
|
||||
this function fails and returns 0.
|
||||
|
||||
\subsection{Hooks}
|
||||
|
||||
The Lua interpreter offers two hooks for debug purposes:
|
||||
\begin{verbatim}
|
||||
typedef void (*lua_CHFunction) (lua_Function func, char *file, int line);
|
||||
extern lua_CHFunction lua_callhook;
|
||||
|
||||
typedef void (*lua_LHFunction) (int line);
|
||||
extern lua_LHFunction lua_linehook;
|
||||
\end{verbatim}
|
||||
The first one is called whenever the interpreter enters or leaves a
|
||||
function.
|
||||
When entering a function,
|
||||
its parameters are a handle to the function activation record,
|
||||
plus the file and the line where the function is defined (the same
|
||||
information which is provided by \verb'lua_funcinfo');
|
||||
when leaving a function, \verb'func' is \verb'LUA_NOOBJECT',
|
||||
\verb'file' is \verb'"(return)"', and \verb'line' is 0.
|
||||
|
||||
The other hook is called every time the interpreter changes
|
||||
the line of code it is executing.
|
||||
Its only parameter is the line number
|
||||
(the same information which is provided by the call
|
||||
\verb'lua_currentline(lua_stackedfunction(0))').
|
||||
This second hook is only called if the active function
|
||||
has been pre-compiled with debug information (\see{pragma}).
|
||||
|
||||
A hook is disabled when its value is NULL (0),
|
||||
which is the initial value of both hooks.
|
||||
|
||||
|
||||
\section{Some Examples}
|
||||
|
||||
This section gives examples showing some features of Lua.
|
||||
@@ -1403,6 +1620,50 @@ It does not intend to cover the whole language,
|
||||
but only to illustrate some interesting uses of the system.
|
||||
|
||||
|
||||
\subsection{\Index{Data Structures}}
|
||||
Tables are a strong unifying data constructor.
|
||||
They directly implement a multitude of data types,
|
||||
like ordinary arrays, records, sets, bags, and lists.
|
||||
|
||||
Arrays need no explanations.
|
||||
In Lua, it is conventional to start indices from 1,
|
||||
but this is only a convention.
|
||||
Arrays can be indexed by 0, negative numbers, or any other value (but \nil).
|
||||
Records are also trivially implemented by the syntactic sugar
|
||||
\verb'a.x'.
|
||||
|
||||
The best way to implement a set is to store
|
||||
its elements as indices of a table.
|
||||
The statement \verb's = {}' creates an empty set \verb's'.
|
||||
The statement \verb's[x] = 1' inserts the value of \verb'x' into
|
||||
the set \verb's'.
|
||||
The expression \verb's[x]' is true if and only if
|
||||
\verb'x' belongs to \verb's'.
|
||||
Finally, the statement \verb's[x] = nil' erases \verb'x' from \verb's'.
|
||||
|
||||
Bags can be implemented similarly to sets,
|
||||
but using the value associated to an element as its counter.
|
||||
So, to insert an element,
|
||||
the following code is enough:
|
||||
\begin{verbatim}
|
||||
if s[x] then s[x] = s[x]+1
|
||||
else s[x] = 1 end
|
||||
\end{verbatim}
|
||||
and to remove an element:
|
||||
\begin{verbatim}
|
||||
if s[x] then s[x] = s[x]-1 end
|
||||
if s[x] == 0 then s[x] = nil end
|
||||
\end{verbatim}
|
||||
|
||||
Lisp-like lists also have an easy implementation.
|
||||
The ``cons'' of two elements \verb'x' and \verb'y' can be
|
||||
created with the code \verb'l = {car=x, cdr=y}'.
|
||||
The expression \verb'l.car' extracts the header,
|
||||
while \verb'l.cdr' extracts the tail.
|
||||
An alternative way is to create the list directly with \verb'l={x,y}',
|
||||
and then to extract the header with \verb'l[1]' and
|
||||
the tail with \verb'l[2]'.
|
||||
|
||||
\subsection{The Functions {\tt next} and {\tt nextvar}} \label{exnext}
|
||||
\Deffunc{next}\Deffunc{nextvar}
|
||||
This example shows how to use the function \verb'next' to iterate
|
||||
@@ -1410,7 +1671,7 @@ over the fields of a table.
|
||||
Function \Def{clone} receives any table and returns a clone of it.
|
||||
\begin{verbatim}
|
||||
function clone (t) -- t is a table
|
||||
local new_t = {} -- creates a new table
|
||||
local new_t = {} -- create a new table
|
||||
local i, v = next(t, nil) -- i is an index of t, v = t[i]
|
||||
while i do
|
||||
new_t[i] = v
|
||||
@@ -1465,6 +1726,28 @@ end
|
||||
\end{verbatim}
|
||||
|
||||
|
||||
\subsection{\Index{Variable number of arguments}}
|
||||
Lua does not provide any explicit mechanism to deal with
|
||||
variable number of arguments.
|
||||
However, one can use table constructors to simulate this mechanism.
|
||||
As an example, suppose a function to concatenate all its arguments.
|
||||
It could be written like
|
||||
\begin{verbatim}
|
||||
function concat (o)
|
||||
local i = 1
|
||||
local s = ''
|
||||
while o[i] do
|
||||
s = s .. o[i]
|
||||
i = i+1
|
||||
end
|
||||
return s
|
||||
end
|
||||
\end{verbatim}
|
||||
To call it, one uses a table constructor to join all arguments:
|
||||
\begin{verbatim}
|
||||
x = concat{"hello ", "john", " and ", "mary"}
|
||||
\end{verbatim}
|
||||
|
||||
\subsection{\Index{Persistence}}
|
||||
Because of its reflexive facilities,
|
||||
persistence in Lua can be achieved within the language.
|
||||
@@ -1475,7 +1758,7 @@ To store a single value with a name,
|
||||
the following code is enough:
|
||||
\begin{verbatim}
|
||||
function store (name, value)
|
||||
write('\n' .. name .. '=')
|
||||
write(format('\n%s =', name))
|
||||
write_value(value)
|
||||
end
|
||||
\end{verbatim}
|
||||
@@ -1484,7 +1767,7 @@ function write_value (value)
|
||||
local t = type(value)
|
||||
if t == 'nil' then write('nil')
|
||||
elseif t == 'number' then write(value)
|
||||
elseif t == 'string' then write('[[' .. value .. ']]')
|
||||
elseif t == 'string' then write(value, 'q')
|
||||
end
|
||||
end
|
||||
\end{verbatim}
|
||||
@@ -1501,7 +1784,7 @@ function write_value (value)
|
||||
local t = type(value)
|
||||
if t == 'nil' then write('nil')
|
||||
elseif t == 'number' then write(value)
|
||||
elseif t == 'string' then write('"' .. value .. '"')
|
||||
elseif t == 'string' then write(value, 'q')
|
||||
elseif t == 'table' then write_record(value)
|
||||
end
|
||||
end
|
||||
@@ -1596,7 +1879,7 @@ void Index (void)
|
||||
\end{figure}
|
||||
This code must be registered with:
|
||||
\begin{verbatim}
|
||||
lua_pushliteral("parent");
|
||||
lua_pushstring("parent");
|
||||
lockedParentName = lua_lock();
|
||||
lua_pushobject(lua_setfallback("index", Index));
|
||||
lockedOldIndex = lua_lock();
|
||||
@@ -1604,6 +1887,112 @@ This code must be registered with:
|
||||
Notice how the string \verb'"parent"' is kept
|
||||
locked in Lua for optimal performance.
|
||||
|
||||
\subsection{\Index{Programming with Classes}}
|
||||
There are many different ways to do object-oriented programming in Lua.
|
||||
This section presents one possible way to
|
||||
implement classes,
|
||||
using the inheritance mechanism presented above.
|
||||
|
||||
As one could expect, a good way to represent a class is
|
||||
as a table.
|
||||
This table will contain all instance methods of the class,
|
||||
plus eventual default values for instance variables.
|
||||
An instance of a class has its \verb'parent' field pointing to
|
||||
the class,
|
||||
and so it ``inherits'' all methods.
|
||||
|
||||
For instance, a class \verb'Point' can be described as in
|
||||
Figure~\ref{Point}.
|
||||
Function \verb'create' helps the creation of new points,
|
||||
adding the parent field.
|
||||
Function \verb'move' is an example of an instance method.
|
||||
\begin{figure}
|
||||
\Line
|
||||
\begin{verbatim}
|
||||
Point = {x = 0, y = 0}
|
||||
|
||||
function Point:create (o)
|
||||
o.parent = self
|
||||
return o
|
||||
end
|
||||
|
||||
function Point:move (p)
|
||||
self.x = self.x + p.x
|
||||
self.y = self.y + p.y
|
||||
end
|
||||
|
||||
...
|
||||
|
||||
--
|
||||
-- creating points
|
||||
--
|
||||
p1 = Point:create{x = 10, y = 20}
|
||||
p2 = Point:create{x = 10} -- y will be inherited until it is set
|
||||
|
||||
--
|
||||
-- example of a method invocation
|
||||
--
|
||||
p1:move(p2)
|
||||
\end{verbatim}
|
||||
\caption{A Class Point.\label{Point}}
|
||||
\Line
|
||||
\end{figure}
|
||||
Finally, a subclass can be created as a new table,
|
||||
with the \verb'parent' field pointing to its superclass.
|
||||
It is interesting to notice how the use of \verb'self' in
|
||||
method \verb'create' allows this method to work properly even
|
||||
when inherited by a subclass.
|
||||
As usual, a subclass may overwrite any inherited method with
|
||||
its own version.
|
||||
|
||||
\subsection{\Index{Modules}}
|
||||
Here we explain one possible way to simulate modules in Lua.
|
||||
The main idea is to use a table to store the module functions.
|
||||
|
||||
A module should be written as a separate chunk, starting with:
|
||||
\begin{verbatim}
|
||||
if modulename then return end -- avoid loading twice the same module
|
||||
modulename = {} -- create a table to represent the module
|
||||
\end{verbatim}
|
||||
After that, functions can be directly defined with the syntax
|
||||
\begin{verbatim}
|
||||
function modulename.foo (...)
|
||||
...
|
||||
end
|
||||
\end{verbatim}
|
||||
|
||||
Any code that needs this module has only to execute
|
||||
\verb'dofile("filename")', where \verb'filename' is the file
|
||||
where the module is written.
|
||||
After this, any function can be called with
|
||||
\begin{verbatim}
|
||||
modulename.foo(...)
|
||||
\end{verbatim}
|
||||
|
||||
If a module function is going to be used many times,
|
||||
the program can give a local name to it.
|
||||
Because functions are values, it is enough to write
|
||||
\begin{verbatim}
|
||||
localname = modulename.foo
|
||||
\end{verbatim}
|
||||
Finally, a module may be {\em opened},
|
||||
giving direct access to all its functions,
|
||||
as shown in the code in Figure~\ref{openmod}.
|
||||
\begin{figure}
|
||||
\Line
|
||||
\begin{verbatim}
|
||||
function open (mod)
|
||||
local n, f = next(mod, nil)
|
||||
while n do
|
||||
setglobal(n, f)
|
||||
n, f = next(mod, n)
|
||||
end
|
||||
end
|
||||
\end{verbatim}
|
||||
\caption{Opening a module.\label{openmod}}
|
||||
\Line
|
||||
\end{figure}
|
||||
|
||||
\subsection{A CFunction} \label{exCFunction}\index{functions in C}
|
||||
A CFunction to compute the maximum of a variable number of arguments
|
||||
is shown in Figure~\ref{Cmax}.
|
||||
@@ -1664,11 +2053,11 @@ void remove_blanks (char *s)
|
||||
\section*{Acknowledgments}
|
||||
|
||||
The authors would like to thank CENPES/PETROBR\'AS which,
|
||||
jointly with TeCGraf, used extensively early versions of
|
||||
jointly with \tecgraf, used extensively early versions of
|
||||
this system and gave valuable comments.
|
||||
The authors would also like to thank Carlos Henrique Levy,
|
||||
who found the name of the game%
|
||||
\footnote{BTW, Lua means {\em moon} in Portuguese.}.
|
||||
who found the name of the game.
|
||||
Lua means {\em moon} in Portuguese.
|
||||
|
||||
|
||||
|
||||
@@ -1681,10 +2070,24 @@ the previous public versions of Lua,
|
||||
some differences had to be introduced.
|
||||
Here is a list of all these differences.
|
||||
|
||||
\subsection*{Incompatibilities with \Index{version 2.2}}
|
||||
\begin{itemize}
|
||||
\item
|
||||
Functions \verb'date' and \verb'time' (from \verb'iolib')
|
||||
have been superseded by the new version of function \verb'date'.
|
||||
\item
|
||||
Function \verb'int2str' (from \verb'strlib') has been superseded by new
|
||||
function \verb'format', with parameter \verb'"%c"'.
|
||||
\item
|
||||
API function \verb'lua_pushliteral' now is just a macro to
|
||||
\verb'lua_pushstring'.
|
||||
Programmers are encouraged not to use this macro.
|
||||
\end{itemize}
|
||||
|
||||
\subsection*{Incompatibilities with \Index{version 2.1}}
|
||||
\begin{itemize}
|
||||
\item
|
||||
The function {\tt type} now returns the string {\tt function}
|
||||
The function \verb'type' now returns the string \verb'"function"'
|
||||
both for C and Lua functions.
|
||||
Because Lua functions and C functions are compatible,
|
||||
this behavior is usually more useful.
|
||||
@@ -1738,4 +2141,13 @@ Special care should be taken with macros like
|
||||
\verb'lua_getindexed' and \verb'lua_getfield'.
|
||||
\end{itemize}
|
||||
|
||||
\newcommand{\indexentry}[2]{\item {#1} #2}
|
||||
%\catcode`\_=12
|
||||
\begin{theindex}
|
||||
\input{manual.id}
|
||||
\end{theindex}
|
||||
|
||||
\pagebreak
|
||||
\tableofcontents
|
||||
|
||||
\end{document}
|
||||
|
||||
19
mathlib.c
19
mathlib.c
@@ -3,9 +3,9 @@
|
||||
** Mathematics library to LUA
|
||||
*/
|
||||
|
||||
char *rcs_mathlib="$Id: mathlib.c,v 1.12 1995/10/09 12:48:38 roberto Exp roberto $";
|
||||
char *rcs_mathlib="$Id: mathlib.c,v 1.13 1995/11/10 17:54:31 roberto Exp roberto $";
|
||||
|
||||
#include <stdio.h> /* NULL */
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "lualib.h"
|
||||
@@ -184,6 +184,18 @@ static void math_rad (void)
|
||||
lua_pushnumber (d/180.*PI);
|
||||
}
|
||||
|
||||
static void math_random (void)
|
||||
{
|
||||
lua_pushnumber((double)(rand()%RAND_MAX) / (double)RAND_MAX);
|
||||
}
|
||||
|
||||
static void math_randomseed (void)
|
||||
{
|
||||
srand(lua_check_number(1, "randomseed"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Open math library
|
||||
*/
|
||||
@@ -208,5 +220,8 @@ void mathlib_open (void)
|
||||
lua_register ("exp", math_exp);
|
||||
lua_register ("deg", math_deg);
|
||||
lua_register ("rad", math_rad);
|
||||
lua_register ("random", math_random);
|
||||
lua_register ("randomseed", math_randomseed);
|
||||
|
||||
old_pow = lua_lockobject(lua_setfallback("arith", math_pow));
|
||||
}
|
||||
|
||||
247
opcode.c
247
opcode.c
@@ -3,10 +3,9 @@
|
||||
** TecCGraf - PUC-Rio
|
||||
*/
|
||||
|
||||
char *rcs_opcode="$Id: opcode.c,v 3.49 1995/11/10 14:12:02 roberto Exp roberto $";
|
||||
char *rcs_opcode="$Id: opcode.c,v 3.65 1996/03/21 18:55:02 roberto Exp roberto $";
|
||||
|
||||
#include <setjmp.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -18,6 +17,7 @@ char *rcs_opcode="$Id: opcode.c,v 3.49 1995/11/10 14:12:02 roberto Exp roberto $
|
||||
#include "table.h"
|
||||
#include "lua.h"
|
||||
#include "fallback.h"
|
||||
#include "undump.h"
|
||||
|
||||
#define tonumber(o) ((tag(o) != LUA_T_NUMBER) && (lua_tonumber(o) != 0))
|
||||
#define tostring(o) ((tag(o) != LUA_T_STRING) && (lua_tostring(o) != 0))
|
||||
@@ -25,6 +25,10 @@ char *rcs_opcode="$Id: opcode.c,v 3.49 1995/11/10 14:12:02 roberto Exp roberto $
|
||||
|
||||
#define STACK_SIZE 128
|
||||
|
||||
#ifndef STACK_LIMIT
|
||||
#define STACK_LIMIT 6000
|
||||
#endif
|
||||
|
||||
typedef int StkId; /* index to stack elements */
|
||||
|
||||
static Object initial_stack;
|
||||
@@ -53,6 +57,11 @@ static int CnResults = 0; /* when Lua calls C, has the number of parameters; */
|
||||
static jmp_buf *errorJmp = NULL; /* current error recover point */
|
||||
|
||||
|
||||
/* Hooks */
|
||||
lua_LHFunction lua_linehook = NULL;
|
||||
lua_CHFunction lua_callhook = NULL;
|
||||
|
||||
|
||||
static StkId lua_execute (Byte *pc, StkId base);
|
||||
static void do_call (StkId base, int nResults);
|
||||
|
||||
@@ -64,7 +73,6 @@ Object *luaI_Address (lua_Object o)
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Init stack
|
||||
*/
|
||||
@@ -89,14 +97,17 @@ static void growstack (void)
|
||||
lua_initstack();
|
||||
else
|
||||
{
|
||||
static int limit = STACK_LIMIT;
|
||||
StkId t = top-stack;
|
||||
Long maxstack = stackLimit - stack;
|
||||
maxstack *= 2;
|
||||
stack = growvector(stack, maxstack, Object);
|
||||
stackLimit = stack+maxstack;
|
||||
Long stacksize = stackLimit - stack;
|
||||
stacksize = growvector(&stack, stacksize, Object, stackEM, limit+100);
|
||||
stackLimit = stack+stacksize;
|
||||
top = stack + t;
|
||||
if (maxstack >= MAX_WORD/2)
|
||||
lua_error("stack size overflow");
|
||||
if (stacksize >= limit)
|
||||
{
|
||||
limit = stacksize;
|
||||
lua_error(stackEM);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,18 +117,9 @@ static void growstack (void)
|
||||
*/
|
||||
static char *lua_strconc (char *l, char *r)
|
||||
{
|
||||
static char *buffer = NULL;
|
||||
static int buffer_size = 0;
|
||||
int nl = strlen(l);
|
||||
int n = nl+strlen(r)+1;
|
||||
if (n > buffer_size)
|
||||
{
|
||||
buffer_size = n;
|
||||
if (buffer != NULL)
|
||||
luaI_free(buffer);
|
||||
buffer = newvector(buffer_size, char);
|
||||
}
|
||||
strcpy(buffer,l);
|
||||
int nl = strlen(l);
|
||||
char *buffer = luaI_buffer(nl+strlen(r)+1);
|
||||
strcpy(buffer, l);
|
||||
strcpy(buffer+nl, r);
|
||||
return buffer;
|
||||
}
|
||||
@@ -150,7 +152,7 @@ static int lua_tonumber (Object *obj)
|
||||
*/
|
||||
static int lua_tostring (Object *obj)
|
||||
{
|
||||
static char s[256];
|
||||
char s[256];
|
||||
if (tag(obj) != LUA_T_NUMBER)
|
||||
return 1;
|
||||
if ((int) nvalue(obj) == nvalue(obj))
|
||||
@@ -158,8 +160,6 @@ static int lua_tostring (Object *obj)
|
||||
else
|
||||
sprintf (s, "%g", nvalue(obj));
|
||||
tsvalue(obj) = lua_createstring(s);
|
||||
if (tsvalue(obj) == NULL)
|
||||
return 1;
|
||||
tag(obj) = LUA_T_STRING;
|
||||
return 0;
|
||||
}
|
||||
@@ -192,6 +192,48 @@ static void open_stack (int nelems)
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** call Line hook
|
||||
*/
|
||||
static void lineHook (int line)
|
||||
{
|
||||
StkId oldBase = CBase;
|
||||
int oldCnResults = CnResults;
|
||||
StkId old_top = CBase = top-stack;
|
||||
CnResults = 0;
|
||||
(*lua_linehook)(line);
|
||||
top = stack+old_top;
|
||||
CnResults = oldCnResults;
|
||||
CBase = oldBase;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Call hook
|
||||
** The function being called is in [stack+base-1]
|
||||
*/
|
||||
static void callHook (StkId base, lua_Type type, int isreturn)
|
||||
{
|
||||
StkId oldBase = CBase;
|
||||
int oldCnResults = CnResults;
|
||||
StkId old_top = CBase = top-stack;
|
||||
CnResults = 0;
|
||||
if (isreturn)
|
||||
(*lua_callhook)(LUA_NOOBJECT, "(return)", 0);
|
||||
else
|
||||
{
|
||||
Object *f = stack+base-1;
|
||||
if (type == LUA_T_MARK)
|
||||
(*lua_callhook)(Ref(f), f->value.tf->fileName, f->value.tf->lineDefined);
|
||||
else
|
||||
(*lua_callhook)(Ref(f), "(C)", -1);
|
||||
}
|
||||
top = stack+old_top;
|
||||
CnResults = oldCnResults;
|
||||
CBase = oldBase;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Call a C function. CBase will point to the top of the stack,
|
||||
** and CnResults is the number of parameters. Returns an index
|
||||
@@ -204,8 +246,12 @@ static StkId callC (lua_CFunction func, StkId base)
|
||||
StkId firstResult;
|
||||
CnResults = (top-stack) - base;
|
||||
/* incorporate parameters on the stack */
|
||||
CBase = base+CnResults;
|
||||
CBase = base+CnResults; /* == top-stack */
|
||||
if (lua_callhook)
|
||||
callHook(base, LUA_T_CMARK, 0);
|
||||
(*func)();
|
||||
if (lua_callhook) /* func may have changed lua_callhook */
|
||||
callHook(base, LUA_T_CMARK, 1);
|
||||
firstResult = CBase;
|
||||
CBase = oldBase;
|
||||
CnResults = oldCnResults;
|
||||
@@ -303,6 +349,18 @@ static void storesubscript (void)
|
||||
}
|
||||
|
||||
|
||||
static void getglobal (Word n)
|
||||
{
|
||||
*top = lua_table[n].object;
|
||||
incr_top;
|
||||
if (tag(top-1) == LUA_T_NIL)
|
||||
{ /* must call getglobal fallback */
|
||||
tag(top-1) = LUA_T_STRING;
|
||||
tsvalue(top-1) = lua_table[n].varname;
|
||||
callFB(FB_GETGLOBAL);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Traverse all objects on stack
|
||||
*/
|
||||
@@ -340,7 +398,7 @@ void lua_error (char *s)
|
||||
}
|
||||
|
||||
|
||||
lua_Object lua_stackedfunction (int level)
|
||||
lua_Function lua_stackedfunction (int level)
|
||||
{
|
||||
Object *p = top;
|
||||
while (--p >= stack)
|
||||
@@ -351,13 +409,45 @@ lua_Object lua_stackedfunction (int level)
|
||||
}
|
||||
|
||||
|
||||
int lua_currentline (lua_Object func)
|
||||
int lua_currentline (lua_Function func)
|
||||
{
|
||||
Object *f = Address(func);
|
||||
return (f+1 < top && (f+1)->tag == LUA_T_LINE) ? (f+1)->value.i : -1;
|
||||
}
|
||||
|
||||
|
||||
lua_Object lua_getlocal (lua_Function func, int local_number, char **name)
|
||||
{
|
||||
Object *f = luaI_Address(func);
|
||||
*name = luaI_getlocalname(f->value.tf, local_number, lua_currentline(func));
|
||||
if (*name)
|
||||
{
|
||||
/* if "*name", there must be a LUA_T_LINE */
|
||||
/* therefore, f+2 points to function base */
|
||||
return Ref((f+2)+(local_number-1));
|
||||
}
|
||||
else
|
||||
return LUA_NOOBJECT;
|
||||
}
|
||||
|
||||
int lua_setlocal (lua_Function func, int local_number)
|
||||
{
|
||||
Object *f = Address(func);
|
||||
char *name = luaI_getlocalname(f->value.tf, local_number, lua_currentline(func));
|
||||
adjustC(1);
|
||||
--top;
|
||||
if (name)
|
||||
{
|
||||
/* if "name", there must be a LUA_T_LINE */
|
||||
/* therefore, f+2 points to function base */
|
||||
*((f+2)+(local_number-1)) = *top;
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Execute a protected call. Assumes that function is at CBase and
|
||||
** parameters are on top of it. Leave nResults on the stack.
|
||||
@@ -386,6 +476,16 @@ static int do_protectedrun (int nResults)
|
||||
return status;
|
||||
}
|
||||
|
||||
int luaI_dorun (TFunc *tf)
|
||||
{
|
||||
int status;
|
||||
adjustC(1); /* one slot for the pseudo-function */
|
||||
stack[CBase].tag = LUA_T_FUNCTION;
|
||||
stack[CBase].value.tf = tf;
|
||||
status = do_protectedrun(0);
|
||||
adjustC(0);
|
||||
return status;
|
||||
}
|
||||
|
||||
static int do_protectedmain (void)
|
||||
{
|
||||
@@ -394,16 +494,12 @@ static int do_protectedmain (void)
|
||||
jmp_buf myErrorJmp;
|
||||
jmp_buf *oldErr = errorJmp;
|
||||
errorJmp = &myErrorJmp;
|
||||
adjustC(1); /* one slot for the pseudo-function */
|
||||
stack[CBase].tag = LUA_T_FUNCTION;
|
||||
stack[CBase].value.tf = &tf;
|
||||
tf.lineDefined = 0;
|
||||
luaI_initTFunc(&tf);
|
||||
tf.fileName = lua_parsedfile;
|
||||
tf.code = NULL;
|
||||
if (setjmp(myErrorJmp) == 0)
|
||||
{
|
||||
lua_parse(&tf);
|
||||
status = do_protectedrun(0);
|
||||
status = luaI_dorun(&tf);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -411,8 +507,7 @@ static int do_protectedmain (void)
|
||||
adjustC(0); /* erase extra slot */
|
||||
}
|
||||
errorJmp = oldErr;
|
||||
if (tf.code)
|
||||
luaI_free(tf.code);
|
||||
luaI_free(tf.code);
|
||||
return status;
|
||||
}
|
||||
|
||||
@@ -449,13 +544,13 @@ int lua_call (char *funcname)
|
||||
int lua_dofile (char *filename)
|
||||
{
|
||||
int status;
|
||||
char *message = lua_openfile (filename);
|
||||
if (message)
|
||||
{
|
||||
lua_message(message);
|
||||
int c;
|
||||
FILE *f = lua_openfile(filename);
|
||||
if (f == NULL)
|
||||
return 1;
|
||||
}
|
||||
status = do_protectedmain();
|
||||
c = fgetc(f);
|
||||
ungetc(c, f);
|
||||
status = (c == ID_CHUNK) ? luaI_undump(f) : do_protectedmain();
|
||||
lua_closefile();
|
||||
return status;
|
||||
}
|
||||
@@ -514,8 +609,9 @@ static StkId Cblocks[MAX_C_BLOCKS];
|
||||
*/
|
||||
void lua_beginblock (void)
|
||||
{
|
||||
if (numCblocks < MAX_C_BLOCKS)
|
||||
Cblocks[numCblocks] = CBase;
|
||||
if (numCblocks >= MAX_C_BLOCKS)
|
||||
lua_error("`lua_beginblock': too many nested blocks");
|
||||
Cblocks[numCblocks] = CBase;
|
||||
numCblocks++;
|
||||
}
|
||||
|
||||
@@ -525,11 +621,8 @@ void lua_beginblock (void)
|
||||
void lua_endblock (void)
|
||||
{
|
||||
--numCblocks;
|
||||
if (numCblocks < MAX_C_BLOCKS)
|
||||
{
|
||||
CBase = Cblocks[numCblocks];
|
||||
adjustC(0);
|
||||
}
|
||||
CBase = Cblocks[numCblocks];
|
||||
adjustC(0);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -566,6 +659,23 @@ lua_Object lua_getparam (int number)
|
||||
return CBase-CnResults+number;
|
||||
}
|
||||
|
||||
int lua_isnumber (lua_Object object)
|
||||
{
|
||||
return (object != LUA_NOOBJECT) && (tonumber(Address(object)) == 0);
|
||||
}
|
||||
|
||||
int lua_isstring (lua_Object object)
|
||||
{
|
||||
int t = lua_type(object);
|
||||
return (t == LUA_T_STRING) || (t == LUA_T_NUMBER);
|
||||
}
|
||||
|
||||
int lua_isfunction (lua_Object object)
|
||||
{
|
||||
int t = lua_type(object);
|
||||
return (t == LUA_T_FUNCTION) || (t == LUA_T_CFUNCTION);
|
||||
}
|
||||
|
||||
/*
|
||||
** Given an object handle, return its number value. On error, return 0.0.
|
||||
*/
|
||||
@@ -637,10 +747,8 @@ int lua_lock (void)
|
||||
*/
|
||||
lua_Object lua_getglobal (char *name)
|
||||
{
|
||||
Word n = luaI_findsymbolbyname(name);
|
||||
adjustC(0);
|
||||
*top = s_object(n);
|
||||
incr_top;
|
||||
getglobal(luaI_findsymbolbyname(name));
|
||||
CBase++; /* incorporate object in the stack */
|
||||
return Ref(top-1);
|
||||
}
|
||||
@@ -678,20 +786,17 @@ void lua_pushnumber (real n)
|
||||
*/
|
||||
void lua_pushstring (char *s)
|
||||
{
|
||||
tsvalue(top) = lua_createstring(s);
|
||||
tag(top) = LUA_T_STRING;
|
||||
incr_top;
|
||||
}
|
||||
|
||||
/*
|
||||
** Push an object (tag=string) on stack and register it on the constant table.
|
||||
*/
|
||||
void lua_pushliteral (char *s)
|
||||
{
|
||||
tsvalue(top) = lua_constant[luaI_findconstantbyname(s)];
|
||||
tag(top) = LUA_T_STRING;
|
||||
incr_top;
|
||||
if (s == NULL)
|
||||
tag(top) = LUA_T_NIL;
|
||||
else
|
||||
{
|
||||
tsvalue(top) = lua_createstring(s);
|
||||
tag(top) = LUA_T_STRING;
|
||||
}
|
||||
incr_top;
|
||||
}
|
||||
/*>>>>>>>>>#undef lua_pushliteral
|
||||
void lua_pushliteral(char *s) { lua_pushstring(s); }*/
|
||||
|
||||
/*
|
||||
** Push an object (tag=cfunction) to stack.
|
||||
@@ -783,6 +888,8 @@ static void comparison (lua_Type tag_less, lua_Type tag_equal,
|
||||
*/
|
||||
static StkId lua_execute (Byte *pc, StkId base)
|
||||
{
|
||||
if (lua_callhook)
|
||||
callHook (base, LUA_T_MARK, 0);
|
||||
while (1)
|
||||
{
|
||||
OpCode opcode;
|
||||
@@ -849,8 +956,7 @@ static StkId lua_execute (Byte *pc, StkId base)
|
||||
{
|
||||
CodeWord code;
|
||||
get_word(code,pc);
|
||||
*top = s_object(code.w);
|
||||
incr_top;
|
||||
getglobal(code.w);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -1143,10 +1249,10 @@ static StkId lua_execute (Byte *pc, StkId base)
|
||||
break;
|
||||
|
||||
case RETCODE0:
|
||||
return base;
|
||||
|
||||
case RETCODE:
|
||||
return base+*pc;
|
||||
if (lua_callhook)
|
||||
callHook (base, LUA_T_MARK, 1);
|
||||
return (base + ((opcode==RETCODE0) ? 0 : *pc));
|
||||
|
||||
case SETLINE:
|
||||
{
|
||||
@@ -1160,6 +1266,8 @@ static StkId lua_execute (Byte *pc, StkId base)
|
||||
(stack+base-1)->tag = LUA_T_LINE;
|
||||
}
|
||||
(stack+base-1)->value.i = code.w;
|
||||
if (lua_linehook)
|
||||
lineHook (code.w);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -1169,4 +1277,3 @@ static StkId lua_execute (Byte *pc, StkId base)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
26
opcode.h
26
opcode.h
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
** TeCGraf - PUC-Rio
|
||||
** $Id: opcode.h,v 3.13 1995/10/17 11:58:41 roberto Exp roberto $
|
||||
** $Id: opcode.h,v 3.19 1996/03/06 13:11:23 roberto Exp $
|
||||
*/
|
||||
|
||||
#ifndef opcode_h
|
||||
@@ -11,9 +11,6 @@
|
||||
#include "tree.h"
|
||||
#include "func.h"
|
||||
|
||||
#ifndef real
|
||||
#define real float
|
||||
#endif
|
||||
|
||||
#define FIELDS_PER_FLUSH 40
|
||||
|
||||
@@ -74,12 +71,9 @@ typedef enum
|
||||
#define MULT_RET 255
|
||||
|
||||
|
||||
typedef void (*Cfunction) (void);
|
||||
typedef int (*Input) (void);
|
||||
|
||||
typedef union
|
||||
{
|
||||
Cfunction f;
|
||||
lua_CFunction f;
|
||||
real n;
|
||||
TaggedString *ts;
|
||||
TFunc *tf;
|
||||
@@ -94,10 +88,6 @@ typedef struct Object
|
||||
Value value;
|
||||
} Object;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
Object object;
|
||||
} Symbol;
|
||||
|
||||
/* Macros to access structure members */
|
||||
#define tag(o) ((o)->tag)
|
||||
@@ -119,14 +109,14 @@ typedef struct
|
||||
|
||||
typedef union
|
||||
{
|
||||
struct {char c1; char c2;} m;
|
||||
struct {Byte c1; Byte c2;} m;
|
||||
Word w;
|
||||
} CodeWord;
|
||||
#define get_word(code,pc) {code.m.c1 = *pc++; code.m.c2 = *pc++;}
|
||||
|
||||
typedef union
|
||||
{
|
||||
struct {char c1; char c2; char c3; char c4;} m;
|
||||
struct {Byte c1; Byte c2; Byte c3; Byte c4;} m;
|
||||
float f;
|
||||
} CodeFloat;
|
||||
#define get_float(code,pc) {code.m.c1 = *pc++; code.m.c2 = *pc++;\
|
||||
@@ -134,7 +124,7 @@ typedef union
|
||||
|
||||
typedef union
|
||||
{
|
||||
struct {char c1; char c2; char c3; char c4;} m;
|
||||
struct {Byte c1; Byte c2; Byte c3; Byte c4;} m;
|
||||
TFunc *tf;
|
||||
} CodeCode;
|
||||
#define get_code(code,pc) {code.m.c1 = *pc++; code.m.c2 = *pc++;\
|
||||
@@ -142,16 +132,12 @@ typedef union
|
||||
|
||||
|
||||
/* Exported functions */
|
||||
char *lua_strdup (char *l);
|
||||
|
||||
void lua_setinput (Input fn); /* from "lex.c" module */
|
||||
char *lua_lasttext (void); /* from "lex.c" module */
|
||||
int yylex (void); /* from "lex.c" module */
|
||||
void lua_parse (TFunc *tf); /* from "lua.stx" module */
|
||||
void luaI_codedebugline (int line); /* from "lua.stx" module */
|
||||
void lua_travstack (int (*fn)(Object *));
|
||||
Object *luaI_Address (lua_Object o);
|
||||
void luaI_pushobject (Object *o);
|
||||
void luaI_gcFB (Object *o);
|
||||
int luaI_dorun (TFunc *tf);
|
||||
|
||||
#endif
|
||||
|
||||
186
strlib.c
186
strlib.c
@@ -3,12 +3,13 @@
|
||||
** String library to LUA
|
||||
*/
|
||||
|
||||
char *rcs_strlib="$Id: strlib.c,v 1.13 1995/10/09 12:49:21 roberto Exp roberto $";
|
||||
char *rcs_strlib="$Id: strlib.c,v 1.21 1996/03/21 22:18:08 roberto Exp roberto $";
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include "lua.h"
|
||||
#include "lualib.h"
|
||||
@@ -24,12 +25,12 @@ void lua_arg_error(char *funcname)
|
||||
char *lua_check_string (int numArg, char *funcname)
|
||||
{
|
||||
lua_Object o = lua_getparam(numArg);
|
||||
if (!(lua_isstring(o) || lua_isnumber(o)))
|
||||
if (!lua_isstring(o))
|
||||
lua_arg_error(funcname);
|
||||
return lua_getstring(o);
|
||||
}
|
||||
|
||||
float lua_check_number (int numArg, char *funcname)
|
||||
double lua_check_number (int numArg, char *funcname)
|
||||
{
|
||||
lua_Object o = lua_getparam(numArg);
|
||||
if (!lua_isnumber(o))
|
||||
@@ -37,13 +38,36 @@ float lua_check_number (int numArg, char *funcname)
|
||||
return lua_getnumber(o);
|
||||
}
|
||||
|
||||
static char *newstring (char *s)
|
||||
static int lua_opt_number (int numArg, int def, char *funcname)
|
||||
{
|
||||
char *ns = (char *)malloc(strlen(s)+1);
|
||||
if (ns == 0)
|
||||
lua_error("not enough memory for new string");
|
||||
strcpy(ns, s);
|
||||
return ns;
|
||||
return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
|
||||
(int)lua_check_number(numArg, funcname);
|
||||
}
|
||||
|
||||
char *luaI_addchar (int c)
|
||||
{
|
||||
static char *buff = NULL;
|
||||
static int max = 0;
|
||||
static int n = 0;
|
||||
if (n >= max)
|
||||
{
|
||||
if (max == 0)
|
||||
{
|
||||
max = 100;
|
||||
buff = (char *)malloc(max);
|
||||
}
|
||||
else
|
||||
{
|
||||
max *= 2;
|
||||
buff = (char *)realloc(buff, max);
|
||||
}
|
||||
if (buff == NULL)
|
||||
lua_error("memory overflow");
|
||||
}
|
||||
buff[n++] = c;
|
||||
if (c == 0)
|
||||
n = 0; /* prepare for next string */
|
||||
return buff;
|
||||
}
|
||||
|
||||
|
||||
@@ -56,15 +80,12 @@ static void str_find (void)
|
||||
{
|
||||
char *s1 = lua_check_string(1, "strfind");
|
||||
char *s2 = lua_check_string(2, "strfind");
|
||||
int init = (lua_getparam(3) == LUA_NOOBJECT) ? 0 :
|
||||
(int)lua_check_number(3, "strfind")-1;
|
||||
char *f = strstr(s1+init,s2);
|
||||
int init = lua_opt_number(3, 1, "strfind") - 1;
|
||||
char *f = (init>=0 && init<=strlen(s1)) ? strstr(s1+init,s2) : NULL;
|
||||
if (f != NULL)
|
||||
{
|
||||
int pos = f-s1+1;
|
||||
if (lua_getparam (4) == LUA_NOOBJECT)
|
||||
lua_pushnumber (pos);
|
||||
else if ((int)lua_check_number(4, "strfind") >= pos+strlen(s2)-1)
|
||||
if (lua_opt_number(4, INT_MAX, "strfind") >= pos+strlen(s2)-1)
|
||||
lua_pushnumber (pos);
|
||||
else
|
||||
lua_pushnil();
|
||||
@@ -94,36 +115,18 @@ static void str_sub (void)
|
||||
{
|
||||
char *s = lua_check_string(1, "strsub");
|
||||
int start = (int)lua_check_number(2, "strsub");
|
||||
int end = (lua_getparam(3) == LUA_NOOBJECT) ? strlen(s) :
|
||||
(int)lua_check_number(3, "strsub");
|
||||
int end = lua_opt_number(3, strlen(s), "strsub");
|
||||
if (end < start || start < 1 || end > strlen(s))
|
||||
lua_pushliteral("");
|
||||
else
|
||||
{
|
||||
char temp = s[end];
|
||||
s[end] = 0;
|
||||
lua_pushstring (&s[start-1]);
|
||||
s[end] = temp;
|
||||
luaI_addchar(0);
|
||||
while (start <= end)
|
||||
luaI_addchar(s[start++ - 1]);
|
||||
lua_pushstring (luaI_addchar(0));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Convert a string according to given function.
|
||||
*/
|
||||
typedef int (*strfunc)(int s);
|
||||
static void str_apply (strfunc f, char *funcname)
|
||||
{
|
||||
char *s, *c;
|
||||
c = s = newstring(lua_check_string(1, funcname));
|
||||
while (*c != 0)
|
||||
{
|
||||
*c = f(*c);
|
||||
c++;
|
||||
}
|
||||
lua_pushstring(s);
|
||||
free(s);
|
||||
}
|
||||
|
||||
/*
|
||||
** Convert a string to lower case.
|
||||
** LUA interface:
|
||||
@@ -131,7 +134,11 @@ static void str_apply (strfunc f, char *funcname)
|
||||
*/
|
||||
static void str_lower (void)
|
||||
{
|
||||
str_apply(tolower, "strlower");
|
||||
char *s = lua_check_string(1, "strlower");
|
||||
luaI_addchar(0);
|
||||
while (*s)
|
||||
luaI_addchar(tolower(*s++));
|
||||
lua_pushstring(luaI_addchar(0));
|
||||
}
|
||||
|
||||
|
||||
@@ -142,7 +149,11 @@ static void str_lower (void)
|
||||
*/
|
||||
static void str_upper (void)
|
||||
{
|
||||
str_apply(toupper, "strupper");
|
||||
char *s = lua_check_string(1, "strupper");
|
||||
luaI_addchar(0);
|
||||
while (*s)
|
||||
luaI_addchar(toupper(*s++));
|
||||
lua_pushstring(luaI_addchar(0));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -151,32 +162,93 @@ static void str_upper (void)
|
||||
static void str_ascii (void)
|
||||
{
|
||||
char *s = lua_check_string(1, "ascii");
|
||||
lua_Object o2 = lua_getparam(2);
|
||||
int pos;
|
||||
pos = (o2 == LUA_NOOBJECT) ? 0 : (int)lua_check_number(2, "ascii")-1;
|
||||
int pos = lua_opt_number(2, 1, "ascii") - 1;
|
||||
if (pos<0 || pos>=strlen(s))
|
||||
lua_arg_error("ascii");
|
||||
lua_pushnumber(s[pos]);
|
||||
}
|
||||
|
||||
/*
|
||||
** converts one or more integers to chars in a string
|
||||
*/
|
||||
#define maxparams 50
|
||||
static void str_int2str (void)
|
||||
void luaI_addquoted (char *s)
|
||||
{
|
||||
char s[maxparams+1];
|
||||
int i = 0;
|
||||
while (lua_getparam(++i) != LUA_NOOBJECT)
|
||||
luaI_addchar('"');
|
||||
for (; *s; s++)
|
||||
{
|
||||
if (i > maxparams)
|
||||
lua_error("too many parameters to function `int2str'");
|
||||
s[i-1] = (int)lua_check_number(i, "int2str");
|
||||
if (*s == '"' || *s == '\\' || *s == '\n')
|
||||
luaI_addchar('\\');
|
||||
luaI_addchar(*s);
|
||||
}
|
||||
s[i-1] = 0;
|
||||
lua_pushstring(s);
|
||||
luaI_addchar('"');
|
||||
}
|
||||
|
||||
#define MAX_CONVERTION 2000
|
||||
#define MAX_FORMAT 50
|
||||
|
||||
static void str_format (void)
|
||||
{
|
||||
int arg = 1;
|
||||
char *strfrmt = lua_check_string(arg++, "format");
|
||||
luaI_addchar(0); /* initialize */
|
||||
while (*strfrmt)
|
||||
{
|
||||
if (*strfrmt != '%')
|
||||
luaI_addchar(*strfrmt++);
|
||||
else if (*++strfrmt == '%')
|
||||
luaI_addchar(*strfrmt++); /* %% */
|
||||
else
|
||||
{ /* format item */
|
||||
char form[MAX_FORMAT]; /* store the format ('%...') */
|
||||
char buff[MAX_CONVERTION]; /* store the formated value */
|
||||
int size = 0;
|
||||
int i = 0;
|
||||
form[i++] = '%';
|
||||
form[i] = *strfrmt++;
|
||||
while (!isalpha(form[i]))
|
||||
{
|
||||
if (isdigit(form[i]))
|
||||
{
|
||||
size = size*10 + form[i]-'0';
|
||||
if (size >= MAX_CONVERTION)
|
||||
lua_error("format size/precision too long in function `format'");
|
||||
}
|
||||
else if (form[i] == '.')
|
||||
size = 0; /* re-start */
|
||||
if (++i >= MAX_FORMAT)
|
||||
lua_error("bad format in function `format'");
|
||||
form[i] = *strfrmt++;
|
||||
}
|
||||
form[i+1] = 0; /* ends string */
|
||||
switch (form[i])
|
||||
{
|
||||
case 'q':
|
||||
luaI_addquoted(lua_check_string(arg++, "format"));
|
||||
buff[0] = '\0'; /* addchar already done */
|
||||
break;
|
||||
case 's':
|
||||
{
|
||||
char *s = lua_check_string(arg++, "format");
|
||||
if (strlen(s) >= MAX_CONVERTION)
|
||||
lua_error("string argument too long in function `format'");
|
||||
sprintf(buff, form, s);
|
||||
break;
|
||||
}
|
||||
case 'c': case 'd': case 'i': case 'o':
|
||||
case 'u': case 'x': case 'X':
|
||||
sprintf(buff, form, (int)lua_check_number(arg++, "format"));
|
||||
break;
|
||||
case 'e': case 'E': case 'f': case 'g':
|
||||
sprintf(buff, form, lua_check_number(arg++, "format"));
|
||||
break;
|
||||
default: /* also treat cases 'pnLlh' */
|
||||
lua_error("invalid format option in function `format'");
|
||||
}
|
||||
for (i=0; buff[i]; i++) /* move formated value to result */
|
||||
luaI_addchar(buff[i]);
|
||||
}
|
||||
}
|
||||
lua_pushstring(luaI_addchar(0)); /* push the result */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Open string library
|
||||
*/
|
||||
@@ -188,5 +260,5 @@ void strlib_open (void)
|
||||
lua_register ("strlower", str_lower);
|
||||
lua_register ("strupper", str_upper);
|
||||
lua_register ("ascii", str_ascii);
|
||||
lua_register ("int2str", str_int2str);
|
||||
lua_register ("format", str_format);
|
||||
}
|
||||
|
||||
162
table.c
162
table.c
@@ -3,9 +3,7 @@
|
||||
** Module to control static tables
|
||||
*/
|
||||
|
||||
char *rcs_table="$Id: table.c,v 2.37 1995/10/26 14:21:56 roberto Exp roberto $";
|
||||
|
||||
/*#include <string.h>*/
|
||||
char *rcs_table="$Id: table.c,v 2.50 1996/03/21 16:31:32 roberto Exp roberto $";
|
||||
|
||||
#include "mem.h"
|
||||
#include "opcode.h"
|
||||
@@ -21,11 +19,11 @@ char *rcs_table="$Id: table.c,v 2.37 1995/10/26 14:21:56 roberto Exp roberto $";
|
||||
#define BUFFER_BLOCK 256
|
||||
|
||||
Symbol *lua_table = NULL;
|
||||
static Word lua_ntable = 0;
|
||||
Word lua_ntable = 0;
|
||||
static Long lua_maxsymbol = 0;
|
||||
|
||||
TaggedString **lua_constant = NULL;
|
||||
static Word lua_nconstant = 0;
|
||||
Word lua_nconstant = 0;
|
||||
static Long lua_maxconstant = 0;
|
||||
|
||||
|
||||
@@ -33,49 +31,55 @@ static Long lua_maxconstant = 0;
|
||||
#define MIN_GARBAGE_BLOCK (GARBAGE_BLOCK/2)
|
||||
|
||||
static void lua_nextvar (void);
|
||||
static void setglobal (void);
|
||||
static void getglobal (void);
|
||||
|
||||
/*
|
||||
** Initialise symbol table with internal functions
|
||||
** Internal functions
|
||||
*/
|
||||
static void lua_initsymbol (void)
|
||||
static struct {
|
||||
char *name;
|
||||
lua_CFunction func;
|
||||
} int_funcs[] = {
|
||||
{"assert", luaI_assert},
|
||||
{"dofile", lua_internaldofile},
|
||||
{"dostring", lua_internaldostring},
|
||||
{"error", luaI_error},
|
||||
{"getglobal", luaI_getglobal},
|
||||
{"next", lua_next},
|
||||
{"nextvar", lua_nextvar},
|
||||
{"print", luaI_print},
|
||||
{"setfallback", luaI_setfallback},
|
||||
{"setglobal", luaI_setglobal},
|
||||
{"tonumber", lua_obj2number},
|
||||
{"tostring", luaI_tostring},
|
||||
{"type", luaI_type}
|
||||
};
|
||||
|
||||
#define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0]))
|
||||
|
||||
|
||||
void luaI_initsymbol (void)
|
||||
{
|
||||
Word n;
|
||||
lua_maxsymbol = BUFFER_BLOCK;
|
||||
lua_table = newvector(lua_maxsymbol, Symbol);
|
||||
n = luaI_findsymbolbyname("next");
|
||||
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_next;
|
||||
n = luaI_findsymbolbyname("dofile");
|
||||
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_internaldofile;
|
||||
n = luaI_findsymbolbyname("setglobal");
|
||||
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = setglobal;
|
||||
n = luaI_findsymbolbyname("getglobal");
|
||||
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = getglobal;
|
||||
n = luaI_findsymbolbyname("nextvar");
|
||||
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_nextvar;
|
||||
n = luaI_findsymbolbyname("type");
|
||||
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_type;
|
||||
n = luaI_findsymbolbyname("tonumber");
|
||||
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_obj2number;
|
||||
n = luaI_findsymbolbyname("print");
|
||||
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_print;
|
||||
n = luaI_findsymbolbyname("dostring");
|
||||
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_internaldostring;
|
||||
n = luaI_findsymbolbyname("setfallback");
|
||||
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_setfallback;
|
||||
n = luaI_findsymbolbyname("error");
|
||||
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_error;
|
||||
int i;
|
||||
lua_maxsymbol = BUFFER_BLOCK;
|
||||
lua_table = newvector(lua_maxsymbol, Symbol);
|
||||
for (i=0; i<INTFUNCSIZE; i++)
|
||||
{
|
||||
Word n = luaI_findsymbolbyname(int_funcs[i].name);
|
||||
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = int_funcs[i].func;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Initialise constant table with pre-defined constants
|
||||
*/
|
||||
void lua_initconstant (void)
|
||||
void luaI_initconstant (void)
|
||||
{
|
||||
lua_maxconstant = BUFFER_BLOCK;
|
||||
lua_constant = newvector(lua_maxconstant, TaggedString *);
|
||||
/* pre-register mem error messages, to avoid loop when error arises */
|
||||
luaI_findconstantbyname(tableEM);
|
||||
luaI_findconstantbyname(memEM);
|
||||
}
|
||||
|
||||
|
||||
@@ -83,22 +87,15 @@ void lua_initconstant (void)
|
||||
** Given a name, search it at symbol table and return its index. If not
|
||||
** found, allocate it.
|
||||
*/
|
||||
Word luaI_findsymbol (TreeNode *t)
|
||||
Word luaI_findsymbol (TaggedString *t)
|
||||
{
|
||||
if (lua_table == NULL)
|
||||
lua_initsymbol();
|
||||
if (t->varindex == NOT_USED)
|
||||
{
|
||||
if (lua_ntable == lua_maxsymbol)
|
||||
{
|
||||
if (lua_maxsymbol >= MAX_WORD)
|
||||
lua_error("symbol table overflow");
|
||||
lua_maxsymbol *= 2;
|
||||
if (lua_maxsymbol >= MAX_WORD)
|
||||
lua_maxsymbol = MAX_WORD;
|
||||
lua_table = growvector(lua_table, lua_maxsymbol, Symbol);
|
||||
}
|
||||
lua_maxsymbol = growvector(&lua_table, lua_maxsymbol, Symbol,
|
||||
symbolEM, MAX_WORD);
|
||||
t->varindex = lua_ntable;
|
||||
lua_table[lua_ntable].varname = t;
|
||||
s_tag(lua_ntable) = LUA_T_NIL;
|
||||
lua_ntable++;
|
||||
}
|
||||
@@ -108,7 +105,7 @@ Word luaI_findsymbol (TreeNode *t)
|
||||
|
||||
Word luaI_findsymbolbyname (char *name)
|
||||
{
|
||||
return luaI_findsymbol(lua_constcreate(name));
|
||||
return luaI_findsymbol(luaI_createfixedstring(name));
|
||||
}
|
||||
|
||||
|
||||
@@ -116,23 +113,15 @@ Word luaI_findsymbolbyname (char *name)
|
||||
** Given a tree node, check it is has a correspondent constant index. If not,
|
||||
** allocate it.
|
||||
*/
|
||||
Word luaI_findconstant (TreeNode *t)
|
||||
Word luaI_findconstant (TaggedString *t)
|
||||
{
|
||||
if (lua_constant == NULL)
|
||||
lua_initconstant();
|
||||
if (t->constindex == NOT_USED)
|
||||
{
|
||||
if (lua_nconstant == lua_maxconstant)
|
||||
{
|
||||
if (lua_maxconstant >= MAX_WORD)
|
||||
lua_error("constant table overflow");
|
||||
lua_maxconstant *= 2;
|
||||
if (lua_maxconstant >= MAX_WORD)
|
||||
lua_maxconstant = MAX_WORD;
|
||||
lua_constant = growvector(lua_constant, lua_maxconstant, TaggedString *);
|
||||
}
|
||||
lua_maxconstant = growvector(&lua_constant, lua_maxconstant, TaggedString *,
|
||||
constantEM, MAX_WORD);
|
||||
t->constindex = lua_nconstant;
|
||||
lua_constant[lua_nconstant] = &(t->ts);
|
||||
lua_constant[lua_nconstant] = t;
|
||||
lua_nconstant++;
|
||||
}
|
||||
return t->constindex;
|
||||
@@ -141,7 +130,15 @@ Word luaI_findconstant (TreeNode *t)
|
||||
|
||||
Word luaI_findconstantbyname (char *name)
|
||||
{
|
||||
return luaI_findconstant(lua_constcreate(name));
|
||||
return luaI_findconstant(luaI_createfixedstring(name));
|
||||
}
|
||||
|
||||
TaggedString *luaI_createfixedstring (char *name)
|
||||
{
|
||||
TaggedString *ts = lua_createstring(name);
|
||||
if (!ts->marked)
|
||||
ts->marked = 2; /* avoid GC */
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
@@ -153,7 +150,7 @@ static char *lua_travsymbol (int (*fn)(Object *))
|
||||
Word i;
|
||||
for (i=0; i<lua_ntable; i++)
|
||||
if (fn(&s_object(i)))
|
||||
return luaI_nodebysymbol(i)->ts.str;
|
||||
return lua_table[i].varname->str;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -162,7 +159,7 @@ static char *lua_travsymbol (int (*fn)(Object *))
|
||||
** Mark an object if it is a string or a unmarked array.
|
||||
*/
|
||||
int lua_markobject (Object *o)
|
||||
{
|
||||
{/* if already marked, does not change mark value */
|
||||
if (tag(o) == LUA_T_STRING && !tsvalue(o)->marked)
|
||||
tsvalue(o)->marked = 1;
|
||||
else if (tag(o) == LUA_T_ARRAY)
|
||||
@@ -178,12 +175,9 @@ int lua_markobject (Object *o)
|
||||
** Garbage collection.
|
||||
** Delete all unused strings and arrays.
|
||||
*/
|
||||
void lua_pack (void)
|
||||
Long luaI_collectgarbage (void)
|
||||
{
|
||||
static Long block = GARBAGE_BLOCK; /* when garbage collector will be called */
|
||||
static Long nentity = 0; /* counter of new entities (strings and arrays) */
|
||||
Long recovered = 0;
|
||||
if (nentity++ < block) return;
|
||||
lua_travstack(lua_markobject); /* mark stack objects */
|
||||
lua_travsymbol(lua_markobject); /* mark symbol table objects */
|
||||
luaI_travlock(lua_markobject); /* mark locked objects */
|
||||
@@ -191,6 +185,16 @@ void lua_pack (void)
|
||||
recovered += lua_strcollector();
|
||||
recovered += lua_hashcollector();
|
||||
recovered += luaI_funccollector();
|
||||
return recovered;
|
||||
}
|
||||
|
||||
void lua_pack (void)
|
||||
{
|
||||
static Long block = GARBAGE_BLOCK; /* when garbage collector will be called */
|
||||
static Long nentity = 0; /* counter of new entities (strings and arrays) */
|
||||
Long recovered = 0;
|
||||
if (nentity++ < block) return;
|
||||
recovered = luaI_collectgarbage();
|
||||
nentity = 0; /* reset counter */
|
||||
block=(16*block-7*recovered)/12; /* adapt block size */
|
||||
if (block < MIN_GARBAGE_BLOCK) block = MIN_GARBAGE_BLOCK;
|
||||
@@ -225,36 +229,16 @@ static void lua_nextvar (void)
|
||||
}
|
||||
else
|
||||
{
|
||||
TreeNode *t = luaI_nodebysymbol(next);
|
||||
TaggedString *t = lua_table[next].varname;
|
||||
Object name;
|
||||
tag(&name) = LUA_T_STRING;
|
||||
tsvalue(&name) = &(t->ts);
|
||||
tsvalue(&name) = t;
|
||||
luaI_pushobject(&name);
|
||||
luaI_pushobject(&s_object(next));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void setglobal (void)
|
||||
{
|
||||
lua_Object name = lua_getparam(1);
|
||||
lua_Object value = lua_getparam(2);
|
||||
if (!lua_isstring(name))
|
||||
lua_error("incorrect argument to function `setglobal'");
|
||||
lua_pushobject(value);
|
||||
lua_storeglobal(lua_getstring(name));
|
||||
}
|
||||
|
||||
|
||||
static void getglobal (void)
|
||||
{
|
||||
lua_Object name = lua_getparam(1);
|
||||
if (!lua_isstring(name))
|
||||
lua_error("incorrect argument to function `getglobal'");
|
||||
lua_pushobject(lua_getglobal(lua_getstring(name)));
|
||||
}
|
||||
|
||||
|
||||
static Object *functofind;
|
||||
static int checkfunc (Object *o)
|
||||
{
|
||||
@@ -270,7 +254,7 @@ static int checkfunc (Object *o)
|
||||
}
|
||||
|
||||
|
||||
char *getobjname (lua_Object o, char **name)
|
||||
char *lua_getobjname (lua_Object o, char **name)
|
||||
{ /* try to find a name for given function */
|
||||
functofind = luaI_Address(o);
|
||||
if ((*name = luaI_travfallbacks(checkfunc)) != NULL)
|
||||
|
||||
24
table.h
24
table.h
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
** Module to control static tables
|
||||
** TeCGraf - PUC-Rio
|
||||
** $Id: table.h,v 2.12 1995/10/17 11:58:41 roberto Exp roberto $
|
||||
** $Id: table.h,v 2.19 1996/02/26 21:00:27 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef table_h
|
||||
@@ -10,19 +10,27 @@
|
||||
#include "tree.h"
|
||||
#include "opcode.h"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
Object object;
|
||||
TaggedString *varname;
|
||||
} Symbol;
|
||||
|
||||
|
||||
extern Symbol *lua_table;
|
||||
extern Word lua_ntable;
|
||||
extern TaggedString **lua_constant;
|
||||
extern Word lua_nconstant;
|
||||
|
||||
extern char *lua_file[];
|
||||
extern int lua_nfile;
|
||||
|
||||
|
||||
void lua_initconstant (void);
|
||||
void luaI_initsymbol (void);
|
||||
void luaI_initconstant (void);
|
||||
Word luaI_findsymbolbyname (char *name);
|
||||
Word luaI_findsymbol (TreeNode *t);
|
||||
Word luaI_findconstant (TreeNode *t);
|
||||
Word luaI_findsymbol (TaggedString *t);
|
||||
Word luaI_findconstant (TaggedString *t);
|
||||
Word luaI_findconstantbyname (char *name);
|
||||
TaggedString *luaI_createfixedstring (char *str);
|
||||
int lua_markobject (Object *o);
|
||||
Long luaI_collectgarbage (void);
|
||||
void lua_pack (void);
|
||||
|
||||
|
||||
|
||||
188
tree.c
188
tree.c
@@ -3,7 +3,7 @@
|
||||
** TecCGraf - PUC-Rio
|
||||
*/
|
||||
|
||||
char *rcs_tree="$Id: tree.c,v 1.13 1995/01/12 14:19:04 roberto Exp roberto $";
|
||||
char *rcs_tree="$Id: tree.c,v 1.19 1996/02/22 20:34:33 roberto Exp $";
|
||||
|
||||
|
||||
#include <string.h>
|
||||
@@ -11,66 +11,104 @@ char *rcs_tree="$Id: tree.c,v 1.13 1995/01/12 14:19:04 roberto Exp roberto $";
|
||||
#include "mem.h"
|
||||
#include "lua.h"
|
||||
#include "tree.h"
|
||||
#include "lex.h"
|
||||
#include "hash.h"
|
||||
#include "table.h"
|
||||
|
||||
|
||||
#define lua_strcmp(a,b) (a[0]<b[0]?(-1):(a[0]>b[0]?(1):strcmp(a,b)))
|
||||
#define NUM_HASHS 64
|
||||
|
||||
typedef struct {
|
||||
int size;
|
||||
int nuse; /* number of elements (including EMPTYs) */
|
||||
TaggedString **hash;
|
||||
} stringtable;
|
||||
|
||||
static int initialized = 0;
|
||||
|
||||
static stringtable string_root[NUM_HASHS];
|
||||
|
||||
static TaggedString EMPTY = {NOT_USED, NOT_USED, 0, 2, {0}};
|
||||
|
||||
|
||||
typedef struct StringNode {
|
||||
struct StringNode *next;
|
||||
TaggedString ts;
|
||||
} StringNode;
|
||||
|
||||
static StringNode *string_root = NULL;
|
||||
|
||||
static TreeNode *constant_root = NULL;
|
||||
|
||||
/*
|
||||
** Insert a new constant/variable at the tree.
|
||||
*/
|
||||
static TreeNode *tree_create (TreeNode **node, char *str)
|
||||
static unsigned long hash (char *str)
|
||||
{
|
||||
if (*node == NULL)
|
||||
{
|
||||
*node = (TreeNode *) luaI_malloc(sizeof(TreeNode)+strlen(str));
|
||||
(*node)->left = (*node)->right = NULL;
|
||||
strcpy((*node)->ts.str, str);
|
||||
(*node)->ts.marked = 0;
|
||||
(*node)->ts.hash = 0;
|
||||
(*node)->varindex = (*node)->constindex = NOT_USED;
|
||||
return *node;
|
||||
}
|
||||
else
|
||||
{
|
||||
int c = lua_strcmp(str, (*node)->ts.str);
|
||||
if (c < 0)
|
||||
return tree_create(&(*node)->left, str);
|
||||
else if (c > 0)
|
||||
return tree_create(&(*node)->right, str);
|
||||
unsigned long h = 0;
|
||||
while (*str)
|
||||
h = ((h<<5)-h)^(unsigned char)*(str++);
|
||||
return h;
|
||||
}
|
||||
|
||||
static void initialize (void)
|
||||
{
|
||||
initialized = 1;
|
||||
luaI_addReserved();
|
||||
luaI_initsymbol();
|
||||
luaI_initconstant();
|
||||
}
|
||||
|
||||
|
||||
static void grow (stringtable *tb)
|
||||
{
|
||||
int newsize = luaI_redimension(tb->size);
|
||||
TaggedString **newhash = newvector(newsize, TaggedString *);
|
||||
int i;
|
||||
for (i=0; i<newsize; i++)
|
||||
newhash[i] = NULL;
|
||||
/* rehash */
|
||||
tb->nuse = 0;
|
||||
for (i=0; i<tb->size; i++)
|
||||
if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY)
|
||||
{
|
||||
int h = tb->hash[i]->hash%newsize;
|
||||
while (newhash[h])
|
||||
h = (h+1)%newsize;
|
||||
newhash[h] = tb->hash[i];
|
||||
tb->nuse++;
|
||||
}
|
||||
luaI_free(tb->hash);
|
||||
tb->size = newsize;
|
||||
tb->hash = newhash;
|
||||
}
|
||||
|
||||
static TaggedString *insert (char *str, stringtable *tb)
|
||||
{
|
||||
TaggedString *ts;
|
||||
unsigned long h = hash(str);
|
||||
int i;
|
||||
int j = -1;
|
||||
if ((Long)tb->nuse*3 >= (Long)tb->size*2)
|
||||
{
|
||||
if (!initialized)
|
||||
initialize();
|
||||
grow(tb);
|
||||
}
|
||||
i = h%tb->size;
|
||||
while (tb->hash[i])
|
||||
{
|
||||
if (tb->hash[i] == &EMPTY)
|
||||
j = i;
|
||||
else if (strcmp(str, tb->hash[i]->str) == 0)
|
||||
return tb->hash[i];
|
||||
i = (i+1)%tb->size;
|
||||
}
|
||||
/* not found */
|
||||
lua_pack();
|
||||
if (j != -1) /* is there an EMPTY space? */
|
||||
i = j;
|
||||
else
|
||||
return *node;
|
||||
}
|
||||
tb->nuse++;
|
||||
ts = tb->hash[i] = (TaggedString *)luaI_malloc(sizeof(TaggedString)+strlen(str));
|
||||
strcpy(ts->str, str);
|
||||
ts->marked = 0;
|
||||
ts->hash = h;
|
||||
ts->varindex = ts->constindex = NOT_USED;
|
||||
return ts;
|
||||
}
|
||||
|
||||
TaggedString *lua_createstring (char *str)
|
||||
{
|
||||
StringNode *newString;
|
||||
if (str == NULL) return NULL;
|
||||
lua_pack();
|
||||
newString = (StringNode *)luaI_malloc(sizeof(StringNode)+strlen(str));
|
||||
newString->ts.marked = 0;
|
||||
newString->ts.hash = 0;
|
||||
strcpy(newString->ts.str, str);
|
||||
newString->next = string_root;
|
||||
string_root = newString;
|
||||
return &(newString->ts);
|
||||
}
|
||||
|
||||
|
||||
TreeNode *lua_constcreate (char *str)
|
||||
{
|
||||
return tree_create(&constant_root, str);
|
||||
return insert(str, &string_root[(unsigned)str[0]%NUM_HASHS]);
|
||||
}
|
||||
|
||||
|
||||
@@ -80,44 +118,28 @@ TreeNode *lua_constcreate (char *str)
|
||||
*/
|
||||
Long lua_strcollector (void)
|
||||
{
|
||||
StringNode *curr = string_root, *prev = NULL;
|
||||
Long counter = 0;
|
||||
while (curr)
|
||||
int i;
|
||||
for (i=0; i<NUM_HASHS; i++)
|
||||
{
|
||||
StringNode *next = curr->next;
|
||||
if (!curr->ts.marked)
|
||||
stringtable *tb = &string_root[i];
|
||||
int j;
|
||||
for (j=0; j<tb->size; j++)
|
||||
{
|
||||
if (prev == NULL) string_root = next;
|
||||
else prev->next = next;
|
||||
luaI_free(curr);
|
||||
++counter;
|
||||
TaggedString *t = tb->hash[j];
|
||||
if (t != NULL && t->marked <= 1)
|
||||
{
|
||||
if (t->marked)
|
||||
t->marked = 0;
|
||||
else
|
||||
{
|
||||
luaI_free(t);
|
||||
tb->hash[j] = &EMPTY;
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
curr->ts.marked = 0;
|
||||
prev = curr;
|
||||
}
|
||||
curr = next;
|
||||
}
|
||||
return counter;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Traverse the constant tree looking for a specific symbol number
|
||||
*/
|
||||
static TreeNode *nodebysymbol (TreeNode *root, Word symbol)
|
||||
{
|
||||
TreeNode *t;
|
||||
if (root == NULL) return NULL;
|
||||
if (root->varindex == symbol) return root;
|
||||
t = nodebysymbol(root->left, symbol);
|
||||
if (t) return t;
|
||||
return nodebysymbol(root->right, symbol);
|
||||
}
|
||||
|
||||
TreeNode *luaI_nodebysymbol (Word symbol)
|
||||
{
|
||||
return nodebysymbol(constant_root, symbol);
|
||||
}
|
||||
|
||||
|
||||
17
tree.h
17
tree.h
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
** tree.h
|
||||
** TecCGraf - PUC-Rio
|
||||
** $Id: tree.h,v 1.9 1995/01/12 14:19:04 roberto Exp roberto $
|
||||
** $Id: tree.h,v 1.13 1996/02/14 13:35:51 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef tree_h
|
||||
@@ -14,24 +14,15 @@
|
||||
|
||||
typedef struct TaggedString
|
||||
{
|
||||
Word varindex; /* != NOT_USED if this is a symbol */
|
||||
Word constindex; /* != NOT_USED if this is a constant */
|
||||
unsigned long hash; /* 0 if not initialized */
|
||||
char marked; /* for garbage collection */
|
||||
int marked; /* for garbage collection; never collect (nor change) if > 1 */
|
||||
char str[1]; /* \0 byte already reserved */
|
||||
} TaggedString;
|
||||
|
||||
typedef struct TreeNode
|
||||
{
|
||||
struct TreeNode *right;
|
||||
struct TreeNode *left;
|
||||
unsigned short varindex; /* != NOT_USED if this is a symbol */
|
||||
unsigned short constindex; /* != NOT_USED if this is a constant */
|
||||
TaggedString ts;
|
||||
} TreeNode;
|
||||
|
||||
|
||||
TaggedString *lua_createstring (char *str);
|
||||
TreeNode *lua_constcreate (char *str);
|
||||
Long lua_strcollector (void);
|
||||
TreeNode *luaI_nodebysymbol (Word symbol);
|
||||
|
||||
#endif
|
||||
|
||||
4
types.h
4
types.h
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
** TeCGraf - PUC-Rio
|
||||
** $Id: types.h,v 1.2 1994/12/27 20:41:47 celes Exp roberto $
|
||||
** $Id: types.h,v 1.3 1995/02/06 19:32:43 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef types_h
|
||||
@@ -12,8 +12,6 @@
|
||||
#define real float
|
||||
#endif
|
||||
|
||||
typedef int Bool; /* boolean values */
|
||||
|
||||
#define Byte lua_Byte /* some systems have Byte as a predefined type */
|
||||
typedef unsigned char Byte; /* unsigned 8 bits */
|
||||
|
||||
|
||||
36
ugly.h
36
ugly.h
@@ -1,36 +0,0 @@
|
||||
/*
|
||||
** ugly.h
|
||||
** TecCGraf - PUC-Rio
|
||||
** $Id: $
|
||||
*/
|
||||
|
||||
#ifndef ugly_h
|
||||
#define ugly_h
|
||||
|
||||
/* This enum must have the same order of the array 'reserved' in lex.c */
|
||||
|
||||
enum {
|
||||
U_and=128,
|
||||
U_do,
|
||||
U_else,
|
||||
U_elseif,
|
||||
U_end,
|
||||
U_function,
|
||||
U_if,
|
||||
U_local,
|
||||
U_nil,
|
||||
U_not,
|
||||
U_or,
|
||||
U_repeat,
|
||||
U_return,
|
||||
U_then,
|
||||
U_until,
|
||||
U_while,
|
||||
U_eq = '='+128,
|
||||
U_le = '<'+128,
|
||||
U_ge = '>'+128,
|
||||
U_ne = '~'+128,
|
||||
U_sc = '.'+128
|
||||
};
|
||||
|
||||
#endif
|
||||
326
undump.c
Normal file
326
undump.c
Normal file
@@ -0,0 +1,326 @@
|
||||
/*
|
||||
** undump.c
|
||||
** load bytecodes from files
|
||||
*/
|
||||
|
||||
char* rcs_undump="$Id: undump.c,v 1.13 1996/03/12 20:00:40 lhf Exp lhf $";
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "opcode.h"
|
||||
#include "mem.h"
|
||||
#include "table.h"
|
||||
#include "undump.h"
|
||||
|
||||
static int swapword=0;
|
||||
static int swapfloat=0;
|
||||
static TFunc* Main=NULL; /* functions in a chunk */
|
||||
static TFunc* lastF=NULL;
|
||||
|
||||
static void warn(char* s) /* TODO: remove */
|
||||
{
|
||||
#if 0
|
||||
fprintf(stderr,"undump: %s\n",s);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void FixCode(Byte* code, Byte* end) /* swap words */
|
||||
{
|
||||
Byte* p;
|
||||
for (p=code; p!=end;)
|
||||
{
|
||||
OpCode op=(OpCode)*p;
|
||||
switch (op)
|
||||
{
|
||||
case PUSHNIL:
|
||||
case PUSH0:
|
||||
case PUSH1:
|
||||
case PUSH2:
|
||||
case PUSHLOCAL0:
|
||||
case PUSHLOCAL1:
|
||||
case PUSHLOCAL2:
|
||||
case PUSHLOCAL3:
|
||||
case PUSHLOCAL4:
|
||||
case PUSHLOCAL5:
|
||||
case PUSHLOCAL6:
|
||||
case PUSHLOCAL7:
|
||||
case PUSHLOCAL8:
|
||||
case PUSHLOCAL9:
|
||||
case PUSHINDEXED:
|
||||
case STORELOCAL0:
|
||||
case STORELOCAL1:
|
||||
case STORELOCAL2:
|
||||
case STORELOCAL3:
|
||||
case STORELOCAL4:
|
||||
case STORELOCAL5:
|
||||
case STORELOCAL6:
|
||||
case STORELOCAL7:
|
||||
case STORELOCAL8:
|
||||
case STORELOCAL9:
|
||||
case STOREINDEXED0:
|
||||
case ADJUST0:
|
||||
case EQOP:
|
||||
case LTOP:
|
||||
case LEOP:
|
||||
case GTOP:
|
||||
case GEOP:
|
||||
case ADDOP:
|
||||
case SUBOP:
|
||||
case MULTOP:
|
||||
case DIVOP:
|
||||
case POWOP:
|
||||
case CONCOP:
|
||||
case MINUSOP:
|
||||
case NOTOP:
|
||||
case POP:
|
||||
case RETCODE0:
|
||||
p++;
|
||||
break;
|
||||
case PUSHBYTE:
|
||||
case PUSHLOCAL:
|
||||
case STORELOCAL:
|
||||
case STOREINDEXED:
|
||||
case STORELIST0:
|
||||
case ADJUST:
|
||||
case RETCODE:
|
||||
p+=2;
|
||||
break;
|
||||
case STORELIST:
|
||||
case CALLFUNC:
|
||||
p+=3;
|
||||
break;
|
||||
case PUSHFUNCTION:
|
||||
p+=5;
|
||||
break;
|
||||
case PUSHWORD:
|
||||
case PUSHSELF:
|
||||
case CREATEARRAY:
|
||||
case ONTJMP:
|
||||
case ONFJMP:
|
||||
case JMP:
|
||||
case UPJMP:
|
||||
case IFFJMP:
|
||||
case IFFUPJMP:
|
||||
case SETLINE:
|
||||
case PUSHSTRING:
|
||||
case PUSHGLOBAL:
|
||||
case STOREGLOBAL:
|
||||
{
|
||||
Byte t;
|
||||
t=p[1]; p[1]=p[2]; p[2]=t;
|
||||
p+=3;
|
||||
break;
|
||||
}
|
||||
case PUSHFLOAT:
|
||||
{
|
||||
Byte t;
|
||||
t=p[1]; p[1]=p[4]; p[4]=t;
|
||||
t=p[2]; p[2]=p[3]; p[3]=t;
|
||||
p+=5;
|
||||
break;
|
||||
}
|
||||
case STORERECORD:
|
||||
{
|
||||
int n=*++p;
|
||||
p++;
|
||||
while (n--)
|
||||
{
|
||||
Byte t;
|
||||
t=p[0]; p[0]=p[1]; p[1]=t;
|
||||
p+=2;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
lua_error("corrupt binary file");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void Unthread(Byte* code, int i, int v)
|
||||
{
|
||||
while (i!=0)
|
||||
{
|
||||
CodeWord c;
|
||||
Byte* p=code+i;
|
||||
get_word(c,p);
|
||||
i=c.w;
|
||||
c.w=v;
|
||||
p[-2]=c.m.c1;
|
||||
p[-1]=c.m.c2;
|
||||
}
|
||||
}
|
||||
|
||||
static int LoadWord(FILE* D)
|
||||
{
|
||||
Word w;
|
||||
fread(&w,sizeof(w),1,D);
|
||||
if (swapword)
|
||||
{
|
||||
Byte* p=(Byte*)&w; /* TODO: need union? */
|
||||
Byte t;
|
||||
t=p[0]; p[0]=p[1]; p[1]=t;
|
||||
}
|
||||
return w;
|
||||
}
|
||||
|
||||
static int LoadSize(FILE* D)
|
||||
{
|
||||
Word hi=LoadWord(D);
|
||||
Word lo=LoadWord(D);
|
||||
int s=(hi<<16)|lo;
|
||||
if ((Word)s != s) lua_error("code too long");
|
||||
return s;
|
||||
}
|
||||
|
||||
static char* LoadBlock(int size, FILE* D)
|
||||
{
|
||||
char* b=luaI_malloc(size);
|
||||
fread(b,size,1,D);
|
||||
return b;
|
||||
}
|
||||
|
||||
static char* LoadString(FILE* D)
|
||||
{
|
||||
int size=LoadWord(D);
|
||||
char *b=luaI_buffer(size);
|
||||
fread(b,size,1,D);
|
||||
return b;
|
||||
}
|
||||
|
||||
static char* LoadNewString(FILE* D)
|
||||
{
|
||||
return LoadBlock(LoadWord(D),D);
|
||||
}
|
||||
|
||||
static void LoadFunction(FILE* D)
|
||||
{
|
||||
TFunc* tf=new(TFunc);
|
||||
tf->next=NULL;
|
||||
tf->locvars=NULL;
|
||||
tf->size=LoadSize(D);
|
||||
tf->lineDefined=LoadWord(D);
|
||||
if (IsMain(tf)) /* new main */
|
||||
{
|
||||
tf->fileName=LoadNewString(D);
|
||||
Main=lastF=tf;
|
||||
}
|
||||
else /* fix PUSHFUNCTION */
|
||||
{
|
||||
CodeCode c;
|
||||
Byte* p;
|
||||
tf->marked=LoadWord(D);
|
||||
tf->fileName=Main->fileName;
|
||||
p=Main->code+tf->marked;
|
||||
c.tf=tf;
|
||||
*p++=c.m.c1; *p++=c.m.c2; *p++=c.m.c3; *p++=c.m.c4;
|
||||
lastF=lastF->next=tf;
|
||||
}
|
||||
tf->code=LoadBlock(tf->size,D);
|
||||
if (swapword || swapfloat) FixCode(tf->code,tf->code+tf->size);
|
||||
while (1) /* unthread */
|
||||
{
|
||||
int c=getc(D);
|
||||
if (c==ID_VAR) /* global var */
|
||||
{
|
||||
int i=LoadWord(D);
|
||||
char* s=LoadString(D);
|
||||
int v=luaI_findsymbolbyname(s);
|
||||
Unthread(tf->code,i,v);
|
||||
}
|
||||
else if (c==ID_STR) /* constant string */
|
||||
{
|
||||
int i=LoadWord(D);
|
||||
char* s=LoadString(D);
|
||||
int v=luaI_findconstantbyname(s);
|
||||
Unthread(tf->code,i,v);
|
||||
}
|
||||
else
|
||||
{
|
||||
ungetc(c,D);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void LoadSignature(FILE* D)
|
||||
{
|
||||
char* s=SIGNATURE;
|
||||
while (*s!=0 && getc(D)==*s)
|
||||
++s;
|
||||
if (*s!=0) lua_error("bad signature");
|
||||
}
|
||||
|
||||
static void LoadHeader(FILE* D) /* TODO: error handling */
|
||||
{
|
||||
Word w,tw=TEST_WORD;
|
||||
float f,tf=TEST_FLOAT;
|
||||
LoadSignature(D);
|
||||
getc(D); /* skip version */
|
||||
fread(&w,sizeof(w),1,D); /* test word */
|
||||
if (w!=tw)
|
||||
{
|
||||
swapword=1;
|
||||
warn("different byte order");
|
||||
}
|
||||
fread(&f,sizeof(f),1,D); /* test float */
|
||||
if (f!=tf)
|
||||
{
|
||||
Byte* p=(Byte*)&f; /* TODO: need union? */
|
||||
Byte t;
|
||||
swapfloat=1;
|
||||
t=p[0]; p[0]=p[3]; p[3]=t;
|
||||
t=p[1]; p[1]=p[2]; p[2]=t;
|
||||
if (f!=tf) /* TODO: try another perm? */
|
||||
lua_error("different float representation");
|
||||
else
|
||||
warn("different byte order in floats");
|
||||
}
|
||||
}
|
||||
|
||||
static void LoadChunk(FILE* D)
|
||||
{
|
||||
LoadHeader(D);
|
||||
while (1)
|
||||
{
|
||||
int c=getc(D);
|
||||
if (c==ID_FUN) LoadFunction(D); else { ungetc(c,D); break; }
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** load one chunk from a file.
|
||||
** return list of functions found, headed by main, or NULL at EOF.
|
||||
*/
|
||||
TFunc* luaI_undump1(FILE* D)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
int c=getc(D);
|
||||
if (c==ID_CHUNK)
|
||||
{
|
||||
LoadChunk(D);
|
||||
return Main;
|
||||
}
|
||||
else if (c==EOF)
|
||||
return NULL;
|
||||
else
|
||||
lua_error("not a lua binary file");
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** load and run all chunks in a file
|
||||
*/
|
||||
int luaI_undump(FILE* D)
|
||||
{
|
||||
TFunc* m;
|
||||
while ((m=luaI_undump1(D)))
|
||||
{
|
||||
int status=luaI_dorun(m);
|
||||
luaI_freefunc(m);
|
||||
if (status!=0) return status;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
23
undump.h
Normal file
23
undump.h
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
** undump.h
|
||||
** definitions for lua decompiler
|
||||
** $Id: undump.h,v 1.1 1996/03/08 21:43:21 lhf Exp lhf $
|
||||
*/
|
||||
|
||||
#include "func.h"
|
||||
|
||||
#define IsMain(f) (f->lineDefined==0)
|
||||
|
||||
/* definitions for chunk headers */
|
||||
|
||||
#define ID_CHUNK 27 /* ESC */
|
||||
#define ID_FUN 'F'
|
||||
#define ID_VAR 'V'
|
||||
#define ID_STR 'S'
|
||||
#define SIGNATURE "Lua"
|
||||
#define VERSION 0x23 /* 2.3 */
|
||||
#define TEST_WORD 0x1234 /* a word for testing byte ordering */
|
||||
#define TEST_FLOAT 0.123456789e-23 /* a float for testing representation */
|
||||
|
||||
TFunc* luaI_undump1(FILE* D); /* load one chunk */
|
||||
int luaI_undump(FILE* D); /* load all chunks */
|
||||
Reference in New Issue
Block a user