60 lines
1.5 KiB
Python
60 lines
1.5 KiB
Python
from .code_tables import DEST_TABLE, COMP_TABLE, JUMP_TABLE
|
|
|
|
|
|
class Code:
|
|
"""Translates Hack assembly mnemonics into binary codes."""
|
|
|
|
@staticmethod
|
|
def dest(mnemonic: str) -> str:
|
|
"""
|
|
Returns the 3-bit binary code for the dest mnemonic.
|
|
|
|
Args:
|
|
mnemonic: Destination mnemonic (e.g., "M", "D", "MD", "AMD")
|
|
|
|
Returns:
|
|
3-bit binary string (e.g., "010", "111")
|
|
|
|
Raises:
|
|
KeyError: If mnemonic is invalid
|
|
"""
|
|
# Use dictionary lookup table
|
|
print(mnemonic)
|
|
raise NotImplementedError("not implemented")
|
|
|
|
@staticmethod
|
|
def comp(mnemonic: str) -> str:
|
|
"""
|
|
Returns the 7-bit binary code for the comp mnemonic.
|
|
|
|
Args:
|
|
mnemonic: Computation mnemonic (e.g., "D+1", "M-1", "D&A")
|
|
|
|
Returns:
|
|
7-bit binary string (e.g., "0001111", "1110000")
|
|
|
|
Raises:
|
|
KeyError: If mnemonic is invalid
|
|
"""
|
|
print(mnemonic)
|
|
raise NotImplementedError("not implemented")
|
|
# Use dictionary lookup table
|
|
|
|
@staticmethod
|
|
def jump(mnemonic: str) -> str:
|
|
"""
|
|
Returns the 3-bit binary code for the jump mnemonic.
|
|
|
|
Args:
|
|
mnemonic: Jump mnemonic (e.g., "JGT", "JEQ", "JMP", or "" for no jump)
|
|
|
|
Returns:
|
|
3-bit binary string (e.g., "001", "111")
|
|
|
|
Raises:
|
|
KeyError: If mnemonic is invalid
|
|
"""
|
|
print(mnemonic)
|
|
raise NotImplementedError("not implemented")
|
|
# Use dictionary lookup table
|