本文整理汇总了Python中library.Library.init方法的典型用法代码示例。如果您正苦于以下问题:Python Library.init方法的具体用法?Python Library.init怎么用?Python Library.init使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类library.Library
的用法示例。
在下文中一共展示了Library.init方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Delimiters
# 需要导入模块: from library import Library [as 别名]
# 或者: from library.Library import init [as 别名]
class Parser:
# Chars for multi-char Tokens
chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'"
# Delimiters (single-char Tokens)
dels = "\\/.():,+*="
def __init__( self ):
self.lib = Library() # Create Library
self.lib.init( self ) # parser.lib reference must exist before initialization of Library
def parse( self, str ):
#print list( self.tokenize( str ) )
tokens = self.tokenize( str )
struct = self.getStruct( tokens )
#print struct
return self.getExpression( struct, {} )
def getExpression( self, struct, vars, func=None ):
if not struct:
print "Error: Empty Expression: ()"
return Variable()
head, tail = struct[0], struct[1:]
if type( head ) is not tuple: # head is list
expr = self.getExpression( head, vars )
else:
varname,vartype = head
if LAMBDA == vartype: # Lambda
abs = Abstraction( expr= None )
vars1 = copy.copy( vars ) # Create copy of bound Variables
vars1[ varname ] = abs # And Append/Replace the varname
if not tail:
print "Error: Lambda %s without Expression" % varname
return Variable()
abs.expr = self.getExpression( tail, vars1 )
if func:
return Application( func= func, arg= abs )
return abs
elif BODY == vartype: # Variable (Free or Bound)
if varname in vars: # Try to search in Bound variables
expr = Variable( vars[ varname ] )
else:
try:
expr = self.lib[ varname ] # Try to search in Library
except:
expr = Variable()
print "Warning: Free Variable '%s': %s" % (varname, expr)
elif LET == vartype: # Variable of Let-Expression
let = Let( be= None, expr= None )
if not tail or len( tail ) < 2:
print "Error: Let %s without Expression" % varname
return Variable()
vars1 = copy.copy( vars ) # Create copy of bound Variables
let.be = self.getExpression( tail[0], vars1 )
vars1 = copy.copy( vars ) # Create copy of bound Variables
vars1[ varname ] = let # And Append/Replace the varname
let.expr = self.getExpression( tail[1:], vars1 )
if func:
return Application( func= func, arg= let )
return let
else:
raise "Parser Error: Unknow var %s::%d" % (varname,vartype)
if func:
expr = Application( func= func, arg= expr )
if tail:
expr = self.getExpression( tail, vars, expr )
#.........这里部分代码省略.........