-
Notifications
You must be signed in to change notification settings - Fork 398
MatchOr and guard statements #158
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
base: main
Are you sure you want to change the base?
Changes from 32 commits
77da54b
c213d0f
73425a6
adb9a50
f64490e
4aa1195
607c359
33f7439
a57b6a8
e1d90f9
64a281c
4ab48e8
2dbcf03
62a4633
fc0372a
ad11197
5b6e676
0a55289
f300ee7
c047696
3b78dd7
fc4eca9
1a1cefe
befd6dc
efd60f8
7b67262
33327a3
587c50b
ee82180
4db2356
ccb0c5d
2d5807e
fd2ddee
0daeb72
3cf0b0e
2019d4b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,7 +140,7 @@ def visit_If(self, node: ast.If) -> str: | |
return latex + r", & \mathrm{otherwise} \end{array} \right." | ||
|
||
def visit_Match(self, node: ast.Match) -> str: | ||
"""Visit a Match node""" | ||
"""Visit a Match node.""" | ||
if not ( | ||
len(node.cases) >= 2 | ||
and isinstance(node.cases[-1].pattern, ast.MatchAs) | ||
|
@@ -162,21 +162,47 @@ def visit_Match(self, node: ast.Match) -> str: | |
if i < len(node.cases) - 1: | ||
body_latex = self.visit(case.body[0]) | ||
cond_latex = self.visit(case.pattern) | ||
case_latexes.append( | ||
body_latex + r", & \mathrm{if} \ " + subject_latex + cond_latex | ||
) | ||
if case.guard is not None: | ||
cond_latex = self._expression_codegen.visit(case.guard) | ||
erica-w-fu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
case_latexes.append(body_latex + r", & \mathrm{if} \ " + cond_latex) | ||
else: | ||
case_latexes.append( | ||
self.visit(node.cases[-1].body[0]) + r", & \mathrm{otherwise}" | ||
self.visit(case.body[0]) + r", & \mathrm{otherwise}" | ||
) | ||
|
||
return ( | ||
r"\left\{ \begin{array}{ll} " | ||
latex = ( | ||
r"\left\{ \begin{array}{ll}" | ||
erica-w-fu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
+ r" \\ ".join(case_latexes) | ||
+ r" \end{array} \right." | ||
) | ||
|
||
latex_final = latex.replace("subject_name", subject_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. Weird hack, might cause issues if there's an actual 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. Yes that is something we considered as well and would love to hear suggestions. Since each of the visit_Match... functions cannot access the subject_latex variable(each of those functions can only have the ast node and pattern as the input), we had to find a workaround. We tried to use python format and % to insert the subject_latex but the use of % and {} in latex made this very difficult. Do you have any suggestions for how to better implement this (or possible a better name than "subject_name" to replace) 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. Ah yeah I see the dilemma, bit perplexing lol. Not entirely sure how to solve but I'll give it some thought. Thanks for tackling 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. Not sure what @odashi thinks of this solution, but you could create a stack of subject_latex = self._expression_codegen.visit(node.subject)
self._match_subject_stack.append(subject_latex) and then right before you return do something like latex = (
r"\left\{ \begin{array}{ll} "
+ r" \\ ".join(case_latexes)
+ r" \end{array} \right."
)
self._match_subject_stack.pop()
return latex and you would also have def visit_MatchValue(self, node: ast.MatchValue) -> str:
"""Visit a MatchValue node."""
latex = self._expression_codegen.visit(node.value)
return self._match_subject_stack[-1] + " = " + latex A stack is only required if nested match statements is supported, which it seems like it is, although I have no idea if it's valid edit: 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. I think the @ZibingZhang 's solution is desirable at this point. One minor point is that the private member |
||
return latex_final | ||
|
||
def visit_MatchValue(self, node: ast.MatchValue) -> str: | ||
"""Visit a MatchValue node""" | ||
"""Visit a MatchValue node.""" | ||
latex = self._expression_codegen.visit(node.value) | ||
return " = " + latex | ||
return "subject_name = " + latex | ||
|
||
def visit_MatchAs(self, node: ast.MatchAs) -> str: | ||
""" | ||
Visit a MatchAs node. | ||
Lucybean-hi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
If MatchAs is a wildcard, return 'otherwise' case, otherwise throw an error. | ||
""" | ||
if node.pattern is None: | ||
return "" | ||
else: | ||
raise exceptions.LatexifySyntaxError( | ||
"Nonempty as-patterns are not supported in MatchAs nodes." | ||
) | ||
|
||
erica-w-fu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def visit_MatchOr(self, node: ast.MatchOr) -> str: | ||
"""Visit a MatchOr node.""" | ||
case_latexes = [] | ||
for i, pattern in enumerate(node.patterns): | ||
if i == 0: | ||
case_latexes.append(self.visit(pattern)) | ||
else: | ||
case_latexes.append(r" \lor " + self.visit(pattern)) | ||
return "".join(case_latexes) | ||
erica-w-fu marked this conversation as resolved.
Show resolved
Hide resolved
|
Uh oh!
There was an error while loading. Please reload this page.