Skip to content

Commit 14d3122

Browse files
frenchy64technomancy
authored andcommitted
Reintroduce gensyms to unique renaming machinery
This fixes https://todo.sr.ht/~technomancy/fennel/54 in a different way than 6db6a2c that does not introduce this bug: #355 Approach: decomplect registering a demangling from extracting a gensym's base. This way, the first time a gensym is declared, it is not further mangled. Note that neither commit completely addresses #355 e.g., consider (let [a 1 a 2] (lua "a")) but this commit attempts to do an equivalent job to 6db6a2c.
1 parent 907728a commit 14d3122

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

src/fennel/compiler.fnl

+5-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ implement nesting. "
2222
:manglings (setmetatable [] {:__index (and parent parent.manglings)})
2323
:specials (setmetatable [] {:__index (and parent parent.specials)})
2424
:symmeta (setmetatable [] {:__index (and parent parent.symmeta)})
25+
:gensym-base (setmetatable [] {:__index (and parent parent.gensym-base)})
2526
:unmanglings (setmetatable [] {:__index (and parent parent.unmanglings)})
2627
:gensyms (setmetatable [] {:__index (and parent parent.gensyms)})
2728
:autogensyms (setmetatable [] {:__index (and parent parent.autogensyms)})
@@ -103,7 +104,7 @@ and compile-stream."
103104
(or (not allowed-globals) (utils.member? name allowed-globals)))
104105

105106
(fn unique-mangling [original mangling scope append]
106-
(if (and (. scope.unmanglings mangling) (not (. scope.gensyms mangling)))
107+
(if (. scope.unmanglings mangling)
107108
(unique-mangling original (.. original append) scope (+ append 1))
108109
mangling))
109110

@@ -120,7 +121,7 @@ symbol is unique if the input string is unique in the scope."
120121
(string.gsub "-" "_")
121122
(string.gsub "[^%w_]" #(string.format "_%02x" ($:byte))))
122123
unique (unique-mangling mangling mangling scope 0)]
123-
(tset scope.unmanglings unique str)
124+
(tset scope.unmanglings unique (or (. scope.gensym-base str) str))
124125
(let [manglings (or ?temp-manglings scope.manglings)]
125126
(tset manglings str unique))
126127
unique))
@@ -153,7 +154,8 @@ these new manglings instead of the current manglings."
153154
(var mangling (.. (or ?base "") (next-append) (or ?suffix "")))
154155
(while (. scope.unmanglings mangling)
155156
(set mangling (.. (or ?base "") (next-append) (or ?suffix ""))))
156-
(tset scope.unmanglings mangling (or ?base true))
157+
(when (and ?base (< 0 (length ?base)))
158+
(tset scope.gensym-base mangling ?base))
157159
(tset scope.gensyms mangling true)
158160
mangling)
159161

test/macro.fnl

+2-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@
7777
(== (do (import-macros {: unsandboxed} :test.macros) (unsandboxed))
7878
"[\"no\" \"sandbox\"]" {:compiler-env _G})
7979
(let [not-unqualified "(import-macros hi :test.macros) (print (inc 1))"]
80-
(l.assertFalse (pcall fennel.eval not-unqualified))))
80+
(l.assertFalse (pcall fennel.eval not-unqualified)))
81+
(== 2 (do (import-macros {: gensym-shadow} :test.macros) (gensym-shadow))))
8182

8283
(fn test-macro-path []
8384
(== (do (import-macros m :test.other-macros) (m.m)) "testing macro path"))

test/macros.fnl

+5-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@
2121
y# {:one 1}]
2222
(+ (x#:abc) y#.one)))
2323
:unsandboxed (fn [] (view [:no :sandbox]))
24-
:fail-one (fn [x] (when (= x 1) (abc)) true)}
24+
:fail-one (fn [x] (when (= x 1) (abc)) true)
25+
:gensym-shadow (fn []
26+
(let [g (gensym)]
27+
`(let [,g (let [,g 1] 2)]
28+
,g)))}

0 commit comments

Comments
 (0)