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


Python Parser.hasMoreCommands方法代码示例

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


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

示例1: second_pass

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

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

示例3: Parser

# 需要导入模块: from Parser import Parser [as 别名]
# 或者: from Parser.Parser import hasMoreCommands [as 别名]
        dest='verbose',
        action='store_true',
        help='set output to verbose')

arg_parser.add_argument('--out',
        dest='output_filename',
        default='',
        help='set output file')

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()))
开发者ID:Jsearle01,项目名称:the_elements_of_computing_systems,代码行数:33,代码来源:no_symbols_assembler.py

示例4: Parser

# 需要导入模块: from Parser import Parser [as 别名]
# 或者: from Parser.Parser import hasMoreCommands [as 别名]
Translates HACK assembly into HACK machine code.

@author: Kyle June
"""
import sys
from Parser import Parser
import Code
from SymbolTable import SymbolTable

asmFilename = sys.argv[1]

# This goes through the file and adds the address for each label to the symbol table.
parser = Parser(asmFilename)
symbolTable = SymbolTable()
romAddress = 0
while parser.hasMoreCommands():
    parser.advance()
    if parser.commandType() == "L_COMMAND":
        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()
开发者ID:KyleJune,项目名称:HackAssembler,代码行数:33,代码来源:Assembler.py

示例5: main

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

示例6: Parser

# 需要导入模块: from Parser import Parser [as 别名]
# 或者: from Parser.Parser import hasMoreCommands [as 别名]
#Author: Josh Wretlind
#Python Assignment #3
#Class: CSCI 410 - Elements of Computing Systems
#Written in: Python 2.7

import sys,string,os
from Parser import Parser

infile = sys.argv[1] # Sys.argv is the system argument list object
outfile = sys.argv[2]

parse = Parser(infile)
outfilecontents = ""
while parse.hasMoreCommands():
    parse.advance()
    outfilecontents += parse.output()
    
output = open(outfile, 'w')
output.write(outfilecontents)

parse.stats()
开发者ID:joshWretlind,项目名称:CSCI410-ElementsOfComputingSystems,代码行数:23,代码来源:jacklex.py


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