-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from vkottler/dev/type-system
Add initial type system
- Loading branch information
Showing
36 changed files
with
796 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
[DESIGN] | ||
max-args=7 | ||
max-attributes=12 | ||
max-parents=8 | ||
|
||
[MESSAGES CONTROL] | ||
disable=too-few-public-methods | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
major: 1 | ||
minor: 7 | ||
patch: 4 | ||
major: 2 | ||
minor: 0 | ||
patch: 0 | ||
entry: runtimepy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
""" | ||
A basic type-system implementation. | ||
""" | ||
|
||
# built-in | ||
from typing import Dict | ||
|
||
# third-party | ||
from vcorelib.namespace import CPP_DELIM, Namespace | ||
|
||
# internal | ||
from runtimepy.codec.protocol import Protocol | ||
from runtimepy.enum import RuntimeEnum | ||
from runtimepy.enum.registry import EnumRegistry | ||
from runtimepy.primitives.byte_order import ByteOrder | ||
from runtimepy.primitives.type import AnyPrimitiveType, PrimitiveTypes | ||
|
||
|
||
class TypeSystem: | ||
"""A class for managing a custom type system.""" | ||
|
||
def __init__(self, *namespace: str) -> None: | ||
"""Initialize this instance.""" | ||
|
||
self.primitives: Dict[str, AnyPrimitiveType] = {} | ||
self.custom: Dict[str, Protocol] = {} | ||
|
||
global_namespace = Namespace(delim=CPP_DELIM) | ||
|
||
# Register global names. | ||
for name, kind in PrimitiveTypes.items(): | ||
self.primitives[global_namespace.namespace(name)] = kind | ||
|
||
self.root_namespace = global_namespace.child(*namespace) | ||
|
||
# Register enums. | ||
self._enums = EnumRegistry() | ||
self.runtime_enum( | ||
"ByteOrder", ByteOrder.register_enum(self._enums, name="ByteOrder") | ||
) | ||
|
||
def register(self, name: str) -> Protocol: | ||
"""Register a custom type.""" | ||
|
||
new_type = Protocol(self._enums) | ||
name = self.root_namespace.namespace(name) | ||
self.custom[name] = new_type | ||
return new_type | ||
|
||
def runtime_enum(self, name: str, enum: RuntimeEnum) -> bool: | ||
"""Register an enumeration.""" | ||
|
||
name = self.root_namespace.namespace(name) | ||
|
||
result = self._enums.register(name, enum) | ||
|
||
assert name not in self.primitives, name | ||
self.primitives[name] = PrimitiveTypes[enum.primitive] | ||
|
||
return result | ||
|
||
def enum( | ||
self, name: str, items: Dict[str, int], primitive: str = "uint8" | ||
) -> None: | ||
"""Register an enumeration.""" | ||
|
||
self._enums.enum(name, "int", items=items, primitive=primitive) | ||
|
||
def size(self, name: str) -> int: | ||
"""Get the size of a named type.""" | ||
|
||
matches = list(self.root_namespace.search(pattern=name)) | ||
assert len(matches) == 1, f"Duplicate type names {name}: {matches}" | ||
name = matches[0] | ||
|
||
if name in self.primitives: | ||
return self.primitives[name].size | ||
|
||
return self.custom[name].array.size |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.