From 0aa8207987a1fde5031ee28e6bf1017ce4733388 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 d2730c2..fa34e2f 100644 --- a/phasm/compiler.py +++ b/phasm/compiler.py @@ -599,7 +599,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 84bb20e..9561590 100644 --- a/phasm/ourlang.py +++ b/phasm/ourlang.py @@ -292,7 +292,7 @@ class Function: name: str lineno: int exported: bool - imported: bool + imported: Optional[str] statements: List[Statement] returns_type3: Type3 posonlyargs: List[FunctionParam] @@ -301,7 +301,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 10b5a99..cfa2338 100644 --- a/phasm/parser.py +++ b/phasm/parser.py @@ -133,16 +133,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)