예제 #1
0
    def main():
        filename = sys.argv[1]
        output = open(filename.split('.')[0] + '.hack', 'w')

        firstPass = Parser(filename)
        symbolTable = SymbolTable()
        rom_address = 0
        ramAddress = 16

        # First pass adds L_COMMANDs and ROM addresses to symbol table
        while firstPass.hasMoreCommands():
            firstPass.advance()
            command = firstPass.commandType()
            if command == 'A_COMMAND' or command == 'C_COMMAND':
                rom_address += 1
            elif command == 'L_COMMAND':
                symbolTable.addEntry(firstPass.symbol(), rom_address)

        # When A_COMMAND is encountered:
        #   if symbol is a digit write it to file
        #   if symbol is not a digit, look it up in the symbol table. If it's there, write the address
        #   if symbol is not a digit, look it up in the symbol table. If it is not there, add it then write the address
        secondPass = Parser(filename)
        while secondPass.hasMoreCommands():
            secondPass.advance()
            command = secondPass.commandType()
            symbol = secondPass.symbol()
            if command == 'A_COMMAND' and symbol:
                if symbol.isdigit():
                    output.write('0' + '{0:015b}'.format(int(symbol)) + '\n')
                elif symbolTable.contains(symbol):
                    symbolAddress = symbolTable.getAddress(symbol)
                    output.write('0' + '{0:015b}'.format(int(symbolAddress)) +
                                 '\n')
                else:
                    symbolTable.addEntry(symbol, ramAddress)
                    ramAddress += 1
                    symbolAddress = symbolTable.getAddress(symbol)
                    output.write('0' + '{0:015b}'.format(int(symbolAddress)) +
                                 '\n')
            else:
                dest = Code.dest(secondPass.dest())
                jump = Code.jump(secondPass.jump())
                comp = Code.comp(secondPass.comp())
                if comp != None:
                    output.write('111' + comp + dest + jump + '\n')

        output.close()
예제 #2
0
OUT=open(out_file,'w')
par2 = Parser(in_file,extended_op)

toWrite=''
output=''

#Keeps count for general error reporting
overall_line_num=0

#Builds the output
while par2.hasMoreCommands():
	par2.advance()

	if re.search('A_COMMAND',par2.commandType()) is not None:
		toWrite+=symT.getAddress(par2.symbol())

	elif re.search('C_COMMAND',par2.commandType()) is not None:
		toWrite='111' #Begining of c instruction
		
		#If m is involved in the computation sets a to 1
		if re.search('.*M.*',par2.comp()) is None:
			toWrite+='0'
		else:
			toWrite+='1'

		toWrite+=code.comp(par2.comp(),overall_line_num)
		toWrite+=code.dest(par2.dest(),overall_line_num,extended_op)
		toWrite+=code.jump(par2.jump(),overall_line_num)
	
	overall_line_num+=1
예제 #3
0
current_directory = os.path.dirname(os.path.abspath(__file__))
outfilename = os.path.join(current_directory, os.path.splitext(basename)[0] + '.hack')
outfile = open(outfilename, 'w')

next_available_ram_address = '16'

while parser.hasMoreCommands():
    if parser.getLabel():
        # if its a label, skip this line and go to next loop iteration
        parser.advance()
        continue
    elif parser.getCommandType() == 'A':
        value = parser.address()
        if parser.isSymbol():
            # add new symbol to the table if it's not already there
            if not symbol_table.getAddress(value):
                symbol_table.updateTable(value, next_available_ram_address)
                next_available_ram_address = str(int(next_available_ram_address) + 1)
            value = symbol_table.getAddress(value)
        out = Code.toBinary(value, 16)
    else:
        d = parser.dest() 
        c = parser.comp() 
        j = parser.jump()
        dd = Code.dest(d)
        cc = Code.comp(c)
        jj = Code.jump(j)
        out = "111" + cc + dd + jj
    outfile.write(out + '\n')
    parser.advance()