~technomancy/fennel

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
1

[PATCH antifennel v2] Mangle early returns in more cases.

Details
Message ID
<20220108234543.183609-1-xordspar0@gmail.com>
DKIM signature
pass
Download raw message
Patch: +21 -2
Antifennel does not mangle variables in cases that it considers simple. This
previously included all identifiers. But some identifiers need to be mangled.
For example, antifennel will convert a Lua variable called someVar to some-var
in the Fennel output. An early return that refers to some-var will need to
re-mangle this variable to make it valid in Lua. See the new test case for a
full example.
---

This revision leaves out test/fennel.lua, which had autogenerated
changes unrelated to the patch.

 anticompiler.fnl  |  4 ++--
 test.lua          |  9 +++++++++
 test_expected.fnl | 10 ++++++++++
 3 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/anticompiler.fnl b/anticompiler.fnl
index 4153daf..084c7c9 100644
--- a/anticompiler.fnl
+++ b/anticompiler.fnl
@@ -1,4 +1,4 @@
(local {: list : sym : sym? : view : sequence} (require :fennel))
(local {: list : mangle : sym : sym? : view : sequence} (require :fennel))
(local unpack (or table.unpack _G.unpack))

(fn map [tbl f with-last?]
@@ -64,7 +64,7 @@
(fn any-complex-expressions? [args i]
  (let [a (. args i)]
    (if (= nil a) false
        (not (or (= a.kind "Identifier") (= a.kind "Literal"))) true
        (not (or (= a.kind "Literal") (and (= a.kind "Identifier") (= a.name (mangle a.name))))) true
        (any-complex-expressions? args (+ i 1)))))

(fn early-return-bindings [binding-names bindings i arg originals]
diff --git a/test.lua b/test.lua
index 5e13461..474c69b 100644
--- a/test.lua
+++ b/test.lua
@@ -66,4 +66,13 @@ for i, match in ipairs({}) do
   noprint(match)
end


local function earlyReturns(someVar)
   if true then return someVar end
   return nil
end

local earlyResult = earlyReturns("success")
assert(earlyResult == "success", earlyResult)

return (f123("path") or {}).mode
diff --git a/test_expected.fnl b/test_expected.fnl
index a6d6134..404aa03 100644
--- a/test_expected.fnl
+++ b/test_expected.fnl
@@ -69,5 +69,15 @@
(each [i ___match___ (ipairs {})]
  (noprint ___match___))

(fn early-returns [some-var]
  (when true
    (let [___antifnl_rtn_1___ some-var]
      (lua "return ___antifnl_rtn_1___")))
  nil)

(local early-result (early-returns :success))

(assert (= early-result :success) early-result)

(. (or (f123 :path) {}) :mode)

-- 
2.32.0
Details
Message ID
<87v8ysygim.fsf@mntre-builder>
In-Reply-To
<20220108234543.183609-1-xordspar0@gmail.com> (view parent)
DKIM signature
permerror
Download raw message
Jordan Christiansen <xordspar0@gmail.com> writes:

> Antifennel does not mangle variables in cases that it considers simple. This
> previously included all identifiers. But some identifiers need to be mangled.
> For example, antifennel will convert a Lua variable called someVar to some-var
> in the Fennel output. An early return that refers to some-var will need to
> re-mangle this variable to make it valid in Lua. See the new test case for a
> full example.

Thanks! Applied and pushed.

-Phil
Reply to thread Export thread (mbox)