Skip to content

Commit

Permalink
Merge pull request hylang#2407 from Kodiologist/match-dot
Browse files Browse the repository at this point in the history
Fix `match` patterns with a dotted constructor
  • Loading branch information
Kodiologist authored Feb 5, 2023
2 parents b0647e9 + aeca832 commit fe17f9c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
2 changes: 0 additions & 2 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1255,9 +1255,7 @@ base names, such that ``hy.core.macros.foo`` can be called as just ``foo``.
1.0. Currently, trying to use ``pragma`` is an error.

.. hy:automodule:: hy.core.macros
:members:
:macros:
:tags:

Placeholder macros
~~~~~~~~~~~~~~~~~~
Expand Down
11 changes: 8 additions & 3 deletions hy/core/result_macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,8 @@ def compile_with_expression(compiler, expr, root, args, body):
| pexpr(keepsym("|"), many(_pattern))
| braces(many(LITERAL + _pattern), maybe(pvalue("unpack-mapping", SYM)))
| pexpr(
notsym(".", "|", "unpack-mapping", "unpack-iterable"),
pexpr(keepsym("."), oneplus(SYM))
| notsym(".", "|", "unpack-mapping", "unpack-iterable"),
many(parse_if(lambda x: not isinstance(x, Keyword), _pattern)),
many(KEYWORD + _pattern),
)
Expand Down Expand Up @@ -1219,11 +1220,15 @@ def compile_pattern(compiler, pattern):
)
)
elif isinstance(value, Expression):
root, args, kwargs = value
head, args, kwargs = value
keywords, values = zip(*kwargs) if kwargs else ([], [])
return asty.MatchClass(
value,
cls=compiler.scope.access(asty.Name(root, id=mangle(root), ctx=ast.Load())),
cls=compiler.compile(
# `head` could be a symbol or a dotted form.
(head[:1] + head[1]).replace(head)
if type(head) is Expression
else head).expr,
patterns=[compile_pattern(compiler, v) for v in args],
kwd_attrs=[kwd.name for kwd in keywords],
kwd_patterns=[compile_pattern(compiler, value) for value in values],
Expand Down
18 changes: 10 additions & 8 deletions tests/native_tests/match.hy
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
dataclasses [dataclass]
hy.errors [HySyntaxError])

(defclass [dataclass] Point []
(#^int x)
(#^int y))

(defn test-pattern-matching []
(assert (is (match 0
0 :if False False
Expand Down Expand Up @@ -78,10 +82,6 @@
0)
0))

(defclass [dataclass] Point []
(#^int x)
(#^int y))

(assert (= 0 (match (Point 1 0) (Point 1 :y var) var)))
(assert (is None (match (Point 0 0) (Point 1 :y var) var)))

Expand Down Expand Up @@ -130,6 +130,12 @@
(hy.eval '(match 1
1 :if True :as x x))))

(defn test-dotted-constructor []
; https://github.com/hylang/hy/issues/2404
(defclass C [Point]
(setv C Point))
(assert (= (match (Point 1 2) (C.C 1 2) "ok") "ok")))

(defn test-matching-side-effects []
(setv x 0)
(defn foo []
Expand Down Expand Up @@ -165,10 +171,6 @@
(assert (= [x y] [5 6])))
(assert (= [x y] [3 4])))

(defclass [dataclass] Point []
(#^int x)
(#^int y))

(defn test-let-match-pattern []
(setv [x y] [1 2]
p (Point 5 6))
Expand Down

0 comments on commit fe17f9c

Please sign in to comment.