Skip to content

Commit 219b9f4

Browse files
committed
Initial support for infix ~ (#173).
1 parent 61f6e65 commit 219b9f4

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/core/compiler.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ macro model(fexpr)
168168

169169

170170
dprintln(1, fexpr)
171+
fexpr = translate(fexpr)
171172

172173
fname = fexpr.args[1].args[1] # Get model name f
173174
fargs = fexpr.args[1].args[2:end] # Get model parameters (x,y;z=..)
@@ -326,3 +327,15 @@ getvsym(expr::Expr) = begin
326327
end
327328
curr
328329
end
330+
331+
332+
translate!(ex::Any) = ex
333+
translate!(ex::Expr) = begin
334+
if (ex.head === :call && ex.args[1] === :(~))
335+
ex.head = :macrocall; ex.args[1] = Symbol("@~")
336+
else
337+
map(translate!, ex.args)
338+
end
339+
ex
340+
end
341+
translate(ex::Expr) = translate!(deepcopy(ex))

test/compiler.jl/tilde.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Turing
2+
import Turing.translate!
3+
4+
ex = quote
5+
x = 1
6+
y = rand()
7+
y ~ Normal(0,1)
8+
end
9+
10+
res = translate!(:(y~Normal(1,1)))
11+
12+
Base.@assert res.head == :macrocall
13+
Base.@assert res.args == [Symbol("@~"), :y, :(Normal(1, 1))]
14+
15+
res2 = translate!(ex)
16+
17+
Base.@assert res2.args[end].head == :macrocall
18+
Base.@assert res2.args[end].args == [Symbol("@~"), :y, :(Normal(1, 1))]

0 commit comments

Comments
 (0)