Skip to content

Commit f253b51

Browse files
authored
Merge pull request #985 from SciML/myb/write
Overload `write` on `AbstractSystem`s to write formatted Julia expressions
2 parents 956f858 + fcdb960 commit f253b51

File tree

5 files changed

+34
-0
lines changed

5 files changed

+34
-0
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
1515
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
1616
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
1717
IfElse = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173"
18+
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
1819
LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800"
1920
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
2021
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
@@ -50,6 +51,7 @@ DiffRules = "0.1, 1.0"
5051
Distributions = "0.23, 0.24"
5152
DocStringExtensions = "0.7, 0.8"
5253
IfElse = "0.1"
54+
JuliaFormatter = "0.13"
5355
LabelledArrays = "1.3"
5456
Latexify = "0.11, 0.12, 0.13, 0.14, 0.15"
5557
LightGraphs = "1.3"

src/ModelingToolkit.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import SymbolicUtils: istree, arguments, operation, similarterm, promote_symtype
3535
using SymbolicUtils.Code
3636
import SymbolicUtils.Code: toexpr
3737
import SymbolicUtils.Rewriters: Chain, Postwalk, Prewalk, Fixpoint
38+
import JuliaFormatter
3839

3940
using Reexport
4041
@reexport using Symbolics

src/systems/abstractsystem.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,9 @@ function push_defaults!(stmt, defs, var2name)
468468
return defs_name
469469
end
470470

471+
###
472+
### System I/O
473+
###
471474
function toexpr(sys::AbstractSystem)
472475
sys = flatten(sys)
473476
expr = Expr(:block)
@@ -503,6 +506,8 @@ function toexpr(sys::AbstractSystem)
503506
striplines(expr) # keeping the line numbers is never helpful
504507
end
505508

509+
Base.write(io::IO, sys::AbstractSystem) = write(io, readable_code(toexpr(sys)))
510+
506511
function Base.show(io::IO, ::MIME"text/plain", sys::AbstractSystem)
507512
eqs = equations(sys)
508513
if eqs isa AbstractArray

src/utils.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,23 @@ function indepvar2depvar(s::Sym, args...)
8484
ns = Sym{T}(nameof(s))(args...)
8585
@set! ns.metadata = s.metadata
8686
end
87+
88+
function _readable_code(ex)
89+
ex isa Expr || return ex
90+
if ex.head === :call
91+
f, args = ex.args[1], ex.args[2:end]
92+
if f isa Function && (nf = nameof(f); Base.isoperator(nf))
93+
expr = Expr(:call, nf)
94+
for a in args
95+
push!(expr.args, _readable_code(a))
96+
end
97+
return expr
98+
end
99+
end
100+
expr = Expr(ex.head)
101+
for a in ex.args
102+
push!(expr.args, _readable_code(a))
103+
end
104+
expr
105+
end
106+
readable_code(expr) = JuliaFormatter.format_text(string(Base.remove_linenums!(_readable_code(expr))))

test/serialization.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,9 @@ for prob in [
1919

2020
run(`$(Base.julia_cmd()) -e $(_cmd)`)
2121
end
22+
23+
include("../examples/rc_model.jl")
24+
io = IOBuffer()
25+
write(io, rc_model)
26+
sys = include_string(@__MODULE__, String(take!(io)))
27+
@test sys == flatten(rc_model)

0 commit comments

Comments
 (0)