generated from rochacbruno/python-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7e5f8fa
commit 1818796
Showing
3 changed files
with
49 additions
and
2 deletions.
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import pylox.expr as ast | ||
|
||
|
||
# Define a visitor class for our syntax tree classes that takes an expression, | ||
# converts it to RPN, and returns the resulting string. | ||
class RpnAstPrinter(ast.ExprVisitor): | ||
|
||
def print_expr(self, expr: ast.Expr): | ||
return expr.accept(self) | ||
|
||
def visit_binary_expr(self, expr: ast.Binary): | ||
return f'{str(expr.left.accept(self))} {str(expr.right.accept(self))} {expr.operator.lexeme}' | ||
|
||
def visit_grouping_expr(self, expr: ast.Grouping): | ||
return expr.expr.accept(self) | ||
|
||
def visit_literal_expr(self, expr: ast.Literal): | ||
return str(expr.value) | ||
|
||
def visit_unary_expr(self, expr: ast.Unary): | ||
return f'{str(expr.right.accept(self))} {expr.operator.lexeme}' |
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
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