Skip to content

Commit

Permalink
addressed all comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuqi07 committed Dec 11, 2022
1 parent 587c50b commit ee82180
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
12 changes: 5 additions & 7 deletions src/latexify/codegen/function_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,15 @@ def visit_Match(self, node: ast.Match) -> str:
cond_latex = self.visit(case.pattern)
if case.guard is not None:
cond_latex = self._expression_codegen.visit(case.guard)
subject_latex = "" # getting variable from cond_latex

case_latexes.append(body_latex + r", & \mathrm{if} \ " + cond_latex)
else:
case_latexes.append(
self.visit(node.cases[-1].body[0]) + r", & \mathrm{otherwise}"
self.visit(case.body[0]) + r", & \mathrm{otherwise}"
)

latex = (
r"\left\{ \begin{array}{ll} "
r"\left\{ \begin{array}{ll}"
+ r" \\ ".join(case_latexes)
+ r" \end{array} \right."
)
Expand All @@ -184,7 +183,6 @@ def visit_Match(self, node: ast.Match) -> str:
def visit_MatchValue(self, node: ast.MatchValue) -> str:
"""Visit a MatchValue node."""
latex = self._expression_codegen.visit(node.value)

return "subject_name = " + latex

def visit_MatchAs(self, node: ast.MatchAs) -> str:
Expand All @@ -203,8 +201,8 @@ def visit_MatchOr(self, node: ast.MatchOr) -> str:
"""Visit a MatchOr node."""
case_latexes = []
for i, pattern in enumerate(node.patterns):
if i != 0:
case_latexes.append(r" \lor " + self.visit(pattern))
else:
if i == 0:
case_latexes.append(self.visit(pattern))
else:
case_latexes.append(r" \lor " + self.visit(pattern))
return "".join(case_latexes)
30 changes: 15 additions & 15 deletions src/latexify/codegen/function_codegen_match_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def f(x):
expected = (
r"f(x) ="
r" \left\{ \begin{array}{ll}"
r" 1, & \mathrm{if} \ x = 0 \\"
r"1, & \mathrm{if} \ x = 0 \\"
r" 3 x, & \mathrm{otherwise}"
r" \end{array} \right."
)
Expand All @@ -50,7 +50,7 @@ def test_visit_match() -> None:
).body[0]
expected = (
r"\left\{ \begin{array}{ll}"
r" 1, & \mathrm{if} \ x = 0 \\"
r"1, & \mathrm{if} \ x = 0 \\"
r" 2, & \mathrm{otherwise}"
r" \end{array} \right."
)
Expand All @@ -74,7 +74,7 @@ def test_visit_multiple_match_cases() -> None:
).body[0]
expected = (
r"\left\{ \begin{array}{ll}"
r" 1, & \mathrm{if} \ x = 0 \\"
r"1, & \mathrm{if} \ x = 0 \\"
r" 2, & \mathrm{if} \ x = 1 \\"
r" 3, & \mathrm{otherwise}"
r" \end{array} \right."
Expand Down Expand Up @@ -192,7 +192,7 @@ def test_visit_match_case_with_if() -> None:
textwrap.dedent(
"""
match x:
case x if x>0:
case x if x > 0:
return 1
case _:
return 2
Expand All @@ -202,7 +202,7 @@ def test_visit_match_case_with_if() -> None:

assert (
function_codegen.FunctionCodegen().visit(tree)
== r"\left\{ \begin{array}{ll} "
== r"\left\{ \begin{array}{ll}"
+ r"1, & \mathrm{if} \ x > 0 \\ "
+ r"2, & \mathrm{otherwise} \end{array} \right."
)
Expand All @@ -224,9 +224,9 @@ def test_visit_match_case_with_if_and() -> None:

assert (
function_codegen.FunctionCodegen().visit(tree)
== r"\left\{ \begin{array}{ll} 1, & \mathrm{if} "
+ r"\ x > 0 \land x \le 10 \\ "
+ r"2, & \mathrm{otherwise} \end{array} \right."
== r"\left\{ \begin{array}{ll}1, & \mathrm{if} "
+ r"\ x > 0 \land x \le 10 \\"
+ r" 2, & \mathrm{otherwise} \end{array} \right."
)


Expand All @@ -246,9 +246,9 @@ def test_visit_matchcase_with_if_or() -> None:

assert (
function_codegen.FunctionCodegen().visit(tree)
== r"\left\{ \begin{array}{ll} 1, "
+ r"& \mathrm{if} \ x > 0 \lor x \le 10 \\ "
+ r"2, & \mathrm{otherwise} \end{array} \right."
== r"\left\{ \begin{array}{ll}1,"
+ r" & \mathrm{if} \ x > 0 \lor x \le 10 \\"
+ r" 2, & \mathrm{otherwise} \end{array} \right."
)


Expand All @@ -268,9 +268,9 @@ def test_visit_match_case_with_combined_condition() -> None:

assert (
function_codegen.FunctionCodegen().visit(tree)
== r"\left\{ \begin{array}{ll} 1, "
+ r"& \mathrm{if} \ 0 < x \le 10 \\ 2, "
+ r"& \mathrm{otherwise} \end{array} \right."
== r"\left\{ \begin{array}{ll}1,"
+ r" & \mathrm{if} \ 0 < x \le 10 \\ 2,"
+ r" & \mathrm{otherwise} \end{array} \right."
)


Expand All @@ -290,6 +290,6 @@ def test_visit_match_case_or() -> None:

assert (
function_codegen.FunctionCodegen().visit(tree)
== r"\left\{ \begin{array}{ll} 1, & \mathrm{if} \ x = 0 \lor x = 1 \\"
== r"\left\{ \begin{array}{ll}1, & \mathrm{if} \ x = 0 \lor x = 1 \\"
+ r" 2, & \mathrm{otherwise} \end{array} \right."
)

0 comments on commit ee82180

Please sign in to comment.