From b7cc4effa7b8426a92f9ec5f7e30702890db5de5 Mon Sep 17 00:00:00 2001 From: brianguenter <1brianguenter@gmail.com> Date: Mon, 21 Oct 2024 10:32:44 -0700 Subject: [PATCH] Support for `&&`, `||`, and `isequal` Fixes #98 removed duplicated code from _dag_to_function for generating true,false branches. This is the first step to adding support for &&,|| --- src/CodeGeneration.jl | 68 +++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 32 deletions(-) diff --git a/src/CodeGeneration.jl b/src/CodeGeneration.jl index 9b1ac56c..964169e5 100644 --- a/src/CodeGeneration.jl +++ b/src/CodeGeneration.jl @@ -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) @@ -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)