Removed more extraneous slashes and fixed the formatting on the mandelbrot example in docs.

This commit is contained in:
Maggie Mari 2012-08-19 12:28:55 -05:00
commit ddcf6483d0
6 changed files with 166 additions and 188 deletions

View file

@ -413,8 +413,8 @@ it returns the expression that is passed into it. In our example above,
the code passes the expression for ``a`` into ``ParseBinOpRHS`` and the the code passes the expression for ``a`` into ``ParseBinOpRHS`` and the
current token is ``+``. current token is ``+``.
The precedence value passed into ``ParseBinOpRHS`` indicates the \* The precedence value passed into ``ParseBinOpRHS`` indicates the *
minimal operator precedence \* that the function is allowed to eat. For minimal operator precedence * that the function is allowed to eat. For
example, if the current pair stream is ``[+, x]`` and ``ParseBinOpRHS`` example, if the current pair stream is ``[+, x]`` and ``ParseBinOpRHS``
is passed in a precedence of 40, it will not consume any tokens (because is passed in a precedence of 40, it will not consume any tokens (because
the precedence of '+' is only 20). With this in mind, ``ParseBinOpRHS`` the precedence of '+' is only 20). With this in mind, ``ParseBinOpRHS``

View file

@ -983,7 +983,7 @@ Parser
left = self.ParsePrimary() left = self.ParsePrimary()
return self.ParseBinOpRHS(left, 0) return self.ParseBinOpRHS(left, 0)
# prototype ::= id '(' id\* ')' # prototype ::= id '(' id* ')'
def ParsePrototype(self): def ParsePrototype(self):
if not isinstance(self.current, IdentifierToken): if not isinstance(self.current, IdentifierToken):
raise RuntimeError('Expected function name in prototype.') raise RuntimeError('Expected function name in prototype.')

View file

@ -906,7 +906,7 @@ Main driver code.
'<': 10, '<': 10,
'+': 20, '+': 20,
'-': 20, '-': 20,
'\*': 40 '*': 40
} }
# Run the main "interpreter loop". # Run the main "interpreter loop".

View file

@ -1384,7 +1384,7 @@ Parser
self.current == CharacterToken('('): return self.ParseParenExpr() else: self.current == CharacterToken('('): return self.ParseParenExpr() else:
raise RuntimeError('Unknown token when expecting an expression.') raise RuntimeError('Unknown token when expecting an expression.')
# binoprhs ::= (operator primary)\* def ParseBinOpRHS(self, left, # binoprhs ::= (operator primary)* def ParseBinOpRHS(self, left,
left_precedence): # If this is a binary operator, find its precedence. left_precedence): # If this is a binary operator, find its precedence.
while True: precedence = self.GetCurrentTokenPrecedence() while True: precedence = self.GetCurrentTokenPrecedence()
@ -1413,7 +1413,7 @@ Parser
# expression ::= primary binoprhs def ParseExpression(self): left = # expression ::= primary binoprhs def ParseExpression(self): left =
self.ParsePrimary() return self.ParseBinOpRHS(left, 0) self.ParsePrimary() return self.ParseBinOpRHS(left, 0)
# prototype ::= id '(' id\* ')' def ParsePrototype(self): if not # prototype ::= id '(' id* ')' def ParsePrototype(self): if not
isinstance(self.current, IdentifierToken): raise RuntimeError('Expected isinstance(self.current, IdentifierToken): raise RuntimeError('Expected
function name in prototype.') function name in prototype.')
@ -1481,7 +1481,7 @@ Parser
# Install standard binary operators. # 1 is lowest possible precedence. # Install standard binary operators. # 1 is lowest possible precedence.
40 is the highest. operator_precedence = { '<': 10, '+': 20, '-': 20, 40 is the highest. operator_precedence = { '<': 10, '+': 20, '-': 20,
'\*': 40 } '*': 40 }
# Run the main "interpreter loop". while True: print 'ready>', try: raw # Run the main "interpreter loop". while True: print 'ready>', try: raw
= raw_input() except KeyboardInterrupt: break = raw_input() except KeyboardInterrupt: break

