<s> import jasy . js . parse . Node as Node <EOL> import jasy . core . Console as Console <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> class Error ( Exception ) : <EOL> def __init__ ( self , line ) : <EOL> self . __line = line <EOL> def optimize ( node ) : <EOL> Console . debug ( "<STR_LIT>" ) <EOL> Console . indent ( ) <EOL> result = __optimize ( node ) <EOL> Console . outdent ( ) <EOL> return result <EOL> def __optimize ( node ) : <EOL> copy = node <EOL> if node . type in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> copy = list ( node ) <EOL> for child in copy : <EOL> if child != None : <EOL> __optimize ( child ) <EOL> if node . type in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> __combineSiblings ( node ) <EOL> if node . type == "<STR_LIT>" : <EOL> __combineVarStatements ( node ) <EOL> def __combineSiblings ( node ) : <EOL> """<STR_LIT>""" <EOL> length = len ( node ) <EOL> pos = length - <NUM_LIT:1> <EOL> while pos > <NUM_LIT:0> : <EOL> child = node [ pos ] <EOL> prevChild = node [ pos - <NUM_LIT:1> ] <EOL> if child . type == "<STR_LIT>" and prevChild . type == "<STR_LIT>" : <EOL> setup = getattr ( child , "<STR_LIT>" , None ) <EOL> if setup and setup . type == "<STR_LIT>" : <EOL> Console . debug ( "<STR_LIT>" % setup . line ) <EOL> child . remove ( setup ) <EOL> child = setup <EOL> if child . type == "<STR_LIT>" and prevChild . type == "<STR_LIT>" : <EOL> for variable in list ( child ) : <EOL> prevChild . append ( variable ) <EOL> if child in node : <EOL> node . remove ( child ) <EOL> pos -= <NUM_LIT:1> <EOL> def __combineVarStatements ( node ) : <EOL> """<STR_LIT>""" <EOL> if len ( node . scope . declared ) == <NUM_LIT:0> : <EOL> return <EOL> firstVar = __findFirstVarStatement ( node ) <EOL> if not firstVar : <EOL> firstVar = Node . Node ( None , "<STR_LIT>" ) <EOL> node . insert ( <NUM_LIT:0> , firstVar ) <EOL> __patchVarStatements ( node , firstVar ) <EOL> __cleanFirst ( firstVar ) <EOL> if len ( firstVar ) == <NUM_LIT:0> : <EOL> firstVar . parent . remove ( firstVar ) <EOL> else : <EOL> firstVarParent = firstVar . parent <EOL> firstVarPos = firstVarParent . index ( firstVar ) <EOL> if len ( firstVarParent ) > firstVarPos + <NUM_LIT:1> : <EOL> possibleForStatement = firstVarParent [ firstVarPos + <NUM_LIT:1> ] <EOL> if possibleForStatement . type == "<STR_LIT>" and not hasattr ( possibleForStatement , "<STR_LIT>" ) : <EOL> possibleForStatement . append ( firstVar , "<STR_LIT>" ) <EOL> def __findFirstVarStatement ( node ) : <EOL> """<STR_LIT>""" <EOL> if node . type == "<STR_LIT>" : <EOL> if getattr ( node , "<STR_LIT>" , None ) == "<STR_LIT>" : <EOL> return False <EOL> else : <EOL> return node <EOL> for child in node : <EOL> if child . type == "<STR_LIT>" : <EOL> continue <EOL> result = __findFirstVarStatement ( child ) <EOL> if result : <EOL> return result <EOL> elif result is False : <EOL> return False <EOL> return None <EOL> def __cleanFirst ( first ) : <EOL> """<STR_LIT>""" <EOL> known = set ( ) <EOL> for child in first : <EOL> if hasattr ( child , "<STR_LIT>" ) : <EOL> varName = getattr ( child , "<STR_LIT:name>" , None ) <EOL> if varName != None : <EOL> known . add ( varName ) <EOL> else : <EOL> for varIdentifier in child . names : <EOL> known . add ( varIdentifier . value ) <EOL> for child in list ( first ) : <EOL> if not hasattr ( child , "<STR_LIT>" ) : <EOL> if child . name in known : <EOL> first . remove ( child ) <EOL> else : <EOL> known . add ( child . name ) <EOL> def __createSimpleAssignment ( identifier , valueNode ) : <EOL> assignNode = Node . Node ( None , "<STR_LIT>" ) <EOL> identNode = Node . Node ( None , "<STR_LIT>" ) <EOL> identNode . value = identifier <EOL> assignNode . append ( identNode ) <EOL> assignNode . append ( valueNode ) <EOL> return assignNode <EOL> def __createMultiAssignment ( names , valueNode ) : <EOL> assignNode = Node . Node ( None , "<STR_LIT>" ) <EOL> assignNode . append ( names ) <EOL> assignNode . append ( valueNode ) <EOL> return assignNode <EOL> def __createDeclaration ( name ) : <EOL> declNode = Node . Node ( None , "<STR_LIT>" ) <EOL> declNode . name = name <EOL> declNode . readOnly = False <EOL> return declNode <EOL> def __createIdentifier ( value ) : <EOL> identifier = Node . Node ( None , "<STR_LIT>" ) <EOL> identifier . value = value <EOL> return identifier <EOL> def __patchVarStatements ( node , firstVarStatement ) : <EOL> """<STR_LIT>""" <EOL> if node is firstVarStatement : <EOL> return <EOL> elif node . type == "<STR_LIT>" : <EOL> return <EOL> elif node . type == "<STR_LIT>" : <EOL> __rebuildAsAssignment ( node , firstVarStatement ) <EOL> else : <EOL> for child in list ( node ) : <EOL> __patchVarStatements ( child , firstVarStatement ) <EOL> def __rebuildAsAssignment ( node , firstVarStatement ) : <EOL> """<STR_LIT>""" <EOL> assignment = Node . Node ( node . tokenizer , "<STR_LIT>" ) <EOL> assignmentList = Node . Node ( node . tokenizer , "<STR_LIT>" ) <EOL> assignment . append ( assignmentList , "<STR_LIT>" ) <EOL> for child in list ( node ) : <EOL> if hasattr ( child , "<STR_LIT:name>" ) : <EOL> if hasattr ( child , "<STR_LIT>" ) : <EOL> assign = __createSimpleAssignment ( child . name , child . initializer ) <EOL> assignmentList . append ( assign ) <EOL> firstVarStatement . append ( child ) <EOL> else : <EOL> for identifier in child . names : <EOL> firstVarStatement . append ( __createDeclaration ( identifier . value ) ) <EOL> if hasattr ( child , "<STR_LIT>" ) : <EOL> assign = __createMultiAssignment ( child . names , child . initializer ) <EOL> assignmentList . append ( assign ) <EOL> node . remove ( child ) <EOL> if len ( assignmentList ) > <NUM_LIT:0> : <EOL> node . parent . replace ( node , assignment ) <EOL> elif getattr ( node , "<STR_LIT>" , None ) == "<STR_LIT>" : <EOL> if hasattr ( child , "<STR_LIT:name>" ) : <EOL> node . parent . replace ( node , __createIdentifier ( child . name ) ) <EOL> else : <EOL> node . parent . replace ( node , child . names ) <EOL> else : <EOL> if hasattr ( node , "<STR_LIT>" ) : <EOL> Console . warn ( "<STR_LIT>" % ( node . rel , node ) ) <EOL> node . parent . remove ( node ) <EOL> if len ( assignmentList ) == <NUM_LIT:1> : <EOL> assignment . replace ( assignmentList , assignmentList [ <NUM_LIT:0> ] ) </s>
