p2w 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.
Files changed (70) hide show
  1. p2w-0.1.0/PKG-INFO +189 -0
  2. p2w-0.1.0/README.md +178 -0
  3. p2w-0.1.0/pyproject.toml +46 -0
  4. p2w-0.1.0/src/p2w/__init__.py +24 -0
  5. p2w-0.1.0/src/p2w/browser_runner.py +368 -0
  6. p2w-0.1.0/src/p2w/compiler/__init__.py +18 -0
  7. p2w-0.1.0/src/p2w/compiler/analysis.py +1087 -0
  8. p2w-0.1.0/src/p2w/compiler/builtins.py +119 -0
  9. p2w-0.1.0/src/p2w/compiler/codegen/__init__.py +14 -0
  10. p2w-0.1.0/src/p2w/compiler/codegen/attributes.py +95 -0
  11. p2w-0.1.0/src/p2w/compiler/codegen/calls.py +1060 -0
  12. p2w-0.1.0/src/p2w/compiler/codegen/classes.py +357 -0
  13. p2w-0.1.0/src/p2w/compiler/codegen/closures.py +86 -0
  14. p2w-0.1.0/src/p2w/compiler/codegen/collections.py +97 -0
  15. p2w-0.1.0/src/p2w/compiler/codegen/comprehensions.py +674 -0
  16. p2w-0.1.0/src/p2w/compiler/codegen/context_managers.py +185 -0
  17. p2w-0.1.0/src/p2w/compiler/codegen/control.py +852 -0
  18. p2w-0.1.0/src/p2w/compiler/codegen/exceptions.py +318 -0
  19. p2w-0.1.0/src/p2w/compiler/codegen/expr_handlers.py +1461 -0
  20. p2w-0.1.0/src/p2w/compiler/codegen/expressions.py +23 -0
  21. p2w-0.1.0/src/p2w/compiler/codegen/fstrings.py +37 -0
  22. p2w-0.1.0/src/p2w/compiler/codegen/functions.py +689 -0
  23. p2w-0.1.0/src/p2w/compiler/codegen/generators.py +1227 -0
  24. p2w-0.1.0/src/p2w/compiler/codegen/js_interop.py +170 -0
  25. p2w-0.1.0/src/p2w/compiler/codegen/lambdas.py +120 -0
  26. p2w-0.1.0/src/p2w/compiler/codegen/operators.py +339 -0
  27. p2w-0.1.0/src/p2w/compiler/codegen/pattern_matching.py +309 -0
  28. p2w-0.1.0/src/p2w/compiler/codegen/statements.py +23 -0
  29. p2w-0.1.0/src/p2w/compiler/codegen/stmt_handlers.py +378 -0
  30. p2w-0.1.0/src/p2w/compiler/codegen/subscript.py +769 -0
  31. p2w-0.1.0/src/p2w/compiler/codegen/unpacking.py +157 -0
  32. p2w-0.1.0/src/p2w/compiler/codegen/variables.py +432 -0
  33. p2w-0.1.0/src/p2w/compiler/compiler.py +609 -0
  34. p2w-0.1.0/src/p2w/compiler/context.py +267 -0
  35. p2w-0.1.0/src/p2w/compiler/inference.py +859 -0
  36. p2w-0.1.0/src/p2w/compiler/inlining.py +563 -0
  37. p2w-0.1.0/src/p2w/compiler/types.py +284 -0
  38. p2w-0.1.0/src/p2w/emitter.py +399 -0
  39. p2w-0.1.0/src/p2w/runner.py +240 -0
  40. p2w-0.1.0/src/p2w/testing.py +69 -0
  41. p2w-0.1.0/src/p2w/wat/__init__.py +20 -0
  42. p2w-0.1.0/src/p2w/wat/builtins/__init__.py +159 -0
  43. p2w-0.1.0/src/p2w/wat/builtins/constructors.py +515 -0
  44. p2w-0.1.0/src/p2w/wat/builtins/direct.py +217 -0
  45. p2w-0.1.0/src/p2w/wat/builtins/introspection.py +593 -0
  46. p2w-0.1.0/src/p2w/wat/builtins/io.py +594 -0
  47. p2w-0.1.0/src/p2w/wat/builtins/js_interop.py +92 -0
  48. p2w-0.1.0/src/p2w/wat/builtins/math.py +282 -0
  49. p2w-0.1.0/src/p2w/wat/builtins/sequences.py +1187 -0
  50. p2w-0.1.0/src/p2w/wat/builtins/strings.py +352 -0
  51. p2w-0.1.0/src/p2w/wat/builtins/super.py +82 -0
  52. p2w-0.1.0/src/p2w/wat/helpers/__init__.py +49 -0
  53. p2w-0.1.0/src/p2w/wat/helpers/arithmetic.py +411 -0
  54. p2w-0.1.0/src/p2w/wat/helpers/bytes_ops.py +190 -0
  55. p2w-0.1.0/src/p2w/wat/helpers/comparisons.py +816 -0
  56. p2w-0.1.0/src/p2w/wat/helpers/containers.py +294 -0
  57. p2w-0.1.0/src/p2w/wat/helpers/core.py +207 -0
  58. p2w-0.1.0/src/p2w/wat/helpers/dicts.py +1188 -0
  59. p2w-0.1.0/src/p2w/wat/helpers/exceptions.py +315 -0
  60. p2w-0.1.0/src/p2w/wat/helpers/generators.py +263 -0
  61. p2w-0.1.0/src/p2w/wat/helpers/integers.py +201 -0
  62. p2w-0.1.0/src/p2w/wat/helpers/js_interop.py +467 -0
  63. p2w-0.1.0/src/p2w/wat/helpers/lists.py +3137 -0
  64. p2w-0.1.0/src/p2w/wat/helpers/objects.py +903 -0
  65. p2w-0.1.0/src/p2w/wat/helpers/sets.py +131 -0
  66. p2w-0.1.0/src/p2w/wat/helpers/sorting.py +16 -0
  67. p2w-0.1.0/src/p2w/wat/helpers/strings.py +3485 -0
  68. p2w-0.1.0/src/p2w/wat/helpers/tuples.py +360 -0
  69. p2w-0.1.0/src/p2w/wat/imports.py +86 -0
  70. p2w-0.1.0/src/p2w/wat/types.py +222 -0
