-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzc.js
660 lines (554 loc) · 12.6 KB
/
zc.js
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
#! /usr/local/bin/lua-5.1
/*
* zc - Ziggy's new C language
*
* @author : Vincent Penné (ziggy at zlash dot com)
*
*/
if (arg[0]) standalone = 1;
// useful shortcut
format = string.format;
// lua namespaces
function enter_namespace(name) {
var parent = getfenv(2);
var namespace = { parent_globals = parent };
var i, v;
for (i, v in pairs(parent))
if (i != "parent_globals")
namespace[i] = v;
parent[name] = namespace;
namespace[name] = namespace;
setfenv(2, namespace);
}
function exit_namespace() {
var parent = getfenv(2);
setfenv(2, parent.parent_globals);
}
// enter in zc namespace
enter_namespace("zc");
// all single symbol tokens in js
var symbols = "~!#%%%^&*()%-%+=|/%.,<>:;\"'%[%]{}%?";
var symbolset = "["..symbols.."]";
var msymbols = "<>!%&%*%-%+%=%|%/%%%^%.";
var msymbolset = "["..msymbols.."]";
var nospace = {
["~"] = 1,
["#"] = 1,
};
// paths
var paths = {
"",
};
function add_path(path) {
table.insert(paths, path);
}
var loadfile_orig = loadfile;
function loadfile(name) {
var module, error;
for (_, path in pairs(paths)) {
//emiterror(format("Trying '%s'", path..name));
module, error = loadfile_orig(path..name);
if (module) {
setfenv(module, getfenv(2));
break;
}
}
return module, error;
}
function _message(msg) {
io.stdout:write(msg.."\n");
}
function message(msg) {
if (verbose)
_message(msg);
}
function dbgmessage(msg) {
if (dbgmode)
_message(msg);
}
// emit an error, optionally print information about status in source where the error occured
function emiterror(msg, psource) {
psource = psource || source;
if (source) {
msg = format("%s (%d) at token '%s' : %s", source.filename, source.nline || -1, source.token || "<null>", msg);
var pos = source.tokenpos - 1;
var line = source.tokenlines[pos];
while (pos > 1 && source.tokenlines[pos] == line)
pos--;
pos++;
var i;
i = pos;
msg = msg.."\n\t";
while (source.tokens[i] && source.tokenlines[i] == line) {
msg = msg..source.tokens[i].." ";
i++;
}
i = pos;
msg = msg.."\n\t";
while (source.tokens[i] && source.tokenlines[i] == line) {
if (i == source.tokenpos - 1) {
msg = msg.."^";
break;
}
msg = msg..string.rep(' ', #source.tokens[i] + 1);
i++;
}
}
_message(msg);
has_error = 1;
num_error = (num_error || 0) + 1;
}
// collapse an array of strings into one string, lua-efficiently (lua sucks at string concatenations)
function collapse(t) {
var i, n;
while (t[2]) {
n = #t;
var t2 = { };
for (i=1,n,2)
table.insert(t2, t[i]..(t[i+1] || ""));
t = t2;
}
return t[1] || "";
}
function opensource(realfn, filename) {
var source = { };
source.filename = filename;
if (standalone) {
if (realfn)
source.handle = io.open(filename, "r");
else
source.handle = io.stdin;
if (!source.handle) {
emiterror(format("Can't open source '%s'", filename));
return nil;
}
} else {
source.buffer = gBuffer;
source.bufpos = 1;
}
source.nline = 0;
source.ntoken = 0;
source.tokens = { };
source.tokentypes = { };
source.tokenlines = { };
source.tokencomments = { };
var token = basegettoken(source);
while (token) {
//print(token, source.tokentype);
table.insert(source.tokens, token);
table.insert(source.tokentypes, source.tokentype);
table.insert(source.tokenlines, source.nline);
source.ntoken++;
token = basegettoken(source);
}
source.tokenpos = 1;
return source;
}
function closesource(source) {
if (standalone) {
source.handle:close();
source.handle = nil;
}
source = nil;
}
function getline(source) {
if (standalone)
source.linebuffer = source.handle:read("*l");
else {
if (!source.bufpos) return;
var i = string.find(source.buffer, "\n", source.bufpos);
source.linebuffer = string.sub(source.buffer, source.bufpos, (i || 0) - 1);
source.bufpos = i && (i+1);
}
source.nline++;
source.newline = 1;
//dbgmessage(format("%d %s", source.nline, source.linebuffer));
}
function savepos(psource) {
psource = psource || source;
return {
source = psource,
pos = psource.tokenpos,
};
}
function gotopos(savedpos) {
savedpos.source.tokenpos = savedpos.pos-1;
return gettoken(savedpos.source);
}
function gettoken(psource) {
source = psource or source;
var source = source;
var pos = source.tokenpos;
var token = source.tokens[pos];
source.token = token;
source.tokentype = source.tokentypes[pos];
source.nline = source.tokenlines[pos];
if (!token)
return;
source.tokenpos = pos + 1;
//message(format("\015%d", source.nline));
dbgmessage(token);
if (source.tokentype == "comment") {
if (!source.tokencomments[source.tokenpos - 1]) {
out(string.gsub("// "..token, "\n", output.curindent.."\n//").."\n");
source.tokencomments[source.tokenpos - 1] = 1;
}
return gettoken(source);
}
return token;
}
// This could be rewritten more efficiently in C
function basegettoken(source) {
var newline;
var tokens;
if (!source.linebuffer) {
newline = 1;
getline(source);
if (!source.linebuffer)
return nil;
} else
source.newline=nil;
var i,j;
// remove starting spaces
var s = source.linebuffer;
i = string.find(s, "%S");
if (!i) {
// we reached the end of the line
source.linebuffer = nil;
return basegettoken(source); // tail call so it's fine
}
j = string.find(s, "[%s"..symbols.."]", i);
if (!j)
j = string.len(s) + 1;
else
j--;
source.stick = (i==1);
source.tokentype="word";
if (i != j) {
var c = string.sub(s, i, i);
if (string.find(c, symbolset)) {
j = i;
while (string.find(string.sub(s, j+1, j+1), msymbolset) &&
string.find(c, msymbolset))
j++;
source.tokentype=string.sub(s, i, j);
} else if (string.find(string.sub(s, j-1, j), symbolset))
j--;
}
token = string.sub(s, i, j);
source.token = token;
source.linebuffer=string.sub(s, j+1);
if (token == "\"" || token == "'") {
// string
var t = token;
s = source.linebuffer;
var ok;
while(!ok) {
var _, k;
_,k = string.find(s, t);
while (k && k>1) {
var l = k-1;
var n = 0;
while (l>0 && string.sub(s, l, l)=="\\") {
l--;
n += 0.5;
}
if (n > 0)
dbgmessage(format("N = %g (%g) '%s'", n, math.floor(n),
source.linebuffer));
if (math.floor(n) == n)
break;
_,k = string.find(s, t, k+1);
}
if (k) {
token = token..string.sub(s, 1, k);
source.linebuffer=string.sub(s, k+1);
dbgmessage(format("TOKEN(%s) REST(%s), k(%d)", token, source.linebuffer, k+1));
ok = 1;
} else {
token = token..string.sub(s, 1, -2);
getline(source);
if (!source.linebuffer)
return nil;
s = source.linebuffer;
}
}
source.tokentype=t;
}
if (token == "//" || (source.newline && token == "#")) {
// end of line commentary
getline(source);
//source.tokentype = "comment";
//token = string.sub(s, j+2);
return basegettoken(source);
}
if (token == "/*") {
// block comment
var _, k;
_, k = string.find(s, "*/", j+2);
token = "";
while (!k) {
token = token..string.sub(s, j+2).."\n";
getline(source);
s = source.linebuffer;
if (!s)
return nil;
_, k = string.find(s, "*/");
j = -1;
}
source.linebuffer = string.sub(s, k+1);
source.tokentype = "comment";
token = token..string.sub(s, j+2, k-1);
return basegettoken(source);
}
if (source.tokentype == "word" && !string.find(token, "[^0123456789%.]")) {
source.tokentype = "number";
var s = source.linebuffer;
if (string.sub(s, 1, 1) == ".") {
var i = string.find(s, "%D", 2);
if (i) {
source.linebuffer = string.sub(s, i);
i--;
} else
source.linebuffer = nil;
token = token..string.sub(s, 1, i);
//message(format("FLOAT %s line %d", token, source.nline));
}
}
return token;
}
function include(name) {
var module, error = loadfile(name..".lua");
if (module) {
message(format("Included '%s'", name));
module();
} else {
emiterror(format("Could not load module '%s'", name));
message(error);
}
}
include("types");
include("parse");
include("codegen");
// modules
function loadmodule(name, ns) {
var mname = "mod_"..name;
var ons = getfenv();
if (ns)
setfenv(1, ns);
enter_namespace(mname);
var table = getfenv();
var module, error = loadfile(name..".lua");
if (module) {
message(format("Module '%s' loaded", name));
setfenv(module, table); // why do I need to do this ??
module();
add_options(table.options);
} else {
emiterror(format("Could not load module '%s'", name));
message(error);
table = nil;
}
exit_namespace();
setfenv(1, ons);
return table;
}
function strip_ext(fn) {
fn = string.gsub(fn.."\0", "[.][^.]*\0", "");
fn = string.gsub(fn, "\0", "");
return fn;
}
function zc(f) {
has_error = nil;
num_error = 0;
var filename = f || "stdin";
modulename = strip_ext(filename);
newoutput("header", modulename..".h");
newoutput("code", modulename..".c");
newoutput("zapi", modulename..".zapi");
setoutput("code");
message ("Reading from "..filename);
var source = opensource(f, filename);
if (!source)
return "";
message (format("%d lines, %d tokens", source.nline, source.ntoken));
message ("Processing "..filename);
processsource(source);
closesource(source);
if (!has_error)
codegen();
if (has_error) {
message(format("%d error(s) while compiling", num_error));
return "";
} else
message(format("no error while compiling"));
writeoutputs();
}
function dofile(file) {
var source = zc(file);
var module, error = loadstring(source);
source = nil; // allow source to be garbage collected
if (module) {
//setfenv(module, table); // why do I need to do this ??
module();
} else {
emiterror(format("Could not load string"));
message(error);
}
}
// postprocess
postprocess = { };
function do_postprocess() {
for (_, v in pairs(postprocess))
v();
}
function add_postprocess(f) {
table.insert(postprocess, f);
}
// output
outputs = { };
function newoutput(name, filename) {
o = {
filename = filename,
curindent = "",
indentstring = " ",
indentlevel = 0,
};
outputs[name] = o;
}
function setoutput(name) {
output = outputs[name];
}
function writeoutputs() {
var k, o;
for (k, o in pairs(outputs)) {
var f = io.open(o.filename, "w");
f:write(collapse(o));
}
outputs = { };
}
function out(s) {
s = string.gsub(s, "\t", output.curindent);
table.insert(output, s);
}
function outf(...) {
var s = format(...);
out(s);
}
function get_outcurindent() {
return output.curindent;
}
function outi() {
out(output.curindent);
}
function outfi(...) {
outi();
outf(...);
}
var line = 1;
function outnl() {
line++;
out("\n");
}
function outindent(l) {
output.indentlevel += l;
output.curindent = string.rep(output.indentstring, output.indentlevel);
}
// help
function option_list(opt) {
for (i, v in pairs(opt))
emiterror(i.." "..v.help);
}
function option_help() {
print("usage : zc [options] [filenames]");
option_list(options);
os.exit();
}
// option module
function option_module() {
var name = option_getarg();
loadmodule(name);
}
options = {
["-v"] = {
call = function() { verbose = 1; },
help = "turn verbose mode on"
},
["-d"] = {
call = function() { dbgmode = 1; },
help = "turn debug mode on"
},
["--module"] = {
call = option_module,
help = "<modulename> load a module"
},
["--help"] = {
call = option_help,
help = "display this help message"
}
};
function add_options(table) {
for (i, v in pairs(table)) {
if (options[i])
emiterror(format("Option '%s' overriden", i));
options[i] = v;
}
}
function option_getarg() {
var arg = option_args[option_argind];
option_argind++;
return arg;
}
// exit zc namespace
exit_namespace();
// MAIN ENTRY
if (standalone) {
// compute installation path from executable name
var name = arg[0];
if (name) {
var i = 0;
var j;
while (i != nil) {
j = i;
i = string.find(name, "[/\\]", i+1);
}
if (j) {
name = string.sub(name, 0, j);
zc.message(format("Adding path '%s'", name));
zc.add_path(name);
}
}
// store command line options
zc.option_args = arg;
zc.option_argind = 1;
// parse options
var filename = { };
while (zc.option_argind <= #zc.option_args) {
var arg = zc.option_getarg();
if (string.sub(arg, 1, 1) == "-") {
var opt = zc.options[arg];
if (opt) {
if (opt.call)
opt.call();
if (opt.postcall)
zc.add_postprocess(opt.postcall);
} else {
zc.emiterror(format("Unknown option '%s'\n", arg));
zc.option_help();
}
} else
table.insert(filename, arg);
}
var function doit(filename) {
zc.zc(filename);
}
if (!next(filename))
doit();
else
for (_, v in pairs(filename))
doit(v);
zc.do_postprocess();
if (zc.has_error)
os.exit(-1);
//os.exit()
}