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


Python SymbolTable.add_entry方法代码示例

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


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

示例1: first_pass

# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import add_entry [as 别名]
def first_pass(path):
    p = Parser(path)

    symbol_table = SymbolTable()
    n = 0
    while(p.hasMoreCommands()):
        command_type = p.commandType()
        if(command_type == CommandType.L):
            symbol_table.add_entry(p.symbol(), n)
        else:
            n += 1
        p.advance()
    return symbol_table
开发者ID:bendanon,项目名称:n2t-proj6,代码行数:15,代码来源:Main.py

示例2: Assembler

# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import add_entry [as 别名]
class Assembler(object):

    def __init__(self, input_file_path):
        self.output_file = open(input_file_path.replace('.asm', '.hack'), 'w')

        """
        The ROM address is the address of the current instruction written in the
        .hack file. The first instruction is 0, second is 1, etc. Label is not a instruction.
        """
        self.current_rom_address = ROM_BASE_ADRESS

        self.parser = Parser(input_file_path)
        self.symbol_table = SymbolTable()

        """
        The RAM address of the next free memory that a new variable
        should be at.
        """
        self.next_free_var_address = VARIABLES_BASE_ADDRESS

    #Does the double pass assemble
    def assemble(self):
        self.first_pass()
        self.parser.start_over()
        self.second_pass()

    """
    Find Label commands adds all labels to the symbol table
    so jumps to labels yet-to-be-parsed could work.
    """
    def first_pass(self):
        self.current_rom_address = ROM_BASE_ADRESS
        while self.parser.has_more_commands():
            self.parser.advance()
            if self.parser.command_type() == self.parser.L_COMMAND:
                self.symbol_table.add_entry(self.parser.get_symbol(),
                                            self.current_rom_address)
            else:
                self.current_rom_address += INSTRUCTION_SIZE_IN_WORDS

    """
    Use the symbol table (if symbol is not a number) to get the
    address of the symbol. Create new variable if not in the table.
    """
    def _symbol_to_address(self, symbol):
        if symbol.isdigit():
            return int(symbol)
        if not self.symbol_table.contains(symbol):
            #new var
            self.symbol_table.add_entry(symbol, self.next_free_var_address)
            self.next_free_var_address += VARIABLE_SIZE_IN_WORDS
        return self.symbol_table.get_address(symbol)
    """
    Translate the A and C instructions to hack and write in the output file
    and skip labels
    """
    def second_pass(self):
        self.current_rom_address = ROM_BASE_ADRESS
        while self.parser.has_more_commands():
            self.parser.advance()
            #C instruction
            if self.parser.command_type() == self.parser.C_COMMAND:
                self._write_to_output(code.generate_c(self.parser.get_comp(),
                                                      self.parser.get_dest(),
                                                      self.parser.get_jump()))
            #A instruction
            elif self.parser.command_type() == self.parser.A_COMMAND:
                address = self._symbol_to_address(self.parser.get_symbol())
                self._write_to_output(code.generate_a(address))
            #Lable is not an instruction
            if not self.parser.command_type() == self.parser.L_COMMAND:
                self.current_rom_address += INSTRUCTION_SIZE_IN_WORDS
    """
    Write a single hack instruction in the output .hack file,
    add a newline after it
    """
    def _write_to_output(self, hack_instruction):
        self.output_file.write(hack_instruction + "\n")
开发者ID:adirz,项目名称:sample-projects,代码行数:80,代码来源:Assembler.py

示例3: main

# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import add_entry [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

示例4: Parser

# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import add_entry [as 别名]
    sys.exit(1)
else:
    asm_file =  sys.argv[1]

# PASS 1
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)))
开发者ID:ganesshkumar,项目名称:Computer-0.1,代码行数:33,代码来源:Assembler.py


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