Exemple #1
0
def main():
    if sys.argv[1][-2:] != "vm":
        print("Error: wrong file type for input, use \".vm\" file !")
        sys.exit()

    inputfile = sys.argv[1]
    #inputfile = "BasicTest.vm"
    outputfile = inputfile[:-2] + "asm"

    par = Parse(inputfile)
    cw = CodeWriter(outputfile)

    while par.hasMoreCommands():
        par.advance()
        ctype = par.cmdType()
        #print(par.current_cmd)
        #print(par.cmdType())
        #print(par.arg1())
        #print(par.arg2())
        if ctype == "C_ARITHMETIC":
            cw.writeArithmetic(par.arg1())
        elif ctype == "C_PUSH" or ctype == "C_POP":
            cw.writePushPop(ctype, par.arg1(), par.arg2())

    cw.close()
Exemple #2
0
		#sets current file name
		writer.setFileName(out_file)

		#opens the input file
		par = Parser(d)
		#while not eof
		while par.hasMoreCommands():
			par.advance()
			cType = par.commandType()

			if arith_type in cType:
				writer.writeArithmetic(par.arg_1())
			
			elif push_type in cType:
				if len(par.arg_2()) == 0:
					writer.writePushPop(cType,'constant',par.arg_1())
				else:
					writer.writePushPop(cType,par.arg_1(),par.arg_2())

			elif pop_type in cType:
				if len(par.arg_2()) == 0:
					writer.writePushPop(cType,'constant',par.arg_1())
				else:
					writer.writePushPop(cType,par.arg_1(),par.arg_2())

			elif funct_type in cType:
				writer.writeFunction(par.arg_1(),par.arg_2())

			elif if_type in cType:
				writer.writeIf(par.arg_1())