import SymbolTable filename = sys.argv[1] ifile = open(filename, 'r') strfile = ifile.read() instr = strfile.split('\n') i = 0 l1 = ['', '\r', '\n'] instr_str = [] for x in instr: if not x.startwith('//'): if x not in l1: instr_str.append(x.strip('\r')) address = 16 sym = SymbolTable.Constructor() for x in instr_str: if x.find('@') >= 0 or x.find('(') >= 0: symbol = Parser.symbol(x) if not SymbolTable.contains(symbol, sym) and not symbol.isdigit(): sym = SymbolTable.addEntry(symbol, address, sym) address = address + 1 while Parser.hasMoreCommands(i, instr_str): c_type = Parser.commandType(instr_str[i]) if c_type == 'A': str1 = Parser.symbol(instr_str[i]) if str1.isdigit(): str1 = bin(int(str1))[2:] address = str1.zfill(16)
#!/usr/bin/python import sys import Parser import Code import SymbolTable filename = sys.argv[1] symboldict = SymbolTable.Constructor() rfile = open(filename, 'r') i = 0 linepre = rfile.readline() flag = Parser.hasMoreCommands(linepre) while flag: while linepre == '\r\n' or linepre.startswith('//'): linepre = rfile.readline() if linepre.find('(') >= 0: linepre = linepre.strip() symbol = linepre.strip('()\n') if not SymbolTable.contains(symbol, symboldict): symboldict = SymbolTable.addEntry(symbol, i, symboldict) else: i += 1 linepre = Parser.advance(rfile, linepre) flag = Parser.hasMoreCommands(linepre) rfile.close() j = 0
import Parser import Code import SymbolTable as st from sys import argv st.Constructor() f = open(argv[1]) lines = f.readlines() symbol_count = 0 cmd_count = 0 bin_output = '' # loop 1, check and build jump table for l in lines: cleaned_str = Parser.clean(l) cmd_type = Parser.commandType(cleaned_str) if cmd_type == Parser.A_COMMAND: cmd_count = cmd_count + 1 if cmd_type == Parser.C_COMMAND: cmd_count = cmd_count + 1 if cmd_type == Parser.L_COMMAND: symbol = cleaned_str[1:-1] st.addEntry(symbol, cmd_count) for l in lines: cleaned_str = Parser.clean(l) cmd_type = Parser.commandType(cleaned_str) # A command