aboutsummaryrefslogtreecommitdiffstats
path: root/pyGHDL
diff options
context:
space:
mode:
authorPatrick Lehmann <Patrick.Lehmann@plc2.de>2021-01-06 11:20:42 +0100
committertgingold <tgingold@users.noreply.github.com>2021-01-10 10:14:16 +0100
commit015adfc69e63e46c7823524032636faa99106c1f (patch)
tree8618ae60c4fdfb2c7fc47464d378dedf85e22c2a /pyGHDL
parentc633188e24e35aeb540b89ab142efabef0801f13 (diff)
downloadghdl-015adfc69e63e46c7823524032636faa99106c1f.tar.gz
ghdl-015adfc69e63e46c7823524032636faa99106c1f.tar.bz2
ghdl-015adfc69e63e46c7823524032636faa99106c1f.zip
Implemented a decorator to pre-calculate enum position to enum literal name lookups.
Diffstat (limited to 'pyGHDL')
-rw-r--r--pyGHDL/libghdl/_decorator.py33
-rw-r--r--pyGHDL/libghdl/_types.py18
-rw-r--r--pyGHDL/libghdl/utils/__init__.py57
3 files changed, 70 insertions, 38 deletions
diff --git a/pyGHDL/libghdl/_decorator.py b/pyGHDL/libghdl/_decorator.py
new file mode 100644
index 000000000..01863381a
--- /dev/null
+++ b/pyGHDL/libghdl/_decorator.py
@@ -0,0 +1,33 @@
+from functools import wraps
+from typing import Callable, List
+
+from pydecor import export
+
+
+@export
+def EnumLookupTable(cls) -> Callable:
+ """
+ Decorator to precalculate a enum lookup table (LUT) for enum position to
+ enum literal name.
+
+ .. todo:: Make compatible to chained decorators
+
+ :param cls: Enumerator class for which a LUT shall be pre-calculated.
+ """
+ def decorator(func) -> Callable:
+ def gen() -> List[str]:
+ d = [e for e in dir(cls) if e[0] != "_"]
+ res = [None] * len(d)
+ for e in d:
+ res[getattr(cls, e)] = e
+ return res
+
+ __lut = gen()
+
+ def wrapper(id: int) -> str:
+ # function that replaces the placeholder function
+ return __lut[id]
+
+ return wrapper
+
+ return decorator
diff --git a/pyGHDL/libghdl/_types.py b/pyGHDL/libghdl/_types.py
new file mode 100644
index 000000000..2a486a15b
--- /dev/null
+++ b/pyGHDL/libghdl/_types.py
@@ -0,0 +1,18 @@
+from typing import TypeVar
+
+__all__ = [
+ 'ErrorIndex',
+ 'MessageIdWarnings',
+ 'NameId',
+ 'SourceFileEntry',
+ 'Iir',
+ 'IirKind'
+]
+
+ErrorIndex = TypeVar('ErrorIndex', bound=int)
+MessageIdWarnings = TypeVar('MessageIdWarnings', bound=int)
+NameId = TypeVar('NameId', bound=int)
+SourceFileEntry = TypeVar('SourceFileEntry', bound=int)
+
+Iir = TypeVar('Iir', bound=int)
+IirKind = TypeVar('IirKind', bound=int)
diff --git a/pyGHDL/libghdl/utils/__init__.py b/pyGHDL/libghdl/utils/__init__.py
index a98b6c963..3b64e7698 100644
--- a/pyGHDL/libghdl/utils/__init__.py
+++ b/pyGHDL/libghdl/utils/__init__.py
@@ -37,6 +37,8 @@ from typing import List, Any, Generator
from pydecor import export
+from pyGHDL.libghdl._decorator import EnumLookupTable
+from pyGHDL.libghdl._types import NameId
import pyGHDL.libghdl.name_table as name_table
import pyGHDL.libghdl.files_map as files_map
import pyGHDL.libghdl.vhdl.nodes as nodes
@@ -46,54 +48,33 @@ import pyGHDL.libghdl.vhdl.flists as flists
@export
-def name_image(nameid) -> str:
- """Lookup a :obj:`nameid` and return its string."""
- return name_table.Get_Name_Ptr(nameid).decode("utf-8")
+def name_image(NameId: NameId) -> str:
+ """Lookup a :obj:`NameId` and return its string."""
+ return name_table.Get_Name_Ptr(NameId)
-def _build_enum_image(cls) -> List[str]:
- """Create a lookup table for enumeration values to literal names."""
- d = [e for e in dir(cls) if e[0] != "_"]
- res = [None] * len(d)
- for e in d:
- res[getattr(cls, e)] = e
- return res
+#@export # FIXME: see above
+@EnumLookupTable(nodes_meta.fields)
+def fields_image(idx: int) -> str:
+ """String representation of Nodes_Meta.fields :obj:`idx`."""
-_fields_image = _build_enum_image(nodes_meta.fields)
+#@export # FIXME: see above
+@EnumLookupTable(nodes.Iir_Kind)
+def kind_image(k: int) -> str:
+ """String representation of Nodes.Iir_Kind :obj:`k`."""
-@export
-def fields_image(idx) -> str:
- """String representation of field :obj:`idx`."""
- return _fields_image[idx]
-
-
-_kind_image = _build_enum_image(nodes.Iir_Kind)
-
-
-@export
-def kind_image(k) -> str:
- """String representation of Iir_Kind :obj:`k`."""
- return _kind_image[k]
-
-
-_types_image = _build_enum_image(nodes_meta.types)
-
-
-@export
-def types_image(t) -> str:
+#@export # FIXME: see above
+@EnumLookupTable(nodes_meta.types)
+def types_image(t: int) -> str:
"""String representation of Nodes_Meta.Types :obj:`t`."""
- return _types_image[t]
-_attr_image = _build_enum_image(nodes_meta.Attr)
-
-
-@export
-def attr_image(a) -> str:
+#@export # FIXME: see above
+@EnumLookupTable(nodes_meta.Attr)
+def attr_image(a: int) -> str:
"""String representation of Nodes_Meta.Attr :obj:`a`."""
- return _attr_image[a]
@export