from utils.memory_manager import MemoryManager from utils.literal import Literal class MemoryCell (Literal): def __new__(cls, *args, **kwargs): """Erstellt eine neue Instanz von MemoryCell.""" instance = MemoryManager().acquire_cell() if instance is None: instance = super().__new__(cls) MemoryManager().register_cell(instance) return instance def __enter__(self): return self def __exit__(self, exc_type, exc_val, exc_tb): MemoryManager().release_cell(self) def __init__(self, value=None): """Initialisiert eine Speicherzelle mit optionalem Startwert.""" super().__init__(value) self.write_count = 0 self.add_count = 0 self.sub_count = 0 self.mul_count = 0 self.div_count = 0 self.bitop_count = 0 if value is not None: self.write_count +=1 else: self.value = 0 def reset_counters(self): """Setzt alle Zähler auf 0 zurück.""" super().reset_counters() self.write_count = 0 self.add_count = 0 self.sub_count = 0 self.mul_count = 0 self.div_count = 0 self.bitop_count = 0 def set(self, new_value): """Schreibt einen neuen Wert in die Speicherzelle und erhöht den Schreibzähler.""" self.write_count += 1 if isinstance(new_value, Literal): self.value = new_value.value else: self.value = new_value def add(self, other): """Addiert den Wert der Speicherzelle mit einem anderen Wert.""" self.set((self + other).value) def sub(self, other): """Subtrahiert den Wert der Speicherzelle mit einem anderen Wert.""" self.set((self - other).value) def mul(self, other): """Multipliziert den Wert der Speicherzelle mit einem anderen Wert.""" self.set((self * other).value) def div(self, other): """Dividiert den Wert der Speicherzelle durch einen anderen Wert.""" self.set((self // other).value) def modulo(self, other): """Berechnet den Modulo des Wertes der Speicherzelle durch einen anderen Wert.""" self.set((self % other).value) def lshift(self, other): """Verschiebt den Wert der Speicherzelle um eine bestimmte Anzahl von Bits nach links.""" assert isinstance(other, Literal), "Can only lshift Literal or MemoryCell by MemoryCell" self.bitop_count += 1 self.read_count += 1 self.write_count += 1 other.read_count += 1 self.value <<= other.value def rshift(self, other): """Verschiebt den Wert der Speicherzelle um eine bestimmte Anzahl von Bits nach rechts.""" assert isinstance(other, Literal), "Can only rshift Literal or MemoryCell by MemoryCell" self.bitop_count += 1 self.read_count += 1 self.write_count += 1 other.read_count += 1 self.value >>= other.value def and_op(self, other): """Führt ein Bitweise AND auf den Wert der Speicherzelle mit einem anderen Wert aus.""" assert isinstance(other, Literal), "Can only and Literal or MemoryCell with MemoryCell" self.bitop_count += 1 self.read_count += 1 self.write_count += 1 other.read_count += 1 self.value &= other.value def or_op(self, other): """Führt ein Bitweise OR auf den Wert der Speicherzelle mit einem anderen Wert aus.""" assert isinstance(other, Literal), "Can only or Literal or MemoryCell with MemoryCell" self.bitop_count += 1 self.read_count += 1 self.write_count += 1 other.read_count += 1 self.value |= other.value def xor_op(self, other): """Führt ein Bitweise XOR auf den Wert der Speicherzelle mit einem anderen Wert aus.""" assert isinstance(other, Literal), "Can only xor Literal or MemoryCell with MemoryCell" self.bitop_count += 1 self.read_count += 1 self.write_count += 1 other.read_count += 1 self.value ^= other.value def get_write_count(self): """Gibt zurück, wie oft der Wert geschrieben wurde.""" return self.write_count def __repr__(self): """Repräsentation der Speicherzelle für Debugging-Zwecke.""" return f"MemoryCell(value={self.value}, reads={self.read_count}, writes={self.write_count})" def __add__(self, other): assert isinstance(other, Literal), "Can only add Literal or MemoryCell to MemoryCell" self.add_count += 1 self.read_count += 1 other.read_count += 1 return Literal(self.value + other.value) def __sub__(self, other): assert isinstance(other, Literal), "Can only add Literal or MemoryCell to MemoryCell" self.sub_count += 1 self.read_count += 1 other.read_count += 1 return Literal(self.value - other.value) def __mul__(self, other): assert isinstance(other, Literal), "Can only mul Literal or MemoryCell with MemoryCell" self.mul_count += 1 self.read_count += 1 other.read_count += 1 return Literal(self.value * other.value) def __truediv__(self, other): assert isinstance(other, Literal), "Can only div Literal or MemoryCell by MemoryCell" self.div_count += 1 self.read_count += 1 other.read_count += 1 return Literal(self.value / other.value) def __floordiv__(self, other): assert isinstance(other, Literal), "Can only div Literal or MemoryCell by MemoryCell" self.div_count += 1 self.read_count += 1 other.read_count += 1 return Literal(self.value // other.value) def __mod__(self, other): assert isinstance(other, Literal), "Can only div Literal or MemoryCell by MemoryCell" self.div_count += 1 self.read_count += 1 other.read_count += 1 return Literal(self.value % other.value) def __iadd__(self, other): self.add(other) return self def __isub__(self, other): self.sub(other) return self def __imul__(self, other): self.mul(other) return self def __itruediv__(self, other): self.set(self // other) return self def __ifloordiv__(self, other): self.div(other) return self if __name__ == "__main__": a = MemoryCell(5) b = MemoryCell(3) a += b print(f"Ergebnis: {a}") print(f"a wurde {a.get_read_count()} mal gelesen und {a.get_write_count()} mal geschrieben.") print(f"b wurde {b.get_read_count()} mal gelesen und {b.get_write_count()} mal geschrieben.")