-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Deric-W/import
add import command
- Loading branch information
Showing
4 changed files
with
71 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
from __future__ import annotations | ||
from cmd import Cmd | ||
from importlib import import_module | ||
from typing import Any | ||
from lambda_calculus.terms import Term | ||
from lambda_calculus.visitors.normalisation import ( | ||
|
@@ -14,7 +15,7 @@ | |
from .parsing import LambdaTransformer | ||
from .aliases import Aliases | ||
|
||
__version__ = "1.1.0" | ||
__version__ = "1.2.0" | ||
__author__ = "Eric Niklas Wolf" | ||
__email__ = "[email protected]" | ||
__all__ = ( | ||
|
@@ -51,6 +52,19 @@ def parse_term(self, term: str) -> Term[str] | None: | |
self.stdout.write(error.get_context(term)) | ||
return None | ||
|
||
def import_term(self, location: str) -> Term[str] | None: | ||
"""import a term and handle error display""" | ||
module, _, name = location.strip().rpartition(".") | ||
try: | ||
term = getattr(import_module(module), name) | ||
except Exception as error: | ||
self.stdout.write(f"Error while importing: {error}\n") | ||
return None | ||
if not isinstance(term, Term): | ||
self.stdout.write(f"Error: object {term} is not a lambda term\n") | ||
return None | ||
return term | ||
|
||
def emptyline(self) -> bool: | ||
"""ignore empty lines""" | ||
return False | ||
|
@@ -95,6 +109,17 @@ def do_alias(self, arg: str) -> bool: | |
self.stdout.write("invalid Command: missing alias value\n") | ||
return False | ||
|
||
def do_import(self, arg: str) -> bool: | ||
"""import an alias from a module with name = module.name""" | ||
match arg.partition("="): | ||
case (alias, "=", location): | ||
term = self.import_term(location) | ||
if term is not None: | ||
self.aliases[alias.strip()] = term | ||
case _: | ||
self.stdout.write("invalid Command: missing import location\n") | ||
return False | ||
|
||
def do_aliases(self, _: object) -> bool: | ||
"""list defined aliases""" | ||
for alias, term in self.aliases.items(): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters