-
Notifications
You must be signed in to change notification settings - Fork 7
/
winxed_installed.winxed
299 lines (272 loc) · 8.14 KB
/
winxed_installed.winxed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#! winxed
// Winxed compiler driver.
$include_const 'iglobals.pasm';
$include_const 'libpaths.pasm';
$include_const 'except_types.pasm';
$load 'Getopt/Obj.pbc';
class WinxedDriverOptions : [ 'Getopt', 'Obj']
{
var name;
var options;
var opts;
function WinxedDriverOptions(argv)
{
var options = [
[ 'c', 'Compile to pir' ],
[ 'e=s', 'Evaluate' ],
[ 'o=s', 'Object name' ],
[ 'target=s', 'Set target type' ],
[ 'L=s', 'Add to parrot library search path' ],
[ 'I=s', 'Add to parrot include search path' ],
[ 'X=s', 'Add to parrot dynext search path' ],
[ 'debug', 'Set debug mode' ],
[ 'nowarn', 'No warnings' ],
[ 'noan', 'No code annotations' ],
[ 'help', 'Show this help' ],
[ 'version', 'Show version and exit' ]
];
self.options = options;
for (var o in options)
self.push_string(o[0]);
self.notOptStop(1);
self.name = argv.shift();
self.opts = self.get_options(argv);
}
function getbool(string option)
{
var value = self.opts[option];
return value != null;
}
function getstring(string option)
{
var value = self.opts[option];
string result = null;
if (value != null)
result = value;
return result;
}
function showhelp()
{
say('Usage: ', self.name, ' [options] [program] [args]');
say(' Available options:');
int l= 0;
int i;
var o;
for (o in self.options) {
i= length(o[0]) + 4;
if (i > l) l= i;
}
for (o in self.options) {
string s= o[0];
if (length(s) > 1 && substr(s, 1, 1) != '=')
s= '--' + s;
else
s= '-' + s;
i= l - length(s);
say(' ', s, ' ' * i, '-> ', o[1]);
}
}
}
function path_option(options, string opt, int path_type)
{
string value = options.getstring(opt);
if (value != null) {
var lpaths = getinterp()[IGLOBALS_LIB_PATHS];
var pathlib = lpaths[path_type];
pathlib.push(value);
}
}
function extname[anon](string filename, string ext)
{
// Strip a possible .winxed extension from filename,
// add ext, and return the result.
string newname;
int l = length(filename);
if (l > 7 && substr(filename, -7) == '.winxed')
newname = substr(filename, 0, l - 7) + ext;
else
newname = filename + ext;
return newname;
}
function getcompiler[anon]()
{
var compiler;
try
compiler = load_language('winxed');
catch () { }
if (compiler == null) {
if (__DEBUG__) {
load_bytecode("winxedst2.pbc");
compiler = compreg('winxed');
if (compiler == null)
die("winxed: Cannot load language or stage");
}
else
die("winxed: Cannot load language");
}
return compiler;
}
function process_args [anon] (argv)
{
var optionset = new WinxedDriverOptions(argv);
if (optionset.getbool('version')) {
var compiler = getcompiler();
say(compiler.version_string());
exit(0);
}
int help = optionset.getbool('help');
int compileonly = optionset.getbool('c');
string target = optionset.getstring('target');
string eval = optionset.getstring('e');
string objectname = optionset.getstring('o');
int debug = optionset.getbool('debug');
int nowarn = optionset.getbool('nowarn');
int noan = optionset.getbool('noan');
if (help) {
optionset.showhelp();
exit(0);
}
path_option(optionset, 'L', PARROT_LIB_PATH_LIBRARY);
path_option(optionset, 'I', PARROT_LIB_PATH_INCLUDE);
path_option(optionset, 'X', PARROT_LIB_PATH_DYNEXT);
var compileoptions = {
"debug": debug,
"noan": noan,
"nowarn": nowarn
};
if (compileonly) {
if (target != null)
die("options -c and --target can't be used together");
compileoptions["target"] = "pir";
}
else {
if (target == null)
target = '';
switch (target) {
case '':
case 'run':
break;
case 'pir':
case 'include':
compileonly = true;
compileoptions["target"] = target;
break;
default:
die("Invalid target '" + target + "'");
}
}
if (objectname != null && ! compileonly)
die('-o without -c or --target is not supported yet');
var compiler = getcompiler();
var code;
string outfilename = null;
try [handle_types(__WINXED_ERROR__)] {
if (eval == null) {
if (elements(argv) < 1) {
say("ERROR: No program specified");
optionset.showhelp();
exit(1);
}
string srcfilename = argv[0];
if (compileonly && objectname == null)
outfilename = extname(srcfilename, '.pir');
code = compiler.compile_from_file(srcfilename,
compileoptions:[named, flat]);
}
else {
var output;
var outfile;
if (compileonly) {
if (outfilename == null)
outfilename = objectname;
if (outfilename != null && outfilename != "-") {
outfile = open(outfilename, 'w');
output = outfile;
}
else
output = getstdout();
compileoptions['output'] = output;
}
string expr = 'function main[main](argv){' + eval + ';}';
code = compiler.compile(expr,
compileoptions:[named, flat]);
if (compileonly) {
if (outfile != null)
outfile.close();
exit(0);
}
else
argv.unshift('__EVAL__');
}
}
catch (e) {
var payload = e["payload"];
if (payload != null)
cry(payload.filename, ':', payload.line, ': ', payload.message);
else
cry(e["message"]);
exit(1);
}
if (compileonly) {
if (outfilename == null)
outfilename = objectname;
int create = outfilename != null && outfilename != "-";
var outfile = create ? open(outfilename, 'w') : getstdout();
outfile.print(code);
if (create)
outfile.close();
exit(0);
}
// Program start: look for a sub called 'main'.
// Use get_main if available, else use the old heuristic.
var sub;
try {
for (var load_sub in code.subs_by_tag("load"))
load_sub();
code.mark_initialized("load");
sub = code.main_sub();
}
catch () {
cry("Cannot find main sub");
}
return sub;
}
//**************************************************************
function __PARROT_ENTRY_WINXED_main [main] (argv)
{
var mainsub = process_args(argv);
__ASSERT__(mainsub != null);
int retval = 0;
try [handle_types_except(CONTROL_EXIT)] {
var retvalp = mainsub(argv);
if (retvalp != null)
retval = retvalp;
}
catch (e) {
fail(e);
}
exit(retval);
}
function fail [anon] (exception)
{
var stderr = getstderr();
string message = exception.message;
if (message == null || message == "")
message = "No exception handler and no message";
stderr.print(sprintf("%s\n", [ message ]));
string line_sep = "";
var bts = exception.backtrace_strings();
for (int i = elements(bts) - 1; i >= 0; --i) {
string bt = bts[i];
for (string line in split("\n", bt)) {
if (indexof(line, "__PARROT_ENTRY") >= 0)
continue;
stderr.print(sprintf("%s%s", [ line_sep, line ]));
line_sep = "\n";
}
line_sep = "\nthrown from\n";
}
int exit_code = exception.exit_code;
exit(exit_code != 0 ? exit_code : 1);
}
// End