mirror of
https://github.com/lua/lua.git
synced 2026-07-29 01:19:06 +00:00
Compare commits
161 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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 |
171
fallback.c
Normal file
171
fallback.c
Normal file
@@ -0,0 +1,171 @@
|
|||||||
|
/*
|
||||||
|
** fallback.c
|
||||||
|
** TecCGraf - PUC-Rio
|
||||||
|
*/
|
||||||
|
|
||||||
|
char *rcs_fallback="$Id: fallback.c,v 1.10 1994/12/20 21:20:36 roberto Exp $";
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "mem.h"
|
||||||
|
#include "fallback.h"
|
||||||
|
#include "opcode.h"
|
||||||
|
#include "inout.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}},
|
||||||
|
{"index", {LUA_T_CFUNCTION, indexFB}},
|
||||||
|
{"gettable", {LUA_T_CFUNCTION, gettableFB}},
|
||||||
|
{"arith", {LUA_T_CFUNCTION, arithFB}},
|
||||||
|
{"order", {LUA_T_CFUNCTION, orderFB}},
|
||||||
|
{"concat", {LUA_T_CFUNCTION, concatFB}},
|
||||||
|
{"settable", {LUA_T_CFUNCTION, gettableFB}},
|
||||||
|
{"gc", {LUA_T_CFUNCTION, GDFB}},
|
||||||
|
{"function", {LUA_T_CFUNCTION, funcFB}}
|
||||||
|
};
|
||||||
|
|
||||||
|
#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_iscfunction(func)))
|
||||||
|
{
|
||||||
|
lua_pushnil();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
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_pushnil();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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_reportbug("indexed expression not a table");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void arithFB (void)
|
||||||
|
{
|
||||||
|
lua_reportbug("unexpected type at conversion to number");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void concatFB (void)
|
||||||
|
{
|
||||||
|
lua_reportbug("unexpected type at conversion to string");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void orderFB (void)
|
||||||
|
{
|
||||||
|
lua_reportbug("unexpected type at comparison");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void GDFB (void) { }
|
||||||
|
|
||||||
|
static void funcFB (void)
|
||||||
|
{
|
||||||
|
lua_reportbug("call expression not a function");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Lock routines
|
||||||
|
*/
|
||||||
|
|
||||||
|
static Object *lockArray = NULL;
|
||||||
|
static Word lockSize = 0;
|
||||||
|
|
||||||
|
int luaI_lock (Object *object)
|
||||||
|
{
|
||||||
|
Word i;
|
||||||
|
Word oldSize;
|
||||||
|
if (tag(object) == LUA_T_NIL)
|
||||||
|
return -1;
|
||||||
|
for (i=0; i<lockSize; i++)
|
||||||
|
if (tag(&lockArray[i]) == LUA_T_NIL)
|
||||||
|
{
|
||||||
|
lockArray[i] = *object;
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
/* no more empty spaces */
|
||||||
|
oldSize = lockSize;
|
||||||
|
if (lockArray == NULL)
|
||||||
|
{
|
||||||
|
lockSize = 10;
|
||||||
|
lockArray = newvector(lockSize, Object);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
lockSize = 3*oldSize/2 + 5;
|
||||||
|
lockArray = growvector(lockArray, lockSize, Object);
|
||||||
|
}
|
||||||
|
for (i=oldSize; i<lockSize; i++)
|
||||||
|
tag(&lockArray[i]) = LUA_T_NIL;
|
||||||
|
lockArray[oldSize] = *object;
|
||||||
|
return oldSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void lua_unlock (int ref)
|
||||||
|
{
|
||||||
|
tag(&lockArray[ref]) = LUA_T_NIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Object *luaI_getlocked (int ref)
|
||||||
|
{
|
||||||
|
return &lockArray[ref];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaI_travlock (void (*fn)(Object *))
|
||||||
|
{
|
||||||
|
Word i;
|
||||||
|
for (i=0; i<lockSize; i++)
|
||||||
|
fn(&lockArray[i]);
|
||||||
|
}
|
||||||
|
|
||||||
31
fallback.h
Normal file
31
fallback.h
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
** $Id: fallback.h,v 1.6 1994/11/21 13:30:15 roberto Exp roberto $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef fallback_h
|
||||||
|
#define fallback_h
|
||||||
|
|
||||||
|
#include "opcode.h"
|
||||||
|
|
||||||
|
extern struct FB {
|
||||||
|
char *kind;
|
||||||
|
Object function;
|
||||||
|
} 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
|
||||||
|
|
||||||
|
void luaI_setfallback (void);
|
||||||
|
int luaI_lock (Object *object);
|
||||||
|
Object *luaI_getlocked (int ref);
|
||||||
|
void luaI_travlock (void (*fn)(Object *));
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
423
hash.c
423
hash.c
@@ -1,135 +1,147 @@
|
|||||||
/*
|
/*
|
||||||
** 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.23 1995/01/12 14:19:04 roberto Exp $";
|
||||||
|
|
||||||
#include <string.h>
|
#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 "inout.h"
|
||||||
#include "table.h"
|
#include "table.h"
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#define streq(s1,s2) (strcmp(s1,s2)==0)
|
#define streq(s1,s2) (s1 == s2 || (*(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 */
|
||||||
|
|
||||||
|
static Word 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_reportbug ("unexpected type to index table");
|
||||||
|
return -1; /* UNREACHEABLE */
|
||||||
|
case LUA_T_NUMBER:
|
||||||
|
return (((Word)nvalue(ref))%nhash(t));
|
||||||
|
case LUA_T_STRING:
|
||||||
{
|
{
|
||||||
h <<= 8;
|
unsigned long h = tsvalue(ref)->hash;
|
||||||
h += (unsigned char) *name; /* avoid sign extension */
|
if (h == 0)
|
||||||
h %= nhash(t); /* make it a valid index */
|
{
|
||||||
|
char *name = svalue(ref);
|
||||||
|
while (*name)
|
||||||
|
h = ((h<<5)-h)^(unsigned char)*(name++);
|
||||||
|
tsvalue(ref)->hash = h;
|
||||||
|
}
|
||||||
|
return (Word)h%nhash(t); /* make it a valid index */
|
||||||
}
|
}
|
||||||
return h;
|
case LUA_T_FUNCTION:
|
||||||
}
|
return (((IntPoint)bvalue(ref))%nhash(t));
|
||||||
else
|
case LUA_T_CFUNCTION:
|
||||||
{
|
return (((IntPoint)fvalue(ref))%nhash(t));
|
||||||
lua_reportbug ("unexpected type to index table");
|
case LUA_T_ARRAY:
|
||||||
return -1;
|
return (((IntPoint)avalue(ref))%nhash(t));
|
||||||
|
default: /* user data */
|
||||||
|
return (((IntPoint)uvalue(ref))%nhash(t));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static Node *present(Hash *t, Object *ref, int h)
|
Bool lua_equalObj (Object *t1, Object *t2)
|
||||||
{
|
{
|
||||||
Node *n=NULL, *p;
|
if (tag(t1) != tag(t2)) return 0;
|
||||||
if (tag(ref) == T_NUMBER)
|
switch (tag(t1))
|
||||||
{
|
{
|
||||||
for (p=NULL,n=list(t,h); n!=NULL; p=n, n=n->next)
|
case LUA_T_NIL: return 1;
|
||||||
if (ref_tag(n) == T_NUMBER && nvalue(ref) == ref_nvalue(n)) break;
|
case LUA_T_NUMBER: return nvalue(t1) == nvalue(t2);
|
||||||
}
|
case LUA_T_STRING: return streq(svalue(t1), svalue(t2));
|
||||||
else if (tag(ref) == T_STRING)
|
case LUA_T_ARRAY: return avalue(t1) == avalue(t2);
|
||||||
{
|
case LUA_T_FUNCTION: return bvalue(t1) == bvalue(t2);
|
||||||
for (p=NULL,n=list(t,h); n!=NULL; p=n, n=n->next)
|
case LUA_T_CFUNCTION: return fvalue(t1) == fvalue(t2);
|
||||||
if (ref_tag(n) == T_STRING && streq(svalue(ref),ref_svalue(n))) break;
|
default: return uvalue(t1) == uvalue(t2);
|
||||||
}
|
}
|
||||||
if (n==NULL) /* name not present */
|
|
||||||
return NULL;
|
|
||||||
#if 0
|
|
||||||
if (p!=NULL) /* name present but not first */
|
|
||||||
{
|
|
||||||
p->next=n->next; /* move-to-front self-organization */
|
|
||||||
n->next=list(t,h);
|
|
||||||
list(t,h)=n;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
return n;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void freelist (Node *n)
|
static Word present (Hash *t, Object *ref)
|
||||||
{
|
{
|
||||||
while (n)
|
Word h = hashindex(t, ref);
|
||||||
|
while (tag(ref(node(t, h))) != LUA_T_NIL)
|
||||||
{
|
{
|
||||||
Node *next = n->next;
|
if (lua_equalObj(ref, ref(node(t, h))))
|
||||||
free (n);
|
return h;
|
||||||
n = next;
|
h = (h+1) % nhash(t);
|
||||||
}
|
}
|
||||||
|
return h;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Alloc a vector node
|
||||||
|
*/
|
||||||
|
static Node *hashnodecreate (Word nhash)
|
||||||
|
{
|
||||||
|
Word i;
|
||||||
|
Node *v = newvector (nhash, Node);
|
||||||
|
for (i=0; i<nhash; i++)
|
||||||
|
tag(ref(&v[i])) = LUA_T_NIL;
|
||||||
|
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 = 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 +152,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);
|
||||||
@@ -154,91 +166,124 @@ 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) = 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 +293,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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
17
hash.h
17
hash.h
@@ -2,30 +2,35 @@
|
|||||||
** hash.h
|
** hash.h
|
||||||
** hash manager for lua
|
** hash manager for lua
|
||||||
** Luiz Henrique de Figueiredo - 17 Aug 90
|
** Luiz Henrique de Figueiredo - 17 Aug 90
|
||||||
** $Id: hash.h,v 1.1 1993/12/17 18:41:19 celes Exp celes $
|
** $Id: hash.h,v 2.7 1994/12/20 21:20:36 roberto Exp roberto $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef hash_h
|
#ifndef hash_h
|
||||||
#define hash_h
|
#define hash_h
|
||||||
|
|
||||||
|
#include "types.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);
|
Bool lua_equalObj (Object *t1, Object *t2);
|
||||||
|
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);
|
||||||
|
|
||||||
|
|||||||
238
inout.c
238
inout.c
@@ -2,50 +2,54 @@
|
|||||||
** 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.15 1994/12/16 15:55:04 roberto Exp roberto $";
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "mem.h"
|
||||||
#include "opcode.h"
|
#include "opcode.h"
|
||||||
#include "hash.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"
|
||||||
|
|
||||||
/* Exported variables */
|
/* Exported variables */
|
||||||
int lua_linenumber;
|
Word lua_linenumber;
|
||||||
int lua_debug;
|
Bool lua_debug;
|
||||||
int lua_debugline;
|
Word lua_debugline = 0;
|
||||||
|
|
||||||
|
|
||||||
/* Internal variables */
|
/* Internal variables */
|
||||||
|
|
||||||
#ifndef MAXFUNCSTACK
|
#ifndef MAXFUNCSTACK
|
||||||
#define MAXFUNCSTACK 32
|
#define MAXFUNCSTACK 100
|
||||||
#endif
|
#endif
|
||||||
static struct { int file; int function; } funcstack[MAXFUNCSTACK];
|
|
||||||
static int nfuncstack=0;
|
typedef struct FuncStackNode {
|
||||||
|
struct FuncStackNode *next;
|
||||||
|
char *file;
|
||||||
|
Word function;
|
||||||
|
Word line;
|
||||||
|
} FuncStackNode;
|
||||||
|
|
||||||
|
static FuncStackNode *funcStack = NULL;
|
||||||
|
static Word 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 +57,25 @@ 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 0 on success or error message on error.
|
||||||
*/
|
*/
|
||||||
int lua_openfile (char *fn)
|
char *lua_openfile (char *fn)
|
||||||
{
|
{
|
||||||
lua_linenumber = 1;
|
lua_linenumber = 1;
|
||||||
lua_setinput (fileinput);
|
lua_setinput (fileinput);
|
||||||
fp = fopen (fn, "r");
|
fp = fopen (fn, "r");
|
||||||
if (fp == NULL) return 1;
|
if (fp == NULL)
|
||||||
if (lua_addfile (fn)) return 1;
|
{
|
||||||
return 0;
|
static char buff[32];
|
||||||
|
sprintf(buff, "unable to open file %.10s", fn);
|
||||||
|
return buff;
|
||||||
|
}
|
||||||
|
return lua_addfile (fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -87,7 +94,7 @@ 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)
|
char *lua_openstring (char *s)
|
||||||
{
|
{
|
||||||
lua_linenumber = 1;
|
lua_linenumber = 1;
|
||||||
lua_setinput (stringinput);
|
lua_setinput (stringinput);
|
||||||
@@ -95,9 +102,8 @@ int lua_openstring (char *s)
|
|||||||
{
|
{
|
||||||
char sn[64];
|
char sn[64];
|
||||||
sprintf (sn, "String: %10.10s...", s);
|
sprintf (sn, "String: %10.10s...", s);
|
||||||
if (lua_addfile (sn)) return 1;
|
return lua_addfile (sn);
|
||||||
}
|
}
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -108,40 +114,38 @@ void lua_closestring (void)
|
|||||||
lua_delfile();
|
lua_delfile();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
** Call user function to handle error messages, if registred. Or report error
|
|
||||||
** using standard function (fprintf).
|
|
||||||
*/
|
|
||||||
void lua_error (char *s)
|
|
||||||
{
|
|
||||||
if (usererror != NULL) usererror (s);
|
|
||||||
else fprintf (stderr, "lua: %s\n", s);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Called to execute SETFUNCTION opcode, this function pushs a function into
|
** Called to execute SETFUNCTION opcode, this function pushs a function into
|
||||||
** function stack. Return 0 on success or 1 on error.
|
** function stack.
|
||||||
*/
|
*/
|
||||||
int lua_pushfunction (int file, int function)
|
void lua_pushfunction (char *file, Word function)
|
||||||
{
|
{
|
||||||
if (nfuncstack >= MAXFUNCSTACK-1)
|
FuncStackNode *newNode;
|
||||||
|
if (nfuncstack++ >= MAXFUNCSTACK)
|
||||||
{
|
{
|
||||||
lua_error ("function stack overflow");
|
lua_reportbug("function stack overflow");
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
funcstack[nfuncstack].file = file;
|
newNode = new(FuncStackNode);
|
||||||
funcstack[nfuncstack].function = function;
|
newNode->function = function;
|
||||||
nfuncstack++;
|
newNode->file = file;
|
||||||
return 0;
|
newNode->line= lua_debugline;
|
||||||
|
newNode->next = funcStack;
|
||||||
|
funcStack = newNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Called to execute RESET opcode, this function pops a function from
|
** Called to execute RESET opcode, this function pops a function from
|
||||||
** function stack.
|
** function stack.
|
||||||
*/
|
*/
|
||||||
void lua_popfunction (void)
|
void lua_popfunction (void)
|
||||||
{
|
{
|
||||||
nfuncstack--;
|
FuncStackNode *temp = funcStack;
|
||||||
|
if (temp == NULL) return;
|
||||||
|
--nfuncstack;
|
||||||
|
lua_debugline = temp->line;
|
||||||
|
funcStack = temp->next;
|
||||||
|
luaI_free(temp);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -149,30 +153,142 @@ void lua_popfunction (void)
|
|||||||
*/
|
*/
|
||||||
void lua_reportbug (char *s)
|
void lua_reportbug (char *s)
|
||||||
{
|
{
|
||||||
char msg[1024];
|
char msg[MAXFUNCSTACK*80];
|
||||||
strcpy (msg, s);
|
strcpy (msg, s);
|
||||||
if (lua_debugline != 0)
|
if (lua_debugline != 0)
|
||||||
{
|
{
|
||||||
int i;
|
if (funcStack)
|
||||||
if (nfuncstack > 0)
|
|
||||||
{
|
{
|
||||||
sprintf (strchr(msg,0),
|
FuncStackNode *func = funcStack;
|
||||||
"\n\tin statement begining at line %d in function \"%s\" of file \"%s\"",
|
int line = lua_debugline;
|
||||||
lua_debugline, lua_varname(funcstack[nfuncstack-1].function),
|
sprintf (strchr(msg,0), "\n\tactive stack:\n");
|
||||||
lua_file[funcstack[nfuncstack-1].file]);
|
do
|
||||||
sprintf (strchr(msg,0), "\n\tactive stack\n");
|
{
|
||||||
for (i=nfuncstack-1; i>=0; i--)
|
sprintf (strchr(msg,0),
|
||||||
sprintf (strchr(msg,0), "\t-> function \"%s\" of file \"%s\"\n",
|
"\t-> function \"%s\" at file \"%s\":%u\n",
|
||||||
lua_varname(funcstack[i].function),
|
lua_constant[func->function]->str, func->file, line);
|
||||||
lua_file[funcstack[i].file]);
|
line = func->line;
|
||||||
|
func = func->next;
|
||||||
|
lua_popfunction();
|
||||||
|
} while (func);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
sprintf (strchr(msg,0),
|
sprintf (strchr(msg,0),
|
||||||
"\n\tin statement begining at line %d of file \"%s\"",
|
"\n\tin statement begining at line %u of file \"%s\"",
|
||||||
lua_debugline, lua_filename());
|
lua_debugline, lua_filename());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lua_error (msg);
|
lua_error (msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Internal function: do a file
|
||||||
|
*/
|
||||||
|
void lua_internaldofile (void)
|
||||||
|
{
|
||||||
|
lua_Object obj = lua_getparam (1);
|
||||||
|
if (lua_isstring(obj) && !lua_dofile(lua_getstring(obj)))
|
||||||
|
lua_pushnumber(1);
|
||||||
|
else
|
||||||
|
lua_pushnil();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Internal function: print object values
|
||||||
|
*/
|
||||||
|
void lua_print (void)
|
||||||
|
{
|
||||||
|
int i=1;
|
||||||
|
lua_Object obj;
|
||||||
|
while ((obj=lua_getparam (i++)) != LUA_NOOBJECT)
|
||||||
|
{
|
||||||
|
if (lua_isnumber(obj)) printf("%g\n",lua_getnumber(obj));
|
||||||
|
else if (lua_isstring(obj)) printf("%s\n",lua_getstring(obj));
|
||||||
|
else if (lua_isfunction(obj)) printf("function: %p\n",bvalue(luaI_Address(obj)));
|
||||||
|
else if (lua_iscfunction(obj)) printf("cfunction: %p\n",lua_getcfunction(obj)
|
||||||
|
);
|
||||||
|
else if (lua_isuserdata(obj)) printf("userdata: %p\n",lua_getuserdata(obj));
|
||||||
|
else if (lua_istable(obj)) printf("table: %p\n",avalue(luaI_Address(obj)));
|
||||||
|
else if (lua_isnil(obj)) printf("nil\n");
|
||||||
|
else printf("invalid value to print\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Internal function: return an object type.
|
||||||
|
*/
|
||||||
|
void luaI_type (void)
|
||||||
|
{
|
||||||
|
lua_Object o = lua_getparam(1);
|
||||||
|
if (o == LUA_NOOBJECT)
|
||||||
|
lua_error("no parameter to function 'type'");
|
||||||
|
switch (lua_type(o))
|
||||||
|
{
|
||||||
|
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 :
|
||||||
|
lua_pushliteral("function");
|
||||||
|
break;
|
||||||
|
case LUA_T_CFUNCTION :
|
||||||
|
lua_pushliteral("cfunction");
|
||||||
|
break;
|
||||||
|
default :
|
||||||
|
lua_pushliteral("userdata");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Internal function: convert an object to a number
|
||||||
|
*/
|
||||||
|
void lua_obj2number (void)
|
||||||
|
{
|
||||||
|
lua_Object o = lua_getparam(1);
|
||||||
|
if (lua_isnumber(o))
|
||||||
|
lua_pushobject(o);
|
||||||
|
else if (lua_isstring(o))
|
||||||
|
{
|
||||||
|
char c;
|
||||||
|
float f;
|
||||||
|
if (sscanf(lua_getstring(o),"%f %c",&f,&c) == 1)
|
||||||
|
lua_pushnumber(f);
|
||||||
|
else
|
||||||
|
lua_pushnil();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
lua_pushnil();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaI_error (void)
|
||||||
|
{
|
||||||
|
char *s = lua_getstring(lua_getparam(1));
|
||||||
|
if (s == NULL) s = "(no message)";
|
||||||
|
lua_reportbug(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
23
inout.h
23
inout.h
@@ -1,21 +1,30 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: $
|
** $Id: inout.h,v 1.6 1994/11/21 21:41:09 roberto Stab roberto $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#ifndef inout_h
|
#ifndef inout_h
|
||||||
#define inout_h
|
#define inout_h
|
||||||
|
|
||||||
extern int lua_linenumber;
|
#include "types.h"
|
||||||
extern int lua_debug;
|
|
||||||
extern int lua_debugline;
|
|
||||||
|
|
||||||
int lua_openfile (char *fn);
|
extern Word lua_linenumber;
|
||||||
|
extern Bool lua_debug;
|
||||||
|
extern Word lua_debugline;
|
||||||
|
|
||||||
|
char *lua_openfile (char *fn);
|
||||||
void lua_closefile (void);
|
void lua_closefile (void);
|
||||||
int lua_openstring (char *s);
|
char *lua_openstring (char *s);
|
||||||
void lua_closestring (void);
|
void lua_closestring (void);
|
||||||
int lua_pushfunction (int file, int function);
|
void lua_pushfunction (char *file, Word function);
|
||||||
void lua_popfunction (void);
|
void lua_popfunction (void);
|
||||||
void lua_reportbug (char *s);
|
void lua_reportbug (char *s);
|
||||||
|
|
||||||
|
void lua_internaldofile (void);
|
||||||
|
void lua_internaldostring (void);
|
||||||
|
void lua_print (void);
|
||||||
|
void luaI_type (void);
|
||||||
|
void lua_obj2number (void);
|
||||||
|
void luaI_error (void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
220
iolib.c
220
iolib.c
@@ -3,20 +3,18 @@
|
|||||||
** 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.20 1995/02/02 18:54:58 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 "mm.h"
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
#include "lualib.h"
|
||||||
|
|
||||||
static FILE *in=stdin, *out=stdout;
|
static FILE *in=stdin, *out=stdout;
|
||||||
|
|
||||||
@@ -31,7 +29,7 @@ static FILE *in=stdin, *out=stdout;
|
|||||||
static void io_readfrom (void)
|
static void io_readfrom (void)
|
||||||
{
|
{
|
||||||
lua_Object o = lua_getparam (1);
|
lua_Object o = lua_getparam (1);
|
||||||
if (o == NULL) /* restore standart input */
|
if (o == LUA_NOOBJECT) /* restore standart input */
|
||||||
{
|
{
|
||||||
if (in != stdin)
|
if (in != stdin)
|
||||||
{
|
{
|
||||||
@@ -76,7 +74,7 @@ static void io_readfrom (void)
|
|||||||
static void io_writeto (void)
|
static void io_writeto (void)
|
||||||
{
|
{
|
||||||
lua_Object o = lua_getparam (1);
|
lua_Object o = lua_getparam (1);
|
||||||
if (o == NULL) /* restore standart output */
|
if (o == LUA_NOOBJECT) /* restore standart output */
|
||||||
{
|
{
|
||||||
if (out != stdout)
|
if (out != stdout)
|
||||||
{
|
{
|
||||||
@@ -122,7 +120,7 @@ static void io_writeto (void)
|
|||||||
static void io_appendto (void)
|
static void io_appendto (void)
|
||||||
{
|
{
|
||||||
lua_Object o = lua_getparam (1);
|
lua_Object o = lua_getparam (1);
|
||||||
if (o == NULL) /* restore standart output */
|
if (o == LUA_NOOBJECT) /* restore standart output */
|
||||||
{
|
{
|
||||||
if (out != stdout)
|
if (out != stdout)
|
||||||
{
|
{
|
||||||
@@ -179,7 +177,7 @@ static void io_appendto (void)
|
|||||||
static void io_read (void)
|
static void io_read (void)
|
||||||
{
|
{
|
||||||
lua_Object o = lua_getparam (1);
|
lua_Object o = lua_getparam (1);
|
||||||
if (o == NULL || !lua_isstring(o)) /* free format */
|
if (o == LUA_NOOBJECT || !lua_isstring(o)) /* free format */
|
||||||
{
|
{
|
||||||
int c;
|
int c;
|
||||||
char s[256];
|
char s[256];
|
||||||
@@ -187,7 +185,7 @@ static void io_read (void)
|
|||||||
;
|
;
|
||||||
if (c == '\"')
|
if (c == '\"')
|
||||||
{
|
{
|
||||||
int c, n=0;
|
int n=0;
|
||||||
while((c = fgetc(in)) != '\"')
|
while((c = fgetc(in)) != '\"')
|
||||||
{
|
{
|
||||||
if (c == EOF)
|
if (c == EOF)
|
||||||
@@ -201,7 +199,7 @@ static void io_read (void)
|
|||||||
}
|
}
|
||||||
else if (c == '\'')
|
else if (c == '\'')
|
||||||
{
|
{
|
||||||
int c, n=0;
|
int n=0;
|
||||||
while((c = fgetc(in)) != '\'')
|
while((c = fgetc(in)) != '\'')
|
||||||
{
|
{
|
||||||
if (c == EOF)
|
if (c == EOF)
|
||||||
@@ -215,7 +213,6 @@ static void io_read (void)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
char *ptr;
|
|
||||||
double d;
|
double d;
|
||||||
ungetc (c, in);
|
ungetc (c, in);
|
||||||
if (fscanf (in, "%s", s) != 1)
|
if (fscanf (in, "%s", s) != 1)
|
||||||
@@ -223,8 +220,7 @@ static void io_read (void)
|
|||||||
lua_pushnil ();
|
lua_pushnil ();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
d = strtod (s, &ptr);
|
if (sscanf(s, "%lf %*c", &d) == 1)
|
||||||
if (!(*ptr))
|
|
||||||
{
|
{
|
||||||
lua_pushnumber (d);
|
lua_pushnumber (d);
|
||||||
return;
|
return;
|
||||||
@@ -269,9 +265,9 @@ static void io_read (void)
|
|||||||
break;
|
break;
|
||||||
case 'f': case 'g': case 'e':
|
case 'f': case 'g': case 'e':
|
||||||
{
|
{
|
||||||
float f;
|
float fl;
|
||||||
sscanf (s, "%f", &f);
|
sscanf (s, "%f", &fl);
|
||||||
lua_pushnumber(f);
|
lua_pushnumber(fl);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@@ -313,6 +309,38 @@ static void io_read (void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Read characters until a given one. The delimiter is not read.
|
||||||
|
*/
|
||||||
|
static void io_readuntil (void)
|
||||||
|
{
|
||||||
|
int n=255,m=0;
|
||||||
|
int c,d;
|
||||||
|
char *s;
|
||||||
|
lua_Object lo = lua_getparam(1);
|
||||||
|
if (!lua_isstring(lo))
|
||||||
|
d = EOF;
|
||||||
|
else
|
||||||
|
d = *lua_getstring(lo);
|
||||||
|
|
||||||
|
s = (char *)malloc(n+1);
|
||||||
|
while((c = fgetc(in)) != EOF && c != d)
|
||||||
|
{
|
||||||
|
if (m==n)
|
||||||
|
{
|
||||||
|
n *= 2;
|
||||||
|
s = (char *)realloc(s, n+1);
|
||||||
|
}
|
||||||
|
s[m++] = c;
|
||||||
|
}
|
||||||
|
if (c != EOF) ungetc(c,in);
|
||||||
|
s[m] = 0;
|
||||||
|
lua_pushstring(s);
|
||||||
|
free(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Write a variable. On error put 0 on stack, otherwise put 1.
|
** Write a variable. On error put 0 on stack, otherwise put 1.
|
||||||
** LUA interface:
|
** LUA interface:
|
||||||
@@ -341,39 +369,49 @@ static void io_read (void)
|
|||||||
*/
|
*/
|
||||||
static char *buildformat (char *e, lua_Object o)
|
static char *buildformat (char *e, lua_Object o)
|
||||||
{
|
{
|
||||||
static char buffer[512];
|
static char buffer[2048];
|
||||||
static char f[80];
|
static char f[80];
|
||||||
char *string = &buffer[255];
|
char *string = &buffer[255];
|
||||||
|
char *fstart=e, *fspace, *send;
|
||||||
char t, j='r';
|
char t, j='r';
|
||||||
int m=0, n=0, l;
|
int m=0, n=-1, l;
|
||||||
while (isspace(*e)) e++;
|
while (isspace(*e)) e++;
|
||||||
|
fspace = e;
|
||||||
t = *e++;
|
t = *e++;
|
||||||
if (*e == '<' || *e == '|' || *e == '>') j = *e++;
|
if (*e == '<' || *e == '|' || *e == '>') j = *e++;
|
||||||
while (isdigit(*e))
|
while (isdigit(*e))
|
||||||
m = m*10 + (*e++ - '0');
|
m = m*10 + (*e++ - '0');
|
||||||
e++; /* skip point */
|
if (*e == '.') e++; /* skip point */
|
||||||
while (isdigit(*e))
|
while (isdigit(*e))
|
||||||
n = n*10 + (*e++ - '0');
|
if (n < 0) n = (*e++ - '0');
|
||||||
|
else n = n*10 + (*e++ - '0');
|
||||||
|
|
||||||
sprintf(f,"%%");
|
sprintf(f,"%%");
|
||||||
if (j == '<' || j == '|') sprintf(strchr(f,0),"-");
|
if (j == '<' || j == '|') sprintf(strchr(f,0),"-");
|
||||||
if (m != 0) sprintf(strchr(f,0),"%d", m);
|
if (m > 0) sprintf(strchr(f,0),"%d", m);
|
||||||
if (n != 0) sprintf(strchr(f,0),".%d", n);
|
if (n >= 0) sprintf(strchr(f,0),".%d", n);
|
||||||
sprintf(strchr(f,0), "%c", t);
|
switch (t)
|
||||||
switch (tolower(t))
|
|
||||||
{
|
{
|
||||||
case 'i': t = 'i';
|
case 'i': case 'I': t = 'd';
|
||||||
|
sprintf(strchr(f,0), "%c", t);
|
||||||
sprintf (string, f, (long int)lua_getnumber(o));
|
sprintf (string, f, (long int)lua_getnumber(o));
|
||||||
break;
|
break;
|
||||||
case 'f': case 'g': case 'e': t = 'f';
|
case 'f': case 'g': case 'e': case 'G': case 'E':
|
||||||
|
sprintf(strchr(f,0), "%c", t);
|
||||||
sprintf (string, f, (float)lua_getnumber(o));
|
sprintf (string, f, (float)lua_getnumber(o));
|
||||||
break;
|
break;
|
||||||
case 's': t = 's';
|
case 'F': t = 'f';
|
||||||
|
sprintf(strchr(f,0), "%c", t);
|
||||||
|
sprintf (string, f, (float)lua_getnumber(o));
|
||||||
|
break;
|
||||||
|
case 's': case 'S': t = 's';
|
||||||
|
sprintf(strchr(f,0), "%c", t);
|
||||||
sprintf (string, f, lua_getstring(o));
|
sprintf (string, f, lua_getstring(o));
|
||||||
break;
|
break;
|
||||||
default: return "";
|
default: return "";
|
||||||
}
|
}
|
||||||
l = strlen(string);
|
l = strlen(string);
|
||||||
|
send = string+l;
|
||||||
if (m!=0 && l>m)
|
if (m!=0 && l>m)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
@@ -383,25 +421,34 @@ static char *buildformat (char *e, lua_Object o)
|
|||||||
}
|
}
|
||||||
else if (m!=0 && j=='|')
|
else if (m!=0 && j=='|')
|
||||||
{
|
{
|
||||||
|
int k;
|
||||||
int i=l-1;
|
int i=l-1;
|
||||||
while (isspace(string[i])) i--;
|
while (isspace(string[i]) || string[i]==0) i--;
|
||||||
string -= (m-i) / 2;
|
string -= (m-i)/2;
|
||||||
i=0;
|
for(k=0; k<(m-i)/2; k++)
|
||||||
while (string[i]==0) string[i++] = ' ';
|
string[k] = ' ';
|
||||||
string[l] = 0;
|
|
||||||
}
|
}
|
||||||
|
/* add space characteres */
|
||||||
|
while (fspace != fstart)
|
||||||
|
{
|
||||||
|
string--;
|
||||||
|
fspace--;
|
||||||
|
*string = *fspace;
|
||||||
|
}
|
||||||
|
while (isspace(*e)) *send++ = *e++;
|
||||||
|
*send = 0;
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
static void io_write (void)
|
static void io_write (void)
|
||||||
{
|
{
|
||||||
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 (o1 == NULL) /* new line */
|
if (o1 == LUA_NOOBJECT) /* new line */
|
||||||
{
|
{
|
||||||
fprintf (out, "\n");
|
fprintf (out, "\n");
|
||||||
lua_pushnumber(1);
|
lua_pushnumber(1);
|
||||||
}
|
}
|
||||||
else if (o2 == NULL) /* free format */
|
else if (o2 == LUA_NOOBJECT) /* free format */
|
||||||
{
|
{
|
||||||
int status=0;
|
int status=0;
|
||||||
if (lua_isnumber(o1))
|
if (lua_isnumber(o1))
|
||||||
@@ -426,10 +473,10 @@ static void io_write (void)
|
|||||||
** 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_Object o = lua_getparam (1);
|
||||||
if (o == NULL || !lua_isstring (o))
|
if (o == LUA_NOOBJECT || !lua_isstring (o))
|
||||||
{
|
{
|
||||||
lua_error ("incorrect argument to function 'execute`");
|
lua_error ("incorrect argument to function 'execute`");
|
||||||
lua_pushnumber (0);
|
lua_pushnumber (0);
|
||||||
@@ -446,10 +493,10 @@ void io_execute (void)
|
|||||||
** Remove a file.
|
** Remove a file.
|
||||||
** On error put 0 on stack, otherwise put 1.
|
** On error put 0 on stack, otherwise put 1.
|
||||||
*/
|
*/
|
||||||
void io_remove (void)
|
static void io_remove (void)
|
||||||
{
|
{
|
||||||
lua_Object o = lua_getparam (1);
|
lua_Object o = lua_getparam (1);
|
||||||
if (o == NULL || !lua_isstring (o))
|
if (o == LUA_NOOBJECT || !lua_isstring (o))
|
||||||
{
|
{
|
||||||
lua_error ("incorrect argument to function 'execute`");
|
lua_error ("incorrect argument to function 'execute`");
|
||||||
lua_pushnumber (0);
|
lua_pushnumber (0);
|
||||||
@@ -464,6 +511,88 @@ void io_remove (void)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** To get a environment variables
|
||||||
|
*/
|
||||||
|
static void io_getenv (void)
|
||||||
|
{
|
||||||
|
lua_Object s = lua_getparam(1);
|
||||||
|
if (!lua_isstring(s))
|
||||||
|
lua_pushnil();
|
||||||
|
else
|
||||||
|
{
|
||||||
|
char *env = getenv(lua_getstring(s));
|
||||||
|
if (env == NULL) lua_pushnil();
|
||||||
|
else lua_pushstring(env);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Return time: hour, min, sec
|
||||||
|
*/
|
||||||
|
static void io_time (void)
|
||||||
|
{
|
||||||
|
time_t t;
|
||||||
|
struct tm *s;
|
||||||
|
|
||||||
|
time(&t);
|
||||||
|
s = localtime(&t);
|
||||||
|
lua_pushnumber(s->tm_hour);
|
||||||
|
lua_pushnumber(s->tm_min);
|
||||||
|
lua_pushnumber(s->tm_sec);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Return date: dd, mm, yyyy
|
||||||
|
*/
|
||||||
|
static void io_date (void)
|
||||||
|
{
|
||||||
|
time_t t;
|
||||||
|
struct tm *s;
|
||||||
|
|
||||||
|
time(&t);
|
||||||
|
s = localtime(&t);
|
||||||
|
lua_pushnumber(s->tm_mday);
|
||||||
|
lua_pushnumber(s->tm_mon+1);
|
||||||
|
lua_pushnumber(s->tm_year+1900);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Beep
|
||||||
|
*/
|
||||||
|
static void io_beep (void)
|
||||||
|
{
|
||||||
|
printf("\a");
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** To exit
|
||||||
|
*/
|
||||||
|
static void io_exit (void)
|
||||||
|
{
|
||||||
|
lua_Object o = lua_getparam(1);
|
||||||
|
if (lua_isstring(o))
|
||||||
|
printf("%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 (gets(buffer) == 0) return;
|
||||||
|
if (strcmp(buffer, "cont") == 0) return;
|
||||||
|
lua_dostring(buffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Open io library
|
** Open io library
|
||||||
*/
|
*/
|
||||||
@@ -473,7 +602,14 @@ 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 ("getenv", io_getenv);
|
||||||
|
lua_register ("time", io_time);
|
||||||
|
lua_register ("date", io_date);
|
||||||
|
lua_register ("beep", io_beep);
|
||||||
|
lua_register ("exit", io_exit);
|
||||||
|
lua_register ("debug", io_debug);
|
||||||
}
|
}
|
||||||
|
|||||||
114
lex.c
114
lex.c
@@ -1,25 +1,20 @@
|
|||||||
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.13 1994/12/20 21:20:36 roberto Exp celes $";
|
||||||
/*$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 <math.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "opcode.h"
|
#include "tree.h"
|
||||||
#include "hash.h"
|
|
||||||
#include "inout.h"
|
|
||||||
#include "table.h"
|
#include "table.h"
|
||||||
#include "y.tab.h"
|
#include "opcode.h"
|
||||||
|
#include "inout.h"
|
||||||
|
#include "parser.h"
|
||||||
|
#include "ugly.h"
|
||||||
|
|
||||||
|
#define lua_strcmp(a,b) (a[0]<b[0]?(-1):(a[0]>b[0]?(1):strcmp(a,b)))
|
||||||
|
|
||||||
#define next() { current = input(); }
|
#define next() { current = input(); }
|
||||||
#define save(x) { *yytextLast++ = (x); }
|
#define save(x) { *yytextLast++ = (x); }
|
||||||
@@ -44,6 +39,7 @@ char *lua_lasttext (void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* The reserved words must be listed in lexicographic order */
|
||||||
static struct
|
static struct
|
||||||
{
|
{
|
||||||
char *name;
|
char *name;
|
||||||
@@ -66,17 +62,18 @@ 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)
|
static int findReserved (char *name)
|
||||||
{
|
{
|
||||||
int l = 0;
|
int l = 0;
|
||||||
int h = RESERVEDSIZE - 1;
|
int h = RESERVEDSIZE - 1;
|
||||||
while (l <= h)
|
while (l <= h)
|
||||||
{
|
{
|
||||||
int m = (l+h)/2;
|
int m = (l+h)/2;
|
||||||
int comp = strcmp(name, reserved[m].name);
|
int comp = lua_strcmp(name, reserved[m].name);
|
||||||
if (comp < 0)
|
if (comp < 0)
|
||||||
h = m-1;
|
h = m-1;
|
||||||
else if (comp == 0)
|
else if (comp == 0)
|
||||||
@@ -88,13 +85,20 @@ int findReserved (char *name)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int yylex ()
|
int yylex (void)
|
||||||
{
|
{
|
||||||
|
float a;
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
yytextLast = yytext;
|
yytextLast = yytext;
|
||||||
|
#if 0
|
||||||
|
fprintf(stderr,"'%c' %d\n",current,current);
|
||||||
|
#endif
|
||||||
switch (current)
|
switch (current)
|
||||||
{
|
{
|
||||||
|
case EOF:
|
||||||
|
case 0:
|
||||||
|
return 0;
|
||||||
case '\n': lua_linenumber++;
|
case '\n': lua_linenumber++;
|
||||||
case ' ':
|
case ' ':
|
||||||
case '\t':
|
case '\t':
|
||||||
@@ -106,12 +110,12 @@ int yylex ()
|
|||||||
while (isalnum(current) || current == '_')
|
while (isalnum(current) || current == '_')
|
||||||
save_and_next();
|
save_and_next();
|
||||||
*yytextLast = 0;
|
*yytextLast = 0;
|
||||||
if (strcmp(yytext, "debug") == 0)
|
if (lua_strcmp(yytext, "debug") == 0)
|
||||||
{
|
{
|
||||||
yylval.vInt = 1;
|
yylval.vInt = 1;
|
||||||
return DEBUG;
|
return DEBUG;
|
||||||
}
|
}
|
||||||
else if (strcmp(yytext, "nodebug") == 0)
|
else if (lua_strcmp(yytext, "nodebug") == 0)
|
||||||
{
|
{
|
||||||
yylval.vInt = 0;
|
yylval.vInt = 0;
|
||||||
return DEBUG;
|
return DEBUG;
|
||||||
@@ -124,6 +128,11 @@ int yylex ()
|
|||||||
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(); return EQ; }
|
||||||
|
|
||||||
case '<':
|
case '<':
|
||||||
save_and_next();
|
save_and_next();
|
||||||
if (current != '=') return '<';
|
if (current != '=') return '<';
|
||||||
@@ -148,6 +157,7 @@ int yylex ()
|
|||||||
{
|
{
|
||||||
switch (current)
|
switch (current)
|
||||||
{
|
{
|
||||||
|
case EOF:
|
||||||
case 0:
|
case 0:
|
||||||
case '\n':
|
case '\n':
|
||||||
return WRONGTOKEN;
|
return WRONGTOKEN;
|
||||||
@@ -158,7 +168,7 @@ 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;
|
default : save(current); next(); break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@@ -167,7 +177,7 @@ int yylex ()
|
|||||||
}
|
}
|
||||||
next(); /* skip the delimiter */
|
next(); /* skip the delimiter */
|
||||||
*yytextLast = 0;
|
*yytextLast = 0;
|
||||||
yylval.vWord = lua_findconstant (yytext);
|
yylval.vWord = luaI_findconstant(lua_constcreate(yytext));
|
||||||
return STRING;
|
return STRING;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -185,12 +195,12 @@ int yylex ()
|
|||||||
case 'Z':
|
case 'Z':
|
||||||
case '_':
|
case '_':
|
||||||
{
|
{
|
||||||
int res;
|
Word res;
|
||||||
do { save_and_next(); } while (isalnum(current) || current == '_');
|
do { save_and_next(); } while (isalnum(current) || current == '_');
|
||||||
*yytextLast = 0;
|
*yytextLast = 0;
|
||||||
res = findReserved(yytext);
|
res = findReserved(yytext);
|
||||||
if (res) return res;
|
if (res) return res;
|
||||||
yylval.pChar = yytext;
|
yylval.pNode = lua_constcreate(yytext);
|
||||||
return NAME;
|
return NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -203,31 +213,59 @@ int yylex ()
|
|||||||
}
|
}
|
||||||
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*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')
|
{ float 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;
|
||||||
|
float ea;
|
||||||
|
save_and_next();
|
||||||
|
neg=(current=='-');
|
||||||
|
if (current == '+' || current == '-') save_and_next();
|
||||||
|
if (!isdigit(current)) return WRONGTOKEN;
|
||||||
|
do { e=10*e+current-'0'; save_and_next(); } while (isdigit(current));
|
||||||
|
for (ea=neg?0.1:10.0; e>0; e>>=1)
|
||||||
|
{
|
||||||
|
if (e & 1) a*=ea;
|
||||||
|
ea*=ea;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
yylval.vFloat = a;
|
||||||
|
return NUMBER;
|
||||||
}
|
}
|
||||||
*yytextLast = 0;
|
|
||||||
yylval.vFloat = atof(yytext);
|
case U_and: case U_do: case U_else: case U_elseif: case U_end:
|
||||||
return NUMBER;
|
case U_function: case U_if: case U_local: case U_nil: case U_not:
|
||||||
|
case U_or: case U_repeat: case U_return: case U_then:
|
||||||
|
case U_until: case U_while:
|
||||||
|
{
|
||||||
|
int old = current;
|
||||||
|
next();
|
||||||
|
return reserved[old-U_and].token;
|
||||||
|
}
|
||||||
|
|
||||||
|
case U_eq: next(); return EQ;
|
||||||
|
case U_le: next(); return LE;
|
||||||
|
case U_ge: next(); return GE;
|
||||||
|
case U_ne: next(); return NE;
|
||||||
|
case U_sc: next(); return CONC;
|
||||||
|
|
||||||
default: /* also end of file */
|
default: /* also end of file */
|
||||||
{
|
{
|
||||||
save_and_next();
|
save_and_next();
|
||||||
return *yytext;
|
return yytext[0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
53
lua.c
53
lua.c
@@ -3,29 +3,72 @@
|
|||||||
** Linguagem para Usuarios de Aplicacao
|
** Linguagem para Usuarios de Aplicacao
|
||||||
*/
|
*/
|
||||||
|
|
||||||
char *rcs_lua="$Id: $";
|
char *rcs_lua="$Id: lua.c,v 1.3 1994/12/14 19:58:20 celes Exp $";
|
||||||
|
|
||||||
#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[])
|
/*
|
||||||
|
%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)
|
||||||
|
{
|
||||||
|
lua_Object lo = lua_getparam(1);
|
||||||
|
if (!lua_isnumber(lo))
|
||||||
|
lua_pushnil();
|
||||||
|
else
|
||||||
|
{
|
||||||
|
int n = (int)lua_getnumber(lo);
|
||||||
|
if (n < 1 || n > lua_argc) lua_pushnil();
|
||||||
|
else lua_pushstring(lua_argv[n]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main (int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
int result = 0;
|
||||||
iolib_open ();
|
iolib_open ();
|
||||||
strlib_open ();
|
strlib_open ();
|
||||||
mathlib_open ();
|
mathlib_open ();
|
||||||
|
|
||||||
|
lua_register("argv", lua_getargv);
|
||||||
|
|
||||||
if (argc < 2)
|
if (argc < 2)
|
||||||
{
|
{
|
||||||
char buffer[250];
|
char buffer[250];
|
||||||
while (gets(buffer) != 0)
|
while (gets(buffer) != 0)
|
||||||
lua_dostring(buffer);
|
result = lua_dostring(buffer);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
for (i=1; i<argc; i++)
|
{
|
||||||
lua_dofile (argv[i]);
|
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
|
||||||
|
result = lua_dofile (argv[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
96
lua.h
96
lua.h
@@ -2,53 +2,99 @@
|
|||||||
** 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.15 1995/01/18 20:15:05 celes Exp celes $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#ifndef lua_h
|
#ifndef lua_h
|
||||||
#define lua_h
|
#define lua_h
|
||||||
|
|
||||||
|
/* 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_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(_)
|
||||||
|
|
||||||
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_pushliteral (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)
|
||||||
|
|
||||||
|
#define lua_isnil(_) (lua_type(_)==LUA_T_NIL)
|
||||||
|
#define lua_isnumber(_) (lua_type(_)==LUA_T_NUMBER)
|
||||||
|
#define lua_isstring(_) (lua_type(_)==LUA_T_STRING)
|
||||||
|
#define lua_istable(_) (lua_type(_)==LUA_T_ARRAY)
|
||||||
|
#define lua_isfunction(_) (lua_type(_)==LUA_T_FUNCTION)
|
||||||
|
#define lua_iscfunction(_) (lua_type(_)==LUA_T_CFUNCTION)
|
||||||
|
#define lua_isuserdata(_) (lua_type(_)>=LUA_T_USERDATA)
|
||||||
|
|
||||||
|
|
||||||
|
/* for lua 1.1 compatibility. Avoid using these macros */
|
||||||
|
|
||||||
|
#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;
|
|
||||||
|
|
||||||
2
lualib.h
2
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.2 1994/08/24 15:29:02 celes Stab roberto $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef lualib_h
|
#ifndef lualib_h
|
||||||
|
|||||||
44
luamem.c
Normal file
44
luamem.c
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
** mem.c
|
||||||
|
** TecCGraf - PUC-Rio
|
||||||
|
*/
|
||||||
|
|
||||||
|
char *rcs_mem = "$Id: mem.c,v 1.4 1995/01/13 22:11:12 roberto Exp roberto $";
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "mem.h"
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
void luaI_free (void *block)
|
||||||
|
{
|
||||||
|
*((int *)block) = -1; /* to catch errors */
|
||||||
|
free(block);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void *luaI_malloc (unsigned long size)
|
||||||
|
{
|
||||||
|
void *block = malloc((size_t)size);
|
||||||
|
if (block == NULL)
|
||||||
|
lua_error("not enough memory");
|
||||||
|
return block;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void *luaI_realloc (void *oldblock, unsigned long size)
|
||||||
|
{
|
||||||
|
void *block = realloc(oldblock, (size_t)size);
|
||||||
|
if (block == NULL)
|
||||||
|
lua_error("not enough memory");
|
||||||
|
return block;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
char *luaI_strdup (char *str)
|
||||||
|
{
|
||||||
|
char *newstr = luaI_malloc(strlen(str)+1);
|
||||||
|
strcpy(newstr, str);
|
||||||
|
return newstr;
|
||||||
|
}
|
||||||
25
luamem.h
Normal file
25
luamem.h
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
** mem.c
|
||||||
|
** memory manager for lua
|
||||||
|
** $Id: mem.h,v 1.1 1994/11/16 17:38:08 roberto Stab roberto $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef mem_h
|
||||||
|
#define mem_h
|
||||||
|
|
||||||
|
#ifndef NULL
|
||||||
|
#define NULL 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void luaI_free (void *block);
|
||||||
|
void *luaI_malloc (unsigned long size);
|
||||||
|
void *luaI_realloc (void *oldblock, unsigned long size);
|
||||||
|
|
||||||
|
char *luaI_strdup (char *str);
|
||||||
|
|
||||||
|
#define new(s) ((s *)luaI_malloc(sizeof(s)))
|
||||||
|
#define newvector(n,s) ((s *)luaI_malloc((n)*sizeof(s)))
|
||||||
|
#define growvector(old,n,s) ((s *)luaI_realloc(old,(n)*sizeof(s)))
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
43
makefile
43
makefile
@@ -1,20 +1,25 @@
|
|||||||
# $Id: makefile,v 1.5 1994/01/10 19:49:56 roberto Exp celes $
|
# $Id: makefile,v 1.12 1995/02/02 19:02:03 roberto Exp $
|
||||||
# Compilation parameters
|
# Compilation parameters
|
||||||
CC = gcc
|
CC = gcc
|
||||||
CFLAGS = -I/usr/5include -Wall -O2
|
CFLAGS = -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 = \
|
LUAMOD = \
|
||||||
y.tab \
|
parser \
|
||||||
lex \
|
lex \
|
||||||
opcode \
|
opcode \
|
||||||
hash \
|
hash \
|
||||||
table \
|
table \
|
||||||
inout \
|
inout \
|
||||||
tree
|
tree \
|
||||||
|
fallback\
|
||||||
|
mem
|
||||||
|
|
||||||
LIBMOD = \
|
LIBMOD = \
|
||||||
iolib \
|
iolib \
|
||||||
@@ -28,7 +33,7 @@ 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.c lua.a lualib.a -lm
|
||||||
|
|
||||||
lua.a : y.tab.c $(LUAOBJS)
|
lua.a : parser.c $(LUAOBJS)
|
||||||
$(AR) $(ARFLAGS) $@ $?
|
$(AR) $(ARFLAGS) $@ $?
|
||||||
ranlib lua.a
|
ranlib lua.a
|
||||||
|
|
||||||
@@ -36,8 +41,6 @@ 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)
|
||||||
|
|
||||||
@@ -45,9 +48,31 @@ liblua.so.1.0 : lua.o
|
|||||||
$(CC) $(CFLAGS) -c -o $@ $<
|
$(CC) $(CFLAGS) -c -o $@ $<
|
||||||
|
|
||||||
|
|
||||||
y.tab.c : lua.stx exscript
|
parser.c : lua.stx
|
||||||
yacc -d lua.stx ; ex y.tab.c <exscript
|
yacc++ -d lua.stx ; mv -f y.tab.c parser.c ; mv -f y.tab.h parser.h
|
||||||
|
|
||||||
|
clear :
|
||||||
|
rcsclean
|
||||||
|
rm -f *.o
|
||||||
|
rm -f parser.c parser.h
|
||||||
|
co lua.h lualib.h
|
||||||
|
|
||||||
% : RCS/%,v
|
% : RCS/%,v
|
||||||
co $@
|
co $@
|
||||||
|
|
||||||
|
|
||||||
|
fallback.o : fallback.c mem.h fallback.h opcode.h lua.h types.h tree.h inout.h
|
||||||
|
hash.o : hash.c mem.h opcode.h lua.h types.h tree.h hash.h inout.h table.h
|
||||||
|
inout.o : inout.c mem.h opcode.h lua.h types.h tree.h hash.h inout.h table.h
|
||||||
|
iolib.o : iolib.c mem.h lua.h lualib.h
|
||||||
|
lex.o : lex.c tree.h types.h table.h opcode.h lua.h inout.h parser.h ugly.h
|
||||||
|
lua.o : lua.c lua.h lualib.h
|
||||||
|
mathlib.o : mathlib.c lualib.h lua.h
|
||||||
|
mem.o : mem.c mem.h lua.h
|
||||||
|
opcode.o : opcode.c mem.h opcode.h lua.h types.h tree.h hash.h inout.h table.h \
|
||||||
|
fallback.h
|
||||||
|
strlib.o : strlib.c mem.h lua.h lualib.h
|
||||||
|
table.o : table.c mem.h opcode.h lua.h types.h tree.h hash.h inout.h table.h \
|
||||||
|
fallback.h
|
||||||
|
tree.o : tree.c mem.h lua.h tree.h types.h table.h opcode.h
|
||||||
|
parser.o : parser.c mem.h opcode.h lua.h types.h tree.h hash.h inout.h table.h
|
||||||
|
|||||||
182
mathlib.c
182
mathlib.c
@@ -3,24 +3,26 @@
|
|||||||
** Mathematics library to LUA
|
** Mathematics library to LUA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
char *rcs_mathlib="$Id: $";
|
char *rcs_mathlib="$Id: mathlib.c,v 1.8 1995/01/04 18:49:54 roberto Exp $";
|
||||||
|
|
||||||
#include <stdio.h> /* NULL */
|
#include <stdio.h> /* NULL */
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
|
#include "lualib.h"
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
#define TODEGREE(a) ((a)*180.0/3.14159)
|
#define PI 3.14159265358979323846
|
||||||
#define TORAD(a) ((a)*3.14159/180.0)
|
#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_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 `abs'"); return; }
|
lua_error ("too few arguments to function `abs'");
|
||||||
if (!lua_isnumber(o))
|
if (!lua_isnumber(o))
|
||||||
{ lua_error ("incorrect arguments to function `abs'"); return; }
|
lua_error ("incorrect arguments to function `abs'");
|
||||||
d = lua_getnumber(o);
|
d = lua_getnumber(o);
|
||||||
if (d < 0) d = -d;
|
if (d < 0) d = -d;
|
||||||
lua_pushnumber (d);
|
lua_pushnumber (d);
|
||||||
@@ -31,10 +33,10 @@ static void math_sin (void)
|
|||||||
{
|
{
|
||||||
double d;
|
double d;
|
||||||
lua_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 `sin'"); return; }
|
lua_error ("too few arguments to function `sin'");
|
||||||
if (!lua_isnumber(o))
|
if (!lua_isnumber(o))
|
||||||
{ lua_error ("incorrect arguments to function `sin'"); return; }
|
lua_error ("incorrect arguments to function `sin'");
|
||||||
d = lua_getnumber(o);
|
d = lua_getnumber(o);
|
||||||
lua_pushnumber (sin(TORAD(d)));
|
lua_pushnumber (sin(TORAD(d)));
|
||||||
}
|
}
|
||||||
@@ -45,10 +47,10 @@ static void math_cos (void)
|
|||||||
{
|
{
|
||||||
double d;
|
double d;
|
||||||
lua_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 `cos'"); return; }
|
lua_error ("too few arguments to function `cos'");
|
||||||
if (!lua_isnumber(o))
|
if (!lua_isnumber(o))
|
||||||
{ lua_error ("incorrect arguments to function `cos'"); return; }
|
lua_error ("incorrect arguments to function `cos'");
|
||||||
d = lua_getnumber(o);
|
d = lua_getnumber(o);
|
||||||
lua_pushnumber (cos(TORAD(d)));
|
lua_pushnumber (cos(TORAD(d)));
|
||||||
}
|
}
|
||||||
@@ -59,10 +61,10 @@ static void math_tan (void)
|
|||||||
{
|
{
|
||||||
double d;
|
double d;
|
||||||
lua_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 `tan'"); return; }
|
lua_error ("too few arguments to function `tan'");
|
||||||
if (!lua_isnumber(o))
|
if (!lua_isnumber(o))
|
||||||
{ lua_error ("incorrect arguments to function `tan'"); return; }
|
lua_error ("incorrect arguments to function `tan'");
|
||||||
d = lua_getnumber(o);
|
d = lua_getnumber(o);
|
||||||
lua_pushnumber (tan(TORAD(d)));
|
lua_pushnumber (tan(TORAD(d)));
|
||||||
}
|
}
|
||||||
@@ -72,10 +74,10 @@ static void math_asin (void)
|
|||||||
{
|
{
|
||||||
double d;
|
double d;
|
||||||
lua_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 `asin'"); return; }
|
lua_error ("too few arguments to function `asin'");
|
||||||
if (!lua_isnumber(o))
|
if (!lua_isnumber(o))
|
||||||
{ lua_error ("incorrect arguments to function `asin'"); return; }
|
lua_error ("incorrect arguments to function `asin'");
|
||||||
d = lua_getnumber(o);
|
d = lua_getnumber(o);
|
||||||
lua_pushnumber (TODEGREE(asin(d)));
|
lua_pushnumber (TODEGREE(asin(d)));
|
||||||
}
|
}
|
||||||
@@ -85,10 +87,10 @@ static void math_acos (void)
|
|||||||
{
|
{
|
||||||
double d;
|
double d;
|
||||||
lua_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 `acos'"); return; }
|
lua_error ("too few arguments to function `acos'");
|
||||||
if (!lua_isnumber(o))
|
if (!lua_isnumber(o))
|
||||||
{ lua_error ("incorrect arguments to function `acos'"); return; }
|
lua_error ("incorrect arguments to function `acos'");
|
||||||
d = lua_getnumber(o);
|
d = lua_getnumber(o);
|
||||||
lua_pushnumber (TODEGREE(acos(d)));
|
lua_pushnumber (TODEGREE(acos(d)));
|
||||||
}
|
}
|
||||||
@@ -99,10 +101,10 @@ static void math_atan (void)
|
|||||||
{
|
{
|
||||||
double d;
|
double d;
|
||||||
lua_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 `atan'"); return; }
|
lua_error ("too few arguments to function `atan'");
|
||||||
if (!lua_isnumber(o))
|
if (!lua_isnumber(o))
|
||||||
{ lua_error ("incorrect arguments to function `atan'"); return; }
|
lua_error ("incorrect arguments to function `atan'");
|
||||||
d = lua_getnumber(o);
|
d = lua_getnumber(o);
|
||||||
lua_pushnumber (TODEGREE(atan(d)));
|
lua_pushnumber (TODEGREE(atan(d)));
|
||||||
}
|
}
|
||||||
@@ -112,10 +114,10 @@ static void math_ceil (void)
|
|||||||
{
|
{
|
||||||
double d;
|
double d;
|
||||||
lua_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 `ceil'"); return; }
|
lua_error ("too few arguments to function `ceil'");
|
||||||
if (!lua_isnumber(o))
|
if (!lua_isnumber(o))
|
||||||
{ lua_error ("incorrect arguments to function `ceil'"); return; }
|
lua_error ("incorrect arguments to function `ceil'");
|
||||||
d = lua_getnumber(o);
|
d = lua_getnumber(o);
|
||||||
lua_pushnumber (ceil(d));
|
lua_pushnumber (ceil(d));
|
||||||
}
|
}
|
||||||
@@ -125,10 +127,10 @@ static void math_floor (void)
|
|||||||
{
|
{
|
||||||
double d;
|
double d;
|
||||||
lua_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 `floor'"); return; }
|
lua_error ("too few arguments to function `floor'");
|
||||||
if (!lua_isnumber(o))
|
if (!lua_isnumber(o))
|
||||||
{ lua_error ("incorrect arguments to function `floor'"); return; }
|
lua_error ("incorrect arguments to function `floor'");
|
||||||
d = lua_getnumber(o);
|
d = lua_getnumber(o);
|
||||||
lua_pushnumber (floor(d));
|
lua_pushnumber (floor(d));
|
||||||
}
|
}
|
||||||
@@ -139,7 +141,7 @@ static void math_mod (void)
|
|||||||
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))
|
if (!lua_isnumber(o1) || !lua_isnumber(o2))
|
||||||
{ lua_error ("incorrect arguments to function `mod'"); return; }
|
lua_error ("incorrect arguments to function `mod'");
|
||||||
d1 = (int) lua_getnumber(o1);
|
d1 = (int) lua_getnumber(o1);
|
||||||
d2 = (int) lua_getnumber(o2);
|
d2 = (int) lua_getnumber(o2);
|
||||||
lua_pushnumber (d1%d2);
|
lua_pushnumber (d1%d2);
|
||||||
@@ -150,24 +152,36 @@ static void math_sqrt (void)
|
|||||||
{
|
{
|
||||||
double d;
|
double d;
|
||||||
lua_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 `sqrt'"); return; }
|
lua_error ("too few arguments to function `sqrt'");
|
||||||
if (!lua_isnumber(o))
|
if (!lua_isnumber(o))
|
||||||
{ lua_error ("incorrect arguments to function `sqrt'"); return; }
|
lua_error ("incorrect arguments to function `sqrt'");
|
||||||
d = lua_getnumber(o);
|
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)
|
||||||
@@ -175,15 +189,15 @@ static void math_min (void)
|
|||||||
int i=1;
|
int i=1;
|
||||||
double d, dmin;
|
double d, dmin;
|
||||||
lua_Object o;
|
lua_Object o;
|
||||||
if ((o = lua_getparam(i++)) == NULL)
|
if ((o = lua_getparam(i++)) == LUA_NOOBJECT)
|
||||||
{ lua_error ("too few arguments to function `min'"); return; }
|
lua_error ("too few arguments to function `min'");
|
||||||
if (!lua_isnumber(o))
|
if (!lua_isnumber(o))
|
||||||
{ lua_error ("incorrect arguments to function `min'"); return; }
|
lua_error ("incorrect arguments to function `min'");
|
||||||
dmin = lua_getnumber (o);
|
dmin = lua_getnumber (o);
|
||||||
while ((o = lua_getparam(i++)) != NULL)
|
while ((o = lua_getparam(i++)) != LUA_NOOBJECT)
|
||||||
{
|
{
|
||||||
if (!lua_isnumber(o))
|
if (!lua_isnumber(o))
|
||||||
{ lua_error ("incorrect arguments to function `min'"); return; }
|
lua_error ("incorrect arguments to function `min'");
|
||||||
d = lua_getnumber (o);
|
d = lua_getnumber (o);
|
||||||
if (d < dmin) dmin = d;
|
if (d < dmin) dmin = d;
|
||||||
}
|
}
|
||||||
@@ -196,15 +210,15 @@ static void math_max (void)
|
|||||||
int i=1;
|
int i=1;
|
||||||
double d, dmax;
|
double d, dmax;
|
||||||
lua_Object o;
|
lua_Object o;
|
||||||
if ((o = lua_getparam(i++)) == NULL)
|
if ((o = lua_getparam(i++)) == LUA_NOOBJECT)
|
||||||
{ lua_error ("too few arguments to function `max'"); return; }
|
lua_error ("too few arguments to function `max'");
|
||||||
if (!lua_isnumber(o))
|
if (!lua_isnumber(o))
|
||||||
{ lua_error ("incorrect arguments to function `max'"); return; }
|
lua_error ("incorrect arguments to function `max'");
|
||||||
dmax = lua_getnumber (o);
|
dmax = lua_getnumber (o);
|
||||||
while ((o = lua_getparam(i++)) != NULL)
|
while ((o = lua_getparam(i++)) != LUA_NOOBJECT)
|
||||||
{
|
{
|
||||||
if (!lua_isnumber(o))
|
if (!lua_isnumber(o))
|
||||||
{ lua_error ("incorrect arguments to function `max'"); return; }
|
lua_error ("incorrect arguments to function `max'");
|
||||||
d = lua_getnumber (o);
|
d = lua_getnumber (o);
|
||||||
if (d > dmax) dmax = d;
|
if (d > dmax) dmax = d;
|
||||||
}
|
}
|
||||||
@@ -212,6 +226,67 @@ static void math_max (void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void math_log (void)
|
||||||
|
{
|
||||||
|
double d;
|
||||||
|
lua_Object o = lua_getparam (1);
|
||||||
|
if (o == LUA_NOOBJECT)
|
||||||
|
lua_error ("too few arguments to function `log'");
|
||||||
|
if (!lua_isnumber(o))
|
||||||
|
lua_error ("incorrect arguments to function `log'");
|
||||||
|
d = lua_getnumber(o);
|
||||||
|
lua_pushnumber (log(d));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void math_log10 (void)
|
||||||
|
{
|
||||||
|
double d;
|
||||||
|
lua_Object o = lua_getparam (1);
|
||||||
|
if (o == LUA_NOOBJECT)
|
||||||
|
lua_error ("too few arguments to function `log10'");
|
||||||
|
if (!lua_isnumber(o))
|
||||||
|
lua_error ("incorrect arguments to function `log10'");
|
||||||
|
d = lua_getnumber(o);
|
||||||
|
lua_pushnumber (log10(d));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void math_exp (void)
|
||||||
|
{
|
||||||
|
double d;
|
||||||
|
lua_Object o = lua_getparam (1);
|
||||||
|
if (o == LUA_NOOBJECT)
|
||||||
|
lua_error ("too few arguments to function `exp'");
|
||||||
|
if (!lua_isnumber(o))
|
||||||
|
lua_error ("incorrect arguments to function `exp'");
|
||||||
|
d = lua_getnumber(o);
|
||||||
|
lua_pushnumber (exp(d));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void math_deg (void)
|
||||||
|
{
|
||||||
|
float d;
|
||||||
|
lua_Object o = lua_getparam (1);
|
||||||
|
if (o == LUA_NOOBJECT)
|
||||||
|
lua_error ("too few arguments to function `deg'");
|
||||||
|
if (!lua_isnumber(o))
|
||||||
|
lua_error ("incorrect arguments to function `deg'");
|
||||||
|
d = lua_getnumber(o);
|
||||||
|
lua_pushnumber (d*180./PI);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void math_rad (void)
|
||||||
|
{
|
||||||
|
float d;
|
||||||
|
lua_Object o = lua_getparam (1);
|
||||||
|
if (o == LUA_NOOBJECT)
|
||||||
|
lua_error ("too few arguments to function `rad'");
|
||||||
|
if (!lua_isnumber(o))
|
||||||
|
lua_error ("incorrect arguments to function `rad'");
|
||||||
|
d = lua_getnumber(o);
|
||||||
|
lua_pushnumber (d/180.*PI);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Open math library
|
** Open math library
|
||||||
@@ -229,7 +304,12 @@ void mathlib_open (void)
|
|||||||
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);
|
||||||
|
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
|
|
||||||
|
|
||||||
90
opcode.h
90
opcode.h
@@ -1,11 +1,15 @@
|
|||||||
/*
|
/*
|
||||||
** 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.9 1994/11/23 14:31:11 roberto Stab $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef opcode_h
|
#ifndef opcode_h
|
||||||
#define opcode_h
|
#define opcode_h
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
#include "types.h"
|
||||||
|
#include "tree.h"
|
||||||
|
|
||||||
#ifndef STACKGAP
|
#ifndef STACKGAP
|
||||||
#define STACKGAP 128
|
#define STACKGAP 128
|
||||||
#endif
|
#endif
|
||||||
@@ -16,23 +20,8 @@
|
|||||||
|
|
||||||
#define FIELDS_PER_FLUSH 40
|
#define FIELDS_PER_FLUSH 40
|
||||||
|
|
||||||
typedef unsigned char Byte;
|
#define MAX_TEMPS 20
|
||||||
|
|
||||||
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 +31,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 +47,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,41 +71,32 @@ typedef enum
|
|||||||
IFFUPJMP,
|
IFFUPJMP,
|
||||||
POP,
|
POP,
|
||||||
CALLFUNC,
|
CALLFUNC,
|
||||||
|
RETCODE0,
|
||||||
RETCODE,
|
RETCODE,
|
||||||
HALT,
|
|
||||||
SETFUNCTION,
|
SETFUNCTION,
|
||||||
SETLINE,
|
SETLINE,
|
||||||
RESET
|
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 void (*Cfunction) (void);
|
||||||
typedef int (*Input) (void);
|
typedef int (*Input) (void);
|
||||||
|
|
||||||
typedef union
|
typedef union
|
||||||
{
|
{
|
||||||
Cfunction f;
|
Cfunction f;
|
||||||
real n;
|
real n;
|
||||||
char *s;
|
TaggedString *ts;
|
||||||
Byte *b;
|
Byte *b;
|
||||||
struct Hash *a;
|
struct Hash *a;
|
||||||
void *u;
|
void *u;
|
||||||
} Value;
|
} Value;
|
||||||
|
|
||||||
typedef struct Object
|
typedef struct Object
|
||||||
{
|
{
|
||||||
Type tag;
|
lua_Type tag;
|
||||||
Value value;
|
Value value;
|
||||||
} Object;
|
} Object;
|
||||||
|
|
||||||
@@ -124,7 +108,8 @@ typedef struct
|
|||||||
/* 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 tsvalue(o) ((o)->value.ts)
|
||||||
#define bvalue(o) ((o)->value.b)
|
#define bvalue(o) ((o)->value.b)
|
||||||
#define avalue(o) ((o)->value.a)
|
#define avalue(o) ((o)->value.a)
|
||||||
#define fvalue(o) ((o)->value.f)
|
#define fvalue(o) ((o)->value.f)
|
||||||
@@ -140,25 +125,40 @@ typedef struct
|
|||||||
#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 {char c1; char 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 {char c1; char c2; char c3; char 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 {char c1; char c2; char c3; char c4;} m;
|
||||||
|
Byte *b;
|
||||||
|
} 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_markstack (void);
|
|
||||||
char *lua_strdup (char *l);
|
char *lua_strdup (char *l);
|
||||||
|
|
||||||
void lua_setinput (Input fn); /* from "lex.c" module */
|
void lua_setinput (Input fn); /* from "lex.c" module */
|
||||||
char *lua_lasttext (void); /* from "lex.c" module */
|
char *lua_lasttext (void); /* from "lex.c" module */
|
||||||
int lua_parse (void); /* from "lua.stx" module */
|
int yylex (void); /* from "lex.c" module */
|
||||||
void lua_type (void);
|
void lua_parse (Byte **code); /* from "lua.stx" module */
|
||||||
void lua_obj2number (void);
|
|
||||||
void lua_print (void);
|
|
||||||
void lua_internaldofile (void);
|
|
||||||
void lua_internaldostring (void);
|
|
||||||
void lua_travstack (void (*fn)(Object *));
|
void lua_travstack (void (*fn)(Object *));
|
||||||
|
Object *luaI_Address (lua_Object o);
|
||||||
|
void luaI_pushobject (Object *o);
|
||||||
|
void luaI_gcFB (Object *o);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
74
strlib.c
74
strlib.c
@@ -3,34 +3,66 @@
|
|||||||
** 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.11 1995/02/02 20:05:37 roberto Exp roberto $";
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
#include "mm.h"
|
|
||||||
|
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
#include "lualib.h"
|
||||||
|
|
||||||
|
|
||||||
|
static char *newstring (lua_Object o)
|
||||||
|
{
|
||||||
|
char *s = lua_getstring(o);
|
||||||
|
char *ns = (char *)malloc(strlen(s)+1);
|
||||||
|
if (ns == 0)
|
||||||
|
lua_error("not enough memory for new string");
|
||||||
|
strcpy(ns, s);
|
||||||
|
return ns;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** 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, *s2, *f;
|
||||||
|
int init;
|
||||||
lua_Object o1 = lua_getparam (1);
|
lua_Object o1 = lua_getparam (1);
|
||||||
lua_Object o2 = lua_getparam (2);
|
lua_Object o2 = lua_getparam (2);
|
||||||
|
lua_Object o3 = lua_getparam (3);
|
||||||
|
lua_Object o4 = lua_getparam (4);
|
||||||
if (!lua_isstring(o1) || !lua_isstring(o2))
|
if (!lua_isstring(o1) || !lua_isstring(o2))
|
||||||
{ lua_error ("incorrect arguments to function `strfind'"); return; }
|
lua_error ("incorrect arguments to function `strfind'");
|
||||||
|
if (o3 == LUA_NOOBJECT)
|
||||||
|
init = 0;
|
||||||
|
else if (lua_isnumber(o3))
|
||||||
|
init = lua_getnumber(o3)-1;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
lua_error ("incorrect arguments to function `strfind'");
|
||||||
|
return; /* to avoid warnings */
|
||||||
|
}
|
||||||
s1 = lua_getstring(o1);
|
s1 = lua_getstring(o1);
|
||||||
s2 = lua_getstring(o2);
|
s2 = lua_getstring(o2);
|
||||||
f = strstr(s1,s2);
|
f = strstr(s1+init,s2);
|
||||||
if (f != NULL)
|
if (f != NULL)
|
||||||
lua_pushnumber (f-s1+1);
|
{
|
||||||
|
int pos = f-s1+1;
|
||||||
|
if (o4 == LUA_NOOBJECT)
|
||||||
|
lua_pushnumber (pos);
|
||||||
|
else if (!lua_isnumber(o4))
|
||||||
|
lua_error ("incorrect arguments to function `strfind'");
|
||||||
|
else if ((int)lua_getnumber(o4) >= pos+strlen(s2)-1)
|
||||||
|
lua_pushnumber (pos);
|
||||||
|
else
|
||||||
|
lua_pushnil();
|
||||||
|
}
|
||||||
else
|
else
|
||||||
lua_pushnil();
|
lua_pushnil();
|
||||||
}
|
}
|
||||||
@@ -44,7 +76,7 @@ static void str_len (void)
|
|||||||
{
|
{
|
||||||
lua_Object o = lua_getparam (1);
|
lua_Object o = lua_getparam (1);
|
||||||
if (!lua_isstring(o))
|
if (!lua_isstring(o))
|
||||||
{ lua_error ("incorrect arguments to function `strlen'"); return; }
|
lua_error ("incorrect arguments to function `strlen'");
|
||||||
lua_pushnumber(strlen(lua_getstring(o)));
|
lua_pushnumber(strlen(lua_getstring(o)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,20 +94,20 @@ static void str_sub (void)
|
|||||||
lua_Object o2 = lua_getparam (2);
|
lua_Object o2 = lua_getparam (2);
|
||||||
lua_Object o3 = lua_getparam (3);
|
lua_Object o3 = lua_getparam (3);
|
||||||
if (!lua_isstring(o1) || !lua_isnumber(o2))
|
if (!lua_isstring(o1) || !lua_isnumber(o2))
|
||||||
{ lua_error ("incorrect arguments to function `strsub'"); return; }
|
lua_error ("incorrect arguments to function `strsub'");
|
||||||
if (o3 != NULL && !lua_isnumber(o3))
|
if (o3 != LUA_NOOBJECT && !lua_isnumber(o3))
|
||||||
{ lua_error ("incorrect third argument to function `strsub'"); return; }
|
lua_error ("incorrect third argument to function `strsub'");
|
||||||
s = lua_copystring(o1);
|
s = newstring(o1);
|
||||||
start = lua_getnumber (o2);
|
start = lua_getnumber (o2);
|
||||||
end = o3 == NULL ? strlen(s) : lua_getnumber (o3);
|
end = o3 == LUA_NOOBJECT ? 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;
|
s[end] = 0;
|
||||||
lua_pushstring (&s[start-1]);
|
lua_pushstring (&s[start-1]);
|
||||||
}
|
}
|
||||||
free (s);
|
free(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -88,8 +120,8 @@ static void str_lower (void)
|
|||||||
char *s, *c;
|
char *s, *c;
|
||||||
lua_Object o = lua_getparam (1);
|
lua_Object o = lua_getparam (1);
|
||||||
if (!lua_isstring(o))
|
if (!lua_isstring(o))
|
||||||
{ lua_error ("incorrect arguments to function `strlower'"); return; }
|
lua_error ("incorrect arguments to function `strlower'");
|
||||||
c = s = strdup(lua_getstring(o));
|
c = s = newstring(o);
|
||||||
while (*c != 0)
|
while (*c != 0)
|
||||||
{
|
{
|
||||||
*c = tolower(*c);
|
*c = tolower(*c);
|
||||||
@@ -110,8 +142,8 @@ static void str_upper (void)
|
|||||||
char *s, *c;
|
char *s, *c;
|
||||||
lua_Object o = lua_getparam (1);
|
lua_Object o = lua_getparam (1);
|
||||||
if (!lua_isstring(o))
|
if (!lua_isstring(o))
|
||||||
{ lua_error ("incorrect arguments to function `strlower'"); return; }
|
lua_error ("incorrect arguments to function `strlower'");
|
||||||
c = s = strdup(lua_getstring(o));
|
c = s = newstring(o);
|
||||||
while (*c != 0)
|
while (*c != 0)
|
||||||
{
|
{
|
||||||
*c = toupper(*c);
|
*c = toupper(*c);
|
||||||
|
|||||||
282
table.c
282
table.c
@@ -3,31 +3,29 @@
|
|||||||
** 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.27 1995/01/13 22:11:12 roberto Exp celes $";
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.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 "inout.h"
|
||||||
#include "table.h"
|
#include "table.h"
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
#include "fallback.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;
|
||||||
static Word lua_ntable = 0;
|
static Word lua_ntable = 0;
|
||||||
static Word lua_maxsymbol = 0;
|
static Long lua_maxsymbol = 0;
|
||||||
|
|
||||||
char **lua_constant;
|
TaggedString **lua_constant;
|
||||||
static Word lua_nconstant = 0;
|
static Word lua_nconstant = 0;
|
||||||
static Word lua_maxconstant = 0;
|
static Long lua_maxconstant = 0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -35,39 +33,43 @@ static Word lua_maxconstant = 0;
|
|||||||
char *lua_file[MAXFILE];
|
char *lua_file[MAXFILE];
|
||||||
int lua_nfile;
|
int lua_nfile;
|
||||||
|
|
||||||
/* Variables to controll garbage collection */
|
|
||||||
#define GARBAGE_BLOCK 256
|
#define GARBAGE_BLOCK 256
|
||||||
Word lua_block=GARBAGE_BLOCK; /* when garbage collector will be called */
|
#define MIN_GARBAGE_BLOCK 10
|
||||||
Word lua_nentity; /* counter of new entities (strings and arrays) */
|
|
||||||
|
|
||||||
|
static void lua_nextvar (void);
|
||||||
|
static void setglobal (void);
|
||||||
|
static void getglobal (void);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Initialise symbol table with internal functions
|
** Initialise symbol table with internal functions
|
||||||
*/
|
*/
|
||||||
static void lua_initsymbol (void)
|
static void lua_initsymbol (void)
|
||||||
{
|
{
|
||||||
int n;
|
Word n;
|
||||||
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)
|
n = luaI_findsymbolbyname("next");
|
||||||
{
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_next;
|
||||||
lua_error ("symbol table: not enough memory");
|
n = luaI_findsymbolbyname("dofile");
|
||||||
return;
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_internaldofile;
|
||||||
}
|
n = luaI_findsymbolbyname("setglobal");
|
||||||
n = lua_findsymbol("type");
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = setglobal;
|
||||||
s_tag(n) = T_CFUNCTION; s_fvalue(n) = lua_type;
|
n = luaI_findsymbolbyname("getglobal");
|
||||||
n = lua_findsymbol("tonumber");
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = getglobal;
|
||||||
s_tag(n) = T_CFUNCTION; s_fvalue(n) = lua_obj2number;
|
n = luaI_findsymbolbyname("nextvar");
|
||||||
n = lua_findsymbol("next");
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_nextvar;
|
||||||
s_tag(n) = T_CFUNCTION; s_fvalue(n) = lua_next;
|
n = luaI_findsymbolbyname("type");
|
||||||
n = lua_findsymbol("nextvar");
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_type;
|
||||||
s_tag(n) = T_CFUNCTION; s_fvalue(n) = lua_nextvar;
|
n = luaI_findsymbolbyname("tonumber");
|
||||||
n = lua_findsymbol("print");
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_obj2number;
|
||||||
s_tag(n) = T_CFUNCTION; s_fvalue(n) = lua_print;
|
n = luaI_findsymbolbyname("print");
|
||||||
n = lua_findsymbol("dofile");
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_print;
|
||||||
s_tag(n) = T_CFUNCTION; s_fvalue(n) = lua_internaldofile;
|
n = luaI_findsymbolbyname("dostring");
|
||||||
n = lua_findsymbol("dostring");
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_internaldostring;
|
||||||
s_tag(n) = T_CFUNCTION; s_fvalue(n) = lua_internaldostring;
|
n = luaI_findsymbolbyname("setfallback");
|
||||||
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_setfallback;
|
||||||
|
n = luaI_findsymbolbyname("error");
|
||||||
|
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -77,60 +79,40 @@ static void lua_initsymbol (void)
|
|||||||
void lua_initconstant (void)
|
void lua_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)
|
|
||||||
{
|
|
||||||
lua_error ("constant table: not enough memory");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
lua_findconstant("mark");
|
|
||||||
lua_findconstant("nil");
|
|
||||||
lua_findconstant("number");
|
|
||||||
lua_findconstant("string");
|
|
||||||
lua_findconstant("table");
|
|
||||||
lua_findconstant("function");
|
|
||||||
lua_findconstant("cfunction");
|
|
||||||
lua_findconstant("userdata");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Given a name, search it at symbol table and return its index. If not
|
** 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 (TreeNode *t)
|
||||||
{
|
{
|
||||||
char *n;
|
|
||||||
if (lua_table == NULL)
|
if (lua_table == NULL)
|
||||||
lua_initsymbol();
|
lua_initsymbol();
|
||||||
n = lua_varcreate(s);
|
if (t->varindex == NOT_USED)
|
||||||
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)
|
||||||
{
|
{
|
||||||
|
if (lua_maxsymbol >= MAX_WORD)
|
||||||
|
lua_error("symbol table overflow");
|
||||||
lua_maxsymbol *= 2;
|
lua_maxsymbol *= 2;
|
||||||
if (lua_maxsymbol > MAX_WORD)
|
if (lua_maxsymbol >= MAX_WORD)
|
||||||
{
|
lua_maxsymbol = MAX_WORD;
|
||||||
lua_error("symbol table overflow");
|
lua_table = growvector(lua_table, lua_maxsymbol, Symbol);
|
||||||
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;
|
t->varindex = lua_ntable;
|
||||||
s_tag(lua_ntable) = T_NIL;
|
s_tag(lua_ntable) = LUA_T_NIL;
|
||||||
lua_ntable++;
|
lua_ntable++;
|
||||||
}
|
}
|
||||||
return indexstring(n);
|
return t->varindex;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Word luaI_findsymbolbyname (char *name)
|
||||||
|
{
|
||||||
|
return luaI_findsymbol(lua_constcreate(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -139,39 +121,26 @@ int lua_findsymbol (char *s)
|
|||||||
** found, allocate it.
|
** found, allocate it.
|
||||||
** On error, return -1.
|
** On error, return -1.
|
||||||
*/
|
*/
|
||||||
int lua_findconstant (char *s)
|
Word luaI_findconstant (TreeNode *t)
|
||||||
{
|
{
|
||||||
char *n;
|
|
||||||
if (lua_constant == NULL)
|
if (lua_constant == NULL)
|
||||||
lua_initconstant();
|
lua_initconstant();
|
||||||
n = lua_constcreate(s);
|
if (t->constindex == NOT_USED)
|
||||||
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)
|
||||||
{
|
{
|
||||||
|
if (lua_maxconstant >= MAX_WORD)
|
||||||
|
lua_error("constant table overflow");
|
||||||
lua_maxconstant *= 2;
|
lua_maxconstant *= 2;
|
||||||
if (lua_maxconstant > MAX_WORD)
|
if (lua_maxconstant >= MAX_WORD)
|
||||||
{
|
lua_maxconstant = MAX_WORD;
|
||||||
lua_error("constant table overflow");
|
lua_constant = growvector(lua_constant, lua_maxconstant, TaggedString *);
|
||||||
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;
|
t->constindex = lua_nconstant;
|
||||||
lua_constant[lua_nconstant] = n;
|
lua_constant[lua_nconstant] = &(t->ts);
|
||||||
lua_nconstant++;
|
lua_nconstant++;
|
||||||
}
|
}
|
||||||
return indexstring(n);
|
return t->constindex;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -180,7 +149,7 @@ int lua_findconstant (char *s)
|
|||||||
*/
|
*/
|
||||||
void lua_travsymbol (void (*fn)(Object *))
|
void lua_travsymbol (void (*fn)(Object *))
|
||||||
{
|
{
|
||||||
int i;
|
Word i;
|
||||||
for (i=0; i<lua_ntable; i++)
|
for (i=0; i<lua_ntable; i++)
|
||||||
fn(&s_object(i));
|
fn(&s_object(i));
|
||||||
}
|
}
|
||||||
@@ -191,10 +160,10 @@ void lua_travsymbol (void (*fn)(Object *))
|
|||||||
*/
|
*/
|
||||||
void lua_markobject (Object *o)
|
void lua_markobject (Object *o)
|
||||||
{
|
{
|
||||||
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -204,51 +173,33 @@ void lua_markobject (Object *o)
|
|||||||
*/
|
*/
|
||||||
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);
|
lua_travstack(lua_markobject); /* mark stack objects */
|
||||||
|
lua_travsymbol(lua_markobject); /* mark symbol table objects */
|
||||||
lua_strcollector();
|
luaI_travlock(lua_markobject); /* mark locked objects */
|
||||||
lua_hashcollector();
|
recovered += lua_strcollector();
|
||||||
|
recovered += lua_hashcollector();
|
||||||
lua_nentity = 0; /* reset counter */
|
nentity = 0; /* reset counter */
|
||||||
}
|
block=(16*block-7*recovered)/12; /* adapt block size */
|
||||||
|
if (block < MIN_GARBAGE_BLOCK) block = MIN_GARBAGE_BLOCK;
|
||||||
|
|
||||||
/*
|
|
||||||
** 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
|
** Add a file name at file table, checking overflow. This function also set
|
||||||
** the external variable "lua_filename" with the function filename set.
|
** the external variable "lua_filename" with the function filename set.
|
||||||
** Return 0 on success or 1 on error.
|
** Return 0 on success or error message on error.
|
||||||
*/
|
*/
|
||||||
int lua_addfile (char *fn)
|
char *lua_addfile (char *fn)
|
||||||
{
|
{
|
||||||
if (lua_nfile >= MAXFILE-1)
|
if (lua_nfile >= MAXFILE)
|
||||||
{
|
return "too many files";
|
||||||
lua_error ("too many files");
|
if ((lua_file[lua_nfile++] = luaI_strdup (fn)) == NULL)
|
||||||
return 1;
|
return "not enough memory";
|
||||||
}
|
return NULL;
|
||||||
if ((lua_file[lua_nfile++] = strdup (fn)) == NULL)
|
|
||||||
{
|
|
||||||
lua_error ("not enough memory");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -256,7 +207,7 @@ int lua_addfile (char *fn)
|
|||||||
*/
|
*/
|
||||||
int lua_delfile (void)
|
int lua_delfile (void)
|
||||||
{
|
{
|
||||||
lua_nfile--;
|
luaI_free(lua_file[--lua_nfile]);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -271,27 +222,24 @@ char *lua_filename (void)
|
|||||||
/*
|
/*
|
||||||
** Internal function: return next global variable
|
** Internal function: return next global variable
|
||||||
*/
|
*/
|
||||||
void lua_nextvar (void)
|
static void lua_nextvar (void)
|
||||||
{
|
{
|
||||||
char *varname, *next;
|
char *varname;
|
||||||
Object *o = lua_getparam (1);
|
TreeNode *next;
|
||||||
if (o == NULL)
|
lua_Object o = lua_getparam(1);
|
||||||
{ lua_error ("too few arguments to function `nextvar'"); return; }
|
if (o == LUA_NOOBJECT)
|
||||||
if (lua_getparam (2) != NULL)
|
lua_reportbug("too few arguments to function `nextvar'");
|
||||||
{ lua_error ("too many arguments to function `nextvar'"); return; }
|
if (lua_getparam(2) != LUA_NOOBJECT)
|
||||||
if (tag(o) == T_NIL)
|
lua_reportbug("too many arguments to function `nextvar'");
|
||||||
|
if (lua_isnil(o))
|
||||||
|
varname = NULL;
|
||||||
|
else if (!lua_isstring(o))
|
||||||
{
|
{
|
||||||
varname = 0;
|
lua_reportbug("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
|
||||||
{
|
varname = lua_getstring(o);
|
||||||
varname = svalue(o);
|
|
||||||
}
|
|
||||||
next = lua_varnext(varname);
|
next = lua_varnext(varname);
|
||||||
if (next == NULL)
|
if (next == NULL)
|
||||||
{
|
{
|
||||||
@@ -301,9 +249,29 @@ void lua_nextvar (void)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
Object name;
|
Object name;
|
||||||
tag(&name) = T_STRING;
|
tag(&name) = LUA_T_STRING;
|
||||||
svalue(&name) = next;
|
tsvalue(&name) = &(next->ts);
|
||||||
if (lua_pushobject (&name)) return;
|
luaI_pushobject(&name);
|
||||||
if (lua_pushobject (&s_object(indexstring(next)))) return;
|
luaI_pushobject(&s_object(next->varindex));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void setglobal (void)
|
||||||
|
{
|
||||||
|
lua_Object name = lua_getparam(1);
|
||||||
|
lua_Object value = lua_getparam(2);
|
||||||
|
if (!lua_isstring(name))
|
||||||
|
lua_reportbug("incorrect argument to function `setglobal'");
|
||||||
|
lua_pushobject(value);
|
||||||
|
lua_storeglobal(lua_getstring(name));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void getglobal (void)
|
||||||
|
{
|
||||||
|
lua_Object name = lua_getparam(1);
|
||||||
|
if (!lua_isstring(name))
|
||||||
|
lua_reportbug("incorrect argument to function `getglobal'");
|
||||||
|
lua_pushobject(lua_getglobal(lua_getstring(name)));
|
||||||
|
}
|
||||||
|
|||||||
19
table.h
19
table.h
@@ -1,32 +1,31 @@
|
|||||||
/*
|
/*
|
||||||
** 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.9 1994/11/23 14:31:11 roberto Stab roberto $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef table_h
|
#ifndef table_h
|
||||||
#define table_h
|
#define table_h
|
||||||
|
|
||||||
|
#include "tree.h"
|
||||||
|
#include "opcode.h"
|
||||||
|
|
||||||
extern Symbol *lua_table;
|
extern Symbol *lua_table;
|
||||||
extern char **lua_constant;
|
extern TaggedString **lua_constant;
|
||||||
|
|
||||||
extern char *lua_file[];
|
extern char *lua_file[];
|
||||||
extern int lua_nfile;
|
extern int lua_nfile;
|
||||||
|
|
||||||
extern Word lua_block;
|
|
||||||
extern Word lua_nentity;
|
|
||||||
|
|
||||||
|
|
||||||
void lua_initconstant (void);
|
void lua_initconstant (void);
|
||||||
int lua_findsymbol (char *s);
|
Word luaI_findsymbolbyname (char *name);
|
||||||
int lua_findconstant (char *s);
|
Word luaI_findsymbol (TreeNode *t);
|
||||||
|
Word luaI_findconstant (TreeNode *t);
|
||||||
void lua_travsymbol (void (*fn)(Object *));
|
void lua_travsymbol (void (*fn)(Object *));
|
||||||
void lua_markobject (Object *o);
|
void lua_markobject (Object *o);
|
||||||
void lua_pack (void);
|
void lua_pack (void);
|
||||||
char *lua_createstring (char *s);
|
char *lua_addfile (char *fn);
|
||||||
int lua_addfile (char *fn);
|
|
||||||
int lua_delfile (void);
|
int lua_delfile (void);
|
||||||
char *lua_filename (void);
|
char *lua_filename (void);
|
||||||
void lua_nextvar (void);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
203
tree.c
203
tree.c
@@ -3,150 +3,103 @@
|
|||||||
** TecCGraf - PUC-Rio
|
** TecCGraf - PUC-Rio
|
||||||
*/
|
*/
|
||||||
|
|
||||||
char *rcs_tree="$Id: $";
|
char *rcs_tree="$Id: tree.c,v 1.12 1994/12/20 21:20:36 roberto Exp roberto $";
|
||||||
|
|
||||||
|
|
||||||
#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 "table.h"
|
||||||
|
|
||||||
|
|
||||||
#define lua_strcmp(a,b) (a[0]<b[0]?(-1):(a[0]>b[0]?(1):strcmp(a,b)))
|
#define lua_strcmp(a,b) (a[0]<b[0]?(-1):(a[0]>b[0]?(1):strcmp(a,b)))
|
||||||
|
|
||||||
|
|
||||||
typedef struct TreeNode
|
typedef struct StringNode {
|
||||||
{
|
struct StringNode *next;
|
||||||
struct TreeNode *right;
|
TaggedString ts;
|
||||||
struct TreeNode *left;
|
} StringNode;
|
||||||
Word index;
|
|
||||||
char str[1]; /* \0 byte already reserved */
|
static StringNode *string_root = NULL;
|
||||||
} TreeNode;
|
|
||||||
|
|
||||||
static TreeNode *string_root = NULL;
|
|
||||||
static TreeNode *constant_root = NULL;
|
static TreeNode *constant_root = NULL;
|
||||||
static TreeNode *variable_root = NULL;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Insert a new string/constant/variable at the tree.
|
** Insert a new constant/variable at the tree.
|
||||||
*/
|
*/
|
||||||
static char *tree_create (TreeNode **node, char *str)
|
static TreeNode *tree_create (TreeNode **node, char *str)
|
||||||
{
|
{
|
||||||
if (*node == NULL)
|
if (*node == NULL)
|
||||||
{
|
{
|
||||||
*node = (TreeNode *) malloc (sizeof(TreeNode)+strlen(str));
|
*node = (TreeNode *) luaI_malloc(sizeof(TreeNode)+strlen(str));
|
||||||
if (*node == NULL)
|
|
||||||
lua_error ("memoria insuficiente\n");
|
|
||||||
(*node)->left = (*node)->right = NULL;
|
(*node)->left = (*node)->right = NULL;
|
||||||
strcpy((*node)->str, str);
|
strcpy((*node)->ts.str, str);
|
||||||
(*node)->index = UNMARKED_STRING;
|
(*node)->ts.marked = 0;
|
||||||
return (*node)->str;
|
(*node)->ts.hash = 0;
|
||||||
|
(*node)->varindex = (*node)->constindex = NOT_USED;
|
||||||
|
return *node;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
int c = lua_strcmp(str, (*node)->str);
|
int c = lua_strcmp(str, (*node)->ts.str);
|
||||||
if (c < 0)
|
if (c < 0)
|
||||||
return tree_create(&(*node)->left, str);
|
return tree_create(&(*node)->left, str);
|
||||||
else if (c > 0)
|
else if (c > 0)
|
||||||
return tree_create(&(*node)->right, str);
|
return tree_create(&(*node)->right, str);
|
||||||
else
|
else
|
||||||
return (*node)->str;
|
return *node;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
char *lua_strcreate (char *str)
|
TaggedString *lua_createstring (char *str)
|
||||||
{
|
{
|
||||||
return tree_create(&string_root, str);
|
StringNode *newString;
|
||||||
|
if (str == NULL) return NULL;
|
||||||
|
lua_pack();
|
||||||
|
newString = (StringNode *)luaI_malloc(sizeof(StringNode)+strlen(str));
|
||||||
|
newString->ts.marked = 0;
|
||||||
|
newString->ts.hash = 0;
|
||||||
|
strcpy(newString->ts.str, str);
|
||||||
|
newString->next = string_root;
|
||||||
|
string_root = newString;
|
||||||
|
return &(newString->ts);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *lua_constcreate (char *str)
|
|
||||||
|
TreeNode *lua_constcreate (char *str)
|
||||||
{
|
{
|
||||||
return tree_create(&constant_root, 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);
|
StringNode *curr = string_root, *prev = NULL;
|
||||||
|
Long counter = 0;
|
||||||
|
while (curr)
|
||||||
|
{
|
||||||
|
StringNode *next = curr->next;
|
||||||
|
if (!curr->ts.marked)
|
||||||
|
{
|
||||||
|
if (prev == NULL) string_root = next;
|
||||||
|
else prev->next = next;
|
||||||
|
luaI_free(curr);
|
||||||
|
++counter;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
curr->ts.marked = 0;
|
||||||
|
prev = curr;
|
||||||
|
}
|
||||||
|
curr = next;
|
||||||
|
}
|
||||||
|
return counter;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -154,23 +107,11 @@ void lua_strcollector (void)
|
|||||||
*/
|
*/
|
||||||
static TreeNode *tree_next (TreeNode *node, char *str)
|
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;
|
if (node == NULL) return NULL;
|
||||||
else if (str == NULL) return node;
|
else if (str == NULL) return node;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
int c = lua_strcmp(str, node->str);
|
int c = lua_strcmp(str, node->ts.str);
|
||||||
if (c == 0)
|
if (c == 0)
|
||||||
return node->left != NULL ? node->left : node->right;
|
return node->left != NULL ? node->left : node->right;
|
||||||
else if (c < 0)
|
else if (c < 0)
|
||||||
@@ -181,30 +122,20 @@ static TreeNode *tree_next (TreeNode *node, char *str)
|
|||||||
else
|
else
|
||||||
return tree_next(node->right, str);
|
return tree_next(node->right, str);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char *lua_varnext (char *n)
|
TreeNode *lua_varnext (char *n)
|
||||||
{
|
{
|
||||||
TreeNode *result = tree_next(variable_root, n);
|
TreeNode *result;
|
||||||
return result != NULL ? result->str : NULL;
|
char *name = n;
|
||||||
|
while (1)
|
||||||
|
{ /* repeat until a valid (non nil) variable */
|
||||||
|
result = tree_next(constant_root, name);
|
||||||
|
if (result == NULL) return NULL;
|
||||||
|
if (result->varindex != NOT_USED &&
|
||||||
|
s_tag(result->varindex) != LUA_T_NIL)
|
||||||
|
return result;
|
||||||
|
name = result->ts.str;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** 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);
|
|
||||||
}
|
|
||||||
|
|||||||
34
tree.h
34
tree.h
@@ -1,27 +1,37 @@
|
|||||||
/*
|
/*
|
||||||
** tree.h
|
** tree.h
|
||||||
** TecCGraf - PUC-Rio
|
** TecCGraf - PUC-Rio
|
||||||
** $Id: $
|
** $Id: tree.h,v 1.8 1994/12/20 21:20:36 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
|
unsigned long hash; /* 0 if not initialized */
|
||||||
|
char marked; /* for garbage collection */
|
||||||
|
char str[1]; /* \0 byte already reserved */
|
||||||
|
} TaggedString;
|
||||||
|
|
||||||
#define indexstring(s) (*(((Word *)s)-1))
|
typedef struct TreeNode
|
||||||
|
{
|
||||||
|
struct TreeNode *right;
|
||||||
|
struct TreeNode *left;
|
||||||
|
unsigned short varindex; /* != NOT_USED if this is a symbol */
|
||||||
|
unsigned short constindex; /* != NOT_USED if this is a constant */
|
||||||
|
TaggedString ts;
|
||||||
|
} TreeNode;
|
||||||
|
|
||||||
|
|
||||||
char *lua_strcreate (char *str);
|
TaggedString *lua_createstring (char *str);
|
||||||
char *lua_constcreate (char *str);
|
TreeNode *lua_constcreate (char *str);
|
||||||
char *lua_varcreate (char *str);
|
Long lua_strcollector (void);
|
||||||
void lua_strcollector (void);
|
TreeNode *lua_varnext (char *n);
|
||||||
char *lua_varnext (char *n);
|
|
||||||
char *lua_varname (Word index);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
31
types.h
Normal file
31
types.h
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
** TeCGraf - PUC-Rio
|
||||||
|
** $Id: types.h,v 1.2 1994/12/27 20:41:47 celes Exp roberto $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef types_h
|
||||||
|
#define types_h
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
|
#ifndef real
|
||||||
|
#define real float
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef int Bool; /* boolean values */
|
||||||
|
|
||||||
|
#define Byte lua_Byte /* some systems have Byte as a predefined type */
|
||||||
|
typedef unsigned char Byte; /* unsigned 8 bits */
|
||||||
|
|
||||||
|
#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
|
||||||
36
ugly.h
Normal file
36
ugly.h
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
** ugly.h
|
||||||
|
** TecCGraf - PUC-Rio
|
||||||
|
** $Id: $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ugly_h
|
||||||
|
#define ugly_h
|
||||||
|
|
||||||
|
/* This enum must have the same order of the array 'reserved' in lex.c */
|
||||||
|
|
||||||
|
enum {
|
||||||
|
U_and=128,
|
||||||
|
U_do,
|
||||||
|
U_else,
|
||||||
|
U_elseif,
|
||||||
|
U_end,
|
||||||
|
U_function,
|
||||||
|
U_if,
|
||||||
|
U_local,
|
||||||
|
U_nil,
|
||||||
|
U_not,
|
||||||
|
U_or,
|
||||||
|
U_repeat,
|
||||||
|
U_return,
|
||||||
|
U_then,
|
||||||
|
U_until,
|
||||||
|
U_while,
|
||||||
|
U_eq = '='+128,
|
||||||
|
U_le = '<'+128,
|
||||||
|
U_ge = '>'+128,
|
||||||
|
U_ne = '~'+128,
|
||||||
|
U_sc = '.'+128
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user