-
Notifications
You must be signed in to change notification settings - Fork 11
/
1-0-0.lua
5173 lines (5129 loc) Β· 190 KB
/
1-0-0.lua
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
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package.preload["fennel.repl"] = package.preload["fennel.repl"] or function(...)
local utils = require("fennel.utils")
local parser = require("fennel.parser")
local compiler = require("fennel.compiler")
local specials = require("fennel.specials")
local view = require("fennel.view")
local unpack = (table.unpack or _G.unpack)
local function default_read_chunk(parser_state)
local function _519_()
if (0 < parser_state["stack-size"]) then
return ".."
else
return ">> "
end
end
io.write(_519_())
io.flush()
local input = io.read()
return (input and (input .. "\n"))
end
local function default_on_values(xs)
io.write(table.concat(xs, "\9"))
return io.write("\n")
end
local function default_on_error(errtype, err, lua_source)
local function _521_()
local _520_ = errtype
if (_520_ == "Lua Compile") then
return ("Bad code generated - likely a bug with the compiler:\n" .. "--- Generated Lua Start ---\n" .. lua_source .. "--- Generated Lua End ---\n")
elseif (_520_ == "Runtime") then
return (compiler.traceback(tostring(err), 4) .. "\n")
elseif true then
local _ = _520_
return ("%s error: %s\n"):format(errtype, tostring(err))
else
return nil
end
end
return io.write(_521_())
end
local save_source = table.concat({"local ___i___ = 1", "while true do", " local name, value = debug.getlocal(1, ___i___)", " if(name and name ~= \"___i___\") then", " ___replLocals___[name] = value", " ___i___ = ___i___ + 1", " else break end end"}, "\n")
local function splice_save_locals(env, lua_source)
local spliced_source = {}
local bind = "local %s = ___replLocals___['%s']"
for line in lua_source:gmatch("([^\n]+)\n?") do
table.insert(spliced_source, line)
end
for name in pairs(env.___replLocals___) do
table.insert(spliced_source, 1, bind:format(name, name))
end
if ((1 < #spliced_source) and (spliced_source[#spliced_source]):match("^ *return .*$")) then
table.insert(spliced_source, #spliced_source, save_source)
else
end
return table.concat(spliced_source, "\n")
end
local function completer(env, scope, text)
local matches = {}
local input_fragment = text:gsub(".*[%s)(]+", "")
local stop_looking_3f = false
local function add_partials(input, tbl, prefix, method_3f)
for k in utils.allpairs(tbl) do
local k0
if ((tbl == env) or (tbl == env.___replLocals___)) then
k0 = scope.unmanglings[k]
else
k0 = k
end
if ((#matches < 2000) and (type(k0) == "string") and (input == k0:sub(0, #input)) and (not method_3f or ("function" == type(tbl[k0])))) then
local function _525_()
if method_3f then
return (prefix .. ":" .. k0)
else
return (prefix .. k0)
end
end
table.insert(matches, _525_())
else
end
end
return nil
end
local function descend(input, tbl, prefix, add_matches, method_3f)
local splitter
if method_3f then
splitter = "^([^:]+):(.*)"
else
splitter = "^([^.]+)%.(.*)"
end
local head, tail = input:match(splitter)
local raw_head = (scope.manglings[head] or head)
if (type(tbl[raw_head]) == "table") then
stop_looking_3f = true
if method_3f then
return add_partials(tail, tbl[raw_head], (prefix .. head), true)
else
return add_matches(tail, tbl[raw_head], (prefix .. head))
end
else
return nil
end
end
local function add_matches(input, tbl, prefix)
local prefix0
if prefix then
prefix0 = (prefix .. ".")
else
prefix0 = ""
end
if (not input:find("%.") and input:find(":")) then
return descend(input, tbl, prefix0, add_matches, true)
elseif not input:find("%.") then
return add_partials(input, tbl, prefix0)
else
return descend(input, tbl, prefix0, add_matches, false)
end
end
for _, source in ipairs({scope.specials, scope.macros, (env.___replLocals___ or {}), env, env._G}) do
if stop_looking_3f then break end
add_matches(input_fragment, source)
end
return matches
end
local commands = {}
local function command_3f(input)
return input:match("^%s*,")
end
local function command_docs()
local _532_
do
local tbl_14_auto = {}
local i_15_auto = #tbl_14_auto
for name, f in pairs(commands) do
local val_16_auto = (" ,%s - %s"):format(name, ((compiler.metadata):get(f, "fnl/docstring") or "undocumented"))
if (nil ~= val_16_auto) then
i_15_auto = (i_15_auto + 1)
do end (tbl_14_auto)[i_15_auto] = val_16_auto
else
end
end
_532_ = tbl_14_auto
end
return table.concat(_532_, "\n")
end
commands.help = function(_, _0, on_values)
return on_values({("Welcome to Fennel.\nThis is the REPL where you can enter code to be evaluated.\nYou can also run these repl commands:\n\n" .. command_docs() .. "\n ,exit - Leave the repl.\n\nUse ,doc something to see descriptions for individual macros and special forms.\n\nFor more information about the language, see https://fennel-lang.org/reference")})
end
do end (compiler.metadata):set(commands.help, "fnl/docstring", "Show this message.")
local function reload(module_name, env, on_values, on_error)
local _534_, _535_ = pcall(specials["load-code"]("return require(...)", env), module_name)
if ((_534_ == true) and (nil ~= _535_)) then
local old = _535_
local _
package.loaded[module_name] = nil
_ = nil
local ok, new = pcall(require, module_name)
local new0
if not ok then
on_values({new})
new0 = old
else
new0 = new
end
if ((type(old) == "table") and (type(new0) == "table")) then
for k, v in pairs(new0) do
old[k] = v
end
for k in pairs(old) do
if (nil == (new0)[k]) then
old[k] = nil
else
end
end
package.loaded[module_name] = old
else
end
return on_values({"ok"})
elseif ((_534_ == false) and (nil ~= _535_)) then
local msg = _535_
local function _540_()
local _539_ = msg:gsub("\n.*", "")
return _539_
end
return on_error("Runtime", _540_())
else
return nil
end
end
local function run_command(read, on_error, f)
local _542_, _543_, _544_ = pcall(read)
if ((_542_ == true) and (_543_ == true) and (nil ~= _544_)) then
local val = _544_
return f(val)
elseif (_542_ == false) then
return on_error("Parse", "Couldn't parse input.")
else
return nil
end
end
commands.reload = function(env, read, on_values, on_error)
local function _546_(_241)
return reload(tostring(_241), env, on_values, on_error)
end
return run_command(read, on_error, _546_)
end
do end (compiler.metadata):set(commands.reload, "fnl/docstring", "Reload the specified module.")
commands.reset = function(env, _, on_values)
env.___replLocals___ = {}
return on_values({"ok"})
end
do end (compiler.metadata):set(commands.reset, "fnl/docstring", "Erase all repl-local scope.")
commands.complete = function(env, read, on_values, on_error, scope, chars)
local function _547_()
return on_values(completer(env, scope, string.char(unpack(chars)):gsub(",complete +", ""):sub(1, -2)))
end
return run_command(read, on_error, _547_)
end
do end (compiler.metadata):set(commands.complete, "fnl/docstring", "Print all possible completions for a given input symbol.")
local function apropos_2a(pattern, tbl, prefix, seen, names)
for name, subtbl in pairs(tbl) do
if (("string" == type(name)) and (package ~= subtbl)) then
local _548_ = type(subtbl)
if (_548_ == "function") then
if ((prefix .. name)):match(pattern) then
table.insert(names, (prefix .. name))
else
end
elseif (_548_ == "table") then
if not seen[subtbl] then
local _551_
do
local _550_ = seen
_550_[subtbl] = true
_551_ = _550_
end
apropos_2a(pattern, subtbl, (prefix .. name:gsub("%.", "/") .. "."), _551_, names)
else
end
else
end
else
end
end
return names
end
local function apropos(pattern)
local names = apropos_2a(pattern, package.loaded, "", {}, {})
local tbl_14_auto = {}
local i_15_auto = #tbl_14_auto
for _, name in ipairs(names) do
local val_16_auto = name:gsub("^_G%.", "")
if (nil ~= val_16_auto) then
i_15_auto = (i_15_auto + 1)
do end (tbl_14_auto)[i_15_auto] = val_16_auto
else
end
end
return tbl_14_auto
end
commands.apropos = function(_env, read, on_values, on_error, _scope)
local function _556_(_241)
return on_values(apropos(tostring(_241)))
end
return run_command(read, on_error, _556_)
end
do end (compiler.metadata):set(commands.apropos, "fnl/docstring", "Print all functions matching a pattern in all loaded modules.")
local function apropos_follow_path(path)
local paths
do
local tbl_14_auto = {}
local i_15_auto = #tbl_14_auto
for p in path:gmatch("[^%.]+") do
local val_16_auto = p
if (nil ~= val_16_auto) then
i_15_auto = (i_15_auto + 1)
do end (tbl_14_auto)[i_15_auto] = val_16_auto
else
end
end
paths = tbl_14_auto
end
local tgt = package.loaded
for _, path0 in ipairs(paths) do
if (nil == tgt) then break end
local _559_
do
local _558_ = path0:gsub("%/", ".")
_559_ = _558_
end
tgt = tgt[_559_]
end
return tgt
end
local function apropos_doc(pattern)
local names = {}
for _, path in ipairs(apropos(".*")) do
local tgt = apropos_follow_path(path)
if ("function" == type(tgt)) then
local _560_ = (compiler.metadata):get(tgt, "fnl/docstring")
if (nil ~= _560_) then
local docstr = _560_
if docstr:match(pattern) then
table.insert(names, path)
else
end
else
end
else
end
end
return names
end
commands["apropos-doc"] = function(_env, read, on_values, on_error, _scope)
local function _564_(_241)
return on_values(apropos_doc(tostring(_241)))
end
return run_command(read, on_error, _564_)
end
do end (compiler.metadata):set(commands["apropos-doc"], "fnl/docstring", "Print all functions that match the pattern in their docs")
local function apropos_show_docs(on_values, pattern)
for _, path in ipairs(apropos(pattern)) do
local tgt = apropos_follow_path(path)
if (("function" == type(tgt)) and (compiler.metadata):get(tgt, "fnl/docstring")) then
on_values(specials.doc(tgt, path))
on_values()
else
end
end
return nil
end
commands["apropos-show-docs"] = function(_env, read, on_values, on_error)
local function _566_(_241)
return apropos_show_docs(on_values, tostring(_241))
end
return run_command(read, on_error, _566_)
end
do end (compiler.metadata):set(commands["apropos-show-docs"], "fnl/docstring", "Print all documentations matching a pattern in function name")
local function resolve(identifier, _567_, scope)
local _arg_568_ = _567_
local ___replLocals___ = _arg_568_["___replLocals___"]
local env = _arg_568_
local e
local function _569_(_241, _242)
return (___replLocals___[_242] or env[_242])
end
e = setmetatable({}, {__index = _569_})
local code = compiler["compile-string"](tostring(identifier), {scope = scope})
return specials["load-code"](code, e)()
end
commands.find = function(env, read, on_values, on_error, scope)
local function _570_(_241)
local _571_
do
local _572_ = utils["sym?"](_241)
if (nil ~= _572_) then
local _573_ = resolve(_572_, env, scope)
if (nil ~= _573_) then
_571_ = debug.getinfo(_573_)
else
_571_ = _573_
end
else
_571_ = _572_
end
end
if ((_G.type(_571_) == "table") and (nil ~= (_571_).short_src) and (nil ~= (_571_).linedefined) and ((_571_).what == "Lua") and (nil ~= (_571_).source)) then
local src = (_571_).short_src
local line = (_571_).linedefined
local source = (_571_).source
local fnlsrc
do
local t_576_ = compiler.sourcemap
if (nil ~= t_576_) then
t_576_ = (t_576_)[source]
else
end
if (nil ~= t_576_) then
t_576_ = (t_576_)[line]
else
end
if (nil ~= t_576_) then
t_576_ = (t_576_)[2]
else
end
fnlsrc = t_576_
end
return on_values({string.format("%s:%s", src, (fnlsrc or line))})
elseif (_571_ == nil) then
return on_error("Repl", "Unknown value")
elseif true then
local _ = _571_
return on_error("Repl", "No source info")
else
return nil
end
end
return run_command(read, on_error, _570_)
end
do end (compiler.metadata):set(commands.find, "fnl/docstring", "Print the filename and line number for a given function")
commands.doc = function(env, read, on_values, on_error, scope)
local function _581_(_241)
local name = tostring(_241)
local target = (scope.specials[name] or scope.macros[name] or resolve(name, env, scope))
return on_values({specials.doc(target, name)})
end
return run_command(read, on_error, _581_)
end
do end (compiler.metadata):set(commands.doc, "fnl/docstring", "Print the docstring and arglist for a function, macro, or special form.")
local function load_plugin_commands(plugins)
for _, plugin in ipairs((plugins or {})) do
for name, f in pairs(plugin) do
local _582_ = name:match("^repl%-command%-(.*)")
if (nil ~= _582_) then
local cmd_name = _582_
commands[cmd_name] = (commands[cmd_name] or f)
else
end
end
end
return nil
end
local function run_command_loop(input, read, loop, env, on_values, on_error, scope, chars)
local command_name = input:match(",([^%s/]+)")
do
local _584_ = commands[command_name]
if (nil ~= _584_) then
local command = _584_
command(env, read, on_values, on_error, scope, chars)
elseif true then
local _ = _584_
if ("exit" ~= command_name) then
on_values({"Unknown command", command_name})
else
end
else
end
end
if ("exit" ~= command_name) then
return loop()
else
return nil
end
end
local function repl(options)
local old_root_options = utils.root.options
local env = specials["wrap-env"]((options.env or (rawget(_G, "_ENV") or _G)))
local save_locals_3f = ((options.saveLocals ~= false) and env.debug and env.debug.getlocal)
local opts = utils.copy(options)
local read_chunk = (opts.readChunk or default_read_chunk)
local on_values = (opts.onValues or default_on_values)
local on_error = (opts.onError or default_on_error)
local pp = (opts.pp or view)
local byte_stream, clear_stream = parser.granulate(read_chunk)
local chars = {}
local read, reset = nil, nil
local function _588_(parser_state)
local c = byte_stream(parser_state)
table.insert(chars, c)
return c
end
read, reset = parser.parser(_588_)
opts.env, opts.scope = env, compiler["make-scope"]()
opts.useMetadata = (options.useMetadata ~= false)
if (opts.allowedGlobals == nil) then
opts.allowedGlobals = specials["current-global-names"](env)
else
end
if opts.registerCompleter then
local function _592_()
local _590_ = env
local _591_ = opts.scope
local function _593_(...)
return completer(_590_, _591_, ...)
end
return _593_
end
opts.registerCompleter(_592_())
else
end
load_plugin_commands(opts.plugins)
if save_locals_3f then
local function newindex(t, k, v)
if opts.scope.unmanglings[k] then
return rawset(t, k, v)
else
return nil
end
end
env.___replLocals___ = setmetatable({}, {__newindex = newindex})
else
end
local function print_values(...)
local vals = {...}
local out = {}
env._, env.__ = vals[1], vals
for i = 1, select("#", ...) do
table.insert(out, pp(vals[i]))
end
return on_values(out)
end
local function loop()
for k in pairs(chars) do
chars[k] = nil
end
reset()
local ok, parse_ok_3f, x = pcall(read)
local src_string = string.char(unpack(chars))
if not ok then
on_error("Parse", parse_ok_3f)
clear_stream()
return loop()
elseif command_3f(src_string) then
return run_command_loop(src_string, read, loop, env, on_values, on_error, opts.scope, chars)
else
if parse_ok_3f then
do
local _597_, _598_ = nil, nil
local function _600_()
local _599_ = opts
_599_["source"] = src_string
return _599_
end
_597_, _598_ = pcall(compiler.compile, x, _600_())
if ((_597_ == false) and (nil ~= _598_)) then
local msg = _598_
clear_stream()
on_error("Compile", msg)
elseif ((_597_ == true) and (nil ~= _598_)) then
local src = _598_
local src0
if save_locals_3f then
src0 = splice_save_locals(env, src, opts.scope)
else
src0 = src
end
local _602_, _603_ = pcall(specials["load-code"], src0, env)
if ((_602_ == false) and (nil ~= _603_)) then
local msg = _603_
clear_stream()
on_error("Lua Compile", msg, src0)
elseif (true and (nil ~= _603_)) then
local _ = _602_
local chunk = _603_
local function _604_()
return print_values(chunk())
end
local function _605_()
local function _606_(...)
return on_error("Runtime", ...)
end
return _606_
end
xpcall(_604_, _605_())
else
end
else
end
end
utils.root.options = old_root_options
return loop()
else
return nil
end
end
end
return loop()
end
return repl
end
package.preload["fennel.specials"] = package.preload["fennel.specials"] or function(...)
local utils = require("fennel.utils")
local view = require("fennel.view")
local parser = require("fennel.parser")
local compiler = require("fennel.compiler")
local unpack = (table.unpack or _G.unpack)
local SPECIALS = compiler.scopes.global.specials
local function wrap_env(env)
local function _345_(_, key)
if (type(key) == "string") then
return env[compiler["global-unmangling"](key)]
else
return env[key]
end
end
local function _347_(_, key, value)
if (type(key) == "string") then
env[compiler["global-unmangling"](key)] = value
return nil
else
env[key] = value
return nil
end
end
local function _349_()
local function putenv(k, v)
local _350_
if (type(k) == "string") then
_350_ = compiler["global-unmangling"](k)
else
_350_ = k
end
return _350_, v
end
return next, utils.kvmap(env, putenv), nil
end
return setmetatable({}, {__index = _345_, __newindex = _347_, __pairs = _349_})
end
local function current_global_names(_3fenv)
local mt
do
local _352_ = getmetatable(_3fenv)
if ((_G.type(_352_) == "table") and (nil ~= (_352_).__pairs)) then
local mtpairs = (_352_).__pairs
local tbl_11_auto = {}
for k, v in mtpairs(_3fenv) do
local _353_, _354_ = k, v
if ((nil ~= _353_) and (nil ~= _354_)) then
local k_12_auto = _353_
local v_13_auto = _354_
tbl_11_auto[k_12_auto] = v_13_auto
else
end
end
mt = tbl_11_auto
elseif (_352_ == nil) then
mt = (_3fenv or _G)
else
mt = nil
end
end
return (mt and utils.kvmap(mt, compiler["global-unmangling"]))
end
local function load_code(code, _3fenv, _3ffilename)
local env = (_3fenv or rawget(_G, "_ENV") or _G)
if (rawget(_G, "setfenv") and rawget(_G, "loadstring")) then
local f = assert(_G.loadstring(code, _3ffilename))
local _357_ = f
setfenv(_357_, env)
return _357_
else
return assert(load(code, _3ffilename, "t", env))
end
end
local function doc_2a(tgt, name)
if not tgt then
return (name .. " not found")
else
local docstring = (((compiler.metadata):get(tgt, "fnl/docstring") or "#<undocumented>")):gsub("\n$", ""):gsub("\n", "\n ")
local mt = getmetatable(tgt)
if ((type(tgt) == "function") or ((type(mt) == "table") and (type(mt.__call) == "function"))) then
local arglist = table.concat(((compiler.metadata):get(tgt, "fnl/arglist") or {"#<unknown-arguments>"}), " ")
local _359_
if (#arglist > 0) then
_359_ = " "
else
_359_ = ""
end
return string.format("(%s%s%s)\n %s", name, _359_, arglist, docstring)
else
return string.format("%s\n %s", name, docstring)
end
end
end
local function doc_special(name, arglist, docstring, body_form_3f)
compiler.metadata[SPECIALS[name]] = {["fnl/arglist"] = arglist, ["fnl/docstring"] = docstring, ["fnl/body-form?"] = body_form_3f}
return nil
end
local function compile_do(ast, scope, parent, _3fstart)
local start = (_3fstart or 2)
local len = #ast
local sub_scope = compiler["make-scope"](scope)
for i = start, len do
compiler.compile1(ast[i], sub_scope, parent, {nval = 0})
end
return nil
end
SPECIALS["do"] = function(ast, scope, parent, opts, _3fstart, _3fchunk, _3fsub_scope, _3fpre_syms)
local start = (_3fstart or 2)
local sub_scope = (_3fsub_scope or compiler["make-scope"](scope))
local chunk = (_3fchunk or {})
local len = #ast
local retexprs = {returned = true}
local function compile_body(outer_target, outer_tail, outer_retexprs)
if (len < start) then
compiler.compile1(nil, sub_scope, chunk, {tail = outer_tail, target = outer_target})
else
for i = start, len do
local subopts = {nval = (((i ~= len) and 0) or opts.nval), tail = (((i == len) and outer_tail) or nil), target = (((i == len) and outer_target) or nil)}
local _ = utils["propagate-options"](opts, subopts)
local subexprs = compiler.compile1(ast[i], sub_scope, chunk, subopts)
if (i ~= len) then
compiler["keep-side-effects"](subexprs, parent, nil, ast[i])
else
end
end
end
compiler.emit(parent, chunk, ast)
compiler.emit(parent, "end", ast)
utils.hook("do", ast, sub_scope)
return (outer_retexprs or retexprs)
end
if (opts.target or (opts.nval == 0) or opts.tail) then
compiler.emit(parent, "do", ast)
return compile_body(opts.target, opts.tail)
elseif opts.nval then
local syms = {}
for i = 1, opts.nval do
local s = ((_3fpre_syms and (_3fpre_syms)[i]) or compiler.gensym(scope))
do end (syms)[i] = s
retexprs[i] = utils.expr(s, "sym")
end
local outer_target = table.concat(syms, ", ")
compiler.emit(parent, string.format("local %s", outer_target), ast)
compiler.emit(parent, "do", ast)
return compile_body(outer_target, opts.tail)
else
local fname = compiler.gensym(scope)
local fargs
if scope.vararg then
fargs = "..."
else
fargs = ""
end
compiler.emit(parent, string.format("local function %s(%s)", fname, fargs), ast)
return compile_body(nil, true, utils.expr((fname .. "(" .. fargs .. ")"), "statement"))
end
end
doc_special("do", {"..."}, "Evaluate multiple forms; return last value.", true)
SPECIALS.values = function(ast, scope, parent)
local len = #ast
local exprs = {}
for i = 2, len do
local subexprs = compiler.compile1(ast[i], scope, parent, {nval = ((i ~= len) and 1)})
table.insert(exprs, subexprs[1])
if (i == len) then
for j = 2, #subexprs do
table.insert(exprs, subexprs[j])
end
else
end
end
return exprs
end
doc_special("values", {"..."}, "Return multiple values from a function. Must be in tail position.")
local function deep_tostring(x, key_3f)
if utils["sequence?"](x) then
local _368_
do
local tbl_14_auto = {}
local i_15_auto = #tbl_14_auto
for _, v in ipairs(x) do
local val_16_auto = deep_tostring(v)
if (nil ~= val_16_auto) then
i_15_auto = (i_15_auto + 1)
do end (tbl_14_auto)[i_15_auto] = val_16_auto
else
end
end
_368_ = tbl_14_auto
end
return ("[" .. table.concat(_368_, " ") .. "]")
elseif utils["table?"](x) then
local _370_
do
local tbl_14_auto = {}
local i_15_auto = #tbl_14_auto
for k, v in pairs(x) do
local val_16_auto = (deep_tostring(k, true) .. " " .. deep_tostring(v))
if (nil ~= val_16_auto) then
i_15_auto = (i_15_auto + 1)
do end (tbl_14_auto)[i_15_auto] = val_16_auto
else
end
end
_370_ = tbl_14_auto
end
return ("{" .. table.concat(_370_, " ") .. "}")
elseif (key_3f and (type(x) == "string") and x:find("^[-%w?\\^_!$%&*+./@:|<=>]+$")) then
return (":" .. x)
elseif (type(x) == "string") then
return string.format("%q", x):gsub("\\\"", "\\\\\""):gsub("\"", "\\\"")
else
return tostring(x)
end
end
local function set_fn_metadata(arg_list, docstring, parent, fn_name)
if utils.root.options.useMetadata then
local args
local function _373_(_241)
return ("\"%s\""):format(deep_tostring(_241))
end
args = utils.map(arg_list, _373_)
local meta_fields = {"\"fnl/arglist\"", ("{" .. table.concat(args, ", ") .. "}")}
if docstring then
table.insert(meta_fields, "\"fnl/docstring\"")
table.insert(meta_fields, ("\"" .. docstring:gsub("%s+$", ""):gsub("\\", "\\\\"):gsub("\n", "\\n"):gsub("\"", "\\\"") .. "\""))
else
end
local meta_str = ("require(\"%s\").metadata"):format((utils.root.options.moduleName or "fennel"))
return compiler.emit(parent, ("pcall(function() %s:setall(%s, %s) end)"):format(meta_str, fn_name, table.concat(meta_fields, ", ")))
else
return nil
end
end
local function get_fn_name(ast, scope, fn_name, multi)
if (fn_name and (fn_name[1] ~= "nil")) then
local _376_
if not multi then
_376_ = compiler["declare-local"](fn_name, {}, scope, ast)
else
_376_ = (compiler["symbol-to-expression"](fn_name, scope))[1]
end
return _376_, not multi, 3
else
return nil, true, 2
end
end
local function compile_named_fn(ast, f_scope, f_chunk, parent, index, fn_name, local_3f, arg_name_list, arg_list, docstring)
for i = (index + 1), #ast do
compiler.compile1(ast[i], f_scope, f_chunk, {nval = (((i ~= #ast) and 0) or nil), tail = (i == #ast)})
end
local _379_
if local_3f then
_379_ = "local function %s(%s)"
else
_379_ = "%s = function(%s)"
end
compiler.emit(parent, string.format(_379_, fn_name, table.concat(arg_name_list, ", ")), ast)
compiler.emit(parent, f_chunk, ast)
compiler.emit(parent, "end", ast)
set_fn_metadata(arg_list, docstring, parent, fn_name)
utils.hook("fn", ast, f_scope)
return utils.expr(fn_name, "sym")
end
local function compile_anonymous_fn(ast, f_scope, f_chunk, parent, index, arg_name_list, arg_list, docstring, scope)
local fn_name = compiler.gensym(scope)
return compile_named_fn(ast, f_scope, f_chunk, parent, index, fn_name, true, arg_name_list, arg_list, docstring)
end
SPECIALS.fn = function(ast, scope, parent)
local f_scope
do
local _381_ = compiler["make-scope"](scope)
do end (_381_)["vararg"] = false
f_scope = _381_
end
local f_chunk = {}
local fn_sym = utils["sym?"](ast[2])
local multi = (fn_sym and utils["multi-sym?"](fn_sym[1]))
local fn_name, local_3f, index = get_fn_name(ast, scope, fn_sym, multi)
local arg_list = compiler.assert(utils["table?"](ast[index]), "expected parameters table", ast)
compiler.assert((not multi or not multi["multi-sym-method-call"]), ("unexpected multi symbol " .. tostring(fn_name)), fn_sym)
local function get_arg_name(arg)
if utils["varg?"](arg) then
compiler.assert((arg == arg_list[#arg_list]), "expected vararg as last parameter", ast)
f_scope.vararg = true
return "..."
elseif (utils["sym?"](arg) and (tostring(arg) ~= "nil") and not utils["multi-sym?"](tostring(arg))) then
return compiler["declare-local"](arg, {}, f_scope, ast)
elseif utils["table?"](arg) then
local raw = utils.sym(compiler.gensym(scope))
local declared = compiler["declare-local"](raw, {}, f_scope, ast)
compiler.destructure(arg, raw, ast, f_scope, f_chunk, {declaration = true, nomulti = true, symtype = "arg"})
return declared
else
return compiler.assert(false, ("expected symbol for function parameter: %s"):format(tostring(arg)), ast[index])
end
end
local arg_name_list = utils.map(arg_list, get_arg_name)
local index0, docstring = nil, nil
if ((type(ast[(index + 1)]) == "string") and ((index + 1) < #ast)) then
index0, docstring = (index + 1), ast[(index + 1)]
else
index0, docstring = index, nil
end
if fn_name then
return compile_named_fn(ast, f_scope, f_chunk, parent, index0, fn_name, local_3f, arg_name_list, arg_list, docstring)
else
return compile_anonymous_fn(ast, f_scope, f_chunk, parent, index0, arg_name_list, arg_list, docstring, scope)
end
end
doc_special("fn", {"name?", "args", "docstring?", "..."}, "Function syntax. May optionally include a name and docstring.\nIf a name is provided, the function will be bound in the current scope.\nWhen called with the wrong number of args, excess args will be discarded\nand lacking args will be nil, use lambda for arity-checked functions.", true)
SPECIALS.lua = function(ast, _, parent)
compiler.assert(((#ast == 2) or (#ast == 3)), "expected 1 or 2 arguments", ast)
local _386_
do
local _385_ = utils["sym?"](ast[2])
if (nil ~= _385_) then
_386_ = tostring(_385_)
else
_386_ = _385_
end
end
if ("nil" ~= _386_) then
table.insert(parent, {ast = ast, leaf = tostring(ast[2])})
else
end
local _390_
do
local _389_ = utils["sym?"](ast[3])
if (nil ~= _389_) then
_390_ = tostring(_389_)
else
_390_ = _389_
end
end
if ("nil" ~= _390_) then
return tostring(ast[3])
else
return nil
end
end
local function dot(ast, scope, parent)
compiler.assert((1 < #ast), "expected table argument", ast)
local len = #ast
local _let_393_ = compiler.compile1(ast[2], scope, parent, {nval = 1})
local lhs = _let_393_[1]
if (len == 2) then
return tostring(lhs)
else
local indices = {}
for i = 3, len do
local index = ast[i]
if ((type(index) == "string") and utils["valid-lua-identifier?"](index)) then
table.insert(indices, ("." .. index))
else
local _let_394_ = compiler.compile1(index, scope, parent, {nval = 1})
local index0 = _let_394_[1]
table.insert(indices, ("[" .. tostring(index0) .. "]"))
end
end
if (tostring(lhs):find("[{\"0-9]") or ("nil" == tostring(lhs))) then
return ("(" .. tostring(lhs) .. ")" .. table.concat(indices))
else
return (tostring(lhs) .. table.concat(indices))
end
end
end
SPECIALS["."] = dot
doc_special(".", {"tbl", "key1", "..."}, "Look up key1 in tbl table. If more args are provided, do a nested lookup.")
SPECIALS.global = function(ast, scope, parent)
compiler.assert((#ast == 3), "expected name and value", ast)
compiler.destructure(ast[2], ast[3], ast, scope, parent, {forceglobal = true, nomulti = true, symtype = "global"})
return nil
end
doc_special("global", {"name", "val"}, "Set name as a global with val.")
SPECIALS.set = function(ast, scope, parent)
compiler.assert((#ast == 3), "expected name and value", ast)
compiler.destructure(ast[2], ast[3], ast, scope, parent, {noundef = true, symtype = "set"})
return nil
end
doc_special("set", {"name", "val"}, "Set a local variable to a new value. Only works on locals using var.")
local function set_forcibly_21_2a(ast, scope, parent)
compiler.assert((#ast == 3), "expected name and value", ast)
compiler.destructure(ast[2], ast[3], ast, scope, parent, {forceset = true, symtype = "set"})
return nil
end
SPECIALS["set-forcibly!"] = set_forcibly_21_2a
local function local_2a(ast, scope, parent)
compiler.assert((#ast == 3), "expected name and value", ast)
compiler.destructure(ast[2], ast[3], ast, scope, parent, {declaration = true, nomulti = true, symtype = "local"})
return nil
end
SPECIALS["local"] = local_2a
doc_special("local", {"name", "val"}, "Introduce new top-level immutable local.")
SPECIALS.var = function(ast, scope, parent)
compiler.assert((#ast == 3), "expected name and value", ast)
compiler.destructure(ast[2], ast[3], ast, scope, parent, {declaration = true, isvar = true, nomulti = true, symtype = "var"})
return nil
end
doc_special("var", {"name", "val"}, "Introduce new mutable local.")
local function kv_3f(t)
local _398_
do
local tbl_14_auto = {}
local i_15_auto = #tbl_14_auto
for k in pairs(t) do
local val_16_auto
if not ("number" == type(k)) then
val_16_auto = k
else
val_16_auto = nil
end
if (nil ~= val_16_auto) then
i_15_auto = (i_15_auto + 1)
do end (tbl_14_auto)[i_15_auto] = val_16_auto
else
end
end
_398_ = tbl_14_auto
end
return (_398_)[1]
end
SPECIALS.let = function(ast, scope, parent, opts)
local bindings = ast[2]
local pre_syms = {}
compiler.assert((utils["table?"](bindings) and not kv_3f(bindings)), "expected binding sequence", bindings)
compiler.assert(((#bindings % 2) == 0), "expected even number of name/value bindings", ast[2])
compiler.assert((#ast >= 3), "expected body expression", ast[1])
for _ = 1, (opts.nval or 0) do
table.insert(pre_syms, compiler.gensym(scope))