2 ** $Id: luac.c,v 1.28 2000/11/06 20:06:27 lhf Exp $
3 ** lua compiler (saves bytecodes to files; also list binary files)
4 ** See Copyright Notice in lua.h
16 #define OUTPUT "luac.out" /* default output file */
18 static void usage(const char* message
, const char* arg
);
19 static int doargs(int argc
, const char* argv
[]);
20 static Proto
* load(const char* filename
);
21 static FILE* efopen(const char* name
, const char* mode
);
22 static void strip(Proto
* tf
);
23 static Proto
* combine(Proto
** P
, int n
);
25 lua_State
* lua_state
=NULL
; /* lazy! */
27 static int listing
=0; /* list bytecodes? */
28 static int dumping
=1; /* dump bytecodes? */
29 static int stripping
=0; /* strip debug information? */
30 static int testing
=0; /* test integrity? */
31 static const char* output
=OUTPUT
; /* output file name */
33 #define IS(s) (strcmp(argv[i],s)==0)
35 int main(int argc
, const char* argv
[])
38 int i
=doargs(argc
,argv
);
40 if (argc
<=0) usage("no input files given",NULL
);
42 P
=luaM_newvector(L
,argc
,Proto
*);
43 for (i
=0; i
<argc
; i
++)
44 P
[i
]=load(IS("-")? NULL
: argv
[i
]);
46 if (dumping
) luaU_optchunk(tf
);
47 if (listing
) luaU_printchunk(tf
);
48 if (testing
) luaU_testchunk(tf
);
51 if (stripping
) strip(tf
);
52 luaU_dumpchunk(tf
,efopen(output
,"wb"));
57 static void usage(const char* message
, const char* arg
)
61 fprintf(stderr
,"luac: "); fprintf(stderr
,message
,arg
); fprintf(stderr
,"\n");
64 "usage: luac [options] [filenames]. Available options are:\n"
67 " -o file output file (default is \"" OUTPUT
"\")\n"
69 " -s strip debug information\n"
70 " -t test code integrity\n"
71 " -v show version information\n"
76 static int doargs(int argc
, const char* argv
[])
79 for (i
=1; i
<argc
; i
++)
81 if (*argv
[i
]!='-') /* end of options */
83 else if (IS("-")) /* end of options; use stdin */
85 else if (IS("-l")) /* list */
87 else if (IS("-o")) /* output file */
90 if (output
==NULL
) usage(NULL
,NULL
);
92 else if (IS("-p")) /* parse only */
94 else if (IS("-s")) /* strip debug information */
96 else if (IS("-t")) /* test */
101 else if (IS("-v")) /* show version */
103 printf("%s %s\n",LUA_VERSION
,LUA_COPYRIGHT
);
104 if (argc
==2) exit(0);
106 else /* unknown option */
107 usage("unrecognized option `%s'",argv
[i
]);
109 if (i
==argc
&& (listing
|| testing
))
117 static Proto
* load(const char* filename
)
130 f
=efopen(filename
,"r");
131 c
=ungetc(fgetc(f
),f
);
134 fprintf(stderr
,"luac: cannot read from ");
138 undump
=(c
==ID_CHUNK
);
139 if (undump
&& f
!=stdin
)
142 f
=efopen(filename
,"rb");
144 sprintf(source
,"@%.*s",Sizeof(source
)-2,filename
);
145 luaZ_Fopen(&z
,f
,source
);
146 tf
= undump
? luaU_undump(L
,&z
) : luaY_parser(L
,&z
);
147 if (f
!=stdin
) fclose(f
);
151 static Proto
* combine(Proto
** P
, int n
)
158 Proto
* tf
=luaF_newproto(L
);
159 tf
->source
=luaS_new(L
,"=(luac)");
164 tf
->code
=luaM_newvector(L
,tf
->ncode
,Instruction
);
167 tf
->code
[pc
++]=CREATE_AB(OP_CLOSURE
,i
,0);
168 tf
->code
[pc
++]=CREATE_AB(OP_CALL
,0,0);
170 tf
->code
[pc
++]=OP_END
;
175 static void strip(Proto
* tf
)
180 tf
->source
=luaS_new(L
,"=(none)");
183 for (i
=0; i
<n
; i
++) strip(tf
->kproto
[i
]);
186 static FILE* efopen(const char* name
, const char* mode
)
188 FILE* f
=fopen(name
,mode
);
191 fprintf(stderr
,"luac: cannot open %sput file ",*mode
=='r' ? "in" : "out");
198 void luaU_testchunk(const Proto
* Main
)
201 fprintf(stderr
,"luac: -t not operational in this version\n");