Finished editing PythonLangImpl6.rst.

This commit is contained in:
Maggie Mari 2012-08-21 17:59:44 -05:00
commit 16860436d4

View file

@ -105,7 +105,9 @@ keywords:
pass pass
class UnaryToken(object): class UnaryToken(object):
pass pass
... ...
def Tokenize(string): def Tokenize(string):
... ...
elif identifier == 'in': elif identifier == 'in':
@ -295,7 +297,8 @@ whenever we define a new binary operator:
def main(): def main():
... ...
g_binop_precedence['<'] = 10 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
... ...
@ -826,10 +829,12 @@ Lexer
class UnaryToken(object): class UnaryToken(object):
pass pass
class IdentifierToken(object): def __init__(self, name): class IdentifierToken(object):
def __init__(self, name):
self.name = name self.name = name
class NumberToken(object): def __init__(self, value): class NumberToken(object):
def __init__(self, value):
self.value = value self.value = value
class CharacterToken(object): class CharacterToken(object):
@ -899,7 +904,7 @@ Lexer
Abstract Syntax Tree (aka Parse Tree) Abstract Syntax Tree (aka Parse Tree)
------------------------------------- -------------------------------------
.. code-block:: python .. code-block:: python
# Base class for all expression nodes. # Base class for all expression nodes.
class ExpressionNode(object): class ExpressionNode(object):
@ -911,7 +916,8 @@ Abstract Syntax Tree (aka Parse Tree)
def __init__(self, value): def __init__(self, value):
self.value = value self.value = value
def CodeGen(self): return Constant.real(Type.double(), self.value) def CodeGen(self):
return Constant.real(Type.double(), self.value)
# Expression class for referencing a variable, like "a". # Expression class for referencing a variable, like "a".
class VariableExpressionNode(ExpressionNode): class VariableExpressionNode(ExpressionNode):
@ -978,7 +984,9 @@ Abstract Syntax Tree (aka Parse Tree)
self.then_branch = then_branch self.then_branch = then_branch
self.else_branch = else_branch self.else_branch = else_branch
def CodeGen(self): condition = self.condition.CodeGen() def CodeGen(self):
condition = self.condition.CodeGen()
# Convert condition to a bool by comparing equal to 0.0. # Convert condition to a bool by comparing equal to 0.0.
condition_bool = g_llvm_builder.fcmp( condition_bool = g_llvm_builder.fcmp(
@ -1114,12 +1122,14 @@ Abstract Syntax Tree (aka Parse Tree)
# Expression class for a unary operator. # Expression class for a unary operator.
class UnaryExpressionNode(ExpressionNode): class UnaryExpressionNode(ExpressionNode):
def __init__(self, operator, operand): self.operator = operator def __init__(self, operator, operand):
self.operator = operator
self.operand = operand self.operand = operand
def CodeGen(self): operand = self.operand.CodeGen() function = def CodeGen(self):
g_llvm_module.get_function_named('unary' + self.operator) return operand = self.operand.CodeGen()
g_llvm_builder.call(function, [operand], 'unop') function = g_llvm_module.get_function_named('unary' + self.operator)
return g_llvm_builder.call(function, [operand], 'unop')
# This class represents the "prototype" for a function, which captures its name, # This class represents the "prototype" for a function, which captures its name,
# and its argument names (thus implicitly the number of arguments the function # and its argument names (thus implicitly the number of arguments the function
@ -1127,18 +1137,22 @@ Abstract Syntax Tree (aka Parse Tree)
class PrototypeNode(object): class PrototypeNode(object):
def __init__(self, name, args, is_operator=False, precedence=0): def __init__(self, name, args, is_operator=False, precedence=0):
self.name = name self.args = args self.is_operator = is_operator self.name = name
self.args = args
self.is_operator = is_operator
self.precedence = precedence self.precedence = precedence
def IsBinaryOp(self): return self.is_operator and len(self.args) == 2 def IsBinaryOp(self):
return self.is_operator and len(self.args) == 2
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):
funct_type = Type.function( Type.double(), [Type.double()] * # Make the function type, eg. double(double,double).
len(self.args), False) funct_type = Type.function(
Type.double(), [Type.double()] * len(self.args), False)
::
function = Function.new(g_llvm_module, funct_type, self.name) function = Function.new(g_llvm_module, funct_type, self.name)
@ -1168,12 +1182,13 @@ Abstract Syntax Tree (aka Parse Tree)
# This class represents a function definition itself. # This class represents a function definition itself.
class FunctionNode(object): class FunctionNode(object):
def __init__(self, prototype, body): self.prototype = prototype def __init__(self, prototype, body):
self.prototype = prototype
self.body = body self.body = body
def CodeGen(self): # Clear scope. g_named_values.clear() def CodeGen(self):
# Clear scope.
:: g_named_values.clear()
# Create a function object. # Create a function object.
function = self.prototype.CodeGen() function = self.prototype.CodeGen()
@ -1206,29 +1221,36 @@ Abstract Syntax Tree (aka Parse Tree)
return function return function
Parser Parser
------ ------
.. code-block:: python
class Parser(object): class Parser(object):
def __init__(self, tokens): self.tokens = tokens self.Next() def __init__(self, tokens):
self.tokens = tokens
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. def Next(self): # updates Parser.current with its results.
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. def GetCurrentTokenPrecedence(self): if # operator.
isinstance(self.current, CharacterToken): return def GetCurrentTokenPrecedence(self):
g_binop_precedence.get(self.current.char, -1) else: return -1 if isinstance(self.current, CharacterToken):
return g_binop_precedence.get(self.current.char, -1)
else:
return -1
# identifierexpr ::= identifier | identifier '(' expression* ')' def # identifierexpr ::= identifier | identifier '(' expression* ')'
ParseIdentifierExpr(self): identifier_name = self.current.name def ParseIdentifierExpr(self):
identifier_name = self.current.name
self.Next() # eat identifier. self.Next() # eat identifier.
::
if self.current != CharacterToken('('): # Simple variable reference. if self.current != CharacterToken('('): # Simple variable reference.
return VariableExpressionNode(identifier_name) return VariableExpressionNode(identifier_name)
@ -1247,14 +1269,15 @@ Abstract Syntax Tree (aka Parse Tree)
self.Next() # eat ')'. self.Next() # eat ')'.
return CallExpressionNode(identifier_name, args) return CallExpressionNode(identifier_name, args)
# numberexpr ::= number def ParseNumberExpr(self): result = # numberexpr ::= number
NumberExpressionNode(self.current.value) self.Next() # consume the def ParseNumberExpr(self):
number. return result result = NumberExpressionNode(self.current.value)
self.Next() # consume the number.
return result
# parenexpr ::= '(' expression ')' def ParseParenExpr(self): self.Next() # parenexpr ::= '(' expression ')'
# eat '('. def ParseParenExpr(self):
self.Next() # eat '('.
::
contents = self.ParseExpression() contents = self.ParseExpression()
@ -1264,10 +1287,9 @@ Abstract Syntax Tree (aka Parse Tree)
return contents return contents
# ifexpr ::= 'if' expression 'then' expression 'else' expression def # ifexpr ::= 'if' expression 'then' expression 'else' expression
ParseIfExpr(self): self.Next() # eat the if. def ParseIfExpr(self):
self.Next() # eat the if.
::
# condition. # condition.
condition = self.ParseExpression() condition = self.ParseExpression()
@ -1286,10 +1308,9 @@ Abstract Syntax Tree (aka Parse Tree)
return IfExpressionNode(condition, then_branch, else_branch) return IfExpressionNode(condition, then_branch, else_branch)
# forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' # forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
expression def ParseForExpr(self): self.Next() # eat the for. def ParseForExpr(self):
self.Next() # eat the for.
::
if not isinstance(self.current, IdentifierToken): if not isinstance(self.current, IdentifierToken):
raise RuntimeError('Expected identifier after for.') raise RuntimeError('Expected identifier after for.')
@ -1324,32 +1345,38 @@ Abstract Syntax Tree (aka Parse Tree)
return ForExpressionNode(loop_variable, start, end, step, body) return ForExpressionNode(loop_variable, start, end, step, body)
# primary ::= identifierexpr | numberexpr | parenexpr | ifexpr | # primary ::= identifierexpr | numberexpr | parenexpr | ifexpr | forexpr
forexpr def ParsePrimary(self): if isinstance(self.current, def ParsePrimary(self):
IdentifierToken): return self.ParseIdentifierExpr() elif if isinstance(self.current, IdentifierToken):
isinstance(self.current, NumberToken): return self.ParseNumberExpr() return self.ParseIdentifierExpr()
elif isinstance(self.current, IfToken): return self.ParseIfExpr() elif elif isinstance(self.current, NumberToken):
isinstance(self.current, ForToken): return self.ParseForExpr() elif return self.ParseNumberExpr()
self.current == CharacterToken('('): return self.ParseParenExpr() else: elif isinstance(self.current, IfToken):
return self.ParseIfExpr()
elif isinstance(self.current, ForToken):
return self.ParseForExpr()
elif self.current == CharacterToken('('):
return self.ParseParenExpr()
else:
raise RuntimeError('Unknown token when expecting an expression.') raise RuntimeError('Unknown token when expecting an expression.')
# unary ::= primary | unary_operator unary def ParseUnary(self): # If # unary ::= primary | unary_operator unary
the current token is not an operator, it must be a primary expression. def ParseUnary(self):
if (not isinstance(self.current, CharacterToken) or self.current in # If the current token is not an operator, it must be a primary expression.
[CharacterToken('('), CharacterToken(',')]): return self.ParsePrimary() if (not isinstance(self.current, CharacterToken) or
self.current in [CharacterToken('('), CharacterToken(',')]):
:: return self.ParsePrimary()
# If this is a unary operator, read it. # If this is a unary operator, read it.
operator = self.current.char operator = self.current.char
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)*
left_precedence): # If this is a binary operator, find its precedence. def ParseBinOpRHS(self, left, left_precedence):
while True: precedence = self.GetCurrentTokenPrecedence() # If this is a binary operator, find its precedence.
while True:
:: precedence = self.GetCurrentTokenPrecedence()
# If this is a binary operator that binds at least as tightly as the # If this is a binary operator that binds at least as tightly as the
# current one, consume it; otherwise we are done. # current one, consume it; otherwise we are done.
@ -1371,28 +1398,42 @@ Abstract Syntax Tree (aka Parse Tree)
# Merge left/right. # Merge left/right.
left = BinaryOperatorExpressionNode(binary_operator, left, right) left = BinaryOperatorExpressionNode(binary_operator, left, right)
# expression ::= unary binoprhs def ParseExpression(self): left = # expression ::= unary binoprhs
self.ParseUnary() return self.ParseBinOpRHS(left, 0) def ParseExpression(self):
left = self.ParseUnary()
return self.ParseBinOpRHS(left, 0)
# prototype # ::= id '(' id* ')' # ::= binary LETTER number? (id, id) # # prototype # ::= id '(' id* ')'
::= unary LETTER (id) def ParsePrototype(self): precedence = None if # ::= binary LETTER number? (id, id)
isinstance(self.current, IdentifierToken): kind = 'normal' # ::= unary LETTER (id)
function_name = self.current.name self.Next() # eat function name. elif def ParsePrototype(self):
isinstance(self.current, UnaryToken): kind = 'unary' self.Next() # eat precedence = None
'unary'. if not isinstance(self.current, CharacterToken): raise if isinstance(self.current, IdentifierToken):
RuntimeError('Expected an operator after "unary".') function_name = kind = 'normal'
'unary' + self.current.char self.Next() # eat the operator. elif function_name = self.current.name
isinstance(self.current, BinaryToken): kind = 'binary' self.Next() # eat self.Next() # eat function name.
'binary'. if not isinstance(self.current, CharacterToken): raise elif isinstance(self.current, UnaryToken):
RuntimeError('Expected an operator after "binary".') function_name = kind = 'unary'
'binary' + self.current.char self.Next() # eat the operator. if self.Next() # eat 'unary'.
isinstance(self.current, NumberToken): if not 1 <= self.current.value <= if not isinstance(self.current, CharacterToken):
100: raise RuntimeError('Invalid precedence: must be in range [1, raise RuntimeError('Expected an operator after "unary".')
100].') precedence = self.current.value self.Next() # eat the function_name = 'unary' + self.current.char
precedence. else: raise RuntimeError('Expected function name, "unary" or self.Next() # eat the operator.
"binary" in ' 'prototype.') elif isinstance(self.current, BinaryToken):
kind = 'binary'
:: self.Next() # eat 'binary'.
if not isinstance(self.current, CharacterToken):
raise RuntimeError('Expected an operator after "binary".')
function_name = 'binary' + self.current.char
self.Next() # eat the operator.
if isinstance(self.current, NumberToken):
if not 1 <= self.current.value <= 100:
raise RuntimeError('Invalid precedence: must be in range [1, 100].')
precedence = self.current.value
self.Next() # eat the precedence.
else:
raise RuntimeError('Expected function name, "unary" or "binary" in '
'prototype.')
if self.current != CharacterToken('('): if self.current != CharacterToken('('):
raise RuntimeError('Expected "(" in prototype.') raise RuntimeError('Expected "(" in prototype.')
@ -1416,55 +1457,86 @@ Abstract Syntax Tree (aka Parse Tree)
return PrototypeNode(function_name, arg_names, kind != 'normal', precedence) return PrototypeNode(function_name, arg_names, kind != 'normal', precedence)
# definition ::= 'def' prototype expression def ParseDefinition(self): # definition ::= 'def' prototype expression
self.Next() # eat def. proto = self.ParsePrototype() body = def ParseDefinition(self):
self.ParseExpression() return FunctionNode(proto, body) self.Next() # eat def.
proto = self.ParsePrototype()
body = self.ParseExpression()
return FunctionNode(proto, body)
# toplevelexpr ::= expression def ParseTopLevelExpr(self): proto = # toplevelexpr ::= expression
PrototypeNode('', []) return FunctionNode(proto, self.ParseExpression()) def ParseTopLevelExpr(self):
proto = PrototypeNode('', [])
return FunctionNode(proto, self.ParseExpression())
# external ::= 'extern' prototype def ParseExtern(self): self.Next() # # external ::= 'extern' prototype
eat extern. return self.ParsePrototype() def ParseExtern(self):
self.Next() # eat extern.
return self.ParsePrototype()
# Top-Level parsing def HandleDefinition(self): # Top-Level parsing
def HandleDefinition(self):
self.Handle(self.ParseDefinition, 'Read a function definition:') self.Handle(self.ParseDefinition, 'Read a function definition:')
def HandleExtern(self): self.Handle(self.ParseExtern, 'Read an extern:') def HandleExtern(self):
self.Handle(self.ParseExtern, 'Read an extern:')
def HandleTopLevelExpression(self): try: function = def HandleTopLevelExpression(self):
self.ParseTopLevelExpr().CodeGen() result = try:
g_llvm_executor.run_function(function, []) print 'Evaluated to:', function = self.ParseTopLevelExpr().CodeGen()
result.as_real(Type.double()) except Exception, e: print 'Error:', e result = g_llvm_executor.run_function(function, [])
try: self.Next() # Skip for error recovery. except: pass print 'Evaluated to:', result.as_real(Type.double())
except Exception, e:
print 'Error:', e
try:
self.Next() # Skip for error recovery.
except:
pass
def Handle(self, function, message): try: print message, def Handle(self, function, message):
function().CodeGen() except Exception, e: print 'Error:', e try: try:
self.Next() # Skip for error recovery. except: pass print message, function().CodeGen()
except Exception, e:
print 'Error:', e
try:
self.Next() # Skip for error recovery.
except:
pass
Main driver code. Main driver code.
----------------- -----------------
def main(): # Set up the optimizer pipeline. Start with registering info .. code-block:: python
about how the # target lays out data structures.
g_llvm_pass_manager.add(g_llvm_executor.target_data) # Do simple def main():
"peephole" optimizations and bit-twiddling optzns. # Set up the optimizer pipeline. Start with registering info about how the
g_llvm_pass_manager.add(PASS_INSTRUCTION_COMBINING) # Reassociate # target lays out data structures.
expressions. g_llvm_pass_manager.add(PASS_REASSOCIATE) # Eliminate g_llvm_pass_manager.add(g_llvm_executor.target_data)
Common SubExpressions. g_llvm_pass_manager.add(PASS_GVN) # Simplify # Do simple "peephole" optimizations and bit-twiddling optzns.
the control flow graph (deleting unreachable blocks, etc). g_llvm_pass_manager.add(PASS_INSTRUCTION_COMBINING)
# Reassociate expressions.
g_llvm_pass_manager.add(PASS_REASSOCIATE)
# Eliminate Common SubExpressions.
g_llvm_pass_manager.add(PASS_GVN)
# Simplify the control flow graph (deleting unreachable blocks, etc).
g_llvm_pass_manager.add(PASS_CFG_SIMPLIFICATION) g_llvm_pass_manager.add(PASS_CFG_SIMPLIFICATION)
g_llvm_pass_manager.initialize() g_llvm_pass_manager.initialize()
# Install standard binary operators. # 1 is lowest possible precedence. # Install standard binary operators.
40 is the highest. g_binop_precedence['<'] = 10 # 1 is lowest possible precedence. 40 is the highest.
g_binop_precedence['+'] = 20 g_binop_precedence['-'] = 20 g_binop_precedence['<'] = 10
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".
= raw_input() except KeyboardInterrupt: break while True:
print 'ready>',
:: try:
raw = raw_input()
except KeyboardInterrupt:
break
parser = Parser(Tokenize(raw)) parser = Parser(Tokenize(raw))
while True: while True:
@ -1478,6 +1550,8 @@ Abstract Syntax Tree (aka Parse Tree)
else: else:
parser.HandleTopLevelExpression() parser.HandleTopLevelExpression()
# Print out all of the generated code. print '', g_llvm_module # Print out all of the generated code.
print '', g_llvm_module
if **name** == '__main__': main() if __name__ == '__main__':
main()