Skip to content

Commit b80f051

Browse files
Clean up conversion
1 parent d2e7168 commit b80f051

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

src/operations.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ Base.:(==)(x::Nothing,y::Operation) = false
1515
Base.:(==)(x::Variable,y::Operation) = false
1616
Base.:(==)(x::Operation,y::Variable) = false
1717

18-
Base.convert(::Type{Expr}, O::Operation) = Expr(:call, Symbol(O.op), convert.(Expr, O.args)...)
18+
Base.convert(::Type{Expr}, O::Operation) =
19+
build_expr(:call, Any[Symbol(O.op); convert.(Expr, O.args)])
1920
Base.show(io::IO, O::Operation) = print(io, convert(Expr, O))
2021

2122

src/utils.jl

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@ using MacroTools
33

44
function Base.convert(::Type{Expression}, ex::Expr)
55
ex.head === :call || throw(ArgumentError("internal representation does not support non-call Expr"))
6-
f = ex.args[1]
7-
operands = ex.args[2:end]
8-
return convert(Expression, f, convert.(Expression, operands))
6+
7+
op = eval(ex.args[1]) # HACK
8+
args = convert.(Expression, ex.args[2:end])
9+
10+
return Operation(op, args)
911
end
10-
Base.convert(::Type{Expression}, sym::Symbol, args) = Operation(eval(sym), args)
1112
Base.convert(::Type{Expression}, x::Expression) = x
1213
Base.convert(::Type{Expression}, x::Number) = Constant(x)
1314

14-
15-
function expr_arr_to_block(exprs)
16-
block = :(begin end)
17-
foreach(expr -> push!(block.args, expr), exprs)
18-
block
15+
function build_expr(head::Symbol, args)
16+
ex = Expr(head)
17+
append!(ex.args, args)
18+
ex
1919
end
20+
expr_arr_to_block(exprs) = build_expr(:block, exprs)
2021

2122
# used in parsing
2223
isblock(x) = length(x) == 1 && x[1] isa Expr && x[1].head == :block

0 commit comments

Comments
 (0)