当前位置: 首页>>代码示例>>Python>>正文


Python Parser.arg1方法代码示例

本文整理汇总了Python中Parser.Parser.arg1方法的典型用法代码示例。如果您正苦于以下问题:Python Parser.arg1方法的具体用法?Python Parser.arg1怎么用?Python Parser.arg1使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Parser.Parser的用法示例。


在下文中一共展示了Parser.arg1方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

# 需要导入模块: from Parser import Parser [as 别名]
# 或者: from Parser.Parser import arg1 [as 别名]
def main():
    input_path = sys.argv[FILE_POS]
    if isdir(input_path):
        input_list = [ input_path + "/" +f for f in listdir(input_path)
            if (isfile(join(input_path, f))) and (f.endswith(".vm")) ]
        # check if we're getting a path or something else
        index = input_path.rfind("/")
        name = input_path[index + 1:]
        output_file_name = input_path + "/" + name + ".asm"
    else:
        input_list = [input_path]
        index = input_path.index(".vm")
        output_file_name = input_path[:index] + ".asm"

    code_writer = CodeWriter(output_file_name)

    for input_file in input_list:
        parser = Parser(input_file)
        code_writer.set_file_name(input_file)
        while parser.has_more_commands():
            command = parser.advance()
            if parser.command_type() == C_ARITHMETIC:
                code_writer.write_arithmetic(command)
            if (parser.command_type() == C_PUSH) or (parser.command_type() == C_POP):
                code_writer.write_push_pop(parser.command_type(), parser.arg1(), parser.arg2())

    code_writer.close()
开发者ID:hadarfranco,项目名称:From-NAND-to-Tetris,代码行数:29,代码来源:VMtranslator.py

示例2: __init__

# 需要导入模块: from Parser import Parser [as 别名]
# 或者: from Parser.Parser import arg1 [as 别名]

