Skip to content

Commit 5ac6f7d

Browse files
authored
Merge pull request #113 from JuliaDiffEq/myb/var
Parse an expression of a tuple
2 parents 8041fa6 + da2d2fc commit 5ac6f7d

File tree

4 files changed

+13
-7
lines changed

4 files changed

+13
-7
lines changed

src/differentials.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ _repeat_apply(f, n) = n == 1 ? f : f ∘ _repeat_apply(f, n-1)
6161
function _differential_macro(x)
6262
ex = Expr(:block)
6363
lhss = Symbol[]
64+
x = x isa Tuple && first(x).head == :tuple ? first(x).args : x # tuple handling
6465
x = flatten_expr!(x)
6566
for di in x
66-
@assert di isa Expr && di.args[1] == :~ "@derivatives expects a form that looks like `@derivatives D''~t E'~t`"
67+
@assert di isa Expr && di.args[1] == :~ "@derivatives expects a form that looks like `@derivatives D''~t E'~t` or `@derivatives (D''~t), (E'~t)`"
6768
lhs = di.args[2]
6869
rhs = di.args[3]
6970
order, lhs = count_order(lhs)

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/derivatives.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ using Test
66
@variables x(t) y(t) z(t)
77
@derivatives D'~t D2''~t
88

9+
@test @macroexpand(@derivatives D'~t D2''~t) == @macroexpand(@derivatives (D'~t), (D2''~t))
10+
911
@test isequal(expand_derivatives(D(t)), 1)
1012
@test isequal(expand_derivatives(D(D(t))), 0)
1113

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)