本文整理汇总了Python中SymbolTable.SymbolTable.contains方法的典型用法代码示例。如果您正苦于以下问题:Python SymbolTable.contains方法的具体用法?Python SymbolTable.contains怎么用?Python SymbolTable.contains使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SymbolTable.SymbolTable
的用法示例。
在下文中一共展示了SymbolTable.contains方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: symboltabletests
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import contains [as 别名]
class symboltabletests(unittest.TestCase):
def setUp(self):
self.st = SymbolTable()
def testContains(self):
self.st.addEntry("loop", 100)
self.assertTrue(self.st.contains("loop"))
self.assertFalse(self.st.contains("bobby"))
示例2: open
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import contains [as 别名]
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")
hackFile.close()
示例3: Assembler
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import contains [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")
示例4: Parser
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import contains [as 别名]
p = Parser(args.filename)
offset = 0
symbols = SymbolTable()
while p.hasMoreCommands():
t = p.commandType()
if t == "A_COMMAND":
offset += 1
s = p.symbol()
elif t == "C_COMMAND":
offset += 1
elif t == "L_COMMAND":
s = p.symbol()
if symbols.contains(s):
raise SyntaxError("multiple declarations of %s" % s)
else:
symbols.addEntry(s, offset)
else:
raise Error
p.advance()
p.reset()
output = []
variable_offset = itertools.count(16)
def new_variable(name):
symbols.addEntry(name, variable_offset.__next__())
示例5: main
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import contains [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()
示例6: TestAssemblerFinal
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import contains [as 别名]
class TestAssemblerFinal(unittest.TestCase):
def setUp(self):
self.assembler = Assembler()
parser = Parser()
self.symbolTable = SymbolTable()
self.assembler.setSymbolTable(self.symbolTable)
self.assembler.setParser(parser)
def testFirstPassSimpleLoop(self):
instructions = ["(loop)", "@loop", "0;JNE"]
self.assembler.firstPass(instructions)
self.assertTrue(self.symbolTable.contains("loop"))
self.assertEqual(self.symbolTable.getAddress("loop"), 0)
def testFirstPassComplexLoops(self):
instructions = ["(loop)", "@loop", "0;JNE", "(bob)", "D=D+1", "(tim)", "M=A+D"]
self.assembler.firstPass(instructions)
self.assertTrue(self.symbolTable.contains("loop"))
self.assertTrue(self.symbolTable.contains("bob"))
self.assertTrue(self.symbolTable.contains("tim"))
self.assertEqual(self.symbolTable.getAddress("loop"), 0)
self.assertEqual(self.symbolTable.getAddress("bob"), 2)
self.assertEqual(self.symbolTable.getAddress("tim"), 3)
def testFirstPassMaxAsm(self):
instructions = ["@R0", "D=M", "@R1", "D=D-M", "@OUTPUT_FIRST", "D;JGT", "@R1", "D=M", "@OUTPUT_D", "0;JMP",
"(OUTPUT_FIRST)", "@R0", "D=M", "(OUTPUT_D)", "@R2", "M=D", "(INFINITE_LOOP)",
"@INFINITE_LOOP", "0;JMP"]
self.assembler.firstPass(instructions)
self.assertTrue(self.assembler.symbolTable.contains("OUTPUT_FIRST"))
self.assertEqual(self.assembler.symbolTable.getAddress("OUTPUT_FIRST"), 10)
def testFirstPassMaxAsmInfiniteLoopAddress(self):
instructions = ["@R0", "D=M", "@R1", "D=D-M", "@OUTPUT_FIRST", "D;JGT", "@R1", "D=M", "@OUTPUT_D", "0;JMP",
"(OUTPUT_FIRST)", "@R0", "D=M", "(OUTPUT_D)", "@R2", "M=D", "(INFINITE_LOOP)",
"@INFINITE_LOOP", "0;JMP"]
self.assembler.firstPass(instructions)
self.assertTrue(self.assembler.symbolTable.contains("INFINITE_LOOP"))
self.assertEqual(self.assembler.symbolTable.getAddress("INFINITE_LOOP"), 14)
def testSecondPassMaxAsmInfiniteLoopAddress(self):
instructions = ["@R0", "D=M", "@R1", "D=D-M", "@OUTPUT_FIRST", "D;JGT", "@R1", "D=M", "@OUTPUT_D", "0;JMP",
"(OUTPUT_FIRST)", "@R0", "D=M", "(OUTPUT_D)", "@R2", "M=D", "(INFINITE_LOOP)",
"@INFINITE_LOOP", "0;JMP"]
self.assembler.firstPass(instructions)
self.assembler.secondPass(instructions)
self.assertTrue(self.assembler.symbolTable.contains("INFINITE_LOOP"))
self.assertEqual(self.assembler.symbolTable.getAddress("INFINITE_LOOP"), 14)
def testSecondPassSimple(self):
instructions = ["(loop)", "@myVar", "M=5", "@loop", "0;JNE"]
self.assembler.firstPass(instructions)
self.assembler.secondPass(instructions)
self.assertTrue(self.symbolTable.contains("loop"))
self.assertTrue(self.symbolTable.contains("myVar"))
self.assertEqual(self.symbolTable.getAddress("myVar"), 16)
self.assertEqual(self.symbolTable.getAddress("loop"), 0)
def testAssembleInstructions(self):
instructions = ["(loop)", "@loop", "0;JEQ"]
self.assembler.firstPass(instructions)
self.assembler.secondPass(instructions)
self.assertEqual(["0b0000000000000000", "0b1110101010000010"],
self.assembler.assemble(instructions))
示例7: SymbolTable
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import contains [as 别名]
from SymbolTable import SymbolTable
tab = SymbolTable()
tab.addEntry("testSymbol", 32767)
print "=====Testing SymbolTable"
if not tab.contains("testSymbol"):
print "SymbolTable.contains should return true"
if tab.getAddress("testSymbol") != "111111111111111":
print "SymbolTable.getAddress did not return correct value"
tab.addVariable("testVar")
if tab.getAddress("testVar") != "000000000010000":
print "SymbolTable.getAddress did not return correct value for var"
print "Done testing SymbolTable====="
示例8: Parser
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import contains [as 别名]
print 'Please enter a filename as argument'
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():
示例9: ord
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import contains [as 别名]
#pass 1
parser.reset()
while parser.hasMoreCommands():
parser.advance()
type = parser.commandType()
if type == "A":
sym = parser.symbol()
#sym is a constant
if ord(sym[0]) >= ord("0") and ord(sym[0]) <= ord("9"):
outFile.write("0" + Code.decimalConstantToBinaryString(sym) + "\n")
#sym is in table
elif symbolTable.contains(sym):
outFile.write("0" + symbolTable.getAddress(sym) + "\n")
#sym is a new var
else:
symbolTable.addVariable(sym)
outFile.write("0" + symbolTable.getAddress(sym) + "\n")
elif type == "C":
d = Code.dest(parser.dest())
c = Code.comp(parser.comp())
j = Code.jump(parser.jump())
outFile.write("111" + c + d + j + "\n")
outFile.close()