jb302@1: #!/usr/bin/env python2 jb302@1: # assembler.py jb302@1: import struct jb302@1: import sys jb302@1: from language import * jb302@1: jb302@1: # take source file and return preprocessed assembly code jb302@1: # for each non-empty line in the file: jb302@1: # remove comments from source jb302@25: # replace equated strings jb302@25: # store label definitions and remove label from source jb302@25: # store new equates jb302@25: # make hashable format symbol from arguments jb302@25: # identify and save constant data jb302@25: # save instruction, arguments, symbol and data to list jb302@25: # also prepares org and db instructions for second_pass() jb302@1: def first_pass(f): jb302@25: asm = [] jb302@25: labels = {} jb302@25: equates = {} jb302@25: pc = 0 jb302@25: jb302@1: # read file into list, remove blank line jb302@1: f.seek(0) jb302@25: source_code = filter(lambda l: l != '\n', f.readlines()) jb302@14: jb302@1: # ::= [] [";"] jb302@22: for line in source_code: jb302@18: try: jb302@22: # remove trailing whitespace and comments jb302@18: line = line.strip() jb302@18: for i in range(len(line)): jb302@18: if line[i] == ';': jb302@18: line = line[:i] jb302@18: break jb302@18: jb302@18: # ::= [