Skip to content

Commit ee82180

Browse files
committed
addressed all comments
1 parent 587c50b commit ee82180

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

src/latexify/codegen/function_codegen.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,15 @@ def visit_Match(self, node: ast.Match) -> str:
164164
cond_latex = self.visit(case.pattern)
165165
if case.guard is not None:
166166
cond_latex = self._expression_codegen.visit(case.guard)
167-
subject_latex = "" # getting variable from cond_latex
168167

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

175174
latex = (
176-
r"\left\{ \begin{array}{ll} "
175+
r"\left\{ \begin{array}{ll}"
177176
+ r" \\ ".join(case_latexes)
178177
+ r" \end{array} \right."
179178
)
@@ -184,7 +183,6 @@ def visit_Match(self, node: ast.Match) -> str:
184183
def visit_MatchValue(self, node: ast.MatchValue) -> str:
185184
"""Visit a MatchValue node."""
186185
latex = self._expression_codegen.visit(node.value)
187-
188186
return "subject_name = " + latex
189187

190188
def visit_MatchAs(self, node: ast.MatchAs) -> str:
@@ -203,8 +201,8 @@ def visit_MatchOr(self, node: ast.MatchOr) -> str:
203201
"""Visit a MatchOr node."""
204202
case_latexes = []
205203
for i, pattern in enumerate(node.patterns):
206-
if i != 0:
207-
case_latexes.append(r" \lor " + self.visit(pattern))
208-
else:
204+
if i == 0:
209205
case_latexes.append(self.visit(pattern))
206+
else:
207+
case_latexes.append(r" \lor " + self.visit(pattern))
210208
return "".join(case_latexes)

src/latexify/codegen/function_codegen_match_test.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def f(x):
2828
expected = (
2929
r"f(x) ="
3030
r" \left\{ \begin{array}{ll}"
31-
r" 1, & \mathrm{if} \ x = 0 \\"
31+
r"1, & \mathrm{if} \ x = 0 \\"
3232
r" 3 x, & \mathrm{otherwise}"
3333
r" \end{array} \right."
3434
)
@@ -50,7 +50,7 @@ def test_visit_match() -> None:
5050
).body[0]
5151
expected = (
5252
r"\left\{ \begin{array}{ll}"
53-
r" 1, & \mathrm{if} \ x = 0 \\"
53+
r"1, & \mathrm{if} \ x = 0 \\"
5454
r" 2, & \mathrm{otherwise}"
5555
r" \end{array} \right."
5656
)
@@ -74,7 +74,7 @@ def test_visit_multiple_match_cases() -> None:
7474
).body[0]
7575
expected = (
7676
r"\left\{ \begin{array}{ll}"
77-
r" 1, & \mathrm{if} \ x = 0 \\"
77+
r"1, & \mathrm{if} \ x = 0 \\"
7878
r" 2, & \mathrm{if} \ x = 1 \\"
7979
r" 3, & \mathrm{otherwise}"
8080
r" \end{array} \right."
@@ -192,7 +192,7 @@ def test_visit_match_case_with_if() -> None:
192192
textwrap.dedent(
193193
"""
194194
match x:
195-
case x if x>0:
195+
case x if x > 0:
196196
return 1
197197
case _:
198198
return 2
@@ -202,7 +202,7 @@ def test_visit_match_case_with_if() -> None:
202202

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

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

232232

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

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

254254

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

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

276276

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

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

0 commit comments

Comments
 (0)