-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Make `cookiecutter` a runtime dependency * Add logic for project generation * Add integration test for project generatiion
- Loading branch information
1 parent
e85cda0
commit 1f2715a
Showing
19 changed files
with
737 additions
and
116 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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
from .generate import GenContext, generate |
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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
from __future__ import annotations | ||
|
||
from pathlib import Path | ||
from typing import TYPE_CHECKING, TypedDict | ||
|
||
from ..kore.internal import KoreDefn | ||
from .k2lean4 import K2Lean4 | ||
from .model import Module | ||
|
||
if TYPE_CHECKING: | ||
from collections.abc import Callable, Iterable | ||
|
||
|
||
class GenContext(TypedDict): | ||
package_name: str | ||
library_name: str | ||
|
||
|
||
def generate( | ||
definition_dir: str | Path, | ||
context: GenContext, | ||
*, | ||
output_dir: str | Path | None = None, | ||
) -> Path: | ||
definition_dir = Path(definition_dir) | ||
output_dir = Path(output_dir) if output_dir is not None else Path('.') | ||
|
||
defn = _load_defn(definition_dir) | ||
k2lean4 = K2Lean4(defn) | ||
genmodel = { | ||
'Sorts': (k2lean4.sort_module, ['Prelude']), | ||
'Func': (k2lean4.func_module, ['Sorts']), | ||
'Inj': (k2lean4.inj_module, ['Sorts']), | ||
'Rewrite': (k2lean4.rewrite_module, ['Func', 'Inj']), | ||
} | ||
|
||
modules = _gen_modules(context['library_name'], genmodel) | ||
res = _gen_template(output_dir, context) | ||
_write_modules(res, modules) | ||
return res | ||
|
||
|
||
def _gen_template(output_dir: Path, context: GenContext) -> Path: | ||
from cookiecutter.generate import generate_files | ||
|
||
template_dir = Path(__file__).parent / 'template' | ||
gen_res = generate_files( | ||
repo_dir=str(template_dir), | ||
output_dir=str(output_dir), | ||
context={'cookiecutter': context}, | ||
) | ||
|
||
res = Path(gen_res) | ||
assert res.is_dir() | ||
return res | ||
|
||
|
||
def _load_defn(definition_dir: Path) -> KoreDefn: | ||
from ..kore.parser import KoreParser | ||
|
||
kore_text = (definition_dir / 'definition.kore').read_text() | ||
definition = KoreParser(kore_text).definition() | ||
|
||
defn = KoreDefn.from_definition(definition) | ||
defn = defn.project_to_rewrites() | ||
return defn | ||
|
||
|
||
def _gen_modules( | ||
library_name: str, | ||
genmodel: dict[str, tuple[Callable[[], Module], list[str]]], | ||
) -> dict[Path, Module]: | ||
def gen_module(generator: Callable[[], Module], imports: Iterable[str]) -> Module: | ||
imports = tuple(f'{library_name}.{importt}' for importt in imports) | ||
module = generator() | ||
module = Module(imports=imports, commands=module.commands) | ||
return module | ||
|
||
return { | ||
Path(library_name) / f'{name}.lean': gen_module(generator, imports) | ||
for name, (generator, imports) in genmodel.items() | ||
} | ||
|
||
|
||
def _write_modules(output_dir: Path, modules: dict[Path, Module]) -> None: | ||
for path, module in modules.items(): | ||
(output_dir / path).write_text(str(module)) |
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
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"package_name": null, | ||
"library_name": null | ||
} |
1 change: 1 addition & 0 deletions
1
pyk/src/pyk/klean/template/{{ cookiecutter.package_name }}/.gitignore
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
/.lake |
9 changes: 9 additions & 0 deletions
9
pyk/src/pyk/klean/template/{{ cookiecutter.package_name }}/lakefile.toml
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
name = "{{ cookiecutter.package_name }}" | ||
version = "0.1.0" | ||
defaultTargets = ["{{ cookiecutter.library_name }}"] | ||
weakLeanArgs = [ | ||
"-D maxHeartbeats=10000000" | ||
] | ||
|
||
[[lean_lib]] | ||
name = "{{ cookiecutter.library_name }}" |
1 change: 1 addition & 0 deletions
1
pyk/src/pyk/klean/template/{{ cookiecutter.package_name }}/lean-toolchain
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
stable |
1 change: 1 addition & 0 deletions
1
...c/pyk/klean/template/{{ cookiecutter.package_name }}/{{ cookiecutter.library_name }}.lean
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
import {{ cookiecutter.library_name }}.Rewrite |
Oops, something went wrong.