forked from bakpakin/Fennel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fennel.lua
3117 lines (2913 loc) · 120 KB
/
fennel.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
--[[
Copyright (c) 2016-2019 Calvin Rose and contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
-- Make global variables local.
local setmetatable = setmetatable
local getmetatable = getmetatable
local type = type
local assert = assert
local pairs = pairs
local ipairs = ipairs
local tostring = tostring
local unpack = unpack or table.unpack
--
-- Main Types and support functions
--
-- Like pairs, but gives consistent ordering every time. On 5.1, 5.2, and LuaJIT
-- pairs is already stable, but on 5.3 every run gives different ordering.
local function stablepairs(t)
local keys, succ = {}, {}
for k in pairs(t) do table.insert(keys, k) end
table.sort(keys, function(a, b) return tostring(a) < tostring(b) end)
for i,k in ipairs(keys) do succ[k] = keys[i+1] end
local function stablenext(tbl, idx)
if idx == nil then return keys[1], tbl[keys[1]] end
return succ[idx], tbl[succ[idx]]
end
return stablenext, t, nil
end
-- Map function f over sequential table t, removing values where f returns nil.
-- Optionally takes a target table to insert the mapped values into.
local function map(t, f, out)
out = out or {}
if type(f) ~= "function" then local s = f f = function(x) return x[s] end end
for _,x in ipairs(t) do
local v = f(x)
if v then table.insert(out, v) end
end
return out
end
-- Map function f over key/value table t, similar to above, but it can return a
-- sequential table if f returns a single value or a k/v table if f returns two.
-- Optionally takes a target table to insert the mapped values into.
local function kvmap(t, f, out)
out = out or {}
if type(f) ~= "function" then local s = f f = function(x) return x[s] end end
for k,x in stablepairs(t) do
local korv, v = f(k, x)
if korv and not v then table.insert(out, korv) end
if korv and v then out[korv] = v end
end
return out
end
local function allPairs(t)
assert(type(t) == 'table', 'allPairs expects a table')
local seen = {}
local function allPairsNext(_, state)
local nextState, value = next(t, state)
if seen[nextState] then
return allPairsNext(nil, nextState)
elseif nextState then
seen[nextState] = true
return nextState, value
end
local meta = getmetatable(t)
if meta and meta.__index then
t = meta.__index
return allPairsNext(t)
end
end
return allPairsNext
end
local function deref(self) return self[1] end
local function listToString(self, tostring2)
return '(' .. table.concat(map(self, tostring2 or tostring), ' ', 1, #self) .. ')'
end
local SYMBOL_MT = { 'SYMBOL', __tostring = deref, __fennelview = deref }
local EXPR_MT = { 'EXPR', __tostring = deref }
local VARARG = setmetatable({ '...' },
{ 'VARARG', __tostring = deref, __fennelview = deref })
local LIST_MT = { 'LIST', __tostring = listToString, __fennelview = listToString }
local SEQUENCE_MT = { 'SEQUENCE' }
-- Load code with an environment in all recent Lua versions
local function loadCode(code, environment, filename)
environment = environment or _ENV or _G
if setfenv and loadstring then
local f = assert(loadstring(code, filename))
setfenv(f, environment)
return f
else
return assert(load(code, filename, "t", environment))
end
end
-- Safely load an environment variable
local getenv = os and os.getenv or function() end
local function debugOn(flag)
local level = getenv("FENNEL_DEBUG") or ""
return level == "all" or level:find(flag)
end
-- Create a new list. Lists are a compile-time construct in Fennel; they are
-- represented as tables with a special marker metatable. They only come from
-- the parser, and they represent code which comes from reading a paren form;
-- they are specifically not cons cells.
local function list(...)
return setmetatable({...}, LIST_MT)
end
-- Create a new symbol. Symbols are a compile-time construct in Fennel and are
-- not exposed outside the compiler. Symbols have source data describing what
-- file, line, etc that they came from.
local function sym(str, scope, source)
local s = {str, scope = scope}
for k, v in pairs(source or {}) do
if type(k) == 'string' then s[k] = v end
end
return setmetatable(s, SYMBOL_MT)
end
-- Create a new sequence. Sequences are tables that come from the parser when
-- it encounters a form with square brackets. They are treated as regular tables
-- except when certain macros need to look for binding forms, etc specifically.
local function sequence(...)
return setmetatable({...}, SEQUENCE_MT)
end
-- Create a new expr
-- etype should be one of
-- "literal", -- literals like numbers, strings, nil, true, false
-- "expression", -- Complex strings of Lua code, may have side effects, etc, but is an expression
-- "statement", -- Same as expression, but is also a valid statement (function calls).
-- "vargs", -- varargs symbol
-- "sym", -- symbol reference
local function expr(strcode, etype)
return setmetatable({ strcode, type = etype }, EXPR_MT)
end
local function varg()
return VARARG
end
local function isVarg(x)
return x == VARARG and x
end
-- Checks if an object is a List. Returns the object if is a List.
local function isList(x)
return type(x) == 'table' and getmetatable(x) == LIST_MT and x
end
-- Checks if an object is a symbol. Returns the object if it is a symbol.
local function isSym(x)
return type(x) == 'table' and getmetatable(x) == SYMBOL_MT and x
end
-- Checks if an object any kind of table, EXCEPT list or symbol
local function isTable(x)
return type(x) == 'table' and
x ~= VARARG and
getmetatable(x) ~= LIST_MT and getmetatable(x) ~= SYMBOL_MT and x
end
-- Checks if an object is a sequence (created with a [] literal)
local function isSequence(x)
return type(x) == 'table' and getmetatable(x) == SEQUENCE_MT and x
end
-- Returns a shallow copy of its table argument. Returns an empty table on nil.
local function copy(from)
local to = {}
for k, v in pairs(from or {}) do to[k] = v end
return to
end
--
-- Parser
--
-- Convert a stream of chunks to a stream of bytes.
-- Also returns a second function to clear the buffer in the byte stream
local function granulate(getchunk)
local c = ''
local index = 1
local done = false
return function (parserState)
if done then return nil end
if index <= #c then
local b = c:byte(index)
index = index + 1
return b
else
c = getchunk(parserState)
if not c or c == '' then
done = true
return nil
end
index = 2
return c:byte(1)
end
end, function ()
c = ''
end
end
-- Convert a string into a stream of bytes
local function stringStream(str)
local index = 1
return function()
local r = str:byte(index)
index = index + 1
return r
end
end
-- Table of delimiter bytes - (, ), [, ], {, }
-- Opener keys have closer as the value, and closers keys
-- have true as their value.
local delims = {
[40] = 41, -- (
[41] = true, -- )
[91] = 93, -- [
[93] = true, -- ]
[123] = 125, -- {
[125] = true -- }
}
local function iswhitespace(b)
return b == 32 or (b >= 9 and b <= 13)
end
local function issymbolchar(b)
return b > 32 and
not delims[b] and
b ~= 127 and -- "<BS>"
b ~= 34 and -- "\""
b ~= 39 and -- "'"
b ~= 126 and -- "~"
b ~= 59 and -- ";"
b ~= 44 and -- ","
b ~= 64 and -- "@"
b ~= 96 -- "`"
end
local prefixes = { -- prefix chars substituted while reading
[96] = 'quote', -- `
[44] = 'unquote', -- ,
[39] = 'quote', -- '
[35] = 'hashfn' -- #
}
-- The resetRoot function needs to be called at every exit point of the compiler
-- including when there's a parse error or compiler error. Introduce it up here
-- so error functions have access to it, and set it when we have values below.
local resetRoot = nil
-- Parse one value given a function that
-- returns sequential bytes. Will throw an error as soon
-- as possible without getting more bytes on bad input. Returns
-- if a value was read, and then the value read. Will return nil
-- when input stream is finished.
local function parser(getbyte, filename, options)
-- Stack of unfinished values
local stack = {}
-- Provide one character buffer and keep
-- track of current line and byte index
local line = 1
local byteindex = 0
local lastb
local function ungetb(ub)
if ub == 10 then line = line - 1 end
byteindex = byteindex - 1
lastb = ub
end
local function getb()
local r
if lastb then
r, lastb = lastb, nil
else
r = getbyte({ stackSize = #stack })
end
byteindex = byteindex + 1
if r == 10 then line = line + 1 end
return r
end
-- If you add new calls to this function, please update fenneldfriend.fnl
-- as well to add suggestions for how to fix the new error.
local function parseError(msg)
if resetRoot then resetRoot() end
local override = options and options["parse-error"]
if override then override(msg, filename or "unknown", line or "?", byteindex) end
return error(("Parse error in %s:%s: %s"):
format(filename or "unknown", line or "?", msg), 0)
end
-- Parse stream
return function()
-- Dispatch when we complete a value
local done, retval
local whitespaceSinceDispatch = true
local function dispatch(v)
if #stack == 0 then
retval = v
done = true
elseif stack[#stack].prefix then
local stacktop = stack[#stack]
stack[#stack] = nil
return dispatch(list(sym(stacktop.prefix), v))
else
table.insert(stack[#stack], v)
end
whitespaceSinceDispatch = false
end
-- Throw nice error when we expect more characters
-- but reach end of stream.
local function badend()
local accum = map(stack, "closer")
parseError(('expected closing delimiter%s %s'):format(
#stack == 1 and "" or "s",
string.char(unpack(accum))))
end
-- The main parse loop
repeat
local b
-- Skip whitespace
repeat
b = getb()
if b and iswhitespace(b) then
whitespaceSinceDispatch = true
end
until not b or not iswhitespace(b)
if not b then
if #stack > 0 then badend() end
return nil
end
if b == 59 then -- ; Comment
repeat
b = getb()
until not b or b == 10 -- newline
elseif type(delims[b]) == 'number' then -- Opening delimiter
if not whitespaceSinceDispatch then
parseError('expected whitespace before opening delimiter '
.. string.char(b))
end
table.insert(stack, setmetatable({
closer = delims[b],
line = line,
filename = filename,
bytestart = byteindex
}, LIST_MT))
elseif delims[b] then -- Closing delimiter
if #stack == 0 then parseError('unexpected closing delimiter '
.. string.char(b)) end
local last = stack[#stack]
local val
if last.closer ~= b then
parseError('mismatched closing delimiter ' .. string.char(b) ..
', expected ' .. string.char(last.closer))
end
last.byteend = byteindex -- Set closing byte index
if b == 41 then -- ; )
val = last
elseif b == 93 then -- ; ]
val = sequence()
for i = 1, #last do
val[i] = last[i]
end
-- for table literals we can store file/line/offset source
-- data in fields on the table itself, because the AST node
-- *is* the table, and the fields would show up in the
-- compiled output. keep them on the metatable instead.
setmetatable(val, last)
else -- ; }
if #last % 2 ~= 0 then
byteindex = byteindex - 1
parseError('expected even number of values in table literal')
end
val = {}
setmetatable(val, last) -- see note above about source data
for i = 1, #last, 2 do
if(tostring(last[i]) == ":" and isSym(last[i + 1])
and isSym(last[i])) then
last[i] = tostring(last[i + 1])
end
val[last[i]] = last[i + 1]
end
end
stack[#stack] = nil
dispatch(val)
elseif b == 34 then -- Quoted string
local state = "base"
local chars = {34}
stack[#stack + 1] = {closer = 34}
repeat
b = getb()
chars[#chars + 1] = b
if state == "base" then
if b == 92 then
state = "backslash"
elseif b == 34 then
state = "done"
end
else
-- state == "backslash"
state = "base"
end
until not b or (state == "done")
if not b then badend() end
stack[#stack] = nil
local raw = string.char(unpack(chars))
local formatted = raw:gsub("[\1-\31]", function (c) return '\\' .. c:byte() end)
local loadFn = loadCode(('return %s'):format(formatted), nil, filename)
dispatch(loadFn())
elseif prefixes[b] then
-- expand prefix byte into wrapping form eg. '`a' into '(quote a)'
table.insert(stack, {
prefix = prefixes[b]
})
local nextb = getb()
if iswhitespace(nextb) then
if b == 35 then
stack[#stack] = nil
dispatch(sym('#'))
else
parseError('invalid whitespace after quoting prefix')
end
end
ungetb(nextb)
elseif issymbolchar(b) or b == string.byte("~") then -- Try symbol
local chars = {}
local bytestart = byteindex
repeat
chars[#chars + 1] = b
b = getb()
until not b or not issymbolchar(b)
if b then ungetb(b) end
local rawstr = string.char(unpack(chars))
if rawstr == 'true' then dispatch(true)
elseif rawstr == 'false' then dispatch(false)
elseif rawstr == '...' then dispatch(VARARG)
elseif rawstr:match('^:.+$') then -- colon style strings
dispatch(rawstr:sub(2))
elseif rawstr:match("^~") and rawstr ~= "~=" then
-- for backwards-compatibility, special-case allowance of ~=
-- but all other uses of ~ are disallowed
parseError("illegal character: ~")
else
local forceNumber = rawstr:match('^%d')
local numberWithStrippedUnderscores = rawstr:gsub("_", "")
local x
if forceNumber then
x = tonumber(numberWithStrippedUnderscores) or
parseError('could not read number "' .. rawstr .. '"')
else
x = tonumber(numberWithStrippedUnderscores)
if not x then
if(rawstr:match("%.[0-9]")) then
byteindex = (byteindex - #rawstr +
rawstr:find("%.[0-9]") + 1)
parseError("can't start multisym segment " ..
"with a digit: ".. rawstr)
elseif(rawstr:match("[%.:][%.:]") and
rawstr ~= "..") then
byteindex = (byteindex - #rawstr +
rawstr:find("[%.:][%.:]") + 1)
parseError("malformed multisym: " .. rawstr)
elseif(rawstr:match(":.+[%.:]")) then
byteindex = (byteindex - #rawstr +
rawstr:find(":.+[%.:]"))
parseError("method must be last component " ..
"of multisym: " .. rawstr)
else
x = sym(rawstr, nil, { line = line,
filename = filename,
bytestart = bytestart,
byteend = byteindex, })
end
end
end
dispatch(x)
end
else
parseError("illegal character: " .. string.char(b))
end
until done
return true, retval
end, function ()
stack = {}
end
end
--
-- Compilation
--
-- Top level compilation bindings.
local rootChunk, rootScope, rootOptions
local function setResetRoot(oldChunk, oldScope, oldOptions)
local oldResetRoot = resetRoot -- this needs to nest!
resetRoot = function()
rootChunk, rootScope, rootOptions = oldChunk, oldScope, oldOptions
resetRoot = oldResetRoot
end
end
local GLOBAL_SCOPE
-- Create a new Scope, optionally under a parent scope. Scopes are compile time
-- constructs that are responsible for keeping track of local variables, name
-- mangling, and macros. They are accessible to user code via the
-- 'eval-compiler' special form (may change). They use metatables to implement
-- nesting.
local function makeScope(parent)
if not parent then parent = GLOBAL_SCOPE end
return {
unmanglings = setmetatable({}, {
__index = parent and parent.unmanglings
}),
manglings = setmetatable({}, {
__index = parent and parent.manglings
}),
specials = setmetatable({}, {
__index = parent and parent.specials
}),
macros = setmetatable({}, {
__index = parent and parent.macros
}),
symmeta = setmetatable({}, {
__index = parent and parent.symmeta
}),
includes = setmetatable({}, {
__index = parent and parent.includes
}),
refedglobals = setmetatable({}, {
__index = parent and parent.refedglobals
}),
autogensyms = {},
parent = parent,
vararg = parent and parent.vararg,
depth = parent and ((parent.depth or 0) + 1) or 0,
hashfn = parent and parent.hashfn
}
end
-- Assert a condition and raise a compile error with line numbers. The ast arg
-- should be unmodified so that its first element is the form being called.
-- If you add new calls to this function, please update fenneldfriend.fnl
-- as well to add suggestions for how to fix the new error.
local function assertCompile(condition, msg, ast)
local override = rootOptions and rootOptions["assert-compile"]
if override then
-- don't make custom handlers deal with resetting root; it's error-prone
if not condition and resetRoot then resetRoot() end
override(condition, msg, ast)
-- should we fall thru to the default check, or should we allow the
-- override to swallow the error?
end
if not condition then
if resetRoot then resetRoot() end
local m = getmetatable(ast)
local filename = m and m.filename or ast.filename or "unknown"
local line = m and m.line or ast.line or "?"
-- if we use regular `assert' we can't provide the `level' argument of 0
error(string.format("Compile error in '%s' %s:%s: %s",
tostring(isSym(ast[1]) and ast[1][1] or ast[1] or '()'),
filename, line, msg), 0)
end
return condition
end
GLOBAL_SCOPE = makeScope()
GLOBAL_SCOPE.vararg = true
local SPECIALS = GLOBAL_SCOPE.specials
local COMPILER_SCOPE = makeScope(GLOBAL_SCOPE)
local luaKeywords = {
'and', 'break', 'do', 'else', 'elseif', 'end', 'false', 'for', 'function',
'if', 'in', 'local', 'nil', 'not', 'or', 'repeat', 'return', 'then', 'true',
'until', 'while'
}
for i, v in ipairs(luaKeywords) do luaKeywords[v] = i end
local function isValidLuaIdentifier(str)
return (str:match('^[%a_][%w_]*$') and not luaKeywords[str])
end
-- Allow printing a string to Lua, also keep as 1 line.
local serializeSubst = {
['\a'] = '\\a',
['\b'] = '\\b',
['\f'] = '\\f',
['\n'] = 'n',
['\t'] = '\\t',
['\v'] = '\\v'
}
local function serializeString(str)
local s = ("%q"):format(str)
s = s:gsub('.', serializeSubst):gsub("[\128-\255]", function(c)
return "\\" .. c:byte()
end)
return s
end
-- A multi symbol is a symbol that is actually composed of
-- two or more symbols using the dot syntax. The main differences
-- from normal symbols is that they cannot be declared local, and
-- they may have side effects on invocation (metatables)
local function isMultiSym(str)
if isSym(str) then
return isMultiSym(tostring(str))
end
if type(str) ~= 'string' then return end
local parts = {}
for part in str:gmatch('[^%.%:]+[%.%:]?') do
local lastChar = part:sub(-1)
if lastChar == ":" then
parts.multiSymMethodCall = true
end
if lastChar == ":" or lastChar == "." then
parts[#parts + 1] = part:sub(1, -2)
else
parts[#parts + 1] = part
end
end
return #parts > 0 and
(str:match('%.') or str:match(':')) and
(not str:match('%.%.')) and
str:byte() ~= string.byte '.' and
str:byte(-1) ~= string.byte '.' and
parts
end
local function isQuoted(symbol) return symbol.quoted end
-- Mangler for global symbols. Does not protect against collisions,
-- but makes them unlikely. This is the mangling that is exposed to
-- to the world.
local function globalMangling(str)
if isValidLuaIdentifier(str) then
return str
end
-- Use underscore as escape character
return '__fnl_global__' .. str:gsub('[^%w]', function (c)
return ('_%02x'):format(c:byte())
end)
end
-- Reverse a global mangling. Takes a Lua identifier and
-- returns the fennel symbol string that created it.
local function globalUnmangling(identifier)
local rest = identifier:match('^__fnl_global__(.*)$')
if rest then
local r = rest:gsub('_[%da-f][%da-f]', function (code)
return string.char(tonumber(code:sub(2), 16))
end)
return r -- don't return multiple values
else
return identifier
end
end
-- If there's a provided list of allowed globals, don't let references thru that
-- aren't on the list. This list is set at the compiler entry points of compile
-- and compileStream.
local allowedGlobals
local function globalAllowed(name)
if not allowedGlobals then return true end
for _, g in ipairs(allowedGlobals) do
if g == name then return true end
end
end
-- Creates a symbol from a string by mangling it.
-- ensures that the generated symbol is unique
-- if the input string is unique in the scope.
local function localMangling(str, scope, ast, tempManglings)
local append = 0
local mangling = str
assertCompile(not isMultiSym(str), 'unexpected multi symbol ' .. str, ast)
-- Mapping mangling to a valid Lua identifier
if luaKeywords[mangling] or mangling:match('^%d') then
mangling = '_' .. mangling
end
mangling = mangling:gsub('-', '_')
mangling = mangling:gsub('[^%w_]', function (c)
return ('_%02x'):format(c:byte())
end)
-- Prevent name collisions with existing symbols
local raw = mangling
while scope.unmanglings[mangling] do
mangling = raw .. append
append = append + 1
end
scope.unmanglings[mangling] = str
local manglings = tempManglings or scope.manglings
manglings[str] = mangling
return mangling
end
-- Calling this function will mean that further
-- compilation in scope will use these new manglings
-- instead of the current manglings.
local function applyManglings(scope, newManglings, ast)
for raw, mangled in pairs(newManglings) do
assertCompile(not scope.refedglobals[mangled],
"use of global " .. raw .. " is aliased by a local", ast)
scope.manglings[raw] = mangled
end
end
-- Combine parts of a symbol
local function combineParts(parts, scope)
local ret = scope.manglings[parts[1]] or globalMangling(parts[1])
for i = 2, #parts do
if isValidLuaIdentifier(parts[i]) then
if parts.multiSymMethodCall and i == #parts then
ret = ret .. ':' .. parts[i]
else
ret = ret .. '.' .. parts[i]
end
else
ret = ret .. '[' .. serializeString(parts[i]) .. ']'
end
end
return ret
end
-- Generates a unique symbol in the scope.
local function gensym(scope, base)
local mangling
local append = 0
repeat
mangling = (base or '') .. '_' .. append .. '_'
append = append + 1
until not scope.unmanglings[mangling]
scope.unmanglings[mangling] = true
return mangling
end
-- Generates a unique symbol in the scope based on the base name. Calling
-- repeatedly with the same base and same scope will return existing symbol
-- rather than generating new one.
local function autogensym(base, scope)
if scope.autogensyms[base] then return scope.autogensyms[base] end
local mangling = gensym(scope, base)
scope.autogensyms[base] = mangling
return mangling
end
-- Check if a binding is valid
local function checkBindingValid(symbol, scope, ast)
-- Check if symbol will be over shadowed by special
local name = symbol[1]
assertCompile(not scope.specials[name] and not scope.macros[name],
("local %s was overshadowed by a special form or macro")
:format(name), ast)
assertCompile(not isQuoted(symbol),
("macro tried to bind %s without gensym"):format(name), symbol)
end
-- Declare a local symbol
local function declareLocal(symbol, meta, scope, ast, tempManglings)
checkBindingValid(symbol, scope, ast)
local name = symbol[1]
assertCompile(not isMultiSym(name),
"unexpected multi symbol " .. name, ast)
local mangling = localMangling(name, scope, ast, tempManglings)
scope.symmeta[name] = meta
return mangling
end
-- Convert symbol to Lua code. Will only work for local symbols
-- if they have already been declared via declareLocal
local function symbolToExpression(symbol, scope, isReference)
local name = symbol[1]
local multiSymParts = isMultiSym(name)
if scope.hashfn then
if name == '$' then name = '$1' end
if multiSymParts then
if multiSymParts[1] == "$" then
multiSymParts[1] = "$1"
name = table.concat(multiSymParts, ".")
end
end
end
local parts = multiSymParts or {name}
local etype = (#parts > 1) and "expression" or "sym"
local isLocal = scope.manglings[parts[1]]
if isLocal and scope.symmeta[parts[1]] then scope.symmeta[parts[1]].used = true end
-- if it's a reference and not a symbol which introduces a new binding
-- then we need to check for allowed globals
assertCompile(not isReference or isLocal or globalAllowed(parts[1]),
'unknown global in strict mode: ' .. parts[1], symbol)
if not isLocal then
rootScope.refedglobals[parts[1]] = true
end
return expr(combineParts(parts, scope), etype)
end
-- Emit Lua code
local function emit(chunk, out, ast)
if type(out) == 'table' then
table.insert(chunk, out)
else
table.insert(chunk, {leaf = out, ast = ast})
end
end
-- Do some peephole optimization.
local function peephole(chunk)
if chunk.leaf then return chunk end
-- Optimize do ... end in some cases.
if #chunk >= 3 and
chunk[#chunk - 2].leaf == 'do' and
not chunk[#chunk - 1].leaf and
chunk[#chunk].leaf == 'end' then
local kid = peephole(chunk[#chunk - 1])
local newChunk = {ast = chunk.ast}
for i = 1, #chunk - 3 do table.insert(newChunk, peephole(chunk[i])) end
for i = 1, #kid do table.insert(newChunk, kid[i]) end
return newChunk
end
-- Recurse
return map(chunk, peephole)
end
-- correlate line numbers in input with line numbers in output
local function flattenChunkCorrelated(mainChunk)
local function flatten(chunk, out, lastLine, file)
if chunk.leaf then
out[lastLine] = (out[lastLine] or "") .. " " .. chunk.leaf
else
for _, subchunk in ipairs(chunk) do
-- Ignore empty chunks
if subchunk.leaf or #subchunk > 0 then
-- don't increase line unless it's from the same file
if subchunk.ast and file == subchunk.ast.file then
lastLine = math.max(lastLine, subchunk.ast.line or 0)
end
lastLine = flatten(subchunk, out, lastLine, file)
end
end
end
return lastLine
end
local out = {}
local last = flatten(mainChunk, out, 1, mainChunk.file)
for i = 1, last do
if out[i] == nil then out[i] = "" end
end
return table.concat(out, "\n")
end
-- Flatten a tree of indented Lua source code lines.
-- Tab is what is used to indent a block.
local function flattenChunk(sm, chunk, tab, depth)
if type(tab) == 'boolean' then tab = tab and ' ' or '' end
if chunk.leaf then
local code = chunk.leaf
local info = chunk.ast
-- Just do line info for now to save memory
if sm then sm[#sm + 1] = info and info.line or -1 end
return code
else
local parts = map(chunk, function(c)
if c.leaf or #c > 0 then -- Ignore empty chunks
local sub = flattenChunk(sm, c, tab, depth + 1)
if depth > 0 then sub = tab .. sub:gsub('\n', '\n' .. tab) end
return sub
end
end)
return table.concat(parts, '\n')
end
end
-- Some global state for all fennel sourcemaps. For the time being,
-- this seems the easiest way to store the source maps.
-- Sourcemaps are stored with source being mapped as the key, prepended
-- with '@' if it is a filename (like debug.getinfo returns for source).
-- The value is an array of mappings for each line.
local fennelSourcemap = {}
-- TODO: loading, unloading, and saving sourcemaps?
local function makeShortSrc(source)
source = source:gsub('\n', ' ')
if #source <= 49 then
return '[fennel "' .. source .. '"]'
else
return '[fennel "' .. source:sub(1, 46) .. '..."]'
end
end
-- Return Lua source and source map table
local function flatten(chunk, options)
chunk = peephole(chunk)
if(options.correlate) then
return flattenChunkCorrelated(chunk), {}
else
local sm = {}
local ret = flattenChunk(sm, chunk, options.indent, 0)
if sm then
local key, short_src
if options.filename then
short_src = options.filename
key = '@' .. short_src
else
key = ret
short_src = makeShortSrc(options.source or ret)
end
sm.short_src = short_src
sm.key = key
fennelSourcemap[key] = sm
end
return ret, sm
end
end
-- module-wide state for metadata
-- create metadata table with weakly-referenced keys
local function makeMetadata()
return setmetatable({}, {
__mode = 'k',
__index = {
get = function(self, tgt, key)
if self[tgt] then return self[tgt][key] end
end,
set = function(self, tgt, key, value)
self[tgt] = self[tgt] or {}
self[tgt][key] = value
return tgt
end,
setall = function(self, tgt, ...)
local kvLen, kvs = select('#', ...), {...}
if kvLen % 2 ~= 0 then
error('metadata:setall() expected even number of k/v pairs')
end
self[tgt] = self[tgt] or {}
for i = 1, kvLen, 2 do self[tgt][kvs[i]] = kvs[i + 1] end
return tgt
end,
}})
end
local metadata = makeMetadata()
local doc = function(tgt, name)
if(not tgt) then return name .. " not found" end
local docstring = (metadata:get(tgt, 'fnl/docstring') or
'#<undocumented>'):gsub('\n$', ''):gsub('\n', '\n ')
if type(tgt) == "function" then
local arglist = table.concat(metadata:get(tgt, 'fnl/arglist') or
{'#<unknown-arguments>'}, ' ')
return string.format("(%s%s%s)\n %s", name, #arglist > 0 and ' ' or '',
arglist, docstring)
else
return string.format("%s\n %s", name, docstring)
end
end
local function docSpecial(name, arglist, docstring)
metadata[SPECIALS[name]] =