From: John L. <jr...@us...> - 2005-06-06 23:06:24
|
Update of /cvsroot/wxlua/wxLua/modules/lua/etc In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14733/wxLua/modules/lua/etc Added Files: Makefile README bin2c.c compat.lua doall.lua lua.ico lua.magic lua.xpm luser_number.h luser_tests.h min.c noparser.c saconfig.c trace.c Log Message: moved files to the modules directory structure --- NEW FILE: min.c --- /* * min.c -- a minimal Lua interpreter * loads stdin only with minimal error handling. * no interaction, and no standard library, only a "print" function. */ #include <stdio.h> #include "lua.h" static int print(lua_State *L) { int n=lua_gettop(L); int i; for (i=1; i<=n; i++) { if (i>1) printf("\t"); if (lua_isstring(L,i)) printf("%s",lua_tostring(L,i)); else if (lua_isnil(L,i)) printf("%s","nil"); else if (lua_isboolean(L,i)) printf("%s",lua_toboolean(L,i) ? "true" : "false"); else printf("%s:%p",lua_typename(L,lua_type(L,i)),lua_topointer(L,i)); } printf("\n"); return 0; } static const char *getF(lua_State *L, void *ud, size_t *size) { FILE *f=(FILE *)ud; static char buff[512]; if (feof(f)) return NULL; *size=fread(buff,1,sizeof(buff),f); return (*size>0) ? buff : NULL; } int main(void) { lua_State *L=lua_open(); lua_register(L,"print",print); if (lua_load(L,getF,stdin,"=stdin") || lua_pcall(L,0,0,0)) fprintf(stderr,"%s\n",lua_tostring(L,-1)); return 0; } --- NEW FILE: lua.magic --- # Lua precompiled files. Versions 2.3 and 4.1 were never officially released. 0 string \33Lua precompiled chunk for Lua >4 byte 0x23 2.3* >4 byte 0x24 2.4 >4 byte 0x25 2.5 >4 byte 0x30 3.0 >4 byte 0x31 3.1 >4 byte 0x32 3.2 >4 byte 0x40 4.0 >4 byte 0x41 4.1* >4 byte 0x50 5.0 --- NEW FILE: noparser.c --- /* * The code below can be used to make a Lua core that does not contain the * parsing modules (lcode, llex, lparser), which represent 35% of the total core. * You'll only be able to load binary files and strings, precompiled with luac. * (Of course, you'll have to build luac with the original parsing modules!) * * To use this module, simply compile it ("make noparser" does that) and * list its object file before the Lua libraries. The linker should then not * load the parsing modules. To try it, do "make luab". */ #include "llex.h" #include "lparser.h" #include "lzio.h" void luaX_init (lua_State *L) { UNUSED(L); } Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff) { UNUSED(z); UNUSED(buff); lua_pushstring(L,"parser not loaded"); lua_error(L); return NULL; } --- NEW FILE: saconfig.c --- /* sa-config.c -- configuration for stand-alone Lua interpreter * * #define LUA_USERCONFIG to this file * * Here are the features that can be customized using #define: * *** Line edit and history: * #define USE_READLINE to use the GNU readline library. * * To use another library for this, use the code below as a start. * Make sure you #define lua_readline and lua_saveline accordingly. * If you do not #define lua_readline, you'll get a version based on fgets * that uses a static buffer of size MAXINPUT. * * *** Static Lua libraries to be loaded at startup: * #define lua_userinit(L) to a Lua function that loads libraries; typically * #define lua_userinit(L) openstdlibs(L);myinit(L) * or * #define lua_userinit(L) myinit(L) * * Another way is to add the prototypes of the init functions here and * #define LUA_EXTRALIBS accordingly. For example, * #define LUA_EXTRALIBS {"mylib","luaopen_mylib"}, * Note the ending comma! * * *** Prompts: * The stand-alone Lua interpreter uses two prompts: PROMPT and PROMPT2. * PROMPT is the primary prompt, shown when the intepreter is ready to receive * a new statement. PROMPT2 is the secondary prompt, shown while a statement * is being entered but is still incomplete. * * *** Program name: * Error messages usually show argv[0] as a program name. In systems that do * not give a valid string as argv[0], error messages show PROGNAME instead. * * */ #ifdef USE_READLINE /* * This section implements of lua_readline and lua_saveline for lua.c using * the GNU readline and history libraries. It should also work with drop-in * replacements such as editline and libedit (you may have to include * different headers, though). * */ #define lua_readline myreadline #define lua_saveline mysaveline #include <ctype.h> #include <readline/readline.h> #include <readline/history.h> static int myreadline (lua_State *L, const char *prompt) { char *s=readline(prompt); if (s==NULL) return 0; else { lua_pushstring(L,s); lua_pushliteral(L,"\n"); lua_concat(L,2); free(s); return 1; } } static void mysaveline (lua_State *L, const char *s) { const char *p; for (p=s; isspace(*p); p++) ; if (*p!=0) { size_t n=strlen(s)-1; if (s[n]!='\n') add_history(s); else { lua_pushlstring(L,s,n); s=lua_tostring(L,-1); add_history(s); lua_remove(L,-1); } } } #endif --- NEW FILE: lua.ico --- (This appears to be a binary file; contents omitted.) --- NEW FILE: luser_tests.h --- /* ** $Id: luser_tests.h,v 1.1 2005/06/06 23:06:15 jrl1 Exp $ ** Internal Header for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ #ifndef ltests_h #define ltests_h #include <stdlib.h> #define LUA_DEBUG #define LUA_OPNAMES #undef NDEBUG #include <assert.h> #define lua_assert(c) assert(c) #define check_exp(c,e) (lua_assert(c), (e)) #define api_check(L, o) lua_assert(o) /* to avoid warnings, and to make sure value is really unused */ #define UNUSED(x) (x=0, (void)(x)) /* memory allocator control variables */ extern unsigned long memdebug_numblocks; extern unsigned long memdebug_total; extern unsigned long memdebug_maxmem; extern unsigned long memdebug_memlimit; #define l_realloc(b, os, s) debug_realloc(b, os, s) #define l_free(b, os) debug_realloc(b, os, 0) void *debug_realloc (void *block, size_t oldsize, size_t size); /* test for lock/unlock */ extern int islocked; #define LUA_USERSTATE int * #define getlock(l) (*(cast(LUA_USERSTATE *, l) - 1)) #define lua_userstateopen(l) if (l != NULL) getlock(l) = &islocked; #define lua_lock(l) lua_assert((*getlock(l))++ == 0) #define lua_unlock(l) lua_assert(--(*getlock(l)) == 0) int luaB_opentests (lua_State *L); #define LUA_EXTRALIBS { "tests", luaB_opentests }, /* real main will be defined at `ltests.c' */ int l_main (int argc, char *argv[]); #define main l_main /* change some sizes to give some bugs a chance */ #define LUAL_BUFFERSIZE 27 #define MINSTRTABSIZE 2 #endif --- NEW FILE: lua.xpm --- /* XPM */ static char *magick[] = { /* columns rows colors chars-per-pixel */ "32 32 6 1", " c Gray0", ". c #000000008080", "X c #808080808080", "o c #c0c0c0c0c0c0", "O c Gray100", "+ c None", /* pixels */ "++++++++++++++++++++++++++ooo+++", "++++++++++++++++++++++++oX...Xo+", "++++++++++++++++++++++++X.....X+", "+++++++++++++++++++++++o.......o", "+++++++++XX......XX++++o.......o", "+++++++X............X++o.......o", "+++++o................o+X.....X+", "++++X..................XoX...Xo+", "+++X..............XXX...X+ooo+++", "++o.............XoOOOoX..o++++++", "++..............oOOOOOo...++++++", "+X.............XOOOOOOOX..X+++++", "+..............XOOOOOOOX...+++++", "X..............XOOOOOOOX...X++++", "X...............oOOOOOo....X++++", "................XoOOOoX.....++++", "....XO............XXX.......++++", "....XO......................++++", "....XO.....OX..OX.XOOOo.....++++", "....XO.....OX..OX.OoXXOX....++++", "....XO.....OX..OX....XOX....++++", "X...XO.....OX..OX..OOoOX...X++++", "X...XO.....OX..OX.OX..OX...X++++", "+...XOXXXX.OoXoOX.OXXXOX...+++++", "+X..XOOOOO.XOOXOX.XOOOXo..X+++++", "++........................++++++", "++o......................o++++++", "+++X....................X+++++++", "++++X..................X++++++++", "+++++o................o+++++++++", "+++++++X............X+++++++++++", "+++++++++XX......XX+++++++++++++" }; --- NEW FILE: bin2c.c --- /* * bin2c.c * convert files to byte arrays for automatic loading with lua_dobuffer * Luiz Henrique de Figueiredo (lh...@te...) * 02 Apr 2003 20:44:31 */ #include <ctype.h> #include <stdio.h> #include <stdlib.h> static void dump(FILE* f, int n) { printf("static const unsigned char B%d[]={\n",n); for (n=1;;n++) { int c=getc(f); if (c==EOF) break; printf("%3u,",c); if (n==20) { putchar('\n'); n=0; } } printf("\n};\n\n"); } static void fdump(const char* fn, int n) { FILE* f= fopen(fn,"rb"); /* must open in binary mode */ if (f==NULL) { fprintf(stderr,"bin2c: cannot open "); perror(fn); exit(1); } else { printf("/* %s */\n",fn); dump(f,n); fclose(f); } } static void emit(const char* fn, int n) { printf(" lua_dobuffer(L,(const char*)B%d,sizeof(B%d),\"%s\");\n",n,n,fn); } int main(int argc, char* argv[]) { printf("/* code automatically generated by bin2c -- DO NOT EDIT */\n"); printf("{\n"); if (argc<2) { dump(stdin,0); emit("=stdin",0); } else { int i; printf("/* #include'ing this file in a C program is equivalent to calling\n"); for (i=1; i<argc; i++) printf(" lua_dofile(L,\"%s\");\n",argv[i]); printf("*/\n"); for (i=1; i<argc; i++) fdump(argv[i],i); for (i=1; i<argc; i++) emit(argv[i],i); } printf("}\n"); return 0; } --- NEW FILE: doall.lua --- -- emulate the command line behaviour of Lua 4.0 -- usage: lua doall.lua f1.lua f2.lua f3.lua ... for i=1,table.getn(arg) do dofile(arg[i]) end --- NEW FILE: trace.c --- /* * trace.c -- a simple execution tracer for Lua */ #include <stdio.h> #include <string.h> #include "lua.h" #include "lualib.h" #include "lauxlib.h" static FILE* LOG; /* log file */ static int I=0; /* indentation level */ static void hook(lua_State *L, lua_Debug *ar) { const char* s=""; switch (ar->event) { case LUA_HOOKTAILRET: ar->event=LUA_HOOKRET; case LUA_HOOKRET: s="return"; break; case LUA_HOOKCALL: s="call"; break; case LUA_HOOKLINE: s="line"; break; default: break; } fprintf(LOG,"[%d]\t%*s%s\t-- %d\n",I,I,"",s,ar->currentline); if (ar->event==LUA_HOOKCALL) ++I; else if (ar->event==LUA_HOOKRET) --I; } static void start_trace(lua_State *L, FILE* logfile) { lua_sethook(L,hook,LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE, 0); LOG=logfile; } static void stop_trace(lua_State *L) { lua_sethook(L,NULL,0,0); fclose(LOG); } int main(void) { int rc; lua_State *L=lua_open(); lua_baselibopen(L); lua_tablibopen(L); lua_iolibopen(L); lua_strlibopen(L); lua_mathlibopen(L); lua_dblibopen(L); start_trace(L,stderr); rc=lua_dofile(L,NULL); stop_trace(L); return rc; } --- NEW FILE: Makefile --- # makefile for Lua etc LUA= .. include $(LUA)/config LIBLUA=$(LIB)/liblua.a ALL= bin2c min trace noparser luab all: @echo 'choose a target:' $(ALL) bin2c: bin2c.c $(CC) $(CFLAGS) -o $@ $@.c min: min.c $(LIBLUA) $(CC) $(CFLAGS) -o $@ $@.c -L$(LIB) -llua trace: trace.c $(LIBLUA) $(CC) -g $(CFLAGS) -o $@ $@.c -L$(LIB) -llua -llualib $(EXTRA_LIBS) noparser: noparser.c $(CC) $(CFLAGS) -I$(LUA)/src -o $@.o -c $@.c luab: noparser $(LIBLUA) cc -o $@ noparser.o $(LUA)/src/lua/lua.o -L$(LIB) -llua -llualib $(EXTRA_LIBS) $(BIN)/luac $(LUA)/test/hello.lua $@ luac.out -$@ -e'a=1' flat: cd ..; mkdir flat; mv include/*.h src/*.[ch] src/*/*.[ch] flat $(LIBLUA): cd ../src; $(MAKE) clean: rm -f $(ALL) a.out core *.o luac.out luser_tests.h: RCS/ltests.h,v co -q -M ltests.h mv -f ltests.h $@ --- NEW FILE: luser_number.h --- /* luser_number.h -- number type configuration for Lua core * * #define LUA_USER_H to this file and #define one of USE_* below */ #ifdef USE_DOUBLE #define LUA_NUMBER double #define LUA_NUMBER_SCAN "%lf" #define LUA_NUMBER_FMT "%.14g" #endif #ifdef USE_FLOAT #define LUA_NUMBER float #define LUA_NUMBER_SCAN "%f" #define LUA_NUMBER_FMT "%.5g" #endif #ifdef USE_LONG #define LUA_NUMBER long #define LUA_NUMBER_SCAN "%ld" #define LUA_NUMBER_FMT "%ld" #define lua_str2number(s,p) strtol((s), (p), 10) #endif #ifdef USE_INT #define LUA_NUMBER int #define LUA_NUMBER_SCAN "%d" #define LUA_NUMBER_FMT "%d" #define lua_str2number(s,p) ((int) strtol((s), (p), 10)) #endif #ifdef USE_FASTROUND #define lua_number2int(i,d) __asm__("fldl %1\nfistpl %0":"=m"(i):"m"(d)) #endif --- NEW FILE: README --- This directory contains some useful files and code. Unlike the code in ../src, everything here is in the public domain. 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 the code is included 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. Then, in your C program, just do #include "init.h" anywhere in the *body* of a function. This will be equivalent to calling lua_dofile(L,"file1"); lua_dofile(L,"file2"); ... Note that the Lua state is called "L". If you use a different name, say "mystate", just #define L mystate before you #include "init.h". compat.lua A compatibility module for Lua 4.0 functions. doall.lua Emulate the command line behaviour of Lua 4.0 lua.ico A Lua icon for Windows. Drawn by hand by Markus Gritsch <gr...@iu...>. lua.magic Data for teaching file(1) about Lua precompiled chunks. lua.xpm The same icon as lua.ico, but in XPM format. It was converted with ImageMagick by Andy Tai <an...@ex...>. luser_number.h Number type configuration for Lua core. luser_tests.h Self-test configuration for Lua core. min.c A minimal Lua interpreter. Good for learning and for starting your own. noparser.c Linking with noparser.o avoids loading the parsing modules in lualib.a. Do "make luab" to build a sample Lua intepreter that does not parse Lua programs, only loads precompiled programs. saconfig.c Configuration for Lua interpreter. trace.c A simple execution tracer. An example of how to use the debug hooks in C. --- NEW FILE: compat.lua --- ------------------------------------------------------------------- -- Real globals -- _ALERT -- _ERRORMESSAGE -- _VERSION -- _G -- assert -- error -- metatable -- next -- print -- require -- tonumber -- tostring -- type -- unpack ------------------------------------------------------------------- -- collectgarbage -- gcinfo -- globals -- call -> protect(f, err) -- loadfile -- loadstring -- rawget -- rawset -- getargs = Main.getargs ?? function do_ (f, err) if not f then print(err); return end local a,b = pcall(f) if not a then print(b); return nil else return b or true end end function dostring(s) return do_(loadstring(s)) end -- function dofile(s) return do_(loadfile(s)) end ------------------------------------------------------------------- -- Table library local tab = table foreach = tab.foreach foreachi = tab.foreachi getn = tab.getn tinsert = tab.insert tremove = tab.remove sort = tab.sort ------------------------------------------------------------------- -- Debug library local dbg = debug getinfo = dbg.getinfo getlocal = dbg.getlocal setcallhook = function () error"`setcallhook' is deprecated" end setlinehook = function () error"`setlinehook' is deprecated" end setlocal = dbg.setlocal ------------------------------------------------------------------- -- math library local math = math abs = math.abs acos = function (x) return math.deg(math.acos(x)) end asin = function (x) return math.deg(math.asin(x)) end atan = function (x) return math.deg(math.atan(x)) end atan2 = function (x,y) return math.deg(math.atan2(x,y)) end ceil = math.ceil cos = function (x) return math.cos(math.rad(x)) end deg = math.deg exp = math.exp floor = math.floor frexp = math.frexp ldexp = math.ldexp log = math.log log10 = math.log10 max = math.max min = math.min mod = math.mod PI = math.pi --??? pow = math.pow rad = math.rad random = math.random randomseed = math.randomseed sin = function (x) return math.sin(math.rad(x)) end sqrt = math.sqrt tan = function (x) return math.tan(math.rad(x)) end ------------------------------------------------------------------- -- string library local str = string strbyte = str.byte strchar = str.char strfind = str.find format = str.format gsub = str.gsub strlen = str.len strlower = str.lower strrep = str.rep strsub = str.sub strupper = str.upper ------------------------------------------------------------------- -- os library clock = os.clock date = os.date difftime = os.difftime execute = os.execute --? exit = os.exit getenv = os.getenv remove = os.remove rename = os.rename setlocale = os.setlocale time = os.time tmpname = os.tmpname ------------------------------------------------------------------- -- compatibility only getglobal = function (n) return _G[n] end setglobal = function (n,v) _G[n] = v end ------------------------------------------------------------------- local io, tab = io, table -- IO library (files) _STDIN = io.stdin _STDERR = io.stderr _STDOUT = io.stdout _INPUT = io.stdin _OUTPUT = io.stdout seek = io.stdin.seek -- sick ;-) tmpfile = io.tmpfile closefile = io.close openfile = io.open function flush (f) if f then f:flush() else _OUTPUT:flush() end end function readfrom (name) if name == nil then local f, err, cod = io.close(_INPUT) _INPUT = io.stdin return f, err, cod else local f, err, cod = io.open(name, "r") _INPUT = f or _INPUT return f, err, cod end end function writeto (name) if name == nil then local f, err, cod = io.close(_OUTPUT) _OUTPUT = io.stdout return f, err, cod else local f, err, cod = io.open(name, "w") _OUTPUT = f or _OUTPUT return f, err, cod end end function appendto (name) local f, err, cod = io.open(name, "a") _OUTPUT = f or _OUTPUT return f, err, cod end function read (...) local f = _INPUT if type(arg[1]) == 'userdata' then f = tab.remove(arg, 1) end return f:read(unpack(arg)) end function write (...) local f = _OUTPUT if type(arg[1]) == 'userdata' then f = tab.remove(arg, 1) end return f:write(unpack(arg)) end |