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


Python Parser.jump方法代码示例

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


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

示例1: second_pass

# 需要导入模块: from Parser import Parser [as 别名]
# 或者: from Parser.Parser import jump [as 别名]
def second_pass(path, symbol_table):
    p = Parser(path)
    code = Code()
    ram_address = 16
    hack = []
    while(p.hasMoreCommands()):
        command_type = p.commandType()
        if (command_type == CommandType.L):
            p.advance()
            continue
        elif (command_type == CommandType.C):
            dest = code.dest(p.dest())
            comp = code.comp(p.comp())
            jump = code.jump(p.jump())
            command = int("111" + comp + dest + jump, 2)
        else:  # command_type == CommandType.A
            symbol = p.symbol()
            if symbol.isdigit():
                address = int(symbol)
            else:
                if not symbol_table.contains(symbol):
                    symbol_table.add_entry(symbol, ram_address)
                    ram_address += 1
                address = symbol_table.get_address(symbol)
            command = address
        command = format(command, '016b')
        hack.append(command)
        p.advance()
    return hack
开发者ID:bendanon,项目名称:n2t-proj6,代码行数:31,代码来源:Main.py

示例2: main

# 需要导入模块: from Parser import Parser [as 别名]
# 或者: from Parser.Parser import jump [as 别名]
    def main():

        print("******************************************")
        print("***          FileSet Report            ***")
        print("******************************************")
        print()

        fileORdir = Util.getCommandLineArgument(1)
        level = Util.getCommandLineArgument(2)
        files = FileSet(fileORdir, "hack")
        files.report()

        print()
        print("******************************************")
        print("***         Processing Report          ***")
        print("******************************************")
        print()

        while files.hasMoreFiles():
            inputFileSpec = files.nextFile()
            print("Processing: %s" % inputFileSpec)
            outputFileSpec = os.path.splitext(inputFileSpec)[0]+".dis"
            inputFile = open(inputFileSpec, "rU")
            outputFile = open(outputFileSpec, "w")
            parser = Parser(inputFile)
            while parser.hasMoreInstructions():
                parser.advance()
                if (parser.instructionType() == "A_TYPE"):
                    value = parser.value()
                    inst = Code.a_type(value)
                if (parser.instructionType() == "C_TYPE"):
                    dest = parser.dest()
                    comp = parser.comp()
                    jump = parser.jump()
                    destMnemonic = Code.destMnemonic(dest)
                    compMnemonic = Code.compMnemonic(comp)
                    jumpMnemonic = Code.jumpMnemonic(jump)
                    inst = Code.c_type(destMnemonic, compMnemonic, jumpMnemonic)
                if (parser.instructionType() == "INVALID"):
                    inst = Code.invalid_type()
                inst += Util.repeatedChar(" ", 20-len(inst))
                inst += "// %05i:" % parser.address()
                inst += " [%s]" % parser.hexInstruction()
                inst += " %s\n" % parser.parsedInstruction()
                outputFile.write(inst)
            outputFile.close()
            inputFile.close()

        print()
        print("Processing of file(s) complete.")
开发者ID:kmanzana,项目名称:nand2tetris,代码行数:52,代码来源:Main.py

示例3: Parser

# 需要导入模块: from Parser import Parser [as 别名]
# 或者: from Parser.Parser import jump [as 别名]
args = arg_parser.parse_args()

p = Parser(args.filename)

output = []

while p.hasMoreCommands():
    t = p.commandType()
    if t == 'A_COMMAND':
        a = int(p.symbol())
        byte = '0{:015b}'.format(a)
    elif t == 'C_COMMAND':
        byte = '111{}{}{}'.format(
                comp(p.comp()),
                dest(p.dest()),
                jump(p.jump()))
        pass
    elif t == 'L_COMMAND':
        byte = ('l', p.symbol())
    else:
        raise Error
    output.append((byte, p.assembly()))
    p.advance()


if args.output_filename == '':
    args.output_filename = splitext(args.filename)[0] + '.hack'

if args.output_filename == '-':
    f = stdout
else:
开发者ID:Jsearle01,项目名称:the_elements_of_computing_systems,代码行数:33,代码来源:no_symbols_assembler.py

示例4: open

# 需要导入模块: from Parser import Parser [as 别名]
# 或者: from Parser.Parser import jump [as 别名]
        symbolTable.addEntry(parser.symbol(), romAddress)
    else:
        romAddress += 1

# This opens the file that will be written to.
hackFilename = asmFilename[:-3] + "hack"
hackFile = open(hackFilename, "w")

# This writes the translated code to the hack file.
parser.restart()
ramAddress = 16
while parser.hasMoreCommands():
    parser.advance()
    commandType = parser.commandType()
    if commandType == "C_COMMAND":
        hackFile.write("111" + Code.comp(parser.comp()) + Code.dest(parser.dest()) + Code.jump(parser.jump()) + "\n")
    elif commandType == "A_COMMAND":
        symbol = parser.symbol()
        try:
            address = int(symbol)
        except:
            if symbolTable.contains(symbol):
                address = symbolTable.getAddress(symbol)
            else:
                address = ramAddress
                # This adds an A command symbol to the symbol table if it's not already in it.
                symbolTable.addEntry(symbol, address)
                ramAddress += 1

        hackFile.write(bin(address)[2:].zfill(16) + "\n")
开发者ID:KyleJune,项目名称:HackAssembler,代码行数:32,代码来源:Assembler.py

示例5: int

# 需要导入模块: from Parser import Parser [as 别名]
# 或者: from Parser.Parser import jump [as 别名]
    symbols.addEntry(name, variable_offset.__next__())


