Skip to content

Commit

Permalink
Support for &&, ||, and isequal
Browse files Browse the repository at this point in the history
Fixes #98

removed duplicated code from _dag_to_function for generating true,false branches. This is the first step to adding support for &&,||
  • Loading branch information
brianguenter committed Oct 21, 2024
1 parent 283e079 commit b7cc4ef
Showing 1 changed file with 36 additions and 32 deletions.
68 changes: 36 additions & 32 deletions src/CodeGeneration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ function sparsity(sym_func::AbstractArray{<:Node})
end
export sparsity

function gen_true_or_false_node(node, branch_node, branch_body, node_to_var, variable_to_index)
if is_leaf(branch_node) #handle leaf nodes properly
if is_constant(branch_node)
temp_val = value(branch_node)
else
temp_val = node_to_var[branch_node]
end

push!(branch_body.args, :($(gensym(:s)) = $(temp_val))) #seems roundabout to use an assignment when really just want the value of the node but couldn't figure out how to make this work with Expr
else
visited = get(node_to_var, branch_node, nothing)
if visited !== nothing
push!(branch_body.args, :($(gensym(:s)) = $visited))
else
_dag_to_function!(branch_node, branch_body, variable_to_index, node_to_var)
end
end
end

function _dag_to_function!(node, local_body, variable_to_index, node_to_var)

Expand All @@ -31,38 +49,24 @@ function _dag_to_function!(node, local_body, variable_to_index, node_to_var)
true_node = children(node)[2]
false_node = children(node)[3]

if is_leaf(true_node) #handle leaf nodes properly
if is_constant(true_node)
temp_val = value(true_node)
else
temp_val = node_to_var[true_node]
end

push!(true_body.args, :($(gensym(:s)) = $(temp_val))) #seems roundabout to use an assignment when really just want the value of the node but couldn't figure out how to make this work with Expr
else
visited = get(node_to_var, children(node)[2], nothing)
if visited !== nothing
push!(true_body.args, :($(gensym(:s)) = $visited))
else
_dag_to_function!(children(node)[2], true_body, variable_to_index, node_to_var)
end
end

if is_leaf(false_node)
if is_constant(false_node)
temp_val = value(false_node)
else
temp_val = node_to_var[false_node]
end
push!(false_body.args, :($(gensym(:s)) = $(temp_val))) #seems roundabout to use an assignment when really just want the value of the node but couldn't figure out how to make this work with Expr
else
visited = get(node_to_var, children(node)[3], nothing)
if visited !== nothing
push!(false_body.args, :($(gensym(:s)) = $visited))
else
_dag_to_function!(children(node)[3], false_body, variable_to_index, node_to_var)
end
end
gen_true_or_false_node(node, true_node, true_body, node_to_var, variable_to_index)

gen_true_or_false_node(node, false_node, false_body, node_to_var, variable_to_index)
# if is_leaf(false_node)
# if is_constant(false_node)
# temp_val = value(false_node)
# else
# temp_val = node_to_var[false_node]
# end
# push!(false_body.args, :($(gensym(:s)) = $(temp_val))) #seems roundabout to use an assignment when really just want the value of the node but couldn't figure out how to make this work with Expr
# else
# visited = get(node_to_var, false_node, nothing)
# if visited !== nothing
# push!(false_body.args, :($(gensym(:s)) = $visited))
# else
# _dag_to_function!(false_node, false_body, variable_to_index, node_to_var)
# end
# end

statement = :($(node_to_var[node]) = if $(if_cond_var)
$(true_body)
Expand Down

0 comments on commit b7cc4ef

Please sign in to comment.