Skip to content

Commit e1f2b4d

Browse files
committed
refactor: set symbolic defaults to the kwargs of base sys + treat kwargs of base sys as components kwargs
1 parent 37a8e87 commit e1f2b4d

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
@@ -326,8 +326,49 @@ function component_args!(a, b, dict, expr, varexpr, kwargs)
326326
end
327327
end
328328

329+
function extend_args!(a, b, dict, expr, kwargs, varexpr, has_param=false)
330+
# Whenever `b` is a function call, skip the first arg aka the function name.
331+
# Whenver it is a kwargs list, include it.
332+
start = b.head == :call ? 2 : 1
333+
for i in start:lastindex(b.args)
334+
arg = b.args[i]
335+
arg isa LineNumberNode && continue
336+
MLStyle.@match arg begin
337+
x::Symbol => begin
338+
if b.head != :parameters
339+
if has_param
340+
popat!(b.args, i)
341+
push!(b.args[2].args, x)
342+
else
343+
b.args[i] = Expr(:parameters, x)
344+
end
345+
end
346+
push!(kwargs, Expr(:kw, x, nothing))
347+
dict[:kwargs][x] = nothing
348+
end
349+
Expr(:kw, x) => begin
350+
push!(kwargs, Expr(:kw, x, nothing))
351+
dict[:kwargs][x] = nothing
352+
end
353+
Expr(:kw, x, y) => begin
354+
b.args[i] = Expr(:kw, x, x)
355+
push!(varexpr.args, :($x = $x === nothing ? $y : $x))
356+
push!(kwargs, Expr(:kw, x, nothing))
357+
dict[:kwargs][x] = nothing
358+
end
359+
Expr(:parameters, x...) => begin
360+
has_param = true
361+
extend_args!(a, arg, dict, expr, kwargs, varexpr, has_param)
362+
end
363+
_ => error("Could not parse $arg of component $a")
364+
end
365+
end
366+
end
367+
329368
function parse_extend!(exprs, ext, dict, body, kwargs)
330369
expr = Expr(:block)
370+
varexpr = Expr(:block)
371+
push!(exprs, varexpr)
331372
push!(exprs, expr)
332373
body = deepcopy(body)
333374
MLStyle.@match body begin
@@ -339,13 +380,15 @@ function parse_extend!(exprs, ext, dict, body, kwargs)
339380
error("`@extend` destructuring only takes an tuple as LHS. Got $body")
340381
end
341382
a, b = b.args
342-
component_args!(a, b, expr, kwargs)
383+
extend_args!(a, b, dict, expr, kwargs, varexpr)
343384
vars, a, b
344385
end
345386
ext[] = a
346387
push!(b.args, Expr(:kw, :name, Meta.quot(a)))
347-
dict[:extend] = [Symbol.(vars.args), a, b.args[1]]
348388
push!(expr.args, :($a = $b))
389+
390+
dict[:extend] = [Symbol.(vars.args), a, b.args[1]]
391+
349392
if vars !== nothing
350393
push!(expr.args, :(@unpack $vars = $a))
351394
end

test/model_parsing.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ end
104104
end
105105
end
106106

107-
@named capacitor = Capacitor(C = 10, oneport.v = 10.0)
107+
@named capacitor = Capacitor(C = 10, v = 10.0)
108108
@test getdefault(capacitor.v) == 10.0
109109

110110
@mtkmodel Voltage begin
@@ -129,6 +129,7 @@ end
129129
constant = Constant(; k = 1)
130130
ground = Ground()
131131
end
132+
132133
@equations begin
133134
connect(constant.output, source.V)
134135
connect(source.p, resistor.p)

0 commit comments

Comments
 (0)