From ee8218098002ee9eb99ba23c94f9cc85801f0b04 Mon Sep 17 00:00:00 2001 From: Yuqi Gong Date: Sun, 11 Dec 2022 17:43:53 -0500 Subject: [PATCH] addressed all comments --- src/latexify/codegen/function_codegen.py | 12 ++++---- .../codegen/function_codegen_match_test.py | 30 +++++++++---------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/latexify/codegen/function_codegen.py b/src/latexify/codegen/function_codegen.py index 56abd9a..fa75c44 100644 --- a/src/latexify/codegen/function_codegen.py +++ b/src/latexify/codegen/function_codegen.py @@ -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." ) @@ -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: @@ -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) diff --git a/src/latexify/codegen/function_codegen_match_test.py b/src/latexify/codegen/function_codegen_match_test.py index b90780c..9ef3d13 100644 --- a/src/latexify/codegen/function_codegen_match_test.py +++ b/src/latexify/codegen/function_codegen_match_test.py @@ -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." ) @@ -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." ) @@ -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." @@ -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 @@ -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." ) @@ -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." ) @@ -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." ) @@ -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." ) @@ -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." )