Skip to content

Commit 0076490

Browse files
committed
refactor: set symbolic defaults to the kwargs of base sys + treat kwargs of base sys as components kwargs
1 parent 672e42c commit 0076490

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

src/systems/model_parsing.jl

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,49 @@ function component_args!(a, b, dict, expr, varexpr, kwargs)
296296
end
297297
end
298298

299+
function extend_args!(a, b, dict, expr, kwargs, varexpr, has_param = false)
300+
# Whenever `b` is a function call, skip the first arg aka the function name.
301+
# Whenver it is a kwargs list, include it.
302+
start = b.head == :call ? 2 : 1
303+
for i in start:lastindex(b.args)
304+
arg = b.args[i]
305+
arg isa LineNumberNode && continue
306+
MLStyle.@match arg begin
307+
x::Symbol => begin
308+
if b.head != :parameters
309+
if has_param
310+
popat!(b.args, i)
311+
push!(b.args[2].args, x)
312+
else
313+
b.args[i] = Expr(:parameters, x)
314+
end
315+
end
316+
push!(kwargs, Expr(:kw, x, nothing))
317+
dict[:kwargs][x] = nothing
318+
end
319+
Expr(:kw, x) => begin
320+
push!(kwargs, Expr(:kw, x, nothing))
321+
dict[:kwargs][x] = nothing
322+
end
323+
Expr(:kw, x, y) => begin
324+
b.args[i] = Expr(:kw, x, x)
325+
push!(varexpr.args, :($x = $x === nothing ? $y : $x))
326+
push!(kwargs, Expr(:kw, x, nothing))
327+
dict[:kwargs][x] = nothing
328+
end
329+
Expr(:parameters, x...) => begin
330+
has_param = true
331+
extend_args!(a, arg, dict, expr, kwargs, varexpr, has_param)
332+
end
333+
_ => error("Could not parse $arg of component $a")
334+
end
335+
end
336+
end
337+
299338
function parse_extend!(exprs, ext, dict, body, kwargs)
300339
expr = Expr(:block)
340+
varexpr = Expr(:block)
341+
push!(exprs, varexpr)
301342
push!(exprs, expr)
302343
body = deepcopy(body)
303344
MLStyle.@match body begin
@@ -309,13 +350,15 @@ function parse_extend!(exprs, ext, dict, body, kwargs)
309350
error("`@extend` destructuring only takes an tuple as LHS. Got $body")
310351
end
311352
a, b = b.args
312-
component_args!(a, b, expr, kwargs)
353+
extend_args!(a, b, dict, expr, kwargs, varexpr)
313354
vars, a, b
314355
end
315356
ext[] = a
316357
push!(b.args, Expr(:kw, :name, Meta.quot(a)))
317-
dict[:extend] = [Symbol.(vars.args), a, b.args[1]]
318358
push!(expr.args, :($a = $b))
359+
360+
dict[:extend] = [Symbol.(vars.args), a, b.args[1]]
361+
319362
if vars !== nothing
320363
push!(expr.args, :(@unpack $vars = $a))
321364
end

test/model_parsing.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ end
107107
end
108108
end
109109

110-
@named capacitor = Capacitor(C = 10, oneport.v = 10.0)
110+
@named capacitor = Capacitor(C = 10, v = 10.0)
111111
@test getdefault(capacitor.v) == 10.0
112112

113113
@mtkmodel Voltage begin
@@ -132,6 +132,7 @@ end
132132
constant = Constant(; k = 1)
133133
ground = Ground()
134134
end
135+
135136
@equations begin
136137
connect(constant.output, source.V)
137138
connect(source.p, resistor.p)

0 commit comments

Comments
 (0)