Skip to content

Commit 2cb4091

Browse files
committed
WIP
1 parent 8546ec5 commit 2cb4091

File tree

1 file changed

+54
-3
lines changed

1 file changed

+54
-3
lines changed

src/systems/connectors.jl

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,64 @@ function get_var(mod::Module, b)
9393
b isa Symbol ? getproperty(mod, b) : b
9494
end
9595
macro model(name::Symbol, expr)
96-
model_macro(name, expr)
96+
model_macro(@__MODULE__, name, expr)
9797
end
98-
function model_macro(name, expr)
98+
function model_macro(mod, name, expr)
99+
exprs = Expr(:block)
99100
for arg in expr.args
100101
arg isa LineNumberNode && continue
101-
arg.head == :macrocall && compose
102+
arg.head == :macrocall || error("$arg is not valid syntax. Expected a macro call.")
103+
parse_model!(exprs.args, mod, arg)
102104
end
105+
exprs
106+
end
107+
function parse_model!(exprs, mod, arg)
108+
mname = arg.args[1]
109+
vs = Num[]
110+
dict = Dict{Symbol, Any}()
111+
body = arg.args[end]
112+
if mname == Symbol("@components")
113+
parse_components!(exprs, dict, body)
114+
elseif mname == Symbol("@variables")
115+
parse_variables!(exprs, vs, dict, mod, body)
116+
elseif mname == Symbol("@equations")
117+
parse_equations!(exprs, dict, body)
118+
else
119+
error("$mname is not handled.")
120+
end
121+
end
122+
function parse_components!(exprs, dict, body)
123+
comps = Pair{String, String}[]
124+
comp_name = Symbol("#___comp___")
125+
for arg in body.args
126+
arg isa LineNumberNode && continue
127+
MLStyle.@match arg begin
128+
Expr(:(=), a, b) => begin
129+
push!(comps, String(a) => readable_code(b))
130+
push!(exprs, arg)
131+
end
132+
_ => error("`@components` only takes assignment expressions. Got $arg")
133+
end
134+
end
135+
dict[:components] = comps
136+
end
137+
function parse_variables!(exprs, vs, dict, mod, body)
138+
for arg in body.args
139+
arg isa LineNumberNode && continue
140+
v = Num(parse_variable_def!(dict, mod, arg))
141+
push!(vs, v)
142+
push!(exprs, :($(getname(v)) = $v))
143+
end
144+
end
145+
function parse_equations!(exprs, dict, body)
146+
eqs = :(Equation[])
147+
for arg in body.args
148+
arg isa LineNumberNode && continue
149+
push!(eqs.args, arg)
150+
end
151+
# TODO: does this work with TOML?
152+
dict[:equations] = readable_code.(@view eqs.args[2:end])
153+
push!(exprs, :(var"#___eqs___" = $eqs))
103154
end
104155

105156
abstract type AbstractConnectorType end

0 commit comments

Comments
 (0)