本文整理匯總了Python中SymbolTable.SymbolTable.getAddress方法的典型用法代碼示例。如果您正苦於以下問題:Python SymbolTable.getAddress方法的具體用法?Python SymbolTable.getAddress怎麽用?Python SymbolTable.getAddress使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類SymbolTable.SymbolTable
的用法示例。
在下文中一共展示了SymbolTable.getAddress方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: open
# 需要導入模塊: from SymbolTable import SymbolTable [as 別名]
# 或者: from SymbolTable.SymbolTable import getAddress [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()
示例2: SymbolTable
# 需要導入模塊: from SymbolTable import SymbolTable [as 別名]
# 或者: from SymbolTable.SymbolTable import getAddress [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====="
示例3: TestAssemblerFinal
# 需要導入模塊: from SymbolTable import SymbolTable [as 別名]
# 或者: from SymbolTable.SymbolTable import getAddress [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))
示例4: ord
# 需要導入模塊: from SymbolTable import SymbolTable [as 別名]
# 或者: from SymbolTable.SymbolTable import getAddress [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()