-
Notifications
You must be signed in to change notification settings - Fork 24
Add support for enums and attributes #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 4 commits
80606e4
c8e32cc
bd63c99
f3811d5
0240797
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -136,7 +136,7 @@ class DataType(Element): | |
| """ | ||
| Base class for all data types | ||
| """ | ||
| def __init__(self, name: str | None) -> None: | ||
| def __init__(self, name: str = "") -> None: | ||
| self.name = name | ||
|
|
||
|
|
||
|
|
@@ -150,7 +150,7 @@ def __init__(self, | |
| pointer: bool = False, | ||
| volatile: bool = False, | ||
| array: int | None = None) -> None: # Only used for typedefs to other array types | ||
| super().__init__(None) | ||
| super().__init__("") | ||
| self.base_type = base_type | ||
| self.const = const | ||
| self.volatile = volatile | ||
|
|
@@ -168,6 +168,45 @@ def qualifier(self, name) -> bool: | |
| else: | ||
| raise KeyError(name) | ||
|
|
||
| class EnumMember(Element): | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be named EnumValue
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually not. It is like StructMember. This is also member of Enum type, which has name and value
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another point of view:
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about StructElement?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well it is up to you. But it should be usable for both cases. I am coauthor of eRPC project (similar to this) and at the beginning i was lead by USA architect who decided to use StructMember and EnumMember. Sounds good to me. https://github.com/EmbeddedRPC/erpc/tree/develop/erpcgen/src/types
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could be also StructItem and EnumItem |
||
| """ | ||
| Enum element. | ||
| """ | ||
|
|
||
| def __init__(self, | ||
| name: str, | ||
| value: int | None) -> None: | ||
| self.name = name | ||
| self.value = value | ||
|
|
||
|
|
||
| class Enum(DataType): | ||
| """ | ||
| A enum definition | ||
| """ | ||
|
|
||
| def __init__(self, name: str, members: list[EnumMember] = [], attributes: list[str] = []) -> None: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You must never use empty list as default value in functions. It's dangerous.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? It is valid case, where you have no members.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. t's a very common mistake among newcomers to Python. Google it, there are great explanations on the internet.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That just sad.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's safer to do a proper check against None. These days however I always take it one step further and double-check that each individual element in the given argument list is of expected type. I usually do this by calling a helper function (usually called |
||
| super().__init__(name) | ||
| if isinstance(members, list): | ||
| self.members: list[EnumMember] = members.copy() | ||
| enumValue = -1 | ||
| for enum in self.members: | ||
| if enum.value == None: | ||
| enumValue += 1 | ||
| enum.value = enumValue | ||
| else: | ||
| enumValue = enum.value | ||
| else: | ||
| raise TypeError('Invalid argument type for "elements"') | ||
| self.attributes = attributes.copy() | ||
|
|
||
| def append(self, member: EnumMember) -> None: | ||
| """ | ||
| Appends new element to the struct definition | ||
| """ | ||
| if not isinstance(member, EnumMember): | ||
| raise TypeError(f'Invalid type, expected "EnumMember", got {str(type(member))}') | ||
| self.members.append(member) | ||
|
|
||
| class StructMember(Element): | ||
| """ | ||
|
|
@@ -197,17 +236,13 @@ class Struct(DataType): | |
| """ | ||
| A struct definition | ||
| """ | ||
| def __init__(self, name: str | None, members: StructMember | list[StructMember] | None = None) -> None: | ||
| def __init__(self, name: str = "", members: list[StructMember] = [], attributes: list[str] = []) -> None: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Never use empty list as initializer.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, i just need this one so i implemented them this way. But i was thinking to have something like cfiles attribute class
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, i just need this one so i implemented them this way. But i was thinking to have something like cfiles attribute class |
||
| super().__init__(name) | ||
| self.members: list[StructMember] = [] | ||
| if members is not None: | ||
| if isinstance(members, StructMember): | ||
| self.append(members) | ||
| elif isinstance(members, list): | ||
| for member in members: | ||
| self.append(member) | ||
| else: | ||
| raise TypeError('Invalid argument type for "elements"') | ||
| if isinstance(members, list): | ||
| self.members: list[StructMember] = members.copy() | ||
| else: | ||
| raise TypeError('Invalid argument type for "elements"') | ||
| self.attributes = attributes.copy() | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't use copy. If you want to create a new list which is a copy of another, write it as |
||
|
|
||
| def append(self, member: StructMember) -> None: | ||
| """ | ||
|
|
@@ -322,7 +357,7 @@ def __init__(self, | |
| static: bool = False, | ||
| const: bool = False, # const function (as seen in C++) | ||
| extern: bool = False, | ||
| params: Variable | list[Variable] | None = None) -> None: | ||
| params: list[Variable] = []) -> None: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty list as default value |
||
| self.name = name | ||
| self.static = static | ||
| self.const = const | ||
|
|
@@ -335,13 +370,7 @@ def __init__(self, | |
| self.return_type = Type("void") | ||
| else: | ||
| raise TypeError(str(type(return_type))) | ||
| self.params: list[Variable] = [] | ||
| if params is not None: | ||
| if isinstance(params, Variable): | ||
| self.append(params) | ||
| elif isinstance(params, list): | ||
| for param in params: | ||
| self.append(param) | ||
| self.params = params.copy() | ||
|
|
||
| def append(self, param: Variable) -> "Function": | ||
| """ | ||
|
|
@@ -511,3 +540,22 @@ class Block(Sequence): | |
| """ | ||
| A sequence wrapped in braces | ||
| """ | ||
|
|
||
|
|
||
| class ConditionType(Enum): | ||
| IF = 0 | ||
| ELSE_IF = 1 | ||
| ELSE = 2 | ||
|
|
||
|
|
||
| class Condition(Block): | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now you're taking shortcuts. Not all if-statements are wrapped in braces. if(condition) ++x; else ++y; |
||
| """ | ||
| A condition wrapped in braces | ||
| """ | ||
| def __init__(self, condition: str = "", type: ConditionType = ConditionType.ELSE): | ||
| super().__init__() | ||
| self.condition = condition | ||
| self.type = type | ||
|
|
||
| if (condition and type == ConditionType.ELSE) or (not condition and type != ConditionType.ELSE): | ||
| raise Exception("Condition and type didn't match") | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why change this? It's off topic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Less complicated API. Because what is difference between "" or None? You are checking for None but not for "". So it is simpler and easier to check for "" then for None and ""
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So based on what you said bellow i understand why you are using None, but still i think it is redundant.