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


Python Parser.command_type方法代码示例

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


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

示例1: main

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

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

示例3: Assembler

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

示例4: main

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


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