Skip to content

Commit 6ab41e7

Browse files
committed
feat: macro now sets the defaults of parameters and variables correctly to the passed kw
- RN, codition when all passed vars and pars have a kw default works. - The `parse_variables_with_kw!` and `for_keyword_queue` will be modified suitably to ensure the existing methods will be used for rest of conditions
1 parent 21f49ec commit 6ab41e7

File tree

1 file changed

+40
-16
lines changed

1 file changed

+40
-16
lines changed

src/systems/model_parsing.jl

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,37 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs)
7272
end
7373
end
7474

75+
function for_keyword_queue()
76+
# These args contain potential keywords
77+
# Handle it along with vars without defaults
78+
end
79+
80+
# Takes in args and populates kw and var definition exprs.
81+
# This should be modified to handle the other cases (i.e they should use existing
82+
# methods)
83+
function parse_variables_with_kw!(exprs, dict, mod, body, varclass, kwargs)
84+
expr = if varclass == :parameters
85+
:(ps = @parameters begin
86+
end)
87+
elseif varclass == :variables
88+
:(vs = @variables begin
89+
end)
90+
end
91+
92+
for arg in body.args
93+
arg isa LineNumberNode && continue
94+
MLStyle.@match arg begin
95+
96+
Expr(:(=), a, b) => begin
97+
def = Base.remove_linenums!(b).args[end]
98+
push!(expr.args[end].args[end].args, :($a = $def))
99+
push!(kwargs, def)
100+
end
101+
end
102+
end
103+
push!(exprs, expr)
104+
end
105+
75106
function generate_var(a, varclass)
76107
var = Symbolics.variable(a)
77108
if varclass == :parameters
@@ -104,15 +135,10 @@ function generate_var!(dict, a, b, varclass)
104135
var
105136
end
106137

107-
function set_kwargs!(kwargs, a)
108-
push!(kwargs, a)
109-
return a
110-
end
111-
112138
function parse_default(mod, a, kwargs)
113139
a = Base.remove_linenums!(deepcopy(a))
114140
MLStyle.@match a begin
115-
Expr(:block, a) => set_kwargs!(kwargs, a)
141+
Expr(:block, a) => get_var(mod, a)
116142
::Symbol => get_var(mod, a)
117143
::Number => a
118144
_ => error("Cannot parse default $a")
@@ -132,7 +158,7 @@ function set_var_metadata(a, ms)
132158
a
133159
end
134160
function get_var(mod::Module, b)
135-
b isa Symbol ? getproperty(mod, b) : b
161+
b isa Symbol ? getproperty(mod, b) : for_keyword_queue()
136162
end
137163

138164
macro model(name::Symbol, expr)
@@ -144,44 +170,42 @@ function model_macro(mod, name, expr)
144170
dict = Dict{Symbol, Any}()
145171
comps = Symbol[]
146172
ext = Ref{Any}(nothing)
147-
vs = Symbol[]
148-
ps = Symbol[]
149173
eqs = Expr[]
150174
icon = Ref{Union{String, URI}}()
151175
kwargs = []
152176
for arg in expr.args
153177
arg isa LineNumberNode && continue
154178
arg.head == :macrocall || error("$arg is not valid syntax. Expected a macro call.")
155-
parse_model!(exprs.args, comps, ext, eqs, vs, ps, icon, dict, mod, arg, kwargs)
179+
parse_model!(exprs.args, comps, ext, eqs, icon, dict, mod, arg, kwargs)
156180
end
157181
iv = get(dict, :independent_variable, nothing)
158182
if iv === nothing
159183
iv = dict[:independent_variable] = variable(:t)
160184
end
161185
gui_metadata = isassigned(icon) > 0 ? GUIMetadata(GlobalRef(mod, name), icon[]) :
162186
nothing
163-
sys = :($ODESystem($Equation[$(eqs...)], $iv, [$(vs...)], [$(ps...)];
187+
sys = :($ODESystem($Equation[$(eqs...)], $iv, vs, ps;
164188
systems = [$(comps...)], name, gui_metadata = $gui_metadata))
165189
if ext[] === nothing
166190
push!(exprs.args, sys)
167191
else
168192
push!(exprs.args, :($extend($sys, $(ext[]))))
169193
end
170-
@info "Exprs: $exprs"
171-
:($name = $Model((; name, kwargs...) -> $exprs, $dict))
194+
195+
:($name = $Model((; name, $(kwargs...)) -> $exprs, $dict))
172196
end
173197

174-
function parse_model!(exprs, comps, ext, eqs, vs, ps, icon, dict, mod, arg, kwargs)
198+
function parse_model!(exprs, comps, ext, eqs, icon, dict, mod, arg, kwargs)
175199
mname = arg.args[1]
176200
body = arg.args[end]
177201
if mname == Symbol("@components")
178202
parse_components!(exprs, comps, dict, body)
179203
elseif mname == Symbol("@extend")
180204
parse_extend!(exprs, ext, dict, body)
181205
elseif mname == Symbol("@variables")
182-
parse_variables!(exprs, vs, dict, mod, body, :variables, kwargs)
206+
parse_variables_with_kw!(exprs, dict, mod, body, :variables, kwargs)
183207
elseif mname == Symbol("@parameters")
184-
parse_variables!(exprs, ps, dict, mod, body, :parameters, kwargs)
208+
parse_variables_with_kw!(exprs, dict, mod, body, :parameters, kwargs)
185209
elseif mname == Symbol("@equations")
186210
parse_equations!(exprs, eqs, dict, body)
187211
elseif mname == Symbol("@icon")

0 commit comments

Comments
 (0)