Skip to content

Commit

Permalink
Support implicit name unpack in at extend
Browse files Browse the repository at this point in the history
  • Loading branch information
YingboMa committed Oct 8, 2023
1 parent 3034c9f commit 137d3d1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
21 changes: 16 additions & 5 deletions src/systems/model_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ function parse_model!(exprs, comps, ext, eqs, icon, vs, ps, sps,
if mname == Symbol("@components")
parse_components!(exprs, comps, dict, body, kwargs)
elseif mname == Symbol("@extend")
parse_extend!(exprs, ext, dict, body, kwargs)
parse_extend!(exprs, ext, dict, mod, body, kwargs)

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

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L240

Added line #L240 was not covered by tests
elseif mname == Symbol("@variables")
parse_variables!(exprs, vs, dict, mod, body, :variables, kwargs)
elseif mname == Symbol("@parameters")
Expand Down Expand Up @@ -372,24 +372,35 @@ function extend_args!(a, b, dict, expr, kwargs, varexpr, has_param = false)
end
end

function parse_extend!(exprs, ext, dict, body, kwargs)
function parse_extend!(exprs, ext, dict, mod, body, kwargs)

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

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L375

Added line #L375 was not covered by tests
expr = Expr(:block)
varexpr = Expr(:block)
push!(exprs, varexpr)
push!(exprs, expr)
body = deepcopy(body)
MLStyle.@match body begin
Expr(:(=), a, b) => begin
vars = nothing
if Meta.isexpr(b, :(=))
vars = a
if !Meta.isexpr(vars, :tuple)
error("`@extend` destructuring only takes an tuple as LHS. Got $body")
end
a, b = b.args
extend_args!(a, b, dict, expr, kwargs, varexpr)
vars, a, b
else
if (model = getproperty(mod, b.args[1])) isa Model
_vars = keys(get(model.structure, :variables, Dict()))
_vars = union(_vars, keys(get(model.structure, :parameters, Dict())))
_vars = union(_vars,

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

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L390-L393

Added lines #L390 - L393 were not covered by tests
map(first, get(model.structure, :components, Vector{Symbol}[])))
vars = Expr(:tuple)
append!(vars.args, collect(_vars))

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

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L395-L396

Added lines #L395 - L396 were not covered by tests
else
error("Cannot infer the exact `Model` that `@extend $(body)` refers." *

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

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L398

Added line #L398 was not covered by tests
" Please specify the names that it brings into scope by:" *
" `@extend a, b = oneport = OnePort()`.")
end
end
extend_args!(a, b, dict, expr, kwargs, varexpr)

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

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L403

Added line #L403 was not covered by tests
ext[] = a
push!(b.args, Expr(:kw, :name, Meta.quot(a)))
push!(expr.args, :($a = $b))
Expand Down
2 changes: 1 addition & 1 deletion test/model_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ l15 0" stroke="black" stroke-width="1" stroke-linejoin="bevel" fill="none"></pat
@parameters begin
C, [unit = u"F"]
end
@extend v, i = oneport = OnePort(; v = 0.0)
@extend oneport = OnePort(; v = 0.0)
@icon "https://upload.wikimedia.org/wikipedia/commons/7/78/Capacitor_symbol.svg"
@equations begin
D(v) ~ i / C
Expand Down

0 comments on commit 137d3d1

Please sign in to comment.