2 ** $Id: luac.c,v 1.44a 2003/04/07 20:34:20 lhf Exp $
3 ** Lua compiler (saves bytecodes to files; also list bytecodes)
4 ** See Copyright Notice in lua.h
22 #define luaB_opentests(L)
26 #define PROGNAME "luac" /* program name */
29 #define OUTPUT "luac.out" /* default output file */
31 static int listing
=0; /* list bytecodes? */
32 static int dumping
=1; /* dump bytecodes? */
33 static int stripping
=0; /* strip debug information? */
34 static char Output
[]={ OUTPUT
}; /* default output file name */
35 static const char* output
=Output
; /* output file name */
36 static const char* progname
=PROGNAME
; /* actual program name */
38 static void fatal(const char* message
)
40 fprintf(stderr
,"%s: %s\n",progname
,message
);
44 static void cannot(const char* name
, const char* what
, const char* mode
)
46 fprintf(stderr
,"%s: cannot %s %sput file ",progname
,what
,mode
);
51 static void usage(const char* message
, const char* arg
)
55 fprintf(stderr
,"%s: ",progname
); fprintf(stderr
,message
,arg
); fprintf(stderr
,"\n");
58 "usage: %s [options] [filenames]. Available options are:\n"
61 " -o name output to file `name' (default is \"" OUTPUT
"\")\n"
63 " -s strip debug information\n"
64 " -v show version information\n"
65 " -- stop handling options\n",
70 #define IS(s) (strcmp(argv[i],s)==0)
72 static int doargs(int argc
, char* argv
[])
75 if (argv
[0]!=NULL
&& *argv
[0]!=0) progname
=argv
[0];
76 for (i
=1; i
<argc
; i
++)
78 if (*argv
[i
]!='-') /* end of options; keep it */
80 else if (IS("--")) /* end of options; skip it */
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("`-o' needs argument",NULL
);
94 else if (IS("-p")) /* parse only */
96 else if (IS("-s")) /* strip debug information */
98 else if (IS("-v")) /* show version */
100 printf("%s %s\n",LUA_VERSION
,LUA_COPYRIGHT
);
101 if (argc
==2) exit(EXIT_SUCCESS
);
103 else /* unknown option */
104 usage("unrecognized option `%s'",argv
[i
]);
106 if (i
==argc
&& (listing
|| !dumping
))
114 static Proto
* toproto(lua_State
* L
, int i
)
116 const Closure
* c
=(const Closure
*)lua_topointer(L
,i
);
120 static Proto
* combine(lua_State
* L
, int n
)
123 return toproto(L
,-1);
127 Proto
* f
=luaF_newproto(L
);
128 f
->source
=luaS_newliteral(L
,"=(" PROGNAME
")");
130 f
->p
=luaM_newvector(L
,n
,Proto
*);
133 f
->code
=luaM_newvector(L
,f
->sizecode
,Instruction
);
136 f
->p
[i
]=toproto(L
,i
-n
);
137 f
->code
[pc
++]=CREATE_ABx(OP_CLOSURE
,0,i
);
138 f
->code
[pc
++]=CREATE_ABC(OP_CALL
,0,1,1);
140 f
->code
[pc
++]=CREATE_ABC(OP_RETURN
,0,1,0);
145 static void strip(lua_State
* L
, Proto
* f
)
148 luaM_freearray(L
, f
->lineinfo
, f
->sizelineinfo
, int);
149 luaM_freearray(L
, f
->locvars
, f
->sizelocvars
, struct LocVar
);
150 luaM_freearray(L
, f
->upvalues
, f
->sizeupvalues
, TString
*);
151 f
->lineinfo
=NULL
; f
->sizelineinfo
=0;
152 f
->locvars
=NULL
; f
->sizelocvars
=0;
153 f
->upvalues
=NULL
; f
->sizeupvalues
=0;
154 f
->source
=luaS_newliteral(L
,"=(none)");
155 for (i
=0; i
<n
; i
++) strip(L
,f
->p
[i
]);
158 static int writer(lua_State
* L
, const void* p
, size_t size
, void* u
)
161 return fwrite(p
,size
,1,(FILE*)u
)==1;
164 int main(int argc
, char* argv
[])
168 int i
=doargs(argc
,argv
);
170 if (argc
<=0) usage("no input files given",NULL
);
173 for (i
=0; i
<argc
; i
++)
175 const char* filename
=IS("-") ? NULL
: argv
[i
];
176 if (luaL_loadfile(L
,filename
)!=0) fatal(lua_tostring(L
,-1));
179 if (listing
) luaU_print(f
);
182 FILE* D
=fopen(output
,"wb");
183 if (D
==NULL
) cannot(output
,"open","out");
184 if (stripping
) strip(L
,f
);
186 luaU_dump(L
,f
,writer
,D
);
188 if (ferror(D
)) cannot(output
,"write","out");