2 ** $Id: luac.c,v 1.54 2006/06/02 17:37:11 lhf Exp $
3 ** Lua compiler (saves bytecodes to files; also list bytecodes)
4 ** See Copyright Notice in lua.h
26 #define PROGNAME "luac" /* default program name */
27 #define OUTPUT PROGNAME ".out" /* default output file */
29 static int listing=0; /* list bytecodes? */
30 static int dumping=1; /* dump bytecodes? */
31 static int stripping=0; /* strip debug information? */
32 static char Output[]={ OUTPUT }; /* default output file name */
33 static const char* output=Output; /* actual output file name */
34 static const char* progname=PROGNAME; /* actual program name */
36 static void fatal(const char* message)
38 fprintf(stderr,"%s: %s\n",progname,message);
42 static void cannot(const char* what)
44 fprintf(stderr,"%s: cannot %s %s: %s\n",progname,what,output,strerror(errno));
48 static void usage(const char* message)
51 fprintf(stderr,"%s: unrecognized option " LUA_QS "\n",progname,message);
53 fprintf(stderr,"%s: %s\n",progname,message);
55 "usage: %s [options] [filenames].\n"
56 "Available options are:\n"
59 " -o name output to file " LUA_QL("name") " (default is \"%s\")\n"
61 " -s strip debug information\n"
62 " -v show version information\n"
63 " -- stop handling options\n",
68 #define IS(s) (strcmp(argv[i],s)==0)
70 static int doargs(int argc, char* argv[])
74 if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0];
75 for (i=1; i<argc; i++)
77 if (*argv[i]!='-') /* end of options; keep it */
79 else if (IS("--")) /* end of options; skip it */
82 if (version) ++version;
85 else if (IS("-")) /* end of options; use stdin */
87 else if (IS("-l")) /* list */
89 else if (IS("-o")) /* output file */
92 if (output==NULL || *output==0) usage(LUA_QL("-o") " needs argument");
93 if (IS("-")) output=NULL;
95 else if (IS("-p")) /* parse only */
97 else if (IS("-s")) /* strip debug information */
99 else if (IS("-v")) /* show version */
101 else /* unknown option */
104 if (i==argc && (listing || !dumping))
111 printf("%s %s\n",LUA_RELEASE,LUA_COPYRIGHT);
112 if (version==argc-1) exit(EXIT_SUCCESS);
117 #define toproto(L,i) (clvalue(L->top+(i))->l.p)
119 static const Proto* combine(lua_State* L, int n)
122 return toproto(L,-1);
126 Proto* f=luaF_newproto(L);
127 setptvalue2s(L,L->top,f); incr_top(L);
128 f->source=luaS_newliteral(L,"=(" PROGNAME ")");
131 f->code=luaM_newvector(L,pc,Instruction);
133 f->p=luaM_newvector(L,n,Proto*);
138 f->p[i]=toproto(L,i-n-1);
139 f->code[pc++]=CREATE_ABx(OP_CLOSURE,0,i);
140 f->code[pc++]=CREATE_ABC(OP_CALL,0,1,1);
142 f->code[pc++]=CREATE_ABC(OP_RETURN,0,1,0);
147 static int writer(lua_State* L, const void* p, size_t size, void* u)
150 return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0);
158 static int pmain(lua_State* L)
160 struct Smain* s = (struct Smain*)lua_touserdata(L, 1);
165 if (!lua_checkstack(L,argc)) fatal("too many input files");
166 for (i=0; i<argc; i++)
168 const char* filename=IS("-") ? NULL : argv[i];
169 if (luaL_loadfile(L,filename)!=0) fatal(lua_tostring(L,-1));
172 if (listing) luaU_print(f,listing>1);
175 FILE* D= (output==NULL) ? stdout : fopen(output,"wb");
176 if (D==NULL) cannot("open");
178 luaU_dump(L,f,writer,D,stripping);
180 if (ferror(D)) cannot("write");
181 if (fclose(D)) cannot("close");
186 int main(int argc, char* argv[])
190 int i=doargs(argc,argv);
192 if (argc<=0) usage("no input files given");
194 if (L==NULL) fatal("not enough memory for state");
197 if (lua_cpcall(L,pmain,&s)!=0) fatal(lua_tostring(L,-1));