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
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
);
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"
44 " -q\t\tquiet (default for -c)\n"
45 " -t\t\ttest code integrity\n"
47 " -U name\tundefine 'name' for conditional compilation\n"
48 " -v\t\tshow version information\n"
50 " -\t\tcompile \"stdin\"\n"
55 #define IS(s) (strcmp(argv[i],s)==0)
57 int main(int argc
, char* argv
[])
59 char* d
=OUTPUT
; /* output file name */
62 for (i
=1; i
<argc
; i
++)
64 if (argv
[i
][0]!='-') /* end of options */
66 else if (IS("-")) /* end of options; use stdin */
68 else if (IS("-c")) /* compile (and dump) */
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 */
81 else if (IS("-l")) /* list */
83 else if (IS("-n")) /* native */
85 else if (IS("-o")) /* output file */
87 else if (IS("-O")) /* optimize */
89 else if (IS("-p")) /* parse only */
94 else if (IS("-q")) /* quiet */
96 else if (IS("-t")) /* test */
98 else if (IS("-u")) /* undump */
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 */
113 else /* unknown option */
116 --i
; /* fake new argv[0] */
119 if (dumping
|| parsing
)
121 if (argc
<2) usage(NULL
);
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
);
136 for (i
=1; i
<argc
; i
++) doit(1,IS("-")? NULL
: argv
[i
]);
141 static void do_compile(ZIO
* z
)
144 if (optimizing
) L
->debug
=0;
145 if (debugging
) L
->debug
=1;
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
)
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");
169 char source
[255+2]; /* +2 for '@' and '\0' */
170 luaL_filesource(source
,filename
,sizeof(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
);
182 fprintf(stderr
,"luac: cannot open %sput file ",mode
[0]=='r' ? "in" : "out");