<s> import ufora . native . FORA as ForaNative <EOL> import ufora . FORA . python . Evaluator . Evaluator as Evaluator <EOL> import ufora . FORA . python . ErrorFormatting as ErrorFormatting <EOL> import ufora . util . TypeAwareComparison as TypeAwareComparison <EOL> import traceback <EOL> def evaluator ( ) : <EOL> return Evaluator . evaluator ( ) <EOL> class FORAFailure ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , error ) : <EOL> self . error = error <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % self . error <EOL> class FORAValue ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , x = None ) : <EOL> object . __init__ ( self ) <EOL> if x is None : <EOL> self . implVal_ = ForaNative . nothing <EOL> elif isinstance ( x , FORAValue ) : <EOL> self . implVal_ = x . implVal_ <EOL> elif isinstance ( x , ForaNative . ImplValContainer ) : <EOL> self . implVal_ = x <EOL> elif isinstance ( x , tuple ) : <EOL> self . implVal_ = ForaNative . ImplValContainer ( <EOL> tuple ( [ FORAValue ( elt ) . implVal_ for elt in x ] ) <EOL> ) <EOL> else : <EOL> self . implVal_ = ForaNative . ImplValContainer ( x ) <EOL> def __getattr__ ( self , attr ) : <EOL> if attr . startswith ( '<STR_LIT>' ) and attr . endswith ( '<STR_LIT>' ) : <EOL> raise AttributeError ( ) <EOL> return processComputationResult ( <EOL> Evaluator . evaluator ( ) . evaluate ( <EOL> self . implVal_ , <EOL> FORAValue . symbol_Member . implVal_ , <EOL> ForaNative . makeSymbol ( attr ) <EOL> ) <EOL> ) <EOL> def __call__ ( self , * args ) : <EOL> return processComputationResult ( <EOL> Evaluator . evaluator ( ) . evaluate ( <EOL> self . implVal_ , <EOL> FORAValue . symbol_Call . implVal_ , <EOL> * [ FORAValue ( x ) . implVal_ for x in args ] <EOL> ) <EOL> ) <EOL> def __getitem__ ( self , x ) : <EOL> return processComputationResult ( <EOL> Evaluator . evaluator ( ) . evaluate ( <EOL> self . implVal_ , <EOL> FORAValue . symbol_GetItem . implVal_ , <EOL> FORAValue ( x ) . implVal_ <EOL> ) <EOL> ) <EOL> def __setitem__ ( self , ix , x ) : <EOL> return processComputationResult ( <EOL> Evaluator . evaluator ( ) . evaluate ( <EOL> self . implVal_ , <EOL> FORAValue . symbol_SetItem . implVal_ , <EOL> FORAValue ( ix ) . implVal_ , <EOL> FORAValue ( x ) . implVal_ <EOL> ) <EOL> ) <EOL> def __len__ ( self ) : <EOL> return processComputationResult ( <EOL> Evaluator . evaluator ( ) . evaluate ( <EOL> FORAValue . symbol_Size . implVal_ , <EOL> FORAValue . symbol_Call . implVal_ , <EOL> self . implVal_ <EOL> ) <EOL> ) <EOL> def __add__ ( self , other ) : <EOL> return binaryOp_ ( self , FORAValue ( other ) , ForaOperatorSymbols . plus ) <EOL> def __radd__ ( self , other ) : <EOL> return binaryOp_ ( FORAValue ( other ) , self , ForaOperatorSymbols . plus ) <EOL> def __sub__ ( self , other ) : <EOL> return binaryOp_ ( self , FORAValue ( other ) , ForaOperatorSymbols . sub ) <EOL> def __rsub__ ( self , other ) : <EOL> return binaryOp_ ( FORAValue ( other ) , self , ForaOperatorSymbols . sub ) <EOL> def __mul__ ( self , other ) : <EOL> return binaryOp_ ( self , FORAValue ( other ) , ForaOperatorSymbols . mul ) <EOL> def __rmul__ ( self , other ) : <EOL> return binaryOp_ ( FORAValue ( other ) , self , ForaOperatorSymbols . mul ) <EOL> def __div__ ( self , other ) : <EOL> return binaryOp_ ( self , FORAValue ( other ) , ForaOperatorSymbols . div ) <EOL> def __rdiv__ ( self , other ) : <EOL> return binaryOp_ ( FORAValue ( other ) , self , ForaOperatorSymbols . div ) <EOL> def __mod__ ( self , other ) : <EOL> return binaryOp_ ( self , FORAValue ( other ) , ForaOperatorSymbols . mod ) <EOL> def __rmod__ ( self , other ) : <EOL> return binaryOp_ ( FORAValue ( other ) , self , ForaOperatorSymbols . mod ) <EOL> def __pow__ ( self , other ) : <EOL> return binaryOp_ ( self , FORAValue ( other ) , ForaOperatorSymbols . pow ) <EOL> def __rpow__ ( self , other ) : <EOL> return binaryOp_ ( FORAValue ( other ) , self , ForaOperatorSymbols . pow ) <EOL> def __lshift__ ( self , other ) : <EOL> return binaryOp_ ( self , FORAValue ( other ) , ForaOperatorSymbols . lshift ) <EOL> def __rlshift__ ( self , other ) : <EOL> return binaryOp_ ( FORAValue ( other ) , self , ForaOperatorSymbols . lshift ) <EOL> def __rshift__ ( self , other ) : <EOL> return binaryOp_ ( self , FORAValue ( other ) , ForaOperatorSymbols . rshift ) <EOL> def __rrshift__ ( self , other ) : <EOL> return binaryOp_ ( FORAValue ( other ) , self , ForaOperatorSymbols . rshift ) <EOL> def __cmp__ ( self , other ) : <EOL> return TypeAwareComparison . typecmp ( self , other , <EOL> lambda self , other : cmp ( self . implVal_ , other . implVal_ ) ) <EOL> def __hash__ ( self ) : <EOL> return self . implVal_ . __hash__ ( ) <EOL> def __str__ ( self ) : <EOL> x = self <EOL> ix = <NUM_LIT:0> <EOL> while isinstance ( x , FORAValue ) and x . implVal_ . type != String . implVal_ : <EOL> ix = ix + <NUM_LIT:1> <EOL> x = String ( x ) <EOL> if ( ix > <NUM_LIT:50> ) : <EOL> print x . implVal_ . type , String , x . implVal_ . type != String . implVal_ <EOL> assert ix < <NUM_LIT:100> , "<STR_LIT>" + repr ( self ) + "<STR_LIT>" <EOL> if isinstance ( x , FORAValue ) : <EOL> stringVal = x . implVal_ . getPythonString ( ) <EOL> if stringVal is None : <EOL> x = str ( x ) <EOL> else : <EOL> x = stringVal <EOL> elif not isinstance ( x , str ) : <EOL> return str ( x ) <EOL> return x <EOL> def __repr__ ( self ) : <EOL> return str ( self . implVal_ ) <EOL> def __coerce__ ( self , other ) : <EOL> return None <EOL> def __getstate__ ( self ) : <EOL> return ( self . implVal_ , ) <EOL> def __setstate__ ( self , s ) : <EOL> self . implVal_ = s [ <NUM_LIT:0> ] <EOL> def type ( self ) : <EOL> """<STR_LIT>""" <EOL> return FORAValue ( self . implVal_ . type ) <EOL> def toPythonObject ( self ) : <EOL> """<STR_LIT>""" <EOL> implval = self . implVal_ <EOL> assert isinstance ( implval , ForaNative . ImplValContainer ) <EOL> if implval . isTuple ( ) : <EOL> names = implval . getTupleNames ( ) <EOL> for n in names : <EOL> if n is not None : <EOL> return FORATuple ( implval ) <EOL> return tuple ( [ FORAValue ( x ) . toPythonObject ( ) for x in implval . getTuple ( ) ] ) <EOL> if implval . isVector ( ) : <EOL> return FORAVector ( implval ) <EOL> if implval . isMutableVector ( ) : <EOL> return FORAMutableVector ( implval ) <EOL> if cmp ( implval . type , ForaNative . Nothing ) == <NUM_LIT:0> : <EOL> return None <EOL> if cmp ( implval . type , ForaNative . UInt1 ) == <NUM_LIT:0> : <EOL> return implval . pyval <EOL> if cmp ( implval . type , ForaNative . Int64 ) == <NUM_LIT:0> : <EOL> return implval . pyval <EOL> if cmp ( implval . type , ForaNative . Float64 ) == <NUM_LIT:0> : <EOL> return implval . pyval <EOL> if cmp ( implval . type , ForaNative . String ) == <NUM_LIT:0> : <EOL> return implval . pyval <EOL> return FORAValue ( implval ) <EOL> @ staticmethod <EOL> def makeSymbol ( symbolString ) : <EOL> """<STR_LIT>""" <EOL> return FORAValue ( ForaNative . makeSymbol ( symbolString ) ) <EOL> FORAValue . MutableVector = FORAValue . makeSymbol ( "<STR_LIT>" ) <EOL> FORAValue . Vector = FORAValue . makeSymbol ( "<STR_LIT>" ) <EOL> FORAValue . Type = FORAValue . makeSymbol ( "<STR_LIT>" ) <EOL> FORAValue . symbol_GetItem = FORAValue . makeSymbol ( "<STR_LIT>" ) <EOL> FORAValue . symbol_SetItem = FORAValue . makeSymbol ( "<STR_LIT>" ) <EOL> FORAValue . symbol_SetItemNoconvert = FORAValue . makeSymbol ( "<STR_LIT>" ) <EOL> FORAValue . symbol_Operator = FORAValue . makeSymbol ( "<STR_LIT>" ) <EOL> FORAValue . symbol_Member = FORAValue . makeSymbol ( "<STR_LIT>" ) <EOL> FORAValue . symbol_Size = FORAValue . makeSymbol ( "<STR_LIT:size>" ) <EOL> FORAValue . symbol_Call = FORAValue . makeSymbol ( "<STR_LIT>" ) <EOL> FORAValue . symbol_Next = FORAValue . makeSymbol ( "<STR_LIT>" ) <EOL> FORAValue . symbol_package = FORAValue . makeSymbol ( "<STR_LIT>" ) <EOL> FORAValue . symbol_ExtractMetadata = FORAValue . makeSymbol ( "<STR_LIT>" ) <EOL> FORAValue . symbol_ShortRepresentation = FORAValue . makeSymbol ( "<STR_LIT>" ) <EOL> nothing = FORAValue ( ForaNative . nothing ) <EOL> StackTrace = FORAValue ( ForaNative . StackTrace ) <EOL> String = FORAValue ( ForaNative . String ) <EOL> class FORATuple ( FORAValue ) : <EOL> def __init__ ( self , implval ) : <EOL> FORAValue . __init__ ( self , implval ) <EOL> def __getitem__ ( self , x ) : <EOL> try : <EOL> return FORAValue ( self . implVal_ [ x ] ) . toPythonObject ( ) <EOL> except : <EOL> raise IndexError ( ) <EOL> names_ = property ( lambda self : self . implVal_ . getTupleNames ( ) ) <EOL> @ staticmethod <EOL> def isTuple ( t ) : <EOL> return t . implVal_ . isTuple ( ) <EOL> @ staticmethod <EOL> def getNames ( t ) : <EOL> return t . names_ <EOL> class FORAVector ( FORAValue ) : <EOL> def __init__ ( self , implval ) : <EOL> FORAValue . __init__ ( self , implval ) <EOL> def __getitem__ ( self , x ) : <EOL> try : <EOL> value = Evaluator . evaluator ( ) . getVDM ( ) . extractVectorItem ( self . implVal_ , x ) <EOL> if value is not None : <EOL> return FORAValue ( value ) . toPythonObject ( ) <EOL> else : <EOL> return FORAValue . __getitem__ ( self , x ) <EOL> except : <EOL> raise IndexError ( ) <EOL> class FORAMutableVector ( FORAValue ) : <EOL> def __init__ ( self , implval ) : <EOL> FORAValue . __init__ ( self , implval ) <EOL> def __getitem__ ( self , x ) : <EOL> try : <EOL> return FORAValue ( self . implVal_ [ x ] ) . toPythonObject ( ) <EOL> except : <EOL> raise IndexError ( ) <EOL> def extractImplValContainer ( foraValue ) : <EOL> """<STR_LIT>""" <EOL> return foraValue . implVal_ <EOL> class FORAException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , foraVal = None ) : <EOL> Exception . __init__ ( self ) <EOL> if foraVal is None : <EOL> self . foraVal = nothing <EOL> self . trace = None <EOL> self . valuesInScope = None <EOL> else : <EOL> if not isinstance ( foraVal , tuple ) or len ( foraVal ) != <NUM_LIT:2> : <EOL> assert False , "<STR_LIT>" <EOL> stacktraceAndData = foraVal [ <NUM_LIT:1> ] <EOL> if not isinstance ( foraVal [ <NUM_LIT:1> ] , tuple ) : <EOL> stacktraceAndData = stacktraceAndData . toPythonObject ( ) <EOL> if not isinstance ( stacktraceAndData , tuple ) or len ( stacktraceAndData ) != <NUM_LIT:2> : <EOL> print stacktraceAndData , type ( stacktraceAndData ) <EOL> assert False , "<STR_LIT>" <EOL> if not ( stacktraceAndData [ <NUM_LIT:0> ] . type ( ) == StackTrace ) : <EOL> assert False , "<STR_LIT>" <EOL> self . foraVal = foraVal [ <NUM_LIT:0> ] <EOL> self . trace = stacktraceAndData [ <NUM_LIT:0> ] . implVal_ . getStackTrace ( ) <EOL> self . valuesInScope = stacktraceAndData [ <NUM_LIT:1> ] <EOL> def __str__ ( self ) : <EOL> exceptionString = str ( self . foraVal ) <EOL> if len ( exceptionString ) > <NUM_LIT:1000> : <EOL> exceptionString = exceptionString [ : <NUM_LIT> ] + exceptionString [ - <NUM_LIT:200> : ] <EOL> return "<STR_LIT>" + exceptionString + ( <EOL> "<STR_LIT>" + <EOL> ErrorFormatting . formatStacktrace ( self . trace , self . valuesInScope ) <EOL> if self . trace is not None <EOL> else <EOL> "<STR_LIT>" ) <EOL> def __cmp__ ( self , other ) : <EOL> return TypeAwareComparison . typecmp ( self , other , <EOL> lambda self , other : cmp ( self . foraVal , other . foraVal ) ) <EOL> def __getinitargs__ ( self ) : <EOL> return ( self . foraVal , self . trace ) <EOL> def processComputationResult ( res ) : <EOL> """<STR_LIT>""" <EOL> if res . isException ( ) : <EOL> v = FORAValue ( res . asException . exception ) . toPythonObject ( ) <EOL> if isinstance ( v , Exception ) : <EOL> raise v <EOL> else : <EOL> raise FORAException ( v ) <EOL> if res . isFailure ( ) : <EOL> raise FORAFailure ( res . asFailure . error ) <EOL> return FORAValue ( res . asResult . result ) . toPythonObject ( ) <EOL> def binaryOp_ ( lhs , rhs , op ) : <EOL> """<STR_LIT>""" <EOL> return processComputationResult ( <EOL> Evaluator . evaluator ( ) . evaluate ( <EOL> lhs . implVal_ , <EOL> FORAValue . symbol_Operator . implVal_ , <EOL> op . implVal_ , <EOL> rhs . implVal_ <EOL> ) <EOL> ) <EOL> class ForaOperatorSymbols ( object ) : <EOL> plus = FORAValue . makeSymbol ( "<STR_LIT:+>" ) <EOL> sub = FORAValue . makeSymbol ( "<STR_LIT:->" ) <EOL> mul = FORAValue . makeSymbol ( "<STR_LIT:*>" ) <EOL> div = FORAValue . makeSymbol ( "<STR_LIT:/>" ) <EOL> mod = FORAValue . makeSymbol ( "<STR_LIT:%>" ) <EOL> pow = FORAValue . makeSymbol ( "<STR_LIT>" ) <EOL> lshift = FORAValue . makeSymbol ( "<STR_LIT>" ) <EOL> rshift = FORAValue . makeSymbol ( "<STR_LIT>" ) <EOL> lt = FORAValue . makeSymbol ( "<STR_LIT:<>" ) <EOL> gt = FORAValue . makeSymbol ( "<STR_LIT:>>" ) <EOL> le = FORAValue . makeSymbol ( "<STR_LIT>" ) <EOL> ge = FORAValue . makeSymbol ( "<STR_LIT>" ) <EOL> eq = FORAValue . makeSymbol ( "<STR_LIT>" ) <EOL> ne = FORAValue . makeSymbol ( "<STR_LIT>" ) <EOL> ForaOperatorLambdas = { <EOL> ForaOperatorSymbols . plus : ( lambda x , y : x + y ) , <EOL> ForaOperatorSymbols . sub : ( lambda x , y : x - y ) , <EOL> ForaOperatorSymbols . mul : ( lambda x , y : x * y ) , <EOL> ForaOperatorSymbols . div : ( lambda x , y : x / y ) , <EOL> ForaOperatorSymbols . mod : ( lambda x , y : x % y ) , <EOL> ForaOperatorSymbols . pow : ( lambda x , y : x ** y ) , <EOL> ForaOperatorSymbols . lshift : ( lambda x , y : x << y ) , <EOL> ForaOperatorSymbols . rshift : ( lambda x , y : x >> y ) , <EOL> ForaOperatorSymbols . lt : ( lambda x , y : x < y ) , <EOL> ForaOperatorSymbols . gt : ( lambda x , y : x > y ) , <EOL> ForaOperatorSymbols . le : ( lambda x , y : x <= y ) , <EOL> ForaOperatorSymbols . ge : ( lambda x , y : x >= y ) , <EOL> ForaOperatorSymbols . eq : ( lambda x , y : x == y ) , <EOL> ForaOperatorSymbols . ne : ( lambda x , y : x != y ) <EOL> } </s>
