Misc doc cleanup

This commit is contained in:
Maggie Mari 2012-08-17 14:34:51 -05:00
commit 8e00cd7e66
3 changed files with 154 additions and 145 deletions

View file

@ -160,7 +160,7 @@ The Lexer
When it comes to implementing a language, the first thing needed is the When it comes to implementing a language, the first thing needed is the
ability to process a text file and recognize what it says. The ability to process a text file and recognize what it says. The
traditional way to do this is to use a traditional way to do this is to use a
`lexer <http://en.wikipedia.org/wiki/Lexical_analysis>`_" (aka `lexer <http://en.wikipedia.org/wiki/Lexical_analysis>`_ (aka
'scanner') to break the input up into "tokens". Each token returned by 'scanner') to break the input up into "tokens". Each token returned by
the lexer includes a token type and potentially some metadata (e.g. the the lexer includes a token type and potentially some metadata (e.g. the
numeric value of a number). First, we define the possibilities: numeric value of a number). First, we define the possibilities:
@ -243,10 +243,6 @@ ignoring whitespace between tokens:
string = string[1:] string = string[1:]
continue continue
::
...

View file

@ -442,7 +442,6 @@ at this point. We'll fix this in `Chapter 5 <PythonLangImpl5.html>`_ :).
Once the insertion point is set up, we call the ``CodeGen`` method for Once the insertion point is set up, we call the ``CodeGen`` method for
the root expression of the function. If no error happens, this emits the root expression of the function. If no error happens, this emits
code to compute the expression into the entry block and returns the code to compute the expression into the entry block and returns the
@ -645,6 +644,8 @@ need to `download <../download.html>`_ and
Globals Globals
------- -------
.. code-block:: python
# The LLVM module, which holds all the IR code. # The LLVM module, which holds all the IR code.
g_llvm_module = Module.new('my cool jit') g_llvm_module = Module.new('my cool jit')
@ -658,6 +659,8 @@ need to `download <../download.html>`_ and
Lexer Lexer
----- -----
.. code-block:: python
# The lexer yields one of these types for each token. # The lexer yields one of these types for each token.
class EOFToken(object): class EOFToken(object):
pass pass
@ -678,7 +681,8 @@ need to `download <../download.html>`_ and
class CharacterToken(object): class CharacterToken(object):
def __init__(self, char): def __init__(self, char):
self.char = char def __eq__(self, other): self.char = char
def __eq__(self, other):
return isinstance(other, CharacterToken)and self.char == other.char return isinstance(other, CharacterToken)and self.char == other.char
def __ne__(self, other): def __ne__(self, other):
return not self == other return not self == other
@ -728,6 +732,8 @@ need to `download <../download.html>`_ and
Abstract Syntax Tree (aka Parse Tree) Abstract Syntax Tree (aka Parse Tree)
------------------------------------- -------------------------------------
.. code-block:: python
# Base class for all expression nodes. # Base class for all expression nodes.
class ExpressionNode(object): class ExpressionNode(object):
pass pass
@ -756,8 +762,10 @@ need to `download <../download.html>`_ and
# Expression class for a binary operator. # Expression class for a binary operator.
class BinaryOperatorExpressionNode(ExpressionNode): class BinaryOperatorExpressionNode(ExpressionNode):
def __init__(self, operator, left, right): self.operator = operator def __init__(self, operator, left, right):
self.left = left self.right = right self.operator = operator
self.left = left
self.right = right
def CodeGen(self): def CodeGen(self):
left = self.left.CodeGen() left = self.left.CodeGen()
@ -869,6 +877,8 @@ need to `download <../download.html>`_ and
Parser Parser
------ ------
.. code-block:: python
class Parser(object): class Parser(object):
def __init__(self, tokens, binop_precedence): def __init__(self, tokens, binop_precedence):
@ -877,13 +887,13 @@ need to `download <../download.html>`_ and
self.Next() self.Next()
# Provide a simple token buffer. Parser.current is the current token the # Provide a simple token buffer. Parser.current is the current token the
# parser is looking at. Parser.Next() reads another token from the lexer # parser is looking at. Parser.Next() reads another token from the lexer and
and # updates Parser.current with its results. # updates Parser.current with its results.
def Next(self): def Next(self):
self.current = self.tokens.next() self.current = self.tokens.next()
# Gets the precedence of the current token, or -1 if the token is not a # Gets the precedence of the current token, or -1 if the token is not a binary
binary # operator. # operator.
def GetCurrentTokenPrecedence(self): def GetCurrentTokenPrecedence(self):
if isinstance(self.current, CharacterToken): if isinstance(self.current, CharacterToken):
return self.binop_precedence.get(self.current.char, -1) return self.binop_precedence.get(self.current.char, -1)
@ -1038,6 +1048,8 @@ need to `download <../download.html>`_ and
Main driver code. Main driver code.
----------------- -----------------
.. code-block:: python
def main(): def main():
# Install standard binary operators. # Install standard binary operators.
# 1 is lowest possible precedence. 40 is the highest. # 1 is lowest possible precedence. 40 is the highest.

View file

@ -400,9 +400,10 @@ example, we can create a C file with the following simple function:
.. code-block:: c .. code-block:: c
#include #include <stdio.h>
double putchard(double x) { putchar((char)x); return 0; } {% double putchard(double x) {
putchar((char)x); return 0; } {%
endhighlight %} endhighlight %}
We can then compile this into a shared library with GCC: We can then compile this into a shared library with GCC: