def build_symbol_table(program):
    symbols = SymbolTable()
    i = 0
    for line in program:
        if "(" in line:
            line = line.replace("(", "").replace(")", "")
            symbols.add_label(line, i)
            i -= 1
        i += 1
    return symbols
Exemple #2
0
translated_program = []

# Create the SymbolTable and add the built-in symbols
symbol_table = SymbolTable()

# Do a first run of the assembly program.
# Scan for (XXX) labels and add them to symbol_table
while not assembly_program.is_parsed():
    current_command = assembly_program.advance()
    command_type = assembly_program.get_command_type(current_command)

    if command_type == 'L_COMMAND':
        label_name = current_command[1:len(current_command) - 1]

        if not symbol_table.contains_symbol(label_name):
            symbol_table.add_label(label_name, assembly_program.line_counter)

# Removes all labels from assembly program and restarts it
assembly_program.assembly_program = assembly_program.remove_labels()
assembly_program.line_counter = 0

# Do a second run of the assembly program
# Adds all @symbols to symbol table, with a corresponding address starting from 16
# If the address is already taken increment it by 1 until a free spot in RAM is found
# Generates all A and C-command mnemonics and binaries
while not assembly_program.is_parsed():
    current_command = assembly_program.advance()
    command_type = assembly_program.get_command_type(current_command)

    if command_type == 'A_COMMAND':
        symbol = current_command[1:]