/* ** $Id: luac.c,v 1.10 1998/07/12 00:38:30 lhf Exp $ ** lua compiler (saves bytecodes to files; also list binary files) ** See Copyright Notice in lua.h */ #include #include #include #include "luac.h" #include "lparser.h" #include "lzio.h" #include "luadebug.h" #define OUTPUT "luac.out" /* default output file */ extern void DumpChunk(TProtoFunc* Main, FILE* D); extern void PrintChunk(TProtoFunc* Main); extern void OptChunk(TProtoFunc* Main); static FILE* efopen(char* name, char* mode); static void doit(int undump, char* filename); static int listing=0; /* list bytecodes? */ static int debugging=0; /* debug? */ static int dumping=1; /* dump bytecodes? */ static int undumping=0; /* undump bytecodes? */ static int optimizing=0; /* optimize? */ static int parsing=0; /* parse only? */ static int verbose=0; /* tell user what is done */ static FILE* D; /* output file */ static void usage(void) { fprintf(stderr,"usage: " "luac [-c | -u] [-D name] [-d] [-l] [-o output] [-O] [-p] [-q] [-v] [-V] [files]\n" " -c\tcompile (default)\n" " -u\tundump\n" " -d\tgenerate debugging information\n" " -D\tpredefine symbol for conditional compilation\n" " -l\tlist (default for -u)\n" " -o\toutput file for -c (default is \"" OUTPUT "\")\n" " -O\toptimize\n" " -p\tparse only\n" " -q\tquiet (default for -c)\n" " -v\tshow version information\n" " -V\tverbose\n" " -\tcompile \"stdin\"\n" ); exit(1); } #define IS(s) (strcmp(argv[i],s)==0) int main(int argc, char* argv[]) { char* d=OUTPUT; /* output file name */ int i; lua_open(); for (i=1; iu.s.globalval.ttype=LUA_T_NUMBER; s->u.s.globalval.value.n=1; } else if (IS("-d")) /* debug */ debugging=1; else if (IS("-l")) /* list */ listing=1; else if (IS("-o")) /* output file */ d=argv[++i]; else if (IS("-O")) /* optimize */ optimizing=1; else if (IS("-p")) /* parse only */ { dumping=0; parsing=1; } else if (IS("-q")) /* quiet */ listing=0; else if (IS("-u")) /* undump */ { dumping=0; undumping=1; listing=1; } else if (IS("-v")) /* show version */ printf("%s %s\n(written by %s)\n\n",LUA_VERSION,LUA_COPYRIGHT,LUA_AUTHORS); else if (IS("-V")) /* verbose */ verbose=1; else /* unknown option */ usage(); } --i; /* fake new argv[0] */ argc-=i; argv+=i; if (dumping || parsing) { if (argc<2) usage(); if (dumping) { for (i=1; i