mirror of
https://github.com/lua/lua.git
synced 2026-07-27 16:39:06 +00:00
Compare commits
323 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ce23901f04 | ||
|
|
df1ee1fb1c | ||
|
|
f1d0276684 | ||
|
|
7ecc2ea597 | ||
|
|
7a35f23c16 | ||
|
|
9284742a11 | ||
|
|
9704ff4cb1 | ||
|
|
e3c0ce9a69 | ||
|
|
85b76bcc01 | ||
|
|
a275d9a25b | ||
|
|
7e0be1fbde | ||
|
|
54ba642cc3 | ||
|
|
8826eb7918 | ||
|
|
e701a86385 | ||
|
|
3e1f731826 | ||
|
|
f86c1367db | ||
|
|
58fd8aa851 | ||
|
|
3226ac2da8 | ||
|
|
3e9daa7416 | ||
|
|
7236df875a | ||
|
|
675e608325 | ||
|
|
1dc0e82aeb | ||
|
|
c2eb02aaf6 | ||
|
|
2fee7e42c9 | ||
|
|
281db390e8 | ||
|
|
df8cf53cc9 | ||
|
|
40306b10db | ||
|
|
5eff5d3eac | ||
|
|
8ad8426c43 | ||
|
|
3cab7cd025 | ||
|
|
bb26efbbec | ||
|
|
5c0e5fd36d | ||
|
|
621322a305 | ||
|
|
e33a3b8e0d | ||
|
|
9a6cccb08c | ||
|
|
b58225e93b | ||
|
|
852b919465 | ||
|
|
ef94999647 | ||
|
|
6f30fa98d8 | ||
|
|
74102bd716 | ||
|
|
8d82aa821a | ||
|
|
cec1ffb80b | ||
|
|
870967ca77 | ||
|
|
66fc0f554a | ||
|
|
d6e4c29733 | ||
|
|
3e42969979 | ||
|
|
712ac505e0 | ||
|
|
f935d3397e | ||
|
|
30dd3a2dbc | ||
|
|
b04f88d581 | ||
|
|
b3c10c8c41 | ||
|
|
5c1bd89a1c | ||
|
|
15f3ab09eb | ||
|
|
c7e834f424 | ||
|
|
8c1a9899d4 | ||
|
|
05caf09a36 | ||
|
|
168a865e60 | ||
|
|
15c17c24fa | ||
|
|
45cf24485d | ||
|
|
c56e2b2e30 | ||
|
|
d1608c597e | ||
|
|
0f4903a5d7 | ||
|
|
772f25d3dd | ||
|
|
f1a1eda7c5 | ||
|
|
41259bff31 | ||
|
|
afaa98a666 | ||
|
|
73be918285 | ||
|
|
ca412214cb | ||
|
|
801722825d | ||
|
|
3abc25fa54 | ||
|
|
f4d67761f1 | ||
|
|
369c5fe3c0 | ||
|
|
7918c6cf11 | ||
|
|
826d70fcba | ||
|
|
bbb23048e3 | ||
|
|
5a3a1fe458 | ||
|
|
56fb06b6f5 | ||
|
|
995a9f7188 | ||
|
|
a0ef046ef1 | ||
|
|
5fa51fc426 | ||
|
|
15057aa0a4 | ||
|
|
1431b52e76 | ||
|
|
98fe770cab | ||
|
|
43382ce5a2 | ||
|
|
abfebf1e21 | ||
|
|
b1c02c7f00 | ||
|
|
84df3ac267 | ||
|
|
55a70c9719 | ||
|
|
0d50b87aa4 | ||
|
|
19290a8e92 | ||
|
|
d845963349 | ||
|
|
8dae4657a1 | ||
|
|
ca7be1cfeb | ||
|
|
445872a6e2 | ||
|
|
3681d025ac | ||
|
|
2998049f51 | ||
|
|
24ccc7c038 | ||
|
|
be48c4d91e | ||
|
|
a19f9056f3 | ||
|
|
5b71ab780c | ||
|
|
481bafd581 | ||
|
|
e74b250d71 | ||
|
|
cd54c95ee1 | ||
|
|
bf006eeaf5 | ||
|
|
b2afc410fa | ||
|
|
19cfa32393 | ||
|
|
27ae8432b6 | ||
|
|
415ee250b5 | ||
|
|
f188e1000b | ||
|
|
07d64e78b6 | ||
|
|
fa649fbc26 | ||
|
|
0c3e0fd95d | ||
|
|
3bb6443131 | ||
|
|
f57afd6e32 | ||
|
|
5f664a4516 | ||
|
|
87fe07c0d4 | ||
|
|
f9a9bd77e4 | ||
|
|
63b8a6fd20 | ||
|
|
024f2374ab | ||
|
|
9d9f9c48ff | ||
|
|
15d48576ea | ||
|
|
39b071f7b1 | ||
|
|
9efc257d9d | ||
|
|
fa71304e54 | ||
|
|
b5745d11cd | ||
|
|
ebcf546a55 | ||
|
|
2b45f8967c | ||
|
|
a66404aca6 | ||
|
|
d80659759b | ||
|
|
d24253d92f | ||
|
|
2cffb08a5c | ||
|
|
15f40fddca | ||
|
|
970995c3f2 | ||
|
|
b17c76817d | ||
|
|
b074306267 | ||
|
|
3c75b75516 | ||
|
|
36a7fda014 | ||
|
|
1bb3fb73cc | ||
|
|
7e01348658 | ||
|
|
28b3017baf | ||
|
|
ae808860ae | ||
|
|
a47e8c7dd0 | ||
|
|
79ce619876 | ||
|
|
233f0b0cc7 | ||
|
|
025589f772 | ||
|
|
68f337dfa6 | ||
|
|
f132ac03bc | ||
|
|
ec785a1d65 | ||
|
|
e0621e6115 | ||
|
|
38411aa102 | ||
|
|
3ec4f4eb86 | ||
|
|
367139c6d9 | ||
|
|
457bac94ce | ||
|
|
bcf46ee83b | ||
|
|
97b2fd1ba1 | ||
|
|
e13753e2fb | ||
|
|
ec79f25286 | ||
|
|
18ea2eff80 | ||
|
|
8156604823 | ||
|
|
36b6fdda83 | ||
|
|
3c67d2595b | ||
|
|
2043a0ca30 | ||
|
|
0761c4c036 | ||
|
|
2d053126e6 | ||
|
|
3203460c9e | ||
|
|
bb00cd66a7 | ||
|
|
7c342c488e | ||
|
|
b36cd823b1 | ||
|
|
cda444d7f4 | ||
|
|
dd28b830e9 | ||
|
|
572ee14b52 | ||
|
|
6198626138 | ||
|
|
8795aab83e | ||
|
|
f83db16cab | ||
|
|
6e0e9935ec | ||
|
|
97053335fb | ||
|
|
f4591397da | ||
|
|
8faf4d1de2 | ||
|
|
53c0a0f43c | ||
|
|
ad97e9ccbc | ||
|
|
e4c69cf917 | ||
|
|
5b8ced84b4 | ||
|
|
df3a81ec88 | ||
|
|
b8e76d9b5c | ||
|
|
dc97a07e19 | ||
|
|
4dce79f7e3 | ||
|
|
a8220feed2 | ||
|
|
8bc4b0d741 | ||
|
|
96b2b90c50 | ||
|
|
89d823f16b | ||
|
|
8cb8594a3b | ||
|
|
fe8338335d | ||
|
|
068d1cd1ee | ||
|
|
3365a35243 | ||
|
|
fad57bfa00 | ||
|
|
891cab8a31 | ||
|
|
2486d677c9 | ||
|
|
84b99d25ad | ||
|
|
5dfd17dd76 | ||
|
|
ce4fb88b34 | ||
|
|
e742d54253 | ||
|
|
0f580df73c | ||
|
|
2b301d711b | ||
|
|
10bdd83844 | ||
|
|
fbfa1cbe9b | ||
|
|
10c1641b8e | ||
|
|
e901e0feae | ||
|
|
d490555ec9 | ||
|
|
ad0ec203f6 | ||
|
|
577ae944e9 | ||
|
|
68d1091b79 | ||
|
|
52db68a600 | ||
|
|
bba1ae427f | ||
|
|
609392ff2e | ||
|
|
96ea2e0fb4 | ||
|
|
93ccdd52ef | ||
|
|
333a4f13d0 | ||
|
|
73664eb739 | ||
|
|
feed56a01c | ||
|
|
1929ddcf49 | ||
|
|
aa4cd37adf | ||
|
|
a84aa11f71 | ||
|
|
9bee23fd05 | ||
|
|
3bd0f9e211 | ||
|
|
5406d391cd | ||
|
|
b234da1cc2 | ||
|
|
d6a1699e37 | ||
|
|
a5862498a1 | ||
|
|
2b5bc5d1a8 | ||
|
|
94686ce585 | ||
|
|
86b35cf4f6 | ||
|
|
3b7a36653b | ||
|
|
e1d91fd0e1 | ||
|
|
5e60b961de | ||
|
|
e4645c835d | ||
|
|
0c5ac77c99 | ||
|
|
b8996eaaba | ||
|
|
ff7f769454 | ||
|
|
8a0521fa52 | ||
|
|
9deac27704 | ||
|
|
d531ccd082 | ||
|
|
df0cfc1e19 | ||
|
|
5f2d187b73 | ||
|
|
6b387e01b2 | ||
|
|
d0780fa16d | ||
|
|
fc0de64c2c | ||
|
|
b8bfa9628d | ||
|
|
dabe09518f | ||
|
|
65f28f0824 | ||
|
|
2cf954b8ae | ||
|
|
aa7b1fcec4 | ||
|
|
d95a8b3121 | ||
|
|
9ffba7a3db | ||
|
|
de4e2305c5 | ||
|
|
63d300167e | ||
|
|
62ec3797d5 | ||
|
|
0a5dce5704 | ||
|
|
8c22057b2e | ||
|
|
253655ae4b | ||
|
|
c635044f2f | ||
|
|
3db06a95a3 | ||
|
|
31d58e2f01 | ||
|
|
42ef3f9388 | ||
|
|
2651afc455 | ||
|
|
5cb6856ebc | ||
|
|
852d9a8597 | ||
|
|
6b18cc9a17 | ||
|
|
fbf887ec2b | ||
|
|
ae77864844 | ||
|
|
0162decc58 | ||
|
|
ac68a3abc4 | ||
|
|
f53460aab9 | ||
|
|
41e4c5798e | ||
|
|
fb23cd2e26 | ||
|
|
2f1de3b1e1 | ||
|
|
1a6536aaad | ||
|
|
d7cb47fadf | ||
|
|
f84abc6799 | ||
|
|
3386e3c1fb | ||
|
|
25010f8e09 | ||
|
|
424db1db0c | ||
|
|
e9049cbfc9 | ||
|
|
f8c8159362 | ||
|
|
d1c5f42943 | ||
|
|
ad07c0f638 | ||
|
|
fca10c6733 | ||
|
|
6bc68d4645 | ||
|
|
ceaaa0cca8 | ||
|
|
82ceb12b7a | ||
|
|
87dded9363 | ||
|
|
d107d5bfd2 | ||
|
|
d7d7b477bb | ||
|
|
dc6d0dcc09 | ||
|
|
7cfb5ff41f | ||
|
|
24c962de43 | ||
|
|
98d9509676 | ||
|
|
98263e2ef1 | ||
|
|
d2117d66ec | ||
|
|
0dcae99d74 | ||
|
|
b826a39919 | ||
|
|
1ea0d09281 | ||
|
|
3693f3f062 | ||
|
|
0c6b906c8c | ||
|
|
9294a2787f | ||
|
|
0ec3a21451 | ||
|
|
0624540eef | ||
|
|
a4eeb099c8 | ||
|
|
c364c7286f | ||
|
|
7c05266050 | ||
|
|
592a949272 | ||
|
|
c4b8b1b989 | ||
|
|
f490b1bff8 | ||
|
|
3921b43e44 | ||
|
|
b28da81cfe | ||
|
|
41fd23287a | ||
|
|
be7aa3854b | ||
|
|
088cc3f380 | ||
|
|
5034be6635 | ||
|
|
b1e9b37883 | ||
|
|
467288e5b3 | ||
|
|
e9e9cb03f0 | ||
|
|
0eb6ee3fee | ||
|
|
6c99b8bbdf |
175
fallback.c
Normal file
175
fallback.c
Normal file
@@ -0,0 +1,175 @@
|
|||||||
|
/*
|
||||||
|
** fallback.c
|
||||||
|
** TecCGraf - PUC-Rio
|
||||||
|
*/
|
||||||
|
|
||||||
|
char *rcs_fallback="$Id: fallback.c,v 1.22 1996/03/19 22:28:37 roberto Exp roberto $";
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "mem.h"
|
||||||
|
#include "fallback.h"
|
||||||
|
#include "opcode.h"
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
|
||||||
|
static void errorFB (void);
|
||||||
|
static void indexFB (void);
|
||||||
|
static void gettableFB (void);
|
||||||
|
static void arithFB (void);
|
||||||
|
static void concatFB (void);
|
||||||
|
static void orderFB (void);
|
||||||
|
static void GDFB (void);
|
||||||
|
static void funcFB (void);
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Warning: This list must be in the same order as the #define's
|
||||||
|
*/
|
||||||
|
struct FB luaI_fallBacks[] = {
|
||||||
|
{"error", {LUA_T_CFUNCTION, {errorFB}}, 1, 0},
|
||||||
|
{"index", {LUA_T_CFUNCTION, {indexFB}}, 2, 1},
|
||||||
|
{"gettable", {LUA_T_CFUNCTION, {gettableFB}}, 2, 1},
|
||||||
|
{"arith", {LUA_T_CFUNCTION, {arithFB}}, 3, 1},
|
||||||
|
{"order", {LUA_T_CFUNCTION, {orderFB}}, 3, 1},
|
||||||
|
{"concat", {LUA_T_CFUNCTION, {concatFB}}, 2, 1},
|
||||||
|
{"settable", {LUA_T_CFUNCTION, {gettableFB}}, 3, 0},
|
||||||
|
{"gc", {LUA_T_CFUNCTION, {GDFB}}, 1, 0},
|
||||||
|
{"function", {LUA_T_CFUNCTION, {funcFB}}, -1, -1},
|
||||||
|
/* no fixed number of params or results */
|
||||||
|
{"getglobal", {LUA_T_CFUNCTION, {indexFB}}, 1, 1}
|
||||||
|
/* same default behavior of index FB */
|
||||||
|
};
|
||||||
|
|
||||||
|
#define N_FB (sizeof(luaI_fallBacks)/sizeof(struct FB))
|
||||||
|
|
||||||
|
void luaI_setfallback (void)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
char *name = lua_getstring(lua_getparam(1));
|
||||||
|
lua_Object func = lua_getparam(2);
|
||||||
|
if (name == NULL || !lua_isfunction(func))
|
||||||
|
lua_error("incorrect argument to function `setfallback'");
|
||||||
|
for (i=0; i<N_FB; i++)
|
||||||
|
{
|
||||||
|
if (strcmp(luaI_fallBacks[i].kind, name) == 0)
|
||||||
|
{
|
||||||
|
luaI_pushobject(&luaI_fallBacks[i].function);
|
||||||
|
luaI_fallBacks[i].function = *luaI_Address(func);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* name not found */
|
||||||
|
lua_error("incorrect argument to function `setfallback'");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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 indexFB (void)
|
||||||
|
{
|
||||||
|
lua_pushnil();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void gettableFB (void)
|
||||||
|
{
|
||||||
|
lua_error("indexed expression not a table");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void arithFB (void)
|
||||||
|
{
|
||||||
|
lua_error("unexpected type at conversion to number");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void concatFB (void)
|
||||||
|
{
|
||||||
|
lua_error("unexpected type at conversion to string");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void orderFB (void)
|
||||||
|
{
|
||||||
|
lua_error("unexpected type at comparison");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void GDFB (void) { }
|
||||||
|
|
||||||
|
static void funcFB (void)
|
||||||
|
{
|
||||||
|
lua_error("call expression not a function");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Lock routines
|
||||||
|
*/
|
||||||
|
|
||||||
|
static Object *lockArray = NULL;
|
||||||
|
static int lockSize = 0;
|
||||||
|
|
||||||
|
int luaI_lock (Object *object)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int oldSize;
|
||||||
|
if (tag(object) == LUA_T_NIL)
|
||||||
|
return -1; /* special lock ref for nil */
|
||||||
|
for (i=0; i<lockSize; i++)
|
||||||
|
if (tag(&lockArray[i]) == LUA_T_NIL)
|
||||||
|
{
|
||||||
|
lockArray[i] = *object;
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
/* no more empty spaces */
|
||||||
|
oldSize = lockSize;
|
||||||
|
lockSize = growvector(&lockArray, lockSize, Object, lockEM, MAX_WORD);
|
||||||
|
for (i=oldSize; i<lockSize; i++)
|
||||||
|
tag(&lockArray[i]) = LUA_T_NIL;
|
||||||
|
lockArray[oldSize] = *object;
|
||||||
|
return oldSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void lua_unlock (int ref)
|
||||||
|
{
|
||||||
|
if (ref >= 0 && ref < lockSize)
|
||||||
|
tag(&lockArray[ref]) = LUA_T_NIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Object *luaI_getlocked (int ref)
|
||||||
|
{
|
||||||
|
static Object nul = {LUA_T_NIL, {0}};
|
||||||
|
if (ref >= 0 && ref < lockSize)
|
||||||
|
return &lockArray[ref];
|
||||||
|
else
|
||||||
|
return &nul;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaI_travlock (int (*fn)(Object *))
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
for (i=0; i<lockSize; i++)
|
||||||
|
fn(&lockArray[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
char *luaI_travfallbacks (int (*fn)(Object *))
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
for (i=0; i<N_FB; i++)
|
||||||
|
if (fn(&luaI_fallBacks[i].function))
|
||||||
|
return luaI_fallBacks[i].kind;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
35
fallback.h
Normal file
35
fallback.h
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
** $Id: fallback.h,v 1.10 1995/10/17 11:52:38 roberto Exp roberto $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef fallback_h
|
||||||
|
#define fallback_h
|
||||||
|
|
||||||
|
#include "opcode.h"
|
||||||
|
|
||||||
|
extern struct FB {
|
||||||
|
char *kind;
|
||||||
|
Object function;
|
||||||
|
int nParams;
|
||||||
|
int nResults;
|
||||||
|
} luaI_fallBacks[];
|
||||||
|
|
||||||
|
#define FB_ERROR 0
|
||||||
|
#define FB_INDEX 1
|
||||||
|
#define FB_GETTABLE 2
|
||||||
|
#define FB_ARITH 3
|
||||||
|
#define FB_ORDER 4
|
||||||
|
#define FB_CONCAT 5
|
||||||
|
#define FB_SETTABLE 6
|
||||||
|
#define FB_GC 7
|
||||||
|
#define FB_FUNCTION 8
|
||||||
|
#define FB_GETGLOBAL 9
|
||||||
|
|
||||||
|
void luaI_setfallback (void);
|
||||||
|
int luaI_lock (Object *object);
|
||||||
|
Object *luaI_getlocked (int ref);
|
||||||
|
void luaI_travlock (int (*fn)(Object *));
|
||||||
|
char *luaI_travfallbacks (int (*fn)(Object *));
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
156
func.c
Normal file
156
func.c
Normal file
@@ -0,0 +1,156 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "luadebug.h"
|
||||||
|
#include "table.h"
|
||||||
|
#include "mem.h"
|
||||||
|
#include "func.h"
|
||||||
|
#include "opcode.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 = NULL;
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Garbage collection function.
|
||||||
|
** This function traverse the function list freeing unindexed functions
|
||||||
|
*/
|
||||||
|
Long luaI_funccollector (void)
|
||||||
|
{
|
||||||
|
TFunc *curr = function_root;
|
||||||
|
TFunc *prev = NULL;
|
||||||
|
Long counter = 0;
|
||||||
|
while (curr)
|
||||||
|
{
|
||||||
|
TFunc *next = curr->next;
|
||||||
|
if (!curr->marked)
|
||||||
|
{
|
||||||
|
if (prev == NULL)
|
||||||
|
function_root = next;
|
||||||
|
else
|
||||||
|
prev->next = next;
|
||||||
|
luaI_freefunc (curr);
|
||||||
|
++counter;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
curr->marked = 0;
|
||||||
|
prev = curr;
|
||||||
|
}
|
||||||
|
curr = next;
|
||||||
|
}
|
||||||
|
return counter;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void lua_funcinfo (lua_Object func, char **filename, int *linedefined)
|
||||||
|
{
|
||||||
|
Object *f = luaI_Address(func);
|
||||||
|
if (f->tag == LUA_T_MARK || f->tag == LUA_T_FUNCTION)
|
||||||
|
{
|
||||||
|
*filename = f->value.tf->fileName;
|
||||||
|
*linedefined = f->value.tf->lineDefined;
|
||||||
|
}
|
||||||
|
else if (f->tag == LUA_T_CMARK || f->tag == 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;
|
||||||
|
}
|
||||||
|
|
||||||
44
func.h
Normal file
44
func.h
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
** $Id: func.h,v 1.7 1996/03/08 12:04:04 roberto Exp roberto $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef func_h
|
||||||
|
#define func_h
|
||||||
|
|
||||||
|
#include "types.h"
|
||||||
|
#include "lua.h"
|
||||||
|
#include "tree.h"
|
||||||
|
|
||||||
|
typedef struct LocVar
|
||||||
|
{
|
||||||
|
TaggedString *varname; /* NULL signals end of scope */
|
||||||
|
int line;
|
||||||
|
} LocVar;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Function Headers
|
||||||
|
*/
|
||||||
|
typedef struct TFunc
|
||||||
|
{
|
||||||
|
struct TFunc *next;
|
||||||
|
int marked;
|
||||||
|
int size;
|
||||||
|
Byte *code;
|
||||||
|
int lineDefined;
|
||||||
|
char *fileName;
|
||||||
|
LocVar *locvars;
|
||||||
|
} TFunc;
|
||||||
|
|
||||||
|
Long luaI_funccollector (void);
|
||||||
|
void luaI_insertfunction (TFunc *f);
|
||||||
|
|
||||||
|
void luaI_initTFunc (TFunc *f);
|
||||||
|
void luaI_freefunc (TFunc *f);
|
||||||
|
|
||||||
|
void luaI_registerlocalvar (TaggedString *varname, int line);
|
||||||
|
void luaI_unregisterlocalvar (int line);
|
||||||
|
void luaI_closelocalvars (TFunc *func);
|
||||||
|
char *luaI_getlocalname (TFunc *func, int local_number, int line);
|
||||||
|
|
||||||
|
#endif
|
||||||
419
hash.c
419
hash.c
@@ -1,135 +1,133 @@
|
|||||||
/*
|
/*
|
||||||
** hash.c
|
** hash.c
|
||||||
** hash manager for lua
|
** hash manager for lua
|
||||||
** Luiz Henrique de Figueiredo - 17 Aug 90
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
char *rcs_hash="$Id: hash.c,v 2.1 1994/04/20 22:07:57 celes Exp celes $";
|
char *rcs_hash="$Id: hash.c,v 2.28 1996/02/12 18:32:40 roberto Exp roberto $";
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
#include "mm.h"
|
|
||||||
|
|
||||||
|
#include "mem.h"
|
||||||
#include "opcode.h"
|
#include "opcode.h"
|
||||||
#include "hash.h"
|
#include "hash.h"
|
||||||
#include "inout.h"
|
|
||||||
#include "table.h"
|
#include "table.h"
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#define streq(s1,s2) (strcmp(s1,s2)==0)
|
|
||||||
#define strneq(s1,s2) (strcmp(s1,s2)!=0)
|
|
||||||
|
|
||||||
#define new(s) ((s *)malloc(sizeof(s)))
|
|
||||||
#define newvector(n,s) ((s *)calloc(n,sizeof(s)))
|
|
||||||
|
|
||||||
#define nhash(t) ((t)->nhash)
|
#define nhash(t) ((t)->nhash)
|
||||||
#define nodelist(t) ((t)->list)
|
#define nuse(t) ((t)->nuse)
|
||||||
#define list(t,i) ((t)->list[i])
|
|
||||||
#define markarray(t) ((t)->mark)
|
#define markarray(t) ((t)->mark)
|
||||||
#define ref_tag(n) (tag(&(n)->ref))
|
#define nodevector(t) ((t)->node)
|
||||||
#define ref_nvalue(n) (nvalue(&(n)->ref))
|
#define node(t,i) (&(t)->node[i])
|
||||||
#define ref_svalue(n) (svalue(&(n)->ref))
|
#define ref(n) (&(n)->ref)
|
||||||
|
#define val(n) (&(n)->val)
|
||||||
|
|
||||||
|
|
||||||
typedef struct ArrayList
|
#define REHASH_LIMIT 0.70 /* avoid more than this % full */
|
||||||
|
|
||||||
|
|
||||||
|
static Hash *listhead = NULL;
|
||||||
|
|
||||||
|
|
||||||
|
/* hash dimensions values */
|
||||||
|
static Word dimensions[] =
|
||||||
|
{3, 5, 7, 11, 23, 47, 97, 197, 397, 797, 1597, 3203, 6421,
|
||||||
|
12853, 25717, 51437, 65521, 0}; /* 65521 == last prime < MAX_WORD */
|
||||||
|
|
||||||
|
Word luaI_redimension (Word nhash)
|
||||||
{
|
{
|
||||||
Hash *array;
|
Word i;
|
||||||
struct ArrayList *next;
|
for (i=0; dimensions[i]!=0; i++)
|
||||||
} ArrayList;
|
|
||||||
|
|
||||||
static ArrayList *listhead = NULL;
|
|
||||||
|
|
||||||
static int head (Hash *t, Object *ref) /* hash function */
|
|
||||||
{
|
|
||||||
if (tag(ref) == T_NUMBER) return (((int)nvalue(ref))%nhash(t));
|
|
||||||
else if (tag(ref) == T_STRING)
|
|
||||||
{
|
{
|
||||||
int h;
|
if (dimensions[i] > nhash)
|
||||||
char *name = svalue(ref);
|
return dimensions[i];
|
||||||
for (h=0; *name!=0; name++) /* interpret name as binary number */
|
}
|
||||||
|
lua_error("table overflow");
|
||||||
|
return 0; /* to avoid warnings */
|
||||||
|
}
|
||||||
|
|
||||||
|
static Word hashindex (Hash *t, Object *ref) /* hash function */
|
||||||
|
{
|
||||||
|
switch (tag(ref))
|
||||||
|
{
|
||||||
|
case LUA_T_NIL:
|
||||||
|
lua_error ("unexpected type to index table");
|
||||||
|
return -1; /* UNREACHEABLE */
|
||||||
|
case LUA_T_NUMBER:
|
||||||
|
return (((Word)nvalue(ref))%nhash(t));
|
||||||
|
case LUA_T_STRING:
|
||||||
|
return (Word)((tsvalue(ref)->hash)%nhash(t)); /* make it a valid index */
|
||||||
|
case LUA_T_FUNCTION:
|
||||||
|
return (((IntPoint)ref->value.tf)%nhash(t));
|
||||||
|
case LUA_T_CFUNCTION:
|
||||||
|
return (((IntPoint)fvalue(ref))%nhash(t));
|
||||||
|
case LUA_T_ARRAY:
|
||||||
|
return (((IntPoint)avalue(ref))%nhash(t));
|
||||||
|
default: /* user data */
|
||||||
|
return (((IntPoint)uvalue(ref))%nhash(t));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int lua_equalObj (Object *t1, Object *t2)
|
||||||
|
{
|
||||||
|
if (tag(t1) != tag(t2)) return 0;
|
||||||
|
switch (tag(t1))
|
||||||
{
|
{
|
||||||
h <<= 8;
|
case LUA_T_NIL: return 1;
|
||||||
h += (unsigned char) *name; /* avoid sign extension */
|
case LUA_T_NUMBER: return nvalue(t1) == nvalue(t2);
|
||||||
h %= nhash(t); /* make it a valid index */
|
case LUA_T_STRING: return svalue(t1) == svalue(t2);
|
||||||
|
case LUA_T_ARRAY: return avalue(t1) == avalue(t2);
|
||||||
|
case LUA_T_FUNCTION: return t1->value.tf == t2->value.tf;
|
||||||
|
case LUA_T_CFUNCTION: return fvalue(t1) == fvalue(t2);
|
||||||
|
default: return uvalue(t1) == uvalue(t2);
|
||||||
}
|
}
|
||||||
return h;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
lua_reportbug ("unexpected type to index table");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static Node *present(Hash *t, Object *ref, int h)
|
static Word present (Hash *t, Object *ref)
|
||||||
{
|
{
|
||||||
Node *n=NULL, *p;
|
Word h = hashindex(t, ref);
|
||||||
if (tag(ref) == T_NUMBER)
|
while (tag(ref(node(t, h))) != LUA_T_NIL)
|
||||||
{
|
{
|
||||||
for (p=NULL,n=list(t,h); n!=NULL; p=n, n=n->next)
|
if (lua_equalObj(ref, ref(node(t, h))))
|
||||||
if (ref_tag(n) == T_NUMBER && nvalue(ref) == ref_nvalue(n)) break;
|
return h;
|
||||||
}
|
h = (h+1) % nhash(t);
|
||||||
else if (tag(ref) == T_STRING)
|
|
||||||
{
|
|
||||||
for (p=NULL,n=list(t,h); n!=NULL; p=n, n=n->next)
|
|
||||||
if (ref_tag(n) == T_STRING && streq(svalue(ref),ref_svalue(n))) break;
|
|
||||||
}
|
|
||||||
if (n==NULL) /* name not present */
|
|
||||||
return NULL;
|
|
||||||
#if 0
|
|
||||||
if (p!=NULL) /* name present but not first */
|
|
||||||
{
|
|
||||||
p->next=n->next; /* move-to-front self-organization */
|
|
||||||
n->next=list(t,h);
|
|
||||||
list(t,h)=n;
|
|
||||||
}
|
}
|
||||||
#endif
|
return h;
|
||||||
return n;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void freelist (Node *n)
|
|
||||||
|
/*
|
||||||
|
** Alloc a vector node
|
||||||
|
*/
|
||||||
|
static Node *hashnodecreate (Word nhash)
|
||||||
{
|
{
|
||||||
while (n)
|
Word i;
|
||||||
{
|
Node *v = newvector (nhash, Node);
|
||||||
Node *next = n->next;
|
for (i=0; i<nhash; i++)
|
||||||
free (n);
|
tag(ref(&v[i])) = LUA_T_NIL;
|
||||||
n = next;
|
return v;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Create a new hash. Return the hash pointer or NULL on error.
|
** Create a new hash. Return the hash pointer or NULL on error.
|
||||||
*/
|
*/
|
||||||
static Hash *hashcreate (unsigned int nhash)
|
static Hash *hashcreate (Word nhash)
|
||||||
{
|
{
|
||||||
Hash *t = new (Hash);
|
Hash *t = new(Hash);
|
||||||
if (t == NULL)
|
nhash = luaI_redimension((Word)((float)nhash/REHASH_LIMIT));
|
||||||
{
|
nodevector(t) = hashnodecreate(nhash);
|
||||||
lua_error ("not enough memory");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
nhash(t) = nhash;
|
nhash(t) = nhash;
|
||||||
|
nuse(t) = 0;
|
||||||
markarray(t) = 0;
|
markarray(t) = 0;
|
||||||
nodelist(t) = newvector (nhash, Node*);
|
|
||||||
if (nodelist(t) == NULL)
|
|
||||||
{
|
|
||||||
lua_error ("not enough memory");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Delete a hash
|
** Delete a hash
|
||||||
*/
|
*/
|
||||||
static void hashdelete (Hash *h)
|
static void hashdelete (Hash *t)
|
||||||
{
|
{
|
||||||
int i;
|
luaI_free (nodevector(t));
|
||||||
for (i=0; i<nhash(h); i++)
|
luaI_free(t);
|
||||||
freelist (list(h,i));
|
|
||||||
free (nodelist(h));
|
|
||||||
free(h);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -140,12 +138,12 @@ void lua_hashmark (Hash *h)
|
|||||||
{
|
{
|
||||||
if (markarray(h) == 0)
|
if (markarray(h) == 0)
|
||||||
{
|
{
|
||||||
int i;
|
Word i;
|
||||||
markarray(h) = 1;
|
markarray(h) = 1;
|
||||||
for (i=0; i<nhash(h); i++)
|
for (i=0; i<nhash(h); i++)
|
||||||
{
|
{
|
||||||
Node *n;
|
Node *n = node(h,i);
|
||||||
for (n = list(h,i); n != NULL; n = n->next)
|
if (tag(ref(n)) != LUA_T_NIL)
|
||||||
{
|
{
|
||||||
lua_markobject(&n->ref);
|
lua_markobject(&n->ref);
|
||||||
lua_markobject(&n->val);
|
lua_markobject(&n->val);
|
||||||
@@ -153,92 +151,125 @@ void lua_hashmark (Hash *h)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void call_fallbacks (void)
|
||||||
|
{
|
||||||
|
Hash *curr_array;
|
||||||
|
Object t;
|
||||||
|
tag(&t) = LUA_T_ARRAY;
|
||||||
|
for (curr_array = listhead; curr_array; curr_array = curr_array->next)
|
||||||
|
if (markarray(curr_array) != 1)
|
||||||
|
{
|
||||||
|
avalue(&t) = curr_array;
|
||||||
|
luaI_gcFB(&t);
|
||||||
|
}
|
||||||
|
tag(&t) = LUA_T_NIL;
|
||||||
|
luaI_gcFB(&t); /* end of list */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Garbage collection to arrays
|
** Garbage collection to arrays
|
||||||
** Delete all unmarked arrays.
|
** Delete all unmarked arrays.
|
||||||
*/
|
*/
|
||||||
void lua_hashcollector (void)
|
Long lua_hashcollector (void)
|
||||||
{
|
{
|
||||||
ArrayList *curr = listhead, *prev = NULL;
|
Hash *curr_array = listhead, *prev = NULL;
|
||||||
while (curr != NULL)
|
Long counter = 0;
|
||||||
|
call_fallbacks();
|
||||||
|
while (curr_array != NULL)
|
||||||
{
|
{
|
||||||
ArrayList *next = curr->next;
|
Hash *next = curr_array->next;
|
||||||
if (markarray(curr->array) != 1)
|
if (markarray(curr_array) != 1)
|
||||||
{
|
{
|
||||||
if (prev == NULL) listhead = next;
|
if (prev == NULL) listhead = next;
|
||||||
else prev->next = next;
|
else prev->next = next;
|
||||||
hashdelete(curr->array);
|
hashdelete(curr_array);
|
||||||
free(curr);
|
++counter;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
markarray(curr->array) = 0;
|
markarray(curr_array) = 0;
|
||||||
prev = curr;
|
prev = curr_array;
|
||||||
}
|
}
|
||||||
curr = next;
|
curr_array = next;
|
||||||
}
|
}
|
||||||
|
return counter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Create a new array
|
** Create a new array
|
||||||
** This function insert the new array at array list. It also
|
** This function inserts the new array in the array list. It also
|
||||||
** execute garbage collection if the number of array created
|
** executes garbage collection if the number of arrays created
|
||||||
** exceed a pre-defined range.
|
** exceed a pre-defined range.
|
||||||
*/
|
*/
|
||||||
Hash *lua_createarray (int nhash)
|
Hash *lua_createarray (Word nhash)
|
||||||
{
|
{
|
||||||
ArrayList *new = new(ArrayList);
|
Hash *array;
|
||||||
if (new == NULL)
|
lua_pack();
|
||||||
{
|
array = hashcreate(nhash);
|
||||||
lua_error ("not enough memory");
|
array->next = listhead;
|
||||||
return NULL;
|
listhead = array;
|
||||||
}
|
return array;
|
||||||
new->array = hashcreate(nhash);
|
}
|
||||||
if (new->array == NULL)
|
|
||||||
{
|
|
||||||
lua_error ("not enough memory");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (lua_nentity == lua_block)
|
|
||||||
lua_pack();
|
|
||||||
|
|
||||||
lua_nentity++;
|
/*
|
||||||
new->next = listhead;
|
** Re-hash
|
||||||
listhead = new;
|
*/
|
||||||
return new->array;
|
static void rehash (Hash *t)
|
||||||
|
{
|
||||||
|
Word i;
|
||||||
|
Word nold = nhash(t);
|
||||||
|
Node *vold = nodevector(t);
|
||||||
|
nhash(t) = luaI_redimension(nhash(t));
|
||||||
|
nodevector(t) = hashnodecreate(nhash(t));
|
||||||
|
for (i=0; i<nold; i++)
|
||||||
|
{
|
||||||
|
Node *n = vold+i;
|
||||||
|
if (tag(ref(n)) != LUA_T_NIL && tag(val(n)) != LUA_T_NIL)
|
||||||
|
*node(t, present(t, ref(n))) = *n; /* copy old node to new hahs */
|
||||||
|
}
|
||||||
|
luaI_free(vold);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** If the hash node is present, return its pointer, otherwise return
|
||||||
|
** null.
|
||||||
|
*/
|
||||||
|
Object *lua_hashget (Hash *t, Object *ref)
|
||||||
|
{
|
||||||
|
Word h = present(t, ref);
|
||||||
|
if (tag(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
|
** If the hash node is present, return its pointer, otherwise create a new
|
||||||
** node for the given reference and also return its pointer.
|
** node for the given reference and also return its pointer.
|
||||||
** On error, return NULL.
|
|
||||||
*/
|
*/
|
||||||
Object *lua_hashdefine (Hash *t, Object *ref)
|
Object *lua_hashdefine (Hash *t, Object *ref)
|
||||||
{
|
{
|
||||||
int h;
|
Word h;
|
||||||
Node *n;
|
Node *n;
|
||||||
h = head (t, ref);
|
h = present(t, ref);
|
||||||
if (h < 0) return NULL;
|
n = node(t, h);
|
||||||
|
if (tag(ref(n)) == LUA_T_NIL)
|
||||||
n = present(t, ref, h);
|
|
||||||
if (n == NULL)
|
|
||||||
{
|
{
|
||||||
n = new(Node);
|
nuse(t)++;
|
||||||
if (n == NULL)
|
if ((float)nuse(t) > (float)nhash(t)*REHASH_LIMIT)
|
||||||
{
|
{
|
||||||
lua_error ("not enough memory");
|
rehash(t);
|
||||||
return NULL;
|
h = present(t, ref);
|
||||||
|
n = node(t, h);
|
||||||
}
|
}
|
||||||
n->ref = *ref;
|
*ref(n) = *ref;
|
||||||
tag(&n->val) = T_NIL;
|
tag(val(n)) = LUA_T_NIL;
|
||||||
n->next = list(t,h); /* link node to head of list */
|
|
||||||
list(t,h) = n;
|
|
||||||
}
|
}
|
||||||
return (&n->val);
|
return (val(n));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -248,98 +279,44 @@ Object *lua_hashdefine (Hash *t, Object *ref)
|
|||||||
** in the hash.
|
** in the hash.
|
||||||
** This function pushs the element value and its reference to the stack.
|
** This function pushs the element value and its reference to the stack.
|
||||||
*/
|
*/
|
||||||
static void firstnode (Hash *a, int h)
|
static void hashnext (Hash *t, Word i)
|
||||||
{
|
{
|
||||||
if (h < nhash(a))
|
if (i >= nhash(t))
|
||||||
{
|
{
|
||||||
int i;
|
lua_pushnil(); lua_pushnil();
|
||||||
for (i=h; i<nhash(a); i++)
|
return;
|
||||||
|
}
|
||||||
|
while (tag(ref(node(t,i))) == LUA_T_NIL || tag(val(node(t,i))) == LUA_T_NIL)
|
||||||
|
{
|
||||||
|
if (++i >= nhash(t))
|
||||||
{
|
{
|
||||||
if (list(a,i) != NULL)
|
lua_pushnil(); lua_pushnil();
|
||||||
{
|
return;
|
||||||
if (tag(&list(a,i)->val) != T_NIL)
|
|
||||||
{
|
|
||||||
lua_pushobject (&list(a,i)->ref);
|
|
||||||
lua_pushobject (&list(a,i)->val);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Node *next = list(a,i)->next;
|
|
||||||
while (next != NULL && tag(&next->val) == T_NIL) next = next->next;
|
|
||||||
if (next != NULL)
|
|
||||||
{
|
|
||||||
lua_pushobject (&next->ref);
|
|
||||||
lua_pushobject (&next->val);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lua_pushnil();
|
luaI_pushobject(ref(node(t,i)));
|
||||||
lua_pushnil();
|
luaI_pushobject(val(node(t,i)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void lua_next (void)
|
void lua_next (void)
|
||||||
{
|
{
|
||||||
Hash *a;
|
Hash *t;
|
||||||
Object *o = lua_getparam (1);
|
lua_Object o = lua_getparam(1);
|
||||||
Object *r = lua_getparam (2);
|
lua_Object r = lua_getparam(2);
|
||||||
if (o == NULL || r == NULL)
|
if (o == LUA_NOOBJECT || r == LUA_NOOBJECT)
|
||||||
{ lua_error ("too few arguments to function `next'"); return; }
|
lua_error ("too few arguments to function `next'");
|
||||||
if (lua_getparam (3) != NULL)
|
if (lua_getparam(3) != LUA_NOOBJECT)
|
||||||
{ lua_error ("too many arguments to function `next'"); return; }
|
lua_error ("too many arguments to function `next'");
|
||||||
if (tag(o) != T_ARRAY)
|
if (!lua_istable(o))
|
||||||
{ lua_error ("first argument of function `next' is not a table"); return; }
|
lua_error ("first argument of function `next' is not a table");
|
||||||
a = avalue(o);
|
t = avalue(luaI_Address(o));
|
||||||
if (tag(r) == T_NIL)
|
if (lua_isnil(r))
|
||||||
{
|
{
|
||||||
firstnode (a, 0);
|
hashnext(t, 0);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
int h = head (a, r);
|
Word h = present (t, luaI_Address(r));
|
||||||
if (h >= 0)
|
hashnext(t, h+1);
|
||||||
{
|
|
||||||
Node *n = list(a,h);
|
|
||||||
while (n)
|
|
||||||
{
|
|
||||||
if (memcmp(&n->ref,r,sizeof(Object)) == 0)
|
|
||||||
{
|
|
||||||
if (n->next == NULL)
|
|
||||||
{
|
|
||||||
firstnode (a, h+1);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else if (tag(&n->next->val) != T_NIL)
|
|
||||||
{
|
|
||||||
lua_pushobject (&n->next->ref);
|
|
||||||
lua_pushobject (&n->next->val);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Node *next = n->next->next;
|
|
||||||
while (next != NULL && tag(&next->val) == T_NIL) next = next->next;
|
|
||||||
if (next == NULL)
|
|
||||||
{
|
|
||||||
firstnode (a, h+1);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
lua_pushobject (&next->ref);
|
|
||||||
lua_pushobject (&next->val);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
n = n->next;
|
|
||||||
}
|
|
||||||
if (n == NULL)
|
|
||||||
lua_error ("error in function 'next': reference not found");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
20
hash.h
20
hash.h
@@ -1,31 +1,37 @@
|
|||||||
/*
|
/*
|
||||||
** hash.h
|
** hash.h
|
||||||
** hash manager for lua
|
** hash manager for lua
|
||||||
** Luiz Henrique de Figueiredo - 17 Aug 90
|
** $Id: hash.h,v 2.10 1996/02/12 18:32:40 roberto Exp roberto $
|
||||||
** $Id: hash.h,v 1.1 1993/12/17 18:41:19 celes Exp celes $
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef hash_h
|
#ifndef hash_h
|
||||||
#define hash_h
|
#define hash_h
|
||||||
|
|
||||||
|
#include "types.h"
|
||||||
|
#include "opcode.h"
|
||||||
|
|
||||||
typedef struct node
|
typedef struct node
|
||||||
{
|
{
|
||||||
Object ref;
|
Object ref;
|
||||||
Object val;
|
Object val;
|
||||||
struct node *next;
|
|
||||||
} Node;
|
} Node;
|
||||||
|
|
||||||
typedef struct Hash
|
typedef struct Hash
|
||||||
{
|
{
|
||||||
|
struct Hash *next;
|
||||||
char mark;
|
char mark;
|
||||||
unsigned int nhash;
|
Word nhash;
|
||||||
Node **list;
|
Word nuse;
|
||||||
|
Node *node;
|
||||||
} Hash;
|
} Hash;
|
||||||
|
|
||||||
|
|
||||||
Hash *lua_createarray (int nhash);
|
int lua_equalObj (Object *t1, Object *t2);
|
||||||
|
Word luaI_redimension (Word nhash);
|
||||||
|
Hash *lua_createarray (Word nhash);
|
||||||
void lua_hashmark (Hash *h);
|
void lua_hashmark (Hash *h);
|
||||||
void lua_hashcollector (void);
|
Long lua_hashcollector (void);
|
||||||
|
Object *lua_hashget (Hash *t, Object *ref);
|
||||||
Object *lua_hashdefine (Hash *t, Object *ref);
|
Object *lua_hashdefine (Hash *t, Object *ref);
|
||||||
void lua_next (void);
|
void lua_next (void);
|
||||||
|
|
||||||
|
|||||||
268
inout.c
268
inout.c
@@ -2,50 +2,36 @@
|
|||||||
** inout.c
|
** inout.c
|
||||||
** Provide function to realise the input/output function and debugger
|
** Provide function to realise the input/output function and debugger
|
||||||
** facilities.
|
** facilities.
|
||||||
|
** Also provides some predefined lua functions.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
char *rcs_inout="$Id: inout.c,v 1.2 1993/12/22 21:15:16 roberto Exp celes $";
|
char *rcs_inout="$Id: inout.c,v 2.35 1996/03/19 16:50:24 roberto Exp roberto $";
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
|
#include "lex.h"
|
||||||
#include "opcode.h"
|
#include "opcode.h"
|
||||||
#include "hash.h"
|
|
||||||
#include "inout.h"
|
#include "inout.h"
|
||||||
#include "table.h"
|
#include "table.h"
|
||||||
#include "tree.h"
|
#include "tree.h"
|
||||||
|
#include "lua.h"
|
||||||
|
#include "mem.h"
|
||||||
|
|
||||||
|
|
||||||
/* Exported variables */
|
/* Exported variables */
|
||||||
int lua_linenumber;
|
Word lua_linenumber;
|
||||||
int lua_debug;
|
char *lua_parsedfile;
|
||||||
int lua_debugline;
|
|
||||||
|
|
||||||
/* Internal variables */
|
|
||||||
#ifndef MAXFUNCSTACK
|
|
||||||
#define MAXFUNCSTACK 32
|
|
||||||
#endif
|
|
||||||
static struct { int file; int function; } funcstack[MAXFUNCSTACK];
|
|
||||||
static int nfuncstack=0;
|
|
||||||
|
|
||||||
static FILE *fp;
|
static FILE *fp;
|
||||||
static char *st;
|
static char *st;
|
||||||
static void (*usererror) (char *s);
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Function to set user function to handle errors.
|
|
||||||
*/
|
|
||||||
void lua_errorfunction (void (*fn) (char *s))
|
|
||||||
{
|
|
||||||
usererror = fn;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Function to get the next character from the input file
|
** Function to get the next character from the input file
|
||||||
*/
|
*/
|
||||||
static int fileinput (void)
|
static int fileinput (void)
|
||||||
{
|
{
|
||||||
int c = fgetc (fp);
|
return fgetc (fp);
|
||||||
return (c == EOF ? 0 : c);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -53,22 +39,28 @@ static int fileinput (void)
|
|||||||
*/
|
*/
|
||||||
static int stringinput (void)
|
static int stringinput (void)
|
||||||
{
|
{
|
||||||
st++;
|
return *st++;
|
||||||
return (*(st-1));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Function to open a file to be input unit.
|
** Function to open a file to be input unit.
|
||||||
** Return 0 on success or 1 on error.
|
** Return the file.
|
||||||
*/
|
*/
|
||||||
int lua_openfile (char *fn)
|
FILE *lua_openfile (char *fn)
|
||||||
{
|
{
|
||||||
lua_linenumber = 1;
|
|
||||||
lua_setinput (fileinput);
|
lua_setinput (fileinput);
|
||||||
fp = fopen (fn, "r");
|
if (fn == NULL)
|
||||||
if (fp == NULL) return 1;
|
{
|
||||||
if (lua_addfile (fn)) return 1;
|
fp = stdin;
|
||||||
return 0;
|
fn = "(stdin)";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
fp = fopen (fn, "r");
|
||||||
|
if (fp == NULL)
|
||||||
|
return NULL;
|
||||||
|
lua_linenumber = 1;
|
||||||
|
lua_parsedfile = luaI_createfixedstring(fn)->str;
|
||||||
|
return fp;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -76,9 +68,8 @@ int lua_openfile (char *fn)
|
|||||||
*/
|
*/
|
||||||
void lua_closefile (void)
|
void lua_closefile (void)
|
||||||
{
|
{
|
||||||
if (fp != NULL)
|
if (fp != NULL && fp != stdin)
|
||||||
{
|
{
|
||||||
lua_delfile();
|
|
||||||
fclose (fp);
|
fclose (fp);
|
||||||
fp = NULL;
|
fp = NULL;
|
||||||
}
|
}
|
||||||
@@ -87,17 +78,12 @@ void lua_closefile (void)
|
|||||||
/*
|
/*
|
||||||
** Function to open a string to be input unit
|
** Function to open a string to be input unit
|
||||||
*/
|
*/
|
||||||
int lua_openstring (char *s)
|
void lua_openstring (char *s)
|
||||||
{
|
{
|
||||||
lua_linenumber = 1;
|
|
||||||
lua_setinput (stringinput);
|
lua_setinput (stringinput);
|
||||||
st = s;
|
st = s;
|
||||||
{
|
lua_linenumber = 1;
|
||||||
char sn[64];
|
lua_parsedfile = luaI_createfixedstring("(string)")->str;
|
||||||
sprintf (sn, "String: %10.10s...", s);
|
|
||||||
if (lua_addfile (sn)) return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -105,74 +91,156 @@ int lua_openstring (char *s)
|
|||||||
*/
|
*/
|
||||||
void lua_closestring (void)
|
void lua_closestring (void)
|
||||||
{
|
{
|
||||||
lua_delfile();
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Internal function: do a string
|
||||||
|
*/
|
||||||
|
void lua_internaldostring (void)
|
||||||
|
{
|
||||||
|
lua_Object obj = lua_getparam (1);
|
||||||
|
if (lua_isstring(obj) && !lua_dostring(lua_getstring(obj)))
|
||||||
|
lua_pushnumber(1);
|
||||||
|
else
|
||||||
|
lua_pushnil();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Call user function to handle error messages, if registred. Or report error
|
** Internal function: do a file
|
||||||
** using standard function (fprintf).
|
|
||||||
*/
|
*/
|
||||||
void lua_error (char *s)
|
void lua_internaldofile (void)
|
||||||
{
|
{
|
||||||
if (usererror != NULL) usererror (s);
|
lua_Object obj = lua_getparam (1);
|
||||||
else fprintf (stderr, "lua: %s\n", s);
|
char *fname = NULL;
|
||||||
|
if (lua_isstring(obj))
|
||||||
|
fname = lua_getstring(obj);
|
||||||
|
else if (obj != LUA_NOOBJECT)
|
||||||
|
lua_error("invalid argument to function `dofile'");
|
||||||
|
/* else fname = NULL */
|
||||||
|
if (!lua_dofile(fname))
|
||||||
|
lua_pushnumber(1);
|
||||||
|
else
|
||||||
|
lua_pushnil();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
static char *tostring (lua_Object obj)
|
||||||
** Called to execute SETFUNCTION opcode, this function pushs a function into
|
|
||||||
** function stack. Return 0 on success or 1 on error.
|
|
||||||
*/
|
|
||||||
int lua_pushfunction (int file, int function)
|
|
||||||
{
|
{
|
||||||
if (nfuncstack >= MAXFUNCSTACK-1)
|
char *buff = luaI_buffer(20);
|
||||||
{
|
if (lua_isstring(obj)) /* get strings and numbers */
|
||||||
lua_error ("function stack overflow");
|
return lua_getstring(obj);
|
||||||
return 1;
|
else switch(lua_type(obj))
|
||||||
}
|
|
||||||
funcstack[nfuncstack].file = file;
|
|
||||||
funcstack[nfuncstack].function = function;
|
|
||||||
nfuncstack++;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Called to execute RESET opcode, this function pops a function from
|
|
||||||
** function stack.
|
|
||||||
*/
|
|
||||||
void lua_popfunction (void)
|
|
||||||
{
|
|
||||||
nfuncstack--;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Report bug building a message and sending it to lua_error function.
|
|
||||||
*/
|
|
||||||
void lua_reportbug (char *s)
|
|
||||||
{
|
|
||||||
char msg[1024];
|
|
||||||
strcpy (msg, s);
|
|
||||||
if (lua_debugline != 0)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
if (nfuncstack > 0)
|
|
||||||
{
|
{
|
||||||
sprintf (strchr(msg,0),
|
case LUA_T_FUNCTION:
|
||||||
"\n\tin statement begining at line %d in function \"%s\" of file \"%s\"",
|
sprintf(buff, "function: %p", (luaI_Address(obj))->value.tf);
|
||||||
lua_debugline, lua_varname(funcstack[nfuncstack-1].function),
|
break;
|
||||||
lua_file[funcstack[nfuncstack-1].file]);
|
case LUA_T_CFUNCTION:
|
||||||
sprintf (strchr(msg,0), "\n\tactive stack\n");
|
sprintf(buff, "cfunction: %p", lua_getcfunction(obj));
|
||||||
for (i=nfuncstack-1; i>=0; i--)
|
break;
|
||||||
sprintf (strchr(msg,0), "\t-> function \"%s\" of file \"%s\"\n",
|
case LUA_T_ARRAY:
|
||||||
lua_varname(funcstack[i].function),
|
sprintf(buff, "table: %p", avalue(luaI_Address(obj)));
|
||||||
lua_file[funcstack[i].file]);
|
break;
|
||||||
|
case LUA_T_NIL:
|
||||||
|
sprintf(buff, "nil");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
sprintf(buff, "userdata: %p", lua_getuserdata(obj));
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
return buff;
|
||||||
|
}
|
||||||
|
|
||||||
|
void luaI_tostring (void)
|
||||||
|
{
|
||||||
|
lua_pushstring(tostring(lua_getparam(1)));
|
||||||
|
}
|
||||||
|
|
||||||
|
void luaI_print (void)
|
||||||
|
{
|
||||||
|
int i = 1;
|
||||||
|
lua_Object obj;
|
||||||
|
while ((obj = lua_getparam(i++)) != LUA_NOOBJECT)
|
||||||
|
printf("%s\n", tostring(obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Internal function: return an object type.
|
||||||
|
*/
|
||||||
|
void luaI_type (void)
|
||||||
|
{
|
||||||
|
lua_Object o = lua_getparam(1);
|
||||||
|
int t;
|
||||||
|
if (o == LUA_NOOBJECT)
|
||||||
|
lua_error("no parameter to function 'type'");
|
||||||
|
t = lua_type(o);
|
||||||
|
switch (t)
|
||||||
|
{
|
||||||
|
case LUA_T_NIL :
|
||||||
|
lua_pushliteral("nil");
|
||||||
|
break;
|
||||||
|
case LUA_T_NUMBER :
|
||||||
|
lua_pushliteral("number");
|
||||||
|
break;
|
||||||
|
case LUA_T_STRING :
|
||||||
|
lua_pushliteral("string");
|
||||||
|
break;
|
||||||
|
case LUA_T_ARRAY :
|
||||||
|
lua_pushliteral("table");
|
||||||
|
break;
|
||||||
|
case LUA_T_FUNCTION :
|
||||||
|
case LUA_T_CFUNCTION :
|
||||||
|
lua_pushliteral("function");
|
||||||
|
break;
|
||||||
|
default :
|
||||||
|
lua_pushliteral("userdata");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
lua_pushnumber(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Internal function: convert an object to a number
|
||||||
|
*/
|
||||||
|
void lua_obj2number (void)
|
||||||
|
{
|
||||||
|
lua_Object o = lua_getparam(1);
|
||||||
|
if (lua_isnumber(o))
|
||||||
|
lua_pushnumber(lua_getnumber(o));
|
||||||
else
|
else
|
||||||
{
|
lua_pushnil();
|
||||||
sprintf (strchr(msg,0),
|
|
||||||
"\n\tin statement begining at line %d of file \"%s\"",
|
|
||||||
lua_debugline, lua_filename());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
lua_error (msg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaI_error (void)
|
||||||
|
{
|
||||||
|
char *s = lua_getstring(lua_getparam(1));
|
||||||
|
if (s == NULL) s = "(no message)";
|
||||||
|
lua_error(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
void luaI_assert (void)
|
||||||
|
{
|
||||||
|
lua_Object p = lua_getparam(1);
|
||||||
|
if (p == LUA_NOOBJECT || lua_isnil(p))
|
||||||
|
lua_error("assertion failed!");
|
||||||
|
}
|
||||||
|
|
||||||
|
void luaI_setglobal (void)
|
||||||
|
{
|
||||||
|
lua_Object name = lua_getparam(1);
|
||||||
|
lua_Object value = lua_getparam(2);
|
||||||
|
if (!lua_isstring(name))
|
||||||
|
lua_error("incorrect argument to function `setglobal'");
|
||||||
|
lua_pushobject(value);
|
||||||
|
lua_storeglobal(lua_getstring(name));
|
||||||
|
lua_pushobject(value); /* return given value */
|
||||||
|
}
|
||||||
|
|
||||||
|
void luaI_getglobal (void)
|
||||||
|
{
|
||||||
|
lua_Object name = lua_getparam(1);
|
||||||
|
if (!lua_isstring(name))
|
||||||
|
lua_error("incorrect argument to function `getglobal'");
|
||||||
|
lua_pushobject(lua_getglobal(lua_getstring(name)));
|
||||||
|
}
|
||||||
|
|||||||
30
inout.h
30
inout.h
@@ -1,21 +1,33 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: $
|
** $Id: inout.h,v 1.14 1996/03/15 13:13:13 roberto Exp roberto $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#ifndef inout_h
|
#ifndef inout_h
|
||||||
#define inout_h
|
#define inout_h
|
||||||
|
|
||||||
extern int lua_linenumber;
|
#include "types.h"
|
||||||
extern int lua_debug;
|
#include <stdio.h>
|
||||||
extern int lua_debugline;
|
|
||||||
|
|
||||||
int lua_openfile (char *fn);
|
|
||||||
|
extern Word lua_linenumber;
|
||||||
|
extern Word lua_debugline;
|
||||||
|
extern char *lua_parsedfile;
|
||||||
|
|
||||||
|
FILE *lua_openfile (char *fn);
|
||||||
void lua_closefile (void);
|
void lua_closefile (void);
|
||||||
int lua_openstring (char *s);
|
void lua_openstring (char *s);
|
||||||
void lua_closestring (void);
|
void lua_closestring (void);
|
||||||
int lua_pushfunction (int file, int function);
|
|
||||||
void lua_popfunction (void);
|
void lua_internaldofile (void);
|
||||||
void lua_reportbug (char *s);
|
void lua_internaldostring (void);
|
||||||
|
void luaI_tostring (void);
|
||||||
|
void luaI_print (void);
|
||||||
|
void luaI_type (void);
|
||||||
|
void lua_obj2number (void);
|
||||||
|
void luaI_error (void);
|
||||||
|
void luaI_assert (void);
|
||||||
|
void luaI_setglobal (void);
|
||||||
|
void luaI_getglobal (void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
775
iolib.c
775
iolib.c
@@ -3,64 +3,88 @@
|
|||||||
** Input/output library to LUA
|
** Input/output library to LUA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
char *rcs_iolib="$Id: iolib.c,v 1.3 1994/03/28 15:14:02 celes Exp celes $";
|
char *rcs_iolib="$Id: iolib.c,v 1.39 1996/03/14 15:55:18 roberto Exp roberto $";
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#ifdef __GNUC__
|
#include <string.h>
|
||||||
#include <floatingpoint.h>
|
#include <time.h>
|
||||||
#endif
|
#include <stdlib.h>
|
||||||
|
#include <errno.h>
|
||||||
#include "mm.h"
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
#include "luadebug.h"
|
||||||
|
#include "lualib.h"
|
||||||
|
|
||||||
static FILE *in=stdin, *out=stdout;
|
static FILE *in=stdin, *out=stdout;
|
||||||
|
|
||||||
|
|
||||||
|
#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_pushnumber (1);
|
||||||
|
else
|
||||||
|
lua_pushnil();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void closeread (void)
|
||||||
|
{
|
||||||
|
if (in != stdin)
|
||||||
|
{
|
||||||
|
if (pclose(in) == -1)
|
||||||
|
fclose(in);
|
||||||
|
in = stdin;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void closewrite (void)
|
||||||
|
{
|
||||||
|
if (out != stdout)
|
||||||
|
{
|
||||||
|
if (pclose(out) == -1)
|
||||||
|
fclose(out);
|
||||||
|
out = stdout;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Open a file to read.
|
** Open a file to read.
|
||||||
** LUA interface:
|
** LUA interface:
|
||||||
** status = readfrom (filename)
|
** status = readfrom (filename)
|
||||||
** where:
|
** where:
|
||||||
** status = 1 -> success
|
** status = 1 -> success
|
||||||
** status = 0 -> error
|
** status = nil -> error
|
||||||
*/
|
*/
|
||||||
static void io_readfrom (void)
|
static void io_readfrom (void)
|
||||||
{
|
{
|
||||||
lua_Object o = lua_getparam (1);
|
if (lua_getparam (1) == LUA_NOOBJECT)
|
||||||
if (o == NULL) /* restore standart input */
|
{ /* restore standart input */
|
||||||
{
|
closeread();
|
||||||
if (in != stdin)
|
|
||||||
{
|
|
||||||
fclose (in);
|
|
||||||
in = stdin;
|
|
||||||
}
|
|
||||||
lua_pushnumber (1);
|
lua_pushnumber (1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (!lua_isstring (o))
|
char *s = lua_check_string(1, "readfrom");
|
||||||
{
|
FILE *fp = (*s == '|') ? popen(s+1, "r") : fopen(s, "r");
|
||||||
lua_error ("incorrect argument to function 'readfrom`");
|
|
||||||
lua_pushnumber (0);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
FILE *fp = fopen (lua_getstring(o),"r");
|
|
||||||
if (fp == NULL)
|
if (fp == NULL)
|
||||||
{
|
lua_pushnil();
|
||||||
lua_pushnumber (0);
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (in != stdin) fclose (in);
|
closeread();
|
||||||
in = fp;
|
in = fp;
|
||||||
lua_pushnumber (1);
|
lua_pushnumber (1);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,41 +95,27 @@ static void io_readfrom (void)
|
|||||||
** status = writeto (filename)
|
** status = writeto (filename)
|
||||||
** where:
|
** where:
|
||||||
** status = 1 -> success
|
** status = 1 -> success
|
||||||
** status = 0 -> error
|
** status = nil -> error
|
||||||
*/
|
*/
|
||||||
static void io_writeto (void)
|
static void io_writeto (void)
|
||||||
{
|
{
|
||||||
lua_Object o = lua_getparam (1);
|
if (lua_getparam (1) == LUA_NOOBJECT) /* restore standart output */
|
||||||
if (o == NULL) /* restore standart output */
|
|
||||||
{
|
{
|
||||||
if (out != stdout)
|
closewrite();
|
||||||
{
|
|
||||||
fclose (out);
|
|
||||||
out = stdout;
|
|
||||||
}
|
|
||||||
lua_pushnumber (1);
|
lua_pushnumber (1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (!lua_isstring (o))
|
char *s = lua_check_string(1, "writeto");
|
||||||
{
|
FILE *fp = (*s == '|') ? popen(s+1,"w") : fopen(s,"w");
|
||||||
lua_error ("incorrect argument to function 'writeto`");
|
|
||||||
lua_pushnumber (0);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
FILE *fp = fopen (lua_getstring(o),"w");
|
|
||||||
if (fp == NULL)
|
if (fp == NULL)
|
||||||
{
|
lua_pushnil();
|
||||||
lua_pushnumber (0);
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (out != stdout) fclose (out);
|
closewrite();
|
||||||
out = fp;
|
out = fp;
|
||||||
lua_pushnumber (1);
|
lua_pushnumber (1);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,50 +127,63 @@ static void io_writeto (void)
|
|||||||
** where:
|
** where:
|
||||||
** status = 2 -> success (already exist)
|
** status = 2 -> success (already exist)
|
||||||
** status = 1 -> success (new file)
|
** status = 1 -> success (new file)
|
||||||
** status = 0 -> error
|
** status = nil -> error
|
||||||
*/
|
*/
|
||||||
static void io_appendto (void)
|
static void io_appendto (void)
|
||||||
{
|
{
|
||||||
lua_Object o = lua_getparam (1);
|
char *s = lua_check_string(1, "appendto");
|
||||||
if (o == NULL) /* restore standart output */
|
struct stat st;
|
||||||
{
|
int r = (stat(s, &st) == -1) ? 1 : 2;
|
||||||
if (out != stdout)
|
FILE *fp = fopen (s, "a");
|
||||||
{
|
if (fp == NULL)
|
||||||
fclose (out);
|
lua_pushnil();
|
||||||
out = stdout;
|
|
||||||
}
|
|
||||||
lua_pushnumber (1);
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (!lua_isstring (o))
|
if (out != stdout) fclose (out);
|
||||||
{
|
out = fp;
|
||||||
lua_error ("incorrect argument to function 'appendto`");
|
lua_pushnumber (r);
|
||||||
lua_pushnumber (0);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
int r;
|
|
||||||
FILE *fp;
|
|
||||||
struct stat st;
|
|
||||||
if (stat(lua_getstring(o), &st) == -1) r = 1;
|
|
||||||
else r = 2;
|
|
||||||
fp = fopen (lua_getstring(o),"a");
|
|
||||||
if (fp == NULL)
|
|
||||||
{
|
|
||||||
lua_pushnumber (0);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (out != stdout) fclose (out);
|
|
||||||
out = fp;
|
|
||||||
lua_pushnumber (r);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static char getformat (char *f, int *just, int *m, int *n)
|
||||||
|
{
|
||||||
|
int t;
|
||||||
|
switch (*f++)
|
||||||
|
{
|
||||||
|
case 'q': case 'Q':
|
||||||
|
case 's': case 'S':
|
||||||
|
case 'i': case 'I':
|
||||||
|
t = tolower(*(f-1));
|
||||||
|
break;
|
||||||
|
case 'f': case 'F': case 'g': case 'G': case 'e': case 'E':
|
||||||
|
t = 'f';
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
t = 0; /* to avoid compiler warnings */
|
||||||
|
lua_arg_error("read/write (format)");
|
||||||
|
}
|
||||||
|
*just = (*f == '<' || *f == '>' || *f == '|') ? *f++ : '>';
|
||||||
|
if (isdigit(*f))
|
||||||
|
{
|
||||||
|
*m = 0;
|
||||||
|
while (isdigit(*f))
|
||||||
|
*m = *m*10 + (*f++ - '0');
|
||||||
|
}
|
||||||
|
else
|
||||||
|
*m = -1;
|
||||||
|
if (*f == '.')
|
||||||
|
{
|
||||||
|
f++; /* skip point */
|
||||||
|
*n = 0;
|
||||||
|
while (isdigit(*f))
|
||||||
|
*n = *n*10 + (*f++ - '0');
|
||||||
|
}
|
||||||
|
else
|
||||||
|
*n = -1;
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Read a variable. On error put nil on stack.
|
** Read a variable. On error put nil on stack.
|
||||||
@@ -176,140 +199,130 @@ static void io_appendto (void)
|
|||||||
** Estes especificadores podem vir seguidos de numero que representa
|
** Estes especificadores podem vir seguidos de numero que representa
|
||||||
** o numero de campos a serem lidos.
|
** o numero de campos a serem lidos.
|
||||||
*/
|
*/
|
||||||
static void io_read (void)
|
|
||||||
|
static int read_until_char (int del)
|
||||||
|
{
|
||||||
|
int c;
|
||||||
|
while((c = fgetc(in)) != EOF && c != del)
|
||||||
|
luaI_addchar(c);
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void read_until_blank (void)
|
||||||
|
{
|
||||||
|
int c;
|
||||||
|
while((c = fgetc(in)) != EOF && !isspace(c))
|
||||||
|
luaI_addchar(c);
|
||||||
|
if (c != EOF) ungetc(c,in);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void read_m (int m)
|
||||||
|
{
|
||||||
|
int c;
|
||||||
|
while (m-- && (c = fgetc(in)) != EOF)
|
||||||
|
luaI_addchar(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void read_free (void)
|
||||||
{
|
{
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == NULL || !lua_isstring(o)) /* free format */
|
|
||||||
{
|
|
||||||
int c;
|
int c;
|
||||||
char s[256];
|
|
||||||
while (isspace(c=fgetc(in)))
|
while (isspace(c=fgetc(in)))
|
||||||
;
|
;
|
||||||
if (c == '\"')
|
if (c == EOF)
|
||||||
{
|
{
|
||||||
int c, n=0;
|
|
||||||
while((c = fgetc(in)) != '\"')
|
|
||||||
{
|
|
||||||
if (c == EOF)
|
|
||||||
{
|
|
||||||
lua_pushnil ();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
s[n++] = c;
|
|
||||||
}
|
|
||||||
s[n] = 0;
|
|
||||||
}
|
|
||||||
else if (c == '\'')
|
|
||||||
{
|
|
||||||
int c, n=0;
|
|
||||||
while((c = fgetc(in)) != '\'')
|
|
||||||
{
|
|
||||||
if (c == EOF)
|
|
||||||
{
|
|
||||||
lua_pushnil ();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
s[n++] = c;
|
|
||||||
}
|
|
||||||
s[n] = 0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
char *ptr;
|
|
||||||
double d;
|
|
||||||
ungetc (c, in);
|
|
||||||
if (fscanf (in, "%s", s) != 1)
|
|
||||||
{
|
|
||||||
lua_pushnil ();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
d = strtod (s, &ptr);
|
|
||||||
if (!(*ptr))
|
|
||||||
{
|
|
||||||
lua_pushnumber (d);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
lua_pushstring (s);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else /* formatted */
|
|
||||||
{
|
|
||||||
char *e = lua_getstring(o);
|
|
||||||
char t;
|
|
||||||
int m=0;
|
|
||||||
while (isspace(*e)) e++;
|
|
||||||
t = *e++;
|
|
||||||
while (isdigit(*e))
|
|
||||||
m = m*10 + (*e++ - '0');
|
|
||||||
|
|
||||||
if (m > 0)
|
|
||||||
{
|
|
||||||
char f[80];
|
|
||||||
char s[256];
|
|
||||||
sprintf (f, "%%%ds", m);
|
|
||||||
if (fgets (s, m, in) == NULL)
|
|
||||||
{
|
|
||||||
lua_pushnil();
|
lua_pushnil();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else
|
if (c == '\"' || c == '\'')
|
||||||
{
|
{ /* string */
|
||||||
if (s[strlen(s)-1] == '\n')
|
c = read_until_char(c);
|
||||||
s[strlen(s)-1] = 0;
|
if (c == EOF)
|
||||||
}
|
lua_pushnil();
|
||||||
switch (tolower(t))
|
else
|
||||||
{
|
lua_pushstring(luaI_addchar(0));
|
||||||
case 'i':
|
|
||||||
{
|
|
||||||
long int l;
|
|
||||||
sscanf (s, "%ld", &l);
|
|
||||||
lua_pushnumber(l);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'f': case 'g': case 'e':
|
|
||||||
{
|
|
||||||
float f;
|
|
||||||
sscanf (s, "%f", &f);
|
|
||||||
lua_pushnumber(f);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
lua_pushstring(s);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
switch (tolower(t))
|
double d;
|
||||||
{
|
char dummy;
|
||||||
case 'i':
|
char *s;
|
||||||
{
|
luaI_addchar(c);
|
||||||
long int l;
|
read_until_blank();
|
||||||
if (fscanf (in, "%ld", &l) == EOF)
|
s = luaI_addchar(0);
|
||||||
lua_pushnil();
|
if (sscanf(s, "%lf %c", &d, &dummy) == 1)
|
||||||
else lua_pushnumber(l);
|
lua_pushnumber(d);
|
||||||
}
|
else
|
||||||
break;
|
lua_pushstring(s);
|
||||||
case 'f': case 'g': case 'e':
|
|
||||||
{
|
|
||||||
float f;
|
|
||||||
if (fscanf (in, "%f", &f) == EOF)
|
|
||||||
lua_pushnil();
|
|
||||||
else lua_pushnumber(f);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
{
|
|
||||||
char s[256];
|
|
||||||
if (fscanf (in, "%s", s) == EOF)
|
|
||||||
lua_pushnil();
|
|
||||||
else lua_pushstring(s);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void io_read (void)
|
||||||
|
{
|
||||||
|
lua_Object o = lua_getparam (1);
|
||||||
|
luaI_addchar(0); /* initialize buffer */
|
||||||
|
if (o == LUA_NOOBJECT) /* free format */
|
||||||
|
read_free();
|
||||||
|
else /* formatted */
|
||||||
|
{
|
||||||
|
int m, dummy1, dummy2;
|
||||||
|
switch (getformat(lua_check_string(1, "read"), &dummy1, &m, &dummy2))
|
||||||
|
{
|
||||||
|
case 's':
|
||||||
|
{
|
||||||
|
char *s;
|
||||||
|
if (m < 0)
|
||||||
|
read_until_blank();
|
||||||
|
else
|
||||||
|
read_m(m);
|
||||||
|
s = luaI_addchar(0);
|
||||||
|
if ((m >= 0 && strlen(s) == m) || (m < 0 && strlen(s) > 0))
|
||||||
|
lua_pushstring(s);
|
||||||
|
else
|
||||||
|
lua_pushnil();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case 'i': /* can read as float, since it makes no difference to Lua */
|
||||||
|
case 'f':
|
||||||
|
{
|
||||||
|
double d;
|
||||||
|
int result;
|
||||||
|
if (m < 0)
|
||||||
|
result = fscanf(in, "%lf", &d);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
read_m(m);
|
||||||
|
result = sscanf(luaI_addchar(0), "%lf", &d);
|
||||||
|
}
|
||||||
|
if (result == 1)
|
||||||
|
lua_pushnumber(d);
|
||||||
|
else
|
||||||
|
lua_pushnil();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
lua_arg_error("read (format)");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Read characters until a given one. The delimiter is not read.
|
||||||
|
*/
|
||||||
|
static void io_readuntil (void)
|
||||||
|
{
|
||||||
|
int del, c;
|
||||||
|
lua_Object p = lua_getparam(1);
|
||||||
|
luaI_addchar(0); /* initialize buffer */
|
||||||
|
if (p == LUA_NOOBJECT || lua_isnil(p))
|
||||||
|
del = EOF;
|
||||||
|
else
|
||||||
|
del = *lua_check_string(1, "readuntil");
|
||||||
|
c = read_until_char(del);
|
||||||
|
if (c != EOF) ungetc(c,in);
|
||||||
|
lua_pushstring(luaI_addchar(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -339,131 +352,252 @@ static void io_read (void)
|
|||||||
** inteiros -> numero minimo de digitos
|
** inteiros -> numero minimo de digitos
|
||||||
** string -> nao se aplica
|
** string -> nao se aplica
|
||||||
*/
|
*/
|
||||||
static char *buildformat (char *e, lua_Object o)
|
|
||||||
{
|
|
||||||
static char buffer[512];
|
|
||||||
static char f[80];
|
|
||||||
char *string = &buffer[255];
|
|
||||||
char t, j='r';
|
|
||||||
int m=0, n=0, l;
|
|
||||||
while (isspace(*e)) e++;
|
|
||||||
t = *e++;
|
|
||||||
if (*e == '<' || *e == '|' || *e == '>') j = *e++;
|
|
||||||
while (isdigit(*e))
|
|
||||||
m = m*10 + (*e++ - '0');
|
|
||||||
e++; /* skip point */
|
|
||||||
while (isdigit(*e))
|
|
||||||
n = n*10 + (*e++ - '0');
|
|
||||||
|
|
||||||
sprintf(f,"%%");
|
static int write_fill (int n, int c)
|
||||||
if (j == '<' || j == '|') sprintf(strchr(f,0),"-");
|
{
|
||||||
if (m != 0) sprintf(strchr(f,0),"%d", m);
|
while (n--)
|
||||||
if (n != 0) sprintf(strchr(f,0),".%d", n);
|
if (fputc(c, out) == EOF)
|
||||||
sprintf(strchr(f,0), "%c", t);
|
return 0;
|
||||||
switch (tolower(t))
|
return 1;
|
||||||
{
|
|
||||||
case 'i': t = 'i';
|
|
||||||
sprintf (string, f, (long int)lua_getnumber(o));
|
|
||||||
break;
|
|
||||||
case 'f': case 'g': case 'e': t = 'f';
|
|
||||||
sprintf (string, f, (float)lua_getnumber(o));
|
|
||||||
break;
|
|
||||||
case 's': t = 's';
|
|
||||||
sprintf (string, f, lua_getstring(o));
|
|
||||||
break;
|
|
||||||
default: return "";
|
|
||||||
}
|
|
||||||
l = strlen(string);
|
|
||||||
if (m!=0 && l>m)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
for (i=0; i<m; i++)
|
|
||||||
string[i] = '*';
|
|
||||||
string[i] = 0;
|
|
||||||
}
|
|
||||||
else if (m!=0 && j=='|')
|
|
||||||
{
|
|
||||||
int i=l-1;
|
|
||||||
while (isspace(string[i])) i--;
|
|
||||||
string -= (m-i) / 2;
|
|
||||||
i=0;
|
|
||||||
while (string[i]==0) string[i++] = ' ';
|
|
||||||
string[l] = 0;
|
|
||||||
}
|
|
||||||
return string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int write_string (char *s, int just, int m)
|
||||||
|
{
|
||||||
|
int status;
|
||||||
|
int l = strlen(s);
|
||||||
|
int pre; /* number of blanks before string */
|
||||||
|
if (m < 0) m = l;
|
||||||
|
else if (l > m)
|
||||||
|
{
|
||||||
|
write_fill(m, '*');
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
pre = (just == '<') ? 0 : (just == '>') ? m-l : (m-l)/2;
|
||||||
|
status = write_fill(pre, ' ');
|
||||||
|
status = status && fprintf(out, "%s", s) >= 0;
|
||||||
|
status = status && write_fill(m-(l+pre), ' ');
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int write_quoted (int just, int m)
|
||||||
|
{
|
||||||
|
luaI_addchar(0);
|
||||||
|
luaI_addquoted(lua_check_string(1, "write"));
|
||||||
|
return write_string(luaI_addchar(0), just, m);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int write_float (int just, int m, int n)
|
||||||
|
{
|
||||||
|
char buffer[100];
|
||||||
|
lua_Object p = lua_getparam(1);
|
||||||
|
float number;
|
||||||
|
if (!lua_isnumber(p)) return 0;
|
||||||
|
number = lua_getnumber(p);
|
||||||
|
if (n >= 0)
|
||||||
|
sprintf(buffer, "%.*f", n, number);
|
||||||
|
else
|
||||||
|
sprintf(buffer, "%g", number);
|
||||||
|
return write_string(buffer, just, m);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int write_int (int just, int m, int n)
|
||||||
|
{
|
||||||
|
char buffer[100];
|
||||||
|
lua_Object p = lua_getparam(1);
|
||||||
|
int number;
|
||||||
|
if (!lua_isnumber(p)) return 0;
|
||||||
|
number = (int)lua_getnumber(p);
|
||||||
|
if (n >= 0)
|
||||||
|
sprintf(buffer, "%.*d", n, number);
|
||||||
|
else
|
||||||
|
sprintf(buffer, "%d", number);
|
||||||
|
return write_string(buffer, just, m);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static void io_write (void)
|
static void io_write (void)
|
||||||
{
|
{
|
||||||
lua_Object o1 = lua_getparam (1);
|
int status = 0;
|
||||||
lua_Object o2 = lua_getparam (2);
|
if (lua_getparam (2) == LUA_NOOBJECT) /* free format */
|
||||||
if (o1 == NULL) /* new line */
|
{
|
||||||
{
|
lua_Object o1 = lua_getparam(1);
|
||||||
fprintf (out, "\n");
|
int t = lua_type(o1);
|
||||||
lua_pushnumber(1);
|
if (t == LUA_T_NUMBER)
|
||||||
}
|
status = fprintf (out, "%g", lua_getnumber(o1)) >= 0;
|
||||||
else if (o2 == NULL) /* free format */
|
else if (t == LUA_T_STRING)
|
||||||
{
|
status = fprintf (out, "%s", lua_getstring(o1)) >= 0;
|
||||||
int status=0;
|
|
||||||
if (lua_isnumber(o1))
|
|
||||||
status = fprintf (out, "%g", lua_getnumber(o1));
|
|
||||||
else if (lua_isstring(o1))
|
|
||||||
status = fprintf (out, "%s", lua_getstring(o1));
|
|
||||||
lua_pushnumber(status);
|
|
||||||
}
|
|
||||||
else /* formated */
|
|
||||||
{
|
|
||||||
if (!lua_isstring(o2))
|
|
||||||
{
|
|
||||||
lua_error ("incorrect format to function `write'");
|
|
||||||
lua_pushnumber(0);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
lua_pushnumber(fprintf (out, "%s", buildformat(lua_getstring(o2),o1)));
|
else /* formated */
|
||||||
}
|
{
|
||||||
|
int just, m, n;
|
||||||
|
switch (getformat(lua_check_string(2, "write"), &just, &m, &n))
|
||||||
|
{
|
||||||
|
case 's':
|
||||||
|
{
|
||||||
|
lua_Object p = lua_getparam(1);
|
||||||
|
if (lua_isstring(p))
|
||||||
|
status = write_string(lua_getstring(p), just, m);
|
||||||
|
else
|
||||||
|
status = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'q':
|
||||||
|
status = write_quoted(just, m);
|
||||||
|
break;
|
||||||
|
case 'f':
|
||||||
|
status = write_float(just, m, n);
|
||||||
|
break;
|
||||||
|
case 'i':
|
||||||
|
status = write_int(just, m, n);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (status)
|
||||||
|
lua_pushnumber(status);
|
||||||
|
else
|
||||||
|
lua_pushnil();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Execute a executable program using "system".
|
** Execute a executable program using "system".
|
||||||
** Return the result of execution.
|
** Return the result of execution.
|
||||||
*/
|
*/
|
||||||
void io_execute (void)
|
static void io_execute (void)
|
||||||
{
|
{
|
||||||
lua_Object o = lua_getparam (1);
|
lua_pushnumber(system(lua_check_string(1, "execute")));
|
||||||
if (o == NULL || !lua_isstring (o))
|
|
||||||
{
|
|
||||||
lua_error ("incorrect argument to function 'execute`");
|
|
||||||
lua_pushnumber (0);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
int res = system(lua_getstring(o));
|
|
||||||
lua_pushnumber (res);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Remove a file.
|
** Remove a file. On error return nil.
|
||||||
** On error put 0 on stack, otherwise put 1.
|
|
||||||
*/
|
*/
|
||||||
void io_remove (void)
|
static void io_remove (void)
|
||||||
{
|
{
|
||||||
lua_Object o = lua_getparam (1);
|
pushresult(remove(lua_check_string(1, "remove")) == 0);
|
||||||
if (o == NULL || !lua_isstring (o))
|
|
||||||
{
|
|
||||||
lua_error ("incorrect argument to function 'execute`");
|
|
||||||
lua_pushnumber (0);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (remove(lua_getstring(o)) == 0)
|
|
||||||
lua_pushnumber (1);
|
|
||||||
else
|
|
||||||
lua_pushnumber (0);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void io_rename (void)
|
||||||
|
{
|
||||||
|
char *f1 = lua_check_string(1, "rename");
|
||||||
|
char *f2 = lua_check_string(2, "rename");
|
||||||
|
pushresult(rename(f1, f2) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void io_tmpname (void)
|
||||||
|
{
|
||||||
|
lua_pushstring(tmpnam(NULL));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void io_errorno (void)
|
||||||
|
{
|
||||||
|
/* lua_pushstring(strerror(errno));*/
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** To get a environment variable
|
||||||
|
*/
|
||||||
|
static void io_getenv (void)
|
||||||
|
{
|
||||||
|
char *env = getenv(lua_check_string(1, "getenv"));
|
||||||
|
lua_pushstring(env); /* if NULL push nil */
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Return user formatted time stamp
|
||||||
|
*/
|
||||||
|
static void io_date (void)
|
||||||
|
{
|
||||||
|
time_t t;
|
||||||
|
struct tm *tm;
|
||||||
|
char *s;
|
||||||
|
char b[BUFSIZ];
|
||||||
|
if (lua_getparam(1) == LUA_NOOBJECT)
|
||||||
|
s = "%c";
|
||||||
|
else
|
||||||
|
s = lua_check_string(1, "date");
|
||||||
|
time(&t); tm = localtime(&t);
|
||||||
|
if (strftime(b,sizeof(b),s,tm))
|
||||||
|
lua_pushstring(b);
|
||||||
|
else
|
||||||
|
lua_error("invalid `date' format");
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** To exit
|
||||||
|
*/
|
||||||
|
static void io_exit (void)
|
||||||
|
{
|
||||||
|
lua_Object o = lua_getparam(1);
|
||||||
|
if (lua_isstring(o))
|
||||||
|
fprintf(stderr, "%s\n", lua_getstring(o));
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** To debug a lua program. Start a dialog with the user, interpreting
|
||||||
|
lua commands until an 'cont'.
|
||||||
|
*/
|
||||||
|
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") == 0) return;
|
||||||
|
lua_dostring(buffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void lua_printstack (FILE *f)
|
||||||
|
{
|
||||||
|
int level = 0;
|
||||||
|
lua_Object func;
|
||||||
|
fprintf(f, "Active Stack:\n");
|
||||||
|
while ((func = lua_stackedfunction(level++)) != LUA_NOOBJECT)
|
||||||
|
{
|
||||||
|
char *name;
|
||||||
|
int currentline;
|
||||||
|
fprintf(f, "\t");
|
||||||
|
switch (*lua_getobjname(func, &name))
|
||||||
|
{
|
||||||
|
case 'g':
|
||||||
|
fprintf(f, "function %s", name);
|
||||||
|
break;
|
||||||
|
case 'f':
|
||||||
|
fprintf(f, "`%s' fallback", name);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
char *filename;
|
||||||
|
int linedefined;
|
||||||
|
lua_funcinfo(func, &filename, &linedefined);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ((currentline = lua_currentline(func)) > 0)
|
||||||
|
fprintf(f, " at line %d", currentline);
|
||||||
|
fprintf(f, "\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void errorfb (void)
|
||||||
|
{
|
||||||
|
lua_Object o = lua_getparam(1);
|
||||||
|
char *s = lua_isstring(o) ? lua_getstring(o) : "(no messsage)";
|
||||||
|
fprintf(stderr, "lua: %s\n", s);
|
||||||
|
lua_printstack(stderr);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Open io library
|
** Open io library
|
||||||
*/
|
*/
|
||||||
@@ -473,7 +607,18 @@ void iolib_open (void)
|
|||||||
lua_register ("writeto", io_writeto);
|
lua_register ("writeto", io_writeto);
|
||||||
lua_register ("appendto", io_appendto);
|
lua_register ("appendto", io_appendto);
|
||||||
lua_register ("read", io_read);
|
lua_register ("read", io_read);
|
||||||
|
lua_register ("readuntil",io_readuntil);
|
||||||
lua_register ("write", io_write);
|
lua_register ("write", io_write);
|
||||||
lua_register ("execute", io_execute);
|
lua_register ("execute", io_execute);
|
||||||
lua_register ("remove", io_remove);
|
lua_register ("remove", io_remove);
|
||||||
|
lua_register ("rename", io_rename);
|
||||||
|
lua_register ("tmpname", io_tmpname);
|
||||||
|
lua_register ("ioerror", io_errorno);
|
||||||
|
lua_register ("getenv", io_getenv);
|
||||||
|
lua_register ("date", io_date);
|
||||||
|
lua_register ("exit", io_exit);
|
||||||
|
lua_register ("debug", io_debug);
|
||||||
|
lua_register ("print_stack", errorfb);
|
||||||
|
lua_setfallback("error", errorfb);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
229
lex.c
229
lex.c
@@ -1,32 +1,26 @@
|
|||||||
char *rcs_lex = "$Id: lex.c,v 1.3 1993/12/28 16:42:29 roberto Exp celes $";
|
char *rcs_lex = "$Id: lex.c,v 2.31 1996/03/19 16:50:24 roberto Exp roberto $";
|
||||||
/*$Log: lex.c,v $
|
|
||||||
* Revision 1.3 1993/12/28 16:42:29 roberto
|
|
||||||
* "include"s de string.h e stdlib.h para evitar warnings
|
|
||||||
*
|
|
||||||
* Revision 1.2 1993/12/22 21:39:15 celes
|
|
||||||
* Tratamento do token $debug e $nodebug
|
|
||||||
*
|
|
||||||
* Revision 1.1 1993/12/22 21:15:16 roberto
|
|
||||||
* Initial revision
|
|
||||||
**/
|
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <math.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "opcode.h"
|
#include "mem.h"
|
||||||
#include "hash.h"
|
#include "tree.h"
|
||||||
#include "inout.h"
|
|
||||||
#include "table.h"
|
#include "table.h"
|
||||||
#include "y.tab.h"
|
#include "lex.h"
|
||||||
|
#include "inout.h"
|
||||||
|
#include "luadebug.h"
|
||||||
|
#include "parser.h"
|
||||||
|
|
||||||
|
#define MINBUFF 260
|
||||||
|
|
||||||
#define next() { current = input(); }
|
#define next() { current = input(); }
|
||||||
#define save(x) { *yytextLast++ = (x); }
|
#define save(x) { *yytextLast++ = (x); }
|
||||||
#define save_and_next() { save(current); next(); }
|
#define save_and_next() { save(current); next(); }
|
||||||
|
|
||||||
static int current;
|
static int current;
|
||||||
static char yytext[256];
|
static char *yytext = NULL;
|
||||||
|
static int textsize = 0;
|
||||||
static char *yytextLast;
|
static char *yytextLast;
|
||||||
|
|
||||||
static Input input;
|
static Input input;
|
||||||
@@ -35,6 +29,11 @@ void lua_setinput (Input fn)
|
|||||||
{
|
{
|
||||||
current = ' ';
|
current = ' ';
|
||||||
input = fn;
|
input = fn;
|
||||||
|
if (yytext == NULL)
|
||||||
|
{
|
||||||
|
textsize = MINBUFF;
|
||||||
|
yytext = newvector(textsize, char);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
char *lua_lasttext (void)
|
char *lua_lasttext (void)
|
||||||
@@ -44,7 +43,7 @@ char *lua_lasttext (void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static struct
|
static struct
|
||||||
{
|
{
|
||||||
char *name;
|
char *name;
|
||||||
int token;
|
int token;
|
||||||
@@ -66,37 +65,89 @@ static struct
|
|||||||
{"until", UNTIL},
|
{"until", UNTIL},
|
||||||
{"while", WHILE} };
|
{"while", WHILE} };
|
||||||
|
|
||||||
|
|
||||||
#define RESERVEDSIZE (sizeof(reserved)/sizeof(reserved[0]))
|
#define RESERVEDSIZE (sizeof(reserved)/sizeof(reserved[0]))
|
||||||
|
|
||||||
|
|
||||||
int findReserved (char *name)
|
void luaI_addReserved (void)
|
||||||
{
|
{
|
||||||
int l = 0;
|
int i;
|
||||||
int h = RESERVEDSIZE - 1;
|
for (i=0; i<RESERVEDSIZE; i++)
|
||||||
while (l <= h)
|
|
||||||
{
|
{
|
||||||
int m = (l+h)/2;
|
TaggedString *ts = lua_createstring(reserved[i].name);
|
||||||
int comp = strcmp(name, reserved[m].name);
|
ts->marked = reserved[i].token; /* reserved word (always > 255) */
|
||||||
if (comp < 0)
|
|
||||||
h = m-1;
|
|
||||||
else if (comp == 0)
|
|
||||||
return reserved[m].token;
|
|
||||||
else
|
|
||||||
l = m+1;
|
|
||||||
}
|
}
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int yylex ()
|
static void growtext (void)
|
||||||
{
|
{
|
||||||
|
int size = yytextLast - yytext;
|
||||||
|
textsize = growvector(&yytext, textsize, char, lexEM, MAX_WORD);
|
||||||
|
yytextLast = yytext + size;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int read_long_string (void)
|
||||||
|
{
|
||||||
|
int cont = 0;
|
||||||
|
int spaceleft = textsize - (yytextLast - yytext);
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
if (spaceleft <= 2) /* may read more than 1 char in one cicle */
|
||||||
|
{
|
||||||
|
growtext();
|
||||||
|
spaceleft = textsize - (yytextLast - yytext);
|
||||||
|
}
|
||||||
|
switch (current)
|
||||||
|
{
|
||||||
|
case EOF:
|
||||||
|
case 0:
|
||||||
|
return WRONGTOKEN;
|
||||||
|
case '[':
|
||||||
|
save_and_next(); spaceleft--;
|
||||||
|
if (current == '[')
|
||||||
|
{
|
||||||
|
cont++;
|
||||||
|
save_and_next(); spaceleft--;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
case ']':
|
||||||
|
save_and_next(); spaceleft--;
|
||||||
|
if (current == ']')
|
||||||
|
{
|
||||||
|
if (cont == 0) return STRING;
|
||||||
|
cont--;
|
||||||
|
save_and_next(); spaceleft--;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
case '\n':
|
||||||
|
lua_linenumber++; /* goes through */
|
||||||
|
default:
|
||||||
|
save_and_next(); spaceleft--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int luaY_lex (void)
|
||||||
|
{
|
||||||
|
double a;
|
||||||
|
static int linelasttoken = 0;
|
||||||
|
if (lua_debug)
|
||||||
|
luaI_codedebugline(linelasttoken);
|
||||||
|
linelasttoken = lua_linenumber;
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
yytextLast = yytext;
|
yytextLast = yytext;
|
||||||
switch (current)
|
switch (current)
|
||||||
{
|
{
|
||||||
case '\n': lua_linenumber++;
|
case EOF:
|
||||||
|
case 0:
|
||||||
|
return 0;
|
||||||
|
case '\n': linelasttoken = ++lua_linenumber;
|
||||||
case ' ':
|
case ' ':
|
||||||
|
case '\r': /* CR: to avoid problems with DOS/Windows */
|
||||||
case '\t':
|
case '\t':
|
||||||
next();
|
next();
|
||||||
continue;
|
continue;
|
||||||
@@ -108,32 +159,51 @@ int yylex ()
|
|||||||
*yytextLast = 0;
|
*yytextLast = 0;
|
||||||
if (strcmp(yytext, "debug") == 0)
|
if (strcmp(yytext, "debug") == 0)
|
||||||
{
|
{
|
||||||
yylval.vInt = 1;
|
luaY_lval.vInt = 1;
|
||||||
return DEBUG;
|
return DEBUG;
|
||||||
}
|
}
|
||||||
else if (strcmp(yytext, "nodebug") == 0)
|
else if (strcmp(yytext, "nodebug") == 0)
|
||||||
{
|
{
|
||||||
yylval.vInt = 0;
|
luaY_lval.vInt = 0;
|
||||||
return DEBUG;
|
return DEBUG;
|
||||||
}
|
}
|
||||||
return WRONGTOKEN;
|
return WRONGTOKEN;
|
||||||
|
|
||||||
case '-':
|
case '-':
|
||||||
save_and_next();
|
save_and_next();
|
||||||
if (current != '-') return '-';
|
if (current != '-') return '-';
|
||||||
do { next(); } while (current != '\n' && current != 0);
|
do { next(); } while (current != '\n' && current != 0);
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
case '[':
|
||||||
|
save_and_next();
|
||||||
|
if (current != '[') return '[';
|
||||||
|
else
|
||||||
|
{
|
||||||
|
save_and_next(); /* pass the second '[' */
|
||||||
|
if (read_long_string() == WRONGTOKEN)
|
||||||
|
return WRONGTOKEN;
|
||||||
|
save_and_next(); /* pass the second ']' */
|
||||||
|
*(yytextLast-2) = 0; /* erases ']]' */
|
||||||
|
luaY_lval.vWord = luaI_findconstantbyname(yytext+2);
|
||||||
|
return STRING;
|
||||||
|
}
|
||||||
|
|
||||||
|
case '=':
|
||||||
|
save_and_next();
|
||||||
|
if (current != '=') return '=';
|
||||||
|
else { save_and_next(); return EQ; }
|
||||||
|
|
||||||
case '<':
|
case '<':
|
||||||
save_and_next();
|
save_and_next();
|
||||||
if (current != '=') return '<';
|
if (current != '=') return '<';
|
||||||
else { save_and_next(); return LE; }
|
else { save_and_next(); return LE; }
|
||||||
|
|
||||||
case '>':
|
case '>':
|
||||||
save_and_next();
|
save_and_next();
|
||||||
if (current != '=') return '>';
|
if (current != '=') return '>';
|
||||||
else { save_and_next(); return GE; }
|
else { save_and_next(); return GE; }
|
||||||
|
|
||||||
case '~':
|
case '~':
|
||||||
save_and_next();
|
save_and_next();
|
||||||
if (current != '=') return '~';
|
if (current != '=') return '~';
|
||||||
@@ -143,13 +213,21 @@ int yylex ()
|
|||||||
case '\'':
|
case '\'':
|
||||||
{
|
{
|
||||||
int del = current;
|
int del = current;
|
||||||
|
int spaceleft = textsize - (yytextLast - yytext);
|
||||||
next(); /* skip the delimiter */
|
next(); /* skip the delimiter */
|
||||||
while (current != del)
|
while (current != del)
|
||||||
{
|
{
|
||||||
|
if (spaceleft <= 2) /* may read more than 1 char in one cicle */
|
||||||
|
{
|
||||||
|
growtext();
|
||||||
|
spaceleft = textsize - (yytextLast - yytext);
|
||||||
|
}
|
||||||
|
spaceleft--;
|
||||||
switch (current)
|
switch (current)
|
||||||
{
|
{
|
||||||
case 0:
|
case EOF:
|
||||||
case '\n':
|
case 0:
|
||||||
|
case '\n':
|
||||||
return WRONGTOKEN;
|
return WRONGTOKEN;
|
||||||
case '\\':
|
case '\\':
|
||||||
next(); /* do not save the '\' */
|
next(); /* do not save the '\' */
|
||||||
@@ -158,16 +236,17 @@ int yylex ()
|
|||||||
case 'n': save('\n'); next(); break;
|
case 'n': save('\n'); next(); break;
|
||||||
case 't': save('\t'); next(); break;
|
case 't': save('\t'); next(); break;
|
||||||
case 'r': save('\r'); next(); break;
|
case 'r': save('\r'); next(); break;
|
||||||
default : save('\\'); break;
|
case '\n': lua_linenumber++; /* goes through */
|
||||||
|
default : save(current); next(); break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
save_and_next();
|
save_and_next();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
next(); /* skip the delimiter */
|
next(); /* skip the delimiter */
|
||||||
*yytextLast = 0;
|
*yytextLast = 0;
|
||||||
yylval.vWord = lua_findconstant (yytext);
|
luaY_lval.vWord = luaI_findconstantbyname(yytext);
|
||||||
return STRING;
|
return STRING;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -185,49 +264,63 @@ int yylex ()
|
|||||||
case 'Z':
|
case 'Z':
|
||||||
case '_':
|
case '_':
|
||||||
{
|
{
|
||||||
int res;
|
TaggedString *ts;
|
||||||
do { save_and_next(); } while (isalnum(current) || current == '_');
|
do { save_and_next(); } while (isalnum(current) || current == '_');
|
||||||
*yytextLast = 0;
|
*yytextLast = 0;
|
||||||
res = findReserved(yytext);
|
ts = lua_createstring(yytext);
|
||||||
if (res) return res;
|
if (ts->marked > 2)
|
||||||
yylval.pChar = yytext;
|
return ts->marked; /* reserved word */
|
||||||
|
luaY_lval.pTStr = ts;
|
||||||
|
ts->marked = 2; /* avoid GC */
|
||||||
return NAME;
|
return NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
case '.':
|
case '.':
|
||||||
save_and_next();
|
save_and_next();
|
||||||
if (current == '.')
|
if (current == '.')
|
||||||
{
|
{
|
||||||
save_and_next();
|
save_and_next();
|
||||||
return CONC;
|
return CONC;
|
||||||
}
|
}
|
||||||
else if (!isdigit(current)) return '.';
|
else if (!isdigit(current)) return '.';
|
||||||
/* current is a digit: goes through to number */
|
/* current is a digit: goes through to number */
|
||||||
|
a=0.0;
|
||||||
goto fraction;
|
goto fraction;
|
||||||
|
|
||||||
case '0': case '1': case '2': case '3': case '4':
|
case '0': case '1': case '2': case '3': case '4':
|
||||||
case '5': case '6': case '7': case '8': case '9':
|
case '5': case '6': case '7': case '8': case '9':
|
||||||
|
a=0.0;
|
||||||
do { save_and_next(); } while (isdigit(current));
|
do { a=10.0*a+(current-'0'); save_and_next(); } while (isdigit(current));
|
||||||
if (current == '.') save_and_next();
|
if (current == '.') save_and_next();
|
||||||
fraction: while (isdigit(current)) save_and_next();
|
fraction:
|
||||||
if (current == 'e' || current == 'E')
|
{ double da=0.1;
|
||||||
{
|
while (isdigit(current))
|
||||||
save_and_next();
|
{a+=(current-'0')*da; da/=10.0; save_and_next()};
|
||||||
if (current == '+' || current == '-') save_and_next();
|
if (current == 'e' || current == 'E')
|
||||||
if (!isdigit(current)) return WRONGTOKEN;
|
{
|
||||||
do { save_and_next(); } while (isdigit(current));
|
int e=0;
|
||||||
|
int neg;
|
||||||
|
double ea;
|
||||||
|
save_and_next();
|
||||||
|
neg=(current=='-');
|
||||||
|
if (current == '+' || current == '-') save_and_next();
|
||||||
|
if (!isdigit(current)) return WRONGTOKEN;
|
||||||
|
do { e=10.0*e+(current-'0'); save_and_next(); } while (isdigit(current));
|
||||||
|
for (ea=neg?0.1:10.0; e>0; e>>=1)
|
||||||
|
{
|
||||||
|
if (e & 1) a*=ea;
|
||||||
|
ea*=ea;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
luaY_lval.vFloat = a;
|
||||||
|
return NUMBER;
|
||||||
}
|
}
|
||||||
*yytextLast = 0;
|
|
||||||
yylval.vFloat = atof(yytext);
|
|
||||||
return NUMBER;
|
|
||||||
|
|
||||||
default: /* also end of file */
|
default: /* also end of file */
|
||||||
{
|
{
|
||||||
save_and_next();
|
save_and_next();
|
||||||
return *yytext;
|
return yytext[0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
19
lex.h
Normal file
19
lex.h
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
/*
|
||||||
|
** lex.h
|
||||||
|
** TecCGraf - PUC-Rio
|
||||||
|
** $Id: lex.h,v 1.1 1996/02/13 17:30:39 roberto Exp roberto $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef lex_h
|
||||||
|
#define lex_h
|
||||||
|
|
||||||
|
|
||||||
|
typedef int (*Input) (void);
|
||||||
|
|
||||||
|
void lua_setinput (Input fn);
|
||||||
|
char *lua_lasttext (void);
|
||||||
|
int luaY_lex (void);
|
||||||
|
void luaI_addReserved (void);
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
89
lua.c
89
lua.c
@@ -3,29 +3,90 @@
|
|||||||
** Linguagem para Usuarios de Aplicacao
|
** Linguagem para Usuarios de Aplicacao
|
||||||
*/
|
*/
|
||||||
|
|
||||||
char *rcs_lua="$Id: $";
|
char *rcs_lua="$Id: lua.c,v 1.7 1995/10/31 17:05:35 roberto Exp roberto $";
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
#include "lualib.h"
|
#include "lualib.h"
|
||||||
|
|
||||||
|
static int lua_argc;
|
||||||
|
static char **lua_argv;
|
||||||
|
|
||||||
void main (int argc, char *argv[])
|
/*
|
||||||
|
** although this function is POSIX, there is no standard header file that
|
||||||
|
** defines it
|
||||||
|
*/
|
||||||
|
int isatty (int fd);
|
||||||
|
|
||||||
|
/*
|
||||||
|
%F Allow Lua code to access argv strings.
|
||||||
|
%i Receive from Lua the argument number (starting with 1).
|
||||||
|
%o Return to Lua the argument, or nil if it does not exist.
|
||||||
|
*/
|
||||||
|
static void lua_getargv (void)
|
||||||
{
|
{
|
||||||
int i;
|
lua_Object lo = lua_getparam(1);
|
||||||
iolib_open ();
|
if (!lua_isnumber(lo))
|
||||||
strlib_open ();
|
lua_pushnil();
|
||||||
mathlib_open ();
|
|
||||||
if (argc < 2)
|
|
||||||
{
|
|
||||||
char buffer[250];
|
|
||||||
while (gets(buffer) != 0)
|
|
||||||
lua_dostring(buffer);
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
for (i=1; i<argc; i++)
|
{
|
||||||
lua_dofile (argv[i]);
|
int n = (int)lua_getnumber(lo);
|
||||||
|
if (n < 1 || n > lua_argc) lua_pushnil();
|
||||||
|
else lua_pushstring(lua_argv[n]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void manual_input (void)
|
||||||
|
{
|
||||||
|
if (isatty(fileno(stdin)))
|
||||||
|
{
|
||||||
|
char buffer[250];
|
||||||
|
while (fgets(buffer, sizeof(buffer), stdin) != 0)
|
||||||
|
lua_dostring(buffer);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
lua_dofile(NULL); /* executes stdin as a file */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main (int argc, char *argv[])
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int result = 0;
|
||||||
|
iolib_open ();
|
||||||
|
strlib_open ();
|
||||||
|
mathlib_open ();
|
||||||
|
|
||||||
|
lua_register("argv", lua_getargv);
|
||||||
|
|
||||||
|
if (argc < 2)
|
||||||
|
manual_input();
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (i=1; i<argc; i++)
|
||||||
|
if (strcmp(argv[i], "--") == 0)
|
||||||
|
{
|
||||||
|
lua_argc = argc-i-1;
|
||||||
|
lua_argv = argv+i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
for (i=1; i<argc; i++)
|
||||||
|
{
|
||||||
|
if (strcmp(argv[i], "--") == 0)
|
||||||
|
break;
|
||||||
|
else 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
|
||||||
|
result = lua_dofile (argv[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
105
lua.h
105
lua.h
@@ -2,53 +2,108 @@
|
|||||||
** LUA - Linguagem para Usuarios de Aplicacao
|
** LUA - Linguagem para Usuarios de Aplicacao
|
||||||
** Grupo de Tecnologia em Computacao Grafica
|
** Grupo de Tecnologia em Computacao Grafica
|
||||||
** TeCGraf - PUC-Rio
|
** TeCGraf - PUC-Rio
|
||||||
** $Id: $
|
** $Id: lua.h,v 3.24 1996/03/19 22:28:37 roberto Exp roberto $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#ifndef lua_h
|
#ifndef lua_h
|
||||||
#define lua_h
|
#define lua_h
|
||||||
|
|
||||||
|
#define LUA_VERSION "Lua 2.4 (beta)"
|
||||||
|
#define LUA_COPYRIGHT "Copyright (C) 1994, 1995 TeCGraf"
|
||||||
|
#define LUA_AUTHORS "W. Celes, R. Ierusalimschy & L. H. de Figueiredo"
|
||||||
|
|
||||||
|
|
||||||
|
/* Private Part */
|
||||||
|
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
LUA_T_NIL = -1,
|
||||||
|
LUA_T_NUMBER = -2,
|
||||||
|
LUA_T_STRING = -3,
|
||||||
|
LUA_T_ARRAY = -4,
|
||||||
|
LUA_T_FUNCTION = -5,
|
||||||
|
LUA_T_CFUNCTION= -6,
|
||||||
|
LUA_T_MARK = -7,
|
||||||
|
LUA_T_CMARK = -8,
|
||||||
|
LUA_T_LINE = -9,
|
||||||
|
LUA_T_USERDATA = 0
|
||||||
|
} lua_Type;
|
||||||
|
|
||||||
|
|
||||||
|
/* Public Part */
|
||||||
|
|
||||||
|
#define LUA_NOOBJECT 0
|
||||||
|
|
||||||
typedef void (*lua_CFunction) (void);
|
typedef void (*lua_CFunction) (void);
|
||||||
typedef struct Object *lua_Object;
|
typedef unsigned int lua_Object;
|
||||||
|
|
||||||
#define lua_register(n,f) (lua_pushcfunction(f), lua_storeglobal(n))
|
lua_Object lua_setfallback (char *name, lua_CFunction fallback);
|
||||||
|
|
||||||
|
|
||||||
void lua_errorfunction (void (*fn) (char *s));
|
|
||||||
void lua_error (char *s);
|
void lua_error (char *s);
|
||||||
int lua_dofile (char *filename);
|
int lua_dofile (char *filename);
|
||||||
int lua_dostring (char *string);
|
int lua_dostring (char *string);
|
||||||
int lua_call (char *functionname, int nparam);
|
int lua_callfunction (lua_Object function);
|
||||||
|
int lua_call (char *funcname);
|
||||||
|
|
||||||
|
void lua_beginblock (void);
|
||||||
|
void lua_endblock (void);
|
||||||
|
|
||||||
lua_Object lua_getparam (int number);
|
lua_Object lua_getparam (int number);
|
||||||
|
#define lua_getresult(_) lua_getparam(_)
|
||||||
|
|
||||||
|
#define lua_isnil(_) (lua_type(_)==LUA_T_NIL)
|
||||||
|
#define lua_istable(_) (lua_type(_)==LUA_T_ARRAY)
|
||||||
|
#define lua_isuserdata(_) (lua_type(_)>=LUA_T_USERDATA)
|
||||||
|
#define lua_iscfunction(_) (lua_type(_)==LUA_T_CFUNCTION)
|
||||||
|
int lua_isnumber (lua_Object object);
|
||||||
|
int lua_isstring (lua_Object object);
|
||||||
|
int lua_isfunction (lua_Object object);
|
||||||
|
|
||||||
float lua_getnumber (lua_Object object);
|
float lua_getnumber (lua_Object object);
|
||||||
char *lua_getstring (lua_Object object);
|
char *lua_getstring (lua_Object object);
|
||||||
char *lua_copystring (lua_Object object);
|
|
||||||
lua_CFunction lua_getcfunction (lua_Object object);
|
lua_CFunction lua_getcfunction (lua_Object object);
|
||||||
void *lua_getuserdata (lua_Object object);
|
void *lua_getuserdata (lua_Object object);
|
||||||
lua_Object lua_getfield (lua_Object object, char *field);
|
|
||||||
lua_Object lua_getindexed (lua_Object object, float index);
|
void lua_pushnil (void);
|
||||||
|
void lua_pushnumber (float n);
|
||||||
|
void lua_pushstring (char *s);
|
||||||
|
void lua_pushcfunction (lua_CFunction fn);
|
||||||
|
void lua_pushusertag (void *u, int tag);
|
||||||
|
void lua_pushobject (lua_Object object);
|
||||||
|
|
||||||
lua_Object lua_getglobal (char *name);
|
lua_Object lua_getglobal (char *name);
|
||||||
|
void lua_storeglobal (char *name);
|
||||||
|
|
||||||
lua_Object lua_pop (void);
|
void lua_storesubscript (void);
|
||||||
|
lua_Object lua_getsubscript (void);
|
||||||
|
|
||||||
int lua_pushnil (void);
|
int lua_type (lua_Object object);
|
||||||
int lua_pushnumber (float n);
|
|
||||||
int lua_pushstring (char *s);
|
|
||||||
int lua_pushcfunction (lua_CFunction fn);
|
|
||||||
int lua_pushuserdata (void *u);
|
|
||||||
int lua_pushobject (lua_Object object);
|
|
||||||
|
|
||||||
int lua_storeglobal (char *name);
|
int lua_lock (void);
|
||||||
int lua_storefield (lua_Object object, char *field);
|
lua_Object lua_getlocked (int ref);
|
||||||
int lua_storeindexed (lua_Object object, float index);
|
void lua_pushlocked (int ref);
|
||||||
|
void lua_unlock (int ref);
|
||||||
|
|
||||||
int lua_isnil (lua_Object object);
|
lua_Object lua_createtable (void);
|
||||||
int lua_isnumber (lua_Object object);
|
|
||||||
int lua_isstring (lua_Object object);
|
|
||||||
int lua_istable (lua_Object object);
|
/* some useful macros */
|
||||||
int lua_iscfunction (lua_Object object);
|
|
||||||
int lua_isuserdata (lua_Object object);
|
#define lua_lockobject(o) (lua_pushobject(o), lua_lock())
|
||||||
|
|
||||||
|
#define lua_register(n,f) (lua_pushcfunction(f), lua_storeglobal(n))
|
||||||
|
|
||||||
|
#define lua_pushuserdata(u) lua_pushusertag(u, LUA_T_USERDATA)
|
||||||
|
|
||||||
|
|
||||||
|
/* for compatibility with old versions. Avoid using these macros */
|
||||||
|
|
||||||
|
#define lua_pushliteral(o) lua_pushstring(o)
|
||||||
|
|
||||||
|
#define lua_getindexed(o,n) (lua_pushobject(o), lua_pushnumber(n), lua_getsubscript())
|
||||||
|
#define lua_getfield(o,f) (lua_pushobject(o), lua_pushliteral(f), lua_getsubscript())
|
||||||
|
|
||||||
|
#define lua_copystring(o) (strdup(lua_getstring(o)))
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
85
lua.lex
85
lua.lex
@@ -1,85 +0,0 @@
|
|||||||
%{
|
|
||||||
|
|
||||||
char *rcs_lualex = "$Id: $";
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "opcode.h"
|
|
||||||
#include "hash.h"
|
|
||||||
#include "inout.h"
|
|
||||||
#include "table.h"
|
|
||||||
#include "y.tab.h"
|
|
||||||
|
|
||||||
#undef input
|
|
||||||
#undef unput
|
|
||||||
|
|
||||||
static Input input;
|
|
||||||
static Unput unput;
|
|
||||||
|
|
||||||
void lua_setinput (Input fn)
|
|
||||||
{
|
|
||||||
input = fn;
|
|
||||||
}
|
|
||||||
|
|
||||||
void lua_setunput (Unput fn)
|
|
||||||
{
|
|
||||||
unput = fn;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *lua_lasttext (void)
|
|
||||||
{
|
|
||||||
return yytext;
|
|
||||||
}
|
|
||||||
|
|
||||||
%}
|
|
||||||
|
|
||||||
|
|
||||||
%%
|
|
||||||
[ \t]* ;
|
|
||||||
^"$debug" {yylval.vInt = 1; return DEBUG;}
|
|
||||||
^"$nodebug" {yylval.vInt = 0; return DEBUG;}
|
|
||||||
\n lua_linenumber++;
|
|
||||||
"--".* ;
|
|
||||||
"local" return LOCAL;
|
|
||||||
"if" return IF;
|
|
||||||
"then" return THEN;
|
|
||||||
"else" return ELSE;
|
|
||||||
"elseif" return ELSEIF;
|
|
||||||
"while" return WHILE;
|
|
||||||
"do" return DO;
|
|
||||||
"repeat" return REPEAT;
|
|
||||||
"until" return UNTIL;
|
|
||||||
"function" {
|
|
||||||
yylval.vWord = lua_nfile-1;
|
|
||||||
return FUNCTION;
|
|
||||||
}
|
|
||||||
"end" return END;
|
|
||||||
"return" return RETURN;
|
|
||||||
"local" return LOCAL;
|
|
||||||
"nil" return NIL;
|
|
||||||
"and" return AND;
|
|
||||||
"or" return OR;
|
|
||||||
"not" return NOT;
|
|
||||||
"~=" return NE;
|
|
||||||
"<=" return LE;
|
|
||||||
">=" return GE;
|
|
||||||
".." return CONC;
|
|
||||||
\"[^\"]*\" |
|
|
||||||
\'[^\']*\' {
|
|
||||||
yylval.vWord = lua_findenclosedconstant (yytext);
|
|
||||||
return STRING;
|
|
||||||
}
|
|
||||||
[0-9]+("."[0-9]*)? |
|
|
||||||
([0-9]+)?"."[0-9]+ |
|
|
||||||
[0-9]+("."[0-9]*)?[dDeEgG][+-]?[0-9]+ |
|
|
||||||
([0-9]+)?"."[0-9]+[dDeEgG][+-]?[0-9]+ {
|
|
||||||
yylval.vFloat = atof(yytext);
|
|
||||||
return NUMBER;
|
|
||||||
}
|
|
||||||
[a-zA-Z_][a-zA-Z0-9_]* {
|
|
||||||
yylval.vWord = lua_findsymbol (yytext);
|
|
||||||
return NAME;
|
|
||||||
}
|
|
||||||
. return *yytext;
|
|
||||||
|
|
||||||
31
luadebug.h
Normal file
31
luadebug.h
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
** 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 $
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef luadebug_h
|
||||||
|
#define luadebug_h
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
typedef lua_Object lua_Function;
|
||||||
|
|
||||||
|
typedef void (*lua_LHFunction) (int line);
|
||||||
|
typedef void (*lua_CHFunction) (lua_Function func, char *file, int line);
|
||||||
|
|
||||||
|
lua_Function lua_stackedfunction (int level);
|
||||||
|
void lua_funcinfo (lua_Object func, char **filename, int *linedefined);
|
||||||
|
int lua_currentline (lua_Function func);
|
||||||
|
char *lua_getobjname (lua_Object o, char **name);
|
||||||
|
|
||||||
|
lua_Object lua_getlocal (lua_Function func, int local_number, char **name);
|
||||||
|
int lua_setlocal (lua_Function func, int local_number);
|
||||||
|
|
||||||
|
extern lua_LHFunction lua_linehook;
|
||||||
|
extern lua_CHFunction lua_callhook;
|
||||||
|
extern int lua_debug;
|
||||||
|
|
||||||
|
#endif
|
||||||
10
lualib.h
10
lualib.h
@@ -2,7 +2,7 @@
|
|||||||
** Libraries to be used in LUA programs
|
** Libraries to be used in LUA programs
|
||||||
** Grupo de Tecnologia em Computacao Grafica
|
** Grupo de Tecnologia em Computacao Grafica
|
||||||
** TeCGraf - PUC-Rio
|
** TeCGraf - PUC-Rio
|
||||||
** $Id: $
|
** $Id: lualib.h,v 1.6 1996/02/09 19:00:23 roberto Exp roberto $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef lualib_h
|
#ifndef lualib_h
|
||||||
@@ -12,5 +12,13 @@ void iolib_open (void);
|
|||||||
void strlib_open (void);
|
void strlib_open (void);
|
||||||
void mathlib_open (void);
|
void mathlib_open (void);
|
||||||
|
|
||||||
|
|
||||||
|
/* auxiliar functions (private) */
|
||||||
|
void lua_arg_error(char *funcname);
|
||||||
|
char *lua_check_string (int numArg, char *funcname);
|
||||||
|
double lua_check_number (int numArg, char *funcname);
|
||||||
|
char *luaI_addchar (int c);
|
||||||
|
void luaI_addquoted (char *s);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
70
luamem.c
Normal file
70
luamem.c
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
/*
|
||||||
|
** mem.c
|
||||||
|
** TecCGraf - PUC-Rio
|
||||||
|
*/
|
||||||
|
|
||||||
|
char *rcs_mem = "$Id: mem.c,v 1.10 1996/03/21 16:31:32 roberto Exp roberto $";
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "mem.h"
|
||||||
|
#include "lua.h"
|
||||||
|
#include "table.h"
|
||||||
|
|
||||||
|
|
||||||
|
#define mem_error() lua_error(memEM)
|
||||||
|
|
||||||
|
|
||||||
|
void luaI_free (void *block)
|
||||||
|
{
|
||||||
|
if (block)
|
||||||
|
{
|
||||||
|
*((int *)block) = -1; /* to catch errors */
|
||||||
|
free(block);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void *luaI_malloc (unsigned long size)
|
||||||
|
{
|
||||||
|
void *block = malloc((size_t)size);
|
||||||
|
if (block == NULL)
|
||||||
|
mem_error();
|
||||||
|
return block;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void *luaI_realloc (void *oldblock, unsigned long size)
|
||||||
|
{
|
||||||
|
void *block = oldblock ? realloc(oldblock, (size_t)size) :
|
||||||
|
malloc((size_t)size);
|
||||||
|
if (block == NULL)
|
||||||
|
mem_error();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
39
luamem.h
Normal file
39
luamem.h
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
** mem.c
|
||||||
|
** memory manager for lua
|
||||||
|
** $Id: mem.h,v 1.5 1996/03/21 16:31:32 roberto Exp roberto $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef mem_h
|
||||||
|
#define mem_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 lockEM "lock table overflow"
|
||||||
|
#define tableEM "table overflow"
|
||||||
|
#define memEM "not enough memory"
|
||||||
|
|
||||||
|
|
||||||
|
void luaI_free (void *block);
|
||||||
|
void *luaI_malloc (unsigned long size);
|
||||||
|
void *luaI_realloc (void *oldblock, unsigned long size);
|
||||||
|
void *luaI_buffer (unsigned long size);
|
||||||
|
int luaI_growvector (void **block, unsigned long nelems, int size,
|
||||||
|
char *errormsg, unsigned long limit);
|
||||||
|
|
||||||
|
#define new(s) ((s *)luaI_malloc(sizeof(s)))
|
||||||
|
#define newvector(n,s) ((s *)luaI_malloc((n)*sizeof(s)))
|
||||||
|
#define growvector(old,n,s,e,l) \
|
||||||
|
(luaI_growvector((void**)old,n,sizeof(s),e,l))
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
88
makefile
88
makefile
@@ -1,34 +1,43 @@
|
|||||||
# $Id: makefile,v 1.5 1994/01/10 19:49:56 roberto Exp celes $
|
# $Id: makefile,v 1.21 1996/03/05 15:57:53 roberto Exp roberto $
|
||||||
|
|
||||||
|
#configuration
|
||||||
|
|
||||||
|
# define (undefine) POPEN if your system (does not) support piped I/O
|
||||||
|
CONFIG = -DPOPEN
|
||||||
# Compilation parameters
|
# Compilation parameters
|
||||||
CC = gcc
|
CC = gcc
|
||||||
CFLAGS = -I/usr/5include -Wall -O2
|
CFLAGS = $(CONFIG) -I/usr/5include -Wall -Wmissing-prototypes -Wshadow -ansi -O2
|
||||||
|
|
||||||
|
#CC = acc
|
||||||
|
#CFLAGS = -fast -I/usr/5include
|
||||||
|
|
||||||
AR = ar
|
AR = ar
|
||||||
ARFLAGS = rvl
|
ARFLAGS = rvl
|
||||||
|
|
||||||
# Aplication modules
|
# Aplication modules
|
||||||
LUAMOD = \
|
LUAOBJS = \
|
||||||
y.tab \
|
parser.o \
|
||||||
lex \
|
lex.o \
|
||||||
opcode \
|
opcode.o \
|
||||||
hash \
|
hash.o \
|
||||||
table \
|
table.o \
|
||||||
inout \
|
inout.o \
|
||||||
tree
|
tree.o \
|
||||||
|
fallback.o \
|
||||||
|
mem.o \
|
||||||
|
func.o \
|
||||||
|
undump.o
|
||||||
|
|
||||||
LIBMOD = \
|
LIBOBJS = \
|
||||||
iolib \
|
iolib.o \
|
||||||
strlib \
|
mathlib.o \
|
||||||
mathlib
|
strlib.o
|
||||||
|
|
||||||
LUAOBJS = $(LUAMOD:%=%.o)
|
|
||||||
|
|
||||||
LIBOBJS = $(LIBMOD:%=%.o)
|
|
||||||
|
|
||||||
lua : lua.o lua.a lualib.a
|
lua : lua.o lua.a lualib.a
|
||||||
$(CC) $(CFLAGS) -o $@ lua.c lua.a lualib.a -lm
|
$(CC) $(CFLAGS) -o $@ lua.o lua.a lualib.a -lm
|
||||||
|
|
||||||
lua.a : y.tab.c $(LUAOBJS)
|
lua.a : $(LUAOBJS)
|
||||||
$(AR) $(ARFLAGS) $@ $?
|
$(AR) $(ARFLAGS) $@ $?
|
||||||
ranlib lua.a
|
ranlib lua.a
|
||||||
|
|
||||||
@@ -36,18 +45,47 @@ lualib.a : $(LIBOBJS)
|
|||||||
$(AR) $(ARFLAGS) $@ $?
|
$(AR) $(ARFLAGS) $@ $?
|
||||||
ranlib $@
|
ranlib $@
|
||||||
|
|
||||||
.KEEP_STATE:
|
|
||||||
|
|
||||||
liblua.so.1.0 : lua.o
|
liblua.so.1.0 : lua.o
|
||||||
ld -o liblua.so.1.0 $(LUAOBJS)
|
ld -o liblua.so.1.0 $(LUAOBJS)
|
||||||
|
|
||||||
%.o : %.c
|
y.tab.c y.tab.h : lua.stx
|
||||||
$(CC) $(CFLAGS) -c -o $@ $<
|
yacc++ -d lua.stx
|
||||||
|
|
||||||
|
parser.c : y.tab.c
|
||||||
|
sed -e 's/yy/luaY_/g' y.tab.c > parser.c
|
||||||
|
|
||||||
|
parser.h : y.tab.h
|
||||||
|
sed -e 's/yy/luaY_/g' y.tab.h > parser.h
|
||||||
|
|
||||||
|
clear :
|
||||||
|
rcsclean
|
||||||
|
rm -f *.o
|
||||||
|
rm -f parser.c parser.h y.tab.c y.tab.h
|
||||||
|
co lua.h lualib.h luadebug.h
|
||||||
|
|
||||||
y.tab.c : lua.stx exscript
|
|
||||||
yacc -d lua.stx ; ex y.tab.c <exscript
|
|
||||||
|
|
||||||
% : RCS/%,v
|
% : RCS/%,v
|
||||||
co $@
|
co $@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
fallback.o : fallback.c mem.h fallback.h opcode.h lua.h types.h tree.h func.h
|
||||||
|
func.o : func.c luadebug.h lua.h table.h tree.h types.h opcode.h func.h mem.h
|
||||||
|
hash.o : hash.c mem.h opcode.h lua.h types.h tree.h func.h hash.h table.h
|
||||||
|
inout.o : inout.c lex.h opcode.h lua.h types.h tree.h func.h inout.h table.h \
|
||||||
|
mem.h
|
||||||
|
iolib.o : iolib.c lua.h luadebug.h lualib.h
|
||||||
|
lex.o : lex.c mem.h tree.h types.h table.h opcode.h lua.h func.h lex.h inout.h \
|
||||||
|
luadebug.h parser.h
|
||||||
|
lua.o : lua.c lua.h lualib.h
|
||||||
|
mathlib.o : mathlib.c lualib.h lua.h
|
||||||
|
mem.o : mem.c mem.h lua.h table.h tree.h types.h opcode.h func.h
|
||||||
|
opcode.o : opcode.c luadebug.h lua.h mem.h opcode.h types.h tree.h func.h hash.h \
|
||||||
|
inout.h table.h fallback.h undump.h
|
||||||
|
parser.o : parser.c luadebug.h lua.h mem.h lex.h opcode.h types.h tree.h func.h \
|
||||||
|
hash.h inout.h table.h
|
||||||
|
strlib.o : strlib.c lua.h lualib.h
|
||||||
|
table.o : table.c mem.h opcode.h lua.h types.h tree.h func.h hash.h table.h \
|
||||||
|
inout.h fallback.h luadebug.h
|
||||||
|
tree.o : tree.c mem.h lua.h tree.h types.h lex.h hash.h opcode.h func.h table.h
|
||||||
|
undump.o : undump.c opcode.h lua.h types.h tree.h func.h mem.h table.h undump.h
|
||||||
|
|||||||
2153
manual.tex
Normal file
2153
manual.tex
Normal file
File diff suppressed because it is too large
Load Diff
216
mathlib.c
216
mathlib.c
@@ -3,25 +3,23 @@
|
|||||||
** Mathematics library to LUA
|
** Mathematics library to LUA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
char *rcs_mathlib="$Id: $";
|
char *rcs_mathlib="$Id: mathlib.c,v 1.13 1995/11/10 17:54:31 roberto Exp roberto $";
|
||||||
|
|
||||||
#include <stdio.h> /* NULL */
|
#include <stdlib.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
|
#include "lualib.h"
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#define TODEGREE(a) ((a)*180.0/3.14159)
|
#ifndef PI
|
||||||
#define TORAD(a) ((a)*3.14159/180.0)
|
#define PI 3.14159265358979323846
|
||||||
|
#endif
|
||||||
|
#define TODEGREE(a) ((a)*180.0/PI)
|
||||||
|
#define TORAD(a) ((a)*PI/180.0)
|
||||||
|
|
||||||
static void math_abs (void)
|
static void math_abs (void)
|
||||||
{
|
{
|
||||||
double d;
|
double d = lua_check_number(1, "abs");
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == NULL)
|
|
||||||
{ lua_error ("too few arguments to function `abs'"); return; }
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
{ lua_error ("incorrect arguments to function `abs'"); return; }
|
|
||||||
d = lua_getnumber(o);
|
|
||||||
if (d < 0) d = -d;
|
if (d < 0) d = -d;
|
||||||
lua_pushnumber (d);
|
lua_pushnumber (d);
|
||||||
}
|
}
|
||||||
@@ -29,13 +27,7 @@ static void math_abs (void)
|
|||||||
|
|
||||||
static void math_sin (void)
|
static void math_sin (void)
|
||||||
{
|
{
|
||||||
double d;
|
double d = lua_check_number(1, "sin");
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == NULL)
|
|
||||||
{ lua_error ("too few arguments to function `sin'"); return; }
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
{ lua_error ("incorrect arguments to function `sin'"); return; }
|
|
||||||
d = lua_getnumber(o);
|
|
||||||
lua_pushnumber (sin(TORAD(d)));
|
lua_pushnumber (sin(TORAD(d)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,13 +35,7 @@ static void math_sin (void)
|
|||||||
|
|
||||||
static void math_cos (void)
|
static void math_cos (void)
|
||||||
{
|
{
|
||||||
double d;
|
double d = lua_check_number(1, "cos");
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == NULL)
|
|
||||||
{ lua_error ("too few arguments to function `cos'"); return; }
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
{ lua_error ("incorrect arguments to function `cos'"); return; }
|
|
||||||
d = lua_getnumber(o);
|
|
||||||
lua_pushnumber (cos(TORAD(d)));
|
lua_pushnumber (cos(TORAD(d)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,160 +43,157 @@ static void math_cos (void)
|
|||||||
|
|
||||||
static void math_tan (void)
|
static void math_tan (void)
|
||||||
{
|
{
|
||||||
double d;
|
double d = lua_check_number(1, "tan");
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == NULL)
|
|
||||||
{ lua_error ("too few arguments to function `tan'"); return; }
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
{ lua_error ("incorrect arguments to function `tan'"); return; }
|
|
||||||
d = lua_getnumber(o);
|
|
||||||
lua_pushnumber (tan(TORAD(d)));
|
lua_pushnumber (tan(TORAD(d)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void math_asin (void)
|
static void math_asin (void)
|
||||||
{
|
{
|
||||||
double d;
|
double d = lua_check_number(1, "asin");
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == NULL)
|
|
||||||
{ lua_error ("too few arguments to function `asin'"); return; }
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
{ lua_error ("incorrect arguments to function `asin'"); return; }
|
|
||||||
d = lua_getnumber(o);
|
|
||||||
lua_pushnumber (TODEGREE(asin(d)));
|
lua_pushnumber (TODEGREE(asin(d)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void math_acos (void)
|
static void math_acos (void)
|
||||||
{
|
{
|
||||||
double d;
|
double d = lua_check_number(1, "acos");
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == NULL)
|
|
||||||
{ lua_error ("too few arguments to function `acos'"); return; }
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
{ lua_error ("incorrect arguments to function `acos'"); return; }
|
|
||||||
d = lua_getnumber(o);
|
|
||||||
lua_pushnumber (TODEGREE(acos(d)));
|
lua_pushnumber (TODEGREE(acos(d)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static void math_atan (void)
|
static void math_atan (void)
|
||||||
{
|
{
|
||||||
double d;
|
double d = lua_check_number(1, "atan");
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == NULL)
|
|
||||||
{ lua_error ("too few arguments to function `atan'"); return; }
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
{ lua_error ("incorrect arguments to function `atan'"); return; }
|
|
||||||
d = lua_getnumber(o);
|
|
||||||
lua_pushnumber (TODEGREE(atan(d)));
|
lua_pushnumber (TODEGREE(atan(d)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void math_atan2 (void)
|
||||||
|
{
|
||||||
|
double d1 = lua_check_number(1, "atan2");
|
||||||
|
double d2 = lua_check_number(2, "atan2");
|
||||||
|
lua_pushnumber (TODEGREE(atan2(d1, d2)));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static void math_ceil (void)
|
static void math_ceil (void)
|
||||||
{
|
{
|
||||||
double d;
|
double d = lua_check_number(1, "ceil");
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == NULL)
|
|
||||||
{ lua_error ("too few arguments to function `ceil'"); return; }
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
{ lua_error ("incorrect arguments to function `ceil'"); return; }
|
|
||||||
d = lua_getnumber(o);
|
|
||||||
lua_pushnumber (ceil(d));
|
lua_pushnumber (ceil(d));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void math_floor (void)
|
static void math_floor (void)
|
||||||
{
|
{
|
||||||
double d;
|
double d = lua_check_number(1, "floor");
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == NULL)
|
|
||||||
{ lua_error ("too few arguments to function `floor'"); return; }
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
{ lua_error ("incorrect arguments to function `floor'"); return; }
|
|
||||||
d = lua_getnumber(o);
|
|
||||||
lua_pushnumber (floor(d));
|
lua_pushnumber (floor(d));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void math_mod (void)
|
static void math_mod (void)
|
||||||
{
|
{
|
||||||
int d1, d2;
|
int d1 = (int)lua_check_number(1, "mod");
|
||||||
lua_Object o1 = lua_getparam (1);
|
int d2 = (int)lua_check_number(2, "mod");
|
||||||
lua_Object o2 = lua_getparam (2);
|
|
||||||
if (!lua_isnumber(o1) || !lua_isnumber(o2))
|
|
||||||
{ lua_error ("incorrect arguments to function `mod'"); return; }
|
|
||||||
d1 = (int) lua_getnumber(o1);
|
|
||||||
d2 = (int) lua_getnumber(o2);
|
|
||||||
lua_pushnumber (d1%d2);
|
lua_pushnumber (d1%d2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void math_sqrt (void)
|
static void math_sqrt (void)
|
||||||
{
|
{
|
||||||
double d;
|
double d = lua_check_number(1, "sqrt");
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == NULL)
|
|
||||||
{ lua_error ("too few arguments to function `sqrt'"); return; }
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
{ lua_error ("incorrect arguments to function `sqrt'"); return; }
|
|
||||||
d = lua_getnumber(o);
|
|
||||||
lua_pushnumber (sqrt(d));
|
lua_pushnumber (sqrt(d));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int old_pow;
|
||||||
|
|
||||||
static void math_pow (void)
|
static void math_pow (void)
|
||||||
{
|
{
|
||||||
double d1, d2;
|
|
||||||
lua_Object o1 = lua_getparam (1);
|
lua_Object o1 = lua_getparam (1);
|
||||||
lua_Object o2 = lua_getparam (2);
|
lua_Object o2 = lua_getparam (2);
|
||||||
if (!lua_isnumber(o1) || !lua_isnumber(o2))
|
lua_Object op = lua_getparam(3);
|
||||||
{ lua_error ("incorrect arguments to function `pow'"); return; }
|
if (!lua_isnumber(o1) || !lua_isnumber(o2) || *(lua_getstring(op)) != 'p')
|
||||||
d1 = lua_getnumber(o1);
|
{
|
||||||
d2 = lua_getnumber(o2);
|
lua_Object old = lua_getlocked(old_pow);
|
||||||
lua_pushnumber (pow(d1,d2));
|
lua_pushobject(o1);
|
||||||
|
lua_pushobject(o2);
|
||||||
|
lua_pushobject(op);
|
||||||
|
if (lua_callfunction(old) != 0)
|
||||||
|
lua_error(NULL);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
double d1 = lua_getnumber(o1);
|
||||||
|
double d2 = lua_getnumber(o2);
|
||||||
|
lua_pushnumber (pow(d1,d2));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void math_min (void)
|
static void math_min (void)
|
||||||
{
|
{
|
||||||
int i=1;
|
int i=1;
|
||||||
double d, dmin;
|
double dmin = lua_check_number(i, "min");
|
||||||
lua_Object o;
|
while (lua_getparam(++i) != LUA_NOOBJECT)
|
||||||
if ((o = lua_getparam(i++)) == NULL)
|
|
||||||
{ lua_error ("too few arguments to function `min'"); return; }
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
{ lua_error ("incorrect arguments to function `min'"); return; }
|
|
||||||
dmin = lua_getnumber (o);
|
|
||||||
while ((o = lua_getparam(i++)) != NULL)
|
|
||||||
{
|
{
|
||||||
if (!lua_isnumber(o))
|
double d = lua_check_number(i, "min");
|
||||||
{ lua_error ("incorrect arguments to function `min'"); return; }
|
|
||||||
d = lua_getnumber (o);
|
|
||||||
if (d < dmin) dmin = d;
|
if (d < dmin) dmin = d;
|
||||||
}
|
}
|
||||||
lua_pushnumber (dmin);
|
lua_pushnumber (dmin);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void math_max (void)
|
static void math_max (void)
|
||||||
{
|
{
|
||||||
int i=1;
|
int i=1;
|
||||||
double d, dmax;
|
double dmax = lua_check_number(i, "max");
|
||||||
lua_Object o;
|
while (lua_getparam(++i) != LUA_NOOBJECT)
|
||||||
if ((o = lua_getparam(i++)) == NULL)
|
|
||||||
{ lua_error ("too few arguments to function `max'"); return; }
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
{ lua_error ("incorrect arguments to function `max'"); return; }
|
|
||||||
dmax = lua_getnumber (o);
|
|
||||||
while ((o = lua_getparam(i++)) != NULL)
|
|
||||||
{
|
{
|
||||||
if (!lua_isnumber(o))
|
double d = lua_check_number(i, "max");
|
||||||
{ lua_error ("incorrect arguments to function `max'"); return; }
|
|
||||||
d = lua_getnumber (o);
|
|
||||||
if (d > dmax) dmax = d;
|
if (d > dmax) dmax = d;
|
||||||
}
|
}
|
||||||
lua_pushnumber (dmax);
|
lua_pushnumber (dmax);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void math_log (void)
|
||||||
|
{
|
||||||
|
double d = lua_check_number(1, "log");
|
||||||
|
lua_pushnumber (log(d));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void math_log10 (void)
|
||||||
|
{
|
||||||
|
double d = lua_check_number(1, "log10");
|
||||||
|
lua_pushnumber (log10(d));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void math_exp (void)
|
||||||
|
{
|
||||||
|
double d = lua_check_number(1, "exp");
|
||||||
|
lua_pushnumber (exp(d));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void math_deg (void)
|
||||||
|
{
|
||||||
|
float d = lua_check_number(1, "deg");
|
||||||
|
lua_pushnumber (d*180./PI);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void math_rad (void)
|
||||||
|
{
|
||||||
|
float d = lua_check_number(1, "rad");
|
||||||
|
lua_pushnumber (d/180.*PI);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void math_random (void)
|
||||||
|
{
|
||||||
|
lua_pushnumber((double)(rand()%RAND_MAX) / (double)RAND_MAX);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void math_randomseed (void)
|
||||||
|
{
|
||||||
|
srand(lua_check_number(1, "randomseed"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -225,11 +208,20 @@ void mathlib_open (void)
|
|||||||
lua_register ("asin", math_asin);
|
lua_register ("asin", math_asin);
|
||||||
lua_register ("acos", math_acos);
|
lua_register ("acos", math_acos);
|
||||||
lua_register ("atan", math_atan);
|
lua_register ("atan", math_atan);
|
||||||
|
lua_register ("atan2", math_atan2);
|
||||||
lua_register ("ceil", math_ceil);
|
lua_register ("ceil", math_ceil);
|
||||||
lua_register ("floor", math_floor);
|
lua_register ("floor", math_floor);
|
||||||
lua_register ("mod", math_mod);
|
lua_register ("mod", math_mod);
|
||||||
lua_register ("sqrt", math_sqrt);
|
lua_register ("sqrt", math_sqrt);
|
||||||
lua_register ("pow", math_pow);
|
|
||||||
lua_register ("min", math_min);
|
lua_register ("min", math_min);
|
||||||
lua_register ("max", math_max);
|
lua_register ("max", math_max);
|
||||||
|
lua_register ("log", math_log);
|
||||||
|
lua_register ("log10", math_log10);
|
||||||
|
lua_register ("exp", math_exp);
|
||||||
|
lua_register ("deg", math_deg);
|
||||||
|
lua_register ("rad", math_rad);
|
||||||
|
lua_register ("random", math_random);
|
||||||
|
lua_register ("randomseed", math_randomseed);
|
||||||
|
|
||||||
|
old_pow = lua_lockobject(lua_setfallback("arith", math_pow));
|
||||||
}
|
}
|
||||||
|
|||||||
39
mm.h
39
mm.h
@@ -1,39 +0,0 @@
|
|||||||
/*
|
|
||||||
** mm.h
|
|
||||||
** Waldemar Celes Filho
|
|
||||||
** Sep 16, 1992
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
#ifndef mm_h
|
|
||||||
#define mm_h
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
#ifdef _MM_
|
|
||||||
|
|
||||||
/* switch off the debugger functions */
|
|
||||||
#define malloc(s) MmMalloc(s,__FILE__,__LINE__)
|
|
||||||
#define calloc(n,s) MmCalloc(n,s,__FILE__,__LINE__)
|
|
||||||
#define realloc(a,s) MmRealloc(a,s,__FILE__,__LINE__,#a)
|
|
||||||
#define free(a) MmFree(a,__FILE__,__LINE__,#a)
|
|
||||||
#define strdup(s) MmStrdup(s,__FILE__,__LINE__)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
typedef void (*Ferror) (char *);
|
|
||||||
|
|
||||||
/* Exported functions */
|
|
||||||
void MmInit (Ferror f, Ferror w);
|
|
||||||
void *MmMalloc (unsigned size, char *file, int line);
|
|
||||||
void *MmCalloc (unsigned n, unsigned size, char *file, int line);
|
|
||||||
void MmFree (void *a, char *file, int line, char *var);
|
|
||||||
void *MmRealloc (void *old, unsigned size, char *file, int line, char *var);
|
|
||||||
char *MmStrdup (char *s, char *file, int line);
|
|
||||||
unsigned MmGetBytes (void);
|
|
||||||
void MmListAllocated (void);
|
|
||||||
void MmCheck (void);
|
|
||||||
void MmStatistics (void);
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
117
opcode.h
117
opcode.h
@@ -1,38 +1,19 @@
|
|||||||
/*
|
/*
|
||||||
** TeCGraf - PUC-Rio
|
** TeCGraf - PUC-Rio
|
||||||
** $Id: opcode.h,v 2.1 1994/04/20 22:07:57 celes Exp celes $
|
** $Id: opcode.h,v 3.19 1996/03/06 13:11:23 roberto Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef opcode_h
|
#ifndef opcode_h
|
||||||
#define opcode_h
|
#define opcode_h
|
||||||
|
|
||||||
#ifndef STACKGAP
|
#include "lua.h"
|
||||||
#define STACKGAP 128
|
#include "types.h"
|
||||||
#endif
|
#include "tree.h"
|
||||||
|
#include "func.h"
|
||||||
|
|
||||||
#ifndef real
|
|
||||||
#define real float
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define FIELDS_PER_FLUSH 40
|
#define FIELDS_PER_FLUSH 40
|
||||||
|
|
||||||
typedef unsigned char Byte;
|
|
||||||
|
|
||||||
typedef unsigned short Word;
|
|
||||||
|
|
||||||
typedef signed long Long;
|
|
||||||
|
|
||||||
typedef union
|
|
||||||
{
|
|
||||||
struct {char c1; char c2;} m;
|
|
||||||
Word w;
|
|
||||||
} CodeWord;
|
|
||||||
|
|
||||||
typedef union
|
|
||||||
{
|
|
||||||
struct {char c1; char c2; char c3; char c4;} m;
|
|
||||||
float f;
|
|
||||||
} CodeFloat;
|
|
||||||
|
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
@@ -42,13 +23,13 @@ typedef enum
|
|||||||
PUSHWORD,
|
PUSHWORD,
|
||||||
PUSHFLOAT,
|
PUSHFLOAT,
|
||||||
PUSHSTRING,
|
PUSHSTRING,
|
||||||
|
PUSHFUNCTION,
|
||||||
PUSHLOCAL0, PUSHLOCAL1, PUSHLOCAL2, PUSHLOCAL3, PUSHLOCAL4,
|
PUSHLOCAL0, PUSHLOCAL1, PUSHLOCAL2, PUSHLOCAL3, PUSHLOCAL4,
|
||||||
PUSHLOCAL5, PUSHLOCAL6, PUSHLOCAL7, PUSHLOCAL8, PUSHLOCAL9,
|
PUSHLOCAL5, PUSHLOCAL6, PUSHLOCAL7, PUSHLOCAL8, PUSHLOCAL9,
|
||||||
PUSHLOCAL,
|
PUSHLOCAL,
|
||||||
PUSHGLOBAL,
|
PUSHGLOBAL,
|
||||||
PUSHINDEXED,
|
PUSHINDEXED,
|
||||||
PUSHMARK,
|
PUSHSELF,
|
||||||
PUSHOBJECT,
|
|
||||||
STORELOCAL0, STORELOCAL1, STORELOCAL2, STORELOCAL3, STORELOCAL4,
|
STORELOCAL0, STORELOCAL1, STORELOCAL2, STORELOCAL3, STORELOCAL4,
|
||||||
STORELOCAL5, STORELOCAL6, STORELOCAL7, STORELOCAL8, STORELOCAL9,
|
STORELOCAL5, STORELOCAL6, STORELOCAL7, STORELOCAL8, STORELOCAL9,
|
||||||
STORELOCAL,
|
STORELOCAL,
|
||||||
@@ -58,15 +39,19 @@ typedef enum
|
|||||||
STORELIST0,
|
STORELIST0,
|
||||||
STORELIST,
|
STORELIST,
|
||||||
STORERECORD,
|
STORERECORD,
|
||||||
|
ADJUST0,
|
||||||
ADJUST,
|
ADJUST,
|
||||||
CREATEARRAY,
|
CREATEARRAY,
|
||||||
EQOP,
|
EQOP,
|
||||||
LTOP,
|
LTOP,
|
||||||
LEOP,
|
LEOP,
|
||||||
|
GTOP,
|
||||||
|
GEOP,
|
||||||
ADDOP,
|
ADDOP,
|
||||||
SUBOP,
|
SUBOP,
|
||||||
MULTOP,
|
MULTOP,
|
||||||
DIVOP,
|
DIVOP,
|
||||||
|
POWOP,
|
||||||
CONCOP,
|
CONCOP,
|
||||||
MINUSOP,
|
MINUSOP,
|
||||||
NOTOP,
|
NOTOP,
|
||||||
@@ -78,54 +63,37 @@ typedef enum
|
|||||||
IFFUPJMP,
|
IFFUPJMP,
|
||||||
POP,
|
POP,
|
||||||
CALLFUNC,
|
CALLFUNC,
|
||||||
|
RETCODE0,
|
||||||
RETCODE,
|
RETCODE,
|
||||||
HALT,
|
SETLINE
|
||||||
SETFUNCTION,
|
|
||||||
SETLINE,
|
|
||||||
RESET
|
|
||||||
} OpCode;
|
} OpCode;
|
||||||
|
|
||||||
typedef enum
|
#define MULT_RET 255
|
||||||
{
|
|
||||||
T_MARK,
|
|
||||||
T_NIL,
|
|
||||||
T_NUMBER,
|
|
||||||
T_STRING,
|
|
||||||
T_ARRAY,
|
|
||||||
T_FUNCTION,
|
|
||||||
T_CFUNCTION,
|
|
||||||
T_USERDATA
|
|
||||||
} Type;
|
|
||||||
|
|
||||||
typedef void (*Cfunction) (void);
|
|
||||||
typedef int (*Input) (void);
|
|
||||||
|
|
||||||
typedef union
|
typedef union
|
||||||
{
|
{
|
||||||
Cfunction f;
|
lua_CFunction f;
|
||||||
real n;
|
real n;
|
||||||
char *s;
|
TaggedString *ts;
|
||||||
Byte *b;
|
TFunc *tf;
|
||||||
struct Hash *a;
|
struct Hash *a;
|
||||||
void *u;
|
void *u;
|
||||||
|
int i;
|
||||||
} Value;
|
} Value;
|
||||||
|
|
||||||
typedef struct Object
|
typedef struct Object
|
||||||
{
|
{
|
||||||
Type tag;
|
lua_Type tag;
|
||||||
Value value;
|
Value value;
|
||||||
} Object;
|
} Object;
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
Object object;
|
|
||||||
} Symbol;
|
|
||||||
|
|
||||||
/* Macros to access structure members */
|
/* Macros to access structure members */
|
||||||
#define tag(o) ((o)->tag)
|
#define tag(o) ((o)->tag)
|
||||||
#define nvalue(o) ((o)->value.n)
|
#define nvalue(o) ((o)->value.n)
|
||||||
#define svalue(o) ((o)->value.s)
|
#define svalue(o) ((o)->value.ts->str)
|
||||||
#define bvalue(o) ((o)->value.b)
|
#define tsvalue(o) ((o)->value.ts)
|
||||||
#define avalue(o) ((o)->value.a)
|
#define avalue(o) ((o)->value.a)
|
||||||
#define fvalue(o) ((o)->value.f)
|
#define fvalue(o) ((o)->value.f)
|
||||||
#define uvalue(o) ((o)->value.u)
|
#define uvalue(o) ((o)->value.u)
|
||||||
@@ -135,30 +103,41 @@ typedef struct
|
|||||||
#define s_tag(i) (tag(&s_object(i)))
|
#define s_tag(i) (tag(&s_object(i)))
|
||||||
#define s_nvalue(i) (nvalue(&s_object(i)))
|
#define s_nvalue(i) (nvalue(&s_object(i)))
|
||||||
#define s_svalue(i) (svalue(&s_object(i)))
|
#define s_svalue(i) (svalue(&s_object(i)))
|
||||||
#define s_bvalue(i) (bvalue(&s_object(i)))
|
|
||||||
#define s_avalue(i) (avalue(&s_object(i)))
|
#define s_avalue(i) (avalue(&s_object(i)))
|
||||||
#define s_fvalue(i) (fvalue(&s_object(i)))
|
#define s_fvalue(i) (fvalue(&s_object(i)))
|
||||||
#define s_uvalue(i) (uvalue(&s_object(i)))
|
#define s_uvalue(i) (uvalue(&s_object(i)))
|
||||||
|
|
||||||
|
typedef union
|
||||||
|
{
|
||||||
|
struct {Byte c1; Byte c2;} m;
|
||||||
|
Word w;
|
||||||
|
} CodeWord;
|
||||||
#define get_word(code,pc) {code.m.c1 = *pc++; code.m.c2 = *pc++;}
|
#define get_word(code,pc) {code.m.c1 = *pc++; code.m.c2 = *pc++;}
|
||||||
|
|
||||||
|
typedef union
|
||||||
|
{
|
||||||
|
struct {Byte c1; Byte c2; Byte c3; Byte c4;} m;
|
||||||
|
float f;
|
||||||
|
} CodeFloat;
|
||||||
#define get_float(code,pc) {code.m.c1 = *pc++; code.m.c2 = *pc++;\
|
#define get_float(code,pc) {code.m.c1 = *pc++; code.m.c2 = *pc++;\
|
||||||
code.m.c3 = *pc++; code.m.c4 = *pc++;}
|
code.m.c3 = *pc++; code.m.c4 = *pc++;}
|
||||||
|
|
||||||
|
typedef union
|
||||||
|
{
|
||||||
|
struct {Byte c1; Byte c2; Byte c3; Byte c4;} m;
|
||||||
|
TFunc *tf;
|
||||||
|
} CodeCode;
|
||||||
|
#define get_code(code,pc) {code.m.c1 = *pc++; code.m.c2 = *pc++;\
|
||||||
|
code.m.c3 = *pc++; code.m.c4 = *pc++;}
|
||||||
|
|
||||||
|
|
||||||
/* Exported functions */
|
/* Exported functions */
|
||||||
int lua_execute (Byte *pc);
|
void lua_parse (TFunc *tf); /* from "lua.stx" module */
|
||||||
void lua_markstack (void);
|
void luaI_codedebugline (int line); /* from "lua.stx" module */
|
||||||
char *lua_strdup (char *l);
|
void lua_travstack (int (*fn)(Object *));
|
||||||
|
Object *luaI_Address (lua_Object o);
|
||||||
void lua_setinput (Input fn); /* from "lex.c" module */
|
void luaI_pushobject (Object *o);
|
||||||
char *lua_lasttext (void); /* from "lex.c" module */
|
void luaI_gcFB (Object *o);
|
||||||
int lua_parse (void); /* from "lua.stx" module */
|
int luaI_dorun (TFunc *tf);
|
||||||
void lua_type (void);
|
|
||||||
void lua_obj2number (void);
|
|
||||||
void lua_print (void);
|
|
||||||
void lua_internaldofile (void);
|
|
||||||
void lua_internaldostring (void);
|
|
||||||
void lua_travstack (void (*fn)(Object *));
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
251
strlib.c
251
strlib.c
@@ -3,34 +3,93 @@
|
|||||||
** String library to LUA
|
** String library to LUA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
char *rcs_strlib="$Id: strlib.c,v 1.1 1993/12/17 18:41:19 celes Exp celes $";
|
char *rcs_strlib="$Id: strlib.c,v 1.21 1996/03/21 22:18:08 roberto Exp roberto $";
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <limits.h>
|
||||||
#include "mm.h"
|
|
||||||
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
#include "lualib.h"
|
||||||
|
|
||||||
|
|
||||||
|
void lua_arg_error(char *funcname)
|
||||||
|
{
|
||||||
|
char buff[100];
|
||||||
|
sprintf(buff, "incorrect arguments to function `%s'", funcname);
|
||||||
|
lua_error(buff);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *lua_check_string (int numArg, char *funcname)
|
||||||
|
{
|
||||||
|
lua_Object o = lua_getparam(numArg);
|
||||||
|
if (!lua_isstring(o))
|
||||||
|
lua_arg_error(funcname);
|
||||||
|
return lua_getstring(o);
|
||||||
|
}
|
||||||
|
|
||||||
|
double lua_check_number (int numArg, char *funcname)
|
||||||
|
{
|
||||||
|
lua_Object o = lua_getparam(numArg);
|
||||||
|
if (!lua_isnumber(o))
|
||||||
|
lua_arg_error(funcname);
|
||||||
|
return lua_getnumber(o);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int lua_opt_number (int numArg, int def, char *funcname)
|
||||||
|
{
|
||||||
|
return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
|
||||||
|
(int)lua_check_number(numArg, funcname);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *luaI_addchar (int c)
|
||||||
|
{
|
||||||
|
static char *buff = NULL;
|
||||||
|
static int max = 0;
|
||||||
|
static int n = 0;
|
||||||
|
if (n >= max)
|
||||||
|
{
|
||||||
|
if (max == 0)
|
||||||
|
{
|
||||||
|
max = 100;
|
||||||
|
buff = (char *)malloc(max);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
max *= 2;
|
||||||
|
buff = (char *)realloc(buff, max);
|
||||||
|
}
|
||||||
|
if (buff == NULL)
|
||||||
|
lua_error("memory overflow");
|
||||||
|
}
|
||||||
|
buff[n++] = c;
|
||||||
|
if (c == 0)
|
||||||
|
n = 0; /* prepare for next string */
|
||||||
|
return buff;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Return the position of the first caracter of a substring into a string
|
** Return the position of the first caracter of a substring into a string
|
||||||
** LUA interface:
|
** LUA interface:
|
||||||
** n = strfind (string, substring)
|
** n = strfind (string, substring, init, end)
|
||||||
*/
|
*/
|
||||||
static void str_find (void)
|
static void str_find (void)
|
||||||
{
|
{
|
||||||
char *s1, *s2, *f;
|
char *s1 = lua_check_string(1, "strfind");
|
||||||
lua_Object o1 = lua_getparam (1);
|
char *s2 = lua_check_string(2, "strfind");
|
||||||
lua_Object o2 = lua_getparam (2);
|
int init = lua_opt_number(3, 1, "strfind") - 1;
|
||||||
if (!lua_isstring(o1) || !lua_isstring(o2))
|
char *f = (init>=0 && init<=strlen(s1)) ? strstr(s1+init,s2) : NULL;
|
||||||
{ lua_error ("incorrect arguments to function `strfind'"); return; }
|
|
||||||
s1 = lua_getstring(o1);
|
|
||||||
s2 = lua_getstring(o2);
|
|
||||||
f = strstr(s1,s2);
|
|
||||||
if (f != NULL)
|
if (f != NULL)
|
||||||
lua_pushnumber (f-s1+1);
|
{
|
||||||
|
int pos = f-s1+1;
|
||||||
|
if (lua_opt_number(4, INT_MAX, "strfind") >= pos+strlen(s2)-1)
|
||||||
|
lua_pushnumber (pos);
|
||||||
|
else
|
||||||
|
lua_pushnil();
|
||||||
|
}
|
||||||
else
|
else
|
||||||
lua_pushnil();
|
lua_pushnil();
|
||||||
}
|
}
|
||||||
@@ -42,10 +101,8 @@ static void str_find (void)
|
|||||||
*/
|
*/
|
||||||
static void str_len (void)
|
static void str_len (void)
|
||||||
{
|
{
|
||||||
lua_Object o = lua_getparam (1);
|
char *s = lua_check_string(1, "strlen");
|
||||||
if (!lua_isstring(o))
|
lua_pushnumber(strlen(s));
|
||||||
{ lua_error ("incorrect arguments to function `strlen'"); return; }
|
|
||||||
lua_pushnumber(strlen(lua_getstring(o)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -56,26 +113,18 @@ static void str_len (void)
|
|||||||
*/
|
*/
|
||||||
static void str_sub (void)
|
static void str_sub (void)
|
||||||
{
|
{
|
||||||
int start, end;
|
char *s = lua_check_string(1, "strsub");
|
||||||
char *s;
|
int start = (int)lua_check_number(2, "strsub");
|
||||||
lua_Object o1 = lua_getparam (1);
|
int end = lua_opt_number(3, strlen(s), "strsub");
|
||||||
lua_Object o2 = lua_getparam (2);
|
|
||||||
lua_Object o3 = lua_getparam (3);
|
|
||||||
if (!lua_isstring(o1) || !lua_isnumber(o2))
|
|
||||||
{ lua_error ("incorrect arguments to function `strsub'"); return; }
|
|
||||||
if (o3 != NULL && !lua_isnumber(o3))
|
|
||||||
{ lua_error ("incorrect third argument to function `strsub'"); return; }
|
|
||||||
s = lua_copystring(o1);
|
|
||||||
start = lua_getnumber (o2);
|
|
||||||
end = o3 == NULL ? strlen(s) : lua_getnumber (o3);
|
|
||||||
if (end < start || start < 1 || end > strlen(s))
|
if (end < start || start < 1 || end > strlen(s))
|
||||||
lua_pushstring("");
|
lua_pushliteral("");
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
s[end] = 0;
|
luaI_addchar(0);
|
||||||
lua_pushstring (&s[start-1]);
|
while (start <= end)
|
||||||
|
luaI_addchar(s[start++ - 1]);
|
||||||
|
lua_pushstring (luaI_addchar(0));
|
||||||
}
|
}
|
||||||
free (s);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -85,19 +134,12 @@ static void str_sub (void)
|
|||||||
*/
|
*/
|
||||||
static void str_lower (void)
|
static void str_lower (void)
|
||||||
{
|
{
|
||||||
char *s, *c;
|
char *s = lua_check_string(1, "strlower");
|
||||||
lua_Object o = lua_getparam (1);
|
luaI_addchar(0);
|
||||||
if (!lua_isstring(o))
|
while (*s)
|
||||||
{ lua_error ("incorrect arguments to function `strlower'"); return; }
|
luaI_addchar(tolower(*s++));
|
||||||
c = s = strdup(lua_getstring(o));
|
lua_pushstring(luaI_addchar(0));
|
||||||
while (*c != 0)
|
}
|
||||||
{
|
|
||||||
*c = tolower(*c);
|
|
||||||
c++;
|
|
||||||
}
|
|
||||||
lua_pushstring(s);
|
|
||||||
free(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -107,19 +149,104 @@ static void str_lower (void)
|
|||||||
*/
|
*/
|
||||||
static void str_upper (void)
|
static void str_upper (void)
|
||||||
{
|
{
|
||||||
char *s, *c;
|
char *s = lua_check_string(1, "strupper");
|
||||||
lua_Object o = lua_getparam (1);
|
luaI_addchar(0);
|
||||||
if (!lua_isstring(o))
|
while (*s)
|
||||||
{ lua_error ("incorrect arguments to function `strlower'"); return; }
|
luaI_addchar(toupper(*s++));
|
||||||
c = s = strdup(lua_getstring(o));
|
lua_pushstring(luaI_addchar(0));
|
||||||
while (*c != 0)
|
}
|
||||||
{
|
|
||||||
*c = toupper(*c);
|
/*
|
||||||
c++;
|
** get ascii value of a character in a string
|
||||||
}
|
*/
|
||||||
lua_pushstring(s);
|
static void str_ascii (void)
|
||||||
free(s);
|
{
|
||||||
}
|
char *s = lua_check_string(1, "ascii");
|
||||||
|
int pos = lua_opt_number(2, 1, "ascii") - 1;
|
||||||
|
if (pos<0 || pos>=strlen(s))
|
||||||
|
lua_arg_error("ascii");
|
||||||
|
lua_pushnumber(s[pos]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void luaI_addquoted (char *s)
|
||||||
|
{
|
||||||
|
luaI_addchar('"');
|
||||||
|
for (; *s; s++)
|
||||||
|
{
|
||||||
|
if (*s == '"' || *s == '\\' || *s == '\n')
|
||||||
|
luaI_addchar('\\');
|
||||||
|
luaI_addchar(*s);
|
||||||
|
}
|
||||||
|
luaI_addchar('"');
|
||||||
|
}
|
||||||
|
|
||||||
|
#define MAX_CONVERTION 2000
|
||||||
|
#define MAX_FORMAT 50
|
||||||
|
|
||||||
|
static void str_format (void)
|
||||||
|
{
|
||||||
|
int arg = 1;
|
||||||
|
char *strfrmt = lua_check_string(arg++, "format");
|
||||||
|
luaI_addchar(0); /* initialize */
|
||||||
|
while (*strfrmt)
|
||||||
|
{
|
||||||
|
if (*strfrmt != '%')
|
||||||
|
luaI_addchar(*strfrmt++);
|
||||||
|
else if (*++strfrmt == '%')
|
||||||
|
luaI_addchar(*strfrmt++); /* %% */
|
||||||
|
else
|
||||||
|
{ /* format item */
|
||||||
|
char form[MAX_FORMAT]; /* store the format ('%...') */
|
||||||
|
char buff[MAX_CONVERTION]; /* store the formated value */
|
||||||
|
int size = 0;
|
||||||
|
int i = 0;
|
||||||
|
form[i++] = '%';
|
||||||
|
form[i] = *strfrmt++;
|
||||||
|
while (!isalpha(form[i]))
|
||||||
|
{
|
||||||
|
if (isdigit(form[i]))
|
||||||
|
{
|
||||||
|
size = size*10 + form[i]-'0';
|
||||||
|
if (size >= MAX_CONVERTION)
|
||||||
|
lua_error("format size/precision too long in function `format'");
|
||||||
|
}
|
||||||
|
else if (form[i] == '.')
|
||||||
|
size = 0; /* re-start */
|
||||||
|
if (++i >= MAX_FORMAT)
|
||||||
|
lua_error("bad format in function `format'");
|
||||||
|
form[i] = *strfrmt++;
|
||||||
|
}
|
||||||
|
form[i+1] = 0; /* ends string */
|
||||||
|
switch (form[i])
|
||||||
|
{
|
||||||
|
case 'q':
|
||||||
|
luaI_addquoted(lua_check_string(arg++, "format"));
|
||||||
|
buff[0] = '\0'; /* addchar already done */
|
||||||
|
break;
|
||||||
|
case 's':
|
||||||
|
{
|
||||||
|
char *s = lua_check_string(arg++, "format");
|
||||||
|
if (strlen(s) >= MAX_CONVERTION)
|
||||||
|
lua_error("string argument too long in function `format'");
|
||||||
|
sprintf(buff, form, s);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'c': case 'd': case 'i': case 'o':
|
||||||
|
case 'u': case 'x': case 'X':
|
||||||
|
sprintf(buff, form, (int)lua_check_number(arg++, "format"));
|
||||||
|
break;
|
||||||
|
case 'e': case 'E': case 'f': case 'g':
|
||||||
|
sprintf(buff, form, lua_check_number(arg++, "format"));
|
||||||
|
break;
|
||||||
|
default: /* also treat cases 'pnLlh' */
|
||||||
|
lua_error("invalid format option in function `format'");
|
||||||
|
}
|
||||||
|
for (i=0; buff[i]; i++) /* move formated value to result */
|
||||||
|
luaI_addchar(buff[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lua_pushstring(luaI_addchar(0)); /* push the result */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -132,4 +259,6 @@ void strlib_open (void)
|
|||||||
lua_register ("strsub", str_sub);
|
lua_register ("strsub", str_sub);
|
||||||
lua_register ("strlower", str_lower);
|
lua_register ("strlower", str_lower);
|
||||||
lua_register ("strupper", str_upper);
|
lua_register ("strupper", str_upper);
|
||||||
|
lua_register ("ascii", str_ascii);
|
||||||
|
lua_register ("format", str_format);
|
||||||
}
|
}
|
||||||
|
|||||||
387
table.c
387
table.c
@@ -3,198 +3,171 @@
|
|||||||
** Module to control static tables
|
** Module to control static tables
|
||||||
*/
|
*/
|
||||||
|
|
||||||
char *rcs_table="$Id: table.c,v 2.1 1994/04/20 22:07:57 celes Exp celes $";
|
char *rcs_table="$Id: table.c,v 2.50 1996/03/21 16:31:32 roberto Exp roberto $";
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "mm.h"
|
|
||||||
|
|
||||||
|
#include "mem.h"
|
||||||
#include "opcode.h"
|
#include "opcode.h"
|
||||||
#include "tree.h"
|
#include "tree.h"
|
||||||
#include "hash.h"
|
#include "hash.h"
|
||||||
#include "inout.h"
|
|
||||||
#include "table.h"
|
#include "table.h"
|
||||||
|
#include "inout.h"
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
#include "fallback.h"
|
||||||
|
#include "luadebug.h"
|
||||||
|
|
||||||
#define streq(s1,s2) (s1[0]==s2[0]&&strcmp(s1+1,s2+1)==0)
|
|
||||||
|
|
||||||
#define BUFFER_BLOCK 256
|
#define BUFFER_BLOCK 256
|
||||||
|
|
||||||
Symbol *lua_table;
|
Symbol *lua_table = NULL;
|
||||||
static Word lua_ntable = 0;
|
Word lua_ntable = 0;
|
||||||
static Word lua_maxsymbol = 0;
|
static Long lua_maxsymbol = 0;
|
||||||
|
|
||||||
char **lua_constant;
|
TaggedString **lua_constant = NULL;
|
||||||
static Word lua_nconstant = 0;
|
Word lua_nconstant = 0;
|
||||||
static Word lua_maxconstant = 0;
|
static Long lua_maxconstant = 0;
|
||||||
|
|
||||||
|
|
||||||
|
#define GARBAGE_BLOCK 1024
|
||||||
|
#define MIN_GARBAGE_BLOCK (GARBAGE_BLOCK/2)
|
||||||
|
|
||||||
#define MAXFILE 20
|
static void lua_nextvar (void);
|
||||||
char *lua_file[MAXFILE];
|
|
||||||
int lua_nfile;
|
|
||||||
|
|
||||||
/* Variables to controll garbage collection */
|
|
||||||
#define GARBAGE_BLOCK 256
|
|
||||||
Word lua_block=GARBAGE_BLOCK; /* when garbage collector will be called */
|
|
||||||
Word lua_nentity; /* counter of new entities (strings and arrays) */
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Initialise symbol table with internal functions
|
** Internal functions
|
||||||
*/
|
*/
|
||||||
static void lua_initsymbol (void)
|
static struct {
|
||||||
|
char *name;
|
||||||
|
lua_CFunction func;
|
||||||
|
} int_funcs[] = {
|
||||||
|
{"assert", luaI_assert},
|
||||||
|
{"dofile", lua_internaldofile},
|
||||||
|
{"dostring", lua_internaldostring},
|
||||||
|
{"error", luaI_error},
|
||||||
|
{"getglobal", luaI_getglobal},
|
||||||
|
{"next", lua_next},
|
||||||
|
{"nextvar", lua_nextvar},
|
||||||
|
{"print", luaI_print},
|
||||||
|
{"setfallback", luaI_setfallback},
|
||||||
|
{"setglobal", luaI_setglobal},
|
||||||
|
{"tonumber", lua_obj2number},
|
||||||
|
{"tostring", luaI_tostring},
|
||||||
|
{"type", luaI_type}
|
||||||
|
};
|
||||||
|
|
||||||
|
#define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0]))
|
||||||
|
|
||||||
|
|
||||||
|
void luaI_initsymbol (void)
|
||||||
{
|
{
|
||||||
int n;
|
int i;
|
||||||
lua_maxsymbol = BUFFER_BLOCK;
|
lua_maxsymbol = BUFFER_BLOCK;
|
||||||
lua_table = (Symbol *) calloc(lua_maxsymbol, sizeof(Symbol));
|
lua_table = newvector(lua_maxsymbol, Symbol);
|
||||||
if (lua_table == NULL)
|
for (i=0; i<INTFUNCSIZE; i++)
|
||||||
{
|
{
|
||||||
lua_error ("symbol table: not enough memory");
|
Word n = luaI_findsymbolbyname(int_funcs[i].name);
|
||||||
return;
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = int_funcs[i].func;
|
||||||
}
|
}
|
||||||
n = lua_findsymbol("type");
|
|
||||||
s_tag(n) = T_CFUNCTION; s_fvalue(n) = lua_type;
|
|
||||||
n = lua_findsymbol("tonumber");
|
|
||||||
s_tag(n) = T_CFUNCTION; s_fvalue(n) = lua_obj2number;
|
|
||||||
n = lua_findsymbol("next");
|
|
||||||
s_tag(n) = T_CFUNCTION; s_fvalue(n) = lua_next;
|
|
||||||
n = lua_findsymbol("nextvar");
|
|
||||||
s_tag(n) = T_CFUNCTION; s_fvalue(n) = lua_nextvar;
|
|
||||||
n = lua_findsymbol("print");
|
|
||||||
s_tag(n) = T_CFUNCTION; s_fvalue(n) = lua_print;
|
|
||||||
n = lua_findsymbol("dofile");
|
|
||||||
s_tag(n) = T_CFUNCTION; s_fvalue(n) = lua_internaldofile;
|
|
||||||
n = lua_findsymbol("dostring");
|
|
||||||
s_tag(n) = T_CFUNCTION; s_fvalue(n) = lua_internaldostring;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Initialise constant table with pre-defined constants
|
** Initialise constant table with pre-defined constants
|
||||||
*/
|
*/
|
||||||
void lua_initconstant (void)
|
void luaI_initconstant (void)
|
||||||
{
|
{
|
||||||
lua_maxconstant = BUFFER_BLOCK;
|
lua_maxconstant = BUFFER_BLOCK;
|
||||||
lua_constant = (char **) calloc(lua_maxconstant, sizeof(char *));
|
lua_constant = newvector(lua_maxconstant, TaggedString *);
|
||||||
if (lua_constant == NULL)
|
/* pre-register mem error messages, to avoid loop when error arises */
|
||||||
{
|
luaI_findconstantbyname(tableEM);
|
||||||
lua_error ("constant table: not enough memory");
|
luaI_findconstantbyname(memEM);
|
||||||
return;
|
|
||||||
}
|
|
||||||
lua_findconstant("mark");
|
|
||||||
lua_findconstant("nil");
|
|
||||||
lua_findconstant("number");
|
|
||||||
lua_findconstant("string");
|
|
||||||
lua_findconstant("table");
|
|
||||||
lua_findconstant("function");
|
|
||||||
lua_findconstant("cfunction");
|
|
||||||
lua_findconstant("userdata");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Given a name, search it at symbol table and return its index. If not
|
** Given a name, search it at symbol table and return its index. If not
|
||||||
** found, allocate it.
|
** found, allocate it.
|
||||||
** On error, return -1.
|
|
||||||
*/
|
*/
|
||||||
int lua_findsymbol (char *s)
|
Word luaI_findsymbol (TaggedString *t)
|
||||||
{
|
{
|
||||||
char *n;
|
if (t->varindex == NOT_USED)
|
||||||
if (lua_table == NULL)
|
|
||||||
lua_initsymbol();
|
|
||||||
n = lua_varcreate(s);
|
|
||||||
if (n == NULL)
|
|
||||||
{
|
|
||||||
lua_error ("create symbol: not enough memory");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
if (indexstring(n) == UNMARKED_STRING)
|
|
||||||
{
|
{
|
||||||
if (lua_ntable == lua_maxsymbol)
|
if (lua_ntable == lua_maxsymbol)
|
||||||
{
|
lua_maxsymbol = growvector(&lua_table, lua_maxsymbol, Symbol,
|
||||||
lua_maxsymbol *= 2;
|
symbolEM, MAX_WORD);
|
||||||
if (lua_maxsymbol > MAX_WORD)
|
t->varindex = lua_ntable;
|
||||||
{
|
lua_table[lua_ntable].varname = t;
|
||||||
lua_error("symbol table overflow");
|
s_tag(lua_ntable) = LUA_T_NIL;
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
lua_table = (Symbol *)realloc(lua_table, lua_maxsymbol*sizeof(Symbol));
|
|
||||||
if (lua_table == NULL)
|
|
||||||
{
|
|
||||||
lua_error ("symbol table: not enough memory");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
indexstring(n) = lua_ntable;
|
|
||||||
s_tag(lua_ntable) = T_NIL;
|
|
||||||
lua_ntable++;
|
lua_ntable++;
|
||||||
}
|
}
|
||||||
return indexstring(n);
|
return t->varindex;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Word luaI_findsymbolbyname (char *name)
|
||||||
|
{
|
||||||
|
return luaI_findsymbol(luaI_createfixedstring(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Given a name, search it at constant table and return its index. If not
|
** Given a tree node, check it is has a correspondent constant index. If not,
|
||||||
** found, allocate it.
|
** allocate it.
|
||||||
** On error, return -1.
|
|
||||||
*/
|
*/
|
||||||
int lua_findconstant (char *s)
|
Word luaI_findconstant (TaggedString *t)
|
||||||
{
|
{
|
||||||
char *n;
|
if (t->constindex == NOT_USED)
|
||||||
if (lua_constant == NULL)
|
|
||||||
lua_initconstant();
|
|
||||||
n = lua_constcreate(s);
|
|
||||||
if (n == NULL)
|
|
||||||
{
|
|
||||||
lua_error ("create constant: not enough memory");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
if (indexstring(n) == UNMARKED_STRING)
|
|
||||||
{
|
{
|
||||||
if (lua_nconstant == lua_maxconstant)
|
if (lua_nconstant == lua_maxconstant)
|
||||||
{
|
lua_maxconstant = growvector(&lua_constant, lua_maxconstant, TaggedString *,
|
||||||
lua_maxconstant *= 2;
|
constantEM, MAX_WORD);
|
||||||
if (lua_maxconstant > MAX_WORD)
|
t->constindex = lua_nconstant;
|
||||||
{
|
lua_constant[lua_nconstant] = t;
|
||||||
lua_error("constant table overflow");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
lua_constant = (char**)realloc(lua_constant,lua_maxconstant*sizeof(char*));
|
|
||||||
if (lua_constant == NULL)
|
|
||||||
{
|
|
||||||
lua_error ("constant table: not enough memory");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
indexstring(n) = lua_nconstant;
|
|
||||||
lua_constant[lua_nconstant] = n;
|
|
||||||
lua_nconstant++;
|
lua_nconstant++;
|
||||||
}
|
}
|
||||||
return indexstring(n);
|
return t->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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Traverse symbol table objects
|
** Traverse symbol table objects
|
||||||
*/
|
*/
|
||||||
void lua_travsymbol (void (*fn)(Object *))
|
static char *lua_travsymbol (int (*fn)(Object *))
|
||||||
{
|
{
|
||||||
int i;
|
Word i;
|
||||||
for (i=0; i<lua_ntable; i++)
|
for (i=0; i<lua_ntable; i++)
|
||||||
fn(&s_object(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.
|
** Mark an object if it is a string or a unmarked array.
|
||||||
*/
|
*/
|
||||||
void lua_markobject (Object *o)
|
int lua_markobject (Object *o)
|
||||||
{
|
{/* if already marked, does not change mark value */
|
||||||
if (tag(o) == T_STRING && indexstring(svalue(o)) == UNMARKED_STRING)
|
if (tag(o) == LUA_T_STRING && !tsvalue(o)->marked)
|
||||||
indexstring(svalue(o)) = MARKED_STRING;
|
tsvalue(o)->marked = 1;
|
||||||
else if (tag(o) == T_ARRAY)
|
else if (tag(o) == LUA_T_ARRAY)
|
||||||
lua_hashmark (avalue(o));
|
lua_hashmark (avalue(o));
|
||||||
|
else if ((o->tag == LUA_T_FUNCTION || o->tag == LUA_T_MARK)
|
||||||
|
&& !o->value.tf->marked)
|
||||||
|
o->value.tf->marked = 1;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -202,108 +175,92 @@ void lua_markobject (Object *o)
|
|||||||
** Garbage collection.
|
** Garbage collection.
|
||||||
** Delete all unused strings and arrays.
|
** Delete all unused strings and arrays.
|
||||||
*/
|
*/
|
||||||
|
Long luaI_collectgarbage (void)
|
||||||
|
{
|
||||||
|
Long recovered = 0;
|
||||||
|
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 */
|
||||||
|
recovered += lua_strcollector();
|
||||||
|
recovered += lua_hashcollector();
|
||||||
|
recovered += luaI_funccollector();
|
||||||
|
return recovered;
|
||||||
|
}
|
||||||
|
|
||||||
void lua_pack (void)
|
void lua_pack (void)
|
||||||
{
|
{
|
||||||
/* mark stack strings */
|
static Long block = GARBAGE_BLOCK; /* when garbage collector will be called */
|
||||||
lua_travstack(lua_markobject);
|
static Long nentity = 0; /* counter of new entities (strings and arrays) */
|
||||||
|
Long recovered = 0;
|
||||||
/* mark symbol table strings */
|
if (nentity++ < block) return;
|
||||||
lua_travsymbol(lua_markobject);
|
recovered = luaI_collectgarbage();
|
||||||
|
nentity = 0; /* reset counter */
|
||||||
lua_strcollector();
|
block=(16*block-7*recovered)/12; /* adapt block size */
|
||||||
lua_hashcollector();
|
if (block < MIN_GARBAGE_BLOCK) block = MIN_GARBAGE_BLOCK;
|
||||||
|
|
||||||
lua_nentity = 0; /* reset counter */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** If the string isn't allocated, allocate a new string at string tree.
|
|
||||||
*/
|
|
||||||
char *lua_createstring (char *s)
|
|
||||||
{
|
|
||||||
if (s == NULL) return NULL;
|
|
||||||
|
|
||||||
if (lua_nentity == lua_block)
|
|
||||||
lua_pack ();
|
|
||||||
lua_nentity++;
|
|
||||||
return lua_strcreate(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Add a file name at file table, checking overflow. This function also set
|
|
||||||
** the external variable "lua_filename" with the function filename set.
|
|
||||||
** Return 0 on success or 1 on error.
|
|
||||||
*/
|
|
||||||
int lua_addfile (char *fn)
|
|
||||||
{
|
|
||||||
if (lua_nfile >= MAXFILE-1)
|
|
||||||
{
|
|
||||||
lua_error ("too many files");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
if ((lua_file[lua_nfile++] = strdup (fn)) == NULL)
|
|
||||||
{
|
|
||||||
lua_error ("not enough memory");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Delete a file from file stack
|
|
||||||
*/
|
|
||||||
int lua_delfile (void)
|
|
||||||
{
|
|
||||||
lua_nfile--;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Return the last file name set.
|
|
||||||
*/
|
|
||||||
char *lua_filename (void)
|
|
||||||
{
|
|
||||||
return lua_file[lua_nfile-1];
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Internal function: return next global variable
|
** Internal function: return next global variable
|
||||||
*/
|
*/
|
||||||
void lua_nextvar (void)
|
static void lua_nextvar (void)
|
||||||
{
|
{
|
||||||
char *varname, *next;
|
Word next;
|
||||||
Object *o = lua_getparam (1);
|
lua_Object o = lua_getparam(1);
|
||||||
if (o == NULL)
|
if (o == LUA_NOOBJECT)
|
||||||
{ lua_error ("too few arguments to function `nextvar'"); return; }
|
lua_error("too few arguments to function `nextvar'");
|
||||||
if (lua_getparam (2) != NULL)
|
if (lua_getparam(2) != LUA_NOOBJECT)
|
||||||
{ lua_error ("too many arguments to function `nextvar'"); return; }
|
lua_error("too many arguments to function `nextvar'");
|
||||||
if (tag(o) == T_NIL)
|
if (lua_isnil(o))
|
||||||
|
next = 0;
|
||||||
|
else if (!lua_isstring(o))
|
||||||
{
|
{
|
||||||
varname = 0;
|
lua_error("incorrect argument to function `nextvar'");
|
||||||
}
|
return; /* to avoid warnings */
|
||||||
else if (tag(o) != T_STRING)
|
|
||||||
{
|
|
||||||
lua_error ("incorrect argument to function `nextvar'");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
next = luaI_findsymbolbyname(lua_getstring(o)) + 1;
|
||||||
varname = svalue(o);
|
while (next < lua_ntable && s_tag(next) == LUA_T_NIL) next++;
|
||||||
}
|
if (next >= lua_ntable)
|
||||||
next = lua_varnext(varname);
|
|
||||||
if (next == NULL)
|
|
||||||
{
|
{
|
||||||
lua_pushnil();
|
lua_pushnil();
|
||||||
lua_pushnil();
|
lua_pushnil();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
TaggedString *t = lua_table[next].varname;
|
||||||
Object name;
|
Object name;
|
||||||
tag(&name) = T_STRING;
|
tag(&name) = LUA_T_STRING;
|
||||||
svalue(&name) = next;
|
tsvalue(&name) = t;
|
||||||
if (lua_pushobject (&name)) return;
|
luaI_pushobject(&name);
|
||||||
if (lua_pushobject (&s_object(indexstring(next)))) return;
|
luaI_pushobject(&s_object(next));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static Object *functofind;
|
||||||
|
static int checkfunc (Object *o)
|
||||||
|
{
|
||||||
|
if (o->tag == LUA_T_FUNCTION)
|
||||||
|
return
|
||||||
|
((functofind->tag == LUA_T_FUNCTION || functofind->tag == LUA_T_MARK)
|
||||||
|
&& (functofind->value.tf == o->value.tf));
|
||||||
|
if (o->tag == LUA_T_CFUNCTION)
|
||||||
|
return
|
||||||
|
((functofind->tag == LUA_T_CFUNCTION || functofind->tag == 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 "fallback";
|
||||||
|
else if ((*name = lua_travsymbol(checkfunc)) != NULL)
|
||||||
|
return "global";
|
||||||
|
else return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
43
table.h
43
table.h
@@ -1,32 +1,37 @@
|
|||||||
/*
|
/*
|
||||||
** Module to control static tables
|
** Module to control static tables
|
||||||
** TeCGraf - PUC-Rio
|
** TeCGraf - PUC-Rio
|
||||||
** $Id: table.h,v 2.1 1994/04/20 22:07:57 celes Exp celes $
|
** $Id: table.h,v 2.19 1996/02/26 21:00:27 roberto Exp roberto $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef table_h
|
#ifndef table_h
|
||||||
#define table_h
|
#define table_h
|
||||||
|
|
||||||
|
#include "tree.h"
|
||||||
|
#include "opcode.h"
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
Object object;
|
||||||
|
TaggedString *varname;
|
||||||
|
} Symbol;
|
||||||
|
|
||||||
|
|
||||||
extern Symbol *lua_table;
|
extern Symbol *lua_table;
|
||||||
extern char **lua_constant;
|
extern Word lua_ntable;
|
||||||
|
extern TaggedString **lua_constant;
|
||||||
|
extern Word lua_nconstant;
|
||||||
|
|
||||||
extern char *lua_file[];
|
void luaI_initsymbol (void);
|
||||||
extern int lua_nfile;
|
void luaI_initconstant (void);
|
||||||
|
Word luaI_findsymbolbyname (char *name);
|
||||||
extern Word lua_block;
|
Word luaI_findsymbol (TaggedString *t);
|
||||||
extern Word lua_nentity;
|
Word luaI_findconstant (TaggedString *t);
|
||||||
|
Word luaI_findconstantbyname (char *name);
|
||||||
|
TaggedString *luaI_createfixedstring (char *str);
|
||||||
void lua_initconstant (void);
|
int lua_markobject (Object *o);
|
||||||
int lua_findsymbol (char *s);
|
Long luaI_collectgarbage (void);
|
||||||
int lua_findconstant (char *s);
|
|
||||||
void lua_travsymbol (void (*fn)(Object *));
|
|
||||||
void lua_markobject (Object *o);
|
|
||||||
void lua_pack (void);
|
void lua_pack (void);
|
||||||
char *lua_createstring (char *s);
|
|
||||||
int lua_addfile (char *fn);
|
|
||||||
int lua_delfile (void);
|
|
||||||
char *lua_filename (void);
|
|
||||||
void lua_nextvar (void);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
289
tree.c
289
tree.c
@@ -3,208 +3,143 @@
|
|||||||
** TecCGraf - PUC-Rio
|
** TecCGraf - PUC-Rio
|
||||||
*/
|
*/
|
||||||
|
|
||||||
char *rcs_tree="$Id: $";
|
char *rcs_tree="$Id: tree.c,v 1.19 1996/02/22 20:34:33 roberto Exp $";
|
||||||
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "mem.h"
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
#include "tree.h"
|
#include "tree.h"
|
||||||
|
#include "lex.h"
|
||||||
|
#include "hash.h"
|
||||||
|
#include "table.h"
|
||||||
|
|
||||||
|
|
||||||
#define lua_strcmp(a,b) (a[0]<b[0]?(-1):(a[0]>b[0]?(1):strcmp(a,b)))
|
#define NUM_HASHS 64
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int size;
|
||||||
|
int nuse; /* number of elements (including EMPTYs) */
|
||||||
|
TaggedString **hash;
|
||||||
|
} stringtable;
|
||||||
|
|
||||||
|
static int initialized = 0;
|
||||||
|
|
||||||
|
static stringtable string_root[NUM_HASHS];
|
||||||
|
|
||||||
|
static TaggedString EMPTY = {NOT_USED, NOT_USED, 0, 2, {0}};
|
||||||
|
|
||||||
|
|
||||||
typedef struct TreeNode
|
static unsigned long hash (char *str)
|
||||||
{
|
{
|
||||||
struct TreeNode *right;
|
unsigned long h = 0;
|
||||||
struct TreeNode *left;
|
while (*str)
|
||||||
Word index;
|
h = ((h<<5)-h)^(unsigned char)*(str++);
|
||||||
char str[1]; /* \0 byte already reserved */
|
return h;
|
||||||
} TreeNode;
|
}
|
||||||
|
|
||||||
static TreeNode *string_root = NULL;
|
static void initialize (void)
|
||||||
static TreeNode *constant_root = NULL;
|
|
||||||
static TreeNode *variable_root = NULL;
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Insert a new string/constant/variable at the tree.
|
|
||||||
*/
|
|
||||||
static char *tree_create (TreeNode **node, char *str)
|
|
||||||
{
|
{
|
||||||
if (*node == NULL)
|
initialized = 1;
|
||||||
{
|
luaI_addReserved();
|
||||||
*node = (TreeNode *) malloc (sizeof(TreeNode)+strlen(str));
|
luaI_initsymbol();
|
||||||
if (*node == NULL)
|
luaI_initconstant();
|
||||||
lua_error ("memoria insuficiente\n");
|
}
|
||||||
(*node)->left = (*node)->right = NULL;
|
|
||||||
strcpy((*node)->str, str);
|
|
||||||
(*node)->index = UNMARKED_STRING;
|
static void grow (stringtable *tb)
|
||||||
return (*node)->str;
|
{
|
||||||
}
|
int newsize = luaI_redimension(tb->size);
|
||||||
else
|
TaggedString **newhash = newvector(newsize, TaggedString *);
|
||||||
{
|
int i;
|
||||||
int c = lua_strcmp(str, (*node)->str);
|
for (i=0; i<newsize; i++)
|
||||||
if (c < 0)
|
newhash[i] = NULL;
|
||||||
return tree_create(&(*node)->left, str);
|
/* rehash */
|
||||||
else if (c > 0)
|
tb->nuse = 0;
|
||||||
return tree_create(&(*node)->right, str);
|
for (i=0; i<tb->size; i++)
|
||||||
|
if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY)
|
||||||
|
{
|
||||||
|
int h = tb->hash[i]->hash%newsize;
|
||||||
|
while (newhash[h])
|
||||||
|
h = (h+1)%newsize;
|
||||||
|
newhash[h] = tb->hash[i];
|
||||||
|
tb->nuse++;
|
||||||
|
}
|
||||||
|
luaI_free(tb->hash);
|
||||||
|
tb->size = newsize;
|
||||||
|
tb->hash = newhash;
|
||||||
|
}
|
||||||
|
|
||||||
|
static TaggedString *insert (char *str, stringtable *tb)
|
||||||
|
{
|
||||||
|
TaggedString *ts;
|
||||||
|
unsigned long h = hash(str);
|
||||||
|
int i;
|
||||||
|
int j = -1;
|
||||||
|
if ((Long)tb->nuse*3 >= (Long)tb->size*2)
|
||||||
|
{
|
||||||
|
if (!initialized)
|
||||||
|
initialize();
|
||||||
|
grow(tb);
|
||||||
|
}
|
||||||
|
i = h%tb->size;
|
||||||
|
while (tb->hash[i])
|
||||||
|
{
|
||||||
|
if (tb->hash[i] == &EMPTY)
|
||||||
|
j = i;
|
||||||
|
else if (strcmp(str, tb->hash[i]->str) == 0)
|
||||||
|
return tb->hash[i];
|
||||||
|
i = (i+1)%tb->size;
|
||||||
|
}
|
||||||
|
/* not found */
|
||||||
|
lua_pack();
|
||||||
|
if (j != -1) /* is there an EMPTY space? */
|
||||||
|
i = j;
|
||||||
else
|
else
|
||||||
return (*node)->str;
|
tb->nuse++;
|
||||||
}
|
ts = tb->hash[i] = (TaggedString *)luaI_malloc(sizeof(TaggedString)+strlen(str));
|
||||||
|
strcpy(ts->str, str);
|
||||||
|
ts->marked = 0;
|
||||||
|
ts->hash = h;
|
||||||
|
ts->varindex = ts->constindex = NOT_USED;
|
||||||
|
return ts;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *lua_strcreate (char *str)
|
TaggedString *lua_createstring (char *str)
|
||||||
{
|
{
|
||||||
return tree_create(&string_root, str);
|
return insert(str, &string_root[(unsigned)str[0]%NUM_HASHS]);
|
||||||
}
|
|
||||||
|
|
||||||
char *lua_constcreate (char *str)
|
|
||||||
{
|
|
||||||
return tree_create(&constant_root, str);
|
|
||||||
}
|
|
||||||
|
|
||||||
char *lua_varcreate (char *str)
|
|
||||||
{
|
|
||||||
return tree_create(&variable_root, str);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Free a node of the tree
|
|
||||||
*/
|
|
||||||
static TreeNode *lua_strfree (TreeNode *parent)
|
|
||||||
{
|
|
||||||
if (parent->left == NULL && parent->right == NULL) /* no child */
|
|
||||||
{
|
|
||||||
free (parent);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
else if (parent->left == NULL) /* only right child */
|
|
||||||
{
|
|
||||||
TreeNode *p = parent->right;
|
|
||||||
free (parent);
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
else if (parent->right == NULL) /* only left child */
|
|
||||||
{
|
|
||||||
TreeNode *p = parent->left;
|
|
||||||
free (parent);
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
else /* two children */
|
|
||||||
{
|
|
||||||
TreeNode *p = parent, *r = parent->right;
|
|
||||||
while (r->left != NULL)
|
|
||||||
{
|
|
||||||
p = r;
|
|
||||||
r = r->left;
|
|
||||||
}
|
|
||||||
if (p == parent)
|
|
||||||
{
|
|
||||||
r->left = parent->left;
|
|
||||||
parent->left = NULL;
|
|
||||||
parent->right = r->right;
|
|
||||||
r->right = lua_strfree(parent);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
TreeNode *t = r->right;
|
|
||||||
r->left = parent->left;
|
|
||||||
r->right = parent->right;
|
|
||||||
parent->left = NULL;
|
|
||||||
parent->right = t;
|
|
||||||
p->left = lua_strfree(parent);
|
|
||||||
}
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Traverse tree for garbage collection
|
|
||||||
*/
|
|
||||||
static TreeNode *lua_travcollector (TreeNode *r)
|
|
||||||
{
|
|
||||||
if (r == NULL) return NULL;
|
|
||||||
r->right = lua_travcollector(r->right);
|
|
||||||
r->left = lua_travcollector(r->left);
|
|
||||||
if (r->index == UNMARKED_STRING)
|
|
||||||
return lua_strfree(r);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
r->index = UNMARKED_STRING;
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Garbage collection function.
|
** Garbage collection function.
|
||||||
** This function traverse the tree freening unindexed strings
|
** This function traverse the string list freeing unindexed strings
|
||||||
*/
|
*/
|
||||||
void lua_strcollector (void)
|
Long lua_strcollector (void)
|
||||||
{
|
{
|
||||||
string_root = lua_travcollector(string_root);
|
Long counter = 0;
|
||||||
}
|
int i;
|
||||||
|
for (i=0; i<NUM_HASHS; i++)
|
||||||
/*
|
|
||||||
** Return next variable.
|
|
||||||
*/
|
|
||||||
static TreeNode *tree_next (TreeNode *node, char *str)
|
|
||||||
{
|
|
||||||
#if 0
|
|
||||||
if (node == NULL) return NULL;
|
|
||||||
if (str == NULL || lua_strcmp(str, node->str) < 0)
|
|
||||||
{
|
|
||||||
TreeNode *result = tree_next(node->left, str);
|
|
||||||
return result == NULL ? node : result;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return tree_next(node->right, str);
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
if (node == NULL) return NULL;
|
|
||||||
else if (str == NULL) return node;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
int c = lua_strcmp(str, node->str);
|
|
||||||
if (c == 0)
|
|
||||||
return node->left != NULL ? node->left : node->right;
|
|
||||||
else if (c < 0)
|
|
||||||
{
|
{
|
||||||
TreeNode *result = tree_next(node->left, str);
|
stringtable *tb = &string_root[i];
|
||||||
return result != NULL ? result : node->right;
|
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
|
||||||
|
{
|
||||||
|
luaI_free(t);
|
||||||
|
tb->hash[j] = &EMPTY;
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
return counter;
|
||||||
return tree_next(node->right, str);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char *lua_varnext (char *n)
|
|
||||||
{
|
|
||||||
TreeNode *result = tree_next(variable_root, n);
|
|
||||||
return result != NULL ? result->str : NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Given an id, find the string with exaustive search
|
|
||||||
*/
|
|
||||||
static char *tree_name (TreeNode *node, Word index)
|
|
||||||
{
|
|
||||||
if (node == NULL) return NULL;
|
|
||||||
if (node->index == index) return node->str;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
char *result = tree_name(node->left, index);
|
|
||||||
return result != NULL ? result : tree_name(node->right, index);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
char *lua_varname (Word index)
|
|
||||||
{
|
|
||||||
return tree_name(variable_root, index);
|
|
||||||
}
|
|
||||||
|
|||||||
29
tree.h
29
tree.h
@@ -1,27 +1,28 @@
|
|||||||
/*
|
/*
|
||||||
** tree.h
|
** tree.h
|
||||||
** TecCGraf - PUC-Rio
|
** TecCGraf - PUC-Rio
|
||||||
** $Id: $
|
** $Id: tree.h,v 1.13 1996/02/14 13:35:51 roberto Exp roberto $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef tree_h
|
#ifndef tree_h
|
||||||
#define tree_h
|
#define tree_h
|
||||||
|
|
||||||
#include "opcode.h"
|
#include "types.h"
|
||||||
|
|
||||||
|
#define NOT_USED 0xFFFE
|
||||||
|
|
||||||
|
|
||||||
#define UNMARKED_STRING 0xFFFF
|
typedef struct TaggedString
|
||||||
#define MARKED_STRING 0xFFFE
|
{
|
||||||
#define MAX_WORD 0xFFFD
|
Word varindex; /* != NOT_USED if this is a symbol */
|
||||||
|
Word constindex; /* != NOT_USED if this is a constant */
|
||||||
|
unsigned long hash; /* 0 if not initialized */
|
||||||
|
int marked; /* for garbage collection; never collect (nor change) if > 1 */
|
||||||
|
char str[1]; /* \0 byte already reserved */
|
||||||
|
} TaggedString;
|
||||||
|
|
||||||
|
|
||||||
#define indexstring(s) (*(((Word *)s)-1))
|
TaggedString *lua_createstring (char *str);
|
||||||
|
Long lua_strcollector (void);
|
||||||
|
|
||||||
char *lua_strcreate (char *str);
|
|
||||||
char *lua_constcreate (char *str);
|
|
||||||
char *lua_varcreate (char *str);
|
|
||||||
void lua_strcollector (void);
|
|
||||||
char *lua_varnext (char *n);
|
|
||||||
char *lua_varname (Word index);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
29
types.h
Normal file
29
types.h
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
** 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
|
||||||
326
undump.c
Normal file
326
undump.c
Normal file
@@ -0,0 +1,326 @@
|
|||||||
|
/*
|
||||||
|
** undump.c
|
||||||
|
** load bytecodes from files
|
||||||
|
*/
|
||||||
|
|
||||||
|
char* rcs_undump="$Id: undump.c,v 1.13 1996/03/12 20:00:40 lhf Exp lhf $";
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "opcode.h"
|
||||||
|
#include "mem.h"
|
||||||
|
#include "table.h"
|
||||||
|
#include "undump.h"
|
||||||
|
|
||||||
|
static int swapword=0;
|
||||||
|
static int swapfloat=0;
|
||||||
|
static TFunc* Main=NULL; /* functions in a chunk */
|
||||||
|
static TFunc* lastF=NULL;
|
||||||
|
|
||||||
|
static void warn(char* s) /* TODO: remove */
|
||||||
|
{
|
||||||
|
#if 0
|
||||||
|
fprintf(stderr,"undump: %s\n",s);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static void FixCode(Byte* code, Byte* end) /* swap words */
|
||||||
|
{
|
||||||
|
Byte* p;
|
||||||
|
for (p=code; p!=end;)
|
||||||
|
{
|
||||||
|
OpCode op=(OpCode)*p;
|
||||||
|
switch (op)
|
||||||
|
{
|
||||||
|
case PUSHNIL:
|
||||||
|
case PUSH0:
|
||||||
|
case PUSH1:
|
||||||
|
case PUSH2:
|
||||||
|
case PUSHLOCAL0:
|
||||||
|
case PUSHLOCAL1:
|
||||||
|
case PUSHLOCAL2:
|
||||||
|
case PUSHLOCAL3:
|
||||||
|
case PUSHLOCAL4:
|
||||||
|
case PUSHLOCAL5:
|
||||||
|
case PUSHLOCAL6:
|
||||||
|
case PUSHLOCAL7:
|
||||||
|
case PUSHLOCAL8:
|
||||||
|
case PUSHLOCAL9:
|
||||||
|
case PUSHINDEXED:
|
||||||
|
case STORELOCAL0:
|
||||||
|
case STORELOCAL1:
|
||||||
|
case STORELOCAL2:
|
||||||
|
case STORELOCAL3:
|
||||||
|
case STORELOCAL4:
|
||||||
|
case STORELOCAL5:
|
||||||
|
case STORELOCAL6:
|
||||||
|
case STORELOCAL7:
|
||||||
|
case STORELOCAL8:
|
||||||
|
case STORELOCAL9:
|
||||||
|
case STOREINDEXED0:
|
||||||
|
case ADJUST0:
|
||||||
|
case EQOP:
|
||||||
|
case LTOP:
|
||||||
|
case LEOP:
|
||||||
|
case GTOP:
|
||||||
|
case GEOP:
|
||||||
|
case ADDOP:
|
||||||
|
case SUBOP:
|
||||||
|
case MULTOP:
|
||||||
|
case DIVOP:
|
||||||
|
case POWOP:
|
||||||
|
case CONCOP:
|
||||||
|
case MINUSOP:
|
||||||
|
case NOTOP:
|
||||||
|
case POP:
|
||||||
|
case RETCODE0:
|
||||||
|
p++;
|
||||||
|
break;
|
||||||
|
case PUSHBYTE:
|
||||||
|
case PUSHLOCAL:
|
||||||
|
case STORELOCAL:
|
||||||
|
case STOREINDEXED:
|
||||||
|
case STORELIST0:
|
||||||
|
case ADJUST:
|
||||||
|
case RETCODE:
|
||||||
|
p+=2;
|
||||||
|
break;
|
||||||
|
case STORELIST:
|
||||||
|
case CALLFUNC:
|
||||||
|
p+=3;
|
||||||
|
break;
|
||||||
|
case PUSHFUNCTION:
|
||||||
|
p+=5;
|
||||||
|
break;
|
||||||
|
case PUSHWORD:
|
||||||
|
case PUSHSELF:
|
||||||
|
case CREATEARRAY:
|
||||||
|
case ONTJMP:
|
||||||
|
case ONFJMP:
|
||||||
|
case JMP:
|
||||||
|
case UPJMP:
|
||||||
|
case IFFJMP:
|
||||||
|
case IFFUPJMP:
|
||||||
|
case SETLINE:
|
||||||
|
case PUSHSTRING:
|
||||||
|
case PUSHGLOBAL:
|
||||||
|
case STOREGLOBAL:
|
||||||
|
{
|
||||||
|
Byte t;
|
||||||
|
t=p[1]; p[1]=p[2]; p[2]=t;
|
||||||
|
p+=3;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case PUSHFLOAT:
|
||||||
|
{
|
||||||
|
Byte t;
|
||||||
|
t=p[1]; p[1]=p[4]; p[4]=t;
|
||||||
|
t=p[2]; p[2]=p[3]; p[3]=t;
|
||||||
|
p+=5;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case STORERECORD:
|
||||||
|
{
|
||||||
|
int n=*++p;
|
||||||
|
p++;
|
||||||
|
while (n--)
|
||||||
|
{
|
||||||
|
Byte t;
|
||||||
|
t=p[0]; p[0]=p[1]; p[1]=t;
|
||||||
|
p+=2;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
lua_error("corrupt binary file");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void Unthread(Byte* code, int i, int v)
|
||||||
|
{
|
||||||
|
while (i!=0)
|
||||||
|
{
|
||||||
|
CodeWord c;
|
||||||
|
Byte* p=code+i;
|
||||||
|
get_word(c,p);
|
||||||
|
i=c.w;
|
||||||
|
c.w=v;
|
||||||
|
p[-2]=c.m.c1;
|
||||||
|
p[-1]=c.m.c2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int LoadWord(FILE* D)
|
||||||
|
{
|
||||||
|
Word w;
|
||||||
|
fread(&w,sizeof(w),1,D);
|
||||||
|
if (swapword)
|
||||||
|
{
|
||||||
|
Byte* p=(Byte*)&w; /* TODO: need union? */
|
||||||
|
Byte t;
|
||||||
|
t=p[0]; p[0]=p[1]; p[1]=t;
|
||||||
|
}
|
||||||
|
return w;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int LoadSize(FILE* D)
|
||||||
|
{
|
||||||
|
Word hi=LoadWord(D);
|
||||||
|
Word lo=LoadWord(D);
|
||||||
|
int s=(hi<<16)|lo;
|
||||||
|
if ((Word)s != s) lua_error("code too long");
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
static char* LoadBlock(int size, FILE* D)
|
||||||
|
{
|
||||||
|
char* b=luaI_malloc(size);
|
||||||
|
fread(b,size,1,D);
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
|
static char* LoadString(FILE* D)
|
||||||
|
{
|
||||||
|
int size=LoadWord(D);
|
||||||
|
char *b=luaI_buffer(size);
|
||||||
|
fread(b,size,1,D);
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
|
static char* LoadNewString(FILE* D)
|
||||||
|
{
|
||||||
|
return LoadBlock(LoadWord(D),D);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void LoadFunction(FILE* D)
|
||||||
|
{
|
||||||
|
TFunc* tf=new(TFunc);
|
||||||
|
tf->next=NULL;
|
||||||
|
tf->locvars=NULL;
|
||||||
|
tf->size=LoadSize(D);
|
||||||
|
tf->lineDefined=LoadWord(D);
|
||||||
|
if (IsMain(tf)) /* new main */
|
||||||
|
{
|
||||||
|
tf->fileName=LoadNewString(D);
|
||||||
|
Main=lastF=tf;
|
||||||
|
}
|
||||||
|
else /* fix PUSHFUNCTION */
|
||||||
|
{
|
||||||
|
CodeCode c;
|
||||||
|
Byte* p;
|
||||||
|
tf->marked=LoadWord(D);
|
||||||
|
tf->fileName=Main->fileName;
|
||||||
|
p=Main->code+tf->marked;
|
||||||
|
c.tf=tf;
|
||||||
|
*p++=c.m.c1; *p++=c.m.c2; *p++=c.m.c3; *p++=c.m.c4;
|
||||||
|
lastF=lastF->next=tf;
|
||||||
|
}
|
||||||
|
tf->code=LoadBlock(tf->size,D);
|
||||||
|
if (swapword || swapfloat) FixCode(tf->code,tf->code+tf->size);
|
||||||
|
while (1) /* unthread */
|
||||||
|
{
|
||||||
|
int c=getc(D);
|
||||||
|
if (c==ID_VAR) /* global var */
|
||||||
|
{
|
||||||
|
int i=LoadWord(D);
|
||||||
|
char* s=LoadString(D);
|
||||||
|
int v=luaI_findsymbolbyname(s);
|
||||||
|
Unthread(tf->code,i,v);
|
||||||
|
}
|
||||||
|
else if (c==ID_STR) /* constant string */
|
||||||
|
{
|
||||||
|
int i=LoadWord(D);
|
||||||
|
char* s=LoadString(D);
|
||||||
|
int v=luaI_findconstantbyname(s);
|
||||||
|
Unthread(tf->code,i,v);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ungetc(c,D);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void LoadSignature(FILE* D)
|
||||||
|
{
|
||||||
|
char* s=SIGNATURE;
|
||||||
|
while (*s!=0 && getc(D)==*s)
|
||||||
|
++s;
|
||||||
|
if (*s!=0) lua_error("bad signature");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void LoadHeader(FILE* D) /* TODO: error handling */
|
||||||
|
{
|
||||||
|
Word w,tw=TEST_WORD;
|
||||||
|
float f,tf=TEST_FLOAT;
|
||||||
|
LoadSignature(D);
|
||||||
|
getc(D); /* skip version */
|
||||||
|
fread(&w,sizeof(w),1,D); /* test word */
|
||||||
|
if (w!=tw)
|
||||||
|
{
|
||||||
|
swapword=1;
|
||||||
|
warn("different byte order");
|
||||||
|
}
|
||||||
|
fread(&f,sizeof(f),1,D); /* test float */
|
||||||
|
if (f!=tf)
|
||||||
|
{
|
||||||
|
Byte* p=(Byte*)&f; /* TODO: need union? */
|
||||||
|
Byte t;
|
||||||
|
swapfloat=1;
|
||||||
|
t=p[0]; p[0]=p[3]; p[3]=t;
|
||||||
|
t=p[1]; p[1]=p[2]; p[2]=t;
|
||||||
|
if (f!=tf) /* TODO: try another perm? */
|
||||||
|
lua_error("different float representation");
|
||||||
|
else
|
||||||
|
warn("different byte order in floats");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void LoadChunk(FILE* D)
|
||||||
|
{
|
||||||
|
LoadHeader(D);
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
int c=getc(D);
|
||||||
|
if (c==ID_FUN) LoadFunction(D); else { ungetc(c,D); break; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** load one chunk from a file.
|
||||||
|
** return list of functions found, headed by main, or NULL at EOF.
|
||||||
|
*/
|
||||||
|
TFunc* luaI_undump1(FILE* D)
|
||||||
|
{
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
int c=getc(D);
|
||||||
|
if (c==ID_CHUNK)
|
||||||
|
{
|
||||||
|
LoadChunk(D);
|
||||||
|
return Main;
|
||||||
|
}
|
||||||
|
else if (c==EOF)
|
||||||
|
return NULL;
|
||||||
|
else
|
||||||
|
lua_error("not a lua binary file");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** load and run all chunks in a file
|
||||||
|
*/
|
||||||
|
int luaI_undump(FILE* D)
|
||||||
|
{
|
||||||
|
TFunc* m;
|
||||||
|
while ((m=luaI_undump1(D)))
|
||||||
|
{
|
||||||
|
int status=luaI_dorun(m);
|
||||||
|
luaI_freefunc(m);
|
||||||
|
if (status!=0) return status;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
23
undump.h
Normal file
23
undump.h
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
/*
|
||||||
|
** undump.h
|
||||||
|
** definitions for lua decompiler
|
||||||
|
** $Id: undump.h,v 1.1 1996/03/08 21:43:21 lhf Exp lhf $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "func.h"
|
||||||
|
|
||||||
|
#define IsMain(f) (f->lineDefined==0)
|
||||||
|
|
||||||
|
/* definitions for chunk headers */
|
||||||
|
|
||||||
|
#define ID_CHUNK 27 /* ESC */
|
||||||
|
#define ID_FUN 'F'
|
||||||
|
#define ID_VAR 'V'
|
||||||
|
#define ID_STR 'S'
|
||||||
|
#define SIGNATURE "Lua"
|
||||||
|
#define VERSION 0x23 /* 2.3 */
|
||||||
|
#define TEST_WORD 0x1234 /* a word for testing byte ordering */
|
||||||
|
#define TEST_FLOAT 0.123456789e-23 /* a float for testing representation */
|
||||||
|
|
||||||
|
TFunc* luaI_undump1(FILE* D); /* load one chunk */
|
||||||
|
int luaI_undump(FILE* D); /* load all chunks */
|
||||||
Reference in New Issue
Block a user