@@ -129,8 +129,9 @@ function _model_macro(mod, fullname::Union{Expr, Symbol}, expr, isconnector)
129
129
Ref (dict), [:constants , :defaults , :kwargs , :structural_parameters ])
130
130
131
131
sys = :($ type ($ (flatten_equations)(equations), $ iv, variables, parameters;
132
- name, description = $ description, systems, gui_metadata = $ gui_metadata, defaults,
133
- costs = [$ (costs... )], constraints = [$ (cons... )], consolidate = $ consolidate))
132
+ name, description = $ description, systems, gui_metadata = $ gui_metadata,
133
+ continuous_events = [$ (c_evts... )], discrete_events = [$ (d_evts... )],
134
+ defaults, costs = [$ (costs... )], constraints = [$ (cons... )], consolidate = $ consolidate))
134
135
135
136
if length (ext) == 0
136
137
push! (exprs. args, :(var"#___sys___" = $ sys))
@@ -141,16 +142,6 @@ function _model_macro(mod, fullname::Union{Expr, Symbol}, expr, isconnector)
141
142
isconnector && push! (exprs. args,
142
143
:($ Setfield. @set! (var"#___sys___" . connector_type= $ connector_type (var"#___sys___" ))))
143
144
144
- ! isempty (c_evts) && push! (exprs. args,
145
- :($ Setfield. @set! (var"#___sys___" . continuous_events= $ SymbolicContinuousCallback .([
146
- $ (c_evts... )
147
- ]))))
148
-
149
- ! isempty (d_evts) && push! (exprs. args,
150
- :($ Setfield. @set! (var"#___sys___" . discrete_events= $ SymbolicDiscreteCallback .([
151
- $ (d_evts... )
152
- ]))))
153
-
154
145
f = if length (where_types) == 0
155
146
:($ (Symbol (:__ , name, :__ ))(; name, $ (kwargs... )) = $ exprs)
156
147
else
@@ -1144,21 +1135,47 @@ end
1144
1135
function parse_continuous_events! (c_evts, dict, body)
1145
1136
dict[:continuous_events ] = []
1146
1137
Base. remove_linenums! (body)
1147
- for arg in body. args
1148
- push! (c_evts, arg)
1138
+ for line in body. args
1139
+ if length (line. args) == 3 && line. args[1 ] == :(=> )
1140
+ push! (c_evts, :(($ line, ())))
1141
+ elseif length (line. args) == 2
1142
+ event = line. args[1 ]
1143
+ kwargs = parse_event_kwargs (line. args[2 ])
1144
+ push! (c_evts, :(($ event, $ kwargs)))
1145
+ else
1146
+ error (" Malformed continuous event $line ." )
1147
+ end
1149
1148
push! (dict[:continuous_events ], readable_code .(c_evts)... )
1150
1149
end
1151
1150
end
1152
1151
1153
1152
function parse_discrete_events! (d_evts, dict, body)
1154
1153
dict[:discrete_events ] = []
1155
1154
Base. remove_linenums! (body)
1156
- for arg in body. args
1157
- push! (d_evts, arg)
1155
+ for line in body. args
1156
+ if length (line. args) == 3 && line. args[1 ] == :(=> )
1157
+ push! (d_evts, :(($ line, ())))
1158
+ elseif length (line. args) == 2
1159
+ event = line. args[1 ]
1160
+ kwargs = parse_event_kwargs (line. args[2 ])
1161
+ push! (d_evts, :(($ event, $ kwargs)))
1162
+ else
1163
+ error (" Malformed discrete event $line ." )
1164
+ end
1158
1165
push! (dict[:discrete_events ], readable_code .(d_evts)... )
1159
1166
end
1160
1167
end
1161
1168
1169
+ function parse_event_kwargs (disc_expr)
1170
+ kwargs = :([])
1171
+ for arg in disc_expr. args
1172
+ (arg. head != :(= )) && error (" Malformed event kwarg $arg ." )
1173
+ (arg. args[1 ] isa Symbol) || error (" Invalid keyword argument name $(arg. args[1 ]) ." )
1174
+ push! (kwargs. args, :($ (QuoteNode (arg. args[1 ])) => $ (arg. args[2 ])))
1175
+ end
1176
+ kwargs
1177
+ end
1178
+
1162
1179
function parse_constraints! (cons, dict, body)
1163
1180
dict[:constraints ] = []
1164
1181
Base. remove_linenums! (body)
0 commit comments