def run(self, filename): print("Filename: " + str(filename)) # Second pass outputFilename = os.path.splitext(filename)[0] + ".asm" print("outputFilename: " + str(outputFilename)) p = parser.Parser(filename) cw = code_writer.CodeWriter(outputFilename) print("Start advance") p.advance() while p.hasMoreCommands(): commandType = p.commandType() if commandType == command_types.C_PUSH or commandType == command_types.C_POP: cw.WritePushPop(commandType, p.arg1(), p.arg2()) elif commandType == command_types.C_ARITHMETIC: cw.WriteArithmetic(p.arg1()) else: # Unimplemented pass p.advance() print("Finished") cw.Close()
def main(filename): # Second pass outputFilename = os.path.splitext(filename)[0] + ".asm" p = parser.Parser(filename) cw = code_writer.CodeWriter(outputFilename) p.advance() while p.hasMoreCommands(): commandType = p.commandType() if commandType == command_types.C_PUSH or commandType == command_types.C_POP: cw.WritePushPop(commandType, p.arg1(), p.arg2()) elif commandType == command_types.C_ARITHMETIC: cw.WriteArithmetic(p.arg1()) else: # Unimplemented pass p.advance()
#!/usr/bin/python3 import sys import constants as c import code_writer import parser file_name = sys.argv[1] p = parser.Parser(file_name) cw = code_writer.CodeWriter(file_name) while (p.has_more_commands()): p.advance() c_type = p.command_type() if (c_type == c.C_ARITHMETIC): cw.write_arithmetic(p) elif (c_type in [c.C_PUSH, c.C_POP]): cw.write_push_pop(p) cw.close()
help="Path to compiled .asm file to output", type=str) parser.add_argument("--do_bootstrap", help="Generate bootstrap code.", action='store_true') args = parser.parse_args() if os.path.isdir(args.vmpath): vmpaths = [ os.path.join(args.vmpath, p) for p in os.listdir(args.vmpath) if p.endswith('.vm') ] else: vmpaths = [args.vmpath] codeWriter = code_writer.CodeWriter(args.outpath, args.do_bootstrap) for vmpath in vmpaths: print(vmpath) codeWriter.setFileName(vmpath) while True: command_type = codeWriter.vmParser.commandType() if command_type == codeWriter.vmParser.COMMAND_TYPES[0]: # C_ARITHMETIC. command = codeWriter.vmParser.arg1() codeWriter.writeArithmetic(command) elif command_type in codeWriter.vmParser.COMMAND_TYPES[1:3]: # C_PUSH or C_POP. segment = codeWriter.vmParser.arg1() value = codeWriter.vmParser.arg2()
def __init__(self, asmFilename, writeInit=False): self.cw = code_writer.CodeWriter(asmFilename) if writeInit: self.cw.WriteInit()
print('Writing function:', parser.arg1(), parser.arg2()) cw.writeFunction(parser.arg1(), parser.arg2()) elif cmd_type == C_CALL: print('Writing Call:', parser.arg1(), parser.arg2()) cw.writeCall(parser.arg1(), parser.arg2()) elif cmd_type == C_RETURN: print('Writing return') cw.writeReturn() file_name = sys.argv[1] if os.path.isfile(file_name): output = '%s.asm' % os.path.splitext(file_name)[0] cw = code_writer.CodeWriter(output) cw.set_input_f_name(file_name) parse_a_file(file_name, cw) else: dir_name = sys.argv[1] # Get last part of dirtectory name. Example: a/b => b output = '%s.asm' % os.path.basename(dir_name) print(dir_name, output) cw = code_writer.CodeWriter(join(dir_name, output)) cw.writeInit() for f in listdir(dir_name): if isfile(join(dir_name, f)) and f.endswith('.vm'): cw.set_input_f_name(f) parse_a_file(join(dir_name, f), cw)
import argparse import os import vmparser import code_writer if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument("vmpath", help="Path to .vm file to compile", type=str) parser.add_argument("outpath", help="Path to compiled .asm file to output", type=str) args = parser.parse_args() codeWriter = code_writer.CodeWriter(args.outpath) codeWriter.setFileName(args.vmpath) while True: command_type = codeWriter.vmParser.commandType() if command_type == codeWriter.vmParser.COMMAND_TYPES[0]: # C_ARITHMETIC. command = codeWriter.vmParser.arg1() codeWriter.writeArithmetic(command) elif command_type in codeWriter.vmParser.COMMAND_TYPES[1:3]: # C_PUSH or C_POP. segment = codeWriter.vmParser.arg1() value = codeWriter.vmParser.arg2() assert value.isdigit() codeWriter.writePushPop(command_type, segment, value)