35
35
36
36
function _model_macro (mod, name, expr, isconnector)
37
37
exprs = Expr (:block )
38
- dict = Dict {Symbol, Any} ()
39
- dict[:kwargs ] = Dict {Symbol, Any} ()
38
+ dict = Dict {Symbol, Any} (
39
+ :kwargs => Dict {Symbol, Dict} (),
40
+ )
40
41
comps = Symbol[]
41
42
ext = Ref {Any} (nothing )
42
43
eqs = Expr[]
@@ -107,7 +108,8 @@ function _model_macro(mod, name, expr, isconnector)
107
108
end
108
109
109
110
function parse_variable_def! (dict, mod, arg, varclass, kwargs;
110
- def = nothing , indices:: Union{Vector{UnitRange{Int}}, Nothing} = nothing )
111
+ def = nothing , indices:: Union{Vector{UnitRange{Int}}, Nothing} = nothing ,
112
+ type:: Union{Type, Nothing} = nothing )
111
113
metatypes = [(:connection_type , VariableConnectType),
112
114
(:description , VariableDescription),
113
115
(:unit , VariableUnit),
@@ -127,15 +129,34 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs;
127
129
arg isa LineNumberNode && return
128
130
MLStyle. @match arg begin
129
131
a:: Symbol => begin
130
- push! (kwargs, Expr (:kw , a, nothing ))
132
+ if type isa Nothing
133
+ push! (kwargs, Expr (:kw , a, nothing ))
134
+ else
135
+ push! (kwargs, Expr (:kw , Expr (:(:: ), a, Union{Nothing, type}), nothing ))
136
+ end
131
137
var = generate_var! (dict, a, varclass; indices)
132
- dict[:kwargs ][getname (var)] = def
138
+ dict[:kwargs ][getname (var)] = Dict ( :value => def, :type => type)
133
139
(var, def)
134
140
end
141
+ Expr (:(:: ), a, type) => begin
142
+ type = Core. eval (mod, type)
143
+ _type_check! (a, type)
144
+ parse_variable_def! (dict, mod, a, varclass, kwargs; def, type)
145
+ end
146
+ Expr (:(:: ), Expr (:call , a, b), type) => begin
147
+ type = Core. eval (mod, type)
148
+ def = _type_check! (def, a, type)
149
+ parse_variable_def! (dict, mod, a, varclass, kwargs; def, type)
150
+ end
135
151
Expr (:call , a, b) => begin
136
- push! (kwargs, Expr (:kw , a, nothing ))
152
+ if type isa Nothing
153
+ push! (kwargs, Expr (:kw , a, nothing ))
154
+ else
155
+ push! (kwargs, Expr (:kw , Expr (:(:: ), a, Union{Nothing, type}), nothing ))
156
+ end
137
157
var = generate_var! (dict, a, b, varclass; indices)
138
- dict[:kwargs ][getname (var)] = def
158
+ type != = nothing && (dict[varclass][getname (var)][:type ] = type)
159
+ dict[:kwargs ][getname (var)] = Dict (:value => def, :type => type)
139
160
(var, def)
140
161
end
141
162
Expr (:(= ), a, b) => begin
@@ -306,15 +327,22 @@ function parse_structural_parameters!(exprs, sps, dict, mod, body, kwargs)
306
327
Base. remove_linenums! (body)
307
328
for arg in body. args
308
329
MLStyle. @match arg begin
330
+ Expr (:(= ), Expr (:(:: ), a, type), b) => begin
331
+ type = Core. eval (mod, type)
332
+ b = _type_check! (Core. eval (mod, b), a, type)
333
+ push! (sps, a)
334
+ push! (kwargs, Expr (:kw , Expr (:(:: ), a, type), b))
335
+ dict[:kwargs ][a] = Dict (:value => b, :type => type)
336
+ end
309
337
Expr (:(= ), a, b) => begin
310
338
push! (sps, a)
311
339
push! (kwargs, Expr (:kw , a, b))
312
- dict[:kwargs ][a] = b
340
+ dict[:kwargs ][a] = Dict ( :value => b)
313
341
end
314
342
a => begin
315
343
push! (sps, a)
316
344
push! (kwargs, a)
317
- dict[:kwargs ][a] = nothing
345
+ dict[:kwargs ][a] = Dict ( :value => nothing )
318
346
end
319
347
end
320
348
end
@@ -338,17 +366,17 @@ function extend_args!(a, b, dict, expr, kwargs, varexpr, has_param = false)
338
366
end
339
367
end
340
368
push! (kwargs, Expr (:kw , x, nothing ))
341
- dict[:kwargs ][x] = nothing
369
+ dict[:kwargs ][x] = Dict ( :value => nothing )
342
370
end
343
371
Expr (:kw , x) => begin
344
372
push! (kwargs, Expr (:kw , x, nothing ))
345
- dict[:kwargs ][x] = nothing
373
+ dict[:kwargs ][x] = Dict ( :value => nothing )
346
374
end
347
375
Expr (:kw , x, y) => begin
348
376
b. args[i] = Expr (:kw , x, x)
349
377
push! (varexpr. args, :($ x = $ x === nothing ? $ y : $ x))
350
378
push! (kwargs, Expr (:kw , x, nothing ))
351
- dict[:kwargs ][x] = nothing
379
+ dict[:kwargs ][x] = Dict ( :value => nothing )
352
380
end
353
381
Expr (:parameters , x... ) => begin
354
382
has_param = true
@@ -853,3 +881,18 @@ function parse_conditional_model_statements(comps, dict, eqs, exprs, kwargs, mod
853
881
$ equations_blk
854
882
end ))
855
883
end
884
+
885
+ _type_check! (a, type) = return
886
+ function _type_check! (val, a, type)
887
+ if val isa type
888
+ return val
889
+ else
890
+ try
891
+ return convert (type, val)
892
+ catch
893
+ (e)
894
+ throw (TypeError (Symbol (" `@mtkmodel`" ),
895
+ " `@structural_parameters`, while assigning to `$a `" , type, typeof (val)))
896
+ end
897
+ end
898
+ end
0 commit comments