Skip to content

Commit

Permalink
feat: update compilation priority depending on execution order
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Sep 11, 2022
1 parent 7d53dc4 commit d6aa9ec
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 11 deletions.
15 changes: 10 additions & 5 deletions bolt/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -115,7 +116,7 @@ def __call__(
...


@dataclass(frozen=True)
@dataclass
class ModuleManager(Mapping[TextFileBase[Any], CompiledModule]):
"""Container for managing bolt modules."""

Expand All @@ -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."""
Expand All @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -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(
{
Expand Down
8 changes: 5 additions & 3 deletions bolt/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions examples/bolt_order/beet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require:
- bolt
data_pack:
load: "src"
pipeline:
- mecha
meta:
bolt:
entrypoint: hello:entry_point
3 changes: 3 additions & 0 deletions examples/bolt_order/src/data/hello/modules/entry_point.bolt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from hello:main import i_run_last

i_run_last()
8 changes: 8 additions & 0 deletions examples/bolt_order/src/data/hello/modules/main.bolt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def i_run_last():
print(2)
append function hello:world:
say second

print(1)
append function hello:world:
say first
4 changes: 2 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
23 changes: 23 additions & 0 deletions tests/snapshots/examples__build_bolt_order__0.pack.md
Original file line number Diff line number Diff line change
@@ -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
```

0 comments on commit d6aa9ec

Please sign in to comment.