Esempio n. 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()
Esempio n. 2
0
def write_vm1(inputfile, in_filename, ctabel, cw):
    par = Parse(inputfile, ctabel)
    cw.setInputname(in_filename)

    while par.hasMoreCommands():
        par.advance()
        ctype = par.cmdType()
        if ctype == "C_ARITHMETIC":
            cw.writeArithmetic(par.arg1())
        elif ctype == "C_PUSH" or ctype == "C_POP":
            cw.writePushPop(ctype, par.arg1(), int(par.arg2()))
        elif ctype == "C_LABEL":
            cw.writeLabel(par.arg1())
        elif ctype == "C_IF":
            cw.writeIf(par.arg1())
        elif ctype == "C_GOTO":
            cw.writeGoto(par.arg1())
        elif ctype == "C_FUNCTION":
            cw.writeFunction(par.arg1(), int(par.arg2()))
        elif ctype == "C_RETURN":
            cw.writeReturn()
        elif ctype == "C_CALL":
            cw.writeCall(par.arg1(), int(par.arg2()))