summaryrefslogtreecommitdiff
path: root/lib/sly/ast.py
diff options
context:
space:
mode:
authorDaniel Friesel <daniel.friesel@uos.de>2019-12-12 17:33:13 +0100
committerDaniel Friesel <daniel.friesel@uos.de>2019-12-12 17:33:13 +0100
commitff6fc70456354b23663bee037c5e737a2b437ca8 (patch)
tree57397e592e664bf5f20b72032be887557bc2df08 /lib/sly/ast.py
parent575a33b819d29ee99fa7d4264a943043d1d64032 (diff)
import SLY
Diffstat (limited to 'lib/sly/ast.py')
-rw-r--r--lib/sly/ast.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/lib/sly/ast.py b/lib/sly/ast.py
new file mode 100644
index 0000000..7b79ac5
--- /dev/null
+++ b/lib/sly/ast.py
@@ -0,0 +1,25 @@
+# sly/ast.py
+import sys
+
+class AST(object):
+
+ @classmethod
+ def __init_subclass__(cls, **kwargs):
+ mod = sys.modules[cls.__module__]
+ if not hasattr(cls, '__annotations__'):
+ return
+
+ hints = list(cls.__annotations__.items())
+
+ def __init__(self, *args, **kwargs):
+ if len(hints) != len(args):
+ raise TypeError(f'Expected {len(hints)} arguments')
+ for arg, (name, val) in zip(args, hints):
+ if isinstance(val, str):
+ val = getattr(mod, val)
+ if not isinstance(arg, val):
+ raise TypeError(f'{name} argument must be {val}')
+ setattr(self, name, arg)
+
+ cls.__init__ = __init__
+