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
61 changes: 61 additions & 0 deletions src/latexify/codegen/function_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,67 @@ 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 len(match_case.body) != 1:
raise exceptions.LatexifySyntaxError(
"Multiple statements are not supported in Match nodes."
)
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.BinOp) -> str:
# ast.Constant class is added in Python 3.8
# ast.Num is the relevant node type in previous versions
if sys.version_info.minor < 8:
if isinstance(node.right, ast.Num):
if isinstance(node.op, ast.Add):
if node.right.n == 1:
upper = "{" + self.visit(node.left) + "}"
else:
reduced_constant = ast.Num(node.right.n - 1)
new_node = ast.BinOp(node.left, node.op, reduced_constant)
upper = "{" + self.visit(new_node) + "}"
else:
if node.right.n == -1:
upper = "{" + self.visit(node.left) + "}"
else:
reduced_constant = ast.Num(node.right.n + 1)
new_node = ast.BinOp(node.left, node.op, reduced_constant)
upper = "{" + self.visit(new_node) + "}"
else:
upper = "{" + self.visit(node) + "}"
else:
if isinstance(node.right, ast.Constant):
if isinstance(node.op, ast.Add):
if node.right.value == 1:
upper = "{" + self.visit(node.left) + "}"
else:
reduced_constant = ast.Constant(node.right.value - 1)
new_node = ast.BinOp(node.left, node.op, reduced_constant)
upper = "{" + self.visit(new_node) + "}"
else:
if node.right.value == -1:
upper = "{" + self.visit(node.left) + "}"
else:
reduced_constant = ast.Constant(node.right.value + 1)
new_node = ast.BinOp(node.left, node.op, reduced_constant)
upper = "{" + self.visit(new_node) + "}"
else:
upper = "{" + self.visit(node) + "}"

Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove this function.

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

Expand Down
52 changes: 51 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 ast_utils, exceptions, test_utils
from latexify.codegen import FunctionCodegen, function_codegen


def test_generic_visit() -> None:
class UnknownNode(ast.AST):
pass
Expand Down Expand Up @@ -915,6 +914,56 @@ def test_use_set_symbols_compare(code: str, latex: str) -> None:
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."

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

with pytest.raises(
exceptions.LatexifySyntaxError,
match=r"Multiple statements are not supported in Match nodes.",
):
FunctionCodegen().visit(tree)


@pytest.mark.parametrize(
"code,latex",
[
Expand Down Expand Up @@ -959,3 +1008,4 @@ def test_numpy_array(code: str, latex: str) -> None:
tree = ast_utils.parse_expr(code)
assert isinstance(tree, ast.Call)
assert function_codegen.FunctionCodegen().visit(tree) == latex