Misc doc cleanup
This commit is contained in:
parent
258efb0518
commit
8e00cd7e66
3 changed files with 154 additions and 145 deletions
|
|
@ -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
|
||||||
|
|
||||||
::
|
|
||||||
|
|
||||||
...
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
@ -642,8 +641,10 @@ need to `download <../download.html>`_ and
|
||||||
import re
|
import re
|
||||||
from llvm.core import Module, Constant, Type, Function, Builder, FCMP_ULT
|
from llvm.core import Module, Constant, Type, Function, Builder, FCMP_ULT
|
||||||
|
|
||||||
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')
|
||||||
|
|
@ -655,8 +656,10 @@ need to `download <../download.html>`_ and
|
||||||
# and what their LLVM representation is.
|
# and what their LLVM representation is.
|
||||||
g_named_values = {}
|
g_named_values = {}
|
||||||
|
|
||||||
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):
|
||||||
|
|
@ -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
|
||||||
|
|
@ -725,8 +729,10 @@ need to `download <../download.html>`_ and
|
||||||
|
|
||||||
yield EOFToken()
|
yield EOFToken()
|
||||||
|
|
||||||
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):
|
||||||
|
|
@ -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()
|
||||||
|
|
@ -866,8 +874,10 @@ need to `download <../download.html>`_ and
|
||||||
|
|
||||||
return function
|
return function
|
||||||
|
|
||||||
Parser
|
Parser
|
||||||
------
|
------
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
class Parser(object):
|
class Parser(object):
|
||||||
|
|
||||||
|
|
@ -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)
|
||||||
|
|
@ -1035,8 +1045,10 @@ need to `download <../download.html>`_ and
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
Main driver code.
|
Main driver code.
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# Install standard binary operators.
|
# Install standard binary operators.
|
||||||
|
|
|
||||||
|
|
@ -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:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue