Skip to content

Commit

Permalink
Merge pull request #18 from lynnporu/dev
Browse files Browse the repository at this point in the history
Forbid to change global variables
  • Loading branch information
lynnporu authored Jan 14, 2022
2 parents e46c6d3 + a8f8760 commit 3b31aaa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
21 changes: 12 additions & 9 deletions annotatec/declarations.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,28 +226,31 @@ def __getattr__(self, key):
return self.members[key]


class MembersWrapper:
class DeclarationWrapper:

def __init__(self, subject, wrapper):
self.subject = subject
self.wrapper = wrapper

def __getattr__(self, key):
try:
return getattr(self.wrapper, key)
except KeyError:
return getattr(self.subject, key)

def __call__(self, *args, **kwargs):
return self.subject(*args, **kwargs)
return self.subject.__call__(*args, **kwargs)

def __mul__(self, amount):
return MembersWrapper(
subject=(self.subject * amount),
subject=(self.subject.__mul__(amount)),
wrapper=self.wrapper
)


class MembersWrapper(DeclarationWrapper):

def __getattr__(self, key):
try:
return getattr(self.wrapper, key)
except KeyError:
return getattr(self.subject, key)


class StructDeclaration(Declaration, MembersDeclaration):
type_name: str = "struct"
plural_units = ["member"]
Expand Down
10 changes: 10 additions & 0 deletions annotatec/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from . import libtypes
from . import parser
from . import declarations


class Loader:
Expand Down Expand Up @@ -48,6 +49,15 @@ def __init__(
def __getattr__(self, key):
return self.parser.declarations.compile(key)

def __setattr__(self, key, value):
compiled = self.parser.declarations.compile(key)
if isinstance(compiled, declarations.VariableDeclaration):
raise ValueError(
f"Better implement setter function for `{key}` global "
"variable.")
else:
raise RuntimeError(f"`{key}` is not a variable")

def parse_sources(
self,
sources: typing.Union[
Expand Down

0 comments on commit 3b31aaa

Please sign in to comment.