[docs]class_ABCBasicMnemonic(ABC):""" Base class for assembly mnemonics with flexible operand handling. This class provides core functionality for constructing assembly instructions with variable number of operands and automatic comment generation. Attributes: mnemonic_name: Assembly instruction name (e.g., 'mov', 'add'). operands: Sequence of instruction operands. enable_comment: Flag to control comment generation. comment: Custom comment for the instruction. """
[docs]def__init__(self,mnemonic_name:str,*operands:Union["Register",str,int],enable_comment:bool=True,)->None:""" Initializes mnemonic with operands and comment settings. Args: mnemonic_name: Name of the assembly instruction. *operands: Variable-length sequence of instruction operands. enable_comment: Whether to generate comments in output. """self.mnemonic_name=mnemonic_nameself.operands=operandsself._enable_comment=enable_commentself._comment:str|None=Noneself._validate()
[docs]@abstractmethoddef_validate(self):"""Validate mnemonics operands and other fields."""
@propertydefcomment(self)->str|None:"""Gets the current comment for the instruction."""returnself._comment@comment.setterdefcomment(self,value:str|None)->None:""" Sets a custom comment or resets to default. Args: value: Custom comment string or None to use default. """self._comment=value
[docs]def_generate_default_comment(self)->str:""" Generates context-sensitive default comment based on operands. Returns: Appropriately formatted comment string. """operand_count=len(self.operands)instruction=self.mnemonic_name.upper()ifoperand_count==0:returnf"{instruction} operation."ifoperand_count==1:returnf"{instruction} operand {self.operands[0]!s}."ifoperand_count==2:returnf"{instruction} from {self.operands[1]!s} to {self.operands[0]!s}."returnf"{instruction} with {operand_count} operands."
[docs]def_validate_operand_types(self)->None:""" Validates operand types against allowed types. Raises: TypeError: If any operand has invalid type. """allowed_types=(Register,str,int)fori,operandinenumerate(self.operands,1):ifnotisinstance(operand,allowed_types):raiseTypeError(f"Operand {i} has invalid type {type(operand).__name__}. "f"Allowed types: Register, str, int.")