viv-compiler 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- viv_compiler-0.1.0/LICENSE +21 -0
- viv_compiler-0.1.0/PKG-INFO +284 -0
- viv_compiler-0.1.0/README.md +241 -0
- viv_compiler-0.1.0/pyproject.toml +48 -0
- viv_compiler-0.1.0/setup.cfg +4 -0
- viv_compiler-0.1.0/src/viv_compiler/__init__.py +14 -0
- viv_compiler-0.1.0/src/viv_compiler/__main__.py +3 -0
- viv_compiler-0.1.0/src/viv_compiler/_samples/__init__.py +0 -0
- viv_compiler-0.1.0/src/viv_compiler/_samples/smoke-test.viv +5 -0
- viv_compiler-0.1.0/src/viv_compiler/api.py +58 -0
- viv_compiler-0.1.0/src/viv_compiler/backports/__init__.py +1 -0
- viv_compiler-0.1.0/src/viv_compiler/backports/backports.py +12 -0
- viv_compiler-0.1.0/src/viv_compiler/cli.py +237 -0
- viv_compiler-0.1.0/src/viv_compiler/config/__init__.py +1 -0
- viv_compiler-0.1.0/src/viv_compiler/config/config.py +88 -0
- viv_compiler-0.1.0/src/viv_compiler/core/__init__.py +5 -0
- viv_compiler-0.1.0/src/viv_compiler/core/core.py +185 -0
- viv_compiler-0.1.0/src/viv_compiler/core/importer.py +111 -0
- viv_compiler-0.1.0/src/viv_compiler/core/postprocessor.py +749 -0
- viv_compiler-0.1.0/src/viv_compiler/core/validator.py +915 -0
- viv_compiler-0.1.0/src/viv_compiler/core/visitor.py +1188 -0
- viv_compiler-0.1.0/src/viv_compiler/grammar/__init__.py +0 -0
- viv_compiler-0.1.0/src/viv_compiler/grammar/viv.peg +228 -0
- viv_compiler-0.1.0/src/viv_compiler/py.typed +1 -0
- viv_compiler-0.1.0/src/viv_compiler/types/__init__.py +3 -0
- viv_compiler-0.1.0/src/viv_compiler/types/content_public_schemas.py +420 -0
- viv_compiler-0.1.0/src/viv_compiler/types/dsl_public_schemas.py +566 -0
- viv_compiler-0.1.0/src/viv_compiler/types/internal_types.py +167 -0
- viv_compiler-0.1.0/src/viv_compiler/utils/__init__.py +1 -0
- viv_compiler-0.1.0/src/viv_compiler/utils/_version.py +2 -0
- viv_compiler-0.1.0/src/viv_compiler/utils/utils.py +171 -0
- viv_compiler-0.1.0/src/viv_compiler.egg-info/PKG-INFO +284 -0
- viv_compiler-0.1.0/src/viv_compiler.egg-info/SOURCES.txt +35 -0
- viv_compiler-0.1.0/src/viv_compiler.egg-info/dependency_links.txt +1 -0
- viv_compiler-0.1.0/src/viv_compiler.egg-info/entry_points.txt +3 -0
- viv_compiler-0.1.0/src/viv_compiler.egg-info/requires.txt +3 -0
- viv_compiler-0.1.0/src/viv_compiler.egg-info/top_level.txt +1 -0
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 James Ryan
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
@@ -0,0 +1,284 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: viv-compiler
|
3
|
+
Version: 0.1.0
|
4
|
+
Summary: Compiler for the DSL of Viv, an action system for emergent narrative.
|
5
|
+
Author-email: James Ryan <mail@jamesryan.ai>
|
6
|
+
License: MIT License
|
7
|
+
|
8
|
+
Copyright (c) 2025 James Ryan
|
9
|
+
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
12
|
+
in the Software without restriction, including without limitation the rights
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
15
|
+
furnished to do so, subject to the following conditions:
|
16
|
+
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
18
|
+
copies or substantial portions of the Software.
|
19
|
+
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
26
|
+
SOFTWARE.
|
27
|
+
Project-URL: Homepage, https://github.com/james-owen-ryan/viv
|
28
|
+
Project-URL: Repository, https://github.com/james-owen-ryan/viv/tree/main/compiler
|
29
|
+
Project-URL: Issues, https://github.com/james-owen-ryan/viv/issues
|
30
|
+
Classifier: License :: OSI Approved :: MIT License
|
31
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
32
|
+
Classifier: Programming Language :: Python :: 3.10
|
33
|
+
Classifier: Programming Language :: Python :: 3.11
|
34
|
+
Classifier: Programming Language :: Python :: 3.12
|
35
|
+
Classifier: Topic :: Software Development :: Compilers
|
36
|
+
Requires-Python: >=3.10
|
37
|
+
Description-Content-Type: text/markdown
|
38
|
+
License-File: LICENSE
|
39
|
+
Requires-Dist: arpeggio<3.0,>=2.0
|
40
|
+
Requires-Dist: pydantic<2.12,>=2.11
|
41
|
+
Requires-Dist: typing_extensions<5.0,>=4.6
|
42
|
+
Dynamic: license-file
|
43
|
+
|
44
|
+
# Viv Compiler
|
45
|
+
|
46
|
+
This package contains the reference compiler for the domain-specific language (DSL) at the heart of [Viv](https://github.com/james-owen-ryan/viv), an action system for emergent narrative.
|
47
|
+
|
48
|
+
The Viv compiler accepts a *Viv source file* (`.viv`) and produces a *Viv content bundle* in a JSON-serializable format conforming to the `CompiledContentBundle` schema defined [here](https://github.com/james-owen-ryan/viv/blob/main/compiler/src/viv_compiler/types/dsl_public_schemas.py), making it ready for usage in any Viv runtime.
|
49
|
+
|
50
|
+
Once you've installed this package, you'll have access to the two compiler interfaces that are documented below:
|
51
|
+
|
52
|
+
* A **command-line interface** (`vivc`) for invoking the compiler from the command line.
|
53
|
+
|
54
|
+
* A **Python API** for invoking the compiler programmatically.
|
55
|
+
|
56
|
+
|
57
|
+
## Table of Contents
|
58
|
+
|
59
|
+
- [Installation](#installation)
|
60
|
+
- [Command-Line Interface](#command-line-interface-cli)
|
61
|
+
- [Python API](#python-api)
|
62
|
+
- [Running from Source](#running-from-source)
|
63
|
+
- [License](#license)
|
64
|
+
|
65
|
+
|
66
|
+
## Installation
|
67
|
+
|
68
|
+
Install from PyPI:
|
69
|
+
|
70
|
+
```
|
71
|
+
pip install viv-compiler
|
72
|
+
```
|
73
|
+
|
74
|
+
This installs both the `viv_compiler` Python package and the `vivc` command-line interface.
|
75
|
+
|
76
|
+
Smoke test to confirm your installation looks good:
|
77
|
+
|
78
|
+
```
|
79
|
+
vivc --test
|
80
|
+
```
|
81
|
+
|
82
|
+
|
83
|
+
## Command-Line Interface (CLI)
|
84
|
+
|
85
|
+
Once you've installed `viv-compiler`, the Viv compiler CLI will be exposed via the command `vivc` (and its alias `viv-compiler`).
|
86
|
+
|
87
|
+
|
88
|
+
### Usage
|
89
|
+
|
90
|
+
```
|
91
|
+
vivc --input path/to/source.viv [options]
|
92
|
+
```
|
93
|
+
|
94
|
+
|
95
|
+
### Arguments
|
96
|
+
|
97
|
+
* `-i, --input <path_to_source_file>`
|
98
|
+
|
99
|
+
* Required unless `--version` or `--test` is specified.
|
100
|
+
* Relative or absolute path to the Viv source file (`.viv`) to compile.
|
101
|
+
* If you are using `include` statements to import between files, this should be the main entrypoint file.
|
102
|
+
|
103
|
+
* `-o, --output <path_to_output_file>`
|
104
|
+
|
105
|
+
* Optional.
|
106
|
+
* Path to write the compiled JSON bundle.
|
107
|
+
|
108
|
+
|
109
|
+
### Flags and Options (Optional)
|
110
|
+
|
111
|
+
* `-h, --help`
|
112
|
+
|
113
|
+
* Show help message and exit.
|
114
|
+
|
115
|
+
* `-s, --default_salience <float>`
|
116
|
+
|
117
|
+
* Sets the default salience (floating-point number) for actions when unspecified.
|
118
|
+
* Default: value from `viv_compiler.config.DEFAULT_SALIENCE_VALUE`.
|
119
|
+
|
120
|
+
* `-a, --default_associations <string ...>`
|
121
|
+
|
122
|
+
* Sets the default associations (zero or more strings) for actions when unspecified.
|
123
|
+
* Default: value from `viv_compiler.config.DEFAULT_ASSOCIATIONS_VALUE`.
|
124
|
+
|
125
|
+
* `-r, --default_reaction_priority <float>`
|
126
|
+
|
127
|
+
* Sets the default reaction priority (floating-point number) for actions when unspecified.
|
128
|
+
* Default: value from `viv_compiler.config.DEFAULT_REACTION_PRIORITY_VALUE`.
|
129
|
+
|
130
|
+
* `-m, --memoization, --no-memoization`
|
131
|
+
|
132
|
+
* Enable/disable memoization in the underlying PEG parser (slower but uses less memory).
|
133
|
+
* Default: enabled.
|
134
|
+
|
135
|
+
* `-p, --print`
|
136
|
+
|
137
|
+
* After compilation, pretty-print the compiled bundle JSON.
|
138
|
+
|
139
|
+
* `-l, --list`
|
140
|
+
|
141
|
+
* After compilation, print out a list of compiled action names.
|
142
|
+
|
143
|
+
* `-d, --debug`
|
144
|
+
|
145
|
+
* Enable verbose debugging for the underlying PEG parser.
|
146
|
+
|
147
|
+
* `-t, --test`
|
148
|
+
|
149
|
+
* Run a smoke test using a sample Viv file to confirm the installation works.
|
150
|
+
* Ignores `--input`.
|
151
|
+
|
152
|
+
* `-v, --version`
|
153
|
+
|
154
|
+
* Print the current compiler version and exit.
|
155
|
+
|
156
|
+
|
157
|
+
### Examples
|
158
|
+
|
159
|
+
Compile a source file and write the resulting content bundle to file:
|
160
|
+
|
161
|
+
```
|
162
|
+
vivc --input /path/to/my-actions.viv --output /path/to/myContentBundle.json
|
163
|
+
```
|
164
|
+
|
165
|
+
Compile a source file and log the output in the console:
|
166
|
+
|
167
|
+
```
|
168
|
+
vivc --input /path/to/my-actions.viv --print
|
169
|
+
```
|
170
|
+
|
171
|
+
Log the version number for the installed Viv compiler:
|
172
|
+
|
173
|
+
```
|
174
|
+
vivc -v
|
175
|
+
```
|
176
|
+
|
177
|
+
|
178
|
+
## Python API
|
179
|
+
|
180
|
+
The API is intended for programmatic invocation of the compiler.
|
181
|
+
|
182
|
+
|
183
|
+
### `compile_from_path()`
|
184
|
+
|
185
|
+
This function invokes the compiler for a specified Viv source file.
|
186
|
+
|
187
|
+
**Arguments**
|
188
|
+
|
189
|
+
* `source_file_path` (`Path`)
|
190
|
+
|
191
|
+
* Absolute path to a `.viv` source file.
|
192
|
+
|
193
|
+
* `default_salience` (`float`)
|
194
|
+
|
195
|
+
* Default salience for actions (if unspecified).
|
196
|
+
|
197
|
+
* `default_associations` `(list[str])`
|
198
|
+
|
199
|
+
* Default associations for actions (if unspecified).
|
200
|
+
|
201
|
+
* `default_reaction_priority` (`float`)
|
202
|
+
|
203
|
+
* Default reaction priority for actions (if unspecified).
|
204
|
+
|
205
|
+
* `use_memoization` (`bool`)
|
206
|
+
|
207
|
+
* Whether to enable memoization in the underlying PEG parser (faster but uses more memory).
|
208
|
+
|
209
|
+
* `debug` (`bool`)
|
210
|
+
|
211
|
+
* Whether to enable verbose debugging for the underlying PEG parser.
|
212
|
+
|
213
|
+
**Returns**
|
214
|
+
|
215
|
+
* The compiled Viv bundle, in a JSON-serializable format conforming to the `CompiledContentBundle` schema defined in the project code.
|
216
|
+
|
217
|
+
**Raises**
|
218
|
+
|
219
|
+
* `VivCompileError`
|
220
|
+
|
221
|
+
* Raised when compilation fails.
|
222
|
+
|
223
|
+
|
224
|
+
### `get_version()`
|
225
|
+
|
226
|
+
Returns the version string for the currently installed compiler.
|
227
|
+
|
228
|
+
|
229
|
+
### `VivCompileError`
|
230
|
+
|
231
|
+
Custom exception type raised by the API when compilation fails. Inherits from `Exception`.
|
232
|
+
|
233
|
+
|
234
|
+
### Examples
|
235
|
+
|
236
|
+
Compile a source file:
|
237
|
+
|
238
|
+
```python
|
239
|
+
from pathlib import Path
|
240
|
+
from viv_compiler import compile_from_path, VivCompileError
|
241
|
+
|
242
|
+
try:
|
243
|
+
content_bundle = compile_from_path(source_file_path=Path("my-actions.viv"))
|
244
|
+
print("Compilation succeeded:", content_bundle)
|
245
|
+
except VivCompileError as e:
|
246
|
+
print("Compilation failed:", e)
|
247
|
+
```
|
248
|
+
|
249
|
+
Print the version number for the installed Viv compiler:
|
250
|
+
|
251
|
+
```python
|
252
|
+
from viv_compiler import get_version
|
253
|
+
print(get_version())
|
254
|
+
```
|
255
|
+
|
256
|
+
|
257
|
+
## Running from Source
|
258
|
+
|
259
|
+
For contributors or developers working directly from a repo checkout:
|
260
|
+
|
261
|
+
```
|
262
|
+
# Clone the Viv monorepo
|
263
|
+
git clone https://github.com/james-owen-ryan/viv
|
264
|
+
cd viv/compiler
|
265
|
+
|
266
|
+
# Create a virtual environment
|
267
|
+
python -m venv .venv-viv-compiler
|
268
|
+
source .venv-viv-compiler/bin/activate # macOS/Linux
|
269
|
+
# Windows PowerShell: .\.venv-viv-compiler\Scripts\Activate.ps1
|
270
|
+
|
271
|
+
# Install the compiler package from source (editable)
|
272
|
+
python -m pip install -e .
|
273
|
+
|
274
|
+
# Invoke the CLI directly
|
275
|
+
python -m viv_compiler --test
|
276
|
+
|
277
|
+
# Or use the installed console script
|
278
|
+
vivc --test
|
279
|
+
```
|
280
|
+
|
281
|
+
|
282
|
+
## License
|
283
|
+
|
284
|
+
MIT License © 2025 James Ryan
|
@@ -0,0 +1,241 @@
|
|
1
|
+
# Viv Compiler
|
2
|
+
|
3
|
+
This package contains the reference compiler for the domain-specific language (DSL) at the heart of [Viv](https://github.com/james-owen-ryan/viv), an action system for emergent narrative.
|
4
|
+
|
5
|
+
The Viv compiler accepts a *Viv source file* (`.viv`) and produces a *Viv content bundle* in a JSON-serializable format conforming to the `CompiledContentBundle` schema defined [here](https://github.com/james-owen-ryan/viv/blob/main/compiler/src/viv_compiler/types/dsl_public_schemas.py), making it ready for usage in any Viv runtime.
|
6
|
+
|
7
|
+
Once you've installed this package, you'll have access to the two compiler interfaces that are documented below:
|
8
|
+
|
9
|
+
* A **command-line interface** (`vivc`) for invoking the compiler from the command line.
|
10
|
+
|
11
|
+
* A **Python API** for invoking the compiler programmatically.
|
12
|
+
|
13
|
+
|
14
|
+
## Table of Contents
|
15
|
+
|
16
|
+
- [Installation](#installation)
|
17
|
+
- [Command-Line Interface](#command-line-interface-cli)
|
18
|
+
- [Python API](#python-api)
|
19
|
+
- [Running from Source](#running-from-source)
|
20
|
+
- [License](#license)
|
21
|
+
|
22
|
+
|
23
|
+
## Installation
|
24
|
+
|
25
|
+
Install from PyPI:
|
26
|
+
|
27
|
+
```
|
28
|
+
pip install viv-compiler
|
29
|
+
```
|
30
|
+
|
31
|
+
This installs both the `viv_compiler` Python package and the `vivc` command-line interface.
|
32
|
+
|
33
|
+
Smoke test to confirm your installation looks good:
|
34
|
+
|
35
|
+
```
|
36
|
+
vivc --test
|
37
|
+
```
|
38
|
+
|
39
|
+
|
40
|
+
## Command-Line Interface (CLI)
|
41
|
+
|
42
|
+
Once you've installed `viv-compiler`, the Viv compiler CLI will be exposed via the command `vivc` (and its alias `viv-compiler`).
|
43
|
+
|
44
|
+
|
45
|
+
### Usage
|
46
|
+
|
47
|
+
```
|
48
|
+
vivc --input path/to/source.viv [options]
|
49
|
+
```
|
50
|
+
|
51
|
+
|
52
|
+
### Arguments
|
53
|
+
|
54
|
+
* `-i, --input <path_to_source_file>`
|
55
|
+
|
56
|
+
* Required unless `--version` or `--test` is specified.
|
57
|
+
* Relative or absolute path to the Viv source file (`.viv`) to compile.
|
58
|
+
* If you are using `include` statements to import between files, this should be the main entrypoint file.
|
59
|
+
|
60
|
+
* `-o, --output <path_to_output_file>`
|
61
|
+
|
62
|
+
* Optional.
|
63
|
+
* Path to write the compiled JSON bundle.
|
64
|
+
|
65
|
+
|
66
|
+
### Flags and Options (Optional)
|
67
|
+
|
68
|
+
* `-h, --help`
|
69
|
+
|
70
|
+
* Show help message and exit.
|
71
|
+
|
72
|
+
* `-s, --default_salience <float>`
|
73
|
+
|
74
|
+
* Sets the default salience (floating-point number) for actions when unspecified.
|
75
|
+
* Default: value from `viv_compiler.config.DEFAULT_SALIENCE_VALUE`.
|
76
|
+
|
77
|
+
* `-a, --default_associations <string ...>`
|
78
|
+
|
79
|
+
* Sets the default associations (zero or more strings) for actions when unspecified.
|
80
|
+
* Default: value from `viv_compiler.config.DEFAULT_ASSOCIATIONS_VALUE`.
|
81
|
+
|
82
|
+
* `-r, --default_reaction_priority <float>`
|
83
|
+
|
84
|
+
* Sets the default reaction priority (floating-point number) for actions when unspecified.
|
85
|
+
* Default: value from `viv_compiler.config.DEFAULT_REACTION_PRIORITY_VALUE`.
|
86
|
+
|
87
|
+
* `-m, --memoization, --no-memoization`
|
88
|
+
|
89
|
+
* Enable/disable memoization in the underlying PEG parser (slower but uses less memory).
|
90
|
+
* Default: enabled.
|
91
|
+
|
92
|
+
* `-p, --print`
|
93
|
+
|
94
|
+
* After compilation, pretty-print the compiled bundle JSON.
|
95
|
+
|
96
|
+
* `-l, --list`
|
97
|
+
|
98
|
+
* After compilation, print out a list of compiled action names.
|
99
|
+
|
100
|
+
* `-d, --debug`
|
101
|
+
|
102
|
+
* Enable verbose debugging for the underlying PEG parser.
|
103
|
+
|
104
|
+
* `-t, --test`
|
105
|
+
|
106
|
+
* Run a smoke test using a sample Viv file to confirm the installation works.
|
107
|
+
* Ignores `--input`.
|
108
|
+
|
109
|
+
* `-v, --version`
|
110
|
+
|
111
|
+
* Print the current compiler version and exit.
|
112
|
+
|
113
|
+
|
114
|
+
### Examples
|
115
|
+
|
116
|
+
Compile a source file and write the resulting content bundle to file:
|
117
|
+
|
118
|
+
```
|
119
|
+
vivc --input /path/to/my-actions.viv --output /path/to/myContentBundle.json
|
120
|
+
```
|
121
|
+
|
122
|
+
Compile a source file and log the output in the console:
|
123
|
+
|
124
|
+
```
|
125
|
+
vivc --input /path/to/my-actions.viv --print
|
126
|
+
```
|
127
|
+
|
128
|
+
Log the version number for the installed Viv compiler:
|
129
|
+
|
130
|
+
```
|
131
|
+
vivc -v
|
132
|
+
```
|
133
|
+
|
134
|
+
|
135
|
+
## Python API
|
136
|
+
|
137
|
+
The API is intended for programmatic invocation of the compiler.
|
138
|
+
|
139
|
+
|
140
|
+
### `compile_from_path()`
|
141
|
+
|
142
|
+
This function invokes the compiler for a specified Viv source file.
|
143
|
+
|
144
|
+
**Arguments**
|
145
|
+
|
146
|
+
* `source_file_path` (`Path`)
|
147
|
+
|
148
|
+
* Absolute path to a `.viv` source file.
|
149
|
+
|
150
|
+
* `default_salience` (`float`)
|
151
|
+
|
152
|
+
* Default salience for actions (if unspecified).
|
153
|
+
|
154
|
+
* `default_associations` `(list[str])`
|
155
|
+
|
156
|
+
* Default associations for actions (if unspecified).
|
157
|
+
|
158
|
+
* `default_reaction_priority` (`float`)
|
159
|
+
|
160
|
+
* Default reaction priority for actions (if unspecified).
|
161
|
+
|
162
|
+
* `use_memoization` (`bool`)
|
163
|
+
|
164
|
+
* Whether to enable memoization in the underlying PEG parser (faster but uses more memory).
|
165
|
+
|
166
|
+
* `debug` (`bool`)
|
167
|
+
|
168
|
+
* Whether to enable verbose debugging for the underlying PEG parser.
|
169
|
+
|
170
|
+
**Returns**
|
171
|
+
|
172
|
+
* The compiled Viv bundle, in a JSON-serializable format conforming to the `CompiledContentBundle` schema defined in the project code.
|
173
|
+
|
174
|
+
**Raises**
|
175
|
+
|
176
|
+
* `VivCompileError`
|
177
|
+
|
178
|
+
* Raised when compilation fails.
|
179
|
+
|
180
|
+
|
181
|
+
### `get_version()`
|
182
|
+
|
183
|
+
Returns the version string for the currently installed compiler.
|
184
|
+
|
185
|
+
|
186
|
+
### `VivCompileError`
|
187
|
+
|
188
|
+
Custom exception type raised by the API when compilation fails. Inherits from `Exception`.
|
189
|
+
|
190
|
+
|
191
|
+
### Examples
|
192
|
+
|
193
|
+
Compile a source file:
|
194
|
+
|
195
|
+
```python
|
196
|
+
from pathlib import Path
|
197
|
+
from viv_compiler import compile_from_path, VivCompileError
|
198
|
+
|
199
|
+
try:
|
200
|
+
content_bundle = compile_from_path(source_file_path=Path("my-actions.viv"))
|
201
|
+
print("Compilation succeeded:", content_bundle)
|
202
|
+
except VivCompileError as e:
|
203
|
+
print("Compilation failed:", e)
|
204
|
+
```
|
205
|
+
|
206
|
+
Print the version number for the installed Viv compiler:
|
207
|
+
|
208
|
+
```python
|
209
|
+
from viv_compiler import get_version
|
210
|
+
print(get_version())
|
211
|
+
```
|
212
|
+
|
213
|
+
|
214
|
+
## Running from Source
|
215
|
+
|
216
|
+
For contributors or developers working directly from a repo checkout:
|
217
|
+
|
218
|
+
```
|
219
|
+
# Clone the Viv monorepo
|
220
|
+
git clone https://github.com/james-owen-ryan/viv
|
221
|
+
cd viv/compiler
|
222
|
+
|
223
|
+
# Create a virtual environment
|
224
|
+
python -m venv .venv-viv-compiler
|
225
|
+
source .venv-viv-compiler/bin/activate # macOS/Linux
|
226
|
+
# Windows PowerShell: .\.venv-viv-compiler\Scripts\Activate.ps1
|
227
|
+
|
228
|
+
# Install the compiler package from source (editable)
|
229
|
+
python -m pip install -e .
|
230
|
+
|
231
|
+
# Invoke the CLI directly
|
232
|
+
python -m viv_compiler --test
|
233
|
+
|
234
|
+
# Or use the installed console script
|
235
|
+
vivc --test
|
236
|
+
```
|
237
|
+
|
238
|
+
|
239
|
+
## License
|
240
|
+
|
241
|
+
MIT License © 2025 James Ryan
|
@@ -0,0 +1,48 @@
|
|
1
|
+
[build-system]
|
2
|
+
requires = ["setuptools>=61.0"]
|
3
|
+
build-backend = "setuptools.build_meta"
|
4
|
+
|
5
|
+
[project]
|
6
|
+
name = "viv-compiler"
|
7
|
+
version = "0.1.0" # This gets updated programmatically by `viv/scripts/bump_version.sh`
|
8
|
+
description = "Compiler for the DSL of Viv, an action system for emergent narrative."
|
9
|
+
license = { file = "LICENSE" }
|
10
|
+
authors = [{ name = "James Ryan", email = "mail@jamesryan.ai" }]
|
11
|
+
requires-python = ">=3.10"
|
12
|
+
dependencies = [
|
13
|
+
"arpeggio>=2.0,<3.0",
|
14
|
+
"pydantic>=2.11,<2.12",
|
15
|
+
"typing_extensions>=4.6,<5.0",
|
16
|
+
]
|
17
|
+
classifiers = [
|
18
|
+
"License :: OSI Approved :: MIT License",
|
19
|
+
"Programming Language :: Python :: 3 :: Only",
|
20
|
+
"Programming Language :: Python :: 3.10",
|
21
|
+
"Programming Language :: Python :: 3.11",
|
22
|
+
"Programming Language :: Python :: 3.12",
|
23
|
+
"Topic :: Software Development :: Compilers",
|
24
|
+
]
|
25
|
+
|
26
|
+
[project.readme]
|
27
|
+
file = "README.md"
|
28
|
+
content-type = "text/markdown"
|
29
|
+
|
30
|
+
[project.urls]
|
31
|
+
Homepage = "https://github.com/james-owen-ryan/viv"
|
32
|
+
Repository = "https://github.com/james-owen-ryan/viv/tree/main/compiler"
|
33
|
+
Issues = "https://github.com/james-owen-ryan/viv/issues"
|
34
|
+
|
35
|
+
[project.scripts]
|
36
|
+
vivc = "viv_compiler.cli:main"
|
37
|
+
viv-compiler = "viv_compiler.cli:main" # Alias in case of a name collision
|
38
|
+
|
39
|
+
[tool.setuptools]
|
40
|
+
package-dir = { "" = "src" }
|
41
|
+
include-package-data = true
|
42
|
+
license-files = ["LICENSE*"]
|
43
|
+
|
44
|
+
[tool.setuptools.packages.find]
|
45
|
+
where = ["src"]
|
46
|
+
|
47
|
+
[tool.setuptools.package-data]
|
48
|
+
"viv_compiler" = ["grammar/viv.peg", "py.typed", "_samples/*.viv"]
|
@@ -0,0 +1,14 @@
|
|
1
|
+
try:
|
2
|
+
from importlib.metadata import version, PackageNotFoundError
|
3
|
+
try:
|
4
|
+
__version__ = version("viv-compiler")
|
5
|
+
except PackageNotFoundError:
|
6
|
+
# If we're running from a repo checkout, not the published package,
|
7
|
+
# we can pull the version from file.
|
8
|
+
from ._version import __version__
|
9
|
+
except ImportError as e:
|
10
|
+
raise RuntimeError("viv_compiler appears to be corrupted: missing file `_version.py`") from e
|
11
|
+
|
12
|
+
|
13
|
+
from .api import compile_from_path, get_version, VivCompileError
|
14
|
+
__all__ = ["compile_from_path", "get_version", "VivCompileError", "__version__"]
|
File without changes
|
@@ -0,0 +1,58 @@
|
|
1
|
+
"""API for the Viv compiler.
|
2
|
+
|
3
|
+
This API is for machines. For human use, see the CLI exposed in `cli.py`.
|
4
|
+
"""
|
5
|
+
|
6
|
+
from pathlib import Path
|
7
|
+
from typing import Sequence
|
8
|
+
from viv_compiler import __version__
|
9
|
+
from viv_compiler.types import CompiledContentBundle
|
10
|
+
from .core import compile_viv_source_code
|
11
|
+
|
12
|
+
|
13
|
+
def compile_from_path(
|
14
|
+
*, # Require keyword arguments only
|
15
|
+
source_file_path: Path,
|
16
|
+
default_salience: float = 1.0,
|
17
|
+
default_associations: Sequence[str] = (),
|
18
|
+
default_reaction_priority: float = 1.0,
|
19
|
+
use_memoization: bool = True,
|
20
|
+
debug: bool = False,
|
21
|
+
) -> CompiledContentBundle:
|
22
|
+
"""Compile the given Viv source file to produce a JSON-serializable compiled content bundle.
|
23
|
+
|
24
|
+
Args:
|
25
|
+
source_file_path: The absolute path to the Viv source file to be parsed.
|
26
|
+
default_salience: A user-provided default salience value to use when one is not specified
|
27
|
+
in an action definition.
|
28
|
+
default_associations: A user-provided default associations value to use when one is not
|
29
|
+
specified in an action definition.
|
30
|
+
default_reaction_priority: A user-provided default reaction priority to use when one is not
|
31
|
+
specified in a reaction declaration.
|
32
|
+
use_memoization: Whether to use memoization during PEG parsing (faster, but uses more memory).
|
33
|
+
debug: Whether to invoke verbose debugging for the PEG parser itself.
|
34
|
+
|
35
|
+
Returns:
|
36
|
+
The compiled content bundle.
|
37
|
+
"""
|
38
|
+
try:
|
39
|
+
return compile_viv_source_code(
|
40
|
+
source_file_path=source_file_path,
|
41
|
+
default_salience=float(default_salience),
|
42
|
+
default_associations=list(default_associations),
|
43
|
+
default_reaction_priority=float(default_reaction_priority),
|
44
|
+
use_memoization=use_memoization,
|
45
|
+
debug=debug,
|
46
|
+
)
|
47
|
+
except Exception as e:
|
48
|
+
raise VivCompileError(str(e)) from e
|
49
|
+
|
50
|
+
|
51
|
+
def get_version() -> str:
|
52
|
+
"""Return the Viv version number associated with this compiler instance."""
|
53
|
+
return __version__
|
54
|
+
|
55
|
+
|
56
|
+
class VivCompileError(Exception):
|
57
|
+
"""Raised when Viv compilation fails."""
|
58
|
+
pass
|
@@ -0,0 +1 @@
|
|
1
|
+
from .backports import *
|