<s> import pl0_machine <EOL> import sys <EOL> import StringIO <EOL> import re <EOL> label_re = re . compile ( '<STR_LIT>' ) <EOL> whitespace_re = re . compile ( '<STR_LIT>' ) <EOL> comment_re = re . compile ( '<STR_LIT>' ) <EOL> def is_integer ( string ) : <EOL> try : <EOL> int ( string ) <EOL> except ValueError : <EOL> return False <EOL> return True <EOL> def assemble ( input ) : <EOL> buffer = [ ] <EOL> labels = { } <EOL> for line in input : <EOL> if comment_re . match ( line ) : <EOL> continue <EOL> match = label_re . match ( line ) <EOL> line = line . strip ( ) <EOL> if line == '<STR_LIT>' : <EOL> continue <EOL> if match : <EOL> labels [ match . group ( <NUM_LIT:1> ) ] = len ( buffer ) <EOL> else : <EOL> command = re . split ( whitespace_re , line . strip ( ) ) <EOL> for argument in command : <EOL> if is_integer ( argument ) : <EOL> buffer . append ( int ( argument ) ) <EOL> elif pl0_machine . OPCODES . has_key ( argument ) : <EOL> buffer . append ( pl0_machine . OPCODES [ argument ] ) <EOL> else : <EOL> buffer . append ( argument ) <EOL> return list ( labels . get ( x , x ) for x in buffer ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> code = assemble ( sys . stdin ) <EOL> print ` code ` </s>
