You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/compiler.jl
+8-117Lines changed: 8 additions & 117 deletions
Original file line number
Diff line number
Diff line change
@@ -33,116 +33,7 @@ function _error_msg()
33
33
return"This macro is only for use in the `@model` macro and not for external use."
34
34
end
35
35
36
-
"""
37
-
@varname(var)
38
-
39
-
A macro that returns an instance of `VarName` given the symbol or expression of a Julia variable, e.g. `@varname x[1,2][1+5][45][3]` returns `VarName{:x}("[1,2][6][45][3]")`.
40
-
"""
41
-
macrovarname(expr::Union{Expr, Symbol})
42
-
expr |> varname |> esc
43
-
end
44
-
functionvarname(expr)
45
-
ex =deepcopy(expr)
46
-
(ex isa Symbol) &&returnquote
47
-
DynamicPPL.VarName{$(QuoteNode(ex))}("")
48
-
end
49
-
(ex.head ==:ref) ||throw("VarName: Mis-formed variable name $(expr)!")
throw("VarName: Mis-formed variable name $(expr)!")
104
-
end
105
-
106
-
"""
107
-
split_var_str(var_str, inds_as = Vector)
108
-
109
-
This function splits a variable string, e.g. `"x[1:3,1:2][3,2]"` to the variable's symbol `"x"` and the indexing `"[1:3,1:2][3,2]"`. If `inds_as = String`, the indices are returned as a string, e.g. `"[1:3,1:2][3,2]"`. If `inds_as = Vector`, the indices are returned as a vector of vectors of strings, e.g. `[["1:3", "1:2"], ["3", "2"]]`.
110
-
"""
111
-
functionsplit_var_str(var_str, inds_as = Vector)
112
-
ind =findfirst(c -> c =='[', var_str)
113
-
if inds_as === String
114
-
if ind ===nothing
115
-
return var_str, ""
116
-
else
117
-
return var_str[1:ind-1], var_str[ind:end]
118
-
end
119
-
end
120
-
@assert inds_as === Vector
121
-
inds = Vector{String}[]
122
-
if ind ===nothing
123
-
return var_str, inds
124
-
end
125
-
sym = var_str[1:ind-1]
126
-
ind =length(sym)
127
-
while ind <length(var_str)
128
-
ind +=1
129
-
@assert var_str[ind] =='['
130
-
push!(inds, String[])
131
-
while var_str[ind] !=']'
132
-
ind +=1
133
-
if var_str[ind] =='['
134
-
ind2 =findnext(c -> c ==']', var_str, ind)
135
-
push!(inds[end], strip(var_str[ind:ind2]))
136
-
ind = ind2+1
137
-
else
138
-
ind2 =findnext(c -> c ==','|| c ==']', var_str, ind)
139
-
push!(inds[end], strip(var_str[ind:ind2-1]))
140
-
ind = ind2
141
-
end
142
-
end
143
-
end
144
-
return sym, inds
145
-
end
146
37
147
38
# Check if the right-hand side is a distribution.
148
39
functionassert_dist(dist; msg)
@@ -404,21 +295,21 @@ function replace_tilde!(model_info)
404
295
ex = model_info[:main_body]
405
296
ex = MacroTools.postwalk(ex) do x
406
297
if @capture(x, @M_ L_ ~ R_) && M == Symbol("@__dot__")
407
-
dot_tilde(L, R, model_info)
298
+
generate_dot_tilde(L, R, model_info)
408
299
else
409
300
x
410
301
end
411
302
end
412
303
$(VERSION>=v"1.1"?"ex = MacroTools.postwalk(ex) do x
413
304
if @capture(x, L_ .~ R_)
414
-
dot_tilde(L, R, model_info)
305
+
generate_dot_tilde(L, R, model_info)
415
306
else
416
307
x
417
308
end
418
309
end":"")
419
310
ex = MacroTools.postwalk(ex) do x
420
311
if @capture(x, L_ ~ R_)
421
-
tilde(L, R, model_info)
312
+
generate_tilde(L, R, model_info)
422
313
else
423
314
x
424
315
end
@@ -429,12 +320,12 @@ end
429
320
"""|> Meta.parse |> eval
430
321
431
322
"""
432
-
tilde(left, right, model_info)
323
+
generate_tilde(left, right, model_info)
433
324
434
325
The `tilde` function generates `observe` expression for data variables and `assume`
435
326
expressions for parameter variables, updating `model_info` in the process.
436
327
"""
437
-
functiontilde(left, right, model_info)
328
+
functiongenerate_tilde(left, right, model_info)
438
329
arg_syms =Val((model_info[:arg_syms]...,))
439
330
model = model_info[:main_body_names][:model]
440
331
vi = model_info[:main_body_names][:vi]
@@ -478,11 +369,11 @@ function tilde(left, right, model_info)
478
369
end
479
370
480
371
"""
481
-
dot_tilde(left, right, model_info)
372
+
generate_dot_tilde(left, right, model_info)
482
373
483
374
This function returns the expression that replaces `left .~ right` in the model body. If `preprocessed isa VarName`, then a `dot_assume` block will be run. Otherwise, a `dot_observe` block will be run.
0 commit comments