def translate_file(path, destination_path): write_path = os.path.join( destination_path, '.'.join(os.path.basename(path).split('.')[:-1]) + OUTPUT_FILE_EXTENSION) with open(path) as reader, open(write_path, 'w') as writer: file_name = "".join(ntpath.basename(path).split('.')[:-1]) code_writer = CodeWriter(writer, file_name) input_line = reader.readline() while input_line: if parser.is_command(input_line): parsed_line = parser.parse(input_line) code_writer.write_command(parsed_line) input_line = reader.readline()
def translate(fileName): import codeWriter from codeWriter import CodeWriter import stackParser from stackParser import Parser import os files = [] ext = os.path.splitext(fileName)[1] if ext == '': outputFile = open( os.path.join(fileName, os.path.basename(os.path.abspath(fileName)) + ".asm"), 'w') for currentFile in os.listdir(fileName): if os.path.splitext(currentFile)[1] == '.vm': files.append(os.path.join(fileName, currentFile)) elif ext == '.vm': outputFile = open(os.path.splitext(fileName)[0] + ".asm", 'w') files.append(fileName) else: raise Exception( "File provided doesn't have a .vm extension and is not a directory" ) codeWriter = CodeWriter(outputFile) codeWriter.writeInit() for currentFile in files: file = open(currentFile, 'r') parser = Parser(file) parse(parser, codeWriter, os.path.basename(currentFile)) codeWriter.close()
codeWriter.writeLabel(arg1) elif command == 'C_GOTO': codeWriter.writeGoto(arg1) elif command == 'C_IF': codeWriter.writeIf(arg1) elif command == 'C_FUNCTION': codeWriter.writeFucntion(arg1, arg2) elif command == 'C_RETURN': codeWriter.writeReturn() elif command == 'C_CALL': codeWriter.writeCall(arg1, arg2) path = sys.argv[1] if os.path.isfile(path): codeWriter = CodeWriter(path.replace('vm', 'asm')) translate(path) elif os.path.isdir(path): if path[-1] != '/': path = path + '/' codeWriter = CodeWriter(path + path.split('/')[-2] + '.asm') files = glob.glob(path + '*.vm') for file in files: translate(file) else: raise Exception('path error!!') codeWriter.close()
from vmParser import Parser from codeWriter import CodeWriter filename = '../MemoryAccess/BasicTest/BasicTest.vm' parser_ = Parser() parser_.read_file(filename) parser_.parse_vm_code() parser_.close_file() codeWriter = CodeWriter() codeWriter.gen_hack_code(parser_.lines) codeWriter.write_to_file(filename.split('.vm')[0] + '.asm')
import os from vmParser import Parser from codeWriter import CodeWriter path = os.path.join('..', 'FunctionCalls', 'FibonacciElement') #path = os.path.join('..', 'ProgramFlow', 'BasicLoop', 'BasicLoop.vm') parser_ = Parser() parser_.read(path) codeWriter = CodeWriter() codeWriter.gen_hack_code(parser_.lines, parser_.is_dir) codeWriter.write_to_file(path)
from vmParser import ParseVM from codeWriter import CodeWriter from constants import * if __name__ == "__main__": argParser = argparse.ArgumentParser() argParser.add_argument('filename', help="The path to the .vm file") args = argParser.parse_args() if not args.filename.endswith(".vm"): raise Exception("You can only parse .vm files") # Create parse object vmParseObj = ParseVM(filename=args.filename) # Create code writer object asmWriter = CodeWriter(filename=args.filename) while vmParseObj.hasMoreCommands: # Get command line = vmParseObj.advance # Get command type cmd = vmParseObj.commandType(line) # Get arg 1 segment = vmParseObj.arg1(line) # Get arg 2 index = vmParseObj.arg2(line) # Write comment asmWriter.openFile.write("// {}\n".format(line)) # Write arithmetic (if command is arithmetic) asmWriter.writeArithmetic(cmd, segment) # Write stack command
from constants import * if __name__ == "__main__": argParser = argparse.ArgumentParser() argParser.add_argument('filename', help="The path to the .vm file") args = argParser.parse_args() filePath = [] if args.filename.endswith(".vm"): filePath.append(args.filename) else: # Entry is a directory filePath = list(Path(args.filename).glob('**/*.vm')) directory = filePath[0].parent # Create code writer object asmWriter = CodeWriter(filename=str(directory / directory.name) + '.asm') asmWriter.writeInit() asmWriter.writeCall(C_CALL, "Sys.init", 0) for eachFile in filePath: asmWriter.setFilename(str(eachFile)) # Create parse object vmParseObj = ParseVM(filename=str(eachFile)) while vmParseObj.hasMoreCommands: # Get command line = vmParseObj.advance # Get command type cmd = vmParseObj.commandType(line) # Get arg 1