Skip to content

Commit

Permalink
Merge pull request #12 from ctrekker/10-remove-parent-expression
Browse files Browse the repository at this point in the history
Parent removal upon argument reassignment
  • Loading branch information
ctrekker authored Jan 6, 2022
2 parents 34d94e8 + eaf1a1f commit 097f896
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/expression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,7 @@ mutable struct LogicalExpression <: AbstractExpression

function LogicalExpression(arguments::Vector{AbstractExpression}, operation::LogicalOperation)
expr = new(arguments, operation, Set{LogicalExpression}(), recursivevariables(arguments), recursiveoperations(arguments, operation), true, true)
for arg arguments
if arg isa LogicalExpression
push!(parents(arg), expr)
end
end
add_to_parents!(expr)
expr
end
end
Expand All @@ -83,6 +79,22 @@ isnode(::LogicalExpression) = false
operation(expr::LogicalExpression) = getfield(expr, :operation)
arguments(expr::LogicalExpression) = getfield(expr, :arguments)
parents(expr::LogicalExpression) = getfield(expr, :parents)
add_to_parents!(expr::LogicalExpression) = add_to_parents!(expr, arguments(expr))
function add_to_parents!(expr::LogicalExpression, relevant_args::Vector{AbstractExpression})
for arg relevant_args
if arg isa LogicalExpression
push!(parents(arg), expr)
end
end
end
remove_from_parents!(expr::LogicalExpression) = remove_from_parents!(expr, arguments(expr))
function remove_from_parents!(expr::LogicalExpression, relevant_args::Vector{AbstractExpression})
for arg relevant_args
if arg isa LogicalExpression
delete!(parents(arg), expr)
end
end
end
metadata(::LogicalExpression) = nothing
function variables(expr::LogicalExpression)
if !getfield(expr, :cached_variables_valid)
Expand Down Expand Up @@ -112,7 +124,9 @@ Base.deepcopy(expr::LogicalExpression) = LogicalExpression(Vector{AbstractExpres
# mutability methods
function Base.setproperty!(expr::LogicalExpression, name::Symbol, x)
if name == :arguments
remove_from_parents!(expr)
setfield!(expr, name, x)
add_to_parents!(expr)
expr.cached_variables_valid = false
elseif name == :operation
@assert argument_count(operation(expr)) == argument_count(x) "cannot assign operation with $(argument_count(x)) arguments to an expression with $(argument_count(operation(expr)))"
Expand Down Expand Up @@ -141,7 +155,9 @@ end
# see utils.jl#FakeVector
function setvectorindex!(expr::LogicalExpression, name::Symbol, x, index::Int)
if name == :arguments
remove_from_parents!(expr, arguments(expr)[index:index])
getfield(expr, :arguments)[index] = x
add_to_parents!(expr, arguments(expr)[index:index])
expr.cached_variables_valid = false
else
@warn "unexpected `setvectorindex!` call for field `$(name)`"
Expand Down
6 changes: 6 additions & 0 deletions test/expression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,12 @@ end
@test st3 parents(st2)
st4 = a st3
@test st4 parents(st3)

# parent removal upon argument reassignment
st4.arguments[2] = b
@test st4 parents(st3)
st3.arguments[2] = st4
@test st3 parents(st4)
end

@testset "Mutate Operation" begin
Expand Down

0 comments on commit 097f896

Please sign in to comment.