Skip to content

Commit

Permalink
Merge pull request #10 from jrevels/jr/warn
Browse files Browse the repository at this point in the history
add flag to disable warnings
  • Loading branch information
rdeits authored Apr 1, 2018
2 parents 912260d + ab2005f commit 5163da2
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/CommonSubexpressions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ disqualify!(cache::Cache, s::Symbol) = push!(cache.disqualified_symbols, s)
disqualify!(cache::Cache, expr::Expr) = foreach(arg -> disqualify!(cache, arg), expr.args)

# fallback for non-Expr arguments
combine_subexprs!(setup, expr) = expr
combine_subexprs!(setup, expr, warn_enabled::Bool) = expr

const standard_expression_forms = Set{Symbol}(
(:call,
Expand All @@ -49,31 +49,31 @@ const assignment_expression_forms = Set{Symbol}(
:(*=),
:(/=)))

function combine_subexprs!(cache::Cache, expr::Expr)
function combine_subexprs!(cache::Cache, expr::Expr, warn_enabled::Bool)
if expr.head == :function
# We can't continue CSE through a function definition, but we can
# start over inside the body of the function:
for i in 2:length(expr.args)
expr.args[i] = combine_subexprs!(expr.args[i])
expr.args[i] = combine_subexprs!(expr.args[i], warn_enabled)
end
elseif expr.head == :line
# nothing
elseif expr.head in assignment_expression_forms
disqualify!(cache, expr.args[1])
for i in 2:length(expr.args)
expr.args[i] = combine_subexprs!(cache, expr.args[i])
expr.args[i] = combine_subexprs!(cache, expr.args[i], warn_enabled)
end
elseif expr.head == :generator
for i in vcat(2:length(expr.args), 1)
expr.args[i] = combine_subexprs!(cache, expr.args[i])
expr.args[i] = combine_subexprs!(cache, expr.args[i], warn_enabled)
end
elseif expr.head in standard_expression_forms
for (i, child) in enumerate(expr.args)
expr.args[i] = combine_subexprs!(cache, child)
expr.args[i] = combine_subexprs!(cache, child, warn_enabled)
end
if expr.head == :call
for (i, child) in enumerate(expr.args)
expr.args[i] = combine_subexprs!(cache, child)
expr.args[i] = combine_subexprs!(cache, child, warn_enabled)
end
if all(!isa(arg, Expr) && !(arg in cache.disqualified_symbols) for arg in expr.args)
combined_args = Symbol(expr.args...)
Expand All @@ -87,25 +87,25 @@ function combine_subexprs!(cache::Cache, expr::Expr)
end
end
else
warn("CommonSubexpressions can't yet handle expressions of this form: $(expr.head)")
warn_enabled && warn("CommonSubexpressions can't yet handle expressions of this form: $(expr.head)")
end
return expr
end

combine_subexprs!(x) = x
combine_subexprs!(x, warn_enabled::Bool = true) = x

function combine_subexprs!(expr::Expr)
function combine_subexprs!(expr::Expr, warn_enabled::Bool)
cache = Cache()
expr = combine_subexprs!(cache, expr)
expr = combine_subexprs!(cache, expr, warn_enabled)
Expr(:block, cache.setup..., expr)
end

macro cse(expr)
result = combine_subexprs!(expr)
macro cse(expr, warn_enabled::Bool = true)
result = combine_subexprs!(expr, warn_enabled)
# println(result)
esc(result)
end

cse(expr) = combine_subexprs!(copy(expr))
cse(expr, warn_enabled::Bool = true) = combine_subexprs!(copy(expr), warn_enabled)

end

0 comments on commit 5163da2

Please sign in to comment.