mirror of
https://github.com/lua/lua.git
synced 2026-07-26 16:09:07 +00:00
Compare commits
1 Commits
v3.1-alpha
...
v1.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b9dde086db |
29
Makefile
Normal file
29
Makefile
Normal file
@@ -0,0 +1,29 @@
|
||||
OBJS= hash.o inout.o lex_yy.o opcode.o table.o y_tab.o lua.o iolib.o mathlib.o strlib.o
|
||||
|
||||
CFLAGS= -O2 -I.
|
||||
|
||||
T= lua
|
||||
|
||||
all: $T
|
||||
|
||||
$T: $(OBJS)
|
||||
$(CC) -o $@ $(OBJS) -lm
|
||||
|
||||
A=--------------------------------------------------------------------------
|
||||
test: $T
|
||||
@echo "$A"
|
||||
./$T sort.lua main
|
||||
@echo "$A"
|
||||
./$T globals.lua | sort | column
|
||||
@echo "$A"
|
||||
./$T array.lua
|
||||
@echo "$A"
|
||||
./$T save.lua
|
||||
@echo "$A"
|
||||
./$T test.lua retorno_multiplo norma
|
||||
|
||||
clean:
|
||||
rm -f $T $(OBJS) core core.*
|
||||
|
||||
diff:
|
||||
diff . fixed | grep -v ^Only
|
||||
22
README
Normal file
22
README
Normal file
@@ -0,0 +1,22 @@
|
||||
This is Lua 1.0. It was never publicly released. This code is a snapshot of
|
||||
the status of Lua on 28 Jul 1993. It is distributed for historical curiosity
|
||||
to celebrate 10 years of Lua and is hereby placed in the public domain.
|
||||
|
||||
There is no documentation, except the test programs. The manual for Lua 1.1
|
||||
probably works for this version as well.
|
||||
|
||||
The source files for the lexer and parser have been lost: all that is left is
|
||||
the output of lex and yacc. A grammar can be found inside y_tab.c in yyreds.
|
||||
|
||||
The code compiles and runs in RedHat 5.2 with gcc 2.7.2.3. It may not run in
|
||||
newer systems, because it assumes that stdin and stdout are constants, though
|
||||
ANSI C does not promise they are. If make fails, try using the fixed modules
|
||||
provided in the "fixed" directory. To see the differences (which are really
|
||||
quite minor), do "make diff".
|
||||
|
||||
To see Lua 1.0 in action, do "make test". (The last test raises an error on
|
||||
purpose.)
|
||||
|
||||
Enjoy!
|
||||
|
||||
-- The Lua team, lua@tecgraf.puc-rio.br
|
||||
15
array.lua
Normal file
15
array.lua
Normal file
@@ -0,0 +1,15 @@
|
||||
$debug
|
||||
|
||||
a = @()
|
||||
|
||||
i=0
|
||||
while i<10 do
|
||||
a[i] = i*i
|
||||
i=i+1
|
||||
end
|
||||
|
||||
r,v = next(a,nil)
|
||||
while r ~= nil do
|
||||
print ("array["..r.."] = "..v)
|
||||
r,v = next(a,r)
|
||||
end
|
||||
4
bugs
4
bugs
@@ -1,4 +0,0 @@
|
||||
** lua.stx / llex.c
|
||||
Tue Dec 2 10:45:48 EDT 1997
|
||||
>> BUG: "lastline" was not reset on function entry, so debug information
|
||||
>> started only in the 2nd line of a function.
|
||||
402
fixed/iolib.c
Normal file
402
fixed/iolib.c
Normal file
@@ -0,0 +1,402 @@
|
||||
/*
|
||||
** iolib.c
|
||||
** Input/output library to LUA
|
||||
**
|
||||
** Waldemar Celes Filho
|
||||
** TeCGraf - PUC-Rio
|
||||
** 19 May 93
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#ifdef __GNUC__
|
||||
#include <floatingpoint.h>
|
||||
#endif
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
static FILE *in=NULL, *out=NULL;
|
||||
|
||||
/*
|
||||
** Open a file to read.
|
||||
** LUA interface:
|
||||
** status = readfrom (filename)
|
||||
** where:
|
||||
** status = 1 -> success
|
||||
** status = 0 -> error
|
||||
*/
|
||||
static void io_readfrom (void)
|
||||
{
|
||||
lua_Object o = lua_getparam (1);
|
||||
if (o == NULL) /* restore standart input */
|
||||
{
|
||||
if (in != stdin)
|
||||
{
|
||||
fclose (in);
|
||||
in = stdin;
|
||||
}
|
||||
lua_pushnumber (1);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!lua_isstring (o))
|
||||
{
|
||||
lua_error ("incorrect argument to function 'readfrom`");
|
||||
lua_pushnumber (0);
|
||||
}
|
||||
else
|
||||
{
|
||||
FILE *fp = fopen (lua_getstring(o),"r");
|
||||
if (fp == NULL)
|
||||
{
|
||||
lua_pushnumber (0);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (in != stdin) fclose (in);
|
||||
in = fp;
|
||||
lua_pushnumber (1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Open a file to write.
|
||||
** LUA interface:
|
||||
** status = writeto (filename)
|
||||
** where:
|
||||
** status = 1 -> success
|
||||
** status = 0 -> error
|
||||
*/
|
||||
static void io_writeto (void)
|
||||
{
|
||||
lua_Object o = lua_getparam (1);
|
||||
if (o == NULL) /* restore standart output */
|
||||
{
|
||||
if (out != stdout)
|
||||
{
|
||||
fclose (out);
|
||||
out = stdout;
|
||||
}
|
||||
lua_pushnumber (1);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!lua_isstring (o))
|
||||
{
|
||||
lua_error ("incorrect argument to function 'writeto`");
|
||||
lua_pushnumber (0);
|
||||
}
|
||||
else
|
||||
{
|
||||
FILE *fp = fopen (lua_getstring(o),"w");
|
||||
if (fp == NULL)
|
||||
{
|
||||
lua_pushnumber (0);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (out != stdout) fclose (out);
|
||||
out = fp;
|
||||
lua_pushnumber (1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Read a variable. On error put nil on stack.
|
||||
** LUA interface:
|
||||
** variable = read ([format])
|
||||
**
|
||||
** O formato pode ter um dos seguintes especificadores:
|
||||
**
|
||||
** s ou S -> para string
|
||||
** f ou F, g ou G, e ou E -> para reais
|
||||
** i ou I -> para inteiros
|
||||
**
|
||||
** Estes especificadores podem vir seguidos de numero que representa
|
||||
** o numero de campos a serem lidos.
|
||||
*/
|
||||
static void io_read (void)
|
||||
{
|
||||
lua_Object o = lua_getparam (1);
|
||||
if (o == NULL) /* free format */
|
||||
{
|
||||
int c;
|
||||
char s[256];
|
||||
while (isspace(c=fgetc(in)))
|
||||
;
|
||||
if (c == '\"')
|
||||
{
|
||||
if (fscanf (in, "%[^\"]\"", s) != 1)
|
||||
{
|
||||
lua_pushnil ();
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (c == '\'')
|
||||
{
|
||||
if (fscanf (in, "%[^\']\'", s) != 1)
|
||||
{
|
||||
lua_pushnil ();
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
char *ptr;
|
||||
double d;
|
||||
ungetc (c, in);
|
||||
if (fscanf (in, "%s", s) != 1)
|
||||
{
|
||||
lua_pushnil ();
|
||||
return;
|
||||
}
|
||||
d = strtod (s, &ptr);
|
||||
if (!(*ptr))
|
||||
{
|
||||
lua_pushnumber (d);
|
||||
return;
|
||||
}
|
||||
}
|
||||
lua_pushstring (s);
|
||||
return;
|
||||
}
|
||||
else /* formatted */
|
||||
{
|
||||
char *e = lua_getstring(o);
|
||||
char t;
|
||||
int m=0;
|
||||
while (isspace(*e)) e++;
|
||||
t = *e++;
|
||||
while (isdigit(*e))
|
||||
m = m*10 + (*e++ - '0');
|
||||
|
||||
if (m > 0)
|
||||
{
|
||||
char f[80];
|
||||
char s[256];
|
||||
sprintf (f, "%%%ds", m);
|
||||
fscanf (in, f, s);
|
||||
switch (tolower(t))
|
||||
{
|
||||
case 'i':
|
||||
{
|
||||
long int l;
|
||||
sscanf (s, "%ld", &l);
|
||||
lua_pushnumber(l);
|
||||
}
|
||||
break;
|
||||
case 'f': case 'g': case 'e':
|
||||
{
|
||||
float f;
|
||||
sscanf (s, "%f", &f);
|
||||
lua_pushnumber(f);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
lua_pushstring(s);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (tolower(t))
|
||||
{
|
||||
case 'i':
|
||||
{
|
||||
long int l;
|
||||
fscanf (in, "%ld", &l);
|
||||
lua_pushnumber(l);
|
||||
}
|
||||
break;
|
||||
case 'f': case 'g': case 'e':
|
||||
{
|
||||
float f;
|
||||
fscanf (in, "%f", &f);
|
||||
lua_pushnumber(f);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
{
|
||||
char s[256];
|
||||
fscanf (in, "%s", s);
|
||||
lua_pushstring(s);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Write a variable. On error put 0 on stack, otherwise put 1.
|
||||
** LUA interface:
|
||||
** status = write (variable [,format])
|
||||
**
|
||||
** O formato pode ter um dos seguintes especificadores:
|
||||
**
|
||||
** s ou S -> para string
|
||||
** f ou F, g ou G, e ou E -> para reais
|
||||
** i ou I -> para inteiros
|
||||
**
|
||||
** Estes especificadores podem vir seguidos de:
|
||||
**
|
||||
** [?][m][.n]
|
||||
**
|
||||
** onde:
|
||||
** ? -> indica justificacao
|
||||
** < = esquerda
|
||||
** | = centro
|
||||
** > = direita (default)
|
||||
** m -> numero maximo de campos (se exceder estoura)
|
||||
** n -> indica precisao para
|
||||
** reais -> numero de casas decimais
|
||||
** inteiros -> numero minimo de digitos
|
||||
** string -> nao se aplica
|
||||
*/
|
||||
static char *buildformat (char *e, lua_Object o)
|
||||
{
|
||||
static char buffer[512];
|
||||
static char f[80];
|
||||
char *string = &buffer[255];
|
||||
char t, j='r';
|
||||
int m=0, n=0, l;
|
||||
while (isspace(*e)) e++;
|
||||
t = *e++;
|
||||
if (*e == '<' || *e == '|' || *e == '>') j = *e++;
|
||||
while (isdigit(*e))
|
||||
m = m*10 + (*e++ - '0');
|
||||
e++; /* skip point */
|
||||
while (isdigit(*e))
|
||||
n = n*10 + (*e++ - '0');
|
||||
|
||||
sprintf(f,"%%");
|
||||
if (j == '<' || j == '|') sprintf(strchr(f,0),"-");
|
||||
if (m != 0) sprintf(strchr(f,0),"%d", m);
|
||||
if (n != 0) sprintf(strchr(f,0),".%d", n);
|
||||
sprintf(strchr(f,0), "%c", t);
|
||||
switch (tolower(t))
|
||||
{
|
||||
case 'i': t = 'i';
|
||||
sprintf (string, f, (long int)lua_getnumber(o));
|
||||
break;
|
||||
case 'f': case 'g': case 'e': t = 'f';
|
||||
sprintf (string, f, (float)lua_getnumber(o));
|
||||
break;
|
||||
case 's': t = 's';
|
||||
sprintf (string, f, lua_getstring(o));
|
||||
break;
|
||||
default: return "";
|
||||
}
|
||||
l = strlen(string);
|
||||
if (m!=0 && l>m)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<m; i++)
|
||||
string[i] = '*';
|
||||
string[i] = 0;
|
||||
}
|
||||
else if (m!=0 && j=='|')
|
||||
{
|
||||
int i=l-1;
|
||||
while (isspace(string[i])) i--;
|
||||
string -= (m-i) / 2;
|
||||
i=0;
|
||||
while (string[i]==0) string[i++] = ' ';
|
||||
string[l] = 0;
|
||||
}
|
||||
return string;
|
||||
}
|
||||
static void io_write (void)
|
||||
{
|
||||
lua_Object o1 = lua_getparam (1);
|
||||
lua_Object o2 = lua_getparam (2);
|
||||
if (o1 == NULL) /* new line */
|
||||
{
|
||||
fprintf (out, "\n");
|
||||
lua_pushnumber(1);
|
||||
}
|
||||
else if (o2 == NULL) /* free format */
|
||||
{
|
||||
int status=0;
|
||||
if (lua_isnumber(o1))
|
||||
status = fprintf (out, "%g", lua_getnumber(o1));
|
||||
else if (lua_isstring(o1))
|
||||
status = fprintf (out, "%s", lua_getstring(o1));
|
||||
lua_pushnumber(status);
|
||||
}
|
||||
else /* formated */
|
||||
{
|
||||
if (!lua_isstring(o2))
|
||||
{
|
||||
lua_error ("incorrect format to function `write'");
|
||||
lua_pushnumber(0);
|
||||
return;
|
||||
}
|
||||
lua_pushnumber(fprintf (out, "%s", buildformat(lua_getstring(o2),o1)));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Execute a executable program using "sustem".
|
||||
** On error put 0 on stack, otherwise put 1.
|
||||
*/
|
||||
void io_execute (void)
|
||||
{
|
||||
lua_Object o = lua_getparam (1);
|
||||
if (o == NULL || !lua_isstring (o))
|
||||
{
|
||||
lua_error ("incorrect argument to function 'execute`");
|
||||
lua_pushnumber (0);
|
||||
}
|
||||
else
|
||||
{
|
||||
system(lua_getstring(o));
|
||||
lua_pushnumber (1);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
** Remove a file.
|
||||
** On error put 0 on stack, otherwise put 1.
|
||||
*/
|
||||
void io_remove (void)
|
||||
{
|
||||
lua_Object o = lua_getparam (1);
|
||||
if (o == NULL || !lua_isstring (o))
|
||||
{
|
||||
lua_error ("incorrect argument to function 'execute`");
|
||||
lua_pushnumber (0);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (remove(lua_getstring(o)) == 0)
|
||||
lua_pushnumber (1);
|
||||
else
|
||||
lua_pushnumber (0);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
** Open io library
|
||||
*/
|
||||
void iolib_open (void)
|
||||
{
|
||||
in=stdin; out=stdout;
|
||||
lua_register ("readfrom", io_readfrom);
|
||||
lua_register ("writeto", io_writeto);
|
||||
lua_register ("read", io_read);
|
||||
lua_register ("write", io_write);
|
||||
lua_register ("execute", io_execute);
|
||||
lua_register ("remove", io_remove);
|
||||
}
|
||||
923
fixed/lex_yy.c
Normal file
923
fixed/lex_yy.c
Normal file
@@ -0,0 +1,923 @@
|
||||
# include "stdio.h"
|
||||
# define U(x) x
|
||||
# define NLSTATE yyprevious=YYNEWLINE
|
||||
# define BEGIN yybgin = yysvec + 1 +
|
||||
# define INITIAL 0
|
||||
# define YYLERR yysvec
|
||||
# define YYSTATE (yyestate-yysvec-1)
|
||||
# define YYOPTIM 1
|
||||
# define YYLMAX BUFSIZ
|
||||
# define output(c) putc(c,yyout)
|
||||
# define input() (((yytchar=yysptr>yysbuf?U(*--yysptr):getc(yyin))==10?(yylineno++,yytchar):yytchar)==EOF?0:yytchar)
|
||||
# define unput(c) {yytchar= (c);if(yytchar=='\n')yylineno--;*yysptr++=yytchar;}
|
||||
# define yymore() (yymorfg=1)
|
||||
# define ECHO fprintf(yyout, "%s",yytext)
|
||||
# define REJECT { nstr = yyreject(); goto yyfussy;}
|
||||
int yyleng; extern char yytext[];
|
||||
int yymorfg;
|
||||
extern char *yysptr, yysbuf[];
|
||||
int yytchar;
|
||||
FILE *yyin = {NULL}, *yyout = {NULL};
|
||||
extern int yylineno;
|
||||
struct yysvf {
|
||||
struct yywork *yystoff;
|
||||
struct yysvf *yyother;
|
||||
int *yystops;};
|
||||
struct yysvf *yyestate;
|
||||
extern struct yysvf yysvec[], *yybgin;
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "opcode.h"
|
||||
#include "hash.h"
|
||||
#include "inout.h"
|
||||
#include "table.h"
|
||||
#include "y_tab.h"
|
||||
|
||||
#undef input
|
||||
#undef unput
|
||||
|
||||
static Input input;
|
||||
static Unput unput;
|
||||
|
||||
void lua_setinput (Input fn)
|
||||
{
|
||||
input = fn;
|
||||
}
|
||||
|
||||
void lua_setunput (Unput fn)
|
||||
{
|
||||
unput = fn;
|
||||
}
|
||||
|
||||
char *lua_lasttext (void)
|
||||
{
|
||||
return yytext;
|
||||
}
|
||||
|
||||
# define YYNEWLINE 10
|
||||
yylex(){
|
||||
int nstr; extern int yyprevious;
|
||||
while((nstr = yylook()) >= 0)
|
||||
yyfussy: switch(nstr){
|
||||
case 0:
|
||||
if(yywrap()) return(0); break;
|
||||
case 1:
|
||||
;
|
||||
break;
|
||||
case 2:
|
||||
{yylval.vInt = 1; return DEBUG;}
|
||||
break;
|
||||
case 3:
|
||||
{yylval.vInt = 0; return DEBUG;}
|
||||
break;
|
||||
case 4:
|
||||
lua_linenumber++;
|
||||
break;
|
||||
case 5:
|
||||
;
|
||||
break;
|
||||
case 6:
|
||||
return LOCAL;
|
||||
break;
|
||||
case 7:
|
||||
return IF;
|
||||
break;
|
||||
case 8:
|
||||
return THEN;
|
||||
break;
|
||||
case 9:
|
||||
return ELSE;
|
||||
break;
|
||||
case 10:
|
||||
return ELSEIF;
|
||||
break;
|
||||
case 11:
|
||||
return WHILE;
|
||||
break;
|
||||
case 12:
|
||||
return DO;
|
||||
break;
|
||||
case 13:
|
||||
return REPEAT;
|
||||
break;
|
||||
case 14:
|
||||
return UNTIL;
|
||||
break;
|
||||
case 15:
|
||||
{
|
||||
yylval.vWord = lua_nfile-1;
|
||||
return FUNCTION;
|
||||
}
|
||||
break;
|
||||
case 16:
|
||||
return END;
|
||||
break;
|
||||
case 17:
|
||||
return RETURN;
|
||||
break;
|
||||
case 18:
|
||||
return LOCAL;
|
||||
break;
|
||||
case 19:
|
||||
return NIL;
|
||||
break;
|
||||
case 20:
|
||||
return AND;
|
||||
break;
|
||||
case 21:
|
||||
return OR;
|
||||
break;
|
||||
case 22:
|
||||
return NOT;
|
||||
break;
|
||||
case 23:
|
||||
return NE;
|
||||
break;
|
||||
case 24:
|
||||
return LE;
|
||||
break;
|
||||
case 25:
|
||||
return GE;
|
||||
break;
|
||||
case 26:
|
||||
return CONC;
|
||||
break;
|
||||
case 27:
|
||||
case 28:
|
||||
{
|
||||
yylval.vWord = lua_findenclosedconstant (yytext);
|
||||
return STRING;
|
||||
}
|
||||
break;
|
||||
case 29:
|
||||
case 30:
|
||||
case 31:
|
||||
case 32:
|
||||
{
|
||||
yylval.vFloat = atof(yytext);
|
||||
return NUMBER;
|
||||
}
|
||||
break;
|
||||
case 33:
|
||||
{
|
||||
yylval.vWord = lua_findsymbol (yytext);
|
||||
return NAME;
|
||||
}
|
||||
break;
|
||||
case 34:
|
||||
return *yytext;
|
||||
break;
|
||||
case -1:
|
||||
break;
|
||||
default:
|
||||
fprintf(yyout,"bad switch yylook %d",nstr);
|
||||
} return(0); }
|
||||
/* end of yylex */
|
||||
int yyvstop[] = {
|
||||
0,
|
||||
|
||||
1,
|
||||
0,
|
||||
|
||||
1,
|
||||
0,
|
||||
|
||||
34,
|
||||
0,
|
||||
|
||||
1,
|
||||
34,
|
||||
0,
|
||||
|
||||
4,
|
||||
0,
|
||||
|
||||
34,
|
||||
0,
|
||||
|
||||
34,
|
||||
0,
|
||||
|
||||
34,
|
||||
0,
|
||||
|
||||
34,
|
||||
0,
|
||||
|
||||
29,
|
||||
34,
|
||||
0,
|
||||
|
||||
34,
|
||||
0,
|
||||
|
||||
34,
|
||||
0,
|
||||
|
||||
33,
|
||||
34,
|
||||
0,
|
||||
|
||||
33,
|
||||
34,
|
||||
0,
|
||||
|
||||
33,
|
||||
34,
|
||||
0,
|
||||
|
||||
33,
|
||||
34,
|
||||
0,
|
||||
|
||||
33,
|
||||
34,
|
||||
0,
|
||||
|
||||
33,
|
||||
34,
|
||||
0,
|
||||
|
||||
33,
|
||||
34,
|
||||
0,
|
||||
|
||||
33,
|
||||
34,
|
||||
0,
|
||||
|
||||
33,
|
||||
34,
|
||||
0,
|
||||
|
||||
33,
|
||||
34,
|
||||
0,
|
||||
|
||||
33,
|
||||
34,
|
||||
0,
|
||||
|
||||
33,
|
||||
34,
|
||||
0,
|
||||
|
||||
33,
|
||||
34,
|
||||
0,
|
||||
|
||||
34,
|
||||
0,
|
||||
|
||||
34,
|
||||
0,
|
||||
|
||||
1,
|
||||
0,
|
||||
|
||||
27,
|
||||
0,
|
||||
|
||||
28,
|
||||
0,
|
||||
|
||||
5,
|
||||
0,
|
||||
|
||||
26,
|
||||
0,
|
||||
|
||||
30,
|
||||
0,
|
||||
|
||||
29,
|
||||
0,
|
||||
|
||||
29,
|
||||
0,
|
||||
|
||||
24,
|
||||
0,
|
||||
|
||||
25,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
12,
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
7,
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
21,
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
23,
|
||||
0,
|
||||
|
||||
29,
|
||||
30,
|
||||
0,
|
||||
|
||||
31,
|
||||
0,
|
||||
|
||||
20,
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
16,
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
19,
|
||||
33,
|
||||
0,
|
||||
|
||||
22,
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
32,
|
||||
0,
|
||||
|
||||
9,
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
8,
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
31,
|
||||
32,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
6,
|
||||
18,
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
14,
|
||||
33,
|
||||
0,
|
||||
|
||||
11,
|
||||
33,
|
||||
0,
|
||||
|
||||
10,
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
13,
|
||||
33,
|
||||
0,
|
||||
|
||||
17,
|
||||
33,
|
||||
0,
|
||||
|
||||
2,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
15,
|
||||
33,
|
||||
0,
|
||||
|
||||
3,
|
||||
0,
|
||||
0};
|
||||
# define YYTYPE char
|
||||
struct yywork { YYTYPE verify, advance; } yycrank[] = {
|
||||
0,0, 0,0, 1,3, 0,0,
|
||||
0,0, 0,0, 0,0, 0,0,
|
||||
0,0, 0,0, 1,4, 1,5,
|
||||
6,29, 4,28, 0,0, 0,0,
|
||||
0,0, 0,0, 7,31, 0,0,
|
||||
6,29, 6,29, 0,0, 0,0,
|
||||
0,0, 0,0, 7,31, 7,31,
|
||||
0,0, 0,0, 0,0, 0,0,
|
||||
0,0, 0,0, 0,0, 1,6,
|
||||
4,28, 0,0, 0,0, 0,0,
|
||||
1,7, 0,0, 0,0, 0,0,
|
||||
1,3, 6,30, 1,8, 1,9,
|
||||
0,0, 1,10, 6,29, 7,31,
|
||||
8,33, 0,0, 6,29, 0,0,
|
||||
7,32, 0,0, 0,0, 6,29,
|
||||
7,31, 1,11, 0,0, 1,12,
|
||||
2,27, 7,31, 1,13, 11,39,
|
||||
12,40, 1,13, 26,56, 0,0,
|
||||
0,0, 2,8, 2,9, 0,0,
|
||||
6,29, 0,0, 0,0, 6,29,
|
||||
0,0, 0,0, 7,31, 0,0,
|
||||
0,0, 7,31, 0,0, 0,0,
|
||||
2,11, 0,0, 2,12, 0,0,
|
||||
0,0, 0,0, 0,0, 0,0,
|
||||
0,0, 0,0, 1,14, 0,0,
|
||||
0,0, 1,15, 1,16, 1,17,
|
||||
0,0, 22,52, 1,18, 18,47,
|
||||
23,53, 1,19, 42,63, 1,20,
|
||||
1,21, 25,55, 14,42, 1,22,
|
||||
15,43, 1,23, 1,24, 16,44,
|
||||
1,25, 16,45, 17,46, 19,48,
|
||||
21,51, 2,14, 20,49, 1,26,
|
||||
2,15, 2,16, 2,17, 24,54,
|
||||
20,50, 2,18, 44,64, 45,65,
|
||||
2,19, 46,66, 2,20, 2,21,
|
||||
27,57, 48,67, 2,22, 49,68,
|
||||
2,23, 2,24, 50,69, 2,25,
|
||||
52,70, 53,72, 27,58, 54,73,
|
||||
52,71, 9,34, 2,26, 9,35,
|
||||
9,35, 9,35, 9,35, 9,35,
|
||||
9,35, 9,35, 9,35, 9,35,
|
||||
9,35, 10,36, 55,74, 10,37,
|
||||
10,37, 10,37, 10,37, 10,37,
|
||||
10,37, 10,37, 10,37, 10,37,
|
||||
10,37, 57,75, 58,76, 64,80,
|
||||
66,81, 67,82, 70,83, 71,84,
|
||||
72,85, 73,86, 74,87, 10,38,
|
||||
10,38, 38,61, 10,38, 38,61,
|
||||
75,88, 76,89, 38,62, 38,62,
|
||||
38,62, 38,62, 38,62, 38,62,
|
||||
38,62, 38,62, 38,62, 38,62,
|
||||
80,92, 81,93, 13,41, 13,41,
|
||||
13,41, 13,41, 13,41, 13,41,
|
||||
13,41, 13,41, 13,41, 13,41,
|
||||
82,94, 83,95, 84,96, 10,38,
|
||||
10,38, 86,97, 10,38, 13,41,
|
||||
13,41, 13,41, 13,41, 13,41,
|
||||
13,41, 13,41, 13,41, 13,41,
|
||||
13,41, 13,41, 13,41, 13,41,
|
||||
13,41, 13,41, 13,41, 13,41,
|
||||
13,41, 13,41, 13,41, 13,41,
|
||||
13,41, 13,41, 13,41, 13,41,
|
||||
13,41, 87,98, 88,99, 60,79,
|
||||
60,79, 13,41, 60,79, 13,41,
|
||||
13,41, 13,41, 13,41, 13,41,
|
||||
13,41, 13,41, 13,41, 13,41,
|
||||
13,41, 13,41, 13,41, 13,41,
|
||||
13,41, 13,41, 13,41, 13,41,
|
||||
13,41, 13,41, 13,41, 13,41,
|
||||
13,41, 13,41, 13,41, 13,41,
|
||||
13,41, 33,33, 89,100, 60,79,
|
||||
60,79, 92,101, 60,79, 93,102,
|
||||
95,103, 33,33, 33,0, 96,104,
|
||||
99,105, 100,106, 102,107, 106,108,
|
||||
107,109, 35,35, 35,35, 35,35,
|
||||
35,35, 35,35, 35,35, 35,35,
|
||||
35,35, 35,35, 35,35, 108,110,
|
||||
0,0, 0,0, 0,0, 0,0,
|
||||
0,0, 0,0, 33,33, 0,0,
|
||||
0,0, 35,59, 35,59, 33,33,
|
||||
35,59, 0,0, 0,0, 33,33,
|
||||
0,0, 0,0, 0,0, 0,0,
|
||||
33,33, 0,0, 0,0, 0,0,
|
||||
0,0, 36,60, 36,60, 36,60,
|
||||
36,60, 36,60, 36,60, 36,60,
|
||||
36,60, 36,60, 36,60, 0,0,
|
||||
0,0, 33,33, 0,0, 0,0,
|
||||
33,33, 35,59, 35,59, 0,0,
|
||||
35,59, 36,38, 36,38, 59,77,
|
||||
36,38, 59,77, 0,0, 0,0,
|
||||
59,78, 59,78, 59,78, 59,78,
|
||||
59,78, 59,78, 59,78, 59,78,
|
||||
59,78, 59,78, 61,62, 61,62,
|
||||
61,62, 61,62, 61,62, 61,62,
|
||||
61,62, 61,62, 61,62, 61,62,
|
||||
0,0, 0,0, 0,0, 0,0,
|
||||
0,0, 36,38, 36,38, 0,0,
|
||||
36,38, 77,78, 77,78, 77,78,
|
||||
77,78, 77,78, 77,78, 77,78,
|
||||
77,78, 77,78, 77,78, 79,90,
|
||||
0,0, 79,90, 0,0, 0,0,
|
||||
79,91, 79,91, 79,91, 79,91,
|
||||
79,91, 79,91, 79,91, 79,91,
|
||||
79,91, 79,91, 90,91, 90,91,
|
||||
90,91, 90,91, 90,91, 90,91,
|
||||
90,91, 90,91, 90,91, 90,91,
|
||||
0,0};
|
||||
struct yysvf yysvec[] = {
|
||||
0, 0, 0,
|
||||
yycrank+-1, 0, yyvstop+1,
|
||||
yycrank+-28, yysvec+1, yyvstop+3,
|
||||
yycrank+0, 0, yyvstop+5,
|
||||
yycrank+4, 0, yyvstop+7,
|
||||
yycrank+0, 0, yyvstop+10,
|
||||
yycrank+-11, 0, yyvstop+12,
|
||||
yycrank+-17, 0, yyvstop+14,
|
||||
yycrank+7, 0, yyvstop+16,
|
||||
yycrank+107, 0, yyvstop+18,
|
||||
yycrank+119, 0, yyvstop+20,
|
||||
yycrank+6, 0, yyvstop+23,
|
||||
yycrank+7, 0, yyvstop+25,
|
||||
yycrank+158, 0, yyvstop+27,
|
||||
yycrank+4, yysvec+13, yyvstop+30,
|
||||
yycrank+5, yysvec+13, yyvstop+33,
|
||||
yycrank+11, yysvec+13, yyvstop+36,
|
||||
yycrank+5, yysvec+13, yyvstop+39,
|
||||
yycrank+5, yysvec+13, yyvstop+42,
|
||||
yycrank+12, yysvec+13, yyvstop+45,
|
||||
yycrank+21, yysvec+13, yyvstop+48,
|
||||
yycrank+10, yysvec+13, yyvstop+51,
|
||||
yycrank+4, yysvec+13, yyvstop+54,
|
||||
yycrank+4, yysvec+13, yyvstop+57,
|
||||
yycrank+21, yysvec+13, yyvstop+60,
|
||||
yycrank+9, yysvec+13, yyvstop+63,
|
||||
yycrank+9, 0, yyvstop+66,
|
||||
yycrank+40, 0, yyvstop+68,
|
||||
yycrank+0, yysvec+4, yyvstop+70,
|
||||
yycrank+0, yysvec+6, 0,
|
||||
yycrank+0, 0, yyvstop+72,
|
||||
yycrank+0, yysvec+7, 0,
|
||||
yycrank+0, 0, yyvstop+74,
|
||||
yycrank+-280, 0, yyvstop+76,
|
||||
yycrank+0, 0, yyvstop+78,
|
||||
yycrank+249, 0, yyvstop+80,
|
||||
yycrank+285, 0, yyvstop+82,
|
||||
yycrank+0, yysvec+10, yyvstop+84,
|
||||
yycrank+146, 0, 0,
|
||||
yycrank+0, 0, yyvstop+86,
|
||||
yycrank+0, 0, yyvstop+88,
|
||||
yycrank+0, yysvec+13, yyvstop+90,
|
||||
yycrank+10, yysvec+13, yyvstop+92,
|
||||
yycrank+0, yysvec+13, yyvstop+94,
|
||||
yycrank+19, yysvec+13, yyvstop+97,
|
||||
yycrank+35, yysvec+13, yyvstop+99,
|
||||
yycrank+27, yysvec+13, yyvstop+101,
|
||||
yycrank+0, yysvec+13, yyvstop+103,
|
||||
yycrank+42, yysvec+13, yyvstop+106,
|
||||
yycrank+35, yysvec+13, yyvstop+108,
|
||||
yycrank+30, yysvec+13, yyvstop+110,
|
||||
yycrank+0, yysvec+13, yyvstop+112,
|
||||
yycrank+36, yysvec+13, yyvstop+115,
|
||||
yycrank+48, yysvec+13, yyvstop+117,
|
||||
yycrank+35, yysvec+13, yyvstop+119,
|
||||
yycrank+61, yysvec+13, yyvstop+121,
|
||||
yycrank+0, 0, yyvstop+123,
|
||||
yycrank+76, 0, 0,
|
||||
yycrank+67, 0, 0,
|
||||
yycrank+312, 0, 0,
|
||||
yycrank+183, yysvec+36, yyvstop+125,
|
||||
yycrank+322, 0, 0,
|
||||
yycrank+0, yysvec+61, yyvstop+128,
|
||||
yycrank+0, yysvec+13, yyvstop+130,
|
||||
yycrank+78, yysvec+13, yyvstop+133,
|
||||
yycrank+0, yysvec+13, yyvstop+135,
|
||||
yycrank+81, yysvec+13, yyvstop+138,
|
||||
yycrank+84, yysvec+13, yyvstop+140,
|
||||
yycrank+0, yysvec+13, yyvstop+142,
|
||||
yycrank+0, yysvec+13, yyvstop+145,
|
||||
yycrank+81, yysvec+13, yyvstop+148,
|
||||
yycrank+66, yysvec+13, yyvstop+150,
|
||||
yycrank+74, yysvec+13, yyvstop+152,
|
||||
yycrank+80, yysvec+13, yyvstop+154,
|
||||
yycrank+78, yysvec+13, yyvstop+156,
|
||||
yycrank+94, 0, 0,
|
||||
yycrank+93, 0, 0,
|
||||
yycrank+341, 0, 0,
|
||||
yycrank+0, yysvec+77, yyvstop+158,
|
||||
yycrank+356, 0, 0,
|
||||
yycrank+99, yysvec+13, yyvstop+160,
|
||||
yycrank+89, yysvec+13, yyvstop+163,
|
||||
yycrank+108, yysvec+13, yyvstop+165,
|
||||
yycrank+120, yysvec+13, yyvstop+167,
|
||||
yycrank+104, yysvec+13, yyvstop+169,
|
||||
yycrank+0, yysvec+13, yyvstop+171,
|
||||
yycrank+113, yysvec+13, yyvstop+174,
|
||||
yycrank+148, yysvec+13, yyvstop+176,
|
||||
yycrank+133, 0, 0,
|
||||
yycrank+181, 0, 0,
|
||||
yycrank+366, 0, 0,
|
||||
yycrank+0, yysvec+90, yyvstop+178,
|
||||
yycrank+183, yysvec+13, yyvstop+181,
|
||||
yycrank+182, yysvec+13, yyvstop+183,
|
||||
yycrank+0, yysvec+13, yyvstop+185,
|
||||
yycrank+172, yysvec+13, yyvstop+189,
|
||||
yycrank+181, yysvec+13, yyvstop+191,
|
||||
yycrank+0, yysvec+13, yyvstop+193,
|
||||
yycrank+0, yysvec+13, yyvstop+196,
|
||||
yycrank+189, 0, 0,
|
||||
yycrank+195, 0, 0,
|
||||
yycrank+0, yysvec+13, yyvstop+199,
|
||||
yycrank+183, yysvec+13, yyvstop+202,
|
||||
yycrank+0, yysvec+13, yyvstop+204,
|
||||
yycrank+0, yysvec+13, yyvstop+207,
|
||||
yycrank+0, 0, yyvstop+210,
|
||||
yycrank+178, 0, 0,
|
||||
yycrank+186, yysvec+13, yyvstop+212,
|
||||
yycrank+204, 0, 0,
|
||||
yycrank+0, yysvec+13, yyvstop+214,
|
||||
yycrank+0, 0, yyvstop+217,
|
||||
0, 0, 0};
|
||||
struct yywork *yytop = yycrank+423;
|
||||
struct yysvf *yybgin = yysvec+1;
|
||||
char yymatch[] = {
|
||||
00 ,01 ,01 ,01 ,01 ,01 ,01 ,01 ,
|
||||
01 ,011 ,012 ,01 ,01 ,01 ,01 ,01 ,
|
||||
01 ,01 ,01 ,01 ,01 ,01 ,01 ,01 ,
|
||||
01 ,01 ,01 ,01 ,01 ,01 ,01 ,01 ,
|
||||
011 ,01 ,'"' ,01 ,01 ,01 ,01 ,047 ,
|
||||
01 ,01 ,01 ,'+' ,01 ,'+' ,01 ,01 ,
|
||||
'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,
|
||||
'0' ,'0' ,01 ,01 ,01 ,01 ,01 ,01 ,
|
||||
01 ,'A' ,'A' ,'A' ,'D' ,'D' ,'A' ,'D' ,
|
||||
'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,
|
||||
'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,
|
||||
'A' ,'A' ,'A' ,01 ,01 ,01 ,01 ,'A' ,
|
||||
01 ,'A' ,'A' ,'A' ,'D' ,'D' ,'A' ,'D' ,
|
||||
'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,
|
||||
'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,
|
||||
'A' ,'A' ,'A' ,01 ,01 ,01 ,01 ,01 ,
|
||||
0};
|
||||
char yyextra[] = {
|
||||
0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,
|
||||
0};
|
||||
#ifndef lint
|
||||
static char ncform_sccsid[] = "@(#)ncform 1.6 88/02/08 SMI"; /* from S5R2 1.2 */
|
||||
#endif
|
||||
|
||||
int yylineno =1;
|
||||
# define YYU(x) x
|
||||
# define NLSTATE yyprevious=YYNEWLINE
|
||||
char yytext[YYLMAX];
|
||||
struct yysvf *yylstate [YYLMAX], **yylsp, **yyolsp;
|
||||
char yysbuf[YYLMAX];
|
||||
char *yysptr = yysbuf;
|
||||
int *yyfnd;
|
||||
extern struct yysvf *yyestate;
|
||||
int yyprevious = YYNEWLINE;
|
||||
yylook(){
|
||||
register struct yysvf *yystate, **lsp;
|
||||
register struct yywork *yyt;
|
||||
struct yysvf *yyz;
|
||||
int yych, yyfirst;
|
||||
struct yywork *yyr;
|
||||
# ifdef LEXDEBUG
|
||||
int debug;
|
||||
# endif
|
||||
char *yylastch;
|
||||
/* start off machines */
|
||||
# ifdef LEXDEBUG
|
||||
debug = 0;
|
||||
# endif
|
||||
yyfirst=1;
|
||||
if (!yymorfg)
|
||||
yylastch = yytext;
|
||||
else {
|
||||
yymorfg=0;
|
||||
yylastch = yytext+yyleng;
|
||||
}
|
||||
for(;;){
|
||||
lsp = yylstate;
|
||||
yyestate = yystate = yybgin;
|
||||
if (yyprevious==YYNEWLINE) yystate++;
|
||||
for (;;){
|
||||
# ifdef LEXDEBUG
|
||||
if(debug)fprintf(yyout,"state %d\n",yystate-yysvec-1);
|
||||
# endif
|
||||
yyt = yystate->yystoff;
|
||||
if(yyt == yycrank && !yyfirst){ /* may not be any transitions */
|
||||
yyz = yystate->yyother;
|
||||
if(yyz == 0)break;
|
||||
if(yyz->yystoff == yycrank)break;
|
||||
}
|
||||
*yylastch++ = yych = input();
|
||||
yyfirst=0;
|
||||
tryagain:
|
||||
# ifdef LEXDEBUG
|
||||
if(debug){
|
||||
fprintf(yyout,"char ");
|
||||
allprint(yych);
|
||||
putchar('\n');
|
||||
}
|
||||
# endif
|
||||
yyr = yyt;
|
||||
if ( (int)yyt > (int)yycrank){
|
||||
yyt = yyr + yych;
|
||||
if (yyt <= yytop && yyt->verify+yysvec == yystate){
|
||||
if(yyt->advance+yysvec == YYLERR) /* error transitions */
|
||||
{unput(*--yylastch);break;}
|
||||
*lsp++ = yystate = yyt->advance+yysvec;
|
||||
goto contin;
|
||||
}
|
||||
}
|
||||
# ifdef YYOPTIM
|
||||
else if((int)yyt < (int)yycrank) { /* r < yycrank */
|
||||
yyt = yyr = yycrank+(yycrank-yyt);
|
||||
# ifdef LEXDEBUG
|
||||
if(debug)fprintf(yyout,"compressed state\n");
|
||||
# endif
|
||||
yyt = yyt + yych;
|
||||
if(yyt <= yytop && yyt->verify+yysvec == yystate){
|
||||
if(yyt->advance+yysvec == YYLERR) /* error transitions */
|
||||
{unput(*--yylastch);break;}
|
||||
*lsp++ = yystate = yyt->advance+yysvec;
|
||||
goto contin;
|
||||
}
|
||||
yyt = yyr + YYU(yymatch[yych]);
|
||||
# ifdef LEXDEBUG
|
||||
if(debug){
|
||||
fprintf(yyout,"try fall back character ");
|
||||
allprint(YYU(yymatch[yych]));
|
||||
putchar('\n');
|
||||
}
|
||||
# endif
|
||||
if(yyt <= yytop && yyt->verify+yysvec == yystate){
|
||||
if(yyt->advance+yysvec == YYLERR) /* error transition */
|
||||
{unput(*--yylastch);break;}
|
||||
*lsp++ = yystate = yyt->advance+yysvec;
|
||||
goto contin;
|
||||
}
|
||||
}
|
||||
if ((yystate = yystate->yyother) && (yyt= yystate->yystoff) != yycrank){
|
||||
# ifdef LEXDEBUG
|
||||
if(debug)fprintf(yyout,"fall back to state %d\n",yystate-yysvec-1);
|
||||
# endif
|
||||
goto tryagain;
|
||||
}
|
||||
# endif
|
||||
else
|
||||
{unput(*--yylastch);break;}
|
||||
contin:
|
||||
# ifdef LEXDEBUG
|
||||
if(debug){
|
||||
fprintf(yyout,"state %d char ",yystate-yysvec-1);
|
||||
allprint(yych);
|
||||
putchar('\n');
|
||||
}
|
||||
# endif
|
||||
;
|
||||
}
|
||||
# ifdef LEXDEBUG
|
||||
if(debug){
|
||||
fprintf(yyout,"stopped at %d with ",*(lsp-1)-yysvec-1);
|
||||
allprint(yych);
|
||||
putchar('\n');
|
||||
}
|
||||
# endif
|
||||
while (lsp-- > yylstate){
|
||||
*yylastch-- = 0;
|
||||
if (*lsp != 0 && (yyfnd= (*lsp)->yystops) && *yyfnd > 0){
|
||||
yyolsp = lsp;
|
||||
if(yyextra[*yyfnd]){ /* must backup */
|
||||
while(yyback((*lsp)->yystops,-*yyfnd) != 1 && lsp > yylstate){
|
||||
lsp--;
|
||||
unput(*yylastch--);
|
||||
}
|
||||
}
|
||||
yyprevious = YYU(*yylastch);
|
||||
yylsp = lsp;
|
||||
yyleng = yylastch-yytext+1;
|
||||
yytext[yyleng] = 0;
|
||||
# ifdef LEXDEBUG
|
||||
if(debug){
|
||||
fprintf(yyout,"\nmatch ");
|
||||
sprint(yytext);
|
||||
fprintf(yyout," action %d\n",*yyfnd);
|
||||
}
|
||||
# endif
|
||||
return(*yyfnd++);
|
||||
}
|
||||
unput(*yylastch);
|
||||
}
|
||||
if (yytext[0] == 0 /* && feof(yyin) */)
|
||||
{
|
||||
yysptr=yysbuf;
|
||||
return(0);
|
||||
}
|
||||
yyprevious = yytext[0] = input();
|
||||
if (yyprevious>0)
|
||||
output(yyprevious);
|
||||
yylastch=yytext;
|
||||
# ifdef LEXDEBUG
|
||||
if(debug)putchar('\n');
|
||||
# endif
|
||||
}
|
||||
}
|
||||
yyback(p, m)
|
||||
int *p;
|
||||
{
|
||||
if (p==0) return(0);
|
||||
while (*p)
|
||||
{
|
||||
if (*p++ == m)
|
||||
return(1);
|
||||
}
|
||||
return(0);
|
||||
}
|
||||
/* the following are only used in the lex library */
|
||||
yyinput(){
|
||||
return(input());
|
||||
}
|
||||
yyoutput(c)
|
||||
int c; {
|
||||
output(c);
|
||||
}
|
||||
yyunput(c)
|
||||
int c; {
|
||||
unput(c);
|
||||
}
|
||||
55
fixed/lua.c
Normal file
55
fixed/lua.c
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
** lua.c
|
||||
** Linguagem para Usuarios de Aplicacao
|
||||
** TeCGraf - PUC-Rio
|
||||
** 28 Apr 93
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "lua.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
void test (void)
|
||||
{
|
||||
lua_pushobject(lua_getparam(1));
|
||||
lua_call ("c", 1);
|
||||
}
|
||||
|
||||
|
||||
static void callfunc (void)
|
||||
{
|
||||
lua_Object obj = lua_getparam (1);
|
||||
if (lua_isstring(obj)) lua_call(lua_getstring(obj),0);
|
||||
}
|
||||
|
||||
static void execstr (void)
|
||||
{
|
||||
lua_Object obj = lua_getparam (1);
|
||||
if (lua_isstring(obj)) lua_dostring(lua_getstring(obj));
|
||||
}
|
||||
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
int i;
|
||||
if (argc < 2)
|
||||
{
|
||||
puts ("usage: lua filename [functionnames]");
|
||||
return;
|
||||
}
|
||||
lua_register ("callfunc", callfunc);
|
||||
lua_register ("execstr", execstr);
|
||||
lua_register ("test", test);
|
||||
iolib_open ();
|
||||
strlib_open ();
|
||||
mathlib_open ();
|
||||
lua_dofile (argv[1]);
|
||||
for (i=2; i<argc; i++)
|
||||
{
|
||||
lua_call (argv[i],0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
1
floatingpoint.h
Normal file
1
floatingpoint.h
Normal file
@@ -0,0 +1 @@
|
||||
/* empty file to please silly code in iolib.c and opcode.c */
|
||||
5
globals.lua
Normal file
5
globals.lua
Normal file
@@ -0,0 +1,5 @@
|
||||
k,v=nextvar(k)
|
||||
while k do
|
||||
print(k)
|
||||
k,v=nextvar(k)
|
||||
end
|
||||
259
hash.c
Normal file
259
hash.c
Normal file
@@ -0,0 +1,259 @@
|
||||
/*
|
||||
** hash.c
|
||||
** hash manager for lua
|
||||
** Luiz Henrique de Figueiredo - 17 Aug 90
|
||||
** Modified by Waldemar Celes Filho
|
||||
** 12 May 93
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "opcode.h"
|
||||
#include "hash.h"
|
||||
#include "inout.h"
|
||||
#include "table.h"
|
||||
#include "lua.h"
|
||||
|
||||
#define streq(s1,s2) (strcmp(s1,s2)==0)
|
||||
#define strneq(s1,s2) (strcmp(s1,s2)!=0)
|
||||
|
||||
#define new(s) ((s *)malloc(sizeof(s)))
|
||||
#define newvector(n,s) ((s *)calloc(n,sizeof(s)))
|
||||
|
||||
#define nhash(t) ((t)->nhash)
|
||||
#define nodelist(t) ((t)->list)
|
||||
#define list(t,i) ((t)->list[i])
|
||||
#define ref_tag(n) (tag(&(n)->ref))
|
||||
#define ref_nvalue(n) (nvalue(&(n)->ref))
|
||||
#define ref_svalue(n) (svalue(&(n)->ref))
|
||||
|
||||
static int head (Hash *t, Object *ref) /* hash function */
|
||||
{
|
||||
if (tag(ref) == T_NUMBER) return (((int)nvalue(ref))%nhash(t));
|
||||
else if (tag(ref) == T_STRING)
|
||||
{
|
||||
int h;
|
||||
char *name = svalue(ref);
|
||||
for (h=0; *name!=0; name++) /* interpret name as binary number */
|
||||
{
|
||||
h <<= 8;
|
||||
h += (unsigned char) *name; /* avoid sign extension */
|
||||
h %= nhash(t); /* make it a valid index */
|
||||
}
|
||||
return h;
|
||||
}
|
||||
else
|
||||
{
|
||||
lua_reportbug ("unexpected type to index table");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
static Node *present(Hash *t, Object *ref, int h)
|
||||
{
|
||||
Node *n=NULL, *p;
|
||||
if (tag(ref) == T_NUMBER)
|
||||
{
|
||||
for (p=NULL,n=list(t,h); n!=NULL; p=n, n=n->next)
|
||||
if (ref_tag(n) == T_NUMBER && nvalue(ref) == ref_nvalue(n)) break;
|
||||
}
|
||||
else if (tag(ref) == T_STRING)
|
||||
{
|
||||
for (p=NULL,n=list(t,h); n!=NULL; p=n, n=n->next)
|
||||
if (ref_tag(n) == T_STRING && streq(svalue(ref),ref_svalue(n))) break;
|
||||
}
|
||||
if (n==NULL) /* name not present */
|
||||
return NULL;
|
||||
#if 0
|
||||
if (p!=NULL) /* name present but not first */
|
||||
{
|
||||
p->next=n->next; /* move-to-front self-organization */
|
||||
n->next=list(t,h);
|
||||
list(t,h)=n;
|
||||
}
|
||||
#endif
|
||||
return n;
|
||||
}
|
||||
|
||||
static void freelist (Node *n)
|
||||
{
|
||||
while (n)
|
||||
{
|
||||
Node *next = n->next;
|
||||
free (n);
|
||||
n = next;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Create a new hash. Return the hash pointer or NULL on error.
|
||||
*/
|
||||
Hash *lua_hashcreate (unsigned int nhash)
|
||||
{
|
||||
Hash *t = new (Hash);
|
||||
if (t == NULL)
|
||||
{
|
||||
lua_error ("not enough memory");
|
||||
return NULL;
|
||||
}
|
||||
nhash(t) = nhash;
|
||||
markarray(t) = 0;
|
||||
nodelist(t) = newvector (nhash, Node*);
|
||||
if (nodelist(t) == NULL)
|
||||
{
|
||||
lua_error ("not enough memory");
|
||||
return NULL;
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
/*
|
||||
** Delete a hash
|
||||
*/
|
||||
void lua_hashdelete (Hash *h)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<nhash(h); i++)
|
||||
freelist (list(h,i));
|
||||
free (nodelist(h));
|
||||
free(h);
|
||||
}
|
||||
|
||||
/*
|
||||
** If the hash node is present, return its pointer, otherwise create a new
|
||||
** node for the given reference and also return its pointer.
|
||||
** On error, return NULL.
|
||||
*/
|
||||
Object *lua_hashdefine (Hash *t, Object *ref)
|
||||
{
|
||||
int h;
|
||||
Node *n;
|
||||
h = head (t, ref);
|
||||
if (h < 0) return NULL;
|
||||
|
||||
n = present(t, ref, h);
|
||||
if (n == NULL)
|
||||
{
|
||||
n = new(Node);
|
||||
if (n == NULL)
|
||||
{
|
||||
lua_error ("not enough memory");
|
||||
return NULL;
|
||||
}
|
||||
n->ref = *ref;
|
||||
tag(&n->val) = T_NIL;
|
||||
n->next = list(t,h); /* link node to head of list */
|
||||
list(t,h) = n;
|
||||
}
|
||||
return (&n->val);
|
||||
}
|
||||
|
||||
/*
|
||||
** Mark a hash and check its elements
|
||||
*/
|
||||
void lua_hashmark (Hash *h)
|
||||
{
|
||||
int i;
|
||||
|
||||
markarray(h) = 1;
|
||||
|
||||
for (i=0; i<nhash(h); i++)
|
||||
{
|
||||
Node *n;
|
||||
for (n = list(h,i); n != NULL; n = n->next)
|
||||
{
|
||||
lua_markobject (&n->ref);
|
||||
lua_markobject (&n->val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Internal function to manipulate arrays.
|
||||
** Given an array object and a reference value, return the next element
|
||||
** in the hash.
|
||||
** This function pushs the element value and its reference to the stack.
|
||||
*/
|
||||
#include "lua.h"
|
||||
static void firstnode (Hash *a, int h)
|
||||
{
|
||||
if (h < nhash(a))
|
||||
{
|
||||
int i;
|
||||
for (i=h; i<nhash(a); i++)
|
||||
{
|
||||
if (list(a,i) != NULL && tag(&list(a,i)->val) != T_NIL)
|
||||
{
|
||||
lua_pushobject (&list(a,i)->ref);
|
||||
lua_pushobject (&list(a,i)->val);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
lua_pushnil();
|
||||
lua_pushnil();
|
||||
}
|
||||
void lua_next (void)
|
||||
{
|
||||
Hash *a;
|
||||
Object *o = lua_getparam (1);
|
||||
Object *r = lua_getparam (2);
|
||||
if (o == NULL || r == NULL)
|
||||
{ lua_error ("too few arguments to function `next'"); return; }
|
||||
if (lua_getparam (3) != NULL)
|
||||
{ lua_error ("too many arguments to function `next'"); return; }
|
||||
if (tag(o) != T_ARRAY)
|
||||
{ lua_error ("first argument of function `next' is not a table"); return; }
|
||||
a = avalue(o);
|
||||
if (tag(r) == T_NIL)
|
||||
{
|
||||
firstnode (a, 0);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
int h = head (a, r);
|
||||
if (h >= 0)
|
||||
{
|
||||
Node *n = list(a,h);
|
||||
while (n)
|
||||
{
|
||||
if (memcmp(&n->ref,r,sizeof(Object)) == 0)
|
||||
{
|
||||
if (n->next == NULL)
|
||||
{
|
||||
firstnode (a, h+1);
|
||||
return;
|
||||
}
|
||||
else if (tag(&n->next->val) != T_NIL)
|
||||
{
|
||||
lua_pushobject (&n->next->ref);
|
||||
lua_pushobject (&n->next->val);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
Node *next = n->next->next;
|
||||
while (next != NULL && tag(&next->val) == T_NIL) next = next->next;
|
||||
if (next == NULL)
|
||||
{
|
||||
firstnode (a, h+1);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
lua_pushobject (&next->ref);
|
||||
lua_pushobject (&next->val);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
n = n->next;
|
||||
}
|
||||
if (n == NULL)
|
||||
lua_error ("error in function 'next': reference not found");
|
||||
}
|
||||
}
|
||||
}
|
||||
35
hash.h
Normal file
35
hash.h
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
** hash.h
|
||||
** hash manager for lua
|
||||
** Luiz Henrique de Figueiredo - 17 Aug 90
|
||||
** Modified by Waldemar Celes Filho
|
||||
** 26 Apr 93
|
||||
*/
|
||||
|
||||
#ifndef hash_h
|
||||
#define hash_h
|
||||
|
||||
typedef struct node
|
||||
{
|
||||
Object ref;
|
||||
Object val;
|
||||
struct node *next;
|
||||
} Node;
|
||||
|
||||
typedef struct Hash
|
||||
{
|
||||
char mark;
|
||||
unsigned int nhash;
|
||||
Node **list;
|
||||
} Hash;
|
||||
|
||||
#define markarray(t) ((t)->mark)
|
||||
|
||||
Hash *lua_hashcreate (unsigned int nhash);
|
||||
void lua_hashdelete (Hash *h);
|
||||
Object *lua_hashdefine (Hash *t, Object *ref);
|
||||
void lua_hashmark (Hash *h);
|
||||
|
||||
void lua_next (void);
|
||||
|
||||
#endif
|
||||
188
inout.c
Normal file
188
inout.c
Normal file
@@ -0,0 +1,188 @@
|
||||
/*
|
||||
** inout.c
|
||||
** Provide function to realise the input/output function and debugger
|
||||
** facilities.
|
||||
**
|
||||
** Waldemar Celes Filho
|
||||
** TeCGraf - PUC-Rio
|
||||
** 11 May 93
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "opcode.h"
|
||||
#include "hash.h"
|
||||
#include "inout.h"
|
||||
#include "table.h"
|
||||
|
||||
/* Exported variables */
|
||||
int lua_linenumber;
|
||||
int lua_debug;
|
||||
int lua_debugline;
|
||||
|
||||
/* Internal variables */
|
||||
#ifndef MAXFUNCSTACK
|
||||
#define MAXFUNCSTACK 32
|
||||
#endif
|
||||
static struct { int file; int function; } funcstack[MAXFUNCSTACK];
|
||||
static int nfuncstack=0;
|
||||
|
||||
static FILE *fp;
|
||||
static char *st;
|
||||
static void (*usererror) (char *s);
|
||||
|
||||
/*
|
||||
** Function to set user function to handle errors.
|
||||
*/
|
||||
void lua_errorfunction (void (*fn) (char *s))
|
||||
{
|
||||
usererror = fn;
|
||||
}
|
||||
|
||||
/*
|
||||
** Function to get the next character from the input file
|
||||
*/
|
||||
static int fileinput (void)
|
||||
{
|
||||
int c = fgetc (fp);
|
||||
return (c == EOF ? 0 : c);
|
||||
}
|
||||
|
||||
/*
|
||||
** Function to unget the next character from to input file
|
||||
*/
|
||||
static void fileunput (int c)
|
||||
{
|
||||
ungetc (c, fp);
|
||||
}
|
||||
|
||||
/*
|
||||
** Function to get the next character from the input string
|
||||
*/
|
||||
static int stringinput (void)
|
||||
{
|
||||
st++;
|
||||
return (*(st-1));
|
||||
}
|
||||
|
||||
/*
|
||||
** Function to unget the next character from to input string
|
||||
*/
|
||||
static void stringunput (int c)
|
||||
{
|
||||
st--;
|
||||
}
|
||||
|
||||
/*
|
||||
** Function to open a file to be input unit.
|
||||
** Return 0 on success or 1 on error.
|
||||
*/
|
||||
int lua_openfile (char *fn)
|
||||
{
|
||||
lua_linenumber = 1;
|
||||
lua_setinput (fileinput);
|
||||
lua_setunput (fileunput);
|
||||
fp = fopen (fn, "r");
|
||||
if (fp == NULL) return 1;
|
||||
if (lua_addfile (fn)) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
** Function to close an opened file
|
||||
*/
|
||||
void lua_closefile (void)
|
||||
{
|
||||
if (fp != NULL)
|
||||
{
|
||||
fclose (fp);
|
||||
fp = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Function to open a string to be input unit
|
||||
*/
|
||||
int lua_openstring (char *s)
|
||||
{
|
||||
lua_linenumber = 1;
|
||||
lua_setinput (stringinput);
|
||||
lua_setunput (stringunput);
|
||||
st = s;
|
||||
{
|
||||
char sn[64];
|
||||
sprintf (sn, "String: %10.10s...", s);
|
||||
if (lua_addfile (sn)) return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
** Call user function to handle error messages, if registred. Or report error
|
||||
** using standard function (fprintf).
|
||||
*/
|
||||
void lua_error (char *s)
|
||||
{
|
||||
if (usererror != NULL) usererror (s);
|
||||
else fprintf (stderr, "lua: %s\n", s);
|
||||
}
|
||||
|
||||
/*
|
||||
** Called to execute SETFUNCTION opcode, this function pushs a function into
|
||||
** function stack. Return 0 on success or 1 on error.
|
||||
*/
|
||||
int lua_pushfunction (int file, int function)
|
||||
{
|
||||
if (nfuncstack >= MAXFUNCSTACK-1)
|
||||
{
|
||||
lua_error ("function stack overflow");
|
||||
return 1;
|
||||
}
|
||||
funcstack[nfuncstack].file = file;
|
||||
funcstack[nfuncstack].function = function;
|
||||
nfuncstack++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
** Called to execute RESET opcode, this function pops a function from
|
||||
** function stack.
|
||||
*/
|
||||
void lua_popfunction (void)
|
||||
{
|
||||
nfuncstack--;
|
||||
}
|
||||
|
||||
/*
|
||||
** Report bug building a message and sending it to lua_error function.
|
||||
*/
|
||||
void lua_reportbug (char *s)
|
||||
{
|
||||
char msg[1024];
|
||||
strcpy (msg, s);
|
||||
if (lua_debugline != 0)
|
||||
{
|
||||
int i;
|
||||
if (nfuncstack > 0)
|
||||
{
|
||||
sprintf (strchr(msg,0),
|
||||
"\n\tin statement begining at line %d in function \"%s\" of file \"%s\"",
|
||||
lua_debugline, s_name(funcstack[nfuncstack-1].function),
|
||||
lua_file[funcstack[nfuncstack-1].file]);
|
||||
sprintf (strchr(msg,0), "\n\tactive stack\n");
|
||||
for (i=nfuncstack-1; i>=0; i--)
|
||||
sprintf (strchr(msg,0), "\t-> function \"%s\" of file \"%s\"\n",
|
||||
s_name(funcstack[i].function),
|
||||
lua_file[funcstack[i].file]);
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf (strchr(msg,0),
|
||||
"\n\tin statement begining at line %d of file \"%s\"",
|
||||
lua_debugline, lua_filename());
|
||||
}
|
||||
}
|
||||
lua_error (msg);
|
||||
}
|
||||
|
||||
24
inout.h
Normal file
24
inout.h
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
** inout.h
|
||||
**
|
||||
** Waldemar Celes Filho
|
||||
** TeCGraf - PUC-Rio
|
||||
** 11 May 93
|
||||
*/
|
||||
|
||||
|
||||
#ifndef inout_h
|
||||
#define inout_h
|
||||
|
||||
extern int lua_linenumber;
|
||||
extern int lua_debug;
|
||||
extern int lua_debugline;
|
||||
|
||||
int lua_openfile (char *fn);
|
||||
void lua_closefile (void);
|
||||
int lua_openstring (char *s);
|
||||
int lua_pushfunction (int file, int function);
|
||||
void lua_popfunction (void);
|
||||
void lua_reportbug (char *s);
|
||||
|
||||
#endif
|
||||
401
iolib.c
Normal file
401
iolib.c
Normal file
@@ -0,0 +1,401 @@
|
||||
/*
|
||||
** iolib.c
|
||||
** Input/output library to LUA
|
||||
**
|
||||
** Waldemar Celes Filho
|
||||
** TeCGraf - PUC-Rio
|
||||
** 19 May 93
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#ifdef __GNUC__
|
||||
#include <floatingpoint.h>
|
||||
#endif
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
static FILE *in=stdin, *out=stdout;
|
||||
|
||||
/*
|
||||
** Open a file to read.
|
||||
** LUA interface:
|
||||
** status = readfrom (filename)
|
||||
** where:
|
||||
** status = 1 -> success
|
||||
** status = 0 -> error
|
||||
*/
|
||||
static void io_readfrom (void)
|
||||
{
|
||||
lua_Object o = lua_getparam (1);
|
||||
if (o == NULL) /* restore standart input */
|
||||
{
|
||||
if (in != stdin)
|
||||
{
|
||||
fclose (in);
|
||||
in = stdin;
|
||||
}
|
||||
lua_pushnumber (1);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!lua_isstring (o))
|
||||
{
|
||||
lua_error ("incorrect argument to function 'readfrom`");
|
||||
lua_pushnumber (0);
|
||||
}
|
||||
else
|
||||
{
|
||||
FILE *fp = fopen (lua_getstring(o),"r");
|
||||
if (fp == NULL)
|
||||
{
|
||||
lua_pushnumber (0);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (in != stdin) fclose (in);
|
||||
in = fp;
|
||||
lua_pushnumber (1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Open a file to write.
|
||||
** LUA interface:
|
||||
** status = writeto (filename)
|
||||
** where:
|
||||
** status = 1 -> success
|
||||
** status = 0 -> error
|
||||
*/
|
||||
static void io_writeto (void)
|
||||
{
|
||||
lua_Object o = lua_getparam (1);
|
||||
if (o == NULL) /* restore standart output */
|
||||
{
|
||||
if (out != stdout)
|
||||
{
|
||||
fclose (out);
|
||||
out = stdout;
|
||||
}
|
||||
lua_pushnumber (1);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!lua_isstring (o))
|
||||
{
|
||||
lua_error ("incorrect argument to function 'writeto`");
|
||||
lua_pushnumber (0);
|
||||
}
|
||||
else
|
||||
{
|
||||
FILE *fp = fopen (lua_getstring(o),"w");
|
||||
if (fp == NULL)
|
||||
{
|
||||
lua_pushnumber (0);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (out != stdout) fclose (out);
|
||||
out = fp;
|
||||
lua_pushnumber (1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Read a variable. On error put nil on stack.
|
||||
** LUA interface:
|
||||
** variable = read ([format])
|
||||
**
|
||||
** O formato pode ter um dos seguintes especificadores:
|
||||
**
|
||||
** s ou S -> para string
|
||||
** f ou F, g ou G, e ou E -> para reais
|
||||
** i ou I -> para inteiros
|
||||
**
|
||||
** Estes especificadores podem vir seguidos de numero que representa
|
||||
** o numero de campos a serem lidos.
|
||||
*/
|
||||
static void io_read (void)
|
||||
{
|
||||
lua_Object o = lua_getparam (1);
|
||||
if (o == NULL) /* free format */
|
||||
{
|
||||
int c;
|
||||
char s[256];
|
||||
while (isspace(c=fgetc(in)))
|
||||
;
|
||||
if (c == '\"')
|
||||
{
|
||||
if (fscanf (in, "%[^\"]\"", s) != 1)
|
||||
{
|
||||
lua_pushnil ();
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (c == '\'')
|
||||
{
|
||||
if (fscanf (in, "%[^\']\'", s) != 1)
|
||||
{
|
||||
lua_pushnil ();
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
char *ptr;
|
||||
double d;
|
||||
ungetc (c, in);
|
||||
if (fscanf (in, "%s", s) != 1)
|
||||
{
|
||||
lua_pushnil ();
|
||||
return;
|
||||
}
|
||||
d = strtod (s, &ptr);
|
||||
if (!(*ptr))
|
||||
{
|
||||
lua_pushnumber (d);
|
||||
return;
|
||||
}
|
||||
}
|
||||
lua_pushstring (s);
|
||||
return;
|
||||
}
|
||||
else /* formatted */
|
||||
{
|
||||
char *e = lua_getstring(o);
|
||||
char t;
|
||||
int m=0;
|
||||
while (isspace(*e)) e++;
|
||||
t = *e++;
|
||||
while (isdigit(*e))
|
||||
m = m*10 + (*e++ - '0');
|
||||
|
||||
if (m > 0)
|
||||
{
|
||||
char f[80];
|
||||
char s[256];
|
||||
sprintf (f, "%%%ds", m);
|
||||
fscanf (in, f, s);
|
||||
switch (tolower(t))
|
||||
{
|
||||
case 'i':
|
||||
{
|
||||
long int l;
|
||||
sscanf (s, "%ld", &l);
|
||||
lua_pushnumber(l);
|
||||
}
|
||||
break;
|
||||
case 'f': case 'g': case 'e':
|
||||
{
|
||||
float f;
|
||||
sscanf (s, "%f", &f);
|
||||
lua_pushnumber(f);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
lua_pushstring(s);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (tolower(t))
|
||||
{
|
||||
case 'i':
|
||||
{
|
||||
long int l;
|
||||
fscanf (in, "%ld", &l);
|
||||
lua_pushnumber(l);
|
||||
}
|
||||
break;
|
||||
case 'f': case 'g': case 'e':
|
||||
{
|
||||
float f;
|
||||
fscanf (in, "%f", &f);
|
||||
lua_pushnumber(f);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
{
|
||||
char s[256];
|
||||
fscanf (in, "%s", s);
|
||||
lua_pushstring(s);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Write a variable. On error put 0 on stack, otherwise put 1.
|
||||
** LUA interface:
|
||||
** status = write (variable [,format])
|
||||
**
|
||||
** O formato pode ter um dos seguintes especificadores:
|
||||
**
|
||||
** s ou S -> para string
|
||||
** f ou F, g ou G, e ou E -> para reais
|
||||
** i ou I -> para inteiros
|
||||
**
|
||||
** Estes especificadores podem vir seguidos de:
|
||||
**
|
||||
** [?][m][.n]
|
||||
**
|
||||
** onde:
|
||||
** ? -> indica justificacao
|
||||
** < = esquerda
|
||||
** | = centro
|
||||
** > = direita (default)
|
||||
** m -> numero maximo de campos (se exceder estoura)
|
||||
** n -> indica precisao para
|
||||
** reais -> numero de casas decimais
|
||||
** inteiros -> numero minimo de digitos
|
||||
** string -> nao se aplica
|
||||
*/
|
||||
static char *buildformat (char *e, lua_Object o)
|
||||
{
|
||||
static char buffer[512];
|
||||
static char f[80];
|
||||
char *string = &buffer[255];
|
||||
char t, j='r';
|
||||
int m=0, n=0, l;
|
||||
while (isspace(*e)) e++;
|
||||
t = *e++;
|
||||
if (*e == '<' || *e == '|' || *e == '>') j = *e++;
|
||||
while (isdigit(*e))
|
||||
m = m*10 + (*e++ - '0');
|
||||
e++; /* skip point */
|
||||
while (isdigit(*e))
|
||||
n = n*10 + (*e++ - '0');
|
||||
|
||||
sprintf(f,"%%");
|
||||
if (j == '<' || j == '|') sprintf(strchr(f,0),"-");
|
||||
if (m != 0) sprintf(strchr(f,0),"%d", m);
|
||||
if (n != 0) sprintf(strchr(f,0),".%d", n);
|
||||
sprintf(strchr(f,0), "%c", t);
|
||||
switch (tolower(t))
|
||||
{
|
||||
case 'i': t = 'i';
|
||||
sprintf (string, f, (long int)lua_getnumber(o));
|
||||
break;
|
||||
case 'f': case 'g': case 'e': t = 'f';
|
||||
sprintf (string, f, (float)lua_getnumber(o));
|
||||
break;
|
||||
case 's': t = 's';
|
||||
sprintf (string, f, lua_getstring(o));
|
||||
break;
|
||||
default: return "";
|
||||
}
|
||||
l = strlen(string);
|
||||
if (m!=0 && l>m)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<m; i++)
|
||||
string[i] = '*';
|
||||
string[i] = 0;
|
||||
}
|
||||
else if (m!=0 && j=='|')
|
||||
{
|
||||
int i=l-1;
|
||||
while (isspace(string[i])) i--;
|
||||
string -= (m-i) / 2;
|
||||
i=0;
|
||||
while (string[i]==0) string[i++] = ' ';
|
||||
string[l] = 0;
|
||||
}
|
||||
return string;
|
||||
}
|
||||
static void io_write (void)
|
||||
{
|
||||
lua_Object o1 = lua_getparam (1);
|
||||
lua_Object o2 = lua_getparam (2);
|
||||
if (o1 == NULL) /* new line */
|
||||
{
|
||||
fprintf (out, "\n");
|
||||
lua_pushnumber(1);
|
||||
}
|
||||
else if (o2 == NULL) /* free format */
|
||||
{
|
||||
int status=0;
|
||||
if (lua_isnumber(o1))
|
||||
status = fprintf (out, "%g", lua_getnumber(o1));
|
||||
else if (lua_isstring(o1))
|
||||
status = fprintf (out, "%s", lua_getstring(o1));
|
||||
lua_pushnumber(status);
|
||||
}
|
||||
else /* formated */
|
||||
{
|
||||
if (!lua_isstring(o2))
|
||||
{
|
||||
lua_error ("incorrect format to function `write'");
|
||||
lua_pushnumber(0);
|
||||
return;
|
||||
}
|
||||
lua_pushnumber(fprintf (out, "%s", buildformat(lua_getstring(o2),o1)));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Execute a executable program using "sustem".
|
||||
** On error put 0 on stack, otherwise put 1.
|
||||
*/
|
||||
void io_execute (void)
|
||||
{
|
||||
lua_Object o = lua_getparam (1);
|
||||
if (o == NULL || !lua_isstring (o))
|
||||
{
|
||||
lua_error ("incorrect argument to function 'execute`");
|
||||
lua_pushnumber (0);
|
||||
}
|
||||
else
|
||||
{
|
||||
system(lua_getstring(o));
|
||||
lua_pushnumber (1);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
** Remove a file.
|
||||
** On error put 0 on stack, otherwise put 1.
|
||||
*/
|
||||
void io_remove (void)
|
||||
{
|
||||
lua_Object o = lua_getparam (1);
|
||||
if (o == NULL || !lua_isstring (o))
|
||||
{
|
||||
lua_error ("incorrect argument to function 'execute`");
|
||||
lua_pushnumber (0);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (remove(lua_getstring(o)) == 0)
|
||||
lua_pushnumber (1);
|
||||
else
|
||||
lua_pushnumber (0);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
** Open io library
|
||||
*/
|
||||
void iolib_open (void)
|
||||
{
|
||||
lua_register ("readfrom", io_readfrom);
|
||||
lua_register ("writeto", io_writeto);
|
||||
lua_register ("read", io_read);
|
||||
lua_register ("write", io_write);
|
||||
lua_register ("execute", io_execute);
|
||||
lua_register ("remove", io_remove);
|
||||
}
|
||||
596
lapi.c
596
lapi.c
@@ -1,596 +0,0 @@
|
||||
/*
|
||||
** $Id: lapi.c,v 1.18 1998/01/07 16:26:48 roberto Exp roberto $
|
||||
** Lua API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lapi.h"
|
||||
#include "lauxlib.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lgc.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
#include "lvm.h"
|
||||
|
||||
|
||||
char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
|
||||
"$Autores: " LUA_AUTHORS " $";
|
||||
|
||||
|
||||
|
||||
TObject *luaA_Address (lua_Object o)
|
||||
{
|
||||
return Address(o);
|
||||
}
|
||||
|
||||
|
||||
static int normalized_type (TObject *o)
|
||||
{
|
||||
int t = ttype(o);
|
||||
switch (t) {
|
||||
case LUA_T_PMARK:
|
||||
return LUA_T_PROTO;
|
||||
case LUA_T_CMARK:
|
||||
return LUA_T_CPROTO;
|
||||
case LUA_T_CLMARK:
|
||||
return LUA_T_CLOSURE;
|
||||
default:
|
||||
return t;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void set_normalized (TObject *d, TObject *s)
|
||||
{
|
||||
d->value = s->value;
|
||||
d->ttype = normalized_type(s);
|
||||
}
|
||||
|
||||
|
||||
static TObject *luaA_protovalue (TObject *o)
|
||||
{
|
||||
return (normalized_type(o) == LUA_T_CLOSURE) ? protovalue(o) : o;
|
||||
}
|
||||
|
||||
|
||||
void luaA_packresults (void)
|
||||
{
|
||||
luaV_pack(L->Cstack.lua2C, L->Cstack.num, L->stack.top);
|
||||
incr_top;
|
||||
}
|
||||
|
||||
|
||||
int luaA_passresults (void)
|
||||
{
|
||||
luaD_checkstack(L->Cstack.num);
|
||||
memcpy(L->stack.top, L->Cstack.lua2C+L->stack.stack,
|
||||
L->Cstack.num*sizeof(TObject));
|
||||
L->stack.top += L->Cstack.num;
|
||||
return L->Cstack.num;
|
||||
}
|
||||
|
||||
|
||||
static void checkCparams (int nParams)
|
||||
{
|
||||
if (L->stack.top-L->stack.stack < L->Cstack.base+nParams)
|
||||
lua_error("API error - wrong number of arguments in C2lua stack");
|
||||
}
|
||||
|
||||
|
||||
static lua_Object put_luaObject (TObject *o)
|
||||
{
|
||||
luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base);
|
||||
L->stack.stack[L->Cstack.base++] = *o;
|
||||
return L->Cstack.base; /* this is +1 real position (see Ref) */
|
||||
}
|
||||
|
||||
|
||||
static lua_Object put_luaObjectonTop (void)
|
||||
{
|
||||
luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base);
|
||||
L->stack.stack[L->Cstack.base++] = *(--L->stack.top);
|
||||
return L->Cstack.base; /* this is +1 real position (see Ref) */
|
||||
}
|
||||
|
||||
|
||||
lua_Object lua_pop (void)
|
||||
{
|
||||
checkCparams(1);
|
||||
return put_luaObjectonTop();
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Get a parameter, returning the object handle or LUA_NOOBJECT on error.
|
||||
** 'number' must be 1 to get the first parameter.
|
||||
*/
|
||||
lua_Object lua_lua2C (int number)
|
||||
{
|
||||
if (number <= 0 || number > L->Cstack.num) return LUA_NOOBJECT;
|
||||
/* Ref(L->stack.stack+(L->Cstack.lua2C+number-1)) ==
|
||||
L->stack.stack+(L->Cstack.lua2C+number-1)-L->stack.stack+1 == */
|
||||
return L->Cstack.lua2C+number;
|
||||
}
|
||||
|
||||
|
||||
int lua_callfunction (lua_Object function)
|
||||
{
|
||||
if (function == LUA_NOOBJECT)
|
||||
return 1;
|
||||
else {
|
||||
luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base);
|
||||
set_normalized(L->stack.stack+L->Cstack.base, Address(function));
|
||||
return luaD_protectedrun(MULT_RET);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
lua_Object lua_gettagmethod (int tag, char *event)
|
||||
{
|
||||
return put_luaObject(luaT_gettagmethod(tag, event));
|
||||
}
|
||||
|
||||
|
||||
lua_Object lua_settagmethod (int tag, char *event)
|
||||
{
|
||||
checkCparams(1);
|
||||
luaT_settagmethod(tag, event, L->stack.top-1);
|
||||
return put_luaObjectonTop();
|
||||
}
|
||||
|
||||
|
||||
lua_Object lua_seterrormethod (void)
|
||||
{
|
||||
TObject temp = L->errorim;
|
||||
checkCparams(1);
|
||||
L->errorim = *(--L->stack.top);
|
||||
return put_luaObject(&temp);
|
||||
}
|
||||
|
||||
|
||||
lua_Object lua_gettable (void)
|
||||
{
|
||||
checkCparams(2);
|
||||
luaV_gettable();
|
||||
return put_luaObjectonTop();
|
||||
}
|
||||
|
||||
|
||||
lua_Object lua_rawgettable (void)
|
||||
{
|
||||
checkCparams(2);
|
||||
if (ttype(L->stack.top-2) != LUA_T_ARRAY)
|
||||
lua_error("indexed expression not a table in rawgettable");
|
||||
else {
|
||||
TObject *h = luaH_get(avalue(L->stack.top-2), L->stack.top-1);
|
||||
--L->stack.top;
|
||||
if (h != NULL)
|
||||
*(L->stack.top-1) = *h;
|
||||
else
|
||||
ttype(L->stack.top-1) = LUA_T_NIL;
|
||||
}
|
||||
return put_luaObjectonTop();
|
||||
}
|
||||
|
||||
|
||||
void lua_settable (void)
|
||||
{
|
||||
checkCparams(3);
|
||||
luaV_settable(L->stack.top-3, 1);
|
||||
}
|
||||
|
||||
|
||||
void lua_rawsettable (void)
|
||||
{
|
||||
checkCparams(3);
|
||||
luaV_settable(L->stack.top-3, 0);
|
||||
}
|
||||
|
||||
|
||||
lua_Object lua_createtable (void)
|
||||
{
|
||||
TObject o;
|
||||
luaC_checkGC();
|
||||
avalue(&o) = luaH_new(0);
|
||||
ttype(&o) = LUA_T_ARRAY;
|
||||
return put_luaObject(&o);
|
||||
}
|
||||
|
||||
|
||||
lua_Object lua_getglobal (char *name)
|
||||
{
|
||||
luaD_checkstack(2); /* may need that to call T.M. */
|
||||
luaV_getglobal(luaS_new(name));
|
||||
return put_luaObjectonTop();
|
||||
}
|
||||
|
||||
|
||||
lua_Object lua_rawgetglobal (char *name)
|
||||
{
|
||||
TaggedString *ts = luaS_new(name);
|
||||
return put_luaObject(&ts->u.globalval);
|
||||
}
|
||||
|
||||
|
||||
void lua_setglobal (char *name)
|
||||
{
|
||||
checkCparams(1);
|
||||
luaD_checkstack(2); /* may need that to call T.M. */
|
||||
luaV_setglobal(luaS_new(name));
|
||||
}
|
||||
|
||||
|
||||
void lua_rawsetglobal (char *name)
|
||||
{
|
||||
TaggedString *ts = luaS_new(name);
|
||||
checkCparams(1);
|
||||
luaS_rawsetglobal(ts, --L->stack.top);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int lua_isnil (lua_Object o)
|
||||
{
|
||||
return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_NIL);
|
||||
}
|
||||
|
||||
int lua_istable (lua_Object o)
|
||||
{
|
||||
return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_ARRAY);
|
||||
}
|
||||
|
||||
int lua_isuserdata (lua_Object o)
|
||||
{
|
||||
return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_USERDATA);
|
||||
}
|
||||
|
||||
int lua_iscfunction (lua_Object o)
|
||||
{
|
||||
return (lua_tag(o) == LUA_T_CPROTO);
|
||||
}
|
||||
|
||||
int lua_isnumber (lua_Object o)
|
||||
{
|
||||
return (o!= LUA_NOOBJECT) && (tonumber(Address(o)) == 0);
|
||||
}
|
||||
|
||||
int lua_isstring (lua_Object o)
|
||||
{
|
||||
int t = lua_tag(o);
|
||||
return (t == LUA_T_STRING) || (t == LUA_T_NUMBER);
|
||||
}
|
||||
|
||||
int lua_isfunction (lua_Object o)
|
||||
{
|
||||
int t = lua_tag(o);
|
||||
return (t == LUA_T_PROTO) || (t == LUA_T_CPROTO);
|
||||
}
|
||||
|
||||
|
||||
double lua_getnumber (lua_Object object)
|
||||
{
|
||||
if (object == LUA_NOOBJECT) return 0.0;
|
||||
if (tonumber(Address(object))) return 0.0;
|
||||
else return (nvalue(Address(object)));
|
||||
}
|
||||
|
||||
char *lua_getstring (lua_Object object)
|
||||
{
|
||||
if (object == LUA_NOOBJECT || tostring(Address(object)))
|
||||
return NULL;
|
||||
else return (svalue(Address(object)));
|
||||
}
|
||||
|
||||
void *lua_getuserdata (lua_Object object)
|
||||
{
|
||||
if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA)
|
||||
return NULL;
|
||||
else return tsvalue(Address(object))->u.d.v;
|
||||
}
|
||||
|
||||
lua_CFunction lua_getcfunction (lua_Object object)
|
||||
{
|
||||
if (!lua_iscfunction(object))
|
||||
return NULL;
|
||||
else return fvalue(luaA_protovalue(Address(object)));
|
||||
}
|
||||
|
||||
|
||||
void lua_pushnil (void)
|
||||
{
|
||||
ttype(L->stack.top) = LUA_T_NIL;
|
||||
incr_top;
|
||||
}
|
||||
|
||||
void lua_pushnumber (double n)
|
||||
{
|
||||
ttype(L->stack.top) = LUA_T_NUMBER;
|
||||
nvalue(L->stack.top) = n;
|
||||
incr_top;
|
||||
}
|
||||
|
||||
void lua_pushstring (char *s)
|
||||
{
|
||||
if (s == NULL)
|
||||
ttype(L->stack.top) = LUA_T_NIL;
|
||||
else {
|
||||
tsvalue(L->stack.top) = luaS_new(s);
|
||||
ttype(L->stack.top) = LUA_T_STRING;
|
||||
}
|
||||
incr_top;
|
||||
luaC_checkGC();
|
||||
}
|
||||
|
||||
void lua_pushCclosure (lua_CFunction fn, int n)
|
||||
{
|
||||
if (fn == NULL)
|
||||
lua_error("API error - attempt to push a NULL Cfunction");
|
||||
checkCparams(n);
|
||||
ttype(L->stack.top) = LUA_T_CPROTO;
|
||||
fvalue(L->stack.top) = fn;
|
||||
incr_top;
|
||||
luaV_closure(n);
|
||||
}
|
||||
|
||||
void lua_pushusertag (void *u, int tag)
|
||||
{
|
||||
if (tag < 0 && tag != LUA_ANYTAG)
|
||||
luaT_realtag(tag); /* error if tag is not valid */
|
||||
tsvalue(L->stack.top) = luaS_createudata(u, tag);
|
||||
ttype(L->stack.top) = LUA_T_USERDATA;
|
||||
incr_top;
|
||||
luaC_checkGC();
|
||||
}
|
||||
|
||||
void luaA_pushobject (TObject *o)
|
||||
{
|
||||
*L->stack.top = *o;
|
||||
incr_top;
|
||||
}
|
||||
|
||||
void lua_pushobject (lua_Object o)
|
||||
{
|
||||
if (o == LUA_NOOBJECT)
|
||||
lua_error("API error - attempt to push a NOOBJECT");
|
||||
else {
|
||||
set_normalized(L->stack.top, Address(o));
|
||||
incr_top;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int lua_tag (lua_Object lo)
|
||||
{
|
||||
if (lo == LUA_NOOBJECT)
|
||||
return LUA_T_NIL;
|
||||
else {
|
||||
TObject *o = Address(lo);
|
||||
int t;
|
||||
switch (t = ttype(o)) {
|
||||
case LUA_T_USERDATA:
|
||||
return o->value.ts->u.d.tag;
|
||||
case LUA_T_ARRAY:
|
||||
return o->value.a->htag;
|
||||
case LUA_T_PMARK:
|
||||
return LUA_T_PROTO;
|
||||
case LUA_T_CMARK:
|
||||
return LUA_T_CPROTO;
|
||||
case LUA_T_CLOSURE: case LUA_T_CLMARK:
|
||||
return o->value.cl->consts[0].ttype;
|
||||
#ifdef DEBUG
|
||||
case LUA_T_LINE:
|
||||
lua_error("internal error");
|
||||
#endif
|
||||
default:
|
||||
return t;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void lua_settag (int tag)
|
||||
{
|
||||
checkCparams(1);
|
||||
luaT_realtag(tag);
|
||||
switch (ttype(L->stack.top-1)) {
|
||||
case LUA_T_ARRAY:
|
||||
(L->stack.top-1)->value.a->htag = tag;
|
||||
break;
|
||||
case LUA_T_USERDATA:
|
||||
(L->stack.top-1)->value.ts->u.d.tag = tag;
|
||||
break;
|
||||
default:
|
||||
luaL_verror("cannot change the tag of a %.20s",
|
||||
luaO_typenames[-ttype((L->stack.top-1))]);
|
||||
}
|
||||
L->stack.top--;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** =======================================================
|
||||
** Debug interface
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
/* Hooks */
|
||||
lua_CHFunction lua_callhook = NULL;
|
||||
lua_LHFunction lua_linehook = NULL;
|
||||
|
||||
|
||||
lua_Function lua_stackedfunction (int level)
|
||||
{
|
||||
StkId i;
|
||||
for (i = (L->stack.top-1)-L->stack.stack; i>=0; i--) {
|
||||
int t = L->stack.stack[i].ttype;
|
||||
if (t == LUA_T_CLMARK || t == LUA_T_PMARK || t == LUA_T_CMARK)
|
||||
if (level-- == 0)
|
||||
return Ref(L->stack.stack+i);
|
||||
}
|
||||
return LUA_NOOBJECT;
|
||||
}
|
||||
|
||||
|
||||
int lua_currentline (lua_Function func)
|
||||
{
|
||||
TObject *f = Address(func);
|
||||
return (f+1 < L->stack.top && (f+1)->ttype == LUA_T_LINE) ?
|
||||
(f+1)->value.i : -1;
|
||||
}
|
||||
|
||||
|
||||
lua_Object lua_getlocal (lua_Function func, int local_number, char **name)
|
||||
{
|
||||
/* check whether func is a Lua function */
|
||||
if (lua_tag(func) != LUA_T_PROTO)
|
||||
return LUA_NOOBJECT;
|
||||
else {
|
||||
TObject *f = Address(func);
|
||||
TProtoFunc *fp = luaA_protovalue(f)->value.tf;
|
||||
*name = luaF_getlocalname(fp, 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)
|
||||
{
|
||||
/* check whether func is a Lua function */
|
||||
if (lua_tag(func) != LUA_T_PROTO)
|
||||
return 0;
|
||||
else {
|
||||
TObject *f = Address(func);
|
||||
TProtoFunc *fp = luaA_protovalue(f)->value.tf;
|
||||
char *name = luaF_getlocalname(fp, local_number, lua_currentline(func));
|
||||
checkCparams(1);
|
||||
--L->stack.top;
|
||||
if (name) {
|
||||
/* if "name", there must be a LUA_T_LINE */
|
||||
/* therefore, f+2 points to function base */
|
||||
*((f+2)+(local_number-1)) = *L->stack.top;
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void lua_funcinfo (lua_Object func, char **filename, int *linedefined)
|
||||
{
|
||||
if (!lua_isfunction(func))
|
||||
lua_error("API - `funcinfo' called with a non-function value");
|
||||
else {
|
||||
TObject *f = luaA_protovalue(Address(func));
|
||||
if (normalized_type(f) == LUA_T_PROTO) {
|
||||
*filename = tfvalue(f)->fileName->str;
|
||||
*linedefined = tfvalue(f)->lineDefined;
|
||||
}
|
||||
else {
|
||||
*filename = "(C)";
|
||||
*linedefined = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int checkfunc (TObject *o)
|
||||
{
|
||||
return luaO_equalObj(o, L->stack.top);
|
||||
}
|
||||
|
||||
|
||||
char *lua_getobjname (lua_Object o, char **name)
|
||||
{ /* try to find a name for given function */
|
||||
set_normalized(L->stack.top, Address(o)); /* to be accessed by "checkfunc */
|
||||
if ((*name = luaT_travtagmethods(checkfunc)) != NULL)
|
||||
return "tag-method";
|
||||
else if ((*name = luaS_travsymbol(checkfunc)) != NULL)
|
||||
return "global";
|
||||
else return "";
|
||||
}
|
||||
|
||||
/*
|
||||
** =======================================================
|
||||
** BLOCK mechanism
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
void lua_beginblock (void)
|
||||
{
|
||||
if (L->numCblocks >= MAX_C_BLOCKS)
|
||||
lua_error("too many nested blocks");
|
||||
L->Cblocks[L->numCblocks] = L->Cstack;
|
||||
L->numCblocks++;
|
||||
}
|
||||
|
||||
void lua_endblock (void)
|
||||
{
|
||||
--L->numCblocks;
|
||||
L->Cstack = L->Cblocks[L->numCblocks];
|
||||
luaD_adjusttop(L->Cstack.base);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int lua_ref (int lock)
|
||||
{
|
||||
int ref;
|
||||
checkCparams(1);
|
||||
ref = luaC_ref(L->stack.top-1, lock);
|
||||
L->stack.top--;
|
||||
return ref;
|
||||
}
|
||||
|
||||
|
||||
|
||||
lua_Object lua_getref (int ref)
|
||||
{
|
||||
TObject *o = luaC_getref(ref);
|
||||
return (o ? put_luaObject(o) : LUA_NOOBJECT);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef LUA_COMPAT2_5
|
||||
/*
|
||||
** API: set a function as a fallback
|
||||
*/
|
||||
|
||||
static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults)
|
||||
{
|
||||
StkId base = (L->stack.top-L->stack.stack)-nParams;
|
||||
luaD_openstack(nParams);
|
||||
L->stack.stack[base].ttype = LUA_T_CPROTO;
|
||||
L->stack.stack[base].value.f = f;
|
||||
luaD_call(base+1, nResults);
|
||||
}
|
||||
|
||||
lua_Object lua_setfallback (char *name, lua_CFunction fallback)
|
||||
{
|
||||
lua_pushstring(name);
|
||||
lua_pushcfunction(fallback);
|
||||
do_unprotectedrun(luaT_setfallback, 2, 1);
|
||||
return put_luaObjectonTop();
|
||||
}
|
||||
#endif
|
||||
|
||||
20
lapi.h
20
lapi.h
@@ -1,20 +0,0 @@
|
||||
/*
|
||||
** $Id: $
|
||||
** auxiliar functions from Lua API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lapi_h
|
||||
#define lapi_h
|
||||
|
||||
|
||||
#include "lua.h"
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
TObject *luaA_Address (lua_Object o);
|
||||
void luaA_pushobject (TObject *o);
|
||||
void luaA_packresults (void);
|
||||
int luaA_passresults (void);
|
||||
|
||||
#endif
|
||||
101
lauxlib.c
101
lauxlib.c
@@ -1,101 +0,0 @@
|
||||
/*
|
||||
** $Id: lauxlib.c,v 1.8 1998/01/09 15:06:07 roberto Exp $
|
||||
** Auxiliar functions for building Lua libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
/* Please Notice: This file uses only the oficial API of Lua
|
||||
** Any function declared here could be written as an application
|
||||
** function. With care, these functions can be used by other libraries.
|
||||
*/
|
||||
#include "lauxlib.h"
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
|
||||
|
||||
|
||||
void luaL_argerror (int numarg, char *extramsg)
|
||||
{
|
||||
char *funcname;
|
||||
lua_getobjname(lua_stackedfunction(0), &funcname);
|
||||
if (funcname == NULL)
|
||||
funcname = "???";
|
||||
if (extramsg == NULL)
|
||||
luaL_verror("bad argument #%d to function `%.50s'", numarg, funcname);
|
||||
else
|
||||
luaL_verror("bad argument #%d to function `%.50s' (%.100s)",
|
||||
numarg, funcname, extramsg);
|
||||
}
|
||||
|
||||
char *luaL_check_string (int numArg)
|
||||
{
|
||||
lua_Object o = lua_getparam(numArg);
|
||||
luaL_arg_check(lua_isstring(o), numArg, "string expected");
|
||||
return lua_getstring(o);
|
||||
}
|
||||
|
||||
char *luaL_opt_string (int numArg, char *def)
|
||||
{
|
||||
return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
|
||||
luaL_check_string(numArg);
|
||||
}
|
||||
|
||||
double luaL_check_number (int numArg)
|
||||
{
|
||||
lua_Object o = lua_getparam(numArg);
|
||||
luaL_arg_check(lua_isnumber(o), numArg, "number expected");
|
||||
return lua_getnumber(o);
|
||||
}
|
||||
|
||||
|
||||
double luaL_opt_number (int numArg, double def)
|
||||
{
|
||||
return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
|
||||
luaL_check_number(numArg);
|
||||
}
|
||||
|
||||
|
||||
lua_Object luaL_tablearg (int arg)
|
||||
{
|
||||
lua_Object o = lua_getparam(arg);
|
||||
luaL_arg_check(lua_istable(o), arg, "table expected");
|
||||
return o;
|
||||
}
|
||||
|
||||
lua_Object luaL_functionarg (int arg)
|
||||
{
|
||||
lua_Object o = lua_getparam(arg);
|
||||
luaL_arg_check(lua_isfunction(o), arg, "function expected");
|
||||
return o;
|
||||
}
|
||||
|
||||
lua_Object luaL_nonnullarg (int numArg)
|
||||
{
|
||||
lua_Object o = lua_getparam(numArg);
|
||||
luaL_arg_check(o != LUA_NOOBJECT, numArg, "value expected");
|
||||
return o;
|
||||
}
|
||||
|
||||
void luaL_openlib (struct luaL_reg *l, int n)
|
||||
{
|
||||
int i;
|
||||
lua_open(); /* make sure lua is already open */
|
||||
for (i=0; i<n; i++)
|
||||
lua_register(l[i].name, l[i].func);
|
||||
}
|
||||
|
||||
|
||||
void luaL_verror (char *fmt, ...)
|
||||
{
|
||||
char buff[500];
|
||||
va_list argp;
|
||||
va_start(argp, fmt);
|
||||
vsprintf(buff, fmt, argp);
|
||||
va_end(argp);
|
||||
lua_error(buff);
|
||||
}
|
||||
|
||||
43
lauxlib.h
43
lauxlib.h
@@ -1,43 +0,0 @@
|
||||
/*
|
||||
** $Id: lauxlib.h,v 1.5 1997/12/17 20:48:58 roberto Exp roberto $
|
||||
** Auxiliar functions for building Lua libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#ifndef auxlib_h
|
||||
#define auxlib_h
|
||||
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
struct luaL_reg {
|
||||
char *name;
|
||||
lua_CFunction func;
|
||||
};
|
||||
|
||||
|
||||
#define luaL_arg_check(cond,numarg,extramsg) if (!(cond)) \
|
||||
luaL_argerror(numarg,extramsg)
|
||||
|
||||
void luaL_openlib (struct luaL_reg *l, int n);
|
||||
void luaL_argerror (int numarg, char *extramsg);
|
||||
char *luaL_check_string (int numArg);
|
||||
char *luaL_opt_string (int numArg, char *def);
|
||||
double luaL_check_number (int numArg);
|
||||
double luaL_opt_number (int numArg, double def);
|
||||
lua_Object luaL_functionarg (int arg);
|
||||
lua_Object luaL_tablearg (int arg);
|
||||
lua_Object luaL_nonnullarg (int numArg);
|
||||
void luaL_verror (char *fmt, ...);
|
||||
char *luaL_openspace (int size);
|
||||
void luaL_resetbuffer (void);
|
||||
void luaL_addchar (int c);
|
||||
void luaL_addsize (int n);
|
||||
int luaL_newbuffer (int size);
|
||||
void luaL_oldbuffer (int old);
|
||||
char *luaL_buffer (void);
|
||||
|
||||
|
||||
#endif
|
||||
81
lbuffer.c
81
lbuffer.c
@@ -1,81 +0,0 @@
|
||||
/*
|
||||
** $Id: $
|
||||
** Auxiliar functions for building Lua libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lmem.h"
|
||||
#include "lstate.h"
|
||||
|
||||
|
||||
/*-------------------------------------------------------
|
||||
** Auxiliar buffer
|
||||
-------------------------------------------------------*/
|
||||
|
||||
#define BUFF_STEP 32
|
||||
|
||||
#define openspace(size) if (L->Mbuffnext+(size) > L->Mbuffsize) Openspace(size)
|
||||
|
||||
static void Openspace (int size)
|
||||
{
|
||||
LState *l = L; /* to optimize */
|
||||
int base = l->Mbuffbase-l->Mbuffer;
|
||||
l->Mbuffsize *= 2;
|
||||
if (l->Mbuffnext+size > l->Mbuffsize) /* still not big enough? */
|
||||
l->Mbuffsize = l->Mbuffnext+size;
|
||||
l->Mbuffer = luaM_realloc(l->Mbuffer, l->Mbuffsize);
|
||||
l->Mbuffbase = l->Mbuffer+base;
|
||||
}
|
||||
|
||||
|
||||
char *luaL_openspace (int size)
|
||||
{
|
||||
openspace(size);
|
||||
return L->Mbuffer+L->Mbuffnext;
|
||||
}
|
||||
|
||||
|
||||
void luaL_addchar (int c)
|
||||
{
|
||||
openspace(BUFF_STEP);
|
||||
L->Mbuffer[L->Mbuffnext++] = c;
|
||||
}
|
||||
|
||||
|
||||
void luaL_resetbuffer (void)
|
||||
{
|
||||
L->Mbuffnext = L->Mbuffbase-L->Mbuffer;
|
||||
}
|
||||
|
||||
|
||||
void luaL_addsize (int n)
|
||||
{
|
||||
L->Mbuffnext += n;
|
||||
}
|
||||
|
||||
|
||||
int luaL_newbuffer (int size)
|
||||
{
|
||||
int old = L->Mbuffbase-L->Mbuffer;
|
||||
openspace(size);
|
||||
L->Mbuffbase = L->Mbuffer+L->Mbuffnext;
|
||||
return old;
|
||||
}
|
||||
|
||||
|
||||
void luaL_oldbuffer (int old)
|
||||
{
|
||||
L->Mbuffnext = L->Mbuffbase-L->Mbuffer;
|
||||
L->Mbuffbase = L->Mbuffer+old;
|
||||
}
|
||||
|
||||
|
||||
char *luaL_buffer (void)
|
||||
{
|
||||
return L->Mbuffbase;
|
||||
}
|
||||
|
||||
516
lbuiltin.c
516
lbuiltin.c
@@ -1,516 +0,0 @@
|
||||
/*
|
||||
** $Id: lbuiltin.c,v 1.21 1998/01/02 17:46:32 roberto Exp roberto $
|
||||
** Built-in functions
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lapi.h"
|
||||
#include "lauxlib.h"
|
||||
#include "lbuiltin.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
|
||||
static void pushstring (TaggedString *s)
|
||||
{
|
||||
TObject o;
|
||||
o.ttype = LUA_T_STRING;
|
||||
o.value.ts = s;
|
||||
luaA_pushobject(&o);
|
||||
}
|
||||
|
||||
|
||||
static void nextvar (void)
|
||||
{
|
||||
TObject *o = luaA_Address(luaL_nonnullarg(1));
|
||||
TaggedString *g;
|
||||
if (ttype(o) == LUA_T_NIL)
|
||||
g = (TaggedString *)L->rootglobal.next;
|
||||
else {
|
||||
luaL_arg_check(ttype(o) == LUA_T_STRING, 1, "variable name expected");
|
||||
g = tsvalue(o);
|
||||
/* check whether name is in global var list */
|
||||
luaL_arg_check((GCnode *)g != g->head.next, 1, "variable name expected");
|
||||
g = (TaggedString *)g->head.next;
|
||||
}
|
||||
while (g && g->u.globalval.ttype == LUA_T_NIL) /* skip globals with nil */
|
||||
g = (TaggedString *)g->head.next;
|
||||
if (g) {
|
||||
pushstring(g);
|
||||
luaA_pushobject(&g->u.globalval);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void foreachvar (void)
|
||||
{
|
||||
TObject f = *luaA_Address(luaL_functionarg(1));
|
||||
GCnode *g;
|
||||
StkId name = L->Cstack.base++; /* place to keep var name (to avoid GC) */
|
||||
ttype(L->stack.stack+name) = LUA_T_NIL;
|
||||
L->stack.top++;
|
||||
for (g = L->rootglobal.next; g; g = g->next) {
|
||||
TaggedString *s = (TaggedString *)g;
|
||||
if (s->u.globalval.ttype != LUA_T_NIL) {
|
||||
ttype(L->stack.stack+name) = LUA_T_STRING;
|
||||
tsvalue(L->stack.stack+name) = s; /* keep s on stack to avoid GC */
|
||||
luaA_pushobject(&f);
|
||||
pushstring(s);
|
||||
luaA_pushobject(&s->u.globalval);
|
||||
luaD_call((L->stack.top-L->stack.stack)-2, 1);
|
||||
if (ttype(L->stack.top-1) != LUA_T_NIL)
|
||||
return;
|
||||
L->stack.top--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void next (void)
|
||||
{
|
||||
lua_Object o = luaL_tablearg(1);
|
||||
lua_Object r = luaL_nonnullarg(2);
|
||||
Node *n = luaH_next(luaA_Address(o), luaA_Address(r));
|
||||
if (n) {
|
||||
luaA_pushobject(&n->ref);
|
||||
luaA_pushobject(&n->val);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void foreach (void)
|
||||
{
|
||||
TObject t = *luaA_Address(luaL_tablearg(1));
|
||||
TObject f = *luaA_Address(luaL_functionarg(2));
|
||||
int i;
|
||||
for (i=0; i<avalue(&t)->nhash; i++) {
|
||||
Node *nd = &(avalue(&t)->node[i]);
|
||||
if (ttype(ref(nd)) != LUA_T_NIL && ttype(val(nd)) != LUA_T_NIL) {
|
||||
luaA_pushobject(&f);
|
||||
luaA_pushobject(ref(nd));
|
||||
luaA_pushobject(val(nd));
|
||||
luaD_call((L->stack.top-L->stack.stack)-2, 1);
|
||||
if (ttype(L->stack.top-1) != LUA_T_NIL)
|
||||
return;
|
||||
L->stack.top--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void internaldostring (void)
|
||||
{
|
||||
if (lua_getparam(2) != LUA_NOOBJECT)
|
||||
lua_error("invalid 2nd argument (probably obsolete code)");
|
||||
if (lua_dostring(luaL_check_string(1)) == 0)
|
||||
if (luaA_passresults() == 0)
|
||||
lua_pushuserdata(NULL); /* at least one result to signal no errors */
|
||||
}
|
||||
|
||||
|
||||
static void internaldofile (void)
|
||||
{
|
||||
char *fname = luaL_opt_string(1, NULL);
|
||||
if (lua_dofile(fname) == 0)
|
||||
if (luaA_passresults() == 0)
|
||||
lua_pushuserdata(NULL); /* at least one result to signal no errors */
|
||||
}
|
||||
|
||||
|
||||
static char *to_string (lua_Object obj)
|
||||
{
|
||||
char *buff = luaL_openspace(30);
|
||||
TObject *o = luaA_Address(obj);
|
||||
switch (ttype(o)) {
|
||||
case LUA_T_NUMBER: case LUA_T_STRING:
|
||||
return lua_getstring(obj);
|
||||
case LUA_T_ARRAY: {
|
||||
sprintf(buff, "table: %p", (void *)o->value.a);
|
||||
return buff;
|
||||
}
|
||||
case LUA_T_CLOSURE: {
|
||||
sprintf(buff, "function: %p", (void *)o->value.cl);
|
||||
return buff;
|
||||
}
|
||||
case LUA_T_PROTO: {
|
||||
sprintf(buff, "function: %p", (void *)o->value.tf);
|
||||
return buff;
|
||||
}
|
||||
case LUA_T_CPROTO: {
|
||||
sprintf(buff, "function: %p", (void *)o->value.f);
|
||||
return buff;
|
||||
}
|
||||
case LUA_T_USERDATA: {
|
||||
sprintf(buff, "userdata: %p", o->value.ts->u.d.v);
|
||||
return buff;
|
||||
}
|
||||
case LUA_T_NIL:
|
||||
return "nil";
|
||||
default:
|
||||
lua_error("internal error");
|
||||
return NULL; /* to avoid warnings */
|
||||
}
|
||||
}
|
||||
|
||||
static void bi_tostring (void)
|
||||
{
|
||||
lua_pushstring(to_string(lua_getparam(1)));
|
||||
}
|
||||
|
||||
|
||||
static void luaI_print (void)
|
||||
{
|
||||
int i = 1;
|
||||
lua_Object obj;
|
||||
while ((obj = lua_getparam(i++)) != LUA_NOOBJECT)
|
||||
printf("%s\t", to_string(obj));
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
static void luaI_type (void)
|
||||
{
|
||||
lua_Object o = luaL_nonnullarg(1);
|
||||
lua_pushstring(luaO_typenames[-ttype(luaA_Address(o))]);
|
||||
lua_pushnumber(lua_tag(o));
|
||||
}
|
||||
|
||||
|
||||
static void tonumber (void)
|
||||
{
|
||||
int base = luaL_opt_number(2, 10);
|
||||
if (base == 10) { /* standard convertion */
|
||||
lua_Object o = lua_getparam(1);
|
||||
if (lua_isnumber(o))
|
||||
lua_pushnumber(lua_getnumber(o));
|
||||
}
|
||||
else {
|
||||
char *s = luaL_check_string(1);
|
||||
unsigned long n;
|
||||
luaL_arg_check(0 <= base && base <= 36, 2, "base out of range");
|
||||
n = strtol(s, &s, base);
|
||||
while (isspace(*s)) s++; /* skip trailing spaces */
|
||||
if (*s) return; /* invalid format: return nil */
|
||||
lua_pushnumber(n);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void luaI_error (void)
|
||||
{
|
||||
lua_error(lua_getstring(lua_getparam(1)));
|
||||
}
|
||||
|
||||
|
||||
static void luaI_assert (void)
|
||||
{
|
||||
lua_Object p = lua_getparam(1);
|
||||
if (p == LUA_NOOBJECT || lua_isnil(p))
|
||||
luaL_verror("assertion failed! %.100s", luaL_opt_string(2, ""));
|
||||
}
|
||||
|
||||
|
||||
static void setglobal (void)
|
||||
{
|
||||
char *n = luaL_check_string(1);
|
||||
lua_Object value = luaL_nonnullarg(2);
|
||||
lua_pushobject(value);
|
||||
lua_setglobal(n);
|
||||
lua_pushobject(value); /* return given value */
|
||||
}
|
||||
|
||||
static void rawsetglobal (void)
|
||||
{
|
||||
char *n = luaL_check_string(1);
|
||||
lua_Object value = luaL_nonnullarg(2);
|
||||
lua_pushobject(value);
|
||||
lua_rawsetglobal(n);
|
||||
lua_pushobject(value); /* return given value */
|
||||
}
|
||||
|
||||
static void getglobal (void)
|
||||
{
|
||||
lua_pushobject(lua_getglobal(luaL_check_string(1)));
|
||||
}
|
||||
|
||||
static void rawgetglobal (void)
|
||||
{
|
||||
lua_pushobject(lua_rawgetglobal(luaL_check_string(1)));
|
||||
}
|
||||
|
||||
static void luatag (void)
|
||||
{
|
||||
lua_pushnumber(lua_tag(lua_getparam(1)));
|
||||
}
|
||||
|
||||
|
||||
static int getnarg (lua_Object table)
|
||||
{
|
||||
lua_Object temp;
|
||||
/* temp = table.n */
|
||||
lua_pushobject(table); lua_pushstring("n"); temp = lua_rawgettable();
|
||||
return (lua_isnumber(temp) ? lua_getnumber(temp) : MAX_WORD);
|
||||
}
|
||||
|
||||
static void luaI_call (void)
|
||||
{
|
||||
lua_Object f = luaL_nonnullarg(1);
|
||||
lua_Object arg = luaL_tablearg(2);
|
||||
char *options = luaL_opt_string(3, "");
|
||||
lua_Object err = lua_getparam(4);
|
||||
int narg = getnarg(arg);
|
||||
int i, status;
|
||||
if (err != LUA_NOOBJECT) { /* set new error method */
|
||||
lua_pushobject(err);
|
||||
err = lua_seterrormethod();
|
||||
}
|
||||
/* push arg[1...n] */
|
||||
for (i=0; i<narg; i++) {
|
||||
lua_Object temp;
|
||||
/* temp = arg[i+1] */
|
||||
lua_pushobject(arg); lua_pushnumber(i+1); temp = lua_rawgettable();
|
||||
if (narg == MAX_WORD && lua_isnil(temp))
|
||||
break;
|
||||
lua_pushobject(temp);
|
||||
}
|
||||
status = lua_callfunction(f);
|
||||
if (err != LUA_NOOBJECT) { /* restore old error method */
|
||||
lua_pushobject(err);
|
||||
lua_seterrormethod();
|
||||
}
|
||||
if (status != 0) { /* error in call? */
|
||||
if (strchr(options, 'x'))
|
||||
return; /* return nil to signal the error */
|
||||
else
|
||||
lua_error(NULL);
|
||||
}
|
||||
else { /* no errors */
|
||||
if (strchr(options, 'p'))
|
||||
luaA_packresults();
|
||||
else
|
||||
luaA_passresults();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void settag (void)
|
||||
{
|
||||
lua_Object o = luaL_tablearg(1);
|
||||
lua_pushobject(o);
|
||||
lua_settag(luaL_check_number(2));
|
||||
}
|
||||
|
||||
|
||||
static void newtag (void)
|
||||
{
|
||||
lua_pushnumber(lua_newtag());
|
||||
}
|
||||
|
||||
|
||||
static void copytagmethods (void)
|
||||
{
|
||||
lua_pushnumber(lua_copytagmethods(luaL_check_number(1),
|
||||
luaL_check_number(2)));
|
||||
}
|
||||
|
||||
|
||||
static void rawgettable (void)
|
||||
{
|
||||
lua_Object t = luaL_nonnullarg(1);
|
||||
lua_Object i = luaL_nonnullarg(2);
|
||||
lua_pushobject(t);
|
||||
lua_pushobject(i);
|
||||
lua_pushobject(lua_rawgettable());
|
||||
}
|
||||
|
||||
|
||||
static void rawsettable (void)
|
||||
{
|
||||
lua_Object t = luaL_nonnullarg(1);
|
||||
lua_Object i = luaL_nonnullarg(2);
|
||||
lua_Object v = luaL_nonnullarg(3);
|
||||
lua_pushobject(t);
|
||||
lua_pushobject(i);
|
||||
lua_pushobject(v);
|
||||
lua_rawsettable();
|
||||
}
|
||||
|
||||
|
||||
static void settagmethod (void)
|
||||
{
|
||||
lua_Object nf = luaL_nonnullarg(3);
|
||||
lua_pushobject(nf);
|
||||
lua_pushobject(lua_settagmethod((int)luaL_check_number(1),
|
||||
luaL_check_string(2)));
|
||||
}
|
||||
|
||||
|
||||
static void gettagmethod (void)
|
||||
{
|
||||
lua_pushobject(lua_gettagmethod((int)luaL_check_number(1),
|
||||
luaL_check_string(2)));
|
||||
}
|
||||
|
||||
|
||||
static void seterrormethod (void)
|
||||
{
|
||||
lua_Object nf = luaL_functionarg(1);
|
||||
lua_pushobject(nf);
|
||||
lua_pushobject(lua_seterrormethod());
|
||||
}
|
||||
|
||||
|
||||
static void luaI_collectgarbage (void)
|
||||
{
|
||||
lua_pushnumber(lua_collectgarbage(luaL_opt_number(1, 0)));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** =======================================================
|
||||
** some DEBUG functions
|
||||
** =======================================================
|
||||
*/
|
||||
#ifdef DEBUG
|
||||
|
||||
static void mem_query (void)
|
||||
{
|
||||
lua_pushnumber(totalmem);
|
||||
lua_pushnumber(numblocks);
|
||||
}
|
||||
|
||||
|
||||
static void countlist (void)
|
||||
{
|
||||
char *s = luaL_check_string(1);
|
||||
GCnode *l = (s[0]=='t') ? L->roottable.next : (s[0]=='c') ? L->rootcl.next :
|
||||
(s[0]=='p') ? L->rootproto.next : L->rootglobal.next;
|
||||
int i=0;
|
||||
while (l) {
|
||||
i++;
|
||||
l = l->next;
|
||||
}
|
||||
lua_pushnumber(i);
|
||||
}
|
||||
|
||||
|
||||
static void testC (void)
|
||||
{
|
||||
#define getnum(s) ((*s++) - '0')
|
||||
#define getname(s) (nome[0] = *s++, nome)
|
||||
|
||||
static int locks[10];
|
||||
lua_Object reg[10];
|
||||
char nome[2];
|
||||
char *s = luaL_check_string(1);
|
||||
nome[1] = 0;
|
||||
while (1) {
|
||||
switch (*s++) {
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9':
|
||||
lua_pushnumber(*(s-1) - '0');
|
||||
break;
|
||||
|
||||
case 'c': reg[getnum(s)] = lua_createtable(); break;
|
||||
case 'C': { lua_CFunction f = lua_getcfunction(lua_getglobal(getname(s)));
|
||||
lua_pushCclosure(f, getnum(s));
|
||||
break;
|
||||
}
|
||||
case 'P': reg[getnum(s)] = lua_pop(); break;
|
||||
case 'g': { int n=getnum(s); reg[n]=lua_getglobal(getname(s)); break; }
|
||||
case 'G': { int n = getnum(s);
|
||||
reg[n] = lua_rawgetglobal(getname(s));
|
||||
break;
|
||||
}
|
||||
case 'l': locks[getnum(s)] = lua_ref(1); break;
|
||||
case 'L': locks[getnum(s)] = lua_ref(0); break;
|
||||
case 'r': { int n=getnum(s); reg[n]=lua_getref(locks[getnum(s)]); break; }
|
||||
case 'u': lua_unref(locks[getnum(s)]); break;
|
||||
case 'p': { int n = getnum(s); reg[n] = lua_getparam(getnum(s)); break; }
|
||||
case '=': lua_setglobal(getname(s)); break;
|
||||
case 's': lua_pushstring(getname(s)); break;
|
||||
case 'o': lua_pushobject(reg[getnum(s)]); break;
|
||||
case 'f': lua_call(getname(s)); break;
|
||||
case 'i': reg[getnum(s)] = lua_gettable(); break;
|
||||
case 'I': reg[getnum(s)] = lua_rawgettable(); break;
|
||||
case 't': lua_settable(); break;
|
||||
case 'T': lua_rawsettable(); break;
|
||||
default: luaL_verror("unknown command in `testC': %c", *(s-1));
|
||||
}
|
||||
if (*s == 0) return;
|
||||
if (*s++ != ' ') lua_error("missing ` ' between commands in `testC'");
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** Internal functions
|
||||
*/
|
||||
static struct luaL_reg int_funcs[] = {
|
||||
#ifdef LUA_COMPAT2_5
|
||||
{"setfallback", luaT_setfallback},
|
||||
#endif
|
||||
#ifdef DEBUG
|
||||
{"testC", testC},
|
||||
{"totalmem", mem_query},
|
||||
{"count", countlist},
|
||||
#endif
|
||||
{"assert", luaI_assert},
|
||||
{"call", luaI_call},
|
||||
{"collectgarbage", luaI_collectgarbage},
|
||||
{"dofile", internaldofile},
|
||||
{"copytagmethods", copytagmethods},
|
||||
{"dostring", internaldostring},
|
||||
{"error", luaI_error},
|
||||
{"foreach", foreach},
|
||||
{"foreachvar", foreachvar},
|
||||
{"getglobal", getglobal},
|
||||
{"newtag", newtag},
|
||||
{"next", next},
|
||||
{"nextvar", nextvar},
|
||||
{"print", luaI_print},
|
||||
{"rawgetglobal", rawgetglobal},
|
||||
{"rawgettable", rawgettable},
|
||||
{"rawsetglobal", rawsetglobal},
|
||||
{"rawsettable", rawsettable},
|
||||
{"seterrormethod", seterrormethod},
|
||||
{"setglobal", setglobal},
|
||||
{"settagmethod", settagmethod},
|
||||
{"gettagmethod", gettagmethod},
|
||||
{"settag", settag},
|
||||
{"tonumber", tonumber},
|
||||
{"tostring", bi_tostring},
|
||||
{"tag", luatag},
|
||||
{"type", luaI_type}
|
||||
};
|
||||
|
||||
|
||||
#define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0]))
|
||||
|
||||
|
||||
void luaB_predefine (void)
|
||||
{
|
||||
/* pre-register mem error messages, to avoid loop when error arises */
|
||||
luaS_newfixedstring(tableEM);
|
||||
luaS_newfixedstring(memEM);
|
||||
luaL_openlib(int_funcs, (sizeof(int_funcs)/sizeof(int_funcs[0])));
|
||||
lua_pushstring(LUA_VERSION);
|
||||
lua_setglobal("_VERSION");
|
||||
}
|
||||
|
||||
14
lbuiltin.h
14
lbuiltin.h
@@ -1,14 +0,0 @@
|
||||
/*
|
||||
** $Id: $
|
||||
** Built-in functions
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lbuiltin_h
|
||||
#define lbuiltin_h
|
||||
|
||||
|
||||
void luaB_predefine (void);
|
||||
|
||||
|
||||
#endif
|
||||
420
ldo.c
420
ldo.c
@@ -1,420 +0,0 @@
|
||||
/*
|
||||
** $Id: ldo.c,v 1.20 1997/12/26 18:38:16 roberto Exp roberto $
|
||||
** Stack and Call structure of Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <setjmp.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lgc.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lparser.h"
|
||||
#include "lstate.h"
|
||||
#include "ltm.h"
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
#include "lundump.h"
|
||||
#include "lvm.h"
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
|
||||
#ifndef STACK_LIMIT
|
||||
#define STACK_LIMIT 6000
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Error messages
|
||||
*/
|
||||
|
||||
static void stderrorim (void)
|
||||
{
|
||||
fprintf(stderr, "lua error: %s\n", lua_getstring(lua_getparam(1)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
#define STACK_UNIT 128
|
||||
|
||||
|
||||
void luaD_init (void)
|
||||
{
|
||||
L->stack.stack = luaM_newvector(STACK_UNIT, TObject);
|
||||
L->stack.top = L->stack.stack;
|
||||
L->stack.last = L->stack.stack+(STACK_UNIT-1);
|
||||
ttype(&L->errorim) = LUA_T_CPROTO;
|
||||
fvalue(&L->errorim) = stderrorim;
|
||||
}
|
||||
|
||||
|
||||
void luaD_checkstack (int n)
|
||||
{
|
||||
struct Stack *S = &L->stack;
|
||||
if (S->last-S->top <= n) {
|
||||
StkId top = S->top-S->stack;
|
||||
int stacksize = (S->last-S->stack)+1+STACK_UNIT+n;
|
||||
S->stack = luaM_reallocvector(S->stack, stacksize, TObject);
|
||||
S->last = S->stack+(stacksize-1);
|
||||
S->top = S->stack + top;
|
||||
if (stacksize >= STACK_LIMIT) { /* stack overflow? */
|
||||
if (lua_stackedfunction(100) == LUA_NOOBJECT) /* 100 funcs on stack? */
|
||||
lua_error("Lua2C - C2Lua overflow"); /* doesn't look like a rec. loop */
|
||||
else
|
||||
lua_error("stack size overflow");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Adjust stack. Set top to the given value, pushing NILs if needed.
|
||||
*/
|
||||
void luaD_adjusttop (StkId newtop)
|
||||
{
|
||||
int diff = newtop-(L->stack.top-L->stack.stack);
|
||||
if (diff <= 0)
|
||||
L->stack.top += diff;
|
||||
else {
|
||||
luaD_checkstack(diff);
|
||||
while (diff--)
|
||||
ttype(L->stack.top++) = LUA_T_NIL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Open a hole below "nelems" from the L->stack.top.
|
||||
*/
|
||||
void luaD_openstack (int nelems)
|
||||
{
|
||||
luaO_memup(L->stack.top-nelems+1, L->stack.top-nelems,
|
||||
nelems*sizeof(TObject));
|
||||
incr_top;
|
||||
}
|
||||
|
||||
|
||||
void luaD_lineHook (int line)
|
||||
{
|
||||
struct C_Lua_Stack oldCLS = L->Cstack;
|
||||
StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->stack.top-L->stack.stack;
|
||||
L->Cstack.num = 0;
|
||||
(*lua_linehook)(line);
|
||||
L->stack.top = L->stack.stack+old_top;
|
||||
L->Cstack = oldCLS;
|
||||
}
|
||||
|
||||
|
||||
void luaD_callHook (StkId base, TProtoFunc *tf, int isreturn)
|
||||
{
|
||||
struct C_Lua_Stack oldCLS = L->Cstack;
|
||||
StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->stack.top-L->stack.stack;
|
||||
L->Cstack.num = 0;
|
||||
if (isreturn)
|
||||
(*lua_callhook)(LUA_NOOBJECT, "(return)", 0);
|
||||
else {
|
||||
TObject *f = L->stack.stack+base-1;
|
||||
if (tf)
|
||||
(*lua_callhook)(Ref(f), tf->fileName->str, tf->lineDefined);
|
||||
else
|
||||
(*lua_callhook)(Ref(f), "(C)", -1);
|
||||
}
|
||||
L->stack.top = L->stack.stack+old_top;
|
||||
L->Cstack = oldCLS;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Call a C function.
|
||||
** Cstack.num is the number of arguments; Cstack.lua2C points to the
|
||||
** first argument. Returns an index to the first result from C.
|
||||
*/
|
||||
static StkId callC (lua_CFunction f, StkId base)
|
||||
{
|
||||
struct C_Lua_Stack *CS = &L->Cstack;
|
||||
struct C_Lua_Stack oldCLS = *CS;
|
||||
StkId firstResult;
|
||||
int numarg = (L->stack.top-L->stack.stack) - base;
|
||||
CS->num = numarg;
|
||||
CS->lua2C = base;
|
||||
CS->base = base+numarg; /* == top-stack */
|
||||
if (lua_callhook)
|
||||
luaD_callHook(base, NULL, 0);
|
||||
(*f)(); /* do the actual call */
|
||||
if (lua_callhook) /* func may have changed lua_callhook */
|
||||
luaD_callHook(base, NULL, 1);
|
||||
firstResult = CS->base;
|
||||
*CS = oldCLS;
|
||||
return firstResult;
|
||||
}
|
||||
|
||||
|
||||
static StkId callCclosure (struct Closure *cl, lua_CFunction f, StkId base)
|
||||
{
|
||||
TObject *pbase;
|
||||
int nup = cl->nelems; /* number of upvalues */
|
||||
luaD_checkstack(nup);
|
||||
pbase = L->stack.stack+base; /* care: previous call may change this */
|
||||
/* open space for upvalues as extra arguments */
|
||||
luaO_memup(pbase+nup, pbase, (L->stack.top-pbase)*sizeof(TObject));
|
||||
/* copy upvalues into stack */
|
||||
memcpy(pbase, cl->consts+1, nup*sizeof(TObject));
|
||||
L->stack.top += nup;
|
||||
return callC(f, base);
|
||||
}
|
||||
|
||||
|
||||
void luaD_callTM (TObject *f, int nParams, int nResults)
|
||||
{
|
||||
luaD_openstack(nParams);
|
||||
*(L->stack.top-nParams-1) = *f;
|
||||
luaD_call((L->stack.top-L->stack.stack)-nParams, nResults);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Call a function (C or Lua). The parameters must be on the L->stack.stack,
|
||||
** between [L->stack.stack+base,L->stack.top). The function to be called is at L->stack.stack+base-1.
|
||||
** When returns, the results are on the L->stack.stack, between [L->stack.stack+base-1,L->stack.top).
|
||||
** The number of results is nResults, unless nResults=MULT_RET.
|
||||
*/
|
||||
void luaD_call (StkId base, int nResults)
|
||||
{
|
||||
StkId firstResult;
|
||||
TObject *func = L->stack.stack+base-1;
|
||||
int i;
|
||||
switch (ttype(func)) {
|
||||
case LUA_T_CPROTO:
|
||||
ttype(func) = LUA_T_CMARK;
|
||||
firstResult = callC(fvalue(func), base);
|
||||
break;
|
||||
case LUA_T_PROTO:
|
||||
ttype(func) = LUA_T_PMARK;
|
||||
firstResult = luaV_execute(NULL, tfvalue(func), base);
|
||||
break;
|
||||
case LUA_T_CLOSURE: {
|
||||
Closure *c = clvalue(func);
|
||||
TObject *proto = &(c->consts[0]);
|
||||
ttype(func) = LUA_T_CLMARK;
|
||||
firstResult = (ttype(proto) == LUA_T_CPROTO) ?
|
||||
callCclosure(c, fvalue(proto), base) :
|
||||
luaV_execute(c, tfvalue(proto), base);
|
||||
break;
|
||||
}
|
||||
default: { /* func is not a function */
|
||||
/* Check the tag method for invalid functions */
|
||||
TObject *im = luaT_getimbyObj(func, IM_FUNCTION);
|
||||
if (ttype(im) == LUA_T_NIL)
|
||||
lua_error("call expression not a function");
|
||||
luaD_callTM(im, (L->stack.top-L->stack.stack)-(base-1), nResults);
|
||||
return;
|
||||
}
|
||||
}
|
||||
/* adjust the number of results */
|
||||
if (nResults != MULT_RET)
|
||||
luaD_adjusttop(firstResult+nResults);
|
||||
/* move results to base-1 (to erase parameters and function) */
|
||||
base--;
|
||||
nResults = L->stack.top - (L->stack.stack+firstResult); /* actual number of results */
|
||||
for (i=0; i<nResults; i++)
|
||||
*(L->stack.stack+base+i) = *(L->stack.stack+firstResult+i);
|
||||
L->stack.top -= firstResult-base;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Traverse all objects on L->stack.stack
|
||||
*/
|
||||
void luaD_travstack (int (*fn)(TObject *))
|
||||
{
|
||||
StkId i;
|
||||
for (i = (L->stack.top-1)-L->stack.stack; i>=0; i--)
|
||||
fn(L->stack.stack+i);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void message (char *s)
|
||||
{
|
||||
TObject im = L->errorim;
|
||||
if (ttype(&im) != LUA_T_NIL) {
|
||||
lua_pushstring(s);
|
||||
luaD_callTM(&im, 1, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Reports an error, and jumps up to the available recover label
|
||||
*/
|
||||
void lua_error (char *s)
|
||||
{
|
||||
if (s) message(s);
|
||||
if (L->errorJmp)
|
||||
longjmp(*((jmp_buf *)L->errorJmp), 1);
|
||||
else {
|
||||
fprintf (stderr, "lua: exit(1). Unable to recover\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Call the function at L->Cstack.base, and incorporate results on
|
||||
** the Lua2C structure.
|
||||
*/
|
||||
static void do_callinc (int nResults)
|
||||
{
|
||||
StkId base = L->Cstack.base;
|
||||
luaD_call(base+1, nResults);
|
||||
L->Cstack.lua2C = base; /* position of the luaM_new results */
|
||||
L->Cstack.num = (L->stack.top-L->stack.stack) - base; /* number of results */
|
||||
L->Cstack.base = base + L->Cstack.num; /* incorporate results on L->stack.stack */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Execute a protected call. Assumes that function is at L->Cstack.base and
|
||||
** parameters are on top of it. Leave nResults on the stack.
|
||||
*/
|
||||
int luaD_protectedrun (int nResults)
|
||||
{
|
||||
jmp_buf myErrorJmp;
|
||||
int status;
|
||||
struct C_Lua_Stack oldCLS = L->Cstack;
|
||||
jmp_buf *oldErr = L->errorJmp;
|
||||
L->errorJmp = &myErrorJmp;
|
||||
if (setjmp(myErrorJmp) == 0) {
|
||||
do_callinc(nResults);
|
||||
status = 0;
|
||||
}
|
||||
else { /* an error occurred: restore L->Cstack and L->stack.top */
|
||||
L->Cstack = oldCLS;
|
||||
L->stack.top = L->stack.stack+L->Cstack.base;
|
||||
status = 1;
|
||||
}
|
||||
L->errorJmp = oldErr;
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** returns 0 = chunk loaded; 1 = error; 2 = no more chunks to load
|
||||
*/
|
||||
static int protectedparser (ZIO *z, int bin)
|
||||
{
|
||||
int status;
|
||||
TProtoFunc *tf;
|
||||
jmp_buf myErrorJmp;
|
||||
jmp_buf *oldErr = L->errorJmp;
|
||||
L->errorJmp = &myErrorJmp;
|
||||
if (setjmp(myErrorJmp) == 0) {
|
||||
tf = bin ? luaU_undump1(z) : luaY_parser(z);
|
||||
status = 0;
|
||||
}
|
||||
else {
|
||||
tf = NULL;
|
||||
status = 1;
|
||||
}
|
||||
L->errorJmp = oldErr;
|
||||
if (status) return 1; /* error code */
|
||||
if (tf == NULL) return 2; /* 'natural' end */
|
||||
luaD_adjusttop(L->Cstack.base+1); /* one slot for the pseudo-function */
|
||||
L->stack.stack[L->Cstack.base].ttype = LUA_T_PROTO;
|
||||
L->stack.stack[L->Cstack.base].value.tf = tf;
|
||||
luaV_closure(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int do_main (ZIO *z, int bin)
|
||||
{
|
||||
int status;
|
||||
do {
|
||||
long old_blocks = (luaC_checkGC(), L->nblocks);
|
||||
status = protectedparser(z, bin);
|
||||
if (status == 1) return 1; /* error */
|
||||
else if (status == 2) return 0; /* 'natural' end */
|
||||
else {
|
||||
unsigned long newelems2 = 2*(L->nblocks-old_blocks);
|
||||
L->GCthreshold += newelems2;
|
||||
status = luaD_protectedrun(MULT_RET);
|
||||
L->GCthreshold -= newelems2;
|
||||
}
|
||||
} while (bin && status == 0);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
void luaD_gcIM (TObject *o)
|
||||
{
|
||||
TObject *im = luaT_getimbyObj(o, IM_GC);
|
||||
if (ttype(im) != LUA_T_NIL) {
|
||||
*L->stack.top = *o;
|
||||
incr_top;
|
||||
luaD_callTM(im, 1, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int lua_dofile (char *filename)
|
||||
{
|
||||
ZIO z;
|
||||
int status;
|
||||
int c;
|
||||
int bin;
|
||||
FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
|
||||
if (f == NULL)
|
||||
return 2;
|
||||
if (filename == NULL)
|
||||
filename = "(stdin)";
|
||||
c = fgetc(f);
|
||||
ungetc(c, f);
|
||||
bin = (c == ID_CHUNK);
|
||||
if (bin)
|
||||
f = freopen(filename, "rb", f); /* set binary mode */
|
||||
luaZ_Fopen(&z, f, filename);
|
||||
status = do_main(&z, bin);
|
||||
if (f != stdin)
|
||||
fclose(f);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
#define SIZE_PREF 20 /* size of string prefix to appear in error messages */
|
||||
#define SSIZE_PREF "20"
|
||||
|
||||
|
||||
int lua_dostring (char *str)
|
||||
{
|
||||
int status;
|
||||
char name[SIZE_PREF+25];
|
||||
char *temp;
|
||||
ZIO z;
|
||||
if (str == NULL) return 1;
|
||||
sprintf(name, "(dostring) >> %." SSIZE_PREF "s", str);
|
||||
temp = strchr(name, '\n');
|
||||
if (temp) *temp = 0; /* end string after first line */
|
||||
luaZ_sopen(&z, str, name);
|
||||
status = do_main(&z, 0);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
#if 0
|
||||
int lua_dobuffer (char *buff, int size)
|
||||
{
|
||||
int status;
|
||||
ZIO z;
|
||||
luaZ_mopen(&z, buff, size, "(buffer)");
|
||||
status = do_main(&z, 1);
|
||||
return status;
|
||||
}
|
||||
#endif
|
||||
46
ldo.h
46
ldo.h
@@ -1,46 +0,0 @@
|
||||
/*
|
||||
** $Id: ldo.h,v 1.3 1997/11/19 17:29:23 roberto Exp roberto $
|
||||
** Stack and Call structure of Lua
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef ldo_h
|
||||
#define ldo_h
|
||||
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
|
||||
|
||||
#define MULT_RET 255
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** macro to increment stack top.
|
||||
** There must be always an empty slot at the L->stack.top
|
||||
*/
|
||||
#define incr_top { if (L->stack.top >= L->stack.last) luaD_checkstack(1); \
|
||||
L->stack.top++; }
|
||||
|
||||
|
||||
/* macros to convert from lua_Object to (TObject *) and back */
|
||||
|
||||
#define Address(lo) ((lo)+L->stack.stack-1)
|
||||
#define Ref(st) ((st)-L->stack.stack+1)
|
||||
|
||||
|
||||
void luaD_init (void);
|
||||
void luaD_adjusttop (StkId newtop);
|
||||
void luaD_openstack (int nelems);
|
||||
void luaD_lineHook (int line);
|
||||
void luaD_callHook (StkId base, TProtoFunc *tf, int isreturn);
|
||||
void luaD_call (StkId base, int nResults);
|
||||
void luaD_callTM (TObject *f, int nParams, int nResults);
|
||||
int luaD_protectedrun (int nResults);
|
||||
void luaD_gcIM (TObject *o);
|
||||
void luaD_travstack (int (*fn)(TObject *));
|
||||
void luaD_checkstack (int n);
|
||||
|
||||
|
||||
#endif
|
||||
923
lex_yy.c
Normal file
923
lex_yy.c
Normal file
@@ -0,0 +1,923 @@
|
||||
# include "stdio.h"
|
||||
# define U(x) x
|
||||
# define NLSTATE yyprevious=YYNEWLINE
|
||||
# define BEGIN yybgin = yysvec + 1 +
|
||||
# define INITIAL 0
|
||||
# define YYLERR yysvec
|
||||
# define YYSTATE (yyestate-yysvec-1)
|
||||
# define YYOPTIM 1
|
||||
# define YYLMAX BUFSIZ
|
||||
# define output(c) putc(c,yyout)
|
||||
# define input() (((yytchar=yysptr>yysbuf?U(*--yysptr):getc(yyin))==10?(yylineno++,yytchar):yytchar)==EOF?0:yytchar)
|
||||
# define unput(c) {yytchar= (c);if(yytchar=='\n')yylineno--;*yysptr++=yytchar;}
|
||||
# define yymore() (yymorfg=1)
|
||||
# define ECHO fprintf(yyout, "%s",yytext)
|
||||
# define REJECT { nstr = yyreject(); goto yyfussy;}
|
||||
int yyleng; extern char yytext[];
|
||||
int yymorfg;
|
||||
extern char *yysptr, yysbuf[];
|
||||
int yytchar;
|
||||
FILE *yyin = {stdin}, *yyout = {stdout};
|
||||
extern int yylineno;
|
||||
struct yysvf {
|
||||
struct yywork *yystoff;
|
||||
struct yysvf *yyother;
|
||||
int *yystops;};
|
||||
struct yysvf *yyestate;
|
||||
extern struct yysvf yysvec[], *yybgin;
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "opcode.h"
|
||||
#include "hash.h"
|
||||
#include "inout.h"
|
||||
#include "table.h"
|
||||
#include "y_tab.h"
|
||||
|
||||
#undef input
|
||||
#undef unput
|
||||
|
||||
static Input input;
|
||||
static Unput unput;
|
||||
|
||||
void lua_setinput (Input fn)
|
||||
{
|
||||
input = fn;
|
||||
}
|
||||
|
||||
void lua_setunput (Unput fn)
|
||||
{
|
||||
unput = fn;
|
||||
}
|
||||
|
||||
char *lua_lasttext (void)
|
||||
{
|
||||
return yytext;
|
||||
}
|
||||
|
||||
# define YYNEWLINE 10
|
||||
yylex(){
|
||||
int nstr; extern int yyprevious;
|
||||
while((nstr = yylook()) >= 0)
|
||||
yyfussy: switch(nstr){
|
||||
case 0:
|
||||
if(yywrap()) return(0); break;
|
||||
case 1:
|
||||
;
|
||||
break;
|
||||
case 2:
|
||||
{yylval.vInt = 1; return DEBUG;}
|
||||
break;
|
||||
case 3:
|
||||
{yylval.vInt = 0; return DEBUG;}
|
||||
break;
|
||||
case 4:
|
||||
lua_linenumber++;
|
||||
break;
|
||||
case 5:
|
||||
;
|
||||
break;
|
||||
case 6:
|
||||
return LOCAL;
|
||||
break;
|
||||
case 7:
|
||||
return IF;
|
||||
break;
|
||||
case 8:
|
||||
return THEN;
|
||||
break;
|
||||
case 9:
|
||||
return ELSE;
|
||||
break;
|
||||
case 10:
|
||||
return ELSEIF;
|
||||
break;
|
||||
case 11:
|
||||
return WHILE;
|
||||
break;
|
||||
case 12:
|
||||
return DO;
|
||||
break;
|
||||
case 13:
|
||||
return REPEAT;
|
||||
break;
|
||||
case 14:
|
||||
return UNTIL;
|
||||
break;
|
||||
case 15:
|
||||
{
|
||||
yylval.vWord = lua_nfile-1;
|
||||
return FUNCTION;
|
||||
}
|
||||
break;
|
||||
case 16:
|
||||
return END;
|
||||
break;
|
||||
case 17:
|
||||
return RETURN;
|
||||
break;
|
||||
case 18:
|
||||
return LOCAL;
|
||||
break;
|
||||
case 19:
|
||||
return NIL;
|
||||
break;
|
||||
case 20:
|
||||
return AND;
|
||||
break;
|
||||
case 21:
|
||||
return OR;
|
||||
break;
|
||||
case 22:
|
||||
return NOT;
|
||||
break;
|
||||
case 23:
|
||||
return NE;
|
||||
break;
|
||||
case 24:
|
||||
return LE;
|
||||
break;
|
||||
case 25:
|
||||
return GE;
|
||||
break;
|
||||
case 26:
|
||||
return CONC;
|
||||
break;
|
||||
case 27:
|
||||
case 28:
|
||||
{
|
||||
yylval.vWord = lua_findenclosedconstant (yytext);
|
||||
return STRING;
|
||||
}
|
||||
break;
|
||||
case 29:
|
||||
case 30:
|
||||
case 31:
|
||||
case 32:
|
||||
{
|
||||
yylval.vFloat = atof(yytext);
|
||||
return NUMBER;
|
||||
}
|
||||
break;
|
||||
case 33:
|
||||
{
|
||||
yylval.vWord = lua_findsymbol (yytext);
|
||||
return NAME;
|
||||
}
|
||||
break;
|
||||
case 34:
|
||||
return *yytext;
|
||||
break;
|
||||
case -1:
|
||||
break;
|
||||
default:
|
||||
fprintf(yyout,"bad switch yylook %d",nstr);
|
||||
} return(0); }
|
||||
/* end of yylex */
|
||||
int yyvstop[] = {
|
||||
0,
|
||||
|
||||
1,
|
||||
0,
|
||||
|
||||
1,
|
||||
0,
|
||||
|
||||
34,
|
||||
0,
|
||||
|
||||
1,
|
||||
34,
|
||||
0,
|
||||
|
||||
4,
|
||||
0,
|
||||
|
||||
34,
|
||||
0,
|
||||
|
||||
34,
|
||||
0,
|
||||
|
||||
34,
|
||||
0,
|
||||
|
||||
34,
|
||||
0,
|
||||
|
||||
29,
|
||||
34,
|
||||
0,
|
||||
|
||||
34,
|
||||
0,
|
||||
|
||||
34,
|
||||
0,
|
||||
|
||||
33,
|
||||
34,
|
||||
0,
|
||||
|
||||
33,
|
||||
34,
|
||||
0,
|
||||
|
||||
33,
|
||||
34,
|
||||
0,
|
||||
|
||||
33,
|
||||
34,
|
||||
0,
|
||||
|
||||
33,
|
||||
34,
|
||||
0,
|
||||
|
||||
33,
|
||||
34,
|
||||
0,
|
||||
|
||||
33,
|
||||
34,
|
||||
0,
|
||||
|
||||
33,
|
||||
34,
|
||||
0,
|
||||
|
||||
33,
|
||||
34,
|
||||
0,
|
||||
|
||||
33,
|
||||
34,
|
||||
0,
|
||||
|
||||
33,
|
||||
34,
|
||||
0,
|
||||
|
||||
33,
|
||||
34,
|
||||
0,
|
||||
|
||||
33,
|
||||
34,
|
||||
0,
|
||||
|
||||
34,
|
||||
0,
|
||||
|
||||
34,
|
||||
0,
|
||||
|
||||
1,
|
||||
0,
|
||||
|
||||
27,
|
||||
0,
|
||||
|
||||
28,
|
||||
0,
|
||||
|
||||
5,
|
||||
0,
|
||||
|
||||
26,
|
||||
0,
|
||||
|
||||
30,
|
||||
0,
|
||||
|
||||
29,
|
||||
0,
|
||||
|
||||
29,
|
||||
0,
|
||||
|
||||
24,
|
||||
0,
|
||||
|
||||
25,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
12,
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
7,
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
21,
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
23,
|
||||
0,
|
||||
|
||||
29,
|
||||
30,
|
||||
0,
|
||||
|
||||
31,
|
||||
0,
|
||||
|
||||
20,
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
16,
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
19,
|
||||
33,
|
||||
0,
|
||||
|
||||
22,
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
32,
|
||||
0,
|
||||
|
||||
9,
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
8,
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
31,
|
||||
32,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
6,
|
||||
18,
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
14,
|
||||
33,
|
||||
0,
|
||||
|
||||
11,
|
||||
33,
|
||||
0,
|
||||
|
||||
10,
|
||||
33,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
13,
|
||||
33,
|
||||
0,
|
||||
|
||||
17,
|
||||
33,
|
||||
0,
|
||||
|
||||
2,
|
||||
0,
|
||||
|
||||
33,
|
||||
0,
|
||||
|
||||
15,
|
||||
33,
|
||||
0,
|
||||
|
||||
3,
|
||||
0,
|
||||
0};
|
||||
# define YYTYPE char
|
||||
struct yywork { YYTYPE verify, advance; } yycrank[] = {
|
||||
0,0, 0,0, 1,3, 0,0,
|
||||
0,0, 0,0, 0,0, 0,0,
|
||||
0,0, 0,0, 1,4, 1,5,
|
||||
6,29, 4,28, 0,0, 0,0,
|
||||
0,0, 0,0, 7,31, 0,0,
|
||||
6,29, 6,29, 0,0, 0,0,
|
||||
0,0, 0,0, 7,31, 7,31,
|
||||
0,0, 0,0, 0,0, 0,0,
|
||||
0,0, 0,0, 0,0, 1,6,
|
||||
4,28, 0,0, 0,0, 0,0,
|
||||
1,7, 0,0, 0,0, 0,0,
|
||||
1,3, 6,30, 1,8, 1,9,
|
||||
0,0, 1,10, 6,29, 7,31,
|
||||
8,33, 0,0, 6,29, 0,0,
|
||||
7,32, 0,0, 0,0, 6,29,
|
||||
7,31, 1,11, 0,0, 1,12,
|
||||
2,27, 7,31, 1,13, 11,39,
|
||||
12,40, 1,13, 26,56, 0,0,
|
||||
0,0, 2,8, 2,9, 0,0,
|
||||
6,29, 0,0, 0,0, 6,29,
|
||||
0,0, 0,0, 7,31, 0,0,
|
||||
0,0, 7,31, 0,0, 0,0,
|
||||
2,11, 0,0, 2,12, 0,0,
|
||||
0,0, 0,0, 0,0, 0,0,
|
||||
0,0, 0,0, 1,14, 0,0,
|
||||
0,0, 1,15, 1,16, 1,17,
|
||||
0,0, 22,52, 1,18, 18,47,
|
||||
23,53, 1,19, 42,63, 1,20,
|
||||
1,21, 25,55, 14,42, 1,22,
|
||||
15,43, 1,23, 1,24, 16,44,
|
||||
1,25, 16,45, 17,46, 19,48,
|
||||
21,51, 2,14, 20,49, 1,26,
|
||||
2,15, 2,16, 2,17, 24,54,
|
||||
20,50, 2,18, 44,64, 45,65,
|
||||
2,19, 46,66, 2,20, 2,21,
|
||||
27,57, 48,67, 2,22, 49,68,
|
||||
2,23, 2,24, 50,69, 2,25,
|
||||
52,70, 53,72, 27,58, 54,73,
|
||||
52,71, 9,34, 2,26, 9,35,
|
||||
9,35, 9,35, 9,35, 9,35,
|
||||
9,35, 9,35, 9,35, 9,35,
|
||||
9,35, 10,36, 55,74, 10,37,
|
||||
10,37, 10,37, 10,37, 10,37,
|
||||
10,37, 10,37, 10,37, 10,37,
|
||||
10,37, 57,75, 58,76, 64,80,
|
||||
66,81, 67,82, 70,83, 71,84,
|
||||
72,85, 73,86, 74,87, 10,38,
|
||||
10,38, 38,61, 10,38, 38,61,
|
||||
75,88, 76,89, 38,62, 38,62,
|
||||
38,62, 38,62, 38,62, 38,62,
|
||||
38,62, 38,62, 38,62, 38,62,
|
||||
80,92, 81,93, 13,41, 13,41,
|
||||
13,41, 13,41, 13,41, 13,41,
|
||||
13,41, 13,41, 13,41, 13,41,
|
||||
82,94, 83,95, 84,96, 10,38,
|
||||
10,38, 86,97, 10,38, 13,41,
|
||||
13,41, 13,41, 13,41, 13,41,
|
||||
13,41, 13,41, 13,41, 13,41,
|
||||
13,41, 13,41, 13,41, 13,41,
|
||||
13,41, 13,41, 13,41, 13,41,
|
||||
13,41, 13,41, 13,41, 13,41,
|
||||
13,41, 13,41, 13,41, 13,41,
|
||||
13,41, 87,98, 88,99, 60,79,
|
||||
60,79, 13,41, 60,79, 13,41,
|
||||
13,41, 13,41, 13,41, 13,41,
|
||||
13,41, 13,41, 13,41, 13,41,
|
||||
13,41, 13,41, 13,41, 13,41,
|
||||
13,41, 13,41, 13,41, 13,41,
|
||||
13,41, 13,41, 13,41, 13,41,
|
||||
13,41, 13,41, 13,41, 13,41,
|
||||
13,41, 33,33, 89,100, 60,79,
|
||||
60,79, 92,101, 60,79, 93,102,
|
||||
95,103, 33,33, 33,0, 96,104,
|
||||
99,105, 100,106, 102,107, 106,108,
|
||||
107,109, 35,35, 35,35, 35,35,
|
||||
35,35, 35,35, 35,35, 35,35,
|
||||
35,35, 35,35, 35,35, 108,110,
|
||||
0,0, 0,0, 0,0, 0,0,
|
||||
0,0, 0,0, 33,33, 0,0,
|
||||
0,0, 35,59, 35,59, 33,33,
|
||||
35,59, 0,0, 0,0, 33,33,
|
||||
0,0, 0,0, 0,0, 0,0,
|
||||
33,33, 0,0, 0,0, 0,0,
|
||||
0,0, 36,60, 36,60, 36,60,
|
||||
36,60, 36,60, 36,60, 36,60,
|
||||
36,60, 36,60, 36,60, 0,0,
|
||||
0,0, 33,33, 0,0, 0,0,
|
||||
33,33, 35,59, 35,59, 0,0,
|
||||
35,59, 36,38, 36,38, 59,77,
|
||||
36,38, 59,77, 0,0, 0,0,
|
||||
59,78, 59,78, 59,78, 59,78,
|
||||
59,78, 59,78, 59,78, 59,78,
|
||||
59,78, 59,78, 61,62, 61,62,
|
||||
61,62, 61,62, 61,62, 61,62,
|
||||
61,62, 61,62, 61,62, 61,62,
|
||||
0,0, 0,0, 0,0, 0,0,
|
||||
0,0, 36,38, 36,38, 0,0,
|
||||
36,38, 77,78, 77,78, 77,78,
|
||||
77,78, 77,78, 77,78, 77,78,
|
||||
77,78, 77,78, 77,78, 79,90,
|
||||
0,0, 79,90, 0,0, 0,0,
|
||||
79,91, 79,91, 79,91, 79,91,
|
||||
79,91, 79,91, 79,91, 79,91,
|
||||
79,91, 79,91, 90,91, 90,91,
|
||||
90,91, 90,91, 90,91, 90,91,
|
||||
90,91, 90,91, 90,91, 90,91,
|
||||
0,0};
|
||||
struct yysvf yysvec[] = {
|
||||
0, 0, 0,
|
||||
yycrank+-1, 0, yyvstop+1,
|
||||
yycrank+-28, yysvec+1, yyvstop+3,
|
||||
yycrank+0, 0, yyvstop+5,
|
||||
yycrank+4, 0, yyvstop+7,
|
||||
yycrank+0, 0, yyvstop+10,
|
||||
yycrank+-11, 0, yyvstop+12,
|
||||
yycrank+-17, 0, yyvstop+14,
|
||||
yycrank+7, 0, yyvstop+16,
|
||||
yycrank+107, 0, yyvstop+18,
|
||||
yycrank+119, 0, yyvstop+20,
|
||||
yycrank+6, 0, yyvstop+23,
|
||||
yycrank+7, 0, yyvstop+25,
|
||||
yycrank+158, 0, yyvstop+27,
|
||||
yycrank+4, yysvec+13, yyvstop+30,
|
||||
yycrank+5, yysvec+13, yyvstop+33,
|
||||
yycrank+11, yysvec+13, yyvstop+36,
|
||||
yycrank+5, yysvec+13, yyvstop+39,
|
||||
yycrank+5, yysvec+13, yyvstop+42,
|
||||
yycrank+12, yysvec+13, yyvstop+45,
|
||||
yycrank+21, yysvec+13, yyvstop+48,
|
||||
yycrank+10, yysvec+13, yyvstop+51,
|
||||
yycrank+4, yysvec+13, yyvstop+54,
|
||||
yycrank+4, yysvec+13, yyvstop+57,
|
||||
yycrank+21, yysvec+13, yyvstop+60,
|
||||
yycrank+9, yysvec+13, yyvstop+63,
|
||||
yycrank+9, 0, yyvstop+66,
|
||||
yycrank+40, 0, yyvstop+68,
|
||||
yycrank+0, yysvec+4, yyvstop+70,
|
||||
yycrank+0, yysvec+6, 0,
|
||||
yycrank+0, 0, yyvstop+72,
|
||||
yycrank+0, yysvec+7, 0,
|
||||
yycrank+0, 0, yyvstop+74,
|
||||
yycrank+-280, 0, yyvstop+76,
|
||||
yycrank+0, 0, yyvstop+78,
|
||||
yycrank+249, 0, yyvstop+80,
|
||||
yycrank+285, 0, yyvstop+82,
|
||||
yycrank+0, yysvec+10, yyvstop+84,
|
||||
yycrank+146, 0, 0,
|
||||
yycrank+0, 0, yyvstop+86,
|
||||
yycrank+0, 0, yyvstop+88,
|
||||
yycrank+0, yysvec+13, yyvstop+90,
|
||||
yycrank+10, yysvec+13, yyvstop+92,
|
||||
yycrank+0, yysvec+13, yyvstop+94,
|
||||
yycrank+19, yysvec+13, yyvstop+97,
|
||||
yycrank+35, yysvec+13, yyvstop+99,
|
||||
yycrank+27, yysvec+13, yyvstop+101,
|
||||
yycrank+0, yysvec+13, yyvstop+103,
|
||||
yycrank+42, yysvec+13, yyvstop+106,
|
||||
yycrank+35, yysvec+13, yyvstop+108,
|
||||
yycrank+30, yysvec+13, yyvstop+110,
|
||||
yycrank+0, yysvec+13, yyvstop+112,
|
||||
yycrank+36, yysvec+13, yyvstop+115,
|
||||
yycrank+48, yysvec+13, yyvstop+117,
|
||||
yycrank+35, yysvec+13, yyvstop+119,
|
||||
yycrank+61, yysvec+13, yyvstop+121,
|
||||
yycrank+0, 0, yyvstop+123,
|
||||
yycrank+76, 0, 0,
|
||||
yycrank+67, 0, 0,
|
||||
yycrank+312, 0, 0,
|
||||
yycrank+183, yysvec+36, yyvstop+125,
|
||||
yycrank+322, 0, 0,
|
||||
yycrank+0, yysvec+61, yyvstop+128,
|
||||
yycrank+0, yysvec+13, yyvstop+130,
|
||||
yycrank+78, yysvec+13, yyvstop+133,
|
||||
yycrank+0, yysvec+13, yyvstop+135,
|
||||
yycrank+81, yysvec+13, yyvstop+138,
|
||||
yycrank+84, yysvec+13, yyvstop+140,
|
||||
yycrank+0, yysvec+13, yyvstop+142,
|
||||
yycrank+0, yysvec+13, yyvstop+145,
|
||||
yycrank+81, yysvec+13, yyvstop+148,
|
||||
yycrank+66, yysvec+13, yyvstop+150,
|
||||
yycrank+74, yysvec+13, yyvstop+152,
|
||||
yycrank+80, yysvec+13, yyvstop+154,
|
||||
yycrank+78, yysvec+13, yyvstop+156,
|
||||
yycrank+94, 0, 0,
|
||||
yycrank+93, 0, 0,
|
||||
yycrank+341, 0, 0,
|
||||
yycrank+0, yysvec+77, yyvstop+158,
|
||||
yycrank+356, 0, 0,
|
||||
yycrank+99, yysvec+13, yyvstop+160,
|
||||
yycrank+89, yysvec+13, yyvstop+163,
|
||||
yycrank+108, yysvec+13, yyvstop+165,
|
||||
yycrank+120, yysvec+13, yyvstop+167,
|
||||
yycrank+104, yysvec+13, yyvstop+169,
|
||||
yycrank+0, yysvec+13, yyvstop+171,
|
||||
yycrank+113, yysvec+13, yyvstop+174,
|
||||
yycrank+148, yysvec+13, yyvstop+176,
|
||||
yycrank+133, 0, 0,
|
||||
yycrank+181, 0, 0,
|
||||
yycrank+366, 0, 0,
|
||||
yycrank+0, yysvec+90, yyvstop+178,
|
||||
yycrank+183, yysvec+13, yyvstop+181,
|
||||
yycrank+182, yysvec+13, yyvstop+183,
|
||||
yycrank+0, yysvec+13, yyvstop+185,
|
||||
yycrank+172, yysvec+13, yyvstop+189,
|
||||
yycrank+181, yysvec+13, yyvstop+191,
|
||||
yycrank+0, yysvec+13, yyvstop+193,
|
||||
yycrank+0, yysvec+13, yyvstop+196,
|
||||
yycrank+189, 0, 0,
|
||||
yycrank+195, 0, 0,
|
||||
yycrank+0, yysvec+13, yyvstop+199,
|
||||
yycrank+183, yysvec+13, yyvstop+202,
|
||||
yycrank+0, yysvec+13, yyvstop+204,
|
||||
yycrank+0, yysvec+13, yyvstop+207,
|
||||
yycrank+0, 0, yyvstop+210,
|
||||
yycrank+178, 0, 0,
|
||||
yycrank+186, yysvec+13, yyvstop+212,
|
||||
yycrank+204, 0, 0,
|
||||
yycrank+0, yysvec+13, yyvstop+214,
|
||||
yycrank+0, 0, yyvstop+217,
|
||||
0, 0, 0};
|
||||
struct yywork *yytop = yycrank+423;
|
||||
struct yysvf *yybgin = yysvec+1;
|
||||
char yymatch[] = {
|
||||
00 ,01 ,01 ,01 ,01 ,01 ,01 ,01 ,
|
||||
01 ,011 ,012 ,01 ,01 ,01 ,01 ,01 ,
|
||||
01 ,01 ,01 ,01 ,01 ,01 ,01 ,01 ,
|
||||
01 ,01 ,01 ,01 ,01 ,01 ,01 ,01 ,
|
||||
011 ,01 ,'"' ,01 ,01 ,01 ,01 ,047 ,
|
||||
01 ,01 ,01 ,'+' ,01 ,'+' ,01 ,01 ,
|
||||
'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,'0' ,
|
||||
'0' ,'0' ,01 ,01 ,01 ,01 ,01 ,01 ,
|
||||
01 ,'A' ,'A' ,'A' ,'D' ,'D' ,'A' ,'D' ,
|
||||
'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,
|
||||
'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,
|
||||
'A' ,'A' ,'A' ,01 ,01 ,01 ,01 ,'A' ,
|
||||
01 ,'A' ,'A' ,'A' ,'D' ,'D' ,'A' ,'D' ,
|
||||
'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,
|
||||
'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,'A' ,
|
||||
'A' ,'A' ,'A' ,01 ,01 ,01 ,01 ,01 ,
|
||||
0};
|
||||
char yyextra[] = {
|
||||
0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,
|
||||
0};
|
||||
#ifndef lint
|
||||
static char ncform_sccsid[] = "@(#)ncform 1.6 88/02/08 SMI"; /* from S5R2 1.2 */
|
||||
#endif
|
||||
|
||||
int yylineno =1;
|
||||
# define YYU(x) x
|
||||
# define NLSTATE yyprevious=YYNEWLINE
|
||||
char yytext[YYLMAX];
|
||||
struct yysvf *yylstate [YYLMAX], **yylsp, **yyolsp;
|
||||
char yysbuf[YYLMAX];
|
||||
char *yysptr = yysbuf;
|
||||
int *yyfnd;
|
||||
extern struct yysvf *yyestate;
|
||||
int yyprevious = YYNEWLINE;
|
||||
yylook(){
|
||||
register struct yysvf *yystate, **lsp;
|
||||
register struct yywork *yyt;
|
||||
struct yysvf *yyz;
|
||||
int yych, yyfirst;
|
||||
struct yywork *yyr;
|
||||
# ifdef LEXDEBUG
|
||||
int debug;
|
||||
# endif
|
||||
char *yylastch;
|
||||
/* start off machines */
|
||||
# ifdef LEXDEBUG
|
||||
debug = 0;
|
||||
# endif
|
||||
yyfirst=1;
|
||||
if (!yymorfg)
|
||||
yylastch = yytext;
|
||||
else {
|
||||
yymorfg=0;
|
||||
yylastch = yytext+yyleng;
|
||||
}
|
||||
for(;;){
|
||||
lsp = yylstate;
|
||||
yyestate = yystate = yybgin;
|
||||
if (yyprevious==YYNEWLINE) yystate++;
|
||||
for (;;){
|
||||
# ifdef LEXDEBUG
|
||||
if(debug)fprintf(yyout,"state %d\n",yystate-yysvec-1);
|
||||
# endif
|
||||
yyt = yystate->yystoff;
|
||||
if(yyt == yycrank && !yyfirst){ /* may not be any transitions */
|
||||
yyz = yystate->yyother;
|
||||
if(yyz == 0)break;
|
||||
if(yyz->yystoff == yycrank)break;
|
||||
}
|
||||
*yylastch++ = yych = input();
|
||||
yyfirst=0;
|
||||
tryagain:
|
||||
# ifdef LEXDEBUG
|
||||
if(debug){
|
||||
fprintf(yyout,"char ");
|
||||
allprint(yych);
|
||||
putchar('\n');
|
||||
}
|
||||
# endif
|
||||
yyr = yyt;
|
||||
if ( (int)yyt > (int)yycrank){
|
||||
yyt = yyr + yych;
|
||||
if (yyt <= yytop && yyt->verify+yysvec == yystate){
|
||||
if(yyt->advance+yysvec == YYLERR) /* error transitions */
|
||||
{unput(*--yylastch);break;}
|
||||
*lsp++ = yystate = yyt->advance+yysvec;
|
||||
goto contin;
|
||||
}
|
||||
}
|
||||
# ifdef YYOPTIM
|
||||
else if((int)yyt < (int)yycrank) { /* r < yycrank */
|
||||
yyt = yyr = yycrank+(yycrank-yyt);
|
||||
# ifdef LEXDEBUG
|
||||
if(debug)fprintf(yyout,"compressed state\n");
|
||||
# endif
|
||||
yyt = yyt + yych;
|
||||
if(yyt <= yytop && yyt->verify+yysvec == yystate){
|
||||
if(yyt->advance+yysvec == YYLERR) /* error transitions */
|
||||
{unput(*--yylastch);break;}
|
||||
*lsp++ = yystate = yyt->advance+yysvec;
|
||||
goto contin;
|
||||
}
|
||||
yyt = yyr + YYU(yymatch[yych]);
|
||||
# ifdef LEXDEBUG
|
||||
if(debug){
|
||||
fprintf(yyout,"try fall back character ");
|
||||
allprint(YYU(yymatch[yych]));
|
||||
putchar('\n');
|
||||
}
|
||||
# endif
|
||||
if(yyt <= yytop && yyt->verify+yysvec == yystate){
|
||||
if(yyt->advance+yysvec == YYLERR) /* error transition */
|
||||
{unput(*--yylastch);break;}
|
||||
*lsp++ = yystate = yyt->advance+yysvec;
|
||||
goto contin;
|
||||
}
|
||||
}
|
||||
if ((yystate = yystate->yyother) && (yyt= yystate->yystoff) != yycrank){
|
||||
# ifdef LEXDEBUG
|
||||
if(debug)fprintf(yyout,"fall back to state %d\n",yystate-yysvec-1);
|
||||
# endif
|
||||
goto tryagain;
|
||||
}
|
||||
# endif
|
||||
else
|
||||
{unput(*--yylastch);break;}
|
||||
contin:
|
||||
# ifdef LEXDEBUG
|
||||
if(debug){
|
||||
fprintf(yyout,"state %d char ",yystate-yysvec-1);
|
||||
allprint(yych);
|
||||
putchar('\n');
|
||||
}
|
||||
# endif
|
||||
;
|
||||
}
|
||||
# ifdef LEXDEBUG
|
||||
if(debug){
|
||||
fprintf(yyout,"stopped at %d with ",*(lsp-1)-yysvec-1);
|
||||
allprint(yych);
|
||||
putchar('\n');
|
||||
}
|
||||
# endif
|
||||
while (lsp-- > yylstate){
|
||||
*yylastch-- = 0;
|
||||
if (*lsp != 0 && (yyfnd= (*lsp)->yystops) && *yyfnd > 0){
|
||||
yyolsp = lsp;
|
||||
if(yyextra[*yyfnd]){ /* must backup */
|
||||
while(yyback((*lsp)->yystops,-*yyfnd) != 1 && lsp > yylstate){
|
||||
lsp--;
|
||||
unput(*yylastch--);
|
||||
}
|
||||
}
|
||||
yyprevious = YYU(*yylastch);
|
||||
yylsp = lsp;
|
||||
yyleng = yylastch-yytext+1;
|
||||
yytext[yyleng] = 0;
|
||||
# ifdef LEXDEBUG
|
||||
if(debug){
|
||||
fprintf(yyout,"\nmatch ");
|
||||
sprint(yytext);
|
||||
fprintf(yyout," action %d\n",*yyfnd);
|
||||
}
|
||||
# endif
|
||||
return(*yyfnd++);
|
||||
}
|
||||
unput(*yylastch);
|
||||
}
|
||||
if (yytext[0] == 0 /* && feof(yyin) */)
|
||||
{
|
||||
yysptr=yysbuf;
|
||||
return(0);
|
||||
}
|
||||
yyprevious = yytext[0] = input();
|
||||
if (yyprevious>0)
|
||||
output(yyprevious);
|
||||
yylastch=yytext;
|
||||
# ifdef LEXDEBUG
|
||||
if(debug)putchar('\n');
|
||||
# endif
|
||||
}
|
||||
}
|
||||
yyback(p, m)
|
||||
int *p;
|
||||
{
|
||||
if (p==0) return(0);
|
||||
while (*p)
|
||||
{
|
||||
if (*p++ == m)
|
||||
return(1);
|
||||
}
|
||||
return(0);
|
||||
}
|
||||
/* the following are only used in the lex library */
|
||||
yyinput(){
|
||||
return(input());
|
||||
}
|
||||
yyoutput(c)
|
||||
int c; {
|
||||
output(c);
|
||||
}
|
||||
yyunput(c)
|
||||
int c; {
|
||||
unput(c);
|
||||
}
|
||||
98
lfunc.c
98
lfunc.c
@@ -1,98 +0,0 @@
|
||||
/*
|
||||
** $Id: lfunc.c,v 1.7 1997/12/09 13:35:19 roberto Exp roberto $
|
||||
** Auxiliar functions to manipulate prototypes and closures
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "lfunc.h"
|
||||
#include "lmem.h"
|
||||
#include "lstate.h"
|
||||
|
||||
#define gcsizeproto(p) 5 /* approximate "weight" for a prototype */
|
||||
#define gcsizeclosure(c) 1 /* approximate "weight" for a closure */
|
||||
|
||||
|
||||
|
||||
Closure *luaF_newclosure (int nelems)
|
||||
{
|
||||
Closure *c = (Closure *)luaM_malloc(sizeof(Closure)+nelems*sizeof(TObject));
|
||||
luaO_insertlist(&(L->rootcl), (GCnode *)c);
|
||||
L->nblocks += gcsizeclosure(c);
|
||||
c->nelems = nelems;
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
TProtoFunc *luaF_newproto (void)
|
||||
{
|
||||
TProtoFunc *f = luaM_new(TProtoFunc);
|
||||
f->code = NULL;
|
||||
f->lineDefined = 0;
|
||||
f->fileName = NULL;
|
||||
f->consts = NULL;
|
||||
f->nconsts = 0;
|
||||
f->locvars = NULL;
|
||||
luaO_insertlist(&(L->rootproto), (GCnode *)f);
|
||||
L->nblocks += gcsizeproto(f);
|
||||
return f;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void freefunc (TProtoFunc *f)
|
||||
{
|
||||
luaM_free(f->code);
|
||||
luaM_free(f->locvars);
|
||||
luaM_free(f->consts);
|
||||
luaM_free(f);
|
||||
}
|
||||
|
||||
|
||||
void luaF_freeproto (TProtoFunc *l)
|
||||
{
|
||||
while (l) {
|
||||
TProtoFunc *next = (TProtoFunc *)l->head.next;
|
||||
L->nblocks -= gcsizeproto(l);
|
||||
freefunc(l);
|
||||
l = next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaF_freeclosure (Closure *l)
|
||||
{
|
||||
while (l) {
|
||||
Closure *next = (Closure *)l->head.next;
|
||||
L->nblocks -= gcsizeclosure(l);
|
||||
luaM_free(l);
|
||||
l = next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Look for n-th local variable at line "line" in function "func".
|
||||
** Returns NULL if not found.
|
||||
*/
|
||||
char *luaF_getlocalname (TProtoFunc *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;
|
||||
}
|
||||
|
||||
23
lfunc.h
23
lfunc.h
@@ -1,23 +0,0 @@
|
||||
/*
|
||||
** $Id: lfunc.h,v 1.4 1997/11/19 17:29:23 roberto Exp roberto $
|
||||
** Lua Function structures
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lfunc_h
|
||||
#define lfunc_h
|
||||
|
||||
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
|
||||
TProtoFunc *luaF_newproto (void);
|
||||
Closure *luaF_newclosure (int nelems);
|
||||
void luaF_freeproto (TProtoFunc *l);
|
||||
void luaF_freeclosure (Closure *l);
|
||||
|
||||
char *luaF_getlocalname (TProtoFunc *func, int local_number, int line);
|
||||
|
||||
|
||||
#endif
|
||||
286
lgc.c
286
lgc.c
@@ -1,286 +0,0 @@
|
||||
/*
|
||||
** $Id: lgc.c,v 1.14 1997/12/17 20:48:58 roberto Exp roberto $
|
||||
** Garbage Collector
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lgc.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
|
||||
static int markobject (TObject *o);
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** =======================================================
|
||||
** REF mechanism
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
int luaC_ref (TObject *o, int lock)
|
||||
{
|
||||
int ref;
|
||||
if (ttype(o) == LUA_T_NIL)
|
||||
ref = -1; /* special ref for nil */
|
||||
else {
|
||||
for (ref=0; ref<L->refSize; ref++)
|
||||
if (L->refArray[ref].status == FREE)
|
||||
goto found;
|
||||
/* no more empty spaces */ {
|
||||
int oldSize = L->refSize;
|
||||
L->refSize = luaM_growvector(&L->refArray, L->refSize, struct ref,
|
||||
refEM, MAX_WORD);
|
||||
for (ref=oldSize; ref<L->refSize; ref++)
|
||||
L->refArray[ref].status = FREE;
|
||||
ref = oldSize;
|
||||
} found:
|
||||
L->refArray[ref].o = *o;
|
||||
L->refArray[ref].status = lock ? LOCK : HOLD;
|
||||
}
|
||||
return ref;
|
||||
}
|
||||
|
||||
|
||||
void lua_unref (int ref)
|
||||
{
|
||||
if (ref >= 0 && ref < L->refSize)
|
||||
L->refArray[ref].status = FREE;
|
||||
}
|
||||
|
||||
|
||||
TObject* luaC_getref (int ref)
|
||||
{
|
||||
if (ref == -1)
|
||||
return &luaO_nilobject;
|
||||
if (ref >= 0 && ref < L->refSize &&
|
||||
(L->refArray[ref].status == LOCK || L->refArray[ref].status == HOLD))
|
||||
return &L->refArray[ref].o;
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
static void travlock (void)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<L->refSize; i++)
|
||||
if (L->refArray[i].status == LOCK)
|
||||
markobject(&L->refArray[i].o);
|
||||
}
|
||||
|
||||
|
||||
static int ismarked (TObject *o)
|
||||
{
|
||||
/* valid only for locked objects */
|
||||
switch (o->ttype) {
|
||||
case LUA_T_STRING: case LUA_T_USERDATA:
|
||||
return o->value.ts->head.marked;
|
||||
case LUA_T_ARRAY:
|
||||
return o->value.a->head.marked;
|
||||
case LUA_T_CLOSURE:
|
||||
return o->value.cl->head.marked;
|
||||
case LUA_T_PROTO:
|
||||
return o->value.tf->head.marked;
|
||||
#ifdef DEBUG
|
||||
case LUA_T_LINE: case LUA_T_CLMARK:
|
||||
case LUA_T_CMARK: case LUA_T_PMARK:
|
||||
lua_error("internal error");
|
||||
#endif
|
||||
default: /* nil, number or cproto */
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void invalidaterefs (void)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<L->refSize; i++)
|
||||
if (L->refArray[i].status == HOLD && !ismarked(&L->refArray[i].o))
|
||||
L->refArray[i].status = COLLECTED;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void luaC_hashcallIM (Hash *l)
|
||||
{
|
||||
TObject t;
|
||||
ttype(&t) = LUA_T_ARRAY;
|
||||
for (; l; l=(Hash *)l->head.next) {
|
||||
avalue(&t) = l;
|
||||
luaD_gcIM(&t);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaC_strcallIM (TaggedString *l)
|
||||
{
|
||||
TObject o;
|
||||
ttype(&o) = LUA_T_USERDATA;
|
||||
for (; l; l=(TaggedString *)l->head.next)
|
||||
if (l->constindex == -1) { /* is userdata? */
|
||||
tsvalue(&o) = l;
|
||||
luaD_gcIM(&o);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static GCnode *listcollect (GCnode *l)
|
||||
{
|
||||
GCnode *frees = NULL;
|
||||
while (l) {
|
||||
GCnode *next = l->next;
|
||||
l->marked = 0;
|
||||
while (next && !next->marked) {
|
||||
l->next = next->next;
|
||||
next->next = frees;
|
||||
frees = next;
|
||||
next = l->next;
|
||||
}
|
||||
l = next;
|
||||
}
|
||||
return frees;
|
||||
}
|
||||
|
||||
|
||||
static void strmark (TaggedString *s)
|
||||
{
|
||||
if (!s->head.marked)
|
||||
s->head.marked = 1;
|
||||
}
|
||||
|
||||
|
||||
static void protomark (TProtoFunc *f)
|
||||
{
|
||||
if (!f->head.marked) {
|
||||
LocVar *v = f->locvars;
|
||||
int i;
|
||||
f->head.marked = 1;
|
||||
if (f->fileName)
|
||||
strmark(f->fileName);
|
||||
for (i=0; i<f->nconsts; i++)
|
||||
markobject(&f->consts[i]);
|
||||
if (v) {
|
||||
for (; v->line != -1; v++)
|
||||
if (v->varname)
|
||||
strmark(v->varname);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void closuremark (Closure *f)
|
||||
{
|
||||
if (!f->head.marked) {
|
||||
int i;
|
||||
f->head.marked = 1;
|
||||
for (i=f->nelems; i>=0; i--)
|
||||
markobject(&f->consts[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void hashmark (Hash *h)
|
||||
{
|
||||
if (!h->head.marked) {
|
||||
int i;
|
||||
h->head.marked = 1;
|
||||
for (i=0; i<nhash(h); i++) {
|
||||
Node *n = node(h,i);
|
||||
if (ttype(ref(n)) != LUA_T_NIL) {
|
||||
markobject(&n->ref);
|
||||
markobject(&n->val);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void globalmark (void)
|
||||
{
|
||||
TaggedString *g;
|
||||
for (g=(TaggedString *)L->rootglobal.next; g; g=(TaggedString *)g->head.next)
|
||||
if (g->u.globalval.ttype != LUA_T_NIL) {
|
||||
markobject(&g->u.globalval);
|
||||
strmark(g); /* cannot collect non nil global variables */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int markobject (TObject *o)
|
||||
{
|
||||
switch (ttype(o)) {
|
||||
case LUA_T_USERDATA: case LUA_T_STRING:
|
||||
strmark(tsvalue(o));
|
||||
break;
|
||||
case LUA_T_ARRAY:
|
||||
hashmark(avalue(o));
|
||||
break;
|
||||
case LUA_T_CLOSURE: case LUA_T_CLMARK:
|
||||
closuremark(o->value.cl);
|
||||
break;
|
||||
case LUA_T_PROTO: case LUA_T_PMARK:
|
||||
protomark(o->value.tf);
|
||||
break;
|
||||
default: break; /* numbers, cprotos, etc */
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void markall (void)
|
||||
{
|
||||
luaD_travstack(markobject); /* mark stack objects */
|
||||
globalmark(); /* mark global variable values and names */
|
||||
travlock(); /* mark locked objects */
|
||||
luaT_travtagmethods(markobject); /* mark fallbacks */
|
||||
}
|
||||
|
||||
|
||||
long lua_collectgarbage (long limit)
|
||||
{
|
||||
unsigned long recovered = L->nblocks; /* to subtract nblocks after gc */
|
||||
Hash *freetable;
|
||||
TaggedString *freestr;
|
||||
TProtoFunc *freefunc;
|
||||
Closure *freeclos;
|
||||
markall();
|
||||
invalidaterefs();
|
||||
freestr = luaS_collector();
|
||||
freetable = (Hash *)listcollect(&(L->roottable));
|
||||
freefunc = (TProtoFunc *)listcollect(&(L->rootproto));
|
||||
freeclos = (Closure *)listcollect(&(L->rootcl));
|
||||
L->GCthreshold *= 4; /* to avoid GC during GC */
|
||||
luaC_hashcallIM(freetable); /* GC tag methods for tables */
|
||||
luaC_strcallIM(freestr); /* GC tag methods for userdata */
|
||||
luaD_gcIM(&luaO_nilobject); /* GC tag method for nil (signal end of GC) */
|
||||
luaH_free(freetable);
|
||||
luaS_free(freestr);
|
||||
luaF_freeproto(freefunc);
|
||||
luaF_freeclosure(freeclos);
|
||||
recovered = recovered-L->nblocks;
|
||||
L->GCthreshold = (limit == 0) ? 2*L->nblocks : L->nblocks+limit;
|
||||
return recovered;
|
||||
}
|
||||
|
||||
|
||||
void luaC_checkGC (void)
|
||||
{
|
||||
if (L->nblocks >= L->GCthreshold)
|
||||
lua_collectgarbage(0);
|
||||
}
|
||||
|
||||
21
lgc.h
21
lgc.h
@@ -1,21 +0,0 @@
|
||||
/*
|
||||
** $Id: lgc.h,v 1.3 1997/11/19 17:29:23 roberto Exp roberto $
|
||||
** Garbage Collector
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lgc_h
|
||||
#define lgc_h
|
||||
|
||||
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
void luaC_checkGC (void);
|
||||
TObject* luaC_getref (int ref);
|
||||
int luaC_ref (TObject *o, int lock);
|
||||
void luaC_hashcallIM (Hash *l);
|
||||
void luaC_strcallIM (TaggedString *l);
|
||||
|
||||
|
||||
#endif
|
||||
413
liolib.c
413
liolib.c
@@ -1,413 +0,0 @@
|
||||
/*
|
||||
** $Id: liolib.c,v 1.13 1997/12/26 18:38:16 roberto Exp roberto $
|
||||
** Standard I/O (and system) library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
#ifndef OLD_ANSI
|
||||
#include <locale.h>
|
||||
#else
|
||||
#define setlocale(a,b) 0
|
||||
#define LC_ALL 0
|
||||
#define LC_COLLATE 0
|
||||
#define LC_CTYPE 0
|
||||
#define LC_MONETARY 0
|
||||
#define LC_NUMERIC 0
|
||||
#define LC_TIME 0
|
||||
#define strerror(e) "(no error message provided by operating system)"
|
||||
#endif
|
||||
|
||||
|
||||
#define CLOSEDTAG 2
|
||||
#define IOTAG 1
|
||||
|
||||
#define FIRSTARG 3 /* 1st and 2nd are upvalues */
|
||||
|
||||
#define FINPUT "_INPUT"
|
||||
#define FOUTPUT "_OUTPUT"
|
||||
|
||||
|
||||
#ifdef POPEN
|
||||
FILE *popen();
|
||||
int pclose();
|
||||
#else
|
||||
#define popen(x,y) NULL /* that is, popen always fails */
|
||||
#define pclose(x) (-1)
|
||||
#endif
|
||||
|
||||
|
||||
static int gettag (int i)
|
||||
{
|
||||
return lua_getnumber(lua_getparam(i));
|
||||
}
|
||||
|
||||
|
||||
static void pushresult (int i)
|
||||
{
|
||||
if (i)
|
||||
lua_pushuserdata(NULL);
|
||||
else {
|
||||
lua_pushnil();
|
||||
lua_pushstring(strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int ishandler (lua_Object f)
|
||||
{
|
||||
if (lua_isuserdata(f)) {
|
||||
if (lua_tag(f) == gettag(CLOSEDTAG))
|
||||
lua_error("cannot access a closed file");
|
||||
return lua_tag(f) == gettag(IOTAG);
|
||||
}
|
||||
else return 0;
|
||||
}
|
||||
|
||||
static FILE *getfile (char *name)
|
||||
{
|
||||
lua_Object f = lua_getglobal(name);
|
||||
if (!ishandler(f))
|
||||
luaL_verror("global variable `%.50s' is not a file handle", name);
|
||||
return lua_getuserdata(f);
|
||||
}
|
||||
|
||||
|
||||
static FILE *getfileparam (char *name, int *arg)
|
||||
{
|
||||
lua_Object f = lua_getparam(*arg);
|
||||
if (ishandler(f)) {
|
||||
(*arg)++;
|
||||
return lua_getuserdata(f);
|
||||
}
|
||||
else
|
||||
return getfile(name);
|
||||
}
|
||||
|
||||
|
||||
static void closefile (char *name)
|
||||
{
|
||||
FILE *f = getfile(name);
|
||||
if (f == stdin || f == stdout) return;
|
||||
if (pclose(f) == -1)
|
||||
fclose(f);
|
||||
lua_pushobject(lua_getglobal(name));
|
||||
lua_settag(gettag(CLOSEDTAG));
|
||||
}
|
||||
|
||||
|
||||
static void setfile (FILE *f, char *name, int tag)
|
||||
{
|
||||
lua_pushusertag(f, tag);
|
||||
lua_setglobal(name);
|
||||
}
|
||||
|
||||
|
||||
static void setreturn (FILE *f, char *name)
|
||||
{
|
||||
int tag = gettag(IOTAG);
|
||||
setfile(f, name, tag);
|
||||
lua_pushusertag(f, tag);
|
||||
}
|
||||
|
||||
|
||||
static void io_readfrom (void)
|
||||
{
|
||||
FILE *current;
|
||||
lua_Object f = lua_getparam(FIRSTARG);
|
||||
if (f == LUA_NOOBJECT) {
|
||||
closefile(FINPUT);
|
||||
current = stdin;
|
||||
}
|
||||
else if (lua_tag(f) == gettag(IOTAG))
|
||||
current = lua_getuserdata(f);
|
||||
else {
|
||||
char *s = luaL_check_string(FIRSTARG);
|
||||
current = (*s == '|') ? popen(s+1, "r") : fopen(s, "r");
|
||||
if (current == NULL) {
|
||||
pushresult(0);
|
||||
return;
|
||||
}
|
||||
}
|
||||
setreturn(current, FINPUT);
|
||||
}
|
||||
|
||||
|
||||
static void io_writeto (void)
|
||||
{
|
||||
FILE *current;
|
||||
lua_Object f = lua_getparam(FIRSTARG);
|
||||
if (f == LUA_NOOBJECT) {
|
||||
closefile(FOUTPUT);
|
||||
current = stdout;
|
||||
}
|
||||
else if (lua_tag(f) == gettag(IOTAG))
|
||||
current = lua_getuserdata(f);
|
||||
else {
|
||||
char *s = luaL_check_string(FIRSTARG);
|
||||
current = (*s == '|') ? popen(s+1,"w") : fopen(s,"w");
|
||||
if (current == NULL) {
|
||||
pushresult(0);
|
||||
return;
|
||||
}
|
||||
}
|
||||
setreturn(current, FOUTPUT);
|
||||
}
|
||||
|
||||
|
||||
static void io_appendto (void)
|
||||
{
|
||||
char *s = luaL_check_string(FIRSTARG);
|
||||
FILE *fp = fopen (s, "a");
|
||||
if (fp != NULL)
|
||||
setreturn(fp, FOUTPUT);
|
||||
else
|
||||
pushresult(0);
|
||||
}
|
||||
|
||||
|
||||
#define NEED_OTHER (EOF-1) /* just some flag different from EOF */
|
||||
|
||||
static void io_read (void)
|
||||
{
|
||||
int arg = FIRSTARG;
|
||||
FILE *f = getfileparam(FINPUT, &arg);
|
||||
char *buff;
|
||||
char *p = luaL_opt_string(arg, "[^\n]*{\n}");
|
||||
int inskip = 0; /* to control {skips} */
|
||||
int c = NEED_OTHER;
|
||||
luaL_resetbuffer();
|
||||
while (*p) {
|
||||
if (*p == '{') {
|
||||
inskip++;
|
||||
p++;
|
||||
}
|
||||
else if (*p == '}') {
|
||||
if (inskip == 0)
|
||||
lua_error("unbalanced braces in read pattern");
|
||||
inskip--;
|
||||
p++;
|
||||
}
|
||||
else {
|
||||
char *ep; /* get what is next */
|
||||
int m; /* match result */
|
||||
if (c == NEED_OTHER) c = getc(f);
|
||||
m = luaI_singlematch((c == EOF) ? 0 : (char)c, p, &ep);
|
||||
if (m) {
|
||||
if (inskip == 0) luaL_addchar(c);
|
||||
c = NEED_OTHER;
|
||||
}
|
||||
switch (*ep) {
|
||||
case '*': /* repetition */
|
||||
if (!m) p = ep+1; /* else stay in (repeat) the same item */
|
||||
break;
|
||||
case '?': /* optional */
|
||||
p = ep+1; /* continues reading the pattern */
|
||||
break;
|
||||
default:
|
||||
if (m) p = ep; /* continues reading the pattern */
|
||||
else
|
||||
goto break_while; /* pattern fails */
|
||||
}
|
||||
}
|
||||
} break_while:
|
||||
if (c >= 0) /* not EOF nor NEED_OTHER? */
|
||||
ungetc(c, f);
|
||||
luaL_addchar(0);
|
||||
buff = luaL_buffer();
|
||||
if (*buff != 0 || *p == 0) /* read something or did not fail? */
|
||||
lua_pushstring(buff);
|
||||
}
|
||||
|
||||
|
||||
static void io_write (void)
|
||||
{
|
||||
int arg = FIRSTARG;
|
||||
FILE *f = getfileparam(FOUTPUT, &arg);
|
||||
int status = 1;
|
||||
char *s;
|
||||
while ((s = luaL_opt_string(arg++, NULL)) != NULL)
|
||||
status = status && (fputs(s, f) != EOF);
|
||||
pushresult(status);
|
||||
}
|
||||
|
||||
|
||||
static void io_execute (void)
|
||||
{
|
||||
lua_pushnumber(system(luaL_check_string(1)));
|
||||
}
|
||||
|
||||
|
||||
static void io_remove (void)
|
||||
{
|
||||
pushresult(remove(luaL_check_string(1)) == 0);
|
||||
}
|
||||
|
||||
|
||||
static void io_rename (void)
|
||||
{
|
||||
pushresult(rename(luaL_check_string(1),
|
||||
luaL_check_string(2)) == 0);
|
||||
}
|
||||
|
||||
|
||||
static void io_tmpname (void)
|
||||
{
|
||||
lua_pushstring(tmpnam(NULL));
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void io_getenv (void)
|
||||
{
|
||||
lua_pushstring(getenv(luaL_check_string(1))); /* if NULL push nil */
|
||||
}
|
||||
|
||||
|
||||
static void io_date (void)
|
||||
{
|
||||
time_t t;
|
||||
struct tm *tm;
|
||||
char *s = luaL_opt_string(1, "%c");
|
||||
char b[BUFSIZ];
|
||||
time(&t); tm = localtime(&t);
|
||||
if (strftime(b,sizeof(b),s,tm))
|
||||
lua_pushstring(b);
|
||||
else
|
||||
lua_error("invalid `date' format");
|
||||
}
|
||||
|
||||
|
||||
static void setloc (void)
|
||||
{
|
||||
static int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC,
|
||||
LC_TIME};
|
||||
int op = (int)luaL_opt_number(2, 0);
|
||||
luaL_arg_check(0 <= op && op <= 5, 2, "invalid option");
|
||||
lua_pushstring(setlocale(cat[op], luaL_check_string(1)));
|
||||
}
|
||||
|
||||
|
||||
static void io_exit (void)
|
||||
{
|
||||
lua_Object o = lua_getparam(1);
|
||||
exit(lua_isnumber(o) ? (int)lua_getnumber(o) : 1);
|
||||
}
|
||||
|
||||
|
||||
static void io_debug (void)
|
||||
{
|
||||
while (1) {
|
||||
char buffer[250];
|
||||
fprintf(stderr, "lua_debug> ");
|
||||
if (fgets(buffer, sizeof(buffer), stdin) == 0) return;
|
||||
if (strcmp(buffer, "cont\n") == 0) return;
|
||||
lua_dostring(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void lua_printstack (FILE *f)
|
||||
{
|
||||
int level = 1; /* skip level 0 (it's this function) */
|
||||
lua_Object func;
|
||||
while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT) {
|
||||
char *name;
|
||||
int currentline;
|
||||
char *filename;
|
||||
int linedefined;
|
||||
lua_funcinfo(func, &filename, &linedefined);
|
||||
fprintf(f, (level==2) ? "Active Stack:\n\t" : "\t");
|
||||
switch (*lua_getobjname(func, &name)) {
|
||||
case 'g':
|
||||
fprintf(f, "function %s", name);
|
||||
break;
|
||||
case 't':
|
||||
fprintf(f, "`%s' tag method", name);
|
||||
break;
|
||||
default: {
|
||||
if (linedefined == 0)
|
||||
fprintf(f, "main of %s", filename);
|
||||
else if (linedefined < 0)
|
||||
fprintf(f, "%s", filename);
|
||||
else
|
||||
fprintf(f, "function (%s:%d)", filename, linedefined);
|
||||
filename = NULL;
|
||||
}
|
||||
}
|
||||
if ((currentline = lua_currentline(func)) > 0)
|
||||
fprintf(f, " at line %d", currentline);
|
||||
if (filename)
|
||||
fprintf(f, " [in file %s]", filename);
|
||||
fprintf(f, "\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void errorfb (void)
|
||||
{
|
||||
fprintf(stderr, "lua: %s\n", lua_getstring(lua_getparam(1)));
|
||||
lua_printstack(stderr);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static struct luaL_reg iolib[] = {
|
||||
{"setlocale", setloc},
|
||||
{"execute", io_execute},
|
||||
{"remove", io_remove},
|
||||
{"rename", io_rename},
|
||||
{"tmpname", io_tmpname},
|
||||
{"getenv", io_getenv},
|
||||
{"date", io_date},
|
||||
{"exit", io_exit},
|
||||
{"debug", io_debug},
|
||||
{"print_stack", errorfb}
|
||||
};
|
||||
|
||||
static struct luaL_reg iolibtag[] = {
|
||||
{"readfrom", io_readfrom},
|
||||
{"writeto", io_writeto},
|
||||
{"appendto", io_appendto},
|
||||
{"read", io_read},
|
||||
{"write", io_write}
|
||||
};
|
||||
|
||||
static void openwithtags (void)
|
||||
{
|
||||
int iotag = lua_newtag();
|
||||
int closedtag = lua_newtag();
|
||||
int i;
|
||||
for (i=0; i<sizeof(iolibtag)/sizeof(iolibtag[0]); i++) {
|
||||
/* put both tags as upvalues for these functions */
|
||||
lua_pushnumber(iotag);
|
||||
lua_pushnumber(closedtag);
|
||||
lua_pushCclosure(iolibtag[i].func, 2);
|
||||
lua_setglobal(iolibtag[i].name);
|
||||
}
|
||||
setfile(stdin, FINPUT, iotag);
|
||||
setfile(stdout, FOUTPUT, iotag);
|
||||
setfile(stdin, "_STDIN", iotag);
|
||||
setfile(stdout, "_STDOUT", iotag);
|
||||
setfile(stderr, "_STDERR", iotag);
|
||||
}
|
||||
|
||||
void lua_iolibopen (void)
|
||||
{
|
||||
luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0])));
|
||||
openwithtags();
|
||||
lua_pushcfunction(errorfb);
|
||||
lua_seterrormethod();
|
||||
}
|
||||
437
llex.c
437
llex.c
@@ -1,437 +0,0 @@
|
||||
/*
|
||||
** $Id: llex.c,v 1.12 1997/12/22 17:52:20 roberto Exp roberto $
|
||||
** Lexical Analizer
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "llex.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lparser.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "lstx.h"
|
||||
#include "luadebug.h"
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
|
||||
int lua_debug=0;
|
||||
|
||||
|
||||
#define next(LS) (LS->current = zgetc(LS->lex_z))
|
||||
|
||||
|
||||
static struct {
|
||||
char *name;
|
||||
int token;
|
||||
} reserved [] = {
|
||||
{"and", AND}, {"do", DO}, {"else", ELSE}, {"elseif", ELSEIF},
|
||||
{"end", END}, {"function", FUNCTION}, {"if", IF}, {"local", LOCAL},
|
||||
{"nil", NIL}, {"not", NOT}, {"or", OR}, {"repeat", REPEAT},
|
||||
{"return", RETURN}, {"then", THEN}, {"until", UNTIL}, {"while", WHILE}
|
||||
};
|
||||
|
||||
void luaX_init (void)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<(sizeof(reserved)/sizeof(reserved[0])); i++) {
|
||||
TaggedString *ts = luaS_new(reserved[i].name);
|
||||
ts->head.marked = reserved[i].token; /* reserved word (always > 255) */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void firstline (LexState *LS)
|
||||
{
|
||||
int c = zgetc(LS->lex_z);
|
||||
if (c == '#') {
|
||||
LS->linenumber++;
|
||||
while ((c=zgetc(LS->lex_z)) != '\n' && c != EOZ) /* skip first line */;
|
||||
}
|
||||
zungetc(LS->lex_z);
|
||||
}
|
||||
|
||||
|
||||
void luaX_setinput (ZIO *z)
|
||||
{
|
||||
LexState *LS = L->lexstate;
|
||||
LS->current = '\n';
|
||||
LS->linelasttoken = 0;
|
||||
LS->linenumber = 0;
|
||||
LS->iflevel = 0;
|
||||
LS->ifstate[0].skip = 0;
|
||||
LS->ifstate[0].elsepart = 1; /* to avoid a free $else */
|
||||
LS->lex_z = z;
|
||||
firstline(LS);
|
||||
luaL_resetbuffer();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** =======================================================
|
||||
** PRAGMAS
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
#define PRAGMASIZE 20
|
||||
|
||||
static void skipspace (LexState *LS)
|
||||
{
|
||||
while (LS->current == ' ' || LS->current == '\t' || LS->current == '\r')
|
||||
next(LS);
|
||||
}
|
||||
|
||||
|
||||
static int checkcond (char *buff)
|
||||
{
|
||||
static char *opts[] = {"nil", "1", NULL};
|
||||
int i = luaO_findstring(buff, opts);
|
||||
if (i >= 0) return i;
|
||||
else if (isalpha((unsigned char)buff[0]) || buff[0] == '_')
|
||||
return luaS_globaldefined(buff);
|
||||
else {
|
||||
luaY_syntaxerror("invalid $if condition", buff);
|
||||
return 0; /* to avoid warnings */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void readname (LexState *LS, char *buff)
|
||||
{
|
||||
int i = 0;
|
||||
skipspace(LS);
|
||||
while (isalnum(LS->current) || LS->current == '_') {
|
||||
if (i >= PRAGMASIZE) {
|
||||
buff[PRAGMASIZE] = 0;
|
||||
luaY_syntaxerror("pragma too long", buff);
|
||||
}
|
||||
buff[i++] = LS->current;
|
||||
next(LS);
|
||||
}
|
||||
buff[i] = 0;
|
||||
}
|
||||
|
||||
|
||||
static void inclinenumber (LexState *LS);
|
||||
|
||||
|
||||
static void ifskip (LexState *LS)
|
||||
{
|
||||
while (LS->ifstate[LS->iflevel].skip) {
|
||||
if (LS->current == '\n')
|
||||
inclinenumber(LS);
|
||||
else if (LS->current == EOZ)
|
||||
luaY_syntaxerror("input ends inside a $if", "");
|
||||
else next(LS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void inclinenumber (LexState *LS)
|
||||
{
|
||||
static char *pragmas [] =
|
||||
{"debug", "nodebug", "endinput", "end", "ifnot", "if", "else", NULL};
|
||||
next(LS); /* skip '\n' */
|
||||
++LS->linenumber;
|
||||
if (LS->current == '$') { /* is a pragma? */
|
||||
char buff[PRAGMASIZE+1];
|
||||
int ifnot = 0;
|
||||
int skip = LS->ifstate[LS->iflevel].skip;
|
||||
next(LS); /* skip $ */
|
||||
readname(LS, buff);
|
||||
switch (luaO_findstring(buff, pragmas)) {
|
||||
case 0: /* debug */
|
||||
if (!skip) lua_debug = 1;
|
||||
break;
|
||||
case 1: /* nodebug */
|
||||
if (!skip) lua_debug = 0;
|
||||
break;
|
||||
case 2: /* endinput */
|
||||
if (!skip) {
|
||||
LS->current = EOZ;
|
||||
LS->iflevel = 0; /* to allow $endinput inside a $if */
|
||||
}
|
||||
break;
|
||||
case 3: /* end */
|
||||
if (LS->iflevel-- == 0)
|
||||
luaY_syntaxerror("unmatched $end", "$end");
|
||||
break;
|
||||
case 4: /* ifnot */
|
||||
ifnot = 1;
|
||||
/* go through */
|
||||
case 5: /* if */
|
||||
if (LS->iflevel == MAX_IFS-1)
|
||||
luaY_syntaxerror("too many nested $ifs", "$if");
|
||||
readname(LS, buff);
|
||||
LS->iflevel++;
|
||||
LS->ifstate[LS->iflevel].elsepart = 0;
|
||||
LS->ifstate[LS->iflevel].condition = checkcond(buff) ? !ifnot : ifnot;
|
||||
LS->ifstate[LS->iflevel].skip = skip || !LS->ifstate[LS->iflevel].condition;
|
||||
break;
|
||||
case 6: /* else */
|
||||
if (LS->ifstate[LS->iflevel].elsepart)
|
||||
luaY_syntaxerror("unmatched $else", "$else");
|
||||
LS->ifstate[LS->iflevel].elsepart = 1;
|
||||
LS->ifstate[LS->iflevel].skip = LS->ifstate[LS->iflevel-1].skip ||
|
||||
LS->ifstate[LS->iflevel].condition;
|
||||
break;
|
||||
default:
|
||||
luaY_syntaxerror("unknown pragma", buff);
|
||||
}
|
||||
skipspace(LS);
|
||||
if (LS->current == '\n') /* pragma must end with a '\n' ... */
|
||||
inclinenumber(LS);
|
||||
else if (LS->current != EOZ) /* or eof */
|
||||
luaY_syntaxerror("invalid pragma format", buff);
|
||||
ifskip(LS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** =======================================================
|
||||
** LEXICAL ANALIZER
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#define save(c) luaL_addchar(c)
|
||||
#define save_and_next(LS) (save(LS->current), next(LS))
|
||||
|
||||
|
||||
char *luaX_lasttoken (void)
|
||||
{
|
||||
save(0);
|
||||
return luaL_buffer();
|
||||
}
|
||||
|
||||
|
||||
static int read_long_string (LexState *LS, YYSTYPE *l)
|
||||
{
|
||||
int cont = 0;
|
||||
while (1) {
|
||||
switch (LS->current) {
|
||||
case EOZ:
|
||||
save(0);
|
||||
return WRONGTOKEN;
|
||||
case '[':
|
||||
save_and_next(LS);
|
||||
if (LS->current == '[') {
|
||||
cont++;
|
||||
save_and_next(LS);
|
||||
}
|
||||
continue;
|
||||
case ']':
|
||||
save_and_next(LS);
|
||||
if (LS->current == ']') {
|
||||
if (cont == 0) goto endloop;
|
||||
cont--;
|
||||
save_and_next(LS);
|
||||
}
|
||||
continue;
|
||||
case '\n':
|
||||
save('\n');
|
||||
inclinenumber(LS);
|
||||
continue;
|
||||
default:
|
||||
save_and_next(LS);
|
||||
}
|
||||
} endloop:
|
||||
save_and_next(LS); /* pass the second ']' */
|
||||
L->Mbuffer[L->Mbuffnext-2] = 0; /* erases ']]' */
|
||||
l->pTStr = luaS_new(L->Mbuffbase+2);
|
||||
L->Mbuffer[L->Mbuffnext-2] = ']'; /* restores ']]' */
|
||||
return STRING;
|
||||
}
|
||||
|
||||
|
||||
/* to avoid warnings; this declaration cannot be public since YYSTYPE
|
||||
** cannot be visible in llex.h (otherwise there is an error, since
|
||||
** the parser body redefines it!)
|
||||
*/
|
||||
int luaY_lex (YYSTYPE *l);
|
||||
int luaY_lex (YYSTYPE *l)
|
||||
{
|
||||
LexState *LS = L->lexstate;
|
||||
double a;
|
||||
luaL_resetbuffer();
|
||||
if (lua_debug)
|
||||
luaY_codedebugline(LS->linelasttoken);
|
||||
LS->linelasttoken = LS->linenumber;
|
||||
while (1) {
|
||||
switch (LS->current) {
|
||||
|
||||
case ' ': case '\t': case '\r': /* CR: to avoid problems with DOS */
|
||||
next(LS);
|
||||
continue;
|
||||
|
||||
case '\n':
|
||||
inclinenumber(LS);
|
||||
LS->linelasttoken = LS->linenumber;
|
||||
continue;
|
||||
|
||||
case '-':
|
||||
save_and_next(LS);
|
||||
if (LS->current != '-') return '-';
|
||||
do { next(LS); } while (LS->current != '\n' && LS->current != EOZ);
|
||||
luaL_resetbuffer();
|
||||
continue;
|
||||
|
||||
case '[':
|
||||
save_and_next(LS);
|
||||
if (LS->current != '[') return '[';
|
||||
else {
|
||||
save_and_next(LS); /* pass the second '[' */
|
||||
return read_long_string(LS, l);
|
||||
}
|
||||
|
||||
case '=':
|
||||
save_and_next(LS);
|
||||
if (LS->current != '=') return '=';
|
||||
else { save_and_next(LS); return EQ; }
|
||||
|
||||
case '<':
|
||||
save_and_next(LS);
|
||||
if (LS->current != '=') return '<';
|
||||
else { save_and_next(LS); return LE; }
|
||||
|
||||
case '>':
|
||||
save_and_next(LS);
|
||||
if (LS->current != '=') return '>';
|
||||
else { save_and_next(LS); return GE; }
|
||||
|
||||
case '~':
|
||||
save_and_next(LS);
|
||||
if (LS->current != '=') return '~';
|
||||
else { save_and_next(LS); return NE; }
|
||||
|
||||
case '"':
|
||||
case '\'': {
|
||||
int del = LS->current;
|
||||
save_and_next(LS);
|
||||
while (LS->current != del) {
|
||||
switch (LS->current) {
|
||||
case EOZ:
|
||||
case '\n':
|
||||
save(0);
|
||||
return WRONGTOKEN;
|
||||
case '\\':
|
||||
next(LS); /* do not save the '\' */
|
||||
switch (LS->current) {
|
||||
case 'n': save('\n'); next(LS); break;
|
||||
case 't': save('\t'); next(LS); break;
|
||||
case 'r': save('\r'); next(LS); break;
|
||||
case '\n': save('\n'); inclinenumber(LS); break;
|
||||
default : save_and_next(LS); break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
save_and_next(LS);
|
||||
}
|
||||
}
|
||||
next(LS); /* skip delimiter */
|
||||
save(0);
|
||||
l->pTStr = luaS_new(L->Mbuffbase+1);
|
||||
L->Mbuffer[L->Mbuffnext-1] = del; /* restore delimiter */
|
||||
return STRING;
|
||||
}
|
||||
|
||||
case '.':
|
||||
save_and_next(LS);
|
||||
if (LS->current == '.')
|
||||
{
|
||||
save_and_next(LS);
|
||||
if (LS->current == '.')
|
||||
{
|
||||
save_and_next(LS);
|
||||
return DOTS; /* ... */
|
||||
}
|
||||
else return CONC; /* .. */
|
||||
}
|
||||
else if (!isdigit(LS->current)) return '.';
|
||||
/* LS->current is a digit: goes through to number */
|
||||
a=0.0;
|
||||
goto fraction;
|
||||
|
||||
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.0*a+(LS->current-'0');
|
||||
save_and_next(LS);
|
||||
} while (isdigit(LS->current));
|
||||
if (LS->current == '.') {
|
||||
save_and_next(LS);
|
||||
if (LS->current == '.') {
|
||||
save(0);
|
||||
luaY_error(
|
||||
"ambiguous syntax (decimal point x string concatenation)");
|
||||
}
|
||||
}
|
||||
fraction:
|
||||
{ double da=0.1;
|
||||
while (isdigit(LS->current))
|
||||
{
|
||||
a+=(LS->current-'0')*da;
|
||||
da/=10.0;
|
||||
save_and_next(LS);
|
||||
}
|
||||
if (toupper(LS->current) == 'E') {
|
||||
int e=0;
|
||||
int neg;
|
||||
double ea;
|
||||
save_and_next(LS);
|
||||
neg=(LS->current=='-');
|
||||
if (LS->current == '+' || LS->current == '-') save_and_next(LS);
|
||||
if (!isdigit(LS->current)) {
|
||||
save(0); return WRONGTOKEN; }
|
||||
do {
|
||||
e=10.0*e+(LS->current-'0');
|
||||
save_and_next(LS);
|
||||
} while (isdigit(LS->current));
|
||||
for (ea=neg?0.1:10.0; e>0; e>>=1)
|
||||
{
|
||||
if (e & 1) a*=ea;
|
||||
ea*=ea;
|
||||
}
|
||||
}
|
||||
l->vReal = a;
|
||||
return NUMBER;
|
||||
}
|
||||
|
||||
case EOZ:
|
||||
save(0);
|
||||
if (LS->iflevel > 0)
|
||||
luaY_syntaxerror("input ends inside a $if", "");
|
||||
return 0;
|
||||
|
||||
default:
|
||||
if (LS->current != '_' && !isalpha(LS->current)) {
|
||||
int c = LS->current;
|
||||
save_and_next(LS);
|
||||
return c;
|
||||
}
|
||||
else { /* identifier or reserved word */
|
||||
TaggedString *ts;
|
||||
do {
|
||||
save_and_next(LS);
|
||||
} while (isalnum(LS->current) || LS->current == '_');
|
||||
save(0);
|
||||
ts = luaS_new(L->Mbuffbase);
|
||||
if (ts->head.marked > 255)
|
||||
return ts->head.marked; /* reserved word */
|
||||
l->pTStr = ts;
|
||||
return NAME;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
41
llex.h
41
llex.h
@@ -1,41 +0,0 @@
|
||||
/*
|
||||
** $Id: llex.h,v 1.6 1997/12/17 20:48:58 roberto Exp roberto $
|
||||
** Lexical Analizer
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef llex_h
|
||||
#define llex_h
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
#define MAX_IFS 5
|
||||
|
||||
/* "ifstate" keeps the state of each nested $if the lexical is dealing with. */
|
||||
|
||||
struct ifState {
|
||||
int elsepart; /* true if its in the $else part */
|
||||
int condition; /* true if $if condition is true */
|
||||
int skip; /* true if part must be skiped */
|
||||
};
|
||||
|
||||
|
||||
typedef struct LexState {
|
||||
int current; /* look ahead character */
|
||||
struct zio *lex_z; /* input stream */
|
||||
int linenumber; /* input line counter */
|
||||
int linelasttoken; /* line where last token was read */
|
||||
int lastline; /* last line wherein a SETLINE was generated */
|
||||
int iflevel; /* level of nested $if's (for lexical analysis) */
|
||||
struct ifState ifstate[MAX_IFS];
|
||||
} LexState;
|
||||
|
||||
|
||||
void luaX_init (void);
|
||||
void luaX_setinput (ZIO *z);
|
||||
char *luaX_lasttoken (void);
|
||||
|
||||
|
||||
#endif
|
||||
201
lmathlib.c
201
lmathlib.c
@@ -1,201 +0,0 @@
|
||||
/*
|
||||
** $Id: lmathlib.c,v 1.7 1997/12/09 13:35:19 roberto Exp roberto $
|
||||
** Lua standard mathematical library
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lua.h"
|
||||
#include "lualib.h"
|
||||
|
||||
#ifdef M_PI
|
||||
#define PI M_PI
|
||||
#else
|
||||
#define PI ((double)3.14159265358979323846)
|
||||
#endif
|
||||
|
||||
|
||||
#define FROMRAD(a) ((a)*(180.0/PI))
|
||||
#define TORAD(a) ((a)*(PI/180.0))
|
||||
|
||||
|
||||
static void math_abs (void)
|
||||
{
|
||||
double d = luaL_check_number(1);
|
||||
if (d < 0) d = -d;
|
||||
lua_pushnumber(d);
|
||||
}
|
||||
|
||||
static void math_sin (void)
|
||||
{
|
||||
lua_pushnumber(sin(TORAD(luaL_check_number(1))));
|
||||
}
|
||||
|
||||
static void math_cos (void)
|
||||
{
|
||||
lua_pushnumber(cos(TORAD(luaL_check_number(1))));
|
||||
}
|
||||
|
||||
static void math_tan (void)
|
||||
{
|
||||
lua_pushnumber(tan(TORAD(luaL_check_number(1))));
|
||||
}
|
||||
|
||||
static void math_asin (void)
|
||||
{
|
||||
lua_pushnumber(FROMRAD(asin(luaL_check_number(1))));
|
||||
}
|
||||
|
||||
static void math_acos (void)
|
||||
{
|
||||
lua_pushnumber(FROMRAD(acos(luaL_check_number(1))));
|
||||
}
|
||||
|
||||
static void math_atan (void)
|
||||
{
|
||||
lua_pushnumber(FROMRAD(atan(luaL_check_number(1))));
|
||||
}
|
||||
|
||||
static void math_atan2 (void)
|
||||
{
|
||||
lua_pushnumber(FROMRAD(atan2(luaL_check_number(1), luaL_check_number(2))));
|
||||
}
|
||||
|
||||
static void math_ceil (void)
|
||||
{
|
||||
lua_pushnumber(ceil(luaL_check_number(1)));
|
||||
}
|
||||
|
||||
static void math_floor (void)
|
||||
{
|
||||
lua_pushnumber(floor(luaL_check_number(1)));
|
||||
}
|
||||
|
||||
static void math_mod (void)
|
||||
{
|
||||
lua_pushnumber(fmod(luaL_check_number(1), luaL_check_number(2)));
|
||||
}
|
||||
|
||||
static void math_sqrt (void)
|
||||
{
|
||||
lua_pushnumber(sqrt(luaL_check_number(1)));
|
||||
}
|
||||
|
||||
static void math_pow (void)
|
||||
{
|
||||
lua_pushnumber(pow(luaL_check_number(1), luaL_check_number(2)));
|
||||
}
|
||||
|
||||
static void math_log (void)
|
||||
{
|
||||
lua_pushnumber(log(luaL_check_number(1)));
|
||||
}
|
||||
|
||||
static void math_log10 (void)
|
||||
{
|
||||
lua_pushnumber(log10(luaL_check_number(1)));
|
||||
}
|
||||
|
||||
static void math_exp (void)
|
||||
{
|
||||
lua_pushnumber(exp(luaL_check_number(1)));
|
||||
}
|
||||
|
||||
static void math_deg (void)
|
||||
{
|
||||
lua_pushnumber(luaL_check_number(1)*(180.0/PI));
|
||||
}
|
||||
|
||||
static void math_rad (void)
|
||||
{
|
||||
lua_pushnumber(luaL_check_number(1)*(PI/180.0));
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void math_min (void)
|
||||
{
|
||||
int i = 1;
|
||||
double dmin = luaL_check_number(i);
|
||||
while (lua_getparam(++i) != LUA_NOOBJECT) {
|
||||
double d = luaL_check_number(i);
|
||||
if (d < dmin)
|
||||
dmin = d;
|
||||
}
|
||||
lua_pushnumber(dmin);
|
||||
}
|
||||
|
||||
|
||||
static void math_max (void)
|
||||
{
|
||||
int i = 1;
|
||||
double dmax = luaL_check_number(i);
|
||||
while (lua_getparam(++i) != LUA_NOOBJECT) {
|
||||
double d = luaL_check_number(i);
|
||||
if (d > dmax)
|
||||
dmax = d;
|
||||
}
|
||||
lua_pushnumber(dmax);
|
||||
}
|
||||
|
||||
|
||||
static void math_random (void)
|
||||
{
|
||||
/* the '%' is needed because on some sistems (SunOS!) "rand()" may */
|
||||
/* return a value bigger than RAND_MAX... */
|
||||
double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX;
|
||||
double l = luaL_opt_number(1, 0);
|
||||
if (l == 0)
|
||||
lua_pushnumber(r);
|
||||
else
|
||||
lua_pushnumber((int)(r*l)+1);
|
||||
}
|
||||
|
||||
|
||||
static void math_randomseed (void)
|
||||
{
|
||||
srand(luaL_check_number(1));
|
||||
}
|
||||
|
||||
|
||||
static struct luaL_reg mathlib[] = {
|
||||
{"abs", math_abs},
|
||||
{"sin", math_sin},
|
||||
{"cos", math_cos},
|
||||
{"tan", math_tan},
|
||||
{"asin", math_asin},
|
||||
{"acos", math_acos},
|
||||
{"atan", math_atan},
|
||||
{"atan2", math_atan2},
|
||||
{"ceil", math_ceil},
|
||||
{"floor", math_floor},
|
||||
{"mod", math_mod},
|
||||
{"sqrt", math_sqrt},
|
||||
{"min", math_min},
|
||||
{"max", math_max},
|
||||
{"log", math_log},
|
||||
{"log10", math_log10},
|
||||
{"exp", math_exp},
|
||||
{"deg", math_deg},
|
||||
{"rad", math_rad},
|
||||
{"random", math_random},
|
||||
{"randomseed", math_randomseed}
|
||||
};
|
||||
|
||||
/*
|
||||
** Open math library
|
||||
*/
|
||||
void lua_mathlibopen (void)
|
||||
{
|
||||
luaL_openlib(mathlib, (sizeof(mathlib)/sizeof(mathlib[0])));
|
||||
lua_pushstring("deg"); lua_setglobal("_TRIGMODE");
|
||||
lua_pushcfunction(math_pow);
|
||||
lua_pushnumber(0); /* to get its tag */
|
||||
lua_settagmethod(lua_tag(lua_pop()), "pow");
|
||||
lua_pushnumber(PI); lua_setglobal("PI");
|
||||
}
|
||||
|
||||
110
lmem.c
110
lmem.c
@@ -1,110 +0,0 @@
|
||||
/*
|
||||
** $Id: lmem.c,v 1.3 1997/12/01 20:30:44 roberto Exp roberto $
|
||||
** Interface to Memory Manager
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "lmem.h"
|
||||
#include "lstate.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
|
||||
int luaM_growaux (void **block, unsigned long nelems, int size,
|
||||
char *errormsg, unsigned long limit)
|
||||
{
|
||||
if (nelems >= limit)
|
||||
lua_error(errormsg);
|
||||
nelems = (nelems == 0) ? 32 : nelems*2;
|
||||
if (nelems > limit)
|
||||
nelems = limit;
|
||||
*block = luaM_realloc(*block, nelems*size);
|
||||
return (int)nelems;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifndef DEBUG
|
||||
|
||||
/*
|
||||
** generic allocation routine.
|
||||
** real ANSI systems do not need some of these tests,
|
||||
** since realloc(NULL, s)==malloc(s) and realloc(b, 0)==free(b).
|
||||
** But some systems (e.g. Sun OS) are not that ANSI...
|
||||
*/
|
||||
void *luaM_realloc (void *block, unsigned long size)
|
||||
{
|
||||
size_t s = (size_t)size;
|
||||
if (s != size)
|
||||
lua_error("Allocation Error: Block too big");
|
||||
if (size == 0) {
|
||||
if (block) {
|
||||
free(block);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
block = block ? realloc(block, s) : malloc(s);
|
||||
if (block == NULL)
|
||||
lua_error(memEM);
|
||||
return block;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#else
|
||||
/* DEBUG */
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
#define MARK 55
|
||||
|
||||
unsigned long numblocks = 0;
|
||||
unsigned long totalmem = 0;
|
||||
|
||||
|
||||
static void *checkblock (void *block)
|
||||
{
|
||||
unsigned long *b = (unsigned long *)block - 1;
|
||||
unsigned long size = *b;
|
||||
assert(*(((char *)b)+size+sizeof(unsigned long)) == MARK);
|
||||
numblocks--;
|
||||
totalmem -= size;
|
||||
return b;
|
||||
}
|
||||
|
||||
|
||||
void *luaM_realloc (void *block, unsigned long size)
|
||||
{
|
||||
unsigned long realsize = sizeof(unsigned long)+size+sizeof(char);
|
||||
if (realsize != (size_t)realsize)
|
||||
lua_error("Allocation Error: Block too big");
|
||||
if (size == 0) { /* ANSI doen't need this, but some machines... */
|
||||
if (block) {
|
||||
memset(block, -1, *((unsigned long *)block-1)); /* erase block */
|
||||
block = checkblock(block);
|
||||
free(block);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
if (block) {
|
||||
block = checkblock(block);
|
||||
block = (unsigned long *)realloc(block, realsize);
|
||||
}
|
||||
else
|
||||
block = (unsigned long *)malloc(realsize);
|
||||
if (block == NULL)
|
||||
lua_error(memEM);
|
||||
totalmem += size;
|
||||
numblocks++;
|
||||
*(unsigned long *)block = size;
|
||||
*(((char *)block)+size+sizeof(unsigned long)) = MARK;
|
||||
return (unsigned long *)block+1;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
43
lmem.h
43
lmem.h
@@ -1,43 +0,0 @@
|
||||
/*
|
||||
** $Id: lmem.h,v 1.4 1997/12/01 20:30:44 roberto Exp roberto $
|
||||
** Interface to Memory Manager
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lmem_h
|
||||
#define lmem_h
|
||||
|
||||
|
||||
#ifndef NULL
|
||||
#define NULL 0
|
||||
#endif
|
||||
|
||||
|
||||
/* memory error messages */
|
||||
#define codeEM "code size overflow"
|
||||
#define constantEM "constant table overflow"
|
||||
#define refEM "reference table overflow"
|
||||
#define tableEM "table overflow"
|
||||
#define memEM "not enough memory"
|
||||
|
||||
void *luaM_realloc (void *oldblock, unsigned long size);
|
||||
int luaM_growaux (void **block, unsigned long nelems, int size,
|
||||
char *errormsg, unsigned long limit);
|
||||
|
||||
#define luaM_free(b) luaM_realloc((b), 0)
|
||||
#define luaM_malloc(t) luaM_realloc(NULL, (t))
|
||||
#define luaM_new(t) ((t *)luaM_malloc(sizeof(t)))
|
||||
#define luaM_newvector(n,t) ((t *)luaM_malloc((n)*sizeof(t)))
|
||||
#define luaM_growvector(old,n,t,e,l) \
|
||||
(luaM_growaux((void**)old,n,sizeof(t),e,l))
|
||||
#define luaM_reallocvector(v,n,t) ((t *)luaM_realloc(v,(n)*sizeof(t)))
|
||||
|
||||
|
||||
#ifdef DEBUG
|
||||
extern unsigned long numblocks;
|
||||
extern unsigned long totalmem;
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
94
lobject.c
94
lobject.c
@@ -1,94 +0,0 @@
|
||||
/*
|
||||
** $Id: lobject.c,v 1.9 1997/12/26 18:38:16 roberto Exp roberto $
|
||||
** Some generic functions over Lua objects
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
char *luaO_typenames[] = { /* ORDER LUA_T */
|
||||
"userdata", "number", "string", "table", "function", "function",
|
||||
"nil", "function", "mark", "mark", "mark", "line", NULL
|
||||
};
|
||||
|
||||
|
||||
TObject luaO_nilobject = {LUA_T_NIL, {NULL}};
|
||||
|
||||
|
||||
|
||||
/* hash dimensions values */
|
||||
static long dimensions[] =
|
||||
{5L, 11L, 23L, 47L, 97L, 197L, 397L, 797L, 1597L, 3203L, 6421L,
|
||||
12853L, 25717L, 51437L, 102811L, 205619L, 411233L, 822433L,
|
||||
1644817L, 3289613L, 6579211L, 13158023L, MAX_INT};
|
||||
|
||||
|
||||
int luaO_redimension (int oldsize)
|
||||
{
|
||||
int i;
|
||||
for (i=0; dimensions[i]<MAX_INT; i++) {
|
||||
if (dimensions[i] > oldsize)
|
||||
return dimensions[i];
|
||||
}
|
||||
lua_error("table overflow");
|
||||
return 0; /* to avoid warnings */
|
||||
}
|
||||
|
||||
|
||||
int luaO_equalObj (TObject *t1, TObject *t2)
|
||||
{
|
||||
if (ttype(t1) != ttype(t2)) return 0;
|
||||
switch (ttype(t1)) {
|
||||
case LUA_T_NIL: return 1;
|
||||
case LUA_T_NUMBER: return nvalue(t1) == nvalue(t2);
|
||||
case LUA_T_STRING: case LUA_T_USERDATA: return svalue(t1) == svalue(t2);
|
||||
case LUA_T_ARRAY: return avalue(t1) == avalue(t2);
|
||||
case LUA_T_PROTO: return tfvalue(t1) == tfvalue(t2);
|
||||
case LUA_T_CPROTO: return fvalue(t1) == fvalue(t2);
|
||||
case LUA_T_CLOSURE: return t1->value.cl == t2->value.cl;
|
||||
default:
|
||||
lua_error("internal error in `lua_equalObj'");
|
||||
return 0; /* UNREACHEABLE */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int luaO_findstring (char *name, char *list[])
|
||||
{
|
||||
int i;
|
||||
for (i=0; list[i]; i++)
|
||||
if (strcmp(list[i], name) == 0)
|
||||
return i;
|
||||
return -1; /* name not found */
|
||||
}
|
||||
|
||||
|
||||
void luaO_insertlist (GCnode *root, GCnode *node)
|
||||
{
|
||||
node->next = root->next;
|
||||
root->next = node;
|
||||
node->marked = 0;
|
||||
}
|
||||
|
||||
#ifdef OLD_ANSI
|
||||
void luaO_memup (void *dest, void *src, int size)
|
||||
{
|
||||
char *d = dest;
|
||||
char *s = src;
|
||||
while (size--) d[size]=s[size];
|
||||
}
|
||||
|
||||
void luaO_memdown (void *dest, void *src, int size)
|
||||
{
|
||||
char *d = dest;
|
||||
char *s = src;
|
||||
int i;
|
||||
for (i=0; i<size; i++) d[i]=s[i];
|
||||
}
|
||||
#endif
|
||||
|
||||
184
lobject.h
184
lobject.h
@@ -1,184 +0,0 @@
|
||||
/*
|
||||
** $Id: lobject.h,v 1.15 1998/01/13 13:27:25 roberto Exp $
|
||||
** Type definitions for Lua objects
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lobject_h
|
||||
#define lobject_h
|
||||
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
/*
|
||||
** "real" is the type "number" of Lua
|
||||
** GREP LUA_NUMBER to change that
|
||||
*/
|
||||
#ifndef real
|
||||
#define real float
|
||||
#endif
|
||||
|
||||
|
||||
#define Byte lua_Byte /* some systems have Byte as a predefined type */
|
||||
typedef unsigned char Byte; /* unsigned 8 bits */
|
||||
|
||||
#define MAX_WORD (65534U) /* maximum value of a word (-2 for safety) */
|
||||
#define MAX_INT (INT_MAX-2) /* maximum value of a int (-2 for safety) */
|
||||
|
||||
typedef unsigned int IntPoint; /* unsigned with same size as a pointer (for hashing) */
|
||||
|
||||
|
||||
/*
|
||||
** Lua TYPES
|
||||
** WARNING: if you change the order of this enumeration,
|
||||
** grep "ORDER LUA_T"
|
||||
*/
|
||||
typedef enum {
|
||||
LUA_T_USERDATA = 0, /* tag default for userdata */
|
||||
LUA_T_NUMBER = -1, /* fixed tag for numbers */
|
||||
LUA_T_STRING = -2, /* fixed tag for strings */
|
||||
LUA_T_ARRAY = -3, /* tag default for tables (or arrays) */
|
||||
LUA_T_PROTO = -4, /* fixed tag for functions */
|
||||
LUA_T_CPROTO = -5, /* fixed tag for Cfunctions */
|
||||
LUA_T_NIL = -6, /* last "pre-defined" tag */
|
||||
LUA_T_CLOSURE = -7,
|
||||
LUA_T_CLMARK = -8, /* mark for closures */
|
||||
LUA_T_PMARK = -9, /* mark for Lua prototypes */
|
||||
LUA_T_CMARK = -10, /* mark for C prototypes */
|
||||
LUA_T_LINE = -11
|
||||
} lua_Type;
|
||||
|
||||
#define NUM_TYPES 11
|
||||
#define NUM_TAGS 7
|
||||
|
||||
|
||||
typedef union {
|
||||
lua_CFunction f; /* LUA_T_CPROTO, LUA_T_CMARK */
|
||||
real n; /* LUA_T_NUMBER */
|
||||
struct TaggedString *ts; /* LUA_T_STRING, LUA_T_USERDATA */
|
||||
struct TProtoFunc *tf; /* LUA_T_PROTO, LUA_T_PMARK */
|
||||
struct Closure *cl; /* LUA_T_CLOSURE, LUA_T_CLMARK */
|
||||
struct Hash *a; /* LUA_T_ARRAY */
|
||||
int i; /* LUA_T_LINE */
|
||||
} Value;
|
||||
|
||||
|
||||
typedef struct TObject {
|
||||
lua_Type ttype;
|
||||
Value value;
|
||||
} TObject;
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** generic header for garbage collector lists
|
||||
*/
|
||||
typedef struct GCnode {
|
||||
struct GCnode *next;
|
||||
int marked;
|
||||
} GCnode;
|
||||
|
||||
|
||||
/*
|
||||
** String headers for string table
|
||||
*/
|
||||
|
||||
typedef struct TaggedString {
|
||||
GCnode head;
|
||||
int constindex; /* hint to reuse constants (= -1 if this is a userdata) */
|
||||
unsigned long hash;
|
||||
union {
|
||||
TObject globalval;
|
||||
struct {
|
||||
int tag;
|
||||
void *v; /* if this is a userdata, here is its value */
|
||||
} d;
|
||||
} u;
|
||||
char str[1]; /* \0 byte already reserved */
|
||||
} TaggedString;
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Function Prototypes
|
||||
*/
|
||||
typedef struct TProtoFunc {
|
||||
GCnode head;
|
||||
struct TObject *consts;
|
||||
int nconsts;
|
||||
Byte *code; /* ends with opcode ENDCODE */
|
||||
int lineDefined;
|
||||
TaggedString *fileName;
|
||||
struct LocVar *locvars; /* ends with line = -1 */
|
||||
} TProtoFunc;
|
||||
|
||||
typedef struct LocVar {
|
||||
TaggedString *varname; /* NULL signals end of scope */
|
||||
int line;
|
||||
} LocVar;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* Macros to access structure members */
|
||||
#define ttype(o) ((o)->ttype)
|
||||
#define nvalue(o) ((o)->value.n)
|
||||
#define svalue(o) ((o)->value.ts->str)
|
||||
#define tsvalue(o) ((o)->value.ts)
|
||||
#define clvalue(o) ((o)->value.cl)
|
||||
#define avalue(o) ((o)->value.a)
|
||||
#define fvalue(o) ((o)->value.f)
|
||||
#define tfvalue(o) ((o)->value.tf)
|
||||
|
||||
#define protovalue(o) ((o)->value.cl->consts)
|
||||
|
||||
|
||||
/*
|
||||
** Closures
|
||||
*/
|
||||
typedef struct Closure {
|
||||
GCnode head;
|
||||
int nelems; /* not included the first one (always the prototype) */
|
||||
TObject consts[1]; /* at least one for prototype */
|
||||
} Closure;
|
||||
|
||||
|
||||
|
||||
typedef struct node {
|
||||
TObject ref;
|
||||
TObject val;
|
||||
} Node;
|
||||
|
||||
typedef struct Hash {
|
||||
GCnode head;
|
||||
Node *node;
|
||||
int nhash;
|
||||
int nuse;
|
||||
int htag;
|
||||
} Hash;
|
||||
|
||||
|
||||
extern char *luaO_typenames[];
|
||||
|
||||
extern TObject luaO_nilobject;
|
||||
|
||||
int luaO_equalObj (TObject *t1, TObject *t2);
|
||||
int luaO_redimension (int oldsize);
|
||||
int luaO_findstring (char *name, char *list[]);
|
||||
void luaO_insertlist (GCnode *root, GCnode *node);
|
||||
|
||||
#ifdef OLD_ANSI
|
||||
void luaO_memup (void *dest, void *src, int size);
|
||||
void luaO_memdown (void *dest, void *src, int size);
|
||||
#else
|
||||
#include <string.h>
|
||||
#define luaO_memup(d,s,n) memmove(d,s,n)
|
||||
#define luaO_memdown(d,s,n) memmove(d,s,n)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
182
lopcodes.h
182
lopcodes.h
@@ -1,182 +0,0 @@
|
||||
/*
|
||||
** $Id: lopcodes.h,v 1.14 1997/12/30 19:08:23 roberto Exp roberto $
|
||||
** Opcodes for Lua virtual machine
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lopcodes_h
|
||||
#define lopcodes_h
|
||||
|
||||
|
||||
/*
|
||||
** NOTICE: variants of the same opcode must be consecutive: First, those
|
||||
** with byte parameter, then with built-in parameters, and last with
|
||||
** word parameter.
|
||||
*/
|
||||
|
||||
|
||||
typedef enum {
|
||||
/* name parm before after side effect
|
||||
-----------------------------------------------------------------------------*/
|
||||
ENDCODE,/* - - - */
|
||||
|
||||
PUSHNIL,/* b - nil_0...nil_b */
|
||||
PUSHNIL0,/* - - nil */
|
||||
|
||||
PUSHNUMBER,/* b - (float)b */
|
||||
PUSHNUMBER0,/* - - 0.0 */
|
||||
PUSHNUMBER1,/* - - 1.0 */
|
||||
PUSHNUMBER2,/* - - 2.0 */
|
||||
PUSHNUMBERW,/* w - (float)w */
|
||||
|
||||
PUSHCONSTANT,/* b - CNST[b] */
|
||||
PUSHCONSTANT0,/*- - CNST[0] */
|
||||
PUSHCONSTANT1,/*- - CNST[1] */
|
||||
PUSHCONSTANT2,/*- - CNST[2] */
|
||||
PUSHCONSTANT3,/*- - CNST[3] */
|
||||
PUSHCONSTANT4,/*- - CNST[4] */
|
||||
PUSHCONSTANT5,/*- - CNST[5] */
|
||||
PUSHCONSTANT6,/*- - CNST[6] */
|
||||
PUSHCONSTANT7,/*- - CNST[7] */
|
||||
PUSHCONSTANTW,/*w - CNST[w] */
|
||||
|
||||
PUSHUPVALUE,/* b - Closure[b] */
|
||||
PUSHUPVALUE0,/* - - Closure[0] */
|
||||
PUSHUPVALUE1,/* - - Closure[1] */
|
||||
|
||||
PUSHLOCAL,/* b - LOC[b] */
|
||||
PUSHLOCAL0,/* - - LOC[0] */
|
||||
PUSHLOCAL1,/* - - LOC[1] */
|
||||
PUSHLOCAL2,/* - - LOC[2] */
|
||||
PUSHLOCAL3,/* - - LOC[3] */
|
||||
PUSHLOCAL4,/* - - LOC[4] */
|
||||
PUSHLOCAL5,/* - - LOC[5] */
|
||||
PUSHLOCAL6,/* - - LOC[6] */
|
||||
PUSHLOCAL7,/* - - LOC[7] */
|
||||
|
||||
GETGLOBAL,/* b - VAR[CNST[b]] */
|
||||
GETGLOBAL0,/* - - VAR[CNST[0]] */
|
||||
GETGLOBAL1,/* - - VAR[CNST[1]] */
|
||||
GETGLOBAL2,/* - - VAR[CNST[2]] */
|
||||
GETGLOBAL3,/* - - VAR[CNST[3]] */
|
||||
GETGLOBAL4,/* - - VAR[CNST[4]] */
|
||||
GETGLOBAL5,/* - - VAR[CNST[5]] */
|
||||
GETGLOBAL6,/* - - VAR[CNST[6]] */
|
||||
GETGLOBAL7,/* - - VAR[CNST[7]] */
|
||||
GETGLOBALW,/* w - VAR[CNST[w]] */
|
||||
|
||||
GETTABLE,/* - i t t[i] */
|
||||
|
||||
GETDOTTED,/* b t t[CONST[b]] */
|
||||
GETDOTTED0,/* - t t[CONST[0]] */
|
||||
GETDOTTED1,/* - t t[CONST[1]] */
|
||||
GETDOTTED2,/* - t t[CONST[2]] */
|
||||
GETDOTTED3,/* - t t[CONST[3]] */
|
||||
GETDOTTED4,/* - t t[CONST[4]] */
|
||||
GETDOTTED5,/* - t t[CONST[5]] */
|
||||
GETDOTTED6,/* - t t[CONST[6]] */
|
||||
GETDOTTED7,/* - t t[CONST[7]] */
|
||||
GETDOTTEDW,/* w t t[CONST[w]] */
|
||||
|
||||
PUSHSELF,/* b t t t[CNST[b]] */
|
||||
PUSHSELF0,/* - t t t[CNST[0]] */
|
||||
PUSHSELF1,/* - t t t[CNST[1]] */
|
||||
PUSHSELF2,/* - t t t[CNST[2]] */
|
||||
PUSHSELF3,/* - t t t[CNST[3]] */
|
||||
PUSHSELF4,/* - t t t[CNST[4]] */
|
||||
PUSHSELF5,/* - t t t[CNST[5]] */
|
||||
PUSHSELF6,/* - t t t[CNST[6]] */
|
||||
PUSHSELF7,/* - t t t[CNST[7]] */
|
||||
PUSHSELFW,/* w t t t[CNST[w]] */
|
||||
|
||||
CREATEARRAY,/* b - newarray(size = b) */
|
||||
CREATEARRAY0,/* - - newarray(size = 0) */
|
||||
CREATEARRAY1,/* - - newarray(size = 1) */
|
||||
CREATEARRAYW,/* w - newarray(size = w) */
|
||||
|
||||
SETLOCAL,/* b x - LOC[b]=x */
|
||||
SETLOCAL0,/* - x - LOC[0]=x */
|
||||
SETLOCAL1,/* - x - LOC[1]=x */
|
||||
SETLOCAL2,/* - x - LOC[2]=x */
|
||||
SETLOCAL3,/* - x - LOC[3]=x */
|
||||
SETLOCAL4,/* - x - LOC[4]=x */
|
||||
SETLOCAL5,/* - x - LOC[5]=x */
|
||||
SETLOCAL6,/* - x - LOC[6]=x */
|
||||
SETLOCAL7,/* - x - LOC[7]=x */
|
||||
|
||||
SETGLOBAL,/* b x - VAR[CNST[b]]=x */
|
||||
SETGLOBAL0,/* - x - VAR[CNST[0]]=x */
|
||||
SETGLOBAL1,/* - x - VAR[CNST[1]]=x */
|
||||
SETGLOBAL2,/* - x - VAR[CNST[2]]=x */
|
||||
SETGLOBAL3,/* - x - VAR[CNST[3]]=x */
|
||||
SETGLOBAL4,/* - x - VAR[CNST[4]]=x */
|
||||
SETGLOBAL5,/* - x - VAR[CNST[5]]=x */
|
||||
SETGLOBAL6,/* - x - VAR[CNST[6]]=x */
|
||||
SETGLOBAL7,/* - x - VAR[CNST[7]]=x */
|
||||
SETGLOBALW,/* w x - VAR[CNST[w]]=x */
|
||||
|
||||
SETTABLE0,/* - v i t - t[i]=v */
|
||||
|
||||
SETTABLE,/* b v a_b...a_1 i t a_b...a_1 i t t[i]=v */
|
||||
|
||||
SETLIST,/* b c v_b...v_1 t - t[i+c*FPF]=v_i */
|
||||
SETLIST0,/* b v_b...v_1 t - t[i]=v_i */
|
||||
SETLISTW,/* w c v_b...v_1 t - t[i+c*FPF]=v_i */
|
||||
|
||||
SETMAP,/* b v_b k_b ...v_0 k_0 t t t[k_i]=v_i */
|
||||
SETMAP0,/* - v_0 k_0 t t t[k_0]=v_0 */
|
||||
|
||||
EQOP,/* - y x (x==y)? 1 : nil */
|
||||
NEQOP,/* - y x (x~=y)? 1 : nil */
|
||||
LTOP,/* - y x (x<y)? 1 : nil */
|
||||
LEOP,/* - y x (x<y)? 1 : nil */
|
||||
GTOP,/* - y x (x>y)? 1 : nil */
|
||||
GEOP,/* - y x (x>=y)? 1 : nil */
|
||||
ADDOP,/* - y x x+y */
|
||||
SUBOP,/* - y x x-y */
|
||||
MULTOP,/* - y x x*y */
|
||||
DIVOP,/* - y x x/y */
|
||||
POWOP,/* - y x x^y */
|
||||
CONCOP,/* - y x x..y */
|
||||
MINUSOP,/* - x -x */
|
||||
NOTOP,/* - x (x==nil)? 1 : nil */
|
||||
|
||||
ONTJMP,/* b x (x!=nil)? x : - (x!=nil)? PC+=b */
|
||||
ONTJMPW,/* w x (x!=nil)? x : - (x!=nil)? PC+=w */
|
||||
ONFJMP,/* b x (x==nil)? x : - (x==nil)? PC+=b */
|
||||
ONFJMPW,/* w x (x==nil)? x : - (x==nil)? PC+=w */
|
||||
JMP,/* b - - PC+=b */
|
||||
JMPW,/* w - - PC+=w */
|
||||
IFFJMP,/* b x - (x==nil)? PC+=b */
|
||||
IFFJMPW,/* w x - (x==nil)? PC+=w */
|
||||
IFTUPJMP,/* b x - (x!=nil)? PC-=b */
|
||||
IFTUPJMPW,/* w x - (x!=nil)? PC-=w */
|
||||
IFFUPJMP,/* b x - (x==nil)? PC-=b */
|
||||
IFFUPJMPW,/* w x - (x==nil)? PC-=w */
|
||||
|
||||
CLOSURE,/* b proto v_b...v_1 c(proto) */
|
||||
CLOSURE0,/* - proto c(proto) */
|
||||
CLOSURE1,/* - proto v_1 c(proto) */
|
||||
|
||||
CALLFUNC,/* b c v_c...v_1 f r_b...r_1 f(v1,...,v_c) */
|
||||
CALLFUNC0,/* b v_b...v_1 f - f(v1,...,v_b) */
|
||||
CALLFUNC1,/* b v_b...v_1 f r_1 f(v1,...,v_b) */
|
||||
|
||||
RETCODE,/* b - - */
|
||||
|
||||
SETLINE,/* b - - LINE=b */
|
||||
SETLINEW,/* w - - LINE=w */
|
||||
|
||||
POP,/* b - - TOP-=(b+1) */
|
||||
POP0,/* - - - TOP-=1 */
|
||||
POP1/* - - - TOP-=2 */
|
||||
|
||||
} OpCode;
|
||||
|
||||
|
||||
#define RFIELDS_PER_FLUSH 32 /* records (SETMAP) */
|
||||
#define LFIELDS_PER_FLUSH 64 /* lists (SETLIST) */
|
||||
|
||||
#define ZEROVARARG 64
|
||||
|
||||
#endif
|
||||
20
lparser.h
20
lparser.h
@@ -1,20 +0,0 @@
|
||||
/*
|
||||
** $Id: lparser.h,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $
|
||||
** Syntax analizer and code generator
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lparser_h
|
||||
#define lparser_h
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
void luaY_codedebugline (int line);
|
||||
TProtoFunc *luaY_parser (ZIO *z);
|
||||
void luaY_error (char *s);
|
||||
void luaY_syntaxerror (char *s, char *token);
|
||||
|
||||
|
||||
#endif
|
||||
78
lstate.c
78
lstate.c
@@ -1,78 +0,0 @@
|
||||
/*
|
||||
** $Id: lstate.c,v 1.4 1997/12/11 14:48:46 roberto Exp roberto $
|
||||
** Global State
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include "lbuiltin.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lgc.h"
|
||||
#include "llex.h"
|
||||
#include "lmem.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
|
||||
|
||||
LState *lua_state = NULL;
|
||||
|
||||
|
||||
void lua_open (void)
|
||||
{
|
||||
if (lua_state) return;
|
||||
lua_state = luaM_new(LState);
|
||||
L->numCblocks = 0;
|
||||
L->Cstack.base = 0;
|
||||
L->Cstack.lua2C = 0;
|
||||
L->Cstack.num = 0;
|
||||
L->errorJmp = NULL;
|
||||
L->rootproto.next = NULL;
|
||||
L->rootproto.marked = 0;
|
||||
L->rootcl.next = NULL;
|
||||
L->rootcl.marked = 0;
|
||||
L->rootglobal.next = NULL;
|
||||
L->rootglobal.marked = 0;
|
||||
L->roottable.next = NULL;
|
||||
L->roottable.marked = 0;
|
||||
L->refArray = NULL;
|
||||
L->refSize = 0;
|
||||
L->Mbuffsize = 0;
|
||||
L->Mbuffnext = 0;
|
||||
L->Mbuffbase = NULL;
|
||||
L->Mbuffer = NULL;
|
||||
L->GCthreshold = GARBAGE_BLOCK;
|
||||
L->nblocks = 0;
|
||||
luaD_init();
|
||||
luaS_init();
|
||||
luaX_init();
|
||||
luaT_init();
|
||||
luaB_predefine();
|
||||
}
|
||||
|
||||
|
||||
void lua_close (void)
|
||||
{
|
||||
TaggedString *alludata = luaS_collectudata();
|
||||
L->GCthreshold = MAX_INT; /* to avoid GC during GC */
|
||||
luaC_hashcallIM((Hash *)L->roottable.next); /* GC t.methods for tables */
|
||||
luaC_strcallIM(alludata); /* GC tag methods for userdata */
|
||||
luaD_gcIM(&luaO_nilobject); /* GC tag method for nil (signal end of GC) */
|
||||
luaH_free((Hash *)L->roottable.next);
|
||||
luaF_freeproto((TProtoFunc *)L->rootproto.next);
|
||||
luaF_freeclosure((Closure *)L->rootcl.next);
|
||||
luaS_free(alludata);
|
||||
luaS_freeall();
|
||||
luaM_free(L->stack.stack);
|
||||
luaM_free(L->IMtable);
|
||||
luaM_free(L->refArray);
|
||||
luaM_free(L->Mbuffer);
|
||||
luaM_free(L);
|
||||
L = NULL;
|
||||
#ifdef DEBUG
|
||||
printf("total de blocos: %ld\n", numblocks);
|
||||
printf("total de memoria: %ld\n", totalmem);
|
||||
#endif
|
||||
}
|
||||
81
lstate.h
81
lstate.h
@@ -1,81 +0,0 @@
|
||||
/*
|
||||
** $Id: lstate.h,v 1.6 1997/12/17 20:48:58 roberto Exp roberto $
|
||||
** Global State
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lstate_h
|
||||
#define lstate_h
|
||||
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
#define MAX_C_BLOCKS 10
|
||||
|
||||
#define GARBAGE_BLOCK 150
|
||||
|
||||
|
||||
typedef int StkId; /* index to stack elements */
|
||||
|
||||
struct Stack {
|
||||
TObject *top;
|
||||
TObject *stack;
|
||||
TObject *last;
|
||||
};
|
||||
|
||||
struct C_Lua_Stack {
|
||||
StkId base; /* when Lua calls C or C calls Lua, points to */
|
||||
/* the first slot after the last parameter. */
|
||||
StkId lua2C; /* points to first element of "array" lua2C */
|
||||
int num; /* size of "array" lua2C */
|
||||
};
|
||||
|
||||
|
||||
typedef struct {
|
||||
int size;
|
||||
int nuse; /* number of elements (including EMPTYs) */
|
||||
TaggedString **hash;
|
||||
} stringtable;
|
||||
|
||||
|
||||
struct ref {
|
||||
TObject o;
|
||||
enum {LOCK, HOLD, FREE, COLLECTED} status;
|
||||
};
|
||||
|
||||
|
||||
typedef struct LState {
|
||||
struct Stack stack; /* Lua stack */
|
||||
struct C_Lua_Stack Cstack; /* C2lua struct */
|
||||
void *errorJmp; /* current error recover point */
|
||||
TObject errorim; /* error tag method */
|
||||
GCnode rootproto; /* list of all prototypes */
|
||||
GCnode rootcl; /* list of all closures */
|
||||
GCnode roottable; /* list of all tables */
|
||||
GCnode rootglobal; /* list of strings with global values */
|
||||
stringtable *string_root; /* array of hash tables for strings and udata */
|
||||
struct IM *IMtable; /* table for tag methods */
|
||||
int IMtable_size; /* size of IMtable */
|
||||
int last_tag; /* last used tag in IMtable */
|
||||
struct FuncState *mainState, *currState; /* point to local structs in yacc */
|
||||
struct LexState *lexstate; /* point to local struct in yacc */
|
||||
struct ref *refArray; /* locked objects */
|
||||
int refSize; /* size of refArray */
|
||||
unsigned long GCthreshold;
|
||||
unsigned long nblocks; /* number of 'blocks' currently allocated */
|
||||
char *Mbuffer; /* global buffer */
|
||||
char *Mbuffbase; /* current first position of Mbuffer */
|
||||
int Mbuffsize; /* size of Mbuffer */
|
||||
int Mbuffnext; /* next position to fill in Mbuffer */
|
||||
struct C_Lua_Stack Cblocks[MAX_C_BLOCKS];
|
||||
int numCblocks; /* number of nested Cblocks */
|
||||
} LState;
|
||||
|
||||
|
||||
extern LState *lua_state;
|
||||
|
||||
|
||||
#define L lua_state
|
||||
|
||||
|
||||
#endif
|
||||
262
lstring.c
262
lstring.c
@@ -1,262 +0,0 @@
|
||||
/*
|
||||
** $Id: lstring.c,v 1.9 1997/12/30 19:15:52 roberto Exp roberto $
|
||||
** String table (keeps all strings handled by Lua)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
#define NUM_HASHS 61
|
||||
|
||||
|
||||
#define gcsizestring(l) (1+(l/64)) /* "weight" for a string with length 'l' */
|
||||
|
||||
|
||||
|
||||
static TaggedString EMPTY = {{NULL, 2}, 0, 0L, {{LUA_T_NIL, {NULL}}}, {0}};
|
||||
|
||||
|
||||
void luaS_init (void)
|
||||
{
|
||||
int i;
|
||||
L->string_root = luaM_newvector(NUM_HASHS, stringtable);
|
||||
for (i=0; i<NUM_HASHS; i++) {
|
||||
L->string_root[i].size = 0;
|
||||
L->string_root[i].nuse = 0;
|
||||
L->string_root[i].hash = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static unsigned long hash (char *s, int tag)
|
||||
{
|
||||
unsigned long h;
|
||||
if (tag != LUA_T_STRING)
|
||||
h = (unsigned long)s;
|
||||
else {
|
||||
h = 0;
|
||||
while (*s)
|
||||
h = ((h<<5)-h)^(unsigned char)*(s++);
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
|
||||
static void grow (stringtable *tb)
|
||||
{
|
||||
int newsize = luaO_redimension(tb->size);
|
||||
TaggedString **newhash = luaM_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++;
|
||||
}
|
||||
}
|
||||
luaM_free(tb->hash);
|
||||
tb->size = newsize;
|
||||
tb->hash = newhash;
|
||||
}
|
||||
|
||||
|
||||
static TaggedString *newone (char *buff, int tag, unsigned long h)
|
||||
{
|
||||
TaggedString *ts;
|
||||
if (tag == LUA_T_STRING) {
|
||||
long l = strlen(buff);
|
||||
ts = (TaggedString *)luaM_malloc(sizeof(TaggedString)+l);
|
||||
strcpy(ts->str, buff);
|
||||
ts->u.globalval.ttype = LUA_T_NIL; /* initialize global value */
|
||||
ts->constindex = 0;
|
||||
L->nblocks += gcsizestring(l);
|
||||
}
|
||||
else {
|
||||
ts = (TaggedString *)luaM_malloc(sizeof(TaggedString));
|
||||
ts->u.d.v = buff;
|
||||
ts->u.d.tag = tag == LUA_ANYTAG ? 0 : tag;
|
||||
ts->constindex = -1; /* tag -> this is a userdata */
|
||||
L->nblocks++;
|
||||
}
|
||||
ts->head.marked = 0;
|
||||
ts->head.next = (GCnode *)ts; /* signal it is in no list */
|
||||
ts->hash = h;
|
||||
return ts;
|
||||
}
|
||||
|
||||
static TaggedString *insert (char *buff, int tag, stringtable *tb)
|
||||
{
|
||||
TaggedString *ts;
|
||||
unsigned long h = hash(buff, tag);
|
||||
int size = tb->size;
|
||||
int i;
|
||||
int j = -1;
|
||||
if ((long)tb->nuse*3 >= (long)size*2) {
|
||||
grow(tb);
|
||||
size = tb->size;
|
||||
}
|
||||
for (i = h%size; (ts = tb->hash[i]) != NULL; ) {
|
||||
if (ts == &EMPTY)
|
||||
j = i;
|
||||
else if ((ts->constindex >= 0) ? /* is a string? */
|
||||
(tag == LUA_T_STRING && (strcmp(buff, ts->str) == 0)) :
|
||||
((tag == ts->u.d.tag || tag == LUA_ANYTAG) && buff == ts->u.d.v))
|
||||
return ts;
|
||||
if (++i == size) i=0;
|
||||
}
|
||||
/* not found */
|
||||
if (j != -1) /* is there an EMPTY space? */
|
||||
i = j;
|
||||
else
|
||||
tb->nuse++;
|
||||
ts = tb->hash[i] = newone(buff, tag, h);
|
||||
return ts;
|
||||
}
|
||||
|
||||
TaggedString *luaS_createudata (void *udata, int tag)
|
||||
{
|
||||
return insert(udata, tag, &L->string_root[(unsigned)udata%NUM_HASHS]);
|
||||
}
|
||||
|
||||
TaggedString *luaS_new (char *str)
|
||||
{
|
||||
return insert(str, LUA_T_STRING, &L->string_root[(unsigned)str[0]%NUM_HASHS]);
|
||||
}
|
||||
|
||||
TaggedString *luaS_newfixedstring (char *str)
|
||||
{
|
||||
TaggedString *ts = luaS_new(str);
|
||||
if (ts->head.marked == 0)
|
||||
ts->head.marked = 2; /* avoid GC */
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
void luaS_free (TaggedString *l)
|
||||
{
|
||||
while (l) {
|
||||
TaggedString *next = (TaggedString *)l->head.next;
|
||||
L->nblocks -= (l->constindex == -1) ? 1 : gcsizestring(strlen(l->str));
|
||||
luaM_free(l);
|
||||
l = next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Garbage collection functions.
|
||||
*/
|
||||
|
||||
static void remove_from_list (GCnode *l)
|
||||
{
|
||||
while (l) {
|
||||
GCnode *next = l->next;
|
||||
while (next && !next->marked)
|
||||
next = l->next = next->next;
|
||||
l = next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TaggedString *luaS_collector (void)
|
||||
{
|
||||
TaggedString *frees = NULL;
|
||||
int i;
|
||||
remove_from_list(&(L->rootglobal));
|
||||
for (i=0; i<NUM_HASHS; i++) {
|
||||
stringtable *tb = &L->string_root[i];
|
||||
int j;
|
||||
for (j=0; j<tb->size; j++) {
|
||||
TaggedString *t = tb->hash[j];
|
||||
if (t == NULL) continue;
|
||||
if (t->head.marked == 1)
|
||||
t->head.marked = 0;
|
||||
else if (!t->head.marked) {
|
||||
t->head.next = (GCnode *)frees;
|
||||
frees = t;
|
||||
tb->hash[j] = &EMPTY;
|
||||
}
|
||||
}
|
||||
}
|
||||
return frees;
|
||||
}
|
||||
|
||||
|
||||
TaggedString *luaS_collectudata (void)
|
||||
{
|
||||
TaggedString *frees = NULL;
|
||||
int i;
|
||||
L->rootglobal.next = NULL; /* empty list of globals */
|
||||
for (i=0; i<NUM_HASHS; i++) {
|
||||
stringtable *tb = &L->string_root[i];
|
||||
int j;
|
||||
for (j=0; j<tb->size; j++) {
|
||||
TaggedString *t = tb->hash[j];
|
||||
if (t == NULL || t == &EMPTY || t->constindex != -1)
|
||||
continue; /* get only user datas */
|
||||
t->head.next = (GCnode *)frees;
|
||||
frees = t;
|
||||
tb->hash[j] = &EMPTY;
|
||||
}
|
||||
}
|
||||
return frees;
|
||||
}
|
||||
|
||||
|
||||
void luaS_freeall (void)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<NUM_HASHS; i++) {
|
||||
stringtable *tb = &L->string_root[i];
|
||||
int j;
|
||||
for (j=0; j<tb->size; j++) {
|
||||
TaggedString *t = tb->hash[j];
|
||||
if (t == &EMPTY) continue;
|
||||
luaM_free(t);
|
||||
}
|
||||
luaM_free(tb->hash);
|
||||
}
|
||||
luaM_free(L->string_root);
|
||||
}
|
||||
|
||||
|
||||
void luaS_rawsetglobal (TaggedString *ts, TObject *newval)
|
||||
{
|
||||
ts->u.globalval = *newval;
|
||||
if (ts->head.next == (GCnode *)ts) { /* is not in list? */
|
||||
ts->head.next = L->rootglobal.next;
|
||||
L->rootglobal.next = (GCnode *)ts;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
char *luaS_travsymbol (int (*fn)(TObject *))
|
||||
{
|
||||
TaggedString *g;
|
||||
for (g=(TaggedString *)L->rootglobal.next; g; g=(TaggedString *)g->head.next)
|
||||
if (fn(&g->u.globalval))
|
||||
return g->str;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
int luaS_globaldefined (char *name)
|
||||
{
|
||||
TaggedString *ts = luaS_new(name);
|
||||
return ts->u.globalval.ttype != LUA_T_NIL;
|
||||
}
|
||||
|
||||
27
lstring.h
27
lstring.h
@@ -1,27 +0,0 @@
|
||||
/*
|
||||
** $Id: lstring.h,v 1.5 1997/11/26 18:53:45 roberto Exp roberto $
|
||||
** String table (keep all strings handled by Lua)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lstring_h
|
||||
#define lstring_h
|
||||
|
||||
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
void luaS_init (void);
|
||||
TaggedString *luaS_createudata (void *udata, int tag);
|
||||
TaggedString *luaS_collector (void);
|
||||
void luaS_free (TaggedString *l);
|
||||
TaggedString *luaS_new (char *str);
|
||||
TaggedString *luaS_newfixedstring (char *str);
|
||||
void luaS_rawsetglobal (TaggedString *ts, TObject *newval);
|
||||
char *luaS_travsymbol (int (*fn)(TObject *));
|
||||
int luaS_globaldefined (char *name);
|
||||
TaggedString *luaS_collectudata (void);
|
||||
void luaS_freeall (void);
|
||||
|
||||
|
||||
#endif
|
||||
509
lstrlib.c
509
lstrlib.c
@@ -1,509 +0,0 @@
|
||||
/*
|
||||
** $Id: lstrlib.c,v 1.6 1998/01/09 14:44:55 roberto Exp $
|
||||
** Standard library for strings and pattern-matching
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lua.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
|
||||
static void addnchar (char *s, int n)
|
||||
{
|
||||
char *b = luaL_openspace(n);
|
||||
strncpy(b, s, n);
|
||||
luaL_addsize(n);
|
||||
}
|
||||
|
||||
|
||||
static void addstr (char *s)
|
||||
{
|
||||
addnchar(s, strlen(s));
|
||||
}
|
||||
|
||||
|
||||
static void str_len (void)
|
||||
{
|
||||
lua_pushnumber(strlen(luaL_check_string(1)));
|
||||
}
|
||||
|
||||
|
||||
static void closeandpush (void)
|
||||
{
|
||||
luaL_addchar(0);
|
||||
lua_pushstring(luaL_buffer());
|
||||
}
|
||||
|
||||
|
||||
static void str_sub (void)
|
||||
{
|
||||
char *s = luaL_check_string(1);
|
||||
long l = strlen(s);
|
||||
long start = (long)luaL_check_number(2);
|
||||
long end = (long)luaL_opt_number(3, -1);
|
||||
if (start < 0) start = l+start+1;
|
||||
if (end < 0) end = l+end+1;
|
||||
if (1 <= start && start <= end && end <= l) {
|
||||
luaL_resetbuffer();
|
||||
addnchar(s+start-1, end-start+1);
|
||||
closeandpush();
|
||||
}
|
||||
else lua_pushstring("");
|
||||
}
|
||||
|
||||
|
||||
static void str_lower (void)
|
||||
{
|
||||
char *s;
|
||||
luaL_resetbuffer();
|
||||
for (s = luaL_check_string(1); *s; s++)
|
||||
luaL_addchar(tolower((unsigned char)*s));
|
||||
closeandpush();
|
||||
}
|
||||
|
||||
|
||||
static void str_upper (void)
|
||||
{
|
||||
char *s;
|
||||
luaL_resetbuffer();
|
||||
for (s = luaL_check_string(1); *s; s++)
|
||||
luaL_addchar(toupper((unsigned char)*s));
|
||||
closeandpush();
|
||||
}
|
||||
|
||||
static void str_rep (void)
|
||||
{
|
||||
char *s = luaL_check_string(1);
|
||||
int n = (int)luaL_check_number(2);
|
||||
luaL_resetbuffer();
|
||||
while (n-- > 0)
|
||||
addstr(s);
|
||||
closeandpush();
|
||||
}
|
||||
|
||||
|
||||
static void str_ascii (void)
|
||||
{
|
||||
char *s = luaL_check_string(1);
|
||||
long pos = (long)luaL_opt_number(2, 1) - 1;
|
||||
luaL_arg_check(0<=pos && pos<strlen(s), 2, "out of range");
|
||||
lua_pushnumber((unsigned char)s[pos]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** =======================================================
|
||||
** PATTERN MATCHING
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
#define MAX_CAPT 9
|
||||
|
||||
struct Capture {
|
||||
int level; /* total number of captures (finished or unfinished) */
|
||||
struct {
|
||||
char *init;
|
||||
int len; /* -1 signals unfinished capture */
|
||||
} capture[MAX_CAPT];
|
||||
};
|
||||
|
||||
|
||||
#define ESC '%'
|
||||
#define SPECIALS "^$*?.([%-"
|
||||
|
||||
|
||||
static void push_captures (struct Capture *cap)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<cap->level; i++) {
|
||||
int l = cap->capture[i].len;
|
||||
char *buff = luaL_openspace(l+1);
|
||||
if (l == -1) lua_error("unfinished capture");
|
||||
strncpy(buff, cap->capture[i].init, l);
|
||||
buff[l] = 0;
|
||||
lua_pushstring(buff);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int check_cap (int l, struct Capture *cap)
|
||||
{
|
||||
l -= '1';
|
||||
if (!(0 <= l && l < cap->level && cap->capture[l].len != -1))
|
||||
lua_error("invalid capture index");
|
||||
return l;
|
||||
}
|
||||
|
||||
|
||||
static int capture_to_close (struct Capture *cap)
|
||||
{
|
||||
int level = cap->level;
|
||||
for (level--; level>=0; level--)
|
||||
if (cap->capture[level].len == -1) return level;
|
||||
lua_error("invalid pattern capture");
|
||||
return 0; /* to avoid warnings */
|
||||
}
|
||||
|
||||
|
||||
static char *bracket_end (char *p)
|
||||
{
|
||||
return (*p == 0) ? NULL : strchr((*p=='^') ? p+2 : p+1, ']');
|
||||
}
|
||||
|
||||
|
||||
static int matchclass (int c, int cl)
|
||||
{
|
||||
int res;
|
||||
if (c == 0) return 0;
|
||||
switch (tolower((unsigned char)cl)) {
|
||||
case 'w' : res = isalnum((unsigned char)c); break;
|
||||
case 'd' : res = isdigit((unsigned char)c); break;
|
||||
case 's' : res = isspace((unsigned char)c); break;
|
||||
case 'a' : res = isalpha((unsigned char)c); break;
|
||||
case 'p' : res = ispunct((unsigned char)c); break;
|
||||
case 'l' : res = islower((unsigned char)c); break;
|
||||
case 'u' : res = isupper((unsigned char)c); break;
|
||||
case 'c' : res = iscntrl((unsigned char)c); break;
|
||||
default: return (cl == c);
|
||||
}
|
||||
return (islower((unsigned char)cl) ? res : !res);
|
||||
}
|
||||
|
||||
|
||||
int luaI_singlematch (int c, char *p, char **ep)
|
||||
{
|
||||
switch (*p) {
|
||||
case '.':
|
||||
*ep = p+1;
|
||||
return (c != 0);
|
||||
case '\0':
|
||||
*ep = p;
|
||||
return 0;
|
||||
case ESC:
|
||||
if (*(++p) == '\0')
|
||||
luaL_verror("incorrect pattern (ends with `%c')", ESC);
|
||||
*ep = p+1;
|
||||
return matchclass(c, *p);
|
||||
case '[': {
|
||||
char *end = bracket_end(p+1);
|
||||
int sig = *(p+1) == '^' ? (p++, 0) : 1;
|
||||
if (end == NULL) lua_error("incorrect pattern (missing `]')");
|
||||
*ep = end+1;
|
||||
if (c == 0) return 0;
|
||||
while (++p < end) {
|
||||
if (*p == ESC) {
|
||||
if (((p+1) < end) && matchclass(c, *++p)) return sig;
|
||||
}
|
||||
else if ((*(p+1) == '-') && (p+2 < end)) {
|
||||
p+=2;
|
||||
if (*(p-2) <= c && c <= *p) return sig;
|
||||
}
|
||||
else if (*p == c) return sig;
|
||||
}
|
||||
return !sig;
|
||||
}
|
||||
default:
|
||||
*ep = p+1;
|
||||
return (*p == c);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static char *matchbalance (char *s, int b, int e)
|
||||
{
|
||||
if (*s != b) return NULL;
|
||||
else {
|
||||
int cont = 1;
|
||||
while (*(++s)) {
|
||||
if (*s == e) {
|
||||
if (--cont == 0) return s+1;
|
||||
}
|
||||
else if (*s == b) cont++;
|
||||
}
|
||||
}
|
||||
return NULL; /* string ends out of balance */
|
||||
}
|
||||
|
||||
|
||||
static char *matchitem (char *s, char *p, struct Capture *cap, char **ep)
|
||||
{
|
||||
if (*p == ESC) {
|
||||
p++;
|
||||
if (isdigit((unsigned char)*p)) { /* capture */
|
||||
int l = check_cap(*p, cap);
|
||||
*ep = p+1;
|
||||
if (strncmp(cap->capture[l].init, s, cap->capture[l].len) == 0)
|
||||
return s+cap->capture[l].len;
|
||||
else return NULL;
|
||||
}
|
||||
else if (*p == 'b') { /* balanced string */
|
||||
p++;
|
||||
if (*p == 0 || *(p+1) == 0)
|
||||
lua_error("unbalanced pattern");
|
||||
*ep = p+2;
|
||||
return matchbalance(s, *p, *(p+1));
|
||||
}
|
||||
else p--; /* and go through */
|
||||
}
|
||||
return (luaI_singlematch(*s, p, ep) ? s+1 : NULL);
|
||||
}
|
||||
|
||||
|
||||
static char *match (char *s, char *p, struct Capture *cap)
|
||||
{
|
||||
init: /* using goto's to optimize tail recursion */
|
||||
switch (*p) {
|
||||
case '(': { /* start capture */
|
||||
char *res;
|
||||
if (cap->level >= MAX_CAPT) lua_error("too many captures");
|
||||
cap->capture[cap->level].init = s;
|
||||
cap->capture[cap->level].len = -1;
|
||||
cap->level++;
|
||||
if ((res=match(s, p+1, cap)) == NULL) /* match failed? */
|
||||
cap->level--; /* undo capture */
|
||||
return res;
|
||||
}
|
||||
case ')': { /* end capture */
|
||||
int l = capture_to_close(cap);
|
||||
char *res;
|
||||
cap->capture[l].len = s - cap->capture[l].init; /* close capture */
|
||||
if ((res = match(s, p+1, cap)) == NULL) /* match failed? */
|
||||
cap->capture[l].len = -1; /* undo capture */
|
||||
return res;
|
||||
}
|
||||
case '\0': case '$': /* (possibly) end of pattern */
|
||||
if (*p == 0 || (*(p+1) == 0 && *s == 0))
|
||||
return s;
|
||||
/* else go through */
|
||||
default: { /* it is a pattern item */
|
||||
char *ep; /* get what is next */
|
||||
char *s1 = matchitem(s, p, cap, &ep);
|
||||
switch (*ep) {
|
||||
case '*': { /* repetition */
|
||||
char *res;
|
||||
if (s1 && (res = match(s1, p, cap)))
|
||||
return res;
|
||||
p=ep+1; goto init; /* else return match(s, ep+1, cap); */
|
||||
}
|
||||
case '?': { /* optional */
|
||||
char *res;
|
||||
if (s1 && (res = match(s1, ep+1, cap)))
|
||||
return res;
|
||||
p=ep+1; goto init; /* else return match(s, ep+1, cap); */
|
||||
}
|
||||
case '-': { /* repetition */
|
||||
char *res;
|
||||
if ((res = match(s, ep+1, cap)) != 0)
|
||||
return res;
|
||||
else if (s1) {
|
||||
s = s1;
|
||||
goto init; /* return match(s1, p, cap); */
|
||||
}
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
default:
|
||||
if (s1) { s=s1; p=ep; goto init; } /* return match(s1, ep, cap); */
|
||||
else return NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void str_find (void)
|
||||
{
|
||||
char *s = luaL_check_string(1);
|
||||
char *p = luaL_check_string(2);
|
||||
long init = (long)luaL_opt_number(3, 1) - 1;
|
||||
luaL_arg_check(0 <= init && init <= strlen(s), 3, "out of range");
|
||||
if (lua_getparam(4) != LUA_NOOBJECT ||
|
||||
strpbrk(p, SPECIALS) == NULL) { /* no special caracters? */
|
||||
char *s2 = strstr(s+init, p);
|
||||
if (s2) {
|
||||
lua_pushnumber(s2-s+1);
|
||||
lua_pushnumber(s2-s+strlen(p));
|
||||
}
|
||||
}
|
||||
else {
|
||||
int anchor = (*p == '^') ? (p++, 1) : 0;
|
||||
char *s1=s+init;
|
||||
do {
|
||||
struct Capture cap;
|
||||
char *res;
|
||||
cap.level = 0;
|
||||
if ((res=match(s1, p, &cap)) != NULL) {
|
||||
lua_pushnumber(s1-s+1); /* start */
|
||||
lua_pushnumber(res-s); /* end */
|
||||
push_captures(&cap);
|
||||
return;
|
||||
}
|
||||
} while (*s1++ && !anchor);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void add_s (lua_Object newp, struct Capture *cap)
|
||||
{
|
||||
if (lua_isstring(newp)) {
|
||||
char *news = lua_getstring(newp);
|
||||
while (*news) {
|
||||
if (*news != ESC || !isdigit((unsigned char)*++news))
|
||||
luaL_addchar(*news++);
|
||||
else {
|
||||
int l = check_cap(*news++, cap);
|
||||
addnchar(cap->capture[l].init, cap->capture[l].len);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (lua_isfunction(newp)) {
|
||||
lua_Object res;
|
||||
int status;
|
||||
int oldbuff;
|
||||
lua_beginblock();
|
||||
push_captures(cap);
|
||||
/* function may use buffer, so save it and create a new one */
|
||||
oldbuff = luaL_newbuffer(0);
|
||||
status = lua_callfunction(newp);
|
||||
/* restore old buffer */
|
||||
luaL_oldbuffer(oldbuff);
|
||||
if (status != 0) {
|
||||
lua_endblock();
|
||||
lua_error(NULL);
|
||||
}
|
||||
res = lua_getresult(1);
|
||||
addstr(lua_isstring(res) ? lua_getstring(res) : "");
|
||||
lua_endblock();
|
||||
}
|
||||
else luaL_arg_check(0, 3, "string or function expected");
|
||||
}
|
||||
|
||||
|
||||
static void str_gsub (void)
|
||||
{
|
||||
char *src = luaL_check_string(1);
|
||||
char *p = luaL_check_string(2);
|
||||
lua_Object newp = lua_getparam(3);
|
||||
int max_s = (int)luaL_opt_number(4, strlen(src)+1);
|
||||
int anchor = (*p == '^') ? (p++, 1) : 0;
|
||||
int n = 0;
|
||||
luaL_resetbuffer();
|
||||
while (n < max_s) {
|
||||
struct Capture cap;
|
||||
char *e;
|
||||
cap.level = 0;
|
||||
e = match(src, p, &cap);
|
||||
if (e) {
|
||||
n++;
|
||||
add_s(newp, &cap);
|
||||
}
|
||||
if (e && e>src) /* non empty match? */
|
||||
src = e; /* skip it */
|
||||
else if (*src)
|
||||
luaL_addchar(*src++);
|
||||
else break;
|
||||
if (anchor) break;
|
||||
}
|
||||
addstr(src);
|
||||
closeandpush();
|
||||
lua_pushnumber(n); /* number of substitutions */
|
||||
}
|
||||
|
||||
|
||||
static void luaI_addquoted (char *s)
|
||||
{
|
||||
luaL_addchar('"');
|
||||
for (; *s; s++) {
|
||||
if (strchr("\"\\\n", *s))
|
||||
luaL_addchar('\\');
|
||||
luaL_addchar(*s);
|
||||
}
|
||||
luaL_addchar('"');
|
||||
}
|
||||
|
||||
#define MAX_FORMAT 200
|
||||
|
||||
static void str_format (void)
|
||||
{
|
||||
int arg = 1;
|
||||
char *strfrmt = luaL_check_string(arg);
|
||||
luaL_resetbuffer();
|
||||
while (*strfrmt) {
|
||||
if (*strfrmt != '%')
|
||||
luaL_addchar(*strfrmt++);
|
||||
else if (*++strfrmt == '%')
|
||||
luaL_addchar(*strfrmt++); /* %% */
|
||||
else { /* format item */
|
||||
char form[MAX_FORMAT]; /* store the format ('%...') */
|
||||
struct Capture cap;
|
||||
char *buff;
|
||||
char *initf = strfrmt;
|
||||
form[0] = '%';
|
||||
cap.level = 0;
|
||||
strfrmt = match(strfrmt, "%d?%$?[-+ #]*(%d*)%.?(%d*)", &cap);
|
||||
if (cap.capture[0].len > 3 || cap.capture[1].len > 3) /* < 1000? */
|
||||
lua_error("invalid format (width or precision too long)");
|
||||
if (isdigit((unsigned char)initf[0]) && initf[1] == '$') {
|
||||
arg = initf[0] - '0';
|
||||
initf += 2; /* skip the 'n$' */
|
||||
}
|
||||
arg++;
|
||||
strncpy(form+1, initf, strfrmt-initf+1); /* +1 to include convertion */
|
||||
form[strfrmt-initf+2] = 0;
|
||||
buff = luaL_openspace(1000); /* to store the formatted value */
|
||||
switch (*strfrmt++) {
|
||||
case 'q':
|
||||
luaI_addquoted(luaL_check_string(arg));
|
||||
continue;
|
||||
case 's': {
|
||||
char *s = luaL_check_string(arg);
|
||||
buff = luaL_openspace(strlen(s));
|
||||
sprintf(buff, form, s);
|
||||
break;
|
||||
}
|
||||
case 'c': case 'd': case 'i': case 'o':
|
||||
case 'u': case 'x': case 'X':
|
||||
sprintf(buff, form, (int)luaL_check_number(arg));
|
||||
break;
|
||||
case 'e': case 'E': case 'f': case 'g': case 'G':
|
||||
sprintf(buff, form, luaL_check_number(arg));
|
||||
break;
|
||||
default: /* also treat cases 'pnLlh' */
|
||||
lua_error("invalid option in `format'");
|
||||
}
|
||||
luaL_addsize(strlen(buff));
|
||||
}
|
||||
}
|
||||
closeandpush(); /* push the result */
|
||||
}
|
||||
|
||||
|
||||
static struct luaL_reg strlib[] = {
|
||||
{"strlen", str_len},
|
||||
{"strsub", str_sub},
|
||||
{"strlower", str_lower},
|
||||
{"strupper", str_upper},
|
||||
{"strrep", str_rep},
|
||||
{"ascii", str_ascii},
|
||||
{"format", str_format},
|
||||
{"strfind", str_find},
|
||||
{"gsub", str_gsub}
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
** Open string library
|
||||
*/
|
||||
void strlib_open (void)
|
||||
{
|
||||
luaL_openlib(strlib, (sizeof(strlib)/sizeof(strlib[0])));
|
||||
}
|
||||
216
ltable.c
216
ltable.c
@@ -1,216 +0,0 @@
|
||||
/*
|
||||
** $Id: ltable.c,v 1.10 1998/01/09 14:44:55 roberto Exp roberto $
|
||||
** Lua tables (hash)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "ltable.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
#define gcsize(n) (1+(n/16))
|
||||
|
||||
#define nuse(t) ((t)->nuse)
|
||||
#define nodevector(t) ((t)->node)
|
||||
|
||||
|
||||
#define REHASH_LIMIT 0.70 /* avoid more than this % full */
|
||||
|
||||
#define TagDefault LUA_T_ARRAY;
|
||||
|
||||
|
||||
|
||||
static long int hashindex (TObject *ref)
|
||||
{
|
||||
long int h;
|
||||
switch (ttype(ref)) {
|
||||
case LUA_T_NUMBER:
|
||||
h = (long int)nvalue(ref);
|
||||
break;
|
||||
case LUA_T_STRING: case LUA_T_USERDATA:
|
||||
h = (IntPoint)tsvalue(ref);
|
||||
break;
|
||||
case LUA_T_ARRAY:
|
||||
h = (IntPoint)avalue(ref);
|
||||
break;
|
||||
case LUA_T_PROTO:
|
||||
h = (IntPoint)tfvalue(ref);
|
||||
break;
|
||||
case LUA_T_CPROTO:
|
||||
h = (IntPoint)fvalue(ref);
|
||||
break;
|
||||
case LUA_T_CLOSURE:
|
||||
h = (IntPoint)clvalue(ref);
|
||||
break;
|
||||
default:
|
||||
lua_error("unexpected type to index table");
|
||||
h = 0; /* to avoid warnings */
|
||||
}
|
||||
return (h >= 0 ? h : -(h+1));
|
||||
}
|
||||
|
||||
|
||||
static int present (Hash *t, TObject *key)
|
||||
{
|
||||
int tsize = nhash(t);
|
||||
long int h = hashindex(key);
|
||||
int h1 = h%tsize;
|
||||
TObject *rf = ref(node(t, h1));
|
||||
if (ttype(rf) != LUA_T_NIL && !luaO_equalObj(key, rf)) {
|
||||
int h2 = h%(tsize-2) + 1;
|
||||
do {
|
||||
h1 += h2;
|
||||
if (h1 >= tsize) h1 -= tsize;
|
||||
rf = ref(node(t, h1));
|
||||
} while (ttype(rf) != LUA_T_NIL && !luaO_equalObj(key, rf));
|
||||
}
|
||||
return h1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Alloc a vector node
|
||||
*/
|
||||
static Node *hashnodecreate (int nhash)
|
||||
{
|
||||
Node *v = luaM_newvector(nhash, Node);
|
||||
int i;
|
||||
for (i=0; i<nhash; i++)
|
||||
ttype(ref(&v[i])) = LUA_T_NIL;
|
||||
return v;
|
||||
}
|
||||
|
||||
/*
|
||||
** Delete a hash
|
||||
*/
|
||||
static void hashdelete (Hash *t)
|
||||
{
|
||||
luaM_free(nodevector(t));
|
||||
luaM_free(t);
|
||||
}
|
||||
|
||||
|
||||
void luaH_free (Hash *frees)
|
||||
{
|
||||
while (frees) {
|
||||
Hash *next = (Hash *)frees->head.next;
|
||||
L->nblocks -= gcsize(frees->nhash);
|
||||
hashdelete(frees);
|
||||
frees = next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Hash *luaH_new (int nhash)
|
||||
{
|
||||
Hash *t = luaM_new(Hash);
|
||||
nhash = luaO_redimension((int)((float)nhash/REHASH_LIMIT));
|
||||
nodevector(t) = hashnodecreate(nhash);
|
||||
nhash(t) = nhash;
|
||||
nuse(t) = 0;
|
||||
t->htag = TagDefault;
|
||||
luaO_insertlist(&(L->roottable), (GCnode *)t);
|
||||
L->nblocks += gcsize(nhash);
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Rehash:
|
||||
** Check if table has deleted slots. It it has, it does not need to
|
||||
** grow, since rehash will reuse them.
|
||||
*/
|
||||
static int emptyslots (Hash *t)
|
||||
{
|
||||
int i;
|
||||
for (i=nhash(t)-1; i>=0; i--) {
|
||||
Node *n = node(t, i);
|
||||
if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) == LUA_T_NIL)
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void rehash (Hash *t)
|
||||
{
|
||||
int nold = nhash(t);
|
||||
Node *vold = nodevector(t);
|
||||
int i;
|
||||
if (!emptyslots(t))
|
||||
nhash(t) = luaO_redimension(nhash(t));
|
||||
nodevector(t) = hashnodecreate(nhash(t));
|
||||
for (i=0; i<nold; i++) {
|
||||
Node *n = vold+i;
|
||||
if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL)
|
||||
*node(t, present(t, ref(n))) = *n; /* copy old node to luaM_new hash */
|
||||
}
|
||||
L->nblocks += gcsize(t->nhash)-gcsize(nold);
|
||||
luaM_free(vold);
|
||||
}
|
||||
|
||||
/*
|
||||
** If the hash node is present, return its pointer, otherwise return
|
||||
** null.
|
||||
*/
|
||||
TObject *luaH_get (Hash *t, TObject *ref)
|
||||
{
|
||||
int h = present(t, ref);
|
||||
if (ttype(ref(node(t, h))) != LUA_T_NIL) return val(node(t, h));
|
||||
else return NULL;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** If the hash node is present, return its pointer, otherwise create a luaM_new
|
||||
** node for the given reference and also return its pointer.
|
||||
*/
|
||||
TObject *luaH_set (Hash *t, TObject *ref)
|
||||
{
|
||||
Node *n = node(t, present(t, ref));
|
||||
if (ttype(ref(n)) == LUA_T_NIL) {
|
||||
nuse(t)++;
|
||||
if ((float)nuse(t) > (float)nhash(t)*REHASH_LIMIT) {
|
||||
rehash(t);
|
||||
n = node(t, present(t, ref));
|
||||
}
|
||||
*ref(n) = *ref;
|
||||
ttype(val(n)) = LUA_T_NIL;
|
||||
}
|
||||
return (val(n));
|
||||
}
|
||||
|
||||
|
||||
static Node *hashnext (Hash *t, int i)
|
||||
{
|
||||
Node *n;
|
||||
int tsize = nhash(t);
|
||||
if (i >= tsize)
|
||||
return NULL;
|
||||
n = node(t, i);
|
||||
while (ttype(ref(n)) == LUA_T_NIL || ttype(val(n)) == LUA_T_NIL) {
|
||||
if (++i >= tsize)
|
||||
return NULL;
|
||||
n = node(t, i);
|
||||
}
|
||||
return node(t, i);
|
||||
}
|
||||
|
||||
Node *luaH_next (TObject *o, TObject *r)
|
||||
{
|
||||
Hash *t = avalue(o);
|
||||
if (ttype(r) == LUA_T_NIL)
|
||||
return hashnext(t, 0);
|
||||
else {
|
||||
int i = present(t, r);
|
||||
Node *n = node(t, i);
|
||||
luaL_arg_check(ttype(ref(n))!=LUA_T_NIL && ttype(val(n))!=LUA_T_NIL,
|
||||
2, "key not found");
|
||||
return hashnext(t, i+1);
|
||||
}
|
||||
}
|
||||
24
ltable.h
24
ltable.h
@@ -1,24 +0,0 @@
|
||||
/*
|
||||
** $Id: ltable.h,v 1.4 1997/11/19 17:29:23 roberto Exp roberto $
|
||||
** Lua tables (hash)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef ltable_h
|
||||
#define ltable_h
|
||||
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
#define node(t,i) (&(t)->node[i])
|
||||
#define ref(n) (&(n)->ref)
|
||||
#define val(n) (&(n)->val)
|
||||
#define nhash(t) ((t)->nhash)
|
||||
|
||||
Hash *luaH_new (int nhash);
|
||||
void luaH_free (Hash *frees);
|
||||
TObject *luaH_get (Hash *t, TObject *ref);
|
||||
TObject *luaH_set (Hash *t, TObject *ref);
|
||||
Node *luaH_next (TObject *o, TObject *r);
|
||||
|
||||
#endif
|
||||
263
ltm.c
263
ltm.c
@@ -1,263 +0,0 @@
|
||||
/*
|
||||
** $Id: ltm.c,v 1.12 1997/12/15 16:17:20 roberto Exp roberto $
|
||||
** Tag methods
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
#include "ltm.h"
|
||||
|
||||
|
||||
char *luaT_eventname[] = { /* ORDER IM */
|
||||
"gettable", "settable", "index", "getglobal", "setglobal", "add",
|
||||
"sub", "mul", "div", "pow", "unm", "lt", "le", "gt", "ge",
|
||||
"concat", "gc", "function", NULL
|
||||
};
|
||||
|
||||
|
||||
static int luaI_checkevent (char *name, char *list[])
|
||||
{
|
||||
int e = luaO_findstring(name, list);
|
||||
if (e < 0)
|
||||
luaL_verror("`%.50s' is not a valid event name", name);
|
||||
return e;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* events in LUA_T_NIL are all allowed, since this is used as a
|
||||
* 'placeholder' for "default" fallbacks
|
||||
*/
|
||||
static char validevents[NUM_TAGS][IM_N] = { /* ORDER LUA_T, ORDER IM */
|
||||
{1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_T_USERDATA */
|
||||
{1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_NUMBER */
|
||||
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_STRING */
|
||||
{0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_ARRAY */
|
||||
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_PROTO */
|
||||
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_CPROTO */
|
||||
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} /* LUA_T_NIL */
|
||||
};
|
||||
|
||||
|
||||
static int validevent (int t, int e)
|
||||
{ /* ORDER LUA_T */
|
||||
return (t < LUA_T_NIL) ? 1 : validevents[-t][e];
|
||||
}
|
||||
|
||||
|
||||
static void init_entry (int tag)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<IM_N; i++)
|
||||
ttype(luaT_getim(tag, i)) = LUA_T_NIL;
|
||||
}
|
||||
|
||||
|
||||
void luaT_init (void)
|
||||
{
|
||||
int t;
|
||||
L->IMtable_size = NUM_TAGS*2;
|
||||
L->last_tag = -(NUM_TAGS-1);
|
||||
L->IMtable = luaM_newvector(L->IMtable_size, struct IM);
|
||||
for (t=L->last_tag; t<=0; t++)
|
||||
init_entry(t);
|
||||
}
|
||||
|
||||
|
||||
int lua_newtag (void)
|
||||
{
|
||||
--L->last_tag;
|
||||
if ((-L->last_tag) >= L->IMtable_size)
|
||||
L->IMtable_size = luaM_growvector(&L->IMtable, L->IMtable_size,
|
||||
struct IM, memEM, MAX_INT);
|
||||
init_entry(L->last_tag);
|
||||
return L->last_tag;
|
||||
}
|
||||
|
||||
|
||||
static void checktag (int tag)
|
||||
{
|
||||
if (!(L->last_tag <= tag && tag <= 0))
|
||||
luaL_verror("%d is not a valid tag", tag);
|
||||
}
|
||||
|
||||
void luaT_realtag (int tag)
|
||||
{
|
||||
if (!(L->last_tag <= tag && tag < LUA_T_NIL))
|
||||
luaL_verror("tag %d is not result of `newtag'", tag);
|
||||
}
|
||||
|
||||
|
||||
int lua_copytagmethods (int tagto, int tagfrom)
|
||||
{
|
||||
int e;
|
||||
checktag(tagto);
|
||||
checktag(tagfrom);
|
||||
for (e=0; e<IM_N; e++) {
|
||||
if (validevent(tagto, e))
|
||||
*luaT_getim(tagto, e) = *luaT_getim(tagfrom, e);
|
||||
}
|
||||
return tagto;
|
||||
}
|
||||
|
||||
|
||||
int luaT_efectivetag (TObject *o)
|
||||
{
|
||||
int t;
|
||||
switch (t = ttype(o)) {
|
||||
case LUA_T_ARRAY:
|
||||
return o->value.a->htag;
|
||||
case LUA_T_USERDATA: {
|
||||
int tag = o->value.ts->u.d.tag;
|
||||
return (tag >= 0) ? LUA_T_USERDATA : tag;
|
||||
}
|
||||
case LUA_T_CLOSURE:
|
||||
return o->value.cl->consts[0].ttype;
|
||||
#ifdef DEBUG
|
||||
case LUA_T_PMARK: case LUA_T_CMARK:
|
||||
case LUA_T_CLMARK: case LUA_T_LINE:
|
||||
lua_error("internal error");
|
||||
#endif
|
||||
default:
|
||||
return t;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TObject *luaT_gettagmethod (int t, char *event)
|
||||
{
|
||||
int e = luaI_checkevent(event, luaT_eventname);
|
||||
checktag(t);
|
||||
if (validevent(t, e))
|
||||
return luaT_getim(t,e);
|
||||
else
|
||||
return &luaO_nilobject;
|
||||
}
|
||||
|
||||
|
||||
void luaT_settagmethod (int t, char *event, TObject *func)
|
||||
{
|
||||
TObject temp = *func;
|
||||
int e = luaI_checkevent(event, luaT_eventname);
|
||||
checktag(t);
|
||||
if (!validevent(t, e))
|
||||
luaL_verror("settagmethod: cannot change internal method `%.20s' for tag %d",
|
||||
luaT_eventname[e], t);
|
||||
*func = *luaT_getim(t,e);
|
||||
*luaT_getim(t, e) = temp;
|
||||
}
|
||||
|
||||
|
||||
char *luaT_travtagmethods (int (*fn)(TObject *))
|
||||
{
|
||||
int e;
|
||||
if (fn(&L->errorim))
|
||||
return "error";
|
||||
for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) { /* ORDER IM */
|
||||
int t;
|
||||
for (t=0; t>=L->last_tag; t--)
|
||||
if (fn(luaT_getim(t,e)))
|
||||
return luaT_eventname[e];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* ===================================================================
|
||||
* compatibility with old fallback system
|
||||
*/
|
||||
#ifdef LUA_COMPAT2_5
|
||||
|
||||
#include "lapi.h"
|
||||
|
||||
static void errorFB (void)
|
||||
{
|
||||
lua_Object o = lua_getparam(1);
|
||||
if (lua_isstring(o))
|
||||
fprintf(stderr, "lua: %s\n", lua_getstring(o));
|
||||
else
|
||||
fprintf(stderr, "lua: unknown error\n");
|
||||
}
|
||||
|
||||
|
||||
static void nilFB (void) { }
|
||||
|
||||
|
||||
static void typeFB (void)
|
||||
{
|
||||
lua_error("unexpected type");
|
||||
}
|
||||
|
||||
|
||||
static void fillvalids (IMS e, TObject *func)
|
||||
{
|
||||
int t;
|
||||
for (t=LUA_T_NIL; t<=LUA_T_USERDATA; t++)
|
||||
if (validevent(t, e))
|
||||
*luaT_getim(t, e) = *func;
|
||||
}
|
||||
|
||||
|
||||
void luaT_setfallback (void)
|
||||
{
|
||||
static char *oldnames [] = {"error", "getglobal", "arith", "order", NULL};
|
||||
TObject oldfunc;
|
||||
lua_CFunction replace;
|
||||
char *name = luaL_check_string(1);
|
||||
lua_Object func = lua_getparam(2);
|
||||
luaL_arg_check(lua_isfunction(func), 2, "function expected");
|
||||
switch (luaO_findstring(name, oldnames)) {
|
||||
case 0: /* old error fallback */
|
||||
oldfunc = L->errorim;
|
||||
L->errorim = *luaA_Address(func);
|
||||
replace = errorFB;
|
||||
break;
|
||||
case 1: /* old getglobal fallback */
|
||||
oldfunc = *luaT_getim(LUA_T_NIL, IM_GETGLOBAL);
|
||||
*luaT_getim(LUA_T_NIL, IM_GETGLOBAL) = *luaA_Address(func);
|
||||
replace = nilFB;
|
||||
break;
|
||||
case 2: { /* old arith fallback */
|
||||
int i;
|
||||
oldfunc = *luaT_getim(LUA_T_NUMBER, IM_POW);
|
||||
for (i=IM_ADD; i<=IM_UNM; i++) /* ORDER IM */
|
||||
fillvalids(i, luaA_Address(func));
|
||||
replace = typeFB;
|
||||
break;
|
||||
}
|
||||
case 3: { /* old order fallback */
|
||||
int i;
|
||||
oldfunc = *luaT_getim(LUA_T_NIL, IM_LT);
|
||||
for (i=IM_LT; i<=IM_GE; i++) /* ORDER IM */
|
||||
fillvalids(i, luaA_Address(func));
|
||||
replace = typeFB;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
int e;
|
||||
if ((e = luaO_findstring(name, luaT_eventname)) >= 0) {
|
||||
oldfunc = *luaT_getim(LUA_T_NIL, e);
|
||||
fillvalids(e, luaA_Address(func));
|
||||
replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB;
|
||||
}
|
||||
else {
|
||||
luaL_verror("`%.50s' is not a valid fallback name", name);
|
||||
replace = NULL; /* to avoid warnings */
|
||||
}
|
||||
}
|
||||
}
|
||||
if (oldfunc.ttype != LUA_T_NIL)
|
||||
luaA_pushobject(&oldfunc);
|
||||
else
|
||||
lua_pushcfunction(replace);
|
||||
}
|
||||
#endif
|
||||
|
||||
62
ltm.h
62
ltm.h
@@ -1,62 +0,0 @@
|
||||
/*
|
||||
** $Id: ltm.h,v 1.3 1997/11/19 17:29:23 roberto Exp roberto $
|
||||
** Tag methods
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef ltm_h
|
||||
#define ltm_h
|
||||
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lstate.h"
|
||||
|
||||
/*
|
||||
* WARNING: if you change the order of this enumeration,
|
||||
* grep "ORDER IM"
|
||||
*/
|
||||
typedef enum {
|
||||
IM_GETTABLE = 0,
|
||||
IM_SETTABLE,
|
||||
IM_INDEX,
|
||||
IM_GETGLOBAL,
|
||||
IM_SETGLOBAL,
|
||||
IM_ADD,
|
||||
IM_SUB,
|
||||
IM_MUL,
|
||||
IM_DIV,
|
||||
IM_POW,
|
||||
IM_UNM,
|
||||
IM_LT,
|
||||
IM_LE,
|
||||
IM_GT,
|
||||
IM_GE,
|
||||
IM_CONCAT,
|
||||
IM_GC,
|
||||
IM_FUNCTION
|
||||
} IMS;
|
||||
|
||||
#define IM_N 18
|
||||
|
||||
|
||||
struct IM {
|
||||
TObject int_method[IM_N];
|
||||
};
|
||||
|
||||
|
||||
#define luaT_getim(tag,event) (&L->IMtable[-(tag)].int_method[event])
|
||||
#define luaT_getimbyObj(o,e) (luaT_getim(luaT_efectivetag(o),(e)))
|
||||
|
||||
extern char *luaT_eventname[];
|
||||
|
||||
|
||||
void luaT_init (void);
|
||||
void luaT_realtag (int tag);
|
||||
int luaT_efectivetag (TObject *o);
|
||||
void luaT_settagmethod (int t, char *event, TObject *func);
|
||||
TObject *luaT_gettagmethod (int t, char *event);
|
||||
char *luaT_travtagmethods (int (*fn)(TObject *));
|
||||
|
||||
void luaT_setfallback (void); /* only if LUA_COMPAT2_5 */
|
||||
|
||||
#endif
|
||||
173
lua.c
173
lua.c
@@ -1,159 +1,54 @@
|
||||
/*
|
||||
** $Id: lua.c,v 1.11 1997/12/22 18:05:23 roberto Exp roberto $
|
||||
** Lua stand-alone interpreter
|
||||
** See Copyright Notice in lua.h
|
||||
** lua.c
|
||||
** Linguagem para Usuarios de Aplicacao
|
||||
** TeCGraf - PUC-Rio
|
||||
** 28 Apr 93
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
#ifndef OLD_ANSI
|
||||
#include <locale.h>
|
||||
#else
|
||||
#define setlocale(a,b) 0
|
||||
#endif
|
||||
|
||||
#ifdef _POSIX_SOURCE
|
||||
#include <unistd.h>
|
||||
#else
|
||||
#define isatty(x) (x==0) /* assume stdin is a tty */
|
||||
#endif
|
||||
|
||||
|
||||
static void print_message (void)
|
||||
void test (void)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"Lua: command line options:\n"
|
||||
" -v print version information\n"
|
||||
" -d turn debug on\n"
|
||||
" -e stat dostring `stat'\n"
|
||||
" -q interactive mode without prompt\n"
|
||||
" -i interactive mode with prompt\n"
|
||||
" - executes stdin as a file\n"
|
||||
" a=b sets global `a' with string `b'\n"
|
||||
" name dofile `name'\n\n");
|
||||
lua_pushobject(lua_getparam(1));
|
||||
lua_call ("c", 1);
|
||||
}
|
||||
|
||||
|
||||
static void assign (char *arg)
|
||||
static void callfunc (void)
|
||||
{
|
||||
if (strlen(arg) >= 500)
|
||||
fprintf(stderr, "lua: shell argument too long");
|
||||
else {
|
||||
char buffer[500];
|
||||
char *eq = strchr(arg, '=');
|
||||
lua_pushstring(eq+1);
|
||||
strncpy(buffer, arg, eq-arg);
|
||||
buffer[eq-arg] = 0;
|
||||
lua_setglobal(buffer);
|
||||
}
|
||||
lua_Object obj = lua_getparam (1);
|
||||
if (lua_isstring(obj)) lua_call(lua_getstring(obj),0);
|
||||
}
|
||||
|
||||
#define BUF_SIZE 512
|
||||
|
||||
static void manual_input (int prompt)
|
||||
static void execstr (void)
|
||||
{
|
||||
int cont = 1;
|
||||
while (cont) {
|
||||
char buffer[BUF_SIZE];
|
||||
int i = 0;
|
||||
lua_beginblock();
|
||||
if (prompt)
|
||||
printf("%s", lua_getstring(lua_getglobal("_PROMPT")));
|
||||
for(;;) {
|
||||
int c = getchar();
|
||||
if (c == EOF) {
|
||||
cont = 0;
|
||||
break;
|
||||
}
|
||||
else if (c == '\n') {
|
||||
if (i>0 && buffer[i-1] == '\\')
|
||||
buffer[i-1] = '\n';
|
||||
else break;
|
||||
}
|
||||
else if (i >= BUF_SIZE-1) {
|
||||
fprintf(stderr, "lua: argument line too long\n");
|
||||
break;
|
||||
}
|
||||
else buffer[i++] = c;
|
||||
}
|
||||
buffer[i] = 0;
|
||||
lua_dostring(buffer);
|
||||
lua_endblock();
|
||||
}
|
||||
printf("\n");
|
||||
lua_Object obj = lua_getparam (1);
|
||||
if (lua_isstring(obj)) lua_dostring(lua_getstring(obj));
|
||||
}
|
||||
|
||||
void main (int argc, char *argv[])
|
||||
{
|
||||
int i;
|
||||
if (argc < 2)
|
||||
{
|
||||
puts ("usage: lua filename [functionnames]");
|
||||
return;
|
||||
}
|
||||
lua_register ("callfunc", callfunc);
|
||||
lua_register ("execstr", execstr);
|
||||
lua_register ("test", test);
|
||||
iolib_open ();
|
||||
strlib_open ();
|
||||
mathlib_open ();
|
||||
lua_dofile (argv[1]);
|
||||
for (i=2; i<argc; i++)
|
||||
{
|
||||
lua_call (argv[i],0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
int i;
|
||||
setlocale(LC_ALL, "");
|
||||
lua_iolibopen();
|
||||
lua_strlibopen();
|
||||
lua_mathlibopen();
|
||||
lua_pushstring("> "); lua_setglobal("_PROMPT");
|
||||
if (argc < 2) { /* no arguments? */
|
||||
if (isatty(0)) {
|
||||
printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT);
|
||||
manual_input(1);
|
||||
}
|
||||
else
|
||||
lua_dofile(NULL); /* executes stdin as a file */
|
||||
}
|
||||
else for (i=1; i<argc; i++) {
|
||||
if (argv[i][0] == '-') { /* option? */
|
||||
switch (argv[i][1]) {
|
||||
case 0:
|
||||
lua_dofile(NULL); /* executes stdin as a file */
|
||||
break;
|
||||
case 'i':
|
||||
manual_input(1);
|
||||
break;
|
||||
case 'q':
|
||||
manual_input(0);
|
||||
break;
|
||||
case 'd':
|
||||
lua_debug = 1;
|
||||
break;
|
||||
case 'v':
|
||||
printf("%s %s\n(written by %s)\n\n",
|
||||
LUA_VERSION, LUA_COPYRIGHT, LUA_AUTHORS);
|
||||
break;
|
||||
case 'e':
|
||||
i++;
|
||||
if (lua_dostring(argv[i]) != 0) {
|
||||
fprintf(stderr, "lua: error running argument `%s'\n", argv[i]);
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
print_message();
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
else if (strchr(argv[i], '='))
|
||||
assign(argv[i]);
|
||||
else {
|
||||
int result = lua_dofile(argv[i]);
|
||||
if (result) {
|
||||
if (result == 2) {
|
||||
fprintf(stderr, "lua: cannot execute file ");
|
||||
perror(argv[i]);
|
||||
}
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
#ifdef DEBUG
|
||||
lua_close();
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
190
lua.h
190
lua.h
@@ -1,178 +1,54 @@
|
||||
/*
|
||||
** $Id: lua.h,v 1.13 1998/01/02 17:46:32 roberto Exp roberto $
|
||||
** Lua - An Extensible Extension Language
|
||||
** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
|
||||
** e-mail: lua@tecgraf.puc-rio.br
|
||||
** www: http://www.tecgraf.puc-rio.br/lua/
|
||||
** LUA - Linguagem para Usuarios de Aplicacao
|
||||
** Grupo de Tecnologia em Computacao Grafica
|
||||
** TeCGraf - PUC-Rio
|
||||
** 19 May 93
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* Copyright (c) 1994-1998 TeCGraf, PUC-Rio.
|
||||
* Written by Waldemar Celes Filho, Roberto Ierusalimschy and
|
||||
* Luiz Henrique de Figueiredo.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Permission is hereby granted, without written agreement and with
|
||||
* out license or royalty fees, to use, copy, modify, and distribute
|
||||
* this software and its documentation for any purpose, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall ap
|
||||
* pear in all copies or substantial portions of this software.
|
||||
*
|
||||
* The name "Lua" cannot be used for any modified form of this soft
|
||||
* ware that does not originate from the authors. Nevertheless, the
|
||||
* name "Lua" may and should be used to designate the language im
|
||||
* plemented and described in this package, even if embedded in any
|
||||
* other system, as long as its syntax and semantics remain un
|
||||
* changed.
|
||||
*
|
||||
* The authors specifically disclaim any warranties, including, but
|
||||
* not limited to, the implied warranties of merchantability and
|
||||
* fitness for a particular purpose. The software provided hereunder
|
||||
* is on an "as is" basis, and the authors have no obligation to
|
||||
* provide maintenance, support, updates, enhancements, or modifica
|
||||
* tions. In no event shall TeCGraf, PUC-Rio, or the authors be li
|
||||
* able to any party for direct, indirect, special, incidental, or
|
||||
* consequential damages arising out of the use of this software and
|
||||
* its documentation.
|
||||
*********************************************************************/
|
||||
|
||||
|
||||
|
||||
#ifndef lua_h
|
||||
#define lua_h
|
||||
|
||||
#define LUA_VERSION "Lua 3.1 (alpha)"
|
||||
#define LUA_COPYRIGHT "Copyright (C) 1994-1998 TeCGraf"
|
||||
#define LUA_AUTHORS "W. Celes, R. Ierusalimschy & L. H. de Figueiredo"
|
||||
|
||||
|
||||
#define LUA_NOOBJECT 0
|
||||
|
||||
#define LUA_ANYTAG (-1)
|
||||
|
||||
typedef void (*lua_CFunction) (void);
|
||||
typedef unsigned int lua_Object;
|
||||
typedef struct Object *lua_Object;
|
||||
|
||||
#define lua_register(n,f) (lua_pushcfunction(f), lua_storeglobal(n))
|
||||
|
||||
|
||||
void lua_open (void);
|
||||
void lua_close (void);
|
||||
|
||||
lua_Object lua_settagmethod (int tag, char *event); /* In: new method */
|
||||
lua_Object lua_gettagmethod (int tag, char *event);
|
||||
lua_Object lua_seterrormethod (void); /* In: new method */
|
||||
|
||||
int lua_newtag (void);
|
||||
int lua_copytagmethods (int tagto, int tagfrom);
|
||||
void lua_settag (int tag); /* In: object */
|
||||
|
||||
void lua_errorfunction (void (*fn) (char *s));
|
||||
void lua_error (char *s);
|
||||
int lua_dofile (char *filename); /* Out: returns */
|
||||
int lua_dostring (char *string); /* Out: returns */
|
||||
int lua_callfunction (lua_Object f);
|
||||
/* In: parameters; Out: returns */
|
||||
int lua_dofile (char *filename);
|
||||
int lua_dostring (char *string);
|
||||
int lua_call (char *functionname, int nparam);
|
||||
|
||||
void lua_beginblock (void);
|
||||
void lua_endblock (void);
|
||||
|
||||
lua_Object lua_lua2C (int number);
|
||||
#define lua_getparam(_) lua_lua2C(_)
|
||||
#define lua_getresult(_) lua_lua2C(_)
|
||||
|
||||
int lua_isnil (lua_Object object);
|
||||
int lua_istable (lua_Object object);
|
||||
int lua_isuserdata (lua_Object object);
|
||||
int lua_iscfunction (lua_Object object);
|
||||
int lua_isnumber (lua_Object object);
|
||||
int lua_isstring (lua_Object object);
|
||||
int lua_isfunction (lua_Object object);
|
||||
|
||||
double lua_getnumber (lua_Object object);
|
||||
lua_Object lua_getparam (int number);
|
||||
float lua_getnumber (lua_Object object);
|
||||
char *lua_getstring (lua_Object object);
|
||||
char *lua_copystring (lua_Object object);
|
||||
lua_CFunction lua_getcfunction (lua_Object object);
|
||||
void *lua_getuserdata (lua_Object object);
|
||||
|
||||
|
||||
void lua_pushnil (void);
|
||||
void lua_pushnumber (double n);
|
||||
void lua_pushstring (char *s);
|
||||
void lua_pushCclosure (lua_CFunction fn, int n);
|
||||
void lua_pushusertag (void *u, int tag);
|
||||
void lua_pushobject (lua_Object object);
|
||||
|
||||
lua_Object lua_pop (void);
|
||||
|
||||
void *lua_getuserdata (lua_Object object);
|
||||
lua_Object lua_getfield (lua_Object object, char *field);
|
||||
lua_Object lua_getindexed (lua_Object object, float index);
|
||||
lua_Object lua_getglobal (char *name);
|
||||
lua_Object lua_rawgetglobal (char *name);
|
||||
void lua_setglobal (char *name); /* In: value */
|
||||
void lua_rawsetglobal (char *name); /* In: value */
|
||||
|
||||
void lua_settable (void); /* In: table, index, value */
|
||||
void lua_rawsettable (void); /* In: table, index, value */
|
||||
lua_Object lua_gettable (void); /* In: table, index */
|
||||
lua_Object lua_rawgettable (void); /* In: table, index */
|
||||
lua_Object lua_pop (void);
|
||||
|
||||
int lua_tag (lua_Object object);
|
||||
int lua_pushnil (void);
|
||||
int lua_pushnumber (float n);
|
||||
int lua_pushstring (char *s);
|
||||
int lua_pushcfunction (lua_CFunction fn);
|
||||
int lua_pushuserdata (void *u);
|
||||
int lua_pushobject (lua_Object object);
|
||||
|
||||
int lua_storeglobal (char *name);
|
||||
int lua_storefield (lua_Object object, char *field);
|
||||
int lua_storeindexed (lua_Object object, float index);
|
||||
|
||||
int lua_ref (int lock); /* In: value */
|
||||
lua_Object lua_getref (int ref);
|
||||
void lua_unref (int ref);
|
||||
|
||||
lua_Object lua_createtable (void);
|
||||
|
||||
long lua_collectgarbage (long limit);
|
||||
|
||||
|
||||
/* =============================================================== */
|
||||
/* some useful macros */
|
||||
|
||||
#define lua_call(name) lua_callfunction(lua_getglobal(name))
|
||||
|
||||
#define lua_pushref(ref) lua_pushobject(lua_getref(ref))
|
||||
|
||||
#define lua_refobject(o,l) (lua_pushobject(o), lua_ref(l))
|
||||
|
||||
#define lua_register(n,f) (lua_pushcfunction(f), lua_setglobal(n))
|
||||
|
||||
#define lua_pushuserdata(u) lua_pushusertag(u, 0)
|
||||
|
||||
#define lua_pushcfunction(f) lua_pushCclosure(f, 0)
|
||||
|
||||
#define lua_clonetag(t) lua_copytagmethods(lua_newtag(), (t))
|
||||
|
||||
|
||||
/* ==========================================================================
|
||||
** for compatibility with old versions. Avoid using these macros/functions
|
||||
** If your program does need any of these, define LUA_COMPAT2_5
|
||||
*/
|
||||
|
||||
|
||||
#ifdef LUA_COMPAT2_5
|
||||
|
||||
|
||||
lua_Object lua_setfallback (char *event, lua_CFunction fallback);
|
||||
|
||||
#define lua_storeglobal lua_setglobal
|
||||
#define lua_type lua_tag
|
||||
|
||||
#define lua_lockobject(o) lua_refobject(o,1)
|
||||
#define lua_lock() lua_ref(1)
|
||||
#define lua_getlocked lua_getref
|
||||
#define lua_pushlocked lua_pushref
|
||||
#define lua_unlock lua_unref
|
||||
|
||||
#define lua_pushliteral(o) lua_pushstring(o)
|
||||
|
||||
#define lua_getindexed(o,n) (lua_pushobject(o), lua_pushnumber(n), lua_gettable())
|
||||
#define lua_getfield(o,f) (lua_pushobject(o), lua_pushstring(f), lua_gettable())
|
||||
|
||||
#define lua_copystring(o) (strdup(lua_getstring(o)))
|
||||
|
||||
#define lua_getsubscript lua_gettable
|
||||
#define lua_storesubscript lua_settable
|
||||
|
||||
#endif
|
||||
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_iscfunction (lua_Object object);
|
||||
int lua_isuserdata (lua_Object object);
|
||||
|
||||
#endif
|
||||
|
||||
939
lua.stx
939
lua.stx
@@ -1,939 +0,0 @@
|
||||
%{
|
||||
/*
|
||||
** $Id: lua.stx,v 1.32 1998/01/12 13:00:51 roberto Exp roberto $
|
||||
** Syntax analizer and code generator
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "llex.h"
|
||||
#include "lmem.h"
|
||||
#include "lopcodes.h"
|
||||
#include "lparser.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
int luaY_parse (void);
|
||||
|
||||
|
||||
#define MES_LIM(x) "(limit=" x ")"
|
||||
|
||||
|
||||
/* size of a "normal" jump instruction: OpCode + 1 byte */
|
||||
#define JMPSIZE 2
|
||||
|
||||
/* maximum number of local variables */
|
||||
#define MAXLOCALS 32
|
||||
#define SMAXLOCALS "32"
|
||||
|
||||
#define MINGLOBAL (MAXLOCALS+1)
|
||||
|
||||
/* maximum number of variables in a multiple assignment */
|
||||
#define MAXVAR 32
|
||||
#define SMAXVAR "32"
|
||||
|
||||
/* maximum number of nested functions */
|
||||
#define MAXSTATES 6
|
||||
#define SMAXSTATES "6"
|
||||
|
||||
/* maximum number of upvalues */
|
||||
#define MAXUPVALUES 16
|
||||
#define SMAXUPVALUES "16"
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Variable descriptor:
|
||||
** if 0<n<MINGLOBAL, represents local variable indexed by (n-1);
|
||||
** if MINGLOBAL<=n, represents global variable at position (n-MINGLOBAL);
|
||||
** if n<0, indexed variable with index (-n)-1 (table on top of stack);
|
||||
** if n==0, an indexed variable (table and index on top of stack)
|
||||
** Must be long to store negative Word values.
|
||||
*/
|
||||
typedef long vardesc;
|
||||
|
||||
#define isglobal(v) (MINGLOBAL<=(v))
|
||||
#define globalindex(v) ((v)-MINGLOBAL)
|
||||
#define islocal(v) (0<(v) && (v)<MINGLOBAL)
|
||||
#define localindex(v) ((v)-1)
|
||||
#define isdot(v) (v<0)
|
||||
#define dotindex(v) ((-(v))-1)
|
||||
|
||||
/* state needed to generate code for a given function */
|
||||
typedef struct FuncState {
|
||||
TProtoFunc *f; /* current function header */
|
||||
int pc; /* next position to code */
|
||||
TaggedString *localvar[MAXLOCALS]; /* store local variable names */
|
||||
int stacksize; /* number of values on activation register */
|
||||
int maxstacksize; /* maximum number of values on activation register */
|
||||
int nlocalvar; /* number of active local variables */
|
||||
int nupvalues; /* number of upvalues */
|
||||
int nvars; /* number of entries in f->locvars */
|
||||
int maxcode; /* size of f->code */
|
||||
int maxvars; /* size of f->locvars (-1 if no debug information) */
|
||||
int maxconsts; /* size of f->consts */
|
||||
vardesc varbuffer[MAXVAR]; /* variables in an assignment list */
|
||||
vardesc upvalues[MAXUPVALUES]; /* upvalues */
|
||||
} FuncState;
|
||||
|
||||
|
||||
|
||||
#define YYPURE 1
|
||||
|
||||
|
||||
void luaY_syntaxerror (char *s, char *token)
|
||||
{
|
||||
if (token[0] == 0)
|
||||
token = "<eof>";
|
||||
luaL_verror("%.100s;\n last token read: \"%.50s\" at line %d in file %.50s",
|
||||
s, token, L->lexstate->linenumber, L->mainState->f->fileName->str);
|
||||
}
|
||||
|
||||
|
||||
void luaY_error (char *s)
|
||||
{
|
||||
luaY_syntaxerror(s, luaX_lasttoken());
|
||||
}
|
||||
|
||||
|
||||
static void check_pc (int n)
|
||||
{
|
||||
FuncState *fs = L->currState;
|
||||
if (fs->pc+n > fs->maxcode)
|
||||
fs->maxcode = luaM_growvector(&fs->f->code, fs->maxcode,
|
||||
Byte, codeEM, MAX_INT);
|
||||
}
|
||||
|
||||
|
||||
static void code_byte (Byte c)
|
||||
{
|
||||
check_pc(1);
|
||||
L->currState->f->code[L->currState->pc++] = c;
|
||||
}
|
||||
|
||||
|
||||
static void deltastack (int delta)
|
||||
{
|
||||
FuncState *fs = L->currState;
|
||||
fs->stacksize += delta;
|
||||
if (fs->stacksize > fs->maxstacksize) {
|
||||
if (fs->stacksize > 255)
|
||||
luaY_error("function/expression too complex");
|
||||
fs->maxstacksize = fs->stacksize;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int code_oparg_at (int pc, OpCode op, int builtin, int arg, int delta)
|
||||
{
|
||||
Byte *code = L->currState->f->code;
|
||||
deltastack(delta);
|
||||
if (arg < builtin) {
|
||||
code[pc] = op+1+arg;
|
||||
return 1;
|
||||
}
|
||||
else if (arg <= 255) {
|
||||
code[pc] = op;
|
||||
code[pc+1] = arg;
|
||||
return 2;
|
||||
}
|
||||
else if (arg <= MAX_WORD) {
|
||||
code[pc] = op+1+builtin;
|
||||
code[pc+1] = arg&0xFF;
|
||||
code[pc+2] = arg>>8;
|
||||
return 3;
|
||||
}
|
||||
else luaY_error("code too long " MES_LIM("64K"));
|
||||
return 0; /* to avoid warnings */
|
||||
}
|
||||
|
||||
|
||||
static int fix_opcode (int pc, OpCode op, int builtin, int arg)
|
||||
{
|
||||
FuncState *fs = L->currState;
|
||||
if (arg < builtin) { /* close space */
|
||||
luaO_memdown(fs->f->code+pc+1, fs->f->code+pc+2, fs->pc-(pc+2));
|
||||
fs->pc--;
|
||||
}
|
||||
else if (arg > 255) { /* open space */
|
||||
check_pc(1);
|
||||
luaO_memup(fs->f->code+pc+1, fs->f->code+pc, fs->pc-pc);
|
||||
fs->pc++;
|
||||
}
|
||||
return code_oparg_at(pc, op, builtin, arg, 0) - 2;
|
||||
}
|
||||
|
||||
|
||||
static void code_oparg (OpCode op, int builtin, int arg, int delta)
|
||||
{
|
||||
check_pc(3); /* maximum code size */
|
||||
L->currState->pc += code_oparg_at(L->currState->pc, op, builtin, arg, delta);
|
||||
}
|
||||
|
||||
|
||||
static void code_opcode (OpCode op, int delta)
|
||||
{
|
||||
deltastack(delta);
|
||||
code_byte(op);
|
||||
}
|
||||
|
||||
|
||||
static void code_pop (OpCode op)
|
||||
{
|
||||
code_opcode(op, -1);
|
||||
}
|
||||
|
||||
/* binary operations get 2 arguments and leave one, so they pop one */
|
||||
#define code_binop(op) code_pop(op)
|
||||
|
||||
|
||||
static void code_neutralop (OpCode op)
|
||||
{
|
||||
code_opcode(op, 0);
|
||||
}
|
||||
|
||||
/* unary operations get 1 argument and leave one, so they are neutral */
|
||||
#define code_unop(op) code_neutralop(op)
|
||||
|
||||
|
||||
static void code_constant (int c)
|
||||
{
|
||||
code_oparg(PUSHCONSTANT, 8, c, 1);
|
||||
}
|
||||
|
||||
|
||||
static int next_constant (FuncState *cs)
|
||||
{
|
||||
TProtoFunc *f = cs->f;
|
||||
if (f->nconsts >= cs->maxconsts) {
|
||||
cs->maxconsts = luaM_growvector(&f->consts, cs->maxconsts, TObject,
|
||||
constantEM, MAX_WORD);
|
||||
}
|
||||
return f->nconsts++;
|
||||
}
|
||||
|
||||
|
||||
static int string_constant (TaggedString *s, FuncState *cs)
|
||||
{
|
||||
TProtoFunc *f = cs->f;
|
||||
int c = s->constindex;
|
||||
if (!(c < f->nconsts &&
|
||||
ttype(&f->consts[c]) == LUA_T_STRING && tsvalue(&f->consts[c]) == s)) {
|
||||
c = next_constant(cs);
|
||||
ttype(&f->consts[c]) = LUA_T_STRING;
|
||||
tsvalue(&f->consts[c]) = s;
|
||||
s->constindex = c; /* hint for next time */
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
static void code_string (TaggedString *s)
|
||||
{
|
||||
code_constant(string_constant(s, L->currState));
|
||||
}
|
||||
|
||||
|
||||
#define LIM 20
|
||||
static int real_constant (real r)
|
||||
{
|
||||
/* check whether 'r' has appeared within the last LIM entries */
|
||||
TObject *cnt = L->currState->f->consts;
|
||||
int c = L->currState->f->nconsts;
|
||||
int lim = c < LIM ? 0 : c-LIM;
|
||||
while (--c >= lim) {
|
||||
if (ttype(&cnt[c]) == LUA_T_NUMBER && nvalue(&cnt[c]) == r)
|
||||
return c;
|
||||
}
|
||||
/* not found; create a luaM_new entry */
|
||||
c = next_constant(L->currState);
|
||||
cnt = L->currState->f->consts; /* 'next_constant' may reallocate this vector */
|
||||
ttype(&cnt[c]) = LUA_T_NUMBER;
|
||||
nvalue(&cnt[c]) = r;
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
static void code_number (real f)
|
||||
{
|
||||
int i;
|
||||
if (f >= 0 && f <= (real)MAX_WORD && (real)(i=(int)f) == f)
|
||||
code_oparg(PUSHNUMBER, 3, i, 1); /* f has an (short) integer value */
|
||||
else
|
||||
code_constant(real_constant(f));
|
||||
}
|
||||
|
||||
|
||||
static void flush_record (int n)
|
||||
{
|
||||
if (n > 0)
|
||||
code_oparg(SETMAP, 1, n-1, -2*n);
|
||||
}
|
||||
|
||||
static void flush_list (int m, int n)
|
||||
{
|
||||
if (n == 0) return;
|
||||
code_oparg(SETLIST, 1, m, -n);
|
||||
code_byte(n);
|
||||
}
|
||||
|
||||
|
||||
static void luaI_registerlocalvar (TaggedString *varname, int line)
|
||||
{
|
||||
FuncState *fs = L->currState;
|
||||
if (fs->maxvars != -1) { /* debug information? */
|
||||
if (fs->nvars >= fs->maxvars)
|
||||
fs->maxvars = luaM_growvector(&fs->f->locvars, fs->maxvars,
|
||||
LocVar, "", MAX_WORD);
|
||||
fs->f->locvars[fs->nvars].varname = varname;
|
||||
fs->f->locvars[fs->nvars].line = line;
|
||||
fs->nvars++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void luaI_unregisterlocalvar (int line)
|
||||
{
|
||||
luaI_registerlocalvar(NULL, line);
|
||||
}
|
||||
|
||||
|
||||
static void store_localvar (TaggedString *name, int n)
|
||||
{
|
||||
if (L->currState->nlocalvar+n < MAXLOCALS)
|
||||
L->currState->localvar[L->currState->nlocalvar+n] = name;
|
||||
else
|
||||
luaY_error("too many local variables " MES_LIM(SMAXLOCALS));
|
||||
luaI_registerlocalvar(name, L->lexstate->linenumber);
|
||||
}
|
||||
|
||||
static void add_localvar (TaggedString *name)
|
||||
{
|
||||
store_localvar(name, 0);
|
||||
L->currState->nlocalvar++;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** dotted variables <a.x> must be stored like regular indexed vars <a["x"]>
|
||||
*/
|
||||
static vardesc var2store (vardesc var)
|
||||
{
|
||||
if (isdot(var)) {
|
||||
code_constant(dotindex(var));
|
||||
var = 0;
|
||||
}
|
||||
return var;
|
||||
}
|
||||
|
||||
|
||||
static void add_varbuffer (vardesc var, int n)
|
||||
{
|
||||
if (n >= MAXVAR)
|
||||
luaY_error("variable buffer overflow " MES_LIM(SMAXVAR));
|
||||
L->currState->varbuffer[n] = var2store(var);
|
||||
}
|
||||
|
||||
|
||||
static int aux_localname (TaggedString *n, FuncState *st)
|
||||
{
|
||||
int i;
|
||||
for (i=st->nlocalvar-1; i >= 0; i--)
|
||||
if (n == st->localvar[i]) return i; /* local var index */
|
||||
return -1; /* not found */
|
||||
}
|
||||
|
||||
|
||||
static vardesc singlevar (TaggedString *n, FuncState *st)
|
||||
{
|
||||
int i = aux_localname(n, st);
|
||||
if (i == -1) { /* check shadowing */
|
||||
int l;
|
||||
for (l=1; l<=(st-L->mainState); l++)
|
||||
if (aux_localname(n, st-l) >= 0)
|
||||
luaY_syntaxerror("cannot access a variable in outer scope", n->str);
|
||||
return string_constant(n, st)+MINGLOBAL; /* global value */
|
||||
}
|
||||
else return i+1; /* local value */
|
||||
}
|
||||
|
||||
|
||||
static int indexupvalue (TaggedString *n)
|
||||
{
|
||||
vardesc v = singlevar(n, L->currState-1);
|
||||
int i;
|
||||
for (i=0; i<L->currState->nupvalues; i++) {
|
||||
if (L->currState->upvalues[i] == v)
|
||||
return i;
|
||||
}
|
||||
/* new one */
|
||||
if (++(L->currState->nupvalues) > MAXUPVALUES)
|
||||
luaY_error("too many upvalues in a single function " MES_LIM(SMAXUPVALUES));
|
||||
L->currState->upvalues[i] = v; /* i = L->currState->nupvalues - 1 */
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
static void pushupvalue (TaggedString *n)
|
||||
{
|
||||
int i;
|
||||
if (L->currState == L->mainState)
|
||||
luaY_error("cannot access upvalue in main");
|
||||
if (aux_localname(n, L->currState) >= 0)
|
||||
luaY_syntaxerror("cannot access an upvalue in current scope", n->str);
|
||||
i = indexupvalue(n);
|
||||
code_oparg(PUSHUPVALUE, 2, i, 1);
|
||||
}
|
||||
|
||||
|
||||
void luaY_codedebugline (int line)
|
||||
{
|
||||
if (lua_debug && line != L->lexstate->lastline) {
|
||||
code_oparg(SETLINE, 0, line, 0);
|
||||
L->lexstate->lastline = line;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void adjuststack (int n)
|
||||
{
|
||||
if (n > 0)
|
||||
code_oparg(POP, 2, n-1, -n);
|
||||
else if (n < 0)
|
||||
code_oparg(PUSHNIL, 1, (-n)-1, -n);
|
||||
}
|
||||
|
||||
|
||||
static long adjust_functioncall (long exp, int nresults)
|
||||
{
|
||||
if (exp <= 0)
|
||||
return -exp; /* exp is -list length */
|
||||
else {
|
||||
int temp = L->currState->f->code[exp];
|
||||
int nparams = L->currState->f->code[exp-1];
|
||||
exp += fix_opcode(exp-2, CALLFUNC, 2, nresults);
|
||||
L->currState->f->code[exp] = nparams;
|
||||
if (nresults != MULT_RET)
|
||||
deltastack(nresults);
|
||||
deltastack(-(nparams+1));
|
||||
return temp+nresults;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void adjust_mult_assign (int vars, long exps)
|
||||
{
|
||||
if (exps > 0) { /* must correct function call */
|
||||
int diff = L->currState->f->code[exps] - vars;
|
||||
if (diff < 0)
|
||||
adjust_functioncall(exps, -diff);
|
||||
else {
|
||||
adjust_functioncall(exps, 0);
|
||||
adjuststack(diff);
|
||||
}
|
||||
}
|
||||
else adjuststack((-exps)-vars);
|
||||
}
|
||||
|
||||
|
||||
static void code_args (int nparams, int dots)
|
||||
{
|
||||
L->currState->nlocalvar += nparams; /* "self" may already be there */
|
||||
nparams = L->currState->nlocalvar;
|
||||
if (!dots) {
|
||||
L->currState->f->code[1] = nparams; /* fill-in arg information */
|
||||
deltastack(nparams);
|
||||
}
|
||||
else {
|
||||
L->currState->f->code[1] = nparams+ZEROVARARG;
|
||||
deltastack(nparams+1);
|
||||
add_localvar(luaS_new("arg"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void lua_pushvar (vardesc var)
|
||||
{
|
||||
if (isglobal(var))
|
||||
code_oparg(GETGLOBAL, 8, globalindex(var), 1);
|
||||
else if (islocal(var))
|
||||
code_oparg(PUSHLOCAL, 8, localindex(var), 1);
|
||||
else if (isdot(var))
|
||||
code_oparg(GETDOTTED, 8, dotindex(var), 0);
|
||||
else
|
||||
code_pop(GETTABLE);
|
||||
}
|
||||
|
||||
|
||||
static void storevar (vardesc var)
|
||||
{
|
||||
if (var == 0) /* indexed var */
|
||||
code_opcode(SETTABLE0, -3);
|
||||
else if (isglobal(var))
|
||||
code_oparg(SETGLOBAL, 8, globalindex(var), -1);
|
||||
else /* local var */
|
||||
code_oparg(SETLOCAL, 8, localindex(var), -1);
|
||||
}
|
||||
|
||||
|
||||
/* returns how many elements are left as 'garbage' on the stack */
|
||||
static int lua_codestore (int i, int left)
|
||||
{
|
||||
if (L->currState->varbuffer[i] != 0 || /* global or local var or */
|
||||
left+i == 0) { /* indexed var without values in between */
|
||||
storevar(L->currState->varbuffer[i]);
|
||||
return left;
|
||||
}
|
||||
else { /* indexed var with values in between*/
|
||||
code_oparg(SETTABLE, 0, left+i, -1);
|
||||
return left+2; /* table/index are not popped, since they are not on top */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int fix_jump (int pc, OpCode op, int n)
|
||||
{
|
||||
/* jump is relative to position following jump instruction */
|
||||
return fix_opcode(pc, op, 0, n-(pc+JMPSIZE));
|
||||
}
|
||||
|
||||
|
||||
static void fix_upjmp (OpCode op, int pos)
|
||||
{
|
||||
int delta = L->currState->pc+JMPSIZE - pos; /* jump is relative */
|
||||
if (delta > 255) delta++;
|
||||
code_oparg(op, 0, delta, 0);
|
||||
}
|
||||
|
||||
|
||||
static void codeIf (int thenAdd, int elseAdd)
|
||||
{
|
||||
int elseinit = elseAdd+JMPSIZE;
|
||||
if (L->currState->pc == elseinit) { /* no else part */
|
||||
L->currState->pc -= JMPSIZE;
|
||||
elseinit = L->currState->pc;
|
||||
}
|
||||
else
|
||||
elseinit += fix_jump(elseAdd, JMP, L->currState->pc);
|
||||
fix_jump(thenAdd, IFFJMP, elseinit);
|
||||
}
|
||||
|
||||
|
||||
static void code_shortcircuit (int pc, OpCode op)
|
||||
{
|
||||
fix_jump(pc, op, L->currState->pc);
|
||||
}
|
||||
|
||||
|
||||
static void codereturn (void)
|
||||
{
|
||||
code_oparg(RETCODE, 0, L->currState->nlocalvar, 0);
|
||||
L->currState->stacksize = L->currState->nlocalvar;
|
||||
}
|
||||
|
||||
|
||||
static void func_onstack (TProtoFunc *f)
|
||||
{
|
||||
int i;
|
||||
int nupvalues = (L->currState+1)->nupvalues;
|
||||
int c = next_constant(L->currState);
|
||||
ttype(&L->currState->f->consts[c]) = LUA_T_PROTO;
|
||||
L->currState->f->consts[c].value.tf = (L->currState+1)->f;
|
||||
if (nupvalues == 0)
|
||||
code_constant(c);
|
||||
else {
|
||||
for (i=0; i<nupvalues; i++)
|
||||
lua_pushvar((L->currState+1)->upvalues[i]);
|
||||
code_constant(c);
|
||||
code_oparg(CLOSURE, 2, nupvalues, -nupvalues);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void init_state (TaggedString *filename)
|
||||
{
|
||||
TProtoFunc *f = luaF_newproto();
|
||||
FuncState *fs = L->currState;
|
||||
fs->stacksize = 0;
|
||||
fs->maxstacksize = 0;
|
||||
fs->nlocalvar = 0;
|
||||
fs->nupvalues = 0;
|
||||
fs->f = f;
|
||||
f->fileName = filename;
|
||||
fs->pc = 0;
|
||||
fs->maxcode = 0;
|
||||
f->code = NULL;
|
||||
fs->maxconsts = 0;
|
||||
if (lua_debug) {
|
||||
fs->nvars = 0;
|
||||
fs->maxvars = 0;
|
||||
}
|
||||
else
|
||||
fs->maxvars = -1; /* flag no debug information */
|
||||
code_byte(0); /* to be filled with stacksize */
|
||||
code_byte(0); /* to be filled with arg information */
|
||||
L->lexstate->lastline = 0; /* invalidate it */
|
||||
}
|
||||
|
||||
|
||||
static void init_func (void)
|
||||
{
|
||||
if (L->currState-L->mainState >= MAXSTATES-1)
|
||||
luaY_error("too many nested functions " MES_LIM(SMAXSTATES));
|
||||
L->currState++;
|
||||
init_state(L->mainState->f->fileName);
|
||||
luaY_codedebugline(L->lexstate->linenumber);
|
||||
L->currState->f->lineDefined = L->lexstate->linenumber;
|
||||
}
|
||||
|
||||
static TProtoFunc *close_func (void)
|
||||
{
|
||||
TProtoFunc *f = L->currState->f;
|
||||
code_neutralop(ENDCODE);
|
||||
f->code[0] = L->currState->maxstacksize;
|
||||
f->code = luaM_reallocvector(f->code, L->currState->pc, Byte);
|
||||
f->consts = luaM_reallocvector(f->consts, f->nconsts, TObject);
|
||||
if (L->currState->maxvars != -1) { /* debug information? */
|
||||
luaI_registerlocalvar(NULL, -1); /* flag end of vector */
|
||||
f->locvars = luaM_reallocvector(f->locvars, L->currState->nvars, LocVar);
|
||||
}
|
||||
L->currState--;
|
||||
return f;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Parse Lua code.
|
||||
*/
|
||||
TProtoFunc *luaY_parser (ZIO *z)
|
||||
{
|
||||
struct LexState lexstate;
|
||||
FuncState state[MAXSTATES];
|
||||
L->currState = L->mainState = &state[0];
|
||||
L->lexstate = &lexstate;
|
||||
luaX_setinput(z);
|
||||
init_state(luaS_new(zname(z)));
|
||||
if (luaY_parse()) lua_error("parse error");
|
||||
return close_func();
|
||||
}
|
||||
|
||||
|
||||
%}
|
||||
|
||||
|
||||
%union
|
||||
{
|
||||
int vInt;
|
||||
real vReal;
|
||||
char *pChar;
|
||||
long vLong;
|
||||
TaggedString *pTStr;
|
||||
TProtoFunc *pFunc;
|
||||
}
|
||||
|
||||
%start chunk
|
||||
|
||||
%token WRONGTOKEN
|
||||
%token NIL
|
||||
%token IF THEN ELSE ELSEIF WHILE DO REPEAT UNTIL END
|
||||
%token RETURN
|
||||
%token LOCAL
|
||||
%token FUNCTION
|
||||
%token DOTS
|
||||
%token <vReal> NUMBER
|
||||
%token <pTStr> NAME STRING
|
||||
|
||||
%type <vInt> SaveWord, cond, GetPC, SaveWordPop, SaveWordPush
|
||||
%type <vLong> exprlist, exprlist1 /* if > 0, points to function return
|
||||
counter (which has list length); if <= 0, -list lenght */
|
||||
%type <vLong> functioncall, expr, sexp /* if != 0, points to function return
|
||||
counter */
|
||||
%type <vInt> varlist1, funcParams, funcvalue
|
||||
%type <vInt> fieldlist, localnamelist, decinit
|
||||
%type <vInt> ffieldlist1, lfieldlist1, ffieldlist, lfieldlist, part
|
||||
%type <vLong> var, varname, funcname /* vardesc */
|
||||
|
||||
|
||||
%left AND OR
|
||||
%left EQ NE '>' '<' LE GE
|
||||
%left CONC
|
||||
%left '+' '-'
|
||||
%left '*' '/'
|
||||
%left UNARY NOT
|
||||
%right '^'
|
||||
|
||||
|
||||
%% /* beginning of rules section */
|
||||
|
||||
|
||||
chunk : statlist ret ;
|
||||
|
||||
statlist : /* empty */
|
||||
| statlist stat sc
|
||||
;
|
||||
|
||||
sc : /* empty */ | ';' ;
|
||||
|
||||
stat : IF cond THEN block SaveWord elsepart END { codeIf($2, $5); }
|
||||
|
||||
| DO block END
|
||||
|
||||
| WHILE GetPC cond DO block END
|
||||
{{
|
||||
FuncState *fs = L->currState;
|
||||
int expsize = $3-$2;
|
||||
int newpos = $2+JMPSIZE;
|
||||
check_pc(expsize);
|
||||
memcpy(fs->f->code+fs->pc, fs->f->code+$2, expsize);
|
||||
luaO_memdown(fs->f->code+$2, fs->f->code+$3, fs->pc-$2);
|
||||
newpos += fix_jump($2, JMP, fs->pc-expsize);
|
||||
fix_upjmp(IFTUPJMP, newpos);
|
||||
}}
|
||||
|
||||
| REPEAT GetPC block UNTIL expr1
|
||||
{
|
||||
fix_upjmp(IFFUPJMP, $2);
|
||||
deltastack(-1); /* pops condition */
|
||||
}
|
||||
|
||||
| varlist1 '=' exprlist1
|
||||
{{
|
||||
int i;
|
||||
int left = 0;
|
||||
adjust_mult_assign($1, $3);
|
||||
for (i=$1-1; i>=0; i--)
|
||||
left = lua_codestore(i, left);
|
||||
adjuststack(left); /* remove eventual 'garbage' left on stack */
|
||||
}}
|
||||
|
||||
| functioncall { adjust_functioncall($1, 0); }
|
||||
|
||||
| LOCAL localnamelist decinit
|
||||
{
|
||||
L->currState->nlocalvar += $2;
|
||||
adjust_mult_assign($2, $3);
|
||||
}
|
||||
|
||||
| FUNCTION funcname body { storevar($2); }
|
||||
;
|
||||
|
||||
block : {$<vInt>$ = L->currState->nlocalvar;} chunk
|
||||
{
|
||||
adjuststack(L->currState->nlocalvar - $<vInt>1);
|
||||
for (; L->currState->nlocalvar > $<vInt>1; L->currState->nlocalvar--)
|
||||
luaI_unregisterlocalvar(L->lexstate->linenumber);
|
||||
}
|
||||
;
|
||||
|
||||
funcname : varname { $$ = $1; init_func(); }
|
||||
| fvarname '.' fname
|
||||
{
|
||||
$$ = 0; /* flag indexed variable */
|
||||
init_func();
|
||||
}
|
||||
| fvarname ':' fname
|
||||
{
|
||||
$$ = 0; /* flag indexed variable */
|
||||
init_func();
|
||||
add_localvar(luaS_new("self"));
|
||||
}
|
||||
;
|
||||
|
||||
fvarname : varname { lua_pushvar($1); } ;
|
||||
|
||||
fname : NAME { code_string($1); } ;
|
||||
|
||||
body : '(' parlist ')' chunk END { func_onstack(close_func()); } ;
|
||||
|
||||
elsepart : /* empty */
|
||||
| ELSE block
|
||||
| ELSEIF cond THEN block SaveWord elsepart { codeIf($2, $5); }
|
||||
;
|
||||
|
||||
ret : /* empty */
|
||||
| RETURN exprlist sc
|
||||
{
|
||||
adjust_functioncall($2, MULT_RET);
|
||||
codereturn();
|
||||
}
|
||||
;
|
||||
|
||||
GetPC : /* empty */ { $$ = L->currState->pc; } ;
|
||||
|
||||
SaveWord : /* empty */
|
||||
{ $$ = L->currState->pc;
|
||||
check_pc(JMPSIZE);
|
||||
L->currState->pc += JMPSIZE; /* open space */
|
||||
}
|
||||
;
|
||||
|
||||
SaveWordPop : SaveWord { $$ = $1; deltastack(-1); /* pop condition */ } ;
|
||||
|
||||
SaveWordPush : SaveWord { $$ = $1; deltastack(1); /* push a value */ } ;
|
||||
|
||||
cond : expr1 SaveWordPop { $$ = $2; } ;
|
||||
|
||||
expr1 : expr { adjust_functioncall($1, 1); } ;
|
||||
|
||||
expr : '(' expr ')' { $$ = $2; }
|
||||
| expr1 EQ expr1 { code_binop(EQOP); $$ = 0; }
|
||||
| expr1 '<' expr1 { code_binop(LTOP); $$ = 0; }
|
||||
| expr1 '>' expr1 { code_binop(GTOP); $$ = 0; }
|
||||
| expr1 NE expr1 { code_binop(NEQOP); $$ = 0; }
|
||||
| expr1 LE expr1 { code_binop(LEOP); $$ = 0; }
|
||||
| expr1 GE expr1 { code_binop(GEOP); $$ = 0; }
|
||||
| expr1 '+' expr1 { code_binop(ADDOP); $$ = 0; }
|
||||
| expr1 '-' expr1 { code_binop(SUBOP); $$ = 0; }
|
||||
| expr1 '*' expr1 { code_binop(MULTOP); $$ = 0; }
|
||||
| expr1 '/' expr1 { code_binop(DIVOP); $$ = 0; }
|
||||
| expr1 '^' expr1 { code_binop(POWOP); $$ = 0; }
|
||||
| expr1 CONC expr1 { code_binop(CONCOP); $$ = 0; }
|
||||
| '-' expr1 %prec UNARY { code_unop(MINUSOP); $$ = 0;}
|
||||
| NOT expr1 { code_unop(NOTOP); $$ = 0;}
|
||||
| sexp { $$ = $1; /* simple expressions */ }
|
||||
| table { $$ = 0; }
|
||||
| NUMBER { code_number($1); $$ = 0; }
|
||||
| STRING { code_string($1); $$ = 0; }
|
||||
| NIL { adjuststack(-1); $$ = 0; }
|
||||
| FUNCTION { init_func(); } body { $$ = 0; }
|
||||
| expr1 AND SaveWordPop expr1 { code_shortcircuit($3, ONFJMP); $$ = 0; }
|
||||
| expr1 OR SaveWordPop expr1 { code_shortcircuit($3, ONTJMP); $$ = 0; }
|
||||
;
|
||||
|
||||
sexp1 : sexp { adjust_functioncall($1, 1); } ;
|
||||
|
||||
sexp : var { lua_pushvar($1); $$ = 0; }
|
||||
| '%' NAME { pushupvalue($2); $$ = 0; }
|
||||
| functioncall { $$ = $1; }
|
||||
;
|
||||
|
||||
var : varname { $$ = $1; }
|
||||
| sexp1 '[' expr1 ']' { $$ = 0; } /* indexed variable */
|
||||
| sexp1 '.' NAME { $$ = (-string_constant($3, L->currState))-1; }
|
||||
;
|
||||
|
||||
varname : NAME { $$ = singlevar($1, L->currState); } ;
|
||||
|
||||
table : '{' SaveWordPush fieldlist '}' { fix_opcode($2, CREATEARRAY, 2, $3); } ;
|
||||
|
||||
functioncall : funcvalue funcParams
|
||||
{
|
||||
code_byte(0); /* save space for opcode */
|
||||
code_byte($1+$2); /* number of parameters */
|
||||
$$ = L->currState->pc;
|
||||
code_byte(0); /* must be adjusted by other rules */
|
||||
}
|
||||
;
|
||||
|
||||
funcvalue : sexp1 { $$ = 0; }
|
||||
| sexp1 ':' NAME
|
||||
{
|
||||
code_oparg(PUSHSELF, 8, string_constant($3, L->currState), 1);
|
||||
$$ = 1;
|
||||
}
|
||||
;
|
||||
|
||||
funcParams : '(' exprlist ')' { $$ = adjust_functioncall($2, 1); }
|
||||
| table { $$ = 1; }
|
||||
| STRING { code_string($1); $$ = 1; }
|
||||
;
|
||||
|
||||
exprlist : /* empty */ { $$ = 0; }
|
||||
| exprlist1 { $$ = $1; }
|
||||
;
|
||||
|
||||
exprlist1 : expr { if ($1 != 0) $$ = $1; else $$ = -1; }
|
||||
| exprlist1 ',' { $<vLong>$ = adjust_functioncall($1, 1); } expr
|
||||
{
|
||||
if ($4 == 0) $$ = -($<vLong>3 + 1); /* -length */
|
||||
else {
|
||||
L->currState->f->code[$4] = $<vLong>3; /* store list length */
|
||||
$$ = $4;
|
||||
}
|
||||
}
|
||||
;
|
||||
|
||||
parlist : /* empty */ { code_args(0, 0); }
|
||||
| DOTS { code_args(0, 1); }
|
||||
| localnamelist { code_args($1, 0); }
|
||||
| localnamelist ',' DOTS { code_args($1, 1); }
|
||||
;
|
||||
|
||||
fieldlist : part { $$ = abs($1); }
|
||||
| part ';' part
|
||||
{
|
||||
if ($1*$3 > 0) /* repeated parts? */
|
||||
luaY_error("invalid constructor syntax");
|
||||
$$ = abs($1)+abs($3);
|
||||
}
|
||||
;
|
||||
|
||||
part : /* empty */ { $$ = 0; }
|
||||
| ffieldlist { $$ = $1; }
|
||||
| lfieldlist { $$ = $1; }
|
||||
;
|
||||
|
||||
lastcomma : /* empty */ | ',' ;
|
||||
|
||||
ffieldlist : ffieldlist1 lastcomma
|
||||
{
|
||||
flush_record($1%RFIELDS_PER_FLUSH);
|
||||
$$ = -$1; /* negative signals a "record" part */
|
||||
}
|
||||
;
|
||||
|
||||
lfieldlist : lfieldlist1 lastcomma
|
||||
{
|
||||
flush_list($1/LFIELDS_PER_FLUSH, $1%LFIELDS_PER_FLUSH);
|
||||
$$ = $1;
|
||||
}
|
||||
;
|
||||
|
||||
ffieldlist1 : ffield {$$=1;}
|
||||
| ffieldlist1 ',' ffield
|
||||
{
|
||||
$$=$1+1;
|
||||
if ($$%RFIELDS_PER_FLUSH == 0)
|
||||
flush_record(RFIELDS_PER_FLUSH);
|
||||
}
|
||||
;
|
||||
|
||||
ffield : ffieldkey '=' expr1 ;
|
||||
|
||||
ffieldkey : '[' expr1 ']'
|
||||
| fname
|
||||
;
|
||||
|
||||
lfieldlist1 : expr1 {$$=1;}
|
||||
| lfieldlist1 ',' expr1
|
||||
{
|
||||
$$=$1+1;
|
||||
if ($$%LFIELDS_PER_FLUSH == 0)
|
||||
flush_list($$/LFIELDS_PER_FLUSH - 1, LFIELDS_PER_FLUSH);
|
||||
}
|
||||
;
|
||||
|
||||
varlist1 : var { $$ = 1; add_varbuffer($1, 0); }
|
||||
| varlist1 ',' var { add_varbuffer($3, $1); $$ = $1+1; }
|
||||
;
|
||||
|
||||
localnamelist : NAME {store_localvar($1, 0); $$ = 1;}
|
||||
| localnamelist ',' NAME { store_localvar($3, $1); $$ = $1+1; }
|
||||
;
|
||||
|
||||
decinit : /* empty */ { $$ = 0; }
|
||||
| '=' exprlist1 { $$ = $2; }
|
||||
;
|
||||
|
||||
%%
|
||||
|
||||
33
luadebug.h
33
luadebug.h
@@ -1,33 +0,0 @@
|
||||
/*
|
||||
** $Id: $
|
||||
** Debuging API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#ifndef luadebug_h
|
||||
#define luadebug_h
|
||||
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
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
|
||||
33
lualib.h
33
lualib.h
@@ -1,34 +1,15 @@
|
||||
/*
|
||||
** $Id: lualib.h,v 1.2 1997/11/26 18:53:45 roberto Exp roberto $
|
||||
** Lua standard libraries
|
||||
** See Copyright Notice in lua.h
|
||||
** Libraries to use in LUA programs
|
||||
** Grupo de Tecnologia em Computacao Grafica
|
||||
** TeCGraf - PUC-Rio
|
||||
** 19 May 93
|
||||
*/
|
||||
|
||||
|
||||
#ifndef lualib_h
|
||||
#define lualib_h
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
void lua_iolibopen (void);
|
||||
void lua_strlibopen (void);
|
||||
void lua_mathlibopen (void);
|
||||
|
||||
|
||||
|
||||
|
||||
/* To keep compatibility with old versions */
|
||||
|
||||
#define iolib_open lua_iolibopen
|
||||
#define strlib_open lua_strlibopen
|
||||
#define mathlib_open lua_mathlibopen
|
||||
|
||||
|
||||
|
||||
/* auxiliar functions (private) */
|
||||
|
||||
int luaI_singlematch (int c, char *p, char **ep);
|
||||
void iolib_open (void);
|
||||
void strlib_open (void);
|
||||
void mathlib_open (void);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
272
lundump.c
272
lundump.c
@@ -1,272 +0,0 @@
|
||||
/*
|
||||
** $Id: lundump.c,v 1.4 1998/01/13 20:05:24 lhf Exp $
|
||||
** load bytecodes from files
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "lauxlib.h"
|
||||
#include "lfunc.h"
|
||||
#include "lmem.h"
|
||||
#include "lstring.h"
|
||||
#include "lundump.h"
|
||||
|
||||
typedef struct {
|
||||
ZIO* Z;
|
||||
int SwapNumber;
|
||||
int LoadFloat;
|
||||
} Sundump;
|
||||
|
||||
static void unexpectedEOZ(ZIO* Z)
|
||||
{
|
||||
luaL_verror("unexpected end of binary file %s",Z->name);
|
||||
}
|
||||
|
||||
static int ezgetc(ZIO* Z)
|
||||
{
|
||||
int c=zgetc(Z);
|
||||
if (c==EOZ) unexpectedEOZ(Z);
|
||||
return c;
|
||||
}
|
||||
|
||||
static int ezread(ZIO* Z, void* b, int n)
|
||||
{
|
||||
int r=zread(Z,b,n);
|
||||
if (r!=0) unexpectedEOZ(Z);
|
||||
return r;
|
||||
}
|
||||
|
||||
static int LoadWord(ZIO* Z)
|
||||
{
|
||||
int hi=ezgetc(Z);
|
||||
int lo=ezgetc(Z);
|
||||
return (hi<<8)|lo;
|
||||
}
|
||||
|
||||
static void* LoadBlock(int size, ZIO* Z)
|
||||
{
|
||||
void* b=luaM_malloc(size);
|
||||
ezread(Z,b,size);
|
||||
return b;
|
||||
}
|
||||
|
||||
static int LoadSize(ZIO* Z)
|
||||
{
|
||||
int hi=LoadWord(Z);
|
||||
int lo=LoadWord(Z);
|
||||
int s=(hi<<16)|lo;
|
||||
if (hi!=0 && s==lo)
|
||||
luaL_verror("code too long (%ld bytes)",(hi<<16)|(long)lo);
|
||||
return s;
|
||||
}
|
||||
|
||||
static char* LoadString(ZIO* Z)
|
||||
{
|
||||
int size=LoadWord(Z);
|
||||
if (size==0)
|
||||
return NULL;
|
||||
else
|
||||
{
|
||||
char* b=luaL_openspace(size);
|
||||
ezread(Z,b,size);
|
||||
return b;
|
||||
}
|
||||
}
|
||||
|
||||
static TaggedString* LoadTString(ZIO* Z)
|
||||
{
|
||||
char* s=LoadString(Z);
|
||||
return (s==NULL) ? NULL : luaS_new(s);
|
||||
}
|
||||
|
||||
static void SwapFloat(float* f)
|
||||
{
|
||||
Byte* p=(Byte*)f;
|
||||
Byte* q=p+sizeof(float)-1;
|
||||
Byte t;
|
||||
t=*p; *p++=*q; *q--=t;
|
||||
t=*p; *p++=*q; *q--=t;
|
||||
}
|
||||
|
||||
static void SwapDouble(double* f)
|
||||
{
|
||||
Byte* p=(Byte*)f;
|
||||
Byte* q=p+sizeof(double)-1;
|
||||
Byte t;
|
||||
t=*p; *p++=*q; *q--=t;
|
||||
t=*p; *p++=*q; *q--=t;
|
||||
t=*p; *p++=*q; *q--=t;
|
||||
t=*p; *p++=*q; *q--=t;
|
||||
}
|
||||
|
||||
static real LoadNumber(Sundump* S)
|
||||
{
|
||||
if (S->LoadFloat)
|
||||
{
|
||||
float f;
|
||||
ezread(S->Z,&f,sizeof(f));
|
||||
if (S->SwapNumber) SwapFloat(&f);
|
||||
return f;
|
||||
}
|
||||
else
|
||||
{
|
||||
double f;
|
||||
ezread(S->Z,&f,sizeof(f));
|
||||
if (S->SwapNumber) SwapDouble(&f);
|
||||
return f;
|
||||
}
|
||||
}
|
||||
|
||||
static void LoadLocals(TProtoFunc* tf, ZIO* Z)
|
||||
{
|
||||
int i,n=LoadWord(Z);
|
||||
if (n==0) return;
|
||||
tf->locvars=luaM_newvector(n+1,LocVar);
|
||||
for (i=0; i<n; i++)
|
||||
{
|
||||
tf->locvars[i].line=LoadWord(Z);
|
||||
tf->locvars[i].varname=LoadTString(Z);
|
||||
}
|
||||
tf->locvars[i].line=-1; /* flag end of vector */
|
||||
tf->locvars[i].varname=NULL;
|
||||
}
|
||||
|
||||
static void LoadConstants(TProtoFunc* tf, Sundump* S)
|
||||
{
|
||||
int i,n=LoadWord(S->Z);
|
||||
tf->nconsts=n;
|
||||
if (n==0) return;
|
||||
tf->consts=luaM_newvector(n,TObject);
|
||||
for (i=0; i<n; i++)
|
||||
{
|
||||
TObject* o=tf->consts+i;
|
||||
int c=ezgetc(S->Z);
|
||||
switch (c)
|
||||
{
|
||||
case ID_NUM:
|
||||
ttype(o)=LUA_T_NUMBER;
|
||||
nvalue(o)=LoadNumber(S);
|
||||
break;
|
||||
case ID_STR:
|
||||
ttype(o)=LUA_T_STRING;
|
||||
tsvalue(o)=LoadTString(S->Z);
|
||||
break;
|
||||
case ID_FUN:
|
||||
ttype(o)=LUA_T_PROTO;
|
||||
tfvalue(o)=NULL;
|
||||
break;
|
||||
#ifdef DEBUG
|
||||
default: /* cannot happen */
|
||||
luaL_verror("internal error in LoadConstants: "
|
||||
"bad constant #%d type=%d ('%c')\n",i,c,c);
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static TProtoFunc* LoadFunction(Sundump* S);
|
||||
|
||||
static void LoadFunctions(TProtoFunc* tf, Sundump* S)
|
||||
{
|
||||
while (zgetc(S->Z)==ID_FUNCTION)
|
||||
{
|
||||
int i=LoadWord(S->Z);
|
||||
TProtoFunc* t=LoadFunction(S);
|
||||
TObject* o=tf->consts+i;
|
||||
tfvalue(o)=t;
|
||||
}
|
||||
}
|
||||
|
||||
static TProtoFunc* LoadFunction(Sundump* S)
|
||||
{
|
||||
ZIO* Z=S->Z;
|
||||
TProtoFunc* tf=luaF_newproto();
|
||||
tf->lineDefined=LoadWord(Z);
|
||||
tf->fileName=LoadTString(Z);
|
||||
tf->code=LoadBlock(LoadSize(Z),Z);
|
||||
LoadConstants(tf,S);
|
||||
LoadLocals(tf,Z);
|
||||
LoadFunctions(tf,S);
|
||||
return tf;
|
||||
}
|
||||
|
||||
static void LoadSignature(ZIO* Z)
|
||||
{
|
||||
char* s=SIGNATURE;
|
||||
while (*s!=0 && ezgetc(Z)==*s)
|
||||
++s;
|
||||
if (*s!=0) luaL_verror("bad signature in binary file %s",Z->name);
|
||||
}
|
||||
|
||||
static void LoadHeader(Sundump* S)
|
||||
{
|
||||
ZIO* Z=S->Z;
|
||||
int version,sizeofR;
|
||||
LoadSignature(Z);
|
||||
version=ezgetc(Z);
|
||||
if (version>VERSION)
|
||||
luaL_verror(
|
||||
"binary file %s too new: version=0x%02x; expected at most 0x%02x",
|
||||
Z->name,version,VERSION);
|
||||
if (version<0x31) /* major change in 3.1 */
|
||||
luaL_verror(
|
||||
"binary file %s too old: version=0x%02x; expected at least 0x%02x",
|
||||
Z->name,version,0x31);
|
||||
sizeofR=ezgetc(Z); /* test float representation */
|
||||
if (sizeofR==sizeof(float))
|
||||
{
|
||||
float f,tf=TEST_FLOAT;
|
||||
ezread(Z,&f,sizeof(f));
|
||||
if (f!=tf)
|
||||
{
|
||||
SwapFloat(&f);
|
||||
if (f!=tf)
|
||||
luaL_verror("unknown float representation in binary file %s",Z->name);
|
||||
S->SwapNumber=1;
|
||||
}
|
||||
S->LoadFloat=1;
|
||||
}
|
||||
else if (sizeofR==sizeof(double))
|
||||
{
|
||||
double f,tf=TEST_FLOAT;
|
||||
ezread(Z,&f,sizeof(f));
|
||||
if (f!=tf)
|
||||
{
|
||||
SwapDouble(&f);
|
||||
if (f!=tf)
|
||||
luaL_verror("unknown float representation in binary file %s",Z->name);
|
||||
S->SwapNumber=1;
|
||||
}
|
||||
S->LoadFloat=0;
|
||||
}
|
||||
else
|
||||
luaL_verror(
|
||||
"floats in binary file %s have %d bytes; "
|
||||
"expected %d (float) or %d (double)",
|
||||
Z->name,sizeofR,sizeof(float),sizeof(double));
|
||||
}
|
||||
|
||||
static TProtoFunc* LoadChunk(Sundump* S)
|
||||
{
|
||||
LoadHeader(S);
|
||||
return LoadFunction(S);
|
||||
}
|
||||
|
||||
/*
|
||||
** load one chunk from a file or buffer
|
||||
** return main if ok and NULL at EOF
|
||||
*/
|
||||
TProtoFunc* luaU_undump1(ZIO* Z)
|
||||
{
|
||||
int c=zgetc(Z);
|
||||
Sundump S;
|
||||
S.Z=Z;
|
||||
S.SwapNumber=0;
|
||||
S.LoadFloat=1;
|
||||
if (c==ID_CHUNK)
|
||||
return LoadChunk(&S);
|
||||
else if (c!=EOZ)
|
||||
luaL_verror("%s is not a lua binary file",Z->name);
|
||||
return NULL;
|
||||
}
|
||||
27
lundump.h
27
lundump.h
@@ -1,27 +0,0 @@
|
||||
/*
|
||||
** $Id: lundump.h,v 1.4 1998/01/13 20:05:24 lhf Exp $
|
||||
** load pre-compiled Lua chunks
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lundump_h
|
||||
#define lundump_h
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lzio.h"
|
||||
|
||||
#define ID_CHUNK 27 /* ESC */
|
||||
#define ID_FUNCTION '#'
|
||||
#define ID_END '$'
|
||||
#define ID_NUM 'N'
|
||||
#define ID_STR 'S'
|
||||
#define ID_FUN 'F'
|
||||
#define SIGNATURE "Lua"
|
||||
#define VERSION 0x31 /* last format change was in 3.1 */
|
||||
#define TEST_FLOAT 0.123456789e-23 /* a float for testing representation */
|
||||
|
||||
#define IsMain(f) (f->lineDefined==0)
|
||||
|
||||
TProtoFunc* luaU_undump1(ZIO* Z); /* load one chunk */
|
||||
|
||||
#endif
|
||||
717
lvm.c
717
lvm.c
@@ -1,717 +0,0 @@
|
||||
/*
|
||||
** $Id: lvm.c,v 1.22 1998/01/12 13:35:37 roberto Exp roberto $
|
||||
** Lua virtual machine
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lgc.h"
|
||||
#include "lmem.h"
|
||||
#include "lopcodes.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.h"
|
||||
#include "ltable.h"
|
||||
#include "ltm.h"
|
||||
#include "luadebug.h"
|
||||
#include "lvm.h"
|
||||
|
||||
|
||||
#ifdef OLD_ANSI
|
||||
#define strcoll(a,b) strcmp(a,b)
|
||||
#endif
|
||||
|
||||
|
||||
#define skip_word(pc) (pc+=2)
|
||||
#define get_word(pc) (*(pc)+(*((pc)+1)<<8))
|
||||
#define next_word(pc) (pc+=2, get_word(pc-2))
|
||||
|
||||
|
||||
/* Extra stack size to run a function: LUA_T_LINE(1), TM calls(2), ... */
|
||||
#define EXTRA_STACK 5
|
||||
|
||||
|
||||
|
||||
static TaggedString *strconc (char *l, char *r)
|
||||
{
|
||||
size_t nl = strlen(l);
|
||||
char *buffer = luaL_openspace(nl+strlen(r)+1);
|
||||
strcpy(buffer, l);
|
||||
strcpy(buffer+nl, r);
|
||||
return luaS_new(buffer);
|
||||
}
|
||||
|
||||
|
||||
int luaV_tonumber (TObject *obj)
|
||||
{ /* LUA_NUMBER */
|
||||
double t;
|
||||
char c;
|
||||
if (ttype(obj) != LUA_T_STRING)
|
||||
return 1;
|
||||
else if (sscanf(svalue(obj), "%lf %c",&t, &c) == 1) {
|
||||
nvalue(obj) = (real)t;
|
||||
ttype(obj) = LUA_T_NUMBER;
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
||||
int luaV_tostring (TObject *obj)
|
||||
{ /* LUA_NUMBER */
|
||||
if (ttype(obj) != LUA_T_NUMBER)
|
||||
return 1;
|
||||
else {
|
||||
char s[60];
|
||||
real f = nvalue(obj);
|
||||
int i;
|
||||
if ((real)(-MAX_INT) <= f && f <= (real)MAX_INT && (real)(i=(int)f) == f)
|
||||
sprintf (s, "%d", i);
|
||||
else
|
||||
sprintf (s, "%g", (double)nvalue(obj));
|
||||
tsvalue(obj) = luaS_new(s);
|
||||
ttype(obj) = LUA_T_STRING;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaV_closure (int nelems)
|
||||
{
|
||||
if (nelems > 0) {
|
||||
struct Stack *S = &L->stack;
|
||||
Closure *c = luaF_newclosure(nelems);
|
||||
c->consts[0] = *(S->top-1);
|
||||
memcpy(&c->consts[1], S->top-(nelems+1), nelems*sizeof(TObject));
|
||||
S->top -= nelems;
|
||||
ttype(S->top-1) = LUA_T_CLOSURE;
|
||||
(S->top-1)->value.cl = c;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Function to index a table.
|
||||
** Receives the table at top-2 and the index at top-1.
|
||||
*/
|
||||
void luaV_gettable (void)
|
||||
{
|
||||
struct Stack *S = &L->stack;
|
||||
TObject *im;
|
||||
if (ttype(S->top-2) != LUA_T_ARRAY) /* not a table, get "gettable" method */
|
||||
im = luaT_getimbyObj(S->top-2, IM_GETTABLE);
|
||||
else { /* object is a table... */
|
||||
int tg = (S->top-2)->value.a->htag;
|
||||
im = luaT_getim(tg, IM_GETTABLE);
|
||||
if (ttype(im) == LUA_T_NIL) { /* and does not have a "gettable" method */
|
||||
TObject *h = luaH_get(avalue(S->top-2), S->top-1);
|
||||
if (h != NULL && ttype(h) != LUA_T_NIL) {
|
||||
--S->top;
|
||||
*(S->top-1) = *h;
|
||||
}
|
||||
else if (ttype(im=luaT_getim(tg, IM_INDEX)) != LUA_T_NIL)
|
||||
luaD_callTM(im, 2, 1);
|
||||
else {
|
||||
--S->top;
|
||||
ttype(S->top-1) = LUA_T_NIL;
|
||||
}
|
||||
return;
|
||||
}
|
||||
/* else it has a "gettable" method, go through to next command */
|
||||
}
|
||||
/* object is not a table, or it has a "gettable" method */
|
||||
if (ttype(im) != LUA_T_NIL)
|
||||
luaD_callTM(im, 2, 1);
|
||||
else
|
||||
lua_error("indexed expression not a table");
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Function to store indexed based on values at the stack.top
|
||||
** mode = 0: raw store (without internal methods)
|
||||
** mode = 1: normal store (with internal methods)
|
||||
** mode = 2: "deep L->stack.stack" store (with internal methods)
|
||||
*/
|
||||
void luaV_settable (TObject *t, int mode)
|
||||
{
|
||||
struct Stack *S = &L->stack;
|
||||
TObject *im = (mode == 0) ? NULL : luaT_getimbyObj(t, IM_SETTABLE);
|
||||
if (ttype(t) == LUA_T_ARRAY && (im == NULL || ttype(im) == LUA_T_NIL)) {
|
||||
TObject *h = luaH_set(avalue(t), t+1);
|
||||
*h = *(S->top-1);
|
||||
S->top -= (mode == 2) ? 1 : 3;
|
||||
}
|
||||
else { /* object is not a table, and/or has a specific "settable" method */
|
||||
if (im && ttype(im) != LUA_T_NIL) {
|
||||
if (mode == 2) {
|
||||
*(S->top+1) = *(L->stack.top-1);
|
||||
*(S->top) = *(t+1);
|
||||
*(S->top-1) = *t;
|
||||
S->top += 2; /* WARNING: caller must assure stack space */
|
||||
}
|
||||
luaD_callTM(im, 3, 0);
|
||||
}
|
||||
else
|
||||
lua_error("indexed expression not a table");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaV_getglobal (TaggedString *ts)
|
||||
{
|
||||
/* WARNING: caller must assure stack space */
|
||||
TObject *value = &ts->u.globalval;
|
||||
TObject *im = luaT_getimbyObj(value, IM_GETGLOBAL);
|
||||
if (ttype(im) == LUA_T_NIL) { /* default behavior */
|
||||
*L->stack.top++ = *value;
|
||||
}
|
||||
else {
|
||||
struct Stack *S = &L->stack;
|
||||
ttype(S->top) = LUA_T_STRING;
|
||||
tsvalue(S->top) = ts;
|
||||
S->top++;
|
||||
*S->top++ = *value;
|
||||
luaD_callTM(im, 2, 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaV_setglobal (TaggedString *ts)
|
||||
{
|
||||
TObject *oldvalue = &ts->u.globalval;
|
||||
TObject *im = luaT_getimbyObj(oldvalue, IM_SETGLOBAL);
|
||||
if (ttype(im) == LUA_T_NIL) /* default behavior */
|
||||
luaS_rawsetglobal(ts, --L->stack.top);
|
||||
else {
|
||||
/* WARNING: caller must assure stack space */
|
||||
struct Stack *S = &L->stack;
|
||||
TObject newvalue = *(S->top-1);
|
||||
ttype(S->top-1) = LUA_T_STRING;
|
||||
tsvalue(S->top-1) = ts;
|
||||
*S->top++ = *oldvalue;
|
||||
*S->top++ = newvalue;
|
||||
luaD_callTM(im, 3, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void call_binTM (IMS event, char *msg)
|
||||
{
|
||||
TObject *im = luaT_getimbyObj(L->stack.top-2, event);/* try first operand */
|
||||
if (ttype(im) == LUA_T_NIL) {
|
||||
im = luaT_getimbyObj(L->stack.top-1, event); /* try second operand */
|
||||
if (ttype(im) == LUA_T_NIL) {
|
||||
im = luaT_getim(0, event); /* try a 'global' i.m. */
|
||||
if (ttype(im) == LUA_T_NIL)
|
||||
lua_error(msg);
|
||||
}
|
||||
}
|
||||
lua_pushstring(luaT_eventname[event]);
|
||||
luaD_callTM(im, 3, 1);
|
||||
}
|
||||
|
||||
|
||||
static void call_arith (IMS event)
|
||||
{
|
||||
call_binTM(event, "unexpected type in arithmetic operation");
|
||||
}
|
||||
|
||||
|
||||
static void comparison (lua_Type ttype_less, lua_Type ttype_equal,
|
||||
lua_Type ttype_great, IMS op)
|
||||
{
|
||||
struct Stack *S = &L->stack;
|
||||
TObject *l = S->top-2;
|
||||
TObject *r = S->top-1;
|
||||
int result;
|
||||
if (ttype(l) == LUA_T_NUMBER && ttype(r) == LUA_T_NUMBER)
|
||||
result = (nvalue(l) < nvalue(r)) ? -1 : (nvalue(l) == nvalue(r)) ? 0 : 1;
|
||||
else if (ttype(l) == LUA_T_STRING && ttype(r) == LUA_T_STRING)
|
||||
result = strcoll(svalue(l), svalue(r));
|
||||
else {
|
||||
call_binTM(op, "unexpected type in comparison");
|
||||
return;
|
||||
}
|
||||
S->top--;
|
||||
nvalue(S->top-1) = 1;
|
||||
ttype(S->top-1) = (result < 0) ? ttype_less :
|
||||
(result == 0) ? ttype_equal : ttype_great;
|
||||
}
|
||||
|
||||
|
||||
void luaV_pack (StkId firstel, int nvararg, TObject *tab)
|
||||
{
|
||||
TObject *firstelem = L->stack.stack+firstel;
|
||||
int i;
|
||||
if (nvararg < 0) nvararg = 0;
|
||||
avalue(tab) = luaH_new(nvararg+1); /* +1 for field 'n' */
|
||||
ttype(tab) = LUA_T_ARRAY;
|
||||
for (i=0; i<nvararg; i++) {
|
||||
TObject index;
|
||||
ttype(&index) = LUA_T_NUMBER;
|
||||
nvalue(&index) = i+1;
|
||||
*(luaH_set(avalue(tab), &index)) = *(firstelem+i);
|
||||
}
|
||||
/* store counter in field "n" */ {
|
||||
TObject index, extra;
|
||||
ttype(&index) = LUA_T_STRING;
|
||||
tsvalue(&index) = luaS_new("n");
|
||||
ttype(&extra) = LUA_T_NUMBER;
|
||||
nvalue(&extra) = nvararg;
|
||||
*(luaH_set(avalue(tab), &index)) = extra;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void adjust_varargs (StkId first_extra_arg)
|
||||
{
|
||||
TObject arg;
|
||||
luaV_pack(first_extra_arg,
|
||||
(L->stack.top-L->stack.stack)-first_extra_arg, &arg);
|
||||
luaD_adjusttop(first_extra_arg);
|
||||
*L->stack.top++ = arg;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Execute the given opcode, until a RET. Parameters are between
|
||||
** [stack+base,top). Returns n such that the the results are between
|
||||
** [stack+n,top).
|
||||
*/
|
||||
StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base)
|
||||
{
|
||||
struct Stack *S = &L->stack; /* to optimize */
|
||||
Byte *pc = tf->code;
|
||||
TObject *consts = tf->consts;
|
||||
if (lua_callhook)
|
||||
luaD_callHook(base, tf, 0);
|
||||
luaD_checkstack((*pc++)+EXTRA_STACK);
|
||||
if (*pc < ZEROVARARG)
|
||||
luaD_adjusttop(base+*(pc++));
|
||||
else { /* varargs */
|
||||
luaC_checkGC();
|
||||
adjust_varargs(base+(*pc++)-ZEROVARARG);
|
||||
}
|
||||
while (1) {
|
||||
int aux;
|
||||
switch ((OpCode)(aux = *pc++)) {
|
||||
|
||||
case PUSHNIL0:
|
||||
ttype(S->top++) = LUA_T_NIL;
|
||||
break;
|
||||
|
||||
case PUSHNIL:
|
||||
aux = *pc++;
|
||||
do {
|
||||
ttype(S->top++) = LUA_T_NIL;
|
||||
} while (aux--);
|
||||
break;
|
||||
|
||||
case PUSHNUMBER:
|
||||
aux = *pc++; goto pushnumber;
|
||||
|
||||
case PUSHNUMBERW:
|
||||
aux = next_word(pc); goto pushnumber;
|
||||
|
||||
case PUSHNUMBER0: case PUSHNUMBER1: case PUSHNUMBER2:
|
||||
aux -= PUSHNUMBER0;
|
||||
pushnumber:
|
||||
ttype(S->top) = LUA_T_NUMBER;
|
||||
nvalue(S->top) = aux;
|
||||
S->top++;
|
||||
break;
|
||||
|
||||
case PUSHLOCAL:
|
||||
aux = *pc++; goto pushlocal;
|
||||
|
||||
case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2: case PUSHLOCAL3:
|
||||
case PUSHLOCAL4: case PUSHLOCAL5: case PUSHLOCAL6: case PUSHLOCAL7:
|
||||
aux -= PUSHLOCAL0;
|
||||
pushlocal:
|
||||
*S->top++ = *((S->stack+base) + aux);
|
||||
break;
|
||||
|
||||
case GETGLOBALW:
|
||||
aux = next_word(pc); goto getglobal;
|
||||
|
||||
case GETGLOBAL:
|
||||
aux = *pc++; goto getglobal;
|
||||
|
||||
case GETGLOBAL0: case GETGLOBAL1: case GETGLOBAL2: case GETGLOBAL3:
|
||||
case GETGLOBAL4: case GETGLOBAL5: case GETGLOBAL6: case GETGLOBAL7:
|
||||
aux -= GETGLOBAL0;
|
||||
getglobal:
|
||||
luaV_getglobal(tsvalue(&consts[aux]));
|
||||
break;
|
||||
|
||||
case GETTABLE:
|
||||
luaV_gettable();
|
||||
break;
|
||||
|
||||
case GETDOTTEDW:
|
||||
aux = next_word(pc); goto getdotted;
|
||||
|
||||
case GETDOTTED:
|
||||
aux = *pc++; goto getdotted;
|
||||
|
||||
case GETDOTTED0: case GETDOTTED1: case GETDOTTED2: case GETDOTTED3:
|
||||
case GETDOTTED4: case GETDOTTED5: case GETDOTTED6: case GETDOTTED7:
|
||||
aux -= GETDOTTED0;
|
||||
getdotted:
|
||||
*S->top++ = consts[aux];
|
||||
luaV_gettable();
|
||||
break;
|
||||
|
||||
case PUSHSELFW:
|
||||
aux = next_word(pc); goto pushself;
|
||||
|
||||
case PUSHSELF:
|
||||
aux = *pc++; goto pushself;
|
||||
|
||||
case PUSHSELF0: case PUSHSELF1: case PUSHSELF2: case PUSHSELF3:
|
||||
case PUSHSELF4: case PUSHSELF5: case PUSHSELF6: case PUSHSELF7:
|
||||
aux -= PUSHSELF0;
|
||||
pushself: {
|
||||
TObject receiver = *(S->top-1);
|
||||
*S->top++ = consts[aux];
|
||||
luaV_gettable();
|
||||
*S->top++ = receiver;
|
||||
break;
|
||||
}
|
||||
|
||||
case PUSHCONSTANTW:
|
||||
aux = next_word(pc); goto pushconstant;
|
||||
|
||||
case PUSHCONSTANT:
|
||||
aux = *pc++; goto pushconstant;
|
||||
|
||||
case PUSHCONSTANT0: case PUSHCONSTANT1: case PUSHCONSTANT2:
|
||||
case PUSHCONSTANT3: case PUSHCONSTANT4: case PUSHCONSTANT5:
|
||||
case PUSHCONSTANT6: case PUSHCONSTANT7:
|
||||
aux -= PUSHCONSTANT0;
|
||||
pushconstant:
|
||||
*S->top++ = consts[aux];
|
||||
break;
|
||||
|
||||
case PUSHUPVALUE:
|
||||
aux = *pc++; goto pushupvalue;
|
||||
|
||||
case PUSHUPVALUE0: case PUSHUPVALUE1:
|
||||
aux -= PUSHUPVALUE0;
|
||||
pushupvalue:
|
||||
*S->top++ = cl->consts[aux+1];
|
||||
break;
|
||||
|
||||
case SETLOCAL:
|
||||
aux = *pc++; goto setlocal;
|
||||
|
||||
case SETLOCAL0: case SETLOCAL1: case SETLOCAL2: case SETLOCAL3:
|
||||
case SETLOCAL4: case SETLOCAL5: case SETLOCAL6: case SETLOCAL7:
|
||||
aux -= SETLOCAL0;
|
||||
setlocal:
|
||||
*((S->stack+base) + aux) = *(--S->top);
|
||||
break;
|
||||
|
||||
case SETGLOBALW:
|
||||
aux = next_word(pc); goto setglobal;
|
||||
|
||||
case SETGLOBAL:
|
||||
aux = *pc++; goto setglobal;
|
||||
|
||||
case SETGLOBAL0: case SETGLOBAL1: case SETGLOBAL2: case SETGLOBAL3:
|
||||
case SETGLOBAL4: case SETGLOBAL5: case SETGLOBAL6: case SETGLOBAL7:
|
||||
aux -= SETGLOBAL0;
|
||||
setglobal:
|
||||
luaV_setglobal(tsvalue(&consts[aux]));
|
||||
break;
|
||||
|
||||
case SETTABLE0:
|
||||
luaV_settable(S->top-3, 1);
|
||||
break;
|
||||
|
||||
case SETTABLE:
|
||||
luaV_settable(S->top-3-(*pc++), 2);
|
||||
break;
|
||||
|
||||
case SETLISTW:
|
||||
aux = next_word(pc); aux *= LFIELDS_PER_FLUSH; goto setlist;
|
||||
|
||||
case SETLIST:
|
||||
aux = *(pc++) * LFIELDS_PER_FLUSH; goto setlist;
|
||||
|
||||
case SETLIST0:
|
||||
aux = 0;
|
||||
setlist: {
|
||||
int n = *(pc++);
|
||||
TObject *arr = S->top-n-1;
|
||||
for (; n; n--) {
|
||||
ttype(S->top) = LUA_T_NUMBER;
|
||||
nvalue(S->top) = n+aux;
|
||||
*(luaH_set(avalue(arr), S->top)) = *(S->top-1);
|
||||
S->top--;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case SETMAP0:
|
||||
aux = 0; goto setmap;
|
||||
|
||||
case SETMAP:
|
||||
aux = *pc++;
|
||||
setmap: {
|
||||
TObject *arr = S->top-(2*aux)-3;
|
||||
do {
|
||||
*(luaH_set(avalue(arr), S->top-2)) = *(S->top-1);
|
||||
S->top-=2;
|
||||
} while (aux--);
|
||||
break;
|
||||
}
|
||||
|
||||
case POP:
|
||||
aux = *pc++; goto pop;
|
||||
|
||||
case POP0: case POP1:
|
||||
aux -= POP0;
|
||||
pop:
|
||||
S->top -= (aux+1);
|
||||
break;
|
||||
|
||||
case CREATEARRAYW:
|
||||
aux = next_word(pc); goto createarray;
|
||||
|
||||
case CREATEARRAY0: case CREATEARRAY1:
|
||||
aux -= CREATEARRAY0; goto createarray;
|
||||
|
||||
case CREATEARRAY:
|
||||
aux = *pc++;
|
||||
createarray:
|
||||
luaC_checkGC();
|
||||
avalue(S->top) = luaH_new(aux);
|
||||
ttype(S->top) = LUA_T_ARRAY;
|
||||
S->top++;
|
||||
break;
|
||||
|
||||
case EQOP: case NEQOP: {
|
||||
int res = luaO_equalObj(S->top-2, S->top-1);
|
||||
S->top--;
|
||||
if (aux == NEQOP) res = !res;
|
||||
ttype(S->top-1) = res ? LUA_T_NUMBER : LUA_T_NIL;
|
||||
nvalue(S->top-1) = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
case LTOP:
|
||||
comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, IM_LT);
|
||||
break;
|
||||
|
||||
case LEOP:
|
||||
comparison(LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, IM_LE);
|
||||
break;
|
||||
|
||||
case GTOP:
|
||||
comparison(LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, IM_GT);
|
||||
break;
|
||||
|
||||
case GEOP:
|
||||
comparison(LUA_T_NIL, LUA_T_NUMBER, LUA_T_NUMBER, IM_GE);
|
||||
break;
|
||||
|
||||
case ADDOP: {
|
||||
TObject *l = S->top-2;
|
||||
TObject *r = S->top-1;
|
||||
if (tonumber(r) || tonumber(l))
|
||||
call_arith(IM_ADD);
|
||||
else {
|
||||
nvalue(l) += nvalue(r);
|
||||
--S->top;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case SUBOP: {
|
||||
TObject *l = S->top-2;
|
||||
TObject *r = S->top-1;
|
||||
if (tonumber(r) || tonumber(l))
|
||||
call_arith(IM_SUB);
|
||||
else {
|
||||
nvalue(l) -= nvalue(r);
|
||||
--S->top;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case MULTOP: {
|
||||
TObject *l = S->top-2;
|
||||
TObject *r = S->top-1;
|
||||
if (tonumber(r) || tonumber(l))
|
||||
call_arith(IM_MUL);
|
||||
else {
|
||||
nvalue(l) *= nvalue(r);
|
||||
--S->top;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case DIVOP: {
|
||||
TObject *l = S->top-2;
|
||||
TObject *r = S->top-1;
|
||||
if (tonumber(r) || tonumber(l))
|
||||
call_arith(IM_DIV);
|
||||
else {
|
||||
nvalue(l) /= nvalue(r);
|
||||
--S->top;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case POWOP:
|
||||
call_arith(IM_POW);
|
||||
break;
|
||||
|
||||
case CONCOP: {
|
||||
TObject *l = S->top-2;
|
||||
TObject *r = S->top-1;
|
||||
if (tostring(l) || tostring(r))
|
||||
call_binTM(IM_CONCAT, "unexpected type for concatenation");
|
||||
else {
|
||||
tsvalue(l) = strconc(svalue(l), svalue(r));
|
||||
--S->top;
|
||||
}
|
||||
luaC_checkGC();
|
||||
break;
|
||||
}
|
||||
|
||||
case MINUSOP:
|
||||
if (tonumber(S->top-1)) {
|
||||
ttype(S->top) = LUA_T_NIL;
|
||||
S->top++;
|
||||
call_arith(IM_UNM);
|
||||
}
|
||||
else
|
||||
nvalue(S->top-1) = - nvalue(S->top-1);
|
||||
break;
|
||||
|
||||
case NOTOP:
|
||||
ttype(S->top-1) =
|
||||
(ttype(S->top-1) == LUA_T_NIL) ? LUA_T_NUMBER : LUA_T_NIL;
|
||||
nvalue(S->top-1) = 1;
|
||||
break;
|
||||
|
||||
case ONTJMPW:
|
||||
aux = next_word(pc); goto ontjmp;
|
||||
|
||||
case ONTJMP:
|
||||
aux = *pc++;
|
||||
ontjmp:
|
||||
if (ttype(S->top-1) != LUA_T_NIL) pc += aux;
|
||||
else S->top--;
|
||||
break;
|
||||
|
||||
case ONFJMPW:
|
||||
aux = next_word(pc); goto onfjmp;
|
||||
|
||||
case ONFJMP:
|
||||
aux = *pc++;
|
||||
onfjmp:
|
||||
if (ttype(S->top-1) == LUA_T_NIL) pc += aux;
|
||||
else S->top--;
|
||||
break;
|
||||
|
||||
case JMPW:
|
||||
aux = next_word(pc); goto jmp;
|
||||
|
||||
case JMP:
|
||||
aux = *pc++;
|
||||
jmp:
|
||||
pc += aux;
|
||||
break;
|
||||
|
||||
case IFFJMPW:
|
||||
aux = next_word(pc); goto iffjmp;
|
||||
|
||||
case IFFJMP:
|
||||
aux = *pc++;
|
||||
iffjmp:
|
||||
if (ttype(--S->top) == LUA_T_NIL) pc += aux;
|
||||
break;
|
||||
|
||||
case IFTUPJMPW:
|
||||
aux = next_word(pc); goto iftupjmp;
|
||||
|
||||
case IFTUPJMP:
|
||||
aux = *pc++;
|
||||
iftupjmp:
|
||||
if (ttype(--S->top) != LUA_T_NIL) pc -= aux;
|
||||
break;
|
||||
|
||||
case IFFUPJMPW:
|
||||
aux = next_word(pc); goto iffupjmp;
|
||||
|
||||
case IFFUPJMP:
|
||||
aux = *pc++;
|
||||
iffupjmp:
|
||||
if (ttype(--S->top) == LUA_T_NIL) pc -= aux;
|
||||
break;
|
||||
|
||||
case CLOSURE:
|
||||
aux = *pc++; goto closure;
|
||||
|
||||
case CLOSURE0: case CLOSURE1:
|
||||
aux -= CLOSURE0;
|
||||
closure:
|
||||
luaV_closure(aux);
|
||||
luaC_checkGC();
|
||||
break;
|
||||
|
||||
case CALLFUNC:
|
||||
aux = *pc++; goto callfunc;
|
||||
|
||||
case CALLFUNC0: case CALLFUNC1:
|
||||
aux -= CALLFUNC0;
|
||||
callfunc: {
|
||||
StkId newBase = (S->top-S->stack)-(*pc++);
|
||||
luaD_call(newBase, aux);
|
||||
break;
|
||||
}
|
||||
|
||||
case ENDCODE:
|
||||
S->top = S->stack + base;
|
||||
/* goes through */
|
||||
case RETCODE:
|
||||
if (lua_callhook)
|
||||
luaD_callHook(base, NULL, 1);
|
||||
return (base + ((aux==RETCODE) ? *pc : 0));
|
||||
|
||||
case SETLINEW:
|
||||
aux = next_word(pc); goto setline;
|
||||
|
||||
case SETLINE:
|
||||
aux = *pc++;
|
||||
setline:
|
||||
if ((S->stack+base-1)->ttype != LUA_T_LINE) {
|
||||
/* open space for LINE value */
|
||||
luaD_openstack((S->top-S->stack)-base);
|
||||
base++;
|
||||
(S->stack+base-1)->ttype = LUA_T_LINE;
|
||||
}
|
||||
(S->stack+base-1)->value.i = aux;
|
||||
if (lua_linehook)
|
||||
luaD_lineHook(aux);
|
||||
break;
|
||||
|
||||
#ifdef DEBUG
|
||||
default:
|
||||
lua_error("internal error - opcode doesn't match");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
29
lvm.h
29
lvm.h
@@ -1,29 +0,0 @@
|
||||
/*
|
||||
** $Id: lvm.h,v 1.3 1997/10/16 10:59:34 roberto Exp roberto $
|
||||
** Lua virtual machine
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lvm_h
|
||||
#define lvm_h
|
||||
|
||||
|
||||
#include "ldo.h"
|
||||
#include "lobject.h"
|
||||
|
||||
|
||||
#define tonumber(o) ((ttype(o) != LUA_T_NUMBER) && (luaV_tonumber(o) != 0))
|
||||
#define tostring(o) ((ttype(o) != LUA_T_STRING) && (luaV_tostring(o) != 0))
|
||||
|
||||
|
||||
void luaV_pack (StkId firstel, int nvararg, TObject *tab);
|
||||
int luaV_tonumber (TObject *obj);
|
||||
int luaV_tostring (TObject *obj);
|
||||
void luaV_gettable (void);
|
||||
void luaV_settable (TObject *t, int mode);
|
||||
void luaV_getglobal (TaggedString *ts);
|
||||
void luaV_setglobal (TaggedString *ts);
|
||||
StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base);
|
||||
void luaV_closure (int nelems);
|
||||
|
||||
#endif
|
||||
84
lzio.c
84
lzio.c
@@ -1,84 +0,0 @@
|
||||
/*
|
||||
** $Id: lzio.c,v 1.2 1997/11/21 19:00:46 roberto Exp roberto $
|
||||
** a generic input stream interface
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
|
||||
/* ----------------------------------------------------- memory buffers --- */
|
||||
|
||||
static int zmfilbuf (ZIO* z)
|
||||
{
|
||||
return EOZ;
|
||||
}
|
||||
|
||||
ZIO* zmopen (ZIO* z, char* b, int size, char *name)
|
||||
{
|
||||
if (b==NULL) return NULL;
|
||||
z->n=size;
|
||||
z->p= (unsigned char *)b;
|
||||
z->filbuf=zmfilbuf;
|
||||
z->u=NULL;
|
||||
z->name=name;
|
||||
return z;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ strings --- */
|
||||
|
||||
ZIO* zsopen (ZIO* z, char* s, char *name)
|
||||
{
|
||||
if (s==NULL) return NULL;
|
||||
return zmopen(z,s,strlen(s),name);
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------- FILEs --- */
|
||||
|
||||
static int zffilbuf (ZIO* z)
|
||||
{
|
||||
int n=fread(z->buffer,1,ZBSIZE,z->u);
|
||||
if (n==0) return EOZ;
|
||||
z->n=n-1;
|
||||
z->p=z->buffer;
|
||||
return *(z->p++);
|
||||
}
|
||||
|
||||
|
||||
ZIO* zFopen (ZIO* z, FILE* f, char *name)
|
||||
{
|
||||
if (f==NULL) return NULL;
|
||||
z->n=0;
|
||||
z->p=z->buffer;
|
||||
z->filbuf=zffilbuf;
|
||||
z->u=f;
|
||||
z->name=name;
|
||||
return z;
|
||||
}
|
||||
|
||||
|
||||
/* --------------------------------------------------------------- read --- */
|
||||
int zread (ZIO *z, void *b, int n)
|
||||
{
|
||||
while (n) {
|
||||
int m;
|
||||
if (z->n == 0) {
|
||||
if (z->filbuf(z) == EOZ)
|
||||
return n; /* retorna quantos faltaram ler */
|
||||
zungetc(z); /* poe o resultado de filbuf no buffer */
|
||||
}
|
||||
m = (n <= z->n) ? n : z->n; /* minimo de n e z->n */
|
||||
memcpy(b, z->p, m);
|
||||
z->n -= m;
|
||||
z->p += m;
|
||||
b = (char *)b + m;
|
||||
n -= m;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
50
lzio.h
50
lzio.h
@@ -1,50 +0,0 @@
|
||||
/*
|
||||
** $Id: lzio.h,v 1.3 1997/12/22 20:57:18 roberto Exp roberto $
|
||||
** Buffered streams
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#ifndef lzio_h
|
||||
#define lzio_h
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
|
||||
/* For Lua only */
|
||||
#define zFopen luaZ_Fopen
|
||||
#define zsopen luaZ_sopen
|
||||
#define zmopen luaZ_mopen
|
||||
#define zread luaZ_read
|
||||
|
||||
#define EOZ (-1) /* end of stream */
|
||||
|
||||
typedef struct zio ZIO;
|
||||
|
||||
ZIO* zFopen (ZIO* z, FILE* f, char *name); /* open FILEs */
|
||||
ZIO* zsopen (ZIO* z, char* s, char *name); /* string */
|
||||
ZIO* zmopen (ZIO* z, char* b, int size, char *name); /* memory */
|
||||
|
||||
int zread (ZIO* z, void* b, int n); /* read next n bytes */
|
||||
|
||||
#define zgetc(z) (--(z)->n>=0 ? ((int)*(z)->p++): (z)->filbuf(z))
|
||||
#define zungetc(z) (++(z)->n,--(z)->p)
|
||||
#define zname(z) ((z)->name)
|
||||
|
||||
|
||||
/* --------- Private Part ------------------ */
|
||||
|
||||
#define ZBSIZE 256 /* buffer size */
|
||||
|
||||
struct zio {
|
||||
int n; /* bytes still unread */
|
||||
unsigned char* p; /* current position in buffer */
|
||||
int (*filbuf)(ZIO* z);
|
||||
void* u; /* additional data */
|
||||
char *name;
|
||||
unsigned char buffer[ZBSIZE]; /* buffer */
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
125
makefile
125
makefile
@@ -1,125 +0,0 @@
|
||||
#
|
||||
## $Id: makefile,v 1.9 1998/01/02 17:46:32 roberto Exp roberto $
|
||||
## Makefile
|
||||
## See Copyright Notice in lua.h
|
||||
#
|
||||
|
||||
|
||||
#CONFIGURATION
|
||||
|
||||
# define (undefine) POPEN if your system (does not) support piped I/O
|
||||
#
|
||||
# define (undefine) _POSIX_SOURCE if your system is (not) POSIX compliant
|
||||
#
|
||||
# define (undefine) OLD_ANSI if your system does NOT have some new ANSI
|
||||
# facilities (e.g. strerror, locale.h, memmove). Although they are ANSI,
|
||||
# SunOS does not comply; so, add "-DOLD_ANSI" on SunOS
|
||||
#
|
||||
# define LUA_COMPAT2_5 if yous system does need to be compatible with
|
||||
# version 2.5 (or older)
|
||||
|
||||
CONFIG = -DPOPEN -D_POSIX_SOURCE
|
||||
#CONFIG = -DLUA_COMPAT2_5 -DOLD_ANSI -DDEBUG
|
||||
|
||||
|
||||
# Compilation parameters
|
||||
CC = gcc
|
||||
CWARNS = -Wall -Wmissing-prototypes -Wshadow -pedantic -Wpointer-arith -Wcast-align -Waggregate-return
|
||||
CFLAGS = $(CONFIG) $(CWARNS) -ansi -O2
|
||||
|
||||
|
||||
AR = ar
|
||||
ARFLAGS = rvl
|
||||
|
||||
|
||||
# Aplication modules
|
||||
LUAOBJS = \
|
||||
lapi.o \
|
||||
lauxlib.o \
|
||||
lbuffer.o \
|
||||
lbuiltin.o \
|
||||
ldo.o \
|
||||
lfunc.o \
|
||||
lgc.o \
|
||||
llex.o \
|
||||
lmem.o \
|
||||
lobject.o \
|
||||
lstate.o \
|
||||
lstx.o \
|
||||
lstring.o \
|
||||
ltable.o \
|
||||
ltm.o \
|
||||
lvm.o \
|
||||
lundump.o \
|
||||
lzio.o
|
||||
|
||||
LIBOBJS = \
|
||||
liolib.o \
|
||||
lmathlib.o \
|
||||
lstrlib.o
|
||||
|
||||
|
||||
lua : lua.o liblua.a liblualib.a
|
||||
$(CC) $(CFLAGS) -o $@ lua.o -L. -llua -llualib -lm
|
||||
|
||||
liblua.a : $(LUAOBJS)
|
||||
$(AR) $(ARFLAGS) $@ $?
|
||||
ranlib $@
|
||||
|
||||
liblualib.a : $(LIBOBJS)
|
||||
$(AR) $(ARFLAGS) $@ $?
|
||||
ranlib $@
|
||||
|
||||
liblua.so.1.0 : lua.o
|
||||
ld -o liblua.so.1.0 $(LUAOBJS)
|
||||
|
||||
lstx.c lstx.h : lua.stx
|
||||
bison -o lstx.c -p luaY_ -d lua.stx
|
||||
# yacc -d lua.stx
|
||||
# sed -e 's/yy/luaY_/g' -e 's/malloc\.h/stdlib\.h/g' y.tab.c > lstx.c
|
||||
# sed -e 's/yy/luaY_/g' y.tab.h > lstx.h
|
||||
|
||||
clear :
|
||||
rcsclean
|
||||
rm -f *.o
|
||||
rm -f lstx.c lstx.h
|
||||
co lua.h lualib.h luadebug.h
|
||||
|
||||
|
||||
%.h : RCS/%.h,v
|
||||
co $@
|
||||
|
||||
%.c : RCS/%.c,v
|
||||
co $@
|
||||
|
||||
lapi.o: lapi.c lapi.h lua.h lobject.h lauxlib.h ldo.h lstate.h lfunc.h \
|
||||
lgc.h lmem.h lstring.h ltable.h ltm.h luadebug.h lvm.h
|
||||
lauxlib.o: lauxlib.c lauxlib.h lua.h luadebug.h
|
||||
lbuffer.o: lbuffer.c lauxlib.h lua.h lmem.h lstate.h lobject.h
|
||||
lbuiltin.o: lbuiltin.c lapi.h lua.h lobject.h lauxlib.h lbuiltin.h \
|
||||
ldo.h lstate.h lfunc.h lmem.h lstring.h ltable.h ltm.h
|
||||
ldo.o: ldo.c ldo.h lobject.h lua.h lstate.h lfunc.h lgc.h lmem.h \
|
||||
lparser.h lzio.h ltm.h luadebug.h lundump.h lvm.h
|
||||
lfunc.o: lfunc.c lfunc.h lobject.h lua.h lmem.h lstate.h
|
||||
lgc.o: lgc.c ldo.h lobject.h lua.h lstate.h lfunc.h lgc.h lmem.h \
|
||||
lstring.h ltable.h ltm.h
|
||||
liolib.o: liolib.c lauxlib.h lua.h luadebug.h lualib.h
|
||||
llex.o: llex.c lauxlib.h lua.h llex.h lobject.h lzio.h lmem.h \
|
||||
lparser.h lstate.h lstring.h lstx.h luadebug.h
|
||||
lmathlib.o: lmathlib.c lauxlib.h lua.h lualib.h
|
||||
lmem.o: lmem.c lmem.h lstate.h lobject.h lua.h
|
||||
lobject.o: lobject.c lobject.h lua.h
|
||||
lstate.o: lstate.c lbuiltin.h ldo.h lobject.h lua.h lstate.h lfunc.h \
|
||||
lgc.h llex.h lzio.h lmem.h lstring.h ltable.h ltm.h
|
||||
lstring.o: lstring.c lmem.h lobject.h lua.h lstate.h lstring.h
|
||||
lstrlib.o: lstrlib.c lauxlib.h lua.h lualib.h
|
||||
lstx.o: lstx.c lauxlib.h lua.h ldo.h lobject.h lstate.h lfunc.h llex.h \
|
||||
lzio.h lmem.h lopcodes.h lparser.h lstring.h luadebug.h
|
||||
ltable.o: ltable.c lauxlib.h lua.h lmem.h lobject.h lstate.h ltable.h
|
||||
ltm.o: ltm.c lauxlib.h lua.h lmem.h lobject.h lstate.h ltm.h lapi.h
|
||||
lua.o: lua.c lua.h luadebug.h lualib.h
|
||||
lundump.o: lundump.c lauxlib.h lua.h lfunc.h lobject.h lmem.h \
|
||||
lstring.h lundump.h lzio.h
|
||||
lvm.o: lvm.c lauxlib.h lua.h ldo.h lobject.h lstate.h lfunc.h lgc.h \
|
||||
lmem.h lopcodes.h lstring.h ltable.h ltm.h luadebug.h lvm.h
|
||||
lzio.o: lzio.c lzio.h
|
||||
2749
manual.tex
2749
manual.tex
File diff suppressed because it is too large
Load Diff
234
mathlib.c
Normal file
234
mathlib.c
Normal file
@@ -0,0 +1,234 @@
|
||||
/*
|
||||
** mathlib.c
|
||||
** Mathematica library to LUA
|
||||
**
|
||||
** Waldemar Celes Filho
|
||||
** TeCGraf - PUC-Rio
|
||||
** 19 May 93
|
||||
*/
|
||||
|
||||
#include <stdio.h> /* NULL */
|
||||
#include <math.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
static void math_abs (void)
|
||||
{
|
||||
double d;
|
||||
lua_Object o = lua_getparam (1);
|
||||
if (o == NULL)
|
||||
{ lua_error ("too few arguments to function `abs'"); return; }
|
||||
if (!lua_isnumber(o))
|
||||
{ lua_error ("incorrect arguments to function `abs'"); return; }
|
||||
d = lua_getnumber(o);
|
||||
if (d < 0) d = -d;
|
||||
lua_pushnumber (d);
|
||||
}
|
||||
|
||||
|
||||
static void math_sin (void)
|
||||
{
|
||||
double d;
|
||||
lua_Object o = lua_getparam (1);
|
||||
if (o == NULL)
|
||||
{ lua_error ("too few arguments to function `sin'"); return; }
|
||||
if (!lua_isnumber(o))
|
||||
{ lua_error ("incorrect arguments to function `sin'"); return; }
|
||||
d = lua_getnumber(o);
|
||||
lua_pushnumber (sin(d));
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void math_cos (void)
|
||||
{
|
||||
double d;
|
||||
lua_Object o = lua_getparam (1);
|
||||
if (o == NULL)
|
||||
{ lua_error ("too few arguments to function `cos'"); return; }
|
||||
if (!lua_isnumber(o))
|
||||
{ lua_error ("incorrect arguments to function `cos'"); return; }
|
||||
d = lua_getnumber(o);
|
||||
lua_pushnumber (cos(d));
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void math_tan (void)
|
||||
{
|
||||
double d;
|
||||
lua_Object o = lua_getparam (1);
|
||||
if (o == NULL)
|
||||
{ lua_error ("too few arguments to function `tan'"); return; }
|
||||
if (!lua_isnumber(o))
|
||||
{ lua_error ("incorrect arguments to function `tan'"); return; }
|
||||
d = lua_getnumber(o);
|
||||
lua_pushnumber (tan(d));
|
||||
}
|
||||
|
||||
|
||||
static void math_asin (void)
|
||||
{
|
||||
double d;
|
||||
lua_Object o = lua_getparam (1);
|
||||
if (o == NULL)
|
||||
{ lua_error ("too few arguments to function `asin'"); return; }
|
||||
if (!lua_isnumber(o))
|
||||
{ lua_error ("incorrect arguments to function `asin'"); return; }
|
||||
d = lua_getnumber(o);
|
||||
lua_pushnumber (asin(d));
|
||||
}
|
||||
|
||||
|
||||
static void math_acos (void)
|
||||
{
|
||||
double d;
|
||||
lua_Object o = lua_getparam (1);
|
||||
if (o == NULL)
|
||||
{ lua_error ("too few arguments to function `acos'"); return; }
|
||||
if (!lua_isnumber(o))
|
||||
{ lua_error ("incorrect arguments to function `acos'"); return; }
|
||||
d = lua_getnumber(o);
|
||||
lua_pushnumber (acos(d));
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void math_atan (void)
|
||||
{
|
||||
double d;
|
||||
lua_Object o = lua_getparam (1);
|
||||
if (o == NULL)
|
||||
{ lua_error ("too few arguments to function `atan'"); return; }
|
||||
if (!lua_isnumber(o))
|
||||
{ lua_error ("incorrect arguments to function `atan'"); return; }
|
||||
d = lua_getnumber(o);
|
||||
lua_pushnumber (atan(d));
|
||||
}
|
||||
|
||||
|
||||
static void math_ceil (void)
|
||||
{
|
||||
double d;
|
||||
lua_Object o = lua_getparam (1);
|
||||
if (o == NULL)
|
||||
{ lua_error ("too few arguments to function `ceil'"); return; }
|
||||
if (!lua_isnumber(o))
|
||||
{ lua_error ("incorrect arguments to function `ceil'"); return; }
|
||||
d = lua_getnumber(o);
|
||||
lua_pushnumber (ceil(d));
|
||||
}
|
||||
|
||||
|
||||
static void math_floor (void)
|
||||
{
|
||||
double d;
|
||||
lua_Object o = lua_getparam (1);
|
||||
if (o == NULL)
|
||||
{ lua_error ("too few arguments to function `floor'"); return; }
|
||||
if (!lua_isnumber(o))
|
||||
{ lua_error ("incorrect arguments to function `floor'"); return; }
|
||||
d = lua_getnumber(o);
|
||||
lua_pushnumber (floor(d));
|
||||
}
|
||||
|
||||
static void math_mod (void)
|
||||
{
|
||||
int d1, d2;
|
||||
lua_Object o1 = lua_getparam (1);
|
||||
lua_Object o2 = lua_getparam (2);
|
||||
if (!lua_isnumber(o1) || !lua_isnumber(o2))
|
||||
{ lua_error ("incorrect arguments to function `mod'"); return; }
|
||||
d1 = (int) lua_getnumber(o1);
|
||||
d2 = (int) lua_getnumber(o2);
|
||||
lua_pushnumber (d1%d2);
|
||||
}
|
||||
|
||||
|
||||
static void math_sqrt (void)
|
||||
{
|
||||
double d;
|
||||
lua_Object o = lua_getparam (1);
|
||||
if (o == NULL)
|
||||
{ lua_error ("too few arguments to function `sqrt'"); return; }
|
||||
if (!lua_isnumber(o))
|
||||
{ lua_error ("incorrect arguments to function `sqrt'"); return; }
|
||||
d = lua_getnumber(o);
|
||||
lua_pushnumber (sqrt(d));
|
||||
}
|
||||
|
||||
static void math_pow (void)
|
||||
{
|
||||
double d1, d2;
|
||||
lua_Object o1 = lua_getparam (1);
|
||||
lua_Object o2 = lua_getparam (2);
|
||||
if (!lua_isnumber(o1) || !lua_isnumber(o2))
|
||||
{ lua_error ("incorrect arguments to function `pow'"); return; }
|
||||
d1 = lua_getnumber(o1);
|
||||
d2 = lua_getnumber(o2);
|
||||
lua_pushnumber (pow(d1,d2));
|
||||
}
|
||||
|
||||
static void math_min (void)
|
||||
{
|
||||
int i=1;
|
||||
double d, dmin;
|
||||
lua_Object o;
|
||||
if ((o = lua_getparam(i++)) == NULL)
|
||||
{ lua_error ("too few arguments to function `min'"); return; }
|
||||
if (!lua_isnumber(o))
|
||||
{ lua_error ("incorrect arguments to function `min'"); return; }
|
||||
dmin = lua_getnumber (o);
|
||||
while ((o = lua_getparam(i++)) != NULL)
|
||||
{
|
||||
if (!lua_isnumber(o))
|
||||
{ lua_error ("incorrect arguments to function `min'"); return; }
|
||||
d = lua_getnumber (o);
|
||||
if (d < dmin) dmin = d;
|
||||
}
|
||||
lua_pushnumber (dmin);
|
||||
}
|
||||
|
||||
|
||||
static void math_max (void)
|
||||
{
|
||||
int i=1;
|
||||
double d, dmax;
|
||||
lua_Object o;
|
||||
if ((o = lua_getparam(i++)) == NULL)
|
||||
{ lua_error ("too few arguments to function `max'"); return; }
|
||||
if (!lua_isnumber(o))
|
||||
{ lua_error ("incorrect arguments to function `max'"); return; }
|
||||
dmax = lua_getnumber (o);
|
||||
while ((o = lua_getparam(i++)) != NULL)
|
||||
{
|
||||
if (!lua_isnumber(o))
|
||||
{ lua_error ("incorrect arguments to function `max'"); return; }
|
||||
d = lua_getnumber (o);
|
||||
if (d > dmax) dmax = d;
|
||||
}
|
||||
lua_pushnumber (dmax);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Open math library
|
||||
*/
|
||||
void mathlib_open (void)
|
||||
{
|
||||
lua_register ("abs", math_abs);
|
||||
lua_register ("sin", math_sin);
|
||||
lua_register ("cos", math_cos);
|
||||
lua_register ("tan", math_tan);
|
||||
lua_register ("asin", math_asin);
|
||||
lua_register ("acos", math_acos);
|
||||
lua_register ("atan", math_atan);
|
||||
lua_register ("ceil", math_ceil);
|
||||
lua_register ("floor", math_floor);
|
||||
lua_register ("mod", math_mod);
|
||||
lua_register ("sqrt", math_sqrt);
|
||||
lua_register ("pow", math_pow);
|
||||
lua_register ("min", math_min);
|
||||
lua_register ("max", math_max);
|
||||
}
|
||||
933
opcode.c
Normal file
933
opcode.c
Normal file
@@ -0,0 +1,933 @@
|
||||
/*
|
||||
** opcode.c
|
||||
** TecCGraf - PUC-Rio
|
||||
** 26 Apr 93
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#ifdef __GNUC__
|
||||
#include <floatingpoint.h>
|
||||
#endif
|
||||
|
||||
#include "opcode.h"
|
||||
#include "hash.h"
|
||||
#include "inout.h"
|
||||
#include "table.h"
|
||||
#include "lua.h"
|
||||
|
||||
#define tonumber(o) ((tag(o) != T_NUMBER) && (lua_tonumber(o) != 0))
|
||||
#define tostring(o) ((tag(o) != T_STRING) && (lua_tostring(o) != 0))
|
||||
|
||||
#ifndef MAXSTACK
|
||||
#define MAXSTACK 256
|
||||
#endif
|
||||
static Object stack[MAXSTACK] = {{T_MARK, {NULL}}};
|
||||
static Object *top=stack+1, *base=stack+1;
|
||||
|
||||
|
||||
/*
|
||||
** Concatenate two given string, creating a mark space at the beginning.
|
||||
** Return the new string pointer.
|
||||
*/
|
||||
static char *lua_strconc (char *l, char *r)
|
||||
{
|
||||
char *s = calloc (strlen(l)+strlen(r)+2, sizeof(char));
|
||||
if (s == NULL)
|
||||
{
|
||||
lua_error ("not enough memory");
|
||||
return NULL;
|
||||
}
|
||||
*s++ = 0; /* create mark space */
|
||||
return strcat(strcpy(s,l),r);
|
||||
}
|
||||
|
||||
/*
|
||||
** Duplicate a string, creating a mark space at the beginning.
|
||||
** Return the new string pointer.
|
||||
*/
|
||||
char *lua_strdup (char *l)
|
||||
{
|
||||
char *s = calloc (strlen(l)+2, sizeof(char));
|
||||
if (s == NULL)
|
||||
{
|
||||
lua_error ("not enough memory");
|
||||
return NULL;
|
||||
}
|
||||
*s++ = 0; /* create mark space */
|
||||
return strcpy(s,l);
|
||||
}
|
||||
|
||||
/*
|
||||
** Convert, if possible, to a number tag.
|
||||
** Return 0 in success or not 0 on error.
|
||||
*/
|
||||
static int lua_tonumber (Object *obj)
|
||||
{
|
||||
char *ptr;
|
||||
if (tag(obj) != T_STRING)
|
||||
{
|
||||
lua_reportbug ("unexpected type at conversion to number");
|
||||
return 1;
|
||||
}
|
||||
nvalue(obj) = strtod(svalue(obj), &ptr);
|
||||
if (*ptr)
|
||||
{
|
||||
lua_reportbug ("string to number convertion failed");
|
||||
return 2;
|
||||
}
|
||||
tag(obj) = T_NUMBER;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
** Test if is possible to convert an object to a number one.
|
||||
** If possible, return the converted object, otherwise return nil object.
|
||||
*/
|
||||
static Object *lua_convtonumber (Object *obj)
|
||||
{
|
||||
static Object cvt;
|
||||
|
||||
if (tag(obj) == T_NUMBER)
|
||||
{
|
||||
cvt = *obj;
|
||||
return &cvt;
|
||||
}
|
||||
|
||||
tag(&cvt) = T_NIL;
|
||||
if (tag(obj) == T_STRING)
|
||||
{
|
||||
char *ptr;
|
||||
nvalue(&cvt) = strtod(svalue(obj), &ptr);
|
||||
if (*ptr == 0)
|
||||
tag(&cvt) = T_NUMBER;
|
||||
}
|
||||
return &cvt;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** Convert, if possible, to a string tag
|
||||
** Return 0 in success or not 0 on error.
|
||||
*/
|
||||
static int lua_tostring (Object *obj)
|
||||
{
|
||||
static char s[256];
|
||||
if (tag(obj) != T_NUMBER)
|
||||
{
|
||||
lua_reportbug ("unexpected type at conversion to string");
|
||||
return 1;
|
||||
}
|
||||
if ((int) nvalue(obj) == nvalue(obj))
|
||||
sprintf (s, "%d", (int) nvalue(obj));
|
||||
else
|
||||
sprintf (s, "%g", nvalue(obj));
|
||||
svalue(obj) = lua_createstring(lua_strdup(s));
|
||||
if (svalue(obj) == NULL)
|
||||
return 1;
|
||||
tag(obj) = T_STRING;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Execute the given opcode. Return 0 in success or 1 on error.
|
||||
*/
|
||||
int lua_execute (Byte *pc)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
switch ((OpCode)*pc++)
|
||||
{
|
||||
case NOP: break;
|
||||
|
||||
case PUSHNIL: tag(top++) = T_NIL; break;
|
||||
|
||||
case PUSH0: tag(top) = T_NUMBER; nvalue(top++) = 0; break;
|
||||
case PUSH1: tag(top) = T_NUMBER; nvalue(top++) = 1; break;
|
||||
case PUSH2: tag(top) = T_NUMBER; nvalue(top++) = 2; break;
|
||||
|
||||
case PUSHBYTE: tag(top) = T_NUMBER; nvalue(top++) = *pc++; break;
|
||||
|
||||
case PUSHWORD:
|
||||
tag(top) = T_NUMBER; nvalue(top++) = *((Word *)(pc)); pc += sizeof(Word);
|
||||
break;
|
||||
|
||||
case PUSHFLOAT:
|
||||
tag(top) = T_NUMBER; nvalue(top++) = *((float *)(pc)); pc += sizeof(float);
|
||||
break;
|
||||
case PUSHSTRING:
|
||||
{
|
||||
int w = *((Word *)(pc));
|
||||
pc += sizeof(Word);
|
||||
tag(top) = T_STRING; svalue(top++) = lua_constant[w];
|
||||
}
|
||||
break;
|
||||
|
||||
case PUSHLOCAL0: *top++ = *(base + 0); break;
|
||||
case PUSHLOCAL1: *top++ = *(base + 1); break;
|
||||
case PUSHLOCAL2: *top++ = *(base + 2); break;
|
||||
case PUSHLOCAL3: *top++ = *(base + 3); break;
|
||||
case PUSHLOCAL4: *top++ = *(base + 4); break;
|
||||
case PUSHLOCAL5: *top++ = *(base + 5); break;
|
||||
case PUSHLOCAL6: *top++ = *(base + 6); break;
|
||||
case PUSHLOCAL7: *top++ = *(base + 7); break;
|
||||
case PUSHLOCAL8: *top++ = *(base + 8); break;
|
||||
case PUSHLOCAL9: *top++ = *(base + 9); break;
|
||||
|
||||
case PUSHLOCAL: *top++ = *(base + (*pc++)); break;
|
||||
|
||||
case PUSHGLOBAL:
|
||||
*top++ = s_object(*((Word *)(pc))); pc += sizeof(Word);
|
||||
break;
|
||||
|
||||
case PUSHINDEXED:
|
||||
--top;
|
||||
if (tag(top-1) != T_ARRAY)
|
||||
{
|
||||
lua_reportbug ("indexed expression not a table");
|
||||
return 1;
|
||||
}
|
||||
{
|
||||
Object *h = lua_hashdefine (avalue(top-1), top);
|
||||
if (h == NULL) return 1;
|
||||
*(top-1) = *h;
|
||||
}
|
||||
break;
|
||||
|
||||
case PUSHMARK: tag(top++) = T_MARK; break;
|
||||
|
||||
case PUSHOBJECT: *top = *(top-3); top++; break;
|
||||
|
||||
case STORELOCAL0: *(base + 0) = *(--top); break;
|
||||
case STORELOCAL1: *(base + 1) = *(--top); break;
|
||||
case STORELOCAL2: *(base + 2) = *(--top); break;
|
||||
case STORELOCAL3: *(base + 3) = *(--top); break;
|
||||
case STORELOCAL4: *(base + 4) = *(--top); break;
|
||||
case STORELOCAL5: *(base + 5) = *(--top); break;
|
||||
case STORELOCAL6: *(base + 6) = *(--top); break;
|
||||
case STORELOCAL7: *(base + 7) = *(--top); break;
|
||||
case STORELOCAL8: *(base + 8) = *(--top); break;
|
||||
case STORELOCAL9: *(base + 9) = *(--top); break;
|
||||
|
||||
case STORELOCAL: *(base + (*pc++)) = *(--top); break;
|
||||
|
||||
case STOREGLOBAL:
|
||||
s_object(*((Word *)(pc))) = *(--top); pc += sizeof(Word);
|
||||
break;
|
||||
|
||||
case STOREINDEXED0:
|
||||
if (tag(top-3) != T_ARRAY)
|
||||
{
|
||||
lua_reportbug ("indexed expression not a table");
|
||||
return 1;
|
||||
}
|
||||
{
|
||||
Object *h = lua_hashdefine (avalue(top-3), top-2);
|
||||
if (h == NULL) return 1;
|
||||
*h = *(top-1);
|
||||
}
|
||||
top -= 3;
|
||||
break;
|
||||
|
||||
case STOREINDEXED:
|
||||
{
|
||||
int n = *pc++;
|
||||
if (tag(top-3-n) != T_ARRAY)
|
||||
{
|
||||
lua_reportbug ("indexed expression not a table");
|
||||
return 1;
|
||||
}
|
||||
{
|
||||
Object *h = lua_hashdefine (avalue(top-3-n), top-2-n);
|
||||
if (h == NULL) return 1;
|
||||
*h = *(top-1);
|
||||
}
|
||||
--top;
|
||||
}
|
||||
break;
|
||||
|
||||
case STOREFIELD:
|
||||
if (tag(top-3) != T_ARRAY)
|
||||
{
|
||||
lua_error ("internal error - table expected");
|
||||
return 1;
|
||||
}
|
||||
*(lua_hashdefine (avalue(top-3), top-2)) = *(top-1);
|
||||
top -= 2;
|
||||
break;
|
||||
|
||||
case ADJUST:
|
||||
{
|
||||
Object *newtop = base + *(pc++);
|
||||
if (top != newtop)
|
||||
{
|
||||
while (top < newtop) tag(top++) = T_NIL;
|
||||
top = newtop;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case CREATEARRAY:
|
||||
if (tag(top-1) == T_NIL)
|
||||
nvalue(top-1) = 101;
|
||||
else
|
||||
{
|
||||
if (tonumber(top-1)) return 1;
|
||||
if (nvalue(top-1) <= 0) nvalue(top-1) = 101;
|
||||
}
|
||||
avalue(top-1) = lua_createarray(lua_hashcreate(nvalue(top-1)));
|
||||
if (avalue(top-1) == NULL)
|
||||
return 1;
|
||||
tag(top-1) = T_ARRAY;
|
||||
break;
|
||||
|
||||
case EQOP:
|
||||
{
|
||||
Object *l = top-2;
|
||||
Object *r = top-1;
|
||||
--top;
|
||||
if (tag(l) != tag(r))
|
||||
tag(top-1) = T_NIL;
|
||||
else
|
||||
{
|
||||
switch (tag(l))
|
||||
{
|
||||
case T_NIL: tag(top-1) = T_NUMBER; break;
|
||||
case T_NUMBER: tag(top-1) = (nvalue(l) == nvalue(r)) ? T_NUMBER : T_NIL; break;
|
||||
case T_ARRAY: tag(top-1) = (avalue(l) == avalue(r)) ? T_NUMBER : T_NIL; break;
|
||||
case T_FUNCTION: tag(top-1) = (bvalue(l) == bvalue(r)) ? T_NUMBER : T_NIL; break;
|
||||
case T_CFUNCTION: tag(top-1) = (fvalue(l) == fvalue(r)) ? T_NUMBER : T_NIL; break;
|
||||
case T_USERDATA: tag(top-1) = (uvalue(l) == uvalue(r)) ? T_NUMBER : T_NIL; break;
|
||||
case T_STRING: tag(top-1) = (strcmp (svalue(l), svalue(r)) == 0) ? T_NUMBER : T_NIL; break;
|
||||
case T_MARK: return 1;
|
||||
}
|
||||
}
|
||||
nvalue(top-1) = 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case LTOP:
|
||||
{
|
||||
Object *l = top-2;
|
||||
Object *r = top-1;
|
||||
--top;
|
||||
if (tag(l) == T_NUMBER && tag(r) == T_NUMBER)
|
||||
tag(top-1) = (nvalue(l) < nvalue(r)) ? T_NUMBER : T_NIL;
|
||||
else
|
||||
{
|
||||
if (tostring(l) || tostring(r))
|
||||
return 1;
|
||||
tag(top-1) = (strcmp (svalue(l), svalue(r)) < 0) ? T_NUMBER : T_NIL;
|
||||
}
|
||||
nvalue(top-1) = 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case LEOP:
|
||||
{
|
||||
Object *l = top-2;
|
||||
Object *r = top-1;
|
||||
--top;
|
||||
if (tag(l) == T_NUMBER && tag(r) == T_NUMBER)
|
||||
tag(top-1) = (nvalue(l) <= nvalue(r)) ? T_NUMBER : T_NIL;
|
||||
else
|
||||
{
|
||||
if (tostring(l) || tostring(r))
|
||||
return 1;
|
||||
tag(top-1) = (strcmp (svalue(l), svalue(r)) <= 0) ? T_NUMBER : T_NIL;
|
||||
}
|
||||
nvalue(top-1) = 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case ADDOP:
|
||||
{
|
||||
Object *l = top-2;
|
||||
Object *r = top-1;
|
||||
if (tonumber(r) || tonumber(l))
|
||||
return 1;
|
||||
nvalue(l) += nvalue(r);
|
||||
--top;
|
||||
}
|
||||
break;
|
||||
|
||||
case SUBOP:
|
||||
{
|
||||
Object *l = top-2;
|
||||
Object *r = top-1;
|
||||
if (tonumber(r) || tonumber(l))
|
||||
return 1;
|
||||
nvalue(l) -= nvalue(r);
|
||||
--top;
|
||||
}
|
||||
break;
|
||||
|
||||
case MULTOP:
|
||||
{
|
||||
Object *l = top-2;
|
||||
Object *r = top-1;
|
||||
if (tonumber(r) || tonumber(l))
|
||||
return 1;
|
||||
nvalue(l) *= nvalue(r);
|
||||
--top;
|
||||
}
|
||||
break;
|
||||
|
||||
case DIVOP:
|
||||
{
|
||||
Object *l = top-2;
|
||||
Object *r = top-1;
|
||||
if (tonumber(r) || tonumber(l))
|
||||
return 1;
|
||||
nvalue(l) /= nvalue(r);
|
||||
--top;
|
||||
}
|
||||
break;
|
||||
|
||||
case CONCOP:
|
||||
{
|
||||
Object *l = top-2;
|
||||
Object *r = top-1;
|
||||
if (tostring(r) || tostring(l))
|
||||
return 1;
|
||||
svalue(l) = lua_createstring (lua_strconc(svalue(l),svalue(r)));
|
||||
if (svalue(l) == NULL)
|
||||
return 1;
|
||||
--top;
|
||||
}
|
||||
break;
|
||||
|
||||
case MINUSOP:
|
||||
if (tonumber(top-1))
|
||||
return 1;
|
||||
nvalue(top-1) = - nvalue(top-1);
|
||||
break;
|
||||
|
||||
case NOTOP:
|
||||
tag(top-1) = tag(top-1) == T_NIL ? T_NUMBER : T_NIL;
|
||||
break;
|
||||
|
||||
case ONTJMP:
|
||||
{
|
||||
int n = *((Word *)(pc));
|
||||
pc += sizeof(Word);
|
||||
if (tag(top-1) != T_NIL) pc += n;
|
||||
}
|
||||
break;
|
||||
|
||||
case ONFJMP:
|
||||
{
|
||||
int n = *((Word *)(pc));
|
||||
pc += sizeof(Word);
|
||||
if (tag(top-1) == T_NIL) pc += n;
|
||||
}
|
||||
break;
|
||||
|
||||
case JMP: pc += *((Word *)(pc)) + sizeof(Word); break;
|
||||
|
||||
case UPJMP: pc -= *((Word *)(pc)) - sizeof(Word); break;
|
||||
|
||||
case IFFJMP:
|
||||
{
|
||||
int n = *((Word *)(pc));
|
||||
pc += sizeof(Word);
|
||||
top--;
|
||||
if (tag(top) == T_NIL) pc += n;
|
||||
}
|
||||
break;
|
||||
|
||||
case IFFUPJMP:
|
||||
{
|
||||
int n = *((Word *)(pc));
|
||||
pc += sizeof(Word);
|
||||
top--;
|
||||
if (tag(top) == T_NIL) pc -= n;
|
||||
}
|
||||
break;
|
||||
|
||||
case POP: --top; break;
|
||||
|
||||
case CALLFUNC:
|
||||
{
|
||||
Byte *newpc;
|
||||
Object *b = top-1;
|
||||
while (tag(b) != T_MARK) b--;
|
||||
if (tag(b-1) == T_FUNCTION)
|
||||
{
|
||||
lua_debugline = 0; /* always reset debug flag */
|
||||
newpc = bvalue(b-1);
|
||||
bvalue(b-1) = pc; /* store return code */
|
||||
nvalue(b) = (base-stack); /* store base value */
|
||||
base = b+1;
|
||||
pc = newpc;
|
||||
if (MAXSTACK-(base-stack) < STACKGAP)
|
||||
{
|
||||
lua_error ("stack overflow");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else if (tag(b-1) == T_CFUNCTION)
|
||||
{
|
||||
int nparam;
|
||||
lua_debugline = 0; /* always reset debug flag */
|
||||
nvalue(b) = (base-stack); /* store base value */
|
||||
base = b+1;
|
||||
nparam = top-base; /* number of parameters */
|
||||
(fvalue(b-1))(); /* call C function */
|
||||
|
||||
/* shift returned values */
|
||||
{
|
||||
int i;
|
||||
int nretval = top - base - nparam;
|
||||
top = base - 2;
|
||||
base = stack + (int) nvalue(base-1);
|
||||
for (i=0; i<nretval; i++)
|
||||
{
|
||||
*top = *(top+nparam+2);
|
||||
++top;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
lua_reportbug ("call expression not a function");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case RETCODE:
|
||||
{
|
||||
int i;
|
||||
int shift = *pc++;
|
||||
int nretval = top - base - shift;
|
||||
top = base - 2;
|
||||
pc = bvalue(base-2);
|
||||
base = stack + (int) nvalue(base-1);
|
||||
for (i=0; i<nretval; i++)
|
||||
{
|
||||
*top = *(top+shift+2);
|
||||
++top;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case HALT:
|
||||
return 0; /* success */
|
||||
|
||||
case SETFUNCTION:
|
||||
{
|
||||
int file, func;
|
||||
file = *((Word *)(pc));
|
||||
pc += sizeof(Word);
|
||||
func = *((Word *)(pc));
|
||||
pc += sizeof(Word);
|
||||
if (lua_pushfunction (file, func))
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case SETLINE:
|
||||
lua_debugline = *((Word *)(pc));
|
||||
pc += sizeof(Word);
|
||||
break;
|
||||
|
||||
case RESET:
|
||||
lua_popfunction ();
|
||||
break;
|
||||
|
||||
default:
|
||||
lua_error ("internal error - opcode didn't match");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Mark all strings and arrays used by any object stored at stack.
|
||||
*/
|
||||
void lua_markstack (void)
|
||||
{
|
||||
Object *o;
|
||||
for (o = top-1; o >= stack; o--)
|
||||
lua_markobject (o);
|
||||
}
|
||||
|
||||
/*
|
||||
** Open file, generate opcode and execute global statement. Return 0 on
|
||||
** success or 1 on error.
|
||||
*/
|
||||
int lua_dofile (char *filename)
|
||||
{
|
||||
if (lua_openfile (filename)) return 1;
|
||||
if (lua_parse ()) { lua_closefile (); return 1; }
|
||||
lua_closefile ();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
** Generate opcode stored on string and execute global statement. Return 0 on
|
||||
** success or 1 on error.
|
||||
*/
|
||||
int lua_dostring (char *string)
|
||||
{
|
||||
if (lua_openstring (string)) return 1;
|
||||
if (lua_parse ()) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
** Execute the given function. Return 0 on success or 1 on error.
|
||||
*/
|
||||
int lua_call (char *functionname, int nparam)
|
||||
{
|
||||
static Byte startcode[] = {CALLFUNC, HALT};
|
||||
int i;
|
||||
Object func = s_object(lua_findsymbol(functionname));
|
||||
if (tag(&func) != T_FUNCTION) return 1;
|
||||
for (i=1; i<=nparam; i++)
|
||||
*(top-i+2) = *(top-i);
|
||||
top += 2;
|
||||
tag(top-nparam-1) = T_MARK;
|
||||
*(top-nparam-2) = func;
|
||||
return (lua_execute (startcode));
|
||||
}
|
||||
|
||||
/*
|
||||
** Get a parameter, returning the object handle or NULL on error.
|
||||
** 'number' must be 1 to get the first parameter.
|
||||
*/
|
||||
Object *lua_getparam (int number)
|
||||
{
|
||||
if (number <= 0 || number > top-base) return NULL;
|
||||
return (base+number-1);
|
||||
}
|
||||
|
||||
/*
|
||||
** Given an object handle, return its number value. On error, return 0.0.
|
||||
*/
|
||||
real lua_getnumber (Object *object)
|
||||
{
|
||||
if (tonumber (object)) return 0.0;
|
||||
else return (nvalue(object));
|
||||
}
|
||||
|
||||
/*
|
||||
** Given an object handle, return its string pointer. On error, return NULL.
|
||||
*/
|
||||
char *lua_getstring (Object *object)
|
||||
{
|
||||
if (tostring (object)) return NULL;
|
||||
else return (svalue(object));
|
||||
}
|
||||
|
||||
/*
|
||||
** Given an object handle, return a copy of its string. On error, return NULL.
|
||||
*/
|
||||
char *lua_copystring (Object *object)
|
||||
{
|
||||
if (tostring (object)) return NULL;
|
||||
else return (strdup(svalue(object)));
|
||||
}
|
||||
|
||||
/*
|
||||
** Given an object handle, return its cfuntion pointer. On error, return NULL.
|
||||
*/
|
||||
lua_CFunction lua_getcfunction (Object *object)
|
||||
{
|
||||
if (tag(object) != T_CFUNCTION) return NULL;
|
||||
else return (fvalue(object));
|
||||
}
|
||||
|
||||
/*
|
||||
** Given an object handle, return its user data. On error, return NULL.
|
||||
*/
|
||||
void *lua_getuserdata (Object *object)
|
||||
{
|
||||
if (tag(object) != T_USERDATA) return NULL;
|
||||
else return (uvalue(object));
|
||||
}
|
||||
|
||||
/*
|
||||
** Given an object handle and a field name, return its field object.
|
||||
** On error, return NULL.
|
||||
*/
|
||||
Object *lua_getfield (Object *object, char *field)
|
||||
{
|
||||
if (tag(object) != T_ARRAY)
|
||||
return NULL;
|
||||
else
|
||||
{
|
||||
Object ref;
|
||||
tag(&ref) = T_STRING;
|
||||
svalue(&ref) = lua_createstring(lua_strdup(field));
|
||||
return (lua_hashdefine(avalue(object), &ref));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Given an object handle and an index, return its indexed object.
|
||||
** On error, return NULL.
|
||||
*/
|
||||
Object *lua_getindexed (Object *object, float index)
|
||||
{
|
||||
if (tag(object) != T_ARRAY)
|
||||
return NULL;
|
||||
else
|
||||
{
|
||||
Object ref;
|
||||
tag(&ref) = T_NUMBER;
|
||||
nvalue(&ref) = index;
|
||||
return (lua_hashdefine(avalue(object), &ref));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Get a global object. Return the object handle or NULL on error.
|
||||
*/
|
||||
Object *lua_getglobal (char *name)
|
||||
{
|
||||
int n = lua_findsymbol(name);
|
||||
if (n < 0) return NULL;
|
||||
return &s_object(n);
|
||||
}
|
||||
|
||||
/*
|
||||
** Pop and return an object
|
||||
*/
|
||||
Object *lua_pop (void)
|
||||
{
|
||||
if (top <= base) return NULL;
|
||||
top--;
|
||||
return top;
|
||||
}
|
||||
|
||||
/*
|
||||
** Push a nil object
|
||||
*/
|
||||
int lua_pushnil (void)
|
||||
{
|
||||
if ((top-stack) >= MAXSTACK-1)
|
||||
{
|
||||
lua_error ("stack overflow");
|
||||
return 1;
|
||||
}
|
||||
tag(top) = T_NIL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
** Push an object (tag=number) to stack. Return 0 on success or 1 on error.
|
||||
*/
|
||||
int lua_pushnumber (real n)
|
||||
{
|
||||
if ((top-stack) >= MAXSTACK-1)
|
||||
{
|
||||
lua_error ("stack overflow");
|
||||
return 1;
|
||||
}
|
||||
tag(top) = T_NUMBER; nvalue(top++) = n;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
** Push an object (tag=string) to stack. Return 0 on success or 1 on error.
|
||||
*/
|
||||
int lua_pushstring (char *s)
|
||||
{
|
||||
if ((top-stack) >= MAXSTACK-1)
|
||||
{
|
||||
lua_error ("stack overflow");
|
||||
return 1;
|
||||
}
|
||||
tag(top) = T_STRING;
|
||||
svalue(top++) = lua_createstring(lua_strdup(s));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
** Push an object (tag=cfunction) to stack. Return 0 on success or 1 on error.
|
||||
*/
|
||||
int lua_pushcfunction (lua_CFunction fn)
|
||||
{
|
||||
if ((top-stack) >= MAXSTACK-1)
|
||||
{
|
||||
lua_error ("stack overflow");
|
||||
return 1;
|
||||
}
|
||||
tag(top) = T_CFUNCTION; fvalue(top++) = fn;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
** Push an object (tag=userdata) to stack. Return 0 on success or 1 on error.
|
||||
*/
|
||||
int lua_pushuserdata (void *u)
|
||||
{
|
||||
if ((top-stack) >= MAXSTACK-1)
|
||||
{
|
||||
lua_error ("stack overflow");
|
||||
return 1;
|
||||
}
|
||||
tag(top) = T_USERDATA; uvalue(top++) = u;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
** Push an object to stack.
|
||||
*/
|
||||
int lua_pushobject (Object *o)
|
||||
{
|
||||
if ((top-stack) >= MAXSTACK-1)
|
||||
{
|
||||
lua_error ("stack overflow");
|
||||
return 1;
|
||||
}
|
||||
*top++ = *o;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
** Store top of the stack at a global variable array field.
|
||||
** Return 1 on error, 0 on success.
|
||||
*/
|
||||
int lua_storeglobal (char *name)
|
||||
{
|
||||
int n = lua_findsymbol (name);
|
||||
if (n < 0) return 1;
|
||||
if (tag(top-1) == T_MARK) return 1;
|
||||
s_object(n) = *(--top);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
** Store top of the stack at an array field. Return 1 on error, 0 on success.
|
||||
*/
|
||||
int lua_storefield (lua_Object object, char *field)
|
||||
{
|
||||
if (tag(object) != T_ARRAY)
|
||||
return 1;
|
||||
else
|
||||
{
|
||||
Object ref, *h;
|
||||
tag(&ref) = T_STRING;
|
||||
svalue(&ref) = lua_createstring(lua_strdup(field));
|
||||
h = lua_hashdefine(avalue(object), &ref);
|
||||
if (h == NULL) return 1;
|
||||
if (tag(top-1) == T_MARK) return 1;
|
||||
*h = *(--top);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Store top of the stack at an array index. Return 1 on error, 0 on success.
|
||||
*/
|
||||
int lua_storeindexed (lua_Object object, float index)
|
||||
{
|
||||
if (tag(object) != T_ARRAY)
|
||||
return 1;
|
||||
else
|
||||
{
|
||||
Object ref, *h;
|
||||
tag(&ref) = T_NUMBER;
|
||||
nvalue(&ref) = index;
|
||||
h = lua_hashdefine(avalue(object), &ref);
|
||||
if (h == NULL) return 1;
|
||||
if (tag(top-1) == T_MARK) return 1;
|
||||
*h = *(--top);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Given an object handle, return if it is nil.
|
||||
*/
|
||||
int lua_isnil (Object *object)
|
||||
{
|
||||
return (object != NULL && tag(object) == T_NIL);
|
||||
}
|
||||
|
||||
/*
|
||||
** Given an object handle, return if it is a number one.
|
||||
*/
|
||||
int lua_isnumber (Object *object)
|
||||
{
|
||||
return (object != NULL && tag(object) == T_NUMBER);
|
||||
}
|
||||
|
||||
/*
|
||||
** Given an object handle, return if it is a string one.
|
||||
*/
|
||||
int lua_isstring (Object *object)
|
||||
{
|
||||
return (object != NULL && tag(object) == T_STRING);
|
||||
}
|
||||
|
||||
/*
|
||||
** Given an object handle, return if it is an array one.
|
||||
*/
|
||||
int lua_istable (Object *object)
|
||||
{
|
||||
return (object != NULL && tag(object) == T_ARRAY);
|
||||
}
|
||||
|
||||
/*
|
||||
** Given an object handle, return if it is a cfunction one.
|
||||
*/
|
||||
int lua_iscfunction (Object *object)
|
||||
{
|
||||
return (object != NULL && tag(object) == T_CFUNCTION);
|
||||
}
|
||||
|
||||
/*
|
||||
** Given an object handle, return if it is an user data one.
|
||||
*/
|
||||
int lua_isuserdata (Object *object)
|
||||
{
|
||||
return (object != NULL && tag(object) == T_USERDATA);
|
||||
}
|
||||
|
||||
/*
|
||||
** Internal function: return an object type.
|
||||
*/
|
||||
void lua_type (void)
|
||||
{
|
||||
Object *o = lua_getparam(1);
|
||||
lua_pushstring (lua_constant[tag(o)]);
|
||||
}
|
||||
|
||||
/*
|
||||
** Internal function: convert an object to a number
|
||||
*/
|
||||
void lua_obj2number (void)
|
||||
{
|
||||
Object *o = lua_getparam(1);
|
||||
lua_pushobject (lua_convtonumber(o));
|
||||
}
|
||||
|
||||
/*
|
||||
** Internal function: print object values
|
||||
*/
|
||||
void lua_print (void)
|
||||
{
|
||||
int i=1;
|
||||
void *obj;
|
||||
while ((obj=lua_getparam (i++)) != NULL)
|
||||
{
|
||||
if (lua_isnumber(obj)) printf("%g\n",lua_getnumber (obj));
|
||||
else if (lua_isstring(obj)) printf("%s\n",lua_getstring (obj));
|
||||
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",obj);
|
||||
else if (lua_isnil(obj)) printf("nil\n");
|
||||
else printf("invalid value to print\n");
|
||||
}
|
||||
}
|
||||
|
||||
144
opcode.h
Normal file
144
opcode.h
Normal file
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
** opcode.h
|
||||
** TeCGraf - PUC-Rio
|
||||
** 16 Apr 92
|
||||
*/
|
||||
|
||||
#ifndef opcode_h
|
||||
#define opcode_h
|
||||
|
||||
#ifndef STACKGAP
|
||||
#define STACKGAP 128
|
||||
#endif
|
||||
|
||||
#ifndef real
|
||||
#define real float
|
||||
#endif
|
||||
|
||||
typedef unsigned char Byte;
|
||||
|
||||
typedef unsigned short Word;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
NOP,
|
||||
PUSHNIL,
|
||||
PUSH0, PUSH1, PUSH2,
|
||||
PUSHBYTE,
|
||||
PUSHWORD,
|
||||
PUSHFLOAT,
|
||||
PUSHSTRING,
|
||||
PUSHLOCAL0, PUSHLOCAL1, PUSHLOCAL2, PUSHLOCAL3, PUSHLOCAL4,
|
||||
PUSHLOCAL5, PUSHLOCAL6, PUSHLOCAL7, PUSHLOCAL8, PUSHLOCAL9,
|
||||
PUSHLOCAL,
|
||||
PUSHGLOBAL,
|
||||
PUSHINDEXED,
|
||||
PUSHMARK,
|
||||
PUSHOBJECT,
|
||||
STORELOCAL0, STORELOCAL1, STORELOCAL2, STORELOCAL3, STORELOCAL4,
|
||||
STORELOCAL5, STORELOCAL6, STORELOCAL7, STORELOCAL8, STORELOCAL9,
|
||||
STORELOCAL,
|
||||
STOREGLOBAL,
|
||||
STOREINDEXED0,
|
||||
STOREINDEXED,
|
||||
STOREFIELD,
|
||||
ADJUST,
|
||||
CREATEARRAY,
|
||||
EQOP,
|
||||
LTOP,
|
||||
LEOP,
|
||||
ADDOP,
|
||||
SUBOP,
|
||||
MULTOP,
|
||||
DIVOP,
|
||||
CONCOP,
|
||||
MINUSOP,
|
||||
NOTOP,
|
||||
ONTJMP,
|
||||
ONFJMP,
|
||||
JMP,
|
||||
UPJMP,
|
||||
IFFJMP,
|
||||
IFFUPJMP,
|
||||
POP,
|
||||
CALLFUNC,
|
||||
RETCODE,
|
||||
HALT,
|
||||
SETFUNCTION,
|
||||
SETLINE,
|
||||
RESET
|
||||
} OpCode;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
T_MARK,
|
||||
T_NIL,
|
||||
T_NUMBER,
|
||||
T_STRING,
|
||||
T_ARRAY,
|
||||
T_FUNCTION,
|
||||
T_CFUNCTION,
|
||||
T_USERDATA
|
||||
} Type;
|
||||
|
||||
typedef void (*Cfunction) (void);
|
||||
typedef int (*Input) (void);
|
||||
typedef void (*Unput) (int );
|
||||
|
||||
typedef union
|
||||
{
|
||||
Cfunction f;
|
||||
real n;
|
||||
char *s;
|
||||
Byte *b;
|
||||
struct Hash *a;
|
||||
void *u;
|
||||
} Value;
|
||||
|
||||
typedef struct Object
|
||||
{
|
||||
Type tag;
|
||||
Value value;
|
||||
} Object;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char *name;
|
||||
Object object;
|
||||
} Symbol;
|
||||
|
||||
/* Macros to access structure members */
|
||||
#define tag(o) ((o)->tag)
|
||||
#define nvalue(o) ((o)->value.n)
|
||||
#define svalue(o) ((o)->value.s)
|
||||
#define bvalue(o) ((o)->value.b)
|
||||
#define avalue(o) ((o)->value.a)
|
||||
#define fvalue(o) ((o)->value.f)
|
||||
#define uvalue(o) ((o)->value.u)
|
||||
|
||||
/* Macros to access symbol table */
|
||||
#define s_name(i) (lua_table[i].name)
|
||||
#define s_object(i) (lua_table[i].object)
|
||||
#define s_tag(i) (tag(&s_object(i)))
|
||||
#define s_nvalue(i) (nvalue(&s_object(i)))
|
||||
#define s_svalue(i) (svalue(&s_object(i)))
|
||||
#define s_bvalue(i) (bvalue(&s_object(i)))
|
||||
#define s_avalue(i) (avalue(&s_object(i)))
|
||||
#define s_fvalue(i) (fvalue(&s_object(i)))
|
||||
#define s_uvalue(i) (uvalue(&s_object(i)))
|
||||
|
||||
|
||||
/* Exported functions */
|
||||
int lua_execute (Byte *pc);
|
||||
void lua_markstack (void);
|
||||
char *lua_strdup (char *l);
|
||||
|
||||
void lua_setinput (Input fn); /* from "lua.lex" module */
|
||||
void lua_setunput (Unput fn); /* from "lua.lex" module */
|
||||
char *lua_lasttext (void); /* from "lua.lex" module */
|
||||
int lua_parse (void); /* from "lua.stx" module */
|
||||
void lua_type (void);
|
||||
void lua_obj2number (void);
|
||||
void lua_print (void);
|
||||
|
||||
#endif
|
||||
47
save.lua
Normal file
47
save.lua
Normal file
@@ -0,0 +1,47 @@
|
||||
$debug
|
||||
|
||||
|
||||
function savevar (n,v)
|
||||
if v = nil then return end;
|
||||
if type(v) = "number" then print(n.."="..v) return end
|
||||
if type(v) = "string" then print(n.."='"..v.."'") return end
|
||||
if type(v) = "table" then
|
||||
if v.__visited__ ~= nil then
|
||||
print(n .. "=" .. v.__visited__);
|
||||
else
|
||||
print(n.."=@()")
|
||||
v.__visited__ = n;
|
||||
local r,f;
|
||||
r,f = next(v,nil);
|
||||
while r ~= nil do
|
||||
if r ~= "__visited__" then
|
||||
if type(r) = 'string' then
|
||||
savevar(n.."['"..r.."']",f)
|
||||
else
|
||||
savevar(n.."["..r.."]",f)
|
||||
end
|
||||
end
|
||||
r,f = next(v,r)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function save ()
|
||||
local n,v
|
||||
n,v = nextvar(nil)
|
||||
while n ~= nil do
|
||||
savevar(n,v);
|
||||
n,v = nextvar(n)
|
||||
end
|
||||
end
|
||||
|
||||
a = 3
|
||||
x = @{a = 4, b = "name", l=@[4,5,67]}
|
||||
|
||||
b = @{t=5}
|
||||
x.next = b
|
||||
|
||||
|
||||
save()
|
||||
|
||||
56
sort.lua
Normal file
56
sort.lua
Normal file
@@ -0,0 +1,56 @@
|
||||
$debug
|
||||
|
||||
function quicksort(r,s)
|
||||
if s<=r then return end -- caso basico da recursao
|
||||
local v=x[r]
|
||||
local i=r
|
||||
local j=s+1
|
||||
i=i+1; while x[i]<v do i=i+1 end
|
||||
j=j-1; while x[j]>v do j=j-1 end
|
||||
x[i],x[j]=x[j],x[i]
|
||||
while j>i do -- separacao
|
||||
i=i+1; while x[i]<v do i=i+1 end
|
||||
j=j-1; while x[j]>v do j=j-1 end
|
||||
x[i],x[j]=x[j],x[i]
|
||||
end
|
||||
x[i],x[j]=x[j],x[i] -- undo last swap
|
||||
x[j],x[r]=x[r],x[j]
|
||||
quicksort(r,j-1) -- recursao
|
||||
quicksort(j+1,s)
|
||||
end
|
||||
|
||||
function sort(a,n) -- selection sort
|
||||
local i=1
|
||||
while i<=n do
|
||||
local m=i
|
||||
local j=i+1
|
||||
while j<=n do
|
||||
if a[j]<a[m] then m=j end
|
||||
j=j+1
|
||||
end
|
||||
a[i],a[m]=a[m],a[i] -- swap a[i] and a[m]
|
||||
i=i+1
|
||||
end
|
||||
end
|
||||
|
||||
function main()
|
||||
x=@()
|
||||
n=-1
|
||||
n=n+1; x[n]="a"
|
||||
n=n+1; x[n]="waldemar"
|
||||
n=n+1; x[n]="luiz"
|
||||
n=n+1; x[n]="lula"
|
||||
n=n+1; x[n]="peter"
|
||||
n=n+1; x[n]="raquel"
|
||||
n=n+1; x[n]="camilo"
|
||||
n=n+1; x[n]="andre"
|
||||
n=n+1; x[n]="marcelo"
|
||||
n=n+1; x[n]="sedrez"
|
||||
n=n+1; x[n]="z"
|
||||
-- quicksort(1,n-1)
|
||||
print(x[0]..","..x[1]..","..x[2]..","..x[3]..","..x[4]..","..x[5]..","..x[6]..","..x[7]..","..x[8]..","..x[9]..","..x[10])
|
||||
sort (x, n-1)
|
||||
print(x[0]..","..x[1]..","..x[2]..","..x[3]..","..x[4]..","..x[5]..","..x[6]..","..x[7]..","..x[8]..","..x[9]..","..x[10])
|
||||
end
|
||||
|
||||
|
||||
131
strlib.c
Normal file
131
strlib.c
Normal file
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
** strlib.c
|
||||
** String library to LUA
|
||||
**
|
||||
** Waldemar Celes Filho
|
||||
** TeCGraf - PUC-Rio
|
||||
** 19 May 93
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
/*
|
||||
** Return the position of the first caracter of a substring into a string
|
||||
** LUA interface:
|
||||
** n = strfind (string, substring)
|
||||
*/
|
||||
static void str_find (void)
|
||||
{
|
||||
int n;
|
||||
char *s1, *s2;
|
||||
lua_Object o1 = lua_getparam (1);
|
||||
lua_Object o2 = lua_getparam (2);
|
||||
if (!lua_isstring(o1) || !lua_isstring(o2))
|
||||
{ lua_error ("incorrect arguments to function `strfind'"); return; }
|
||||
s1 = lua_getstring(o1);
|
||||
s2 = lua_getstring(o2);
|
||||
n = strstr(s1,s2) - s1 + 1;
|
||||
lua_pushnumber (n);
|
||||
}
|
||||
|
||||
/*
|
||||
** Return the string length
|
||||
** LUA interface:
|
||||
** n = strlen (string)
|
||||
*/
|
||||
static void str_len (void)
|
||||
{
|
||||
lua_Object o = lua_getparam (1);
|
||||
if (!lua_isstring(o))
|
||||
{ lua_error ("incorrect arguments to function `strlen'"); return; }
|
||||
lua_pushnumber(strlen(lua_getstring(o)));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Return the substring of a string, from start to end
|
||||
** LUA interface:
|
||||
** substring = strsub (string, start, end)
|
||||
*/
|
||||
static void str_sub (void)
|
||||
{
|
||||
int start, end;
|
||||
char *s;
|
||||
lua_Object o1 = lua_getparam (1);
|
||||
lua_Object o2 = lua_getparam (2);
|
||||
lua_Object o3 = lua_getparam (3);
|
||||
if (!lua_isstring(o1) || !lua_isnumber(o2) || !lua_isnumber(o3))
|
||||
{ lua_error ("incorrect arguments to function `strsub'"); return; }
|
||||
s = strdup (lua_getstring(o1));
|
||||
start = lua_getnumber (o2);
|
||||
end = lua_getnumber (o3);
|
||||
if (end < start || start < 1 || end > strlen(s))
|
||||
lua_pushstring ("");
|
||||
else
|
||||
{
|
||||
s[end] = 0;
|
||||
lua_pushstring (&s[start-1]);
|
||||
}
|
||||
free (s);
|
||||
}
|
||||
|
||||
/*
|
||||
** Convert a string to lower case.
|
||||
** LUA interface:
|
||||
** lowercase = strlower (string)
|
||||
*/
|
||||
static void str_lower (void)
|
||||
{
|
||||
char *s, *c;
|
||||
lua_Object o = lua_getparam (1);
|
||||
if (!lua_isstring(o))
|
||||
{ lua_error ("incorrect arguments to function `strlower'"); return; }
|
||||
c = s = strdup(lua_getstring(o));
|
||||
while (*c != 0)
|
||||
{
|
||||
*c = tolower(*c);
|
||||
c++;
|
||||
}
|
||||
lua_pushstring(s);
|
||||
free(s);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Convert a string to upper case.
|
||||
** LUA interface:
|
||||
** uppercase = strupper (string)
|
||||
*/
|
||||
static void str_upper (void)
|
||||
{
|
||||
char *s, *c;
|
||||
lua_Object o = lua_getparam (1);
|
||||
if (!lua_isstring(o))
|
||||
{ lua_error ("incorrect arguments to function `strlower'"); return; }
|
||||
c = s = strdup(lua_getstring(o));
|
||||
while (*c != 0)
|
||||
{
|
||||
*c = toupper(*c);
|
||||
c++;
|
||||
}
|
||||
lua_pushstring(s);
|
||||
free(s);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Open string library
|
||||
*/
|
||||
void strlib_open (void)
|
||||
{
|
||||
lua_register ("strfind", str_find);
|
||||
lua_register ("strlen", str_len);
|
||||
lua_register ("strsub", str_sub);
|
||||
lua_register ("strlower", str_lower);
|
||||
lua_register ("strupper", str_upper);
|
||||
}
|
||||
351
table.c
Normal file
351
table.c
Normal file
@@ -0,0 +1,351 @@
|
||||
/*
|
||||
** table.c
|
||||
** Module to control static tables
|
||||
** TeCGraf - PUC-Rio
|
||||
** 11 May 93
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "opcode.h"
|
||||
#include "hash.h"
|
||||
#include "inout.h"
|
||||
#include "table.h"
|
||||
#include "lua.h"
|
||||
|
||||
#define streq(s1,s2) (strcmp(s1,s2)==0)
|
||||
|
||||
#ifndef MAXSYMBOL
|
||||
#define MAXSYMBOL 512
|
||||
#endif
|
||||
static Symbol tablebuffer[MAXSYMBOL] = {
|
||||
{"type",{T_CFUNCTION,{lua_type}}},
|
||||
{"tonumber",{T_CFUNCTION,{lua_obj2number}}},
|
||||
{"next",{T_CFUNCTION,{lua_next}}},
|
||||
{"nextvar",{T_CFUNCTION,{lua_nextvar}}},
|
||||
{"print",{T_CFUNCTION,{lua_print}}}
|
||||
};
|
||||
Symbol *lua_table=tablebuffer;
|
||||
Word lua_ntable=5;
|
||||
|
||||
#ifndef MAXCONSTANT
|
||||
#define MAXCONSTANT 256
|
||||
#endif
|
||||
static char *constantbuffer[MAXCONSTANT] = {"mark","nil","number",
|
||||
"string","table",
|
||||
"function","cfunction"
|
||||
};
|
||||
char **lua_constant = constantbuffer;
|
||||
Word lua_nconstant=T_CFUNCTION+1;
|
||||
|
||||
#ifndef MAXSTRING
|
||||
#define MAXSTRING 512
|
||||
#endif
|
||||
static char *stringbuffer[MAXSTRING];
|
||||
char **lua_string = stringbuffer;
|
||||
Word lua_nstring=0;
|
||||
|
||||
#ifndef MAXARRAY
|
||||
#define MAXARRAY 512
|
||||
#endif
|
||||
static Hash *arraybuffer[MAXARRAY];
|
||||
Hash **lua_array = arraybuffer;
|
||||
Word lua_narray=0;
|
||||
|
||||
#define MAXFILE 20
|
||||
char *lua_file[MAXFILE];
|
||||
int lua_nfile;
|
||||
|
||||
|
||||
/*
|
||||
** Given a name, search it at symbol table and return its index. If not
|
||||
** found, allocate at end of table, checking oveflow and return its index.
|
||||
** On error, return -1.
|
||||
*/
|
||||
int lua_findsymbol (char *s)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<lua_ntable; i++)
|
||||
if (streq(s,s_name(i)))
|
||||
return i;
|
||||
if (lua_ntable >= MAXSYMBOL-1)
|
||||
{
|
||||
lua_error ("symbol table overflow");
|
||||
return -1;
|
||||
}
|
||||
s_name(lua_ntable) = strdup(s);
|
||||
if (s_name(lua_ntable) == NULL)
|
||||
{
|
||||
lua_error ("not enough memory");
|
||||
return -1;
|
||||
}
|
||||
s_tag(lua_ntable++) = T_NIL;
|
||||
|
||||
return (lua_ntable-1);
|
||||
}
|
||||
|
||||
/*
|
||||
** Given a constant string, eliminate its delimeters (" or '), search it at
|
||||
** constant table and return its index. If not found, allocate at end of
|
||||
** the table, checking oveflow and return its index.
|
||||
**
|
||||
** For each allocation, the function allocate a extra char to be used to
|
||||
** mark used string (it's necessary to deal with constant and string
|
||||
** uniformily). The function store at the table the second position allocated,
|
||||
** that represents the beginning of the real string. On error, return -1.
|
||||
**
|
||||
*/
|
||||
int lua_findenclosedconstant (char *s)
|
||||
{
|
||||
int i, j, l=strlen(s);
|
||||
char *c = calloc (l, sizeof(char)); /* make a copy */
|
||||
|
||||
c++; /* create mark space */
|
||||
|
||||
/* introduce scape characters */
|
||||
for (i=1,j=0; i<l-1; i++)
|
||||
{
|
||||
if (s[i] == '\\')
|
||||
{
|
||||
switch (s[++i])
|
||||
{
|
||||
case 'n': c[j++] = '\n'; break;
|
||||
case 't': c[j++] = '\t'; break;
|
||||
case 'r': c[j++] = '\r'; break;
|
||||
default : c[j++] = '\\'; c[j++] = c[i]; break;
|
||||
}
|
||||
}
|
||||
else
|
||||
c[j++] = s[i];
|
||||
}
|
||||
c[j++] = 0;
|
||||
|
||||
for (i=0; i<lua_nconstant; i++)
|
||||
if (streq(c,lua_constant[i]))
|
||||
{
|
||||
free (c-1);
|
||||
return i;
|
||||
}
|
||||
if (lua_nconstant >= MAXCONSTANT-1)
|
||||
{
|
||||
lua_error ("lua: constant string table overflow");
|
||||
return -1;
|
||||
}
|
||||
lua_constant[lua_nconstant++] = c;
|
||||
return (lua_nconstant-1);
|
||||
}
|
||||
|
||||
/*
|
||||
** Given a constant string, search it at constant table and return its index.
|
||||
** If not found, allocate at end of the table, checking oveflow and return
|
||||
** its index.
|
||||
**
|
||||
** For each allocation, the function allocate a extra char to be used to
|
||||
** mark used string (it's necessary to deal with constant and string
|
||||
** uniformily). The function store at the table the second position allocated,
|
||||
** that represents the beginning of the real string. On error, return -1.
|
||||
**
|
||||
*/
|
||||
int lua_findconstant (char *s)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<lua_nconstant; i++)
|
||||
if (streq(s,lua_constant[i]))
|
||||
return i;
|
||||
if (lua_nconstant >= MAXCONSTANT-1)
|
||||
{
|
||||
lua_error ("lua: constant string table overflow");
|
||||
return -1;
|
||||
}
|
||||
{
|
||||
char *c = calloc(strlen(s)+2,sizeof(char));
|
||||
c++; /* create mark space */
|
||||
lua_constant[lua_nconstant++] = strcpy(c,s);
|
||||
}
|
||||
return (lua_nconstant-1);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Mark an object if it is a string or a unmarked array.
|
||||
*/
|
||||
void lua_markobject (Object *o)
|
||||
{
|
||||
if (tag(o) == T_STRING)
|
||||
lua_markstring (svalue(o)) = 1;
|
||||
else if (tag(o) == T_ARRAY && markarray(avalue(o)) == 0)
|
||||
lua_hashmark (avalue(o));
|
||||
}
|
||||
|
||||
/*
|
||||
** Mark all strings and arrays used by any object stored at symbol table.
|
||||
*/
|
||||
static void lua_marktable (void)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<lua_ntable; i++)
|
||||
lua_markobject (&s_object(i));
|
||||
}
|
||||
|
||||
/*
|
||||
** Simulate a garbage colection. When string table or array table overflows,
|
||||
** this function check if all allocated strings and arrays are in use. If
|
||||
** there are unused ones, pack (compress) the tables.
|
||||
*/
|
||||
static void lua_pack (void)
|
||||
{
|
||||
lua_markstack ();
|
||||
lua_marktable ();
|
||||
|
||||
{ /* pack string */
|
||||
int i, j;
|
||||
for (i=j=0; i<lua_nstring; i++)
|
||||
if (lua_markstring(lua_string[i]) == 1)
|
||||
{
|
||||
lua_string[j++] = lua_string[i];
|
||||
lua_markstring(lua_string[i]) = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
free (lua_string[i]-1);
|
||||
}
|
||||
lua_nstring = j;
|
||||
}
|
||||
|
||||
{ /* pack array */
|
||||
int i, j;
|
||||
for (i=j=0; i<lua_narray; i++)
|
||||
if (markarray(lua_array[i]) == 1)
|
||||
{
|
||||
lua_array[j++] = lua_array[i];
|
||||
markarray(lua_array[i]) = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
lua_hashdelete (lua_array[i]);
|
||||
}
|
||||
lua_narray = j;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Allocate a new string at string table. The given string is already
|
||||
** allocated with mark space and the function puts it at the end of the
|
||||
** table, checking overflow, and returns its own pointer, or NULL on error.
|
||||
*/
|
||||
char *lua_createstring (char *s)
|
||||
{
|
||||
if (s == NULL) return NULL;
|
||||
|
||||
if (lua_nstring >= MAXSTRING-1)
|
||||
{
|
||||
lua_pack ();
|
||||
if (lua_nstring >= MAXSTRING-1)
|
||||
{
|
||||
lua_error ("string table overflow");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
lua_string[lua_nstring++] = s;
|
||||
return s;
|
||||
}
|
||||
|
||||
/*
|
||||
** Allocate a new array, already created, at array table. The function puts
|
||||
** it at the end of the table, checking overflow, and returns its own pointer,
|
||||
** or NULL on error.
|
||||
*/
|
||||
void *lua_createarray (void *a)
|
||||
{
|
||||
if (a == NULL) return NULL;
|
||||
|
||||
if (lua_narray >= MAXARRAY-1)
|
||||
{
|
||||
lua_pack ();
|
||||
if (lua_narray >= MAXARRAY-1)
|
||||
{
|
||||
lua_error ("indexed table overflow");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
lua_array[lua_narray++] = a;
|
||||
return a;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Add a file name at file table, checking overflow. This function also set
|
||||
** the external variable "lua_filename" with the function filename set.
|
||||
** Return 0 on success or 1 on error.
|
||||
*/
|
||||
int lua_addfile (char *fn)
|
||||
{
|
||||
if (lua_nfile >= MAXFILE-1)
|
||||
{
|
||||
lua_error ("too many files");
|
||||
return 1;
|
||||
}
|
||||
if ((lua_file[lua_nfile++] = strdup (fn)) == NULL)
|
||||
{
|
||||
lua_error ("not enough memory");
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
** Return the last file name set.
|
||||
*/
|
||||
char *lua_filename (void)
|
||||
{
|
||||
return lua_file[lua_nfile-1];
|
||||
}
|
||||
|
||||
/*
|
||||
** Internal function: return next global variable
|
||||
*/
|
||||
void lua_nextvar (void)
|
||||
{
|
||||
int index;
|
||||
Object *o = lua_getparam (1);
|
||||
if (o == NULL)
|
||||
{ lua_error ("too few arguments to function `nextvar'"); return; }
|
||||
if (lua_getparam (2) != NULL)
|
||||
{ lua_error ("too many arguments to function `nextvar'"); return; }
|
||||
if (tag(o) == T_NIL)
|
||||
{
|
||||
index = 0;
|
||||
}
|
||||
else if (tag(o) != T_STRING)
|
||||
{
|
||||
lua_error ("incorrect argument to function `nextvar'");
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (index=0; index<lua_ntable; index++)
|
||||
if (streq(s_name(index),svalue(o))) break;
|
||||
if (index == lua_ntable)
|
||||
{
|
||||
lua_error ("name not found in function `nextvar'");
|
||||
return;
|
||||
}
|
||||
index++;
|
||||
while (index < lua_ntable-1 && tag(&s_object(index)) == T_NIL) index++;
|
||||
|
||||
if (index == lua_ntable-1)
|
||||
{
|
||||
lua_pushnil();
|
||||
lua_pushnil();
|
||||
return;
|
||||
}
|
||||
}
|
||||
{
|
||||
Object name;
|
||||
tag(&name) = T_STRING;
|
||||
svalue(&name) = lua_createstring(lua_strdup(s_name(index)));
|
||||
if (lua_pushobject (&name)) return;
|
||||
if (lua_pushobject (&s_object(index))) return;
|
||||
}
|
||||
}
|
||||
39
table.h
Normal file
39
table.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
** table.c
|
||||
** Module to control static tables
|
||||
** TeCGraf - PUC-Rio
|
||||
** 11 May 93
|
||||
*/
|
||||
|
||||
#ifndef table_h
|
||||
#define table_h
|
||||
|
||||
extern Symbol *lua_table;
|
||||
extern Word lua_ntable;
|
||||
|
||||
extern char **lua_constant;
|
||||
extern Word lua_nconstant;
|
||||
|
||||
extern char **lua_string;
|
||||
extern Word lua_nstring;
|
||||
|
||||
extern Hash **lua_array;
|
||||
extern Word lua_narray;
|
||||
|
||||
extern char *lua_file[];
|
||||
extern int lua_nfile;
|
||||
|
||||
#define lua_markstring(s) (*((s)-1))
|
||||
|
||||
|
||||
int lua_findsymbol (char *s);
|
||||
int lua_findenclosedconstant (char *s);
|
||||
int lua_findconstant (char *s);
|
||||
void lua_markobject (Object *o);
|
||||
char *lua_createstring (char *s);
|
||||
void *lua_createarray (void *a);
|
||||
int lua_addfile (char *fn);
|
||||
char *lua_filename (void);
|
||||
void lua_nextvar (void);
|
||||
|
||||
#endif
|
||||
15
test.lua
Normal file
15
test.lua
Normal file
@@ -0,0 +1,15 @@
|
||||
$debug
|
||||
|
||||
|
||||
function somaP (x1,y1,x2,y2)
|
||||
return x1+x2, y1+y2
|
||||
end
|
||||
|
||||
function norma (x,y)
|
||||
return x*x+y*y
|
||||
end
|
||||
|
||||
function retorno_multiplo ()
|
||||
print (norma(somaP(2,3,4,5)))
|
||||
end
|
||||
|
||||
35
type.lua
Normal file
35
type.lua
Normal file
@@ -0,0 +1,35 @@
|
||||
$debug
|
||||
|
||||
function check (object, class)
|
||||
local v = next(object,nil);
|
||||
while v ~= nil do
|
||||
if class[v] = nil then print("unknown field: " .. v)
|
||||
elseif type(object[v]) ~= class[v].type
|
||||
then print("wrong type for field " .. v)
|
||||
end
|
||||
v = next(object,v);
|
||||
end
|
||||
v = next(class,nil);
|
||||
while v ~= nil do
|
||||
if object[v] = nil then
|
||||
if class[v].default ~= nil then
|
||||
object[v] = class[v].default
|
||||
else print("field "..v.." not initialized")
|
||||
end
|
||||
end
|
||||
v = next(class,v);
|
||||
end
|
||||
end
|
||||
|
||||
typetrilha = @{x = @{default = 0, type = "number"},
|
||||
y = @{default = 0, type = "number"},
|
||||
name = @{type = "string"}
|
||||
}
|
||||
|
||||
function trilha (t) check(t,typetrilha) end
|
||||
|
||||
t1 = @trilha{ x = 4, name = "3"}
|
||||
|
||||
a = "na".."me"
|
||||
|
||||
|
||||
35
y_tab.h
Normal file
35
y_tab.h
Normal file
@@ -0,0 +1,35 @@
|
||||
|
||||
typedef union
|
||||
{
|
||||
int vInt;
|
||||
long vLong;
|
||||
float vFloat;
|
||||
Word vWord;
|
||||
Byte *pByte;
|
||||
} YYSTYPE;
|
||||
extern YYSTYPE yylval;
|
||||
# define NIL 257
|
||||
# define IF 258
|
||||
# define THEN 259
|
||||
# define ELSE 260
|
||||
# define ELSEIF 261
|
||||
# define WHILE 262
|
||||
# define DO 263
|
||||
# define REPEAT 264
|
||||
# define UNTIL 265
|
||||
# define END 266
|
||||
# define RETURN 267
|
||||
# define LOCAL 268
|
||||
# define NUMBER 269
|
||||
# define FUNCTION 270
|
||||
# define NAME 271
|
||||
# define STRING 272
|
||||
# define DEBUG 273
|
||||
# define NOT 274
|
||||
# define AND 275
|
||||
# define OR 276
|
||||
# define NE 277
|
||||
# define LE 278
|
||||
# define GE 279
|
||||
# define CONC 280
|
||||
# define UNARY 281
|
||||
Reference in New Issue
Block a user