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.
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.
- Python-like syntax for expressions, functions, classes, conditionals, loops, slicing, and container literals
- Core data structures:
list,tuple,dict,set,str,bytes - Typed exceptions with
try/except, includingexcept Exception as e - Improved function call semantics for keyword arguments, duplicate parameter checks, missing-argument reporting, and
*args - Basic nested-function closure capture for outer bindings
- REPL plus file execution
- Basic module imports:
import moduleimport package.modulefrom module import namefrom package import modulefrom package.module import namefrom module import name as alias
- Module lookup currently checks:
modules/<name>.pymodules/<pkg>/__init__.pylib/<name>.py./<name>.py
- Builtin-like modules written in TensorPy:
jsonremathtimerandomos
- Platform-facing runtime operations are routed through a portability layer in
src/platform.c
Phase 1: completed- common builtins expanded
- reflection helpers added
math,time,random, andosmodules added- platform abstraction layer added for system-facing operations
Phase 2: completed- module globals are isolated from script globals
- module cache is active across repeated imports
- package imports now support
pkg,pkg.mod, andfrom pkg.mod import name - package submodules are attached back onto parent package objects
Phase 3: completed- exception matching and exception objects were made more Python-like
- function call argument checking and keyword handling were tightened
- nested functions now capture outer bindings
jsonvalidation andrealternation support received a second pass- REPL multi-line block input was improved
Phase 4: completed- focus: GC readiness and memory model stabilization
- object environment and closure ownership have been normalized ahead of GC
- a non-collecting mark walker now traverses VM roots and heap object edges
- object finalization and VM teardown now free heap-owned buffers and objects
- an explicit mark-and-sweep collector now reclaims unreachable heap objects
- a conservative automatic GC trigger now runs from object allocation thresholds
- next step: return to language and compatibility work on top of the stabilized runtime
The runtime already includes a practical set of methods for:
listdictsettuplestrbytes
Examples include methods such as append, pop, remove, sort, setdefault, popitem, union, intersection, count, index, split, join, encode, decode, and hex.
Common builtins now also include:
isinstancegetattrsetattrhasattrreversedzipmapfilterroundordchrdir
Functions, lambdas, and methods now support *args collection.
The json module supports:
json.loads(...)json.dumps(...)json.JSON(...).parse()
The re module currently supports a useful regex subset:
re.compile(...)re.match(...)re.search(...)re.fullmatch(...)re.findall(...)re.split(...)re.sub(...)re.subn(...)
Supported regex constructs currently include:
- literals
.^and$- top-level alternation with
| *,+,?- character classes like
[abc] - ranges like
[a-z] - negated classes like
[^0-9] \d,\w,\s
makeThis produces the interpreter binary:
./tensorpyRun a script:
./tensorpy path/to/script.pyRun one command:
./tensorpy -c "print(1 + 2)"Start the interactive REPL:
./tensorpyIn the REPL, expression-like input is automatically echoed:
> 1 + 2
3
> x = 7
> x
7
import json
from json import loads as jl
print(json.dumps({"ok": True, "nums": [1, 2]}))
print(jl("[1,2,3]")[2])import re
print(re.findall("\\d+", "a12 b34 c5"))
print(re.sub("\\d+", "#", "a12 b34 c5"))Run the full test suite:
python3 run_tests.pyAt the time of writing, the suite contains 29 organized test files and passes in the current workspace.
- src/main.c: CLI entry point and REPL
- src/compiler.c: parser and bytecode compiler
- src/vm.c: virtual machine and runtime behavior
- src/platform.c: portability layer for filesystem, time, random, and OS-facing helpers
- modules: TensorPy standard-library-style modules
- modules/json.py: TensorPy JSON module
- modules/re.py: TensorPy regex module
- modules/math.py: TensorPy math module
- modules/time.py: TensorPy time module
- modules/random.py: TensorPy random module
- modules/os.py: TensorPy os module
- tests: ordered regression and feature tests
TensorPy is not yet a full Python implementation. Notable gaps still include:
- incomplete builtin and standard library coverage
- partial regex compatibility
- missing GC work for later phases
- incomplete Python compatibility for many edge cases and advanced syntax forms
- expand data-structure method coverage, especially remaining
set,tuple,str, andbytesbehavior gaps - broaden general builtins beyond the current reflection and iterable helpers
- extend module loading with richer search rules and better error reporting
- continue improving package semantics and nested module behavior
- strengthen exception compatibility, including richer exception objects and more Python-like error messages
- improve REPL behavior for multi-line input, block handling, and interactive error display
- expand
jsoncompatibility and validation behavior - extend
resupport with groups, alternation, counted repetition, and flags - add more large end-to-end language stress tests
- revisit garbage collection in a later phase
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.