Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

defmacro! no longer crashes when modifying arguments #115

Merged
merged 1 commit into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Bug Fixes
* A lot of incompatibilities of `meth`, `ameth`, `defn+`, and `fn+`
with the core `defn` and `fn` have been fixed. You can now use `:async`,
docstrings are recognized properly, etc.
* `defmacro!` no longer crashes when mutating macro arguments.

0.8.0 (released 2025-01-08)
======================================================
Expand Down
33 changes: 25 additions & 8 deletions hyrule/macrotools.hy
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(import
hy.compiler [HyASTCompiler calling-module]
hy.model-patterns [sym pexpr]
hyrule.iterables [coll? flatten])


Expand Down Expand Up @@ -124,20 +125,21 @@
#** (if p-rest {p-rest (tuple collected-rest)} {})
#** (if p-kwargs {p-kwargs collected-kwargs} {})))

(defn _pvalue [root wanted]
(>> (pexpr (+ (sym root) wanted)) (fn [x] (get x 0))))

(defn parse-fn-params [params]
"A subroutine for `defmacro-kwargs` and `match-params`."
(import
funcparserlib.parser [maybe many]
hy.model-patterns [SYM FORM sym brackets pexpr whole])

(setv msym (>> SYM hy.mangle))
(defn pvalue [root wanted]
(>> (pexpr (+ (sym root) wanted)) (fn [x] (get x 0))))
(setv [ps p-rest p-kwargs] (.parse
(whole [
(many (| msym (brackets msym FORM)))
(maybe (pvalue "unpack-iterable" msym))
(maybe (pvalue "unpack-mapping" msym))])
(maybe (_pvalue "unpack-iterable" msym))
(maybe (_pvalue "unpack-mapping" msym))])
params))
(setv ps (dfor
p ps
Expand Down Expand Up @@ -171,12 +173,27 @@
(setv l (list "abc"))
(m (.pop l)) ; => "ccc"]]

(setv os (lfor x (flatten args)
:if (and (isinstance x hy.models.Symbol)
(.startswith x "o!"))
(import
funcparserlib.parser [maybe many]
hy.model-patterns [SYM FORM brackets whole])

(setv [ps p-rest] (.parse
(whole [
(many (| SYM (brackets SYM FORM)))
(maybe (_pvalue "unpack-iterable" SYM))])
args))
(setv ps (lfor
p [#* ps #* (if p-rest [p-rest] [])]
(if (isinstance p hy.models.List) (get p 0) p)))

(setv os (lfor x ps
:if (.startswith x "o!")
x)
gs (lfor s os
(hy.models.Symbol (+ "g!" (cut s 2 None))))
nl (lfor x ps
:if (not (.startswith x "o!"))
x)
gensyms (gfor sym (sfor x (flatten [gs body])
:if (and (isinstance x hy.models.Symbol)
(.startswith x "g!"))
Expand All @@ -193,7 +210,7 @@
`(defmacro ~name ~args
~docstring
(setv ~@gensyms
~res ((fn [] ~@body)))
~res ((fn [] (nonlocal ~@nl) ~@body)))
`(do
(setv ~~gs ~~os)
~~res)))
Expand Down
21 changes: 21 additions & 0 deletions tests/test_macrotools.hy
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,27 @@
(assert (= (m (f)) 6)))


(defn test-defmacro!-modify-args []
;; https://github.com/hylang/hyrule/issues/112
;; test that modifying args compiles
(defmacro! foo [x]
(+= x 1)
x)
(assert (= (foo 1) 2))
;; test optional/default args
(defmacro! bar [x [y 0]]
(+= y 1)
y)
(assert (= (bar 2) 1))
(assert (= (bar 2 3) 4))
;; test unpack-iterable args
(defmacro! baz [o!x #* ys]
(+= ys #(3))
`(+ ~g!x ~g!x ~(. ys [-1])))
(setv z 9)
(assert (= (baz (do (+= z 1) z) bing bang bong)) 23))


(defmacro foo-walk []
42)

Expand Down