diff options
-rw-r--r-- | pyGHDL/dom/Symbol.py | 3 | ||||
-rw-r--r-- | pyGHDL/dom/formatting/prettyprint.py | 128 | ||||
-rw-r--r-- | pyGHDL/requirements.txt | 2 | ||||
-rw-r--r-- | testsuite/pyunit/dom/Expressions.py | 39 |
4 files changed, 38 insertions, 134 deletions
diff --git a/pyGHDL/dom/Symbol.py b/pyGHDL/dom/Symbol.py index ffe45f89b..020f9fbc7 100644 --- a/pyGHDL/dom/Symbol.py +++ b/pyGHDL/dom/Symbol.py @@ -81,9 +81,6 @@ class ConstrainedSubTypeSymbol(VHDLModel_ConstrainedSubTypeSymbol): @export class SimpleObjectSymbol(VHDLModel_SimpleObjectSymbol): - def __init__(self, symbolName: str): - super().__init__(symbolName) - @classmethod def parse(cls, node): name = NodeToName(node) diff --git a/pyGHDL/dom/formatting/prettyprint.py b/pyGHDL/dom/formatting/prettyprint.py index c10b7cf87..4cbf00300 100644 --- a/pyGHDL/dom/formatting/prettyprint.py +++ b/pyGHDL/dom/formatting/prettyprint.py @@ -4,16 +4,12 @@ from pydecor import export from pyVHDLModel.VHDLModel import ( GenericInterfaceItem, - Expression, Direction, Mode, NamedEntity, PortInterfaceItem, - BinaryExpression, IdentityExpression, - UnaryExpression, - AggregateElement, - WithDefaultExpression, + WithDefaultExpression ) from pyGHDL import GHDLBaseException @@ -35,9 +31,7 @@ from pyGHDL.dom.InterfaceItem import ( from pyGHDL.dom.Symbol import ( SimpleSubTypeSymbol, ConstrainedSubTypeSymbol, - SimpleObjectSymbol, ) -from pyGHDL.dom.Literal import IntegerLiteral, CharacterLiteral, FloatingPointLiteral from pyGHDL.dom.Expression import ( SubtractionExpression, AdditionExpression, @@ -47,45 +41,11 @@ from pyGHDL.dom.Expression import ( AbsoluteExpression, NegationExpression, ExponentiationExpression, - Aggregate, ParenthesisExpression, ) -from pyGHDL.dom.Aggregates import ( - SimpleAggregateElement, - IndexedAggregateElement, - RangedAggregateElement, - NamedAggregateElement, - OthersAggregateElement, -) StringBuffer = List[str] -DirectionTranslation = {Direction.To: "to", Direction.DownTo: "downto"} - -ModeTranslation = { - Mode.In: "in", - Mode.Out: "out", - Mode.InOut: "inout", - Mode.Buffer: "buffer", - Mode.Linkage: "linkage", -} - -UnaryExpressionTranslation = { - IdentityExpression: (" +", ""), - NegationExpression: (" -", ""), - InverseExpression: ("not ", ""), - AbsoluteExpression: ("abs ", ""), - ParenthesisExpression: ("(", ")"), -} - -BinaryExpressionTranslation = { - AdditionExpression: ("", " + ", ""), - SubtractionExpression: ("", " - ", ""), - MultiplyExpression: ("", " * ", ""), - DivisionExpression: ("", " / ", ""), - ExponentiationExpression: ("", "**", ""), -} - @export class PrettyPrintException(GHDLBaseException): @@ -286,10 +246,10 @@ class PrettyPrint: prefix = " " * level buffer.append( - "{prefix} - {name} : {mode} {subtypeindication}{initialValue}".format( + "{prefix} - {name} : {mode!s} {subtypeindication}{initialValue}".format( prefix=prefix, name=generic.Name, - mode=ModeTranslation[generic.Mode], + mode=generic.Mode, subtypeindication=self.formatSubtypeIndication( generic.SubType, "generic", generic.Name ), @@ -306,10 +266,10 @@ class PrettyPrint: prefix = " " * level buffer.append( - "{prefix} - {name} : {mode} {subtypeindication}{initialValue}".format( + "{prefix} - {name} : {mode!s} {subtypeindication}{initialValue}".format( prefix=prefix, name=port.Name, - mode=ModeTranslation[port.Mode], + mode=port.Mode, subtypeindication=self.formatSubtypeIndication( port.SubType, "port", port.Name ), @@ -379,81 +339,7 @@ class PrettyPrint: if item.DefaultExpression is None: return "" - return " := {expr}".format(expr=self.formatExpression(item.DefaultExpression)) + return " := {expr!s}".format(expr=item.DefaultExpression) def formatRange(self, r: Range): - return "{left} {dir} {right}".format( - left=self.formatExpression(r.LeftBound), - right=self.formatExpression(r.RightBound), - dir=DirectionTranslation[r.Direction], - ) - - def formatExpression(self, expression: Expression) -> str: - if isinstance(expression, SimpleObjectSymbol): - return "{name}".format(name=expression.SymbolName) - elif isinstance(expression, IntegerLiteral): - return "{value}".format(value=expression.Value) - elif isinstance(expression, FloatingPointLiteral): - return "{value}".format(value=expression.Value) - elif isinstance(expression, CharacterLiteral): - return "'{value}'".format(value=expression.Value) - elif isinstance(expression, UnaryExpression): - try: - operator = UnaryExpressionTranslation[type(expression)] - except KeyError: - raise PrettyPrintException("Unhandled operator for unary expression.") - - return "{leftOp}{operand}{rightOp}".format( - leftOp=operator[0], - rightOp=operator[1], - operand=self.formatExpression(expression.Operand), - ) - elif isinstance(expression, BinaryExpression): - try: - operator = BinaryExpressionTranslation[type(expression)] - except KeyError: - raise PrettyPrintException("Unhandled operator for binary expression.") - - 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( - choices=", ".join( - [ - self.formatAggregateElement(element) - for element in expression.Elements - ] - ) - ) - else: - raise PrettyPrintException("Unhandled expression kind.") - - def formatAggregateElement(self, aggregateElement: AggregateElement): - if isinstance(aggregateElement, SimpleAggregateElement): - return "{value}".format( - value=self.formatExpression(aggregateElement.Expression) - ) - elif isinstance(aggregateElement, IndexedAggregateElement): - return "{index} => {value}".format( - index=self.formatExpression(aggregateElement.Index), - value=self.formatExpression(aggregateElement.Expression), - ) - elif isinstance(aggregateElement, RangedAggregateElement): - return "{range} => {value}".format( - range=self.formatRange(aggregateElement.Range), - value=self.formatExpression(aggregateElement.Expression), - ) - elif isinstance(aggregateElement, NamedAggregateElement): - return "{name} => {value}".format( - name=aggregateElement.Name, - value=self.formatExpression(aggregateElement.Expression), - ) - elif isinstance(aggregateElement, OthersAggregateElement): - return "other => {value}".format( - value=self.formatExpression(aggregateElement.Expression) - ) + return str(r) diff --git a/pyGHDL/requirements.txt b/pyGHDL/requirements.txt index 0b7708418..d8892cfae 100644 --- a/pyGHDL/requirements.txt +++ b/pyGHDL/requirements.txt @@ -1,2 +1,2 @@ pydecor>=2.0.1 -pyVHDLModel>=0.10.0 +pyVHDLModel>=0.10.1 diff --git a/testsuite/pyunit/dom/Expressions.py b/testsuite/pyunit/dom/Expressions.py index 8ef013ef7..3a4f658af 100644 --- a/testsuite/pyunit/dom/Expressions.py +++ b/testsuite/pyunit/dom/Expressions.py @@ -2,6 +2,8 @@ from pathlib import Path from textwrap import dedent from unittest import TestCase +from pyGHDL.dom.DesignUnit import Package + from pyGHDL.dom import Expression from pyGHDL.dom.Misc import Design, Document from pyGHDL.dom.Symbol import SimpleObjectSymbol @@ -33,17 +35,36 @@ class Expressions(TestCase): document = Document(self._filename) design.Documents.append(document) - self.assertEqual(len(design.Documents[0].Packages), 1) - package = design.Documents[0].Packages[0] - self.assertTrue(package.Name == "package_1") - self.assertEqual(len(package.DeclaredItems), 1) - + package: Package = design.Documents[0].Packages[0] item: Constant = package.DeclaredItems[0] - self.assertTrue(isinstance(item, Constant)) - self.assertTrue(item.Name == "c0") - self.assertTrue(item.SubType.SymbolName == "boolean") - default: Expression = item.DefaultExpression self.assertTrue(isinstance(default, InverseExpression)) self.assertTrue(isinstance(default.Operand, SimpleObjectSymbol)) self.assertTrue(default.Operand.SymbolName == "true") + + # def test_Aggregare(self): + # self._filename: Path = self._root / "{className}.vhdl".format(className=self.__class__.__name__) + # + # sourceCode = dedent("""\ + # package package_1 is + # constant c0 : integer_vector := (0, 1, 2); 0 =>); + # constant c1 : integer_vector := (0 => 0, 1 => 1, 2 => 2); + # constant c3 : integer_vector := (a => 0, b => 1, c => 2); + # constant c3 : integer_vector := (0 to 2 => 3, 3 to 4 => 2); + # constant c2 : integer_vector := (others => 0); + # end package; + # """) + # + # with self._filename.open(mode="w", encoding="utf-8") as file: + # file.write(sourceCode) + # + # design = Design() + # document = Document(self._filename) + # design.Documents.append(document) + # + # package: Package = design.Documents[0].Packages[0] + # item: Constant = package.DeclaredItems[0] + # default: Expression = item.DefaultExpression + # self.assertTrue(isinstance(default, InverseExpression)) + # self.assertTrue(isinstance(default.Operand, SimpleObjectSymbol)) + # self.assertTrue(default.Operand.SymbolName == "true") |