Skip to content

Commit

Permalink
Convert || and && to function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
neonWhiteout committed Oct 18, 2024
1 parent 025c59b commit 9f8130b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/StockFlow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 15 additions & 1 deletion src/Syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 9f8130b

Please sign in to comment.