<s> import sys <EOL> sys . path . insert ( <NUM_LIT:0> , "<STR_LIT>" ) <EOL> from ply import * <EOL> import decimal <EOL> tokens = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> def t_NUMBER ( t ) : <EOL> r"""<STR_LIT>""" <EOL> t . value = decimal . Decimal ( t . value ) <EOL> return t <EOL> def t_STRING ( t ) : <EOL> r"<STR_LIT>" <EOL> t . value = t . value [ <NUM_LIT:1> : - <NUM_LIT:1> ] . decode ( "<STR_LIT>" ) <EOL> return t <EOL> t_COLON = r'<STR_LIT::>' <EOL> t_EQ = r'<STR_LIT>' <EOL> t_ASSIGN = r'<STR_LIT:=>' <EOL> t_LT = r'<STR_LIT:<>' <EOL> t_GT = r'<STR_LIT:>>' <EOL> t_PLUS = r'<STR_LIT>' <EOL> t_MINUS = r'<STR_LIT:->' <EOL> t_MULT = r'<STR_LIT>' <EOL> t_DIV = r'<STR_LIT:/>' <EOL> t_COMMA = r'<STR_LIT:U+002C>' <EOL> t_SEMICOLON = r'<STR_LIT:;>' <EOL> RESERVED = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> def t_NAME ( t ) : <EOL> r'<STR_LIT>' <EOL> t . type = RESERVED . get ( t . value , "<STR_LIT>" ) <EOL> return t <EOL> def t_comment ( t ) : <EOL> r"<STR_LIT>" <EOL> pass <EOL> def t_WS ( t ) : <EOL> r'<STR_LIT>' <EOL> if t . lexer . at_line_start and t . lexer . paren_count == <NUM_LIT:0> : <EOL> return t <EOL> def t_newline ( t ) : <EOL> r'<STR_LIT>' <EOL> t . lexer . lineno += len ( t . value ) <EOL> t . type = "<STR_LIT>" <EOL> if t . lexer . paren_count == <NUM_LIT:0> : <EOL> return t <EOL> def t_LPAR ( t ) : <EOL> r'<STR_LIT>' <EOL> t . lexer . paren_count += <NUM_LIT:1> <EOL> return t <EOL> def t_RPAR ( t ) : <EOL> r'<STR_LIT>' <EOL> t . lexer . paren_count -= <NUM_LIT:1> <EOL> return t <EOL> def t_error ( t ) : <EOL> raise SyntaxError ( "<STR_LIT>" % ( t . value [ <NUM_LIT:0> ] , ) ) <EOL> print "<STR_LIT>" , repr ( t . value [ <NUM_LIT:0> ] ) <EOL> t . lexer . skip ( <NUM_LIT:1> ) <EOL> NO_INDENT = <NUM_LIT:0> <EOL> MAY_INDENT = <NUM_LIT:1> <EOL> MUST_INDENT = <NUM_LIT:2> <EOL> def track_tokens_filter ( lexer , tokens ) : <EOL> lexer . at_line_start = at_line_start = True <EOL> indent = NO_INDENT <EOL> saw_colon = False <EOL> for token in tokens : <EOL> token . at_line_start = at_line_start <EOL> if token . type == "<STR_LIT>" : <EOL> at_line_start = False <EOL> indent = MAY_INDENT <EOL> token . must_indent = False <EOL> elif token . type == "<STR_LIT>" : <EOL> at_line_start = True <EOL> if indent == MAY_INDENT : <EOL> indent = MUST_INDENT <EOL> token . must_indent = False <EOL> elif token . type == "<STR_LIT>" : <EOL> assert token . at_line_start == True <EOL> at_line_start = True <EOL> token . must_indent = False <EOL> else : <EOL> if indent == MUST_INDENT : <EOL> token . must_indent = True <EOL> else : <EOL> token . must_indent = False <EOL> at_line_start = False <EOL> indent = NO_INDENT <EOL> yield token <EOL> lexer . at_line_start = at_line_start <EOL> def _new_token ( type , lineno ) : <EOL> tok = lex . LexToken ( ) <EOL> tok . type = type <EOL> tok . value = None <EOL> tok . lineno = lineno <EOL> return tok <EOL> def DEDENT ( lineno ) : <EOL> return _new_token ( "<STR_LIT>" , lineno ) <EOL> def INDENT ( lineno ) : <EOL> return _new_token ( "<STR_LIT>" , lineno ) <EOL> def indentation_filter ( tokens ) : <EOL> levels = [ <NUM_LIT:0> ] <EOL> token = None <EOL> depth = <NUM_LIT:0> <EOL> prev_was_ws = False <EOL> for token in tokens : <EOL> if token . type == "<STR_LIT>" : <EOL> assert depth == <NUM_LIT:0> <EOL> depth = len ( token . value ) <EOL> prev_was_ws = True <EOL> continue <EOL> if token . type == "<STR_LIT>" : <EOL> depth = <NUM_LIT:0> <EOL> if prev_was_ws or token . at_line_start : <EOL> continue <EOL> yield token <EOL> continue <EOL> prev_was_ws = False <EOL> if token . must_indent : <EOL> if not ( depth > levels [ - <NUM_LIT:1> ] ) : <EOL> raise IndentationError ( "<STR_LIT>" ) <EOL> levels . append ( depth ) <EOL> yield INDENT ( token . lineno ) <EOL> elif token . at_line_start : <EOL> if depth == levels [ - <NUM_LIT:1> ] : <EOL> pass <EOL> elif depth > levels [ - <NUM_LIT:1> ] : <EOL> raise IndentationError ( "<STR_LIT>" ) <EOL> else : <EOL> try : <EOL> i = levels . index ( depth ) <EOL> except ValueError : <EOL> raise IndentationError ( "<STR_LIT>" ) <EOL> for _ in range ( i + <NUM_LIT:1> , len ( levels ) ) : <EOL> yield DEDENT ( token . lineno ) <EOL> levels . pop ( ) <EOL> yield token <EOL> if len ( levels ) > <NUM_LIT:1> : <EOL> assert token is not None <EOL> for _ in range ( <NUM_LIT:1> , len ( levels ) ) : <EOL> yield DEDENT ( token . lineno ) <EOL> def filter ( lexer , add_endmarker = True ) : <EOL> token = None <EOL> tokens = iter ( lexer . token , None ) <EOL> tokens = track_tokens_filter ( lexer , tokens ) <EOL> for token in indentation_filter ( tokens ) : <EOL> yield token <EOL> if add_endmarker : <EOL> lineno = <NUM_LIT:1> <EOL> if token is not None : <EOL> lineno = token . lineno <EOL> yield _new_token ( "<STR_LIT>" , lineno ) <EOL> class IndentLexer ( object ) : <EOL> def __init__ ( self , debug = <NUM_LIT:0> , optimize = <NUM_LIT:0> , lextab = '<STR_LIT>' , reflags = <NUM_LIT:0> ) : <EOL> self . lexer = lex . lex ( debug = debug , optimize = optimize , lextab = lextab , reflags = reflags ) <EOL> self . token_stream = None <EOL> def input ( self , s , add_endmarker = True ) : <EOL> self . lexer . paren_count = <NUM_LIT:0> <EOL> self . lexer . input ( s ) <EOL> self . token_stream = filter ( self . lexer , add_endmarker ) <EOL> def token ( self ) : <EOL> try : <EOL> return self . token_stream . next ( ) <EOL> except StopIteration : <EOL> return None <EOL> from compiler import ast <EOL> def Assign ( left , right ) : <EOL> names = [ ] <EOL> if isinstance ( left , ast . Name ) : <EOL> return ast . Assign ( [ ast . AssName ( left . name , '<STR_LIT>' ) ] , right ) <EOL> elif isinstance ( left , ast . Tuple ) : <EOL> names = [ ] <EOL> for child in left . getChildren ( ) : <EOL> if not isinstance ( child , ast . Name ) : <EOL> raise SyntaxError ( "<STR_LIT>" ) <EOL> names . append ( child . name ) <EOL> ass_list = [ ast . AssName ( name , '<STR_LIT>' ) for name in names ] <EOL> return ast . Assign ( [ ast . AssTuple ( ass_list ) ] , right ) <EOL> else : <EOL> raise SyntaxError ( "<STR_LIT>" ) <EOL> def p_file_input_end ( p ) : <EOL> """<STR_LIT>""" <EOL> p [ <NUM_LIT:0> ] = ast . Stmt ( p [ <NUM_LIT:1> ] ) <EOL> def p_file_input ( p ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( p [ len ( p ) - <NUM_LIT:1> ] , basestring ) : <EOL> if len ( p ) == <NUM_LIT:3> : <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] <EOL> else : <EOL> p [ <NUM_LIT:0> ] = [ ] <EOL> else : <EOL> if len ( p ) == <NUM_LIT:3> : <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] + p [ <NUM_LIT:2> ] <EOL> else : <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] <EOL> def p_funcdef ( p ) : <EOL> "<STR_LIT>" <EOL> p [ <NUM_LIT:0> ] = ast . Function ( None , p [ <NUM_LIT:2> ] , tuple ( p [ <NUM_LIT:3> ] ) , ( ) , <NUM_LIT:0> , None , p [ <NUM_LIT:5> ] ) <EOL> def p_parameters ( p ) : <EOL> """<STR_LIT>""" <EOL> if len ( p ) == <NUM_LIT:3> : <EOL> p [ <NUM_LIT:0> ] = [ ] <EOL> else : <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:2> ] <EOL> def p_varargslist ( p ) : <EOL> """<STR_LIT>""" <EOL> if len ( p ) == <NUM_LIT:4> : <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] + p [ <NUM_LIT:3> ] <EOL> else : <EOL> p [ <NUM_LIT:0> ] = [ p [ <NUM_LIT:1> ] ] <EOL> def p_stmt_simple ( p ) : <EOL> """<STR_LIT>""" <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] <EOL> def p_stmt_compound ( p ) : <EOL> """<STR_LIT>""" <EOL> p [ <NUM_LIT:0> ] = [ p [ <NUM_LIT:1> ] ] <EOL> def p_simple_stmt ( p ) : <EOL> """<STR_LIT>""" <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] <EOL> def p_small_stmts ( p ) : <EOL> """<STR_LIT>""" <EOL> if len ( p ) == <NUM_LIT:4> : <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] + [ p [ <NUM_LIT:3> ] ] <EOL> else : <EOL> p [ <NUM_LIT:0> ] = [ p [ <NUM_LIT:1> ] ] <EOL> def p_small_stmt ( p ) : <EOL> """<STR_LIT>""" <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] <EOL> def p_expr_stmt ( p ) : <EOL> """<STR_LIT>""" <EOL> if len ( p ) == <NUM_LIT:2> : <EOL> p [ <NUM_LIT:0> ] = ast . Discard ( p [ <NUM_LIT:1> ] ) <EOL> else : <EOL> p [ <NUM_LIT:0> ] = Assign ( p [ <NUM_LIT:1> ] , p [ <NUM_LIT:3> ] ) <EOL> def p_flow_stmt ( p ) : <EOL> "<STR_LIT>" <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] <EOL> def p_return_stmt ( p ) : <EOL> "<STR_LIT>" <EOL> p [ <NUM_LIT:0> ] = ast . Return ( p [ <NUM_LIT:2> ] ) <EOL> def p_compound_stmt ( p ) : <EOL> """<STR_LIT>""" <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] <EOL> def p_if_stmt ( p ) : <EOL> '<STR_LIT>' <EOL> p [ <NUM_LIT:0> ] = ast . If ( [ ( p [ <NUM_LIT:2> ] , p [ <NUM_LIT:4> ] ) ] , None ) <EOL> def p_suite ( p ) : <EOL> """<STR_LIT>""" <EOL> if len ( p ) == <NUM_LIT:2> : <EOL> p [ <NUM_LIT:0> ] = ast . Stmt ( p [ <NUM_LIT:1> ] ) <EOL> else : <EOL> p [ <NUM_LIT:0> ] = ast . Stmt ( p [ <NUM_LIT:3> ] ) <EOL> def p_stmts ( p ) : <EOL> """<STR_LIT>""" <EOL> if len ( p ) == <NUM_LIT:3> : <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] + p [ <NUM_LIT:2> ] <EOL> else : <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] <EOL> def make_lt_compare ( ( left , right ) ) : <EOL> return ast . Compare ( left , [ ( '<STR_LIT:<>' , right ) , ] ) <EOL> def make_gt_compare ( ( left , right ) ) : <EOL> return ast . Compare ( left , [ ( '<STR_LIT:>>' , right ) , ] ) <EOL> def make_eq_compare ( ( left , right ) ) : <EOL> return ast . Compare ( left , [ ( '<STR_LIT>' , right ) , ] ) <EOL> binary_ops = { <EOL> "<STR_LIT:+>" : ast . Add , <EOL> "<STR_LIT:->" : ast . Sub , <EOL> "<STR_LIT:*>" : ast . Mul , <EOL> "<STR_LIT:/>" : ast . Div , <EOL> "<STR_LIT:<>" : make_lt_compare , <EOL> "<STR_LIT:>>" : make_gt_compare , <EOL> "<STR_LIT>" : make_eq_compare , <EOL> } <EOL> unary_ops = { <EOL> "<STR_LIT:+>" : ast . UnaryAdd , <EOL> "<STR_LIT:->" : ast . UnarySub , <EOL> } <EOL> precedence = ( <EOL> ( "<STR_LIT:left>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT:left>" , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT:left>" , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ) <EOL> def p_comparison ( p ) : <EOL> """<STR_LIT>""" <EOL> if len ( p ) == <NUM_LIT:4> : <EOL> p [ <NUM_LIT:0> ] = binary_ops [ p [ <NUM_LIT:2> ] ] ( ( p [ <NUM_LIT:1> ] , p [ <NUM_LIT:3> ] ) ) <EOL> elif len ( p ) == <NUM_LIT:3> : <EOL> p [ <NUM_LIT:0> ] = unary_ops [ p [ <NUM_LIT:1> ] ] ( p [ <NUM_LIT:2> ] ) <EOL> else : <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] <EOL> def p_power ( p ) : <EOL> """<STR_LIT>""" <EOL> if len ( p ) == <NUM_LIT:2> : <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] <EOL> else : <EOL> if p [ <NUM_LIT:2> ] [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> p [ <NUM_LIT:0> ] = ast . CallFunc ( p [ <NUM_LIT:1> ] , p [ <NUM_LIT:2> ] [ <NUM_LIT:1> ] , None , None ) <EOL> else : <EOL> raise AssertionError ( "<STR_LIT>" ) <EOL> def p_atom_name ( p ) : <EOL> """<STR_LIT>""" <EOL> p [ <NUM_LIT:0> ] = ast . Name ( p [ <NUM_LIT:1> ] ) <EOL> def p_atom_number ( p ) : <EOL> """<STR_LIT>""" <EOL> p [ <NUM_LIT:0> ] = ast . Const ( p [ <NUM_LIT:1> ] ) <EOL> def p_atom_tuple ( p ) : <EOL> """<STR_LIT>""" <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:2> ] <EOL> def p_trailer ( p ) : <EOL> "<STR_LIT>" <EOL> p [ <NUM_LIT:0> ] = ( "<STR_LIT>" , p [ <NUM_LIT:2> ] ) <EOL> def p_testlist ( p ) : <EOL> """<STR_LIT>""" <EOL> if len ( p ) == <NUM_LIT:2> : <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] <EOL> else : <EOL> if isinstance ( p [ <NUM_LIT:1> ] , list ) : <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] <EOL> else : <EOL> p [ <NUM_LIT:0> ] = [ p [ <NUM_LIT:1> ] ] <EOL> if isinstance ( p [ <NUM_LIT:0> ] , list ) : <EOL> p [ <NUM_LIT:0> ] = ast . Tuple ( p [ <NUM_LIT:0> ] ) <EOL> def p_testlist_multi ( p ) : <EOL> """<STR_LIT>""" <EOL> if len ( p ) == <NUM_LIT:2> : <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] <EOL> else : <EOL> if isinstance ( p [ <NUM_LIT:1> ] , list ) : <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] + [ p [ <NUM_LIT:3> ] ] <EOL> else : <EOL> p [ <NUM_LIT:0> ] = [ p [ <NUM_LIT:1> ] , p [ <NUM_LIT:3> ] ] <EOL> def p_test ( p ) : <EOL> "<STR_LIT>" <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] <EOL> def p_arglist ( p ) : <EOL> """<STR_LIT>""" <EOL> if len ( p ) == <NUM_LIT:4> : <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] + [ p [ <NUM_LIT:3> ] ] <EOL> else : <EOL> p [ <NUM_LIT:0> ] = [ p [ <NUM_LIT:1> ] ] <EOL> def p_argument ( p ) : <EOL> "<STR_LIT>" <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] <EOL> def p_error ( p ) : <EOL> raise SyntaxError ( p ) <EOL> class GardenSnakeParser ( object ) : <EOL> def __init__ ( self , lexer = None ) : <EOL> if lexer is None : <EOL> lexer = IndentLexer ( ) <EOL> self . lexer = lexer <EOL> self . parser = yacc . yacc ( start = "<STR_LIT>" ) <EOL> def parse ( self , code ) : <EOL> self . lexer . input ( code ) <EOL> result = self . parser . parse ( lexer = self . lexer ) <EOL> return ast . Module ( None , result ) <EOL> from compiler import misc , syntax , pycodegen <EOL> class GardenSnakeCompiler ( object ) : <EOL> def __init__ ( self ) : <EOL> self . parser = GardenSnakeParser ( ) <EOL> def compile ( self , code , filename = "<STR_LIT>" ) : <EOL> tree = self . parser . parse ( code ) <EOL> misc . set_filename ( filename , tree ) <EOL> syntax . check ( tree ) <EOL> gen = pycodegen . ModuleCodeGenerator ( tree ) <EOL> code = gen . getCode ( ) <EOL> return code <EOL> compile = GardenSnakeCompiler ( ) . compile <EOL> code = r"""<STR_LIT>""" <EOL> def print_ ( * args ) : <EOL> print "<STR_LIT>" , "<STR_LIT:U+0020>" . join ( map ( str , args ) ) <EOL> globals ( ) [ "<STR_LIT>" ] = print_ <EOL> compiled_code = compile ( code ) <EOL> exec compiled_code in globals ( ) <EOL> print "<STR_LIT>" </s>
