Nimic
Nimic is a pure Python module that facilitates writing AOT compilable code with a subset of Python (domain specific language), aiming to get C-level performance without leaving Python. Based on ctypes built-in module, it includes emulation of native types, pointers and operations on them, implementing dispatch, operator overloading, and templates. Nimic closely follows Nim programming language, to which nimic code transpiles.
Key principle: nimic code is valid Python that runs natively and transpiles to equivalent Nim code.
Module Architecture
nimic/ ├── ntypes.py — Public API: re-exports type system + Nim keyword/builtin shims ├── ntypesystem.py — Core type system (Object, NScalar, seq, dispatch, distinct, converter) ├── transpiler.py — AST-based Python → Nim source code transpiler ├── inliner.py — Template function inlining (@template, @template_expand) ├── ncode/ — Nim definitions (pydefs.nim, pystd/) ├── nimpy/ — API for generating Python libraries ├── std/ — Python shims for Nim stdlib (math, options, os, paths, strformat, ...) └── system/ — Python shims for Nim system modules (ansi_c)
ntypesystem.py — Core Type System
Organized in layers from low-level memory to high-level abstractions:
Layer Classes Purpose Memory Ntype , NTypeRegistry ctypes-backed buffers with value semantics Scalars NScalar → NInteger / NFloat Fixed-width types ( int8 .. int64 , uint8 .. uint64 , float16 .. float64 ) with arithmetic promotion Structs Object Nim "object" — fields via annotations, backed by ctypes.Structure Enums NIntEnum Nim integer enums with auto-registration Variants Object + match kind: Nim "case object" — discriminated unions Containers seq[T] , UncheckedArray[T] Growable sequence and pointer-indexed array Dispatch @dispatch , DispDict , NMetaClass Nim-style multi-dispatch via type annotations Modifiers @distinct , @converter Type distinctness and trivial type conversions Strings string str subclass with Nim-compatible & , % , isEmpty
ntypes.py — Public API & Keywords
Re-exports all of ntypesystem and adds Nim keyword/builtin emulation:
... continue reading