#.........这里部分代码省略.........




    def translatePushPop(self, command):
        '''
        Return assembly code for a given push/pop VM command, ie push local 2
        Calls translatePush or translatePop depending on type
        :param command:
        :return:
        '''

        self.parser.setInstruction(command)
        if (self.parser.currentInstructionType != self.parser.C_PUSH
            and self.parser.currentInstructionType != self.parser.C_POP):
            raise RuntimeError("Translate Push Pop failed - not C_PUSH or C_POP")

        if self.parser.currentInstructionType == self.parser.C_POP:
            return self.translatePop(command)
        else:
            return self.translatePush(command)





    def translatePush(self, command):
        '''
        return assembly instructions for any push VM command
        :param command:
        :return:
        '''
        self.parser.setInstruction(command)
        label = self.parser.arg1()
        offset = int(self.parser.arg2())

        if label == "local":
            return self.translatePushLocal(offset)
        elif label == "argument":
            return self.translatePushArgument(offset)
        elif label == "pointer":
            return self.translatePushPointer(offset)
        elif label == "static":
            return self.translatePushStatic(offset)
        elif label == "temp":
            return self.translatePushTemp(offset)
        elif label == "this":
            return self.translatePushThis(offset)
        elif label == "that":
            return self.translatePushThat(offset)
        elif label == "constant":
            return self.translatePushConstant(offset)


    def translatePopTemp(self, constant):
        '''

        :param constant: integer
        :return:
        '''

        return ["@SP",
                "M=M-1",
                "A=M",
                "D=M",
                "@" + str(5 + constant),
开发者ID:dserver,项目名称:Nand2Tetris,代码行数:70,代码来源:CodeWriter.py

示例3: len

# 需要导入模块: from Parser import Parser [as 别名]
# 或者: from Parser.Parser import arg1 [as 别名]
#/usr/bin/python

import sys
from Parser import Parser
from Coder import Coder

if len(sys.argv) < 3:
    print 'Please enter input and output filenames'
    sys.exit(1)
else:
    input_files = sys.argv[1:len(sys.argv)-1]
    output_file = sys.argv[len(sys.argv)-1:]

coder = Coder(output_file[0])

for input_file in input_files:
    coder.set_filename(input_file)
    parser = Parser(input_file)
    while(parser.has_more_commands()):
        parser.advance()
        command_type = parser.command_type()
        if command_type == 'C_PUSH' or command_type == 'C_POP':
            coder.write_push_pop(command_type, parser.arg1(), int(parser.arg2()))
        if command_type == 'C_ARITHMETIC':
            coder.write_arithmetic(parser.arg1())
开发者ID:ganesshkumar,项目名称:Computer-0.1,代码行数:27,代码来源:VMTranslator.py

示例4: main

# 需要导入模块: from Parser import Parser [as 别名]
# 或者: from Parser.Parser import arg1 [as 别名]
def main(args):
    '''
    You can set vm_file_path to be either a folder (and then an asm file
    with the folder source_file will be created in the folder) or set it to be
    a vm file (and then an asm file with the file source_file will be created
    in the same location)
    '''
    if len(args) != 1:
        print "Usage: (python) Main.py [<.vm file path>|<source dir path>]"
        return

    vm_file_path = args[0]

    # vm_file_path = "Input/StackArithmetic/SimpleAdd/SimpleAdd.vm"
    # vm_file_path = "Input/StackArithmetic/StackTest/StackTest.vm"
    # vm_file_path = "Input/MemoryAccess/BasicTest/BasicTest.vm"
    # vm_file_path = "Input/MemoryAccess/PointerTest/PointerTest.vm"
    # vm_file_path = "Input/MemoryAccess/StaticTest/StaticTest.vm"
    # vm_file_path = "Input/ProgramFlow/BasicLoop/BasicLoop.vm"
    # vm_file_path = "Input/ProgramFlow/FibonacciSeries/FibonacciSeries.vm"
    # vm_file_path = "Input/FunctionCalls/SimpleFunction/SimpleFunction.vm"

    init_code_required = False
    source_file_paths = []
    sep = '/' if '/' in vm_file_path else os.sep

    if not vm_file_path.endswith(".vm"):
        source_file_names = [file_name for file_name in
                             os.listdir(vm_file_path) if
                             file_name.endswith('.vm')]
        source_file_paths += [os.path.join(vm_file_path, file_name) for
                              file_name in source_file_names]
        init_code_required = 'Sys.vm' in source_file_names
        asm_file_name = "{0}.asm".format(vm_file_path.split(sep)[-2])
        asm_file_path = os.path.join(vm_file_path, asm_file_name)
    else:
        source_file_paths = [vm_file_path]
        asm_file_path = vm_file_path.replace(".vm", ".asm")

    cw = CodeWriter(asm_file_path)
    if init_code_required:
        cw.writeInit()
        cw.writeFinishLoop()

    for source_file in source_file_paths:
        cw.setFileName(source_file)
        p = Parser(source_file)

        while(p.hasMoreCommands()):
            cmdType = p.commandType()

            if(cmdType == CommandType.C_ARITHMETIC):
                cw.writeArithmetic(p.arg1())

            elif(cmdType == CommandType.C_PUSH or
                 cmdType == CommandType.C_POP):
                cw.writePushPop(cmdType, p.arg1(), p.arg2())

            elif(cmdType == CommandType.C_LABEL):
                cw.writeLabel(p.arg1())

            elif(cmdType == CommandType.C_GOTO):
                cw.writeGoto(p.arg1())

            elif(cmdType == CommandType.C_IF):
                cw.writeIf(p.arg1())

            elif(cmdType == CommandType.C_CALL):
                cw.writeCall(p.arg1(), p.arg2())

            elif(cmdType == CommandType.C_FUNCTION):
                cw.writeFunction(p.arg1(), p.arg2())

            elif(cmdType == CommandType.C_RETURN):
                cw.writeReturn()

            p.advance()

    cw.Close()
开发者ID:bendanon,项目名称:n2t-proj7,代码行数:81,代码来源:Main.py

示例5: CodeWriter

# 需要导入模块: from Parser import Parser [as 别名]
# 或者: from Parser.Parser import arg1 [as 别名]
finally:
	outFile.close()



writer = CodeWriter(outPath)

for f in vmNames:
	fn = path.basename(f)
	print "Working on:",fn
	writer.setFileName(fn)
	parser = Parser(f)
	while parser.hasMoreCommands():
		parser.advance()
		if parser.commandType() == "C_ARITHMETIC":
			writer.writeArithmetic(parser.arg1())
		elif parser.commandType() == "C_PUSH":
			writer.writePushPop("PUSH", parser.arg1(), parser.arg2())
		elif parser.commandType() == "C_POP":
			writer.writePushPop("POP", parser.arg1(), parser.arg2())
		elif parser.commandType() == "C_LABEL":
			writer.writeLabel(parser.arg1())
		elif parser.commandType() == "C_GOTO" or parser.commandType() == "C_IF-GOTO":
			writer.writeGoto(parser.commandType()[2:], parser.arg1())
		elif parser.commandType() == "C_FUNCTION":
			writer.writeFunct(parser.arg1(), parser.arg2())
		elif parser.commandType() == "C_CALL":
			writer.writeCall(parser.arg1(), parser.arg2())
		elif parser.commandType() == "C_RETURN":
			writer.writeReturn()
		else:
开发者ID:joeboxes,项目名称:csci498-jkeyoth,代码行数:33,代码来源:translator.py

示例6: __init__

# 需要导入模块: from Parser import Parser [as 别名]
# 或者: from Parser.Parser import arg1 [as 别名]
class CodeWriter:


    def __init__(self, parser):
        '''
        Opens the output file and gets ready to write into it
        :param parser object to use
        :return:
        '''
        self.parser = Parser()
        self.eqCounter = 0 # appended to end of EQUAL labels so they are unique
        self.callerCounter = 0 # same as eqCounter but for return address labels
        self.memoryMappedRegisters = {"local": "LCL",
                       "argument": "ARG",
                       "this": "R3",
                       "that": "R4",
                       "pointer": "R3",
                        "temp": "R5"}

        self.currentVMFile = ""
        self.file = None
        self.currentFunction = ""





    def setCurrentVMFile(self, fileName):
        '''
        Changes VM file that is currently being translated
        :param fileName:
        :return:
        '''
        self.currentVMFile = fileName[:-3]

    def setCurrentFunction(self, function):
        '''
        Set function scope to what is being compiled. Used for label, if-goto, goto, etc.
        :param function:
        :return:
        '''

        self.currentFunction = function



    def assembleVMCommand(self, command):
        self.parser.setInstruction(command)
        if self.parser.commandType(command) == self.parser.C_ARITHMETIC:
            return self.translateArithmetic(command)


        elif self.parser.commandType(command) in [self.parser.C_PUSH, self.parser.C_POP]:
            return self.translatePushPop(command)


        elif self.parser.commandType(command) == self.parser.C_FUNCTION:
            return self.translateFunction(self.parser.arg2())


        elif self.parser.commandType(command) == self.parser.C_CALL:
            return self.translateCall(self.parser.arg1(), int(self.parser.arg2()))


        elif self.parser.commandType(command) == self.parser.C_RETURN:
            return self.translateReturn()


        elif self.parser.commandType(command) == self.parser.C_LABEL:
            return self.translateLabel(self.parser.arg1())


        elif self.parser.commandType(command) == self.parser.C_IF:
            return self.translateIfGoto(self.parser.arg1())


        elif self.parser.commandType(command) == self.parser.C_GOTO:
            return self.translateGoto(self.parser.arg1())





    def translateArithmetic(self, command):
        '''
        Writes the assembly code that is the translation of the given
        arithmetic command
        :param command: command string
        :return: list where each element is one line of assembly code

        :todo "sub", "neg", "gt", "lt", "and", "or", "not"]
        '''
        self.parser.setInstruction(command)
        if self.parser.currentInstructionType != self.parser.C_ARITHMETIC:
            raise RuntimeError("translate arithmetic cannot translate non-arithmetic type")

        if command == "add":
            return ["@SP", "M=M-1", "A=M", "D=M", "@SP", "M=M-1", "A=M", "M=D+M",
                             "@SP", "M=M+1"]
        elif command == "sub":
#.........这里部分代码省略.........
开发者ID:dserver,项目名称:Nand2Tetris,代码行数:103,代码来源:CodeWriter.py

示例7: open

# 需要导入模块: from Parser import Parser [as 别名]
# 或者: from Parser.Parser import arg1 [as 别名]
#open output file, fail nicely if it fails
try:
	outFile = open(outPath, "w")
	
except:
	print outPath + ": file could not be opened"
	exit(1)
finally:
	outFile.close()



writer = CodeWriter(outPath)

for f in vmNames:
	fn = path.basename(f)
	print "Working on:",fn
	writer.setFileName(fn)
	parser = Parser(f)
	while parser.hasMoreCommands():
		parser.advance()
		if parser.commandType() == "C_ARITHMETIC":
			writer.writeArithmetic(parser.arg1())
		if parser.commandType() == "C_PUSH":
			writer.writePushPop("PUSH", parser.arg1(), parser.arg2())
		if parser.commandType() == "C_POP":
			writer.writePushPop("POP", parser.arg1(), parser.arg2())
			
writer.close()
print inPath,"=>",outPath
			
开发者ID:joeboxes,项目名称:csci498-jkeyoth,代码行数:32,代码来源:translator.py


注:本文中的Parser.Parser.arg1方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。