3 ** lua compiler (saves bytecodes to files; also list binary files)
6 char* rcs_luac
="$Id: luac.c,v 1.23 1997/06/20 20:34:04 lhf Exp $";
15 static void compile(char* filename
);
16 static void undump(char* filename
);
18 static int listing
=0; /* list bytecodes? */
19 static int dumping
=1; /* dump bytecodes? */
20 static int undumping
=0; /* undump bytecodes? */
21 static FILE* D
; /* output file */
23 static void usage(void)
26 "usage: luac [-c | -u] [-d] [-l] [-p] [-q] [-v] [-o output] file ...\n"
27 " -c\tcompile (default)\n"
29 " -d\tgenerate debugging information\n"
30 " -l\tlist (default for -u)\n"
31 " -o\toutput file for -c (default \"luac.out\")\n"
33 " -q\tquiet (default for -c)\n"
34 " -v\tshow version information\n"
39 #define IS(s) (strcmp(argv[i],s)==0)
41 int main(int argc
, char* argv
[])
43 char* d
="luac.out"; /* default output file */
45 for (i
=1; i
<argc
; i
++)
47 if (argv
[i
][0]!='-') /* end of options */
49 else if (IS("-")) /* use stdin */
51 else if (IS("-c")) /* compile (and dump) */
56 else if (IS("-d")) /* debug */
58 else if (IS("-l")) /* list */
60 else if (IS("-o")) /* output file */
62 else if (IS("-p")) /* parse only (for timing purposes) */
64 else if (IS("-q")) /* quiet */
66 else if (IS("-u")) /* undump */
72 else if (IS("-v")) /* show version */
73 printf("%s %s\n(written by %s)\n\n",LUA_VERSION
,LUA_COPYRIGHT
,LUA_AUTHORS
);
74 else /* unknown option */
77 --i
; /* fake new argv[0] */
83 for (i
=1; i
<argc
; i
++) /* play safe with output file */
86 fprintf(stderr
,"luac: will not overwrite input file \"%s\"\n",d
);
89 D
=fopen(d
,"wb"); /* must open in binary mode */
92 fprintf(stderr
,"luac: cannot open ");
96 for (i
=1; i
<argc
; i
++) compile(IS("-")? NULL
: argv
[i
]);
104 for (i
=1; i
<argc
; i
++) undump(IS("-")? NULL
: argv
[i
]);
109 static void do_dump(TFunc
* Main
)
115 for (tf
=Main
; tf
!=NULL
; tf
=tf
->next
) PrintFunction(tf
,Main
);
120 for (tf
=Main
; tf
!=NULL
; tf
=tf
->next
) DumpFunction(tf
,D
);
122 for (tf
=Main
; tf
!=NULL
; )
130 static void do_compile(ZIO
* z
)
132 TFunc
* tf
=new(TFunc
);
135 tf
->fileName
=lua_parsedfile
;
140 static void compile(char* filename
)
142 FILE* f
= (filename
==NULL
) ? stdin
: fopen(filename
, "r");
145 fprintf(stderr
,"luac: cannot open ");
153 luaI_setparsedfile(filename
?filename
:"(stdin)");
159 static void do_undump(ZIO
* z
)
162 while ((Main
=luaI_undump1(z
)))
167 for (tf
=Main
; tf
!=NULL
; tf
=tf
->next
)
168 PrintFunction(tf
,Main
);
170 luaI_freefunc(Main
); /* TODO: free others */
174 static void undump(char* filename
)
176 FILE* f
= (filename
==NULL
) ? stdin
: fopen(filename
, "rb");
179 fprintf(stderr
,"luac: cannot open ");