From d6aa9ecf55027e411889459986d79123239ec81b Mon Sep 17 00:00:00 2001 From: Valentin Berlier Date: Sun, 11 Sep 2022 21:12:38 +0200 Subject: [PATCH] feat: update compilation priority depending on execution order --- bolt/module.py | 15 ++++++++---- bolt/runtime.py | 8 ++++--- examples/bolt_order/beet.yml | 9 ++++++++ .../src/data/hello/modules/entry_point.bolt | 3 +++ .../src/data/hello/modules/main.bolt | 8 +++++++ poetry.lock | 4 ++-- pyproject.toml | 2 +- .../examples__build_bolt_order__0.pack.md | 23 +++++++++++++++++++ 8 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 examples/bolt_order/beet.yml create mode 100644 examples/bolt_order/src/data/hello/modules/entry_point.bolt create mode 100644 examples/bolt_order/src/data/hello/modules/main.bolt create mode 100644 tests/snapshots/examples__build_bolt_order__0.pack.md diff --git a/bolt/module.py b/bolt/module.py index 424f36d..bb4754a 100644 --- a/bolt/module.py +++ b/bolt/module.py @@ -92,6 +92,7 @@ class CompiledModule: executing: bool = False executed: bool = False execution_hooks: List[Callable[[], Any]] = field(default_factory=list) + execution_index: int = 0 @dataclass(frozen=True) @@ -115,7 +116,7 @@ def __call__( ... -@dataclass(frozen=True) +@dataclass class ModuleManager(Mapping[TextFileBase[Any], CompiledModule]): """Container for managing bolt modules.""" @@ -133,6 +134,8 @@ class ModuleManager(Mapping[TextFileBase[Any], CompiledModule]): globals: JsonDict = extra_field(default_factory=dict) builtins: Set[str] = extra_field(default_factory=set) + execution_count: int = 0 + @property def current(self) -> CompiledModule: """Return the currently executing module.""" @@ -153,8 +156,8 @@ def match_ast( self, node: AstRoot, current: Optional[Union[TextFileBase[Any], str]] = None, - ) -> CompiledModule: - """Return executable module for the current ast.""" + ) -> Tuple[CompilationUnit, CompiledModule]: + """Return the compilation unit and executable module for the current ast.""" if not current: current = self.database.current elif isinstance(current, str): @@ -163,7 +166,7 @@ def match_ast( compilation_unit = self.database[current] compilation_unit.ast = node - return self[current] + return compilation_unit, self[current] def __getitem__(self, current: Union[TextFileBase[Any], str]) -> CompiledModule: if isinstance(current, str): @@ -279,6 +282,8 @@ def eval(self, module: CompiledModule) -> AstRoot: finally: module.executing = False self.stack.pop() + self.execution_count += 1 + module.execution_index = self.execution_count return module.namespace[module.output] @@ -369,7 +374,7 @@ def load(self, f: BufferedReader) -> AstRoot: return data["ast"] def dump(self, node: AstRoot, f: BufferedWriter): - module = self.modules.match_ast(node) + _, module = self.modules.match_ast(node) self.dump_data( { diff --git a/bolt/runtime.py b/bolt/runtime.py index b8002fd..94dbca9 100644 --- a/bolt/runtime.py +++ b/bolt/runtime.py @@ -184,7 +184,7 @@ def add_entrypoint(self, *args: Union[str, Iterable[str]]): @rule(AstRoot) @internal def root(self, node: AstRoot) -> Optional[AstRoot]: - module = self.modules.match_ast(node) + compilation_unit, module = self.modules.match_ast(node) if ( isinstance(node, AstModuleRoot) @@ -205,12 +205,14 @@ def root(self, node: AstRoot) -> Optional[AstRoot]: with self.modules.error_handler( "Top-level statement raised an exception.", module.resource_location ): - return self.modules.eval(module) + node = self.modules.eval(module) + compilation_unit.priority = module.execution_index + return node def restore_module(self, key: TextFileBase[Any], node: AstModuleRoot, step: int): compilation_unit = self.modules.database[key] compilation_unit.ast = node - self.modules.database.enqueue(key, step) + self.modules.database.enqueue(key, step, compilation_unit.priority) @rule(AstModuleRoot) diff --git a/examples/bolt_order/beet.yml b/examples/bolt_order/beet.yml new file mode 100644 index 0000000..60c4315 --- /dev/null +++ b/examples/bolt_order/beet.yml @@ -0,0 +1,9 @@ +require: + - bolt +data_pack: + load: "src" +pipeline: + - mecha +meta: + bolt: + entrypoint: hello:entry_point diff --git a/examples/bolt_order/src/data/hello/modules/entry_point.bolt b/examples/bolt_order/src/data/hello/modules/entry_point.bolt new file mode 100644 index 0000000..dfd085f --- /dev/null +++ b/examples/bolt_order/src/data/hello/modules/entry_point.bolt @@ -0,0 +1,3 @@ +from hello:main import i_run_last + +i_run_last() diff --git a/examples/bolt_order/src/data/hello/modules/main.bolt b/examples/bolt_order/src/data/hello/modules/main.bolt new file mode 100644 index 0000000..eedb32a --- /dev/null +++ b/examples/bolt_order/src/data/hello/modules/main.bolt @@ -0,0 +1,8 @@ +def i_run_last(): + print(2) + append function hello:world: + say second + +print(1) +append function hello:world: + say first diff --git a/poetry.lock b/poetry.lock index 2717dd7..dacc922 100644 --- a/poetry.lock +++ b/poetry.lock @@ -356,7 +356,7 @@ python-versions = ">=3.7" [[package]] name = "mecha" -version = "0.54.10" +version = "0.55.1" description = "A powerful Minecraft command library" category = "main" optional = false @@ -817,7 +817,7 @@ testing = ["pytest (>=6)", "pytest-checkdocs (>=2.4)", "pytest-flake8", "pytest- [metadata] lock-version = "1.1" python-versions = "^3.8" -content-hash = "9d88889d399c632dfe9fe7c7d1650e14e82ef08a34563051a15cfc8e8c1628a5" +content-hash = "3e6fdc6222ef765be16fdd361efd33602f0ae0bd866a7aa2fec3fec1f5c55fbc" [metadata.files] attrs = [] diff --git a/pyproject.toml b/pyproject.toml index edd2688..6389e41 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,7 +24,7 @@ include = ["bolt/py.typed"] [tool.poetry.dependencies] python = "^3.8" beet = ">=0.71.2" -mecha = ">=0.54.10" +mecha = ">=0.55.1" [tool.poetry.dev-dependencies] pytest = "^7.1.3" diff --git a/tests/snapshots/examples__build_bolt_order__0.pack.md b/tests/snapshots/examples__build_bolt_order__0.pack.md new file mode 100644 index 0000000..1f809e4 --- /dev/null +++ b/tests/snapshots/examples__build_bolt_order__0.pack.md @@ -0,0 +1,23 @@ +# Lectern snapshot + +## Data pack + +`@data_pack pack.mcmeta` + +```json +{ + "pack": { + "pack_format": 10, + "description": "" + } +} +``` + +### hello + +`@function hello:world` + +```mcfunction +say first +say second +```