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
17 changes: 17 additions & 0 deletions src/latexify/codegen/function_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,23 @@ def visit_IfExp(self, node: ast.IfExp) -> str:
latex += self.visit(node)
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 i, match_case in enumerate(node.cases):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the enumerator i is never used.

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 r" = " + latex
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return r" = " + latex
return " = " + latex


def _reduce_stop_parameter(self, node: ast.BinOp) -> str:
# ast.Constant class is added in Python 3.8
# ast.Num is the relevant node type in previous versions
Expand Down
33 changes: 32 additions & 1 deletion src/latexify/codegen/function_codegen_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from latexify import exceptions, test_utils
from latexify.codegen import FunctionCodegen, function_codegen


def test_generic_visit() -> None:
class UnknownNode(ast.AST):
pass
Expand Down Expand Up @@ -744,3 +743,35 @@ def test_use_set_symbols_compare(code: str, latex: str) -> None:
tree = ast.parse(code).body[0].value
assert isinstance(tree, ast.Compare)
assert function_codegen.FunctionCodegen(use_set_symbols=True).visit(tree) == latex


def test_matchvalue() -> None:
tree = ast.parse(
textwrap.dedent(
"""
match x:
case 0:
return 1
Copy link
Collaborator

@odashi odashi Dec 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this (and the following test case) should raise the error, according to the comment above.

"""
)
).body[0]

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


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} \\ {2}, & \mathrm{if} \ x = {1} \\ \end{array} \right."