<s> class Random ( object ) : <EOL> def __init__ ( self , seed ) : <EOL> self . cur = seed <EOL> def next ( self ) : <EOL> self . cur = ( self . cur * <NUM_LIT> + <NUM_LIT> ) % ( <NUM_LIT:1> << <NUM_LIT> ) <EOL> return self . cur <EOL> class AstAssign ( object ) : <EOL> def __init__ ( self , name , expr ) : <EOL> self . name = name <EOL> self . expr = expr <EOL> class AstWhile ( object ) : <EOL> def __init__ ( self , test , body ) : <EOL> self . test = test <EOL> self . body = body <EOL> class AstBinop ( object ) : <EOL> def __init__ ( self , lhs , rhs , op ) : <EOL> self . lhs = lhs <EOL> self . rhs = rhs <EOL> self . op = op <EOL> class AstPrint ( object ) : <EOL> def __init__ ( self , expr ) : <EOL> self . expr = expr <EOL> class AstIf ( object ) : <EOL> def __init__ ( self , test , then , orelse ) : <EOL> self . test = test <EOL> self . then = then <EOL> self . orelse = orelse <EOL> class State ( object ) : <EOL> def __init__ ( self ) : <EOL> self . syms = { } <EOL> def _run ( state , node ) : <EOL> if isinstance ( node , list ) : <EOL> for s in node : <EOL> _run ( state , s ) <EOL> return None <EOL> elif isinstance ( node , AstAssign ) : <EOL> v = _run ( state , node . expr ) <EOL> state . syms [ node . name ] = v <EOL> return None <EOL> elif isinstance ( node , int ) : <EOL> return node <EOL> elif isinstance ( node , str ) : <EOL> return state . syms [ node ] <EOL> elif isinstance ( node , AstPrint ) : <EOL> v = _run ( state , node . expr ) <EOL> print v <EOL> elif isinstance ( node , AstBinop ) : <EOL> l = _run ( state , node . lhs ) <EOL> r = _run ( state , node . rhs ) <EOL> if node . op == '<STR_LIT:+>' : <EOL> return l + r <EOL> if node . op == '<STR_LIT:->' : <EOL> return l - r <EOL> if node . op == '<STR_LIT:*>' : <EOL> return l * r <EOL> if node . op == '<STR_LIT:%>' : <EOL> return l % r <EOL> if node . op == "<STR_LIT:<>" : <EOL> return l < r <EOL> if node . op == "<STR_LIT>" : <EOL> return l <= r <EOL> if node . op == "<STR_LIT>" : <EOL> return l == r <EOL> print node . op <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> elif isinstance ( node , AstIf ) : <EOL> v = _run ( state , node . test ) <EOL> if v : <EOL> _run ( state , node . then ) <EOL> else : <EOL> _run ( state , node . orelse ) <EOL> elif isinstance ( node , AstWhile ) : <EOL> while True : <EOL> v = _run ( state , node . test ) <EOL> if not v : <EOL> break <EOL> _run ( state , node . body ) <EOL> return None <EOL> else : <EOL> print type ( node ) <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> def run ( node ) : <EOL> _run ( State ( ) , node ) <EOL> prog = [ <EOL> AstAssign ( "<STR_LIT:x>" , <NUM_LIT> ) , <EOL> AstAssign ( "<STR_LIT:t>" , <NUM_LIT:0> ) , <EOL> AstWhile ( "<STR_LIT:x>" , [ <EOL> AstAssign ( "<STR_LIT:t>" , AstBinop ( "<STR_LIT:t>" , "<STR_LIT:x>" , '<STR_LIT:+>' ) ) , <EOL> AstAssign ( "<STR_LIT:x>" , AstBinop ( "<STR_LIT:x>" , <NUM_LIT:1> , '<STR_LIT:->' ) ) , <EOL> ] ) , <EOL> AstPrint ( "<STR_LIT:t>" ) , <EOL> ] <EOL> prog = [ <EOL> AstAssign ( "<STR_LIT:i>" , <NUM_LIT:2> ) , <EOL> AstAssign ( "<STR_LIT:t>" , <NUM_LIT:0> ) , <EOL> AstWhile ( AstBinop ( "<STR_LIT:i>" , <NUM_LIT> , '<STR_LIT:<>' ) , [ <EOL> AstAssign ( "<STR_LIT>" , <NUM_LIT:2> ) , <EOL> AstAssign ( "<STR_LIT>" , <NUM_LIT:1> ) , <EOL> AstWhile ( AstBinop ( AstBinop ( "<STR_LIT>" , "<STR_LIT>" , '<STR_LIT:*>' ) , "<STR_LIT:i>" , "<STR_LIT>" ) , [ <EOL> AstIf ( AstBinop ( AstBinop ( "<STR_LIT:i>" , "<STR_LIT>" , "<STR_LIT:%>" ) , <NUM_LIT:0> , "<STR_LIT>" ) , [ <EOL> AstAssign ( "<STR_LIT>" , <NUM_LIT:0> ) , <EOL> ] , [ <EOL> ] ) , <EOL> AstAssign ( "<STR_LIT>" , AstBinop ( "<STR_LIT>" , <NUM_LIT:1> , "<STR_LIT:+>" ) ) , <EOL> ] ) , <EOL> AstIf ( "<STR_LIT>" , [ <EOL> AstPrint ( "<STR_LIT:i>" ) , <EOL> AstAssign ( "<STR_LIT:t>" , AstBinop ( "<STR_LIT:t>" , "<STR_LIT:i>" , "<STR_LIT:+>" ) ) , <EOL> ] , [ ] ) , <EOL> AstAssign ( "<STR_LIT:i>" , AstBinop ( "<STR_LIT:i>" , <NUM_LIT:1> , "<STR_LIT:+>" ) ) , <EOL> ] ) , <EOL> AstPrint ( "<STR_LIT:t>" ) , <EOL> ] <EOL> run ( prog ) </s>