View file

@ -134,7 +134,7 @@ user-defined operator, we need to parse it:
.. code-block:: python .. code-block:: python
# prototype # ::= id '(' id\* ')' # ::= binary # prototype # ::= id '(' id* ')' # ::= binary
LETTER number? (id, id) # ::= unary LETTER (id) def LETTER number? (id, id) # ::= unary LETTER (id) def
ParsePrototype(self): precedence = None if isinstance(self.current, ParsePrototype(self): precedence = None if isinstance(self.current,
IdentifierToken): kind = 'normal' function_name = self.current.name IdentifierToken): kind = 'normal' function_name = self.current.name
@ -250,7 +250,7 @@ whenever we define a new binary operator:
... def main(): ... g_binop_precedence['<'] = 10 ... def main(): ... g_binop_precedence['<'] = 10
g_binop_precedence['+'] = 20 g_binop_precedence['-'] = 20 g_binop_precedence['+'] = 20 g_binop_precedence['-'] = 20
g_binop_precedence['\*'] = 40 ... g_binop_precedence['*'] = 40 ...
@ -325,7 +325,7 @@ call ParseUnary instead:
.. code-block:: python .. code-block:: python
# binoprhs ::= (binary_operator unary)\* def # binoprhs ::= (binary_operator unary)* def
ParseBinOpRHS(self, left, left_precedence): ... # Parse the unary ParseBinOpRHS(self, left, left_precedence): ... # Parse the unary
expression after the binary operator. right = self.ParseUnary() ... expression after the binary operator. right = self.ParseUnary() ...
@ -342,7 +342,7 @@ operator code above with:
.. code-block:: python .. code-block:: python
# prototype # ::= id '(' id\* ')' # ::= binary # prototype # ::= id '(' id* ')' # ::= binary
LETTER number? (id, id) # ::= unary LETTER (id) def LETTER number? (id, id) # ::= unary LETTER (id) def
ParsePrototype(self): precedence = None if isinstance(self.current, ParsePrototype(self): precedence = None if isinstance(self.current,
IdentifierToken): ... elif isinstance(self.current, UnaryToken): kind = IdentifierToken): ... elif isinstance(self.current, UnaryToken): kind =
@ -497,157 +497,135 @@ Given this, we can try plotting out the mandlebrot set! Lets try it out:
.. code-block:: bash .. code-block:: bash
ready> mandel(-2.3, -1.3, 0.05, 0.07) ready> mandel(-2.3, -1.3, 0.05, 0.07)
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* *******************************************************************************
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* *******************************************************************************
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ****************************************++++++*********************************
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*+++++...++++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ************************************+++++...++++++*****************************
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++++.. *********************************++++++++.. ...+++++***************************
...+++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* *******************************++++++++++.. ..+++++**************************
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++++++.. ******************************++++++++++. ..++++++*************************
..+++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ****************************+++++++++.... ..++++++************************
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++++++. **************************++++++++....... .....++++***********************
..++++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* *************************++++++++. . ... .++**********************
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*+++++++++.... ***********************++++++++... ++**********************
..++++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* *********************+++++++++.... .+++*********************
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++++....... ******************+++..+++++.... ..+++********************
.....++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* **************++++++. .......... +++********************
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++++. . ... ***********++++++++.. .. .++********************
.++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* *********++++++++++... .++++*******************
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++++... ********++++++++++.. .++++*******************
++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* *******++++++..... ..++++*******************
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*+++++++++.... *******+........ ...++++*******************
.+++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* *******+... .... ...++++*******************
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*+++..+++++.... *******+++++...... ..++++*******************
..+++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* *******++++++++++... .++++*******************
\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++. .......... *********++++++++++... ++++*******************
+++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* **********+++++++++.. .. ..++********************
\*\*\*\*\*\*\*\*\*\*\*++++++++.. .. *************++++++.. .......... +++********************
.++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ******************+++...+++..... ..+++********************
\*\*\*\*\*\*\*\*\*++++++++++... *********************+++++++++.... ..++*********************
.++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* \*\*\*\*\*\*\*\*++++++++++.. ***********************++++++++... +++*********************
.++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* \*\*\*\*\*\*\*++++++..... *************************+++++++.. . ... .++**********************
..++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* \*\*\*\*\*\*\*+........ **************************++++++++....... ......+++***********************
...++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* \*\*\*\*\*\*\*+... .... ****************************+++++++++.... ..++++++************************
...++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* \*\*\*\*\*\*\*+++++...... *****************************++++++++++.. ..++++++*************************
..++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* \*\*\*\*\*\*\*++++++++++... *******************************++++++++++.. ...+++++**************************
.++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* *********************************++++++++.. ...+++++***************************
\*\*\*\*\*\*\*\*\*++++++++++... ***********************************++++++....+++++*****************************
++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ***************************************++++++++********************************
\*\*\*\*\*\*\*\*\*\*+++++++++.. .. *******************************************************************************
..++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* *******************************************************************************
\*\*\*\*\*\*\*\*\*\*\*\*\*++++++.. .......... *******************************************************************************
+++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* *******************************************************************************
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*+++...+++..... *******************************************************************************
..+++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* Evaluated to 0.0
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*+++++++++.... ready> mandel(-2, -1, 0.02, 0.04)
..++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ******************************************************************+++++++++++++
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++++... ****************************************************************+++++++++++++++
+++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* *************************************************************++++++++++++++++++
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*+++++++.. . ... ***********************************************************++++++++++++++++++++
.++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ********************************************************+++++++++++++++++++++++
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++++....... ******************************************************++++++++++++++++++++++...
......+++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ***************************************************+++++++++++++++++++++.......
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*+++++++++.... *************************************************++++++++++++++++++++..........
..++++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ***********************************************+++++++++++++++++++... ...
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++++++.. ********************************************++++++++++++++++++++......
..++++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ******************************************++++++++++++++++++++.......
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++++++.. ***************************************+++++++++++++++++++++..........
...+++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ************************************++++++++++++++++++++++...........
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++++.. ********************************++++++++++++++++++++++++.........
...+++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ***************************++++++++...........+++++..............
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++....+++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* *********************++++++++++++.... .........................
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ***************+++++++++++++++++.... ......... ............
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ***********+++++++++++++++++++++..... ......
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ********+++++++++++++++++++++++.......
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ******+++++++++++++++++++++++++........
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ****+++++++++++++++++++++++++.......
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ***+++++++++++++++++++++++.........
Evaluated to 0.0 ready> mandel(-2, -1, 0.02, 0.04) **++++++++++++++++...........
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*+++++++++++++ *++++++++++++................
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*+++++++++++++++ *++++....................
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++++++++++++++
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++++++++++++++++ *++++....................
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*+++++++++++++++++++++++ *++++++++++++................
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++++++++++++++++++... **++++++++++++++++...........
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*+++++++++++++++++++++....... ***+++++++++++++++++++++++.........
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++++++++++++++++.......... ****+++++++++++++++++++++++++.......
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*+++++++++++++++++++... ******+++++++++++++++++++++++++........
... ********+++++++++++++++++++++++.......
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++++++++++++++++...... ***********+++++++++++++++++++++..... ......
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++++++++++++++++....... ***************+++++++++++++++++.... ......... ............
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*+++++++++++++++++++++.......... *********************++++++++++++.... .........................
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++++++++++++++++++........... ***************************++++++++...........+++++..............
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++++++++++++++++++++......... ********************************++++++++++++++++++++++++.........
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++++...........+++++.............. ************************************++++++++++++++++++++++...........
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++++++++.... ***************************************+++++++++++++++++++++..........
......................... ******************************************++++++++++++++++++++.......
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*+++++++++++++++++.... ......... Evaluated to: 0.0
............ \*\*\*\*\*\*\*\*\*\*\*+++++++++++++++++++++..... ...... ready> mandel(-0.9, -1.4, 0.02, 0.03)
\*\*\*\*\*\*\*\*+++++++++++++++++++++++....... *******************************************************************************
\*\*\*\*\*\*+++++++++++++++++++++++++........ *******************************************************************************
\*\*\*\*+++++++++++++++++++++++++....... *******************************************************************************
***+++++++++++++++++++++++.........**\ ++++++++++++++++...........*\ ++++++++++++................ *******************************************************************************
\*++++.................... *******************************************************************************
*******************************************************************************
*++++....................*\ ++++++++++++................ *******************************************************************************
**++++++++++++++++...........**\ *+++++++++++++++++++++++......... *******************************************************************************
\*\*\*\*+++++++++++++++++++++++++....... ****************************+++++++++++++++++**********************************
\*\*\*\*\*\*+++++++++++++++++++++++++........ ***********************+++++++++++...++++++++++++******************************
\*\*\*\*\*\*\*\*+++++++++++++++++++++++....... ********************+++++++++++++.. . .++++++++++++++**************************
\*\*\*\*\*\*\*\*\*\*\*+++++++++++++++++++++..... ...... *****************++++++++++++++++... ......++++++++++++************************
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*+++++++++++++++++.... ......... **************+++++++++++++++++++... .......+++++++++++**********************
............ \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++++++++.... ************++++++++++++++++++++.... .... ..++++++++++++********************
......................... **********++++++++++++++++++++++...... ...++++++++++++*******************
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++++...........+++++.............. ********+++++++++++++++++++++++....... .....++++++++++++++*****************
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++++++++++++++++++++......... ******++++++++++++++++++++++++....... .....+++++++++++++++****************
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++++++++++++++++++........... ****+++++++++++++++++++++++++.... . .....+++++++++++++++***************
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*+++++++++++++++++++++.......... **+++++++++++++++++++++++++.... ...++++++++++++++++*************
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++++++++++++++++....... *+++++++++++++++++++++++....... ....++++++++++++++++************
Evaluated to: 0.0 ready> mandel(-0.9, -1.4, 0.02, 0.03) +++++++++++++++++++++.......... .....++++++++++++++++***********
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ++++++++++++++++++............. .......+++++++++++++++**********
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* +++++++++++++++................ ............++++++++++**********
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* +++++++++++++................. .................+++++*********
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* +++++++++++... .... .......... .+++++********
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ++++++++++..... ........ ...+++++*******
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ++++++++...... ..++++++******
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* +++++++........ ..+++++******
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* +++++.......... ..++++++*****
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*+++++++++++++++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ++++.......... ....++++++*****
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*+++++++++++...++++++++++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ++.......... ....+++++++****
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*+++++++++++++.. . .......... ......+++++++***
.++++++++++++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* .......... .....+++++++***
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++++++++++++++++... .......... .....++++++***
......++++++++++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ......... .+++++++**
\*\*\*\*\*\*\*\*\*\*\*\*\*\*+++++++++++++++++++... ........ .+++++++**
.......+++++++++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ...... ...+++++++*
\*\*\*\*\*\*\*\*\*\*\*\*++++++++++++++++++++.... .... . ....++++++++*
..++++++++++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ...++++++++*
\*\*\*\*\*\*\*\*\*\*++++++++++++++++++++++...... ..+++++++++
...++++++++++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ..+++++++++
\*\*\*\*\*\*\*\*+++++++++++++++++++++++....... Evaluated to: 0.0
.....++++++++++++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ready> ^C
\*\*\*\*\*\*++++++++++++++++++++++++.......
.....+++++++++++++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
\*\*\*\*+++++++++++++++++++++++++.... .
.....+++++++++++++++\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
**+++++++++++++++++++++++++....
...++++++++++++++++**\ \*\*\*\*\*\*\*\*\*\*\**\ +++++++++++++++++++++++.......
....++++++++++++++++\*\*\*\*\*\*\*\*\*\*\*\*
+++++++++++++++++++++..........
.....++++++++++++++++\*\*\*\*\*\*\*\*\*\*\*
++++++++++++++++++.............
.......+++++++++++++++\*\*\*\*\*\*\*\*\*\*
+++++++++++++++................
............++++++++++\*\*\*\*\*\*\*\*\*\*
+++++++++++++................. .................+++++\*\*\*\*\*\*\*\*\*
+++++++++++... .... .......... .+++++\*\*\*\*\*\*\*\* ++++++++++.....
........ ...+++++\*\*\*\*\*\*\* ++++++++...... ..++++++\*\*\*\*\*\*
+++++++........ ..+++++\*\*\*\*\*\* +++++.......... ..++++++\*\*\*\*\*
++++.......... ....++++++\*\*\*\*\* ++.......... ....+++++++\*\*\*\*
.......... ......+++++++\ **\* .......... .....+++++++**\ \* ..........
.....++++++\ **\* ......... .+++++++** ........ .+++++++\ *\* ......
...+++++++* . ....++++++++\* ...++++++++\* ..+++++++++ ..+++++++++
Evaluated to: 0.0 ready> ^C
@ -1007,7 +985,7 @@ the if/then/else and for expressions:
def GetOperatorName(self): assert self.is_operator return self.name[-1] def GetOperatorName(self): assert self.is_operator return self.name[-1]
def CodeGen(self): # Make the function type, eg. double(double,double). def CodeGen(self): # Make the function type, eg. double(double,double).
funct_type = Type.function( Type.double(), [Type.double()] \* funct_type = Type.function( Type.double(), [Type.double()] *
len(self.args), False) len(self.args), False)
:: ::
@ -1095,7 +1073,7 @@ the if/then/else and for expressions:
isinstance(self.current, CharacterToken): return isinstance(self.current, CharacterToken): return
g_binop_precedence.get(self.current.char, -1) else: return -1 g_binop_precedence.get(self.current.char, -1) else: return -1
# identifierexpr ::= identifier | identifier '(' expression\* ')' def # identifierexpr ::= identifier | identifier '(' expression* ')' def
ParseIdentifierExpr(self): identifier_name = self.current.name ParseIdentifierExpr(self): identifier_name = self.current.name
self.Next() # eat identifier. self.Next() # eat identifier.
@ -1217,7 +1195,7 @@ the if/then/else and for expressions:
self.Next() # eat the operator. self.Next() # eat the operator.
return UnaryExpressionNode(operator, self.ParseUnary()) return UnaryExpressionNode(operator, self.ParseUnary())
# binoprhs ::= (binary_operator unary)\* def ParseBinOpRHS(self, left, # binoprhs ::= (binary_operator unary)* def ParseBinOpRHS(self, left,
left_precedence): # If this is a binary operator, find its precedence. left_precedence): # If this is a binary operator, find its precedence.
while True: precedence = self.GetCurrentTokenPrecedence() while True: precedence = self.GetCurrentTokenPrecedence()
@ -1246,7 +1224,7 @@ the if/then/else and for expressions:
# expression ::= unary binoprhs def ParseExpression(self): left = # expression ::= unary binoprhs def ParseExpression(self): left =
self.ParseUnary() return self.ParseBinOpRHS(left, 0) self.ParseUnary() return self.ParseBinOpRHS(left, 0)
# prototype # ::= id '(' id\* ')' # ::= binary LETTER number? (id, id) # # prototype # ::= id '(' id* ')' # ::= binary LETTER number? (id, id) #
::= unary LETTER (id) def ParsePrototype(self): precedence = None if ::= unary LETTER (id) def ParsePrototype(self): precedence = None if
isinstance(self.current, IdentifierToken): kind = 'normal' isinstance(self.current, IdentifierToken): kind = 'normal'
function_name = self.current.name self.Next() # eat function name. elif function_name = self.current.name self.Next() # eat function name. elif
@ -1331,7 +1309,7 @@ the if/then/else and for expressions:
# Install standard binary operators. # 1 is lowest possible precedence. # Install standard binary operators. # 1 is lowest possible precedence.
40 is the highest. g_binop_precedence['<'] = 10 40 is the highest. g_binop_precedence['<'] = 10
g_binop_precedence['+'] = 20 g_binop_precedence['-'] = 20 g_binop_precedence['+'] = 20 g_binop_precedence['-'] = 20
g_binop_precedence['\*'] = 40 g_binop_precedence['*'] = 40
# Run the main "interpreter loop". while True: print 'ready>', try: raw # Run the main "interpreter loop". while True: print 'ready>', try: raw
= raw_input() except KeyboardInterrupt: break = raw_input() except KeyboardInterrupt: break

