https://github.com/liwt31/npython
(Subset of) Python programming language implemented in Nim
https://github.com/liwt31/npython
interpreter nim python
Last synced: 5 months ago
JSON representation
(Subset of) Python programming language implemented in Nim
- Host: GitHub
- URL: https://github.com/liwt31/npython
- Owner: liwt31
- Created: 2018-12-23T15:07:50.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-02-19T09:19:28.000Z (almost 7 years ago)
- Last Synced: 2025-04-03T14:04:49.554Z (8 months ago)
- Topics: interpreter, nim, python
- Language: Nim
- Size: 876 KB
- Stars: 19
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# NPython
(Subset of) Python programming language implemented in Nim, from the compiler to the VM.
[Online interactive demo by compiling Nim to Javascript](https://liwt31.github.io/NPython-demo/).
### Purpose
Fun and practice. Learn both Python and Nim.
### Status
Capable of:
* flow control with `if else`, `while` and `for`
* basic function (closure) defination and call. Decorators.
* builtin print, dir, len, range, tuple, list, dict, exceptions and bunch of other simple helper functions
* list comprehension (no set or dict yet).
* basic import such as `import foo`. No alias, no `from`, etc
* raise exceptions, basic `try ... except XXXError ... `, with detailed traceback message. Assert statement.
* primitive `class` defination. No inheritance, no metatype, etc
* interactive mode and file mode
Check out `./tests` to see more examples.
### How to use
```
git clone https://github.com/liwt31/NPython.git
cd NPython
nimble c ./Python/python
./Python/python
```
### Todo
* more features on user defined class
* builtin compat dict
* yield stmt
* better bigint lib
### Performance
Nim is claimed to be as fast as C, and indeed it is. According to some primitive micro benchmarks (`spin.py` and `f_spin.py` in `tests/benchmark/`), although NPython is currently 5x-10x slower than CPython 3.7, it is at least in some cases faster than CPython < 2.4. This is already a huge achievement considering the numerous optimizations out there in the CPython codebase and NPython is focused on quick prototyping and lefts many rooms for optimization. For comparison, [RustPython0.0.1](https://github.com/RustPython/RustPython) is 100x slower than CPython3.7 and uses 10x more memory.
Currently, the performance bottlenecks are object allocation, seq accessing (compared with CPython direct memory accessing). The object allocation and seq accessing issue are basically impossible to solve unless we do GC on our own just like CPython.
### Drawbacks
NPython aims for both C and JavaScript targets, so it's hard (if not impossible) to perform low-level address based optimization.
NPython currently relies on Nim GC. Frankly speaking it's not satisfactory.
* The GC uses thread-local heap, makes threading nearly impossible (for Python).
* The GC can hardly be shared between different dynamic libs, which means NPython can not import extensions written in Nim.
If memory is managed manually, hopefully these drawbacks can be overcomed. Of course that's a huge sacrifice.
### License
Not sure. I think it should follow CPython license, but other Python implementations like RustPython use licenses like MIT.