Skip to content

Commit

Permalink
refactor: set symbolic defaults to the kwargs of base sys + treat kwa…
Browse files Browse the repository at this point in the history
…rgs of base sys as components kwargs
  • Loading branch information
ven-k committed Aug 14, 2023
1 parent 37a8e87 commit e1f2b4d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
47 changes: 45 additions & 2 deletions src/systems/model_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,49 @@ function component_args!(a, b, dict, expr, varexpr, kwargs)
end

Check warning on line 326 in src/systems/model_parsing.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L326

Added line #L326 was not covered by tests
end

function extend_args!(a, b, dict, expr, kwargs, varexpr, has_param=false)

Check warning on line 329 in src/systems/model_parsing.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L329

Added line #L329 was not covered by tests
# Whenever `b` is a function call, skip the first arg aka the function name.
# Whenver it is a kwargs list, include it.
start = b.head == :call ? 2 : 1
for i in start:lastindex(b.args)
arg = b.args[i]
arg isa LineNumberNode && continue
MLStyle.@match arg begin
x::Symbol => begin
if b.head != :parameters
if has_param
popat!(b.args, i)
push!(b.args[2].args, x)

Check warning on line 341 in src/systems/model_parsing.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L332-L341

Added lines #L332 - L341 were not covered by tests
else
b.args[i] = Expr(:parameters, x)

Check warning on line 343 in src/systems/model_parsing.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L343

Added line #L343 was not covered by tests
end
end
push!(kwargs, Expr(:kw, x, nothing))
dict[:kwargs][x] = nothing

Check warning on line 347 in src/systems/model_parsing.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L346-L347

Added lines #L346 - L347 were not covered by tests
end
Expr(:kw, x) => begin
push!(kwargs, Expr(:kw, x, nothing))
dict[:kwargs][x] = nothing

Check warning on line 351 in src/systems/model_parsing.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L349-L351

Added lines #L349 - L351 were not covered by tests
end
Expr(:kw, x, y) => begin
b.args[i] = Expr(:kw, x, x)
push!(varexpr.args, :($x = $x === nothing ? $y : $x))
push!(kwargs, Expr(:kw, x, nothing))
dict[:kwargs][x] = nothing

Check warning on line 357 in src/systems/model_parsing.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L353-L357

Added lines #L353 - L357 were not covered by tests
end
Expr(:parameters, x...) => begin
has_param = true
extend_args!(a, arg, dict, expr, kwargs, varexpr, has_param)

Check warning on line 361 in src/systems/model_parsing.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L359-L361

Added lines #L359 - L361 were not covered by tests
end
_ => error("Could not parse $arg of component $a")
end
end
end

function parse_extend!(exprs, ext, dict, body, kwargs)
expr = Expr(:block)
varexpr = Expr(:block)
push!(exprs, varexpr)

Check warning on line 371 in src/systems/model_parsing.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L370-L371

Added lines #L370 - L371 were not covered by tests
push!(exprs, expr)
body = deepcopy(body)
MLStyle.@match body begin
Expand All @@ -339,13 +380,15 @@ function parse_extend!(exprs, ext, dict, body, kwargs)
error("`@extend` destructuring only takes an tuple as LHS. Got $body")
end
a, b = b.args
component_args!(a, b, expr, kwargs)
extend_args!(a, b, dict, expr, kwargs, varexpr)

Check warning on line 383 in src/systems/model_parsing.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L383

Added line #L383 was not covered by tests
vars, a, b
end
ext[] = a
push!(b.args, Expr(:kw, :name, Meta.quot(a)))
dict[:extend] = [Symbol.(vars.args), a, b.args[1]]
push!(expr.args, :($a = $b))

dict[:extend] = [Symbol.(vars.args), a, b.args[1]]

Check warning on line 390 in src/systems/model_parsing.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L390

Added line #L390 was not covered by tests

if vars !== nothing
push!(expr.args, :(@unpack $vars = $a))
end
Expand Down
3 changes: 2 additions & 1 deletion test/model_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ end
end
end

@named capacitor = Capacitor(C = 10, oneport.v = 10.0)
@named capacitor = Capacitor(C = 10, v = 10.0)
@test getdefault(capacitor.v) == 10.0

@mtkmodel Voltage begin
Expand All @@ -129,6 +129,7 @@ end
constant = Constant(; k = 1)
ground = Ground()
end

@equations begin
connect(constant.output, source.V)
connect(source.p, resistor.p)
Expand Down

0 comments on commit e1f2b4d

Please sign in to comment.