View file

@ -53,11 +53,11 @@ two values. The LLVM IR that we want for this example looks like this:
.. code-block:: llvm .. code-block:: llvm
@G = weak global i32 0 ; type of @G is i32\* @H = @G = weak global i32 0 ; type of @G is i32* @H =
weak global i32 0 ; type of @H is i32\* define i32 @test(i1 %Condition) weak global i32 0 ; type of @H is i32* define i32 @test(i1 %Condition)
{ entry: br i1 %Condition, label %cond_true, label %cond_false { entry: br i1 %Condition, label %cond_true, label %cond_false
cond_true: %X.0 = load i32\* @G br label %cond_next cond_false: %X.1 cond_true: %X.0 = load i32* @G br label %cond_next cond_false: %X.1
= load i32\* @H br label %cond_next cond_next: %X.2 = phi i32 [ %X.1, = load i32* @H br label %cond_next cond_next: %X.2 = phi i32 [ %X.1,
%cond_false ], [ %X.0, %cond_true ] ret i32 %X.2 } %cond_false ], [ %X.0, %cond_true ] ret i32 %X.2 }
@ -102,7 +102,7 @@ In LLVM, all memory accesses are explicit with load/store instructions,
and it is carefully designed not to have (or need) an "address-of" and it is carefully designed not to have (or need) an "address-of"
operator. Notice how the type of the @G/@H global variables is actually operator. Notice how the type of the @G/@H global variables is actually
"i32\ *" even though the variable is defined as "i32". What this means "i32\ *" even though the variable is defined as "i32". What this means
is that @G defines*\ space\* for an i32 in the global data area, but its is that @G defines*\ space* for an i32 in the global data area, but its
*name* actually refers to the address for that space. Stack variables *name* actually refers to the address for that space. Stack variables
work the same way, except that instead of being declared with global work the same way, except that instead of being declared with global
variable definitions, they are declared with the `LLVM alloca variable definitions, they are declared with the `LLVM alloca
@ -114,7 +114,7 @@ instruction <http://www.llvm.org/docs/LangRef.html#i_alloca>`_:
define i32 @example() { entry: %X = alloca i32 ; define i32 @example() { entry: %X = alloca i32 ;
type of %X is i32\ *. ... %tmp = load i32* %X ; load the stack value %X type of %X is i32\ *. ... %tmp = load i32* %X ; load the stack value %X
from the stack. %tmp2 = add i32 %tmp, 1 ; increment it store i32 %tmp2, from the stack. %tmp2 = add i32 %tmp, 1 ; increment it store i32 %tmp2,
i32\* %X ; store it back ... i32* %X ; store it back ...
@ -128,13 +128,13 @@ using a PHI node:
.. code-block:: llvm .. code-block:: llvm
@G = weak global i32 0 ; type of @G is i32\* @H = @G = weak global i32 0 ; type of @G is i32* @H =
weak global i32 0 ; type of @H is i32\* define i32 @test(i1 %Condition) weak global i32 0 ; type of @H is i32* define i32 @test(i1 %Condition)
{ entry: %X = alloca i32 ; type of %X is i32\ *. br i1 %Condition, label { entry: %X = alloca i32 ; type of %X is i32\ *. br i1 %Condition, label
%cond_true, label %cond_false cond_true: %X.0 = load i32* @G store %cond_true, label %cond_false cond_true: %X.0 = load i32* @G store
i32 %X.0, i32\* %X ; Update X br label %cond_next cond_false: %X.1 = i32 %X.0, i32* %X ; Update X br label %cond_next cond_false: %X.1 =
load i32\* @H store i32 %X.1, i32\* %X ; Update X br label %cond_next load i32* @H store i32 %X.1, i32* %X ; Update X br label %cond_next
cond_next: %X.2 = load i32\* %X ; Read X ret i32 %X.2 } {% endhighlight cond_next: %X.2 = load i32* %X ; Read X ret i32 %X.2 } {% endhighlight
%} %}
With this, we have discovered a way to handle arbitrary mutable With this, we have discovered a way to handle arbitrary mutable
@ -191,8 +191,8 @@ using a PHI node:
@G = weak global i32 0 @H = weak global i32 0 @G = weak global i32 0 @H = weak global i32 0
define i32 @test(i1 %Condition) { entry: br i1 %Condition, label define i32 @test(i1 %Condition) { entry: br i1 %Condition, label
%cond_true, label %cond_false cond_true: %X.0 = load i32\* @G br %cond_true, label %cond_false cond_true: %X.0 = load i32* @G br
label %cond_next cond_false: %X.1 = load i32\* @H br label %cond_next label %cond_next cond_false: %X.1 = load i32* @H br label %cond_next
cond_next: %X.01 = phi i32 [ %X.1, %cond_false ], [ %X.0, %cond_true cond_next: %X.01 = phi i32 [ %X.1, %cond_false ], [ %X.0, %cond_true
] ret i32 %X.01 } ] ret i32 %X.01 }
@ -437,12 +437,12 @@ get good codegen once again:
for our recursive fib function. Before the optimization: for our recursive fib function. Before the optimization:
define double @fib(double %x) { entry: %x1 = alloca define double @fib(double %x) { entry: %x1 = alloca
double store double %x, double\* %x1 %x2 = load double\* %x1 %cmptmp = double store double %x, double* %x1 %x2 = load double* %x1 %cmptmp =
fcmp ult double %x2, 3.000000e+00 %booltmp = uitofp i1 %cmptmp to double fcmp ult double %x2, 3.000000e+00 %booltmp = uitofp i1 %cmptmp to double
%ifcond = fcmp one double %booltmp, 0.000000e+00 br i1 %ifcond, label %ifcond = fcmp one double %booltmp, 0.000000e+00 br i1 %ifcond, label
%then, label %else then: ; preds = %entry br label %ifcont else: ; preds %then, label %else then: ; preds = %entry br label %ifcont else: ; preds
= %entry %x3 = load double\* %x1 %subtmp = fsub double %x3, 1.000000e+00 = %entry %x3 = load double* %x1 %subtmp = fsub double %x3, 1.000000e+00
%calltmp = call double @fib(double %subtmp) %x4 = load double\* %x1 %calltmp = call double @fib(double %subtmp) %x4 = load double* %x1
%subtmp5 = fsub double %x4, 2.000000e+00 %calltmp6 = call double %subtmp5 = fsub double %x4, 2.000000e+00 %calltmp6 = call double
@fib(double %subtmp5) %addtmp = fadd double %calltmp, %calltmp6 br label @fib(double %subtmp5) %addtmp = fadd double %calltmp, %calltmp6 br label
%ifcont ifcont: ; preds = %else, %then %iftmp = phi double [ %ifcont ifcont: ; preds = %else, %then %iftmp = phi double [
@ -1208,7 +1208,7 @@ mutable variables and var/in support:
def GetOperatorName(self): assert self.is_operator return self.name[-1] def GetOperatorName(self): assert self.is_operator return self.name[-1]
def CodeGen(self): # Make the function type, eg. double(double,double). def CodeGen(self): # Make the function type, eg. double(double,double).
funct_type = Type.function( Type.double(), [Type.double()] \* funct_type = Type.function( Type.double(), [Type.double()] *
len(self.args), False) len(self.args), False)
:: ::
@ -1304,7 +1304,7 @@ mutable variables and var/in support:
isinstance(self.current, CharacterToken): return isinstance(self.current, CharacterToken): return
g_binop_precedence.get(self.current.char, -1) else: return -1 g_binop_precedence.get(self.current.char, -1) else: return -1
# identifierexpr ::= identifier | identifier '(' expression\* ')' def # identifierexpr ::= identifier | identifier '(' expression* ')' def
ParseIdentifierExpr(self): identifier_name = self.current.name ParseIdentifierExpr(self): identifier_name = self.current.name
self.Next() # eat identifier. self.Next() # eat identifier.
@ -1466,7 +1466,7 @@ mutable variables and var/in support:
self.Next() # eat the operator. self.Next() # eat the operator.
return UnaryExpressionNode(operator, self.ParseUnary()) return UnaryExpressionNode(operator, self.ParseUnary())
# binoprhs ::= (binary_operator unary)\* def ParseBinOpRHS(self, left, # binoprhs ::= (binary_operator unary)* def ParseBinOpRHS(self, left,
left_precedence): # If this is a binary operator, find its precedence. left_precedence): # If this is a binary operator, find its precedence.
while True: precedence = self.GetCurrentTokenPrecedence() while True: precedence = self.GetCurrentTokenPrecedence()
@ -1495,7 +1495,7 @@ mutable variables and var/in support:
# expression ::= unary binoprhs def ParseExpression(self): left = # expression ::= unary binoprhs def ParseExpression(self): left =
self.ParseUnary() return self.ParseBinOpRHS(left, 0) self.ParseUnary() return self.ParseBinOpRHS(left, 0)
# prototype # ::= id '(' id\* ')' # ::= binary LETTER number? (id, id) # # prototype # ::= id '(' id* ')' # ::= binary LETTER number? (id, id) #
::= unary LETTER (id) def ParsePrototype(self): precedence = None if ::= unary LETTER (id) def ParsePrototype(self): precedence = None if
isinstance(self.current, IdentifierToken): kind = 'normal' isinstance(self.current, IdentifierToken): kind = 'normal'
function_name = self.current.name self.Next() # eat function name. elif function_name = self.current.name self.Next() # eat function name. elif
@ -1582,7 +1582,7 @@ mutable variables and var/in support:
# Install standard binary operators. # 1 is lowest possible precedence. # Install standard binary operators. # 1 is lowest possible precedence.
40 is the highest. g_binop_precedence['='] = 2 40 is the highest. g_binop_precedence['='] = 2
g_binop_precedence['<'] = 10 g_binop_precedence['+'] = 20 g_binop_precedence['<'] = 10 g_binop_precedence['+'] = 20
g_binop_precedence['-'] = 20 g_binop_precedence['\*'] = 40 g_binop_precedence['-'] = 20 g_binop_precedence['*'] = 40
# Run the main "interpreter loop". while True: print 'ready<', try: raw # Run the main "interpreter loop". while True: print 'ready<', try: raw
= raw_input() except KeyboardInterrupt: break = raw_input() except KeyboardInterrupt: break