p2w-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,189 @@
1
+ Metadata-Version: 2.3
2
+ Name: p2w
3
+ Version: 0.1.0
4
+ Summary: Add your description here
5
+ Author: Stefane Fermigier
6
+ Author-email: Stefane Fermigier <sf@abilian.com>
7
+ Requires-Python: >=3.12
8
+ Project-URL: Homepage, https://git.sr.ht/~sfermigier/p2w
9
+ Project-URL: Repository, https://git.sr.ht/~sfermigier/p2w
10
+ Description-Content-Type: text/markdown
11
+
12
+ # p2w - Python to WebAssembly Compiler
13
+
14
+ **p2w** compiles a substantial subset of Python to WebAssembly, leveraging WASM GC for automatic memory management.
15
+
16
+ ## Features
17
+
18
+ ### Supported Python Features
19
+
20
+ - **Data types**: integers (arbitrary precision), floats, booleans, strings, bytes, lists, tuples, dicts, sets
21
+ - **Control flow**: if/elif/else, for/while loops, break/continue, match statements
22
+ - **Functions**: definitions, default arguments, *args/**kwargs, closures, lambdas, decorators
23
+ - **Classes**: inheritance, properties, static/class methods, special methods (`__init__`, `__str__`, etc.)
24
+ - **Comprehensions**: list, dict, set, generator expressions
25
+ - **Exception handling**: try/except/finally, raise, exception chaining
26
+ - **Context managers**: `with` statement
27
+ - **Generators**: yield, generator functions
28
+ - **Other**: f-strings, type annotations (ignored at runtime), walrus operator, unpacking
29
+
30
+ ### JavaScript Interop
31
+
32
+ p2w provides seamless JavaScript interoperability via the `js` module:
33
+
34
+ ```python
35
+ import js
36
+
37
+ canvas = js.document.getElementById("chart")
38
+ ctx = canvas.getContext("2d")
39
+ ctx.fillRect(0, 0, 100, 100)
40
+ js.console.log("Hello from Python!")
41
+ ```
42
+
43
+ ## What Works
44
+
45
+ ### Browser Demos
46
+
47
+ The `demos/` directory contains two (somewhat) working browser demos:
48
+
49
+ - **data-dashboard**: Bar chart visualization using Canvas API
50
+ - **simulation**: Physics simulation with real-time rendering
51
+
52
+ ### Golden Programs
53
+
54
+ The `programs/internal/` directory contains **104 test programs** covering the supported Python subset. These serve as both regression tests and documentation of working features, including:
55
+
56
+ - Classes with inheritance, properties, and special methods
57
+ - Generators and comprehensions
58
+ - Exception handling with chaining
59
+ - Context managers
60
+ - Match statements
61
+ - F-strings and string operations
62
+ - Collection types and methods
63
+
64
+ ### Benchmarks
65
+
66
+ Two benchmark suites validate correctness and measure performance:
67
+
68
+ **`programs/benchmarks/`** - Classic benchmarks adapted for p2w:
69
+ - fibonacci, primes, sieve, matmul
70
+ - fannkuch, binarytrees, nbody
71
+ - mandelbrot, spectralnorm, fasta
72
+ - pystone
73
+
74
+ **`programs/benchmarks-alioth/`** - Benchmarks from the [Debian Benchmark Game](https://benchmarksgame-team.pages.debian.net/benchmarksgame/) with GCC baseline comparison:
75
+ - binarytrees, nbody, spectralnorm, mandelbrot, fannkuchredux
76
+ - Includes a runner script for automated comparison against GCC (`-O3 -ffast-math`)
77
+
78
+ ## Installation
79
+
80
+ ```bash
81
+ # Clone the repository
82
+ git clone https://git.sr.ht/~sfermigier/p2w
83
+ cd p2w
84
+
85
+ # Install with uv
86
+ uv sync
87
+ ```
88
+
89
+ ## Usage
90
+
91
+ ### Command Line
92
+
93
+ ```bash
94
+ # Compile Python to WAT
95
+ uv run p2w source.py > output.wat
96
+
97
+ # Convert WAT to WASM (requires wabt)
98
+ wat2wasm output.wat -o output.wasm
99
+ ```
100
+
101
+ ### As a Library
102
+
103
+ ```python
104
+ from p2w import compile_to_wat
105
+
106
+ source = """
107
+ def fib(n):
108
+ a, b = 0, 1
109
+ for _ in range(n):
110
+ a, b = b, a + b
111
+ return a
112
+
113
+ print(fib(10))
114
+ """
115
+
116
+ wat_code = compile_to_wat(source)
117
+ ```
118
+
119
+ ## Examples
120
+
121
+ ### Fibonacci
122
+
123
+ ```python
124
+ def fib(n: int) -> int:
125
+ a, b = 0, 1
126
+ for i in range(n):
127
+ a, b = b, a + b
128
+ return a
129
+
130
+ print(f"fib(30) = {fib(30)}")
131
+ ```
132
+
133
+ ### Browser Demo
134
+
135
+ The `demos/` directory contains browser examples:
136
+
137
+ - **data-dashboard**: Interactive bar chart visualization
138
+ - **simulation**: Physics simulation
139
+
140
+ To run a demo:
141
+
142
+ ```bash
143
+ cd demos/data-dashboard
144
+ make # Builds app.wasm
145
+ # Serve with any HTTP server and open index.html
146
+ ```
147
+
148
+ ## Development
149
+
150
+ ```bash
151
+ # Run tests
152
+ make test
153
+
154
+ # Run linting and type checking
155
+ make lint
156
+
157
+ # Format code
158
+ make format
159
+
160
+ # Run tests with coverage
161
+ make test-cov
162
+ ```
163
+
164
+ ### Test Structure
165
+
166
+ - `tests/a_unit/` - Unit tests
167
+ - `tests/b_integration/` - Integration tests
168
+ - `tests/c_e2e/` - End-to-end tests
169
+
170
+ ## Architecture
171
+
172
+ p2w follows a straightforward compilation pipeline:
173
+
174
+ 1. **Parse**: Python source -> AST (using Python's `ast` module)
175
+ 2. **Analyze**: Scope analysis, type inference
176
+ 3. **Compile**: AST -> WAT (WebAssembly Text format)
177
+ 4. **Assemble**: WAT -> WASM (via external tools like `wat2wasm`)
178
+
179
+ The compiler generates WAT code that uses WASM 3.0 GC features for automatic memory management of Python objects.
180
+
181
+ ## Requirements
182
+
183
+ - Python 3.12+
184
+ - [wabt](https://github.com/WebAssembly/wabt) (for `wat2wasm`)
185
+ - A WASM runtime with GC support (e.g., recent Chrome/Firefox, wasmtime with GC enabled)
186
+
187
+ ## License
188
+
189
+ MIT
p2w-0.1.0/README.md ADDED
@@ -0,0 +1,178 @@
1
+ # p2w - Python to WebAssembly Compiler
2
+
3
+ **p2w** compiles a substantial subset of Python to WebAssembly, leveraging WASM GC for automatic memory management.
4
+
5
+ ## Features
6
+
7
+ ### Supported Python Features
8
+
9
+ - **Data types**: integers (arbitrary precision), floats, booleans, strings, bytes, lists, tuples, dicts, sets
10
+ - **Control flow**: if/elif/else, for/while loops, break/continue, match statements
11
+ - **Functions**: definitions, default arguments, *args/**kwargs, closures, lambdas, decorators
12
+ - **Classes**: inheritance, properties, static/class methods, special methods (`__init__`, `__str__`, etc.)
13
+ - **Comprehensions**: list, dict, set, generator expressions
14
+ - **Exception handling**: try/except/finally, raise, exception chaining
15
+ - **Context managers**: `with` statement
16
+ - **Generators**: yield, generator functions
17
+ - **Other**: f-strings, type annotations (ignored at runtime), walrus operator, unpacking
18
+
19
+ ### JavaScript Interop
20
+
21
+ p2w provides seamless JavaScript interoperability via the `js` module:
22
+
23
+ ```python
24
+ import js
25
+
26
+ canvas = js.document.getElementById("chart")
27
+ ctx = canvas.getContext("2d")
28
+ ctx.fillRect(0, 0, 100, 100)
29
+ js.console.log("Hello from Python!")
30
+ ```
31
+
32
+ ## What Works
33
+
34
+ ### Browser Demos
35
+
36
+ The `demos/` directory contains two (somewhat) working browser demos:
37
+
38
+ - **data-dashboard**: Bar chart visualization using Canvas API
39
+ - **simulation**: Physics simulation with real-time rendering
40
+
41
+ ### Golden Programs
42
+
43
+ The `programs/internal/` directory contains **104 test programs** covering the supported Python subset. These serve as both regression tests and documentation of working features, including:
44
+
45
+ - Classes with inheritance, properties, and special methods
46
+ - Generators and comprehensions
47
+ - Exception handling with chaining
48
+ - Context managers
49
+ - Match statements
50
+ - F-strings and string operations
51
+ - Collection types and methods
52
+
53
+ ### Benchmarks
54
+
55
+ Two benchmark suites validate correctness and measure performance:
56
+
57
+ **`programs/benchmarks/`** - Classic benchmarks adapted for p2w:
58
+ - fibonacci, primes, sieve, matmul
59
+ - fannkuch, binarytrees, nbody
60
+ - mandelbrot, spectralnorm, fasta
61
+ - pystone
62
+
63
+ **`programs/benchmarks-alioth/`** - Benchmarks from the [Debian Benchmark Game](https://benchmarksgame-team.pages.debian.net/benchmarksgame/) with GCC baseline comparison:
64
+ - binarytrees, nbody, spectralnorm, mandelbrot, fannkuchredux
65
+ - Includes a runner script for automated comparison against GCC (`-O3 -ffast-math`)
66
+
67
+ ## Installation
68
+
69
+ ```bash
70
+ # Clone the repository
71
+ git clone https://git.sr.ht/~sfermigier/p2w
72
+ cd p2w
73
+
74
+ # Install with uv
75
+ uv sync
76
+ ```
77
+
78
+ ## Usage
79
+
80
+ ### Command Line
81
+
82
+ ```bash
83
+ # Compile Python to WAT
84
+ uv run p2w source.py > output.wat
85
+
86
+ # Convert WAT to WASM (requires wabt)
87
+ wat2wasm output.wat -o output.wasm
88
+ ```
89
+
90
+ ### As a Library
91
+
92
+ ```python
93
+ from p2w import compile_to_wat
94
+
95
+ source = """
96
+ def fib(n):
97
+ a, b = 0, 1
98
+ for _ in range(n):
99
+ a, b = b, a + b
100
+ return a
101
+
102
+ print(fib(10))
103
+ """
104
+
105
+ wat_code = compile_to_wat(source)
106
+ ```
107
+
108
+ ## Examples
109
+
110
+ ### Fibonacci
111
+
112
+ ```python
113
+ def fib(n: int) -> int:
114
+ a, b = 0, 1
115
+ for i in range(n):
116
+ a, b = b, a + b
117
+ return a
118
+
119
+ print(f"fib(30) = {fib(30)}")
120
+ ```
121
+
122
+ ### Browser Demo
123
+
124
+ The `demos/` directory contains browser examples:
125
+
126
+ - **data-dashboard**: Interactive bar chart visualization
127
+ - **simulation**: Physics simulation
128
+
129
+ To run a demo:
130
+
131
+ ```bash
132
+ cd demos/data-dashboard
133
+ make # Builds app.wasm
134
+ # Serve with any HTTP server and open index.html
135
+ ```
136
+
137
+ ## Development
138
+
139
+ ```bash
140
+ # Run tests
141
+ make test
142
+
143
+ # Run linting and type checking
144
+ make lint
145
+
146
+ # Format code
147
+ make format
148
+
149
+ # Run tests with coverage
150
+ make test-cov
151
+ ```
152
+
153
+ ### Test Structure
154
+
155
+ - `tests/a_unit/` - Unit tests
156
+ - `tests/b_integration/` - Integration tests
157
+ - `tests/c_e2e/` - End-to-end tests
158
+
159
+ ## Architecture
160
+
161
+ p2w follows a straightforward compilation pipeline:
162
+
163
+ 1. **Parse**: Python source -> AST (using Python's `ast` module)
164
+ 2. **Analyze**: Scope analysis, type inference
165
+ 3. **Compile**: AST -> WAT (WebAssembly Text format)
166
+ 4. **Assemble**: WAT -> WASM (via external tools like `wat2wasm`)
167
+
168
+ The compiler generates WAT code that uses WASM 3.0 GC features for automatic memory management of Python objects.
169
+
170
+ ## Requirements
171
+
172
+ - Python 3.12+
173
+ - [wabt](https://github.com/WebAssembly/wabt) (for `wat2wasm`)
174
+ - A WASM runtime with GC support (e.g., recent Chrome/Firefox, wasmtime with GC enabled)
175
+
176
+ ## License
177
+
178
+ MIT
@@ -0,0 +1,46 @@
1
+ [project]
2
+ name = "p2w"
3
+ version = "0.1.0"
4
+ description = "Add your description here"
5
+ readme = "README.md"
6
+ authors = [
7
+ { name = "Stefane Fermigier", email = "sf@abilian.com" }
8
+ ]
9
+ requires-python = ">=3.12"
10
+ dependencies = []
11
+
12
+ [project.urls]
13
+ Homepage = "https://git.sr.ht/~sfermigier/p2w"
14
+ Repository = "https://git.sr.ht/~sfermigier/p2w"
15
+
16
+ [project.scripts]
17
+ p2w = "p2w:main"
18
+
19
+ [build-system]
20
+ requires = ["uv_build>=0.9.26,<0.10.0"]
21
+ build-backend = "uv_build"
22
+
23
+ [dependency-groups]
24
+ dev = [
25
+ # Testing
26
+ "pytest>=9.0.2",
27
+ "pytest-cov>=7.0.0",
28
+ "nox>=2025.11.12",
29
+ # Linting / type checking
30
+ "mypy>=1.19.1",
31
+ # "pyrefly>=0.49.0",
32
+ "ty>=0.0.13",
33
+ "ruff>=0.14.14",
34
+ "pre-commit>=4.5.1",
35
+ ]
36
+
37
+ [tool.pytest.ini_options]
38
+ markers = [
39
+ "unit: Unit tests (fast, isolated)",
40
+ "integration: Integration tests (component interactions)",
41
+ "e2e: End-to-end tests (full workflows)",
42
+ ]
43
+ testpaths = [
44
+ "tests",
45
+ "src",
46
+ ]
@@ -0,0 +1,24 @@
1
+ """p2w: Python subset to WebAssembly compiler."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import pathlib
6
+ import sys
7
+
8
+ from p2w.compiler import compile_to_wat
9
+
10
+
11
+ def main() -> None:
12
+ """Entry point for p2w CLI."""
13
+ if len(sys.argv) < 2:
14
+ print("Usage: p2w <source.py>")
15
+ sys.exit(1)
16
+
17
+ source_file = sys.argv[1]
18
+ source = pathlib.Path(source_file).read_text()
19
+
20
+ wat_code = compile_to_wat(source)
21
+ print(wat_code)
22
+
23
+
24
+ __all__ = ["compile_to_wat", "main"]