Skip to content

Commit

Permalink
feat: support components with fully-qualified names with @mtkmodel
Browse files Browse the repository at this point in the history
With the `@named` macro, we could use fully-qualified names for
components. However, trying to do the same with `@mtkmodel`,
```
@mtkmodel Model begin
     @components begin
          resistor = ModelingToolkitStandardLibrary.Electrical.Resistor(R = 1)
          ...
     end
     ...
end
```
throws the following error.
```
ERROR: LoadError: MethodError: Cannot `convert` an object of type Expr to an object of type Symbol

Closest candidates are:
  convert(::Type{T}, ::T) where T
   @ Base Base.jl:84
  Symbol(::Any...)
   @ Base strings/basic.jl:229
```

Fix this and support fully qualified names by considering the
fully-qualifed name's Expr while parsing components.
  • Loading branch information
thazhemadam committed Dec 7, 2023
1 parent 493e695 commit 973c09a
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/systems/model_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ function _parse_components!(exprs, body, kwargs)
expr = Expr(:block)
varexpr = Expr(:block)
# push!(exprs, varexpr)
comps = Vector{Symbol}[]
comps = Vector{Union{Symbol, Expr}}[]
comp_names = []

for arg in body.args
Expand All @@ -692,7 +692,9 @@ function _parse_components!(exprs, body, kwargs)
arg.args[2] = b
push!(expr.args, arg)
push!(comp_names, a)
push!(comps, [a, b.args[1]])
if(isa(b.args[1], Symbol) || Meta.isexpr(b.args[1], :.))
push!(comps, [a, b.args[1]])
end
end
_ => error("Couldn't parse the component body: $arg")
end
Expand Down

0 comments on commit 973c09a

Please sign in to comment.