while p.hasMoreCommands():
    t = p.commandType()
    if t == "A_COMMAND":
        s = p.symbol()
        try:
            a = int(s)
        except ValueError:
            if not symbols.contains(s):
                new_variable(s)
            a = symbols.GetAddress(p.symbol())
        byte = "0{:015b}".format(a)
    elif t == "C_COMMAND":
        byte = "111{}{}{}".format(comp(p.comp()), dest(p.dest()), jump(p.jump()))
        pass
    elif t == "L_COMMAND":
        byte = ""
        pass  # handled in first pass
    else:
        raise Error
    output.append((byte, p.assembly()))
    p.advance()

if args.output_filename == "":
    args.output_filename = splitext(args.filename)[0] + ".hack"

if args.output_filename == "-":
    f = stdout
else:
开发者ID:willbr,项目名称:the_elements_of_computing_systems,代码行数:33,代码来源:assembler.py

示例6: main

# 需要导入模块: from Parser import Parser [as 别名]
# 或者: from Parser.Parser import jump [as 别名]
def main():
    # open an output file
    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(".asm")) ]
    else:
        input_list = [input_path]
    
    for input_file in input_list:
        index = input_file.index(".")
        output_file = open(input_file[:index] + ".hack", "w")
        # parse a new line
        code = Code()
        symbol_table = SymbolTable()
        counter_address = FIRST_ADDRESS_RAM
        counter_rom = FIRST_ADDRESS_ROM

        # first pass
        parser_first_pass = Parser(input_file)
        while parser_first_pass.has_more_commands():
            command = parser_first_pass.advance()
            parse_type = parser_first_pass.command_type()
            if parse_type == L_COMMAND:
                if not symbol_table.contains(command[1:-1]):
                    symbol_table.add_entry(command[1:-1], counter_rom)
            else:
               counter_rom+=1

        # second pass
        parser_second_pass = Parser(input_file)
        while parser_second_pass.has_more_commands():
            command = parser_second_pass.advance()
            line_to_hack = ""
            parse_type = parser_second_pass.command_type()

            # translate the line to an A Command
            if parse_type == A_COMMAND:
                if command[1:].isdigit():
                    address = command[1:]
                else:
                    if symbol_table.contains(command[1:]):
                        address = symbol_table.get_address(command[1:])
                    else:
                        symbol_table.add_entry(command[1:], counter_address)
                        address = counter_address
                        
                        counter_address += 1
                binary_repr = str(bin(int(address))[2:].zfill(15))
                line_to_hack = A_PREFIX + binary_repr

            # translate the line to a C Command
            if parse_type == C_COMMAND:
                # C command comp
                comp_type = parser_second_pass.comp()
                code_comp = code.comp(comp_type)
                # C command dest
                dest_type = parser_second_pass.dest()
                code_dest = code.dest(dest_type)
                # C command jump
                jump_type = parser_second_pass.jump()
                code_jump = code.jump(jump_type)
                if ("<" in comp_type) or (">" in comp_type):
                    line_to_hack = C_PREFIX_SPE + code_comp + code_dest + code_jump
                else:
                    line_to_hack = C_PREFIX_REG + code_comp + code_dest + code_jump
                
            if parse_type == L_COMMAND:
                continue

            # write the line to the output file
            output_file.write(line_to_hack + "\n")


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

示例7: Parser

# 需要导入模块: from Parser import Parser [as 别名]
# 或者: from Parser.Parser import jump [as 别名]
RAM_top = 16
pc = 0
parser = Parser(asm_file)
while parser.has_more_commands():
    pc += 1
    parser.advance()
    command_type = parser.command_type()
    if command_type == 'A_COMMAND':
        symbol = parser.symbol()
        if not stable.contains(symbol):
            stable.add_entry(symbol, RAM_top)
            RAM_top += 1
    elif command_type == 'L_COMMAND':
        pc -= 1
        symbol = parser.symbol()
        stable.add_entry(symbol, pc)

# PASS 2 
parser = Parser(asm_file)
while parser.has_more_commands():
    parser.advance()
    command_type = parser.command_type()
    if command_type == 'A_COMMAND':
        symbol = parser.symbol()
        if symbol.isdigit():
            print "0" + str(coder.a_address(int(symbol)))
        else:
            print "0" + str(coder.a_address(stable.get_address(symbol)))
    elif command_type == 'C_COMMAND':
        print "111" + coder.comp(parser.comp()) + coder.dest(parser.dest()) + coder.jump(parser.jump())
开发者ID:ganesshkumar,项目名称:Computer-0.1,代码行数:32,代码来源:Assembler.py

示例8: int

# 需要导入模块: from Parser import Parser [as 别名]
# 或者: from Parser.Parser import jump [as 别名]
        # Sets whether it's using B in the comp
        comp = parser.comp()
        if comp.find('B') == -1:
            commandStart += '1'
        else:
            commandStart += '0'
            comp = comp.replace('B', 'D')
        
        # Sets whether it's using B in the dest
        dest = parser.dest()
        if dest == None or dest.find('B') == -1:
            commandStart += '1'
        else:
            commandStart += '0'
        
        hackFile.write(commandStart + Code.comp(comp) + Code.dest(dest) + Code.jump(parser.jump()) + '\n')
    elif commandType == 'A_COMMAND':
        symbol = parser.symbol()
        try:
            address = int(symbol)
        except:
            if symbolTable.contains(symbol):
                address = symbolTable.getAddress(symbol)
            else:
                address = ramAddress
                # This adds an A command symbol to the symbol table if it's not already in it.
                symbolTable.addEntry(symbol, address)
                ramAddress += 1
        
        hackFile.write(bin(address)[2:].zfill(16) + '\n')
开发者ID:KyleJune,项目名称:HackAssembler,代码行数:32,代码来源:ModAssembler.py


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