diff options
author | Lua Team <team@lua.org> | 1998-07-11 12:00:00 +0000 |
---|---|---|
committer | repogen <> | 1998-07-11 12:00:00 +0000 |
commit | 377347776f1f3d820f92151f70bec667f96d5e6b (patch) | |
tree | cdb3ba26158df33547dfe765547177afcee119d1 /etc | |
parent | 4f8c5d0f284e1f4da717aea5008915f185cd2e05 (diff) | |
download | lua-github-377347776f1f3d820f92151f70bec667f96d5e6b.tar.gz |
Lua 3.13.1
Diffstat (limited to 'etc')
-rw-r--r-- | etc/Makefile | 24 | ||||
-rw-r--r-- | etc/README | 25 | ||||
-rw-r--r-- | etc/bin2c.c | 66 | ||||
-rw-r--r-- | etc/min.c | 13 | ||||
-rw-r--r-- | etc/setfallback.lua | 55 | ||||
-rw-r--r-- | etc/trace.c | 53 |
6 files changed, 236 insertions, 0 deletions
diff --git a/etc/Makefile b/etc/Makefile new file mode 100644 index 00000000..216d3ef6 --- /dev/null +++ b/etc/Makefile @@ -0,0 +1,24 @@ +# makefile for etc + +LUA= .. + +include $(LUA)/config + +ALL= bin2c min trace + +all: $(ALL) + +bin2c: bin2c.c + $(CC) -o $@ $< + +min: min.c $(LIB)/liblua.a + $(CC) $(CFLAGS) -o $@ $< -L$(LIB) -llua + +trace: trace.c $(LIB)/liblua.a + $(CC) $(CFLAGS) -o $@ $< -L$(LIB) -llua + +$(LIB)/liblua.a: + cd ../src; make + +clean: + rm -f $(ALL) diff --git a/etc/README b/etc/README new file mode 100644 index 00000000..fda602db --- /dev/null +++ b/etc/README @@ -0,0 +1,25 @@ +This directory contains some code that might be useful. + +bin2c.c + This program converts files to byte arrays that are automatically + run with lua_dobuffer. + This allows C programs to include all necessary Lua code, even in + precompiled form. + Even if code is include in source form, bin2c is useful because it + avoids the hassle of having to quote special characters in C strings. + Example of usage: run bin2c file1 file2 ... > init.h. The in your C + program, just do #include "init.h" anywhere in the *body* of a + function. This will be equivalent to calling + lua_dofile("file1"); lua_dofile("file2"); ... + +min.c + The smallest Lua interpreter possible. + +setfallback.lua + An implementation of fallbacks on top of tag methods. + Useful if you have Lua code written for version 2.5 or earlier. + If you have C code that uses lua_setfallback, then define LUA_COMPAT2_5 + before building Lua (see config). + +trace.c + A simple execution tracer. An example of how to use the debugging hooks. diff --git a/etc/bin2c.c b/etc/bin2c.c new file mode 100644 index 00000000..349b7eee --- /dev/null +++ b/etc/bin2c.c @@ -0,0 +1,66 @@ +/* +* bin2c.c +* convert binary files to byte arrays +* Luiz Henrique de Figueiredo (lhf@tecgraf.puc-rio.br) +* 25 Jun 98 10:55:12 +*/ + +#include <ctype.h> +#include <stdio.h> + +void dump(FILE* f, int n) +{ + printf("static unsigned char B%d[]={\n"); + for (n=1;;n++) + { + int c=getc(f); + if (c==EOF) break; +#if 0 + printf("0x%02x,",c); +#else + printf("%3u,",c); +#endif + if (n==20) { putchar('\n'); n=0; } + } + printf("\n};\n\n"); +} + +void fdump(char* fn, int n) +{ + FILE* f= (fn==NULL) ? stdin : fopen(fn,"rb"); /* must open in binary mode */ + if (f==NULL) + { + fprintf(stderr,"bin2c: cannot open "); + perror(fn); + exit(1); + } + else + { + if (fn!=NULL) printf("/* %s */\n",fn); + dump(f,n); + fclose(f); + } +} + +void emit(char* fn, int n) +{ + printf(" lua_dobuffer(B%d,sizeof(B%d),\"%s\");\n",n,n,fn); +} + +int main(int argc, char* argv[]) +{ + printf("{\n"); + if (argc<2) + { + dump(stdin,0); + emit("(stdin)",0); + } + else + { + int i; + for (i=1; i<argc; i++) fdump(argv[i],i); + for (i=1; i<argc; i++) emit(argv[i],i); + } + printf("}\n"); + return 0; +} diff --git a/etc/min.c b/etc/min.c new file mode 100644 index 00000000..7d45f5b6 --- /dev/null +++ b/etc/min.c @@ -0,0 +1,13 @@ +/* +* min.c +* a minimal Lua interpreter. loads stdin only. +* no standard library, only builtin functions. +*/ + +#include "lua.h" + +int main(void) +{ + lua_open(); + return lua_dofile(0); +} diff --git a/etc/setfallback.lua b/etc/setfallback.lua new file mode 100644 index 00000000..783b8667 --- /dev/null +++ b/etc/setfallback.lua @@ -0,0 +1,55 @@ +-------------------------------------------------------------- +-- Definition of "setfallback" using tag methods +-- (for compatibility with old code) +-------------------------------------------------------------- + + +-- default fallbacks for each event: +local defaults = { + gettable = function () error('indexed expression not a table') end, + settable = function () error('indexed expression not a table') end, + index = function () return nil end, + getglobal = function () return nil end, + arith = function () error('number expected in arithmetic operation') end, + order = function () error('incompatible types in comparison') end, + concat = function () error('string expected in concatenation') end, + gc = function () return nil end, + ['function'] = function () error('called expression not a function') end, + error = function (s) write(_STDERR, s, '\n') end, +} + + +function setfallback (name, func) + + -- set the given function as the tag method for all "standard" tags + -- (since some combinations may cause errors, use call to avoid messages) + local fillvalids = function (n, func) + call(settagmethod, {0, n, func}, 'x', nil) + call(settagmethod, {tag(0), n, func}, 'x', nil) + call(settagmethod, {tag(''), n, func}, 'x', nil) + call(settagmethod, {tag{}, n, func}, 'x', nil) + call(settagmethod, {tag(function () end), n, func}, 'x', nil) + call(settagmethod, {tag(settagmethod), n, func}, 'x', nil) + call(settagmethod, {tag(nil), n, func}, 'x', nil) + end + + assert(type(func) == 'function') + local oldfunc + if name == 'error' then + oldfunc = seterrormethod(func) + elseif name == 'getglobal' then + oldfunc = settagmethod(tag(nil), 'getglobal', func) + elseif name == 'arith' then + oldfunc = gettagmethod(tag(0), 'pow') + foreach({"add", "sub", "mul", "div", "unm", "pow"}, + function(_, n) %fillvalids(n, %func) end) + elseif name == 'order' then + oldfunc = gettagmethod(tag(nil), 'lt') + foreach({"lt", "gt", "le", "ge"}, + function(_, n) %fillvalids(n, %func) end) + else + oldfunc = gettagmethod(tag(nil), name) + fillvalids(name, func) + end + return oldfunc or rawgettable(%defaults, name) +end diff --git a/etc/trace.c b/etc/trace.c new file mode 100644 index 00000000..1da38a1c --- /dev/null +++ b/etc/trace.c @@ -0,0 +1,53 @@ +/* +* trace.c +* a simple execution tracer +*/ + +#include <stdio.h> +#include <string.h> +#include "lua.h" +#include "luadebug.h" + +static FILE* P; /* output file */ +static int L=0; /* indentation level */ + +static void linehook(int line) +{ + fprintf(P,"%*sLINE(%d)\t-- %d\n",L,"",line,L); +} + +static void callhook(lua_Function func, char* file, int line) +{ + fprintf(P,"%*sCALL('%s',%d)\t-- %d\n",L,"",file,line,L); + if (line==0 && strcmp(file,"(return)")==0) --L; else ++L; +} + +void start_trace(void) +{ + lua_linehook=linehook; + lua_callhook=callhook; + lua_debug=1; +#if 0 + P=fopen("trace.out","w"); +#else + P=stderr; +#endif +} + +void stop_trace(void) +{ + lua_linehook=NULL; + lua_callhook=NULL; + lua_debug=0; + fclose(P); +} + +int main(void) +{ + int rc; + lua_open(); + start_trace(); + rc=lua_dofile(0); + stop_trace(); + return rc; +} |