-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjslua.js
848 lines (703 loc) · 16.2 KB
/
jslua.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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
#! /usr/local/bin/lua-5.1
/*
* jslua - transform javascript like syntax into lua
*
* @author : Vincent Penne (ziggy at zlash.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 jslua namespace
enter_namespace("jslua");
// 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.stderr: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, source) {
var p="";
source = source || cursource;
if (source)
p = format("%s (%d) at token '%s' : ", source.filename, source.nline || -1, source.token || "<null>");
_message(p..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 colapse(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;
}
cursource = 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(source) {
return source.tokenpos;
}
function gotopos(source, pos) {
source.tokenpos = pos-1;
return gettoken(source);
}
function gettoken(source) {
cursource = 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", outcurindent.."\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;
}
var exprstack = { };
function processaccum(source, token, what) {
out("= ");
for (i = exprstack[#exprstack] - 1, source.tokenpos - 3, 1)
out(source.tokens[i].." ");
out(string.sub(what, 1, 1).." ");
return token;
}
function processincr(source, token, what) {
processaccum(source, token, what);
out("1 ");
return token;
}
var exprkeywords = {
["function"] = function(source, token, what) {
out("function ") ;
if (token != "(") {
// named function
out(token);
token = gettoken(source);
}
if (token != "(") {
emiterror("'(' expected", source);
return token;
}
out("(");
token = processblock(source, "(", ")", 1);
out(")");
outnl();
outindent(1);
token = processstatement(source, gettoken(source), 1);
outindent(-1);
outi(); out("end"); outnl();
gotopos(source, source.tokenpos - 1);
return ";";
//return token;
},
["var"] = "local",
["||"] = "or",
["&&"] = "and",
["!="] = "~=",
["!"] = "not",
["+="] = processaccum,
["-="] = processaccum,
["*="] = processaccum,
["/="] = processaccum,
["++"] = processincr,
["--"] = processincr,
};
function eatexpr(source, token) {
// if (keywords[token])
// return token;
while (token && exprkeywords[token]) {
var expr = exprkeywords[token];
if (type(expr) == "string") {
out(expr.." ");
token = gettoken(source);
} else
token = exprkeywords[token](source, gettoken(source), token);
}
// if (token && keywords[token])
// emiterror("unexpected reserved keyword '"..token.."'", source);
return token;
}
// process a block enclosed by 'open' and 'close' pair
function processblock(source, open, close, n) {
if (n < 1) {
if (gettoken(source) != open) {
emiterror(format("expected '%s' but got '%s'", open, source.token), source);
return nil;
}
n = 1;
}
var token = gettoken(source);
table.insert(exprstack, savepos(source));
while (n>=1) {
token = eatexpr(source, token);
if (!token)
return nil;
if (token == open)
n++;
else if (token == close)
n--;
if (n>=1) {
if (token != ";") {
if (nospace[token])
out(token);
else
out(token.." ");
}
token = gettoken(source);
}
}
table.remove(exprstack);
return token;
}
var expression_terminators = {
[";"] = 1,
/* [")"] = 1,
[","] = 1,
["]"] = 1,
["}"] = 1 */
};
var openclose = {
["("] = ")",
["["] = "]",
["{"] = "}",
};
// parse an expression
function processexpression(source, token) {
table.insert(exprstack, savepos(source));
while (token) {
token = eatexpr(source, token);
if (!token || expression_terminators[token]) break;
if (nospace[token])
out(token);
else
out(token.." ");
var close = openclose[token];
if (close) {
processblock(source, token, close, 1);
out(close);
}
token = gettoken(source);
}
table.remove(exprstack);
return token;
}
function process_if(source, token, what) {
if (token != "(") {
emiterror("'(' expected", source);
return token;
}
outi(); out(what.." ");
token = processblock(source, "(", ")", 1);
if (what == "if") out("then"); else out("do");
outnl();
outindent(1);
token = processstatement(source, gettoken(source), 1);
outindent(-1);
while (what == "if" && token == "elseif") {
token = gettoken(source);
if (token != "(") {
emiterror("'(' expected", source);
return token;
}
outi(); out("elseif ");
token = processblock(source, "(", ")", 1);
out("then"); outnl();
outindent(1);
token = processstatement(source, gettoken(source), 1);
outindent(-1);
}
if (what == "if" && token == "else") {
outi(); out(token); outnl();
outindent(1);
token = processstatement(source, gettoken(source), 1);
outindent(-1);
}
outi(); out("end"); outnl();
return token;
}
keywords = {
["if"] = process_if,
["while"] = process_if,
["for"] = process_if,
};
function processstatement(source, token, delimited) {
if (keywords[token])
return keywords[token](source, gettoken(source), token);
else if (token == "{") {
if (!delimited) {
outi(); out("do"); outnl();
outindent(1);
}
token = gettoken(source);
while (token && token != "}")
token = processstatement(source, token);
if (!delimited) {
outindent(-1);
outi(); out("end"); outnl();
}
token = gettoken(source);
} else {
outi();
token = processexpression(source, token);
outnl();
if (token && token != ";" && token != "}") {
emiterror("warning ';' or '}' expected", source);
token = gettoken(source);
}
if (token == ";")
token = gettoken(source);
}
return token;
}
function processsource(source) {
var token = gettoken(source);
while (token)
token = processstatement(source, token);
}
// 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 jslua(f) {
has_error = nil;
num_error = 0;
resultString = { };
var filename = f || "stdin";
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) {
message(format("%d error(s) while compiling", num_error));
return "";
} else
message(format("no error while compiling"));
return colapse(resultString);
}
function dofile(file) {
var source = jslua(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
outcurindent = "";
outindentstring = " ";
outindentlevel = 0;
function out(s) {
table.insert(resultString, s);
}
function outf(...) {
var s = format(...);
out(s);
}
function get_outcurindent() {
return outcurindent;
}
function outi() {
out(outcurindent);
}
var line = 1;
function outnl() {
line++;
out("\n");
}
function outindent(l) {
outindentlevel += l;
outcurindent = string.rep(outindentstring, outindentlevel);
}
// help
function option_list(opt) {
for (i, v in pairs(opt))
emiterror(i.." "..v.help);
}
function option_help() {
print("usage : jslua [options] [filenames]");
option_list(options);
os.exit();
}
// option module
function option_module() {
var name = option_getarg();
loadmodule(name);
}
var outhandle = io.stdout;
var compileonly;
function option_output() {
compileonly = 1;
var fn = option_getarg();
outhandle = io.open(fn, "w");
if (!outhandle)
emiterror("Failed to open '"..fn.."' for writing.");
else
message("Opened '"..fn.."' for writing ...");
}
options = {
["-o"] = {
call = option_output,
help = "compile only, and output lua source code to specified file"
},
["-c"] = {
call = function() { compileonly = 1; },
help = "compile only, and output lua source code on stdout"
},
["-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 jslua 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);
jslua.message(format("Adding path '%s'", name));
jslua.add_path(name);
}
}
// store command line options
jslua.option_args = arg;
jslua.option_argind = 1;
// parse options
var filename = { };
while (jslua.option_argind <= #jslua.option_args) {
var arg = jslua.option_getarg();
if (string.sub(arg, 1, 1) == "-") {
var opt = jslua.options[arg];
if (opt) {
if (opt.call)
opt.call();
if (opt.postcall)
jslua.add_postprocess(opt.postcall);
} else {
jslua.emiterror(format("Unknown option '%s'\n", arg));
jslua.option_help();
}
} else
table.insert(filename, arg);
}
var function doit(filename) {
if (compileonly)
outhandle:write(jslua.jslua(filename));
else
jslua.dofile(filename);
}
if (!next(filename))
doit();
else
for (_, v in pairs(filename))
doit(v);
jslua.do_postprocess();
if (jslua.has_error)
os.exit(-1);
//os.exit()
}