Skip to content

Commit 17f4081

Browse files
committed
Merge remote-tracking branch 'willow/compromise-interface' into compromise-interface
2 parents 5b19f95 + d707674 commit 17f4081

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
lines changed

src/TermInterface.jl

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ export issym
5050
Returns the head of the S-expression.
5151
"""
5252
function head end
53+
export head
5354

5455
"""
5556
children(x)
5657
Returns the children (aka tail) of the S-expression.
5758
"""
5859
function children end
60+
export children
5961

6062
"""
6163
operation(x)
@@ -119,20 +121,20 @@ end
119121
similarterm(x, op, args, symtype=nothing; metadata=nothing)
120122
121123
"""
122-
function similarterm(x, op, args, symtype = nothing; metadata = nothing)
123-
Base.depwarn("""`similarterm` is deprecated, use `maketerm` instead.
124-
See https://github.com/JuliaSymbolics/TermInterface.jl for details.
125-
The present call can be replaced by
126-
`maketerm(typeof(x), $(callhead(x)), [op, args...], symtype, metadata)`""", :similarterm)
124+
function similarterm(x, op, args, symtype=nothing; metadata=nothing)
125+
Base.depwarn("""`similarterm` is deprecated, use `maketerm` instead.
126+
See https://github.com/JuliaSymbolics/TermInterface.jl for details.
127+
The present call can be replaced by
128+
`maketerm(typeof(x), $(head(x)), [op, args...], symtype, metadata)`""", :similarterm)
127129

128-
maketerm(typeof(x), callhead(x), [op, args...], symtype, metadata)
130+
maketerm(typeof(x), callhead(x), [op, args...], symtype, metadata)
129131
end
130132

131133
# Old fallback
132-
function similarterm(T::Type, op, args, symtype = nothing; metadata = nothing)
133-
Base.depwarn("`similarterm` is deprecated, use `maketerm` instead." *
134-
"See https://github.com/JuliaSymbolics/TermInterface.jl for details.", :similarterm)
135-
op(args...)
134+
function similarterm(T::Type, op, args, symtype=nothing; metadata=nothing)
135+
Base.depwarn("`similarterm` is deprecated, use `maketerm` instead." *
136+
"See https://github.com/JuliaSymbolics/TermInterface.jl for details.", :similarterm)
137+
op(args...)
136138
end
137139

138140
export similarterm
@@ -141,7 +143,7 @@ export similarterm
141143
"""
142144
callhead(x)
143145
Used in this deprecation cycle of `similarterm` to find the `head` argument to
144-
`makterm`. Do not implement this, or use `similarterm` if you're using this package.
146+
`maketerm`. Do not implement this, or use `similarterm` if you're using this package.
145147
"""
146148
callhead(x) = typeof(x)
147149

@@ -166,9 +168,10 @@ If your types do not support type information or metadata, you still need to acc
166168
these arguments and may choose to not use them.
167169
"""
168170

169-
function maketerm(T::Type, head, children, type, metadata)
170-
error("maketerm for $T is not implemented")
171+
function maketerm(T::Type, head, children, type=nothing, metadata=nothing)
172+
error("maketerm for $T is not implemented")
171173
end
174+
export maketerm
172175

173176
include("utils.jl")
174177

src/expr.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33

44
iscall(x::Expr) = x.head == :call
55

6+
callhead(e::Expr) = e.head
67
head(e::Expr) = e.head
78
children(e::Expr) = e.args
89

9-
operation(e::Expr) = e.args[1]
10-
arguments(e::Expr) = e.args[2:end]
10+
operation(e::Expr) = iscall(e) ? first(children(e)) : error("operation called on a non-function call expression")
11+
arguments(e::Expr) = iscall(e) ? @view(e.args[2:end]) : error("arguments called on a non-function call expression")
1112

12-
function maketerm(::Type{Expr}, head, args, symtype, metadata)
13+
function maketerm(::Type{Expr}, head, args, symtype=nothing, metadata=nothing)
1314
Expr(head, args...)
1415
end

test/runtests.jl

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@ using Test
33

44
@testset "Expr" begin
55
ex = :(f(a, b))
6-
@test operation(ex) == :call
7-
@test arguments(ex) == [:f, :a, :b]
8-
@test ex == similarterm(ex, :call, [:f, :a, :b])
6+
@test head(ex) == :call
7+
@test children(ex) == [:f, :a, :b]
8+
@test operation(ex) == :f
9+
@test arguments(ex) == [:a, :b]
10+
@test iscall(ex)
11+
@test ex == similarterm(ex, :f, [:a, :b])
12+
@test ex == maketerm(Expr, :call, [:f, :a, :b])
13+
914

1015
ex = :(arr[i, j])
11-
@test operation(ex) == :ref
12-
@test arguments(ex) == [:arr, :i, :j]
13-
@test ex == similarterm(ex, :ref, [:arr, :i, :j])
16+
@test head(ex) == :ref
17+
@test_throws ErrorException operation(ex)
18+
@test_throws ErrorException arguments(ex)
19+
@test !iscall(ex)
20+
@test ex == maketerm(Expr, :ref, [:arr, :i, :j])
1421
end

0 commit comments

Comments
 (0)