From ef0dbc726749df434036b23480b89f01cbe67d44 Mon Sep 17 00:00:00 2001 From: Patrick Lehmann Date: Sat, 19 Jun 2021 03:20:59 +0200 Subject: Added handling of Parenthesis. --- pyGHDL/dom/formatting/prettyprint.py | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) (limited to 'pyGHDL/dom/formatting') diff --git a/pyGHDL/dom/formatting/prettyprint.py b/pyGHDL/dom/formatting/prettyprint.py index 8ee49275c..c10b7cf87 100644 --- a/pyGHDL/dom/formatting/prettyprint.py +++ b/pyGHDL/dom/formatting/prettyprint.py @@ -48,6 +48,7 @@ from pyGHDL.dom.Expression import ( NegationExpression, ExponentiationExpression, Aggregate, + ParenthesisExpression, ) from pyGHDL.dom.Aggregates import ( SimpleAggregateElement, @@ -70,18 +71,19 @@ ModeTranslation = { } UnaryExpressionTranslation = { - IdentityExpression: " +", - NegationExpression: " -", - InverseExpression: "not ", - AbsoluteExpression: "abs ", + IdentityExpression: (" +", ""), + NegationExpression: (" -", ""), + InverseExpression: ("not ", ""), + AbsoluteExpression: ("abs ", ""), + ParenthesisExpression: ("(", ")"), } BinaryExpressionTranslation = { - AdditionExpression: " + ", - SubtractionExpression: " - ", - MultiplyExpression: " * ", - DivisionExpression: " / ", - ExponentiationExpression: "**", + AdditionExpression: ("", " + ", ""), + SubtractionExpression: ("", " - ", ""), + MultiplyExpression: ("", " * ", ""), + DivisionExpression: ("", " / ", ""), + ExponentiationExpression: ("", "**", ""), } @@ -401,8 +403,10 @@ class PrettyPrint: except KeyError: raise PrettyPrintException("Unhandled operator for unary expression.") - return "{operator}{operand}".format( - operand=self.formatExpression(expression.Operand), operator=operator + return "{leftOp}{operand}{rightOp}".format( + leftOp=operator[0], + rightOp=operator[1], + operand=self.formatExpression(expression.Operand), ) elif isinstance(expression, BinaryExpression): try: @@ -410,10 +414,12 @@ class PrettyPrint: except KeyError: raise PrettyPrintException("Unhandled operator for binary expression.") - return "{left}{operator}{right}".format( - left=self.formatExpression(expression.LeftOperand), - right=self.formatExpression(expression.RightOperand), - operator=operator, + return "{leftOp}{leftExpr}{middleOp}{rightExpr}{rightOp}".format( + leftOp=operator[0], + middleOp=operator[1], + rightOp=operator[2], + leftExpr=self.formatExpression(expression.LeftOperand), + rightExpr=self.formatExpression(expression.RightOperand), ) elif isinstance(expression, Aggregate): return "({choices})".format( -- cgit v1.2.3