typesht 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.
- typesht-0.1.0/LICENSE +21 -0
- typesht-0.1.0/PKG-INFO +372 -0
- typesht-0.1.0/README.md +343 -0
- typesht-0.1.0/pyproject.toml +52 -0
- typesht-0.1.0/setup.cfg +4 -0
- typesht-0.1.0/src/typesht/__init__.py +2 -0
- typesht-0.1.0/src/typesht/codegen.py +507 -0
- typesht-0.1.0/src/typesht/lexer.py +495 -0
- typesht-0.1.0/src/typesht/main.py +878 -0
- typesht-0.1.0/src/typesht/parser.py +878 -0
- typesht-0.1.0/src/typesht/test.py +15 -0
- typesht-0.1.0/src/typesht.egg-info/PKG-INFO +372 -0
- typesht-0.1.0/src/typesht.egg-info/SOURCES.txt +16 -0
- typesht-0.1.0/src/typesht.egg-info/dependency_links.txt +1 -0
- typesht-0.1.0/src/typesht.egg-info/entry_points.txt +2 -0
- typesht-0.1.0/src/typesht.egg-info/requires.txt +5 -0
- typesht-0.1.0/src/typesht.egg-info/top_level.txt +1 -0
- typesht-0.1.0/tests/test_compiler.py +814 -0
typesht-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jeiel K Libert
|
|
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.
|
typesht-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: typesht
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python-syntax to TypeScript/JavaScript compiler
|
|
5
|
+
Author: Jeiel K Libert
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/jlibert/typesht
|
|
8
|
+
Project-URL: Repository, https://github.com/jlibert/typesht
|
|
9
|
+
Project-URL: Bug Tracker, https://github.com/jlibert/typesht/issues
|
|
10
|
+
Keywords: compiler,transpiler,python,typescript,javascript,type-safe,python-to-javascript
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Topic :: Software Development :: Compilers
|
|
20
|
+
Classifier: Topic :: Software Development :: Code Generators
|
|
21
|
+
Requires-Python: >=3.10
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Provides-Extra: dev
|
|
25
|
+
Requires-Dist: coverage; extra == "dev"
|
|
26
|
+
Requires-Dist: pytest; extra == "dev"
|
|
27
|
+
Requires-Dist: pytest-cov; extra == "dev"
|
|
28
|
+
Dynamic: license-file
|
|
29
|
+
|
|
30
|
+
# TypeSht
|
|
31
|
+
|
|
32
|
+
A statically-typed programming language that uses Python-like syntax and compiles to TypeScript/JavaScript. Designed for Python developers who want to write type-safe code that runs in JavaScript environments (browsers, Node.js) without learning TypeScript or JavaScript syntax.
|
|
33
|
+
|
|
34
|
+
> **If it works in Python, it works the same in TypeSht. No surprises.**
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
## Why TypeSht?
|
|
39
|
+
|
|
40
|
+
Python developers often need to target JavaScript environments — web frontends, Node.js servers, serverless functions. The usual options are:
|
|
41
|
+
- Learn JavaScript/TypeScript (steep learning curve)
|
|
42
|
+
- Use a transpiler that loses Python semantics (silent bugs)
|
|
43
|
+
|
|
44
|
+
TypeSht takes a different approach: compile Python-syntax source files to TypeScript, and ship a runtime package (`typesht-runtime`) that preserves Python behavior faithfully in JavaScript environments. Floor division, negative indexing, Python-style dicts, `print()` with `sep`/`end` — it all works exactly as you'd expect.
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## Requirements
|
|
49
|
+
|
|
50
|
+
**Compiler:**
|
|
51
|
+
- Python 3.10+
|
|
52
|
+
|
|
53
|
+
**Runtime & output:**
|
|
54
|
+
- Node.js 16+
|
|
55
|
+
- TypeScript (`npm install -g typescript`)
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
## Installation
|
|
60
|
+
|
|
61
|
+
**1. Install the TypeSht compiler (Python):**
|
|
62
|
+
```bash
|
|
63
|
+
pip install typesht
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
**2. Install the TypeSht runtime (Node.js):**
|
|
67
|
+
```bash
|
|
68
|
+
npm install typesht-runtime
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
## Usage
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
# Compile to JavaScript (default)
|
|
77
|
+
typesht compile myprogram.tsht
|
|
78
|
+
|
|
79
|
+
# Compile to TypeScript only
|
|
80
|
+
typesht compile myprogram.tsht --target ts
|
|
81
|
+
|
|
82
|
+
# Compile to both JS and TS
|
|
83
|
+
typesht compile myprogram.tsht --target both
|
|
84
|
+
|
|
85
|
+
# Specify output directory
|
|
86
|
+
typesht compile myprogram.tsht --output dist/
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Output goes to an `output/` folder next to your source file by default.
|
|
90
|
+
|
|
91
|
+
To run the compiled output:
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
cd output/
|
|
95
|
+
npm init -y
|
|
96
|
+
# add "type": "module" to package.json
|
|
97
|
+
npm install typesht-runtime
|
|
98
|
+
node myprogram.js
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
## Language Features
|
|
104
|
+
|
|
105
|
+
### Variables & Type Annotations
|
|
106
|
+
|
|
107
|
+
```python
|
|
108
|
+
name: str = "Alice"
|
|
109
|
+
age: int = 30
|
|
110
|
+
score: float = 9.5
|
|
111
|
+
is_active: bool = True
|
|
112
|
+
x = 42 # untyped
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### F-Strings
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
msg: str = f"Hello, {name}! You are {age} years old."
|
|
119
|
+
result: str = f"Next year: {age + 1}"
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Functions
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
def greet(name: str, age: int) -> str:
|
|
126
|
+
return f"Hi, I am {name}"
|
|
127
|
+
|
|
128
|
+
def add(a: int, b: int) -> int:
|
|
129
|
+
return a + b
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### Lambda Functions
|
|
133
|
+
|
|
134
|
+
```python
|
|
135
|
+
double = lambda x: x * 2
|
|
136
|
+
add = lambda x, y: x + y
|
|
137
|
+
no_args = lambda: 42
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Classes
|
|
141
|
+
|
|
142
|
+
```python
|
|
143
|
+
class Person:
|
|
144
|
+
name: str
|
|
145
|
+
age: int
|
|
146
|
+
|
|
147
|
+
def __init__(self, name: str, age: int):
|
|
148
|
+
self.name = name
|
|
149
|
+
self.age = age
|
|
150
|
+
|
|
151
|
+
def greet(self) -> str:
|
|
152
|
+
return f"Hi, I am {self.name}"
|
|
153
|
+
|
|
154
|
+
person = Person("Alice", 30)
|
|
155
|
+
print(person.greet())
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### Decorators
|
|
159
|
+
|
|
160
|
+
```python
|
|
161
|
+
class MathHelper:
|
|
162
|
+
@staticmethod
|
|
163
|
+
def add(x: int, y: int) -> int:
|
|
164
|
+
return x + y
|
|
165
|
+
|
|
166
|
+
@classmethod
|
|
167
|
+
def create(cls, base: int) -> int:
|
|
168
|
+
return base
|
|
169
|
+
|
|
170
|
+
@property
|
|
171
|
+
def value(self) -> int:
|
|
172
|
+
return self._value
|
|
173
|
+
|
|
174
|
+
# Custom decorators compile to higher-order function calls
|
|
175
|
+
@my_decorator
|
|
176
|
+
def process(data: str) -> str:
|
|
177
|
+
return data
|
|
178
|
+
# → process = myDecorator(process)
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### Generics
|
|
182
|
+
|
|
183
|
+
```python
|
|
184
|
+
numbers: list[int] = [1, 2, 3]
|
|
185
|
+
words: list[str] = ["hello", "world"]
|
|
186
|
+
data: dict[str, int] = {"a": 1}
|
|
187
|
+
|
|
188
|
+
def get_first(items: list[int]) -> int:
|
|
189
|
+
return items[0]
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### Lists & Dicts
|
|
193
|
+
|
|
194
|
+
```python
|
|
195
|
+
items: list = [10, 20, 30, 40, 50]
|
|
196
|
+
print(items[0]) # → 10
|
|
197
|
+
print(items[-1]) # → 50 (negative indexing)
|
|
198
|
+
print(items[1:3]) # → [20, 30] (slicing)
|
|
199
|
+
|
|
200
|
+
data: dict = {"lang": "TypeSht", "version": "0.1"}
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### Control Flow
|
|
204
|
+
|
|
205
|
+
```python
|
|
206
|
+
if age > 18:
|
|
207
|
+
print("Adult")
|
|
208
|
+
elif age == 18:
|
|
209
|
+
print("Just turned 18")
|
|
210
|
+
else:
|
|
211
|
+
print("Minor")
|
|
212
|
+
|
|
213
|
+
for i in range(10):
|
|
214
|
+
print(i)
|
|
215
|
+
|
|
216
|
+
for item in items:
|
|
217
|
+
print(item)
|
|
218
|
+
|
|
219
|
+
for k, v in pairs:
|
|
220
|
+
print(f"{k} = {v}")
|
|
221
|
+
|
|
222
|
+
while x > 0:
|
|
223
|
+
x -= 1
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
### Operators
|
|
227
|
+
|
|
228
|
+
```python
|
|
229
|
+
result = 10 // 3 # floor division → 3
|
|
230
|
+
remainder = 10 % 3 # modulo → 1
|
|
231
|
+
power = 2 ** 8 # power → 256
|
|
232
|
+
x = a and b # logical and
|
|
233
|
+
y = a or b # logical or
|
|
234
|
+
z = not a # logical not
|
|
235
|
+
found = item in items # membership test
|
|
236
|
+
missing = item not in items
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### Triple-Quoted Strings
|
|
240
|
+
|
|
241
|
+
```python
|
|
242
|
+
msg = """
|
|
243
|
+
This is a
|
|
244
|
+
multi-line string
|
|
245
|
+
"""
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
### Imports
|
|
249
|
+
|
|
250
|
+
```python
|
|
251
|
+
# Standard library (provided by typesht-runtime)
|
|
252
|
+
import math
|
|
253
|
+
import random
|
|
254
|
+
import json
|
|
255
|
+
import re
|
|
256
|
+
import sys
|
|
257
|
+
import os
|
|
258
|
+
|
|
259
|
+
# With alias
|
|
260
|
+
import random as rng
|
|
261
|
+
|
|
262
|
+
# From imports
|
|
263
|
+
from json import dumps, loads
|
|
264
|
+
from math import sqrt, pi
|
|
265
|
+
|
|
266
|
+
# User modules
|
|
267
|
+
import mymodule
|
|
268
|
+
from mymodule import my_func
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
---
|
|
272
|
+
|
|
273
|
+
## Standard Library
|
|
274
|
+
|
|
275
|
+
| Module | Available Functions |
|
|
276
|
+
|--------|-------------------|
|
|
277
|
+
| `math` | `pi`, `e`, `sqrt`, `floor`, `ceil`, `abs`, `pow`, `log`, `log2`, `log10`, `sin`, `cos`, `tan`, `exp`, `trunc`, `inf`, `nan`, `isnan`, `isinf`, `gcd`, `factorial` |
|
|
278
|
+
| `random` | `random`, `randint`, `uniform`, `choice`, `shuffle`, `sample`, `seed` |
|
|
279
|
+
| `json` | `dumps`, `loads` |
|
|
280
|
+
| `re` | `match`, `search`, `findall`, `sub`, `split`, `compile` |
|
|
281
|
+
| `sys` | `argv`, `exit`, `version`, `platform` |
|
|
282
|
+
| `os` | `path.join`, `path.dirname`, `path.basename`, `path.abspath`, `path.normpath`, `path.splitext`, `path.split`, `path.exists`, `path.isfile`, `path.isdir`, `path.sep`, `getcwd`, `getenv` |
|
|
283
|
+
|
|
284
|
+
---
|
|
285
|
+
|
|
286
|
+
## Type Mappings
|
|
287
|
+
|
|
288
|
+
| TypeSht | TypeScript |
|
|
289
|
+
|---------|------------|
|
|
290
|
+
| `str` | `string` |
|
|
291
|
+
| `int` | `number` |
|
|
292
|
+
| `float` | `number` |
|
|
293
|
+
| `bool` | `boolean` |
|
|
294
|
+
| `list` | `any[]` |
|
|
295
|
+
| `list[int]` | `number[]` |
|
|
296
|
+
| `list[str]` | `string[]` |
|
|
297
|
+
| `dict` | `any` (backed by `PyDict` runtime class) |
|
|
298
|
+
| `None` | `null` |
|
|
299
|
+
|
|
300
|
+
---
|
|
301
|
+
|
|
302
|
+
## Python Semantics Preserved
|
|
303
|
+
|
|
304
|
+
TypeSht ships `typesht-runtime` — an npm package that ensures Python behavior is preserved at runtime:
|
|
305
|
+
|
|
306
|
+
- `print()` with `sep` and `end` keyword arguments
|
|
307
|
+
- Negative indexing on strings and lists (`items[-1]`)
|
|
308
|
+
- Slicing on strings and lists (`items[1:3]`)
|
|
309
|
+
- Floor division (`//`) toward negative infinity
|
|
310
|
+
- Python-style modulo (`%`) for negative numbers
|
|
311
|
+
- `dict` with ordered keys and Python iteration behavior (`for k in d`, `.items()`, `.values()`)
|
|
312
|
+
- `in` / `not in` membership testing
|
|
313
|
+
- Python-style error types (`ValueError`, `TypeError`, `IndexError`, `KeyError`, `ZeroDivisionError`)
|
|
314
|
+
|
|
315
|
+
---
|
|
316
|
+
|
|
317
|
+
## Project Structure
|
|
318
|
+
|
|
319
|
+
```
|
|
320
|
+
typesht/
|
|
321
|
+
├── src/typesht/ — pip-installable compiler package
|
|
322
|
+
│ ├── lexer.py — Tokenizer
|
|
323
|
+
│ ├── parser.py — AST + Parser
|
|
324
|
+
│ ├── codegen.py — TypeScript code generator
|
|
325
|
+
│ └── main.py — CLI entry point
|
|
326
|
+
│
|
|
327
|
+
├── compiler/ — Development copy (used by the test runner)
|
|
328
|
+
│ ├── lexer.py
|
|
329
|
+
│ ├── parser.py
|
|
330
|
+
│ ├── codegen.py
|
|
331
|
+
│ └── main.py
|
|
332
|
+
│
|
|
333
|
+
├── runtime/
|
|
334
|
+
│ ├── typesht-runtime.ts — Python behavior runtime (source)
|
|
335
|
+
│ └── package.json
|
|
336
|
+
│
|
|
337
|
+
├── tests/
|
|
338
|
+
│ └── test_compiler.py — 136 compiler unit tests
|
|
339
|
+
│
|
|
340
|
+
└── examples/
|
|
341
|
+
└── hello.tsht — Comprehensive example program
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
---
|
|
345
|
+
|
|
346
|
+
## Running Tests
|
|
347
|
+
|
|
348
|
+
```bash
|
|
349
|
+
python -m pytest tests/
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
---
|
|
353
|
+
|
|
354
|
+
## Roadmap
|
|
355
|
+
|
|
356
|
+
- [ ] f-string format specs (`f"{value:.2f}"`)
|
|
357
|
+
- [ ] Exception handling (`try` / `except` / `finally`)
|
|
358
|
+
- [ ] Multiple inheritance
|
|
359
|
+
- [ ] Type aliases
|
|
360
|
+
- [ ] Standard library expansion (`itertools`, `functools`, `collections`)
|
|
361
|
+
- [ ] Optional JS extras (explicit opt-in JavaScript-specific features)
|
|
362
|
+
- [x] Package manager integration (`pip install typesht`, `npm install typesht-runtime`)
|
|
363
|
+
|
|
364
|
+
---
|
|
365
|
+
|
|
366
|
+
## Author
|
|
367
|
+
|
|
368
|
+
Jeiel K Libert
|
|
369
|
+
|
|
370
|
+
## License
|
|
371
|
+
|
|
372
|
+
MIT
|