[Libre-soc-bugs] [Bug 1094] insndb instruction database visitor-walker is needed
bugzilla-daemon at libre-soc.org
bugzilla-daemon at libre-soc.org
Wed Jun 7 12:30:10 BST 2023
https://bugs.libre-soc.org/show_bug.cgi?id=1094
--- Comment #54 from Luke Kenneth Casson Leighton <lkcl at lkcl.net> ---
to make Instruction "conform" to the IVisitable pattern:
class Instruction(str):
def __new__(cls, string):
svp64 = False
if string.startswith("sv."):
string = string[len("sv."):]
svp64 = True
self = super().__new__(cls, string)
self.__svp64 = svp64
return self
@property
def svp64(self):
return self.__svp64
add this:
def accept(self, visitor):
"required by the Visitor that will traverse"
for opcode in self.opcodes:
opcode.accept(visitor)
visitor.visit(self)
and if we want to extend it to include "filtering":
def accept(self, visitor, accept_filter=lambda x:x):
"required by the Visitor that will traverse"
for opcode in self.opcodes:
if accept_filter(opcode):
opcode.accept(visitor)
visitor.visit(self)
then, OpcodesVisitor becomes:
class OpcodesPrintVisitor:
def Record(self, opcode):
print(opcode)
and that's all!!
for Records:
class Record:
.....
def accept(self, visitor, accept_filter=lambda x:x):
"required by the Visitor that will traverse"
for operand in self.dynamic_operands:
if accept_filter(operand): operand.accept(visitor)
for operand in self.static_operands:
if accept_filter(operand): operand.accept(visitor)
visitor.visit(self)
at which point it becomes possible to do this:
class Record:
.....
def accept(self, visitor, accept_filter=lambda x:x):
"required by the Visitor that will traverse"
for field in dataclass.fields(self):
for subthing in field:
if accept_filter(subthing): subthing.accept(visitor)
visitor.visit(self)
--
You are receiving this mail because:
You are on the CC list for the bug.
More information about the libre-soc-bugs
mailing list