Skip to content

Commit

Permalink
[docs][examples] fix bugs and inconsistencies
Browse files Browse the repository at this point in the history
  • Loading branch information
apalala committed Aug 25, 2023
1 parent 9de37bf commit 50fb3b1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 40 deletions.
48 changes: 25 additions & 23 deletions docs/mini-tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -660,29 +660,31 @@ The following code generator translates input expressions to the postfix instruc
class PostfixCodeGenerator(NodeWalker, IndentPrintMixin):
def walk_Add(self, node: Node, *args, **kwargs):
self.walk(node.left)
self.walk(node.right)
print('ADD')
with self.indent():
self.walk(node.left) # type: ignore
self.walk(node.right) # type: ignore
self.print('ADD')
def walk_Subtract(self, node: Node, *args, **kwargs):
self.walk(node.left)
self.walk(node.right)
print('SUB')
with self.indent():
self.walk(node.left) # type: ignore
self.walk(node.right) # type: ignore
self.print('SUB')
def walk_Multiply(self, node: Node, *args, **kwargs):
self.walk(node.left)
self.walk(node.right)
print('MUL')
with self.indent():
self.walk(node.left) # type: ignore
self.walk(node.right) # type: ignore
self.print('MUL')
def walk_Divide(self, node: Node, *args, **kwargs):
self.walk(node.left)
self.walk(node.right)
print('DIV')
with self.indent():
self.walk(node.left) # type: ignore
self.walk(node.right) # type: ignore
self.print('DIV')
def walk_int(self, node:Node, *args, **kwargs):
print('PUSH', node)
def walk_int(self, node: Node, *args, **kwargs):
self.print('PUSH', node)
Expand All @@ -691,10 +693,10 @@ Save the above program in ``calc_translate.py`` and execute it to get this resul
.. code:: python
# TRANSLATED TO POSTFIX
PUSH 3
PUSH 5
PUSH 10
PUSH 20
SUB
MUL
ADD
PUSH 3
PUSH 5
PUSH 10
PUSH 20
SUB
MUL
ADD
10 changes: 6 additions & 4 deletions docs/print_translation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ and should be used thus:
class MyTranslationWalker(NodeWalker, IndentPrintMixin):
with self.indent*():
# ccontinue walking the tree
def walk_SomeNode(self, node):
with self.indent():
# ccontinue walking the tree
The ``self.print()`` method takes note of the current level of indentation, so
Expand All @@ -29,8 +30,9 @@ the ``IndentPrintConstructor``:

.. code:: python
with self.indent*():
self.prtin(walk_expression(node.exp))
def walk_SomeNode(self, node):
with self.indent():
self.print(walk_expression(node.exp))
The printed code can be retrieved using the ``printed_text()`` method. Other
posibilities are available by assigning a text-like object to
Expand Down
30 changes: 17 additions & 13 deletions examples/calc/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,28 @@
class PostfixCodeGenerator(NodeWalker, IndentPrintMixin):

def walk_Add(self, node: Node, *args, **kwargs):
self.walk(node.left) # type: ignore
self.walk(node.right) # type: ignore
print('ADD')
with self.indent():
self.walk(node.left) # type: ignore
self.walk(node.right) # type: ignore
self.print('ADD')

def walk_Subtract(self, node: Node, *args, **kwargs):
self.walk(node.left) # type: ignore
self.walk(node.right) # type: ignore
print('SUB')
with self.indent():
self.walk(node.left) # type: ignore
self.walk(node.right) # type: ignore
self.print('SUB')

def walk_Multiply(self, node: Node, *args, **kwargs):
self.walk(node.left) # type: ignore
self.walk(node.right) # type: ignore
print('MUL')
with self.indent():
self.walk(node.left) # type: ignore
self.walk(node.right) # type: ignore
self.print('MUL')

def walk_Divide(self, node: Node, *args, **kwargs):
self.walk(node.left) # type: ignore
self.walk(node.right) # type: ignore
print('DIV')
with self.indent():
self.walk(node.left) # type: ignore
self.walk(node.right) # type: ignore
self.print('DIV')

def walk_int(self, node: Node, *args, **kwargs):
print('PUSH', node)
self.print('PUSH', node)

0 comments on commit 50fb3b1

Please sign in to comment.