1
- macro connector (name:: Symbol , body)
2
- esc (connector_macro (__module__, name, body))
3
- end
4
-
5
1
struct Model{F, S}
6
2
f:: F
7
3
structure:: S
8
4
end
9
5
(m:: Model )(args... ; kw... ) = m. f (args... ; kw... )
10
6
11
- function connector_macro (mod, name, body)
7
+ for f in (:connector , :model )
8
+ @eval begin
9
+ macro $f (name:: Symbol , body)
10
+ esc ($ (Symbol (f, :_macro ))(__module__, name, body))
11
+ end
12
+
13
+ macro $f (fcall:: Expr , body)
14
+ fcall. head == :call || " Couldn't comprehend the $f $arg "
15
+
16
+ arglist, kwargs = if lastindex (fcall. args) > 1 && is_kwarg (fcall. args[2 ])
17
+ (lastindex (fcall. args) > 2 ? Set (fcall. args[3 : end ]) : Set ()),
18
+ Set (fcall. args[2 ]. args)
19
+ else
20
+ Set (), Set (fcall. args[2 : end ])
21
+ end
22
+ esc ($ (Symbol (f, :_macro ))(__module__, fcall. args[1 ], body; arglist, kwargs))
23
+ end
24
+ end
25
+ end
26
+
27
+ @inline is_kwarg (:: Symbol ) = false
28
+ @inline is_kwarg (e:: Expr ) = (e. head == :parameters )
29
+
30
+ function connector_macro (mod, name, body; arglist = Set ([]), kwargs = Set ([]))
12
31
if ! Meta. isexpr (body, :block )
13
32
err = """
14
33
connector body must be a block! It should be in the form of
@@ -21,16 +40,17 @@ function connector_macro(mod, name, body)
21
40
"""
22
41
error (err)
23
42
end
24
- vs = Num []
43
+ vs = []
25
44
icon = Ref {Union{String, URI}} ()
26
45
dict = Dict {Symbol, Any} ()
46
+ expr = Expr (:block )
27
47
for arg in body. args
28
48
arg isa LineNumberNode && continue
29
49
if arg. head == :macrocall && arg. args[1 ] == Symbol (" @icon" )
30
50
parse_icon! (icon, dict, dict, arg. args[end ])
31
51
continue
32
52
end
33
- push! ( vs, Num ( parse_variable_def! ( dict, mod, arg, :variables )) )
53
+ parse_variable_arg! (expr, vs, dict, mod, arg, :variables )
34
54
end
35
55
iv = get (dict, :independent_variable , nothing )
36
56
if iv === nothing
@@ -39,8 +59,9 @@ function connector_macro(mod, name, body)
39
59
gui_metadata = isassigned (icon) ? GUIMetadata (GlobalRef (mod, name), icon[]) :
40
60
nothing
41
61
quote
42
- $ name = $ Model ((; name) -> begin
43
- var"#___sys___" = $ ODESystem ($ (Equation[]), $ iv, $ vs, $ ([]);
62
+ $ name = $ Model (($ (arglist... ); name, $ (kwargs... )) -> begin
63
+ $ expr
64
+ var"#___sys___" = $ ODESystem ($ (Equation[]), $ iv, [$ (vs... )], $ ([]);
44
65
name, gui_metadata = $ gui_metadata)
45
66
$ Setfield. @set! (var"#___sys___" . connector_type= $ connector_type (var"#___sys___" ))
46
67
end , $ dict)
@@ -147,25 +168,6 @@ function get_var(mod::Module, b)
147
168
b isa Symbol ? getproperty (mod, b) : b
148
169
end
149
170
150
- macro model (name:: Symbol , expr)
151
- esc (model_macro (__module__, name, expr))
152
- end
153
-
154
- @inline is_kwarg (:: Symbol ) = false
155
- @inline is_kwarg (e:: Expr ) = (e. head == :parameters )
156
-
157
- macro model (fcall:: Expr , expr)
158
- fcall. head == :call || " Couldn't comprehend the model $arg "
159
-
160
- arglist, kwargs = if lastindex (fcall. args) > 1 && is_kwarg (fcall. args[2 ])
161
- (lastindex (fcall. args) > 2 ? Set (fcall. args[3 : end ]) : Set ()),
162
- Set (fcall. args[2 ]. args)
163
- else
164
- Set (), Set (fcall. args[2 : end ])
165
- end
166
- esc (model_macro (__module__, fcall. args[1 ], expr; arglist, kwargs))
167
- end
168
-
169
171
function model_macro (mod, name, expr; arglist = Set ([]), kwargs = Set ([]))
170
172
exprs = Expr (:block )
171
173
dict = Dict {Symbol, Any} ()
@@ -324,7 +326,8 @@ function parse_variable_arg!(expr, vs, dict, mod, arg, varclass)
324
326
v = Num (vv)
325
327
name = getname (v)
326
328
push! (vs, name)
327
- def === nothing ? push! (expr. args, :($ name = $ v)) : push! (expr. args, :($ name = $ setdefault ($ v, $ def)))
329
+ def === nothing ? push! (expr. args, :($ name = $ v)) :
330
+ push! (expr. args, :($ name = $ setdefault ($ v, $ def)))
328
331
end
329
332
330
333
function parse_variables! (exprs, vs, dict, mod, body, varclass)
0 commit comments