Exemple #1
0
if os.path.isdir(inputVMFilePath):
    fileNames = filter(lambda f: f.endswith('.vm'), 
                       os.listdir(inputVMFilePath))

    # Make sure that 'Sys.vm' is first in the list
    if fileNames.count('Sys.vm'):
        fileNames.remove('Sys.vm')
        fileNames.insert(0, 'Sys.vm')

    # Make file relative paths from file names
    filePaths = [os.path.join(inputVMFilePath, fileName) for fileName in fileNames]

    # Calculate output file path
    outputFileName = os.path.basename(os.path.normpath(inputVMFilePath)) + '.asm'
    outputFilePath = os.path.join(inputVMFilePath, outputFileName)
else:
    # This is the only file path to translate
    filePaths = [inputVMFilePath]

    # Calculate the output file
    outputDirectory = os.path.dirname(inputVMFilePath)
    outputFileName = os.path.basename(inputVMFilePath).split('.vm')[0] + '.asm'
    outputFilePath = os.path.join(outputDirectory, outputFileName)

# Write commands to output file
with open(outputFilePath, 'w') as outputStream:
    for vmFilePath in filePaths:
        VMCommandsToHack.writeHackCommandsToFile(outputStream,
                                                 vmFilePath,
                                                 Parser.parseVirtualCommands(vmFilePath))
Exemple #2
0
import Parser
import VMCommandsToHack
import sys
import argparse

if len(sys.argv) == 1:
    print "Please enter a file name"
    sys.exit()

inputVMFilePath = sys.argv[1]

outputHackFilePath = inputVMFilePath.replace(".vm", ".asm")     #store asm file to the same location
VMCommandsToHack.writeHackCommandsToFile(outputHackFilePath, Parser.parseVirtualCommands(inputVMFilePath))