Files
nand2tetris/project-6-assembler/assembler/src/assembler/symbol_table.py

45 lines
1.2 KiB
Python

class SymbolTable:
def __init__(self):
"""
Initialize dictionary with predefined Hack platform symbols
"""
self.symbols = {
"SP": 0,
"LCL": 1,
"ARG": 2,
"THIS": 3,
"THAT": 4,
"R0": 0,
"R1": 1,
"R2": 2,
"R3": 3,
"R4": 4,
"R5": 5,
"R6": 6,
"R7": 7,
"R8": 8,
"R9": 9,
"R10": 10,
"R11": 11,
"R12": 12,
"R13": 13,
"R14": 14,
"R15": 15,
"SCREEN": 16384,
"KBD": 24576,
}
def add_entry(self, symbol: str, address: int) -> None:
# Add symbol-address pair to dictionary
self.symbols[symbol] = address
def contains(self, symbol: str) -> bool:
print(symbol)
raise NotImplementedError("contains not yet implemented")
# Check if symbol exists in dictionary
def get_address(self, symbol: str) -> int:
print(symbol)
raise NotImplementedError("get_address not yet implemented")
# Return address for symbol (raises KeyError if not found)