migrate substitution keywords to SVN
[reactos.git] / reactos / lib / kjs / jsas / main.js
1 /*
2 * Assembler for JavaScript assembler.
3 * Copyright (c) 1998 New Generation Software (NGS) Oy
4 *
5 * Author: Markku Rossi <mtr@ngs.fi>
6 */
7
8 /*
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the Free
21 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * MA 02111-1307, USA
23 */
24
25 /*
26 * $Source: /cygdrive/c/RCVS/CVS/ReactOS/reactos/lib/kjs/jsas/main.js,v $
27 * $Id$
28 */
29
30 /*
31 * Variables and definitions.
32 */
33
34 version_number = "0.0.1";
35
36 /*
37 * Options.
38 */
39
40 /*
41 * -g, --debug
42 *
43 * Generate debugging information.
44 */
45
46 opt_debug = false;
47
48 /*
49 * -h, --help
50 *
51 * Print short help and exit successfully.
52 */
53
54 /*
55 * -O, --optimize
56 *
57 * Optimize the assembler.
58 */
59
60 opt_optimize = false;
61
62 /*
63 * -v, --verbose
64 *
65 * Tell what we are doing.
66 */
67
68 opt_verbose = false;
69
70 /*
71 * -V, --version
72 *
73 * Print version information and exit successfully.
74 */
75
76 /*
77 * Functions.
78 */
79
80 function main ()
81 {
82 var idx = ARGS[0].lastIndexOf ("/");
83 if (idx >= 0)
84 program = ARGS[0].substr (idx + 1);
85 else
86 program = ARGS[0];
87
88 /* Handle arguments. */
89 var i;
90 for (i = 1; i < ARGS.length; i++)
91 {
92 if (ARGS[i][0] == #'-')
93 {
94 if (ARGS[i] == "-g" || ARGS[i] == "--debug")
95 opt_debug = true;
96 else if (ARGS[i] == "-h" || ARGS[i] == "--help")
97 {
98 usage ();
99 System.exit (1);
100 }
101 else if (ARGS[i] == "-O" || ARGS[i] == "--optimize")
102 opt_optimize = true;
103 else if (ARGS[i] == "-v" || ARGS[i] == "--verbose")
104 opt_verbose = true;
105 else if (ARGS[i] == "-V" || ARGS[i] == "--version")
106 {
107 version ();
108 System.exit (0);
109 }
110 else
111 {
112 /* Unrecognized option. */
113 System.error (program, ": unrecognized option `",
114 ARGS[i], "'\n");
115 System.error ("Try `", program,
116 " --help' for more information.\n");
117 System.exit (1);
118 }
119 }
120 else
121 {
122 /* End of arguments. */
123 break;
124 }
125 }
126
127 if (i >= ARGS.length)
128 {
129 System.error (program, ": no files specified\n");
130 System.exit (1);
131 }
132
133 /* Options. */
134
135 JSC$verbose = opt_verbose;
136 JSC$generate_debug_info = opt_debug;
137
138 /* Process files. */
139 for (; i < ARGS.length; i++)
140 {
141 /* Reset the JSC's assembler package. */
142 JSC$asm_reset ();
143
144 /* Process the input file. */
145 JSC$filename = ARGS[i];
146 process_file (JSC$filename);
147
148 if (opt_optimize)
149 JSC$asm_optimize (JSC$FLAG_OPTIMIZE_MASK);
150
151 JSC$asm_finalize ();
152
153 var result = JSC$asm_bytecode ();
154
155 /* Create the output file name. */
156 var oname = ARGS[i].replace (/\.jas$/, ".jsc");
157 if (oname == ARGS[i])
158 oname += ".jsc";
159
160 var ostream = new File (oname);
161 if (ostream.open ("w"))
162 {
163 ostream.write (result);
164 ostream.close ();
165 }
166 else
167 {
168 System.stderr.writeln ("jsas: couldn't create bc file `"
169 + oname + "': "
170 + System.strerror (System.errno));
171 System.exit (1);
172 }
173 }
174 }
175
176 function usage ()
177 {
178 System.print ("\
179 Usage: ", program, " [OPTION]... FILE...\n\
180 Mandatory arguments to long options are mandatory for short options too.\n");
181
182 System.print ("\
183 -g, --debug generate debugging information\n\
184 -h, --help print this help and exit\n\
185 -O, --optimize optimize the assembler code\n\
186 -v, --verbose tell what the assembler is doing\n\
187 -V, --version print version number\n\
188 ");
189
190 System.print ("\nReport bugs to mtr@ngs.fi.\n");
191 }
192
193 function version ()
194 {
195 System.print ("NGS JavaScript assembler ", version_number, "\n");
196 System.print ("\
197 Copyright (C) 1998 New Generation Software (NGS) Oy.\n\
198 NGS JavaScript Interpreter comes with NO WARRANTY, to the extent\n\
199 permitted by law. You may redistribute copies of NGS JavaScript\n\
200 Interpreter under the terms of the GNU Library General Public License.\n\
201 For more information about these matters, see the files named COPYING.\n\
202 ");
203 }
204
205 main ();
206
207 \f
208 /*
209 Local variables:
210 mode: c
211 End:
212 */