From 9f8130b53c93c7fa1c5e2c36bac4d81ca67bf4e1 Mon Sep 17 00:00:00 2001 From: Thomas Purdy Date: Thu, 17 Oct 2024 18:47:13 -0600 Subject: [PATCH] Convert || and && to function calls --- src/StockFlow.jl | 2 ++ src/Syntax.jl | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/StockFlow.jl b/src/StockFlow.jl index 5d9ebdf..fa5c434 100644 --- a/src/StockFlow.jl +++ b/src/StockFlow.jl @@ -40,6 +40,8 @@ vectorify(n) = [n] state_dict(n) = Dict(s=>i for (i, s) in enumerate(n)) cond(c,t,f) = c ? t : f +or(a,b) = a || b +and(a,b) = a && b #= operators definition # Operators: diff --git a/src/Syntax.jl b/src/Syntax.jl index 2e1f9ba..019414a 100644 --- a/src/Syntax.jl +++ b/src/Syntax.jl @@ -358,7 +358,7 @@ function is_recursive_dyvar(dyvar_name, dyvar_def) @match dyvar_def begin ::Symbol => dyvar_def == dyvar_name :($f()) => f == dyvar_name - Expr(:call, args...) => true in map(arg -> is_recursive_dyvar(dyvar_name, arg), args) + Expr(:call, args...) || Expr(:if, args...) || Expr(:||, args...) || Expr(:&&, args...) => true in map(arg -> is_recursive_dyvar(dyvar_name, arg), args) end end """ @@ -387,6 +387,18 @@ function parse_dyvar!(dyvars::Vector{Tuple{Symbol,Expr}}, dyvar::Expr) else error("Recursive dyvar detected in " * String(dyvar_name)) end + :($dyvar_name = $A || $B) => + if !is_recursive_dyvar(dyvar_name, A) && !is_recursive_dyvar(dyvar_name, B) + return (dyvar_name, :(or($A,$B))) + else + error("Recursive dyvar detected in " * String(dyvar_name)) + end + :($dyvar_name = $A && $B) => + if !is_recursive_dyvar(dyvar_name, A) && !is_recursive_dyvar(dyvar_name, B) + return (dyvar_name, :(and($A,$B))) + else + error("Recursive dyvar detected in " * String(dyvar_name)) + end :($dyvar_name = $dyvar_def) => if !is_recursive_dyvar(dyvar_name, dyvar_def) return (dyvar_name, dyvar_def) @@ -830,6 +842,8 @@ function infix_expression_to_binops( function loop(e) @match e begin ::Symbol || ::Float32 || ::Float64 || ::Int || ::String => e + Expr(:||, args...) => loop(Expr(:call, :or, args...)) + Expr(:&&, args...) => loop(Expr(:call, :and, args...)) Expr(:call, f, a) => begin asym = loop(a) varname = gensym(gensymbase)