Skip to content

Commit c9aa240

Browse files
committed
Parse an expression of a tuple to Variables
1 parent 6450e3b commit c9aa240

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/variables.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ function _parse_vars(macroname, known, x)
4949
# y
5050
# z
5151
# end
52+
x = x isa Tuple && first(x) isa Expr && first(x).head == :tuple ? first(x).args : x # tuple handling
5253
x = flatten_expr!(x)
5354
for _var in x
5455
iscall = isa(_var, Expr) && _var.head == :call
5556
issym = _var isa Symbol
56-
@assert iscall || issym "@$macroname expects a tuple of expressions (`@$macroname x y z(t)`)"
57+
@assert iscall || issym "@$macroname expects a tuple of expressions or an expression of a tuple (`@$macroname x y z(t)` or `@$macroname x, y, z(t)`)"
5758

5859
if iscall
5960
dependents = :(Variable[$(_var.args[2:end]...)])
@@ -71,8 +72,8 @@ function _parse_vars(macroname, known, x)
7172
return ex
7273
end
7374
macro variables(xs...)
74-
esc(_parse_vars(:Variable, false, xs))
75+
esc(_parse_vars(:variables, false, xs))
7576
end
7677
macro parameters(xs...)
77-
esc(_parse_vars(:Param, true, xs))
78+
esc(_parse_vars(:parameters, true, xs))
7879
end

test/variable_parsing.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ using ModelingToolkit
22
using Test
33

44
@parameters t
5-
@variables x(t)
6-
@variables y(t)
7-
@variables z(t)
5+
@variables x(t) y(t) # test multi-arg
6+
@variables z(t) # test single-arg
87
x1 = Variable(:x, [t])
98
y1 = Variable(:y, [t])
109
z1 = Variable(:z, [t])
@@ -33,3 +32,6 @@ D1 = Differential(t)
3332
@test convert(Expr, D) == D
3433

3534
@test isequal(x y + 1, (x < y + 1) | (x == y + 1))
35+
36+
@test @macroexpand(@parameters x, y, z(t)) == @macroexpand(@parameters x y z(t))
37+
@test @macroexpand(@variables x, y, z(t)) == @macroexpand(@variables x y z(t))

0 commit comments

Comments
 (0)