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
		#Strips .vm of the end to get the file name
		temp_out=re.search('(.*[\.\/]+)(.*)(\.vm)',d)
		out_file = temp_out.group(2)

		#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())
Exemple #3
0
from parser import Parser
from codewriter import CodeWriter
import sys
import re

if __name__ == "__main__":
    path = sys.argv[1]
    codewriter = CodeWriter(path)
    parser = Parser(path)

    while parser.hasMoreCommands():
        parser.advance()
        print(parser.current_cmd)
        cmdtype = parser.commandType()
        arg1 = parser.arg1()
        if cmdtype == 'C_ARITHMETIC':
            codewriter.writeArithmetic(arg1)
        elif cmdtype in ['C_PUSH', 'C_POP']:
            arg2 = parser.arg2()
            codewriter.wirtePushPop(cmdtype, arg1, arg2)

    codewriter.fileToOutput()