From d1b593d4e5c6529fdfa588dca693ec6b215d15f5 Mon Sep 17 00:00:00 2001 From: "Johan B.W. de Vries" Date: Tue, 11 Apr 2023 10:25:30 +0200 Subject: [PATCH] Import service names --- phasm/compiler.py | 2 +- phasm/ourlang.py | 4 ++-- phasm/parser.py | 34 ++++++++++++++++++++++++++-------- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/phasm/compiler.py b/phasm/compiler.py index 3ff1eb0..835d153 100644 --- a/phasm/compiler.py +++ b/phasm/compiler.py @@ -613,7 +613,7 @@ def import_(inp: ourlang.Function) -> wasm.Import: assert inp.imported return wasm.Import( - 'imports', + inp.imported, inp.name, inp.name, [ diff --git a/phasm/ourlang.py b/phasm/ourlang.py index 8e3dfc7..94cecc2 100644 --- a/phasm/ourlang.py +++ b/phasm/ourlang.py @@ -305,7 +305,7 @@ class Function: name: str lineno: int exported: bool - imported: bool + imported: Optional[str] statements: List[Statement] returns_type3: Type3 posonlyargs: List[FunctionParam] @@ -314,7 +314,7 @@ class Function: self.name = name self.lineno = lineno self.exported = False - self.imported = False + self.imported = None self.statements = [] self.returns_type3 = type3types.none # FIXME: This could be a placeholder self.posonlyargs = [] diff --git a/phasm/parser.py b/phasm/parser.py index 7780bdb..53bdd9a 100644 --- a/phasm/parser.py +++ b/phasm/parser.py @@ -134,16 +134,34 @@ class OurVisitor: # Do stmts at the end so we have the return value for decorator in node.decorator_list: - if not isinstance(decorator, ast.Name): - _raise_static_error(decorator, 'Function decorators must be string') - if not isinstance(decorator.ctx, ast.Load): - _raise_static_error(decorator, 'Must be load context') - _not_implemented(decorator.id in ('exported', 'imported'), 'Custom decorators') + if isinstance(decorator, ast.Call): + if not isinstance(decorator.func, ast.Name): + _raise_static_error(decorator, 'Function decorators must be string') + if not isinstance(decorator.func.ctx, ast.Load): + _raise_static_error(decorator, 'Must be load context') + _not_implemented(decorator.func.id == 'imported', 'Custom decorators') - if decorator.id == 'exported': - function.exported = True + if 1 != len(decorator.args): + _raise_static_error(decorator, 'One argument expected') + if not isinstance(decorator.args[0], ast.Constant): + _raise_static_error(decorator, 'Service name must be a constant') + if not isinstance(decorator.args[0].value, str): + _raise_static_error(decorator, 'Service name must be a stirng') + if 0 != len(decorator.keywords): # TODO: Allow for namespace keyword + _raise_static_error(decorator, 'No keyword arguments expected') + + function.imported = decorator.args[0].value else: - function.imported = True + if not isinstance(decorator, ast.Name): + _raise_static_error(decorator, 'Function decorators must be string') + if not isinstance(decorator.ctx, ast.Load): + _raise_static_error(decorator, 'Must be load context') + _not_implemented(decorator.id in ('exported', 'imported'), 'Custom decorators') + + if decorator.id == 'exported': + function.exported = True + else: + function.imported = 'imports' if node.returns is not None: # Note: `-> None` would be a ast.Constant function.returns_type3 = self.visit_type(module, node.returns)