~technomancy/fennel

[PATCH] Make hashfn arguments work when beginning multisyms

Details
Message ID
<20190902003138.5210-1-benaiah@mischenko.com>
DKIM signature
pass
Download raw message
Patch: +25 -5
Converts $ arguments to $1 when they begin a multisym (matches the
behavior of a lone $).

Sets used-local metadata correctly for symbols which begin
multisyms. This was previously incorrect for all symbols which started
multisyms, but the hashfn argument bug fixed in this commit is the
only known effect.

Added test cases for multisyms and immediately-returned arguments to
ensure neither regress.

Enables a getter function shorthand: #$symbol.property
---
This version of the patch adds a changelog entry and expands the
description of hashfns in reference.md

 changelog.md |  1 +
 fennel.lua   | 15 ++++++++++++---
 reference.md |  9 +++++++--
 test.lua     |  5 +++++
 4 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/changelog.md b/changelog.md
index e34b03e..945b2d8 100644
--- a/changelog.md
+++ b/changelog.md
@@ -19,6 +19,7 @@
 * **Disallow** `~` in symbols
 * **Remove** `~=` special; use `not=` instead
 * Add `hashfn` and `#` reader macro for shorthand functions like `#(+ $1 $2)`
+* Allow hashfn arguments to be used in multisyms
 * Add `macro` to make defining a single macro easier
 * Add `(comment)` special which emits a Lua comment in the generated source
 
diff --git a/fennel.lua b/fennel.lua
index f68a38e..3f1a5d0 100644
--- a/fennel.lua
+++ b/fennel.lua
@@ -638,11 +638,20 @@ end
 -- if they have already been declared via declareLocal
 local function symbolToExpression(symbol, scope, isReference)
     local name = symbol[1]
-    if scope.hashfn and name == '$' then name = '$1' end
-    local parts = isMultiSym(name) or {name}
+    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[name] then scope.symmeta[name].used = true end
+    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]),
diff --git a/reference.md b/reference.md
index b9897cf..a3560b1 100644
--- a/reference.md
+++ b/reference.md
@@ -55,8 +55,9 @@ This style of anonymous function is useful as a parameter to
 higher order functions, such as those provided by Lua libraries
 like lume and luafun.
 
-The current implementation only allows for functions of up to
-9 arguments, each named `$1` through `$9`.
+The current implementation only allows for functions of up to 9
+arguments, each named `$1` through `$9`. A lone `$` in a hash function
+is treated as an alias for `$1`.
 
 Hash functions are defined with the `hashfn` macro, which wraps
 it's single argument in a function literal. For example, `#$3`
@@ -64,6 +65,10 @@ is a function that returns it's third argument. `#[$1 $2 $3]` is
 a function that returns a table from the first 3 arguments. And
 so on.
 
+Hash arguments can also be used as parts of multisyms. For instance,
+`#$.foo` is a function which will return the value of the "foo" key in
+its first argument.
+
 ### `partial` partial application
 
 Returns a new function which works like its first argument, but fills
diff --git a/test.lua b/test.lua
index 2c627c3..78d96b1 100644
--- a/test.lua
+++ b/test.lua
@@ -295,6 +295,8 @@ local cases = {
         ["(#(+ $3 $4) 1 1 3 4)"]=7,
         -- One argument
         ["(#(+ $1 45) 1)"]=46,
+        -- Immediately returned argument
+        ["(+ (#$ 1) (#$2 2 3))"]=4,
         -- With let
         ["(let [f #(+ $1 45)] (f 1))"]=46,
         -- Complex body
@@ -303,6 +305,9 @@ local cases = {
         ["(#(+ $ 2) 3)"]=5,
         -- Mixed $ types
         ["(let [f #(+ $ $1 $2)] (f 1 2))"]=4,
+        -- Multisyms containing $ arguments
+        ["(#$.foo {:foo :bar})"]="bar",
+        ["(#$2.foo.bar.baz nil {:foo {:bar {:baz :quux}}})"]="quux",
     },
     match = {
         -- basic literal
-- 
2.23.0.rc1