mirror of
https://github.com/lua/lua.git
synced 2026-07-27 00:19:07 +00:00
Compare commits
371 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e64dbc390a | ||
|
|
a0fd8d1787 | ||
|
|
d9d04a9274 | ||
|
|
b4ad600b93 | ||
|
|
cb7f027380 | ||
|
|
0bbd96bd5f | ||
|
|
4eb67aa710 | ||
|
|
0133610315 | ||
|
|
de04533dc0 | ||
|
|
7c9aee64c2 | ||
|
|
e0ff4e5d22 | ||
|
|
bf7f85d609 | ||
|
|
a775a2d81a | ||
|
|
e9aa98d594 | ||
|
|
3e9c6a8a24 | ||
|
|
1f4e2ba7b2 | ||
|
|
d6ff06751a | ||
|
|
7a11c7f8e4 | ||
|
|
c454dc7bdd | ||
|
|
82ad0d5770 | ||
|
|
256d1bea08 | ||
|
|
f2d35bdc78 | ||
|
|
2679461637 | ||
|
|
0870a2d1d8 | ||
|
|
78edc241e9 | ||
|
|
e907c711c0 | ||
|
|
5a8bb00df4 | ||
|
|
677188de8a | ||
|
|
6233d21c9d | ||
|
|
ab8ea5c38a | ||
|
|
ae9fd122fa | ||
|
|
da18ec5d54 | ||
|
|
038848eccd | ||
|
|
b678e465a1 | ||
|
|
72d675aba7 | ||
|
|
ba57f7d946 | ||
|
|
e63b542c9b | ||
|
|
6a853fcb8b | ||
|
|
31bea2190b | ||
|
|
4b954e9b2e | ||
|
|
055823c04d | ||
|
|
26d1e21c89 | ||
|
|
24a2c08145 | ||
|
|
9d7bae0b6a | ||
|
|
082aded149 | ||
|
|
aa9c75c06e | ||
|
|
f04c83e075 | ||
|
|
c364e9f97e | ||
|
|
e3a02e6a9c | ||
|
|
d5feffdb60 | ||
|
|
bb5627f3a4 | ||
|
|
21107d7c2c | ||
|
|
b5cd7d426f | ||
|
|
bf6d2ccf92 | ||
|
|
b82ff713e3 | ||
|
|
77113ee02f | ||
|
|
ad6c7b0dd4 | ||
|
|
8b2d97d187 | ||
|
|
fb1cf6ab2d | ||
|
|
19ca2087de | ||
|
|
7bdbd833b5 | ||
|
|
b22baf386d | ||
|
|
8fdd06ba3c | ||
|
|
028ec00ab9 | ||
|
|
1dcf1c9cbd | ||
|
|
76179a1014 | ||
|
|
bdfab46c22 | ||
|
|
5687949560 | ||
|
|
19de5b2205 | ||
|
|
cbc58af260 | ||
|
|
80001ab0eb | ||
|
|
ae29ab9858 | ||
|
|
27407fc1f5 | ||
|
|
1a17da2ff9 | ||
|
|
50248e440a | ||
|
|
0f0079f394 | ||
|
|
68267ed878 | ||
|
|
fd25d4ad85 | ||
|
|
2431534f10 | ||
|
|
fd7d0774e5 | ||
|
|
57ffc3f009 | ||
|
|
4a13f513f8 | ||
|
|
13ad46b67d | ||
|
|
1b45e967b4 | ||
|
|
933bead92e | ||
|
|
3314f49ec4 | ||
|
|
bc930aa5ff | ||
|
|
67b44c9493 | ||
|
|
758a381644 | ||
|
|
eec31aaca5 | ||
|
|
595738f6fe | ||
|
|
b5eb4f3126 | ||
|
|
3fecf187ff | ||
|
|
54840fb256 | ||
|
|
e87fddf1ad | ||
|
|
dea400bc1d | ||
|
|
fb663f768d | ||
|
|
e03767b3eb | ||
|
|
8396027516 | ||
|
|
e24f7fd2d2 | ||
|
|
8081f39dab | ||
|
|
3cc4ca821e | ||
|
|
01772cefa5 | ||
|
|
dc90d4bce3 | ||
|
|
f5bc671030 | ||
|
|
d7294c6de8 | ||
|
|
63a752f961 | ||
|
|
03d38b66fd | ||
|
|
b9c9ccfbb4 | ||
|
|
b94110a68f | ||
|
|
8278468041 | ||
|
|
4fbb2531b3 | ||
|
|
59f8e6fb77 | ||
|
|
05d89b5c05 | ||
|
|
fe5c41fb8a | ||
|
|
9a45543841 | ||
|
|
766e67ef3b | ||
|
|
4c94d8cc2c | ||
|
|
d2de2d5eda | ||
|
|
96a7695275 | ||
|
|
63166c0ca0 | ||
|
|
a881abfd1e | ||
|
|
d3ac7075a2 | ||
|
|
0c9080c7a9 | ||
|
|
b8fcb7b151 | ||
|
|
5d6de9075d | ||
|
|
21cff3015a | ||
|
|
5ca2709ba0 | ||
|
|
bb1cb7b9f1 | ||
|
|
c64f36ab2b | ||
|
|
e4830ddce3 | ||
|
|
758e330d6e | ||
|
|
8e3bd752bb | ||
|
|
a84bca67fc | ||
|
|
4ccfb2f9bc | ||
|
|
ce9609296c | ||
|
|
b1450721be | ||
|
|
b04294d3d8 | ||
|
|
22c2704842 | ||
|
|
ee22af5ced | ||
|
|
cc117253c8 | ||
|
|
8e226e6a09 | ||
|
|
1d420c2c11 | ||
|
|
5378331f2d | ||
|
|
894a264671 | ||
|
|
e1a127245d | ||
|
|
afb5ef72e1 | ||
|
|
1d8edd347d | ||
|
|
41d9ea948c | ||
|
|
ee912e5a7f | ||
|
|
ad446a0eb0 | ||
|
|
176cb39feb | ||
|
|
64ad009fb2 | ||
|
|
dcb1a08906 | ||
|
|
1788501eed | ||
|
|
bee1a5aeb2 | ||
|
|
994aba062b | ||
|
|
e869d17eb1 | ||
|
|
9a0221ef58 | ||
|
|
07008b5d45 | ||
|
|
8f31eda649 | ||
|
|
da94130160 | ||
|
|
468fbdbde7 | ||
|
|
eb45f8b631 | ||
|
|
df0df08bc5 | ||
|
|
9618aaf07d | ||
|
|
bec9bc4154 | ||
|
|
955a811aa1 | ||
|
|
c9902be294 | ||
|
|
112c9d53ab | ||
|
|
0789451458 | ||
|
|
d97af0de26 | ||
|
|
1917149fdd | ||
|
|
0845e73b6a | ||
|
|
7dfa952091 | ||
|
|
02134b4a87 | ||
|
|
bdb1db4d37 | ||
|
|
02a6891939 | ||
|
|
741c6f5006 | ||
|
|
6152973f9c | ||
|
|
243a808067 | ||
|
|
62c36a6056 | ||
|
|
74719afc33 | ||
|
|
7e59a8901d | ||
|
|
abc6eac404 | ||
|
|
054e0b888a | ||
|
|
da252eeff7 | ||
|
|
9890bedaab | ||
|
|
0a0c9593b8 | ||
|
|
d470792517 | ||
|
|
439236773b | ||
|
|
2a2b64d6ac | ||
|
|
daa937c043 | ||
|
|
21455162b5 | ||
|
|
99cc4b20f2 | ||
|
|
0969a971cd | ||
|
|
be6d215f67 | ||
|
|
e74817f8aa | ||
|
|
043c2ac258 | ||
|
|
88a2023c32 | ||
|
|
5ef1989c4b | ||
|
|
f380d627f8 | ||
|
|
aafa106d10 | ||
|
|
29b7b8e52c | ||
|
|
a9dd2c6717 | ||
|
|
aee3f97acb | ||
|
|
46968b8ffa | ||
|
|
6cdf0d8768 | ||
|
|
07ff251a17 | ||
|
|
b3b7cf7335 | ||
|
|
8622dc18bf | ||
|
|
d22e2644dd | ||
|
|
f529a22ca5 | ||
|
|
783ba75129 | ||
|
|
d49e4dd752 | ||
|
|
981fddea02 | ||
|
|
81b953f27e | ||
|
|
b9acf4b4af | ||
|
|
44ace0aefd | ||
|
|
5981161360 | ||
|
|
763c64be9b | ||
|
|
f0dffaa209 | ||
|
|
77a6836fef | ||
|
|
9f043e8017 | ||
|
|
6ac047afc4 | ||
|
|
0e1058cfdd | ||
|
|
26679b1a48 | ||
|
|
e04c2b9aa8 | ||
|
|
0c031dcc8b | ||
|
|
c332c4e927 | ||
|
|
964c503a63 | ||
|
|
90d87e3a78 | ||
|
|
f76bca23ef | ||
|
|
a5fd7d722c | ||
|
|
4e0bf95622 | ||
|
|
498a934abf | ||
|
|
ce53872684 | ||
|
|
da96eb2cce | ||
|
|
fada8efd01 | ||
|
|
d916487d7c | ||
|
|
1bf762ba38 | ||
|
|
541e722360 | ||
|
|
807ba6301c | ||
|
|
03f3f9e707 | ||
|
|
a78eecee48 | ||
|
|
43461d267f | ||
|
|
fae0b52825 | ||
|
|
22439a7511 | ||
|
|
7ecc3ce827 | ||
|
|
4e91384e14 | ||
|
|
de79e7fc58 | ||
|
|
8b5b42563c | ||
|
|
502343b402 | ||
|
|
82d09fbf0d | ||
|
|
9be85d1648 | ||
|
|
45e533599f | ||
|
|
94144a7821 | ||
|
|
4daae2165d | ||
|
|
cdd261f332 | ||
|
|
034f16892e | ||
|
|
c759520bc8 | ||
|
|
80b3d28f4a | ||
|
|
69d97712ec | ||
|
|
5d89dad9b8 | ||
|
|
525a91fed3 | ||
|
|
868d16dee0 | ||
|
|
3393fd7f25 | ||
|
|
00c122cc29 | ||
|
|
03160920cf | ||
|
|
b42cc6a4d2 | ||
|
|
a6ad644bf2 | ||
|
|
39fd5bb9b0 | ||
|
|
5482992dec | ||
|
|
024528e0c2 | ||
|
|
ef37c87e93 | ||
|
|
9e029f98b9 | ||
|
|
e962330df9 | ||
|
|
b291e50006 | ||
|
|
9ae0c082a3 | ||
|
|
accd7bc253 | ||
|
|
6153200bc2 | ||
|
|
2e7595522d | ||
|
|
b79ffdc4ce | ||
|
|
592a3f289b | ||
|
|
9cdeb275e7 | ||
|
|
c957b270d2 | ||
|
|
92791b9dd6 | ||
|
|
45cad43c3f | ||
|
|
dad5a01fb0 | ||
|
|
66713181c1 | ||
|
|
7135803cc8 | ||
|
|
b7567b6673 | ||
|
|
f8c95fa9e8 | ||
|
|
9c965d0ffb | ||
|
|
6103dca8ee | ||
|
|
18cd7adac6 | ||
|
|
41223a01ec | ||
|
|
e78cf96c97 | ||
|
|
0cb3843956 | ||
|
|
907368ead5 | ||
|
|
81489beea1 | ||
|
|
ac30aad09b | ||
|
|
2c89651fc6 | ||
|
|
3a89c973ff | ||
|
|
52d5e8032c | ||
|
|
19c178fa14 | ||
|
|
45ccb0e881 | ||
|
|
4be18fa889 | ||
|
|
7c261a13b5 | ||
|
|
2bb94d9e22 | ||
|
|
a3235ad270 | ||
|
|
f6a9cc9a67 | ||
|
|
28d47a0aaa | ||
|
|
eb617df2d8 | ||
|
|
a580480b07 | ||
|
|
0dd6d1080e | ||
|
|
3c820d622e | ||
|
|
d6c867ea50 | ||
|
|
2079cfe8fa | ||
|
|
dfe03c7abe | ||
|
|
8cd67ac676 | ||
|
|
9828893f7e | ||
|
|
6990da0057 | ||
|
|
d985dc0629 | ||
|
|
451124005b | ||
|
|
2f1fa3d427 | ||
|
|
189d64409b | ||
|
|
60cc473bcf | ||
|
|
43a2ee6ea1 | ||
|
|
4b91e9cde6 | ||
|
|
26c5f56ad1 | ||
|
|
daa858ef27 | ||
|
|
ea169d2083 | ||
|
|
c31aa863ac | ||
|
|
ff08b0f406 | ||
|
|
c1801e623f | ||
|
|
a404f6e0e6 | ||
|
|
2d2440a753 | ||
|
|
0c4ed2b3dc | ||
|
|
b945fae40e | ||
|
|
dadba4d6ed | ||
|
|
d600a6b5b3 | ||
|
|
75ac0d2172 | ||
|
|
9f3785a2f3 | ||
|
|
84e92e0976 | ||
|
|
b8a049abed | ||
|
|
e18f681333 | ||
|
|
dd1aa28390 | ||
|
|
abbf14cd32 | ||
|
|
e8292f076d | ||
|
|
3037dccaf6 | ||
|
|
a7793468aa | ||
|
|
caa987faad | ||
|
|
0892f0e5b7 | ||
|
|
1d7857bc63 | ||
|
|
72a1d81b51 | ||
|
|
2c580a0afb | ||
|
|
05e8b0ae80 | ||
|
|
16dd77e8d9 | ||
|
|
0600f968c3 | ||
|
|
971b1d557d | ||
|
|
11d97c34d5 | ||
|
|
66be42549e | ||
|
|
067db30d71 | ||
|
|
da4dbe65b2 | ||
|
|
4321fde2a7 | ||
|
|
8f3df1d471 | ||
|
|
1a17211707 | ||
|
|
d56e3a6481 | ||
|
|
7820a47184 | ||
|
|
88b185ada1 |
81
auxlib.c
81
auxlib.c
@@ -1,81 +0,0 @@
|
||||
char *rcs_auxlib="$Id: auxlib.c,v 1.4 1997/04/07 14:48:53 roberto Exp roberto $";
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lua.h"
|
||||
#include "auxlib.h"
|
||||
#include "luadebug.h"
|
||||
|
||||
|
||||
|
||||
int luaI_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 luaL_arg_check(int cond, int numarg, char *extramsg)
|
||||
{
|
||||
if (!cond) {
|
||||
char *funcname;
|
||||
lua_getobjname(lua_stackedfunction(0), &funcname);
|
||||
if (funcname == NULL)
|
||||
funcname = "???";
|
||||
if (extramsg == NULL)
|
||||
luaL_verror("bad argument #%d to function `%s'", numarg, funcname);
|
||||
else
|
||||
luaL_verror("bad argument #%d to function `%s' (%s)",
|
||||
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);
|
||||
}
|
||||
|
||||
void luaL_openlib (struct luaL_reg *l, int n)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<n; i++)
|
||||
lua_register(l[i].name, l[i].func);
|
||||
}
|
||||
|
||||
|
||||
void luaL_verror (char *fmt, ...)
|
||||
{
|
||||
char buff[1000];
|
||||
va_list argp;
|
||||
va_start(argp, fmt);
|
||||
vsprintf(buff, fmt, argp);
|
||||
va_end(argp);
|
||||
lua_error(buff);
|
||||
}
|
||||
30
auxlib.h
30
auxlib.h
@@ -1,30 +0,0 @@
|
||||
/*
|
||||
** $Id: auxlib.h,v 1.2 1997/04/06 14:08:08 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef auxlib_h
|
||||
#define auxlib_h
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
struct luaL_reg {
|
||||
char *name;
|
||||
lua_CFunction func;
|
||||
};
|
||||
|
||||
void luaL_openlib (struct luaL_reg *l, int n);
|
||||
void luaL_arg_check(int cond, 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);
|
||||
void luaL_verror (char *fmt, ...);
|
||||
|
||||
|
||||
|
||||
/* -- private part (only for Lua modules */
|
||||
|
||||
int luaI_findstring (char *name, char *list[]);
|
||||
|
||||
|
||||
#endif
|
||||
77
bugs
Normal file
77
bugs
Normal file
@@ -0,0 +1,77 @@
|
||||
** 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.
|
||||
|
||||
|
||||
--- Version 3.1 alpha
|
||||
|
||||
** lua.c
|
||||
Thu Jan 15 14:34:58 EDT 1998
|
||||
>> must include "stdlib.h" (for "exit()").
|
||||
|
||||
** lbuiltin.c / lobject.h
|
||||
Thu Jan 15 14:34:58 EDT 1998
|
||||
>> MAX_WORD may be bigger than MAX_INT
|
||||
|
||||
|
||||
** llex.c
|
||||
Mon Jan 19 18:17:18 EDT 1998
|
||||
>> wrong line number (+1) in error report when file starts with "#..."
|
||||
|
||||
** lstrlib.c
|
||||
Tue Jan 27 15:27:49 EDT 1998
|
||||
>> formats like "%020d" were considered too big (3 digits); moreover,
|
||||
>> some sistems limit printf to at most 500 chars, so we can limit sizes
|
||||
>> to 2 digits (99).
|
||||
|
||||
** lapi.c
|
||||
Tue Jan 27 17:12:36 EDT 1998
|
||||
>> "lua_getstring" may create a new string, so should check GC
|
||||
|
||||
** lstring.c / ltable.c
|
||||
Wed Jan 28 14:48:12 EDT 1998
|
||||
>> tables can become full of "empty" slots, and keep growing without limits.
|
||||
|
||||
** lstrlib.c
|
||||
Mon Mar 9 15:26:09 EST 1998
|
||||
>> gsub('a', '(b?)%1*' ...) loops (because the capture is empty).
|
||||
|
||||
** lstrlib.c
|
||||
Mon May 18 19:20:00 EST 1998
|
||||
>> arguments for "format" 'x', 'X', 'o' and 'u' must be unsigned int.
|
||||
|
||||
|
||||
--- Version 3.1
|
||||
|
||||
** liolib.c / lauxlib.c
|
||||
Mon Sep 7 15:57:02 EST 1998
|
||||
>> function "luaL_argerror" prints wrong argument number (from a user's point
|
||||
of view) when functions have upvalues.
|
||||
|
||||
** lstrlib.c
|
||||
Tue Nov 10 17:29:36 EDT 1998
|
||||
>> gsub/strfind do not check whether captures are properly finished.
|
||||
|
||||
** lbuiltin.c
|
||||
Fri Dec 18 11:22:55 EDT 1998
|
||||
>> "tonumber" goes crazy with negative numbers in other bases (not 10),
|
||||
because "strtol" returns long, not unsigned long.
|
||||
|
||||
** lstrlib.c
|
||||
Mon Jan 4 10:41:40 EDT 1999
|
||||
>> "format" does not check size of format item (such as "%00000...00000d").
|
||||
|
||||
** lapi.c
|
||||
Wed Feb 3 14:40:21 EDT 1999
|
||||
>> getlocal cannot return the local itself, since lua_isstring and
|
||||
lua_isnumber can modify it.
|
||||
|
||||
** lstrlib.c
|
||||
Thu Feb 4 17:08:50 EDT 1999
|
||||
>> format "%s" may break limit of "sprintf" on some machines.
|
||||
|
||||
|
||||
** lzio.c
|
||||
Thu Mar 4 11:49:37 EST 1999
|
||||
>> file stream cannot call fread after EOF.
|
||||
368
fallback.c
368
fallback.c
@@ -1,368 +0,0 @@
|
||||
/*
|
||||
** fallback.c
|
||||
** TecCGraf - PUC-Rio
|
||||
*/
|
||||
|
||||
char *rcs_fallback="$Id: fallback.c,v 2.8 1997/06/17 17:27:07 roberto Exp roberto $";
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "auxlib.h"
|
||||
#include "luamem.h"
|
||||
#include "fallback.h"
|
||||
#include "opcode.h"
|
||||
#include "lua.h"
|
||||
#include "table.h"
|
||||
#include "tree.h"
|
||||
#include "hash.h"
|
||||
|
||||
|
||||
|
||||
/* -------------------------------------------
|
||||
** Reference routines
|
||||
*/
|
||||
|
||||
static struct ref {
|
||||
TObject o;
|
||||
enum {LOCK, HOLD, FREE, COLLECTED} status;
|
||||
} *refArray = NULL;
|
||||
static int refSize = 0;
|
||||
|
||||
int luaI_ref (TObject *object, int lock)
|
||||
{
|
||||
int i;
|
||||
int oldSize;
|
||||
if (ttype(object) == LUA_T_NIL)
|
||||
return -1; /* special ref for nil */
|
||||
for (i=0; i<refSize; i++)
|
||||
if (refArray[i].status == FREE)
|
||||
goto found;
|
||||
/* no more empty spaces */
|
||||
oldSize = refSize;
|
||||
refSize = growvector(&refArray, refSize, struct ref, refEM, MAX_WORD);
|
||||
for (i=oldSize; i<refSize; i++)
|
||||
refArray[i].status = FREE;
|
||||
i = oldSize;
|
||||
found:
|
||||
refArray[i].o = *object;
|
||||
refArray[i].status = lock ? LOCK : HOLD;
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
void lua_unref (int ref)
|
||||
{
|
||||
if (ref >= 0 && ref < refSize)
|
||||
refArray[ref].status = FREE;
|
||||
}
|
||||
|
||||
|
||||
TObject *luaI_getref (int ref)
|
||||
{
|
||||
static TObject nul = {LUA_T_NIL, {0}};
|
||||
if (ref == -1)
|
||||
return &nul;
|
||||
if (ref >= 0 && ref < refSize &&
|
||||
(refArray[ref].status == LOCK || refArray[ref].status == HOLD))
|
||||
return &refArray[ref].o;
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
void luaI_travlock (int (*fn)(TObject *))
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<refSize; i++)
|
||||
if (refArray[i].status == LOCK)
|
||||
fn(&refArray[i].o);
|
||||
}
|
||||
|
||||
|
||||
void luaI_invalidaterefs (void)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<refSize; i++)
|
||||
if (refArray[i].status == HOLD && !luaI_ismarked(&refArray[i].o))
|
||||
refArray[i].status = COLLECTED;
|
||||
}
|
||||
|
||||
|
||||
/* -------------------------------------------
|
||||
* Internal Methods
|
||||
*/
|
||||
|
||||
char *luaI_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 = luaI_findstring(name, list);
|
||||
if (e < 0)
|
||||
luaL_verror("`%s' is not a valid event name", name);
|
||||
return e;
|
||||
}
|
||||
|
||||
|
||||
struct IM *luaI_IMtable = NULL;
|
||||
|
||||
static int IMtable_size = 0;
|
||||
static int last_tag = LUA_T_NIL; /* ORDER LUA_T */
|
||||
|
||||
|
||||
/* events in LUA_T_LINE are all allowed, since this is used as a
|
||||
* 'placeholder' for "default" fallbacks
|
||||
*/
|
||||
static char validevents[NUM_TYPES][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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_T_LINE */
|
||||
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_CMARK */
|
||||
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* LUA_T_MARK */
|
||||
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_CFUNCTION */
|
||||
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* LUA_T_FUNCTION */
|
||||
{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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_STRING */
|
||||
{1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_T_NUMBER */
|
||||
{1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} /* LUA_T_NIL */
|
||||
};
|
||||
|
||||
static int validevent (lua_Type 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(luaI_getim(tag, i)) = LUA_T_NIL;
|
||||
}
|
||||
|
||||
void luaI_initfallbacks (void)
|
||||
{
|
||||
if (luaI_IMtable == NULL) {
|
||||
int i;
|
||||
IMtable_size = NUM_TYPES+10;
|
||||
luaI_IMtable = newvector(IMtable_size, struct IM);
|
||||
for (i=LUA_T_NIL; i<=LUA_T_USERDATA; i++)
|
||||
init_entry(i);
|
||||
}
|
||||
}
|
||||
|
||||
int lua_newtag (void)
|
||||
{
|
||||
--last_tag;
|
||||
if ((-last_tag) >= IMtable_size) {
|
||||
luaI_initfallbacks();
|
||||
IMtable_size = growvector(&luaI_IMtable, IMtable_size,
|
||||
struct IM, memEM, MAX_INT);
|
||||
}
|
||||
init_entry(last_tag);
|
||||
return last_tag;
|
||||
}
|
||||
|
||||
|
||||
static void checktag (int tag)
|
||||
{
|
||||
if (!(last_tag <= tag && tag <= 0))
|
||||
luaL_verror("%d is not a valid tag", tag);
|
||||
}
|
||||
|
||||
void luaI_realtag (int tag)
|
||||
{
|
||||
if (!(last_tag <= tag && tag < LUA_T_NIL))
|
||||
luaL_verror("tag %d is not result of `newtag'", tag);
|
||||
}
|
||||
|
||||
|
||||
void luaI_settag (int tag, TObject *o)
|
||||
{
|
||||
luaI_realtag(tag);
|
||||
switch (ttype(o)) {
|
||||
case LUA_T_ARRAY:
|
||||
o->value.a->htag = tag;
|
||||
break;
|
||||
case LUA_T_USERDATA:
|
||||
o->value.ts->tag = tag;
|
||||
break;
|
||||
default:
|
||||
luaL_verror("cannot change the tag of a %s", luaI_typenames[-ttype(o)]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int luaI_efectivetag (TObject *o)
|
||||
{
|
||||
lua_Type t = ttype(o);
|
||||
if (t == LUA_T_USERDATA) {
|
||||
int tag = o->value.ts->tag;
|
||||
return (tag >= 0) ? LUA_T_USERDATA : tag;
|
||||
}
|
||||
else if (t == LUA_T_ARRAY)
|
||||
return o->value.a->htag;
|
||||
else return t;
|
||||
}
|
||||
|
||||
|
||||
void luaI_gettagmethod (void)
|
||||
{
|
||||
int t = (int)luaL_check_number(1);
|
||||
int e = luaI_checkevent(luaL_check_string(2), luaI_eventname);
|
||||
checktag(t);
|
||||
if (validevent(t, e))
|
||||
luaI_pushobject(luaI_getim(t,e));
|
||||
}
|
||||
|
||||
|
||||
void luaI_settagmethod (void)
|
||||
{
|
||||
int t = (int)luaL_check_number(1);
|
||||
int e = luaI_checkevent(luaL_check_string(2), luaI_eventname);
|
||||
lua_Object func = lua_getparam(3);
|
||||
checktag(t);
|
||||
if (!validevent(t, e))
|
||||
luaL_verror("cannot change internal method `%s' for tag %d",
|
||||
luaI_eventname[e], t);
|
||||
luaL_arg_check(lua_isnil(func) || lua_isfunction(func),
|
||||
3, "function expected");
|
||||
luaI_pushobject(luaI_getim(t,e));
|
||||
*luaI_getim(t, e) = *luaI_Address(func);
|
||||
}
|
||||
|
||||
|
||||
static void stderrorim (void)
|
||||
{
|
||||
lua_Object s = lua_getparam(1);
|
||||
if (lua_isstring(s))
|
||||
fprintf(stderr, "lua: %s\n", lua_getstring(s));
|
||||
}
|
||||
|
||||
static TObject errorim = {LUA_T_CFUNCTION, {stderrorim}};
|
||||
|
||||
|
||||
TObject *luaI_geterrorim (void)
|
||||
{
|
||||
return &errorim;
|
||||
}
|
||||
|
||||
void luaI_seterrormethod (void)
|
||||
{
|
||||
lua_Object func = lua_getparam(1);
|
||||
luaL_arg_check(lua_isnil(func) || lua_isfunction(func),
|
||||
1, "function expected");
|
||||
luaI_pushobject(&errorim);
|
||||
errorim = *luaI_Address(func);
|
||||
}
|
||||
|
||||
char *luaI_travfallbacks (int (*fn)(TObject *))
|
||||
{
|
||||
int e;
|
||||
if (fn(&errorim))
|
||||
return "error";
|
||||
for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) { /* ORDER IM */
|
||||
int t;
|
||||
for (t=0; t>=last_tag; t--)
|
||||
if (fn(luaI_getim(t,e)))
|
||||
return luaI_eventname[e];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* ===================================================================
|
||||
* compatibility with old fallback system
|
||||
*/
|
||||
#if LUA_COMPAT2_5
|
||||
|
||||
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))
|
||||
*luaI_getim(t, e) = *func;
|
||||
}
|
||||
|
||||
|
||||
void luaI_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);
|
||||
luaI_initfallbacks();
|
||||
luaL_arg_check(lua_isfunction(func), 2, "function expected");
|
||||
switch (luaI_findstring(name, oldnames)) {
|
||||
case 0: /* old error fallback */
|
||||
oldfunc = errorim;
|
||||
errorim = *luaI_Address(func);
|
||||
replace = errorFB;
|
||||
break;
|
||||
case 1: /* old getglobal fallback */
|
||||
oldfunc = *luaI_getim(LUA_T_NIL, IM_GETGLOBAL);
|
||||
*luaI_getim(LUA_T_NIL, IM_GETGLOBAL) = *luaI_Address(func);
|
||||
replace = nilFB;
|
||||
break;
|
||||
case 2: { /* old arith fallback */
|
||||
int i;
|
||||
oldfunc = *luaI_getim(LUA_T_NUMBER, IM_POW);
|
||||
for (i=IM_ADD; i<=IM_UNM; i++) /* ORDER IM */
|
||||
fillvalids(i, luaI_Address(func));
|
||||
replace = typeFB;
|
||||
break;
|
||||
}
|
||||
case 3: { /* old order fallback */
|
||||
int i;
|
||||
oldfunc = *luaI_getim(LUA_T_LINE, IM_LT);
|
||||
for (i=IM_LT; i<=IM_GE; i++) /* ORDER IM */
|
||||
fillvalids(i, luaI_Address(func));
|
||||
replace = typeFB;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
int e;
|
||||
if ((e = luaI_findstring(name, luaI_eventname)) >= 0) {
|
||||
oldfunc = *luaI_getim(LUA_T_LINE, e);
|
||||
fillvalids(e, luaI_Address(func));
|
||||
replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB;
|
||||
}
|
||||
else {
|
||||
luaL_verror("`%s' is not a valid fallback name", name);
|
||||
replace = NULL; /* to avoid warnings */
|
||||
}
|
||||
}
|
||||
}
|
||||
if (oldfunc.ttype != LUA_T_NIL)
|
||||
luaI_pushobject(&oldfunc);
|
||||
else
|
||||
lua_pushcfunction(replace);
|
||||
}
|
||||
#endif
|
||||
65
fallback.h
65
fallback.h
@@ -1,65 +0,0 @@
|
||||
/*
|
||||
** $Id: fallback.h,v 1.22 1997/04/04 22:24:51 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef fallback_h
|
||||
#define fallback_h
|
||||
|
||||
#include "lua.h"
|
||||
#include "opcode.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
|
||||
|
||||
|
||||
extern struct IM {
|
||||
TObject int_method[IM_N];
|
||||
} *luaI_IMtable;
|
||||
|
||||
extern char *luaI_eventname[];
|
||||
|
||||
#define luaI_getim(tag,event) (&luaI_IMtable[-(tag)].int_method[event])
|
||||
#define luaI_getimbyObj(o,e) (luaI_getim(luaI_efectivetag(o),(e)))
|
||||
|
||||
void luaI_setfallback (void);
|
||||
int luaI_ref (TObject *object, int lock);
|
||||
TObject *luaI_getref (int ref);
|
||||
void luaI_travlock (int (*fn)(TObject *));
|
||||
void luaI_invalidaterefs (void);
|
||||
char *luaI_travfallbacks (int (*fn)(TObject *));
|
||||
|
||||
void luaI_settag (int tag, TObject *o);
|
||||
void luaI_realtag (int tag);
|
||||
TObject *luaI_geterrorim (void);
|
||||
int luaI_efectivetag (TObject *o);
|
||||
void luaI_settagmethod (void);
|
||||
void luaI_gettagmethod (void);
|
||||
void luaI_seterrormethod (void);
|
||||
void luaI_initfallbacks (void);
|
||||
|
||||
#endif
|
||||
|
||||
166
func.c
166
func.c
@@ -1,166 +0,0 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "luadebug.h"
|
||||
#include "table.h"
|
||||
#include "luamem.h"
|
||||
#include "func.h"
|
||||
#include "opcode.h"
|
||||
#include "inout.h"
|
||||
|
||||
|
||||
static TFunc *function_root = NULL;
|
||||
static LocVar *currvars = NULL;
|
||||
static int numcurrvars = 0;
|
||||
static int maxcurrvars = 0;
|
||||
|
||||
|
||||
/*
|
||||
** Initialize TFunc struct
|
||||
*/
|
||||
void luaI_initTFunc (TFunc *f)
|
||||
{
|
||||
f->next = NULL;
|
||||
f->marked = 0;
|
||||
f->size = 0;
|
||||
f->code = NULL;
|
||||
f->lineDefined = 0;
|
||||
f->fileName = lua_parsedfile;
|
||||
f->locvars = NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
** Insert function in list for GC
|
||||
*/
|
||||
void luaI_insertfunction (TFunc *f)
|
||||
{
|
||||
lua_pack();
|
||||
f->next = function_root;
|
||||
function_root = f;
|
||||
f->marked = 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Free function
|
||||
*/
|
||||
void luaI_freefunc (TFunc *f)
|
||||
{
|
||||
luaI_free (f->code);
|
||||
luaI_free (f->locvars);
|
||||
luaI_free (f);
|
||||
}
|
||||
|
||||
|
||||
void luaI_funcfree (TFunc *l)
|
||||
{
|
||||
while (l) {
|
||||
TFunc *next = l->next;
|
||||
luaI_freefunc(l);
|
||||
l = next;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Garbage collection function.
|
||||
*/
|
||||
TFunc *luaI_funccollector (long *acum)
|
||||
{
|
||||
TFunc *curr = function_root;
|
||||
TFunc *prev = NULL;
|
||||
TFunc *frees = NULL;
|
||||
long counter = 0;
|
||||
while (curr) {
|
||||
TFunc *next = curr->next;
|
||||
if (!curr->marked) {
|
||||
if (prev == NULL)
|
||||
function_root = next;
|
||||
else
|
||||
prev->next = next;
|
||||
curr->next = frees;
|
||||
frees = curr;
|
||||
++counter;
|
||||
}
|
||||
else {
|
||||
curr->marked = 0;
|
||||
prev = curr;
|
||||
}
|
||||
curr = next;
|
||||
}
|
||||
*acum += counter;
|
||||
return frees;
|
||||
}
|
||||
|
||||
|
||||
void lua_funcinfo (lua_Object func, char **filename, int *linedefined)
|
||||
{
|
||||
TObject *f = luaI_Address(func);
|
||||
if (f->ttype == LUA_T_MARK || f->ttype == LUA_T_FUNCTION)
|
||||
{
|
||||
*filename = f->value.tf->fileName;
|
||||
*linedefined = f->value.tf->lineDefined;
|
||||
}
|
||||
else if (f->ttype == LUA_T_CMARK || f->ttype == LUA_T_CFUNCTION)
|
||||
{
|
||||
*filename = "(C)";
|
||||
*linedefined = -1;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Stores information to know that variable has been declared in given line
|
||||
*/
|
||||
void luaI_registerlocalvar (TaggedString *varname, int line)
|
||||
{
|
||||
if (numcurrvars >= maxcurrvars)
|
||||
maxcurrvars = growvector(&currvars, maxcurrvars, LocVar, "", MAX_WORD);
|
||||
currvars[numcurrvars].varname = varname;
|
||||
currvars[numcurrvars].line = line;
|
||||
numcurrvars++;
|
||||
}
|
||||
|
||||
/*
|
||||
** Stores information to know that variable has been out of scope in given line
|
||||
*/
|
||||
void luaI_unregisterlocalvar (int line)
|
||||
{
|
||||
luaI_registerlocalvar(NULL, line);
|
||||
}
|
||||
|
||||
/*
|
||||
** Copies "currvars" into a new area and store it in function header.
|
||||
** The values (varname = NULL, line = -1) signal the end of vector.
|
||||
*/
|
||||
void luaI_closelocalvars (TFunc *func)
|
||||
{
|
||||
func->locvars = newvector (numcurrvars+1, LocVar);
|
||||
memcpy (func->locvars, currvars, numcurrvars*sizeof(LocVar));
|
||||
func->locvars[numcurrvars].varname = NULL;
|
||||
func->locvars[numcurrvars].line = -1;
|
||||
numcurrvars = 0; /* prepares for next function */
|
||||
}
|
||||
|
||||
/*
|
||||
** Look for n-esim local variable at line "line" in function "func".
|
||||
** Returns NULL if not found.
|
||||
*/
|
||||
char *luaI_getlocalname (TFunc *func, int local_number, int line)
|
||||
{
|
||||
int count = 0;
|
||||
char *varname = NULL;
|
||||
LocVar *lv = func->locvars;
|
||||
if (lv == NULL)
|
||||
return NULL;
|
||||
for (; lv->line != -1 && lv->line < line; lv++)
|
||||
{
|
||||
if (lv->varname) /* register */
|
||||
{
|
||||
if (++count == local_number)
|
||||
varname = lv->varname->str;
|
||||
}
|
||||
else /* unregister */
|
||||
if (--count < local_number)
|
||||
varname = NULL;
|
||||
}
|
||||
return varname;
|
||||
}
|
||||
|
||||
45
func.h
45
func.h
@@ -1,45 +0,0 @@
|
||||
/*
|
||||
** $Id: func.h,v 1.8 1996/03/14 15:54:20 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef func_h
|
||||
#define func_h
|
||||
|
||||
#include "types.h"
|
||||
#include "lua.h"
|
||||
#include "tree.h"
|
||||
|
||||
typedef struct LocVar
|
||||
{
|
||||
TaggedString *varname; /* NULL signals end of scope */
|
||||
int line;
|
||||
} LocVar;
|
||||
|
||||
|
||||
/*
|
||||
** Function Headers
|
||||
*/
|
||||
typedef struct TFunc
|
||||
{
|
||||
struct TFunc *next;
|
||||
int marked;
|
||||
int size;
|
||||
Byte *code;
|
||||
int lineDefined;
|
||||
char *fileName;
|
||||
LocVar *locvars;
|
||||
} TFunc;
|
||||
|
||||
TFunc *luaI_funccollector (long *cont);
|
||||
void luaI_funcfree (TFunc *l);
|
||||
void luaI_insertfunction (TFunc *f);
|
||||
|
||||
void luaI_initTFunc (TFunc *f);
|
||||
void luaI_freefunc (TFunc *f);
|
||||
|
||||
void luaI_registerlocalvar (TaggedString *varname, int line);
|
||||
void luaI_unregisterlocalvar (int line);
|
||||
void luaI_closelocalvars (TFunc *func);
|
||||
char *luaI_getlocalname (TFunc *func, int local_number, int line);
|
||||
|
||||
#endif
|
||||
332
hash.c
332
hash.c
@@ -1,332 +0,0 @@
|
||||
/*
|
||||
** hash.c
|
||||
** hash manager for lua
|
||||
*/
|
||||
|
||||
char *rcs_hash="$Id: hash.c,v 2.42 1997/05/08 20:43:30 roberto Exp roberto $";
|
||||
|
||||
|
||||
#include "luamem.h"
|
||||
#include "opcode.h"
|
||||
#include "hash.h"
|
||||
#include "table.h"
|
||||
#include "lua.h"
|
||||
#include "auxlib.h"
|
||||
|
||||
|
||||
#define nhash(t) ((t)->nhash)
|
||||
#define nuse(t) ((t)->nuse)
|
||||
#define markarray(t) ((t)->mark)
|
||||
#define nodevector(t) ((t)->node)
|
||||
#define node(t,i) (&(t)->node[i])
|
||||
#define ref(n) (&(n)->ref)
|
||||
#define val(n) (&(n)->val)
|
||||
|
||||
|
||||
#define REHASH_LIMIT 0.70 /* avoid more than this % full */
|
||||
|
||||
#define TagDefault LUA_T_ARRAY;
|
||||
|
||||
|
||||
static Hash *listhead = 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 luaI_redimension (int nhash)
|
||||
{
|
||||
int i;
|
||||
for (i=0; dimensions[i]<MAX_INT; i++)
|
||||
{
|
||||
if (dimensions[i] > nhash)
|
||||
return dimensions[i];
|
||||
}
|
||||
lua_error("table overflow");
|
||||
return 0; /* to avoid warnings */
|
||||
}
|
||||
|
||||
|
||||
int lua_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_FUNCTION: return t1->value.tf == t2->value.tf;
|
||||
case LUA_T_CFUNCTION: return fvalue(t1) == fvalue(t2);
|
||||
default:
|
||||
lua_error("internal error in `lua_equalObj'");
|
||||
return 0; /* UNREACHEABLE */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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 = tsvalue(ref)->hash; break;
|
||||
case LUA_T_FUNCTION:
|
||||
h = (IntPoint)ref->value.tf; break;
|
||||
case LUA_T_CFUNCTION:
|
||||
h = (IntPoint)fvalue(ref); break;
|
||||
case LUA_T_ARRAY:
|
||||
h = (IntPoint)avalue(ref); break;
|
||||
default:
|
||||
lua_error ("unexpected type to index table");
|
||||
h = 0; /* UNREACHEABLE */
|
||||
}
|
||||
if (h < 0) h = -h;
|
||||
return h;
|
||||
}
|
||||
|
||||
|
||||
static int present (Hash *t, TObject *key)
|
||||
{
|
||||
long int h = hashindex(key);
|
||||
int tsize = nhash(t);
|
||||
int h1 = h%tsize;
|
||||
TObject *rf = ref(node(t, h1));
|
||||
if (ttype(rf) != LUA_T_NIL && !lua_equalObj(key, rf)) {
|
||||
int h2 = h%(tsize-2) + 1;
|
||||
do {
|
||||
h1 = (h1+h2)%tsize;
|
||||
rf = ref(node(t, h1));
|
||||
} while (ttype(rf) != LUA_T_NIL && !lua_equalObj(key, rf));
|
||||
}
|
||||
return h1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Alloc a vector node
|
||||
*/
|
||||
static Node *hashnodecreate (int nhash)
|
||||
{
|
||||
int i;
|
||||
Node *v = newvector (nhash, Node);
|
||||
for (i=0; i<nhash; i++)
|
||||
ttype(ref(&v[i])) = LUA_T_NIL;
|
||||
return v;
|
||||
}
|
||||
|
||||
/*
|
||||
** Create a new hash. Return the hash pointer or NULL on error.
|
||||
*/
|
||||
static Hash *hashcreate (int nhash)
|
||||
{
|
||||
Hash *t = new(Hash);
|
||||
nhash = luaI_redimension((int)((float)nhash/REHASH_LIMIT));
|
||||
nodevector(t) = hashnodecreate(nhash);
|
||||
nhash(t) = nhash;
|
||||
nuse(t) = 0;
|
||||
markarray(t) = 0;
|
||||
t->htag = TagDefault;
|
||||
return t;
|
||||
}
|
||||
|
||||
/*
|
||||
** Delete a hash
|
||||
*/
|
||||
static void hashdelete (Hash *t)
|
||||
{
|
||||
luaI_free (nodevector(t));
|
||||
luaI_free(t);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Mark a hash and check its elements
|
||||
*/
|
||||
void lua_hashmark (Hash *h)
|
||||
{
|
||||
if (markarray(h) == 0)
|
||||
{
|
||||
int i;
|
||||
markarray(h) = 1;
|
||||
for (i=0; i<nhash(h); i++)
|
||||
{
|
||||
Node *n = node(h,i);
|
||||
if (ttype(ref(n)) != LUA_T_NIL)
|
||||
{
|
||||
lua_markobject(&n->ref);
|
||||
lua_markobject(&n->val);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaI_hashcallIM (Hash *l)
|
||||
{
|
||||
TObject t;
|
||||
ttype(&t) = LUA_T_ARRAY;
|
||||
for (; l; l=l->next) {
|
||||
avalue(&t) = l;
|
||||
luaI_gcIM(&t);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaI_hashfree (Hash *frees)
|
||||
{
|
||||
while (frees) {
|
||||
Hash *next = frees->next;
|
||||
hashdelete(frees);
|
||||
frees = next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Hash *luaI_hashcollector (long *acum)
|
||||
{
|
||||
Hash *curr_array = listhead, *prev = NULL, *frees = NULL;
|
||||
long counter = 0;
|
||||
while (curr_array != NULL) {
|
||||
Hash *next = curr_array->next;
|
||||
if (markarray(curr_array) != 1) {
|
||||
if (prev == NULL)
|
||||
listhead = next;
|
||||
else
|
||||
prev->next = next;
|
||||
curr_array->next = frees;
|
||||
frees = curr_array;
|
||||
++counter;
|
||||
}
|
||||
else {
|
||||
markarray(curr_array) = 0;
|
||||
prev = curr_array;
|
||||
}
|
||||
curr_array = next;
|
||||
}
|
||||
*acum += counter;
|
||||
return frees;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Create a new array
|
||||
** This function inserts the new array in the array list. It also
|
||||
** executes garbage collection if the number of arrays created
|
||||
** exceed a pre-defined range.
|
||||
*/
|
||||
Hash *lua_createarray (int nhash)
|
||||
{
|
||||
Hash *array;
|
||||
lua_pack();
|
||||
array = hashcreate(nhash);
|
||||
array->next = listhead;
|
||||
listhead = array;
|
||||
return array;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** 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) = luaI_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 new hash */
|
||||
}
|
||||
luaI_free(vold);
|
||||
}
|
||||
|
||||
/*
|
||||
** If the hash node is present, return its pointer, otherwise return
|
||||
** null.
|
||||
*/
|
||||
TObject *lua_hashget (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 new
|
||||
** node for the given reference and also return its pointer.
|
||||
*/
|
||||
TObject *lua_hashdefine (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));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** 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.
|
||||
*/
|
||||
static void hashnext (Hash *t, int i)
|
||||
{
|
||||
Node *n;
|
||||
int tsize = nhash(t);
|
||||
if (i >= tsize)
|
||||
return;
|
||||
n = node(t, i);
|
||||
while (ttype(ref(n)) == LUA_T_NIL || ttype(val(n)) == LUA_T_NIL) {
|
||||
if (++i >= tsize)
|
||||
return;
|
||||
n = node(t, i);
|
||||
}
|
||||
luaI_pushobject(ref(node(t,i)));
|
||||
luaI_pushobject(val(node(t,i)));
|
||||
}
|
||||
|
||||
void lua_next (void)
|
||||
{
|
||||
Hash *t;
|
||||
lua_Object o = lua_getparam(1);
|
||||
lua_Object r = lua_getparam(2);
|
||||
luaL_arg_check(lua_istable(o), 1, "table expected");
|
||||
luaL_arg_check(r != LUA_NOOBJECT, 2, "value expected");
|
||||
t = avalue(luaI_Address(o));
|
||||
if (lua_isnil(r))
|
||||
hashnext(t, 0);
|
||||
else
|
||||
hashnext(t, present(t, luaI_Address(r))+1);
|
||||
}
|
||||
39
hash.h
39
hash.h
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
** hash.h
|
||||
** hash manager for lua
|
||||
** $Id: hash.h,v 2.15 1997/03/31 14:02:58 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef hash_h
|
||||
#define hash_h
|
||||
|
||||
#include "types.h"
|
||||
#include "opcode.h"
|
||||
|
||||
typedef struct node {
|
||||
TObject ref;
|
||||
TObject val;
|
||||
} Node;
|
||||
|
||||
typedef struct Hash {
|
||||
struct Hash *next;
|
||||
Node *node;
|
||||
int nhash;
|
||||
int nuse;
|
||||
int htag;
|
||||
char mark;
|
||||
} Hash;
|
||||
|
||||
|
||||
int lua_equalObj (TObject *t1, TObject *t2);
|
||||
int luaI_redimension (int nhash);
|
||||
Hash *lua_createarray (int nhash);
|
||||
void lua_hashmark (Hash *h);
|
||||
Hash *luaI_hashcollector (long *count);
|
||||
void luaI_hashcallIM (Hash *l);
|
||||
void luaI_hashfree (Hash *frees);
|
||||
TObject *lua_hashget (Hash *t, TObject *ref);
|
||||
TObject *lua_hashdefine (Hash *t, TObject *ref);
|
||||
void lua_next (void);
|
||||
|
||||
#endif
|
||||
408
inout.c
408
inout.c
@@ -1,408 +0,0 @@
|
||||
/*
|
||||
** inout.c
|
||||
** Provide function to realise the input/output function and debugger
|
||||
** facilities.
|
||||
** Also provides some predefined lua functions.
|
||||
*/
|
||||
|
||||
char *rcs_inout="$Id: inout.c,v 2.68 1997/06/26 20:47:43 roberto Exp roberto $";
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "auxlib.h"
|
||||
#include "fallback.h"
|
||||
#include "hash.h"
|
||||
#include "inout.h"
|
||||
#include "lex.h"
|
||||
#include "lua.h"
|
||||
#include "luamem.h"
|
||||
#include "luamem.h"
|
||||
#include "opcode.h"
|
||||
#include "table.h"
|
||||
#include "tree.h"
|
||||
#include "undump.h"
|
||||
#include "zio.h"
|
||||
|
||||
|
||||
/* Exported variables */
|
||||
Word lua_linenumber;
|
||||
char *lua_parsedfile;
|
||||
|
||||
|
||||
char *luaI_typenames[] = { /* ORDER LUA_T */
|
||||
"userdata", "line", "cmark", "mark", "function",
|
||||
"function", "table", "string", "number", "nil",
|
||||
NULL
|
||||
};
|
||||
|
||||
|
||||
|
||||
void luaI_setparsedfile (char *name)
|
||||
{
|
||||
lua_parsedfile = luaI_createfixedstring(name)->str;
|
||||
}
|
||||
|
||||
|
||||
int lua_doFILE (FILE *f, int bin)
|
||||
{
|
||||
ZIO z;
|
||||
luaZ_Fopen(&z, f);
|
||||
if (bin)
|
||||
return luaI_undump(&z);
|
||||
else {
|
||||
lua_setinput(&z);
|
||||
return lua_domain();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int lua_dofile (char *filename)
|
||||
{
|
||||
int status;
|
||||
int c;
|
||||
FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
|
||||
if (f == NULL)
|
||||
return 2;
|
||||
luaI_setparsedfile(filename?filename:"(stdin)");
|
||||
c = fgetc(f);
|
||||
ungetc(c, f);
|
||||
if (c == ID_CHUNK) {
|
||||
f = freopen(filename, "rb", f); /* set binary mode */
|
||||
status = lua_doFILE(f, 1);
|
||||
}
|
||||
else {
|
||||
if (c == '#')
|
||||
while ((c=fgetc(f)) != '\n') /* skip first line */;
|
||||
status = lua_doFILE(f, 0);
|
||||
}
|
||||
if (f != stdin)
|
||||
fclose(f);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#define SIZE_PREF 20 /* size of string prefix to appear in error messages */
|
||||
|
||||
|
||||
int lua_dobuffer (char *buff, int size)
|
||||
{
|
||||
int status;
|
||||
ZIO z;
|
||||
luaI_setparsedfile("(buffer)");
|
||||
luaZ_mopen(&z, buff, size);
|
||||
status = luaI_undump(&z);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
int lua_dostring (char *str)
|
||||
{
|
||||
int status;
|
||||
char buff[SIZE_PREF+25];
|
||||
char *temp;
|
||||
ZIO z;
|
||||
if (str == NULL) return 1;
|
||||
sprintf(buff, "(dostring) >> %.20s", str);
|
||||
temp = strchr(buff, '\n');
|
||||
if (temp) *temp = 0; /* end string after first line */
|
||||
luaI_setparsedfile(buff);
|
||||
luaZ_sopen(&z, str);
|
||||
lua_setinput(&z);
|
||||
status = lua_domain();
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
static int passresults (void)
|
||||
{
|
||||
int arg = 0;
|
||||
lua_Object obj;
|
||||
while ((obj = lua_getresult(++arg)) != LUA_NOOBJECT)
|
||||
lua_pushobject(obj);
|
||||
return arg-1;
|
||||
}
|
||||
|
||||
|
||||
static void packresults (void)
|
||||
{
|
||||
int arg = 0;
|
||||
lua_Object obj;
|
||||
lua_Object table = lua_createtable();
|
||||
while ((obj = lua_getresult(++arg)) != LUA_NOOBJECT) {
|
||||
lua_pushobject(table);
|
||||
lua_pushnumber(arg);
|
||||
lua_pushobject(obj);
|
||||
lua_rawsettable();
|
||||
}
|
||||
lua_pushobject(table);
|
||||
lua_pushstring("n");
|
||||
lua_pushnumber(arg-1);
|
||||
lua_rawsettable();
|
||||
lua_pushobject(table); /* final result */
|
||||
}
|
||||
|
||||
/*
|
||||
** Internal function: do a string
|
||||
*/
|
||||
static void lua_internaldostring (void)
|
||||
{
|
||||
lua_Object err = lua_getparam(2);
|
||||
if (err != LUA_NOOBJECT) { /* set new error method */
|
||||
luaL_arg_check(lua_isnil(err) || lua_isfunction(err), 2,
|
||||
"must be a valid error handler");
|
||||
lua_pushobject(err);
|
||||
err = lua_seterrormethod();
|
||||
}
|
||||
if (lua_dostring(luaL_check_string(1)) == 0)
|
||||
if (passresults() == 0)
|
||||
lua_pushuserdata(NULL); /* at least one result to signal no errors */
|
||||
if (err != LUA_NOOBJECT) { /* restore old error method */
|
||||
lua_pushobject(err);
|
||||
lua_seterrormethod();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Internal function: do a file
|
||||
*/
|
||||
static void lua_internaldofile (void)
|
||||
{
|
||||
char *fname = luaL_opt_string(1, NULL);
|
||||
if (lua_dofile(fname) == 0)
|
||||
if (passresults() == 0)
|
||||
lua_pushuserdata(NULL); /* at least one result to signal no errors */
|
||||
}
|
||||
|
||||
|
||||
static char *tostring (lua_Object obj)
|
||||
{
|
||||
TObject *o = luaI_Address(obj);
|
||||
switch (ttype(o)) {
|
||||
case LUA_T_NUMBER: case LUA_T_STRING:
|
||||
return lua_getstring(obj);
|
||||
case LUA_T_ARRAY: case LUA_T_FUNCTION:
|
||||
case LUA_T_CFUNCTION: case LUA_T_NIL:
|
||||
return luaI_typenames[-ttype(o)];
|
||||
case LUA_T_USERDATA: {
|
||||
char *buff = luaI_buffer(30);
|
||||
sprintf(buff, "userdata: %p", o->value.ts->u.v);
|
||||
return buff;
|
||||
}
|
||||
default: return "<unknown object>";
|
||||
}
|
||||
}
|
||||
|
||||
static void luaI_tostring (void)
|
||||
{
|
||||
lua_pushstring(tostring(lua_getparam(1)));
|
||||
}
|
||||
|
||||
static void luaI_print (void)
|
||||
{
|
||||
int i = 1;
|
||||
lua_Object obj;
|
||||
while ((obj = lua_getparam(i++)) != LUA_NOOBJECT)
|
||||
printf("%s\n", tostring(obj));
|
||||
}
|
||||
|
||||
static void luaI_type (void)
|
||||
{
|
||||
lua_Object o = lua_getparam(1);
|
||||
luaL_arg_check(o != LUA_NOOBJECT, 1, "no argument");
|
||||
lua_pushstring(luaI_typenames[-ttype(luaI_Address(o))]);
|
||||
lua_pushnumber(lua_tag(o));
|
||||
}
|
||||
|
||||
/*
|
||||
** Internal function: convert an object to a number
|
||||
*/
|
||||
static void lua_obj2number (void)
|
||||
{
|
||||
lua_Object o = lua_getparam(1);
|
||||
if (lua_isnumber(o))
|
||||
lua_pushnumber(lua_getnumber(o));
|
||||
}
|
||||
|
||||
|
||||
static void luaI_error (void)
|
||||
{
|
||||
char *s = lua_getstring(lua_getparam(1));
|
||||
if (s == NULL) s = "(no message)";
|
||||
lua_error(s);
|
||||
}
|
||||
|
||||
static void luaI_assert (void)
|
||||
{
|
||||
lua_Object p = lua_getparam(1);
|
||||
if (p == LUA_NOOBJECT || lua_isnil(p))
|
||||
lua_error("assertion failed!");
|
||||
}
|
||||
|
||||
static void luaI_setglobal (void)
|
||||
{
|
||||
lua_Object value = lua_getparam(2);
|
||||
luaL_arg_check(value != LUA_NOOBJECT, 2, NULL);
|
||||
lua_pushobject(value);
|
||||
lua_setglobal(luaL_check_string(1));
|
||||
lua_pushobject(value); /* return given value */
|
||||
}
|
||||
|
||||
static void luaI_rawsetglobal (void)
|
||||
{
|
||||
lua_Object value = lua_getparam(2);
|
||||
luaL_arg_check(value != LUA_NOOBJECT, 2, NULL);
|
||||
lua_pushobject(value);
|
||||
lua_rawsetglobal(luaL_check_string(1));
|
||||
lua_pushobject(value); /* return given value */
|
||||
}
|
||||
|
||||
static void luaI_getglobal (void)
|
||||
{
|
||||
lua_pushobject(lua_getglobal(luaL_check_string(1)));
|
||||
}
|
||||
|
||||
static void luaI_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_gettable();
|
||||
return (lua_isnumber(temp) ? lua_getnumber(temp) : MAX_WORD);
|
||||
}
|
||||
|
||||
static void luaI_call (void)
|
||||
{
|
||||
lua_Object f = lua_getparam(1);
|
||||
lua_Object arg = lua_getparam(2);
|
||||
int withtable = (strcmp(luaL_opt_string(3, ""), "pack") == 0);
|
||||
int narg, i;
|
||||
luaL_arg_check(lua_isfunction(f), 1, "function expected");
|
||||
luaL_arg_check(lua_istable(arg), 2, "table expected");
|
||||
narg = getnarg(arg);
|
||||
/* 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_gettable();
|
||||
if (narg == MAX_WORD && lua_isnil(temp))
|
||||
break;
|
||||
lua_pushobject(temp);
|
||||
}
|
||||
if (lua_callfunction(f))
|
||||
lua_error(NULL);
|
||||
else if (withtable)
|
||||
packresults();
|
||||
else
|
||||
passresults();
|
||||
}
|
||||
|
||||
static void luaIl_settag (void)
|
||||
{
|
||||
lua_Object o = lua_getparam(1);
|
||||
luaL_arg_check(lua_istable(o), 1, "table expected");
|
||||
lua_pushobject(o);
|
||||
lua_settag(luaL_check_number(2));
|
||||
}
|
||||
|
||||
static void luaIl_newtag (void)
|
||||
{
|
||||
lua_pushnumber(lua_newtag());
|
||||
}
|
||||
|
||||
static void rawgettable (void)
|
||||
{
|
||||
lua_Object t = lua_getparam(1);
|
||||
lua_Object i = lua_getparam(2);
|
||||
luaL_arg_check(t != LUA_NOOBJECT, 1, NULL);
|
||||
luaL_arg_check(i != LUA_NOOBJECT, 2, NULL);
|
||||
lua_pushobject(t);
|
||||
lua_pushobject(i);
|
||||
lua_pushobject(lua_rawgettable());
|
||||
}
|
||||
|
||||
static void rawsettable (void)
|
||||
{
|
||||
lua_Object t = lua_getparam(1);
|
||||
lua_Object i = lua_getparam(2);
|
||||
lua_Object v = lua_getparam(3);
|
||||
luaL_arg_check(t != LUA_NOOBJECT && i != LUA_NOOBJECT && v != LUA_NOOBJECT,
|
||||
0, NULL);
|
||||
lua_pushobject(t);
|
||||
lua_pushobject(i);
|
||||
lua_pushobject(v);
|
||||
lua_rawsettable();
|
||||
}
|
||||
|
||||
|
||||
static void luaI_collectgarbage (void)
|
||||
{
|
||||
lua_pushnumber(lua_collectgarbage(luaL_opt_number(1, 0)));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Internal functions
|
||||
*/
|
||||
static struct {
|
||||
char *name;
|
||||
lua_CFunction func;
|
||||
} int_funcs[] = {
|
||||
{"assert", luaI_assert},
|
||||
{"call", luaI_call},
|
||||
{"collectgarbage", luaI_collectgarbage},
|
||||
{"dofile", lua_internaldofile},
|
||||
{"dostring", lua_internaldostring},
|
||||
{"error", luaI_error},
|
||||
{"getglobal", luaI_getglobal},
|
||||
{"newtag", luaIl_newtag},
|
||||
{"next", lua_next},
|
||||
{"nextvar", luaI_nextvar},
|
||||
{"print", luaI_print},
|
||||
{"rawgetglobal", luaI_rawgetglobal},
|
||||
{"rawgettable", rawgettable},
|
||||
{"rawsetglobal", luaI_rawsetglobal},
|
||||
{"rawsettable", rawsettable},
|
||||
{"seterrormethod", luaI_seterrormethod},
|
||||
#if LUA_COMPAT2_5
|
||||
{"setfallback", luaI_setfallback},
|
||||
#endif
|
||||
{"setglobal", luaI_setglobal},
|
||||
{"settagmethod", luaI_settagmethod},
|
||||
{"gettagmethod", luaI_gettagmethod},
|
||||
{"settag", luaIl_settag},
|
||||
{"tonumber", lua_obj2number},
|
||||
{"tostring", luaI_tostring},
|
||||
{"tag", luatag},
|
||||
{"type", luaI_type}
|
||||
};
|
||||
|
||||
#define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0]))
|
||||
|
||||
|
||||
void luaI_predefine (void)
|
||||
{
|
||||
int i;
|
||||
Word n;
|
||||
for (i=0; i<INTFUNCSIZE; i++) {
|
||||
n = luaI_findsymbolbyname(int_funcs[i].name);
|
||||
s_ttype(n) = LUA_T_CFUNCTION; s_fvalue(n) = int_funcs[i].func;
|
||||
}
|
||||
n = luaI_findsymbolbyname("_VERSION");
|
||||
s_ttype(n) = LUA_T_STRING; s_tsvalue(n) = lua_createstring(LUA_VERSION);
|
||||
}
|
||||
|
||||
|
||||
25
inout.h
25
inout.h
@@ -1,25 +0,0 @@
|
||||
/*
|
||||
** $Id: inout.h,v 1.19 1997/06/18 20:35:49 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
|
||||
#ifndef inout_h
|
||||
#define inout_h
|
||||
|
||||
#include "types.h"
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
extern Word lua_linenumber;
|
||||
extern Word lua_debugline;
|
||||
extern char *lua_parsedfile;
|
||||
|
||||
void luaI_setparsedfile (char *name);
|
||||
|
||||
void luaI_predefine (void);
|
||||
|
||||
int lua_dobuffer (char *buff, int size);
|
||||
int lua_doFILE (FILE *f, int bin);
|
||||
|
||||
|
||||
#endif
|
||||
330
iolib.c
330
iolib.c
@@ -1,330 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "lua.h"
|
||||
#include "auxlib.h"
|
||||
#include "luadebug.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
int lua_tagio;
|
||||
|
||||
|
||||
#ifdef POPEN
|
||||
FILE *popen();
|
||||
int pclose();
|
||||
#else
|
||||
#define popen(x,y) NULL /* that is, popen always fails */
|
||||
#define pclose(x) (-1)
|
||||
#endif
|
||||
|
||||
|
||||
static void pushresult (int i)
|
||||
{
|
||||
if (i)
|
||||
lua_pushuserdata(NULL);
|
||||
else {
|
||||
lua_pushnil();
|
||||
#ifndef NOSTRERROR
|
||||
lua_pushstring(strerror(errno));
|
||||
#else
|
||||
lua_pushstring("O.S. unable to define the error");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static FILE *getfile (char *name)
|
||||
{
|
||||
lua_Object f = lua_getglobal(name);
|
||||
if (!lua_isuserdata(f) || lua_tag(f) != lua_tagio)
|
||||
luaL_verror("global variable %s is not a file handle", name);
|
||||
return lua_getuserdata(f);
|
||||
}
|
||||
|
||||
|
||||
static void closefile (char *name)
|
||||
{
|
||||
FILE *f = getfile(name);
|
||||
if (f == stdin || f == stdout) return;
|
||||
if (pclose(f) == -1)
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
|
||||
static void setfile (FILE *f, char *name)
|
||||
{
|
||||
lua_pushusertag(f, lua_tagio);
|
||||
lua_setglobal(name);
|
||||
}
|
||||
|
||||
|
||||
static void setreturn (FILE *f, char *name)
|
||||
{
|
||||
setfile(f, name);
|
||||
lua_pushusertag(f, lua_tagio);
|
||||
}
|
||||
|
||||
|
||||
static void io_readfrom (void)
|
||||
{
|
||||
FILE *current;
|
||||
lua_Object f = lua_getparam(1);
|
||||
if (f == LUA_NOOBJECT) {
|
||||
closefile("_INPUT");
|
||||
current = stdin;
|
||||
}
|
||||
else if (lua_tag(f) == lua_tagio)
|
||||
current = lua_getuserdata(f);
|
||||
else {
|
||||
char *s = luaL_check_string(1);
|
||||
current = (*s == '|') ? popen(s+1, "r") : fopen(s, "r");
|
||||
if (current == NULL) {
|
||||
pushresult(0);
|
||||
return;
|
||||
}
|
||||
}
|
||||
setreturn(current, "_INPUT");
|
||||
}
|
||||
|
||||
|
||||
static void io_writeto (void)
|
||||
{
|
||||
FILE *current;
|
||||
lua_Object f = lua_getparam(1);
|
||||
if (f == LUA_NOOBJECT) {
|
||||
closefile("_OUTPUT");
|
||||
current = stdout;
|
||||
}
|
||||
else if (lua_tag(f) == lua_tagio)
|
||||
current = lua_getuserdata(f);
|
||||
else {
|
||||
char *s = luaL_check_string(1);
|
||||
current = (*s == '|') ? popen(s+1,"w") : fopen(s,"w");
|
||||
if (current == NULL) {
|
||||
pushresult(0);
|
||||
return;
|
||||
}
|
||||
}
|
||||
setreturn(current, "_OUTPUT");
|
||||
}
|
||||
|
||||
|
||||
static void io_appendto (void)
|
||||
{
|
||||
char *s = luaL_check_string(1);
|
||||
FILE *fp = fopen (s, "a");
|
||||
if (fp != NULL)
|
||||
setreturn(fp, "_OUTPUT");
|
||||
else
|
||||
pushresult(0);
|
||||
}
|
||||
|
||||
|
||||
#define NEED_OTHER (EOF-1) /* just some flag different from EOF */
|
||||
|
||||
static void io_read (void)
|
||||
{
|
||||
FILE *f = getfile("_INPUT");
|
||||
char *buff;
|
||||
char *p = luaL_opt_string(1, "[^\n]*{\n}");
|
||||
int inskip = 0; /* to control {skips} */
|
||||
int c = NEED_OTHER;
|
||||
luaI_emptybuff();
|
||||
while (*p) {
|
||||
if (*p == '{') {
|
||||
inskip++;
|
||||
p++;
|
||||
}
|
||||
else if (*p == '}') {
|
||||
if (inskip == 0)
|
||||
lua_error("unbalanced braces in read pattern");
|
||||
inskip--;
|
||||
p++;
|
||||
}
|
||||
else {
|
||||
char *ep = luaL_item_end(p); /* get what is next */
|
||||
int m; /* match result */
|
||||
if (c == NEED_OTHER) c = getc(f);
|
||||
m = (c == EOF) ? 0 : luaL_singlematch((char)c, p);
|
||||
if (m) {
|
||||
if (inskip == 0) luaI_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);
|
||||
buff = luaI_addchar(0);
|
||||
if (*buff != 0 || *p == 0) /* read something or did not fail? */
|
||||
lua_pushstring(buff);
|
||||
}
|
||||
|
||||
|
||||
static void io_write (void)
|
||||
{
|
||||
FILE *f = getfile("_OUTPUT");
|
||||
int arg = 1;
|
||||
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 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[] = {
|
||||
{"readfrom", io_readfrom},
|
||||
{"writeto", io_writeto},
|
||||
{"appendto", io_appendto},
|
||||
{"read", io_read},
|
||||
{"write", io_write},
|
||||
{"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}
|
||||
};
|
||||
|
||||
void iolib_open (void)
|
||||
{
|
||||
lua_tagio = lua_newtag();
|
||||
setfile(stdin, "_INPUT");
|
||||
setfile(stdout, "_OUTPUT");
|
||||
setfile(stdin, "_STDIN");
|
||||
setfile(stdout, "_STDOUT");
|
||||
setfile(stderr, "_STDERR");
|
||||
luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0])));
|
||||
lua_pushcfunction(errorfb);
|
||||
lua_seterrormethod();
|
||||
}
|
||||
700
lapi.c
Normal file
700
lapi.c
Normal file
@@ -0,0 +1,700 @@
|
||||
/*
|
||||
** $Id: lapi.c,v 1.41 1999/03/04 21:17:26 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"
|
||||
"$Authors: " LUA_AUTHORS " $";
|
||||
|
||||
|
||||
|
||||
TObject *luaA_Address (lua_Object o) {
|
||||
return (o != LUA_NOOBJECT) ? Address(o) : NULL;
|
||||
}
|
||||
|
||||
|
||||
static lua_Type 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) {
|
||||
L->Cstack.base = L->Cstack.lua2C; /* position of first result */
|
||||
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) */
|
||||
}
|
||||
|
||||
|
||||
static void top2LC (int n) {
|
||||
/* Put the 'n' elements on the top as the Lua2C contents */
|
||||
L->Cstack.base = (L->stack.top-L->stack.stack); /* new base */
|
||||
L->Cstack.lua2C = L->Cstack.base-n; /* position of the new results */
|
||||
L->Cstack.num = n; /* number of results */
|
||||
}
|
||||
|
||||
|
||||
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) {
|
||||
lua_Object temp;
|
||||
checkCparams(1);
|
||||
temp = lua_getglobal("_ERRORMESSAGE");
|
||||
lua_setglobal("_ERRORMESSAGE");
|
||||
return 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 {
|
||||
*(L->stack.top-2) = *luaH_get(avalue(L->stack.top-2), L->stack.top-1);
|
||||
--L->stack.top;
|
||||
}
|
||||
return put_luaObjectonTop();
|
||||
}
|
||||
|
||||
|
||||
void lua_settable (void) {
|
||||
checkCparams(3);
|
||||
luaV_settable(L->stack.top-3);
|
||||
L->stack.top -= 2; /* pop table and index */
|
||||
}
|
||||
|
||||
|
||||
void lua_rawsettable (void) {
|
||||
checkCparams(3);
|
||||
luaV_rawsettable(L->stack.top-3);
|
||||
}
|
||||
|
||||
|
||||
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.s.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)
|
||||
{
|
||||
luaC_checkGC(); /* "tostring" may create a new string */
|
||||
if (object == LUA_NOOBJECT || tostring(Address(object)))
|
||||
return NULL;
|
||||
else return (svalue(Address(object)));
|
||||
}
|
||||
|
||||
long lua_strlen (lua_Object object)
|
||||
{
|
||||
luaC_checkGC(); /* "tostring" may create a new string */
|
||||
if (object == LUA_NOOBJECT || tostring(Address(object)))
|
||||
return 0L;
|
||||
else return (tsvalue(Address(object))->u.s.len);
|
||||
}
|
||||
|
||||
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_pushlstring (char *s, long len)
|
||||
{
|
||||
tsvalue(L->stack.top) = luaS_newlstr(s, len);
|
||||
ttype(L->stack.top) = LUA_T_STRING;
|
||||
incr_top;
|
||||
luaC_checkGC();
|
||||
}
|
||||
|
||||
void lua_pushstring (char *s)
|
||||
{
|
||||
if (s == NULL)
|
||||
lua_pushnil();
|
||||
else
|
||||
lua_pushlstring(s, strlen(s));
|
||||
}
|
||||
|
||||
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);
|
||||
luaC_checkGC();
|
||||
}
|
||||
|
||||
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_INTERNALERROR("invalid type");
|
||||
#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_typename(L->stack.top-1));
|
||||
}
|
||||
L->stack.top--;
|
||||
}
|
||||
|
||||
|
||||
TaggedString *luaA_nextvar (TaggedString *g) {
|
||||
if (g == NULL)
|
||||
g = (TaggedString *)L->rootglobal.next; /* first variable */
|
||||
else {
|
||||
/* 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; /* get next */
|
||||
}
|
||||
while (g && g->u.s.globalval.ttype == LUA_T_NIL) /* skip globals with nil */
|
||||
g = (TaggedString *)g->head.next;
|
||||
if (g) {
|
||||
ttype(L->stack.top) = LUA_T_STRING; tsvalue(L->stack.top) = g;
|
||||
incr_top;
|
||||
luaA_pushobject(&g->u.s.globalval);
|
||||
}
|
||||
return g;
|
||||
}
|
||||
|
||||
|
||||
char *lua_nextvar (char *varname) {
|
||||
TaggedString *g = (varname == NULL) ? NULL : luaS_new(varname);
|
||||
g = luaA_nextvar(g);
|
||||
if (g) {
|
||||
top2LC(2);
|
||||
return g->str;
|
||||
}
|
||||
else {
|
||||
top2LC(0);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int luaA_next (Hash *t, int i) {
|
||||
int tsize = nhash(t);
|
||||
for (; i<tsize; i++) {
|
||||
Node *n = node(t, i);
|
||||
if (ttype(val(n)) != LUA_T_NIL) {
|
||||
luaA_pushobject(ref(n));
|
||||
luaA_pushobject(val(n));
|
||||
return i+1; /* index to be used next time */
|
||||
}
|
||||
}
|
||||
return 0; /* no more elements */
|
||||
}
|
||||
|
||||
|
||||
int lua_next (lua_Object o, int i) {
|
||||
TObject *t = Address(o);
|
||||
if (ttype(t) != LUA_T_ARRAY)
|
||||
lua_error("API error - object is not a table in `lua_next'");
|
||||
i = luaA_next(avalue(t), i);
|
||||
top2LC((i==0) ? 0 : 2);
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** To manipulate some state information
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
lua_State *lua_setstate (lua_State *st) {
|
||||
lua_State *old = lua_state;
|
||||
lua_state = st;
|
||||
return old;
|
||||
}
|
||||
|
||||
lua_LHFunction lua_setlinehook (lua_LHFunction func) {
|
||||
lua_LHFunction old = L->linehook;
|
||||
L->linehook = func;
|
||||
return old;
|
||||
}
|
||||
|
||||
lua_CHFunction lua_setcallhook (lua_CHFunction func) {
|
||||
lua_CHFunction old = L->callhook;
|
||||
L->callhook = func;
|
||||
return old;
|
||||
}
|
||||
|
||||
int lua_setdebug (int debug) {
|
||||
int old = L->debug;
|
||||
L->debug = debug;
|
||||
return old;
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Debug interface
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
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_nups (lua_Function func) {
|
||||
TObject *o = luaA_Address(func);
|
||||
return (!o || normalized_type(o) != LUA_T_CLOSURE) ? 0 : o->value.cl->nelems;
|
||||
}
|
||||
|
||||
|
||||
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 put_luaObject((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 **source, 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) {
|
||||
*source = tfvalue(f)->source->str;
|
||||
*linedefined = tfvalue(f)->lineDefined;
|
||||
}
|
||||
else {
|
||||
*source = "(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 = luaS_travsymbol(checkfunc)) != NULL)
|
||||
return "global";
|
||||
else if ((*name = luaT_travtagmethods(checkfunc)) != NULL)
|
||||
return "tag-method";
|
||||
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
|
||||
|
||||
22
lapi.h
Normal file
22
lapi.h
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
** $Id: lapi.h,v 1.3 1999/02/22 19:13:12 roberto Exp roberto $
|
||||
** Auxiliary 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);
|
||||
TaggedString *luaA_nextvar (TaggedString *g);
|
||||
int luaA_next (Hash *t, int i);
|
||||
|
||||
#endif
|
||||
133
lauxlib.c
Normal file
133
lauxlib.c
Normal file
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
** $Id: lauxlib.c,v 1.16 1999/03/10 14:19:41 roberto Exp roberto $
|
||||
** Auxiliary functions for building Lua libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
/* Please Notice: This file uses only the official 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"
|
||||
|
||||
|
||||
|
||||
int luaL_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 luaL_argerror (int numarg, char *extramsg) {
|
||||
lua_Function f = lua_stackedfunction(0);
|
||||
char *funcname;
|
||||
lua_getobjname(f, &funcname);
|
||||
numarg -= lua_nups(f);
|
||||
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_lstr (int numArg, long *len)
|
||||
{
|
||||
lua_Object o = lua_getparam(numArg);
|
||||
luaL_arg_check(lua_isstring(o), numArg, "string expected");
|
||||
if (len) *len = lua_strlen(o);
|
||||
return lua_getstring(o);
|
||||
}
|
||||
|
||||
char *luaL_opt_lstr (int numArg, char *def, long *len)
|
||||
{
|
||||
return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
|
||||
luaL_check_lstr(numArg, len);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
void luaL_chunkid (char *out, char *source, int len) {
|
||||
len -= 13; /* 13 = strlen("string ''...\0") */
|
||||
if (*source == '@')
|
||||
sprintf(out, "file `%.*s'", len, source+1);
|
||||
else if (*source == '(')
|
||||
strcpy(out, "(C code)");
|
||||
else {
|
||||
char *b = strchr(source , '\n'); /* stop string at first new line */
|
||||
int lim = (b && (b-source)<len) ? b-source : len;
|
||||
sprintf(out, "string `%.*s'", lim, source);
|
||||
strcpy(out+lim+(13-5), "...'"); /* 5 = strlen("...'\0") */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaL_filesource (char *out, char *filename, int len) {
|
||||
if (filename == NULL) filename = "(stdin)";
|
||||
sprintf(out, "@%.*s", len-2, filename); /* -2 for '@' and '\0' */
|
||||
}
|
||||
53
lauxlib.h
Normal file
53
lauxlib.h
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
** $Id: lauxlib.h,v 1.11 1999/03/04 21:17:26 roberto Exp roberto $
|
||||
** Auxiliary 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);
|
||||
#define luaL_check_string(n) (luaL_check_lstr((n), NULL))
|
||||
char *luaL_check_lstr (int numArg, long *len);
|
||||
#define luaL_opt_string(n, d) (luaL_opt_lstr((n), (d), NULL))
|
||||
char *luaL_opt_lstr (int numArg, char *def, long *len);
|
||||
double luaL_check_number (int numArg);
|
||||
#define luaL_check_int(n) ((int)luaL_check_number(n))
|
||||
#define luaL_check_long(n) ((long)luaL_check_number(n))
|
||||
double luaL_opt_number (int numArg, double def);
|
||||
#define luaL_opt_int(n,d) ((int)luaL_opt_number(n,d))
|
||||
#define luaL_opt_long(n,d) ((long)luaL_opt_number(n,d))
|
||||
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);
|
||||
int luaL_getsize (void);
|
||||
void luaL_addsize (int n);
|
||||
int luaL_newbuffer (int size);
|
||||
void luaL_oldbuffer (int old);
|
||||
char *luaL_buffer (void);
|
||||
int luaL_findstring (char *name, char *list[]);
|
||||
void luaL_chunkid (char *out, char *source, int len);
|
||||
void luaL_filesource (char *out, char *filename, int len);
|
||||
|
||||
|
||||
#endif
|
||||
75
lbuffer.c
Normal file
75
lbuffer.c
Normal file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
** $Id: lbuffer.c,v 1.8 1999/02/25 19:20:40 roberto Exp roberto $
|
||||
** Auxiliary functions for building Lua libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lmem.h"
|
||||
#include "lstate.h"
|
||||
|
||||
|
||||
/*-------------------------------------------------------
|
||||
** Auxiliary buffer
|
||||
-------------------------------------------------------*/
|
||||
|
||||
|
||||
#define EXTRABUFF 32
|
||||
|
||||
|
||||
#define openspace(size) if (L->Mbuffnext+(size) > L->Mbuffsize) Openspace(size)
|
||||
|
||||
static void Openspace (int size) {
|
||||
lua_State *l = L; /* to optimize */
|
||||
size += EXTRABUFF;
|
||||
l->Mbuffsize = l->Mbuffnext+size;
|
||||
luaM_growvector(l->Mbuffer, l->Mbuffnext, size, char, arrEM, MAX_INT);
|
||||
}
|
||||
|
||||
|
||||
char *luaL_openspace (int size) {
|
||||
openspace(size);
|
||||
return L->Mbuffer+L->Mbuffnext;
|
||||
}
|
||||
|
||||
|
||||
void luaL_addchar (int c) {
|
||||
openspace(1);
|
||||
L->Mbuffer[L->Mbuffnext++] = (char)c;
|
||||
}
|
||||
|
||||
|
||||
void luaL_resetbuffer (void) {
|
||||
L->Mbuffnext = L->Mbuffbase;
|
||||
}
|
||||
|
||||
|
||||
void luaL_addsize (int n) {
|
||||
L->Mbuffnext += n;
|
||||
}
|
||||
|
||||
int luaL_getsize (void) {
|
||||
return L->Mbuffnext-L->Mbuffbase;
|
||||
}
|
||||
|
||||
int luaL_newbuffer (int size) {
|
||||
int old = L->Mbuffbase;
|
||||
openspace(size);
|
||||
L->Mbuffbase = L->Mbuffnext;
|
||||
return old;
|
||||
}
|
||||
|
||||
|
||||
void luaL_oldbuffer (int old) {
|
||||
L->Mbuffnext = L->Mbuffbase;
|
||||
L->Mbuffbase = old;
|
||||
}
|
||||
|
||||
|
||||
char *luaL_buffer (void) {
|
||||
return L->Mbuffer+L->Mbuffbase;
|
||||
}
|
||||
|
||||
726
lbuiltin.c
Normal file
726
lbuiltin.c
Normal file
@@ -0,0 +1,726 @@
|
||||
/*
|
||||
** $Id: lbuiltin.c,v 1.55 1999/03/01 20:22:16 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"
|
||||
#include "lundump.h"
|
||||
#include "lvm.h"
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Auxiliary functions
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
static void pushtagstring (TaggedString *s) {
|
||||
TObject o;
|
||||
o.ttype = LUA_T_STRING;
|
||||
o.value.ts = s;
|
||||
luaA_pushobject(&o);
|
||||
}
|
||||
|
||||
|
||||
static real getsize (Hash *h) {
|
||||
real max = 0;
|
||||
int i;
|
||||
for (i = 0; i<nhash(h); i++) {
|
||||
Node *n = h->node+i;
|
||||
if (ttype(ref(n)) == LUA_T_NUMBER &&
|
||||
ttype(val(n)) != LUA_T_NIL &&
|
||||
nvalue(ref(n)) > max)
|
||||
max = nvalue(ref(n));
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
|
||||
static real getnarg (Hash *a) {
|
||||
TObject index;
|
||||
TObject *value;
|
||||
/* value = table.n */
|
||||
ttype(&index) = LUA_T_STRING;
|
||||
tsvalue(&index) = luaS_new("n");
|
||||
value = luaH_get(a, &index);
|
||||
return (ttype(value) == LUA_T_NUMBER) ? nvalue(value) : getsize(a);
|
||||
}
|
||||
|
||||
|
||||
static Hash *gethash (int arg) {
|
||||
return avalue(luaA_Address(luaL_tablearg(arg)));
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Functions that use only the official API
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
** If your system does not support "stderr", redefine this function, or
|
||||
** redefine _ERRORMESSAGE so that it won't need _ALERT.
|
||||
*/
|
||||
static void luaB_alert (void) {
|
||||
fputs(luaL_check_string(1), stderr);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Standard implementation of _ERRORMESSAGE.
|
||||
** The library "iolib" redefines _ERRORMESSAGE for better error information.
|
||||
*/
|
||||
static void error_message (void) {
|
||||
lua_Object al = lua_rawgetglobal("_ALERT");
|
||||
if (lua_isfunction(al)) { /* avoid error loop if _ALERT is not defined */
|
||||
char buff[600];
|
||||
sprintf(buff, "lua error: %.500s\n", luaL_check_string(1));
|
||||
lua_pushstring(buff);
|
||||
lua_callfunction(al);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** If your system does not support "stdout", just remove this function.
|
||||
** If you need, you can define your own "print" function, following this
|
||||
** model but changing "fputs" to put the strings at a proper place
|
||||
** (a console window or a log file, for instance).
|
||||
*/
|
||||
#define MAXPRINT 40
|
||||
static void luaB_print (void) {
|
||||
lua_Object args[MAXPRINT];
|
||||
lua_Object obj;
|
||||
int n = 0;
|
||||
int i;
|
||||
while ((obj = lua_getparam(n+1)) != LUA_NOOBJECT) {
|
||||
luaL_arg_check(n < MAXPRINT, n+1, "too many arguments");
|
||||
args[n++] = obj;
|
||||
}
|
||||
for (i=0; i<n; i++) {
|
||||
lua_pushobject(args[i]);
|
||||
if (lua_call("tostring"))
|
||||
lua_error("error in `tostring' called by `print'");
|
||||
obj = lua_getresult(1);
|
||||
if (!lua_isstring(obj))
|
||||
lua_error("`tostring' must return a string to `print'");
|
||||
if (i>0) fputs("\t", stdout);
|
||||
fputs(lua_getstring(obj), stdout);
|
||||
}
|
||||
fputs("\n", stdout);
|
||||
}
|
||||
|
||||
|
||||
static void luaB_tonumber (void) {
|
||||
int base = luaL_opt_int(2, 10);
|
||||
if (base == 10) { /* standard conversion */
|
||||
lua_Object o = lua_getparam(1);
|
||||
if (lua_isnumber(o)) lua_pushnumber(lua_getnumber(o));
|
||||
else lua_pushnil(); /* not a number */
|
||||
}
|
||||
else {
|
||||
char *s = luaL_check_string(1);
|
||||
long n;
|
||||
luaL_arg_check(0 <= base && base <= 36, 2, "base out of range");
|
||||
n = strtol(s, &s, base);
|
||||
while (isspace((unsigned char)*s)) s++; /* skip trailing spaces */
|
||||
if (*s) lua_pushnil(); /* invalid format: return nil */
|
||||
else lua_pushnumber(n);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void luaB_error (void) {
|
||||
lua_error(lua_getstring(lua_getparam(1)));
|
||||
}
|
||||
|
||||
static void luaB_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 luaB_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 luaB_getglobal (void) {
|
||||
lua_pushobject(lua_getglobal(luaL_check_string(1)));
|
||||
}
|
||||
|
||||
static void luaB_rawgetglobal (void) {
|
||||
lua_pushobject(lua_rawgetglobal(luaL_check_string(1)));
|
||||
}
|
||||
|
||||
static void luaB_luatag (void) {
|
||||
lua_pushnumber(lua_tag(lua_getparam(1)));
|
||||
}
|
||||
|
||||
static void luaB_settag (void) {
|
||||
lua_Object o = luaL_tablearg(1);
|
||||
lua_pushobject(o);
|
||||
lua_settag(luaL_check_int(2));
|
||||
lua_pushobject(o); /* return first argument */
|
||||
}
|
||||
|
||||
static void luaB_newtag (void) {
|
||||
lua_pushnumber(lua_newtag());
|
||||
}
|
||||
|
||||
static void luaB_copytagmethods (void) {
|
||||
lua_pushnumber(lua_copytagmethods(luaL_check_int(1),
|
||||
luaL_check_int(2)));
|
||||
}
|
||||
|
||||
static void luaB_rawgettable (void) {
|
||||
lua_pushobject(luaL_nonnullarg(1));
|
||||
lua_pushobject(luaL_nonnullarg(2));
|
||||
lua_pushobject(lua_rawgettable());
|
||||
}
|
||||
|
||||
static void luaB_rawsettable (void) {
|
||||
lua_pushobject(luaL_nonnullarg(1));
|
||||
lua_pushobject(luaL_nonnullarg(2));
|
||||
lua_pushobject(luaL_nonnullarg(3));
|
||||
lua_rawsettable();
|
||||
}
|
||||
|
||||
static void luaB_settagmethod (void) {
|
||||
lua_Object nf = luaL_nonnullarg(3);
|
||||
lua_pushobject(nf);
|
||||
lua_pushobject(lua_settagmethod(luaL_check_int(1), luaL_check_string(2)));
|
||||
}
|
||||
|
||||
static void luaB_gettagmethod (void) {
|
||||
lua_pushobject(lua_gettagmethod(luaL_check_int(1), luaL_check_string(2)));
|
||||
}
|
||||
|
||||
static void luaB_seterrormethod (void) {
|
||||
lua_Object nf = luaL_functionarg(1);
|
||||
lua_pushobject(nf);
|
||||
lua_pushobject(lua_seterrormethod());
|
||||
}
|
||||
|
||||
static void luaB_collectgarbage (void) {
|
||||
lua_pushnumber(lua_collectgarbage(luaL_opt_int(1, 0)));
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Functions that could use only the official API but
|
||||
** do not, for efficiency.
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
static void luaB_dostring (void) {
|
||||
long l;
|
||||
char *s = luaL_check_lstr(1, &l);
|
||||
if (*s == ID_CHUNK)
|
||||
lua_error("`dostring' cannot run pre-compiled code");
|
||||
if (lua_dobuffer(s, l, luaL_opt_string(2, s)) == 0)
|
||||
if (luaA_passresults() == 0)
|
||||
lua_pushuserdata(NULL); /* at least one result to signal no errors */
|
||||
}
|
||||
|
||||
|
||||
static void luaB_dofile (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 void luaB_call (void) {
|
||||
lua_Object f = luaL_nonnullarg(1);
|
||||
Hash *arg = gethash(2);
|
||||
char *options = luaL_opt_string(3, "");
|
||||
lua_Object err = lua_getparam(4);
|
||||
int narg = (int)getnarg(arg);
|
||||
int i, status;
|
||||
if (err != LUA_NOOBJECT) { /* set new error method */
|
||||
lua_pushobject(err);
|
||||
err = lua_seterrormethod();
|
||||
}
|
||||
/* push arg[1...n] */
|
||||
luaD_checkstack(narg);
|
||||
for (i=0; i<narg; i++)
|
||||
*(L->stack.top++) = *luaH_getint(arg, i+1);
|
||||
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')) {
|
||||
lua_pushnil();
|
||||
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 luaB_nextvar (void) {
|
||||
TObject *o = luaA_Address(luaL_nonnullarg(1));
|
||||
TaggedString *g;
|
||||
if (ttype(o) == LUA_T_NIL)
|
||||
g = NULL;
|
||||
else {
|
||||
luaL_arg_check(ttype(o) == LUA_T_STRING, 1, "variable name expected");
|
||||
g = tsvalue(o);
|
||||
}
|
||||
if (!luaA_nextvar(g))
|
||||
lua_pushnil();
|
||||
}
|
||||
|
||||
|
||||
static void luaB_next (void) {
|
||||
Hash *a = gethash(1);
|
||||
TObject *k = luaA_Address(luaL_nonnullarg(2));
|
||||
int i = (ttype(k) == LUA_T_NIL) ? 0 : luaH_pos(a, k)+1;
|
||||
if (luaA_next(a, i) == 0)
|
||||
lua_pushnil();
|
||||
}
|
||||
|
||||
|
||||
static void luaB_tostring (void) {
|
||||
lua_Object obj = lua_getparam(1);
|
||||
TObject *o = luaA_Address(obj);
|
||||
char buff[64];
|
||||
switch (ttype(o)) {
|
||||
case LUA_T_NUMBER:
|
||||
lua_pushstring(lua_getstring(obj));
|
||||
return;
|
||||
case LUA_T_STRING:
|
||||
lua_pushobject(obj);
|
||||
return;
|
||||
case LUA_T_ARRAY:
|
||||
sprintf(buff, "table: %p", (void *)o->value.a);
|
||||
break;
|
||||
case LUA_T_CLOSURE:
|
||||
sprintf(buff, "function: %p", (void *)o->value.cl);
|
||||
break;
|
||||
case LUA_T_PROTO:
|
||||
sprintf(buff, "function: %p", (void *)o->value.tf);
|
||||
break;
|
||||
case LUA_T_CPROTO:
|
||||
sprintf(buff, "function: %p", (void *)o->value.f);
|
||||
break;
|
||||
case LUA_T_USERDATA:
|
||||
sprintf(buff, "userdata: %p", o->value.ts->u.d.v);
|
||||
break;
|
||||
case LUA_T_NIL:
|
||||
lua_pushstring("nil");
|
||||
return;
|
||||
default:
|
||||
LUA_INTERNALERROR("invalid type");
|
||||
}
|
||||
lua_pushstring(buff);
|
||||
}
|
||||
|
||||
|
||||
static void luaB_type (void) {
|
||||
lua_Object o = luaL_nonnullarg(1);
|
||||
lua_pushstring(luaO_typename(luaA_Address(o)));
|
||||
lua_pushnumber(lua_tag(o));
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** "Extra" functions
|
||||
** These functions can be written in Lua, so you can
|
||||
** delete them if you need a tiny Lua implementation.
|
||||
** If you delete them, remove their entries in array
|
||||
** "builtin_funcs".
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
static void luaB_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 luaB_foreachi (void) {
|
||||
Hash *t = gethash(1);
|
||||
TObject *f = luaA_Address(luaL_functionarg(2));
|
||||
int i;
|
||||
int n = (int)getnarg(t);
|
||||
luaD_checkstack(3); /* for f, ref, and val */
|
||||
for (i=1; i<=n; i++) {
|
||||
*(L->stack.top++) = *f;
|
||||
ttype(L->stack.top) = LUA_T_NUMBER; nvalue(L->stack.top++) = i;
|
||||
*(L->stack.top++) = *luaH_getint(t, i);
|
||||
luaD_calln(2, 1);
|
||||
if (ttype(L->stack.top-1) != LUA_T_NIL)
|
||||
return;
|
||||
L->stack.top--;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void luaB_foreach (void) {
|
||||
Hash *a = gethash(1);
|
||||
TObject *f = luaA_Address(luaL_functionarg(2));
|
||||
int i;
|
||||
luaD_checkstack(3); /* for f, ref, and val */
|
||||
for (i=0; i<a->nhash; i++) {
|
||||
Node *nd = &(a->node[i]);
|
||||
if (ttype(val(nd)) != LUA_T_NIL) {
|
||||
*(L->stack.top++) = *f;
|
||||
*(L->stack.top++) = *ref(nd);
|
||||
*(L->stack.top++) = *val(nd);
|
||||
luaD_calln(2, 1);
|
||||
if (ttype(L->stack.top-1) != LUA_T_NIL)
|
||||
return;
|
||||
L->stack.top--; /* remove result */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void luaB_foreachvar (void) {
|
||||
TObject *f = luaA_Address(luaL_functionarg(1));
|
||||
GCnode *g;
|
||||
luaD_checkstack(4); /* for extra var name, f, var name, and globalval */
|
||||
for (g = L->rootglobal.next; g; g = g->next) {
|
||||
TaggedString *s = (TaggedString *)g;
|
||||
if (s->u.s.globalval.ttype != LUA_T_NIL) {
|
||||
pushtagstring(s); /* keep (extra) s on stack to avoid GC */
|
||||
*(L->stack.top++) = *f;
|
||||
pushtagstring(s);
|
||||
*(L->stack.top++) = s->u.s.globalval;
|
||||
luaD_calln(2, 1);
|
||||
if (ttype(L->stack.top-1) != LUA_T_NIL) {
|
||||
L->stack.top--;
|
||||
*(L->stack.top-1) = *L->stack.top; /* remove extra s */
|
||||
return;
|
||||
}
|
||||
L->stack.top-=2; /* remove result and extra s */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void luaB_getn (void) {
|
||||
lua_pushnumber(getnarg(gethash(1)));
|
||||
}
|
||||
|
||||
|
||||
static void luaB_tinsert (void) {
|
||||
Hash *a = gethash(1);
|
||||
lua_Object v = lua_getparam(3);
|
||||
int n = (int)getnarg(a);
|
||||
int pos;
|
||||
if (v != LUA_NOOBJECT)
|
||||
pos = luaL_check_int(2);
|
||||
else { /* called with only 2 arguments */
|
||||
v = luaL_nonnullarg(2);
|
||||
pos = n+1;
|
||||
}
|
||||
luaV_setn(a, n+1); /* increment field "n" */
|
||||
for ( ;n>=pos; n--)
|
||||
luaH_move(a, n, n+1);
|
||||
luaH_setint(a, pos, luaA_Address(v));
|
||||
}
|
||||
|
||||
|
||||
static void luaB_tremove (void) {
|
||||
Hash *a = gethash(1);
|
||||
int n = (int)getnarg(a);
|
||||
int pos = luaL_opt_int(2, n);
|
||||
if (n <= 0) return; /* table is "empty" */
|
||||
luaA_pushobject(luaH_getint(a, pos)); /* push result */
|
||||
luaV_setn(a, n-1); /* decrement field "n" */
|
||||
for ( ;pos<n; pos++)
|
||||
luaH_move(a, pos+1, pos);
|
||||
}
|
||||
|
||||
|
||||
/* {
|
||||
** Quicksort
|
||||
*/
|
||||
|
||||
static void swap (Hash *a, int i, int j) {
|
||||
TObject temp;
|
||||
temp = *luaH_getint(a, i);
|
||||
luaH_move(a, j, i);
|
||||
luaH_setint(a, j, &temp);
|
||||
}
|
||||
|
||||
static int sort_comp (TObject *f, TObject *a, TObject *b) {
|
||||
/* notice: the caller (auxsort) must check stack space */
|
||||
if (f) {
|
||||
*(L->stack.top) = *f;
|
||||
*(L->stack.top+1) = *a;
|
||||
*(L->stack.top+2) = *b;
|
||||
L->stack.top += 3;
|
||||
luaD_calln(2, 1);
|
||||
}
|
||||
else { /* a < b? */
|
||||
*(L->stack.top) = *a;
|
||||
*(L->stack.top+1) = *b;
|
||||
L->stack.top += 2;
|
||||
luaV_comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, IM_LT);
|
||||
}
|
||||
return ttype(--(L->stack.top)) != LUA_T_NIL;
|
||||
}
|
||||
|
||||
static void auxsort (Hash *a, int l, int u, TObject *f) {
|
||||
while (l < u) { /* for tail recursion */
|
||||
TObject *P;
|
||||
int i, j;
|
||||
/* sort elements a[l], a[(l+u)/2] and a[u] */
|
||||
if (sort_comp(f, luaH_getint(a, u), luaH_getint(a, l))) /* a[l]>a[u]? */
|
||||
swap(a, l, u);
|
||||
if (u-l == 1) break; /* only 2 elements */
|
||||
i = (l+u)/2;
|
||||
P = luaH_getint(a, i);
|
||||
if (sort_comp(f, P, luaH_getint(a, l))) /* a[l]>a[i]? */
|
||||
swap(a, l, i);
|
||||
else if (sort_comp(f, luaH_getint(a, u), P)) /* a[i]>a[u]? */
|
||||
swap(a, i, u);
|
||||
if (u-l == 2) break; /* only 3 elements */
|
||||
P = L->stack.top++;
|
||||
*P = *luaH_getint(a, i); /* save pivot on stack (for GC) */
|
||||
swap(a, i, u-1); /* put median element as pivot (a[u-1]) */
|
||||
/* a[l] <= P == a[u-1] <= a[u], only needs to sort from l+1 to u-2 */
|
||||
i = l; j = u-1;
|
||||
for (;;) {
|
||||
/* invariant: a[l..i] <= P <= a[j..u] */
|
||||
while (sort_comp(f, luaH_getint(a, ++i), P)) /* stop when a[i] >= P */
|
||||
if (i>u) lua_error("invalid order function for sorting");
|
||||
while (sort_comp(f, P, luaH_getint(a, --j))) /* stop when a[j] <= P */
|
||||
if (j<l) lua_error("invalid order function for sorting");
|
||||
if (j<i) break;
|
||||
swap(a, i, j);
|
||||
}
|
||||
swap(a, u-1, i); /* swap pivot (a[u-1]) with a[i] */
|
||||
L->stack.top--; /* remove pivot from stack */
|
||||
/* a[l..i-1] <= a[i] == P <= a[i+1..u] */
|
||||
/* adjust so that smaller "half" is in [j..i] and larger one in [l..u] */
|
||||
if (i-l < u-i) {
|
||||
j=l; i=i-1; l=i+2;
|
||||
}
|
||||
else {
|
||||
j=i+1; i=u; u=j-2;
|
||||
}
|
||||
auxsort(a, j, i, f); /* call recursively the smaller one */
|
||||
} /* repeat the routine for the larger one */
|
||||
}
|
||||
|
||||
static void luaB_sort (void) {
|
||||
lua_Object t = lua_getparam(1);
|
||||
Hash *a = gethash(1);
|
||||
int n = (int)getnarg(a);
|
||||
lua_Object func = lua_getparam(2);
|
||||
TObject *f = luaA_Address(func);
|
||||
luaL_arg_check(!f || lua_isfunction(func), 2, "function expected");
|
||||
luaD_checkstack(4); /* for Pivot, f, a, b (sort_comp) */
|
||||
auxsort(a, 1, n, f);
|
||||
lua_pushobject(t);
|
||||
}
|
||||
|
||||
/* }}===================================================== */
|
||||
|
||||
|
||||
/*
|
||||
** ====================================================== */
|
||||
|
||||
|
||||
|
||||
#ifdef DEBUG
|
||||
/*
|
||||
** {======================================================
|
||||
** some DEBUG functions
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
static void mem_query (void) {
|
||||
lua_pushnumber(totalmem);
|
||||
lua_pushnumber(numblocks);
|
||||
}
|
||||
|
||||
|
||||
static void query_strings (void) {
|
||||
lua_pushnumber(L->string_root[luaL_check_int(1)].nuse);
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
for (;;) {
|
||||
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;
|
||||
case 'N' : lua_pushstring(lua_nextvar(lua_getstring(reg[getnum(s)])));
|
||||
break;
|
||||
case 'n' : { int n=getnum(s);
|
||||
n=lua_next(reg[n], (int)lua_getnumber(reg[getnum(s)]));
|
||||
lua_pushnumber(n); 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
|
||||
|
||||
|
||||
|
||||
static struct luaL_reg builtin_funcs[] = {
|
||||
#ifdef LUA_COMPAT2_5
|
||||
{"setfallback", luaT_setfallback},
|
||||
#endif
|
||||
#ifdef DEBUG
|
||||
{"testC", testC},
|
||||
{"totalmem", mem_query},
|
||||
{"count", countlist},
|
||||
{"querystr", query_strings},
|
||||
#endif
|
||||
{"_ALERT", luaB_alert},
|
||||
{"_ERRORMESSAGE", error_message},
|
||||
{"call", luaB_call},
|
||||
{"collectgarbage", luaB_collectgarbage},
|
||||
{"copytagmethods", luaB_copytagmethods},
|
||||
{"dofile", luaB_dofile},
|
||||
{"dostring", luaB_dostring},
|
||||
{"error", luaB_error},
|
||||
{"getglobal", luaB_getglobal},
|
||||
{"gettagmethod", luaB_gettagmethod},
|
||||
{"newtag", luaB_newtag},
|
||||
{"next", luaB_next},
|
||||
{"nextvar", luaB_nextvar},
|
||||
{"print", luaB_print},
|
||||
{"rawgetglobal", luaB_rawgetglobal},
|
||||
{"rawgettable", luaB_rawgettable},
|
||||
{"rawsetglobal", luaB_rawsetglobal},
|
||||
{"rawsettable", luaB_rawsettable},
|
||||
{"seterrormethod", luaB_seterrormethod},
|
||||
{"setglobal", luaB_setglobal},
|
||||
{"settag", luaB_settag},
|
||||
{"settagmethod", luaB_settagmethod},
|
||||
{"tag", luaB_luatag},
|
||||
{"tonumber", luaB_tonumber},
|
||||
{"tostring", luaB_tostring},
|
||||
{"type", luaB_type},
|
||||
/* "Extra" functions */
|
||||
{"assert", luaB_assert},
|
||||
{"foreach", luaB_foreach},
|
||||
{"foreachi", luaB_foreachi},
|
||||
{"foreachvar", luaB_foreachvar},
|
||||
{"getn", luaB_getn},
|
||||
{"sort", luaB_sort},
|
||||
{"tinsert", luaB_tinsert},
|
||||
{"tremove", luaB_tremove}
|
||||
};
|
||||
|
||||
|
||||
#define INTFUNCSIZE (sizeof(builtin_funcs)/sizeof(builtin_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(builtin_funcs, (sizeof(builtin_funcs)/sizeof(builtin_funcs[0])));
|
||||
lua_pushstring(LUA_VERSION);
|
||||
lua_setglobal("_VERSION");
|
||||
}
|
||||
|
||||
14
lbuiltin.h
Normal file
14
lbuiltin.h
Normal file
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
** $Id: $
|
||||
** Built-in functions
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lbuiltin_h
|
||||
#define lbuiltin_h
|
||||
|
||||
|
||||
void luaB_predefine (void);
|
||||
|
||||
|
||||
#endif
|
||||
217
ldblib.c
Normal file
217
ldblib.c
Normal file
@@ -0,0 +1,217 @@
|
||||
/*
|
||||
** $Id: ldblib.c,v 1.4 1999/02/04 17:47:59 roberto Exp roberto $
|
||||
** Interface from Lua to its debug API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "lua.h"
|
||||
#include "luadebug.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
|
||||
static void settabss (lua_Object t, char *i, char *v) {
|
||||
lua_pushobject(t);
|
||||
lua_pushstring(i);
|
||||
lua_pushstring(v);
|
||||
lua_settable();
|
||||
}
|
||||
|
||||
|
||||
static void settabsi (lua_Object t, char *i, int v) {
|
||||
lua_pushobject(t);
|
||||
lua_pushstring(i);
|
||||
lua_pushnumber(v);
|
||||
lua_settable();
|
||||
}
|
||||
|
||||
|
||||
static lua_Object getfuncinfo (lua_Object func) {
|
||||
lua_Object result = lua_createtable();
|
||||
char *str;
|
||||
int line;
|
||||
lua_funcinfo(func, &str, &line);
|
||||
if (line == -1) /* C function? */
|
||||
settabss(result, "kind", "C");
|
||||
else if (line == 0) { /* "main"? */
|
||||
settabss(result, "kind", "chunk");
|
||||
settabss(result, "source", str);
|
||||
}
|
||||
else { /* Lua function */
|
||||
settabss(result, "kind", "Lua");
|
||||
settabsi(result, "def_line", line);
|
||||
settabss(result, "source", str);
|
||||
}
|
||||
if (line != 0) { /* is it not a "main"? */
|
||||
char *kind = lua_getobjname(func, &str);
|
||||
if (*kind) {
|
||||
settabss(result, "name", str);
|
||||
settabss(result, "where", kind);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
static void getstack (void) {
|
||||
lua_Object func = lua_stackedfunction(luaL_check_int(1));
|
||||
if (func == LUA_NOOBJECT) /* level out of range? */
|
||||
return;
|
||||
else {
|
||||
lua_Object result = getfuncinfo(func);
|
||||
int currline = lua_currentline(func);
|
||||
if (currline > 0)
|
||||
settabsi(result, "current", currline);
|
||||
lua_pushobject(result);
|
||||
lua_pushstring("func");
|
||||
lua_pushobject(func);
|
||||
lua_settable(); /* result.func = func */
|
||||
lua_pushobject(result);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void funcinfo (void) {
|
||||
lua_pushobject(getfuncinfo(luaL_functionarg(1)));
|
||||
}
|
||||
|
||||
|
||||
static int findlocal (lua_Object func, int arg) {
|
||||
lua_Object v = lua_getparam(arg);
|
||||
if (lua_isnumber(v))
|
||||
return (int)lua_getnumber(v);
|
||||
else {
|
||||
char *name = luaL_check_string(arg);
|
||||
int i = 0;
|
||||
int result = -1;
|
||||
char *vname;
|
||||
while (lua_getlocal(func, ++i, &vname) != LUA_NOOBJECT) {
|
||||
if (strcmp(name, vname) == 0)
|
||||
result = i; /* keep looping to get the last var with this name */
|
||||
}
|
||||
if (result == -1)
|
||||
luaL_verror("no local variable `%.50s' at given level", name);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void getlocal (void) {
|
||||
lua_Object func = lua_stackedfunction(luaL_check_int(1));
|
||||
lua_Object val;
|
||||
char *name;
|
||||
if (func == LUA_NOOBJECT) /* level out of range? */
|
||||
return; /* return nil */
|
||||
else if (lua_getparam(2) != LUA_NOOBJECT) { /* 2nd argument? */
|
||||
if ((val = lua_getlocal(func, findlocal(func, 2), &name)) != LUA_NOOBJECT) {
|
||||
lua_pushobject(val);
|
||||
lua_pushstring(name);
|
||||
}
|
||||
/* else return nil */
|
||||
}
|
||||
else { /* collect all locals in a table */
|
||||
lua_Object result = lua_createtable();
|
||||
int i;
|
||||
for (i=1; ;i++) {
|
||||
if ((val = lua_getlocal(func, i, &name)) == LUA_NOOBJECT)
|
||||
break;
|
||||
lua_pushobject(result);
|
||||
lua_pushstring(name);
|
||||
lua_pushobject(val);
|
||||
lua_settable(); /* result[name] = value */
|
||||
}
|
||||
lua_pushobject(result);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void setlocal (void) {
|
||||
lua_Object func = lua_stackedfunction(luaL_check_int(1));
|
||||
int numvar;
|
||||
luaL_arg_check(func != LUA_NOOBJECT, 1, "level out of range");
|
||||
numvar = findlocal(func, 2);
|
||||
lua_pushobject(luaL_nonnullarg(3));
|
||||
if (!lua_setlocal(func, numvar))
|
||||
lua_error("no such local variable");
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int linehook = -1; /* Lua reference to line hook function */
|
||||
static int callhook = -1; /* Lua reference to call hook function */
|
||||
|
||||
|
||||
static void dohook (int ref) {
|
||||
lua_LHFunction oldlinehook = lua_setlinehook(NULL);
|
||||
lua_CHFunction oldcallhook = lua_setcallhook(NULL);
|
||||
lua_callfunction(lua_getref(ref));
|
||||
lua_setlinehook(oldlinehook);
|
||||
lua_setcallhook(oldcallhook);
|
||||
}
|
||||
|
||||
|
||||
static void linef (int line) {
|
||||
lua_pushnumber(line);
|
||||
dohook(linehook);
|
||||
}
|
||||
|
||||
|
||||
static void callf (lua_Function func, char *file, int line) {
|
||||
if (func != LUA_NOOBJECT) {
|
||||
lua_pushobject(func);
|
||||
lua_pushstring(file);
|
||||
lua_pushnumber(line);
|
||||
}
|
||||
dohook(callhook);
|
||||
}
|
||||
|
||||
|
||||
static void setcallhook (void) {
|
||||
lua_Object f = lua_getparam(1);
|
||||
lua_unref(callhook);
|
||||
if (f == LUA_NOOBJECT) {
|
||||
callhook = -1;
|
||||
lua_setcallhook(NULL);
|
||||
}
|
||||
else {
|
||||
lua_pushobject(f);
|
||||
callhook = lua_ref(1);
|
||||
lua_setcallhook(callf);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void setlinehook (void) {
|
||||
lua_Object f = lua_getparam(1);
|
||||
lua_unref(linehook);
|
||||
if (f == LUA_NOOBJECT) {
|
||||
linehook = -1;
|
||||
lua_setlinehook(NULL);
|
||||
}
|
||||
else {
|
||||
lua_pushobject(f);
|
||||
linehook = lua_ref(1);
|
||||
lua_setlinehook(linef);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static struct luaL_reg dblib[] = {
|
||||
{"funcinfo", funcinfo},
|
||||
{"getlocal", getlocal},
|
||||
{"getstack", getstack},
|
||||
{"setcallhook", setcallhook},
|
||||
{"setlinehook", setlinehook},
|
||||
{"setlocal", setlocal}
|
||||
};
|
||||
|
||||
|
||||
void lua_dblibopen (void) {
|
||||
luaL_openlib(dblib, (sizeof(dblib)/sizeof(dblib[0])));
|
||||
}
|
||||
|
||||
395
ldo.c
Normal file
395
ldo.c
Normal file
@@ -0,0 +1,395 @@
|
||||
/*
|
||||
** $Id: ldo.c,v 1.40 1999/03/10 14:23:07 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 "lauxlib.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lgc.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.h"
|
||||
#include "lparser.h"
|
||||
#include "lstate.h"
|
||||
#include "lstring.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
|
||||
|
||||
|
||||
|
||||
#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);
|
||||
}
|
||||
|
||||
|
||||
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)+STACK_UNIT+n;
|
||||
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;
|
||||
(*L->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)
|
||||
(*L->callhook)(LUA_NOOBJECT, "(return)", 0);
|
||||
else {
|
||||
TObject *f = L->stack.stack+base-1;
|
||||
if (tf)
|
||||
(*L->callhook)(Ref(f), tf->source->str, tf->lineDefined);
|
||||
else
|
||||
(*L->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 (L->callhook)
|
||||
luaD_callHook(base, NULL, 0);
|
||||
(*f)(); /* do the actual call */
|
||||
if (L->callhook) /* func may have changed 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_calln(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;
|
||||
}
|
||||
|
||||
|
||||
void luaD_calln (int nArgs, int nResults) {
|
||||
luaD_call((L->stack.top-L->stack.stack)-nArgs, nResults);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** 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 *em = &(luaS_new("_ERRORMESSAGE")->u.s.globalval);
|
||||
if (ttype(em) == LUA_T_PROTO || ttype(em) == LUA_T_CPROTO ||
|
||||
ttype(em) == LUA_T_CLOSURE) {
|
||||
*L->stack.top = *em;
|
||||
incr_top;
|
||||
lua_pushstring(s);
|
||||
luaD_calln(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 {
|
||||
message("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 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 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) {
|
||||
volatile struct C_Lua_Stack oldCLS = L->Cstack;
|
||||
jmp_buf myErrorJmp;
|
||||
volatile int status;
|
||||
jmp_buf *volatile 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) {
|
||||
volatile struct C_Lua_Stack oldCLS = L->Cstack;
|
||||
jmp_buf myErrorJmp;
|
||||
volatile int status;
|
||||
TProtoFunc *volatile tf;
|
||||
jmp_buf *volatile oldErr = L->errorJmp;
|
||||
L->errorJmp = &myErrorJmp;
|
||||
if (setjmp(myErrorJmp) == 0) {
|
||||
tf = bin ? luaU_undump1(z) : luaY_parser(z);
|
||||
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;
|
||||
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;
|
||||
int debug = L->debug; /* save debug 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);
|
||||
L->debug = debug; /* restore debug status */
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#define MAXFILENAME 260 /* maximum part of a file name kept */
|
||||
|
||||
int lua_dofile (char *filename) {
|
||||
ZIO z;
|
||||
int status;
|
||||
int c;
|
||||
int bin;
|
||||
char source[MAXFILENAME];
|
||||
FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
|
||||
if (f == NULL)
|
||||
return 2;
|
||||
c = fgetc(f);
|
||||
ungetc(c, f);
|
||||
bin = (c == ID_CHUNK);
|
||||
if (bin)
|
||||
f = freopen(filename, "rb", f); /* set binary mode */
|
||||
luaL_filesource(source, filename, sizeof(source));
|
||||
luaZ_Fopen(&z, f, source);
|
||||
status = do_main(&z, bin);
|
||||
if (f != stdin)
|
||||
fclose(f);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
int lua_dostring (char *str) {
|
||||
return lua_dobuffer(str, strlen(str), str);
|
||||
}
|
||||
|
||||
|
||||
int lua_dobuffer (char *buff, int size, char *name) {
|
||||
ZIO z;
|
||||
if (!name) name = "?";
|
||||
luaZ_mopen(&z, buff, size, name);
|
||||
return do_main(&z, buff[0]==ID_CHUNK);
|
||||
}
|
||||
|
||||
47
ldo.h
Normal file
47
ldo.h
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
** $Id: ldo.h,v 1.4 1997/12/15 16:17:20 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_calln (int nArgs, 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
|
||||
470
lex.c
470
lex.c
@@ -1,470 +0,0 @@
|
||||
char *rcs_lex = "$Id: lex.c,v 3.4 1997/06/11 18:56:02 roberto Exp roberto $";
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "auxlib.h"
|
||||
#include "luamem.h"
|
||||
#include "tree.h"
|
||||
#include "table.h"
|
||||
#include "lex.h"
|
||||
#include "inout.h"
|
||||
#include "luadebug.h"
|
||||
#include "parser.h"
|
||||
|
||||
#define MINBUFF 250
|
||||
|
||||
static int current; /* look ahead character */
|
||||
static ZIO *lex_z;
|
||||
|
||||
|
||||
#define next() (current = zgetc(lex_z))
|
||||
#define save(x) (yytext[tokensize++] = (x))
|
||||
#define save_and_next() (save(current), next())
|
||||
|
||||
|
||||
#define MAX_IFS 5
|
||||
|
||||
/* "ifstate" keeps the state of each nested $if the lexical is dealing with. */
|
||||
|
||||
static struct {
|
||||
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 */
|
||||
} ifstate[MAX_IFS];
|
||||
|
||||
static int iflevel; /* level of nested $if's */
|
||||
|
||||
|
||||
void lua_setinput (ZIO *z)
|
||||
{
|
||||
current = '\n';
|
||||
lua_linenumber = 0;
|
||||
iflevel = 0;
|
||||
ifstate[0].skip = 0;
|
||||
ifstate[0].elsepart = 1; /* to avoid a free $else */
|
||||
lex_z = z;
|
||||
}
|
||||
|
||||
|
||||
static void luaI_auxsyntaxerror (char *s)
|
||||
{
|
||||
luaL_verror("%s;\n> at line %d in file %s",
|
||||
s, lua_linenumber, lua_parsedfile);
|
||||
}
|
||||
|
||||
static void luaI_auxsynterrbf (char *s, char *token)
|
||||
{
|
||||
if (token[0] == 0)
|
||||
token = "<eof>";
|
||||
luaL_verror("%s;\n> last token read: \"%s\" at line %d in file %s",
|
||||
s, token, lua_linenumber, lua_parsedfile);
|
||||
}
|
||||
|
||||
void luaI_syntaxerror (char *s)
|
||||
{
|
||||
luaI_auxsynterrbf(s, luaI_buffer(1));
|
||||
}
|
||||
|
||||
|
||||
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} };
|
||||
|
||||
|
||||
#define RESERVEDSIZE (sizeof(reserved)/sizeof(reserved[0]))
|
||||
|
||||
|
||||
void luaI_addReserved (void)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<RESERVEDSIZE; i++)
|
||||
{
|
||||
TaggedString *ts = lua_createstring(reserved[i].name);
|
||||
ts->marked = reserved[i].token; /* reserved word (always > 255) */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Pragma handling
|
||||
*/
|
||||
|
||||
#define PRAGMASIZE 20
|
||||
|
||||
static void skipspace (void)
|
||||
{
|
||||
while (current == ' ' || current == '\t') next();
|
||||
}
|
||||
|
||||
|
||||
static int checkcond (char *buff)
|
||||
{
|
||||
static char *opts[] = {"nil", "1"};
|
||||
int i = luaI_findstring(buff, opts);
|
||||
if (i >= 0) return i;
|
||||
else if (isalpha((unsigned char)buff[0]) || buff[0] == '_')
|
||||
return luaI_globaldefined(buff);
|
||||
else {
|
||||
luaI_auxsynterrbf("invalid $if condition", buff);
|
||||
return 0; /* to avoid warnings */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void readname (char *buff)
|
||||
{
|
||||
int i = 0;
|
||||
skipspace();
|
||||
while (isalnum((unsigned char)current) || current == '_') {
|
||||
if (i >= PRAGMASIZE) {
|
||||
buff[PRAGMASIZE] = 0;
|
||||
luaI_auxsynterrbf("pragma too long", buff);
|
||||
}
|
||||
buff[i++] = current;
|
||||
next();
|
||||
}
|
||||
buff[i] = 0;
|
||||
}
|
||||
|
||||
|
||||
static void inclinenumber (void);
|
||||
|
||||
|
||||
static void ifskip (void)
|
||||
{
|
||||
while (ifstate[iflevel].skip) {
|
||||
if (current == '\n')
|
||||
inclinenumber();
|
||||
else if (current == EOZ)
|
||||
luaI_auxsyntaxerror("input ends inside a $if");
|
||||
else next();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void inclinenumber (void)
|
||||
{
|
||||
static char *pragmas [] =
|
||||
{"debug", "nodebug", "endinput", "end", "ifnot", "if", "else", NULL};
|
||||
next(); /* skip '\n' */
|
||||
++lua_linenumber;
|
||||
if (current == '$') { /* is a pragma? */
|
||||
char buff[PRAGMASIZE+1];
|
||||
int ifnot = 0;
|
||||
int skip = ifstate[iflevel].skip;
|
||||
next(); /* skip $ */
|
||||
readname(buff);
|
||||
switch (luaI_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) {
|
||||
current = EOZ;
|
||||
iflevel = 0; /* to allow $endinput inside a $if */
|
||||
}
|
||||
break;
|
||||
case 3: /* end */
|
||||
if (iflevel-- == 0)
|
||||
luaI_auxsyntaxerror("unmatched $endif");
|
||||
break;
|
||||
case 4: /* ifnot */
|
||||
ifnot = 1;
|
||||
/* go through */
|
||||
case 5: /* if */
|
||||
if (iflevel == MAX_IFS-1)
|
||||
luaI_auxsyntaxerror("too many nested `$ifs'");
|
||||
readname(buff);
|
||||
iflevel++;
|
||||
ifstate[iflevel].elsepart = 0;
|
||||
ifstate[iflevel].condition = checkcond(buff) ? !ifnot : ifnot;
|
||||
ifstate[iflevel].skip = skip || !ifstate[iflevel].condition;
|
||||
break;
|
||||
case 6: /* else */
|
||||
if (ifstate[iflevel].elsepart)
|
||||
luaI_auxsyntaxerror("unmatched $else");
|
||||
ifstate[iflevel].elsepart = 1;
|
||||
ifstate[iflevel].skip =
|
||||
ifstate[iflevel-1].skip || ifstate[iflevel].condition;
|
||||
break;
|
||||
default:
|
||||
luaI_auxsynterrbf("invalid pragma", buff);
|
||||
}
|
||||
skipspace();
|
||||
if (current == '\n') /* pragma must end with a '\n' ... */
|
||||
inclinenumber();
|
||||
else if (current != EOZ) /* or eof */
|
||||
luaI_auxsyntaxerror("invalid pragma format");
|
||||
ifskip();
|
||||
}
|
||||
}
|
||||
|
||||
static int read_long_string (char *yytext, int buffsize)
|
||||
{
|
||||
int cont = 0;
|
||||
int tokensize = 2; /* '[[' already stored */
|
||||
while (1)
|
||||
{
|
||||
if (buffsize-tokensize <= 2) /* may read more than 1 char in one cicle */
|
||||
yytext = luaI_buffer(buffsize *= 2);
|
||||
switch (current)
|
||||
{
|
||||
case EOZ:
|
||||
save(0);
|
||||
return WRONGTOKEN;
|
||||
case '[':
|
||||
save_and_next();
|
||||
if (current == '[')
|
||||
{
|
||||
cont++;
|
||||
save_and_next();
|
||||
}
|
||||
continue;
|
||||
case ']':
|
||||
save_and_next();
|
||||
if (current == ']')
|
||||
{
|
||||
if (cont == 0) goto endloop;
|
||||
cont--;
|
||||
save_and_next();
|
||||
}
|
||||
continue;
|
||||
case '\n':
|
||||
save('\n');
|
||||
inclinenumber();
|
||||
continue;
|
||||
default:
|
||||
save_and_next();
|
||||
}
|
||||
} endloop:
|
||||
save_and_next(); /* pass the second ']' */
|
||||
yytext[tokensize-2] = 0; /* erases ']]' */
|
||||
luaY_lval.vWord = luaI_findconstantbyname(yytext+2);
|
||||
yytext[tokensize-2] = ']'; /* restores ']]' */
|
||||
save(0);
|
||||
return STRING;
|
||||
}
|
||||
|
||||
int luaY_lex (void)
|
||||
{
|
||||
static int linelasttoken = 0;
|
||||
double a;
|
||||
int buffsize = MINBUFF;
|
||||
char *yytext = luaI_buffer(buffsize);
|
||||
yytext[1] = yytext[2] = yytext[3] = 0;
|
||||
if (lua_debug)
|
||||
luaI_codedebugline(linelasttoken);
|
||||
linelasttoken = lua_linenumber;
|
||||
while (1)
|
||||
{
|
||||
int tokensize = 0;
|
||||
switch (current)
|
||||
{
|
||||
case '\n':
|
||||
inclinenumber();
|
||||
linelasttoken = lua_linenumber;
|
||||
continue;
|
||||
|
||||
case ' ': case '\t': case '\r': /* CR: to avoid problems with DOS */
|
||||
next();
|
||||
continue;
|
||||
|
||||
case '-':
|
||||
save_and_next();
|
||||
if (current != '-') return '-';
|
||||
do { next(); } while (current != '\n' && current != EOZ);
|
||||
continue;
|
||||
|
||||
case '[':
|
||||
save_and_next();
|
||||
if (current != '[') return '[';
|
||||
else
|
||||
{
|
||||
save_and_next(); /* pass the second '[' */
|
||||
return read_long_string(yytext, buffsize);
|
||||
}
|
||||
|
||||
case '=':
|
||||
save_and_next();
|
||||
if (current != '=') return '=';
|
||||
else { save_and_next(); return EQ; }
|
||||
|
||||
case '<':
|
||||
save_and_next();
|
||||
if (current != '=') return '<';
|
||||
else { save_and_next(); return LE; }
|
||||
|
||||
case '>':
|
||||
save_and_next();
|
||||
if (current != '=') return '>';
|
||||
else { save_and_next(); return GE; }
|
||||
|
||||
case '~':
|
||||
save_and_next();
|
||||
if (current != '=') return '~';
|
||||
else { save_and_next(); return NE; }
|
||||
|
||||
case '"':
|
||||
case '\'':
|
||||
{
|
||||
int del = current;
|
||||
save_and_next();
|
||||
while (current != del)
|
||||
{
|
||||
if (buffsize-tokensize <= 2) /* may read more than 1 char in one cicle */
|
||||
yytext = luaI_buffer(buffsize *= 2);
|
||||
switch (current)
|
||||
{
|
||||
case EOZ:
|
||||
case '\n':
|
||||
save(0);
|
||||
return WRONGTOKEN;
|
||||
case '\\':
|
||||
next(); /* do not save the '\' */
|
||||
switch (current)
|
||||
{
|
||||
case 'n': save('\n'); next(); break;
|
||||
case 't': save('\t'); next(); break;
|
||||
case 'r': save('\r'); next(); break;
|
||||
case '\n': save('\n'); inclinenumber(); break;
|
||||
default : save_and_next(); break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
save_and_next();
|
||||
}
|
||||
}
|
||||
next(); /* skip delimiter */
|
||||
save(0);
|
||||
luaY_lval.vWord = luaI_findconstantbyname(yytext+1);
|
||||
tokensize--;
|
||||
save(del); save(0); /* restore delimiter */
|
||||
return STRING;
|
||||
}
|
||||
|
||||
case 'a': case 'b': case 'c': case 'd': case 'e':
|
||||
case 'f': case 'g': case 'h': case 'i': case 'j':
|
||||
case 'k': case 'l': case 'm': case 'n': case 'o':
|
||||
case 'p': case 'q': case 'r': case 's': case 't':
|
||||
case 'u': case 'v': case 'w': case 'x': case 'y':
|
||||
case 'z':
|
||||
case 'A': case 'B': case 'C': case 'D': case 'E':
|
||||
case 'F': case 'G': case 'H': case 'I': case 'J':
|
||||
case 'K': case 'L': case 'M': case 'N': case 'O':
|
||||
case 'P': case 'Q': case 'R': case 'S': case 'T':
|
||||
case 'U': case 'V': case 'W': case 'X': case 'Y':
|
||||
case 'Z':
|
||||
case '_':
|
||||
{
|
||||
TaggedString *ts;
|
||||
do {
|
||||
save_and_next();
|
||||
} while (isalnum((unsigned char)current) || current == '_');
|
||||
save(0);
|
||||
ts = lua_createstring(yytext);
|
||||
if (ts->marked > 2)
|
||||
return ts->marked; /* reserved word */
|
||||
luaY_lval.pTStr = ts;
|
||||
ts->marked = 2; /* avoid GC */
|
||||
return NAME;
|
||||
}
|
||||
|
||||
case '.':
|
||||
save_and_next();
|
||||
if (current == '.')
|
||||
{
|
||||
save_and_next();
|
||||
if (current == '.')
|
||||
{
|
||||
save_and_next();
|
||||
return DOTS; /* ... */
|
||||
}
|
||||
else return CONC; /* .. */
|
||||
}
|
||||
else if (!isdigit((unsigned char)current)) return '.';
|
||||
/* 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+(current-'0');
|
||||
save_and_next();
|
||||
} while (isdigit((unsigned char)current));
|
||||
if (current == '.') {
|
||||
save_and_next();
|
||||
if (current == '.')
|
||||
luaI_syntaxerror(
|
||||
"ambiguous syntax (decimal point x string concatenation)");
|
||||
}
|
||||
fraction:
|
||||
{ double da=0.1;
|
||||
while (isdigit((unsigned char)current))
|
||||
{
|
||||
a+=(current-'0')*da;
|
||||
da/=10.0;
|
||||
save_and_next();
|
||||
}
|
||||
if (current == 'e' || current == 'E')
|
||||
{
|
||||
int e=0;
|
||||
int neg;
|
||||
double ea;
|
||||
save_and_next();
|
||||
neg=(current=='-');
|
||||
if (current == '+' || current == '-') save_and_next();
|
||||
if (!isdigit((unsigned char)current)) {
|
||||
save(0); return WRONGTOKEN; }
|
||||
do {
|
||||
e=10.0*e+(current-'0');
|
||||
save_and_next();
|
||||
} while (isdigit((unsigned char)current));
|
||||
for (ea=neg?0.1:10.0; e>0; e>>=1)
|
||||
{
|
||||
if (e & 1) a*=ea;
|
||||
ea*=ea;
|
||||
}
|
||||
}
|
||||
luaY_lval.vFloat = a;
|
||||
save(0);
|
||||
return NUMBER;
|
||||
}
|
||||
|
||||
case EOZ:
|
||||
save(0);
|
||||
if (iflevel > 0)
|
||||
luaI_syntaxerror("missing $endif");
|
||||
return 0;
|
||||
|
||||
default:
|
||||
save_and_next();
|
||||
return yytext[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
18
lex.h
18
lex.h
@@ -1,18 +0,0 @@
|
||||
/*
|
||||
** lex.h
|
||||
** TecCGraf - PUC-Rio
|
||||
** $Id: lex.h,v 1.3 1996/11/08 12:49:35 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef lex_h
|
||||
#define lex_h
|
||||
|
||||
#include "zio.h"
|
||||
|
||||
void lua_setinput (ZIO *z);
|
||||
void luaI_syntaxerror (char *s);
|
||||
int luaY_lex (void);
|
||||
void luaI_addReserved (void);
|
||||
|
||||
|
||||
#endif
|
||||
98
lfunc.c
Normal file
98
lfunc.c
Normal file
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
** $Id: lfunc.c,v 1.9 1998/06/19 16:14:09 roberto Exp roberto $
|
||||
** Auxiliary 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->source = 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
Normal file
23
lfunc.h
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
** $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
|
||||
275
lgc.c
Normal file
275
lgc.c
Normal file
@@ -0,0 +1,275 @@
|
||||
/*
|
||||
** $Id: lgc.c,v 1.22 1999/02/26 15:48:55 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)
|
||||
break;
|
||||
if (ref == L->refSize) { /* no more empty spaces? */
|
||||
luaM_growvector(L->refArray, L->refSize, 1, struct ref, refEM, MAX_INT);
|
||||
L->refSize++;
|
||||
}
|
||||
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_INTERNALERROR("invalid type");
|
||||
#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) {
|
||||
int i;
|
||||
f->head.marked = 1;
|
||||
strmark(f->source);
|
||||
for (i=0; i<f->nconsts; i++)
|
||||
markobject(&f->consts[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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){
|
||||
LUA_ASSERT(g->constindex >= 0, "userdata in global list");
|
||||
if (g->u.s.globalval.ttype != LUA_T_NIL) {
|
||||
markobject(&g->u.s.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
Normal file
21
lgc.h
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
** $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
|
||||
17
linit.c
Normal file
17
linit.c
Normal file
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
** $Id: linit.c,v 1.1 1999/01/08 16:47:44 roberto Exp $
|
||||
** Initialization of libraries for lua.c
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#include "lua.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
void lua_userinit (void) {
|
||||
lua_iolibopen();
|
||||
lua_strlibopen();
|
||||
lua_mathlibopen();
|
||||
lua_dblibopen();
|
||||
}
|
||||
|
||||
588
liolib.c
Normal file
588
liolib.c
Normal file
@@ -0,0 +1,588 @@
|
||||
/*
|
||||
** $Id: liolib.c,v 1.37 1999/04/05 19:47:05 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
|
||||
/* no support for locale and for strerror: fake them */
|
||||
#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 IOTAG 1
|
||||
|
||||
#define FIRSTARG 2 /* 1st is upvalue */
|
||||
|
||||
#define CLOSEDTAG(tag) ((tag)-1) /* assume that CLOSEDTAG = iotag-1 */
|
||||
|
||||
|
||||
#define FINPUT "_INPUT"
|
||||
#define FOUTPUT "_OUTPUT"
|
||||
|
||||
|
||||
#ifdef POPEN
|
||||
FILE *popen();
|
||||
int pclose();
|
||||
#define CLOSEFILE(f) {if (pclose(f) == -1) fclose(f);}
|
||||
#else
|
||||
/* no support for popen */
|
||||
#define popen(x,y) NULL /* that is, popen always fails */
|
||||
#define CLOSEFILE(f) {fclose(f);}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
static void pushresult (int i) {
|
||||
if (i)
|
||||
lua_pushuserdata(NULL);
|
||||
else {
|
||||
lua_pushnil();
|
||||
lua_pushstring(strerror(errno));
|
||||
lua_pushnumber(errno);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** FILE Operations
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
static int gettag (void) {
|
||||
return (int)lua_getnumber(lua_getparam(IOTAG));
|
||||
}
|
||||
|
||||
|
||||
static int ishandle (lua_Object f) {
|
||||
if (lua_isuserdata(f)) {
|
||||
int tag = gettag();
|
||||
if (lua_tag(f) == CLOSEDTAG(tag))
|
||||
lua_error("cannot access a closed file");
|
||||
return lua_tag(f) == tag;
|
||||
}
|
||||
else return 0;
|
||||
}
|
||||
|
||||
|
||||
static FILE *getfilebyname (char *name) {
|
||||
lua_Object f = lua_getglobal(name);
|
||||
if (!ishandle(f))
|
||||
luaL_verror("global variable `%.50s' is not a file handle", name);
|
||||
return lua_getuserdata(f);
|
||||
}
|
||||
|
||||
|
||||
static FILE *getfile (int arg) {
|
||||
lua_Object f = lua_getparam(arg);
|
||||
return (ishandle(f)) ? lua_getuserdata(f) : NULL;
|
||||
}
|
||||
|
||||
|
||||
static FILE *getnonullfile (int arg) {
|
||||
FILE *f = getfile(arg);
|
||||
luaL_arg_check(f, arg, "invalid file handle");
|
||||
return f;
|
||||
}
|
||||
|
||||
|
||||
static FILE *getfileparam (char *name, int *arg) {
|
||||
FILE *f = getfile(*arg);
|
||||
if (f) {
|
||||
(*arg)++;
|
||||
return f;
|
||||
}
|
||||
else
|
||||
return getfilebyname(name);
|
||||
}
|
||||
|
||||
|
||||
static void closefile (FILE *f) {
|
||||
if (f != stdin && f != stdout) {
|
||||
int tag = gettag();
|
||||
CLOSEFILE(f);
|
||||
lua_pushusertag(f, tag);
|
||||
lua_settag(CLOSEDTAG(tag));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void io_close (void) {
|
||||
closefile(getnonullfile(FIRSTARG));
|
||||
}
|
||||
|
||||
|
||||
static void gc_close (void) {
|
||||
FILE *f = getnonullfile(FIRSTARG);
|
||||
if (f != stdin && f != stdout && f != stderr) {
|
||||
CLOSEFILE(f);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void io_open (void) {
|
||||
FILE *f = fopen(luaL_check_string(FIRSTARG), luaL_check_string(FIRSTARG+1));
|
||||
if (f) lua_pushusertag(f, gettag());
|
||||
else pushresult(0);
|
||||
}
|
||||
|
||||
|
||||
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();
|
||||
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(getfilebyname(FINPUT));
|
||||
current = stdin;
|
||||
}
|
||||
else if (lua_tag(f) == gettag()) /* deprecated option */
|
||||
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(getfilebyname(FOUTPUT));
|
||||
current = stdout;
|
||||
}
|
||||
else if (lua_tag(f) == gettag()) /* deprecated option */
|
||||
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) {
|
||||
FILE *fp = fopen(luaL_check_string(FIRSTARG), "a");
|
||||
if (fp != NULL)
|
||||
setreturn(fp, FOUTPUT);
|
||||
else
|
||||
pushresult(0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** READ
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
** We cannot lookahead without need, because this can lock stdin.
|
||||
** This flag signals when we need to read a next char.
|
||||
*/
|
||||
#define NEED_OTHER (EOF-1) /* just some flag different from EOF */
|
||||
|
||||
|
||||
static int read_pattern (FILE *f, char *p) {
|
||||
int inskip = 0; /* {skip} level */
|
||||
int c = NEED_OTHER;
|
||||
while (*p != '\0') {
|
||||
switch (*p) {
|
||||
case '{':
|
||||
inskip++;
|
||||
p++;
|
||||
continue;
|
||||
case '}':
|
||||
if (!inskip) lua_error("unbalanced braces in read pattern");
|
||||
inskip--;
|
||||
p++;
|
||||
continue;
|
||||
default: {
|
||||
char *ep; /* get what is next */
|
||||
int m; /* match result */
|
||||
if (c == NEED_OTHER) c = getc(f);
|
||||
if (c != EOF)
|
||||
m = luaI_singlematch(c, p, &ep);
|
||||
else {
|
||||
luaI_singlematch(0, p, &ep); /* to set "ep" */
|
||||
m = 0; /* EOF matches no pattern */
|
||||
}
|
||||
if (m) {
|
||||
if (!inskip) luaL_addchar(c);
|
||||
c = NEED_OTHER;
|
||||
}
|
||||
switch (*ep) {
|
||||
case '*': /* repetition */
|
||||
if (!m) p = ep+1; /* else stay in (repeat) the same item */
|
||||
continue;
|
||||
case '?': /* optional */
|
||||
p = ep+1; /* continues reading the pattern */
|
||||
continue;
|
||||
default:
|
||||
if (!m) goto break_while; /* pattern fails? */
|
||||
p = ep; /* else continues reading the pattern */
|
||||
}
|
||||
}
|
||||
}
|
||||
} break_while:
|
||||
if (c != NEED_OTHER) ungetc(c, f);
|
||||
return (*p == '\0');
|
||||
}
|
||||
|
||||
|
||||
static int read_number (FILE *f) {
|
||||
double d;
|
||||
if (fscanf(f, "%lf", &d) == 1) {
|
||||
lua_pushnumber(d);
|
||||
return 1;
|
||||
}
|
||||
else return 0; /* read fails */
|
||||
}
|
||||
|
||||
|
||||
#define HUNK_LINE 1024
|
||||
#define HUNK_FILE BUFSIZ
|
||||
|
||||
static int read_line (FILE *f) {
|
||||
/* equivalent to: return read_pattern(f, "[^\n]*{\n}"); */
|
||||
int n;
|
||||
char *b;
|
||||
do {
|
||||
b = luaL_openspace(HUNK_LINE);
|
||||
if (!fgets(b, HUNK_LINE, f)) return 0; /* read fails */
|
||||
n = strlen(b);
|
||||
luaL_addsize(n);
|
||||
} while (b[n-1] != '\n');
|
||||
luaL_addsize(-1); /* remove '\n' */
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static void read_file (FILE *f) {
|
||||
/* equivalent to: return read_pattern(f, ".*"); */
|
||||
int n;
|
||||
do {
|
||||
char *b = luaL_openspace(HUNK_FILE);
|
||||
n = fread(b, sizeof(char), HUNK_FILE, f);
|
||||
luaL_addsize(n);
|
||||
} while (n==HUNK_FILE);
|
||||
}
|
||||
|
||||
|
||||
static void io_read (void) {
|
||||
static char *options[] = {"*n", "*l", "*a", ".*", "*w", NULL};
|
||||
int arg = FIRSTARG;
|
||||
FILE *f = getfileparam(FINPUT, &arg);
|
||||
char *p = luaL_opt_string(arg++, "*l");
|
||||
do { /* repeat for each part */
|
||||
long l;
|
||||
int success;
|
||||
luaL_resetbuffer();
|
||||
switch (luaL_findstring(p, options)) {
|
||||
case 0: /* number */
|
||||
if (!read_number(f)) return; /* read fails */
|
||||
continue; /* number is already pushed; avoid the "pushstring" */
|
||||
case 1: /* line */
|
||||
success = read_line(f);
|
||||
break;
|
||||
case 2: case 3: /* file */
|
||||
read_file(f);
|
||||
success = 1; /* always success */
|
||||
break;
|
||||
case 4: /* word */
|
||||
success = read_pattern(f, "{%s*}%S%S*");
|
||||
break;
|
||||
default:
|
||||
success = read_pattern(f, p);
|
||||
}
|
||||
l = luaL_getsize();
|
||||
if (!success && l==0) return; /* read fails */
|
||||
lua_pushlstring(luaL_buffer(), l);
|
||||
} while ((p = luaL_opt_string(arg++, NULL)) != NULL);
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
static void io_write (void) {
|
||||
int arg = FIRSTARG;
|
||||
FILE *f = getfileparam(FOUTPUT, &arg);
|
||||
int status = 1;
|
||||
char *s;
|
||||
long l;
|
||||
while ((s = luaL_opt_lstr(arg++, NULL, &l)) != NULL)
|
||||
status = status && ((long)fwrite(s, 1, l, f) == l);
|
||||
pushresult(status);
|
||||
}
|
||||
|
||||
|
||||
static void io_seek (void) {
|
||||
static int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
|
||||
static char *modenames[] = {"set", "cur", "end", NULL};
|
||||
FILE *f = getnonullfile(FIRSTARG);
|
||||
int op = luaL_findstring(luaL_opt_string(FIRSTARG+1, "cur"), modenames);
|
||||
long offset = luaL_opt_long(FIRSTARG+2, 0);
|
||||
luaL_arg_check(op != -1, FIRSTARG+1, "invalid mode");
|
||||
op = fseek(f, offset, mode[op]);
|
||||
if (op)
|
||||
pushresult(0); /* error */
|
||||
else
|
||||
lua_pushnumber(ftell(f));
|
||||
}
|
||||
|
||||
|
||||
static void io_flush (void) {
|
||||
FILE *f = getfile(FIRSTARG);
|
||||
luaL_arg_check(f || lua_getparam(FIRSTARG) == LUA_NOOBJECT, FIRSTARG,
|
||||
"invalid file handle");
|
||||
pushresult(fflush(f) == 0);
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** Other O.S. Operations
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
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_clock (void) {
|
||||
lua_pushnumber(((double)clock())/CLOCKS_PER_SEC);
|
||||
}
|
||||
|
||||
|
||||
static void io_date (void) {
|
||||
char b[256];
|
||||
char *s = luaL_opt_string(1, "%c");
|
||||
struct tm *tm;
|
||||
time_t t;
|
||||
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};
|
||||
static char *catnames[] = {"all", "collate", "ctype", "monetary",
|
||||
"numeric", "time", NULL};
|
||||
int op = luaL_findstring(luaL_opt_string(2, "all"), catnames);
|
||||
luaL_arg_check(op != -1, 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) {
|
||||
for (;;) {
|
||||
char buffer[250];
|
||||
fprintf(stderr, "lua_debug> ");
|
||||
if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
|
||||
strcmp(buffer, "cont\n") == 0)
|
||||
return;
|
||||
lua_dostring(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#define MESSAGESIZE 150
|
||||
#define MAXMESSAGE (MESSAGESIZE*10)
|
||||
|
||||
|
||||
#define MAXSRC 40
|
||||
|
||||
|
||||
static void errorfb (void) {
|
||||
char buff[MAXMESSAGE];
|
||||
int level = 1; /* skip level 0 (it's this function) */
|
||||
lua_Object func;
|
||||
sprintf(buff, "lua error: %.200s\n", lua_getstring(lua_getparam(1)));
|
||||
while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT) {
|
||||
char *name;
|
||||
int currentline;
|
||||
char *chunkname;
|
||||
char buffchunk[MAXSRC];
|
||||
int linedefined;
|
||||
lua_funcinfo(func, &chunkname, &linedefined);
|
||||
luaL_chunkid(buffchunk, chunkname, sizeof(buffchunk));
|
||||
if (level == 2) strcat(buff, "Active Stack:\n");
|
||||
strcat(buff, "\t");
|
||||
if (strlen(buff) > MAXMESSAGE-MESSAGESIZE) {
|
||||
strcat(buff, "...\n");
|
||||
break; /* buffer is full */
|
||||
}
|
||||
switch (*lua_getobjname(func, &name)) {
|
||||
case 'g':
|
||||
sprintf(buff+strlen(buff), "function `%.50s'", name);
|
||||
break;
|
||||
case 't':
|
||||
sprintf(buff+strlen(buff), "`%.50s' tag method", name);
|
||||
break;
|
||||
default: {
|
||||
if (linedefined == 0)
|
||||
sprintf(buff+strlen(buff), "main of %.50s", buffchunk);
|
||||
else if (linedefined < 0)
|
||||
sprintf(buff+strlen(buff), "%.50s", buffchunk);
|
||||
else
|
||||
sprintf(buff+strlen(buff), "function <%d:%.50s>",
|
||||
linedefined, buffchunk);
|
||||
chunkname = NULL;
|
||||
}
|
||||
}
|
||||
if ((currentline = lua_currentline(func)) > 0)
|
||||
sprintf(buff+strlen(buff), " at line %d", currentline);
|
||||
if (chunkname)
|
||||
sprintf(buff+strlen(buff), " [%.50s]", buffchunk);
|
||||
strcat(buff, "\n");
|
||||
}
|
||||
func = lua_rawgetglobal("_ALERT");
|
||||
if (lua_isfunction(func)) { /* avoid error loop if _ALERT is not defined */
|
||||
lua_pushstring(buff);
|
||||
lua_callfunction(func);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static struct luaL_reg iolib[] = {
|
||||
{"_ERRORMESSAGE", errorfb},
|
||||
{"clock", io_clock},
|
||||
{"date", io_date},
|
||||
{"debug", io_debug},
|
||||
{"execute", io_execute},
|
||||
{"exit", io_exit},
|
||||
{"getenv", io_getenv},
|
||||
{"remove", io_remove},
|
||||
{"rename", io_rename},
|
||||
{"setlocale", setloc},
|
||||
{"tmpname", io_tmpname}
|
||||
};
|
||||
|
||||
|
||||
static struct luaL_reg iolibtag[] = {
|
||||
{"appendto", io_appendto},
|
||||
{"closefile", io_close},
|
||||
{"flush", io_flush},
|
||||
{"openfile", io_open},
|
||||
{"read", io_read},
|
||||
{"readfrom", io_readfrom},
|
||||
{"seek", io_seek},
|
||||
{"write", io_write},
|
||||
{"writeto", io_writeto}
|
||||
};
|
||||
|
||||
|
||||
static void openwithtags (void) {
|
||||
int i;
|
||||
int iotag = lua_newtag();
|
||||
lua_newtag(); /* alloc CLOSEDTAG: assume that CLOSEDTAG = iotag-1 */
|
||||
for (i=0; i<sizeof(iolibtag)/sizeof(iolibtag[0]); i++) {
|
||||
/* put iotag as upvalue for these functions */
|
||||
lua_pushnumber(iotag);
|
||||
lua_pushcclosure(iolibtag[i].func, 1);
|
||||
lua_setglobal(iolibtag[i].name);
|
||||
}
|
||||
/* predefined file handles */
|
||||
setfile(stdin, FINPUT, iotag);
|
||||
setfile(stdout, FOUTPUT, iotag);
|
||||
setfile(stdin, "_STDIN", iotag);
|
||||
setfile(stdout, "_STDOUT", iotag);
|
||||
setfile(stderr, "_STDERR", iotag);
|
||||
/* close file when collected */
|
||||
lua_pushnumber(iotag);
|
||||
lua_pushcclosure(gc_close, 1);
|
||||
lua_settagmethod(iotag, "gc");
|
||||
}
|
||||
|
||||
void lua_iolibopen (void) {
|
||||
/* register lib functions */
|
||||
luaL_openlib(iolib, (sizeof(iolib)/sizeof(iolib[0])));
|
||||
openwithtags();
|
||||
}
|
||||
|
||||
440
llex.c
Normal file
440
llex.c
Normal file
@@ -0,0 +1,440 @@
|
||||
/*
|
||||
** $Id: llex.c,v 1.33 1999/03/11 18:59:19 roberto Exp roberto $
|
||||
** Lexical Analyzer
|
||||
** 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 "luadebug.h"
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
|
||||
#define next(LS) (LS->current = zgetc(LS->lex_z))
|
||||
|
||||
|
||||
#define save(c) luaL_addchar(c)
|
||||
#define save_and_next(LS) (save(LS->current), next(LS))
|
||||
|
||||
|
||||
char *reserved [] = {"and", "do", "else", "elseif", "end", "function",
|
||||
"if", "local", "nil", "not", "or", "repeat", "return", "then",
|
||||
"until", "while"};
|
||||
|
||||
|
||||
void luaX_init (void) {
|
||||
int i;
|
||||
for (i=0; i<(sizeof(reserved)/sizeof(reserved[0])); i++) {
|
||||
TaggedString *ts = luaS_new(reserved[i]);
|
||||
ts->head.marked = FIRST_RESERVED+i; /* reserved word (always > 255) */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#define MAXSRC 40
|
||||
|
||||
void luaX_syntaxerror (LexState *ls, char *s, char *token) {
|
||||
char buff[MAXSRC];
|
||||
luaL_chunkid(buff, zname(ls->lex_z), sizeof(buff));
|
||||
if (token[0] == '\0')
|
||||
token = "<eof>";
|
||||
luaL_verror("%.100s;\n last token read: `%.50s' at line %d in %.50s",
|
||||
s, token, ls->linenumber, buff);
|
||||
}
|
||||
|
||||
|
||||
void luaX_error (LexState *ls, char *s) {
|
||||
save('\0');
|
||||
luaX_syntaxerror(ls, s, luaL_buffer());
|
||||
}
|
||||
|
||||
|
||||
void luaX_token2str (int token, char *s) {
|
||||
if (token < 255) {
|
||||
s[0] = (char)token;
|
||||
s[1] = '\0';
|
||||
}
|
||||
else
|
||||
strcpy(s, reserved[token-FIRST_RESERVED]);
|
||||
}
|
||||
|
||||
|
||||
static void luaX_invalidchar (LexState *ls, int c) {
|
||||
char buff[10];
|
||||
sprintf(buff, "0x%02X", c);
|
||||
luaX_syntaxerror(ls, "invalid control char", buff);
|
||||
}
|
||||
|
||||
|
||||
static void firstline (LexState *LS)
|
||||
{
|
||||
int c = zgetc(LS->lex_z);
|
||||
if (c == '#')
|
||||
while ((c=zgetc(LS->lex_z)) != '\n' && c != EOZ) /* skip first line */;
|
||||
zungetc(LS->lex_z);
|
||||
}
|
||||
|
||||
|
||||
void luaX_setinput (LexState *LS, ZIO *z)
|
||||
{
|
||||
LS->current = '\n';
|
||||
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;
|
||||
LS->fs = NULL;
|
||||
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 (LexState *LS, char *buff)
|
||||
{
|
||||
static char *opts[] = {"nil", "1", NULL};
|
||||
int i = luaL_findstring(buff, opts);
|
||||
if (i >= 0) return i;
|
||||
else if (isalpha((unsigned char)buff[0]) || buff[0] == '_')
|
||||
return luaS_globaldefined(buff);
|
||||
else {
|
||||
luaX_syntaxerror(LS, "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;
|
||||
luaX_syntaxerror(LS, "pragma too long", buff);
|
||||
}
|
||||
buff[i++] = (char)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)
|
||||
luaX_error(LS, "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 (luaL_findstring(buff, pragmas)) {
|
||||
case 0: /* debug */
|
||||
if (!skip) L->debug = 1;
|
||||
break;
|
||||
case 1: /* nodebug */
|
||||
if (!skip) L->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)
|
||||
luaX_syntaxerror(LS, "unmatched $end", "$end");
|
||||
break;
|
||||
case 4: /* ifnot */
|
||||
ifnot = 1;
|
||||
/* go through */
|
||||
case 5: /* if */
|
||||
if (LS->iflevel == MAX_IFS-1)
|
||||
luaX_syntaxerror(LS, "too many nested $ifs", "$if");
|
||||
readname(LS, buff);
|
||||
LS->iflevel++;
|
||||
LS->ifstate[LS->iflevel].elsepart = 0;
|
||||
LS->ifstate[LS->iflevel].condition = checkcond(LS, buff) ? !ifnot : ifnot;
|
||||
LS->ifstate[LS->iflevel].skip = skip || !LS->ifstate[LS->iflevel].condition;
|
||||
break;
|
||||
case 6: /* else */
|
||||
if (LS->ifstate[LS->iflevel].elsepart)
|
||||
luaX_syntaxerror(LS, "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:
|
||||
luaX_syntaxerror(LS, "unknown pragma", buff);
|
||||
}
|
||||
skipspace(LS);
|
||||
if (LS->current == '\n') /* pragma must end with a '\n' ... */
|
||||
inclinenumber(LS);
|
||||
else if (LS->current != EOZ) /* or eof */
|
||||
luaX_syntaxerror(LS, "invalid pragma format", buff);
|
||||
ifskip(LS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** =======================================================
|
||||
** LEXICAL ANALIZER
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
|
||||
|
||||
static int read_long_string (LexState *LS) {
|
||||
int cont = 0;
|
||||
for (;;) {
|
||||
switch (LS->current) {
|
||||
case EOZ:
|
||||
luaX_error(LS, "unfinished long string");
|
||||
return EOS; /* to avoid warnings */
|
||||
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); /* skip the second ']' */
|
||||
LS->seminfo.ts = luaS_newlstr(L->Mbuffer+(L->Mbuffbase+2),
|
||||
L->Mbuffnext-L->Mbuffbase-4);
|
||||
return STRING;
|
||||
}
|
||||
|
||||
|
||||
int luaX_lex (LexState *LS) {
|
||||
luaL_resetbuffer();
|
||||
for (;;) {
|
||||
switch (LS->current) {
|
||||
|
||||
case ' ': case '\t': case '\r': /* CR: to avoid problems with DOS */
|
||||
next(LS);
|
||||
continue;
|
||||
|
||||
case '\n':
|
||||
inclinenumber(LS);
|
||||
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);
|
||||
}
|
||||
|
||||
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':
|
||||
luaX_error(LS, "unfinished string");
|
||||
return EOS; /* to avoid warnings */
|
||||
case '\\':
|
||||
next(LS); /* do not save the '\' */
|
||||
switch (LS->current) {
|
||||
case 'a': save('\a'); next(LS); break;
|
||||
case 'b': save('\b'); next(LS); break;
|
||||
case 'f': save('\f'); next(LS); break;
|
||||
case 'n': save('\n'); next(LS); break;
|
||||
case 'r': save('\r'); next(LS); break;
|
||||
case 't': save('\t'); next(LS); break;
|
||||
case 'v': save('\v'); next(LS); break;
|
||||
case '\n': save('\n'); inclinenumber(LS); break;
|
||||
default : {
|
||||
if (isdigit(LS->current)) {
|
||||
int c = 0;
|
||||
int i = 0;
|
||||
do {
|
||||
c = 10*c + (LS->current-'0');
|
||||
next(LS);
|
||||
} while (++i<3 && isdigit(LS->current));
|
||||
if (c != (unsigned char)c)
|
||||
luaX_error(LS, "escape sequence too large");
|
||||
save(c);
|
||||
}
|
||||
else { /* handles \, ", ', and ? */
|
||||
save(LS->current);
|
||||
next(LS);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
save_and_next(LS);
|
||||
}
|
||||
}
|
||||
save_and_next(LS); /* skip delimiter */
|
||||
LS->seminfo.ts = luaS_newlstr(L->Mbuffer+(L->Mbuffbase+1),
|
||||
L->Mbuffnext-L->Mbuffbase-2);
|
||||
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 '.';
|
||||
goto fraction; /* LS->current is a digit: goes through to number */
|
||||
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9':
|
||||
do {
|
||||
save_and_next(LS);
|
||||
} while (isdigit(LS->current));
|
||||
if (LS->current == '.') {
|
||||
save_and_next(LS);
|
||||
if (LS->current == '.') {
|
||||
save('.');
|
||||
luaX_error(LS,
|
||||
"ambiguous syntax (decimal point x string concatenation)");
|
||||
}
|
||||
}
|
||||
fraction:
|
||||
while (isdigit(LS->current))
|
||||
save_and_next(LS);
|
||||
if (toupper(LS->current) == 'E') {
|
||||
save_and_next(LS); /* read 'E' */
|
||||
save_and_next(LS); /* read '+', '-' or first digit */
|
||||
while (isdigit(LS->current))
|
||||
save_and_next(LS);
|
||||
}
|
||||
save('\0');
|
||||
LS->seminfo.r = luaO_str2d(L->Mbuffer+L->Mbuffbase);
|
||||
if (LS->seminfo.r < 0)
|
||||
luaX_error(LS, "invalid numeric format");
|
||||
return NUMBER;
|
||||
|
||||
case EOZ:
|
||||
if (LS->iflevel > 0)
|
||||
luaX_error(LS, "input ends inside a $if");
|
||||
return EOS;
|
||||
|
||||
default:
|
||||
if (LS->current != '_' && !isalpha(LS->current)) {
|
||||
int c = LS->current;
|
||||
if (iscntrl(c))
|
||||
luaX_invalidchar(LS, c);
|
||||
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->Mbuffer+L->Mbuffbase);
|
||||
if (ts->head.marked >= FIRST_RESERVED)
|
||||
return ts->head.marked; /* reserved word */
|
||||
LS->seminfo.ts = ts;
|
||||
return NAME;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
62
llex.h
Normal file
62
llex.h
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
** $Id: llex.h,v 1.10 1998/07/24 18:02:38 roberto Exp roberto $
|
||||
** Lexical Analyzer
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef llex_h
|
||||
#define llex_h
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
#define FIRST_RESERVED 260
|
||||
|
||||
/* maximum length of a reserved word (+1 for terminal 0) */
|
||||
#define TOKEN_LEN 15
|
||||
|
||||
enum RESERVED {
|
||||
/* terminal symbols denoted by reserved words */
|
||||
AND = FIRST_RESERVED,
|
||||
DO, ELSE, ELSEIF, END, FUNCTION, IF, LOCAL, NIL, NOT, OR,
|
||||
REPEAT, RETURN, THEN, UNTIL, WHILE,
|
||||
/* other terminal symbols */
|
||||
NAME, CONC, DOTS, EQ, GE, LE, NE, NUMBER, STRING, EOS};
|
||||
|
||||
|
||||
#define MAX_IFS 5
|
||||
|
||||
/* "ifstate" keeps the state of each nested $if the lexical is dealing with. */
|
||||
|
||||
struct ifState {
|
||||
int elsepart; /* true if it's in the $else part */
|
||||
int condition; /* true if $if condition is true */
|
||||
int skip; /* true if part must be skipped */
|
||||
};
|
||||
|
||||
|
||||
typedef struct LexState {
|
||||
int current; /* look ahead character */
|
||||
int token; /* look ahead token */
|
||||
struct FuncState *fs; /* 'FuncState' is private for the parser */
|
||||
union {
|
||||
real r;
|
||||
TaggedString *ts;
|
||||
} seminfo; /* semantics information */
|
||||
struct zio *lex_z; /* input stream */
|
||||
int linenumber; /* input line counter */
|
||||
int iflevel; /* level of nested $if's (for lexical analysis) */
|
||||
struct ifState ifstate[MAX_IFS];
|
||||
} LexState;
|
||||
|
||||
|
||||
void luaX_init (void);
|
||||
void luaX_setinput (LexState *LS, ZIO *z);
|
||||
int luaX_lex (LexState *LS);
|
||||
void luaX_syntaxerror (LexState *ls, char *s, char *token);
|
||||
void luaX_error (LexState *ls, char *s);
|
||||
void luaX_token2str (int token, char *s);
|
||||
|
||||
|
||||
#endif
|
||||
202
lmathlib.c
Normal file
202
lmathlib.c
Normal file
@@ -0,0 +1,202 @@
|
||||
/*
|
||||
** $Id: lmathlib.c,v 1.15 1999/01/04 12:41:12 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"
|
||||
|
||||
|
||||
#define PI (3.14159265358979323846)
|
||||
#define RADIANS_PER_DEGREE (PI/180.0)
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** If you want Lua to operate in radians (instead of degrees),
|
||||
** define RADIANS
|
||||
*/
|
||||
#ifdef RADIANS
|
||||
#define FROMRAD(a) (a)
|
||||
#define TORAD(a) (a)
|
||||
#else
|
||||
#define FROMRAD(a) ((a)/RADIANS_PER_DEGREE)
|
||||
#define TORAD(a) ((a)*RADIANS_PER_DEGREE)
|
||||
#endif
|
||||
|
||||
|
||||
static void math_abs (void) {
|
||||
lua_pushnumber(fabs(luaL_check_number(1)));
|
||||
}
|
||||
|
||||
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)/RADIANS_PER_DEGREE);
|
||||
}
|
||||
|
||||
static void math_rad (void) {
|
||||
lua_pushnumber(luaL_check_number(1)*RADIANS_PER_DEGREE);
|
||||
}
|
||||
|
||||
static void math_frexp (void) {
|
||||
int e;
|
||||
lua_pushnumber(frexp(luaL_check_number(1), &e));
|
||||
lua_pushnumber(e);
|
||||
}
|
||||
|
||||
static void math_ldexp (void) {
|
||||
lua_pushnumber(ldexp(luaL_check_number(1), luaL_check_int(2)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
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 '%' avoids the (rare) case of r==1, and is needed also because on
|
||||
some systems (SunOS!) "rand()" may return a value bigger than RAND_MAX */
|
||||
double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX;
|
||||
int l = luaL_opt_int(1, 0);
|
||||
if (l == 0)
|
||||
lua_pushnumber(r);
|
||||
else {
|
||||
int u = luaL_opt_int(2, 0);
|
||||
if (u == 0) {
|
||||
u = l;
|
||||
l = 1;
|
||||
}
|
||||
luaL_arg_check(l<=u, 1, "interval is empty");
|
||||
lua_pushnumber((int)(r*(u-l+1))+l);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void math_randomseed (void) {
|
||||
srand(luaL_check_int(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},
|
||||
{"frexp", math_frexp},
|
||||
{"ldexp", math_ldexp},
|
||||
{"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_pushcfunction(math_pow);
|
||||
lua_pushnumber(0); /* to get its tag */
|
||||
lua_settagmethod(lua_tag(lua_pop()), "pow");
|
||||
lua_pushnumber(PI); lua_setglobal("PI");
|
||||
}
|
||||
|
||||
135
lmem.c
Normal file
135
lmem.c
Normal file
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
** $Id: lmem.c,v 1.13 1999/02/26 15:50:10 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"
|
||||
|
||||
|
||||
/*
|
||||
** real ANSI systems do not need these tests;
|
||||
** but some systems (Sun OS) are not that ANSI...
|
||||
*/
|
||||
#ifdef OLD_ANSI
|
||||
#define realloc(b,s) ((b) == NULL ? malloc(s) : (realloc)(b, s))
|
||||
#define free(b) if (b) (free)(b)
|
||||
#endif
|
||||
|
||||
|
||||
#define MINSIZE 16 /* minimum size for "growing" vectors */
|
||||
|
||||
|
||||
|
||||
#ifndef DEBUG
|
||||
|
||||
|
||||
static unsigned long power2 (unsigned long n) {
|
||||
unsigned long p = MINSIZE;
|
||||
while (p<=n) p<<=1;
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
void *luaM_growaux (void *block, unsigned long nelems, int inc, int size,
|
||||
char *errormsg, unsigned long limit) {
|
||||
unsigned long newn = nelems+inc;
|
||||
if ((newn ^ nelems) <= nelems || /* still the same power of 2 limit? */
|
||||
(nelems > 0 && newn < MINSIZE)) /* or block already is MINSIZE? */
|
||||
return block; /* do not need to reallocate */
|
||||
else { /* it crossed a power of 2 boundary; grow to next power */
|
||||
if (newn >= limit)
|
||||
lua_error(errormsg);
|
||||
newn = power2(newn);
|
||||
if (newn > limit)
|
||||
newn = limit;
|
||||
return luaM_realloc(block, newn*size);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** generic allocation routine.
|
||||
*/
|
||||
void *luaM_realloc (void *block, unsigned long size) {
|
||||
size_t s = (size_t)size;
|
||||
if (s != size)
|
||||
lua_error("memory allocation error: block too big");
|
||||
if (size == 0) {
|
||||
free(block); /* block may be NULL, that is OK for free */
|
||||
return NULL;
|
||||
}
|
||||
block = realloc(block, s);
|
||||
if (block == NULL)
|
||||
lua_error(memEM);
|
||||
return block;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#else
|
||||
/* DEBUG */
|
||||
|
||||
#include <string.h>
|
||||
|
||||
|
||||
void *luaM_growaux (void *block, unsigned long nelems, int inc, int size,
|
||||
char *errormsg, unsigned long limit) {
|
||||
unsigned long newn = nelems+inc;
|
||||
if (newn >= limit)
|
||||
lua_error(errormsg);
|
||||
return luaM_realloc(block, newn*size);
|
||||
}
|
||||
|
||||
|
||||
#define HEADER (sizeof(double))
|
||||
|
||||
#define MARK 55
|
||||
|
||||
unsigned long numblocks = 0;
|
||||
unsigned long totalmem = 0;
|
||||
|
||||
|
||||
static void *checkblock (void *block) {
|
||||
unsigned long *b = (unsigned long *)((char *)block - HEADER);
|
||||
unsigned long size = *b;
|
||||
LUA_ASSERT(*(((char *)b)+size+HEADER) == MARK,
|
||||
"corrupted block");
|
||||
numblocks--;
|
||||
totalmem -= size;
|
||||
return b;
|
||||
}
|
||||
|
||||
|
||||
void *luaM_realloc (void *block, unsigned long size) {
|
||||
unsigned long realsize = HEADER+size+1;
|
||||
if (realsize != (size_t)realsize)
|
||||
lua_error("memory allocation error: block too big");
|
||||
if (size == 0) {
|
||||
if (block) {
|
||||
unsigned long *b = (unsigned long *)((char *)block - HEADER);
|
||||
memset(block, -1, *b); /* erase block */
|
||||
block = checkblock(block);
|
||||
}
|
||||
free(block);
|
||||
return NULL;
|
||||
}
|
||||
if (block)
|
||||
block = checkblock(block);
|
||||
block = (unsigned long *)realloc(block, realsize);
|
||||
if (block == NULL)
|
||||
lua_error(memEM);
|
||||
totalmem += size;
|
||||
numblocks++;
|
||||
*(unsigned long *)block = size;
|
||||
*(((char *)block)+size+HEADER) = MARK;
|
||||
return (unsigned long *)((char *)block+HEADER);
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
41
lmem.h
Normal file
41
lmem.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
** $Id: lmem.h,v 1.7 1999/02/25 15:16:26 roberto Exp roberto $
|
||||
** Interface to Memory Manager
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lmem_h
|
||||
#define lmem_h
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
/* 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"
|
||||
#define arrEM "internal array bigger than `int' limit"
|
||||
|
||||
void *luaM_realloc (void *oldblock, unsigned long size);
|
||||
void *luaM_growaux (void *block, unsigned long nelems, int inc, 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(v,nelems,inc,t,e,l) \
|
||||
((v)=(t *)luaM_growaux(v,nelems,inc,sizeof(t),e,l))
|
||||
#define luaM_reallocvector(v,n,t) ((v)=(t *)luaM_realloc(v,(n)*sizeof(t)))
|
||||
|
||||
|
||||
#ifdef DEBUG
|
||||
extern unsigned long numblocks;
|
||||
extern unsigned long totalmem;
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
129
lobject.c
Normal file
129
lobject.c
Normal file
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
** $Id: lobject.c,v 1.18 1999/02/26 15:48:30 roberto Exp roberto $
|
||||
** Some generic functions over Lua objects
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdlib.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("tableEM");
|
||||
return 0; /* to avoid warnings */
|
||||
}
|
||||
|
||||
|
||||
int luaO_equalval (TObject *t1, TObject *t2) {
|
||||
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_INTERNALERROR("invalid type");
|
||||
return 0; /* UNREACHABLE */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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) {
|
||||
while (size--)
|
||||
((char *)dest)[size]=((char *)src)[size];
|
||||
}
|
||||
|
||||
void luaO_memdown (void *dest, void *src, int size) {
|
||||
int i;
|
||||
for (i=0; i<size; i++)
|
||||
((char *)dest)[i]=((char *)src)[i];
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
static double expten (unsigned int e) {
|
||||
double exp = 10.0;
|
||||
double res = 1.0;
|
||||
for (; e; e>>=1) {
|
||||
if (e & 1) res *= exp;
|
||||
exp *= exp;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
double luaO_str2d (char *s) { /* LUA_NUMBER */
|
||||
double a = 0.0;
|
||||
int point = 0;
|
||||
while (isdigit((unsigned char)*s)) {
|
||||
a = 10.0*a + (*(s++)-'0');
|
||||
}
|
||||
if (*s == '.') {
|
||||
s++;
|
||||
while (isdigit((unsigned char)*s)) {
|
||||
a = 10.0*a + (*(s++)-'0');
|
||||
point++;
|
||||
}
|
||||
}
|
||||
if (toupper((unsigned char)*s) == 'E') {
|
||||
int e = 0;
|
||||
int sig = 1;
|
||||
s++;
|
||||
if (*s == '-') {
|
||||
s++;
|
||||
sig = -1;
|
||||
}
|
||||
else if (*s == '+') s++;
|
||||
if (!isdigit((unsigned char)*s)) return -1; /* no digit in the exponent? */
|
||||
do {
|
||||
e = 10*e + (*(s++)-'0');
|
||||
} while (isdigit((unsigned char)*s));
|
||||
point -= sig*e;
|
||||
}
|
||||
while (isspace((unsigned char)*s)) s++;
|
||||
if (*s != '\0') return -1; /* invalid trailing characters? */
|
||||
if (point > 0)
|
||||
a /= expten(point);
|
||||
else if (point < 0)
|
||||
a *= expten(-point);
|
||||
return a;
|
||||
}
|
||||
|
||||
204
lobject.h
Normal file
204
lobject.h
Normal file
@@ -0,0 +1,204 @@
|
||||
/*
|
||||
** $Id: lobject.h,v 1.27 1999/03/04 21:17:26 roberto Exp roberto $
|
||||
** Type definitions for Lua objects
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lobject_h
|
||||
#define lobject_h
|
||||
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
#ifdef DEBUG
|
||||
#include "lauxlib.h"
|
||||
#define LUA_INTERNALERROR(s) \
|
||||
luaL_verror("INTERNAL ERROR - %s [%s:%d]",(s),__FILE__,__LINE__)
|
||||
#define LUA_ASSERT(c,s) { if (!(c)) LUA_INTERNALERROR(s); }
|
||||
#else
|
||||
#define LUA_INTERNALERROR(s) /* empty */
|
||||
#define LUA_ASSERT(c,s) /* empty */
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
** "real" is the type "number" of Lua
|
||||
** GREP LUA_NUMBER to change that
|
||||
*/
|
||||
#ifndef LUA_NUM_TYPE
|
||||
#define LUA_NUM_TYPE double
|
||||
#endif
|
||||
|
||||
|
||||
typedef LUA_NUM_TYPE real;
|
||||
|
||||
#define Byte lua_Byte /* some systems have Byte as a predefined type */
|
||||
typedef unsigned char Byte; /* unsigned 8 bits */
|
||||
|
||||
|
||||
#define MAX_INT (INT_MAX-2) /* maximum value of an 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_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;
|
||||
unsigned long hash;
|
||||
int constindex; /* hint to reuse constants (= -1 if this is a userdata) */
|
||||
union {
|
||||
struct {
|
||||
TObject globalval;
|
||||
long len; /* if this is a string, here is its length */
|
||||
} s;
|
||||
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 *source;
|
||||
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[];
|
||||
|
||||
#define luaO_typename(o) luaO_typenames[-ttype(o)]
|
||||
|
||||
|
||||
extern TObject luaO_nilobject;
|
||||
|
||||
#define luaO_equalObj(t1,t2) ((ttype(t1) != ttype(t2)) ? 0 \
|
||||
: luaO_equalval(t1,t2))
|
||||
int luaO_equalval (TObject *t1, TObject *t2);
|
||||
int luaO_redimension (int oldsize);
|
||||
void luaO_insertlist (GCnode *root, GCnode *node);
|
||||
double luaO_str2d (char *s);
|
||||
|
||||
#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
|
||||
138
lopcodes.h
Normal file
138
lopcodes.h
Normal file
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
** $Id: lopcodes.h,v 1.31 1999/03/05 21:16:07 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 word parameter, then with byte parameter.
|
||||
*/
|
||||
|
||||
|
||||
typedef enum {
|
||||
/* name parm before after side effect
|
||||
-----------------------------------------------------------------------------*/
|
||||
ENDCODE,/* - - (return) */
|
||||
RETCODE,/* b - (return) */
|
||||
|
||||
CALL,/* b c v_c...v_1 f r_b...r_1 f(v1,...,v_c) */
|
||||
|
||||
TAILCALL,/* b c v_c...v_1 f (return) f(v1,...,v_c) */
|
||||
|
||||
PUSHNIL,/* b - nil_0...nil_b */
|
||||
POP,/* b a_b...a_1 - */
|
||||
|
||||
PUSHNUMBERW,/* w - (float)w */
|
||||
PUSHNUMBER,/* b - (float)b */
|
||||
|
||||
PUSHNUMBERNEGW,/* w - (float)-w */
|
||||
PUSHNUMBERNEG,/* b - (float)-b */
|
||||
|
||||
PUSHCONSTANTW,/*w - CNST[w] */
|
||||
PUSHCONSTANT,/* b - CNST[b] */
|
||||
|
||||
PUSHUPVALUE,/* b - Closure[b] */
|
||||
|
||||
PUSHLOCAL,/* b - LOC[b] */
|
||||
|
||||
GETGLOBALW,/* w - VAR[CNST[w]] */
|
||||
GETGLOBAL,/* b - VAR[CNST[b]] */
|
||||
|
||||
GETTABLE,/* - i t t[i] */
|
||||
|
||||
GETDOTTEDW,/* w t t[CNST[w]] */
|
||||
GETDOTTED,/* b t t[CNST[b]] */
|
||||
|
||||
PUSHSELFW,/* w t t t[CNST[w]] */
|
||||
PUSHSELF,/* b t t t[CNST[b]] */
|
||||
|
||||
CREATEARRAYW,/* w - newarray(size = w) */
|
||||
CREATEARRAY,/* b - newarray(size = b) */
|
||||
|
||||
SETLOCAL,/* b x - LOC[b]=x */
|
||||
|
||||
SETGLOBALW,/* w x - VAR[CNST[w]]=x */
|
||||
SETGLOBAL,/* b x - VAR[CNST[b]]=x */
|
||||
|
||||
SETTABLEPOP,/* - v i t - t[i]=v */
|
||||
|
||||
SETTABLE,/* b v a_b...a_1 i t a_b...a_1 i t t[i]=v */
|
||||
|
||||
SETLISTW,/* w c v_c...v_1 t t t[i+w*FPF]=v_i */
|
||||
SETLIST,/* b c v_c...v_1 t t t[i+b*FPF]=v_i */
|
||||
|
||||
SETMAP,/* b v_b k_b ...v_0 k_0 t t t[k_i]=v_i */
|
||||
|
||||
NEQOP,/* - y x (x~=y)? 1 : nil */
|
||||
EQOP,/* - 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 */
|
||||
|
||||
ONTJMPW,/* w x (x!=nil)? x : - (x!=nil)? PC+=w */
|
||||
ONTJMP,/* b x (x!=nil)? x : - (x!=nil)? PC+=b */
|
||||
ONFJMPW,/* w x (x==nil)? x : - (x==nil)? PC+=w */
|
||||
ONFJMP,/* b x (x==nil)? x : - (x==nil)? PC+=b */
|
||||
JMPW,/* w - - PC+=w */
|
||||
JMP,/* b - - PC+=b */
|
||||
IFFJMPW,/* w x - (x==nil)? PC+=w */
|
||||
IFFJMP,/* b x - (x==nil)? PC+=b */
|
||||
IFTUPJMPW,/* w x - (x!=nil)? PC-=w */
|
||||
IFTUPJMP,/* b x - (x!=nil)? PC-=b */
|
||||
IFFUPJMPW,/* w x - (x==nil)? PC-=w */
|
||||
IFFUPJMP,/* b x - (x==nil)? PC-=b */
|
||||
|
||||
CLOSUREW,/* w c v_c...v_1 closure(CNST[w], v_c...v_1) */
|
||||
CLOSURE,/* b c v_c...v_1 closure(CNST[b], v_c...v_1) */
|
||||
|
||||
SETLINEW,/* w - - LINE=w */
|
||||
SETLINE,/* b - - LINE=b */
|
||||
|
||||
LONGARGW,/* w (add w*(1<<16) to arg of next instruction) */
|
||||
LONGARG,/* b (add b*(1<<16) to arg of next instruction) */
|
||||
|
||||
CHECKSTACK /* b (assert #temporaries == b; only for internal debuging!) */
|
||||
|
||||
} OpCode;
|
||||
|
||||
|
||||
#define RFIELDS_PER_FLUSH 32 /* records (SETMAP) */
|
||||
#define LFIELDS_PER_FLUSH 64 /* FPF - lists (SETLIST) */
|
||||
|
||||
#define ZEROVARARG 64
|
||||
|
||||
|
||||
/* maximum value of an arg of 3 bytes; must fit in an "int" */
|
||||
#if MAX_INT < (1<<24)
|
||||
#define MAX_ARG MAX_INT
|
||||
#else
|
||||
#define MAX_ARG ((1<<24)-1)
|
||||
#endif
|
||||
|
||||
/* maximum value of a word of 2 bytes; cannot be bigger than MAX_ARG */
|
||||
#if MAX_ARG < (1<<16)
|
||||
#define MAX_WORD MAX_ARG
|
||||
#else
|
||||
#define MAX_WORD ((1<<16)-1)
|
||||
#endif
|
||||
|
||||
|
||||
/* maximum value of a byte */
|
||||
#define MAX_BYTE ((1<<8)-1)
|
||||
|
||||
|
||||
#endif
|
||||
20
lparser.h
Normal file
20
lparser.h
Normal file
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
** $Id: lparser.h,v 1.2 1997/12/22 20:57:18 roberto Exp roberto $
|
||||
** LL(1) Parser and code generator for Lua
|
||||
** 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
|
||||
84
lstate.c
Normal file
84
lstate.c
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
** $Id: lstate.c,v 1.9 1999/02/25 15:17:01 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"
|
||||
|
||||
|
||||
lua_State *lua_state = NULL;
|
||||
|
||||
|
||||
void lua_open (void)
|
||||
{
|
||||
if (lua_state) return;
|
||||
lua_state = luaM_new(lua_State);
|
||||
L->Cstack.base = 0;
|
||||
L->Cstack.lua2C = 0;
|
||||
L->Cstack.num = 0;
|
||||
L->errorJmp = NULL;
|
||||
L->Mbuffer = NULL;
|
||||
L->Mbuffbase = 0;
|
||||
L->Mbuffsize = 0;
|
||||
L->Mbuffnext = 0;
|
||||
L->numCblocks = 0;
|
||||
L->debug = 0;
|
||||
L->callhook = NULL;
|
||||
L->linehook = 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->IMtable = NULL;
|
||||
L->refArray = NULL;
|
||||
L->refSize = 0;
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
86
lstate.h
Normal file
86
lstate.h
Normal file
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
** $Id: lstate.h,v 1.15 1999/02/25 15:17:01 roberto Exp roberto $
|
||||
** Global State
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lstate_h
|
||||
#define lstate_h
|
||||
|
||||
#include <setjmp.h>
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lua.h"
|
||||
#include "luadebug.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;
|
||||
|
||||
|
||||
enum Status {LOCK, HOLD, FREE, COLLECTED};
|
||||
|
||||
struct ref {
|
||||
TObject o;
|
||||
enum Status status;
|
||||
};
|
||||
|
||||
|
||||
struct lua_State {
|
||||
/* thread-specific state */
|
||||
struct Stack stack; /* Lua stack */
|
||||
struct C_Lua_Stack Cstack; /* C2lua struct */
|
||||
jmp_buf *errorJmp; /* current error recover point */
|
||||
char *Mbuffer; /* global buffer */
|
||||
int 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 */
|
||||
int debug;
|
||||
lua_CHFunction callhook;
|
||||
lua_LHFunction linehook;
|
||||
/* global state */
|
||||
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 last_tag; /* last used tag in IMtable */
|
||||
struct ref *refArray; /* locked objects */
|
||||
int refSize; /* size of refArray */
|
||||
unsigned long GCthreshold;
|
||||
unsigned long nblocks; /* number of 'blocks' currently allocated */
|
||||
};
|
||||
|
||||
|
||||
#define L lua_state
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
298
lstring.c
Normal file
298
lstring.c
Normal file
@@ -0,0 +1,298 @@
|
||||
/*
|
||||
** $Id: lstring.c,v 1.18 1999/02/08 16:28:48 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_HASHSTR 31
|
||||
#define NUM_HASHUDATA 31
|
||||
#define NUM_HASHS (NUM_HASHSTR+NUM_HASHUDATA)
|
||||
|
||||
|
||||
#define gcsizestring(l) (1+(l/64)) /* "weight" for a string with length 'l' */
|
||||
|
||||
|
||||
|
||||
static TaggedString EMPTY = {{NULL, 2}, 0L, 0,
|
||||
{{{LUA_T_NIL, {NULL}}, 0L}}, {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_s (char *s, long l) {
|
||||
unsigned long h = 0; /* seed */
|
||||
while (l--)
|
||||
h = h ^ ((h<<5)+(h>>2)+(unsigned char)*(s++));
|
||||
return h;
|
||||
}
|
||||
|
||||
static int newsize (stringtable *tb) {
|
||||
int size = tb->size;
|
||||
int realuse = 0;
|
||||
int i;
|
||||
/* count how many entries are really in use */
|
||||
for (i=0; i<size; i++)
|
||||
if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY)
|
||||
realuse++;
|
||||
return luaO_redimension((realuse+1)*2); /* +1 is the new element */
|
||||
}
|
||||
|
||||
|
||||
static void grow (stringtable *tb) {
|
||||
int ns = newsize(tb);
|
||||
TaggedString **newhash = luaM_newvector(ns, TaggedString *);
|
||||
int i;
|
||||
for (i=0; i<ns; i++)
|
||||
newhash[i] = NULL;
|
||||
/* rehash */
|
||||
tb->nuse = 0;
|
||||
for (i=0; i<tb->size; i++) {
|
||||
if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY) {
|
||||
unsigned long h = tb->hash[i]->hash;
|
||||
int h1 = h%ns;
|
||||
while (newhash[h1]) {
|
||||
h1 += (h&(ns-2)) + 1; /* double hashing */
|
||||
if (h1 >= ns) h1 -= ns;
|
||||
}
|
||||
newhash[h1] = tb->hash[i];
|
||||
tb->nuse++;
|
||||
}
|
||||
}
|
||||
luaM_free(tb->hash);
|
||||
tb->size = ns;
|
||||
tb->hash = newhash;
|
||||
}
|
||||
|
||||
|
||||
static TaggedString *newone_s (char *str, long l, unsigned long h) {
|
||||
TaggedString *ts = (TaggedString *)luaM_malloc(sizeof(TaggedString)+l);
|
||||
memcpy(ts->str, str, l);
|
||||
ts->str[l] = 0; /* ending 0 */
|
||||
ts->u.s.globalval.ttype = LUA_T_NIL; /* initialize global value */
|
||||
ts->u.s.len = l;
|
||||
ts->constindex = 0;
|
||||
L->nblocks += gcsizestring(l);
|
||||
ts->head.marked = 0;
|
||||
ts->head.next = (GCnode *)ts; /* signal it is in no list */
|
||||
ts->hash = h;
|
||||
return ts;
|
||||
}
|
||||
|
||||
static TaggedString *newone_u (char *buff, int tag, unsigned long h) {
|
||||
TaggedString *ts = luaM_new(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_s (char *str, long l, stringtable *tb) {
|
||||
TaggedString *ts;
|
||||
unsigned long h = hash_s(str, l);
|
||||
int size = tb->size;
|
||||
int j = -1;
|
||||
int h1;
|
||||
if ((long)tb->nuse*3 >= (long)size*2) {
|
||||
grow(tb);
|
||||
size = tb->size;
|
||||
}
|
||||
h1 = h%size;
|
||||
while ((ts = tb->hash[h1]) != NULL) {
|
||||
if (ts == &EMPTY)
|
||||
j = h1;
|
||||
else if (ts->u.s.len == l && (memcmp(str, ts->str, l) == 0))
|
||||
return ts;
|
||||
h1 += (h&(size-2)) + 1; /* double hashing */
|
||||
if (h1 >= size) h1 -= size;
|
||||
}
|
||||
/* not found */
|
||||
if (j != -1) /* is there an EMPTY space? */
|
||||
h1 = j;
|
||||
else
|
||||
tb->nuse++;
|
||||
ts = tb->hash[h1] = newone_s(str, l, h);
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
static TaggedString *insert_u (void *buff, int tag, stringtable *tb) {
|
||||
TaggedString *ts;
|
||||
unsigned long h = (unsigned long)buff;
|
||||
int size = tb->size;
|
||||
int j = -1;
|
||||
int h1;
|
||||
if ((long)tb->nuse*3 >= (long)size*2) {
|
||||
grow(tb);
|
||||
size = tb->size;
|
||||
}
|
||||
h1 = h%size;
|
||||
while ((ts = tb->hash[h1]) != NULL) {
|
||||
if (ts == &EMPTY)
|
||||
j = h1;
|
||||
else if ((tag == ts->u.d.tag || tag == LUA_ANYTAG) && buff == ts->u.d.v)
|
||||
return ts;
|
||||
h1 += (h&(size-2)) + 1; /* double hashing */
|
||||
if (h1 >= size) h1 -= size;
|
||||
}
|
||||
/* not found */
|
||||
if (j != -1) /* is there an EMPTY space? */
|
||||
h1 = j;
|
||||
else
|
||||
tb->nuse++;
|
||||
ts = tb->hash[h1] = newone_u(buff, tag, h);
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
TaggedString *luaS_createudata (void *udata, int tag) {
|
||||
int t = ((unsigned)udata%NUM_HASHUDATA)+NUM_HASHSTR;
|
||||
return insert_u(udata, tag, &L->string_root[t]);
|
||||
}
|
||||
|
||||
TaggedString *luaS_newlstr (char *str, long l) {
|
||||
int t = (l==0) ? 0 : ((int)((unsigned char)str[0]*l))%NUM_HASHSTR;
|
||||
return insert_s(str, l, &L->string_root[t]);
|
||||
}
|
||||
|
||||
TaggedString *luaS_new (char *str) {
|
||||
return luaS_newlstr(str, strlen(str));
|
||||
}
|
||||
|
||||
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(l->u.s.len);
|
||||
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=NUM_HASHSTR; 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)
|
||||
continue;
|
||||
LUA_ASSERT(t->constindex == -1, "must be userdata");
|
||||
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.s.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.s.globalval))
|
||||
return g->str;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
int luaS_globaldefined (char *name) {
|
||||
TaggedString *ts = luaS_new(name);
|
||||
return ts->u.s.globalval.ttype != LUA_T_NIL;
|
||||
}
|
||||
|
||||
28
lstring.h
Normal file
28
lstring.h
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
** $Id: lstring.h,v 1.6 1997/12/01 20:31:25 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_newlstr (char *str, long 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
|
||||
549
lstrlib.c
Normal file
549
lstrlib.c
Normal file
@@ -0,0 +1,549 @@
|
||||
/*
|
||||
** $Id: lstrlib.c,v 1.27 1999/02/25 19:13:56 roberto Exp roberto $
|
||||
** 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);
|
||||
memcpy(b, s, n);
|
||||
luaL_addsize(n);
|
||||
}
|
||||
|
||||
|
||||
static void str_len (void)
|
||||
{
|
||||
long l;
|
||||
luaL_check_lstr(1, &l);
|
||||
lua_pushnumber(l);
|
||||
}
|
||||
|
||||
|
||||
static void closeandpush (void) {
|
||||
lua_pushlstring(luaL_buffer(), luaL_getsize());
|
||||
}
|
||||
|
||||
|
||||
static long posrelat (long pos, long len) {
|
||||
/* relative string position: negative means back from end */
|
||||
return (pos>=0) ? pos : len+pos+1;
|
||||
}
|
||||
|
||||
|
||||
static void str_sub (void) {
|
||||
long l;
|
||||
char *s = luaL_check_lstr(1, &l);
|
||||
long start = posrelat(luaL_check_long(2), l);
|
||||
long end = posrelat(luaL_opt_long(3, -1), l);
|
||||
if (start < 1) start = 1;
|
||||
if (end > l) end = l;
|
||||
if (start <= end)
|
||||
lua_pushlstring(s+start-1, end-start+1);
|
||||
else lua_pushstring("");
|
||||
}
|
||||
|
||||
|
||||
static void str_lower (void) {
|
||||
long l;
|
||||
int i;
|
||||
char *s = luaL_check_lstr(1, &l);
|
||||
luaL_resetbuffer();
|
||||
for (i=0; i<l; i++)
|
||||
luaL_addchar(tolower((unsigned char)(s[i])));
|
||||
closeandpush();
|
||||
}
|
||||
|
||||
|
||||
static void str_upper (void) {
|
||||
long l;
|
||||
int i;
|
||||
char *s = luaL_check_lstr(1, &l);
|
||||
luaL_resetbuffer();
|
||||
for (i=0; i<l; i++)
|
||||
luaL_addchar(toupper((unsigned char)(s[i])));
|
||||
closeandpush();
|
||||
}
|
||||
|
||||
static void str_rep (void)
|
||||
{
|
||||
long l;
|
||||
char *s = luaL_check_lstr(1, &l);
|
||||
int n = luaL_check_int(2);
|
||||
luaL_resetbuffer();
|
||||
while (n-- > 0)
|
||||
addnchar(s, l);
|
||||
closeandpush();
|
||||
}
|
||||
|
||||
|
||||
static void str_byte (void) {
|
||||
long l;
|
||||
char *s = luaL_check_lstr(1, &l);
|
||||
long pos = posrelat(luaL_opt_long(2, 1), l);
|
||||
luaL_arg_check(0<pos && pos<=l, 2, "out of range");
|
||||
lua_pushnumber((unsigned char)s[pos-1]);
|
||||
}
|
||||
|
||||
|
||||
static void str_char (void) {
|
||||
int i = 0;
|
||||
luaL_resetbuffer();
|
||||
while (lua_getparam(++i) != LUA_NOOBJECT) {
|
||||
double c = luaL_check_number(i);
|
||||
luaL_arg_check((unsigned char)c == c, i, "invalid value");
|
||||
luaL_addchar((unsigned char)c);
|
||||
}
|
||||
closeandpush();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
** PATTERN MATCHING
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
#define MAX_CAPT 9
|
||||
|
||||
struct Capture {
|
||||
char *src_end; /* end ('\0') of source string */
|
||||
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;
|
||||
if (l == -1) lua_error("unfinished capture");
|
||||
lua_pushlstring(cap->capture[i].init, l);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
switch (tolower(cl)) {
|
||||
case 'a' : res = isalpha(c); break;
|
||||
case 'c' : res = iscntrl(c); break;
|
||||
case 'd' : res = isdigit(c); break;
|
||||
case 'l' : res = islower(c); break;
|
||||
case 'p' : res = ispunct(c); break;
|
||||
case 's' : res = isspace(c); break;
|
||||
case 'u' : res = isupper(c); break;
|
||||
case 'w' : res = isalnum(c); break;
|
||||
case 'x' : res = isxdigit(c); break;
|
||||
case 'z' : res = (c == '\0'); break;
|
||||
default: return (cl == c);
|
||||
}
|
||||
return (islower(cl) ? res : !res);
|
||||
}
|
||||
|
||||
|
||||
int luaI_singlematch (int c, char *p, char **ep) {
|
||||
switch (*p) {
|
||||
case '.': /* matches any char */
|
||||
*ep = p+1;
|
||||
return 1;
|
||||
case '\0': /* end of pattern; matches nothing */
|
||||
*ep = p;
|
||||
return 0;
|
||||
case ESC:
|
||||
if (*(++p) == '\0')
|
||||
luaL_verror("incorrect pattern (ends with `%c')", ESC);
|
||||
*ep = p+1;
|
||||
return matchclass(c, (unsigned char)*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;
|
||||
while (++p < end) {
|
||||
if (*p == ESC) {
|
||||
if (((p+1) < end) && matchclass(c, (unsigned char)*++p))
|
||||
return sig;
|
||||
}
|
||||
else if ((*(p+1) == '-') && (p+2 < end)) {
|
||||
p+=2;
|
||||
if ((int)(unsigned char)*(p-2) <= c && c <= (int)(unsigned char)*p)
|
||||
return sig;
|
||||
}
|
||||
else if ((unsigned char)*p == c) return sig;
|
||||
}
|
||||
return !sig;
|
||||
}
|
||||
default:
|
||||
*ep = p+1;
|
||||
return ((unsigned char)*p == c);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static char *matchbalance (char *s, int b, int e, struct Capture *cap) {
|
||||
if (*s != b) return NULL;
|
||||
else {
|
||||
int cont = 1;
|
||||
while (++s < cap->src_end) {
|
||||
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);
|
||||
int len = cap->capture[l].len;
|
||||
*ep = p+1;
|
||||
if (cap->src_end-s >= len && memcmp(cap->capture[l].init, s, len) == 0)
|
||||
return s+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), cap);
|
||||
}
|
||||
else p--; /* and go through */
|
||||
}
|
||||
/* "luaI_singlematch" sets "ep" (so must be called even at the end of "s" */
|
||||
return (luaI_singlematch((unsigned char)*s, p, ep) && s<cap->src_end) ?
|
||||
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 == cap->src_end))
|
||||
return s;
|
||||
/* else go through */
|
||||
default: { /* it is a pattern item */
|
||||
char *ep; /* will point to what is next */
|
||||
char *s1 = matchitem(s, p, cap, &ep);
|
||||
switch (*ep) {
|
||||
case '*': { /* repetition */
|
||||
char *res;
|
||||
if (s1 && s1>s && ((res=match(s1, p, cap)) != NULL))
|
||||
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)) != NULL))
|
||||
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)) != NULL)
|
||||
return res;
|
||||
else if (s1 && s1>s) {
|
||||
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) {
|
||||
long l;
|
||||
char *s = luaL_check_lstr(1, &l);
|
||||
char *p = luaL_check_string(2);
|
||||
long init = posrelat(luaL_opt_long(3, 1), l) - 1;
|
||||
struct Capture cap;
|
||||
luaL_arg_check(0 <= init && init <= l, 3, "out of range");
|
||||
if (lua_getparam(4) != LUA_NOOBJECT ||
|
||||
strpbrk(p, SPECIALS) == NULL) { /* no special characters? */
|
||||
char *s2 = strstr(s+init, p);
|
||||
if (s2) {
|
||||
lua_pushnumber(s2-s+1);
|
||||
lua_pushnumber(s2-s+strlen(p));
|
||||
return;
|
||||
}
|
||||
}
|
||||
else {
|
||||
int anchor = (*p == '^') ? (p++, 1) : 0;
|
||||
char *s1=s+init;
|
||||
cap.src_end = s+l;
|
||||
do {
|
||||
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++<cap.src_end && !anchor);
|
||||
}
|
||||
lua_pushnil(); /* if arrives here, it didn't find */
|
||||
}
|
||||
|
||||
|
||||
static void add_s (lua_Object newp, struct Capture *cap) {
|
||||
if (lua_isstring(newp)) {
|
||||
char *news = lua_getstring(newp);
|
||||
int l = lua_strlen(newp);
|
||||
int i;
|
||||
for (i=0; i<l; i++) {
|
||||
if (news[i] != ESC)
|
||||
luaL_addchar(news[i]);
|
||||
else {
|
||||
i++; /* skip ESC */
|
||||
if (!isdigit((unsigned char)news[i]))
|
||||
luaL_addchar(news[i]);
|
||||
else {
|
||||
int level = check_cap(news[i], cap);
|
||||
addnchar(cap->capture[level].init, cap->capture[level].len);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else { /* is a function */
|
||||
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);
|
||||
if (lua_isstring(res))
|
||||
addnchar(lua_getstring(res), lua_strlen(res));
|
||||
lua_endblock();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void str_gsub (void) {
|
||||
long srcl;
|
||||
char *src = luaL_check_lstr(1, &srcl);
|
||||
char *p = luaL_check_string(2);
|
||||
lua_Object newp = lua_getparam(3);
|
||||
int max_s = luaL_opt_int(4, srcl+1);
|
||||
int anchor = (*p == '^') ? (p++, 1) : 0;
|
||||
int n = 0;
|
||||
struct Capture cap;
|
||||
luaL_arg_check(lua_isstring(newp) || lua_isfunction(newp), 3,
|
||||
"string or function expected");
|
||||
luaL_resetbuffer();
|
||||
cap.src_end = src+srcl;
|
||||
while (n < max_s) {
|
||||
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 < cap.src_end)
|
||||
luaL_addchar(*src++);
|
||||
else break;
|
||||
if (anchor) break;
|
||||
}
|
||||
addnchar(src, cap.src_end-src);
|
||||
closeandpush();
|
||||
lua_pushnumber(n); /* number of substitutions */
|
||||
}
|
||||
|
||||
/* }====================================================== */
|
||||
|
||||
|
||||
static void luaI_addquoted (int arg) {
|
||||
long l;
|
||||
char *s = luaL_check_lstr(arg, &l);
|
||||
luaL_addchar('"');
|
||||
while (l--) {
|
||||
switch (*s) {
|
||||
case '"': case '\\': case '\n':
|
||||
luaL_addchar('\\');
|
||||
luaL_addchar(*s);
|
||||
break;
|
||||
case '\0': addnchar("\\000", 4); break;
|
||||
default: luaL_addchar(*s);
|
||||
}
|
||||
s++;
|
||||
}
|
||||
luaL_addchar('"');
|
||||
}
|
||||
|
||||
/* maximum size of each format specification (such as '%-099.99d') */
|
||||
#define MAX_FORMAT 20
|
||||
|
||||
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 */
|
||||
struct Capture cap;
|
||||
char form[MAX_FORMAT]; /* to store the format ('%...') */
|
||||
char *buff; /* to store the formatted item */
|
||||
char *initf = strfrmt;
|
||||
form[0] = '%';
|
||||
if (isdigit((unsigned char)*initf) && *(initf+1) == '$') {
|
||||
arg = *initf - '0';
|
||||
initf += 2; /* skip the 'n$' */
|
||||
}
|
||||
arg++;
|
||||
cap.src_end = strfrmt+strlen(strfrmt)+1;
|
||||
cap.level = 0;
|
||||
strfrmt = match(initf, "[-+ #0]*(%d*)%.?(%d*)", &cap);
|
||||
if (cap.capture[0].len > 2 || cap.capture[1].len > 2 || /* < 100? */
|
||||
strfrmt-initf > MAX_FORMAT-2)
|
||||
lua_error("invalid format (width or precision too long)");
|
||||
strncpy(form+1, initf, strfrmt-initf+1); /* +1 to include conversion */
|
||||
form[strfrmt-initf+2] = 0;
|
||||
buff = luaL_openspace(512); /* 512 > soid luaI_addquot99.99f', -1e308) */
|
||||
switch (*strfrmt++) {
|
||||
case 'c': case 'd': case 'i':
|
||||
sprintf(buff, form, luaL_check_int(arg));
|
||||
break;
|
||||
case 'o': case 'u': case 'x': case 'X':
|
||||
sprintf(buff, form, (unsigned int)luaL_check_number(arg));
|
||||
break;
|
||||
case 'e': case 'E': case 'f': case 'g': case 'G':
|
||||
sprintf(buff, form, luaL_check_number(arg));
|
||||
break;
|
||||
case 'q':
|
||||
luaI_addquoted(arg);
|
||||
continue; /* skip the "addsize" at the end */
|
||||
case 's': {
|
||||
long l;
|
||||
char *s = luaL_check_lstr(arg, &l);
|
||||
if (cap.capture[1].len == 0 && l >= 100) {
|
||||
/* no precision and string is too big to be formatted;
|
||||
keep original string */
|
||||
addnchar(s, l);
|
||||
continue; /* skip the "addsize" at the end */
|
||||
}
|
||||
else {
|
||||
sprintf(buff, form, s);
|
||||
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},
|
||||
{"strchar", str_char},
|
||||
{"strrep", str_rep},
|
||||
{"ascii", str_byte}, /* for compatibility with 3.0 and earlier */
|
||||
{"strbyte", str_byte},
|
||||
{"format", str_format},
|
||||
{"strfind", str_find},
|
||||
{"gsub", str_gsub}
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
** Open string library
|
||||
*/
|
||||
void strlib_open (void)
|
||||
{
|
||||
luaL_openlib(strlib, (sizeof(strlib)/sizeof(strlib[0])));
|
||||
}
|
||||
176
ltable.c
Normal file
176
ltable.c
Normal file
@@ -0,0 +1,176 @@
|
||||
/*
|
||||
** $Id: ltable.c,v 1.20 1999/01/25 17:41:19 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 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));
|
||||
}
|
||||
|
||||
|
||||
Node *luaH_present (Hash *t, TObject *key) {
|
||||
int tsize = nhash(t);
|
||||
long int h = hashindex(key);
|
||||
int h1 = h%tsize;
|
||||
Node *n = node(t, h1);
|
||||
/* keep looking until an entry with "ref" equal to key or nil */
|
||||
while ((ttype(ref(n)) == ttype(key)) ? !luaO_equalval(key, ref(n))
|
||||
: ttype(ref(n)) != LUA_T_NIL) {
|
||||
h1 += (h&(tsize-2)) + 1; /* double hashing */
|
||||
if (h1 >= tsize) h1 -= tsize;
|
||||
n = node(t, h1);
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
void luaH_free (Hash *frees) {
|
||||
while (frees) {
|
||||
Hash *next = (Hash *)frees->head.next;
|
||||
L->nblocks -= gcsize(frees->nhash);
|
||||
luaM_free(nodevector(frees));
|
||||
luaM_free(frees);
|
||||
frees = next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static Node *hashnodecreate (int nhash) {
|
||||
Node *v = luaM_newvector(nhash, Node);
|
||||
int i;
|
||||
for (i=0; i<nhash; i++)
|
||||
ttype(ref(&v[i])) = ttype(val(&v[i])) = LUA_T_NIL;
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
Hash *luaH_new (int nhash) {
|
||||
Hash *t = luaM_new(Hash);
|
||||
nhash = luaO_redimension(nhash*3/2);
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
static int newsize (Hash *t) {
|
||||
Node *v = t->node;
|
||||
int size = nhash(t);
|
||||
int realuse = 0;
|
||||
int i;
|
||||
for (i=0; i<size; i++) {
|
||||
if (ttype(val(v+i)) != LUA_T_NIL)
|
||||
realuse++;
|
||||
}
|
||||
return luaO_redimension((realuse+1)*2); /* +1 is the new element */
|
||||
}
|
||||
|
||||
|
||||
static void rehash (Hash *t) {
|
||||
int nold = nhash(t);
|
||||
Node *vold = nodevector(t);
|
||||
int nnew = newsize(t);
|
||||
int i;
|
||||
nodevector(t) = hashnodecreate(nnew);
|
||||
nhash(t) = nnew;
|
||||
nuse(t) = 0;
|
||||
for (i=0; i<nold; i++) {
|
||||
Node *n = vold+i;
|
||||
if (ttype(val(n)) != LUA_T_NIL) {
|
||||
*luaH_present(t, ref(n)) = *n; /* copy old node to new hash */
|
||||
nuse(t)++;
|
||||
}
|
||||
}
|
||||
L->nblocks += gcsize(nnew)-gcsize(nold);
|
||||
luaM_free(vold);
|
||||
}
|
||||
|
||||
|
||||
void luaH_set (Hash *t, TObject *ref, TObject *val) {
|
||||
Node *n = luaH_present(t, ref);
|
||||
if (ttype(ref(n)) != LUA_T_NIL)
|
||||
*val(n) = *val;
|
||||
else {
|
||||
TObject buff = *val; /* rehash may invalidate this address */
|
||||
if ((long)nuse(t)*3L > (long)nhash(t)*2L) {
|
||||
rehash(t);
|
||||
n = luaH_present(t, ref);
|
||||
}
|
||||
nuse(t)++;
|
||||
*ref(n) = *ref;
|
||||
*val(n) = buff;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int luaH_pos (Hash *t, TObject *r) {
|
||||
Node *n = luaH_present(t, r);
|
||||
luaL_arg_check(ttype(val(n)) != LUA_T_NIL, 2, "key not found");
|
||||
return n-(t->node);
|
||||
}
|
||||
|
||||
|
||||
void luaH_setint (Hash *t, int ref, TObject *val) {
|
||||
TObject index;
|
||||
ttype(&index) = LUA_T_NUMBER;
|
||||
nvalue(&index) = ref;
|
||||
luaH_set(t, &index, val);
|
||||
}
|
||||
|
||||
|
||||
TObject *luaH_getint (Hash *t, int ref) {
|
||||
TObject index;
|
||||
ttype(&index) = LUA_T_NUMBER;
|
||||
nvalue(&index) = ref;
|
||||
return luaH_get(t, &index);
|
||||
}
|
||||
|
||||
30
ltable.h
Normal file
30
ltable.h
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
** $Id: ltable.h,v 1.10 1999/01/25 17:40:10 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)
|
||||
|
||||
#define luaH_get(t,ref) (val(luaH_present((t), (ref))))
|
||||
#define luaH_move(t,from,to) (luaH_setint(t, to, luaH_getint(t, from)))
|
||||
|
||||
Hash *luaH_new (int nhash);
|
||||
void luaH_free (Hash *frees);
|
||||
Node *luaH_present (Hash *t, TObject *key);
|
||||
void luaH_set (Hash *t, TObject *ref, TObject *val);
|
||||
int luaH_pos (Hash *t, TObject *r);
|
||||
void luaH_setint (Hash *t, int ref, TObject *val);
|
||||
TObject *luaH_getint (Hash *t, int ref);
|
||||
|
||||
|
||||
#endif
|
||||
247
ltm.c
Normal file
247
ltm.c
Normal file
@@ -0,0 +1,247 @@
|
||||
/*
|
||||
** $Id: ltm.c,v 1.23 1999/02/25 19:13:56 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 = luaL_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 luaT_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 luaT_validevent (int t, int e) { /* ORDER LUA_T */
|
||||
return (t < LUA_T_NIL) ? 1 : luaT_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->last_tag = -(NUM_TAGS-1);
|
||||
luaM_growvector(L->IMtable, 0, NUM_TAGS, struct IM, arrEM, MAX_INT);
|
||||
for (t=L->last_tag; t<=0; t++)
|
||||
init_entry(t);
|
||||
}
|
||||
|
||||
|
||||
int lua_newtag (void) {
|
||||
--L->last_tag;
|
||||
luaM_growvector(L->IMtable, -(L->last_tag), 1, struct IM, arrEM, 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 was not created by `newtag'", tag);
|
||||
}
|
||||
|
||||
|
||||
int lua_copytagmethods (int tagto, int tagfrom) {
|
||||
int e;
|
||||
checktag(tagto);
|
||||
checktag(tagfrom);
|
||||
for (e=0; e<IM_N; e++) {
|
||||
if (luaT_validevent(tagto, e))
|
||||
*luaT_getim(tagto, e) = *luaT_getim(tagfrom, e);
|
||||
}
|
||||
return tagto;
|
||||
}
|
||||
|
||||
|
||||
int luaT_effectivetag (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_INTERNALERROR("invalid type");
|
||||
#endif
|
||||
default:
|
||||
return t;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TObject *luaT_gettagmethod (int t, char *event) {
|
||||
int e = luaI_checkevent(event, luaT_eventname);
|
||||
checktag(t);
|
||||
if (luaT_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 (!luaT_validevent(t, e))
|
||||
luaL_verror("cannot change tag method `%.20s' for type `%.20s'%.20s",
|
||||
luaT_eventname[e], luaO_typenames[-t],
|
||||
(t == LUA_T_ARRAY || t == LUA_T_USERDATA) ? " with default tag"
|
||||
: "");
|
||||
*func = *luaT_getim(t,e);
|
||||
*luaT_getim(t, e) = temp;
|
||||
}
|
||||
|
||||
|
||||
char *luaT_travtagmethods (int (*fn)(TObject *)) { /* ORDER IM */
|
||||
int e;
|
||||
for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) {
|
||||
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"
|
||||
#include "lstring.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 (luaT_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 (luaL_findstring(name, oldnames)) {
|
||||
case 0: { /* old error fallback */
|
||||
TObject *em = &(luaS_new("_ERRORMESSAGE")->u.s.globalval);
|
||||
oldfunc = *em;
|
||||
*em = *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 = luaL_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
Normal file
62
ltm.h
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
** $Id: ltm.h,v 1.4 1997/11/26 18:53:45 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_effectivetag(o),(e)))
|
||||
|
||||
extern char *luaT_eventname[];
|
||||
|
||||
|
||||
void luaT_init (void);
|
||||
void luaT_realtag (int tag);
|
||||
int luaT_effectivetag (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
|
||||
240
lua.c
240
lua.c
@@ -1,18 +1,26 @@
|
||||
/*
|
||||
** lua.c
|
||||
** Linguagem para Usuarios de Aplicacao
|
||||
** $Id: lua.c,v 1.18 1999/01/26 11:50:58 roberto Exp roberto $
|
||||
** Lua stand-alone interpreter
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
char *rcs_lua="$Id: lua.c,v 1.17 1997/06/18 21:20:45 roberto Exp roberto $";
|
||||
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lua.h"
|
||||
#include "auxlib.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
|
||||
@@ -20,122 +28,166 @@ char *rcs_lua="$Id: lua.c,v 1.17 1997/06/18 21:20:45 roberto Exp roberto $";
|
||||
#endif
|
||||
|
||||
|
||||
#define DEBUG 0
|
||||
typedef void (*handler)(int); /* type for signal actions */
|
||||
|
||||
static void testC (void)
|
||||
{
|
||||
#if DEBUG
|
||||
#define getnum(s) ((*s++) - '0')
|
||||
#define getname(s) (nome[0] = *s++, nome)
|
||||
static void laction (int i);
|
||||
|
||||
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;
|
||||
static lua_LHFunction old_linehook = NULL;
|
||||
static lua_CHFunction old_callhook = NULL;
|
||||
|
||||
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'");
|
||||
}
|
||||
#else
|
||||
lua_error("`testC' not active");
|
||||
#endif
|
||||
static handler lreset (void) {
|
||||
return signal(SIGINT, laction);
|
||||
}
|
||||
|
||||
|
||||
static void manual_input (void)
|
||||
{
|
||||
if (isatty(0)) {
|
||||
char buffer[250];
|
||||
while (fgets(buffer, sizeof(buffer), stdin) != 0) {
|
||||
lua_beginblock();
|
||||
lua_dostring(buffer);
|
||||
lua_endblock();
|
||||
}
|
||||
static void lstop (void) {
|
||||
lua_setlinehook(old_linehook);
|
||||
lua_setcallhook(old_callhook);
|
||||
lreset();
|
||||
lua_error("interrupted!");
|
||||
}
|
||||
|
||||
|
||||
static void laction (int i) {
|
||||
old_linehook = lua_setlinehook((lua_LHFunction)lstop);
|
||||
old_callhook = lua_setcallhook((lua_CHFunction)lstop);
|
||||
}
|
||||
|
||||
|
||||
static int ldo (int (*f)(char *), char *name) {
|
||||
int res;
|
||||
handler h = lreset();
|
||||
res = f(name); /* dostring | dofile */
|
||||
signal(SIGINT, h); /* restore old action */
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
static void print_message (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");
|
||||
}
|
||||
|
||||
|
||||
static void assign (char *arg) {
|
||||
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);
|
||||
}
|
||||
else
|
||||
lua_dofile(NULL); /* executes stdin as a file */
|
||||
}
|
||||
|
||||
|
||||
static void manual_input (int prompt) {
|
||||
int cont = 1;
|
||||
while (cont) {
|
||||
char buffer[BUFSIZ];
|
||||
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 >= BUFSIZ-1) {
|
||||
fprintf(stderr, "lua: argument line too long\n");
|
||||
break;
|
||||
}
|
||||
else buffer[i++] = (char)c;
|
||||
}
|
||||
buffer[i] = '\0';
|
||||
ldo(lua_dostring, buffer);
|
||||
lua_endblock();
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
int i;
|
||||
int result = 0;
|
||||
iolib_open ();
|
||||
strlib_open ();
|
||||
mathlib_open ();
|
||||
lua_register("testC", testC);
|
||||
if (argc < 2)
|
||||
manual_input();
|
||||
lua_open();
|
||||
lua_pushstring("> "); lua_setglobal("_PROMPT");
|
||||
setlocale(LC_ALL, "");
|
||||
lua_userinit();
|
||||
if (argc < 2) { /* no arguments? */
|
||||
if (isatty(0)) {
|
||||
printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT);
|
||||
manual_input(1);
|
||||
}
|
||||
else
|
||||
ldo(lua_dofile, NULL); /* executes stdin as a file */
|
||||
}
|
||||
else for (i=1; i<argc; i++) {
|
||||
if (strcmp(argv[i], "-") == 0)
|
||||
manual_input();
|
||||
else if (strcmp(argv[i], "-v") == 0)
|
||||
printf("%s %s\n(written by %s)\n\n",
|
||||
LUA_VERSION, LUA_COPYRIGHT, LUA_AUTHORS);
|
||||
else if ((strcmp(argv[i], "-e") == 0 && i++) || strchr(argv[i], '=')) {
|
||||
if (lua_dostring(argv[i]) != 0) {
|
||||
fprintf(stderr, "lua: error running argument `%s'\n", argv[i]);
|
||||
return 1;
|
||||
if (argv[i][0] == '-') { /* option? */
|
||||
switch (argv[i][1]) {
|
||||
case 0:
|
||||
ldo(lua_dofile, NULL); /* executes stdin as a file */
|
||||
break;
|
||||
case 'i':
|
||||
manual_input(1);
|
||||
break;
|
||||
case 'q':
|
||||
manual_input(0);
|
||||
break;
|
||||
case 'd':
|
||||
lua_setdebug(1);
|
||||
break;
|
||||
case 'v':
|
||||
printf("%s %s\n(written by %s)\n\n",
|
||||
LUA_VERSION, LUA_COPYRIGHT, LUA_AUTHORS);
|
||||
break;
|
||||
case 'e':
|
||||
i++;
|
||||
if (ldo(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 {
|
||||
result = lua_dofile (argv[i]);
|
||||
int result = ldo(lua_dofile, argv[i]);
|
||||
if (result) {
|
||||
if (result == 2) {
|
||||
fprintf(stderr, "lua: cannot execute file ");
|
||||
perror(argv[i]);
|
||||
}
|
||||
return 1;
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
#ifdef DEBUG
|
||||
lua_close();
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
86
lua.h
86
lua.h
@@ -1,16 +1,18 @@
|
||||
/*
|
||||
** LUA - An Extensible Extension Language
|
||||
** $Id: lua.h,v 1.30 1999/02/25 19:13:56 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
|
||||
** $Id: lua.h,v 4.10 1997/06/19 18:03:04 roberto Exp roberto $
|
||||
** www: http://www.tecgraf.puc-rio.br/lua/
|
||||
** See Copyright Notice at the end of this file
|
||||
*/
|
||||
|
||||
|
||||
#ifndef lua_h
|
||||
#define lua_h
|
||||
|
||||
#define LUA_VERSION "Lua 3.0"
|
||||
#define LUA_COPYRIGHT "Copyright (C) 1994-1997 TeCGraf"
|
||||
#define LUA_VERSION "Lua 3.2 (beta)"
|
||||
#define LUA_COPYRIGHT "Copyright (C) 1994-1999 TeCGraf, PUC-Rio"
|
||||
#define LUA_AUTHORS "W. Celes, R. Ierusalimschy & L. H. de Figueiredo"
|
||||
|
||||
|
||||
@@ -18,19 +20,28 @@
|
||||
|
||||
#define LUA_ANYTAG (-1)
|
||||
|
||||
typedef struct lua_State lua_State;
|
||||
extern lua_State *lua_state;
|
||||
|
||||
typedef void (*lua_CFunction) (void);
|
||||
typedef unsigned int lua_Object;
|
||||
|
||||
lua_Object lua_settagmethod (int tag, char *event); /* In: new method */
|
||||
void lua_open (void);
|
||||
void lua_close (void);
|
||||
lua_State *lua_setstate (lua_State *st);
|
||||
|
||||
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_error (char *s);
|
||||
int lua_dofile (char *filename); /* Out: returns */
|
||||
int lua_dostring (char *string); /* Out: returns */
|
||||
int lua_dobuffer (char *buff, int size, char *name);
|
||||
/* Out: returns */
|
||||
int lua_callfunction (lua_Object f);
|
||||
/* In: parameters; Out: returns */
|
||||
|
||||
@@ -49,16 +60,18 @@ int lua_isnumber (lua_Object object);
|
||||
int lua_isstring (lua_Object object);
|
||||
int lua_isfunction (lua_Object object);
|
||||
|
||||
float lua_getnumber (lua_Object object);
|
||||
double lua_getnumber (lua_Object object);
|
||||
char *lua_getstring (lua_Object object);
|
||||
long lua_strlen (lua_Object object);
|
||||
lua_CFunction lua_getcfunction (lua_Object object);
|
||||
void *lua_getuserdata (lua_Object object);
|
||||
|
||||
|
||||
void lua_pushnil (void);
|
||||
void lua_pushnumber (float n);
|
||||
void lua_pushnumber (double n);
|
||||
void lua_pushlstring (char *s, long len);
|
||||
void lua_pushstring (char *s);
|
||||
void lua_pushcfunction (lua_CFunction fn);
|
||||
void lua_pushcclosure (lua_CFunction fn, int n);
|
||||
void lua_pushusertag (void *u, int tag);
|
||||
void lua_pushobject (lua_Object object);
|
||||
|
||||
@@ -76,6 +89,9 @@ lua_Object lua_rawgettable (void); /* In: table, index */
|
||||
|
||||
int lua_tag (lua_Object object);
|
||||
|
||||
char *lua_nextvar (char *varname); /* Out: value */
|
||||
int lua_next (lua_Object o, int i);
|
||||
/* Out: ref, value */
|
||||
|
||||
int lua_ref (int lock); /* In: value */
|
||||
lua_Object lua_getref (int ref);
|
||||
@@ -87,7 +103,7 @@ long lua_collectgarbage (long limit);
|
||||
|
||||
|
||||
/* =============================================================== */
|
||||
/* some useful macros */
|
||||
/* some useful macros/functions */
|
||||
|
||||
#define lua_call(name) lua_callfunction(lua_getglobal(name))
|
||||
|
||||
@@ -99,20 +115,19 @@ long lua_collectgarbage (long limit);
|
||||
|
||||
#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))
|
||||
|
||||
lua_Object lua_seterrormethod (void); /* In: new method */
|
||||
|
||||
/* ==========================================================================
|
||||
/* ==========================================================================
|
||||
** for compatibility with old versions. Avoid using these macros/functions
|
||||
** If your program does not use any of these, define LUA_COMPAT2_5 to 0
|
||||
** If your program does need any of these, define LUA_COMPAT2_5
|
||||
*/
|
||||
|
||||
#ifndef LUA_COMPAT2_5
|
||||
#define LUA_COMPAT2_5 1
|
||||
#endif
|
||||
|
||||
|
||||
#if LUA_COMPAT2_5
|
||||
#ifdef LUA_COMPAT2_5
|
||||
|
||||
|
||||
lua_Object lua_setfallback (char *event, lua_CFunction fallback);
|
||||
@@ -139,3 +154,40 @@ lua_Object lua_setfallback (char *event, lua_CFunction fallback);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
* Copyright (c) 1994-1999 TeCGraf, PUC-Rio. All rights reserved.
|
||||
*
|
||||
* Permission is hereby granted, without written agreement and without license
|
||||
* or royalty fees, to use, copy, modify, and distribute this software and its
|
||||
* documentation for any purpose, including commercial applications, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* - The above copyright notice and this permission notice shall appear in all
|
||||
* copies or substantial portions of this software.
|
||||
*
|
||||
* - The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software in a
|
||||
* product, an acknowledgment in the product documentation would be greatly
|
||||
* appreciated (but it is not required).
|
||||
*
|
||||
* - Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 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 modifications. In no event shall TeCGraf, PUC-Rio, or the
|
||||
* authors be held liable to any party for direct, indirect, special,
|
||||
* incidental, or consequential damages arising out of the use of this software
|
||||
* and its documentation.
|
||||
*
|
||||
* The Lua language and this implementation have been entirely designed and
|
||||
* written by Waldemar Celes Filho, Roberto Ierusalimschy and
|
||||
* Luiz Henrique de Figueiredo at TeCGraf, PUC-Rio.
|
||||
*
|
||||
* This implementation contains no third-party code.
|
||||
******************************************************************************/
|
||||
|
||||
791
lua.stx
791
lua.stx
@@ -1,791 +0,0 @@
|
||||
%{
|
||||
|
||||
char *rcs_luastx = "$Id: lua.stx,v 3.46 1997/03/31 14:19:01 roberto Exp roberto $";
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "luadebug.h"
|
||||
#include "luamem.h"
|
||||
#include "lex.h"
|
||||
#include "opcode.h"
|
||||
#include "hash.h"
|
||||
#include "inout.h"
|
||||
#include "tree.h"
|
||||
#include "table.h"
|
||||
#include "lua.h"
|
||||
#include "func.h"
|
||||
|
||||
/* to avoid warnings generated by yacc */
|
||||
int yyparse (void);
|
||||
#define malloc luaI_malloc
|
||||
#define realloc luaI_realloc
|
||||
#define free luaI_free
|
||||
|
||||
#ifndef LISTING
|
||||
#define LISTING 0
|
||||
#endif
|
||||
|
||||
#ifndef CODE_BLOCK
|
||||
#define CODE_BLOCK 256
|
||||
#endif
|
||||
static int maxcode;
|
||||
static int maxmain;
|
||||
static int maxcurr;
|
||||
static Byte *funcCode = NULL;
|
||||
static Byte **initcode;
|
||||
static Byte *basepc;
|
||||
static int maincode;
|
||||
static int pc;
|
||||
|
||||
|
||||
#define MAXVAR 32
|
||||
static Long varbuffer[MAXVAR]; /* variables in an assignment list;
|
||||
it's long to store negative Word values */
|
||||
static int nvarbuffer=0; /* number of variables at a list */
|
||||
|
||||
#define MAXLOCALS 32
|
||||
static TaggedString *localvar[MAXLOCALS]; /* store local variable names */
|
||||
static int nlocalvar=0; /* number of local variables */
|
||||
|
||||
#define MAXFIELDS FIELDS_PER_FLUSH*2
|
||||
|
||||
int lua_debug = 0;
|
||||
|
||||
/* Internal functions */
|
||||
|
||||
static void yyerror (char *s)
|
||||
{
|
||||
luaI_syntaxerror(s);
|
||||
}
|
||||
|
||||
static void check_space (int i)
|
||||
{
|
||||
if (pc+i>maxcurr-1) /* 1 byte free to code HALT of main code */
|
||||
maxcurr = growvector(&basepc, maxcurr, Byte, codeEM, MAX_INT);
|
||||
}
|
||||
|
||||
static void code_byte (Byte c)
|
||||
{
|
||||
check_space(1);
|
||||
basepc[pc++] = c;
|
||||
}
|
||||
|
||||
static void code_word (Word n)
|
||||
{
|
||||
check_space(sizeof(Word));
|
||||
memcpy(basepc+pc, &n, sizeof(Word));
|
||||
pc += sizeof(Word);
|
||||
}
|
||||
|
||||
static void code_float (real n)
|
||||
{
|
||||
check_space(sizeof(real));
|
||||
memcpy(basepc+pc, &n, sizeof(real));
|
||||
pc += sizeof(real);
|
||||
}
|
||||
|
||||
static void code_code (TFunc *tf)
|
||||
{
|
||||
check_space(sizeof(TFunc *));
|
||||
memcpy(basepc+pc, &tf, sizeof(TFunc *));
|
||||
pc += sizeof(TFunc *);
|
||||
}
|
||||
|
||||
static void code_word_at (Byte *p, int n)
|
||||
{
|
||||
Word w = n;
|
||||
if (w != n)
|
||||
yyerror("block too big");
|
||||
memcpy(p, &w, sizeof(Word));
|
||||
}
|
||||
|
||||
static void flush_record (int n)
|
||||
{
|
||||
if (n == 0) return;
|
||||
code_byte(STOREMAP);
|
||||
code_byte(n);
|
||||
}
|
||||
|
||||
static void flush_list (int m, int n)
|
||||
{
|
||||
if (n == 0) return;
|
||||
if (m == 0)
|
||||
code_byte(STORELIST0);
|
||||
else
|
||||
if (m < 255)
|
||||
{
|
||||
code_byte(STORELIST);
|
||||
code_byte(m);
|
||||
}
|
||||
else
|
||||
yyerror ("list constructor too long");
|
||||
code_byte(n);
|
||||
}
|
||||
|
||||
static void store_localvar (TaggedString *name, int n)
|
||||
{
|
||||
if (nlocalvar+n < MAXLOCALS)
|
||||
localvar[nlocalvar+n] = name;
|
||||
else
|
||||
yyerror ("too many local variables");
|
||||
if (lua_debug)
|
||||
luaI_registerlocalvar(name, lua_linenumber);
|
||||
}
|
||||
|
||||
static void add_localvar (TaggedString *name)
|
||||
{
|
||||
store_localvar(name, 0);
|
||||
nlocalvar++;
|
||||
}
|
||||
|
||||
static void add_varbuffer (Long var)
|
||||
{
|
||||
if (nvarbuffer < MAXVAR)
|
||||
varbuffer[nvarbuffer++] = var;
|
||||
else
|
||||
yyerror ("variable buffer overflow");
|
||||
}
|
||||
|
||||
static void code_string (Word w)
|
||||
{
|
||||
code_byte(PUSHSTRING);
|
||||
code_word(w);
|
||||
}
|
||||
|
||||
static void code_constant (TaggedString *s)
|
||||
{
|
||||
code_string(luaI_findconstant(s));
|
||||
}
|
||||
|
||||
static void code_number (float f)
|
||||
{
|
||||
Word i;
|
||||
if (f >= 0 && f <= (float)MAX_WORD && (float)(i=(Word)f) == f) {
|
||||
/* f has an (short) integer value */
|
||||
if (i <= 2) code_byte(PUSH0 + i);
|
||||
else if (i <= 255)
|
||||
{
|
||||
code_byte(PUSHBYTE);
|
||||
code_byte(i);
|
||||
}
|
||||
else
|
||||
{
|
||||
code_byte(PUSHWORD);
|
||||
code_word(i);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
code_byte(PUSHFLOAT);
|
||||
code_float(f);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Search a local name and if find return its index. If do not find return -1
|
||||
*/
|
||||
static int lua_localname (TaggedString *n)
|
||||
{
|
||||
int i;
|
||||
for (i=nlocalvar-1; i >= 0; i--)
|
||||
if (n == localvar[i]) return i; /* local var */
|
||||
return -1; /* global var */
|
||||
}
|
||||
|
||||
/*
|
||||
** Push a variable given a number. If number is positive, push global variable
|
||||
** indexed by (number -1). If negative, push local indexed by ABS(number)-1.
|
||||
** Otherwise, if zero, push indexed variable (record).
|
||||
*/
|
||||
static void lua_pushvar (Long number)
|
||||
{
|
||||
if (number > 0) /* global var */
|
||||
{
|
||||
code_byte(PUSHGLOBAL);
|
||||
code_word(number-1);
|
||||
}
|
||||
else if (number < 0) /* local var */
|
||||
{
|
||||
number = (-number) - 1;
|
||||
if (number < 10) code_byte(PUSHLOCAL0 + number);
|
||||
else
|
||||
{
|
||||
code_byte(PUSHLOCAL);
|
||||
code_byte(number);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
code_byte(PUSHINDEXED);
|
||||
}
|
||||
}
|
||||
|
||||
static void lua_codeadjust (int n)
|
||||
{
|
||||
if (n+nlocalvar == 0)
|
||||
code_byte(ADJUST0);
|
||||
else
|
||||
{
|
||||
code_byte(ADJUST);
|
||||
code_byte(n+nlocalvar);
|
||||
}
|
||||
}
|
||||
|
||||
static void change2main (void)
|
||||
{
|
||||
/* (re)store main values */
|
||||
pc=maincode; basepc=*initcode; maxcurr=maxmain;
|
||||
nlocalvar=0;
|
||||
}
|
||||
|
||||
static void savemain (void)
|
||||
{
|
||||
/* save main values */
|
||||
maincode=pc; *initcode=basepc; maxmain=maxcurr;
|
||||
}
|
||||
|
||||
static void init_func (void)
|
||||
{
|
||||
if (funcCode == NULL) /* first function */
|
||||
{
|
||||
funcCode = newvector(CODE_BLOCK, Byte);
|
||||
maxcode = CODE_BLOCK;
|
||||
}
|
||||
savemain(); /* save main values */
|
||||
/* set func values */
|
||||
pc=0; basepc=funcCode; maxcurr=maxcode;
|
||||
nlocalvar = 0;
|
||||
luaI_codedebugline(lua_linenumber);
|
||||
}
|
||||
|
||||
static void codereturn (void)
|
||||
{
|
||||
if (nlocalvar == 0)
|
||||
code_byte(RETCODE0);
|
||||
else
|
||||
{
|
||||
code_byte(RETCODE);
|
||||
code_byte(nlocalvar);
|
||||
}
|
||||
}
|
||||
|
||||
void luaI_codedebugline (int line)
|
||||
{
|
||||
static int lastline = 0;
|
||||
if (lua_debug && line != lastline)
|
||||
{
|
||||
code_byte(SETLINE);
|
||||
code_word(line);
|
||||
lastline = line;
|
||||
}
|
||||
}
|
||||
|
||||
static int adjust_functioncall (Long exp, int i)
|
||||
{
|
||||
if (exp <= 0)
|
||||
return -exp; /* exp is -list length */
|
||||
else
|
||||
{
|
||||
int temp = basepc[exp];
|
||||
basepc[exp] = i;
|
||||
return temp+i;
|
||||
}
|
||||
}
|
||||
|
||||
static void adjust_mult_assign (int vars, Long exps, int temps)
|
||||
{
|
||||
if (exps > 0)
|
||||
{ /* must correct function call */
|
||||
int diff = vars - basepc[exps];
|
||||
if (diff >= 0)
|
||||
adjust_functioncall(exps, diff);
|
||||
else
|
||||
{
|
||||
adjust_functioncall(exps, 0);
|
||||
lua_codeadjust(temps);
|
||||
}
|
||||
}
|
||||
else if (vars != -exps)
|
||||
lua_codeadjust(temps);
|
||||
}
|
||||
|
||||
static int close_parlist (int dots)
|
||||
{
|
||||
if (!dots)
|
||||
lua_codeadjust(0);
|
||||
else
|
||||
{
|
||||
code_byte(VARARGS);
|
||||
code_byte(nlocalvar);
|
||||
add_localvar(luaI_createfixedstring("arg"));
|
||||
}
|
||||
return lua_linenumber;
|
||||
}
|
||||
|
||||
static void storesinglevar (Long v)
|
||||
{
|
||||
if (v > 0) /* global var */
|
||||
{
|
||||
code_byte(STOREGLOBAL);
|
||||
code_word(v-1);
|
||||
}
|
||||
else if (v < 0) /* local var */
|
||||
{
|
||||
int number = (-v) - 1;
|
||||
if (number < 10) code_byte(STORELOCAL0 + number);
|
||||
else
|
||||
{
|
||||
code_byte(STORELOCAL);
|
||||
code_byte(number);
|
||||
}
|
||||
}
|
||||
else
|
||||
code_byte(STOREINDEXED0);
|
||||
}
|
||||
|
||||
static void lua_codestore (int i)
|
||||
{
|
||||
if (varbuffer[i] != 0) /* global or local var */
|
||||
storesinglevar(varbuffer[i]);
|
||||
else /* indexed var */
|
||||
{
|
||||
int j;
|
||||
int upper=0; /* number of indexed variables upper */
|
||||
int param; /* number of itens until indexed expression */
|
||||
for (j=i+1; j <nvarbuffer; j++)
|
||||
if (varbuffer[j] == 0) upper++;
|
||||
param = upper*2 + i;
|
||||
if (param == 0)
|
||||
code_byte(STOREINDEXED0);
|
||||
else
|
||||
{
|
||||
code_byte(STOREINDEXED);
|
||||
code_byte(param);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void codeIf (Long thenAdd, Long elseAdd)
|
||||
{
|
||||
Long elseinit = elseAdd+sizeof(Word)+1;
|
||||
if (pc == elseinit) /* no else */
|
||||
{
|
||||
pc -= sizeof(Word)+1;
|
||||
elseinit = pc;
|
||||
}
|
||||
else
|
||||
{
|
||||
basepc[elseAdd] = JMP;
|
||||
code_word_at(basepc+elseAdd+1, pc-elseinit);
|
||||
}
|
||||
basepc[thenAdd] = IFFJMP;
|
||||
code_word_at(basepc+thenAdd+1,elseinit-(thenAdd+sizeof(Word)+1));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Parse LUA code.
|
||||
*/
|
||||
void lua_parse (TFunc *tf)
|
||||
{
|
||||
initcode = &(tf->code);
|
||||
*initcode = newvector(CODE_BLOCK, Byte);
|
||||
maincode = 0;
|
||||
maxmain = CODE_BLOCK;
|
||||
change2main();
|
||||
if (yyparse ()) lua_error("parse error");
|
||||
savemain();
|
||||
(*initcode)[maincode++] = RETCODE0;
|
||||
tf->size = maincode;
|
||||
#if LISTING
|
||||
{ static void PrintCode (Byte *c, Byte *end);
|
||||
PrintCode(*initcode,*initcode+maincode); }
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
%}
|
||||
|
||||
|
||||
%union
|
||||
{
|
||||
int vInt;
|
||||
float vFloat;
|
||||
char *pChar;
|
||||
Word vWord;
|
||||
Long vLong;
|
||||
TFunc *pFunc;
|
||||
TaggedString *pTStr;
|
||||
}
|
||||
|
||||
%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 <vFloat> NUMBER
|
||||
%token <vWord> STRING
|
||||
%token <pTStr> NAME
|
||||
|
||||
%type <vLong> PrepJump
|
||||
%type <vLong> exprlist, exprlist1 /* if > 0, points to function return
|
||||
counter (which has list length); if <= 0, -list lenght */
|
||||
%type <vLong> functioncall, expr /* if != 0, points to function return
|
||||
counter */
|
||||
%type <vInt> varlist1, funcParams, funcvalue
|
||||
%type <vInt> fieldlist, localdeclist, decinit
|
||||
%type <vInt> ffieldlist, ffieldlist1, semicolonpart
|
||||
%type <vInt> lfieldlist, lfieldlist1
|
||||
%type <vInt> parlist, parlist1, par
|
||||
%type <vLong> var, singlevar, funcname
|
||||
%type <pFunc> body
|
||||
|
||||
%left AND OR
|
||||
%left EQ NE '>' '<' LE GE
|
||||
%left CONC
|
||||
%left '+' '-'
|
||||
%left '*' '/'
|
||||
%left UNARY NOT
|
||||
%right '^'
|
||||
|
||||
|
||||
%% /* beginning of rules section */
|
||||
|
||||
chunk : chunklist ret ;
|
||||
|
||||
chunklist : /* empty */
|
||||
| chunklist stat sc
|
||||
| chunklist function
|
||||
;
|
||||
|
||||
function : FUNCTION funcname body
|
||||
{
|
||||
code_byte(PUSHFUNCTION);
|
||||
code_code($3);
|
||||
storesinglevar($2);
|
||||
}
|
||||
;
|
||||
|
||||
funcname : var { $$ =$1; init_func(); }
|
||||
| varexp ':' NAME
|
||||
{
|
||||
code_constant($3);
|
||||
$$ = 0; /* indexed variable */
|
||||
init_func();
|
||||
add_localvar(luaI_createfixedstring("self"));
|
||||
}
|
||||
;
|
||||
|
||||
body : '(' parlist ')' block END
|
||||
{
|
||||
codereturn();
|
||||
$$ = new(TFunc);
|
||||
luaI_initTFunc($$);
|
||||
$$->size = pc;
|
||||
$$->code = newvector(pc, Byte);
|
||||
$$->lineDefined = $2;
|
||||
memcpy($$->code, basepc, pc*sizeof(Byte));
|
||||
if (lua_debug)
|
||||
luaI_closelocalvars($$);
|
||||
/* save func values */
|
||||
funcCode = basepc; maxcode=maxcurr;
|
||||
#if LISTING
|
||||
PrintCode(funcCode,funcCode+pc);
|
||||
#endif
|
||||
change2main(); /* change back to main code */
|
||||
}
|
||||
;
|
||||
|
||||
statlist : /* empty */
|
||||
| statlist stat sc
|
||||
;
|
||||
|
||||
sc : /* empty */ | ';' ;
|
||||
|
||||
stat : IF expr1 THEN PrepJump block PrepJump elsepart END
|
||||
{ codeIf($4, $6); }
|
||||
|
||||
| WHILE {$<vLong>$=pc;} expr1 DO PrepJump block PrepJump END
|
||||
{
|
||||
basepc[$5] = IFFJMP;
|
||||
code_word_at(basepc+$5+1, pc - ($5 + sizeof(Word)+1));
|
||||
basepc[$7] = UPJMP;
|
||||
code_word_at(basepc+$7+1, pc - ($<vLong>2));
|
||||
}
|
||||
|
||||
| REPEAT {$<vLong>$=pc;} block UNTIL expr1 PrepJump
|
||||
{
|
||||
basepc[$6] = IFFUPJMP;
|
||||
code_word_at(basepc+$6+1, pc - ($<vLong>2));
|
||||
}
|
||||
|
||||
| varlist1 '=' exprlist1
|
||||
{
|
||||
{
|
||||
int i;
|
||||
adjust_mult_assign(nvarbuffer, $3, $1 * 2 + nvarbuffer);
|
||||
for (i=nvarbuffer-1; i>=0; i--)
|
||||
lua_codestore (i);
|
||||
if ($1 > 1 || ($1 == 1 && varbuffer[0] != 0))
|
||||
lua_codeadjust (0);
|
||||
}
|
||||
}
|
||||
| functioncall {;}
|
||||
| LOCAL localdeclist decinit
|
||||
{ nlocalvar += $2;
|
||||
adjust_mult_assign($2, $3, 0);
|
||||
}
|
||||
;
|
||||
|
||||
elsepart : /* empty */
|
||||
| ELSE block
|
||||
| ELSEIF expr1 THEN PrepJump block PrepJump elsepart
|
||||
{ codeIf($4, $6); }
|
||||
;
|
||||
|
||||
block : {$<vInt>$ = nlocalvar;} statlist ret
|
||||
{
|
||||
if (nlocalvar != $<vInt>1)
|
||||
{
|
||||
if (lua_debug)
|
||||
for (; nlocalvar > $<vInt>1; nlocalvar--)
|
||||
luaI_unregisterlocalvar(lua_linenumber);
|
||||
else
|
||||
nlocalvar = $<vInt>1;
|
||||
lua_codeadjust (0);
|
||||
}
|
||||
}
|
||||
;
|
||||
|
||||
ret : /* empty */
|
||||
| RETURN exprlist sc
|
||||
{
|
||||
adjust_functioncall($2, MULT_RET);
|
||||
codereturn();
|
||||
}
|
||||
;
|
||||
|
||||
PrepJump : /* empty */
|
||||
{
|
||||
$$ = pc;
|
||||
code_byte(0); /* open space */
|
||||
code_word (0);
|
||||
}
|
||||
;
|
||||
|
||||
expr1 : expr { adjust_functioncall($1, 1); }
|
||||
;
|
||||
|
||||
expr : '(' expr ')' { $$ = $2; }
|
||||
| expr1 EQ expr1 { code_byte(EQOP); $$ = 0; }
|
||||
| expr1 '<' expr1 { code_byte(LTOP); $$ = 0; }
|
||||
| expr1 '>' expr1 { code_byte(GTOP); $$ = 0; }
|
||||
| expr1 NE expr1 { code_byte(EQOP); code_byte(NOTOP); $$ = 0; }
|
||||
| expr1 LE expr1 { code_byte(LEOP); $$ = 0; }
|
||||
| expr1 GE expr1 { code_byte(GEOP); $$ = 0; }
|
||||
| expr1 '+' expr1 { code_byte(ADDOP); $$ = 0; }
|
||||
| expr1 '-' expr1 { code_byte(SUBOP); $$ = 0; }
|
||||
| expr1 '*' expr1 { code_byte(MULTOP); $$ = 0; }
|
||||
| expr1 '/' expr1 { code_byte(DIVOP); $$ = 0; }
|
||||
| expr1 '^' expr1 { code_byte(POWOP); $$ = 0; }
|
||||
| expr1 CONC expr1 { code_byte(CONCOP); $$ = 0; }
|
||||
| '-' expr1 %prec UNARY { code_byte(MINUSOP); $$ = 0;}
|
||||
| table { $$ = 0; }
|
||||
| varexp { $$ = 0;}
|
||||
| NUMBER { code_number($1); $$ = 0; }
|
||||
| STRING
|
||||
{
|
||||
code_string($1);
|
||||
$$ = 0;
|
||||
}
|
||||
| NIL {code_byte(PUSHNIL); $$ = 0; }
|
||||
| functioncall { $$ = $1; }
|
||||
| NOT expr1 { code_byte(NOTOP); $$ = 0;}
|
||||
| expr1 AND PrepJump {code_byte(POP); } expr1
|
||||
{
|
||||
basepc[$3] = ONFJMP;
|
||||
code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1));
|
||||
$$ = 0;
|
||||
}
|
||||
| expr1 OR PrepJump {code_byte(POP); } expr1
|
||||
{
|
||||
basepc[$3] = ONTJMP;
|
||||
code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1));
|
||||
$$ = 0;
|
||||
}
|
||||
;
|
||||
|
||||
table :
|
||||
{
|
||||
code_byte(CREATEARRAY);
|
||||
$<vLong>$ = pc; code_word(0);
|
||||
}
|
||||
'{' fieldlist '}'
|
||||
{
|
||||
code_word_at(basepc+$<vLong>1, $3);
|
||||
}
|
||||
;
|
||||
|
||||
functioncall : funcvalue funcParams
|
||||
{
|
||||
code_byte(CALLFUNC);
|
||||
code_byte($1+$2);
|
||||
$$ = pc;
|
||||
code_byte(0); /* may be modified by other rules */
|
||||
}
|
||||
;
|
||||
|
||||
funcvalue : varexp { $$ = 0; }
|
||||
| varexp ':' NAME
|
||||
{
|
||||
code_byte(PUSHSELF);
|
||||
code_word(luaI_findconstant($3));
|
||||
$$ = 1;
|
||||
}
|
||||
;
|
||||
|
||||
funcParams : '(' exprlist ')'
|
||||
{ $$ = adjust_functioncall($2, 1); }
|
||||
| table { $$ = 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
|
||||
{
|
||||
adjust_functioncall($4, $<vLong>3);
|
||||
$$ = $4;
|
||||
}
|
||||
}
|
||||
;
|
||||
|
||||
parlist : /* empty */ { $$ = close_parlist(0); }
|
||||
| parlist1 { $$ = close_parlist($1); }
|
||||
;
|
||||
|
||||
parlist1 : par { $$ = $1; }
|
||||
| parlist1 ',' par
|
||||
{
|
||||
if ($1)
|
||||
lua_error("invalid parameter list");
|
||||
$$ = $3;
|
||||
}
|
||||
;
|
||||
|
||||
par : NAME { add_localvar($1); $$ = 0; }
|
||||
| DOTS { $$ = 1; }
|
||||
;
|
||||
|
||||
fieldlist : lfieldlist
|
||||
{ flush_list($1/FIELDS_PER_FLUSH, $1%FIELDS_PER_FLUSH); }
|
||||
semicolonpart
|
||||
{ $$ = $1+$3; }
|
||||
| ffieldlist1 lastcomma
|
||||
{ $$ = $1; flush_record($1%FIELDS_PER_FLUSH); }
|
||||
;
|
||||
|
||||
semicolonpart : /* empty */
|
||||
{ $$ = 0; }
|
||||
| ';' ffieldlist
|
||||
{ $$ = $2; flush_record($2%FIELDS_PER_FLUSH); }
|
||||
;
|
||||
|
||||
lastcomma : /* empty */
|
||||
| ','
|
||||
;
|
||||
|
||||
ffieldlist : /* empty */ { $$ = 0; }
|
||||
| ffieldlist1 lastcomma { $$ = $1; }
|
||||
;
|
||||
|
||||
ffieldlist1 : ffield {$$=1;}
|
||||
| ffieldlist1 ',' ffield
|
||||
{
|
||||
$$=$1+1;
|
||||
if ($$%FIELDS_PER_FLUSH == 0) flush_record(FIELDS_PER_FLUSH);
|
||||
}
|
||||
;
|
||||
|
||||
ffield : ffieldkey '=' expr1
|
||||
;
|
||||
|
||||
ffieldkey : '[' expr1 ']'
|
||||
| NAME { code_constant($1); }
|
||||
;
|
||||
|
||||
lfieldlist : /* empty */ { $$ = 0; }
|
||||
| lfieldlist1 lastcomma { $$ = $1; }
|
||||
;
|
||||
|
||||
lfieldlist1 : expr1 {$$=1;}
|
||||
| lfieldlist1 ',' expr1
|
||||
{
|
||||
$$=$1+1;
|
||||
if ($$%FIELDS_PER_FLUSH == 0)
|
||||
flush_list($$/FIELDS_PER_FLUSH - 1, FIELDS_PER_FLUSH);
|
||||
}
|
||||
;
|
||||
|
||||
varlist1 : var
|
||||
{
|
||||
nvarbuffer = 0;
|
||||
add_varbuffer($1);
|
||||
$$ = ($1 == 0) ? 1 : 0;
|
||||
}
|
||||
| varlist1 ',' var
|
||||
{
|
||||
add_varbuffer($3);
|
||||
$$ = ($3 == 0) ? $1 + 1 : $1;
|
||||
}
|
||||
;
|
||||
|
||||
var : singlevar { $$ = $1; }
|
||||
| varexp '[' expr1 ']'
|
||||
{
|
||||
$$ = 0; /* indexed variable */
|
||||
}
|
||||
| varexp '.' NAME
|
||||
{
|
||||
code_constant($3);
|
||||
$$ = 0; /* indexed variable */
|
||||
}
|
||||
;
|
||||
|
||||
singlevar : NAME
|
||||
{
|
||||
int local = lua_localname($1);
|
||||
if (local == -1) /* global var */
|
||||
$$ = luaI_findsymbol($1)+1; /* return positive value */
|
||||
else
|
||||
$$ = -(local+1); /* return negative value */
|
||||
}
|
||||
;
|
||||
|
||||
varexp : var { lua_pushvar($1); }
|
||||
;
|
||||
|
||||
localdeclist : NAME {store_localvar($1, 0); $$ = 1;}
|
||||
| localdeclist ',' NAME
|
||||
{
|
||||
store_localvar($3, $1);
|
||||
$$ = $1+1;
|
||||
}
|
||||
;
|
||||
|
||||
decinit : /* empty */ { $$ = 0; }
|
||||
| '=' exprlist1 { $$ = $2; }
|
||||
;
|
||||
|
||||
%%
|
||||
19
luadebug.h
19
luadebug.h
@@ -1,14 +1,14 @@
|
||||
/*
|
||||
** LUA - Linguagem para Usuarios de Aplicacao
|
||||
** Grupo de Tecnologia em Computacao Grafica
|
||||
** TeCGraf - PUC-Rio
|
||||
** $Id: luadebug.h,v 1.5 1996/02/08 17:03:20 roberto Exp roberto $
|
||||
** $Id: luadebug.h,v 1.5 1999/02/04 17:47:59 roberto Exp roberto $
|
||||
** Debugging API
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#ifndef luadebug_h
|
||||
#define luadebug_h
|
||||
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
typedef lua_Object lua_Function;
|
||||
@@ -17,15 +17,18 @@ 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);
|
||||
void lua_funcinfo (lua_Object func, char **source, 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;
|
||||
int lua_nups (lua_Function func);
|
||||
|
||||
lua_LHFunction lua_setlinehook (lua_LHFunction func);
|
||||
lua_CHFunction lua_setcallhook (lua_CHFunction func);
|
||||
int lua_setdebug (int debug);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
34
lualib.h
34
lualib.h
@@ -1,29 +1,35 @@
|
||||
/*
|
||||
** Libraries to be used in LUA programs
|
||||
** Grupo de Tecnologia em Computacao Grafica
|
||||
** TeCGraf - PUC-Rio
|
||||
** $Id: lualib.h,v 1.12 1997/03/18 15:30:50 roberto Exp roberto $
|
||||
** $Id: lualib.h,v 1.4 1998/06/19 16:14:09 roberto Exp roberto $
|
||||
** Lua standard libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#ifndef lualib_h
|
||||
#define lualib_h
|
||||
|
||||
#include "lua.h"
|
||||
|
||||
void iolib_open (void);
|
||||
void strlib_open (void);
|
||||
void mathlib_open (void);
|
||||
void lua_iolibopen (void);
|
||||
void lua_strlibopen (void);
|
||||
void lua_mathlibopen (void);
|
||||
void lua_dblibopen (void);
|
||||
|
||||
|
||||
void lua_userinit (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) */
|
||||
/* Auxiliary functions (private) */
|
||||
|
||||
char *luaI_addchar (int c);
|
||||
void luaI_emptybuff (void);
|
||||
void luaI_addquoted (char *s);
|
||||
|
||||
char *luaL_item_end (char *p);
|
||||
int luaL_singlematch (int c, char *p);
|
||||
int luaI_singlematch (int c, char *p, char **ep);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
159
luamem.c
159
luamem.c
@@ -1,159 +0,0 @@
|
||||
/*
|
||||
** mem.c
|
||||
** TecCGraf - PUC-Rio
|
||||
*/
|
||||
|
||||
char *rcs_luamem = "$Id: luamem.c,v 1.15 1997/03/31 14:17:09 roberto Exp roberto $";
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "luamem.h"
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
#define DEBUG 0
|
||||
|
||||
#if !DEBUG
|
||||
|
||||
void luaI_free (void *block)
|
||||
{
|
||||
if (block)
|
||||
{
|
||||
*((char *)block) = -1; /* to catch errors */
|
||||
free(block);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void *luaI_realloc (void *oldblock, unsigned long size)
|
||||
{
|
||||
void *block;
|
||||
size_t s = (size_t)size;
|
||||
if (s != size)
|
||||
lua_error("Allocation Error: Block too big");
|
||||
block = oldblock ? realloc(oldblock, s) : malloc(s);
|
||||
if (block == NULL)
|
||||
lua_error(memEM);
|
||||
return block;
|
||||
}
|
||||
|
||||
|
||||
int luaI_growvector (void **block, unsigned long nelems, int size,
|
||||
char *errormsg, unsigned long limit)
|
||||
{
|
||||
if (nelems >= limit)
|
||||
lua_error(errormsg);
|
||||
nelems = (nelems == 0) ? 20 : nelems*2;
|
||||
if (nelems > limit)
|
||||
nelems = limit;
|
||||
*block = luaI_realloc(*block, nelems*size);
|
||||
return (int)nelems;
|
||||
}
|
||||
|
||||
|
||||
void* luaI_buffer (unsigned long size)
|
||||
{
|
||||
static unsigned long buffsize = 0;
|
||||
static char* buffer = NULL;
|
||||
if (size > buffsize)
|
||||
buffer = luaI_realloc(buffer, buffsize=size);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
#else
|
||||
/* DEBUG */
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
# define assert(ex) {if (!(ex)){(void)fprintf(stderr, \
|
||||
"Assertion failed: file \"%s\", line %d\n", __FILE__, __LINE__);exit(1);}}
|
||||
|
||||
#define MARK 55
|
||||
|
||||
static unsigned long numblocks = 0;
|
||||
static unsigned long totalmem = 0;
|
||||
|
||||
|
||||
static void message (void)
|
||||
{
|
||||
#define inrange(x,y) ((x) < (((y)*3)/2) && (x) > (((y)*2)/3))
|
||||
static int count = 0;
|
||||
static unsigned long lastnumblocks = 0;
|
||||
static unsigned long lasttotalmem = 0;
|
||||
if (!inrange(numblocks, lastnumblocks) || !inrange(totalmem, lasttotalmem)
|
||||
|| count++ >= 5000)
|
||||
{
|
||||
fprintf(stderr,"blocks = %lu mem = %luK\n", numblocks, totalmem/1000);
|
||||
count = 0;
|
||||
lastnumblocks = numblocks;
|
||||
lasttotalmem = totalmem;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaI_free (void *block)
|
||||
{
|
||||
if (block)
|
||||
{
|
||||
unsigned long *b = (unsigned long *)block - 1;
|
||||
unsigned long size = *b;
|
||||
assert(*(((char *)b)+size+sizeof(unsigned long)) == MARK);
|
||||
numblocks--;
|
||||
totalmem -= size;
|
||||
free(b);
|
||||
message();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void *luaI_realloc (void *oldblock, unsigned long size)
|
||||
{
|
||||
unsigned long *block;
|
||||
unsigned long realsize = sizeof(unsigned long)+size+sizeof(char);
|
||||
if (realsize != (size_t)realsize)
|
||||
lua_error("Allocation Error: Block too big");
|
||||
if (oldblock)
|
||||
{
|
||||
unsigned long *b = (unsigned long *)oldblock - 1;
|
||||
unsigned long oldsize = *b;
|
||||
assert(*(((char *)b)+oldsize+sizeof(unsigned long)) == MARK);
|
||||
totalmem -= oldsize;
|
||||
numblocks--;
|
||||
block = (unsigned long *)realloc(b, realsize);
|
||||
}
|
||||
else
|
||||
block = (unsigned long *)malloc(realsize);
|
||||
if (block == NULL)
|
||||
lua_error("not enough memory");
|
||||
totalmem += size;
|
||||
numblocks++;
|
||||
*block = size;
|
||||
*(((char *)block)+size+sizeof(unsigned long)) = MARK;
|
||||
message();
|
||||
return block+1;
|
||||
}
|
||||
|
||||
|
||||
int luaI_growvector (void **block, unsigned long nelems, int size,
|
||||
char *errormsg, unsigned long limit)
|
||||
{
|
||||
if (nelems >= limit)
|
||||
lua_error(errormsg);
|
||||
nelems = (nelems == 0) ? 20 : nelems*2;
|
||||
if (nelems > limit)
|
||||
nelems = limit;
|
||||
*block = luaI_realloc(*block, nelems*size);
|
||||
return (int)nelems;
|
||||
}
|
||||
|
||||
|
||||
void* luaI_buffer (unsigned long size)
|
||||
{
|
||||
static unsigned long buffsize = 0;
|
||||
static char* buffer = NULL;
|
||||
if (size > buffsize)
|
||||
buffer = luaI_realloc(buffer, buffsize=size);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
#endif
|
||||
39
luamem.h
39
luamem.h
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
** mem.c
|
||||
** memory manager for lua
|
||||
** $Id: luamem.h,v 1.8 1996/05/24 14:31:10 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef luamem_h
|
||||
#define luamem_h
|
||||
|
||||
#ifndef NULL
|
||||
#define NULL 0
|
||||
#endif
|
||||
|
||||
|
||||
/* memory error messages */
|
||||
#define codeEM "code size overflow"
|
||||
#define symbolEM "symbol table overflow"
|
||||
#define constantEM "constant table overflow"
|
||||
#define stackEM "stack size overflow"
|
||||
#define lexEM "lex buffer overflow"
|
||||
#define refEM "reference table overflow"
|
||||
#define tableEM "table overflow"
|
||||
#define memEM "not enough memory"
|
||||
|
||||
|
||||
void luaI_free (void *block);
|
||||
void *luaI_realloc (void *oldblock, unsigned long size);
|
||||
void *luaI_buffer (unsigned long size);
|
||||
int luaI_growvector (void **block, unsigned long nelems, int size,
|
||||
char *errormsg, unsigned long limit);
|
||||
|
||||
#define luaI_malloc(s) luaI_realloc(NULL, (s))
|
||||
#define new(s) ((s *)luaI_malloc(sizeof(s)))
|
||||
#define newvector(n,s) ((s *)luaI_malloc((n)*sizeof(s)))
|
||||
#define growvector(old,n,s,e,l) \
|
||||
(luaI_growvector((void**)old,n,sizeof(s),e,l))
|
||||
|
||||
#endif
|
||||
|
||||
234
lundump.c
Normal file
234
lundump.c
Normal file
@@ -0,0 +1,234 @@
|
||||
/*
|
||||
** $Id: lundump.c,v 1.18 1999/04/09 03:10:40 lhf Exp lhf $
|
||||
** load bytecodes from files
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "lauxlib.h"
|
||||
#include "lfunc.h"
|
||||
#include "lmem.h"
|
||||
#include "lopcodes.h"
|
||||
#include "lstring.h"
|
||||
#include "lundump.h"
|
||||
|
||||
#define LoadBlock(b,size,Z) ezread(Z,b,size)
|
||||
|
||||
#if LUAC_NATIVE
|
||||
#define doLoadNumber(x,Z) LoadBlock(&x,sizeof(x),Z)
|
||||
#else
|
||||
#define doLoadNumber(x,Z) x=LoadNumber(Z)
|
||||
#endif
|
||||
|
||||
static void unexpectedEOZ (ZIO* Z)
|
||||
{
|
||||
luaL_verror("unexpected end of file in %s",zname(Z));
|
||||
}
|
||||
|
||||
static int ezgetc (ZIO* Z)
|
||||
{
|
||||
int c=zgetc(Z);
|
||||
if (c==EOZ) unexpectedEOZ(Z);
|
||||
return c;
|
||||
}
|
||||
|
||||
static void ezread (ZIO* Z, void* b, int n)
|
||||
{
|
||||
int r=zread(Z,b,n);
|
||||
if (r!=0) unexpectedEOZ(Z);
|
||||
}
|
||||
|
||||
static unsigned int LoadWord (ZIO* Z)
|
||||
{
|
||||
unsigned int hi=ezgetc(Z);
|
||||
unsigned int lo=ezgetc(Z);
|
||||
return (hi<<8)|lo;
|
||||
}
|
||||
|
||||
static unsigned long LoadLong (ZIO* Z)
|
||||
{
|
||||
unsigned long hi=LoadWord(Z);
|
||||
unsigned long lo=LoadWord(Z);
|
||||
return (hi<<16)|lo;
|
||||
}
|
||||
|
||||
static real LoadNumber (ZIO* Z)
|
||||
{
|
||||
char b[256];
|
||||
int size=ezgetc(Z);
|
||||
LoadBlock(b,size,Z);
|
||||
b[size]=0;
|
||||
if (b[0]=='-')
|
||||
return -luaO_str2d(b+1);
|
||||
else
|
||||
return luaO_str2d(b);
|
||||
}
|
||||
|
||||
static int LoadInt (ZIO* Z, char* message)
|
||||
{
|
||||
unsigned long l=LoadLong(Z);
|
||||
unsigned int i=l;
|
||||
if (i!=l) luaL_verror(message,l,zname(Z));
|
||||
return i;
|
||||
}
|
||||
|
||||
#define PAD 5 /* two word operands plus opcode */
|
||||
|
||||
static Byte* LoadCode (ZIO* Z)
|
||||
{
|
||||
int size=LoadInt(Z,"code too long (%ld bytes) in %s");
|
||||
Byte* b=luaM_malloc(size+PAD);
|
||||
LoadBlock(b,size,Z);
|
||||
if (b[size-1]!=ENDCODE) luaL_verror("bad code in %s",zname(Z));
|
||||
memset(b+size,ENDCODE,PAD); /* pad code for safety */
|
||||
return b;
|
||||
}
|
||||
|
||||
static TaggedString* LoadTString (ZIO* Z)
|
||||
{
|
||||
long size=LoadLong(Z);
|
||||
if (size==0)
|
||||
return NULL;
|
||||
else
|
||||
{
|
||||
char* s=luaL_openspace(size);
|
||||
LoadBlock(s,size,Z);
|
||||
return luaS_newlstr(s,size-1);
|
||||
}
|
||||
}
|
||||
|
||||
static void LoadLocals (TProtoFunc* tf, ZIO* Z)
|
||||
{
|
||||
int i,n=LoadInt(Z,"too many locals (%ld) in %s");
|
||||
if (n==0) return;
|
||||
tf->locvars=luaM_newvector(n+1,LocVar);
|
||||
for (i=0; i<n; i++)
|
||||
{
|
||||
tf->locvars[i].line=LoadInt(Z,"too many lines (%ld) in %s");
|
||||
tf->locvars[i].varname=LoadTString(Z);
|
||||
}
|
||||
tf->locvars[i].line=-1; /* flag end of vector */
|
||||
tf->locvars[i].varname=NULL;
|
||||
}
|
||||
|
||||
static TProtoFunc* LoadFunction (ZIO* Z);
|
||||
|
||||
static void LoadConstants (TProtoFunc* tf, ZIO* Z)
|
||||
{
|
||||
int i,n=LoadInt(Z,"too many constants (%ld) in %s");
|
||||
tf->nconsts=n;
|
||||
if (n==0) return;
|
||||
tf->consts=luaM_newvector(n,TObject);
|
||||
for (i=0; i<n; i++)
|
||||
{
|
||||
TObject* o=tf->consts+i;
|
||||
ttype(o)=-ezgetc(Z); /* ttype(o) is negative - ORDER LUA_T */
|
||||
switch (ttype(o))
|
||||
{
|
||||
case LUA_T_NUMBER:
|
||||
doLoadNumber(nvalue(o),Z);
|
||||
break;
|
||||
case LUA_T_STRING:
|
||||
tsvalue(o)=LoadTString(Z);
|
||||
break;
|
||||
case LUA_T_PROTO:
|
||||
tfvalue(o)=LoadFunction(Z);
|
||||
break;
|
||||
case LUA_T_NIL:
|
||||
break;
|
||||
default: /* cannot happen */
|
||||
luaU_badconstant("load",i,o,tf);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static TProtoFunc* LoadFunction (ZIO* Z)
|
||||
{
|
||||
TProtoFunc* tf=luaF_newproto();
|
||||
tf->lineDefined=LoadInt(Z,"lineDefined too large (%ld) in %s");
|
||||
tf->source=LoadTString(Z);
|
||||
if (tf->source==NULL) tf->source=luaS_new(zname(Z));
|
||||
tf->code=LoadCode(Z);
|
||||
LoadLocals(tf,Z);
|
||||
LoadConstants(tf,Z);
|
||||
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 %s",zname(Z));
|
||||
}
|
||||
|
||||
static void LoadHeader (ZIO* Z)
|
||||
{
|
||||
int version,sizeofR;
|
||||
LoadSignature(Z);
|
||||
version=ezgetc(Z);
|
||||
if (version>VERSION)
|
||||
luaL_verror(
|
||||
"%s too new: version=0x%02x; expected at most 0x%02x",
|
||||
zname(Z),version,VERSION);
|
||||
if (version<VERSION0) /* check last major change */
|
||||
luaL_verror(
|
||||
"%s too old: version=0x%02x; expected at least 0x%02x",
|
||||
zname(Z),version,VERSION0);
|
||||
sizeofR=ezgetc(Z); /* test number representation */
|
||||
#if LUAC_NATIVE
|
||||
if (sizeofR==0)
|
||||
luaL_verror("cannot read numbers in %s: "
|
||||
"support for decimal format not enabled",
|
||||
zname(Z));
|
||||
if (sizeofR!=sizeof(real))
|
||||
luaL_verror("unknown number size in %s: read %d; expected %d",
|
||||
zname(Z),sizeofR,sizeof(real));
|
||||
else
|
||||
{
|
||||
real f=-TEST_NUMBER,tf=TEST_NUMBER;
|
||||
doLoadNumber(f,Z);
|
||||
if (f!=tf)
|
||||
luaL_verror("unknown number representation in %s: "
|
||||
"read " NUMBER_FMT "; expected " NUMBER_FMT,
|
||||
zname(Z),f,tf);
|
||||
}
|
||||
#else
|
||||
if (sizeofR!=0)
|
||||
luaL_verror("cannot read numbers in %s: "
|
||||
"support for native format not enabled",
|
||||
zname(Z));
|
||||
#endif
|
||||
}
|
||||
|
||||
static TProtoFunc* LoadChunk (ZIO* Z)
|
||||
{
|
||||
LoadHeader(Z);
|
||||
return LoadFunction(Z);
|
||||
}
|
||||
|
||||
/*
|
||||
** 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);
|
||||
if (c==ID_CHUNK)
|
||||
return LoadChunk(Z);
|
||||
else if (c!=EOZ)
|
||||
luaL_verror("%s is not a Lua binary file",zname(Z));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* handle constants that cannot happen
|
||||
*/
|
||||
void luaU_badconstant (char* s, int i, TObject* o, TProtoFunc* tf)
|
||||
{
|
||||
int t=ttype(o);
|
||||
char* name= (t>0 || t<LUA_T_LINE) ? "?" : luaO_typenames[-t];
|
||||
luaL_verror("cannot %s constant #%d: type=%d [%s]" IN,s,i,t,name,INLOC);
|
||||
}
|
||||
48
lundump.h
Normal file
48
lundump.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
** $Id: lundump.h,v 1.13 1999/03/29 16:16:18 lhf Exp lhf $
|
||||
** load pre-compiled Lua chunks
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lundump_h
|
||||
#define lundump_h
|
||||
|
||||
#include "lobject.h"
|
||||
#include "lzio.h"
|
||||
|
||||
TProtoFunc* luaU_undump1 (ZIO* Z); /* load one chunk */
|
||||
void luaU_badconstant (char* s, int i, TObject* o, TProtoFunc* tf);
|
||||
/* handle cases that cannot happen */
|
||||
|
||||
/* definitions for headers of binary files */
|
||||
#define VERSION 0x32 /* last format change was in 3.2 */
|
||||
#define VERSION0 0x32 /* last major change was in 3.2 */
|
||||
#define ID_CHUNK 27 /* binary files start with ESC... */
|
||||
#define SIGNATURE "Lua" /* ...followed by this signature */
|
||||
|
||||
/* formats for error messages */
|
||||
#define SOURCE "<%s:%d>"
|
||||
#define IN " in %p " SOURCE
|
||||
#define INLOC tf,tf->source->str,tf->lineDefined
|
||||
|
||||
/* format for numbers in listings and error messages */
|
||||
#ifndef NUMBER_FMT
|
||||
#define NUMBER_FMT "%.16g" /* LUA_NUMBER */
|
||||
#endif
|
||||
|
||||
/* LUA_NUMBER
|
||||
* by default, numbers are stored in precompiled chunks as decimal strings.
|
||||
* this is completely portable and fast enough for most applications.
|
||||
* if you want to use this default, do nothing.
|
||||
* if you want additional speed at the expense of portability, move the line
|
||||
* below out of this comment.
|
||||
#define LUAC_NATIVE
|
||||
*/
|
||||
|
||||
#ifdef LUAC_NATIVE
|
||||
/* a multiple of PI for testing number representation */
|
||||
/* multiplying by 1E8 gives non-trivial integer values */
|
||||
#define TEST_NUMBER 3.14159265358979323846E8
|
||||
#endif
|
||||
|
||||
#endif
|
||||
632
lvm.c
Normal file
632
lvm.c
Normal file
@@ -0,0 +1,632 @@
|
||||
/*
|
||||
** $Id: lvm.c,v 1.54 1999/03/10 14:09:45 roberto Exp roberto $
|
||||
** Lua virtual machine
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lauxlib.h"
|
||||
#include "ldo.h"
|
||||
#include "lfunc.h"
|
||||
#include "lgc.h"
|
||||
#include "lmem.h"
|
||||
#include "lobject.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 highbyte(x) ((x)<<8)
|
||||
|
||||
|
||||
/* Extra stack size to run a function: LUA_T_LINE(1), TM calls(2), ... */
|
||||
#define EXTRA_STACK 5
|
||||
|
||||
|
||||
|
||||
static TaggedString *strconc (TaggedString *l, TaggedString *r) {
|
||||
long nl = l->u.s.len;
|
||||
long nr = r->u.s.len;
|
||||
char *buffer = luaL_openspace(nl+nr);
|
||||
memcpy(buffer, l->str, nl);
|
||||
memcpy(buffer+nl, r->str, nr);
|
||||
return luaS_newlstr(buffer, nl+nr);
|
||||
}
|
||||
|
||||
|
||||
int luaV_tonumber (TObject *obj) { /* LUA_NUMBER */
|
||||
if (ttype(obj) != LUA_T_STRING)
|
||||
return 1;
|
||||
else {
|
||||
double t;
|
||||
char *e = svalue(obj);
|
||||
int sig = 1;
|
||||
while (isspace((unsigned char)*e)) e++;
|
||||
if (*e == '-') {
|
||||
e++;
|
||||
sig = -1;
|
||||
}
|
||||
else if (*e == '+') e++;
|
||||
/* no digit before or after decimal point? */
|
||||
if (!isdigit((unsigned char)*e) && !isdigit((unsigned char)*(e+1)))
|
||||
return 2;
|
||||
t = luaO_str2d(e);
|
||||
if (t<0) return 2;
|
||||
nvalue(obj) = (real)t*sig;
|
||||
ttype(obj) = LUA_T_NUMBER;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int luaV_tostring (TObject *obj) { /* LUA_NUMBER */
|
||||
if (ttype(obj) != LUA_T_NUMBER)
|
||||
return 1;
|
||||
else {
|
||||
char s[32]; /* 16 digits, signal, point and \0 (+ some extra...) */
|
||||
sprintf(s, "%.16g", (double)nvalue(obj));
|
||||
tsvalue(obj) = luaS_new(s);
|
||||
ttype(obj) = LUA_T_STRING;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaV_setn (Hash *t, int val) {
|
||||
TObject index, value;
|
||||
ttype(&index) = LUA_T_STRING; tsvalue(&index) = luaS_new("n");
|
||||
ttype(&value) = LUA_T_NUMBER; nvalue(&value) = val;
|
||||
luaH_set(t, &index, &value);
|
||||
}
|
||||
|
||||
|
||||
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) {
|
||||
TObject *table = L->stack.top-2;
|
||||
TObject *im;
|
||||
if (ttype(table) != LUA_T_ARRAY) { /* not a table, get gettable method */
|
||||
im = luaT_getimbyObj(table, IM_GETTABLE);
|
||||
if (ttype(im) == LUA_T_NIL)
|
||||
lua_error("indexed expression not a table");
|
||||
}
|
||||
else { /* object is a table... */
|
||||
int tg = table->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(table), table+1);
|
||||
if (ttype(h) == LUA_T_NIL &&
|
||||
(ttype(im=luaT_getim(tg, IM_INDEX)) != LUA_T_NIL)) {
|
||||
/* result is nil and there is an "index" tag method */
|
||||
luaD_callTM(im, 2, 1); /* calls it */
|
||||
}
|
||||
else {
|
||||
L->stack.top--;
|
||||
*table = *h; /* "push" result into table position */
|
||||
}
|
||||
return;
|
||||
}
|
||||
/* else it has a "gettable" method, go through to next command */
|
||||
}
|
||||
/* object is not a table, or it has a "gettable" method */
|
||||
luaD_callTM(im, 2, 1);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Receives table at *t, index at *(t+1) and value at top.
|
||||
*/
|
||||
void luaV_settable (TObject *t) {
|
||||
struct Stack *S = &L->stack;
|
||||
TObject *im;
|
||||
if (ttype(t) != LUA_T_ARRAY) { /* not a table, get "settable" method */
|
||||
im = luaT_getimbyObj(t, IM_SETTABLE);
|
||||
if (ttype(im) == LUA_T_NIL)
|
||||
lua_error("indexed expression not a table");
|
||||
}
|
||||
else { /* object is a table... */
|
||||
im = luaT_getim(avalue(t)->htag, IM_SETTABLE);
|
||||
if (ttype(im) == LUA_T_NIL) { /* and does not have a "settable" method */
|
||||
luaH_set(avalue(t), t+1, S->top-1);
|
||||
S->top--; /* pop value */
|
||||
return;
|
||||
}
|
||||
/* else it has a "settable" method, go through to next command */
|
||||
}
|
||||
/* object is not a table, or it has a "settable" method */
|
||||
/* prepare arguments and call the tag method */
|
||||
*(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);
|
||||
}
|
||||
|
||||
|
||||
void luaV_rawsettable (TObject *t) {
|
||||
if (ttype(t) != LUA_T_ARRAY)
|
||||
lua_error("indexed expression not a table");
|
||||
else {
|
||||
struct Stack *S = &L->stack;
|
||||
luaH_set(avalue(t), t+1, S->top-1);
|
||||
S->top -= 3;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaV_getglobal (TaggedString *ts) {
|
||||
/* WARNING: caller must assure stack space */
|
||||
/* only userdata, tables and nil can have getglobal tag methods */
|
||||
static char valid_getglobals[] = {1, 0, 0, 1, 0, 0, 1, 0}; /* ORDER LUA_T */
|
||||
TObject *value = &ts->u.s.globalval;
|
||||
if (valid_getglobals[-ttype(value)]) {
|
||||
TObject *im = luaT_getimbyObj(value, IM_GETGLOBAL);
|
||||
if (ttype(im) != LUA_T_NIL) { /* is there a tag method? */
|
||||
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);
|
||||
return;
|
||||
}
|
||||
/* else no tag method: go through to default behavior */
|
||||
}
|
||||
*L->stack.top++ = *value; /* default behavior */
|
||||
}
|
||||
|
||||
|
||||
void luaV_setglobal (TaggedString *ts) {
|
||||
TObject *oldvalue = &ts->u.s.globalval;
|
||||
TObject *im = luaT_getimbyObj(oldvalue, IM_SETGLOBAL);
|
||||
if (ttype(im) == LUA_T_NIL) /* is there a tag method? */
|
||||
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 int luaV_strcomp (char *l, long ll, char *r, long lr)
|
||||
{
|
||||
for (;;) {
|
||||
long temp = strcoll(l, r);
|
||||
if (temp != 0) return temp;
|
||||
/* strings are equal up to a '\0' */
|
||||
temp = strlen(l); /* index of first '\0' in both strings */
|
||||
if (temp == ll) /* l is finished? */
|
||||
return (temp == lr) ? 0 : -1; /* l is equal or smaller than r */
|
||||
else if (temp == lr) /* r is finished? */
|
||||
return 1; /* l is greater than r (because l is not finished) */
|
||||
/* both strings longer than temp; go on comparing (after the '\0') */
|
||||
temp++;
|
||||
l += temp; ll -= temp; r += temp; lr -= temp;
|
||||
}
|
||||
}
|
||||
|
||||
void luaV_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;
|
||||
real result;
|
||||
if (ttype(l) == LUA_T_NUMBER && ttype(r) == LUA_T_NUMBER)
|
||||
result = nvalue(l)-nvalue(r);
|
||||
else if (ttype(l) == LUA_T_STRING && ttype(r) == LUA_T_STRING)
|
||||
result = luaV_strcomp(svalue(l), tsvalue(l)->u.s.len,
|
||||
svalue(r), tsvalue(r)->u.s.len);
|
||||
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;
|
||||
Hash *htab;
|
||||
if (nvararg < 0) nvararg = 0;
|
||||
htab = avalue(tab) = luaH_new(nvararg+1); /* +1 for field 'n' */
|
||||
ttype(tab) = LUA_T_ARRAY;
|
||||
for (i=0; i<nvararg; i++)
|
||||
luaH_setint(htab, i+1, firstelem+i);
|
||||
luaV_setn(htab, nvararg); /* store counter in field "n" */
|
||||
}
|
||||
|
||||
|
||||
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 */
|
||||
register Byte *pc = tf->code;
|
||||
TObject *consts = tf->consts;
|
||||
if (L->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);
|
||||
}
|
||||
for (;;) {
|
||||
register int aux = 0;
|
||||
switchentry:
|
||||
switch ((OpCode)*pc++) {
|
||||
|
||||
case ENDCODE:
|
||||
S->top = S->stack + base;
|
||||
goto ret;
|
||||
|
||||
case RETCODE:
|
||||
base += *pc++;
|
||||
goto ret;
|
||||
|
||||
case CALL: aux = *pc++;
|
||||
luaD_call((S->top-S->stack)-(*pc++), aux);
|
||||
break;
|
||||
|
||||
case TAILCALL: aux = *pc++;
|
||||
luaD_call((S->top-S->stack)-(*pc++), MULT_RET);
|
||||
base += aux;
|
||||
goto ret;
|
||||
|
||||
case PUSHNIL: aux = *pc++;
|
||||
do {
|
||||
ttype(S->top++) = LUA_T_NIL;
|
||||
} while (aux--);
|
||||
break;
|
||||
|
||||
case POP: aux = *pc++;
|
||||
S->top -= aux;
|
||||
break;
|
||||
|
||||
case PUSHNUMBERW: aux += highbyte(*pc++);
|
||||
case PUSHNUMBER: aux += *pc++;
|
||||
ttype(S->top) = LUA_T_NUMBER;
|
||||
nvalue(S->top) = aux;
|
||||
S->top++;
|
||||
break;
|
||||
|
||||
case PUSHNUMBERNEGW: aux += highbyte(*pc++);
|
||||
case PUSHNUMBERNEG: aux += *pc++;
|
||||
ttype(S->top) = LUA_T_NUMBER;
|
||||
nvalue(S->top) = -aux;
|
||||
S->top++;
|
||||
break;
|
||||
|
||||
case PUSHCONSTANTW: aux += highbyte(*pc++);
|
||||
case PUSHCONSTANT: aux += *pc++;
|
||||
*S->top++ = consts[aux];
|
||||
break;
|
||||
|
||||
case PUSHUPVALUE: aux = *pc++;
|
||||
*S->top++ = cl->consts[aux+1];
|
||||
break;
|
||||
|
||||
case PUSHLOCAL: aux = *pc++;
|
||||
*S->top++ = *((S->stack+base) + aux);
|
||||
break;
|
||||
|
||||
case GETGLOBALW: aux += highbyte(*pc++);
|
||||
case GETGLOBAL: aux += *pc++;
|
||||
luaV_getglobal(tsvalue(&consts[aux]));
|
||||
break;
|
||||
|
||||
case GETTABLE:
|
||||
luaV_gettable();
|
||||
break;
|
||||
|
||||
case GETDOTTEDW: aux += highbyte(*pc++);
|
||||
case GETDOTTED: aux += *pc++;
|
||||
*S->top++ = consts[aux];
|
||||
luaV_gettable();
|
||||
break;
|
||||
|
||||
case PUSHSELFW: aux += highbyte(*pc++);
|
||||
case PUSHSELF: aux += *pc++; {
|
||||
TObject receiver = *(S->top-1);
|
||||
*S->top++ = consts[aux];
|
||||
luaV_gettable();
|
||||
*S->top++ = receiver;
|
||||
break;
|
||||
}
|
||||
|
||||
case CREATEARRAYW: aux += highbyte(*pc++);
|
||||
case CREATEARRAY: aux += *pc++;
|
||||
luaC_checkGC();
|
||||
avalue(S->top) = luaH_new(aux);
|
||||
ttype(S->top) = LUA_T_ARRAY;
|
||||
S->top++;
|
||||
break;
|
||||
|
||||
case SETLOCAL: aux = *pc++;
|
||||
*((S->stack+base) + aux) = *(--S->top);
|
||||
break;
|
||||
|
||||
case SETGLOBALW: aux += highbyte(*pc++);
|
||||
case SETGLOBAL: aux += *pc++;
|
||||
luaV_setglobal(tsvalue(&consts[aux]));
|
||||
break;
|
||||
|
||||
case SETTABLEPOP:
|
||||
luaV_settable(S->top-3);
|
||||
S->top -= 2; /* pop table and index */
|
||||
break;
|
||||
|
||||
case SETTABLE:
|
||||
luaV_settable(S->top-3-(*pc++));
|
||||
break;
|
||||
|
||||
case SETLISTW: aux += highbyte(*pc++);
|
||||
case SETLIST: aux += *pc++; {
|
||||
int n = *(pc++);
|
||||
TObject *arr = S->top-n-1;
|
||||
aux *= LFIELDS_PER_FLUSH;
|
||||
for (; n; n--)
|
||||
luaH_setint(avalue(arr), n+aux, --S->top);
|
||||
break;
|
||||
}
|
||||
|
||||
case SETMAP: aux = *pc++; {
|
||||
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 NEQOP: aux = 1;
|
||||
case EQOP: {
|
||||
int res = luaO_equalObj(S->top-2, S->top-1);
|
||||
if (aux) res = !res;
|
||||
S->top--;
|
||||
ttype(S->top-1) = res ? LUA_T_NUMBER : LUA_T_NIL;
|
||||
nvalue(S->top-1) = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
case LTOP:
|
||||
luaV_comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, IM_LT);
|
||||
break;
|
||||
|
||||
case LEOP:
|
||||
luaV_comparison(LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, IM_LE);
|
||||
break;
|
||||
|
||||
case GTOP:
|
||||
luaV_comparison(LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, IM_GT);
|
||||
break;
|
||||
|
||||
case GEOP:
|
||||
luaV_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_binTM(IM_POW, "undefined operation");
|
||||
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(tsvalue(l), tsvalue(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 += highbyte(*pc++);
|
||||
case ONTJMP: aux += *pc++;
|
||||
if (ttype(S->top-1) != LUA_T_NIL) pc += aux;
|
||||
else S->top--;
|
||||
break;
|
||||
|
||||
case ONFJMPW: aux += highbyte(*pc++);
|
||||
case ONFJMP: aux += *pc++;
|
||||
if (ttype(S->top-1) == LUA_T_NIL) pc += aux;
|
||||
else S->top--;
|
||||
break;
|
||||
|
||||
case JMPW: aux += highbyte(*pc++);
|
||||
case JMP: aux += *pc++;
|
||||
pc += aux;
|
||||
break;
|
||||
|
||||
case IFFJMPW: aux += highbyte(*pc++);
|
||||
case IFFJMP: aux += *pc++;
|
||||
if (ttype(--S->top) == LUA_T_NIL) pc += aux;
|
||||
break;
|
||||
|
||||
case IFTUPJMPW: aux += highbyte(*pc++);
|
||||
case IFTUPJMP: aux += *pc++;
|
||||
if (ttype(--S->top) != LUA_T_NIL) pc -= aux;
|
||||
break;
|
||||
|
||||
case IFFUPJMPW: aux += highbyte(*pc++);
|
||||
case IFFUPJMP: aux += *pc++;
|
||||
if (ttype(--S->top) == LUA_T_NIL) pc -= aux;
|
||||
break;
|
||||
|
||||
case CLOSUREW: aux += highbyte(*pc++);
|
||||
case CLOSURE: aux += *pc++;
|
||||
*S->top++ = consts[aux];
|
||||
luaV_closure(*pc++);
|
||||
luaC_checkGC();
|
||||
break;
|
||||
|
||||
case SETLINEW: aux += highbyte(*pc++);
|
||||
case SETLINE: aux += *pc++;
|
||||
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 (L->linehook)
|
||||
luaD_lineHook(aux);
|
||||
break;
|
||||
|
||||
case LONGARGW: aux += highbyte(*pc++);
|
||||
case LONGARG: aux += *pc++;
|
||||
aux = highbyte(highbyte(aux));
|
||||
goto switchentry; /* do not reset "aux" */
|
||||
|
||||
case CHECKSTACK: aux = *pc++;
|
||||
LUA_ASSERT((S->top-S->stack)-base == aux, "wrong stack size");
|
||||
break;
|
||||
|
||||
}
|
||||
} ret:
|
||||
if (L->callhook)
|
||||
luaD_callHook(0, NULL, 1);
|
||||
return base;
|
||||
}
|
||||
|
||||
34
lvm.h
Normal file
34
lvm.h
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
** $Id: lvm.h,v 1.7 1998/12/30 17:26:49 roberto Exp roberto $
|
||||
** Lua virtual machine
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef lvm_h
|
||||
#define lvm_h
|
||||
|
||||
|
||||
#include "ldo.h"
|
||||
#include "lobject.h"
|
||||
#include "ltm.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_setn (Hash *t, int val);
|
||||
void luaV_gettable (void);
|
||||
void luaV_settable (TObject *t);
|
||||
void luaV_rawsettable (TObject *t);
|
||||
void luaV_getglobal (TaggedString *ts);
|
||||
void luaV_setglobal (TaggedString *ts);
|
||||
StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base);
|
||||
void luaV_closure (int nelems);
|
||||
void luaV_comparison (lua_Type ttype_less, lua_Type ttype_equal,
|
||||
lua_Type ttype_great, IMS op);
|
||||
|
||||
#endif
|
||||
@@ -1,45 +1,50 @@
|
||||
/*
|
||||
* zio.c
|
||||
* a generic input stream interface
|
||||
* $Id: zio.c,v 1.1 1997/06/16 16:50:22 roberto Exp roberto $
|
||||
** $Id: lzio.c,v 1.6 1999/03/04 14:49:18 roberto Exp roberto $
|
||||
** a generic input stream interface
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "zio.h"
|
||||
|
||||
#include "lzio.h"
|
||||
|
||||
|
||||
|
||||
/* ----------------------------------------------------- memory buffers --- */
|
||||
|
||||
static int zmfilbuf(ZIO* z)
|
||||
{
|
||||
static int zmfilbuf (ZIO* z) {
|
||||
return EOZ;
|
||||
}
|
||||
|
||||
ZIO* zmopen(ZIO* z, char* b, int size)
|
||||
|
||||
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)
|
||||
ZIO* zsopen (ZIO* z, char* s, char *name)
|
||||
{
|
||||
if (s==NULL) return NULL;
|
||||
return zmopen(z,s,strlen(s));
|
||||
return zmopen(z,s,strlen(s),name);
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------- FILEs --- */
|
||||
|
||||
static int zffilbuf(ZIO* z)
|
||||
{
|
||||
int n=fread(z->buffer,1,ZBSIZE,z->u);
|
||||
static int zffilbuf (ZIO* z) {
|
||||
int n;
|
||||
if (feof((FILE *)z->u)) return EOZ;
|
||||
n=fread(z->buffer,1,ZBSIZE,z->u);
|
||||
if (n==0) return EOZ;
|
||||
z->n=n-1;
|
||||
z->p=z->buffer;
|
||||
@@ -47,28 +52,28 @@ static int zffilbuf(ZIO* z)
|
||||
}
|
||||
|
||||
|
||||
ZIO* zFopen(ZIO* z, FILE* f)
|
||||
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)
|
||||
{
|
||||
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 */
|
||||
return n; /* return number of missing bytes */
|
||||
zungetc(z); /* put result from 'filbuf' in the buffer */
|
||||
}
|
||||
m = (n <= z->n) ? n : z->n; /* minimo de n e z->n */
|
||||
m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
|
||||
memcpy(b, z->p, m);
|
||||
z->n -= m;
|
||||
z->p += m;
|
||||
@@ -1,11 +1,12 @@
|
||||
/*
|
||||
* zio.h
|
||||
* a generic input stream interface
|
||||
* $Id: zio.h,v 1.4 1997/06/19 18:55:28 roberto Exp roberto $
|
||||
** $Id: lzio.h,v 1.3 1997/12/22 20:57:18 roberto Exp roberto $
|
||||
** Buffered streams
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
#ifndef zio_h
|
||||
#define zio_h
|
||||
|
||||
#ifndef lzio_h
|
||||
#define lzio_h
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
@@ -21,15 +22,15 @@
|
||||
|
||||
typedef struct zio ZIO;
|
||||
|
||||
ZIO* zFopen(ZIO* z, FILE* f); /* open FILEs */
|
||||
ZIO* zsopen(ZIO* z, char* s); /* string */
|
||||
ZIO* zmopen(ZIO* z, char* b, int size); /* memory */
|
||||
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 */
|
||||
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 ------------------ */
|
||||
@@ -41,6 +42,7 @@ struct zio {
|
||||
unsigned char* p; /* current position in buffer */
|
||||
int (*filbuf)(ZIO* z);
|
||||
void* u; /* additional data */
|
||||
char *name;
|
||||
unsigned char buffer[ZBSIZE]; /* buffer */
|
||||
};
|
||||
|
||||
159
makefile
159
makefile
@@ -1,21 +1,40 @@
|
||||
# $Id: makefile,v 1.35 1997/06/16 16:50:22 roberto Exp roberto $
|
||||
#
|
||||
## $Id: makefile,v 1.18 1999/02/23 15:01:29 roberto Exp roberto $
|
||||
## Makefile
|
||||
## See Copyright Notice in lua.h
|
||||
#
|
||||
|
||||
#configuration
|
||||
|
||||
#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) NOSTRERROR if your system does NOT have function "strerror"
|
||||
# (although this is ANSI, SunOS does not comply; so, add "-DNOSTRERROR" on SunOS)
|
||||
# define LUA_COMPAT2_5=0 if yous system does not need to be compatible with
|
||||
#
|
||||
# define (undefine) OLD_ANSI if your system does NOT have some new ANSI
|
||||
# facilities (e.g. strerror, locale.h, memmove). 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)
|
||||
#
|
||||
# define LUA_NUM_TYPE if you need numbers to be different from double
|
||||
# (for instance, -DLUA_NUM_TYPE=float)
|
||||
#
|
||||
|
||||
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 -fomit-frame-pointer
|
||||
CFLAGS = $(CONFIG) $(CWARNS) -ansi -O2
|
||||
|
||||
|
||||
# To make early versions
|
||||
CO_OPTIONS =
|
||||
|
||||
#CC = acc
|
||||
#CFLAGS = -fast -I/usr/5include
|
||||
|
||||
AR = ar
|
||||
ARFLAGS = rvl
|
||||
@@ -23,24 +42,31 @@ ARFLAGS = rvl
|
||||
|
||||
# Aplication modules
|
||||
LUAOBJS = \
|
||||
parser.o \
|
||||
lex.o \
|
||||
opcode.o \
|
||||
hash.o \
|
||||
table.o \
|
||||
inout.o \
|
||||
tree.o \
|
||||
fallback.o \
|
||||
luamem.o \
|
||||
func.o \
|
||||
undump.o \
|
||||
auxlib.o \
|
||||
zio.o
|
||||
lapi.o \
|
||||
lauxlib.o \
|
||||
lbuffer.o \
|
||||
lbuiltin.o \
|
||||
ldo.o \
|
||||
lfunc.o \
|
||||
lgc.o \
|
||||
llex.o \
|
||||
lmem.o \
|
||||
lobject.o \
|
||||
lparser.o \
|
||||
lstate.o \
|
||||
lstring.o \
|
||||
ltable.o \
|
||||
ltm.o \
|
||||
lvm.o \
|
||||
lundump.o \
|
||||
lzio.o
|
||||
|
||||
LIBOBJS = \
|
||||
iolib.o \
|
||||
mathlib.o \
|
||||
strlib.o
|
||||
liolib.o \
|
||||
lmathlib.o \
|
||||
lstrlib.o \
|
||||
ldblib.o \
|
||||
linit.o
|
||||
|
||||
|
||||
lua : lua.o liblua.a liblualib.a
|
||||
@@ -57,55 +83,58 @@ liblualib.a : $(LIBOBJS)
|
||||
liblua.so.1.0 : lua.o
|
||||
ld -o liblua.so.1.0 $(LUAOBJS)
|
||||
|
||||
y.tab.c y.tab.h : lua.stx
|
||||
yacc -d lua.stx
|
||||
|
||||
parser.c : y.tab.c
|
||||
sed -e 's/yy/luaY_/g' -e 's/malloc\.h/stdlib\.h/g' y.tab.c > parser.c
|
||||
|
||||
parser.h : y.tab.h
|
||||
sed -e 's/yy/luaY_/g' y.tab.h > parser.h
|
||||
|
||||
clear :
|
||||
rcsclean
|
||||
rm -f *.o
|
||||
rm -f parser.c parser.h y.tab.c y.tab.h
|
||||
co lua.h lualib.h luadebug.h
|
||||
rm -f
|
||||
co $(CO_OPTIONS) lua.h lualib.h luadebug.h
|
||||
|
||||
|
||||
%.h : RCS/%.h,v
|
||||
co $@
|
||||
co $(CO_OPTIONS) $@
|
||||
|
||||
%.c : RCS/%.c,v
|
||||
co $@
|
||||
co $(CO_OPTIONS) $@
|
||||
|
||||
|
||||
auxlib.o: auxlib.c lua.h auxlib.h luadebug.h
|
||||
fallback.o: fallback.c auxlib.h lua.h luamem.h fallback.h opcode.h \
|
||||
types.h tree.h func.h table.h hash.h
|
||||
func.o: func.c luadebug.h lua.h table.h tree.h types.h opcode.h func.h \
|
||||
luamem.h
|
||||
hash.o: hash.c luamem.h opcode.h lua.h types.h tree.h func.h hash.h \
|
||||
table.h auxlib.h
|
||||
inout.o: inout.c auxlib.h lua.h fallback.h opcode.h types.h tree.h \
|
||||
func.h hash.h inout.h lex.h zio.h luamem.h table.h undump.h
|
||||
iolib.o: iolib.c lua.h auxlib.h luadebug.h lualib.h
|
||||
lex.o: lex.c auxlib.h lua.h luamem.h tree.h types.h table.h opcode.h \
|
||||
func.h lex.h zio.h inout.h luadebug.h parser.h
|
||||
lua.o: lua.c lua.h auxlib.h lualib.h
|
||||
luamem.o: luamem.c luamem.h lua.h
|
||||
mathlib.o: mathlib.c lualib.h lua.h auxlib.h
|
||||
opcode.o: opcode.c luadebug.h lua.h luamem.h opcode.h types.h tree.h \
|
||||
func.h hash.h inout.h table.h fallback.h auxlib.h lex.h zio.h
|
||||
parser.o: parser.c luadebug.h lua.h luamem.h lex.h zio.h opcode.h \
|
||||
types.h tree.h func.h hash.h inout.h table.h
|
||||
strlib.o: strlib.c lua.h auxlib.h lualib.h
|
||||
table.o: table.c luamem.h auxlib.h lua.h func.h types.h tree.h \
|
||||
opcode.h hash.h table.h inout.h fallback.h luadebug.h
|
||||
tree.o: tree.c luamem.h lua.h tree.h types.h lex.h zio.h hash.h \
|
||||
opcode.h func.h table.h fallback.h
|
||||
undump.o: undump.c auxlib.h lua.h opcode.h types.h tree.h func.h \
|
||||
luamem.h table.h undump.h zio.h
|
||||
y.tab.o: y.tab.c luadebug.h lua.h luamem.h lex.h zio.h opcode.h \
|
||||
types.h tree.h func.h hash.h inout.h table.h
|
||||
zio.o: zio.c zio.h
|
||||
lapi.o: lapi.c lapi.h lua.h lobject.h lauxlib.h ldo.h lstate.h \
|
||||
luadebug.h lfunc.h lgc.h lmem.h lstring.h ltable.h ltm.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 \
|
||||
luadebug.h
|
||||
lbuiltin.o: lbuiltin.c lapi.h lua.h lobject.h lauxlib.h lbuiltin.h \
|
||||
ldo.h lstate.h luadebug.h lfunc.h lmem.h lstring.h ltable.h ltm.h \
|
||||
lundump.h lzio.h lvm.h
|
||||
ldblib.o: ldblib.c lauxlib.h lua.h luadebug.h lualib.h
|
||||
ldo.o: ldo.c ldo.h lobject.h lua.h lstate.h luadebug.h lfunc.h lgc.h \
|
||||
lmem.h lparser.h lzio.h lstring.h ltm.h lundump.h lvm.h
|
||||
lfunc.o: lfunc.c lfunc.h lobject.h lua.h lmem.h lstate.h luadebug.h
|
||||
lgc.o: lgc.c ldo.h lobject.h lua.h lstate.h luadebug.h lfunc.h lgc.h \
|
||||
lmem.h lstring.h ltable.h ltm.h
|
||||
linit.o: linit.c lua.h lualib.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 luadebug.h lstring.h
|
||||
lmathlib.o: lmathlib.c lauxlib.h lua.h lualib.h
|
||||
lmem.o: lmem.c lmem.h lstate.h lobject.h lua.h luadebug.h
|
||||
lobject.o: lobject.c lobject.h lua.h
|
||||
lparser.o: lparser.c lauxlib.h lua.h ldo.h lobject.h lstate.h \
|
||||
luadebug.h lfunc.h llex.h lzio.h lmem.h lopcodes.h lparser.h \
|
||||
lstring.h
|
||||
lstate.o: lstate.c lbuiltin.h ldo.h lobject.h lua.h lstate.h \
|
||||
luadebug.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 luadebug.h \
|
||||
lstring.h
|
||||
lstrlib.o: lstrlib.c lauxlib.h lua.h lualib.h
|
||||
ltable.o: ltable.c lauxlib.h lua.h lmem.h lobject.h lstate.h \
|
||||
luadebug.h ltable.h
|
||||
ltm.o: ltm.c lauxlib.h lua.h lmem.h lobject.h lstate.h luadebug.h \
|
||||
ltm.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 luadebug.h \
|
||||
lfunc.h lgc.h lmem.h lopcodes.h lstring.h ltable.h ltm.h lvm.h
|
||||
lzio.o: lzio.c lzio.h
|
||||
|
||||
2217
manual.tex
2217
manual.tex
File diff suppressed because it is too large
Load Diff
217
mathlib.c
217
mathlib.c
@@ -1,217 +0,0 @@
|
||||
/*
|
||||
** mathlib.c
|
||||
** Mathematics library to LUA
|
||||
*/
|
||||
|
||||
char *rcs_mathlib="$Id: mathlib.c,v 1.24 1997/06/09 17:30:10 roberto Exp roberto $";
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "lualib.h"
|
||||
#include "auxlib.h"
|
||||
#include "lua.h"
|
||||
|
||||
#ifndef PI
|
||||
#define PI 3.14159265358979323846
|
||||
#endif
|
||||
#define TODEGREE(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)
|
||||
{
|
||||
double d = luaL_check_number(1);
|
||||
lua_pushnumber (sin(TORAD(d)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void math_cos (void)
|
||||
{
|
||||
double d = luaL_check_number(1);
|
||||
lua_pushnumber (cos(TORAD(d)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void math_tan (void)
|
||||
{
|
||||
double d = luaL_check_number(1);
|
||||
lua_pushnumber (tan(TORAD(d)));
|
||||
}
|
||||
|
||||
|
||||
static void math_asin (void)
|
||||
{
|
||||
double d = luaL_check_number(1);
|
||||
lua_pushnumber (TODEGREE(asin(d)));
|
||||
}
|
||||
|
||||
|
||||
static void math_acos (void)
|
||||
{
|
||||
double d = luaL_check_number(1);
|
||||
lua_pushnumber (TODEGREE(acos(d)));
|
||||
}
|
||||
|
||||
|
||||
static void math_atan (void)
|
||||
{
|
||||
double d = luaL_check_number(1);
|
||||
lua_pushnumber (TODEGREE(atan(d)));
|
||||
}
|
||||
|
||||
|
||||
static void math_atan2 (void)
|
||||
{
|
||||
double d1 = luaL_check_number(1);
|
||||
double d2 = luaL_check_number(2);
|
||||
lua_pushnumber (TODEGREE(atan2(d1, d2)));
|
||||
}
|
||||
|
||||
|
||||
static void math_ceil (void)
|
||||
{
|
||||
double d = luaL_check_number(1);
|
||||
lua_pushnumber (ceil(d));
|
||||
}
|
||||
|
||||
|
||||
static void math_floor (void)
|
||||
{
|
||||
double d = luaL_check_number(1);
|
||||
lua_pushnumber (floor(d));
|
||||
}
|
||||
|
||||
static void math_mod (void)
|
||||
{
|
||||
float x = luaL_check_number(1);
|
||||
float y = luaL_check_number(2);
|
||||
lua_pushnumber(fmod(x, y));
|
||||
}
|
||||
|
||||
|
||||
static void math_sqrt (void)
|
||||
{
|
||||
double d = luaL_check_number(1);
|
||||
lua_pushnumber (sqrt(d));
|
||||
}
|
||||
|
||||
|
||||
static void math_pow (void)
|
||||
{
|
||||
double d1 = luaL_check_number(1);
|
||||
double d2 = luaL_check_number(2);
|
||||
lua_pushnumber(pow(d1,d2));
|
||||
}
|
||||
|
||||
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_log (void)
|
||||
{
|
||||
double d = luaL_check_number(1);
|
||||
lua_pushnumber (log(d));
|
||||
}
|
||||
|
||||
|
||||
static void math_log10 (void)
|
||||
{
|
||||
double d = luaL_check_number(1);
|
||||
lua_pushnumber (log10(d));
|
||||
}
|
||||
|
||||
|
||||
static void math_exp (void)
|
||||
{
|
||||
double d = luaL_check_number(1);
|
||||
lua_pushnumber (exp(d));
|
||||
}
|
||||
|
||||
static void math_deg (void)
|
||||
{
|
||||
float d = luaL_check_number(1);
|
||||
lua_pushnumber (d*180./PI);
|
||||
}
|
||||
|
||||
static void math_rad (void)
|
||||
{
|
||||
float d = luaL_check_number(1);
|
||||
lua_pushnumber (d/180.*PI);
|
||||
}
|
||||
|
||||
static void math_random (void)
|
||||
{
|
||||
lua_pushnumber((double)(rand()%RAND_MAX) / (double)RAND_MAX);
|
||||
}
|
||||
|
||||
static void math_randomseed (void)
|
||||
{
|
||||
srand(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 mathlib_open (void)
|
||||
{
|
||||
luaL_openlib(mathlib, (sizeof(mathlib)/sizeof(mathlib[0])));
|
||||
lua_pushcfunction(math_pow);
|
||||
lua_pushnumber(0); /* to get its tag */
|
||||
lua_settagmethod(lua_tag(lua_pop()), "pow");
|
||||
}
|
||||
|
||||
13
mathlib.h
13
mathlib.h
@@ -1,13 +0,0 @@
|
||||
/*
|
||||
** Math library to LUA
|
||||
** TeCGraf - PUC-Rio
|
||||
** $Id: $
|
||||
*/
|
||||
|
||||
|
||||
#ifndef strlib_h
|
||||
|
||||
void mathlib_open (void);
|
||||
|
||||
#endif
|
||||
|
||||
171
opcode.h
171
opcode.h
@@ -1,171 +0,0 @@
|
||||
/*
|
||||
** TeCGraf - PUC-Rio
|
||||
** $Id: opcode.h,v 3.33 1997/04/11 21:34:53 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef opcode_h
|
||||
#define opcode_h
|
||||
|
||||
#include "lua.h"
|
||||
#include "types.h"
|
||||
#include "tree.h"
|
||||
#include "func.h"
|
||||
|
||||
|
||||
#define FIELDS_PER_FLUSH 40
|
||||
|
||||
/*
|
||||
* WARNING: if you change the order of this enumeration,
|
||||
* grep "ORDER LUA_T"
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
LUA_T_NIL = -9,
|
||||
LUA_T_NUMBER = -8,
|
||||
LUA_T_STRING = -7,
|
||||
LUA_T_ARRAY = -6, /* array==table */
|
||||
LUA_T_FUNCTION = -5,
|
||||
LUA_T_CFUNCTION= -4,
|
||||
LUA_T_MARK = -3,
|
||||
LUA_T_CMARK = -2,
|
||||
LUA_T_LINE = -1,
|
||||
LUA_T_USERDATA = 0
|
||||
} lua_Type;
|
||||
|
||||
#define NUM_TYPES 10
|
||||
|
||||
|
||||
extern char *luaI_typenames[];
|
||||
|
||||
typedef enum {
|
||||
/* name parm before after side effect
|
||||
-----------------------------------------------------------------------------*/
|
||||
|
||||
PUSHNIL,/* - nil */
|
||||
PUSH0,/* - 0.0 */
|
||||
PUSH1,/* - 1.0 */
|
||||
PUSH2,/* - 2.0 */
|
||||
PUSHBYTE,/* b - (float)b */
|
||||
PUSHWORD,/* w - (float)w */
|
||||
PUSHFLOAT,/* f - f */
|
||||
PUSHSTRING,/* w - STR[w] */
|
||||
PUSHFUNCTION,/* p - FUN(p) */
|
||||
PUSHLOCAL0,/* - LOC[0] */
|
||||
PUSHLOCAL1,/* - LOC[1] */
|
||||
PUSHLOCAL2,/* - LOC[2] */
|
||||
PUSHLOCAL3,/* - LOC[3] */
|
||||
PUSHLOCAL4,/* - LOC[4] */
|
||||
PUSHLOCAL5,/* - LOC[5] */
|
||||
PUSHLOCAL6,/* - LOC[6] */
|
||||
PUSHLOCAL7,/* - LOC[7] */
|
||||
PUSHLOCAL8,/* - LOC[8] */
|
||||
PUSHLOCAL9,/* - LOC[9] */
|
||||
PUSHLOCAL,/* b - LOC[b] */
|
||||
PUSHGLOBAL,/* w - VAR[w] */
|
||||
PUSHINDEXED,/* i t t[i] */
|
||||
PUSHSELF,/* w t t t[STR[w]] */
|
||||
STORELOCAL0,/* x - LOC[0]=x */
|
||||
STORELOCAL1,/* x - LOC[1]=x */
|
||||
STORELOCAL2,/* x - LOC[2]=x */
|
||||
STORELOCAL3,/* x - LOC[3]=x */
|
||||
STORELOCAL4,/* x - LOC[4]=x */
|
||||
STORELOCAL5,/* x - LOC[5]=x */
|
||||
STORELOCAL6,/* x - LOC[6]=x */
|
||||
STORELOCAL7,/* x - LOC[7]=x */
|
||||
STORELOCAL8,/* x - LOC[8]=x */
|
||||
STORELOCAL9,/* x - LOC[9]=x */
|
||||
STORELOCAL,/* b x - LOC[b]=x */
|
||||
STOREGLOBAL,/* w x - VAR[w]=x */
|
||||
STOREINDEXED0,/* v i t - t[i]=v */
|
||||
STOREINDEXED,/* b v a_b...a_1 i t a_b...a_1 i t t[i]=v */
|
||||
STORELIST0,/* b v_b...v_1 t - t[i]=v_i */
|
||||
STORELIST,/* b c v_b...v_1 t - t[i+c*FPF]=v_i */
|
||||
STORERECORD,/* b
|
||||
w_b...w_1 v_b...v_1 t - t[STR[w_i]]=v_i */
|
||||
ADJUST0,/* - - TOP=BASE */
|
||||
ADJUST,/* b - - TOP=BASE+b */
|
||||
CREATEARRAY,/* w - newarray(size = w) */
|
||||
EQOP,/* 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,/* w x - (x!=nil)? PC+=w */
|
||||
ONFJMP,/* w x - (x==nil)? PC+=w */
|
||||
JMP,/* w - - PC+=w */
|
||||
UPJMP,/* w - - PC-=w */
|
||||
IFFJMP,/* w x - (x==nil)? PC+=w */
|
||||
IFFUPJMP,/* w x - (x==nil)? PC-=w */
|
||||
POP,/* x - */
|
||||
CALLFUNC,/* b c v_b...v_1 f r_c...r_1 f(v1,...,v_b) */
|
||||
RETCODE0,
|
||||
RETCODE,/* b - - */
|
||||
SETLINE,/* w - - LINE=w */
|
||||
VARARGS,/* b v_b...v_1 {v_1...v_b;n=b} */
|
||||
STOREMAP/* b v_b k_b ...v_1 k_1 t - t[k_i]=v_i */
|
||||
} OpCode;
|
||||
|
||||
|
||||
#define MULT_RET 255
|
||||
|
||||
|
||||
typedef union
|
||||
{
|
||||
lua_CFunction f;
|
||||
real n;
|
||||
TaggedString *ts;
|
||||
TFunc *tf;
|
||||
struct Hash *a;
|
||||
int i;
|
||||
} Value;
|
||||
|
||||
typedef struct TObject
|
||||
{
|
||||
lua_Type ttype;
|
||||
Value value;
|
||||
} TObject;
|
||||
|
||||
|
||||
/* 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 avalue(o) ((o)->value.a)
|
||||
#define fvalue(o) ((o)->value.f)
|
||||
|
||||
/* Macros to access symbol table */
|
||||
#define s_object(i) (lua_table[i].object)
|
||||
#define s_ttype(i) (ttype(&s_object(i)))
|
||||
#define s_nvalue(i) (nvalue(&s_object(i)))
|
||||
#define s_svalue(i) (svalue(&s_object(i)))
|
||||
#define s_tsvalue(i) (tsvalue(&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)))
|
||||
|
||||
#define get_word(code,pc) {memcpy(&code, pc, sizeof(Word)); pc+=sizeof(Word);}
|
||||
#define get_float(code,pc){memcpy(&code, pc, sizeof(real)); pc+=sizeof(real);}
|
||||
#define get_code(code,pc) {memcpy(&code, pc, sizeof(TFunc *)); \
|
||||
pc+=sizeof(TFunc *);}
|
||||
|
||||
|
||||
/* Exported functions */
|
||||
void lua_parse (TFunc *tf); /* from "lua.stx" module */
|
||||
void luaI_codedebugline (int line); /* from "lua.stx" module */
|
||||
void lua_travstack (int (*fn)(TObject *));
|
||||
TObject *luaI_Address (lua_Object o);
|
||||
void luaI_pushobject (TObject *o);
|
||||
void luaI_gcIM (TObject *o);
|
||||
int luaI_dorun (TFunc *tf);
|
||||
int lua_domain (void);
|
||||
|
||||
#endif
|
||||
535
strlib.c
535
strlib.c
@@ -1,535 +0,0 @@
|
||||
/*
|
||||
** strlib.c
|
||||
** String library to LUA
|
||||
*/
|
||||
|
||||
char *rcs_strlib="$Id: strlib.c,v 1.45 1997/06/19 17:45:28 roberto Exp roberto $";
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include "lua.h"
|
||||
#include "auxlib.h"
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
struct lbuff {
|
||||
char *b;
|
||||
size_t max;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
static struct lbuff lbuffer = {NULL, 0, 0};
|
||||
|
||||
|
||||
static char *strbuffer (unsigned long size)
|
||||
{
|
||||
if (size > lbuffer.max) {
|
||||
/* ANSI "realloc" doesn't need this test, but some machines (Sun!)
|
||||
don't follow ANSI */
|
||||
lbuffer.b = (lbuffer.b) ? realloc(lbuffer.b, lbuffer.max=size) :
|
||||
malloc(lbuffer.max=size);
|
||||
if (lbuffer.b == NULL)
|
||||
lua_error("memory overflow");
|
||||
}
|
||||
return lbuffer.b;
|
||||
}
|
||||
|
||||
static char *openspace (unsigned long size)
|
||||
{
|
||||
char *buff = strbuffer(lbuffer.size+size);
|
||||
return buff+lbuffer.size;
|
||||
}
|
||||
|
||||
char *luaI_addchar (int c)
|
||||
{
|
||||
if (lbuffer.size >= lbuffer.max)
|
||||
strbuffer(lbuffer.max == 0 ? 100 : lbuffer.max*2);
|
||||
lbuffer.b[lbuffer.size++] = c;
|
||||
return lbuffer.b;
|
||||
}
|
||||
|
||||
void luaI_emptybuff (void)
|
||||
{
|
||||
lbuffer.size = 0; /* prepare for next string */
|
||||
}
|
||||
|
||||
|
||||
static void addnchar (char *s, int n)
|
||||
{
|
||||
char *b = openspace(n);
|
||||
strncpy(b, s, n);
|
||||
lbuffer.size += n;
|
||||
}
|
||||
|
||||
static void addstr (char *s)
|
||||
{
|
||||
addnchar(s, strlen(s));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Return the string length
|
||||
*/
|
||||
static void str_len (void)
|
||||
{
|
||||
lua_pushnumber(strlen(luaL_check_string(1)));
|
||||
}
|
||||
|
||||
/*
|
||||
** Return the substring of a string
|
||||
*/
|
||||
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) {
|
||||
luaI_emptybuff();
|
||||
addnchar(s+start-1, end-start+1);
|
||||
lua_pushstring(luaI_addchar(0));
|
||||
}
|
||||
else lua_pushstring("");
|
||||
}
|
||||
|
||||
/*
|
||||
** Convert a string to lower case.
|
||||
*/
|
||||
static void str_lower (void)
|
||||
{
|
||||
char *s;
|
||||
luaI_emptybuff();
|
||||
for (s = luaL_check_string(1); *s; s++)
|
||||
luaI_addchar(tolower((unsigned char)*s));
|
||||
lua_pushstring(luaI_addchar(0));
|
||||
}
|
||||
|
||||
/*
|
||||
** Convert a string to upper case.
|
||||
*/
|
||||
static void str_upper (void)
|
||||
{
|
||||
char *s;
|
||||
luaI_emptybuff();
|
||||
for (s = luaL_check_string(1); *s; s++)
|
||||
luaI_addchar(toupper((unsigned char)*s));
|
||||
lua_pushstring(luaI_addchar(0));
|
||||
}
|
||||
|
||||
static void str_rep (void)
|
||||
{
|
||||
char *s = luaL_check_string(1);
|
||||
int n = (int)luaL_check_number(2);
|
||||
luaI_emptybuff();
|
||||
while (n-- > 0)
|
||||
addstr(s);
|
||||
lua_pushstring(luaI_addchar(0));
|
||||
}
|
||||
|
||||
/*
|
||||
** get ascii value of a character in a string
|
||||
*/
|
||||
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 ESC '%'
|
||||
#define SPECIALS "^$*?.([%-"
|
||||
|
||||
static char *bracket_end (char *p)
|
||||
{
|
||||
return (*p == 0) ? NULL : strchr((*p=='^') ? p+2 : p+1, ']');
|
||||
}
|
||||
|
||||
char *luaL_item_end (char *p)
|
||||
{
|
||||
switch (*p++) {
|
||||
case '\0': return p-1;
|
||||
case ESC:
|
||||
if (*p == 0) luaL_verror("incorrect pattern (ends with `%c')", ESC);
|
||||
return p+1;
|
||||
case '[': {
|
||||
char *end = bracket_end(p);
|
||||
if (end == NULL) lua_error("incorrect pattern (missing `]')");
|
||||
return end+1;
|
||||
}
|
||||
default:
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
||||
static int matchclass (int c, int cl)
|
||||
{
|
||||
int res;
|
||||
switch (tolower((unsigned char)cl)) {
|
||||
case 'a' : res = isalpha((unsigned char)c); break;
|
||||
case 'c' : res = iscntrl((unsigned char)c); break;
|
||||
case 'd' : res = isdigit((unsigned char)c); break;
|
||||
case 'l' : res = islower((unsigned char)c); break;
|
||||
case 'p' : res = ispunct((unsigned char)c); break;
|
||||
case 's' : res = isspace((unsigned char)c); break;
|
||||
case 'u' : res = isupper((unsigned char)c); break;
|
||||
case 'w' : res = isalnum((unsigned char)c); break;
|
||||
default: return (cl == c);
|
||||
}
|
||||
return (islower((unsigned char)cl) ? res : !res);
|
||||
}
|
||||
|
||||
int luaL_singlematch (int c, char *p)
|
||||
{
|
||||
if (c == 0) return 0;
|
||||
switch (*p) {
|
||||
case '.': return 1;
|
||||
case ESC: return matchclass(c, *(p+1));
|
||||
case '[': {
|
||||
char *end = bracket_end(p+1);
|
||||
int sig = *(p+1) == '^' ? (p++, 0) : 1;
|
||||
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: return (*p == c);
|
||||
}
|
||||
}
|
||||
|
||||
#define MAX_CAPT 9
|
||||
|
||||
static struct {
|
||||
char *init;
|
||||
int len; /* -1 signals unfinished capture */
|
||||
} capture[MAX_CAPT];
|
||||
|
||||
static int num_captures; /* only valid after a sucessful call to match */
|
||||
|
||||
|
||||
static void push_captures (void)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<num_captures; i++) {
|
||||
int l = capture[i].len;
|
||||
char *buff = openspace(l+1);
|
||||
if (l == -1) lua_error("unfinished capture");
|
||||
strncpy(buff, capture[i].init, l);
|
||||
buff[l] = 0;
|
||||
lua_pushstring(buff);
|
||||
}
|
||||
}
|
||||
|
||||
static int check_cap (int l, int level)
|
||||
{
|
||||
l -= '1';
|
||||
if (!(0 <= l && l < level && capture[l].len != -1))
|
||||
lua_error("invalid capture index");
|
||||
return l;
|
||||
}
|
||||
|
||||
static int capture_to_close (int level)
|
||||
{
|
||||
for (level--; level>=0; level--)
|
||||
if (capture[level].len == -1) return level;
|
||||
lua_error("invalid pattern capture");
|
||||
return 0; /* to avoid warnings */
|
||||
}
|
||||
|
||||
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 *match (char *s, char *p, int level)
|
||||
{
|
||||
init: /* using goto's to optimize tail recursion */
|
||||
switch (*p) {
|
||||
case '(': /* start capture */
|
||||
if (level >= MAX_CAPT) lua_error("too many captures");
|
||||
capture[level].init = s;
|
||||
capture[level].len = -1;
|
||||
level++; p++; goto init; /* return match(s, p+1, level); */
|
||||
case ')': { /* end capture */
|
||||
int l = capture_to_close(level);
|
||||
char *res;
|
||||
capture[l].len = s - capture[l].init; /* close capture */
|
||||
if ((res = match(s, p+1, level)) == NULL) /* match failed? */
|
||||
capture[l].len = -1; /* undo capture */
|
||||
return res;
|
||||
}
|
||||
case ESC:
|
||||
if (isdigit((unsigned char)(*(p+1)))) { /* capture */
|
||||
int l = check_cap(*(p+1), level);
|
||||
if (strncmp(capture[l].init, s, capture[l].len) == 0) {
|
||||
/* return match(p+2, s+capture[l].len, level); */
|
||||
p+=2; s+=capture[l].len; goto init;
|
||||
}
|
||||
else return NULL;
|
||||
}
|
||||
else if (*(p+1) == 'b') { /* balanced string */
|
||||
if (*(p+2) == 0 || *(p+3) == 0)
|
||||
lua_error("bad balanced pattern specification");
|
||||
s = matchbalance(s, *(p+2), *(p+3));
|
||||
if (s == NULL) return NULL;
|
||||
else { /* return match(p+4, s, level); */
|
||||
p+=4; goto init;
|
||||
}
|
||||
}
|
||||
else goto dflt;
|
||||
case '\0': case '$': /* (possibly) end of pattern */
|
||||
if (*p == 0 || (*(p+1) == 0 && *s == 0)) {
|
||||
num_captures = level;
|
||||
return s;
|
||||
}
|
||||
else goto dflt;
|
||||
default: dflt: { /* it is a pattern item */
|
||||
int m = luaL_singlematch(*s, p);
|
||||
char *ep = luaL_item_end(p); /* get what is next */
|
||||
switch (*ep) {
|
||||
case '*': { /* repetition */
|
||||
char *res;
|
||||
if (m && (res = match(s+1, p, level)))
|
||||
return res;
|
||||
p=ep+1; goto init; /* else return match(s, ep+1, level); */
|
||||
}
|
||||
case '-': { /* repetition */
|
||||
char *res;
|
||||
if ((res = match(s, ep+1, level)) != 0)
|
||||
return res;
|
||||
else if (m) {
|
||||
s++;
|
||||
goto init; /* return match(s+1, p, level); */
|
||||
}
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
case '?': { /* optional */
|
||||
char *res;
|
||||
if (m && (res = match(s+1, ep+1, level)))
|
||||
return res;
|
||||
p=ep+1; goto init; /* else return match(s, ep+1, level); */
|
||||
}
|
||||
default:
|
||||
if (m) { s++; p=ep; goto init; } /* return match(s+1, ep, level); */
|
||||
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 {
|
||||
char *res;
|
||||
if ((res=match(s1, p, 0)) != NULL) {
|
||||
lua_pushnumber(s1-s+1); /* start */
|
||||
lua_pushnumber(res-s); /* end */
|
||||
push_captures();
|
||||
return;
|
||||
}
|
||||
} while (*s1++ && !anchor);
|
||||
}
|
||||
}
|
||||
|
||||
static void add_s (lua_Object newp, lua_Object table, int n)
|
||||
{
|
||||
if (lua_isstring(newp)) {
|
||||
char *news = lua_getstring(newp);
|
||||
while (*news) {
|
||||
if (*news != ESC || !isdigit((unsigned char)*++news))
|
||||
luaI_addchar(*news++);
|
||||
else {
|
||||
int l = check_cap(*news++, num_captures);
|
||||
addnchar(capture[l].init, capture[l].len);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (lua_isfunction(newp)) {
|
||||
lua_Object res;
|
||||
struct lbuff oldbuff;
|
||||
int status;
|
||||
lua_beginblock();
|
||||
if (lua_istable(table)) {
|
||||
lua_pushobject(table);
|
||||
lua_pushnumber(n);
|
||||
}
|
||||
push_captures();
|
||||
/* function may use lbuffer, so save it and create a new one */
|
||||
oldbuff = lbuffer;
|
||||
lbuffer.b = NULL; lbuffer.max = lbuffer.size = 0;
|
||||
status = lua_callfunction(newp);
|
||||
/* restore old buffer */
|
||||
free(lbuffer.b);
|
||||
lbuffer = oldbuff;
|
||||
if (status != 0)
|
||||
lua_error(NULL);
|
||||
res = lua_getresult(1);
|
||||
addstr(lua_isstring(res) ? lua_getstring(res) : "");
|
||||
lua_endblock();
|
||||
}
|
||||
else luaL_arg_check(0, 3, NULL);
|
||||
}
|
||||
|
||||
static void str_gsub (void)
|
||||
{
|
||||
char *src = luaL_check_string(1);
|
||||
char *p = luaL_check_string(2);
|
||||
lua_Object newp = lua_getparam(3);
|
||||
lua_Object table = lua_getparam(4);
|
||||
int max_s = (int)luaL_opt_number(lua_istable(table)?5:4, strlen(src)+1);
|
||||
int anchor = (*p == '^') ? (p++, 1) : 0;
|
||||
int n = 0;
|
||||
luaI_emptybuff();
|
||||
while (n < max_s) {
|
||||
char *e = match(src, p, 0);
|
||||
if (e) {
|
||||
n++;
|
||||
add_s(newp, table, n);
|
||||
}
|
||||
if (e && e>src) /* non empty match? */
|
||||
src = e; /* skip it */
|
||||
else if (*src)
|
||||
luaI_addchar(*src++);
|
||||
else break;
|
||||
if (anchor) break;
|
||||
}
|
||||
addstr(src);
|
||||
lua_pushstring(luaI_addchar(0));
|
||||
lua_pushnumber(n); /* number of substitutions */
|
||||
}
|
||||
|
||||
static void str_set (void)
|
||||
{
|
||||
char *item = luaL_check_string(1);
|
||||
int i;
|
||||
luaL_arg_check(*luaL_item_end(item) == 0, 1, "wrong format");
|
||||
luaI_emptybuff();
|
||||
for (i=1; i<256; i++) /* 0 cannot be part of a set */
|
||||
if (luaL_singlematch(i, item))
|
||||
luaI_addchar(i);
|
||||
lua_pushstring(luaI_addchar(0));
|
||||
}
|
||||
|
||||
|
||||
void luaI_addquoted (char *s)
|
||||
{
|
||||
luaI_addchar('"');
|
||||
for (; *s; s++) {
|
||||
if (strchr("\"\\\n", *s))
|
||||
luaI_addchar('\\');
|
||||
luaI_addchar(*s);
|
||||
}
|
||||
luaI_addchar('"');
|
||||
}
|
||||
|
||||
#define MAX_FORMAT 200
|
||||
|
||||
static void str_format (void)
|
||||
{
|
||||
int arg = 1;
|
||||
char *strfrmt = luaL_check_string(arg++);
|
||||
luaI_emptybuff(); /* initialize */
|
||||
while (*strfrmt) {
|
||||
if (*strfrmt != '%')
|
||||
luaI_addchar(*strfrmt++);
|
||||
else if (*++strfrmt == '%')
|
||||
luaI_addchar(*strfrmt++); /* %% */
|
||||
else { /* format item */
|
||||
char form[MAX_FORMAT]; /* store the format ('%...') */
|
||||
char *buff;
|
||||
char *initf = strfrmt-1; /* -1 to include % */
|
||||
strfrmt = match(strfrmt, "[-+ #]*(%d*)%.?(%d*)", 0);
|
||||
if (capture[0].len > 3 || capture[1].len > 3) /* < 1000? */
|
||||
lua_error("invalid format (width or precision too long)");
|
||||
strncpy(form, initf, strfrmt-initf+1); /* +1 to include convertion */
|
||||
form[strfrmt-initf+1] = 0;
|
||||
buff = openspace(1000); /* to store the formated value */
|
||||
switch (*strfrmt++) {
|
||||
case 'q':
|
||||
luaI_addquoted(luaL_check_string(arg++));
|
||||
continue;
|
||||
case 's': {
|
||||
char *s = luaL_check_string(arg++);
|
||||
buff = 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':
|
||||
sprintf(buff, form, luaL_check_number(arg++));
|
||||
break;
|
||||
default: /* also treat cases 'pnLlh' */
|
||||
lua_error("invalid format option in function `format'");
|
||||
}
|
||||
lbuffer.size += strlen(buff);
|
||||
}
|
||||
}
|
||||
lua_pushstring(luaI_addchar(0)); /* push the result */
|
||||
}
|
||||
|
||||
|
||||
static struct luaL_reg strlib[] = {
|
||||
{"strlen", str_len},
|
||||
{"strsub", str_sub},
|
||||
{"strset", str_set},
|
||||
{"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])));
|
||||
}
|
||||
13
strlib.h
13
strlib.h
@@ -1,13 +0,0 @@
|
||||
/*
|
||||
** String library to LUA
|
||||
** TeCGraf - PUC-Rio
|
||||
** $Id: $
|
||||
*/
|
||||
|
||||
|
||||
#ifndef strlib_h
|
||||
|
||||
void strlib_open (void);
|
||||
|
||||
#endif
|
||||
|
||||
266
table.c
266
table.c
@@ -1,266 +0,0 @@
|
||||
/*
|
||||
** table.c
|
||||
** Module to control static tables
|
||||
*/
|
||||
|
||||
char *rcs_table="$Id: table.c,v 2.71 1997/06/09 17:28:14 roberto Exp roberto $";
|
||||
|
||||
#include "luamem.h"
|
||||
#include "auxlib.h"
|
||||
#include "func.h"
|
||||
#include "opcode.h"
|
||||
#include "tree.h"
|
||||
#include "hash.h"
|
||||
#include "table.h"
|
||||
#include "inout.h"
|
||||
#include "lua.h"
|
||||
#include "fallback.h"
|
||||
#include "luadebug.h"
|
||||
|
||||
|
||||
#define BUFFER_BLOCK 256
|
||||
|
||||
Symbol *lua_table = NULL;
|
||||
Word lua_ntable = 0;
|
||||
static Long lua_maxsymbol = 0;
|
||||
|
||||
TaggedString **lua_constant = NULL;
|
||||
Word lua_nconstant = 0;
|
||||
static Long lua_maxconstant = 0;
|
||||
|
||||
|
||||
#define GARBAGE_BLOCK 100
|
||||
|
||||
|
||||
void luaI_initsymbol (void)
|
||||
{
|
||||
lua_maxsymbol = BUFFER_BLOCK;
|
||||
lua_table = newvector(lua_maxsymbol, Symbol);
|
||||
luaI_predefine();
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Initialise constant table with pre-defined constants
|
||||
*/
|
||||
void luaI_initconstant (void)
|
||||
{
|
||||
lua_maxconstant = BUFFER_BLOCK;
|
||||
lua_constant = newvector(lua_maxconstant, TaggedString *);
|
||||
/* pre-register mem error messages, to avoid loop when error arises */
|
||||
luaI_findconstantbyname(tableEM);
|
||||
luaI_findconstantbyname(memEM);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Given a name, search it at symbol table and return its index. If not
|
||||
** found, allocate it.
|
||||
*/
|
||||
Word luaI_findsymbol (TaggedString *t)
|
||||
{
|
||||
if (t->u.s.varindex == NOT_USED)
|
||||
{
|
||||
if (lua_ntable == lua_maxsymbol)
|
||||
lua_maxsymbol = growvector(&lua_table, lua_maxsymbol, Symbol,
|
||||
symbolEM, MAX_WORD);
|
||||
t->u.s.varindex = lua_ntable;
|
||||
lua_table[lua_ntable].varname = t;
|
||||
s_ttype(lua_ntable) = LUA_T_NIL;
|
||||
lua_ntable++;
|
||||
}
|
||||
return t->u.s.varindex;
|
||||
}
|
||||
|
||||
|
||||
Word luaI_findsymbolbyname (char *name)
|
||||
{
|
||||
return luaI_findsymbol(luaI_createfixedstring(name));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Given a tree node, check it is has a correspondent constant index. If not,
|
||||
** allocate it.
|
||||
*/
|
||||
Word luaI_findconstant (TaggedString *t)
|
||||
{
|
||||
if (t->u.s.constindex == NOT_USED)
|
||||
{
|
||||
if (lua_nconstant == lua_maxconstant)
|
||||
lua_maxconstant = growvector(&lua_constant, lua_maxconstant, TaggedString *,
|
||||
constantEM, MAX_WORD);
|
||||
t->u.s.constindex = lua_nconstant;
|
||||
lua_constant[lua_nconstant] = t;
|
||||
lua_nconstant++;
|
||||
}
|
||||
return t->u.s.constindex;
|
||||
}
|
||||
|
||||
|
||||
Word luaI_findconstantbyname (char *name)
|
||||
{
|
||||
return luaI_findconstant(luaI_createfixedstring(name));
|
||||
}
|
||||
|
||||
TaggedString *luaI_createfixedstring (char *name)
|
||||
{
|
||||
TaggedString *ts = lua_createstring(name);
|
||||
if (!ts->marked)
|
||||
ts->marked = 2; /* avoid GC */
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
int luaI_globaldefined (char *name)
|
||||
{
|
||||
return ttype(&lua_table[luaI_findsymbolbyname(name)].object) != LUA_T_NIL;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Traverse symbol table objects
|
||||
*/
|
||||
static char *lua_travsymbol (int (*fn)(TObject *))
|
||||
{
|
||||
Word i;
|
||||
for (i=0; i<lua_ntable; i++)
|
||||
if (fn(&s_object(i)))
|
||||
return lua_table[i].varname->str;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Mark an object if it is a string or a unmarked array.
|
||||
*/
|
||||
int lua_markobject (TObject *o)
|
||||
{/* if already marked, does not change mark value */
|
||||
if (ttype(o) == LUA_T_USERDATA ||
|
||||
(ttype(o) == LUA_T_STRING && !tsvalue(o)->marked))
|
||||
tsvalue(o)->marked = 1;
|
||||
else if (ttype(o) == LUA_T_ARRAY)
|
||||
lua_hashmark (avalue(o));
|
||||
else if ((o->ttype == LUA_T_FUNCTION || o->ttype == LUA_T_MARK)
|
||||
&& !o->value.tf->marked)
|
||||
o->value.tf->marked = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* returns 0 if the object is going to be (garbage) collected
|
||||
*/
|
||||
int luaI_ismarked (TObject *o)
|
||||
{
|
||||
switch (o->ttype)
|
||||
{
|
||||
case LUA_T_STRING: case LUA_T_USERDATA:
|
||||
return o->value.ts->marked;
|
||||
case LUA_T_FUNCTION:
|
||||
return o->value.tf->marked;
|
||||
case LUA_T_ARRAY:
|
||||
return o->value.a->mark;
|
||||
default: /* nil, number, cfunction, or user data */
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void call_nilIM (void)
|
||||
{ /* signals end of garbage collection */
|
||||
TObject t;
|
||||
ttype(&t) = LUA_T_NIL;
|
||||
luaI_gcIM(&t); /* end of list */
|
||||
}
|
||||
|
||||
/*
|
||||
** Garbage collection.
|
||||
** Delete all unused strings and arrays.
|
||||
*/
|
||||
static long gc_block = GARBAGE_BLOCK;
|
||||
static long gc_nentity = 0; /* total of strings, arrays, etc */
|
||||
|
||||
static void markall (void)
|
||||
{
|
||||
lua_travstack(lua_markobject); /* mark stack objects */
|
||||
lua_travsymbol(lua_markobject); /* mark symbol table objects */
|
||||
luaI_travlock(lua_markobject); /* mark locked objects */
|
||||
luaI_travfallbacks(lua_markobject); /* mark fallbacks */
|
||||
}
|
||||
|
||||
|
||||
long lua_collectgarbage (long limit)
|
||||
{
|
||||
long recovered = 0;
|
||||
Hash *freetable;
|
||||
TaggedString *freestr;
|
||||
TFunc *freefunc;
|
||||
markall();
|
||||
luaI_invalidaterefs();
|
||||
freetable = luaI_hashcollector(&recovered);
|
||||
freestr = luaI_strcollector(&recovered);
|
||||
freefunc = luaI_funccollector(&recovered);
|
||||
gc_nentity -= recovered;
|
||||
gc_block = (limit == 0) ? 2*(gc_block-recovered) : gc_nentity+limit;
|
||||
luaI_hashcallIM(freetable);
|
||||
luaI_strcallIM(freestr);
|
||||
call_nilIM();
|
||||
luaI_hashfree(freetable);
|
||||
luaI_strfree(freestr);
|
||||
luaI_funcfree(freefunc);
|
||||
return recovered;
|
||||
}
|
||||
|
||||
|
||||
void lua_pack (void)
|
||||
{
|
||||
if (++gc_nentity >= gc_block)
|
||||
lua_collectgarbage(0);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Internal function: return next global variable
|
||||
*/
|
||||
void luaI_nextvar (void)
|
||||
{
|
||||
Word next;
|
||||
if (lua_isnil(lua_getparam(1)))
|
||||
next = 0;
|
||||
else
|
||||
next = luaI_findsymbolbyname(luaL_check_string(1)) + 1;
|
||||
while (next < lua_ntable && s_ttype(next) == LUA_T_NIL)
|
||||
next++;
|
||||
if (next < lua_ntable) {
|
||||
lua_pushstring(lua_table[next].varname->str);
|
||||
luaI_pushobject(&s_object(next));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static TObject *functofind;
|
||||
static int checkfunc (TObject *o)
|
||||
{
|
||||
if (o->ttype == LUA_T_FUNCTION)
|
||||
return
|
||||
((functofind->ttype == LUA_T_FUNCTION || functofind->ttype == LUA_T_MARK)
|
||||
&& (functofind->value.tf == o->value.tf));
|
||||
if (o->ttype == LUA_T_CFUNCTION)
|
||||
return
|
||||
((functofind->ttype == LUA_T_CFUNCTION ||
|
||||
functofind->ttype == LUA_T_CMARK) &&
|
||||
(functofind->value.f == o->value.f));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
char *lua_getobjname (lua_Object o, char **name)
|
||||
{ /* try to find a name for given function */
|
||||
functofind = luaI_Address(o);
|
||||
if ((*name = luaI_travfallbacks(checkfunc)) != NULL)
|
||||
return "tag-method";
|
||||
else if ((*name = lua_travsymbol(checkfunc)) != NULL)
|
||||
return "global";
|
||||
else return "";
|
||||
}
|
||||
|
||||
39
table.h
39
table.h
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
** Module to control static tables
|
||||
** TeCGraf - PUC-Rio
|
||||
** $Id: table.h,v 2.24 1997/04/07 14:48:53 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef table_h
|
||||
#define table_h
|
||||
|
||||
#include "tree.h"
|
||||
#include "opcode.h"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
TObject object;
|
||||
TaggedString *varname;
|
||||
} Symbol;
|
||||
|
||||
|
||||
extern Symbol *lua_table;
|
||||
extern Word lua_ntable;
|
||||
extern TaggedString **lua_constant;
|
||||
extern Word lua_nconstant;
|
||||
|
||||
void luaI_initsymbol (void);
|
||||
void luaI_initconstant (void);
|
||||
Word luaI_findsymbolbyname (char *name);
|
||||
Word luaI_findsymbol (TaggedString *t);
|
||||
Word luaI_findconstant (TaggedString *t);
|
||||
Word luaI_findconstantbyname (char *name);
|
||||
int luaI_globaldefined (char *name);
|
||||
void luaI_nextvar (void);
|
||||
TaggedString *luaI_createfixedstring (char *str);
|
||||
int lua_markobject (TObject *o);
|
||||
int luaI_ismarked (TObject *o);
|
||||
void lua_pack (void);
|
||||
|
||||
|
||||
#endif
|
||||
211
tree.c
211
tree.c
@@ -1,211 +0,0 @@
|
||||
/*
|
||||
** tree.c
|
||||
** TecCGraf - PUC-Rio
|
||||
*/
|
||||
|
||||
char *rcs_tree="$Id: tree.c,v 1.27 1997/06/09 17:28:14 roberto Exp roberto $";
|
||||
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "luamem.h"
|
||||
#include "lua.h"
|
||||
#include "tree.h"
|
||||
#include "lex.h"
|
||||
#include "hash.h"
|
||||
#include "table.h"
|
||||
#include "fallback.h"
|
||||
|
||||
|
||||
#define NUM_HASHS 64
|
||||
|
||||
typedef struct {
|
||||
int size;
|
||||
int nuse; /* number of elements (including EMPTYs) */
|
||||
TaggedString **hash;
|
||||
} stringtable;
|
||||
|
||||
static int initialized = 0;
|
||||
|
||||
static stringtable string_root[NUM_HASHS];
|
||||
|
||||
static TaggedString EMPTY = {LUA_T_STRING, NULL, {{NOT_USED, NOT_USED}},
|
||||
0, 2, {0}};
|
||||
|
||||
|
||||
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 luaI_inittree (void)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<NUM_HASHS; i++) {
|
||||
string_root[i].size = 0;
|
||||
string_root[i].nuse = 0;
|
||||
string_root[i].hash = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void initialize (void)
|
||||
{
|
||||
initialized = 1;
|
||||
luaI_inittree();
|
||||
luaI_addReserved();
|
||||
luaI_initsymbol();
|
||||
luaI_initconstant();
|
||||
luaI_initfallbacks();
|
||||
}
|
||||
|
||||
|
||||
static void grow (stringtable *tb)
|
||||
{
|
||||
int newsize = luaI_redimension(tb->size);
|
||||
TaggedString **newhash = newvector(newsize, TaggedString *);
|
||||
int i;
|
||||
for (i=0; i<newsize; i++)
|
||||
newhash[i] = NULL;
|
||||
/* rehash */
|
||||
tb->nuse = 0;
|
||||
for (i=0; i<tb->size; i++)
|
||||
if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY) {
|
||||
int h = tb->hash[i]->hash%newsize;
|
||||
while (newhash[h])
|
||||
h = (h+1)%newsize;
|
||||
newhash[h] = tb->hash[i];
|
||||
tb->nuse++;
|
||||
}
|
||||
luaI_free(tb->hash);
|
||||
tb->size = newsize;
|
||||
tb->hash = newhash;
|
||||
}
|
||||
|
||||
|
||||
static TaggedString *newone(char *buff, int tag, unsigned long h)
|
||||
{
|
||||
TaggedString *ts;
|
||||
if (tag == LUA_T_STRING) {
|
||||
ts = (TaggedString *)luaI_malloc(sizeof(TaggedString)+strlen(buff));
|
||||
strcpy(ts->str, buff);
|
||||
ts->u.s.varindex = ts->u.s.constindex = NOT_USED;
|
||||
ts->tag = LUA_T_STRING;
|
||||
}
|
||||
else {
|
||||
ts = (TaggedString *)luaI_malloc(sizeof(TaggedString));
|
||||
ts->u.v = buff;
|
||||
ts->tag = tag == LUA_ANYTAG ? 0 : tag;
|
||||
}
|
||||
ts->marked = 0;
|
||||
ts->hash = h;
|
||||
return ts;
|
||||
}
|
||||
|
||||
static TaggedString *insert (char *buff, int tag, stringtable *tb)
|
||||
{
|
||||
TaggedString *ts;
|
||||
unsigned long h = hash(buff, tag);
|
||||
int i;
|
||||
int j = -1;
|
||||
if ((Long)tb->nuse*3 >= (Long)tb->size*2)
|
||||
{
|
||||
if (!initialized)
|
||||
initialize();
|
||||
grow(tb);
|
||||
}
|
||||
i = h%tb->size;
|
||||
while ((ts = tb->hash[i]) != NULL)
|
||||
{
|
||||
if (ts == &EMPTY)
|
||||
j = i;
|
||||
else if ((ts->tag == LUA_T_STRING) ?
|
||||
(tag == LUA_T_STRING && (strcmp(buff, ts->str) == 0)) :
|
||||
((tag == ts->tag || tag == LUA_ANYTAG) && buff == ts->u.v))
|
||||
return ts;
|
||||
i = (i+1)%tb->size;
|
||||
}
|
||||
/* not found */
|
||||
lua_pack();
|
||||
if (j != -1) /* is there an EMPTY space? */
|
||||
i = j;
|
||||
else
|
||||
tb->nuse++;
|
||||
ts = tb->hash[i] = newone(buff, tag, h);
|
||||
return ts;
|
||||
}
|
||||
|
||||
TaggedString *luaI_createudata (void *udata, int tag)
|
||||
{
|
||||
return insert(udata, tag, &string_root[(unsigned)udata%NUM_HASHS]);
|
||||
}
|
||||
|
||||
TaggedString *lua_createstring (char *str)
|
||||
{
|
||||
return insert(str, LUA_T_STRING, &string_root[(unsigned)str[0]%NUM_HASHS]);
|
||||
}
|
||||
|
||||
|
||||
void luaI_strcallIM (TaggedString *l)
|
||||
{
|
||||
TObject o;
|
||||
ttype(&o) = LUA_T_USERDATA;
|
||||
for (; l; l=l->next) {
|
||||
tsvalue(&o) = l;
|
||||
luaI_gcIM(&o);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void luaI_strfree (TaggedString *l)
|
||||
{
|
||||
while (l) {
|
||||
TaggedString *next = l->next;
|
||||
luaI_free(l);
|
||||
l = next;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Garbage collection function.
|
||||
*/
|
||||
TaggedString *luaI_strcollector (long *acum)
|
||||
{
|
||||
Long counter = 0;
|
||||
TaggedString *frees = NULL;
|
||||
int i;
|
||||
for (i=0; i<NUM_HASHS; i++)
|
||||
{
|
||||
stringtable *tb = &string_root[i];
|
||||
int j;
|
||||
for (j=0; j<tb->size; j++)
|
||||
{
|
||||
TaggedString *t = tb->hash[j];
|
||||
if (t != NULL && t->marked <= 1)
|
||||
{
|
||||
if (t->marked)
|
||||
t->marked = 0;
|
||||
else
|
||||
{
|
||||
t->next = frees;
|
||||
frees = t;
|
||||
tb->hash[j] = &EMPTY;
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*acum += counter;
|
||||
return frees;
|
||||
}
|
||||
|
||||
38
tree.h
38
tree.h
@@ -1,38 +0,0 @@
|
||||
/*
|
||||
** tree.h
|
||||
** TecCGraf - PUC-Rio
|
||||
** $Id: tree.h,v 1.17 1997/05/14 18:38:29 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef tree_h
|
||||
#define tree_h
|
||||
|
||||
#include "types.h"
|
||||
|
||||
#define NOT_USED 0xFFFE
|
||||
|
||||
|
||||
typedef struct TaggedString
|
||||
{
|
||||
int tag; /* if != LUA_T_STRING, this is a userdata */
|
||||
struct TaggedString *next;
|
||||
union {
|
||||
struct {
|
||||
Word varindex; /* != NOT_USED if this is a symbol */
|
||||
Word constindex; /* != NOT_USED if this is a constant */
|
||||
} s;
|
||||
void *v; /* if this is a userdata, here is its value */
|
||||
} u;
|
||||
unsigned long hash; /* 0 if not initialized */
|
||||
int marked; /* for garbage collection; never collect (nor change) if > 1 */
|
||||
char str[1]; /* \0 byte already reserved */
|
||||
} TaggedString;
|
||||
|
||||
|
||||
TaggedString *lua_createstring (char *str);
|
||||
TaggedString *luaI_createudata (void *udata, int tag);
|
||||
TaggedString *luaI_strcollector (long *cont);
|
||||
void luaI_strfree (TaggedString *l);
|
||||
void luaI_strcallIM (TaggedString *l);
|
||||
|
||||
#endif
|
||||
29
types.h
29
types.h
@@ -1,29 +0,0 @@
|
||||
/*
|
||||
** TeCGraf - PUC-Rio
|
||||
** $Id: types.h,v 1.3 1995/02/06 19:32:43 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef types_h
|
||||
#define types_h
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#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 Word lua_Word /* some systems have Word as a predefined type */
|
||||
typedef unsigned short Word; /* unsigned 16 bits */
|
||||
|
||||
#define MAX_WORD (USHRT_MAX-2) /* maximum value of a word (-2 for safety) */
|
||||
#define MAX_INT (INT_MAX-2) /* maximum value of a int (-2 for safety) */
|
||||
|
||||
#define Long lua_Long /* some systems have Long as a predefined type */
|
||||
typedef signed long Long; /* 32 bits */
|
||||
|
||||
typedef unsigned int IntPoint; /* unsigned with same size as a pointer (for hashing) */
|
||||
|
||||
#endif
|
||||
330
undump.c
330
undump.c
@@ -1,330 +0,0 @@
|
||||
/*
|
||||
** undump.c
|
||||
** load bytecodes from files
|
||||
*/
|
||||
|
||||
char* rcs_undump="$Id: undump.c,v 1.23 1997/06/16 16:50:22 roberto Exp roberto $";
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "auxlib.h"
|
||||
#include "opcode.h"
|
||||
#include "luamem.h"
|
||||
#include "table.h"
|
||||
#include "undump.h"
|
||||
#include "zio.h"
|
||||
|
||||
static int swapword=0;
|
||||
static int swapfloat=0;
|
||||
static TFunc* Main=NULL; /* functions in a chunk */
|
||||
static TFunc* lastF=NULL;
|
||||
|
||||
static void FixCode(Byte* code, Byte* end) /* swap words */
|
||||
{
|
||||
Byte* p;
|
||||
for (p=code; p!=end;)
|
||||
{
|
||||
int op=*p;
|
||||
switch (op)
|
||||
{
|
||||
case PUSHNIL:
|
||||
case PUSH0:
|
||||
case PUSH1:
|
||||
case PUSH2:
|
||||
case PUSHLOCAL0:
|
||||
case PUSHLOCAL1:
|
||||
case PUSHLOCAL2:
|
||||
case PUSHLOCAL3:
|
||||
case PUSHLOCAL4:
|
||||
case PUSHLOCAL5:
|
||||
case PUSHLOCAL6:
|
||||
case PUSHLOCAL7:
|
||||
case PUSHLOCAL8:
|
||||
case PUSHLOCAL9:
|
||||
case PUSHINDEXED:
|
||||
case STORELOCAL0:
|
||||
case STORELOCAL1:
|
||||
case STORELOCAL2:
|
||||
case STORELOCAL3:
|
||||
case STORELOCAL4:
|
||||
case STORELOCAL5:
|
||||
case STORELOCAL6:
|
||||
case STORELOCAL7:
|
||||
case STORELOCAL8:
|
||||
case STORELOCAL9:
|
||||
case STOREINDEXED0:
|
||||
case ADJUST0:
|
||||
case EQOP:
|
||||
case LTOP:
|
||||
case LEOP:
|
||||
case GTOP:
|
||||
case GEOP:
|
||||
case ADDOP:
|
||||
case SUBOP:
|
||||
case MULTOP:
|
||||
case DIVOP:
|
||||
case POWOP:
|
||||
case CONCOP:
|
||||
case MINUSOP:
|
||||
case NOTOP:
|
||||
case POP:
|
||||
case RETCODE0:
|
||||
p++;
|
||||
break;
|
||||
case PUSHBYTE:
|
||||
case PUSHLOCAL:
|
||||
case STORELOCAL:
|
||||
case STOREINDEXED:
|
||||
case STORELIST0:
|
||||
case ADJUST:
|
||||
case RETCODE:
|
||||
case VARARGS:
|
||||
case STOREMAP:
|
||||
p+=2;
|
||||
break;
|
||||
case STORELIST:
|
||||
case CALLFUNC:
|
||||
p+=3;
|
||||
break;
|
||||
case PUSHFUNCTION:
|
||||
p+=5; /* TODO: use sizeof(TFunc*) or old? */
|
||||
break;
|
||||
case PUSHWORD:
|
||||
case PUSHSELF:
|
||||
case CREATEARRAY:
|
||||
case ONTJMP:
|
||||
case ONFJMP:
|
||||
case JMP:
|
||||
case UPJMP:
|
||||
case IFFJMP:
|
||||
case IFFUPJMP:
|
||||
case SETLINE:
|
||||
case PUSHSTRING:
|
||||
case PUSHGLOBAL:
|
||||
case STOREGLOBAL:
|
||||
{
|
||||
Byte t;
|
||||
t=p[1]; p[1]=p[2]; p[2]=t;
|
||||
p+=3;
|
||||
break;
|
||||
}
|
||||
case PUSHFLOAT: /* assumes sizeof(float)==4 */
|
||||
{
|
||||
Byte t;
|
||||
t=p[1]; p[1]=p[4]; p[4]=t;
|
||||
t=p[2]; p[2]=p[3]; p[3]=t;
|
||||
p+=5;
|
||||
break;
|
||||
}
|
||||
case STORERECORD:
|
||||
{
|
||||
int n=*++p;
|
||||
p++;
|
||||
while (n--)
|
||||
{
|
||||
Byte t;
|
||||
t=p[0]; p[0]=p[1]; p[1]=t;
|
||||
p+=2;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
luaL_verror("corrupt binary file: bad opcode %d at %d\n",
|
||||
op,(int)(p-code));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void Unthread(Byte* code, int i, int v)
|
||||
{
|
||||
while (i!=0)
|
||||
{
|
||||
Word w;
|
||||
Byte* p=code+i;
|
||||
memcpy(&w,p,sizeof(w));
|
||||
i=w; w=v;
|
||||
memcpy(p,&w,sizeof(w));
|
||||
}
|
||||
}
|
||||
|
||||
static int LoadWord(ZIO* Z)
|
||||
{
|
||||
Word w;
|
||||
zread(Z,&w,sizeof(w));
|
||||
if (swapword)
|
||||
{
|
||||
Byte* p=(Byte*)&w;
|
||||
Byte t;
|
||||
t=p[0]; p[0]=p[1]; p[1]=t;
|
||||
}
|
||||
return w;
|
||||
}
|
||||
|
||||
static int LoadSize(ZIO* Z)
|
||||
{
|
||||
Word hi=LoadWord(Z);
|
||||
Word lo=LoadWord(Z);
|
||||
int s=(hi<<16)|lo;
|
||||
if ((Word)s != s) lua_error("code too long");
|
||||
return s;
|
||||
}
|
||||
|
||||
static void* LoadBlock(int size, ZIO* Z)
|
||||
{
|
||||
void* b=luaI_malloc(size);
|
||||
zread(Z,b,size);
|
||||
return b;
|
||||
}
|
||||
|
||||
static char* LoadString(ZIO* Z)
|
||||
{
|
||||
int size=LoadWord(Z);
|
||||
char *b=luaI_buffer(size);
|
||||
zread(Z,b,size);
|
||||
return b;
|
||||
}
|
||||
|
||||
static char* LoadNewString(ZIO* Z)
|
||||
{
|
||||
return LoadBlock(LoadWord(Z),Z);
|
||||
}
|
||||
|
||||
static void LoadFunction(ZIO* Z)
|
||||
{
|
||||
TFunc* tf=new(TFunc);
|
||||
tf->next=NULL;
|
||||
tf->locvars=NULL;
|
||||
tf->size=LoadSize(Z);
|
||||
tf->lineDefined=LoadWord(Z);
|
||||
if (IsMain(tf)) /* new main */
|
||||
{
|
||||
tf->fileName=LoadNewString(Z);
|
||||
Main=lastF=tf;
|
||||
}
|
||||
else /* fix PUSHFUNCTION */
|
||||
{
|
||||
tf->marked=LoadWord(Z);
|
||||
tf->fileName=Main->fileName;
|
||||
memcpy(Main->code+tf->marked,&tf,sizeof(tf));
|
||||
lastF=lastF->next=tf;
|
||||
}
|
||||
tf->code=LoadBlock(tf->size,Z);
|
||||
if (swapword || swapfloat) FixCode(tf->code,tf->code+tf->size);
|
||||
while (1) /* unthread */
|
||||
{
|
||||
int c=zgetc(Z);
|
||||
if (c==ID_VAR) /* global var */
|
||||
{
|
||||
int i=LoadWord(Z);
|
||||
char* s=LoadString(Z);
|
||||
int v=luaI_findsymbolbyname(s);
|
||||
Unthread(tf->code,i,v);
|
||||
}
|
||||
else if (c==ID_STR) /* constant string */
|
||||
{
|
||||
int i=LoadWord(Z);
|
||||
char* s=LoadString(Z);
|
||||
int v=luaI_findconstantbyname(s);
|
||||
Unthread(tf->code,i,v);
|
||||
}
|
||||
else
|
||||
{
|
||||
zungetc(Z);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void LoadSignature(ZIO* Z)
|
||||
{
|
||||
char* s=SIGNATURE;
|
||||
while (*s!=0 && zgetc(Z)==*s)
|
||||
++s;
|
||||
if (*s!=0) lua_error("cannot load binary file: bad signature");
|
||||
}
|
||||
|
||||
static void LoadHeader(ZIO* Z)
|
||||
{
|
||||
Word w,tw=TEST_WORD;
|
||||
float f,tf=TEST_FLOAT;
|
||||
int version;
|
||||
LoadSignature(Z);
|
||||
version=zgetc(Z);
|
||||
if (version>0x23) /* after 2.5 */
|
||||
{
|
||||
int oldsizeofW=zgetc(Z);
|
||||
int oldsizeofF=zgetc(Z);
|
||||
int oldsizeofP=zgetc(Z);
|
||||
if (oldsizeofW!=2)
|
||||
luaL_verror(
|
||||
"cannot load binary file created on machine with sizeof(Word)=%d; "
|
||||
"expected 2",oldsizeofW);
|
||||
if (oldsizeofF!=4)
|
||||
luaL_verror(
|
||||
"cannot load binary file created on machine with sizeof(float)=%d; "
|
||||
"expected 4\nnot an IEEE machine?",oldsizeofF);
|
||||
if (oldsizeofP!=sizeof(TFunc*)) /* TODO: pack? */
|
||||
luaL_verror(
|
||||
"cannot load binary file created on machine with sizeof(TFunc*)=%d; "
|
||||
"expected %d",oldsizeofP,(int)sizeof(TFunc*));
|
||||
}
|
||||
zread(Z,&w,sizeof(w)); /* test word */
|
||||
if (w!=tw)
|
||||
{
|
||||
swapword=1;
|
||||
}
|
||||
zread(Z,&f,sizeof(f)); /* test float */
|
||||
if (f!=tf)
|
||||
{
|
||||
Byte* p=(Byte*)&f;
|
||||
Byte t;
|
||||
swapfloat=1;
|
||||
t=p[0]; p[0]=p[3]; p[3]=t;
|
||||
t=p[1]; p[1]=p[2]; p[2]=t;
|
||||
if (f!=tf) /* TODO: try another perm? */
|
||||
lua_error("cannot load binary file: unknown float representation");
|
||||
}
|
||||
}
|
||||
|
||||
static void LoadChunk(ZIO* Z)
|
||||
{
|
||||
LoadHeader(Z);
|
||||
while (1)
|
||||
{
|
||||
int c=zgetc(Z);
|
||||
if (c==ID_FUN) LoadFunction(Z); else { zungetc(Z); break; }
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** load one chunk from a file.
|
||||
** return list of functions found, headed by main, or NULL at EOF.
|
||||
*/
|
||||
TFunc* luaI_undump1(ZIO* Z)
|
||||
{
|
||||
int c=zgetc(Z);
|
||||
if (c==ID_CHUNK)
|
||||
{
|
||||
LoadChunk(Z);
|
||||
return Main;
|
||||
}
|
||||
else if (c!=EOZ)
|
||||
lua_error("not a lua binary file");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
** load and run all chunks in a file
|
||||
*/
|
||||
int luaI_undump(ZIO* Z)
|
||||
{
|
||||
TFunc* m;
|
||||
while ((m=luaI_undump1(Z)))
|
||||
{
|
||||
int status=luaI_dorun(m);
|
||||
luaI_freefunc(m);
|
||||
if (status!=0) return status;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
30
undump.h
30
undump.h
@@ -1,30 +0,0 @@
|
||||
/*
|
||||
** undump.h
|
||||
** definitions for lua decompiler
|
||||
** $Id: undump.h,v 1.5 1997/06/16 16:50:22 roberto Exp roberto $
|
||||
*/
|
||||
|
||||
#ifndef undump_h
|
||||
#define undump_h
|
||||
|
||||
#include "func.h"
|
||||
#include "zio.h"
|
||||
|
||||
#define IsMain(f) (f->lineDefined==0)
|
||||
|
||||
/* definitions for chunk headers */
|
||||
|
||||
#define ID_CHUNK 27 /* ESC */
|
||||
#define ID_FUN 'F'
|
||||
#define ID_VAR 'V'
|
||||
#define ID_STR 'S'
|
||||
#define SIGNATURE "Lua"
|
||||
#define VERSION 0x25 /* last format change was in 2.5 */
|
||||
#define TEST_WORD 0x1234 /* a word for testing byte ordering */
|
||||
#define TEST_FLOAT 0.123456789e-23 /* a float for testing representation */
|
||||
|
||||
|
||||
TFunc* luaI_undump1(ZIO* Z);
|
||||
int luaI_undump(ZIO* Z); /* load all chunks */
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user