コード例 #1
0
def main():
    #put the files in the same folder as the code and put the names in the filenames array, pass codewriter the name you want to output or filename[x] if you want it to be the same name with .asm
    #filenames = ["BasicTest","PointerTest","SimpleAdd","StackTest","StaticTest"]

    filesnames = []
    inputList = []
    ##put the file directory here
    filesPath = "C:\\Users\\drumm\\OneDrive\\Documents\\Code\\nand2tetris\\projects\\08\\FunctionCalls\\StaticsTest\\"
    runInit = False

    inputList = glob.glob(filesPath + "*.vm")
    for x in range(0, len(inputList)):
        filesnames.append(inputList[x].split('\\')[-1].replace('.vm', ''))

    if len(inputList) > 1:
        runInit = True

    if runInit == True:
        codewriter = CodeWriter(
            filesPath + inputList[0].split('\\')[-2].replace('.vm', '') +
            ".asm")
        codewriter.writeInit()
    else:
        codewriter = CodeWriter(filesPath + filesnames[0] + ".asm")

    for x in range(0, len(inputList)):

        filename = inputList[x]
        parser = Parser(filename)

        while parser.hasMoreCommands():
            parser.advance()
            if parser.arg1() == 'push' or parser.arg1() == 'pop':
                codewriter.WritePushPop(
                    parser.arg1(), parser.arg2(), parser.arg3(),
                    filename.split('\\')[-1].replace('.vm', ''))
            elif parser.commandType() == 'C_ARITHMETIC':
                codewriter.writeArithmetic(parser.currentCommand)
            elif parser.commandType() == 'C_LABEL':
                codewriter.writeLabel(parser.arg2())
            elif parser.commandType() == 'C_GOTO':
                codewriter.writeGoto(parser.arg2())
            elif parser.commandType() == 'C_IF':
                codewriter.writeIf(parser.arg2())
            elif parser.commandType() == 'C_CALL':
                codewriter.writeCall(parser.arg2(), parser.arg3())
            elif parser.commandType() == 'C_RETURN':
                codewriter.writeReturn()
            elif parser.commandType() == 'C_FUNCTION':
                codewriter.writeFunction(parser.arg2(), parser.arg3())
            else:
                print("hitelse")

        parser.close()

    codewriter.Close()
コード例 #2
0
def main():
    "output file setup"
    filename = sys.argv[1]
    output = CodeWriter(filename)

    "input file setup"
    input = Parser()
    commandlines = input.commandLines(filename)
    for line in commandlines:
        if input.typeCommand(line) == 'C_ARITHMETIC':
            output.writeArithmetic(line)
            output.Write()
        elif input.typeCommand(line) in ['C_PUSH', 'C_POP']:
            output.writePushPop(line)
            output.Write()
    output.Close()
コード例 #3
0
def main():
	"output file setup"
	arg_length = len( sys.argv ) - 2
	if arg_length == 0:
		objectFileName = sys.argv[1].split('.')[0]
		filename = sys.argv[1]
		output = CodeWriter(objectFileName, filename)
		input = Parser( )
		commandlines = input.commandLines( filename )
		for line in commandlines:
			if input.typeCommand( line ) == 'C_ARITHMETIC':
				output.writeArithmetic( line )
				output.Write()
			elif input.typeCommand( line ) in [ 'C_PUSH', 'C_POP' ]:
				output.writePushPop( line )
				output.Write()
			elif input.typeCommand( line ) == 'C_LABEL':
				output.writeLabel( line )
				output.Write()
			elif input.typeCommand( line ) == 'C_GOTO':
				output.writeGoto( line )
				output.Write()
			elif input.typeCommand( line ) == 'C_IFGOTO':
				output.writeIfGoTo( line )
				output.Write()
			elif input.typeCommand( line ) == 'C_FUNCTION':
				#output.writeInit( line )
				output.writeFunction( line )
				output.Write()
			elif input.typeCommand( line ) == 'C_RETURN':
				output.writeReturn( line )
				output.Write()
			elif input.typeCommand( line ) == 'C_CALL':
				output.writeCall( line )
				output.Write()
	else:
		count = 0
		objectFileName = sys.argv[-1]
		while count < arg_length:
			"input file setup"
			filename = sys.argv[ count + 1 ]
			output = CodeWriter(objectFileName, filename)
			input = Parser( )
			commandlines = input.commandLines( filename )
			for line in commandlines:
				if input.typeCommand( line ) == 'C_ARITHMETIC':
					output.writeArithmetic( line )
					output.Write()
				elif input.typeCommand( line ) in [ 'C_PUSH', 'C_POP' ]:
					output.writePushPop( line )
					output.Write()
				elif input.typeCommand( line ) == 'C_LABEL':
					output.writeLabel( line )
					output.Write()
				elif input.typeCommand( line ) == 'C_GOTO':
					output.writeGoto( line )
					output.Write()
				elif input.typeCommand( line ) == 'C_IFGOTO':
					output.writeIfGoTo( line )
					output.Write()
				elif input.typeCommand( line ) == 'C_FUNCTION':
					#output.writeInit( line )
					output.writeFunction( line )
					output.Write()
				elif input.typeCommand( line ) == 'C_RETURN':
					output.writeReturn( line )
					output.Write()
				elif input.typeCommand( line ) == 'C_CALL':
					output.writeCall( line )
					output.Write()
			count += 1

	output.Close()