|
| 1 | +# TensorPy |
| 2 | + |
| 3 | +TensorPy is a small Python-like interpreter written in C. It currently supports a useful subset of Python syntax, core data structures, exceptions, a simple module system, and a growing standard-library-style layer implemented in TensorPy itself. |
| 4 | + |
| 5 | +This project is still in an active build-out phase. The goal right now is practical language coverage and runtime stability, not full CPython or MicroPython compatibility. |
| 6 | + |
| 7 | +## Current Features |
| 8 | + |
| 9 | +- Python-like syntax for expressions, functions, classes, conditionals, loops, slicing, and container literals |
| 10 | +- Core data structures: `list`, `tuple`, `dict`, `set`, `str`, `bytes` |
| 11 | +- Typed exceptions with `try` / `except` |
| 12 | +- REPL plus file execution |
| 13 | +- Basic module imports: |
| 14 | + - `import module` |
| 15 | + - `from module import name` |
| 16 | + - `from module import name as alias` |
| 17 | +- Module lookup currently checks: |
| 18 | + - `modules/<name>.py` |
| 19 | + - `lib/<name>.py` |
| 20 | + - `./<name>.py` |
| 21 | +- Builtin-like modules written in TensorPy: |
| 22 | + - `json` |
| 23 | + - `re` |
| 24 | + |
| 25 | +## Implemented Library Surface |
| 26 | + |
| 27 | +The runtime already includes a practical set of methods for: |
| 28 | + |
| 29 | +- `list` |
| 30 | +- `dict` |
| 31 | +- `set` |
| 32 | +- `tuple` |
| 33 | +- `str` |
| 34 | +- `bytes` |
| 35 | + |
| 36 | +Examples include methods such as `append`, `pop`, `remove`, `sort`, `setdefault`, `popitem`, `union`, `intersection`, `count`, `index`, `split`, `join`, `encode`, `decode`, and `hex`. |
| 37 | + |
| 38 | +The `json` module supports: |
| 39 | + |
| 40 | +- `json.loads(...)` |
| 41 | +- `json.dumps(...)` |
| 42 | +- `json.JSON(...).parse()` |
| 43 | + |
| 44 | +The `re` module currently supports a useful regex subset: |
| 45 | + |
| 46 | +- `re.compile(...)` |
| 47 | +- `re.match(...)` |
| 48 | +- `re.search(...)` |
| 49 | +- `re.fullmatch(...)` |
| 50 | +- `re.findall(...)` |
| 51 | +- `re.split(...)` |
| 52 | +- `re.sub(...)` |
| 53 | +- `re.subn(...)` |
| 54 | + |
| 55 | +Supported regex constructs currently include: |
| 56 | + |
| 57 | +- literals |
| 58 | +- `.` |
| 59 | +- `^` and `$` |
| 60 | +- `*`, `+`, `?` |
| 61 | +- character classes like `[abc]` |
| 62 | +- ranges like `[a-z]` |
| 63 | +- negated classes like `[^0-9]` |
| 64 | +- `\d`, `\w`, `\s` |
| 65 | + |
| 66 | +## Build |
| 67 | + |
| 68 | +```bash |
| 69 | +make |
| 70 | +``` |
| 71 | + |
| 72 | +This produces the interpreter binary: |
| 73 | + |
| 74 | +```bash |
| 75 | +./tensorpy |
| 76 | +``` |
| 77 | + |
| 78 | +## Usage |
| 79 | + |
| 80 | +Run a script: |
| 81 | + |
| 82 | +```bash |
| 83 | +./tensorpy path/to/script.py |
| 84 | +``` |
| 85 | + |
| 86 | +Run one command: |
| 87 | + |
| 88 | +```bash |
| 89 | +./tensorpy -c "print(1 + 2)" |
| 90 | +``` |
| 91 | + |
| 92 | +Start the interactive REPL: |
| 93 | + |
| 94 | +```bash |
| 95 | +./tensorpy |
| 96 | +``` |
| 97 | + |
| 98 | +In the REPL, expression-like input is automatically echoed: |
| 99 | + |
| 100 | +```text |
| 101 | +> 1 + 2 |
| 102 | +3 |
| 103 | +> x = 7 |
| 104 | +> x |
| 105 | +7 |
| 106 | +``` |
| 107 | + |
| 108 | +## Examples |
| 109 | + |
| 110 | +### Importing Modules |
| 111 | + |
| 112 | +```python |
| 113 | +import json |
| 114 | +from json import loads as jl |
| 115 | + |
| 116 | +print(json.dumps({"ok": True, "nums": [1, 2]})) |
| 117 | +print(jl("[1,2,3]")[2]) |
| 118 | +``` |
| 119 | + |
| 120 | +### Regex |
| 121 | + |
| 122 | +```python |
| 123 | +import re |
| 124 | + |
| 125 | +print(re.findall("\\d+", "a12 b34 c5")) |
| 126 | +print(re.sub("\\d+", "#", "a12 b34 c5")) |
| 127 | +``` |
| 128 | + |
| 129 | +## Testing |
| 130 | + |
| 131 | +Run the full test suite: |
| 132 | + |
| 133 | +```bash |
| 134 | +python3 run_tests.py |
| 135 | +``` |
| 136 | + |
| 137 | +At the time of writing, the suite contains `20` organized test files and passes in the current workspace. |
| 138 | + |
| 139 | +## Project Layout |
| 140 | + |
| 141 | +- [src/main.c](/Users/tensorcraft/Projects/TensorPy/src/main.c): CLI entry point and REPL |
| 142 | +- [src/compiler.c](/Users/tensorcraft/Projects/TensorPy/src/compiler.c): parser and bytecode compiler |
| 143 | +- [src/vm.c](/Users/tensorcraft/Projects/TensorPy/src/vm.c): virtual machine and runtime behavior |
| 144 | +- [modules](/Users/tensorcraft/Projects/TensorPy/modules): TensorPy standard-library-style modules |
| 145 | +- [modules/json.py](/Users/tensorcraft/Projects/TensorPy/modules/json.py): TensorPy JSON module |
| 146 | +- [modules/re.py](/Users/tensorcraft/Projects/TensorPy/modules/re.py): TensorPy regex module |
| 147 | +- [tests](/Users/tensorcraft/Projects/TensorPy/tests): ordered regression and feature tests |
| 148 | + |
| 149 | +## Limitations |
| 150 | + |
| 151 | +TensorPy is not yet a full Python implementation. Notable gaps still include: |
| 152 | + |
| 153 | +- incomplete import/package semantics |
| 154 | +- incomplete builtin and standard library coverage |
| 155 | +- partial regex compatibility |
| 156 | +- missing GC work for later phases |
| 157 | +- incomplete Python compatibility for many edge cases and advanced syntax forms |
| 158 | + |
| 159 | +## TODO |
| 160 | + |
| 161 | +- expand data-structure method coverage, especially remaining `set`, `tuple`, `str`, and `bytes` behavior gaps |
| 162 | +- add more general builtins such as `zip`, `map`, `filter`, `reversed`, `isinstance`, `getattr`, `setattr`, and `hasattr` |
| 163 | +- improve module loading beyond the current minimal `import` / `from ... import ... as ...` support |
| 164 | +- add package semantics and better module path resolution |
| 165 | +- strengthen exception compatibility, including richer exception objects and more Python-like error messages |
| 166 | +- improve REPL behavior for multi-line input, block handling, and interactive error display |
| 167 | +- expand `json` compatibility and validation behavior |
| 168 | +- extend `re` support with groups, alternation, counted repetition, and flags |
| 169 | +- add more large end-to-end language stress tests |
| 170 | +- revisit garbage collection in a later phase |
| 171 | + |
| 172 | +## Status |
| 173 | + |
| 174 | +TensorPy is already useful for language and runtime experimentation, feature prototyping, and growing a test-backed Python-like interpreter. It is not yet a drop-in replacement for CPython or MicroPython. |
0 commit comments