Skip to content

Commit

Permalink
added logic for if statement
Browse files Browse the repository at this point in the history
  • Loading branch information
co1emi11er2 committed Dec 2, 2024
1 parent 419f260 commit 1ab0a71
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/handcalcs_macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,30 @@ end
function clean_expr(expr, len, spa)
expr = expr[2:end-1] # remove the $ from end and beginning of string
expr = expr[end] == " " ? expr : expr * " " # add trailing space if there isn't one
pattern = r"\\begin\{cases\}(.*?)\\end\{cases\}"s # if format pattern

# if format pattern
pattern = r"\\begin\{cases\}(.*?)\\end\{cases\}"s
m = match(pattern, expr)
if !isnothing(m) # expr is an if statement, do something different
expr = split(expr, "=") |> unique |> x -> join(x, "=")[1:end-1] # removes any redundant parts, and removes space at the end
expr = replace(expr, "="=>"&=", count=1) # add alignment
expr = len == :long ? replace(expr, " ="=>"\n\\\\[$spa" *"pt]\n&=", count=2) : expr

# change "=" within ifs to "==" so they do not match to below exprs
for m in eachmatch(pattern, expr)
new_m = replace(m.match, "=" => "==")
expr = replace(expr, m.match => new_m)
end

expr = split(expr, " = ") |> unique |> x -> join(x, " = ")[1:end-1] # removes any redundant parts, and removes space at the end
expr = replace(expr, " = "=>"&=", count=1) # add alignment
expr = len == :long ? replace(expr, " = "=>"\n\\\\[$spa" *"pt]\n&= ", count=2) : expr

# change "==" back to "="
for m in eachmatch(pattern, expr)
new_m = replace(m.match, "==" => "=")
expr = replace(expr, m.match => new_m)
end
return expr
end
# TODO the line below breaks if statements in edge cases

expr = split(expr, "=") |> unique |> x -> join(x, "=")[1:end-1] # removes any redundant parts, and removes space at the end
expr = replace(expr, "="=>"&=", count=1) # add alignment
expr = len == :long ? replace(expr, " ="=>"\n\\\\[$spa" *"pt]\n&=", count=2) : expr
Expand Down
35 changes: 35 additions & 0 deletions test/handcalcs_macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,38 @@ end not_funcs = [calc_Ix; calc_Iy]
@test calc3 == expected
@test calc4 == expected
# ***************************************************

# if statement test
# ***************************************************
# ***************************************************
expected = L"$\begin{aligned}
I&=\begin{cases}
\frac{5 \cdot 15^{3}}{12} & \text{if } type = Ix\\
\frac{15 \cdot 5^{3}}{12} & \text{otherwise}
\end{cases} = \begin{cases}
\frac{5 \cdot 15^{3}}{12} & \text{if } Ix = Ix\\
\frac{15 \cdot 5^{3}}{12} & \text{otherwise}
\end{cases} = 1406.25
\end{aligned}$"
type = :Ix
calc = @handcalcs I= if type == :Ix
5 * 15^3/12
else
15 * 5^3/12
end
@test calc == expected

expected = L"$\begin{aligned}
I&=\begin{cases}
\frac{5 \cdot 15^{3}}{12} & \text{if } Ix = Ix\\
\frac{15 \cdot 5^{3}}{12} & \text{otherwise}
\end{cases} = 1406.25
\end{aligned}$"

calc = @handcalcs I= if :Ix == :Ix
5 * 15^3/12
else
15 * 5^3/12
end
@test calc == expected
# ***************************************************

0 comments on commit 1ab0a71

Please sign in to comment.