Skip to content

Commit 5251051

Browse files
committed
added functionality to return matches dictionary, writing just ~ in th rhs
1 parent 7e4daa2 commit 5251051

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/rule.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ function makeconsequent(expr)
163163
else
164164
return Expr(expr.head, map(makeconsequent, expr.args)...)
165165
end
166+
elseif expr===:(~)
167+
return :(__MATCHES__)
166168
else
167169
# treat as a literal
168170
return esc(expr)

test/rewrite.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,22 @@ end
8282
@test r_mix((a + b)) === 2 #1+1
8383
end
8484

85+
@testset "Return the matches dictionary" begin
86+
r = @rule (~x + (~y)^2) => ~
87+
res = r(a + b^2)
88+
@test isa(res, Base.ImmutableDict)
89+
@test res[:x] === a
90+
@test res[:y] === b
91+
92+
r2 = @rule (~x + (~y)^(~m)) => (~) where ~m===2
93+
@test isa(r2(a + b^2), Base.ImmutableDict)
94+
@test r2(a + b^3)===nothing
95+
96+
r3 = @rule (~x + (~y)^(~m)) => ~m===2 ? (~) : ~x
97+
@test isa(r3(a + b^2), Base.ImmutableDict)
98+
@test r3(a + b^3)===a
99+
end
100+
85101
using SymbolicUtils: @capture
86102

87103
@testset "Capture form" begin

0 commit comments

Comments
 (0)