Imported from ../lua-3.2.tar.gz.
[lua.git] / src / luac / luac.c
blob68af1c7628559c8f2636d746f2326fb7f3ccc838
1 /*
2 ** $Id: luac.c,v 1.17 1999/07/02 19:34:26 lhf Exp $
3 ** lua compiler (saves bytecodes to files; also list binary files)
4 ** See Copyright Notice in lua.h
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include "luac.h"
11 #include "lparser.h"
12 #include "lstate.h"
13 #include "lzio.h"
15 #define OUTPUT "luac.out" /* default output file */
17 static FILE* efopen(char* name, char* mode);
18 static void doit(int undump, char* filename);
20 static int listing=0; /* list bytecodes? */
21 static int debugging=0; /* emit debug information? */
22 static int dumping=1; /* dump bytecodes? */
23 static int undumping=0; /* undump bytecodes? */
24 static int optimizing=0; /* optimize? */
25 static int parsing=0; /* parse only? */
26 static int testing=0; /* test integrity? */
27 static int verbose=0; /* tell user what is done */
28 static int native=0; /* save numbers in native format? */
29 static FILE* D; /* output file */
31 static void usage(char* op)
33 if (op) fprintf(stderr,"luac: unrecognized option '%s'\n",op);
34 fprintf(stderr,
35 "usage: luac [options] [filenames]. Available options are:\n"
36 " -c\t\tcompile (default)\n"
37 " -d\t\tgenerate debugging information\n"
38 " -D name\tpredefine 'name' for conditional compilation\n"
39 " -l\t\tlist (default for -u)\n"
40 " -n\t\tsave numbers in native format (file may not be portable)\n"
41 " -o file\toutput file for -c (default is \"" OUTPUT "\")\n"
42 " -O\t\toptimize\n"
43 " -p\t\tparse only\n"
44 " -q\t\tquiet (default for -c)\n"
45 " -t\t\ttest code integrity\n"
46 " -u\t\tundump\n"
47 " -U name\tundefine 'name' for conditional compilation\n"
48 " -v\t\tshow version information\n"
49 " -V\t\tverbose\n"
50 " -\t\tcompile \"stdin\"\n"
52 exit(1);
55 #define IS(s) (strcmp(argv[i],s)==0)
57 int main(int argc, char* argv[])
59 char* d=OUTPUT; /* output file name */
60 int i;
61 lua_open();
62 for (i=1; i<argc; i++)
64 if (argv[i][0]!='-') /* end of options */
65 break;
66 else if (IS("-")) /* end of options; use stdin */
67 break;
68 else if (IS("-c")) /* compile (and dump) */
70 dumping=1;
71 undumping=0;
73 else if (IS("-D")) /* $define */
75 TaggedString* s=luaS_new(argv[++i]);
76 s->u.s.globalval.ttype=LUA_T_NUMBER;
77 s->u.s.globalval.value.n=1;
79 else if (IS("-d")) /* debug */
80 debugging=1;
81 else if (IS("-l")) /* list */
82 listing=1;
83 else if (IS("-n")) /* native */
84 native=1;
85 else if (IS("-o")) /* output file */
86 d=argv[++i];
87 else if (IS("-O")) /* optimize */
88 optimizing=1;
89 else if (IS("-p")) /* parse only */
91 dumping=0;
92 parsing=1;
94 else if (IS("-q")) /* quiet */
95 listing=0;
96 else if (IS("-t")) /* test */
97 testing=1;
98 else if (IS("-u")) /* undump */
100 dumping=0;
101 undumping=1;
102 listing=1;
104 else if (IS("-U")) /* undefine */
106 TaggedString* s=luaS_new(argv[++i]);
107 s->u.s.globalval.ttype=LUA_T_NIL;
109 else if (IS("-v")) /* show version */
110 printf("%s %s\n(written by %s)\n\n",LUA_VERSION,LUA_COPYRIGHT,LUA_AUTHORS);
111 else if (IS("-V")) /* verbose */
112 verbose=1;
113 else /* unknown option */
114 usage(argv[i]);
116 --i; /* fake new argv[0] */
117 argc-=i;
118 argv+=i;
119 if (dumping || parsing)
121 if (argc<2) usage(NULL);
122 if (dumping)
124 for (i=1; i<argc; i++) /* play safe with output file */
125 if (IS(d)) luaL_verror("will not overwrite input file \"%s\"",d);
126 D=efopen(d,"wb"); /* must open in binary mode */
128 for (i=1; i<argc; i++) doit(0,IS("-")? NULL : argv[i]);
129 if (dumping) fclose(D);
131 if (undumping)
133 if (argc<2)
134 doit(1,OUTPUT);
135 else
136 for (i=1; i<argc; i++) doit(1,IS("-")? NULL : argv[i]);
138 return 0;
141 static void do_compile(ZIO* z)
143 TProtoFunc* Main;
144 if (optimizing) L->debug=0;
145 if (debugging) L->debug=1;
146 Main=luaY_parser(z);
147 if (optimizing) luaU_optchunk(Main);
148 if (listing) luaU_printchunk(Main);
149 if (testing) luaU_testchunk(Main);
150 if (dumping) luaU_dumpchunk(Main,D,native);
153 static void do_undump(ZIO* z)
155 for (;;)
157 TProtoFunc* Main=luaU_undump1(z);
158 if (Main==NULL) break;
159 if (optimizing) luaU_optchunk(Main);
160 if (listing) luaU_printchunk(Main);
161 if (testing) luaU_testchunk(Main);
165 static void doit(int undump, char* filename)
167 FILE* f= (filename==NULL) ? stdin : efopen(filename, undump ? "rb" : "r");
168 ZIO z;
169 char source[255+2]; /* +2 for '@' and '\0' */
170 luaL_filesource(source,filename,sizeof(source));
171 zFopen(&z,f,source);
172 if (verbose) fprintf(stderr,"%s\n",source+1);
173 if (undump) do_undump(&z); else do_compile(&z);
174 if (f!=stdin) fclose(f);
177 static FILE* efopen(char* name, char* mode)
179 FILE* f=fopen(name,mode);
180 if (f==NULL)
182 fprintf(stderr,"luac: cannot open %sput file ",mode[0]=='r' ? "in" : "out");
183 perror(name);
184 exit(1);
186 return f;