Skip to content

Commit e9a2b83

Browse files
committed
Add block at named
1 parent c19fb0a commit e9a2b83

File tree

3 files changed

+50
-22
lines changed

3 files changed

+50
-22
lines changed

src/systems/abstractsystem.jl

+35-14
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,36 @@ function _named_idxs(name::Symbol, idxs, call)
938938
:($name = $map($sym -> $ex, $idxs))
939939
end
940940

941+
function single_named_expr(expr)
942+
name, call = split_assign(expr)
943+
if Meta.isexpr(name, :ref)
944+
name, idxs = name.args
945+
check_name(name)
946+
var = gensym(name)
947+
ex = quote
948+
$var = $(_named(name, call))
949+
$name = map(i -> $rename($var, Symbol($(Meta.quot(name)), :_, i)), $idxs)
950+
end
951+
ex
952+
else
953+
check_name(name)
954+
:($name = $(_named(name, call)))
955+
end
956+
end
957+
958+
function named_expr(expr)
959+
if Meta.isexpr(expr, :block)
960+
newexpr = Expr(:block)
961+
for ex in expr.args
962+
ex isa LineNumberNode && continue
963+
push!(newexpr.args, single_named_expr(ex))
964+
end
965+
newexpr
966+
else
967+
single_named_expr(expr)
968+
end
969+
end
970+
941971
function check_name(name)
942972
name isa Symbol ||
943973
throw(Meta.ParseError("The lhs must be a symbol (a) or a ref (a[1:10]). Got $name."))
@@ -946,6 +976,10 @@ end
946976
"""
947977
@named y = foo(x)
948978
@named y[1:10] = foo(x)
979+
@named begin
980+
y[1:10] = foo(x)
981+
z = foo(x)
982+
end
949983
@named y 1:10 i -> foo(x*i) # This is not recommended
950984
951985
Pass the LHS name to the model. When it's calling anything that's not an
@@ -974,20 +1008,7 @@ julia> @named y[1:3] = foo(x)
9741008
```
9751009
"""
9761010
macro named(expr)
977-
name, call = split_assign(expr)
978-
if Meta.isexpr(name, :ref)
979-
name, idxs = name.args
980-
check_name(name)
981-
var = gensym(name)
982-
ex = quote
983-
$var = $(_named(name, call))
984-
$name = map(i -> $rename($var, Symbol($(Meta.quot(name)), :_, i)), $idxs)
985-
end
986-
esc(ex)
987-
else
988-
check_name(name)
989-
esc(:($name = $(_named(name, call))))
990-
end
1011+
esc(named_expr(expr))
9911012
end
9921013

9931014
macro named(name::Symbol, idxs, call)

test/direct.jl

+6
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,9 @@ foo(i; name) = (; i, name)
257257
@test isequal(goo, [(i = 10, name = Symbol(:goo_, i)) for i in 1:3])
258258
@named koo 1:3 i->foo(10i)
259259
@test isequal(koo, [(i = 10i, name = Symbol(:koo_, i)) for i in 1:3])
260+
@named begin
261+
x = foo(12)
262+
y[1:3] = foo(13)
263+
end
264+
@test isequal(x, (i = 12, name = :x))
265+
@test isequal(y, [(i = 13, name = Symbol(:y_, i)) for i in 1:3])

test/structural_transformation/bareiss.jl

+9-8
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ function det_bareiss!(M)
1616
end
1717

1818
@testset "bareiss tests" begin
19-
# copy gives a dense matrix
20-
@testset "bareiss tests: $T" for T in (copy, sparse)
21-
# matrix determinent pairs
22-
for (M, d) in ((BigInt[9 1 8 0; 0 0 8 7; 7 6 8 3; 2 9 7 7], -1),
23-
(BigInt[1 big(2)^65+1; 3 4], 4 - 3 * (big(2)^65 + 1)))
24-
# test that the determinent was correctly computed
25-
@test det_bareiss!(T(M)) == d
19+
# copy gives a dense matrix
20+
@testset "bareiss tests: $T" for T in (copy, sparse)
21+
# matrix determinent pairs
22+
for (M, d) in ((BigInt[9 1 8 0; 0 0 8 7; 7 6 8 3; 2 9 7 7], -1),
23+
(BigInt[1 big(2)^65+1; 3 4], 4 - 3 * (big(2)^65 + 1)))
24+
# test that the determinent was correctly computed
25+
@test det_bareiss!(T(M)) == d
26+
end
2627
end
27-
end end
28+
end

0 commit comments

Comments
 (0)