-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfennel.lua
More file actions
7061 lines (7061 loc) · 294 KB
/
fennel.lua
File metadata and controls
7061 lines (7061 loc) · 294 KB
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
-- SPDX-License-Identifier: MIT
-- SPDX-FileCopyrightText: Calvin Rose and contributors
package.preload["fennel.repl"] = package.preload["fennel.repl"] or function(...)
local _760_ = require("fennel.utils")
local utils = _760_
local copy = _760_["copy"]
local parser = require("fennel.parser")
local compiler = require("fennel.compiler")
local specials = require("fennel.specials")
local view = require("fennel.view")
local depth = 0
local function prompt_for(top_3f)
if top_3f then
return (string.rep(">", (depth + 1)) .. " ")
else
return (string.rep(".", (depth + 1)) .. " ")
end
end
local function default_read_chunk(parser_state)
io.write(prompt_for((0 == parser_state["stack-size"])))
io.flush()
local _762_0 = io.read()
if (nil ~= _762_0) then
local input = _762_0
return (input .. "\n")
end
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)
local function _765_()
local _764_0 = errtype
if (_764_0 == "Runtime") then
return (compiler.traceback(tostring(err), 4) .. "\n")
else
local _ = _764_0
return ("%s error: %s\n"):format(errtype, tostring(err))
end
end
return io.write(_765_())
end
local function splice_save_locals(env, lua_source, scope)
local saves = nil
do
local tbl_17_ = {}
local i_18_ = #tbl_17_
for name in pairs(env.___replLocals___) do
local val_19_ = ("local %s = ___replLocals___[%q]"):format((scope.manglings[name] or name), name)
if (nil ~= val_19_) then
i_18_ = (i_18_ + 1)
tbl_17_[i_18_] = val_19_
end
end
saves = tbl_17_
end
local binds = nil
do
local tbl_17_ = {}
local i_18_ = #tbl_17_
for raw, name in pairs(scope.manglings) do
local val_19_ = nil
if (scope.symmeta[raw] and not scope.gensyms[name]) then
val_19_ = ("___replLocals___[%q] = %s"):format(raw, name)
else
val_19_ = nil
end
if (nil ~= val_19_) then
i_18_ = (i_18_ + 1)
tbl_17_[i_18_] = val_19_
end
end
binds = tbl_17_
end
local gap = nil
if lua_source:find("\n") then
gap = "\n"
else
gap = " "
end
local function _771_()
if next(saves) then
return (table.concat(saves, " ") .. gap)
else
return ""
end
end
local function _774_()
local _772_0, _773_0 = lua_source:match("^(.*)[\n ](return .*)$")
if ((nil ~= _772_0) and (nil ~= _773_0)) then
local body = _772_0
local _return = _773_0
return (body .. gap .. table.concat(binds, " ") .. gap .. _return)
else
local _ = _772_0
return lua_source
end
end
return (_771_() .. _774_())
end
local commands = {}
local function completer(env, scope, text, _3ffulltext, _from, _to)
local max_items = 2000
local seen = {}
local matches = {}
local input_fragment = text:gsub(".*[%s)(]+", "")
local stop_looking_3f = false
local function add_partials(input, tbl, prefix)
local scope_first_3f = ((tbl == env) or (tbl == env.___replLocals___))
local tbl_17_ = matches
local i_18_ = #tbl_17_
local function _776_()
if scope_first_3f then
return scope.manglings
else
return tbl
end
end
for k, is_mangled in utils.allpairs(_776_()) do
if (max_items <= #matches) then break end
local val_19_ = nil
do
local lookup_k = nil
if scope_first_3f then
lookup_k = is_mangled
else
lookup_k = k
end
if ((type(k) == "string") and (input == k:sub(0, #input)) and not seen[k] and ((":" ~= prefix:sub(-1)) or ("function" == type(tbl[lookup_k])))) then
seen[k] = true
val_19_ = (prefix .. k)
else
val_19_ = nil
end
end
if (nil ~= val_19_) then
i_18_ = (i_18_ + 1)
tbl_17_[i_18_] = val_19_
end
end
return tbl_17_
end
local function descend(input, tbl, prefix, add_matches, method_3f)
local splitter = nil
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 .. ":"))
else
return add_matches(tail, tbl[raw_head], (prefix .. head))
end
end
end
local function add_matches(input, tbl, _3fprefix)
local prefix = nil
if _3fprefix then
prefix = (_3fprefix .. ".")
else
prefix = ""
end
if (not input:find("%.") and input:find(":")) then
return descend(input, tbl, prefix, add_matches, true)
elseif not input:find("%.") then
return add_partials(input, tbl, prefix)
else
return descend(input, tbl, prefix, add_matches, false)
end
end
do
local _785_0 = tostring((_3ffulltext or text)):match("^%s*,([^%s()[%]]*)$")
if (nil ~= _785_0) then
local cmd_fragment = _785_0
add_partials(cmd_fragment, commands, ",")
else
local _ = _785_0
for _0, 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
end
end
return matches
end
local function command_3f(input)
return input:match("^%s*,")
end
local function command_docs()
local _787_
do
local tbl_17_ = {}
local i_18_ = #tbl_17_
for name, f in utils.stablepairs(commands) do
local val_19_ = (" ,%s - %s"):format(name, ((compiler.metadata):get(f, "fnl/docstring") or "undocumented"))
if (nil ~= val_19_) then
i_18_ = (i_18_ + 1)
tbl_17_[i_18_] = val_19_
end
end
_787_ = tbl_17_
end
return table.concat(_787_, "\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 ,return FORM - Evaluate FORM and return its value to the REPL's caller.\n ,exit - Leave the repl.\n\nUse ,doc something to see descriptions for individual macros and special forms.\nValues from previous inputs are kept in *1, *2, and *3.\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 _789_0, _790_0 = pcall(specials["load-code"]("return require(...)", env), module_name)
if ((_789_0 == true) and (nil ~= _790_0)) then
local old = _790_0
local old_macro_module = specials["macro-loaded"][module_name]
local _ = nil
specials["macro-loaded"][module_name] = nil
_ = nil
local _0 = nil
package.loaded[module_name] = nil
_0 = nil
local new = nil
do
local _791_0, _792_0 = pcall(require, module_name)
if ((_791_0 == true) and (nil ~= _792_0)) then
local new0 = _792_0
new = new0
elseif (true and (nil ~= _792_0)) then
local _1 = _791_0
local msg = _792_0
on_error("Repl", msg)
specials["macro-loaded"][module_name] = old_macro_module
new = old
else
new = nil
end
end
if ((type(old) == "table") and (type(new) == "table")) then
for k, v in pairs(new) do
old[k] = v
end
for k in pairs(old) do
if (nil == new[k]) then
old[k] = nil
end
end
package.loaded[module_name] = old
end
return on_values({"ok"})
elseif ((_789_0 == false) and (nil ~= _790_0)) then
local msg = _790_0
if msg:match("loop or previous error loading module") then
package.loaded[module_name] = nil
return reload(module_name, env, on_values, on_error)
elseif specials["macro-loaded"][module_name] then
specials["macro-loaded"][module_name] = nil
return nil
else
local function _797_()
local _796_0 = msg:gsub("\n.*", "")
return _796_0
end
return on_error("Runtime", _797_())
end
end
end
local function run_command(read, on_error, f)
local _800_0, _801_0, _802_0 = pcall(read)
if ((_800_0 == true) and (_801_0 == true) and (nil ~= _802_0)) then
local val = _802_0
local _803_0, _804_0 = pcall(f, val)
if ((_803_0 == false) and (nil ~= _804_0)) then
local msg = _804_0
return on_error("Runtime", msg)
end
elseif (_800_0 == false) then
return on_error("Parse", "Couldn't parse input.")
end
end
commands.reload = function(env, read, on_values, on_error)
local function _807_(_241)
return reload(tostring(_241), env, on_values, on_error)
end
return run_command(read, on_error, _807_)
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 _808_()
return on_values(completer(env, scope, table.concat(chars):gsub("^%s*,complete%s+", ""):sub(1, -2)))
end
return run_command(read, on_error, _808_)
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 _809_0 = type(subtbl)
if (_809_0 == "function") then
if ((prefix .. name)):match(pattern) then
table.insert(names, (prefix .. name))
end
elseif (_809_0 == "table") then
if not seen[subtbl] then
local _811_
do
seen[subtbl] = true
_811_ = seen
end
apropos_2a(pattern, subtbl, (prefix .. name:gsub("%.", "/") .. "."), _811_, names)
end
end
end
end
return names
end
local function apropos(pattern)
return apropos_2a(pattern:gsub("^_G%.", ""), package.loaded, "", {}, {})
end
commands.apropos = function(_env, read, on_values, on_error, _scope)
local function _815_(_241)
return on_values(apropos(tostring(_241)))
end
return run_command(read, on_error, _815_)
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 = nil
do
local tbl_17_ = {}
local i_18_ = #tbl_17_
for p in path:gmatch("[^%.]+") do
local val_19_ = p
if (nil ~= val_19_) then
i_18_ = (i_18_ + 1)
tbl_17_[i_18_] = val_19_
end
end
paths = tbl_17_
end
local tgt = package.loaded
for _, path0 in ipairs(paths) do
if (nil == tgt) then break end
local _818_
do
local _817_0 = path0:gsub("%/", ".")
_818_ = _817_0
end
tgt = tgt[_818_]
end
return tgt
end
local function apropos_doc(pattern)
local tbl_17_ = {}
local i_18_ = #tbl_17_
for _, path in ipairs(apropos(".*")) do
local val_19_ = nil
do
local tgt = apropos_follow_path(path)
if ("function" == type(tgt)) then
local _819_0 = (compiler.metadata):get(tgt, "fnl/docstring")
if (nil ~= _819_0) then
local docstr = _819_0
val_19_ = (docstr:match(pattern) and path)
else
val_19_ = nil
end
else
val_19_ = nil
end
end
if (nil ~= val_19_) then
i_18_ = (i_18_ + 1)
tbl_17_[i_18_] = val_19_
end
end
return tbl_17_
end
commands["apropos-doc"] = function(_env, read, on_values, on_error, _scope)
local function _823_(_241)
return on_values(apropos_doc(tostring(_241)))
end
return run_command(read, on_error, _823_)
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({})
end
end
return nil
end
commands["apropos-show-docs"] = function(_env, read, on_values, on_error)
local function _825_(_241)
return apropos_show_docs(on_values, tostring(_241))
end
return run_command(read, on_error, _825_)
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, _826_0, scope)
local _827_ = _826_0
local env = _827_
local ___replLocals___ = _827_["___replLocals___"]
local e = nil
local function _828_(_241, _242)
return (___replLocals___[scope.unmanglings[_242]] or env[_242])
end
e = setmetatable({}, {__index = _828_})
local function _829_(...)
local _830_0, _831_0 = ...
if ((_830_0 == true) and (nil ~= _831_0)) then
local code = _831_0
local function _832_(...)
local _833_0, _834_0 = ...
if ((_833_0 == true) and (nil ~= _834_0)) then
local val = _834_0
return val
else
local _ = _833_0
return nil
end
end
return _832_(pcall(specials["load-code"](code, e)))
else
local _ = _830_0
return nil
end
end
return _829_(pcall(compiler["compile-string"], tostring(identifier), {scope = scope}))
end
commands.find = function(env, read, on_values, on_error, scope)
local function _837_(_241)
local _838_0 = nil
do
local _839_0 = utils["sym?"](_241)
if (nil ~= _839_0) then
local _840_0 = resolve(_839_0, env, scope)
if (nil ~= _840_0) then
_838_0 = debug.getinfo(_840_0)
else
_838_0 = _840_0
end
else
_838_0 = _839_0
end
end
local function _843_()
local line = _838_0.linedefined
local source = _838_0.source
return (("string" == type(source)) and ("@" == source:sub(1, 1)))
end
if (((_G.type(_838_0) == "table") and (nil ~= _838_0.linedefined) and (nil ~= _838_0.source) and (_838_0.what == "Lua")) and _843_()) then
local line = _838_0.linedefined
local source = _838_0.source
local fnlsrc = nil
do
local _844_0 = compiler.sourcemap
if (nil ~= _844_0) then
_844_0 = _844_0[source]
end
if (nil ~= _844_0) then
_844_0 = _844_0[line]
end
if (nil ~= _844_0) then
_844_0 = _844_0[2]
end
fnlsrc = _844_0
end
return on_values({string.format("%s:%s", source:sub(2), (fnlsrc or line))})
elseif (_838_0 == nil) then
return on_error("Repl", "Unknown value")
else
local _ = _838_0
return on_error("Repl", "No source info")
end
end
return run_command(read, on_error, _837_)
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 _849_(_241)
local name = tostring(_241)
local path = (utils["multi-sym?"](name) or {name})
local ok_3f, target = nil, nil
local function _850_()
return (scope.specials[name] or utils["get-in"](scope.macros, path) or resolve(name, env, scope))
end
ok_3f, target = pcall(_850_)
if ok_3f then
return on_values({specials.doc(target, name)})
else
return on_error("Repl", ("Could not find " .. name .. " for docs."))
end
end
return run_command(read, on_error, _849_)
end
do end (compiler.metadata):set(commands.doc, "fnl/docstring", "Print the docstring and arglist for a function, macro, or special form.")
commands.compile = function(_, read, on_values, on_error, _0, _1, opts)
local function _852_(_241)
local _853_0, _854_0 = pcall(compiler.compile, _241, opts)
if ((_853_0 == true) and (nil ~= _854_0)) then
local result = _854_0
return on_values({result})
elseif (true and (nil ~= _854_0)) then
local _2 = _853_0
local msg = _854_0
return on_error("Repl", ("Error compiling expression: " .. msg))
end
end
return run_command(read, on_error, _852_)
end
do end (compiler.metadata):set(commands.compile, "fnl/docstring", "compiles the expression into lua and prints the result.")
local function load_plugin_commands(plugins)
for i = #(plugins or {}), 1, -1 do
for name, f in pairs(plugins[i]) do
local _856_0 = name:match("^repl%-command%-(.*)")
if (nil ~= _856_0) then
local cmd_name = _856_0
commands[cmd_name] = f
end
end
end
return nil
end
local function run_command_loop(input, read, loop, env, on_values, on_error, scope, chars, opts)
local command_name = input:match(",([^%s/]+)")
do
local _858_0 = commands[command_name]
if (nil ~= _858_0) then
local command = _858_0
command(env, read, on_values, on_error, scope, chars, opts)
else
local _ = _858_0
if ((command_name ~= "exit") and (command_name ~= "return")) then
on_values({"Unknown command", command_name})
end
end
end
if ("exit" ~= command_name) then
return loop((command_name == "return"))
end
end
local function try_readline_21(opts, ok, readline)
if ok then
if readline.set_readline_name then
readline.set_readline_name("fennel")
end
readline.set_options({histfile = "", keeplines = 1000})
opts.readChunk = function(parser_state)
local _863_0 = readline.readline(prompt_for((0 == parser_state["stack-size"])))
if (nil ~= _863_0) then
local input = _863_0
return (input .. "\n")
end
end
local completer0 = nil
opts.registerCompleter = function(repl_completer)
completer0 = repl_completer
return nil
end
local function repl_completer(text, from, to)
if completer0 then
readline.set_completion_append_character("")
return completer0(text:sub(from, to), text, from, to)
else
return {}
end
end
readline.set_complete_function(repl_completer)
return readline
end
end
local function should_use_readline_3f(opts)
return (("dumb" ~= os.getenv("TERM")) and not opts.readChunk and not opts.registerCompleter)
end
local function repl(_3foptions)
local old_root_options = utils.root.options
local _867_ = copy(_3foptions)
local opts = _867_
local _3ffennelrc = _867_["fennelrc"]
local _ = nil
opts.fennelrc = nil
_ = nil
local readline = (should_use_readline_3f(opts) and try_readline_21(opts, pcall(require, "readline")))
local _0 = nil
if _3ffennelrc then
_0 = _3ffennelrc()
else
_0 = nil
end
local env = specials["wrap-env"]((opts.env or rawget(_G, "_ENV") or _G))
local callbacks = {["view-opts"] = (opts["view-opts"] or {depth = 4}), env = env, onError = (opts.onError or default_on_error), onValues = (opts.onValues or default_on_values), pp = (opts.pp or view), readChunk = (opts.readChunk or default_read_chunk)}
local save_locals_3f = (opts.saveLocals ~= false)
local byte_stream, clear_stream = nil, nil
local function _869_(_241)
return callbacks.readChunk(_241)
end
byte_stream, clear_stream = parser.granulate(_869_)
local chars = {}
local read, reset = nil, nil
local function _870_(parser_state)
local b = byte_stream(parser_state)
if b then
table.insert(chars, string.char(b))
end
return b
end
read, reset = parser.parser(_870_)
depth = (depth + 1)
if opts.message then
callbacks.onValues({opts.message})
end
env.___repl___ = callbacks
opts.env, opts.scope = env, compiler["make-scope"]()
opts.useMetadata = (opts.useMetadata ~= false)
if (opts.allowedGlobals == nil) then
opts.allowedGlobals = specials["current-global-names"](env)
end
if opts.init then
opts.init(opts, depth)
end
if opts.registerCompleter then
local function _876_()
local _875_0 = opts.scope
local function _877_(...)
return completer(env, _875_0, ...)
end
return _877_
end
opts.registerCompleter(_876_())
end
load_plugin_commands(opts.plugins)
if save_locals_3f then
local function newindex(t, k, v)
if opts.scope.manglings[k] then
return rawset(t, k, v)
end
end
env.___replLocals___ = setmetatable({}, {__newindex = newindex})
end
local function print_values(...)
local vals = {...}
local out = {}
local pp = callbacks.pp
env._, env.__ = vals[1], vals
for i = 1, select("#", ...) do
table.insert(out, pp(vals[i], callbacks["view-opts"]))
end
return callbacks.onValues(out)
end
local function save_value(...)
env.___replLocals___["*3"] = env.___replLocals___["*2"]
env.___replLocals___["*2"] = env.___replLocals___["*1"]
env.___replLocals___["*1"] = ...
return ...
end
opts.scope.manglings["*1"], opts.scope.unmanglings._1 = "_1", "*1"
opts.scope.manglings["*2"], opts.scope.unmanglings._2 = "_2", "*2"
opts.scope.manglings["*3"], opts.scope.unmanglings._3 = "_3", "*3"
local function loop(_3fexit_next_3f)
for k in pairs(chars) do
chars[k] = nil
end
reset()
local ok, parser_not_eof_3f, form = pcall(read)
local src_string = table.concat(chars)
local readline_not_eof_3f = (not readline or (src_string ~= "(null)"))
local not_eof_3f = (readline_not_eof_3f and parser_not_eof_3f)
if not ok then
callbacks.onError("Parse", not_eof_3f)
clear_stream()
return loop()
elseif command_3f(src_string) then
return run_command_loop(src_string, read, loop, env, callbacks.onValues, callbacks.onError, opts.scope, chars, opts)
else
if not_eof_3f then
local function _881_(...)
local _882_0, _883_0 = ...
if ((_882_0 == true) and (nil ~= _883_0)) then
local src = _883_0
local function _884_(...)
local _885_0, _886_0 = ...
if ((_885_0 == true) and (nil ~= _886_0)) then
local chunk = _886_0
local function _887_()
return print_values(save_value(chunk()))
end
local function _888_(...)
return callbacks.onError("Runtime", ...)
end
return xpcall(_887_, _888_)
elseif ((_885_0 == false) and (nil ~= _886_0)) then
local msg = _886_0
clear_stream()
return callbacks.onError("Compile", msg)
end
end
local function _891_(...)
local src0 = nil
if save_locals_3f then
src0 = splice_save_locals(env, src, opts.scope)
else
src0 = src
end
return pcall(specials["load-code"], src0, env)
end
return _884_(_891_(...))
elseif ((_882_0 == false) and (nil ~= _883_0)) then
local msg = _883_0
clear_stream()
return callbacks.onError("Compile", msg)
end
end
local function _893_()
opts["source"] = src_string
return opts
end
_881_(pcall(compiler.compile, form, _893_()))
utils.root.options = old_root_options
if _3fexit_next_3f then
return env.___replLocals___["*1"]
else
return loop()
end
end
end
end
local value = loop()
depth = (depth - 1)
if readline then
readline.save_history()
end
if opts.exit then
opts.exit(opts, depth)
end
return value
end
local repl_mt = {__index = {repl = repl}}
repl_mt.__call = function(_899_0, _3fopts)
local _900_ = _899_0
local overrides = _900_
local view_opts = _900_["view-opts"]
local opts = copy(_3fopts, copy(overrides))
local _902_
do
local _901_0 = _3fopts
if (nil ~= _901_0) then
_901_0 = _901_0["view-opts"]
end
_902_ = _901_0
end
opts["view-opts"] = copy(_902_, copy(view_opts))
return repl(opts)
end
return setmetatable({["view-opts"] = {}}, repl_mt)
end
package.preload["fennel.specials"] = package.preload["fennel.specials"] or function(...)
local _530_ = require("fennel.utils")
local utils = _530_
local pack = _530_["pack"]
local unpack = _530_["unpack"]
local view = require("fennel.view")
local parser = require("fennel.parser")
local compiler = require("fennel.compiler")
local SPECIALS = compiler.scopes.global.specials
local function str1(x)
return tostring(x[1])
end
local function wrap_env(env)
local function _531_(_, key)
if utils["string?"](key) then
return env[compiler["global-unmangling"](key)]
else
return env[key]
end
end
local function _533_(_, key, value)
if utils["string?"](key) then
env[compiler["global-unmangling"](key)] = value
return nil
else
env[key] = value
return nil
end
end
local function _535_()
local _536_
do
local tbl_14_ = {}
for k, v in utils.stablepairs(env) do
local k_15_, v_16_ = nil, nil
local _537_
if utils["string?"](k) then
_537_ = compiler["global-unmangling"](k)
else
_537_ = k
end
k_15_, v_16_ = _537_, v
if ((k_15_ ~= nil) and (v_16_ ~= nil)) then
tbl_14_[k_15_] = v_16_
end
end
_536_ = tbl_14_
end
return next, _536_, nil
end
return setmetatable({}, {__index = _531_, __newindex = _533_, __pairs = _535_})
end
local function fennel_module_name()
return (utils.root.options.moduleName or "fennel")
end
local function current_global_names(_3fenv)
local mt = nil
do
local _540_0 = getmetatable(_3fenv)
if ((_G.type(_540_0) == "table") and (nil ~= _540_0.__pairs)) then
local mtpairs = _540_0.__pairs
local tbl_14_ = {}
for k, v in mtpairs(_3fenv) do
local k_15_, v_16_ = k, v
if ((k_15_ ~= nil) and (v_16_ ~= nil)) then
tbl_14_[k_15_] = v_16_
end
end
mt = tbl_14_
elseif (_540_0 == nil) then
mt = (_3fenv or _G)
else
mt = nil
end
end
local function _543_()
local tbl_17_ = {}
local i_18_ = #tbl_17_
for k in utils.stablepairs(mt) do
local val_19_ = compiler["global-unmangling"](k)
if (nil ~= val_19_) then
i_18_ = (i_18_ + 1)
tbl_17_[i_18_] = val_19_
end
end
return tbl_17_
end
return (mt and _543_())
end
local function load_code(code, _3fenv, _3ffilename)
local env = (_3fenv or rawget(_G, "_ENV") or _G)
local _545_0, _546_0 = rawget(_G, "setfenv"), rawget(_G, "loadstring")
if ((nil ~= _545_0) and (nil ~= _546_0)) then
local setfenv = _545_0
local loadstring = _546_0
local f = assert(loadstring(code, _3ffilename, "t"))
setfenv(f, env)
return f
else
local _ = _545_0
return assert(load(code, _3ffilename, "t", env))
end
end
local function v__3edocstring(tgt)
return (((compiler.metadata):get(tgt, "fnl/docstring") or "#<undocumented>")):gsub("\n$", ""):gsub("\n", "\n ")
end
local function doc_2a(tgt, name)
assert(("string" == type(name)), "name must be a string")
if not tgt then
return (name .. " not found")
else
local function _549_()
local _548_0 = getmetatable(tgt)
if ((_G.type(_548_0) == "table") and true) then
local __call = _548_0.__call
return ("function" == type(__call))
end
end
if ((type(tgt) == "function") or _549_()) then
local arglist = ((compiler.metadata):get(tgt, "fnl/arglist") or {"#<unknown-arguments>"})
local elts = nil
local function _551_()
local tbl_17_ = {}
local i_18_ = #tbl_17_
for _, a in ipairs(arglist) do
local val_19_ = tostring(a)
if (nil ~= val_19_) then
i_18_ = (i_18_ + 1)
tbl_17_[i_18_] = val_19_
end
end
return tbl_17_
end
elts = {name, unpack(_551_())}
return string.format("(%s)\n %s", table.concat(elts, " "), v__3edocstring(tgt))
else
return string.format("%s\n %s", name, v__3edocstring(tgt))
end
end
end
local function doc_special(name, arglist, docstring, _3fbody_form_3f)
for i, a in ipairs(arglist) do
if ("table" == type(a)) then
arglist[i] = ("[" .. table.concat(a, " ") .. "]")
end
end
compiler.metadata[SPECIALS[name]] = {["fnl/arglist"] = arglist, ["fnl/body-form?"] = _3fbody_form_3f, ["fnl/docstring"] = docstring}
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}
utils.hook("pre-do", ast, sub_scope)
local function compile_body(outer_target, outer_tail, _3fouter_retexprs)
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])
end
end
compiler.emit(parent, chunk, ast)
compiler.emit(parent, "end", ast)
utils.hook("do", ast, sub_scope)
return (_3fouter_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))
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 = nil
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)
local function iter_args(ast)
local ast0, len, i = ast, #ast, 1
local function _558_()
i = (1 + i)
while ((i == len) and utils["call-of?"](ast0[i], "values")) do
ast0 = ast0[i]
len = #ast0
i = 2
end
return ast0[i], (nil == ast0[(i + 1)])
end
return _558_
end
SPECIALS.values = function(ast, scope, parent)
local exprs = {}
for subast, last_3f in iter_args(ast) do
local subexprs = compiler.compile1(subast, scope, parent, {nval = (not last_3f and 1)})
table.insert(exprs, subexprs[1])
if last_3f then
for j = 2, #subexprs do
table.insert(exprs, subexprs[j])
end
end
end
return exprs
end
doc_special("values", {"..."}, "Return multiple values from a function. Must be in tail position.")