Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect positional arguments in @mtkmodel #3019

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/systems/model_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -945,15 +945,17 @@ end

### Parsing Components:

function component_args!(a, b, varexpr, kwargs; index_name = nothing)
function component_args!(a, b, varexpr, kwargs; index_name = nothing, keywords = false )
baggepinnen marked this conversation as resolved.
Show resolved Hide resolved
# keywords indicates if whether or not the keyword arg delimiter ; has been seen
# Whenever `b` is a function call, skip the first arg aka the function name.
# Whenever 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 || Expr(:kw, x) => begin
Expr(:kw, x) || (x::Symbol && if keywords end) => begin # handle keyword args
baggepinnen marked this conversation as resolved.
Show resolved Hide resolved
varname, _varname = _rename(a, x)
b.args[i] = Expr(:kw, x, _varname)
push!(varexpr.args, :((if $varname !== nothing
Expand All @@ -967,8 +969,23 @@ function component_args!(a, b, varexpr, kwargs; index_name = nothing)
push!(kwargs, Expr(:kw, varname, nothing))
# dict[:kwargs][varname] = nothing
end
x::Symbol && if !keywords end => begin # handle positional args
baggepinnen marked this conversation as resolved.
Show resolved Hide resolved
varname, _varname = _rename(a, x)
b.args[i] = :($_varname)
push!(varexpr.args, :((if $varname !== nothing
$_varname = $varname
elseif @isdefined $x
# Allow users to define a var in `structural_parameters` and set
# that as positional arg of subcomponents; it is useful for cases
# where it needs to be passed to multiple subcomponents.
$_varname = $x
end)))
push!(kwargs, Expr(:kw, varname, nothing))
# dict[:kwargs][varname] = nothing
end
Expr(:parameters, x...) => begin
component_args!(a, arg, varexpr, kwargs)

baggepinnen marked this conversation as resolved.
Show resolved Hide resolved
component_args!(a, arg, varexpr, kwargs; keywords = true)
end
Expr(:kw, x, y) => begin
varname, _varname = _rename(a, x)
Expand Down
Loading