fromscratch-pebble 0.3.0__py3-none-any.whl
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.
- fromscratch_pebble-0.3.0.dist-info/METADATA +156 -0
- fromscratch_pebble-0.3.0.dist-info/RECORD +30 -0
- fromscratch_pebble-0.3.0.dist-info/WHEEL +4 -0
- fromscratch_pebble-0.3.0.dist-info/entry_points.txt +2 -0
- fromscratch_pebble-0.3.0.dist-info/licenses/LICENSE +21 -0
- pebble/__init__.py +22 -0
- pebble/__main__.py +5 -0
- pebble/_pipeline.py +137 -0
- pebble/_version.py +24 -0
- pebble/analyzer.py +1136 -0
- pebble/api.py +122 -0
- pebble/ast_nodes.py +664 -0
- pebble/builtins.py +764 -0
- pebble/bytecode.py +211 -0
- pebble/cli.py +154 -0
- pebble/compiler.py +1305 -0
- pebble/debugger.py +372 -0
- pebble/errors.py +194 -0
- pebble/formatter.py +965 -0
- pebble/lexer.py +541 -0
- pebble/linter.py +475 -0
- pebble/optimizer.py +532 -0
- pebble/parser.py +1384 -0
- pebble/py.typed +0 -0
- pebble/repl.py +206 -0
- pebble/resolver.py +393 -0
- pebble/stdlib.py +199 -0
- pebble/tokens.py +169 -0
- pebble/type_checker.py +537 -0
- pebble/vm.py +1834 -0
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: fromscratch-pebble
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: An educational compiler and bytecode VM for the Pebble language
|
|
5
|
+
Project-URL: Homepage, https://github.com/robcox2910/pebble-lang
|
|
6
|
+
Project-URL: Repository, https://github.com/robcox2910/pebble-lang
|
|
7
|
+
Project-URL: Issues, https://github.com/robcox2910/pebble-lang/issues
|
|
8
|
+
Project-URL: Documentation, https://robcox2910.github.io/pebble-lang/
|
|
9
|
+
Author: Rob Cox
|
|
10
|
+
License: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: bytecode,compiler,education,language,virtual-machine
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Intended Audience :: Education
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Classifier: Topic :: Education
|
|
19
|
+
Classifier: Topic :: Software Development :: Compilers
|
|
20
|
+
Classifier: Topic :: Software Development :: Interpreters
|
|
21
|
+
Classifier: Typing :: Typed
|
|
22
|
+
Requires-Python: >=3.14
|
|
23
|
+
Provides-Extra: dev
|
|
24
|
+
Requires-Dist: commitizen>=4.4.0; extra == 'dev'
|
|
25
|
+
Requires-Dist: hypothesis>=6.170; extra == 'dev'
|
|
26
|
+
Requires-Dist: pip-audit>=2.10.0; extra == 'dev'
|
|
27
|
+
Requires-Dist: pre-commit>=4.0.1; extra == 'dev'
|
|
28
|
+
Requires-Dist: pyright>=1.1.400; extra == 'dev'
|
|
29
|
+
Requires-Dist: pytest-cov>=6.0.0; extra == 'dev'
|
|
30
|
+
Requires-Dist: pytest>=8.3.4; extra == 'dev'
|
|
31
|
+
Requires-Dist: ruff>=0.9.3; extra == 'dev'
|
|
32
|
+
Provides-Extra: docs
|
|
33
|
+
Requires-Dist: mkdocs-material>=9.5; extra == 'docs'
|
|
34
|
+
Description-Content-Type: text/markdown
|
|
35
|
+
|
|
36
|
+
# Pebble
|
|
37
|
+
|
|
38
|
+
An educational compiler and bytecode VM for the **Pebble** programming language.
|
|
39
|
+
|
|
40
|
+
Pebble is a small imperative language that compiles to bytecode and runs on a
|
|
41
|
+
custom stack-based virtual machine. The project is designed for learning how
|
|
42
|
+
compilers work, built incrementally using TDD.
|
|
43
|
+
|
|
44
|
+
## Language Features
|
|
45
|
+
|
|
46
|
+
- Integers, floats, strings, booleans
|
|
47
|
+
- Variables (`let` declarations, `const`, reassignment)
|
|
48
|
+
- Arithmetic, comparison, bitwise, and logical operators (`and`, `or`, `not`)
|
|
49
|
+
- Short-circuiting `and` / `or` and chained comparisons (`1 < x < 10`)
|
|
50
|
+
- Control flow: `if/else`, `while`, `for` loops, `match`
|
|
51
|
+
- Collections: lists, dicts, list comprehensions, slicing, tuple unpacking
|
|
52
|
+
- Functions (`fn`, `return`), default arguments, closures, and generators (`yield`)
|
|
53
|
+
- Async / await (`async fn`, `await`)
|
|
54
|
+
- Structs, classes with inheritance, and enums
|
|
55
|
+
- Error handling: `try` / `catch` / `finally` and `throw`
|
|
56
|
+
- Modules and imports (`import`, `from ... import`)
|
|
57
|
+
- String interpolation (`"hello {name}"`)
|
|
58
|
+
- `print()` built-in
|
|
59
|
+
- Comments (`#`)
|
|
60
|
+
- Curly-brace blocks, newline-delimited statements (no semicolons)
|
|
61
|
+
|
|
62
|
+
## Example
|
|
63
|
+
|
|
64
|
+
```
|
|
65
|
+
let count = 0
|
|
66
|
+
while count < 5 {
|
|
67
|
+
if count > 0 {
|
|
68
|
+
print(count)
|
|
69
|
+
}
|
|
70
|
+
count = count + 1
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Pipeline
|
|
75
|
+
|
|
76
|
+
```
|
|
77
|
+
Source -> Lexer -> Parser -> Analyzer -> Module Resolver -> Type Checker -> Compiler -> Optimizer -> VM
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
See the [documentation](https://robcox2910.github.io/pebble-lang/) for a
|
|
81
|
+
kid-friendly walk-through of each stage.
|
|
82
|
+
|
|
83
|
+
## Quick Start
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
# Install dependencies
|
|
87
|
+
uv sync --all-extras
|
|
88
|
+
|
|
89
|
+
# Run tests
|
|
90
|
+
uv run pytest
|
|
91
|
+
|
|
92
|
+
# Lint and type check
|
|
93
|
+
uv run ruff check .
|
|
94
|
+
uv run pyright src tests
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Using Pebble from Python
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
from pebble import run_source
|
|
101
|
+
|
|
102
|
+
print(run_source('print("Hello from Pebble!")'), end="")
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
`run_source`, `compile_source`, `check` and `format_source` are Pebble's public
|
|
106
|
+
API; see [Using Pebble from Python](docs/concepts/using-pebble-from-python.md).
|
|
107
|
+
|
|
108
|
+
<!-- docs:start -->
|
|
109
|
+
## Documentation
|
|
110
|
+
|
|
111
|
+
Full docs (with kid-friendly explanations of every idea): https://robcox2910.github.io/pebble-lang/
|
|
112
|
+
<!-- docs:end -->
|
|
113
|
+
|
|
114
|
+
## Releasing
|
|
115
|
+
|
|
116
|
+
Other projects in the series ([PyStack](https://github.com/robcox2910/pystack) and [PyLLM](https://github.com/robcox2910/pyllm)) use a pinned release of this one,
|
|
117
|
+
not whatever is on `main`. To publish a change for them:
|
|
118
|
+
|
|
119
|
+
1. Merge the change to `main`.
|
|
120
|
+
2. Tag it: `git tag -a vX.Y.Z -m vX.Y.Z && git push origin vX.Y.Z`.
|
|
121
|
+
A change to the public API bumps the minor version (`v0.1.0` → `v0.2.0`);
|
|
122
|
+
anything else bumps the patch (`v0.1.0` → `v0.1.1`).
|
|
123
|
+
3. In each project that uses it, edit the tag in `[tool.uv.sources]`, run
|
|
124
|
+
`uv lock --upgrade-package <name>`, and open a pull request so CI checks
|
|
125
|
+
the pair together.
|
|
126
|
+
|
|
127
|
+
<!-- series:start -->
|
|
128
|
+
## Related Projects
|
|
129
|
+
|
|
130
|
+
This project is one of a series that builds real systems "from scratch in Python" to teach how they work. See the [series home](https://robcox2910.github.io/) for the full story.
|
|
131
|
+
|
|
132
|
+
| Project | What it teaches | Repository | Docs |
|
|
133
|
+
| --- | --- | --- | --- |
|
|
134
|
+
| PyOS | Operating systems | [py-os](https://github.com/robcox2910/py-os) | [Docs](https://robcox2910.github.io/py-os/) |
|
|
135
|
+
| **Pebble** | **Compilers and programming languages** | [pebble-lang](https://github.com/robcox2910/pebble-lang) | [Docs](https://robcox2910.github.io/pebble-lang/) |
|
|
136
|
+
| PyDB | Relational databases | [pydb](https://github.com/robcox2910/pydb) | [Docs](https://robcox2910.github.io/pydb/) |
|
|
137
|
+
| PyKV | Key-value stores | [pykv](https://github.com/robcox2910/pykv) | [Docs](https://robcox2910.github.io/pykv/) |
|
|
138
|
+
| PyDocDB | Document databases | [pydoc-db](https://github.com/robcox2910/pydoc-db) | [Docs](https://robcox2910.github.io/pydoc-db/) |
|
|
139
|
+
| PyGraphDB | Graph databases | [pygraphdb](https://github.com/robcox2910/pygraphdb) | [Docs](https://robcox2910.github.io/pygraphdb/) |
|
|
140
|
+
| PyTSDB | Time-series databases | [pytsdb](https://github.com/robcox2910/pytsdb) | [Docs](https://robcox2910.github.io/pytsdb/) |
|
|
141
|
+
| PyVecDB | Vector databases | [pyvecdb](https://github.com/robcox2910/pyvecdb) | [Docs](https://robcox2910.github.io/pyvecdb/) |
|
|
142
|
+
| PyColDB | Column-family databases | [pycoldb](https://github.com/robcox2910/pycoldb) | [Docs](https://robcox2910.github.io/pycoldb/) |
|
|
143
|
+
| PyCrypt | Cryptography | [pycrypt](https://github.com/robcox2910/pycrypt) | [Docs](https://robcox2910.github.io/pycrypt/) |
|
|
144
|
+
| PyNet | Computer networking | [pynet](https://github.com/robcox2910/pynet) | [Docs](https://robcox2910.github.io/pynet/) |
|
|
145
|
+
| PyWeb | HTTP web servers | [pyweb](https://github.com/robcox2910/pyweb) | [Docs](https://robcox2910.github.io/pyweb/) |
|
|
146
|
+
| PyGit | Version control | [pygit](https://github.com/robcox2910/pygit) | [Docs](https://robcox2910.github.io/pygit/) |
|
|
147
|
+
| PySearch | Search engines | [pysearch](https://github.com/robcox2910/pysearch) | [Docs](https://robcox2910.github.io/pysearch/) |
|
|
148
|
+
| PyMQ | Message queues | [pymq](https://github.com/robcox2910/pymq) | [Docs](https://robcox2910.github.io/pymq/) |
|
|
149
|
+
| PyLLM | Language models (the brain) | [pyllm](https://github.com/robcox2910/pyllm) | [Docs](https://robcox2910.github.io/pyllm/) |
|
|
150
|
+
| PyScheduler | Job scheduling | [pyscheduler](https://github.com/robcox2910/pyscheduler) | [Docs](https://robcox2910.github.io/pyscheduler/) |
|
|
151
|
+
| PyStack | Full-stack integration | [pystack](https://github.com/robcox2910/pystack) | [Docs](https://robcox2910.github.io/pystack/) |
|
|
152
|
+
<!-- series:end -->
|
|
153
|
+
|
|
154
|
+
## License
|
|
155
|
+
|
|
156
|
+
MIT
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
pebble/__init__.py,sha256=Ia56NojvGKghbK7GZ8UgfF_M7kFEQIB-wPtBfeTCvE0,552
|
|
2
|
+
pebble/__main__.py,sha256=ayKH22F_BPVwXQqa-xBUd0wTSIW0cNvAPugA3nJng4Q,91
|
|
3
|
+
pebble/_pipeline.py,sha256=1tL2o4VxhvFfmKx6d15EvC87dCNc6HHyCZHnN0pmV_w,4653
|
|
4
|
+
pebble/_version.py,sha256=RCY04NY0kBCyn6ttH6lnVL6Gz2gceOrt45f0yDOOU8A,520
|
|
5
|
+
pebble/analyzer.py,sha256=6yDcaNeEORG_9_Y4Vx0VjM0FD1-L2Kp06R-N-lp-tGY,48677
|
|
6
|
+
pebble/api.py,sha256=oK0-PW0OebmUI52LCZ3U_zpjBXOoGZydiMmHw8zZhxM,3755
|
|
7
|
+
pebble/ast_nodes.py,sha256=PzmQWFd2ra8i2byw8NaBTM13184-yY8cg5lJPRtk5cU,15281
|
|
8
|
+
pebble/builtins.py,sha256=naboRWF-ukLAa8moXsW0AUiqBSUfe69jO2SljFvk-Z0,23792
|
|
9
|
+
pebble/bytecode.py,sha256=wktNuLBGqNfP_9Wdr-cmN6WIHIMk9v0l1Yd1pDdKb80,7317
|
|
10
|
+
pebble/cli.py,sha256=_ac25mgldhuAf2SP99L8ELoW5Ejd7TP39HBn04ANNsc,4639
|
|
11
|
+
pebble/compiler.py,sha256=p0PeJrFJEqULHGeHIyKa9UqiQEjXvqcg-fdPoPUesY0,54102
|
|
12
|
+
pebble/debugger.py,sha256=TYdbciU57rZHq0FG_eOqdeEXYHYIr5kExvLHwDNDyFc,13276
|
|
13
|
+
pebble/errors.py,sha256=xvN5IoaqFACz7e8Nx6q_VU1bTA2HgJrW2MaQYatAQ0s,5984
|
|
14
|
+
pebble/formatter.py,sha256=rx_i7nr5aklXkA-1gXE-3RqAYK9RU4H9xzIUP7_q_KI,38361
|
|
15
|
+
pebble/lexer.py,sha256=s1XZq9qn6BHEvB-4VzIm6Jubr19MxUkDomk1RA3tsAw,18458
|
|
16
|
+
pebble/linter.py,sha256=TkPXXWDnumdU9zAGsIX7O-1ki5gJrAkuyj4evMFxOsE,17910
|
|
17
|
+
pebble/optimizer.py,sha256=rh77-2EIeMm5UJhDAcbwzIPeVa-F-rVP4G8mGo6ZAmA,18253
|
|
18
|
+
pebble/parser.py,sha256=5M8YgpmE94udYUYepZrT0R8nTUnxgeFBCxdOB5xRU08,57157
|
|
19
|
+
pebble/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
20
|
+
pebble/repl.py,sha256=GWPyubCjtMWLpykw9Kd952z0sbuUUHxdIBzvEU-56-4,7338
|
|
21
|
+
pebble/resolver.py,sha256=tDSdYuW0MTis0jkcenAuIxH346u1pHv724tDlg82UaE,17210
|
|
22
|
+
pebble/stdlib.py,sha256=EsnQaWyWxsPrOzAYO_absj8TlD_71lx_otqjNKJPm8I,6255
|
|
23
|
+
pebble/tokens.py,sha256=qoiFu5e4KCmlIlitbr9AKvSWvAKLVeDOzM0VdT47eFc,4406
|
|
24
|
+
pebble/type_checker.py,sha256=KvZXrOFdep3rcfUMZRfUL6olyXaHqsyxrklyvVkVMu4,20681
|
|
25
|
+
pebble/vm.py,sha256=MKzY-T5rnNKFPfnFk10ONgKKKwWfR4P8rVgXeDdNGCc,74622
|
|
26
|
+
fromscratch_pebble-0.3.0.dist-info/METADATA,sha256=DCsFG-oYTmLkx-GDdkBuygeCSCnhwGJHbzfmmEcYTGA,6906
|
|
27
|
+
fromscratch_pebble-0.3.0.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
|
|
28
|
+
fromscratch_pebble-0.3.0.dist-info/entry_points.txt,sha256=S4qheUTGQBJUiEuAwBs5XgRXURTeKCTv-QuDzeBonso,43
|
|
29
|
+
fromscratch_pebble-0.3.0.dist-info/licenses/LICENSE,sha256=NtHM2ak--oE71VCcY50kcCoC1cKS0LRUsxq_xA89RNc,1064
|
|
30
|
+
fromscratch_pebble-0.3.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Rob Cox
|
|
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.
|
pebble/__init__.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""Pebble - An educational compiler and bytecode VM.
|
|
2
|
+
|
|
3
|
+
The names below are Pebble's public API: the ones promised to stay the same
|
|
4
|
+
between versions. See :mod:`pebble.api`.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from pebble._pipeline import Program
|
|
8
|
+
from pebble.api import check, compile_source, format_source, run_source
|
|
9
|
+
from pebble.builtins import Value
|
|
10
|
+
from pebble.errors import PebbleError
|
|
11
|
+
from pebble.stdlib import StdlibModule
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"PebbleError",
|
|
15
|
+
"Program",
|
|
16
|
+
"StdlibModule",
|
|
17
|
+
"Value",
|
|
18
|
+
"check",
|
|
19
|
+
"compile_source",
|
|
20
|
+
"format_source",
|
|
21
|
+
"run_source",
|
|
22
|
+
]
|
pebble/__main__.py
ADDED
pebble/_pipeline.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"""The one place Pebble's pieces are wired together into a runnable program.
|
|
2
|
+
|
|
3
|
+
Private: use :mod:`pebble.api` instead. The ``pebble`` command and the
|
|
4
|
+
public API both build programs here, so the wiring is written once.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import io
|
|
8
|
+
from collections.abc import Mapping
|
|
9
|
+
from dataclasses import dataclass
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import TextIO, override
|
|
12
|
+
|
|
13
|
+
from pebble.analyzer import SemanticAnalyzer
|
|
14
|
+
from pebble.builtins import Value
|
|
15
|
+
from pebble.bytecode import CompiledProgram
|
|
16
|
+
from pebble.compiler import Compiler
|
|
17
|
+
from pebble.lexer import Lexer
|
|
18
|
+
from pebble.optimizer import optimize
|
|
19
|
+
from pebble.parser import Parser
|
|
20
|
+
from pebble.resolver import ModuleResolver
|
|
21
|
+
from pebble.stdlib import StdlibHandler, StdlibModule
|
|
22
|
+
from pebble.type_checker import type_check
|
|
23
|
+
from pebble.vm import DebugHook, VirtualMachine
|
|
24
|
+
|
|
25
|
+
type StdlibHandlers = dict[str, tuple[int | tuple[int, ...], StdlibHandler]]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class _Capture(io.StringIO):
|
|
29
|
+
"""Collect everything a program prints, copying it to *echo* as it goes."""
|
|
30
|
+
|
|
31
|
+
def __init__(self, echo: TextIO | None) -> None:
|
|
32
|
+
super().__init__()
|
|
33
|
+
self._echo = echo
|
|
34
|
+
|
|
35
|
+
@override
|
|
36
|
+
def write(self, s: str, /) -> int:
|
|
37
|
+
if self._echo is not None:
|
|
38
|
+
self._echo.write(s)
|
|
39
|
+
return super().write(s)
|
|
40
|
+
|
|
41
|
+
@override
|
|
42
|
+
def flush(self) -> None:
|
|
43
|
+
if self._echo is not None:
|
|
44
|
+
self._echo.flush()
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@dataclass(frozen=True)
|
|
48
|
+
class Program:
|
|
49
|
+
"""A checked, compiled Pebble program, ready to run as many times as you like.
|
|
50
|
+
|
|
51
|
+
Make one with :func:`pebble.compile_source`. It remembers its own
|
|
52
|
+
modules, so running it never looks anything up in a shared table.
|
|
53
|
+
Its attributes are Pebble internals and may change between versions;
|
|
54
|
+
:meth:`run` is the promised way to use it.
|
|
55
|
+
"""
|
|
56
|
+
|
|
57
|
+
compiled: CompiledProgram
|
|
58
|
+
stdlib_handlers: StdlibHandlers
|
|
59
|
+
stdlib_constants: dict[str, Value]
|
|
60
|
+
|
|
61
|
+
def run(self, output: TextIO | None = None) -> str:
|
|
62
|
+
"""Run the program and return everything it printed.
|
|
63
|
+
|
|
64
|
+
Args:
|
|
65
|
+
output: Also write the printed text here as it happens
|
|
66
|
+
(for example ``sys.stdout``). Optional.
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
Everything the program printed.
|
|
70
|
+
|
|
71
|
+
Raises:
|
|
72
|
+
PebbleRuntimeError: If the program fails while running.
|
|
73
|
+
|
|
74
|
+
"""
|
|
75
|
+
return run_program(self, output)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def run_program(
|
|
79
|
+
program: Program, output: TextIO | None, debug_hook: DebugHook | None = None
|
|
80
|
+
) -> str:
|
|
81
|
+
"""Run *program*, optionally with a debugger attached (used by ``pebble --debug``)."""
|
|
82
|
+
capture = _Capture(output)
|
|
83
|
+
VirtualMachine(output=capture).run(
|
|
84
|
+
program.compiled,
|
|
85
|
+
stdlib_handlers=program.stdlib_handlers,
|
|
86
|
+
stdlib_constants=program.stdlib_constants,
|
|
87
|
+
debug_hook=debug_hook,
|
|
88
|
+
)
|
|
89
|
+
return capture.getvalue()
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def compile_program(source: str, *, modules: Mapping[str, StdlibModule], base_dir: Path) -> Program:
|
|
93
|
+
"""Lex, parse, analyze, type-check, compile and optimize *source*.
|
|
94
|
+
|
|
95
|
+
Args:
|
|
96
|
+
source: The Pebble program.
|
|
97
|
+
modules: Every importable module by name (built-ins included).
|
|
98
|
+
base_dir: Where ``import "file.pbl"`` looks for files.
|
|
99
|
+
|
|
100
|
+
Returns:
|
|
101
|
+
The runnable program.
|
|
102
|
+
|
|
103
|
+
"""
|
|
104
|
+
program = Parser(Lexer(source).tokenize()).parse()
|
|
105
|
+
analyzer = SemanticAnalyzer()
|
|
106
|
+
resolver = ModuleResolver(base_dir=base_dir, modules=modules)
|
|
107
|
+
resolver.resolve_imports(program, analyzer)
|
|
108
|
+
analyzed = analyzer.analyze(program)
|
|
109
|
+
type_check(analyzed, analyzer=analyzer)
|
|
110
|
+
compiled = Compiler(
|
|
111
|
+
cell_vars=analyzer.cell_vars,
|
|
112
|
+
free_vars=analyzer.free_vars,
|
|
113
|
+
enums=resolver.merged_enums,
|
|
114
|
+
class_parents=resolver.merged_class_parents,
|
|
115
|
+
structs=resolver.merged_structs,
|
|
116
|
+
class_methods=resolver.merged_class_methods,
|
|
117
|
+
functions=resolver.merged_functions,
|
|
118
|
+
variable_arity_functions=resolver.variable_arity_functions,
|
|
119
|
+
).compile(analyzed)
|
|
120
|
+
compiled = optimize(compiled)
|
|
121
|
+
full_program = CompiledProgram(
|
|
122
|
+
main=compiled.main,
|
|
123
|
+
functions={**resolver.merged_functions, **compiled.functions},
|
|
124
|
+
structs={**resolver.merged_structs, **compiled.structs},
|
|
125
|
+
struct_field_types={
|
|
126
|
+
**resolver.merged_struct_field_types,
|
|
127
|
+
**compiled.struct_field_types,
|
|
128
|
+
},
|
|
129
|
+
class_methods={**resolver.merged_class_methods, **compiled.class_methods},
|
|
130
|
+
enums={**resolver.merged_enums, **compiled.enums},
|
|
131
|
+
class_parents={**resolver.merged_class_parents, **compiled.class_parents},
|
|
132
|
+
)
|
|
133
|
+
return Program(
|
|
134
|
+
full_program,
|
|
135
|
+
dict(resolver.merged_stdlib_handlers),
|
|
136
|
+
dict(resolver.merged_stdlib_constants),
|
|
137
|
+
)
|
pebble/_version.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# file generated by vcs-versioning
|
|
2
|
+
# don't change, don't track in version control
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
__all__ = [
|
|
6
|
+
"__version__",
|
|
7
|
+
"__version_tuple__",
|
|
8
|
+
"version",
|
|
9
|
+
"version_tuple",
|
|
10
|
+
"__commit_id__",
|
|
11
|
+
"commit_id",
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
version: str
|
|
15
|
+
__version__: str
|
|
16
|
+
__version_tuple__: tuple[int | str, ...]
|
|
17
|
+
version_tuple: tuple[int | str, ...]
|
|
18
|
+
commit_id: str | None
|
|
19
|
+
__commit_id__: str | None
|
|
20
|
+
|
|
21
|
+
__version__ = version = '0.3.0'
|
|
22
|
+
__version_tuple__ = version_tuple = (0, 3, 0)
|
|
23
|
+
|
|
24
|
+
__commit_id__ = commit_id = None
|