-
Notifications
You must be signed in to change notification settings - Fork 392
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
Changes from 4 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
77da54b
wrote a basic test that calls visit_Match
juliawgraham c213d0f
added extra space to publish branch
juliawgraham 73425a6
implementation for MatchValue
Yuqi07 80f9486
merged match_case into Match (with juliawgraham)
Yuqi07 590ee6c
1. removed unused i; 2. added error handling for multiple statements …
Yuqi07 c430573
Merge branch 'main' into matchvalue
juliawgraham 98db826
Removed unused _reduce_stop_parameter function
juliawgraham fab995c
updated unit test syntax
juliawgraham 2b5ad40
updated formatting using flake
juliawgraham c4a8410
updated unit test formatting after flake changes
juliawgraham 4ea4c3c
reformatted files using black
juliawgraham 6f320f0
Merge branch 'main' into matchvalue
2b2ae26
refactoring
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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): | ||||||
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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
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 | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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." | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.