Add __str__ and __repr__ to Expressions
It is easier to read the expressions in a similar way to source code in
the debugger, which is the reason, why we override the default behaviour
of these methods.
@@ -93,6 +93,12 @@ class Name(Expression):
def accept(self, visitor: StatementVisitor) -> T:
return visitor.visit_name(self)
+ def __str__(self) -> str:
+ return self.identifier
+
+ def __repr__(self) -> str:
+ return self.__str__()
+
@dataclass(init=True)
class Attribute(Expression):
@@ -104,6 +110,12 @@ class Attribute(Expression):
def accept(self, visitor: StatementVisitor) -> T:
return visitor.visit_attribute(self)
+ def __str__(self) -> str:
+ return "{}.{}".format(str(self.owner), self.attribute_name)
+
+ def __repr__(self) -> str:
+ return self.__str__()
+
@dataclass(init=True)
class Call(Expression):
@@ -115,6 +127,14 @@ class Call(Expression):
def accept(self, visitor: StatementVisitor) -> T:
return visitor.visit_call(self)
+ def __str__(self) -> str:
+ return "{}({})".format(
+ str(self.function), ", ".join([str(a) for a in self.arguments])
+ )
+
+ def __repr__(self) -> str:
+ return self.__str__()
+
@dataclass(init=True)
class Assignment(Expression):
@@ -126,6 +146,12 @@ class Assignment(Expression):
def accept(self, visitor: StatementVisitor) -> T:
return visitor.visit_assignment(self)
+ def __str__(self) -> str:
+ return "{} = {}".format(str(self.lhs), str(self.rhs))
+
+ def __repr__(self) -> str:
+ return self.__str__()
+
@dataclass(init=True, repr=True, eq=True)
class FunctionSignature:
