Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Match, match_case, and MatchValue #146

Merged
merged 13 commits into from
Dec 9, 2022
29 changes: 29 additions & 0 deletions src/latexify/codegen/function_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,35 @@ def visit_IfExp(self, node: ast.IfExp) -> str:
latex += self.visit(current_expr)
return latex + r", & \mathrm{otherwise} \end{array} \right."

def visit_Match(self, node: ast.Match) -> str:
"""Visit a match node"""
latex = r"\left\{ \begin{array}{ll}"
subject_latex = self.visit(node.subject)
for match_case in node.cases:
odashi marked this conversation as resolved.
Show resolved Hide resolved
if not (
len(match_case.body) == 1 and isinstance(match_case.body[0], ast.Return)
):
raise exceptions.LatexifyNotSupportedError(
"Match cases must have exactly 1 return statement."
)
true_latex = self.visit(match_case.body[0])
odashi marked this conversation as resolved.
Show resolved Hide resolved
cond_latex = self.visit(match_case.pattern)
latex += (
true_latex
+ r", & \mathrm{if} \ "
+ subject_latex
+ cond_latex
+ r" \\ "
)

latex += r"\end{array} \right."
return latex

def visit_MatchValue(self, node: ast.MatchValue) -> str:
"""Visit a MatchValue node"""
latex = self.visit(node.value)
return " = " + latex

def _reduce_stop_parameter(self, node: ast.expr) -> ast.expr:
"""Adjusts the stop expression of the range.

Expand Down
78 changes: 78 additions & 0 deletions src/latexify/codegen/function_codegen_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,84 @@ def test_use_set_symbols_compare(code: str, latex: str) -> None:
assert function_codegen.FunctionCodegen(use_set_symbols=True).visit(tree) == latex


@test_utils.require_at_least(10)
def test_matchvalue() -> None:
tree = ast.parse(
textwrap.dedent(
"""
match x:
case 0:
return 1
"""
)
).body[0]

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


@test_utils.require_at_least(10)
def test_multiple_matchvalue() -> None:
tree = ast.parse(
textwrap.dedent(
"""
match x:
case 0:
return 1
case 1:
return 2
"""
)
).body[0]

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


@test_utils.require_at_least(10)
def test_matchvalue_no_return() -> None:
tree = ast.parse(
textwrap.dedent(
"""
match x:
case 0:
x = 5
"""
)
).body[0]

with pytest.raises(
exceptions.LatexifyNotSupportedError,
match=r"^Match cases must have exactly 1 return statement\.$",
):
FunctionCodegen().visit(tree)


@test_utils.require_at_least(10)
def test_matchvalue_mutliple_statements() -> None:
tree = ast.parse(
textwrap.dedent(
"""
match x:
case 0:
x = 5
return 1
"""
)
).body[0]

with pytest.raises(
exceptions.LatexifyNotSupportedError,
match=r"^Match cases must have exactly 1 return statement\.$",
):
FunctionCodegen().visit(tree)


@pytest.mark.parametrize(
"code,latex",
[
Expand Down