Skip to content

Commit 8393cb7

Browse files
refactor: improve type-stability of renamespace
1 parent a33a721 commit 8393cb7

File tree

1 file changed

+36
-25
lines changed

1 file changed

+36
-25
lines changed

src/systems/abstractsystem.jl

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,40 +1089,51 @@ renamespace(sys, eq::Equation) = namespace_equation(eq, sys)
10891089

10901090
renamespace(names::AbstractVector, x) = foldr(renamespace, names, init = x)
10911091

1092+
renamespace(sys, tgt::AbstractSystem) = rename(tgt, renamespace(sys, nameof(tgt)))
1093+
renamespace(sys, tgt::Symbol) = Symbol(getname(sys), NAMESPACE_SEPARATOR_SYMBOL, tgt)
1094+
10921095
"""
10931096
$(TYPEDSIGNATURES)
10941097
10951098
Namespace `x` with the name of `sys`.
10961099
"""
1097-
function renamespace(sys, x)
1098-
sys === nothing && return x
1099-
x = unwrap(x)
1100-
if x isa SymbolicT
1101-
T = typeof(x)
1102-
if iscall(x) && operation(x) isa Operator
1103-
return maketerm(typeof(x), operation(x),
1104-
Any[renamespace(sys, only(arguments(x)))],
1105-
metadata(x))::T
1106-
end
1107-
if iscall(x) && operation(x) === getindex
1108-
args = arguments(x)
1109-
return maketerm(
1110-
typeof(x), operation(x), vcat(renamespace(sys, args[1]), args[2:end]),
1111-
metadata(x))::T
1112-
end
1113-
let scope = getmetadata(x, SymScope, LocalScope())
1100+
function renamespace(sys, x::SymbolicT)
1101+
Moshi.Match.@match x begin
1102+
BSImpl.Sym(; name) => let scope = getmetadata(x, SymScope, LocalScope())::Union{LocalScope, ParentScope, GlobalScope}
11141103
if scope isa LocalScope
1115-
rename(x, renamespace(getname(sys), getname(x)))::T
1104+
return rename(x, renamespace(getname(sys), name))::SymbolicT
11161105
elseif scope isa ParentScope
1117-
setmetadata(x, SymScope, scope.parent)::T
1118-
else # GlobalScope
1119-
x::T
1106+
return setmetadata(x, SymScope, scope.parent)::SymbolicT
1107+
elseif scope isa GlobalScope
1108+
return x
11201109
end
1110+
error()
1111+
end
1112+
BSImpl.Term(; f, args, shape, type, metadata) => begin
1113+
if f === getindex
1114+
newargs = copy(parent(args))
1115+
newargs[1] = renamespace(sys, args[1])
1116+
return BSImpl.Term{VartypeT}(getindex, newargs; type, shape, metadata)
1117+
elseif f isa SymbolicT
1118+
let scope = getmetadata(x, SymScope, LocalScope())::Union{LocalScope, ParentScope, GlobalScope}
1119+
if scope isa LocalScope
1120+
return rename(x, renamespace(getname(sys), getname(x)))::SymbolicT
1121+
elseif scope isa ParentScope
1122+
return setmetadata(x, SymScope, scope.parent)::SymbolicT
1123+
elseif scope isa GlobalScope
1124+
return x
1125+
end
1126+
error()
1127+
end
1128+
elseif f isa Operator
1129+
newargs = copy(parent(args))
1130+
for (i, arg) in enumerate(args)
1131+
newargs[i] = renamespace(sys, arg)
1132+
end
1133+
return BSImpl.Term{VartypeT}(f, newargs; type, shape, metadata)
1134+
end
1135+
error()
11211136
end
1122-
elseif x isa AbstractSystem
1123-
rename(x, renamespace(sys, nameof(x)))
1124-
else
1125-
Symbol(getname(sys), NAMESPACE_SEPARATOR_SYMBOL, x)
11261137
end
11271138
end
11281139

0 commit comments

Comments
 (0)