Download
⬇️ Latest Release — v6.0.0
One-Line Install (Recommended):
These scripts download the latest nyx binary from GitHub Releases and install it to your PATH automatically.
Windows (PowerShell):
irm https://raw.githubusercontent.com/suryasekhar06jemsbond-lab/Nyx/main/scripts/install.ps1 | iex
Linux / macOS (Shell):
curl -fsSL https://raw.githubusercontent.com/suryasekhar06jemsbond-lab/Nyx/main/scripts/install.sh | sh
Manual Download (Windows):
# Download nyx.exe and add to PATH
mkdir "$env:LOCALAPPDATA\Programs\nyx" -Force
Invoke-WebRequest -Uri "https://github.com/suryasekhar06jemsbond-lab/Nyx/releases/latest/download/nyx-windows-x64.exe" -OutFile "$env:LOCALAPPDATA\Programs\nyx\nyx.exe"
[Environment]::SetEnvironmentVariable("PATH", "$env:LOCALAPPDATA\Programs\nyx;$env:PATH", "User")
# Restart terminal, then:
nyx --version
Install VS Code Extension:
# Download and install the VSIX
code --install-extension https://github.com/suryasekhar06jemsbond-lab/Nyx/releases/latest/download/nyx-language-6.0.0.vsix
All releases: github.com/suryasekhar06jemsbond-lab/Nyx/releases
Table of Contents
Click to expand full table of contents (40+ chapters)
� Architecture Overview
Nyx has three execution modes — choose whatever fits your use case:
┌──────────────────────────────────────────────────────┐
│ YOUR .ny FILE │
└─────────────────────┬────────────────────────────────┘
│
┌───────────┼───────────┐
▼ ▼ ▼
┌─────────┐ ┌──────────┐ ┌──────────┐
│ Native │ │ Python │ │ Web │
│ Compiler │ │Interpre- │ │ Runtime │
│ (C99) │ │ ter │ │ (HTTP) │
└────┬─────┘ └────┬─────┘ └────┬─────┘
▼ ▼ ▼
┌─────────┐ ┌──────────┐ ┌──────────┐
│ .exe │ │ Evaluated│ │ HTTP │
│ binary │ │ in Python│ │ Server │
└─────────┘ └──────────┘ └──────────┘
| Mode |
Command |
Use Case |
Speed |
| Native C Compiler |
make && ./build/nyx file.ny |
Production, max performance |
v3.3.3, compiled to machine code |
| Python Interpreter |
python run.py file.ny |
Development, prototyping |
Full language features |
| Web Runtime |
python nyx_runtime.py site.ny |
Web apps, HTTP servers |
Threading HTTP server |
How The Interpreter Works
Source Code (.ny)
│
▼
┌─────────┐ ┌─────────┐ ┌──────────────┐ ┌────────────┐
│ Lexer │───▶│ Parser │───▶│ AST (60+ │───▶│ Interpreter│
│ (530 ln) │ │(650 ln) │ │ node types) │ │ (551 ln) │
└─────────┘ └─────────┘ └──────────────┘ └────────────┘
│ │ │
▼ ▼ ▼
150+ Token Pratt Parser Scoped Environment
Types 11 Precedence with built-ins
Levels & std/lib imports
Lexer Features
- Unicode identifiers
- Hash (
#), C++ (//), C block (/* */) comments
- String escape sequences, raw strings (
r"..."), byte strings (b"..."), f-strings (f"...{expr}...")
- Multiline strings (
"""...""")
- Hex (
0xFF), octal (0o77), binary (0b1010) numeric literals
- Token hooks, trivia tracking, error recovery, state save/restore
Parser Features
- Pratt parser with 11 precedence levels (LOWEST → INDEX)
- Extensible via
register_prefix(), register_infix(), register_statement()
- Error recovery via
_synchronize_statement() / _synchronize_expression()
- 60+ AST node types (see AST Reference)
Project Structure
Nyx/
├── run.py # Main entry point: python run.py file.ny
├── nyx_runtime.py # Web runtime server for HTTP apps
├── nyx.bat / nyx.sh # Platform launchers
├── Makefile # Native compiler build: make → build/nyx
├── package.json # Project metadata (v5.5.0)
├── ny.registry # Package registry index
├── nypm.config # Package manager config
├── nypm.js # Package manager (12 commands)
│
├── src/ # Core interpreter (Python)
│ ├── lexer.py # 530 lines — tokenizer
│ ├── parser.py # 650 lines — Pratt parser
│ ├── ast_nodes.py # 900 lines — 60+ AST node types
│ ├── interpreter.py # 551 lines — evaluator
│ └── token_types.py # 680 lines — 150+ token types
│
├── native/ # Native C compiler
│ └── nyx.c # C99 compiler (v3.3.3)
│
├── stdlib/ # 109 standard library modules
│ ├── math.ny, io.ny, json.ny, http.ny, crypto.ny, ...
│ └── dfas/ # Dynamic Field Arithmetic System (10 files)
│
├── engines/ # 117 specialized engines
│ ├── nyai/, nygpu/, nyweb/, nygame/, ...
│
├── compiler/ # Bootstrap compiler
│ ├── bootstrap.ny # Self-hosting bootstrap
│ ├── v3_seed.ny # V3 seed compiler
│ └── v3_compiler_template.c
│
├── language/ # Language specification
│ ├── grammar.ebnf # Complete formal grammar
│ ├── types.md # Full type system spec
│ ├── ownership.md # Ownership & borrowing model
│ └── concurrency.md # Concurrency model spec
│
├── examples/ # Example programs
├── tests/ # 180+ test files
├── benchmarks/ # Performance benchmarks
├── docs/ # 70+ documentation files
├── tools/ # Nyx Studio, crypto CLI, etc.
├── configs/production/ # AAA game engine production configs
└── editor/vscode/ # VS Code extension
�🌟 Why Nyx?
"Learn one language. Build everything. Replace your entire stack."
| What You Get |
Details |
| 10-100x faster than Python |
Native compilation, zero-cost abstractions |
| 60% less code than any language |
Expression-oriented, smart inference |
| Rust-level memory safety |
Ownership + borrowing, no garbage collector |
| 117 built-in engines |
AI, GPU, Web, Game, Database — all native, zero install |
| 98 stdlib modules |
Everything from math to hypervisors — all free, all included |
| Semicolons optional |
Write clean code your way |
| Replaces 9+ languages |
Python, JS, Rust, C++, Go, Java, C#, Zig, and more |
Nyx vs Other Languages
| Feature |
Nyx |
Python |
Rust |
C++ |
Go |
JavaScript |
| Performance |
⚡ 10-100x Python |
🐌 Slow |
⚡ Fast |
⚡ Fast |
⚡ Fast |
🐌 Slow |
| Memory Safety |
✅ Ownership |
❌ GC |
✅ Ownership |
❌ Manual |
✅ GC |
❌ GC |
| Type System |
✅ Static+Infer |
❌ Dynamic |
✅ Static |
✅ Static |
✅ Static |
❌ Dynamic |
| Code Brevity |
✅ 60% less |
✅ Concise |
❌ Verbose |
❌ Very verbose |
❌ Verbose |
⚠️ Medium |
| GPU Computing |
✅ Native |
❌ Needs CuPy |
❌ Needs libs |
⚠️ CUDA |
❌ No |
❌ No |
| AI/ML Built-in |
✅ 21 engines |
❌ pip install |
❌ No |
❌ No |
❌ No |
❌ No |
| Web Server |
✅ Native |
❌ pip install |
❌ cargo add |
❌ No |
✅ Built-in |
✅ Built-in |
| Game Engine |
✅ Native |
❌ pip install |
❌ No |
❌ No |
❌ No |
❌ No |
| Async/Await |
✅ Native |
✅ asyncio |
✅ tokio |
❌ Complex |
✅ goroutines |
✅ Promises |
| Pattern Matching |
✅ Full |
⚠️ Basic |
✅ Full |
❌ No |
❌ No |
❌ No |
| Macros |
✅ Hygienic |
❌ No |
✅ Proc macros |
✅ Templates |
❌ No |
❌ No |
| Null Safety |
✅ Option |
❌ None |
✅ Option |
❌ nullptr |
❌ nil |
❌ null/undefined |
Installation
Install Nyx Runtime
Option 1 — Download Pre-built Binary (Fastest)
Download nyx.exe from the latest release:
# Windows — one-liner install
mkdir "$env:LOCALAPPDATA\Programs\nyx" -Force
Invoke-WebRequest -Uri "https://github.com/suryasekhar06jemsbond-lab/Nyx/releases/latest/download/nyx-windows-x64.exe" -OutFile "$env:LOCALAPPDATA\Programs\nyx\nyx.exe"
[Environment]::SetEnvironmentVariable("PATH", "$env:LOCALAPPDATA\Programs\nyx;$env:PATH", "User")
# Restart terminal, then:
nyx hello.ny
Option 2 — Build from Source
# Clone and build
git clone https://github.com/suryasekhar06jemsbond-lab/Nyx.git
cd Nyx
make
./build/nyx hello.ny
# Windows (with GCC or Clang installed)
powershell -ExecutionPolicy Bypass -File scripts/build_windows.ps1 -Output build/nyx.exe
.\build\nyx.exe hello.ny
Option 3 — Python Interpreter (no compilation needed)
git clone https://github.com/suryasekhar06jemsbond-lab/Nyx.git
cd Nyx
python run.py hello.ny
Install VS Code Extension
Option 1 — Download VSIX from GitHub Releases (Recommended)
# Download the latest VSIX
# → https://github.com/suryasekhar06jemsbond-lab/Nyx/releases/latest/download/nyx-language-6.0.0.vsix
# Install from file
code --install-extension nyx-language-6.0.0.vsix
Option 2 — VS Code Marketplace
1. Open VS Code
2. Press Ctrl+Shift+X (Extensions)
3. Search "Nyx Language"
4. Click Install
Option 3 — Build from Source
cd editor/vscode/nyx-language
npm install
npm run compile
npx vsce package --no-dependencies
code --install-extension nyx-language-6.0.0.vsix
🔧 VS Code Extension Features
9 Integrated Commands
| Command |
Shortcut |
What It Does |
| Nyx: Run File |
Ctrl+Shift+R |
Execute the current .ny file |
| Nyx: Build Project |
Ctrl+Shift+B |
Compile the entire project |
| Nyx: Format Document |
Shift+Alt+F |
Auto-format your code |
| Nyx: Check File |
Ctrl+Shift+C |
Check for syntax errors |
| Nyx: Debug File |
F5 |
Start debugging with breakpoints |
| Nyx: Create New Project |
Command Palette |
Scaffold a new Nyx project |
| Nyx: Open Documentation |
Command Palette |
Browse Nyx docs |
| Nyx: Install Dependencies |
Command Palette |
Install packages via nypm |
| Nyx: Update Extension |
Command Palette |
Check for extension updates |
Language Features
- Syntax Highlighting — Full TextMate grammar with 20+ scopes for all Nyx syntax
- Code Completion — IntelliSense for keywords, builtins, functions, and snippets
- Hover Information — Documentation on hover for keywords and built-in functions
- Go to Definition — Jump to any function, class, or variable definition
- Find All References — Find every usage of any symbol in your file
- Rename Symbol — Safely rename variables, functions, and classes everywhere
- Document Symbols — Outline view showing all functions and classes
- Signature Help — See function parameters as you type
- Code Actions — Quick fixes like "Add import" for unknown modules
- Formatting — Auto-format on save or on demand
- Diagnostics — Real-time error detection as you type
- 50+ Code Snippets — Type
fn, class, for, match, etc. and press Tab
- 2 Professional Themes — Nyx Dark and Nyx Light
20 Configuration Options
Open Settings (Ctrl+,) and search "nyx":
{
"nyx.runtime.path": "nyx",
"nyx.compiler.path": "nyc",
"nyx.formatter.enabled": true,
"nyx.formatter.tabSize": 4,
"nyx.linting.enabled": true,
"nyx.linting.level": "warning",
"nyx.diagnostics.onSave": true,
"nyx.debugger.stopOnEntry": false,
"nyx.language.inferTypes": true,
"nyx.language.strictMode": false,
"nyx.hover.enabled": true,
"nyx.completion.enabled": true
}
📖 The Complete Nyx Language Guide
Goal: After reading this guide, you will be able to build anything with Nyx — from "Hello World" to AI models, web servers, game engines, and operating system kernels.
📘 Chapter 1: Your First Nyx Program
Hello World
Create a file called hello.ny:
print("Hello, World!")
Run it:
nyx hello.ny
Output:
Hello, World!
That's it. No imports, no main function, no boilerplate. Just write and run.
# This is a single-line comment
/* This is a
multi-line comment */
The Main Function (Optional)
# You can write code at the top level (no main needed)
print("I run directly!")
# Or use a main function for larger programs
fn main() {
print("I run from main!")
return 0
}
main()
Semicolons Are Optional
# Both styles are valid — use whichever you prefer
# Without semicolons (clean style)
let name = "Nyx"
print(name)
# With semicolons (traditional style)
let name = "Nyx";
print(name);
Print Multiple Values
print("Name:", "Nyx", "Version:", 6)
# Output: Name: Nyx Version: 6
📘 Chapter 2: Variables & Data Types
Declaring Variables
# Immutable (cannot be changed after creation)
let name = "Nyx"
let age = 25
let pi = 3.14159
# Mutable (can be changed)
let mut counter = 0
counter = counter + 1
# Constants (never changes, known at compile time)
const MAX_SIZE = 1000
const APP_NAME = "MyApp"
All Data Types
| Type |
Example |
Description |
int |
42, -7, 0xFF, 0b1010, 0o77 |
Integer (also i8, i16, i32, i64) |
uint |
255u |
Unsigned integer (u8, u16, u32, u64) |
float |
3.14, -0.5, 1e10 |
Floating point (f32, f64) |
bool |
true, false |
Boolean |
string |
"hello", 'hi' |
Text string |
char |
'A' |
Single character |
null |
null |
No value |
array |
[1, 2, 3] |
Ordered collection |
object |
{name: "Nyx"} |
Key-value pairs |
tuple |
(1, "hello", true) |
Fixed-size mixed collection |
function |
fn(x) = x + 1 |
First-class function |
Option<T> |
Some(42), None |
Value that might not exist |
Result<T,E> |
Ok(value), Err(msg) |
Success or error |
Type Checking
let x = 42
print(type_of(x)) # "int"
print(is_int(x)) # true
print(is_string(x)) # false
# Type checking functions (all built-in, no import needed)
is_int(42) # true
is_bool(true) # true
is_string("hi") # true
is_array([1,2]) # true
is_function(print)# true
is_null(null) # true
Type Conversion
let num = int("42") # String to int → 42
let text = str(42) # Int to string → "42"
let decimal = float("3.14") # String to float → 3.14
Destructuring
# Tuple destructuring
let (x, y) = (10, 20)
# Array destructuring
let [first, second, ...rest] = [1, 2, 3, 4, 5]
# first = 1, second = 2, rest = [3, 4, 5]
# Object destructuring
let {name, age} = {name: "Nyx", age: 1, type: "lang"}
# name = "Nyx", age = 1
📘 Chapter 3: Operators
Arithmetic
let a = 10 + 3 # 13 (addition)
let b = 10 - 3 # 7 (subtraction)
let c = 10 * 3 # 30 (multiplication)
let d = 10 / 3 # 3.33 (division)
let e = 10 % 3 # 1 (modulo/remainder)
let f = 10 // 3 # 3 (integer division)
let g = 2 ** 10 # 1024 (exponentiation)
Comparison
10 == 10 # true (equal)
10 != 5 # true (not equal)
10 > 5 # true (greater than)
10 < 20 # true (less than)
10 >= 10 # true (greater or equal)
10 <= 20 # true (less or equal)
Logical
true && false # false (AND)
true || false # true (OR)
!true # false (NOT)
Bitwise
5 & 3 # 1 (AND)
5 | 3 # 7 (OR)
5 ^ 3 # 6 (XOR)
~5 # -6 (NOT)
5 << 2 # 20 (left shift)
20 >> 2 # 5 (right shift)
Special Operators
# Null coalescing — use fallback if null
let name = user_name ?? "Anonymous"
# Pipeline — chain operations left to right
let result = data |> filter(|x| x > 0) |> map(|x| x * 2) |> sum()
# Range
0..10 # 0, 1, 2, ..., 9
0..=10 # 0, 1, 2, ..., 10
# Optional chaining
let city = user?.address?.city
Assignment
let mut x = 10
x += 5 # x = 15
x -= 3 # x = 12
x *= 2 # x = 24
x /= 4 # x = 6
x %= 5 # x = 1
x **= 3 # x = 1
📘 Chapter 4: Control Flow
If / Else
let age = 18
if age >= 18 {
print("Adult")
} else if age >= 13 {
print("Teenager")
} else {
print("Child")
}
If as Expression (Returns a Value)
let status = if age >= 18 { "adult" } else { "minor" }
print(status) # "adult"
While Loop
let mut i = 0
while i < 5 {
print(i)
i = i + 1
}
# Output: 0 1 2 3 4
For Loop
# For-in loop (iterate over collection)
let fruits = ["apple", "banana", "cherry"]
for fruit in fruits {
print(fruit)
}
# For with index
for i, fruit in fruits {
print(i, ":", fruit)
}
# Output: 0 : apple 1 : banana 2 : cherry
# For with range
for i in 0..5 {
print(i)
}
# Output: 0 1 2 3 4
# C-style for loop
for (let i = 0; i < 10; i = i + 1) {
print(i)
}
Break & Continue
for i in 0..100 {
if i == 5 { break } # Stop the loop entirely
if i % 2 == 0 { continue } # Skip even numbers
print(i)
}
# Output: 1 3
Loop (Infinite)
let mut count = 0
loop {
count = count + 1
if count > 3 { break }
print(count)
}
# Output: 1 2 3
Switch / Case
let day = "Monday"
switch day {
case "Monday": { print("Start of week") }
case "Friday": { print("Almost weekend!") }
case "Saturday": { print("Weekend!") }
case "Sunday": { print("Weekend!") }
default: { print("Regular day") }
}
Match (Powerful Pattern Matching)
See Chapter 10 for full details.
let value = 42
let result = match value {
case 0 => "zero"
case 1..10 => "small"
case n if n > 100 => "big"
case _ => "medium"
}
print(result) # "medium"
📘 Chapter 5: Functions
Basic Functions
fn greet(name) {
print("Hello, " + name + "!")
}
greet("World") # Hello, World!
Functions with Return Values
fn add(a, b) {
return a + b
}
let sum = add(3, 4) # 7
One-Line Functions (Expression Body)
fn add(a, b) = a + b
fn square(x) = x * x
fn is_even(n) = n % 2 == 0
Default Parameters
fn greet(name, greeting = "Hello") {
print(greeting + ", " + name + "!")
}
greet("Nyx") # Hello, Nyx!
greet("Nyx", "Hi") # Hi, Nyx!
Multiple Return Values
fn divide(a, b) {
return (a / b, a % b) # Returns a tuple
}
let (quotient, remainder) = divide(17, 5)
# quotient = 3, remainder = 2
Recursive Functions
fn factorial(n) {
if n <= 1 { return 1 }
return n * factorial(n - 1)
}
print(factorial(10)) # 3628800
fn fibonacci(n) {
if n <= 1 { return n }
return fibonacci(n - 1) + fibonacci(n - 2)
}
print(fibonacci(10)) # 55
Higher-Order Functions
# Functions that take other functions as arguments
fn apply(f, x) = f(x)
fn double(x) = x * 2
print(apply(double, 5)) # 10
# Functions that return functions
fn multiplier(factor) {
return fn(x) = x * factor
}
let triple = multiplier(3)
print(triple(7)) # 21
Typed Functions
fn add(a: int, b: int) -> int {
return a + b
}
fn greet(name: str) -> str {
return "Hello, " + name
}
📘 Chapter 6: Arrays, Objects & Collections
Arrays
# Create arrays
let numbers = [1, 2, 3, 4, 5]
let mixed = [1, "hello", true, null]
let empty = []
# Access elements (0-indexed)
print(numbers[0]) # 1
print(numbers[2]) # 3
# Length
print(len(numbers)) # 5
# Add elements
push(numbers, 6) # [1, 2, 3, 4, 5, 6]
# Remove last element
let last = pop(numbers) # Returns 6, array is now [1, 2, 3, 4, 5]
# Check membership
print(3 in numbers) # true
# Iterate
for num in numbers {
print(num)
}
# Array operations
let sorted = sort(numbers)
let reversed = reverse(numbers)
let total = sum(numbers) # 15
let biggest = max(numbers) # 5
let smallest = min(numbers) # 1
List Comprehensions
# Create arrays with expressions
let squares = [x * x for x in 0..10]
# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
# With filter
let even_squares = [x * x for x in 0..10 if x % 2 == 0]
# [0, 4, 16, 36, 64]
# With index
let indexed = [str(i) + ": " + item for i, item in ["a", "b", "c"]]
# ["0: a", "1: b", "2: c"]
Objects (Dictionaries / Maps)
# Create objects
let person = {
name: "Nyx",
age: 1,
language: true
}
# Access values
print(person.name) # "Nyx"
print(person["age"]) # 1
# Keys, values, items
print(keys(person)) # ["name", "age", "language"]
print(values(person)) # ["Nyx", 1, true]
print(items(person)) # [["name","Nyx"], ["age",1], ...]
# Check if key exists
print(has(person, "name")) # true
# Iterate
for key, value in items(person) {
print(key + " = " + str(value))
}
Dictionary Comprehensions
let squares_map = {str(x): x * x for x in 0..5}
# {"0": 0, "1": 1, "2": 4, "3": 9, "4": 16}
Tuples
let point = (10, 20)
let (x, y) = point
print(x) # 10
print(y) # 20
Built-in Collection Functions
# All of these are native — no imports needed
len([1,2,3]) # 3 — length of any collection
push(arr, item) # Add item to end
pop(arr) # Remove and return last item
sort(arr) # Sort array
reverse(arr) # Reverse array
sum(arr) # Sum all numbers
min(arr) # Smallest value
max(arr) # Largest value
all(arr) # True if all elements are truthy
any(arr) # True if any element is truthy
range(n) # [0, 1, 2, ..., n-1]
range(a, b) # [a, a+1, ..., b-1]
clamp(x, lo, hi) # Clamp x between lo and hi
abs(x) # Absolute value
keys(obj) # Get all keys of an object
values(obj) # Get all values of an object
items(obj) # Get key-value pairs
has(obj, key) # Check if key exists
📘 Chapter 7: Strings
String Basics
let greeting = "Hello, World!"
let name = 'Nyx'
let multi = "This is a
multi-line string"
# String length
print(len(greeting)) # 13
# Concatenation
let full = "Hello" + " " + "World"
# Repetition
let line = "-" * 40 # "----------------------------------------"
# Access characters
print(greeting[0]) # "H"
print(greeting[7]) # "W"
String Methods (from stdlib/string.ny — no import needed)
# Case conversion
upper("hello") # "HELLO"
lower("HELLO") # "hello"
capitalize("hello") # "Hello"
title("hello world") # "Hello World"
swapcase("Hello") # "hELLO"
# Searching
"hello".contains("ell") # true
"hello".starts_with("he") # true
"hello".ends_with("lo") # true
"hello".index_of("ll") # 2
# Trimming
" hello ".trim() # "hello"
" hello ".trim_start() # "hello "
" hello ".trim_end() # " hello"
# Splitting & joining
"a,b,c".split(",") # ["a", "b", "c"]
["a", "b", "c"].join("-") # "a-b-c"
# Replacing
"hello world".replace("world", "Nyx") # "hello Nyx"
# String formatting
let name = "Nyx"
let version = 6
print("${name} v${version}") # "Nyx v6"
📘 Chapter 8: Classes & Object-Oriented Programming
Basic Class
class Animal {
fn init(self, name, sound) {
self.name = name
self.sound = sound
}
fn speak(self) {
print(self.name + " says " + self.sound + "!")
}
}
let dog = Animal("Buddy", "Woof")
dog.speak() # Buddy says Woof!
Inheritance
class Dog extends Animal {
fn init(self, name) {
super.init(name, "Woof")
self.tricks = []
}
fn learn_trick(self, trick) {
push(self.tricks, trick)
}
fn show_tricks(self) {
for trick in self.tricks {
print(self.name + " can " + trick)
}
}
}
let rex = Dog("Rex")
rex.learn_trick("sit")
rex.learn_trick("shake")
rex.show_tricks()
# Rex can sit
# Rex can shake
Typed Class with Enums
pub enum Status {
Active,
Inactive,
Suspended
}
pub class User {
name: str
email: str
age: int
status: Status
fn init(self, name: str, email: str, age: int) {
self.name = name
self.email = email
self.age = age
self.status = Status.Active
}
fn is_adult(self) -> bool {
return self.age >= 18
}
fn to_string(self) -> str {
return "${self.name} (${self.email})"
}
}
Static Methods & Properties
class MathUtils {
fn square(x) = x * x
fn cube(x) = x * x * x
fn is_prime(n) {
if n < 2 { return false }
for i in 2..n {
if n % i == 0 { return false }
}
return true
}
}
print(MathUtils.square(5)) # 25
print(MathUtils.is_prime(7)) # true
📘 Chapter 9: Traits & Generics
Traits (Interfaces)
trait Drawable {
fn draw(self)
fn area(self) -> float
}
class Circle implements Drawable {
fn init(self, radius) {
self.radius = radius
}
fn draw(self) {
print("Drawing circle with radius " + str(self.radius))
}
fn area(self) -> float {
return 3.14159 * self.radius * self.radius
}
}
class Rectangle implements Drawable {
fn init(self, width, height) {
self.width = width
self.height = height
}
fn draw(self) {
print("Drawing rectangle " + str(self.width) + "x" + str(self.height))
}
fn area(self) -> float {
return self.width * self.height
}
}
# Polymorphism
let shapes = [Circle(5), Rectangle(4, 6)]
for shape in shapes {
shape.draw()
print("Area: " + str(shape.area()))
}
Generics
class Box<T> {
fn init(self, value: T) {
self.value = value
}
fn get(self) -> T {
return self.value
}
fn map<U>(self, f: fn(T) -> U) -> Box<U> {
return Box(f(self.value))
}
}
let int_box = Box(42)
let str_box = Box("hello")
let doubled = int_box.map(|x| x * 2) # Box(84)
# Generic functions
fn first<T>(arr: [T]) -> T {
return arr[0]
}
# Generic with constraints
fn largest<T: Comparable>(a: T, b: T) -> T {
return if a > b { a } else { b }
}
📘 Chapter 10: Pattern Matching
Basic Match
fn describe(value) = match value {
case 0 => "zero"
case 1 => "one"
case 2 => "two"
case _ => "something else"
}
print(describe(1)) # "one"
print(describe(99)) # "something else"
Match with Guards
fn classify(n) = match n {
case 0 => "zero"
case n if n > 0 && n < 10 => "small positive"
case n if n >= 10 && n < 100 => "medium positive"
case n if n >= 100 => "large positive"
case n if n < 0 => "negative"
case _ => "unknown"
}
Match on Types
fn process(value) = match value {
case n: int => "integer: " + str(n)
case s: str => "string: " + s
case arr: array => "array with " + str(len(arr)) + " items"
case _ => "unknown type"
}
Match with Destructuring
fn describe_point(point) = match point {
case (0, 0) => "origin"
case (x, 0) => "on x-axis at " + str(x)
case (0, y) => "on y-axis at " + str(y)
case (x, y) => "at (" + str(x) + ", " + str(y) + ")"
}
# Result type matching
fn handle_result(r) = match r {
case Ok(value) => "Success: " + str(value)
case Err(msg) => "Error: " + msg
}
Match with Enums
enum Color {
Red,
Green,
Blue,
Custom(r: int, g: int, b: int)
}
fn to_hex(color) = match color {
case Color.Red => "#FF0000"
case Color.Green => "#00FF00"
case Color.Blue => "#0000FF"
case Color.Custom(r, g, b) => "rgb(" + str(r) + "," + str(g) + "," + str(b) + ")"
}
📘 Chapter 11: Error Handling
Try / Catch / Finally
try {
let result = 10 / 0
print(result)
} catch (err) {
print("Error caught: " + str(err))
} finally {
print("This always runs")
}
Throw Errors
fn validate_age(age) {
if age < 0 {
throw "Age cannot be negative"
}
if age > 150 {
throw "Age seems unrealistic"
}
return true
}
try {
validate_age(-5)
} catch (err) {
print("Validation failed: " + str(err))
}
Result Type (Recommended)
fn divide(a, b) {
if b == 0 { return Err("Division by zero") }
return Ok(a / b)
}
let result = divide(10, 3)
match result {
case Ok(value) => print("Result: " + str(value))
case Err(msg) => print("Error: " + msg)
}
# Short-circuit with try? operator
fn compute() {
let a = divide(10, 2)? # Returns Err early if error
let b = divide(a, 3)?
return Ok(b)
}
Option Type
fn find_user(id) {
if id == 1 { return Some({name: "Nyx", age: 1}) }
return None
}
let user = find_user(1)
match user {
case Some(u) => print("Found: " + u.name)
case None => print("User not found")
}
# With null coalescing
let name = find_user(99)?.name ?? "Unknown"
📘 Chapter 12: Modules & Imports
Import Syntax
# Import a file
import "utils.ny"
# Import from standard library (no install needed!)
import std/io
import std/math
import std/json
import std/http
import std/crypto
# Import specific items
from std/collections import List, Map, Set
from std/math import PI, sqrt, sin, cos
# Import with alias
import std/io as file_io
# Import an engine (no install needed!)
use nyhttpd
use nyai
use nygpu
use nygame
Dual Import Syntax
Nyx supports both import and use for maximum flexibility:
# These are equivalent:
import std/math
use std/math
# 'use' is preferred for engines
use nyai
use nygpu
use nyweb
use nygame
# 'import' is preferred for files and stdlib
import "my_module.ny"
import std/collections
from std/io import read_file, write_file
Creating Modules
# File: math_utils.ny
module MathUtils {
fn add(a, b) = a + b
fn multiply(a, b) = a * b
const PI = 3.14159265358979
}
# File: main.ny
import "math_utils.ny"
print(MathUtils.add(3, 4)) # 7
print(MathUtils.PI) # 3.14159...
📘 Chapter 13: Closures & Lambdas
Lambda Expressions
# Short lambda syntax
let double = |x| x * 2
let add = |a, b| a + b
print(double(5)) # 10
print(add(3, 4)) # 7
# Multi-line lambda
let process = |x| {
let squared = x * x
let result = squared + 1
return result
}
print(process(5)) # 26
Closures (Capture Environment)
fn make_counter() {
let mut count = 0
return fn() {
count = count + 1
return count
}
}
let counter = make_counter()
print(counter()) # 1
print(counter()) # 2
print(counter()) # 3
Using with Higher-Order Functions
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Filter: keep only even numbers
let evens = filter(numbers, |x| x % 2 == 0)
# [2, 4, 6, 8, 10]
# Map: transform each element
let doubled = map(numbers, |x| x * 2)
# [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
# Reduce: combine all elements
let total = reduce(numbers, 0, |acc, x| acc + x)
# 55
# Combined with pipeline
let result = numbers
|> filter(|x| x > 3)
|> map(|x| x * x)
|> reduce(0, |acc, x| acc + x)
# 330
📘 Chapter 14: Pipelines & Comprehensions
Pipeline Operator (|>)
The pipeline operator passes the result of the left side as the first argument to the right side:
# Without pipeline (nested, hard to read)
let result = sum(map(filter(numbers, is_positive), square))
# With pipeline (linear, easy to read!)
let result = numbers
|> filter(is_positive)
|> map(square)
|> sum()
# Real-world example
let report = users
|> filter(|u| u.age >= 18)
|> map(|u| u.name)
|> sort()
|> join(", ")
print(report) # "Alice, Bob, Charlie"
List Comprehensions
# Basic
[x * 2 for x in 0..10]
# [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
# With condition
[x for x in 0..100 if is_prime(x)]
# [2, 3, 5, 7, 11, 13, ...]
# Nested
[(x, y) for x in 0..3 for y in 0..3]
# [(0,0), (0,1), (0,2), (1,0), ...]
# Dictionary comprehension
{name: len(name) for name in ["Nyx", "Python", "Rust"]}
# {"Nyx": 3, "Python": 6, "Rust": 4}
# Generator expression (lazy — doesn't compute all at once)
let squares = (x * x for x in 0..1000000)
📘 Chapter 15: Async & Concurrency
Async / Await
# Define async function
async fn fetch_data(url) {
let response = await http.get(url)
return response.body
}
# Run async code
let data = await fetch_data("https://api.example.com/data")
print(data)
Spawn (Parallel Tasks)
# Run tasks in parallel
let task1 = spawn fetch_data("https://api.example.com/users")
let task2 = spawn fetch_data("https://api.example.com/posts")
let task3 = spawn fetch_data("https://api.example.com/comments")
# Wait for all results
let (users, posts, comments) = await join!(task1, task2, task3)
Channels (Message Passing)
# Create a channel for communication between tasks
let (tx, rx) = channel()
# Producer
spawn fn() {
for i in 0..10 {
tx.send(i)
}
tx.close()
}
# Consumer
for value in rx {
print("Received: " + str(value))
}
Structured Concurrency
# TaskGroup ensures all tasks complete or all are cancelled
async fn fetch_all() {
let group = TaskGroup()
group.spawn(fetch_users)
group.spawn(fetch_posts)
group.spawn(fetch_comments)
let results = await group.join_all()
return results
}
📘 Chapter 16: Memory & Ownership
Ownership (Rust-like)
# Every value has exactly one owner
let name = "Nyx" # 'name' owns this string
let alias = name # Ownership MOVES to 'alias'
# print(name) # ERROR: 'name' no longer valid
# Clone to keep both
let name = "Nyx"
let copy = name.clone() # Both are now valid
Borrowing
# Immutable borrow — read-only access
fn print_length(s: &str) {
print(len(s))
}
let word = "hello"
print_length(&word) # Borrows, doesn't take ownership
print(word) # Still valid!
# Mutable borrow — read/write access
fn add_suffix(s: &mut str) {
s = s + "!"
}
let mut greeting = "hello"
add_suffix(&mut greeting)
print(greeting) # "hello!"
Smart Pointers
# Box — heap allocation with single owner
let boxed = Box(42)
print(boxed.get()) # 42
# Rc — reference counted (multiple owners)
let shared = Rc("shared data")
let ref1 = shared.clone()
let ref2 = shared.clone()
# All three can read the data
# Arc — atomic reference counting (thread-safe Rc)
let thread_safe = Arc("thread-safe data")
RAII (Resource Acquisition Is Initialization)
# Resources are automatically cleaned up when they go out of scope
fn process_file() {
let file = open("data.txt") # File opened
let data = file.read()
process(data)
# file is automatically closed here when it goes out of scope
}
📘 Chapter 17: Low-Level & Systems Programming
Inline Assembly
import std/asm
# Write inline assembly (x86-64)
let builder = AsmBuilder.new("intel")
builder.mov("rax", 1)
builder.mov("rdi", 42)
builder.syscall()
let code = builder.build()
# ARM assembly
let arm = AsmBuilder.new("arm")
arm.mov("r0", 0)
arm.add("r0", "r0", 1)
SIMD Vectorization
import std/simd
# Detect CPU SIMD capabilities
let caps = SIMD_ISA.detect()
print(caps.has_avx2) # true/false
print(caps.has_neon) # true/false (ARM)
# Vector math — 4-16x faster than scalar
let a = Vec4f(1.0, 2.0, 3.0, 4.0)
let b = Vec4f(5.0, 6.0, 7.0, 8.0)
let c = a + b # [6.0, 8.0, 10.0, 12.0] — single CPU instruction!
let d = a * b # [5.0, 12.0, 21.0, 32.0]
let dot = a.dot(b) # 70.0
Memory Allocators
import std/allocators
# Arena allocator — ultra-fast, bulk-free
let arena = ArenaAllocator.new(1024 * 1024) # 1MB
let ptr1 = arena.alloc(256)
let ptr2 = arena.alloc(512)
arena.reset() # Free everything at once — no per-object overhead
# Pool allocator — fixed-size objects, zero fragmentation
let pool = PoolAllocator.new(64, 1024) # 64-byte objects, 1024 slots
let obj = pool.alloc()
pool.free(obj)
# Slab allocator — kernel-grade
let slab = SlabAllocator.new([32, 64, 128, 256, 512, 1024])
let ptr = slab.alloc(100) # Gets 128-byte slab
Atomic Operations
import std/atomics
let counter = AtomicI32.new(0)
counter.fetch_add(1) # Thread-safe increment
counter.compare_and_swap(1, 2) # CAS operation
# Lock-free data structures
let stack = LockFreeStack()
stack.push(42)
let val = stack.pop() # 42 — no locks, no blocking
Hardware Access
import std/hardware
# Read CPU information
let cpu = CPUID.read()
print(cpu.vendor) # "GenuineIntel"
print(cpu.model_name) # "Intel Core i9-13900K"
print(cpu.cores) # 24
# Control registers (requires kernel mode)
let cr0 = ControlRegister.read_cr0()
let cr3 = ControlRegister.read_cr3() # Page table base
DMA (Direct Memory Access)
import std/dma
# Zero-copy I/O with DMA
let buf = DMABuffer.alloc(4096, "read")
buf.lock() # Pin in physical memory
# ... device reads directly into buf ...
buf.unlock()
let data = buf.read(0, 4096)
📘 Chapter 18: FFI & C Interop
Calling C Functions
import std/ffi
# Load a shared library
let libc = ffi.open("libc.so.6") # Linux
# let libc = ffi.open("msvcrt.dll") # Windows
# let libc = ffi.open("libSystem.dylib") # macOS
# Get function pointer
let printf = libc.symbol("printf")
# Call C function
ffi.call(printf, "Hello from C! %d\n", 42)
# Close library
libc.close()
C Memory Operations
# Allocate C memory
let ptr = ffi.malloc(256)
# Write data
ffi.poke(ptr, 0, 42) # Write 42 at offset 0
ffi.poke(ptr, 4, 100) # Write 100 at offset 4
# Read data
let val = ffi.peek(ptr, 0) # Read from offset 0 → 42
# Free memory
ffi.free(ptr)
C String Interop
# Convert Nyx string to C string
let c_str = ffi.to_c_string("Hello, C!")
# Pass to C function
ffi.call(puts, c_str)
# Convert back
let nyx_str = ffi.from_c_string(c_ptr)
📘 Chapter 19: Testing & Debugging
Writing Tests
import std/test
# Basic assertions
assert(1 + 1 == 2)
assert(true)
# Equality
eq(add(2, 3), 5)
neq(add(2, 3), 6)
# Approximate (for floating point)
approx(3.14159, 3.14, 0.01)
# Contains
contains_([1, 2, 3], 2)
# Test that errors are thrown
raises(fn() { divide(1, 0) })
# Test suites
fn test_math() {
eq(add(1, 2), 3)
eq(multiply(3, 4), 12)
eq(factorial(5), 120)
print("All math tests passed!")
}
fn test_strings() {
eq(upper("hello"), "HELLO")
eq(len("Nyx"), 3)
print("All string tests passed!")
}
test_math()
test_strings()
Runtime Debugging Flags
# Trace execution (see every step)
nyx --trace program.ny
# Debug mode (detailed error messages)
nyx --debug program.ny
# Step-through debugging
nyx --step program.ny
# Set breakpoints
nyx --break program.ny
# Parse only (syntax check, don't run)
nyx --parse-only program.ny
nyx --lint program.ny
# Memory profiling
nyx --profile-memory program.ny
nyx --max-alloc 100000 program.ny
# Limit execution
nyx --max-steps 1000000 program.ny
nyx --max-call-depth 100 program.ny
Benchmarking
import std/bench
bench("fibonacci", fn() {
fibonacci(30)
})
bench("sort 10000", fn() {
sort(range(10000))
})
📘 Chapter 20: File I/O & Networking
File Operations
import std/io
# Read entire file
let content = read_file("data.txt")
print(content)
# Read lines
let lines = read_lines("data.txt")
for line in lines {
print(line)
}
# Write file
write_file("output.txt", "Hello, World!")
# Append to file
append_file("log.txt", "New log entry\n")
# File operations
file_exists("data.txt") # true/false
file_size("data.txt") # bytes
copy_file("a.txt", "b.txt")
move_file("old.txt", "new.txt")
mkdir("new_directory")
JSON
import std/json
# Parse JSON string
let data = json.parse('{"name": "Nyx", "version": 6}')
print(data.name) # "Nyx"
print(data.version) # 6
# Create JSON string
let obj = {name: "Nyx", features: ["fast", "safe", "easy"]}
let json_str = json.stringify(obj)
print(json_str)
# {"name":"Nyx","features":["fast","safe","easy"]}
# Pretty print
let pretty = json.pretty(obj)
HTTP Client
import std/http
# GET request
let response = http.request({
method: "GET",
url: "https://api.example.com/data",
headers: {"Accept": "application/json"},
timeout: 5000
})
print(response.status) # 200
print(response.body)
# POST request
let post_response = http.request({
method: "POST",
url: "https://api.example.com/users",
headers: {"Content-Type": "application/json"},
body: json.stringify({name: "Nyx", type: "language"})
})
HTTP Server (Native — No Framework Needed!)
use nyhttpd
let server = nyhttpd.HttpServer.new({
port: 8080,
worker_threads: 4
})
# Define routes
server.get("/", fn(req, res) {
res.html("<h1>Welcome to Nyx!</h1>")
})
server.get("/api/hello", fn(req, res) {
res.json({message: "Hello from Nyx!", time: date.now()})
})
server.post("/api/echo", fn(req, res) {
res.json({echo: req.body})
})
print("Server running on http://localhost:8080")
server.start()
WebSocket
import std/socket
let ws = WebSocket.connect("ws://localhost:8080/ws")
ws.send("Hello, server!")
let msg = ws.receive()
print("Server says: " + msg)
ws.close()
� Chapter 21: Enums & Structs
Basic Enums
enum Direction {
North,
South,
East,
West
}
let heading = Direction.North
match heading {
case Direction.North => print("Going north!")
case Direction.South => print("Going south!")
case Direction.East => print("Going east!")
case Direction.West => print("Going west!")
}
Enums with Associated Data
enum Shape {
Circle(radius: float),
Rectangle(width: float, height: float),
Triangle(base: float, height: float),
Point
}
fn area(shape: Shape) -> float = match shape {
case Shape.Circle(r) => 3.14159 * r * r
case Shape.Rectangle(w, h) => w * h
case Shape.Triangle(b, h) => 0.5 * b * h
case Shape.Point => 0.0
}
let shapes = [
Shape.Circle(5.0),
Shape.Rectangle(4.0, 6.0),
Shape.Triangle(3.0, 8.0),
Shape.Point
]
for s in shapes {
print("Area: " + str(area(s)))
}
Structs
struct Point {
x: float,
y: float
}
struct Color {
r: u8,
g: u8,
b: u8,
a: u8 = 255 # Default value
}
let p = Point { x: 10.0, y: 20.0 }
let c = Color { r: 255, g: 128, b: 0 } # a defaults to 255
print(p.x) # 10.0
print(c.a) # 255
Struct Methods via impl
struct Vector2D {
x: float,
y: float
}
impl Vector2D {
fn new(x: float, y: float) -> Vector2D {
return Vector2D { x: x, y: y }
}
fn magnitude(self) -> float {
return sqrt(self.x * self.x + self.y * self.y)
}
fn normalize(self) -> Vector2D {
let mag = self.magnitude()
return Vector2D { x: self.x / mag, y: self.y / mag }
}
fn dot(self, other: Vector2D) -> float {
return self.x * other.x + self.y * other.y
}
fn add(self, other: Vector2D) -> Vector2D {
return Vector2D { x: self.x + other.x, y: self.y + other.y }
}
fn scale(self, factor: float) -> Vector2D {
return Vector2D { x: self.x * factor, y: self.y * factor }
}
}
let v1 = Vector2D.new(3.0, 4.0)
print(v1.magnitude()) # 5.0
print(v1.normalize()) # Vector2D { x: 0.6, y: 0.8 }
print(v1.dot(Vector2D.new(1.0, 0.0))) # 3.0
Type Aliases
type UserId = int
type Email = str
type Callback = fn(int) -> bool
type Matrix = [[float]]
type Result<T> = Ok(T) | Err(str)
📘 Chapter 22: Iterators & Generators
Generators with yield
fn fibonacci() {
let mut a = 0
let mut b = 1
loop {
yield a
let temp = a
a = b
b = temp + b
}
}
# First 10 fibonacci numbers
for i, fib in fibonacci() |> take(10) {
print(str(i) + ": " + str(fib))
}
Infinite Sequences
fn naturals(start = 0) {
let mut n = start
loop {
yield n
n = n + 1
}
}
fn primes() {
for n in naturals(2) {
let mut is_prime = true
for d in 2..n {
if n % d == 0 {
is_prime = false
break
}
}
if is_prime { yield n }
}
}
# First 20 primes
let first_20_primes = primes() |> take(20) |> collect()
print(first_20_primes)
# [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71]
Iterator Combinators
# Chain iterators
let combined = [1, 2, 3] |> chain([4, 5, 6]) |> collect()
# [1, 2, 3, 4, 5, 6]
# Zip two iterators
let pairs = [1, 2, 3] |> zip(["a", "b", "c"]) |> collect()
# [(1, "a"), (2, "b"), (3, "c")]
# Enumerate
let indexed = ["x", "y", "z"] |> enumerate() |> collect()
# [(0, "x"), (1, "y"), (2, "z")]
# Take / skip / step
let slice = 0..100 |> skip(10) |> take(5) |> collect()
# [10, 11, 12, 13, 14]
# Flat map
let words = ["hello world", "foo bar"]
|> flat_map(|s| s.split(" "))
|> collect()
# ["hello", "world", "foo", "bar"]
Declarative Macros
macro vec!(...items) {
let arr = []
for item in items {
push(arr, item)
}
arr
}
let v = vec!(1, 2, 3, 4, 5)
print(v) # [1, 2, 3, 4, 5]
Code Generation Macros
macro derive_debug!(T) {
impl T {
fn debug(self) -> str {
let fields = reflect(T).fields()
let parts = []
for f in fields {
push(parts, f.name + ": " + str(self[f.name]))
}
return type_name(T) + " { " + join(parts, ", ") + " }"
}
}
}
@derive_debug!
struct User {
name: str,
age: int,
email: str
}
let u = User { name: "Nyx", age: 1, email: "nyx@lang.dev" }
print(u.debug())
# User { name: Nyx, age: 1, email: nyx@lang.dev }
Hygienic Macros
Nyx macros are hygienic — variables inside a macro don't leak into surrounding scope:
macro swap!(a, b) {
let temp = a
a = b
b = temp
}
let mut x = 10
let mut y = 20
swap!(x, y)
print(x) # 20
print(y) # 10
# 'temp' does NOT exist here — it's scoped to the macro
📘 Chapter 24: Compile-Time Computation (comptime)
Comptime Functions
import std/comptime
# Computed at compile time, not runtime
comptime fn fibonacci(n: int) -> int {
if n <= 1 { return n }
return fibonacci(n - 1) + fibonacci(n - 2)
}
# This is a constant — computed during compilation
const FIB_20 = fibonacci(20) # 6765, computed at compile time
# Generate lookup tables at compile time
comptime fn generate_lookup_table(size: int) -> [int] {
let table = []
for i in 0..size {
push(table, i * i)
}
return table
}
const SQUARES = generate_lookup_table(256)
Static Assertions
# Checked at compile time
static_assert(sizeof(int) == 8, "int must be 8 bytes")
static_assert(alignof(float) >= 4, "float must be 4-byte aligned")
comptime fn assert_size(T: type, expected: int) {
static_assert(sizeof(T) == expected)
}
Compile-Time Code Generation
import std/comptime
comptime fn generate_accessors(T: type) {
let fields = reflect(T).fields()
for f in fields {
# Generate getter
generate_code("fn get_" + f.name + "(self) -> " + f.type_name + " { return self." + f.name + " }")
# Generate setter
generate_code("fn set_" + f.name + "(self, val: " + f.type_name + ") { self." + f.name + " = val }")
}
}
comptime fn generate_builder(T: type) {
# Auto-generates a Builder pattern for any struct
let fields = reflect(T).fields()
generate_code("class " + type_name(T) + "Builder {")
for f in fields {
generate_code(" " + f.name + ": Option<" + f.type_name + "> = None")
}
for f in fields {
generate_code(" fn " + f.name + "(self, val: " + f.type_name + ") -> Self { self." + f.name + " = Some(val); return self }")
}
generate_code(" fn build(self) -> " + type_name(T) + " { ... }")
generate_code("}")
}
Compile-Time Reflection
import std/comptime
# Inspect any type at compile time
let info = Reflect.new(MyStruct)
print(info.type_name()) # "MyStruct"
print(info.type_size()) # 24
print(info.type_align()) # 8
print(info.fields()) # [FieldInfo { name: "x", ... }, ...]
print(info.methods()) # [MethodInfo { name: "new", ... }, ...]
print(info.traits()) # [TraitInfo { name: "Debug", ... }, ...]
print(info.implements_trait("Debug")) # true
📘 Chapter 25: Advanced Type System
Dependent Types
import std/types_advanced
# Vec with compile-time-known length
let v3: Vec<float, 3> = Vec.new() # A vector that MUST have exactly 3 elements
v3.push(1.0)
v3.push(2.0)
v3.push(3.0)
# v3.push(4.0) # COMPILE ERROR: Vec<float, 3> is full
# Append preserves length information
let v5: Vec<float, 5> = v3.append(Vec.from([4.0, 5.0])) # 3 + 2 = 5
Refinement Types
import std/types_advanced
# A type that only accepts positive numbers
struct Positive {
fn check(value: int) -> bool = value > 0
}
let x: Refined<int, Positive> = Refined.new(42) # OK
# let y: Refined<int, Positive> = Refined.new(-1) # COMPILE ERROR
# Use as regular int
let val = x.get() # 42
Union Types
type StringOrInt = str | int
type Primitive = int | float | bool | str | null
fn process(value: StringOrInt) {
match value {
case s: str => print("String: " + s)
case n: int => print("Number: " + str(n))
}
}
Generic Constraints
trait Hashable {
fn hash(self) -> int
}
trait Comparable {
fn compare(self, other: Self) -> int
}
# Generic with multiple constraints
fn sorted_unique<T: Hashable + Comparable>(items: [T]) -> [T] {
return items |> unique() |> sort()
}
# Where clause for complex constraints
fn merge<K, V>(a: Map<K, V>, b: Map<K, V>) -> Map<K, V>
where K: Hashable + Comparable,
V: Clone
{
let result = a.clone()
for (k, v) in b.items() {
result[k] = v.clone()
}
return result
}
Higher-Kinded Types
# F is a type constructor (like Array, Option, Result...)
trait Functor<F> {
fn map<A, B>(self: F<A>, f: fn(A) -> B) -> F<B>
}
trait Monad<M> extends Functor<M> {
fn pure<A>(value: A) -> M<A>
fn flat_map<A, B>(self: M<A>, f: fn(A) -> M<B>) -> M<B>
}
Optional Type (T? syntax)
# Nullable type shorthand
let name: str? = get_user_name() # Might be null
# Safe navigation
let city = user?.address?.city # Returns null if any part is null
# Null coalescing
let display_name = name ?? "Anonymous"
# Unwrap with default
let age = user?.age.unwrap_or(0)
📘 Chapter 26: Decorators & Attributes
Function Decorators
@deprecated("Use new_function instead")
fn old_function() {
# ...
}
@inline
fn fast_add(a: int, b: int) -> int = a + b
@test
fn test_addition() {
eq(fast_add(2, 3), 5)
}
# Custom decorator
fn memoize(func) {
let mut cache = {}
return fn(...args) {
let key = str(args)
if !has(cache, key) {
cache[key] = func(...args)
}
return cache[key]
}
}
@memoize
fn expensive_computation(n) {
# Only computed once per unique n
return fibonacci(n)
}
Struct/Class Attributes
@derive(Debug, Clone, PartialEq)
struct Config {
host: str,
port: int,
max_connections: int = 100
}
@serializable
@validate
class User {
@required
name: str
@required @format("email")
email: str
@min(0) @max(150)
age: int
}
📘 Chapter 27: Unsafe & Raw Memory
Unsafe Blocks
import std/systems_extended
# Unsafe code is explicitly marked
unsafe {
let ptr = _sys_malloc(256)
_sys_memset(ptr, 0, 256)
_sys_poke(ptr, 0, 42)
let val = _sys_peek(ptr, 0)
print(val) # 42
_sys_free(ptr)
}
Raw Pointer Operations
import std/systems_extended
unsafe {
# Allocate
let ptr = _sys_malloc(1024)
let zeroed = _sys_calloc(256, 4) # 256 × 4 bytes, zeroed
let resized = _sys_realloc(ptr, 2048)
# Copy/move memory
_sys_memcpy(dest, src, 512)
_sys_memmove(dest, src, 512) # Handles overlapping
# Compare memory
let cmp = _sys_memcmp(a, b, 64) # 0 if equal
# Volatile reads/writes (won't be optimized away)
let val = _sys_volatile_read(mmio_addr)
_sys_volatile_write(mmio_addr, 0xFF)
# Cache control
_sys_cache_flush(ptr, 64)
_sys_cache_invalidate(ptr, 64)
_sys_prefetch(ptr)
}
Bit Manipulation
import std/systems_extended
bit_set(value, 3) # Set bit 3
bit_clear(value, 5) # Clear bit 5
bit_toggle(value, 0) # Toggle bit 0
bit_test(value, 7) # Test bit 7 → true/false
let mask = bit_mask(4, 8) # Mask bits 4-8
let extracted = bit_extract_bits(value, 4, 8) # Extract bits 4-8
popcount(0xFF) # 8 — count set bits
leading_zeros(0x0F) # 28 — count leading zeros
trailing_zeros(0xF0) # 4 — count trailing zeros
import std/systems_extended
let platform = get_platform() # "windows", "linux", "macos"
let arch = get_arch() # "x86_64", "aarch64", "riscv64"
📘 Chapter 28: Cryptography
Hashing (20+ Algorithms — All Built-in)
import std/crypto
# Common hash functions
let h1 = sha256("hello world")
let h2 = sha512("hello world")
let h3 = sha3_256("hello world")
let h4 = blake2b("hello world")
let h5 = blake3("hello world")
let h6 = md5("hello world")
let h7 = sha1("hello world")
# Fast non-crypto hashes
let h8 = fnv1a32("hello")
let h9 = fnv1a64("hello")
let h10 = djb2("hello")
let h11 = crc32("hello")
let h12 = crc32_fast("hello") # Hardware-accelerated
let h13 = murmur3_32("hello", 42)
let h14 = murmur3_128("hello", 42)
# Combine hashes
let combined = hash_combine(h8, h9)
Ciphers
import std/crypto
# XOR encryption
let encrypted = xor_encrypt("secret message", "mykey")
let decrypted = xor_decrypt(encrypted, "mykey")
# Caesar cipher
let shifted = caesar_cipher("HELLO", 3) # "KHOOR"
# Vigenère cipher
let vig_enc = vigenere_encrypt("ATTACKATDAWN", "LEMON")
let vig_dec = vigenere_decrypt(vig_enc, "LEMON")
# ROT13
let rotated = rot13("Hello World")
Hardware-Accelerated Crypto (AES-NI)
import std/crypto_hw
# Check CPU support
if AES_NI.is_supported() {
# AES-128 CBC encryption (hardware-accelerated)
let key = [0x2b, 0x7e, 0x15, 0x16, ...] # 16 bytes
let iv = [0x00, 0x01, 0x02, 0x03, ...] # 16 bytes
let plaintext = [0x6b, 0xc1, ...]
let ciphertext = AES_NI.encrypt_cbc(plaintext, key, iv)
let decrypted = AES_NI.decrypt_cbc(ciphertext, key, iv)
# AES-GCM (authenticated encryption)
let result = AES_NI.encrypt_gcm(plaintext, key, iv, aad)
# result contains ciphertext + authentication tag
}
# Hardware SHA
if SHA_EXT.is_supported() {
let digest = SHA_EXT.sha256(data) # Hardware-accelerated SHA-256
let sha1_digest = SHA_EXT.sha1(data)
}
# Hardware CRC32C
if CRC32C.is_supported() {
let checksum = CRC32C.crc32_u32(0, data)
}
📘 Chapter 29: Database & Storage
Key-Value Store
import std/database
let store = KVStore()
store.put("user:1", {name: "Alice", age: 30})
store.put("user:2", {name: "Bob", age: 25})
let user = store.get("user:1")
print(user.name) # "Alice"
# Query
let young_users = store.query(|k, v| v.age < 28)
store.create_index("age")
File-Backed Persistent Store
import std/database
let db = FileKVStore("mydata.json")
db.load()
db.put("config", {theme: "dark", lang: "nyx"})
db.save()
# Data persists across restarts
Relational Tables
import std/database
let db = Database()
let users = db.create_table("users", {
id: "int",
name: "str",
email: "str",
age: "int"
})
users.insert({id: 1, name: "Alice", age: 30, email: "alice@example.com"})
users.insert({id: 2, name: "Bob", age: 25, email: "bob@example.com"})
users.insert({id: 3, name: "Charlie", age: 35, email: "charlie@example.com"})
# Queries
let adults = users.select(|row| row.age >= 18)
let names = users.select(|row| true) |> map(|row| row.name)
# Aggregations
let avg_age = users.avg("age")
let total_users = users.count()
let oldest = users.max("age")
# Order and limit
let top_3 = users.order_by("age").limit(3)
# Group by
let by_age = users.group_by("age")
# Create index for fast lookups
users.create_index("email")
Document Store (NoSQL)
import std/database
let docs = DocumentStore()
docs.insert({name: "Nyx", type: "language", version: 6})
docs.insert({name: "Python", type: "language", version: 3})
let languages = docs.find({type: "language"})
let nyx = docs.find_one({name: "Nyx"})
docs.create_index("name")
let fast_lookup = docs.find_by_index("name", "Nyx")
Redis Client
import std/redis
let client = RedisClient("localhost", 6379)
client.connect()
# String operations
client.set("greeting", "Hello, Nyx!")
let val = client.get("greeting")
# List operations
client.lpush("queue", "task1")
client.lpush("queue", "task2")
let task = client.rpop("queue")
# Hash operations
client.hset("user:1", "name", "Alice")
client.hset("user:1", "age", "30")
let name = client.hget("user:1", "name")
# Set operations
client.sadd("tags", "nyx")
client.sadd("tags", "language")
let members = client.smembers("tags")
# Pub/Sub
client.subscribe("events")
client.publish("events", "user_logged_in")
client.disconnect()
Caching
import std/cache
# LRU Cache (Least Recently Used)
let cache = LRUCache(1000, 3600) # 1000 max entries, 1 hour TTL
cache.set("user:1", {name: "Alice"})
let user = cache.get("user:1") # Cache hit
# Check stats
let stats = cache.stats()
print(stats.hits)
print(stats.misses)
print(stats.hit_rate)
# LFU Cache (Least Frequently Used)
let lfu = LFUCache(500, 1800)
lfu.set("hot_data", compute_expensive())
📘 Chapter 30: GUI & Desktop Applications
Creating a Window
import std/gui
let app = Application()
let window = Window("My Nyx App", 800, 600)
window.resizable(true)
window.minsize(400, 300)
# Add widgets
window.add_label("title", "Welcome to Nyx!", {font_size: 24, bold: true})
window.add_button("click_me", "Click Me!", fn() {
print("Button clicked!")
})
# Text input
window.add_entry("name_input", {placeholder: "Enter your name..."})
window.add_button("submit", "Submit", fn() {
let name = window.get_value("name_input")
window.update_label("title", "Hello, " + name + "!")
})
# Menu bar
window.create_menu("File", [
{label: "New", shortcut: "Ctrl+N", action: fn() { new_file() }},
{label: "Open", shortcut: "Ctrl+O", action: fn() { open_file() }},
{label: "Save", shortcut: "Ctrl+S", action: fn() { save_file() }},
{label: "---"}, # Separator
{label: "Exit", action: fn() { app.quit() }}
])
# Toolbar
window.create_toolbar([
{icon: "new", tooltip: "New File", action: new_file},
{icon: "open", tooltip: "Open File", action: open_file},
{icon: "save", tooltip: "Save File", action: save_file}
])
# Status bar
window.create_statusbar("Ready")
# Event binding
window.bind("resize", fn(event) {
print("Window resized to " + str(event.width) + "x" + str(event.height))
})
# Timers
window.set_timer(1000, fn() {
window.update_statusbar("Updated: " + str(time.now()))
})
app.run(window)
Game Development
import std/game
# Full game engine — colors, keys, surfaces, clock
let BLACK = game.BLACK
let WHITE = game.WHITE
let RED = game.RED
let game_window = Game("My Nyx Game", 800, 600)
let clock = Clock()
class Player {
fn init(self) {
self.x = 400
self.y = 300
self.speed = 5
self.health = 100
}
fn update(self, keys) {
if keys[game.K_w] { self.y = self.y - self.speed }
if keys[game.K_s] { self.y = self.y + self.speed }
if keys[game.K_a] { self.x = self.x - self.speed }
if keys[game.K_d] { self.x = self.x + self.speed }
self.x = clamp(self.x, 0, 800)
self.y = clamp(self.y, 0, 600)
}
fn draw(self, surface) {
surface.fill_rect(self.x - 16, self.y - 16, 32, 32, RED)
}
}
let player = Player()
# Game loop
game_window.run(fn(surface, events, keys) {
surface.fill(BLACK)
player.update(keys)
player.draw(surface)
clock.tick(60) # 60 FPS
})
Data Visualization
import std/visualize
# 140+ named colors, 8 palettes, 12 gradient maps
# Full matplotlib/plotly-equivalent plotting
let data = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
let chart = BarChart(data, {
title: "Squares",
x_label: "n",
y_label: "n²",
color_palette: "viridis"
})
chart.save("squares.png")
📚 Standard Library (109 Modules — Full API Reference)
All 109 modules are native and free to use. No nypm install needed. Just import and go.
Core Modules
types — import std/types
| Function |
Description |
type_of(value) |
Returns type name as string |
is_int(value) |
Check if integer |
is_bool(value) |
Check if boolean |
is_string(value) |
Check if string |
is_array(value) |
Check if array |
is_function(value) |
Check if function |
is_null(value) |
Check if null |
class — import std/class
| Function |
Description |
object_new() |
Create empty object |
object_set(obj, key, value) |
Set property |
object_get(obj, key) |
Get property |
class_new(name) |
Create class |
class_with_ctor(name, ctor) |
Class with constructor |
class_set_method(cls, name, fn) |
Add method |
class_name(cls) |
Get class name |
class_instantiate0/1/2(cls, ...) |
Create instance |
class_call0/1/2(obj, method, ...) |
Call method |
ffi — import std/ffi — Foreign Function Interface
| Function |
Description |
open(lib_path) |
Load shared library (.so/.dll/.dylib) |
close(lib) |
Unload library |
symbol(lib, name) |
Get function pointer |
call(fn_ptr, ret_type, ...args) |
Call foreign function |
call_with_types(fn_ptr, ret, arg_types, ...args) |
Call with explicit types |
malloc(size) |
Allocate C memory |
free(ptr) |
Free C memory |
to_c_string(str) |
Convert to C string |
from_c_string(ptr) |
Convert from C string |
to_c_array(arr, type) |
Convert to C array |
type_size(type) |
Get type size in bytes |
peek(ptr, type) |
Read from pointer |
poke(ptr, value, type) |
Write to pointer |
address_of(value) |
Get address |
ptr_add(ptr, offset) |
Pointer arithmetic |
as_void_ptr(ptr) |
Cast to void* |
Classes: Library, CFunction, CType, CStruct, Callback, VariadicFunction, VTable, CUnion, BitField, PackedStruct, CallbackTrampoline, LazySymbol, LibraryCache, OpaquePtr, ArrayMarshaller
Type Constants: TYPE_VOID, TYPE_INT8/16/32/64, TYPE_UINT8/16/32/64, TYPE_FLOAT32/64, TYPE_POINTER, TYPE_STRING
c — import std/c — Direct C Interop
| Class |
Key Methods |
CLibrary(path) |
.func(name, ret, args), .var(name, type), .close() |
CFunction(lib, name, ret, args) |
.call(...args) |
CVar(lib, name, type) |
.get(), .set(value) |
Struct(name) |
.add(field, type), .create(), .size_of() |
CStructInstance |
.get(field), .set(field, value), .address(), .free() |
CCallback(fn, ret, args) |
.call(...), .free() |
Type Constants: C_CHAR, C_SHORT, C_INT, C_LONG, C_LONGLONG, C_FLOAT, C_DOUBLE, C_POINTER, C_SIZE_T, C_SIGNED, C_UNSIGNED
Math & Science
math — import std/math — 100+ Functions
22 Constants: PI, E, TAU, INF, NAN, PHI (golden ratio), SQRT2, SQRT3, LN2, LN10, LOG2E, LOG10E, EULER_GAMMA, CATALAN, GLAISHER_KINKELIN, APERY, KHINCHIN, FRANSEN_ROBINSON, MEISSEL_MERTENS, BERNSTEIN, GAUSS_CONSTANT, LEMNISCATE
| Category |
Functions |
| Basic |
abs, min, max, clamp, sign |
| Rounding |
floor, ceil, round, round_n, trunc |
| Powers/Roots |
sqrt, cbrt, pow, ipow, hypot, hypot3, hypot_n |
| Logarithms |
log, log2, log10, logb, exp, expm1, log1p |
| Trigonometry |
sin, cos, tan, cot, sec, csc |
| Inverse Trig |
asin, acos, atan, atan2 |
| Hyperbolic |
sinh, cosh, tanh, asinh, acosh, atanh |
| Angle Conv. |
radians, degrees |
| Number Theory |
gcd, lcm, factorial, fibonacci, is_prime, prime_sieve |
| Special |
gamma, lgamma, beta, erf, erfc, zeta |
science — import std/science — Scientific Computing
| Class |
Key Methods |
Vector(data) |
.len(), .get/set(idx), .add/sub/scale/dot/cross(other), .magnitude(), .normalize(), .angle(other), .distance(other), .project_onto(other), .to_array() |
Matrix(rows, cols, data) |
.get/set(r,c), .row(...), .multiply(...), .determinant(), .inverse(), .transpose() |
Constructors: vector(data), zeros(n), ones(n), random_vector(n)
symbolic — import std/symbolic — Computer Algebra
| Class |
Key Methods |
Symbol(name) |
.simplify(), .expand(), .factor(), .collect(var), .subs(old, new), .diff(var), .integrate(var), .evalf(precision) |
| Checks |
.is_number(), .is_symbol(), .is_constant(), .is_real(), .is_complex(), .is_positive(), .is_negative(), .is_integer(), .is_even(), .is_odd(), .is_prime(), .is_finite(), .is_infinite(), .is_zero() |
| Operators |
Add, Sub, Mul, Div, Pow — expression tree nodes |
import std/symbolic
let x = Symbol("x")
let expr = x**2 + 2*x + 1
print(expr.factor()) # (x + 1)^2
print(expr.diff(x)) # 2*x + 2
print(expr.integrate(x)) # x^3/3 + x^2 + x
print(expr.subs(x, 3)) # 16
fft — import std/fft — Fast Fourier Transform
| Function |
Description |
complex(re, im) |
Create complex number |
c_add/sub/mul/scale/abs/conj/div(...) |
Complex arithmetic |
fft(signal) / ifft(signal) |
Forward/inverse FFT |
rfft(signal) / irfft(signal) |
Real-valued FFT |
fft2d(matrix) / ifft2d(matrix) |
2D FFT |
power_spectrum(signal) |
Power spectral density |
magnitude_spectrum(signal) |
Magnitude |
phase_spectrum(signal) |
Phase |
hann_window/hamming_window/blackman_window(n) |
Window functions |
apply_window(signal, window) |
Apply window |
autocorrelation(signal) |
Auto-correlation |
convolve(a, b) |
Convolution |
xcorr(a, b) |
Cross-correlation |
find_peaks(spectrum) |
Peak detection |
filtfilt/filter(b, a, x) |
Digital filtering |
dct/idct(signal) |
Discrete cosine transform |
blas — import std/blas — Linear Algebra (BLAS/LAPACK)
| Level |
Functions |
| BLAS Level 1 |
dot(x,y), nrm2(x), asum(x), iamax(x), scal(alpha,x), axpy(alpha,x,y) |
| BLAS Level 2 |
gemv(alpha,A,x,beta,y), symv(alpha,A,x,beta,y) |
| BLAS Level 3 |
gemm(alpha,A,B,beta,C), symm(...), trsm(...) |
| LAPACK |
getrf(A) (LU), gesv(A,B) (solve), getri(A) (inverse), syev(A) (eigenvalues), gesdd(A) (SVD), geqrf(A) (QR), potrf(A) (Cholesky), gecon(A) (condition number) |
precision — import std/precision — Arbitrary Precision
| Function |
Description |
bigfloat_from_str(value) |
Parse big float from string |
bigfloat_from_int(value) |
Create from integer |
bigfloat_from_float(value) |
Create from float |
bigfloat_to_string(bf) |
Convert to string |
bigfloat_add(a, b) |
Add big floats |
bigfloat_mul(a, b) |
Multiply big floats |
bigfloat_div(a, b) |
Divide big floats |
Struct: BigFloat(mantissa, exponent, precision, sign)
sparse — import std/sparse — Sparse Matrices
| Function |
Description |
csr_from_dense(matrix) |
Create CSR from dense matrix |
coo_from_dense(matrix) |
Create COO from dense matrix |
coo_to_csr(coo) |
Convert COO to CSR |
csr_get(csr, r, c) |
Get element |
csr_mv(csr, vec) |
Sparse matrix-vector multiply |
csr_mm(a, b) |
Sparse matrix-matrix multiply |
Formats: CSR, CSC, COO, LIL
Data Structures
algorithm — import std/algorithm — 23 Functions
| Function |
Description |
sort(arr) |
Sort array |
sort_with(arr, compare_fn) |
Sort with comparator |
find(arr, value) |
Find value |
find_if(arr, predicate) |
Find by predicate |
binary_search(arr, value) |
Binary search |
filter(arr, predicate) |
Filter |
map(arr, transform) |
Transform |
reduce(arr, initial, reducer) |
Fold/reduce |
union(a, b) |
Set union |
intersection(a, b) |
Set intersection |
difference(a, b) |
Set difference |
flatten(arr) |
Flatten nested |
chunk(arr, size) |
Split into chunks |
zip(a, b) |
Zip two arrays |
any(arr, predicate) |
Any match |
all(arr, predicate) |
All match |
none(arr, predicate) |
None match |
count(arr, predicate) |
Count matches |
shuffle(arr) |
Random shuffle |
sample(arr, n) |
Random sample |
unique(arr) |
Unique elements |
unique_with(arr, key_fn) |
Unique by key |
reverse(arr) |
Reverse |
collections — import std/collections — Advanced Data Structures
| Class |
Key Methods |
LinkedList |
.append(val), .prepend(val), .insert(idx, val), .remove(idx), .get(idx), .set(idx, val), .contains(val), .index_of(val), .clear(), .is_empty(), .to_list(), .reverse(), .for_each(fn), .map(fn), .filter(fn), .reduce(init, fn) |
CircularLinkedList |
Same as LinkedList |
BinarySearchTree |
.insert(key, val), .remove(key), AVL rotations, self-balancing |
TreeNode |
.left, .right, .key, .value, .height |
string — import std/string
| Function |
Description |
upper(s) / lower(s) |
Case conversion |
capitalize(s) / title(s) |
Capitalize first/each word |
swapcase(s) |
Swap upper/lower |
strip/lstrip/rstrip(s) |
Trim whitespace |
strip_chars(s, chars) |
Trim specific chars |
split(s, sep) |
Split by separator |
join(arr, sep) |
Join array to string |
replace(s, old, new) |
Replace substring |
contains(s, sub) |
Check substring |
starts_with/ends_with(s, prefix) |
prefix/suffix check |
index_of(s, sub) |
Find position |
count(s, sub) |
Count occurrences |
repeat(s, n) |
Repeat string |
pad_left/pad_right(s, width, char) |
Padding |
center(s, width, char) |
Center text |
is_digit/alpha/alnum/space/upper/lower(s) |
Character tests |
encode/decode(s, encoding) |
Encoding conversion |
| Unicode, regex, fuzzy matching, Levenshtein distance available |
|
I/O & Serialization
io — import std/io — File I/O (25+ functions)
| Function |
Description |
read_file(path) |
Read entire file |
read_lines(path) |
Read as line array |
read_nlines(path, n) |
Read first N lines |
write_file(path, data) |
Write file |
append_file(path, data) |
Append to file |
file_exists(path) |
Check existence |
file_size(path) |
Get size in bytes |
copy_file(src, dst) |
Copy file |
move_file(src, dst) |
Move/rename file |
delete_file(path) |
Delete file |
mkdir(path) / mkdir_p(path) |
Create directory (recursive) |
list_dir(path) |
List directory contents |
file_ext/file_stem(path) |
Get extension/stem |
dirname/basename(path) |
Path components |
join_path(a, b) |
Join paths |
normalize_path(path) |
Normalize path |
abs_path(path) |
Absolute path |
file_info(path) |
Full file info |
is_absolute(path) |
Check if absolute |
rel_path(path, base) |
Relative path |
| Class |
Key Methods |
File(path, mode) |
.read(n), .read_line(), .read_lines(), .write(data), .write_line(line), .seek(pos), .tell(), .flush(), .close(), .is_closed(), .is_eof() |
BufferedWriter(file, buf_size) |
.write(data), .flush(), .close() |
BufferedReader(file, buf_size) |
.read(n), .read_line(), .close() |
FileWatcher(path) |
.has_changed(), .wait_for_change() |
TempFile(prefix) |
.write(data), .read(), .close(), .unlink() |
DirectoryWalker(path) |
.walk() — recursive walk |
json — import std/json
| Function |
Description |
parse(str) |
Parse JSON string to object |
stringify(value) |
Convert to JSON string |
pretty(value, indent) |
Pretty-print JSON |
xml — import std/xml
| Class |
Key Methods |
XMLParser |
.parse(xmlString) → XMLDocument |
XMLDocument |
DOM access |
XMLElement(name, attrs, children) |
.get_attr(), .find_all(), .text() |
XMLText(text) |
Text node |
Node Types: ELEMENT_NODE, TEXT_NODE, CDATA_NODE, COMMENT_NODE, DOCUMENT_NODE, DOCUMENT_TYPE_NODE, PROCESSING_INSTRUCTION_NODE
compress — import std/compress
| Function |
Description |
gzip_compress(data) |
Gzip compress |
gzip_decompress(data) |
Gzip decompress |
zip_create(entries) |
Create ZIP archive |
tar_create(entries) |
Create TAR archive |
lz77_compress(data) |
LZ77 compress |
lz77_decompress(data) |
LZ77 decompress |
config — import std/config — Multi-format Config
| Function |
Description |
parse_toml/yaml/ini(str) |
Parse config string |
load_toml/yaml/ini(path) |
Load from file |
save_toml/yaml/ini(config, path) |
Save to file |
to_toml/yaml/ini(config) |
Convert to string |
get(config, key) |
Get value |
get_nested(config, key) |
Dot-notation access |
get_string/int/float/bool/table/list(config, key) |
Typed access |
serialization — import std/serialization
Formats: FORMAT_BINARY, FORMAT_MSGPACK, FORMAT_PROTOBUF, FORMAT_JSON, FORMAT_CBOR, FORMAT_UBJSON
| Class |
Methods |
BinarySerializer |
.serialize(value), .deserialize(data) |
regex — import std/regex — Regular Expressions
| Function |
Description |
compile(pattern) |
Compile regex pattern |
match(regex, text) |
Match against text |
match_text(regex, text) |
Full text match |
find_all(regex, text) |
Find all matches |
groups(match) |
Get capture groups |
replace_all(regex, text, replacement) |
Replace all matches |
validator — import std/validator — Data Validation
| Class |
Methods |
Validator |
.validate(value), .addRule(field, rule), .clearRules() |
SchemaValidator(schema) |
.validate(value) — JSON Schema-like validation |
ValidationResult |
.isValid(), .hasErrors(), .getErrors(), .getErrorMessages() |
Built-in Formats: email, url, date, datetime, time, ip, ipv4, ipv6, uuid, phone, credit_card, isbn, hex_color
Networking
http — import std/http
| Function |
Description |
request(url, options, callback) |
Make HTTP request |
get(url, callback) |
GET request |
parse_url(url) |
Parse URL |
parse_http_response(raw) |
Parse raw response |
web — import std/web — Web Framework
| Class |
Methods |
Router |
.get/post/put/delete/patch(path, handler), .use(middleware), .group(prefix, routes_fn) |
Request |
.method, .path, .query, .headers, .body, .params, .cookies, .json, .header/param/query_param/cookie(name), .is_json(), .is_html() |
Response |
.status(code), .header(name, value), .json(data), .html(content), .text(content) |
socket — import std/socket — TCP/UDP/WebSocket
| Class |
Methods |
TCPSocket |
.connect(host, port), .send(data), .recv(size), .close(), .is_connected() |
TCPServer(port) |
.accept(), .close(), .is_running(), .set_reuse_addr(flag) |
UDPSocket |
.bind(host, port), .send_to(data, host, port), .recv_from(size), .close() |
WebSocket |
.connect(url), .send(msg), .receive(), .close() |
Functions: socket(family, type), server(port), accept(server), connect(host, port), send(sock, data), recv(sock, size), close(sock), set_nonblocking/set_blocking(sock)
network — import std/network — Advanced Networking
| Class |
Methods |
IPAddress(addr) |
.isIPv4/IPv6(), .toString(), .toBytes(), .isLoopback/Private/Multicast() |
NetworkInterface(name) |
.getIPv4/IPv6Address() |
Socket(af, type) |
Full POSIX socket API + state management |
ServerSocket(af) |
.bind/listen/accept/close(...), .setBacklog/KeepAlive/NoDelay(...) |
DNSResolver |
.resolve/resolveA/AAAA/CNAME/MX/NS/TXT/SRV(hostname), .reverseLookup(ip), .clearCache() |
Protocols: TCP, UDP, HTTP, HTTPS, FTP, SMTP, POP3, IMAP, SSH, WS, WSS
DNS Record Types: A, AAAA, CNAME, MX, NS, TXT, SRV, SOA, PTR
Async & Concurrency
async — import std/async
| Class |
Methods |
EventLoop |
.run(), .run_until_complete(future), .create_task(coro), .schedule(callback), .stop() |
Future |
.then(cb), .catch(cb), .finally(cb), .resolve(value), .reject(error) |
Promise |
.resolve(value), .reject(error), .future() |
Task |
.cancel(), .done(), .result(), .add_done_callback(cb) |
Semaphore(max) |
.acquire(), .release() |
Lock |
.acquire(), .release() |
Condition |
.wait(), .notify(), .notify_all() |
| Function |
Description |
get_event_loop() |
Get current loop |
create_task(coro) |
Create background task |
async_sleep(seconds) |
Non-blocking sleep |
gather(...futures) |
Wait for all |
race(...futures) |
Wait for first |
any(...futures) |
Wait for any success |
all_settled(...futures) |
Wait for all to settle |
async_runtime — import std/async_runtime — Work-Stealing Runtime
| Type |
Description |
Future<T> (trait) |
.poll(cx) → Poll<T> |
Poll<T> (enum) |
Ready(T) or Pending |
Executor (trait) |
.spawn(future), .block_on(future) |
WorkStealingExecutor |
Multi-threaded work-stealing scheduler |
AsyncTcpStream |
Async TCP stream |
Reactor |
Async I/O reactor |
Cryptography
crypto — import std/crypto — 30+ Hash/Cipher Functions
Hash Functions:
| Function | Description |
|----------|-------------|
| sha1/256/384/512(data) | SHA family |
| sha3_224/256/384/512(data) | SHA-3 family |
| blake2b/2s/3(data) | BLAKE family |
| md5(data) | MD5 |
| whirlpool(data) | Whirlpool |
| tiger(data) | Tiger hashing |
| fnv1a32/64(data) | FNV-1a (fast) |
| djb2(data) | DJB2 (fast) |
| crc32/crc32_fast/crc16/crc64_iso(data) | CRC checksums |
| murmur3_32/128(data, seed) | MurmurHash3 |
| hash_range(data, algo) | Hash with named algorithm |
| hash_combine(h1, h2) | Combine hashes |
Ciphers:
| Function | Description |
|----------|-------------|
| xor_encrypt/decrypt(data, key) | XOR cipher |
| rot13(data) | ROT13 |
| caesar_cipher(data, shift) | Caesar cipher |
| vigenere_encrypt/decrypt(data, key) | Vigenère cipher |
| substitution_encrypt(data, key) | Substitution cipher |
jwt — import std/jwt — JSON Web Tokens
Algorithms: HS256/384/512, RS256/384/512, ES256/384/512, PS256/384/512
| Class |
Methods |
JWTHeader(alg) |
.toJSON(), .toBase64URL(), .fromJSON(), .fromBase64URL() |
JWTPayload |
.setIssuer/getIssuer(...), .setSubject/getSubject(...), .setAudience/getAudience(...), .setExpiration/getExpiration(...), .setNotBefore/getNotBefore(...), .setIssuedAt/getIssuedAt(...), .setJWTID/getJWTID(...), .setClaim/getClaim/hasClaim/removeClaim(...) |
crypto_hw — import std/crypto_hw — Hardware-Accelerated
| Class |
Key Methods |
AES_NI |
.is_supported(), .encrypt/decrypt_block_128(...), .expand_key_128(key), .encrypt/decrypt_cbc(...), .encrypt_gcm(...), .ghash(...) |
SHA_EXT |
.is_supported(), .sha1(data), .sha256(data) |
CRC32C |
.is_supported(), .crc32_u8/u32(...) |
Database
database — import std/database — Full Database Engine
| Class |
Methods |
KVStore |
.put(k,v), .get(k), .delete(k), .has(k), .keys(), .values(), .items(), .clear(), .size(), .create_index(field), .query(predicate) |
FileKVStore(path) |
.load(), .save(), .put/get/delete(...) |
Table(name, schema) |
.insert(row), .select(where), .update(where, set), .delete(where), .create_index(col), .join(other, on), .group_by(col), .order_by(col), .limit(n), .count/sum/avg/min/max(col) |
Database |
.create_table(name, schema), .table(name), .drop_table(name), .tables() |
DocumentStore |
.insert(doc), .find(query), .find_one(query), .update(query, update), .delete(query), .create_index(field), .find_by_index(field, value), .count() |
redis — import std/redis — Full Redis Client
RedisClient(host, port) — .connect(), .disconnect(), .isConnected() + all Redis commands:
String: .get(key), .set(key, value, ...), .mget(keys), .mset(pairs), .incr/decr(key), .append(key, value)
List: .lpush/rpush(key, value), .lpop/rpop(key), .lrange(key, start, stop), .llen(key)
Set: .sadd(key, member), .srem(key, member), .smembers(key), .sismember(key, member), .sunion/sinter/sdiff(keys)
Sorted Set: .zadd(key, score, member), .zrange(key, start, stop), .zscore(key, member)
Hash: .hset/hget(key, field, value), .hgetall(key), .hdel(key, field), .hexists(key, field)
Pub/Sub: .subscribe(channel), .publish(channel, message)
Transaction: .multi(), .exec(), .discard()
cache — import std/cache
| Class |
Methods |
LRUCache(max_size, ttl) |
.get(key), .set(key, value, ttl), .has(key), .delete(key), .clear(), .size(), .keys(), .values(), .items(), .stats(), .cleanup() |
LFUCache(max_size, ttl) |
Same API as LRU |
CacheEntry(key, value, ttl) |
.isExpired(), .touch(), .getAge(), .getTimeToLive() |
Eviction Policies: LRU, LFU, FIFO, LIFO, TTL, RANDOM
AI & Machine Learning
tensor — import std/tensor
| Function |
Description |
tensor(data, dtype, requires_grad) |
Create tensor |
zeros/ones/full(shape, ...) |
Create tensor with fill |
eye(n, ...) |
Identity matrix |
| Property |
Description |
.data, .dtype, .shape, .ndim, .strides, .grad, .requires_grad |
Tensor metadata |
| Method |
Description |
.clone(), .to_list(), .print() |
Basic operations |
nn — import std/nn — Neural Networks
| Class |
Description |
Module |
Base class with .forward(x), .parameters(), .named_parameters(), .train(), .eval(), .zero_grad(), .to(device) |
Parameter(data) |
Trainable parameter with .zero_grad() |
Linear(in, out) |
Fully connected layer |
Conv1d(in_ch, out_ch, kernel) |
1D convolution |
Conv2d(in_ch, out_ch, kernel) |
2D convolution |
Activation functions: relu(), sigmoid(), tanh(), softmax(), leaky_relu(), gelu()
autograd — import std/autograd — Automatic Differentiation
| Function |
Description |
variable(data) |
Create differentiable variable |
constant(data) |
Create non-differentiable constant |
backward(var) |
Compute gradients (backpropagation) |
| Math ops |
add/sub/mul/div/neg/pow/exp/log/sin/cos/tan/sqrt/relu/sigmoid/tanh/softmax/sum/mean/matmul/transpose/flatten/reshape/concat(...) |
linear_new(in_features, out_features) |
Create linear layer |
optimize — import std/optimize — Optimizers
| Optimizer |
Constructor |
Method |
| Gradient Descent |
gradient_descent_new(lr) |
minimize_gd(gd, fn, x0, iters) |
| Momentum |
momentum_new(lr, beta) |
minimize_momentum(m, fn, x0, iters) |
| Adam |
adam_new(lr, beta1, beta2) |
minimize_adam(adam, fn, x0, iters) |
| RMSprop |
rmsprop_new(lr, decay) |
minimize_rmsprop(rms, fn, x0, iters) |
dataset — import std/dataset — Data Loading
| Type |
Description |
Dataset (trait) |
Base dataset interface |
InMemoryDataset |
In-memory dataset |
LazyDataset |
Lazy-loading dataset |
ChainDataset |
Concatenated datasets |
Subset |
Dataset subset |
CSVDataset |
Load from CSV |
JSONDataset |
Load from JSON |
ImageFolder |
Image directory loader |
CacheDataset |
Cached dataset wrapper |
DataLoader |
Batch iterator |
PrefetchDataLoader |
Prefetching data loader |
WeightedRandomSampler |
Weighted sampling |
KFold |
K-fold cross-validation |
Transforms: Normalize, Resize, RandomCrop, RandomHorizontalFlip, RandomRotation, ColorJitter, RandomAffine, Compose, RandomErasing, ToTensor
train — import std/train — Training Pipelines
| Step Types |
Description |
STEP_DATA |
Data loading step |
STEP_PREPROCESS |
Preprocessing step |
STEP_TRAIN |
Training step |
STEP_EVALUATE |
Evaluation step |
STEP_TRANSFORM |
Transform step |
STEP_SAVE |
Checkpoint save step |
| Function |
Description |
Pipeline(name, desc) |
Create training pipeline |
add_step(pipeline, id, type, config, deps) |
Add step |
run_pipeline(pipeline, context) |
Execute pipeline |
distributed — import std/distributed — Distributed Training
| Function |
Description |
init_process_group(backend, world_size) |
Init distributed |
get_rank() / get_world_size() |
Process info |
barrier() |
Synchronize all processes |
all_reduce/all_gather/reduce/broadcast/scatter/gather(tensor, op) |
Collective ops |
data_parallel_new(model, ...) |
Data parallelism |
ring_all_reduce(tensor) |
Ring all-reduce |
federated_avg(models) |
Federated learning |
compress_gradients(grads) |
Gradient compression |
dist_adam_new/step(...) |
Distributed Adam optimizer |
nlp, serving, experiment, feature_store, hub, mlops, monitor — ML Operations
Additional ML modules covering: NLP text processing, model serving (REST/gRPC), experiment tracking, feature stores with time-travel, model registry/hub, Kubernetes deployment generation, prediction monitoring and data drift detection.
Systems & Low-Level
allocators — import std/allocators — 5 Allocator Types
| Allocator |
Constructor |
Key Methods |
Arena(capacity) |
Bulk allocation |
.alloc(size), .alloc_zeroed(size), .alloc_array(count, size), .reset(), .clear(), .destroy(), .used(), .available(), .utilization() |
Pool(block_size, count) |
Fixed-size objects |
.alloc(), .alloc_zeroed(), .free(ptr), .destroy(), .available_blocks(), .allocated_blocks(), .utilization() |
Slab(sizes, counts) |
Size classes |
.alloc(size), .alloc_zeroed(size), .free(ptr), .destroy(), .get_stats() |
Stack(capacity) |
Stack-based |
.alloc(size), .free(ptr), .push_marker(), .pop_marker(), .clear(), .destroy() |
FreeList(min, max) |
Free list |
.alloc(size), .free(ptr), .coalesce(), .destroy() |
Constant: CACHE_LINE_SIZE = 64
atomics — import std/atomics — Lock-Free Programming
| Class |
Methods |
AtomicI32(val) |
.load(order), .store(val, order), .fetch_add/sub(val, order), .compare_exchange(expected, desired, order), .swap(val, order) |
AtomicI64(val) |
Same as AtomicI32 |
AtomicBool(val) |
Same + .toggle(order) |
AtomicPtr(ptr) |
Atomic pointer operations |
Spinlock |
.lock(), .unlock(), .try_lock() |
RwLock |
.read_lock(), .write_lock(), .read_unlock(), .write_unlock() |
AtomicRefCount |
.increment(), .decrement(), .get() |
LockFreeStack |
.push(value), .pop() |
LockFreeQueue |
.enqueue(value), .dequeue() |
Memory Orders: RELAXED, ACQUIRE, RELEASE, ACQ_REL, SEQ_CST
Functions: fence(order), compiler_fence(order)
simd — import std/simd — SIMD Vectorization
| Class |
Methods |
Vec2f(x, y) |
.add/sub/mul(other), .dot(other) |
Vec4f(x, y, z, w) |
.add/sub/mul/mul_vec(other), .dot(other), .length(), .normalize() |
Vec8f(data) |
.add/mul(other) |
SimdArrayOps(data) |
.add(other) |
ISA Detection: detect_simd_support() → SSE, SSE2, SSE3, SSSE3, SSE4.1, SSE4.2, AVX, AVX2, AVX-512, NEON, SVE, SVE2
asm — import std/asm — Inline Assembly
Builders: AsmBuilder, AsmOps, AsmTemplate, AsmARM
Macros: asm!(template, ...), asm_volatile!(template, ...), asm_intel!(template, ...), asm_att!(template, ...)
Register Constants: ASM_REG_RAX/RBX/RCX/RDX/RSI/RDI/RSP/RBP/R8-R15
Architectures: x86/x86_64, ARM/AArch64, RISC-V
smart_ptrs — import std/smart_ptrs
| Smart Pointer |
Description |
Key Methods |
Box<T> |
Single-owner heap alloc |
.new(val), .from_raw(ptr), .into_raw(), .leak() — impl Deref/DerefMut/Drop/Clone |
Rc<T> |
Reference-counted |
.new(val), .strong_count(), .weak_count(), .downgrade(), .get_mut(), .make_mut(), .ptr_eq(other) |
Arc<T> |
Atomic ref-counted (thread-safe) |
Same as Rc but thread-safe |
ownership — import std/ownership
| Class |
Methods |
Lifetime |
.new(name), .outlives(other), .is_subtype_of(other), .intersect(other), .static() |
LifetimeInference |
.new(), .fresh_lifetime(), .add_outlives_constraint(a, b), .solve() |
Ownership States: Owned, Moved, Borrowed, BorrowedMut, PartiallyMoved
paging, interrupts, dma, hardware, process, realtime, debug_hw — Kernel/OS Modules
| Module |
Description |
paging |
Page table entries (4KB/2MB/1GB pages), PageTableEntry, PageTable, PageMapper with flags: PRESENT, WRITABLE, USER, WRITE_THROUGH, CACHE_DISABLE, ACCESSED, DIRTY, HUGE, GLOBAL, NO_EXECUTE |
interrupts |
IDT(size), IDTEntry, InterruptFrame, 22 exception types (DIVIDE_ERROR → CONTROL_PROTECTION), 16 IRQ lines |
dma |
DMAChannel, DMAController, ScatterGatherDMA, DMABuffer, DMABufferPool |
hardware |
CPUID (vendor, brand, feature detection: SSE/AVX/AES/VMX/SVM/RDRAND), MSR (16 MSR constants: IA32_APIC_BASE → KERNEL_GS_BASE), CPURegisters (CR0/CR2/CR3/CR4) |
process |
Process(pid) with 20+ methods (.is_running(), .terminate(), .cpu_times(), .memory_info(), .threads(), .connections()), Popen(cmd, args) |
realtime |
CPUAffinity, ThreadPriority, SchedulingPolicy (FIFO/RR/DEADLINE/SPORADIC), RealTimeTask |
debug_hw |
HardwareBreakpoint, DebugRegisters, StackUnwinder, PerfCounter, PerformanceMonitoring, ProcessorTrace, LastBranchRecord, MemoryTracer with 16 performance counter types |
VM & Hypervisor (10 Modules)
vm, hypervisor, vm_devices, vm_acpi, vm_bios, vm_migration, vm_tpm, vm_hotplug, vm_metrics, vm_production
| Module |
Description |
vm |
Full VM engine: VMConfig, GuestMemory(size), VCPUState(id), VirtualMachine(config) — UEFI/BIOS boot, dirty page tracking, hypercalls |
hypervisor |
Intel VMX: VMX.is_supported/enable/vmxon/vmxoff/vmclear/vmptrld/vmlaunch/vmresume/vmread/vmwrite(...), EPT page tables, VM exit handling |
vm_devices |
Device(name), DeviceBus, InterruptRouter, PIC8259 (full 8259A emulation), PCIConfigSpace |
vm_acpi |
ACPIWriter, RSDP/RSDT/MADT/FADT table generation, ACPI S-states (S0-S5), C-states, D-states, thermal zones |
vm_bios |
BIOSDataArea, IVTSetup, E820MemoryMap — x86 BIOS emulation with IVT, BDA, EBDA memory layout |
vm_migration |
DirtyPageTracker, LiveMigration (pre-copy, iterative pre-copy, stop-and-copy), verification, progress tracking |
vm_tpm |
TPM 2.0 device: PCR banks (SHA-256/384/512), NV storage, handle manager, CRB interface |
vm_hotplug |
PCI hot-plug: PCIHotplugSlot, PCIHotplugController with insert/remove/enable callbacks |
vm_metrics |
PerformanceCounter, VMMetricsCollector, VMPerformanceMonitor with standard counters (vmexits, CPUID/IO/MMIO/MSR exits, interrupt injections) |
vm_production |
Builder pattern: .memory(size).cpus(count).uefi(path).disk(path).nic(model).with_error_handling().with_logging().with_metrics().with_live_migration().with_pci_hotplug().with_tpm().with_iommu().build() |
GUI, Game & Visualization
gui — import std/gui
| Class |
Key Methods |
Application |
.run(), .quit(), .add/remove_widget(w), .create_menu/toolbar/statusbar(...), .set/cancel_timer(...), .bind(event, handler), .update(), .render() |
Window(title, w, h) |
.title/size/position/resizable/maxsize/minsize/aspect/state/attributes/transient(...), .grab_set/release(), .focus/focus_force(), .bind/unbind(...), .protocol(...), .after(ms, fn), .update(), .mainloop(), .destroy() |
game — import std/game — Full Game Engine
| Class |
Key Methods |
Game(title, w, h) |
.init_game(), .set_scene(scene), .add/remove/get_group(name), .run(), .handle_events(), .update(), .render(), .tick(), .cleanup(), .quit(), .set/get_caption(...), .get_version/fps/time() |
Clock |
.tick(fps), .get_fps/time/rawtime(), .wait(ms), .tick_busy_loop(fps) |
Surface(w, h) |
.get/set_at(x, y, ...), .blit(src, ...), .fill/fill_rect(...), .convert/convert_alpha(), .copy(), .scroll(...), .set/get_colorkey/alpha/clip(...), .lock/unlock() |
8 Color Constants: BLACK, WHITE, RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA
Key Constants: K_BACKSPACE through K_F12, K_a-K_z, arrow keys, modifier keys, mouse buttons, joystick buttons
visualize — import std/visualize — Charts & Plots
140+ named colors, 8 color palettes (default, warm, cool, pastel, neon, earth, ocean, sunset), 12 gradient maps (viridis, plasma, inferno, magma, cividis, turbo, hot, cool, spring, copper, bone, gray)
Full matplotlib/plotly-equivalent plotting library for bar charts, line charts, scatter plots, histograms, pie charts, heatmaps, and more.
cli — import std/cli — Argument Parser
| Type |
Description |
ArgType enum |
String, Int, Float, Bool, ListString, ListInt |
arg_string/int/float/bool(name, help) |
Create argument |
with_short(arg, short) |
Add short flag |
with_default(arg, default) |
Set default value |
required(arg) |
Mark required |
parser_new(name, version, desc) |
Create parser |
add_argument(parser, arg) |
Add argument |
parse_args(parser, args) |
Parse CLI args |
print_help/usage/version(parser) |
Display info |
confirm(prompt) |
Yes/no prompt |
select(prompt, options) |
Selection prompt |
ProgressBar |
.update(n), .draw(), .finish() |
Spinner |
.next(), .draw() |
log — import std/log — Logging Framework
Levels: DEBUG=10, INFO=20, WARNING=30, ERROR=40, CRITICAL=50
| Class |
Methods |
Logger(name) |
.set_level(level), .add_handler(handler), .set_formatter(fmt), .add_filter(filter), .debug/info/warning/error/critical/exception(msg) |
ConsoleHandler |
Console output with colors |
FileHandler(path) |
File output |
RotatingFileHandler(path, max_bytes, backup_count) |
Rotating file output |
Formatter(format_str) |
Custom format |
Built-in Formatters: simple_formatter(), detailed_formatter(), json_formatter(), colored_formatter()
import std/log
let logger = get_logger("myapp")
logger.set_level(DEBUG)
logger.add_handler(ConsoleHandler())
logger.add_handler(FileHandler("app.log"))
logger.info("Application started")
logger.error("Something went wrong")
test — import std/test
| Function |
Description |
assert(cond, msg) |
Assert true |
eq(actual, expected, msg) |
Assert equal |
neq(actual, expected, msg) |
Assert not equal |
raises(fn, msg) |
Assert throws |
approx(actual, expected, tolerance, msg) |
Assert approximate |
contains_(container, item, msg) |
Assert contains |
is_true/is_false(value, msg) |
Boolean assertions |
is_null/is_not_null(value, msg) |
Null assertions |
skip(msg) |
Skip test |
test(name, fn) |
Define test |
suite(name, tests) |
Test suite |
results() / summary() |
Get/print results |
bench, debug, time, cron, formatter, monitor, metrics, lsp, parser, state_machine, generator, governance, experiment, ci — More Tools
| Module |
Description |
bench |
Benchmark suites, GPU profiling, memory analysis, kernel fusion analysis, regression detection |
debug |
trace(), inspect(), deep_inspect(), breakpoint(), timed(fn), memory(), memory_profile(fn), profile(fn), dd(), measure(), hardware breakpoints, perf counters |
time |
now(), now_millis/micros/nanos(), sleep/sleep_ms/sleep_us/sleep_ns(), parse_iso(), to_components(), to_timestamp(), format_time() |
cron |
CronExpression(expr), CronJob(name, expr, fn) — scheduled task execution |
formatter |
Code formatter: format_code(code, options), format_file(path), sort_imports(code), add_import(code, module), remove_unused_imports(code) |
monitor |
MetricsCollector, LatencyTracker, PredictionMonitor, DataDriftDetector |
metrics |
Prometheus-compatible: Counter, Gauge, Histogram, Summary, Timer with labels and .toPrometheus() export |
lsp |
Full Language Server Protocol implementation: initialize, completion, hover, definition, references, diagnostics |
parser |
Combinator parser: Parser(fn) with .map(), .flatMap(), .then(), .or(), .many(), .many1(), .between(), .sepBy(), .label() |
state_machine |
StateMachine(name, initial), State(name) with entry/exit actions, transitions, guards, HSM support |
generator |
File format generation: .txt, .md, .csv, .rtf, .svg, .bmp, .png, .ico, .jpg, .pdf |
governance |
A/B testing: ExperimentService, CanaryManager |
experiment |
ML experiment tracking: Experiment(name), ExperimentTracker, dashboards, comparison, similarity |
ci |
TestCase/TestResult/TestSuite — JUnit-style test framework with 25+ assertion methods |
DFAS — Dynamic Field Arithmetic (10 modules in stdlib/dfas/)
dfas/* — import std/dfas — Field Arithmetic System
See DFAS section below for full details.
🔥 All 117+ Engines (Complete Reference)
All engines are native and built-in. No installation required. Just use engine_name and go.
113 engine directories exist in engines/ covering every domain from AI to robotics.
AI & Machine Learning (21 Engines)
Click to expand full AI/ML engine details
| Engine |
Use |
What It Does |
| nyai |
use nyai |
Multi-modal LLMs, autonomous agents, chain-of-thought reasoning, prompt engineering, retrieval-augmented generation (RAG), embeddings, tokenization, NLP pipelines |
| nyml |
use nyml |
Traditional ML algorithms: Random Forest, SVM, k-Means, k-NN, Decision Trees, Naive Bayes, Linear/Logistic Regression, PCA, t-SNE, DBSCAN, Gradient Boosting, AdaBoost, XGBoost-style ensembles |
| nygrad |
use nygrad |
Automatic differentiation engine: forward-mode and reverse-mode AD, computational graph construction, gradient tape recording, Jacobian/Hessian computation, gradient checkpointing |
| nytensor |
use nytensor |
N-dimensional tensor operations with SIMD vectorization (SSE/AVX/NEON), broadcasting, slicing, reshaping, transposing, einsum, tensor contraction, memory-mapped tensors |
| nynet |
use nynet |
Neural network layer library: Linear, Conv1d/2d/3d, BatchNorm, LayerNorm, GroupNorm, Dropout, MaxPool, AvgPool, LSTM, GRU, Transformer, MultiHeadAttention, Embedding, positional encoding |
| nyopt |
use nyopt |
Optimization algorithms: SGD, SGD+Momentum, Adam, AdamW, RMSprop, Adagrad, Adadelta, LBFGS, learning rate schedulers (StepLR, CosineAnnealing, OneCycleLR, WarmupLR), gradient clipping |
| nyloss |
use nyloss |
Loss functions: MSE, MAE, Huber, CrossEntropy, BinaryCE, NLLLoss, KLDivergence, CosineEmbedding, TripletMargin, FocalLoss, DiceLoss, ContrastiveLoss, InfoNCE |
| nyrl |
use nyrl |
Reinforcement learning: Q-Learning, DQN, Double-DQN, PPO, A2C, A3C, SAC, DDPG, TD3, REINFORCE, experience replay, prioritized replay, multi-agent RL, reward shaping, curiosity-driven exploration |
| nygen |
use nygen |
Generative models: GANs (vanilla, DCGAN, WGAN, StyleGAN, CycleGAN), VAEs (vanilla, β-VAE, VQ-VAE), diffusion models, normalizing flows, autoregressive models |
| nygraph_ml |
use nygraph_ml |
Graph neural networks: GCN, GAT, GraphSAGE, GIN, message passing framework, node/edge/graph classification, link prediction, graph generation, knowledge graphs |
| nymodel |
use nymodel |
Model management: versioning, serialization (save/load), ONNX export, model registry, A/B testing, canary deployment, model comparison, checkpoint management |
| nymind |
use nymind |
Cognitive AI: reasoning chains, belief networks, causal inference, theory of mind, planning with world models, metacognition, analogical reasoning |
| nyagent |
use nyagent |
AI agents: tool-using agents, ReAct pattern, planning (BFS/DFS/A*), memory (short-term, long-term, episodic), multi-agent communication, goal decomposition |
| nylinear |
use nylinear |
Linear algebra: matrix multiply, LU/QR/SVD decomposition, eigenvalues, Cholesky, sparse solvers, iterative methods (CG, GMRES, BiCGSTAB), blocked algorithms |
| nylogic |
use nylogic |
Logic programming: Prolog-style unification, backtracking search, horn clauses, forward/backward chaining, constraint satisfaction, SAT solving, SMT solving |
| nyprecision |
use nyprecision |
Mixed-precision training: FP16/BF16/FP8 computation, loss scaling (static/dynamic), gradient accumulation, mixed-precision optimizers, quantization (INT8/INT4) |
| nyswarm |
use nyswarm |
Swarm intelligence: Particle Swarm Optimization, Ant Colony Optimization, Bee Algorithm, Firefly Algorithm, Grey Wolf Optimizer, genetic algorithms, differential evolution |
| nymlbridge |
use nymlbridge |
ML framework interop: import/export PyTorch models, TensorFlow SavedModel, ONNX interchange, scikit-learn pipeline conversion, Hugging Face model loading |
| nyfeature |
use nyfeature |
Feature engineering: feature stores with time-travel queries, online/offline feature serving, feature transformations, feature importance, automated feature selection |
| nytrack |
use nytrack |
Experiment tracking: metrics logging, parameter tracking, artifact storage, experiment comparison, hyperparameter search (grid, random, Bayesian), reproducibility |
| nynet_ml |
use nynet_ml |
ML-specific networking: federated learning communication, parameter server, gradient compression (top-K, random-K), all-reduce implementations |
// Example: Train a neural network with nynet + nyopt
use nytensor
use nynet
use nyopt
use nyloss
let model = Sequential([
Linear(784, 256),
ReLU(),
Dropout(0.2),
Linear(256, 128),
ReLU(),
Linear(128, 10),
Softmax()
])
let optimizer = Adam(model.parameters(), lr: 0.001, weight_decay: 1e-4)
let scheduler = CosineAnnealing(optimizer, T_max: 100)
let loss_fn = CrossEntropy()
for epoch in 0..100 {
for batch in train_loader {
let output = model.forward(batch.x)
let loss = loss_fn(output, batch.y)
loss.backward()
optimizer.step()
optimizer.zero_grad()
}
scheduler.step()
print(f"Epoch {epoch}: loss={loss.item():.4f}")
}
Click to expand full GPU/HPC engine details
| Engine |
Use |
Key Features |
| nykernel |
use nykernel |
Custom CUDA kernel compilation, JIT compilation, kernel fusion, warp-level primitives, shared memory management, occupancy calculator, profiling integration, PTX generation |
| nygpu |
use nygpu |
GPU computing abstraction layer supporting CUDA (NVIDIA), ROCm (AMD), OpenCL (cross-vendor), Metal (Apple), Vulkan Compute; device enumeration, memory management, stream/event synchronization, multi-GPU |
| nyhpc |
use nyhpc |
High-performance computing: MPI-style communication, NUMA-aware allocation, vectorized loops, auto-parallelization hints, OpenMP-style pragmas, cache-oblivious algorithms |
| nycompute |
use nycompute |
Distributed computation: task graphs, data-parallel maps, scatter-gather, map-reduce, barrier synchronization, fault tolerance, checkpoint/restart |
| nyparallel |
use nyparallel |
Parallel processing: thread pools, work-stealing schedulers, parallel-for, parallel-reduce, parallel-sort, fork-join, pipeline parallelism, task dependencies |
| nyaccel |
use nyaccel |
Hardware acceleration: FPGA bitstream loading, TPU integration, DSP offloading, hardware intrinsics, auto-vectorization, platform-specific optimizations |
| nycluster |
use nycluster |
Cluster computing: node discovery, job scheduling (SLURM/PBS integration), distributed file system, cluster health monitoring, auto-scaling, resource quotas |
// Example: GPU tensor computation
use nygpu
use nytensor
let device = nygpu.best_device() // Auto-select fastest GPU
let a = nytensor.randn([1024, 1024]).to(device)
let b = nytensor.randn([1024, 1024]).to(device)
let c = a.matmul(b) // GPU-accelerated matrix multiply
print(f"Result shape: {c.shape}, device: {c.device}")
Data & Storage (9 Engines)
Click to expand full Data/Storage engine details
| Engine |
Use |
Key Features |
| nydata |
use nydata |
Data manipulation: ETL pipelines, data cleaning, missing value imputation, outlier detection, normalization (min-max, z-score, robust), encoding (one-hot, label, ordinal), type inference |
| nydatabase |
use nydatabase |
Database connectivity: SQL (PostgreSQL, MySQL, SQLite), NoSQL (MongoDB, CouchDB), connection pooling, prepared statements, transactions, migrations, ORM-style query builder |
| nydb |
use nydb |
Embedded database: B-tree storage engine, WAL journaling, MVCC concurrency, full SQL subset, in-memory mode, encryption-at-rest, automatic compaction |
| nyarray |
use nyarray |
High-performance arrays: contiguous memory layout, SIMD operations, zero-copy slicing, memory-mapped arrays, typed arrays (i8→f64), interop with C arrays via FFI |
| nycache |
use nycache |
Caching: LRU/LFU/ARC eviction, TTL expiration, write-through/write-back, distributed cache (consistent hashing), cache warming, compression, serialization |
| nystorage |
use nystorage |
Storage abstraction: local filesystem, S3-compatible object storage, distributed storage, content-addressable storage, deduplication, erasure coding |
| nyquery |
use nyquery |
Query optimization: cost-based optimizer, query plan visualization, index recommendations, join order optimization, predicate pushdown, projection pruning |
| nystream |
use nystream |
Stream processing: windowed aggregations (tumbling, sliding, session), event-time processing, watermarks, exactly-once semantics, backpressure handling, Kafka-compatible |
| nyframe |
use nyframe |
DataFrame operations: columnar storage, lazy evaluation, group-by/pivot/melt/join/sort/filter, CSV/Parquet/JSON/Arrow I/O, SQL interface, parallel execution |
// Example: Data pipeline with nyframe
use nyframe
let df = nyframe.read_csv("sales.csv")
|> filter(|row| row.amount > 100)
|> group_by("region")
|> agg({
total: sum("amount"),
avg_amount: mean("amount"),
count: count()
})
|> sort_by("total", descending: true)
df.to_csv("report.csv")
df.show(10)
Web & Networking (6 Engines)
Click to expand full Web/Networking engine details
| Engine |
Use |
Key Features |
| nyweb |
use nyweb |
Full web framework: routing (path params, query params, wildcards), middleware stack, static file serving, template engine, session management, CORS, CSRF protection, rate limiting, WebSocket support |
| nyhttpd |
use nyhttpd |
HTTP server: 15K+ req/sec throughput, HTTP/1.1 and HTTP/2, TLS/SSL, keep-alive, chunked transfer, gzip compression, worker thread pool, graceful shutdown, access logging |
| nyhttp |
use nyhttp |
HTTP client: GET/POST/PUT/DELETE/PATCH/HEAD/OPTIONS, connection pooling, automatic retries, redirect following, cookie jar, multipart uploads, streaming responses, proxy support |
| nyapi |
use nyapi |
REST API framework: OpenAPI/Swagger generation, request validation (JSON Schema), response serialization, API versioning, pagination, HATEOAS links, OAuth2/JWT auth middleware |
| nyqueue |
use nyqueue |
Message queues: in-memory queues, persistent queues, pub/sub, topic routing, dead-letter queues, message deduplication, delayed messages, priority queues, at-least-once/exactly-once delivery |
| nynetwork |
use nynetwork |
Advanced networking: raw sockets, packet crafting, network scanning, DNS resolution, mDNS/DNS-SD, STUN/TURN/ICE (WebRTC), TLS certificate management, connection multiplexing |
// Example: Full REST API with authentication
use nyhttpd
use nyapi
let server = nyhttpd.HttpServer.new({port: 3000, workers: 4})
// Middleware
server.use(nyapi.cors({origins: ["*"]}))
server.use(nyapi.rate_limit({max: 100, window: "1m"}))
server.use(nyapi.jwt_auth({secret: env("JWT_SECRET"), exclude: ["/auth/login"]}))
// Routes
server.get("/api/users", fn(req, res) {
let users = db.query("SELECT * FROM users LIMIT $1 OFFSET $2",
[req.query.limit ?? 20, req.query.offset ?? 0])
res.json({data: users, total: db.count("users")})
})
server.post("/auth/login", fn(req, res) {
let user = db.find_one("users", {email: req.body.email})
if user and verify_password(req.body.password, user.hash) {
res.json({token: nyapi.jwt_sign({sub: user.id}, "24h")})
} else {
res.status(401).json({error: "Invalid credentials"})
}
})
server.start()
Security & Crypto (9 Engines)
Click to expand full Security/Crypto engine details
| Engine |
Use |
Key Features |
| nysec |
use nysec |
Security scanning: static analysis (SAST), dependency vulnerability scanning, secret detection, code quality checks, OWASP Top 10 detection, security headers validation |
| nysecure |
use nysecure |
Adversarial defense: adversarial training, input perturbation detection, differential privacy (ε-δ guarantees), secure aggregation, homomorphic encryption helpers, secure multi-party computation |
| nycrypto |
use nycrypto |
Full cryptographic suite: AES-128/192/256 (CBC/CTR/GCM), RSA (2048/4096), ECDSA (P-256/P-384/secp256k1), Ed25519, X25519, ChaCha20-Poly1305, HMAC, HKDF, PBKDF2, Argon2, scrypt |
| nyaudit |
use nyaudit |
Security auditing: access log analysis, anomaly detection, compliance reporting (SOC2, HIPAA, PCI-DSS), audit trail, permission matrix analysis, privilege escalation detection |
| nycompliance |
use nycompliance |
Compliance checking: GDPR data flow analysis, data retention policies, consent management, right-to-erasure automation, data classification, privacy impact assessment |
| nyexploit |
use nyexploit |
Exploit detection: buffer overflow detection, format string vulnerability detection, use-after-free detection, memory corruption analysis, ROP chain detection |
| nyfuzz |
use nyfuzz |
Fuzz testing: coverage-guided fuzzing, mutation-based fuzzing, grammar-based fuzzing, AFL-style instrumentation, crash deduplication, corpus minimization, distributed fuzzing |
| nyids |
use nyids |
Intrusion detection: network packet inspection, signature-based detection, anomaly-based detection, Snort-compatible rules, honeypot integration, alert correlation |
| nymal |
use nymal |
Malware analysis: static analysis (PE/ELF/Mach-O parsing), dynamic analysis sandbox, API call hooking, behavior graphs, YARA rule matching, unpacking, string extraction |
Click to expand full Multimedia/Game engine details
| Engine |
Use |
Key Features |
| nyrender |
use nyrender |
3D rendering: forward/deferred rendering pipelines, PBR materials, shadow mapping (PCF, VSM, CSM), HDR, bloom, SSAO, screen-space reflections, post-processing stack, instanced rendering, LOD management |
| nyphysics |
use nyphysics |
Physics simulation: rigid body dynamics, collision detection (AABB, OBB, sphere, convex hull, GJK/EPA), constraints (hinges, springs, motors), soft body, fluid simulation (SPH), cloth simulation, Verlet integration |
| nyaudio |
use nyaudio |
3D spatial audio: HRTF-based spatialization, distance attenuation, reverb zones, audio mixing, DSP effects (EQ, compressor, delay, chorus), streaming playback, format support (WAV, OGG, MP3, FLAC) |
| nygame |
use nygame |
Full game engine: scene graph, entity-component-system (ECS), sprite rendering, tile maps, particle systems, input handling (keyboard, mouse, gamepad), camera systems, game state management |
| nyanim |
use nyanim |
Animation: keyframe animation, skeletal animation with bone hierarchies, inverse kinematics (FABRIK, CCD), blend trees, animation state machines, morph targets, motion capture playback, animation retargeting |
| nymedia |
use nymedia |
Media processing: video encoding/decoding (H.264, H.265, VP9, AV1), audio transcoding, image processing (resize, crop, filter, format conversion), subtitle handling, thumbnail generation |
| nyviz |
use nyviz |
Data visualization: line/bar/scatter/pie/histogram/heatmap/treemap/sunburst/sankey charts, 3D plots, real-time dashboards, interactive tooltips, animation, export (PNG, SVG, PDF) |
| nyui |
use nyui |
Native UI framework: windows, buttons, labels, text inputs, dropdowns, checkboxes, radio buttons, sliders, progress bars, tabs, trees, tables, menus, toolbars, status bars, dialogs, file pickers, system tray |
// Example: 2D game with ECS
use nygame
let game = nygame.Game("Space Shooter", 800, 600)
// Define components
struct Position { x: f32, y: f32 }
struct Velocity { dx: f32, dy: f32 }
struct Sprite { texture: string, width: int, height: int }
struct Health { hp: int, max_hp: int }
// Create entities
let player = game.spawn()
.with(Position { x: 400.0, y: 500.0 })
.with(Velocity { dx: 0.0, dy: 0.0 })
.with(Sprite { texture: "ship.png", width: 64, height: 64 })
.with(Health { hp: 100, max_hp: 100 })
// Game loop
game.on_update(fn(dt) {
// Movement system
for entity in game.query(Position, Velocity) {
entity.get(Position).x += entity.get(Velocity).dx * dt
entity.get(Position).y += entity.get(Velocity).dy * dt
}
})
game.run()
DevOps & Infrastructure (8 Engines)
Click to expand full DevOps/Infrastructure engine details
| Engine |
Use |
Key Features |
| nyci |
use nyci |
CI/CD pipelines: pipeline definition DSL, parallel stages, conditional execution, artifact management, test result aggregation, notification hooks (Slack, email, webhook), GitHub/GitLab integration |
| nycloud |
use nycloud |
Cloud infrastructure: AWS/GCP/Azure abstraction, VM provisioning, load balancer configuration, DNS management, certificate provisioning, cost optimization, multi-cloud deployment |
| nycontainer |
use nycontainer |
Container management: Dockerfile-compatible image building, container lifecycle, volume management, network configuration, health checks, resource limits, multi-stage builds |
| nykube |
use nykube |
Kubernetes integration: Pod/Service/Deployment/ConfigMap/Secret management, Helm chart generation, kubectl-style operations, custom resource definitions, operator pattern, rolling updates |
| nyinfra |
use nyinfra |
Infrastructure-as-code: declarative infrastructure definition, dependency graph, plan/apply workflow, state management, drift detection, import existing resources, modular composition |
| nyautomate |
use nyautomate |
Task automation: cron-like scheduling, file watchers, event triggers, workflow DAGs, retry policies, timeout handling, parallel execution, audit logging |
| nyshell |
use nyshell |
Shell scripting: command execution, pipes, redirects, environment variables, glob patterns, path manipulation, temp files, signal handling, exit code management, cross-platform |
| nydeploy |
use nydeploy |
Deployment automation: blue-green deployments, canary releases, rollback, health check gates, deployment hooks, SSH-based deployment, zero-downtime deployment |
Science & Simulation (6 Engines)
Click to expand full Science/Simulation engine details
| Engine |
Use |
Key Features |
| nysci |
use nysci |
Scientific computing: numerical integration (Simpson, Gauss), root finding (Newton, bisection, Brent), interpolation (linear, cubic spline, Lagrange), curve fitting, statistical distributions |
| nychem |
use nychem |
Chemistry modeling: molecular dynamics, force field simulation (Lennard-Jones, Coulomb), reaction kinetics, molecular visualization, PDB file parsing, energy minimization |
| nybio |
use nybio |
Bioinformatics: sequence alignment (Smith-Waterman, Needleman-Wunsch, BLAST-style), phylogenetic trees, gene expression analysis, protein structure prediction, FASTA/FASTQ/VCF parsing |
| nyworld |
use nyworld |
World simulation: terrain generation (Perlin noise, diamond-square), weather simulation, population dynamics, ecosystem modeling, agent-based social simulation |
| nysim |
use nysim |
General simulation: discrete-event simulation, Monte Carlo methods, agent-based modeling, system dynamics, cellular automata, queuing theory, Markov chains |
| nyode |
use nyode |
ODE/PDE numerical solvers: Euler, Runge-Kutta (RK4, RK45), Adams-Bashforth, BDF, implicit methods, adaptive step-size, stiff system detection, finite element method basics |
Finance & Trading (5 Engines)
Click to expand full Finance/Trading engine details
| Engine |
Use |
Key Features |
| nyhft |
use nyhft |
High-frequency trading: sub-microsecond order routing, lock-free order book, market data normalization, co-location support, kernel bypass networking, FPGA acceleration hooks |
| nymarket |
use nymarket |
Market data engine: real-time feeds, historical data, OHLCV candles, order book depth, trade tick data, volume profile, market microstructure analytics, data replay |
| nyrisk |
use nyrisk |
Risk analysis: Value-at-Risk (VaR), Expected Shortfall (CVaR), Monte Carlo simulation, stress testing, scenario analysis, Greeks computation (delta, gamma, theta, vega), portfolio optimization |
| nytrade |
use nytrade |
Trading engine: order management system, position tracking, P&L calculation, FIX protocol, execution algorithms (TWAP, VWAP, iceberg), smart order routing, slippage modeling |
| nybacktest |
use nybacktest |
Strategy backtesting: event-driven backtester, walk-forward analysis, commission/slippage modeling, performance metrics (Sharpe, Sortino, max drawdown, Calmar), Monte Carlo permutation, strategy optimization |
// Example: Algorithmic trading strategy
use nytrade
use nymarket
use nybacktest
let strategy = nybacktest.Strategy("MeanReversion")
strategy.on_bar(fn(bar, portfolio) {
let sma_20 = bar.close.sma(20)
let std_20 = bar.close.std(20)
let z_score = (bar.close[-1] - sma_20) / std_20
if z_score < -2.0 and !portfolio.has_position(bar.symbol) {
portfolio.buy(bar.symbol, shares: 100)
} else if z_score > 0.0 and portfolio.has_position(bar.symbol) {
portfolio.sell(bar.symbol, shares: 100)
}
})
let results = nybacktest.run(strategy, {
data: nymarket.historical("AAPL", "2020-01-01", "2024-12-31"),
initial_capital: 100000,
commission: 0.001
})
results.report()
Distributed Systems (6 Engines)
Click to expand full Distributed Systems engine details
| Engine |
Use |
Key Features |
| nyconsensus |
use nyconsensus |
Consensus protocols: Raft (leader election, log replication, snapshotting), PBFT, Paxos, view change, membership changes, linearizability guarantees |
| nysync |
use nysync |
Synchronization: mutexes, read-write locks, semaphores, barriers, condition variables, countdown latches, CAS operations, compare-and-swap, atomic reference counting |
| nystate |
use nystate |
State machines: hierarchical state machines (HSM), parallel states, history states, guard conditions, entry/exit actions, event-driven transitions, visualization export |
| nyevent |
use nyevent |
Event system: pub/sub, event bus, event sourcing, CQRS pattern, event replay, event versioning, saga/process managers, compensation handling |
| nycontrol |
use nycontrol |
Control systems: PID controllers, state-space models, transfer functions, Bode/Nyquist plots, stability analysis, Kalman filtering, sensor fusion |
| nyplan |
use nyplan |
Planning and scheduling: task dependency DAGs, critical path analysis, resource allocation, constraint-based scheduling, genetic algorithm scheduling, priority-based scheduling |
Robotics & IoT (3 Engines)
Click to expand full Robotics/IoT engine details
| Engine |
Use |
Key Features |
| nyrobot |
use nyrobot |
Robotics: kinematics (forward/inverse), path planning (A*, RRT, PRM), SLAM, sensor integration (LiDAR, IMU, camera), motor control, PID tuning, Gazebo/ROS compatible messages |
| nydevice |
use nydevice |
Device management: device discovery, firmware updates, telemetry collection, remote configuration, MQTT/CoAP protocol support, edge computing, device twin/shadow |
| nyvoice |
use nyvoice |
Voice/speech processing: speech-to-text, text-to-speech, voice activity detection, speaker identification, wake word detection, audio preprocessing (noise reduction, echo cancellation) |
Build, Config & Core (15+ Additional Engines)
Click to expand additional engine details
| Engine |
Use |
Key Features |
| nycore |
use nycore |
Core runtime library and utilities |
| nyruntime |
use nyruntime |
Runtime management and introspection |
| nylang |
use nylang |
Language tooling and metaprogramming |
| nybuild |
use nybuild |
Build system: dependency resolution, incremental builds, cross-compilation |
| nyconfig |
use nyconfig |
Configuration management: environment-based config, feature flags, hot reload |
| nypack |
use nypack |
Package bundling and distribution |
| nypm |
use nypm |
Package manager engine (backing NYPM CLI) |
| nyscript |
use nyscript |
Scripting utilities and REPL support |
| nydoc |
use nydoc |
Documentation generation from source comments |
| nyreport |
use nyreport |
Report generation (PDF, HTML, Markdown) |
| nystats |
use nystats |
Statistical analysis: distributions, hypothesis testing, regression |
| nymetrics |
use nymetrics |
Prometheus-compatible metrics collection |
| nymonitor |
use nymonitor |
Application performance monitoring |
| nyserve / nyserver |
use nyserve |
Server utilities and hosting |
| nyserverless |
use nyserverless |
Serverless function deployment (Lambda/Cloud Functions) |
| nyscale |
use nyscale |
Auto-scaling and load management |
| nyprovision |
use nyprovision |
Infrastructure provisioning |
| nystudio |
use nystudio |
IDE/editor integration |
| nysystem / nysys |
use nysystem |
System-level operations and OS interaction |
| nyls |
use nyls |
Language server protocol implementation |
| nygraph |
use nygraph |
Graph data structures and algorithms (Dijkstra, BFS, DFS, topological sort, MST) |
| nygui |
use nygui |
Alternative GUI toolkit |
| nycalc |
use nycalc |
Calculator and expression evaluation |
| nyalign |
use nyalign |
Memory alignment and data layout optimization |
| nyquant |
use nyquant |
Quantitative analysis and numerical methods |
| nyrecon |
use nyrecon |
Reconnaissance and network discovery |
| nyreverse |
use nyreverse |
Reverse engineering tools |
| nyasync |
use nyasync |
Advanced async patterns and utilities |
Complete Extended Backus-Naur Form grammar for the Nyx programming language.
Program Structure
program = { statement } ;
statement = declaration | expression_stmt | control_flow | import_stmt
| class_decl | module_decl | try_stmt | raise_stmt
| assert_stmt | with_stmt | async_stmt | pass_stmt ;
declaration = let_decl | fn_decl | typealias_decl ;
let_decl = "let" [ "mut" ] identifier [ ":" type ] "=" expression ;
fn_decl = "fn" identifier "(" [ param_list ] ")" [ "->" type ] block ;
typealias_decl = "typealias" identifier "=" type ;
Expressions
expression = assignment ;
assignment = ( postfix "=" ) assignment | logical_or ;
logical_or = logical_and { "||" logical_and } ;
logical_and = bitwise_or { "&&" bitwise_or } ;
bitwise_or = bitwise_xor { "|" bitwise_xor } ;
bitwise_xor = bitwise_and { "^" bitwise_and } ;
bitwise_and = equality { "&" equality } ;
equality = relational { ( "==" | "!=" ) relational } ;
relational = shift { ( "<" | ">" | "<=" | ">=" | "<=>" | "is" | "instanceof" ) shift } ;
shift = additive { ( "<<" | ">>" ) additive } ;
additive = multiplicative { ( "+" | "-" ) multiplicative } ;
multiplicative = power { ( "*" | "/" | "%" | "//" ) power } ;
power = unary { "**" unary } ;
unary = ( "-" | "!" | "~" | "++" | "--" | "&" | "&mut" | "*" | "typeof"
| "sizeof" | "alignof" | "move" | "ref" ) unary | postfix ;
postfix = primary { call | index | member | "?" | "++" | "--" } ;
call = "(" [ arg_list ] ")" ;
index = "[" expression "]" ;
member = ( "." | "?." | "::" ) identifier ;
Primary Expressions
primary = literal | identifier | block | if_expr | switch_expr
| fn_literal | array_literal | object_literal | tuple_literal
| "(" expression ")" | comprehension | new_expr | "super"
| "self" | await_expr ;
literal = integer | float | string | boolean | "null" ;
integer = decimal | "0x" hex | "0o" octal | "0b" binary ;
string = '"' { char | escape } '"' | "'" { char | escape } "'"
| 'r"' { char } '"' | 'b"' { byte } '"' | 'f"' { char | "${" expression "}" } '"' ;
Control Flow
if_expr = "if" expression block { "elif" expression block } [ "else" block ] ;
switch_expr = "switch" expression "{" { case_clause } [ default_clause ] "}" ;
match_expr = "match" expression "{" { pattern "=>" expression "," } "}" ;
while_stmt = "while" expression block ;
for_stmt = "for" identifier "=" expression ";" expression ";" expression block ;
for_in_stmt = "for" pattern "in" expression block ;
loop_stmt = "loop" block ;
Class & Struct Declarations
class_decl = "class" identifier [ "<" type_params ">" ] [ "extends" type ]
[ "implements" type { "," type } ] "{" { class_member } "}" ;
struct_decl = "struct" identifier [ "<" type_params ">" ] "{" { struct_field } "}" ;
enum_decl = "enum" identifier "{" enum_variant { "," enum_variant } "}" ;
trait_decl = "trait" identifier [ "<" type_params ">" ] "{" { method_sig } "}" ;
impl_block = "impl" [ "<" type_params ">" ] [ trait "for" ] type "{" { fn_decl } "}" ;
Type System
type = primitive_type | compound_type | ref_type | user_type
| generic_type | tuple_type | union_type | fn_type ;
primitive_type = "int" | "i8" | "i16" | "i32" | "i64" | "u8" | "u16" | "u32" | "u64"
| "f32" | "f64" | "bool" | "char" | "str" | "void" | "null" | "never" ;
ref_type = ( "&" | "&mut" ) type ;
generic_type = identifier "<" type { "," type } ">" ;
union_type = type "|" type { "|" type } ;
fn_type = "fn" "(" [ type_list ] ")" "->" type ;
Operator Precedence (12 Levels)
| Level |
Category |
Operators |
Associativity |
| 1 |
Postfix |
() [] . ?. :: ? ++ -- |
Left |
| 2 |
Unary |
- ! ~ ++ -- & * typeof sizeof |
Right |
| 3 |
Power |
** |
Right |
| 4 |
Multiplicative |
* / % // |
Left |
| 5 |
Additive |
+ - |
Left |
| 6 |
Shift |
<< >> |
Left |
| 7 |
Relational |
< > <= >= <=> is instanceof |
Left |
| 8 |
Equality |
== != |
Left |
| 9 |
Bitwise AND/XOR/OR |
& ^ \| |
Left |
| 10 |
Logical AND |
&& |
Left |
| 11 |
Logical OR |
\|\| |
Left |
| 12 |
Assignment |
= += -= *= /= %= **= &= \|= ^= <<= >>= |
Right |
🧠 Interpreter Architecture (Complete Internals)
The complete pipeline from source code to execution — every component documented.
Execution Pipeline
Source Code (.ny)
│
▼
┌──────────────────────────────────────────────────────────┐
│ Lexer (src/lexer.py) │
│ Unicode-aware tokenizer · 150+ token types │
│ Supports: raw/byte/format/multiline strings │
│ Number formats: decimal, hex, octal, binary, scientific │
│ Comments: # line, // line, /* block */ (nested) │
│ Token hooks & runtime-extensible registry │
└──────────────────────┬───────────────────────────────────┘
│ Token Stream (with position & trivia)
▼
┌──────────────────────────────────────────────────────────┐
│ Parser (src/parser.py) │
│ Pratt parser · 11 precedence levels · extensible │
│ 25 prefix parse functions · 14 infix parse functions │
│ 20+ statement parsers · Automatic error recovery │
│ Dynamic registration: register_prefix/infix/statement │
└──────────────────────┬───────────────────────────────────┘
│ AST (70+ node types with source locations)
▼
┌──────────────────────────────────────────────────────────┐
│ Interpreter (src/interpreter.py) │
│ Async tree-walking evaluator · 11 built-ins │
│ Scoped Environment chain · Boxed value types │
│ NyxClass/UserFunction · 1M step limit (configurable) │
│ Module system with import resolvers │
│ Auto-wraps return values: int→Integer, str→String, etc │
└──────────────────────────────────────────────────────────┘
Source Files Overview
| File |
Lines |
Purpose |
src/token_types.py |
680 |
Token type enum, keyword map, TokenRegistry |
src/lexer.py |
530 |
Tokenizer with Unicode, hooks, trivia |
src/ast_nodes.py |
973 |
70+ AST node classes with visitor pattern |
src/parser.py |
650 |
Pratt parser with error recovery |
src/interpreter.py |
551 |
Async tree-walking evaluator |
src/async_runtime.py |
— |
Async runtime support |
src/borrow_checker.py |
— |
Ownership/borrow checking |
src/ownership.py |
— |
Ownership model implementation |
src/compiler.py |
— |
Bytecode compiler |
src/debugger.py |
— |
Interactive debugger |
src/polyglot.py |
— |
Multi-language interop |
src/stability.py |
— |
API stability checks |
run.py |
~20 |
Entry point: file → Lexer → Parser → evaluate |
Lexer Deep Dive (src/lexer.py)
The lexer converts source text into a stream of Token objects with full Unicode support, configurable options, and an extensible hook system.
Token Object Fields
Every token carries rich metadata:
| Field |
Type |
Description |
type |
TokenType |
The token's type (enum) |
literal |
str |
Raw source text |
line |
int |
1-based line number |
column |
int |
1-based column number |
byte_offset |
int |
Byte position in source |
byte_length |
int |
Byte length of raw text |
source_file |
str |
Source filename |
leading_trivia |
str |
Whitespace/comments before token |
trailing_trivia |
str |
Whitespace/comments after token |
semantic_type |
str |
Semantic classification |
scope_depth |
int |
Current scope nesting depth |
is_inserted |
bool |
Whether token was auto-inserted |
is_removed |
bool |
Whether token was removed |
metadata |
dict |
Arbitrary key-value metadata |
Lexer Configuration Options
| Option |
Default |
Description |
allow_hash_comments |
True |
Enable # line comments |
allow_cpp_line_comments |
False |
Enable // line comments |
allow_c_block_comments |
True |
Enable /* */ block comments (nested) |
max_token_length |
1,000,000 |
Max token text length |
allow_multiline_strings |
True |
Enable """...""" strings |
allow_format_strings |
True |
Enable f"..." interpolation |
allow_raw_strings |
True |
Enable r"..." raw strings |
allow_byte_strings |
True |
Enable b"..." byte strings |
allow_unicode_identifiers |
True |
Enable Unicode in identifiers |
normalize_unicode |
False |
NFC normalize Unicode identifiers |
recover_from_errors |
True |
Continue lexing after errors |
max_consecutive_errors |
1000 |
Max errors before aborting |
enable_position_tracking |
True |
Track line/column/byte positions |
track_trivia |
False |
Capture whitespace/comment trivia |
String Types
| Prefix |
Example |
Description |
| (none) |
"hello" / 'hello' |
Regular string with escape processing |
r / R |
r"no\nescapes" |
Raw string — backslashes are literal |
b / B |
b"byte data" |
Byte string — raw bytes |
f / F |
f"hello {name}" |
Format string — {expr} interpolation |
""" |
"""multi\nline""" |
Triple-quoted multiline string |
Escape Sequences
| Escape |
Character |
Code |
\n |
Newline |
0x0A |
\t |
Tab |
0x09 |
\r |
Carriage return |
0x0D |
\\ |
Backslash |
0x5C |
\" |
Double quote |
0x22 |
\' |
Single quote |
0x27 |
\0 |
Null byte |
0x00 |
\a |
Bell/Alert |
0x07 |
\b |
Backspace |
0x08 |
\f |
Form feed |
0x0C |
\v |
Vertical tab |
0x0B |
\xHH |
Hex byte |
0x00-0xFF |
\uHHHH |
Unicode (4 hex) |
U+0000-U+FFFF |
\U00HHHHHH |
Unicode (8 hex) |
Full range |
| Format |
Example |
Description |
| Decimal |
42, 1_000_000 |
Standard integers (underscore separators allowed) |
| Hex |
0xFF, 0XFF |
Hexadecimal |
| Octal |
0o77, 0O77 |
Octal |
| Binary |
0b1010, 0B1010 |
Binary |
| Float |
3.14, .5, 1. |
Floating point |
| Scientific |
1e10, 2.5e-3 |
Scientific notation |
TokenRegistry (Runtime-Extensible)
The TokenRegistry class enables dynamic extension of the tokenizer at runtime:
// Register a new keyword
registry.register_keyword("myword", MY_TOKEN)
// Register a new operator with precedence
registry.register_operator("==>", MY_OP, precedence: 5, assoc: "right")
// Add token transformer hooks
registry.add_transformer(fn(token) {
// Transform tokens in-flight
return token
})
// Soft keywords (context-sensitive)
// These are only treated as keywords in specific contexts:
// async, await, match, trait, macro
All 150+ Token Types (Complete with Symbols)
Click to expand ALL token types with their symbols
Specials & Literals (9 tokens)
| TokenType |
Symbol/Usage |
Description |
ILLEGAL |
(invalid char) |
Unrecognized character |
EOF |
(end of file) |
End of input |
IDENT |
myVar, _count, 名前 |
Identifier (Unicode-aware) |
INT |
42, 1_000 |
Integer literal |
FLOAT |
3.14, 1e10 |
Float literal |
STRING |
"hello", 'hi' |
String literal |
BINARY |
0b1010 |
Binary integer literal |
OCTAL |
0o77 |
Octal integer literal |
HEX |
0xFF |
Hexadecimal integer literal |
Single-Character Operators (17 tokens)
| TokenType |
Symbol |
Description |
ASSIGN |
= |
Assignment |
PLUS |
+ |
Addition |
MINUS |
- |
Subtraction / Negation |
BANG |
! |
Logical NOT |
ASTERISK |
* |
Multiplication / Dereference |
SLASH |
/ |
Division |
MODULO |
% |
Modulo / Remainder |
BITWISE_AND |
& |
Bitwise AND / Reference |
BITWISE_OR |
\| |
Bitwise OR |
BITWISE_XOR |
^ |
Bitwise XOR |
BITWISE_NOT |
~ |
Bitwise NOT |
LT |
< |
Less than |
GT |
> |
Greater than |
AT |
@ |
Decorator |
HASH |
# |
Hash / Comment |
QUESTION |
? |
Optional / Ternary |
DOT |
. |
Member access |
Delimiters (8 tokens)
| TokenType |
Symbol |
Description |
COMMA |
, |
Separator |
SEMICOLON |
; |
Statement terminator (optional) |
COLON |
: |
Type annotation / Object key |
LPAREN |
( |
Open parenthesis |
RPAREN |
) |
Close parenthesis |
LBRACE |
{ |
Open brace |
RBRACE |
} |
Close brace |
LBRACKET |
[ |
Open bracket |
RBRACKET |
] |
Close bracket |
Two-Character Operators (26 tokens)
| TokenType |
Symbol |
Description |
EQ |
== |
Equality |
NOT_EQ |
!= |
Not equal |
LE |
<= |
Less or equal |
GE |
>= |
Greater or equal |
LOGICAL_AND |
&& |
Logical AND (short-circuit) |
LOGICAL_OR |
\|\| |
Logical OR (short-circuit) |
PLUS_ASSIGN |
+= |
Add-assign |
MINUS_ASSIGN |
-= |
Subtract-assign |
ASTERISK_ASSIGN |
*= |
Multiply-assign |
SLASH_ASSIGN |
/= |
Divide-assign |
MODULO_ASSIGN |
%= |
Modulo-assign |
AMPERSAND_ASSIGN |
&= |
Bitwise AND-assign |
PIPE_ASSIGN |
\|= |
Bitwise OR-assign |
XOR_ASSIGN |
^= |
Bitwise XOR-assign |
COLON_ASSIGN |
:= |
Walrus operator |
ARROW |
=> |
Fat arrow |
THIN_ARROW |
-> |
Return type annotation |
FLOOR_DIVIDE |
// |
Integer division |
POWER |
** |
Exponentiation |
RANGE |
.. |
Exclusive range |
NULL_COALESCE |
?? |
Null coalescing |
QUESTION_DOT |
?. |
Optional chaining |
PIPELINE |
\|> |
Pipeline operator |
ELVIS |
?: |
Elvis operator |
DOUBLE_COLON |
:: |
Namespace / Path separator |
DOUBLE_HASH |
## |
Macro concatenation |
LEFT_SHIFT |
<< |
Left bit shift |
RIGHT_SHIFT |
>> |
Right bit shift |
Three-Character Operators (7 tokens)
| TokenType |
Symbol |
Description |
FLOOR_DIVIDE_ASSIGN |
//= |
Floor-divide-assign |
LEFT_SHIFT_ASSIGN |
<<= |
Left-shift-assign |
RIGHT_SHIFT_ASSIGN |
>>= |
Right-shift-assign |
POWER_ASSIGN |
**= |
Power-assign |
OR_ASSIGN |
\|\|= |
Logical OR-assign |
AND_ASSIGN |
&&= |
Logical AND-assign |
NULL_COALESCE_ASSIGN |
??= |
Null-coalesce-assign |
SPACESHIP |
<=> |
Three-way comparison |
SPREAD |
... |
Spread / Rest |
RANGE_INCLUSIVE |
..= |
Inclusive range |
SAFE_CAST |
as? |
Safe type cast |
All 82 Keywords
| Category |
Keywords (82 total) |
| Declaration (7) |
fn, let, mut, const, var, true, false |
| Control Flow (19) |
if, else, elif, return, while, for, in, break, continue, print, match, case, when, where, loop, do, goto, defer, pass |
| OOP (11) |
class, struct, trait, interface, impl, enum, super, self, new, extends, implements |
| Module (10) |
import, use, from, as, export, pub, priv, mod, namespace, package |
| Error Handling (7) |
try, catch, except, finally, raise, throw, assert |
| Async (9) |
with, yield, async, await, spawn, channel, select, lock, actor |
| Types (9) |
type, typeof, instanceof, is, static, dynamic, any, void, never |
| Meta (16) |
null, none, undefined, macro, inline, unsafe, extern, ref, move, copy, sizeof, alignof, global, static_assert, comptime |
Parser Deep Dive (src/parser.py)
A Pratt parser (top-down operator precedence parsing) that converts the token stream into a typed AST.
Precedence Levels (11 levels)
| Level |
Name |
Operators |
Associativity |
| 1 |
LOWEST |
(default) |
— |
| 2 |
ASSIGN |
=, +=, -=, *=, /=, %=, //=, := |
Right |
| 3 |
YIELD |
yield |
Right |
| 4 |
LOGICAL |
&&, \|\| |
Left |
| 5 |
EQUALS |
==, != |
Left |
| 6 |
LESSGREATER |
<, >, <=, >= |
Left |
| 7 |
SUM |
+, - |
Left |
| 8 |
PRODUCT |
*, /, **, %, // |
Left |
| 9 |
PREFIX |
-, !, ~ (unary) |
Right |
| 10 |
CALL |
(), . |
Left |
| 11 |
INDEX |
[] |
Left |
All Prefix Parse Functions (25)
| Token |
Method |
Produces |
IDENT |
parse_identifier() |
Identifier |
INT |
parse_integer_literal() |
IntegerLiteral |
FLOAT |
parse_float_literal() |
FloatLiteral |
BINARY |
parse_binary_literal() |
BinaryLiteral |
OCTAL |
parse_octal_literal() |
OctalLiteral |
HEX |
parse_hex_literal() |
HexLiteral |
STRING |
parse_string_literal() |
StringLiteral |
BANG |
parse_prefix_expression() |
PrefixExpression("!") |
MINUS |
parse_prefix_expression() |
PrefixExpression("-") |
BITWISE_NOT |
parse_prefix_expression() |
PrefixExpression("~") |
TRUE |
parse_boolean() |
BooleanLiteral(true) |
FALSE |
parse_boolean() |
BooleanLiteral(false) |
LPAREN |
parse_grouped_expression() |
Inner expression |
IF |
parse_if_expression() |
IfExpression |
FUNCTION |
parse_function_literal() |
FunctionLiteral |
LBRACKET |
parse_array_literal() |
ArrayLiteral |
LBRACE |
parse_hash_literal() |
HashLiteral |
NULL |
parse_null_literal() |
NullLiteral |
SUPER |
parse_super_expression() |
SuperExpression |
SELF |
parse_self_expression() |
SelfExpression |
NEW |
parse_new_expression() |
NewExpression |
AWAIT |
parse_await_expression() |
AwaitExpression |
YIELD |
parse_yield_expression() |
YieldExpression |
PRINT |
parse_print_identifier() |
Identifier("print") |
All Infix Parse Functions (14)
| Tokens |
Method |
Produces |
+, -, *, /, **, %, // |
parse_infix_expression() |
InfixExpression |
==, !=, <, >, <=, >= |
parse_infix_expression() |
InfixExpression |
&&, \|\| |
parse_infix_expression() |
InfixExpression |
. |
parse_infix_expression() |
InfixExpression(".") |
( |
parse_call_expression() |
CallExpression |
[ |
parse_index_expression() |
IndexExpression |
All *= tokens |
parse_assign_expression() |
AssignExpression |
All Statement Parsers (20)
| Token |
Method |
Produces |
; |
(skip) |
None |
let |
parse_let_statement() |
LetStatement |
return |
parse_return_statement() |
ReturnStatement |
class |
parse_class_statement() |
ClassStatement |
import |
parse_import_statement() |
ImportStatement |
use |
parse_use_statement() |
UseStatement |
from |
parse_from_statement() |
FromStatement (supports * wildcard) |
try |
parse_try_statement() |
TryStatement |
raise |
parse_raise_statement() |
RaiseStatement |
assert |
parse_assert_statement() |
AssertStatement (optional message) |
with |
parse_with_statement() |
WithStatement |
async |
parse_async_statement() |
AsyncStatement |
pass |
parse_pass_statement() |
PassStatement |
break |
parse_break_statement() |
BreakStatement |
continue |
parse_continue_statement() |
ContinueStatement |
while |
parse_while_statement() |
WhileStatement |
for |
parse_for_statement() |
ForStatement or ForInStatement |
ILLEGAL |
(error recovery) |
Synchronize + skip |
| (other) |
parse_expression_statement() |
ExpressionStatement |
For-Loop Variants
// C-style for loop → ForStatement
for (let i = 0; i < 10; i = i + 1) { ... }
// Single-variable for-in → ForInStatement
for item in collection { ... }
// Dual-variable for-in (key, value) → ForInStatement
for key, value in object { ... }
// Dual-variable for-in (index, element) → ForInStatement
for i, elem in array { ... }
Parser Extension API
// Register custom prefix parse function
parser.register_prefix(MY_TOKEN, fn(parser) { ... })
// Register custom infix parse function
parser.register_infix(MY_TOKEN, fn(parser, left) { ... })
// Register custom statement parser
parser.register_statement(MY_TOKEN, fn(parser) { ... })
// Register error hook for custom error handling
parser.register_error_hook(fn(error_msg) { ... })
Error Recovery
The parser uses statement synchronization to recover from errors:
- On parse error → record error message
- Skip tokens until finding a synchronization point:
let, fn, class, return, if, while, for, try, import, use, or }
- Resume parsing from the synchronization point
- Configurable:
max_errors (default: 200), stop_on_first_error (default: false)
All 70+ AST Node Types (Complete with Fields)
Click to expand COMPLETE AST node hierarchy with all fields
Base Infrastructure
| Class |
Fields |
Description |
SourceLocation |
line, column, byte_offset, byte_length, source_file |
Source position info |
Node |
token, location, parent, metadata, resolved_type, scope |
Base for all nodes |
Statement(Node) |
(inherited) |
Base for statements |
Expression(Node) |
(inherited) |
Base for expressions |
NodeVisitor |
visit(), generic_visit() |
Visitor pattern base |
Program
| Class |
Fields |
Program |
statements: List[Statement] |
Literals (8 nodes)
| Class |
Fields |
Example |
IntegerLiteral |
value: int |
42 |
FloatLiteral |
value: float |
3.14 |
BinaryLiteral |
value: str |
0b1010 |
OctalLiteral |
value: str |
0o77 |
HexLiteral |
value: str |
0xFF |
StringLiteral |
value: str |
"hello" |
BooleanLiteral |
value: bool |
true |
NullLiteral |
— |
null |
Core Expressions (12 nodes)
| Class |
Fields |
Example |
Identifier |
value: str |
myVar |
PrefixExpression |
operator: str, right: Expression |
!x, -5, ~bits |
InfixExpression |
left, operator: str, right |
a + b, x == y |
AssignExpression |
name, value |
x = 42 |
IfExpression |
condition, consequence, alternative |
if cond { } else { } |
FunctionLiteral |
parameters, body, name |
fn foo(x) { } |
CallExpression |
function, arguments |
foo(1, 2) |
ArrayLiteral |
elements: List |
[1, 2, 3] |
IndexExpression |
left, index |
arr[0] |
HashLiteral |
pairs: Dict |
{a: 1, b: 2} |
SelfExpression |
— |
self |
SuperExpression |
— |
super |
NewExpression |
cls |
new MyClass(args) |
Advanced Expressions (10 nodes)
| Class |
Fields |
Example |
RangeExpression |
start, end, inclusive: bool |
0..10, 0..=10 |
SpreadExpression |
expression |
...arr |
PipelineExpression |
left, right |
data \|> map(fn) |
OptionalChainingExpression |
object, property |
obj?.prop |
NullCoalescingExpression |
left, right |
x ?? default |
LambdaExpression |
parameters, body, is_async |
\|x\| x * 2 |
ComprehensionExpression |
element, iterator, iterable, condition, is_dict |
[x*2 for x in arr] |
TernaryExpression |
condition, true_value, false_value |
cond ? a : b |
YieldExpression |
value |
yield 42 |
AwaitExpression |
expression |
await fetch() |
Statements (10 nodes)
| Class |
Fields |
LetStatement |
name: Identifier, value: Expression |
ReturnStatement |
return_value: Expression |
ExpressionStatement |
expression: Expression |
BlockStatement |
statements: List[Statement] |
WhileStatement |
condition: Expression, body: BlockStatement |
ForStatement |
initialization, condition, increment, body |
ForInStatement |
iterator: Identifier, iterable: Expression, body |
BreakStatement |
— |
ContinueStatement |
— |
PassStatement |
— |
OOP Nodes (11 nodes)
| Class |
Fields |
ClassStatement |
name: str, superclass, body: BlockStatement |
StructDeclaration |
name: str, fields: List[StructField] |
StructField |
name: str, field_type, default_value |
EnumDeclaration |
name: str, variants: List[EnumVariant] |
EnumVariant |
name: str, value, fields |
TraitDeclaration |
name: str, methods: List[MethodSignature], supertraits |
MethodSignature |
name: str, parameters, return_type |
ImplBlock |
trait, type_name: str, methods |
Module & Export Nodes (5 nodes)
| Class |
Fields |
ImportStatement |
path: str |
UseStatement |
module: str |
FromStatement |
path: str, imports: List[str] |
ExportStatement |
names: List[str], statement |
ModuleDeclaration |
name: str, body: BlockStatement |
NamespaceDeclaration |
name: str, body: BlockStatement |
Error Handling Nodes (4 nodes)
| Class |
Fields |
TryStatement |
try_block, except_block, finally_block |
RaiseStatement |
exception: Expression |
AssertStatement |
condition: Expression, message: Expression |
StaticAssertStatement |
condition, message |
Advanced Control Flow Nodes (6 nodes)
| Class |
Fields |
LoopStatement |
body: BlockStatement |
DeferStatement |
statement: Statement |
SelectStatement |
cases: List[SelectCase] |
SelectCase |
channel_op, body |
GuardStatement |
condition, else_block |
UnsafeBlock |
body: BlockStatement |
Pattern Matching Nodes (7 nodes)
| Class |
Fields |
MatchExpression |
value: Expression, cases: List[CaseClause] |
CaseClause |
pattern: Pattern, guard, body |
LiteralPattern |
value |
IdentifierPattern |
name: str |
StructPattern |
struct_name: str, fields |
ArrayPattern |
elements, rest |
WildcardPattern |
— |
Type Annotation Nodes (6 nodes)
| Class |
Fields |
SimpleType |
name: str |
GenericType |
base: str, type_params: List |
UnionType |
types: List[TypeAnnotation] |
FunctionType |
param_types, return_type |
OptionalType |
inner_type: TypeAnnotation |
| Class |
Fields |
DecoratorExpression |
name: str, arguments |
MacroInvocation |
name: str, arguments |
MacroDefinition |
name: str, rules: List[MacroRule] |
MacroRule |
pattern, expansion |
ComptimeExpression |
expression: Expression |
Extension System
| Class |
Fields |
Description |
DynamicNode |
kind: str, payload: dict |
Runtime-extensible node type |
Global registry: NODE_REGISTRY: Dict[str, Type[Node]] — register new node types with register_node(name, cls).
Interpreter Deep Dive (src/interpreter.py)
An async tree-walking evaluator that directly traverses the AST and produces values.
Environment (Scope Chain)
┌─────────────────┐
│ Global Env │ Built-ins: print, len, range, max, min, sum, abs, round, str, int, float
│ store: {...} │ Modules: resolved imports
└────────┬────────┘
│ outer
┌────────▼────────┐
│ Function Env │ Parameters bound here
│ store: {...} │ Local variables
└────────┬────────┘
│ outer
┌────────▼────────┐
│ Block Env │ Block-scoped variables
│ store: {...} │
└─────────────────┘
define(name, value) — always defines in the local scope
set(name, value) — searches up the chain; defines locally if not found anywhere
get(name) — searches up the chain; raises NameError if not found at any level
Boxed Value Types
The interpreter wraps Python values into boxed types for type safety:
| Boxed Type |
Python Type |
Fields |
Boxing Rule |
Integer |
int |
value: int |
bool → int first, then Integer |
Float |
float |
value: float |
Float(value) |
Boolean |
bool |
value: bool |
Boolean(value) |
String |
str |
value: str |
String(value) |
Null |
_Null |
— |
Singleton NULL |
Array |
list |
elements: List |
Recursively wraps elements |
Error |
— |
message: str |
Runtime errors |
Signal System
| Signal |
When Raised |
Caught By |
ReturnSignal |
return expr |
UserFunction.call() |
BreakSignal |
break |
while/for loops |
ContinueSignal |
continue |
while/for loops |
NyxClass and UserFunction
# NyxClass — class wrapper
class NyxClass:
name: str # Class name
methods: Dict[str, UserFunction] # Method map
def instantiate(interpreter, args):
instance = {"__class__": self} # Instance is a dict!
if "init" in methods:
methods["init"].call(interpreter, [instance] + args)
return instance
# UserFunction — function wrapper
class UserFunction:
parameters: List[Identifier] # Parameter names
body: BlockStatement # Function body AST
env: Environment # Closure environment
def call(interpreter, args):
local_env = Environment(outer=self.env) # New scope
for param, arg in zip(parameters, args):
local_env.define(param.value, arg) # Bind args
try:
interpreter.eval(body, local_env)
except ReturnSignal as rs:
return rs.value
Complete eval() Dispatch Table
| AST Node |
Evaluation Behavior |
Program |
Evaluate all statements sequentially, return last |
BlockStatement |
Evaluate all statements in sequence |
ExpressionStatement |
Evaluate the inner expression |
LetStatement |
Evaluate value → env.define(name, value) |
ReturnStatement |
Evaluate value → raise ReturnSignal(value) |
BreakStatement |
Raise BreakSignal |
ContinueStatement |
Raise ContinueSignal |
ImportStatement |
resolve_module(path) → merge into env |
UseStatement |
resolve_module(module) → merge into env |
FromStatement |
Resolve module → import specific names (supports *) |
Identifier |
Check builtins first → then env.get(name) |
SelfExpression |
env.get("self") |
IntegerLiteral |
Return node.value |
FloatLiteral |
Return node.value |
StringLiteral |
Return node.value |
BooleanLiteral |
Return node.value |
NullLiteral |
Return NULL singleton |
ArrayLiteral |
Evaluate all elements → return list |
HashLiteral |
Evaluate all key-value pairs → return dict |
IndexExpression |
Evaluate left + index → return left[index] |
PrefixExpression |
! → logical not; - → negate (error on bool); ~ → bitwise not |
InfixExpression |
Dispatch by operator (see operator table below) |
AssignExpression |
Evaluate value → env.set(name, value) or obj[member] = value |
IfExpression |
Test condition → evaluate consequence or alternative |
WhileStatement |
Loop: test condition → body, catch Break/Continue |
ForStatement |
Init → loop(test → body → increment), catch Break/Continue |
ForInStatement |
Iterate over iterable, bind to iterator var |
FunctionLiteral |
Create UserFunction(params, body, current_env) |
ClassStatement |
Extract methods → create NyxClass → define in env |
NewExpression |
Evaluate class → NyxClass.instantiate(args) |
CallExpression |
Evaluate function → dispatch: NyxClass / UserFunction / Python callable |
All Operator Implementations
| Operator |
Left Type |
Right Type |
Behavior |
+ |
int/float |
int/float |
Arithmetic addition |
+ |
string |
any |
String concatenation (auto str()) |
+ |
any |
string |
String concatenation (auto str()) |
+ |
bool |
any |
Runtime error — no bool arithmetic |
- |
int/float |
int/float |
Subtraction (error on bools) |
* |
int/float |
int/float |
Multiplication (error on bools) |
/ |
int/float |
int/float |
Division (error on bools) |
% |
int/float |
int/float |
Modulo |
** |
int/float |
int/float |
Power |
== |
any |
any |
Equality comparison |
!= |
any |
any |
Not-equal comparison |
< |
int/float |
int/float |
Less than |
<= |
int/float |
int/float |
Less or equal |
> |
int/float |
int/float |
Greater than |
>= |
int/float |
int/float |
Greater or equal |
&& |
any |
any |
Short-circuit AND via _truthy() |
\|\| |
any |
any |
Short-circuit OR via _truthy() |
. |
dict |
string |
Member access → dict[key] |
. |
instance |
string |
Method resolution via __class__ |
- (prefix) |
— |
int/float |
Negate (error on bools) |
! (prefix) |
— |
any |
Logical NOT via _truthy() |
~ (prefix) |
— |
int |
Bitwise NOT → ~int(right) |
Truthiness Rules (_truthy())
| Value |
Truthy? |
NULL / _Null |
false |
0 / 0.0 |
false |
"" (empty string) |
false |
[] (empty array) |
false |
false |
false |
| Everything else |
true |
Interpreter Extension API
// Register a new built-in function
interpreter.register_builtin("my_fn", fn(args) { ... })
// Register a module (importable via `import`)
interpreter.register_module("my_module", {
foo: fn() { ... },
bar: 42
})
// Register a fallback import resolver
interpreter.register_import_resolver(fn(name) {
// Custom module resolution logic
return module_dict_or_none
})
Resource Limits
| Limit |
Default |
CLI Flag |
| Max execution steps |
1,000,000 |
--max-steps N |
| Max call depth |
(Python limit) |
--max-call-depth N |
| Max memory allocs |
(unlimited) |
--max-alloc N |
Entry Point (run.py)
# Simplified pipeline:
source = read_file(sys.argv[1])
lexer = Lexer(source)
parser = Parser(lexer)
program = parser.parse_program()
if parser.errors:
for e in parser.errors: print(e)
sys.exit(1)
env = Environment()
result = asyncio.run(evaluate(program, env))
if result is not Null and hasattr(result, 'inspect'):
print(result)
🔒 DFAS — Dynamic Field Arithmetic System
A complete modular finite field arithmetic library for cryptographic and mathematical applications.
Located in stdlib/dfas/ — 10 specialized modules.
Architecture
┌──────────────────────────────────────────────────┐
│ DFAS System │
├──────────────────────────────────────────────────┤
│ field_core.ny - Field types & elements │
│ arithmetic.ny - Field operations engine │
│ type_system.ny - Type checking & inference │
│ safety.ny - Security validation │
│ encryption.ny - Field-based encryption │
│ compiler.ny - Optimization compiler │
│ tests.ny - Test suite │
│ examples.ny - Usage examples │
│ benchmarks.ny - Performance benchmarks │
│ __init__.ny - Module init │
└──────────────────────────────────────────────────┘
field_core.ny — Types & Definitions
// Field types
enum FieldType {
PrimeField, // GF(p) — prime fields
PolynomialField, // GF(p^n) — extension fields
SecureField, // Seed-generated secure fields
CustomField // User-defined fields
}
// Reduction strategies
enum ReductionType {
Standard, // Basic modular reduction
Barrett, // Barrett reduction (faster for known moduli)
Montgomery // Montgomery multiplication (fastest for repeated ops)
}
// Field configuration
struct FieldConfig {
field_type: FieldType,
modulus: int,
characteristic: int,
degree: int,
polynomial_coeffs: array,
seed: string,
reduction_method: ReductionType,
is_secure: bool,
field_id: string
}
// Field element
struct FieldElement {
value: int,
field_config: FieldConfig,
montgomery_form: int,
is_normalized: bool
}
// Create fields
let prime_field = FieldConfig.prime_field(17) // GF(17)
let ext_field = FieldConfig.polynomial_field(2, 8, [...]) // GF(2^8) — AES field
let secure = FieldConfig.secure_field("myseed", 256) // 256-bit secure field
arithmetic.ny — Operations Engine
// Arithmetic operations
fn field_add(a: FieldElement, b: FieldElement) -> FieldElement
fn field_sub(a: FieldElement, b: FieldElement) -> FieldElement
fn field_mul(a: FieldElement, b: FieldElement) -> FieldElement
fn field_div(a: FieldElement, b: FieldElement) -> FieldElement // modular inverse + mul
fn field_pow(a: FieldElement, exp: int) -> FieldElement // fast exponentiation
fn field_neg(a: FieldElement) -> FieldElement // additive inverse
fn field_inv(a: FieldElement) -> FieldElement // multiplicative inverse
// Montgomery operations (hardware-friendly)
fn montgomery_multiply(a, b, params: MontgomeryParams) -> int
fn to_montgomery(value, params) -> int
fn from_montgomery(value, params) -> int
encryption.ny — Field-Based Encryption
// Block cipher using field arithmetic
fn field_encrypt(plaintext: array, key: FieldElement, field: FieldConfig) -> array
fn field_decrypt(ciphertext: array, key: FieldElement, field: FieldConfig) -> array
// Stream cipher
fn field_stream_encrypt(data: array, seed: string, field: FieldConfig) -> array
// Key derivation
fn field_derive_key(password: string, salt: string, field: FieldConfig) -> FieldElement
safety.ny — Security Validation
fn validate_field_security(config: FieldConfig) -> SecurityReport
fn check_timing_resistance(operation: fn, field: FieldConfig) -> bool
fn audit_field_operations(operations: array) -> AuditReport
📦 NYPM — Nyx Package Manager
Full-featured package manager inspired by npm/cargo/pip.
Commands
| Command |
Description |
Example |
nypm init |
Initialize new project |
nypm init my-project |
nypm install <pkg> |
Install a package |
nypm install json-parser |
nypm add <pkg> |
Alias for install |
nypm add http-server |
nypm remove <pkg> |
Remove a package |
nypm remove json-parser |
nypm rm <pkg> |
Alias for remove |
nypm rm http-server |
nypm update |
Update all packages |
nypm update |
nypm search <query> |
Search registry |
nypm search http |
nypm list |
List installed packages |
nypm list |
nypm info <pkg> |
Show package info |
nypm info json-parser |
nypm versions <pkg> |
List available versions |
nypm versions http-server |
nypm outdated |
Check for updates |
nypm outdated |
nypm publish |
Publish package to registry |
nypm publish |
nypm clean |
Clean node_modules cache |
nypm clean |
nypm doctor |
Diagnose issues |
nypm doctor |
Configuration (nypm.config)
[registry]
url = https://registry.nyxlang.org
auth_token =
[defaults]
save_exact = true
production = false
global_folder = ~/.nypm/global
[network]
timeout = 30000
retries = 3
proxy =
[cache]
folder = ~/.nypm/cache
max_size = 500MB
Package Manifest (ny.registry)
[package]
name = "my-nyx-project"
version = "1.0.0"
description = "My project"
main = "main.ny"
author = "Your Name"
license = "MIT"
repository = ""
[dependencies]
[devDependencies]
[scripts]
start = "nyx main.ny"
test = "nyx test.ny"
build = "nyx build main.ny"
⚙️ Production Configuration
AAA game engine-scale production configs (located in configs/production/).
| Config File |
Description |
team_roster.json |
800+ person team structure: leads, engineers, artists, QA, DevOps |
platform_cert.json |
Sony TRC, Microsoft XR, Nintendo Lotcheck, Apple Review, Google Play |
multi_year_plan.json |
3-year development roadmap with milestones |
liveops_slo.json |
Service Level Objectives: 99.99% uptime, <100ms latency, 10M concurrent |
hardware_matrix.json |
PS5, Xbox Series X/S, Steam Deck, Switch 2, PC min/rec/ultra specs |
gta_scale_program.json |
Open-world game plan: 100km² map, 10K NPCs, 128-player multiplayer |
gate_thresholds.json |
Quality gates: 60 FPS minimum, <3s load times, <0.1% crash rate |
cook_profile.json |
Asset cooking: texture compression, mesh LODs, animation baking |
content_targets.json |
Content requirements: 200+ hours gameplay, 50+ story missions |
anti_cheat_rules.json |
Anti-cheat: memory scanning, packet validation, behavior analysis |
engine_feature_contract.json |
Engine API stability guarantees across versions |
� Native Compiler (v3.3.3)
Nyx compiles to native machine code via C. Zero dependencies. Single binary output.
Compilation Pipeline
Source Code (.ny)
│
▼
┌──────────────────────┐
│ Nyx Native Compiler │ compiler/v3_compiler_template.c
│ (Self-contained C) │ Single-file C99 compiler
└──────────┬───────────┘
│ C source output
▼
┌──────────────────────┐
│ GCC / Clang / MSVC │ Makefile: gcc -O2 -std=c99 -Wall -Wextra -Werror
└──────────┬───────────┘
│
▼
Native Binary (ELF / PE / Mach-O)
Compiler Architecture
The native compiler is a complete self-contained C99 program with its own lexer, parser, and code generator:
| Component |
Description |
| Lexer |
52 token types covering all Nyx tokens (identifiers, literals, operators, keywords) |
| Token |
TokenType type, long long int_val, char text[1024], int line, int col |
| Parser |
Recursive descent parser producing an AST with 13 expression kinds and 19 statement kinds |
| Expression Kinds |
EX_INT, EX_STRING, EX_BOOL, EX_NULL, EX_IDENT, EX_ARRAY, EX_ARRAY_COMP, EX_OBJECT, EX_INDEX, EX_DOT, EX_UNARY, EX_BINARY, EX_CALL |
| Statement Kinds |
ST_LET, ST_ASSIGN, ST_SET_MEMBER, ST_SET_INDEX, ST_EXPR, ST_IF, ST_SWITCH, ST_WHILE, ST_FOR, ST_BREAK, ST_CONTINUE, ST_CLASS, ST_MODULE, ST_TYPE, ST_TRY, ST_FN, ST_RETURN, ST_THROW, ST_IMPORT |
| Version |
NYX_LANG_VERSION "3.3.3" (configurable via -DNYX_LANG_VERSION=...) |
Build Commands
# Build using Makefile (recommended)
make all # Produces build/nyx
# Manual compilation
gcc -O2 -std=c99 -Wall -Wextra -Werror -DNYX_LANG_VERSION=\"3.3.3\" -o nyx native/nyx.c
# Cross-compile for Linux on Windows
x86_64-linux-gnu-gcc -O2 -std=c99 -o nyx-linux native/nyx.c
# Cross-compile for macOS (with osxcross)
o64-clang -O2 -std=c99 -o nyx-macos native/nyx.c
# Build with debug symbols
gcc -g -O0 -std=c99 -o nyx-debug native/nyx.c
# Build with sanitizers
gcc -O1 -std=c99 -fsanitize=address,undefined -o nyx-asan native/nyx.c
# Build optimized release
gcc -O3 -std=c99 -march=native -flto -o nyx-release native/nyx.c
# Clean
make clean
Compiler Flags
| Flag |
Description |
-O2 |
Default optimization level (speed + size balance) |
-O3 |
Maximum optimization (aggressive inlining, vectorization) |
-Os |
Optimize for size |
-std=c99 |
C99 standard (wide compatibility) |
-Wall -Wextra -Werror |
All warnings + warnings-as-errors |
-march=native |
Optimize for current CPU architecture |
-flto |
Link-time optimization (cross-file inlining) |
-DNYX_LANG_VERSION=\"X.Y.Z\" |
Set version string |
Three Execution Modes
| Mode |
Command |
Speed |
Use Case |
| Native Binary |
./build/nyx program.ny |
Fastest |
Production, deployment |
| Python Interpreter |
python run.py program.ny |
Moderate |
Development, debugging |
| Web Runtime |
python nyx_runtime.py |
Web-optimized |
Web applications, APIs |
Bootstrap Compiler
The compiler/bootstrap.ny file is a self-hosting Nyx compiler written in Nyx itself:
// The Nyx compiler, written in Nyx
// This bootstraps the language: Nyx compiles Nyx
import std/io
import std/string
fn tokenize(source: str) -> array {
// Lexer implementation in Nyx
let tokens = []
let pos = 0
while pos < len(source) {
// ... tokenization logic
}
return tokens
}
fn parse(tokens: array) -> AST {
// Parser implementation in Nyx
// Pratt parser with 12 precedence levels
}
fn codegen(ast: AST) -> str {
// Generate C code from AST
}
🔧 All Built-in Functions (Complete Reference)
These work everywhere with no imports. They are part of the language core — available in both the interpreter and native compiler.
Interpreter Built-ins (11 functions)
| Function |
Signature |
Example |
Return |
Description |
print |
print(...values) |
print("hello", 42) |
null |
Print values to stdout (space-separated) |
len |
len(value) |
len([1,2,3]) → 3 |
int |
Length of string, array, or object |
range |
range(n) |
range(5) → [0,1,2,3,4] |
array |
Generate [0, 1, ..., n-1] |
max |
max(array) |
max([3,1,2]) → 3 |
int/float |
Maximum value in array |
min |
min(array) |
min([3,1,2]) → 1 |
int/float |
Minimum value in array |
sum |
sum(array) |
sum([1,2,3]) → 6 |
int/float |
Sum of all elements |
abs |
abs(value) |
abs(-5) → 5 |
int/float |
Absolute value |
round |
round(value) |
round(3.7) → 4 |
int |
Round to nearest integer |
str |
str(value) |
str(42) → "42" |
string |
Convert any value to string |
int |
int(value) |
int("42") → 42 |
int |
Convert to integer |
float |
float(value) |
float("3.14") → 3.14 |
float |
Convert to float |
Native Compiler Built-ins (v3.3.3 — 35+ functions)
Output
| Function |
Example |
Description |
print(...) |
print("hello", 42) |
Print values to console |
Type Checking (7 functions)
| Function |
Example |
Returns |
Description |
type_of(x) |
type_of(42) → "int" |
string |
Get type name as string |
is_int(x) |
is_int(42) → true |
bool |
Check if integer |
is_bool(x) |
is_bool(true) → true |
bool |
Check if boolean |
is_string(x) |
is_string("hi") → true |
bool |
Check if string |
is_array(x) |
is_array([1]) → true |
bool |
Check if array |
is_function(x) |
is_function(print) → true |
bool |
Check if function |
is_null(x) |
is_null(null) → true |
bool |
Check if null |
Conversion (2 functions)
| Function |
Example |
Description |
str(x) |
str(42) → "42" |
Convert to string |
int(x) |
int("42") → 42 |
Convert to integer |
Collections (7 functions)
| Function |
Example |
Returns |
Description |
len(x) |
len([1,2,3]) → 3 |
int |
Length of collection |
push(arr, x) |
push(arr, 4) |
void |
Append element to end |
pop(arr) |
pop(arr) → last item |
any |
Remove and return last element |
keys(obj) |
keys({a:1}) → ["a"] |
array |
Get all object keys |
values(obj) |
values({a:1}) → [1] |
array |
Get all object values |
items(obj) |
items({a:1}) → [["a",1]] |
array |
Get key-value pairs |
has(obj, k) |
has({a:1}, "a") → true |
bool |
Check if key exists |
Math (6 functions)
| Function |
Example |
Returns |
Description |
abs(x) |
abs(-5) → 5 |
int/float |
Absolute value |
min(...) |
min(3, 1, 2) → 1 |
int/float |
Minimum of arguments |
max(...) |
max(3, 1, 2) → 3 |
int/float |
Maximum of arguments |
clamp(x, lo, hi) |
clamp(15, 0, 10) → 10 |
int/float |
Clamp value to range |
sum(arr) |
sum([1,2,3]) → 6 |
int/float |
Sum of array elements |
range(n) |
range(5) → [0,1,2,3,4] |
array |
Generate number sequence |
Logic (2 functions)
| Function |
Example |
Returns |
Description |
all(arr) |
all([true, true]) → true |
bool |
All elements truthy? |
any(arr) |
any([false, true]) → true |
bool |
Any element truthy? |
I/O (2 functions)
| Function |
Example |
Returns |
Description |
read(path) |
read("file.txt") |
string |
Read entire file contents |
write(path, data) |
write("out.txt", "hi") |
void |
Write string to file |
System (3 functions/values)
| Function |
Example |
Returns |
Description |
argc |
argc → 3 |
int |
Command-line argument count |
argv |
argv → ["prog", "a", "b"] |
array |
Command-line argument values |
lang_version() |
lang_version() → "3.3.3" |
string |
Nyx language version |
require_version(v) |
require_version("3.0.0") |
void |
Assert minimum Nyx version |
Object System (11 functions — native compiler only)
| Function |
Example |
Description |
new() |
new() → {} |
Create empty object |
object_new() |
object_new() → {} |
Create empty object (alias) |
object_set(obj, k, v) |
object_set(o, "x", 1) |
Set property on object |
object_get(obj, k) |
object_get(o, "x") → 1 |
Get property from object |
class_new(name) |
class_new("Dog") |
Create new class |
class_with_ctor(name, fn) |
class_with_ctor("Dog", init_fn) |
Create class with constructor |
class_set_method(cls, name, fn) |
class_set_method(Dog, "bark", fn) |
Add method to class |
class_name(cls) |
class_name(Dog) → "Dog" |
Get class name |
class_instantiate0(cls) |
class_instantiate0(Dog) |
Instantiate with 0 args |
class_instantiate1(cls, a) |
class_instantiate1(Dog, "Rex") |
Instantiate with 1 arg |
class_instantiate2(cls, a, b) |
class_instantiate2(Dog, "Rex", 3) |
Instantiate with 2 args |
Native Compiler Built-in Modules (v3.3.3)
| Module |
Import |
Functions |
Description |
nymath |
import nymath |
abs, min, max, clamp, pow, sum |
Math utilities |
nyarrays |
import nyarrays |
first, last, sum, enumerate |
Array helpers |
nyobjects |
import nyobjects |
merge, get_or |
Object merging & safe access |
nyjson |
import nyjson |
parse, stringify |
JSON encode/decode |
nyhttp |
import nyhttp |
get, text, ok |
HTTP client |
🖥️ VM Bytecode Specification
Stack-based bytecode virtual machine for optimized execution.
┌─────────────────────────────────────┐
│ Magic Number: NYX\0 (4 bytes) │
│ Version: u16 │
│ ┌─────────────────────────────────┐│
│ │ Constant Pool ││
│ │ ── Count: u16 ││
│ │ ── Entries: Value[] ││
│ │ int64 | f64 | string | fn ││
│ └─────────────────────────────────┘│
│ ┌─────────────────────────────────┐│
│ │ Bytecode Section ││
│ │ ── Length: u32 ││
│ │ ── Instructions: u8[] ││
│ └─────────────────────────────────┘│
│ ┌─────────────────────────────────┐│
│ │ Debug Info (optional) ││
│ │ ── Line number table ││
│ │ ── Local variable names ││
│ │ ── Source file path ││
│ └─────────────────────────────────┘│
└─────────────────────────────────────┘
Complete Instruction Set (47 opcodes)
Click to expand complete VM instruction set
Stack Operations (6 opcodes)
| Opcode |
Hex |
Name |
Stack Effect |
Description |
| 0 |
0x00 |
NOP |
— → — |
No operation |
| 1 |
0x01 |
POP |
a → — |
Discard top of stack |
| 2 |
0x02 |
DUP |
a → a a |
Duplicate top |
| 3 |
0x03 |
SWAP |
a b → b a |
Swap top two values |
| 4 |
0x04 |
ROT |
a b c → c a b |
Rotate top three |
| 5 |
0x05 |
PICK |
... n → ... v |
Copy nth item to top |
Constants & Literals (4 opcodes)
| Opcode |
Hex |
Name |
Stack Effect |
Operand |
Description |
| 16 |
0x10 |
NULL |
— → null |
— |
Push null value |
| 17 |
0x11 |
TRUE |
— → true |
— |
Push true |
| 18 |
0x12 |
FALSE |
— → false |
— |
Push false |
| 19 |
0x13 |
CONST |
— → value |
u16 index |
Push from constant pool |
Local Variables (4 opcodes)
| Opcode |
Hex |
Name |
Stack Effect |
Operand |
Description |
| 32 |
0x20 |
LOAD |
— → value |
u8 slot |
Load local variable |
| 33 |
0x21 |
STORE |
value → — |
u8 slot |
Store local variable |
| 34 |
0x22 |
LOADN |
— → value |
u16 slot |
Load local (wide) |
| 35 |
0x23 |
STOREN |
value → — |
u16 slot |
Store local (wide) |
Global Variables (3 opcodes)
| Opcode |
Hex |
Name |
Stack Effect |
Operand |
Description |
| 48 |
0x30 |
GLOAD |
— → value |
u16 name |
Load global |
| 49 |
0x31 |
GSTORE |
value → — |
u16 name |
Store global |
| 50 |
0x32 |
GDEF |
value → — |
u16 name |
Define new global |
Control Flow (7 opcodes)
| Opcode |
Hex |
Name |
Stack Effect |
Operand |
Description |
| 64 |
0x40 |
JUMP |
— → — |
i16 offset |
Unconditional jump |
| 65 |
0x41 |
JUMP_IF |
cond → — |
i16 offset |
Jump if truthy |
| 66 |
0x42 |
JUMP_NOT |
cond → — |
i16 offset |
Jump if falsy |
| 67 |
0x43 |
CALL |
fn a₁..aₙ → result |
u8 argc |
Call function |
| 68 |
0x44 |
RETURN |
value → — |
— |
Return from function |
| 69 |
0x45 |
CLOSURE |
fn → closure |
— |
Create closure capturing env |
| 70 |
0x46 |
SUPER |
— → method |
u16 name |
Access superclass method |
Arithmetic (8 opcodes)
| Opcode |
Hex |
Name |
Stack Effect |
Description |
| 80 |
0x50 |
ADD |
a b → a+b |
Add / string concat |
| 81 |
0x51 |
SUB |
a b → a-b |
Subtract |
| 82 |
0x52 |
MUL |
a b → a*b |
Multiply |
| 83 |
0x53 |
DIV |
a b → a/b |
Divide (float) |
| 84 |
0x54 |
MOD |
a b → a%b |
Modulo |
| 85 |
0x55 |
POW |
a b → a**b |
Exponentiation |
| 86 |
0x56 |
NEG |
a → -a |
Negate |
| 87 |
0x57 |
DIV_I |
a b → a//b |
Floor division |
Comparison (6 opcodes)
| Opcode |
Hex |
Name |
Stack Effect |
Description |
| 96 |
0x60 |
EQ |
a b → bool |
Equal |
| 97 |
0x61 |
NEQ |
a b → bool |
Not equal |
| 98 |
0x62 |
LT |
a b → bool |
Less than |
| 99 |
0x63 |
LE |
a b → bool |
Less or equal |
| 100 |
0x64 |
GT |
a b → bool |
Greater than |
| 101 |
0x65 |
GE |
a b → bool |
Greater or equal |
Logical & Bitwise (10 opcodes)
| Opcode |
Hex |
Name |
Stack Effect |
Description |
| 112 |
0x70 |
AND |
a b → bool |
Logical AND |
| 113 |
0x71 |
OR |
a b → bool |
Logical OR |
| 114 |
0x72 |
NOT |
a → !a |
Logical NOT |
| 115 |
0x73 |
COALESCE |
a b → a??b |
Null coalescing |
| 120 |
0x78 |
BAND |
a b → a&b |
Bitwise AND |
| 121 |
0x79 |
BOR |
a b → a\|b |
Bitwise OR |
| 122 |
0x7A |
BXOR |
a b → a^b |
Bitwise XOR |
| 123 |
0x7B |
BNOT |
a → ~a |
Bitwise NOT |
| 124 |
0x7C |
SHL |
a b → a<<b |
Left shift |
| 125 |
0x7D |
SHR |
a b → a>>b |
Right shift |
Object Operations (8 opcodes)
| Opcode |
Hex |
Name |
Stack Effect |
Operand |
Description |
| 128 |
0x80 |
NEWOBJ |
— → {} |
— |
Create empty object |
| 129 |
0x81 |
GETPROP |
obj → value |
u16 name |
Get property |
| 130 |
0x82 |
SETPROP |
obj value → — |
u16 name |
Set property |
| 131 |
0x83 |
NEWARR |
items... → [...] |
u16 count |
Create array |
| 132 |
0x84 |
INDEX |
arr idx → value |
— |
Array/object index |
| 133 |
0x85 |
SETIDX |
arr idx val → — |
— |
Set at index |
| 134 |
0x86 |
LEN |
obj → int |
— |
Get length |
| 135 |
0x87 |
DELETE |
obj key → — |
— |
Delete property |
VM Execution Modes
| Mode |
CLI Flag |
Description |
Use Case |
| Interpreter |
(default) |
Direct AST tree-walking, no compilation step |
Development, debugging |
| Bytecode VM |
--vm |
Compile to bytecode, execute on stack VM |
Better performance |
| Strict VM |
--vm-strict |
Bytecode VM + no implicit type conversions |
Production safety |
📐 Complete Type System
The Nyx type system — from simple primitives through dependent types and lifetimes.
Primitive Types (17 types)
| Type |
Size |
Range / Description |
i8 |
1 byte |
-128 to 127 |
i16 |
2 bytes |
-32,768 to 32,767 |
i32 |
4 bytes |
-2,147,483,648 to 2,147,483,647 |
i64 |
8 bytes |
-9.2×10¹⁸ to 9.2×10¹⁸ |
int |
8 bytes |
Alias for i64 (default integer type) |
u8 |
1 byte |
0 to 255 |
u16 |
2 bytes |
0 to 65,535 |
u32 |
4 bytes |
0 to 4,294,967,295 |
u64 |
8 bytes |
0 to 1.8×10¹⁹ |
f32 |
4 bytes |
IEEE 754 single precision (~7 digits) |
f64 |
8 bytes |
IEEE 754 double precision (~15 digits) |
bool |
1 byte |
true or false |
char |
4 bytes |
Unicode scalar value (U+0000 to U+10FFFF) |
str |
variable |
UTF-8 encoded string |
void |
0 bytes |
No return value |
null |
0 bytes |
Absence of value |
never |
0 bytes |
Divergent (function never returns) |
| Syntax |
Example |
Description |
[T] |
[int] |
Dynamic array of T |
[T; n] |
[int; 10] |
Fixed-size array of n elements |
{K: V} |
{str: int} |
Dictionary / Hash map |
(T1, T2, ...) |
(int, str, bool) |
Tuple (fixed heterogeneous) |
&[T] |
&[u8] |
Slice (borrowed view into array) |
fn(T1, T2) -> T |
fn(int, int) -> int |
Function type |
Generic Types & Wrappers
// Generic functions
fn max_of<T: Ord>(a: T, b: T) -> T {
if a > b { return a }
return b
}
// Generic structs
struct Stack<T> {
items: [T],
size: int
}
// Standard generic wrappers
let opt: Option<int> = Some(42) // Optional value: Some(v) | None
let res: Result<int, str> = Ok(42) // Success/Error: Ok(v) | Err(e)
// Smart pointers
let boxed: Box<int> = Box.new(42) // Heap-allocated, single owner
let shared: Rc<int> = Rc.new(42) // Reference counted (single-thread)
let atomic: Arc<int> = Arc.new(42) // Atomic ref-counted (thread-safe)
let weak: Weak<int> = Arc.downgrade(atomic) // Non-owning reference
Union & Optional Types
// Union type — value can be any of the listed types
let value: int | str | bool = "hello"
// Optional type — value can be T or null
let maybe_name: str? = null // Shorthand for Option<str>
// Null coalescing
let name = maybe_name ?? "default"
// Optional chaining
let len = maybe_name?.length // null if maybe_name is null
Ownership & Borrowing Rules
RULE 1: Each value has exactly ONE owner at any time
RULE 2: When the owner goes out of scope, the value is dropped
RULE 3: You can have EITHER:
(a) Any number of immutable references (&T), OR
(b) Exactly ONE mutable reference (&mut T)
— but not both simultaneously
// Ownership transfer
let s1 = "hello" // s1 owns the string
let s2 = move s1 // Ownership moves to s2
// print(s1) // ERROR: s1 is no longer valid
// Immutable borrowing (multiple simultaneous readers OK)
let s = "hello"
let r1 = &s // Borrow
let r2 = &s // Another borrow — OK
print(len(r1) + len(r2)) // Both valid
// Mutable borrowing (exclusive access)
let mut s = "hello"
let r = &mut s // Exclusive mutable borrow
// let r2 = &s // ERROR: can't borrow while mutably borrowed
r.push(" world") // Mutation through mutable reference
Lifetime Annotations
// Lifetime parameters prevent dangling references
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if len(x) > len(y) { return x }
return y
}
// Static lifetime — lives for entire program
let s: &'static str = "I live forever"
// Struct with lifetime
struct Parser<'a> {
input: &'a str,
position: int
}
Type Aliases & Custom Types
// Type alias
type UserId = int
type Matrix = [[f64]]
type Callback = fn(int, int) -> bool
type Result = Result<int, str>
// Newtype pattern (struct wrapping)
struct Meters(f64)
struct Seconds(f64)
// Meters(5.0) + Seconds(3.0) → TYPE ERROR — not mixable
🔐 Security Deep Dive
Security posture, hardening, and production safety features.
Vulnerability Reporting
| Item |
Detail |
| Acknowledgement SLA |
72 hours |
| Critical patch target |
7 days |
| Reporting channel |
GitHub Security Advisories |
Web Runtime Security
| Feature |
Default |
Description |
| CSRF Protection |
Enabled |
Token-based CSRF prevention |
| Replay Protection |
Enabled |
Request deduplication with TTL |
| Rate Limiting |
Configurable |
Per-IP and per-endpoint limits |
| Content Type Validation |
Strict |
Rejects unexpected content types |
| Payload Size Cap |
Configurable |
Maximum request body size |
| Request ID Tracking |
X-NYX-Request-ID |
Unique ID per request for tracing |
| Input Sanitization |
Enabled |
HTML/SQL injection prevention |
Release Gates (Production Checklist)
All releases must pass:
- Unit tests — All
tests/level1_lexer/ through tests/level3_interpreter/
- Stress tests —
tests/level4_stress/ and tests/stress_tests/
- Stdlib tests —
tests/level5_stdlib/
- Security tests —
tests/level6_security/ and tests/security_tests/
- Performance tests —
tests/level7_performance/
- Compliance tests —
tests/level8_compliance/
- Consistency tests —
tests/level9_consistency/
- Real-world tests —
tests/level10_realworld/
- Thread safety tests —
tests/thread_safety_tests/
- Failure tests —
tests/failure_tests/
- Multi-node tests —
tests/multi_node_test_suite/
- Database tests —
tests/database/
- Deployment tests —
tests/deployment/
- Hardening checks —
tests/hardening/
- Production tests —
tests/production/
- Web framework tests —
tests/web_framework/
- Security web tests —
tests/security_web/
Observability & Monitoring
// Built-in observability endpoints (web runtime)
// GET /__nyx/metrics → JSON counters and gauges
// GET /__nyx/errors → Recent error log
// GET /__nyx/plugins → Loaded plugins
// GET /__nyx/health → Health check
// Trace events emitted automatically:
// http.request.completed — every HTTP request with timing
// runtime.error — every unhandled error
// middleware.timing — middleware execution time
// handler.timing — route handler execution time
// render.completed — template rendering time
// diff.completed — change diff operations
// RuntimeObservability class provides:
// - JSON-structured logging with levels (TRACE→FATAL)
// - Request count/latency metrics
// - Error recording with stack traces
// - WebSocket connection counters
// - Custom trace hook registration
Scaling Guide
| Target |
Configuration |
| 10K concurrent sessions |
worker_concurrency: 1024 |
| Horizontal scaling |
L4/L7 load balancer + multiple instances |
| Backpressure |
Bounded queue (2048) → HTTP 503 on saturation |
| WebSocket connections |
Room-based isolation with configurable limits |
| Persistence |
Atomic writes with multi-process file locks |
📚 Standard Library Deep Dive (109 Modules)
Detailed API documentation for key stdlib modules.
stdlib/math.ny — Mathematical Functions
Constants (22 mathematical constants)
| Constant |
Value |
Description |
PI |
3.141592653589793 |
Circle ratio π |
E |
2.718281828459045 |
Euler's number |
TAU |
6.283185307179586 |
2π (full circle) |
INF |
∞ |
Positive infinity |
NAN |
NaN |
Not a Number |
PHI |
1.618033988749895 |
Golden ratio φ |
SQRT2 |
1.4142135623730951 |
√2 |
SQRT3 |
1.7320508075688772 |
√3 |
LN2 |
0.6931471805599453 |
ln(2) |
LN10 |
2.302585092994046 |
ln(10) |
LOG2E |
1.4426950408889634 |
log₂(e) |
LOG10E |
0.4342944819032518 |
log₁₀(e) |
EULER_GAMMA |
0.5772156649015329 |
Euler–Mascheroni γ |
CATALAN |
0.915965594177219 |
Catalan's constant |
GLAISHER_KINKELIN |
1.2824271291006226 |
Glaisher–Kinkelin A |
APERY |
1.2020569031595942 |
Apéry's constant ζ(3) |
KHINCHIN |
2.6854520010653064 |
Khintchine's constant |
FRANSEN_ROBINSON |
2.8077702420285193 |
Fransén–Robinson constant |
MEISSEL_MERTENS |
0.2614972128476428 |
Meissel–Mertens constant |
BERNSTEIN |
0.2801694990238691 |
Bernstein's constant |
GAUSS_CONSTANT |
0.8346268416740732 |
Gauss's constant |
LEMNISCATE |
2.6220575542921198 |
Lemniscate constant |
Functions (40+ functions across categories)
| Category |
Functions |
| Basic |
abs(x), min(a,b), max(a,b), clamp(x, lo, hi), sign(x) |
| Rounding |
floor(x), ceil(x), round(x), round_n(x, n), trunc(x) |
| Roots |
sqrt(x) (Newton-Raphson), cbrt(x), hypot(a, b), hypot3(a,b,c), hypot_n(...values) |
| Trigonometric |
sin(x), cos(x), tan(x), asin(x), acos(x), atan(x), atan2(y, x) |
| Hyperbolic |
sinh(x), cosh(x), tanh(x), asinh(x), acosh(x), atanh(x) |
| Exponential |
exp(x), log(x), log2(x), log10(x), log_n(x, base) |
| Number Theory |
gcd(a,b), lcm(a,b), is_prime(n), prime_factors(n), euler_totient(n) |
| Combinatorics |
factorial(n), permutations(n,r), combinations(n,r), fibonacci(n) |
| Interpolation |
lerp(a, b, t), cubic_interp(...), bezier(...) |
| Calculus |
derivative(fn, x), integral(fn, a, b), ode_rk4(...) |
| Linear Algebra |
dot(a, b), cross(a, b), norm(v), normalize(v), mat_mul(A, B), det(M), inverse(M) |
stdlib/io.ny — File & Directory Operations (19 functions)
| Function |
Signature |
Description |
read_file(path) |
path: str → str |
Read entire file as string |
read_lines(path) |
path: str → [str] |
Read file as array of lines |
read_nlines(path, n) |
path: str, n: int → [str] |
Read first n lines |
write_file(path, content) |
path: str, content: str → void |
Write string to file |
append_file(path, content) |
path: str, content: str → void |
Append to file |
file_exists(path) |
path: str → bool |
Check if file exists |
file_size(path) |
path: str → int |
Get file size in bytes |
copy_file(src, dst) |
src: str, dst: str → void |
Copy file |
move_file(src, dst) |
src: str, dst: str → void |
Move/rename file |
delete_file(path) |
path: str → void |
Delete file |
mkdir(path) |
path: str → void |
Create directory |
mkdir_p(path) |
path: str → void |
Create directory tree recursively |
list_dir(path) |
path: str → [str] |
List directory contents |
file_ext(path) |
path: str → str |
Get file extension |
file_stem(path) |
path: str → str |
Get filename without extension |
dirname(path) |
path: str → str |
Get parent directory |
basename(path) |
path: str → str |
Get filename from path |
join_path(...parts) |
parts: ...str → str |
Join path components |
normalize_path(path) |
path: str → str |
Normalize path separators |
stdlib/json.ny — JSON Processing
import std/json
// Parse JSON string to Nyx value
let data = json.parse('{"name": "Alice", "age": 30}')
print(data.name) // "Alice"
// Convert Nyx value to JSON string
let text = json.stringify({name: "Bob", scores: [95, 87, 92]})
// → '{"name":"Bob","scores":[95,87,92]}'
// Pretty-print JSON with indentation
let pretty = json.pretty(data)
// → '{\n "name": "Alice",\n "age": 30\n}'
Internal implementation: Full recursive-descent _JsonParser class supporting objects, arrays, strings (with Unicode escape), numbers (int/float/scientific), booleans, and null.
stdlib/http.ny — HTTP Client
import std/http
// Simple GET request
let response = http.get("https://api.example.com/data")
// response = {http_version, status_code, status_text, headers, body}
// Full HTTP request with options
let response = http.request("https://api.example.com/users", {
method: "POST",
headers: {"Content-Type": "application/json", "Authorization": "Bearer token"},
body: json.stringify({name: "Alice"}),
timeout: 30
})
// Parse URL into components
let parts = http.parse_url("https://example.com:8080/path?key=val#section")
// → {scheme: "https", host: "example.com", port: 8080, path: "/path", query: "key=val", fragment: "section"}
stdlib/crypto.ny — Cryptographic Functions
import std/crypto
// Hash functions (non-cryptographic)
let h1 = crypto.fnv1a32("hello") // FNV-1a 32-bit
let h2 = crypto.fnv1a64("hello") // FNV-1a 64-bit
let h3 = crypto.djb2("hello") // DJB2 hash
let h4 = crypto.crc32("hello") // CRC-32
let h5 = crypto.crc32_fast("hello") // CRC-32 (table-based, faster)
let h6 = crypto.crc16("hello") // CRC-16
let h7 = crypto.crc64_iso("hello") // CRC-64 ISO
let h8 = crypto.murmur3_32("hello", 0) // MurmurHash3 32-bit
let h9 = crypto.murmur3_128("hello", 0)// MurmurHash3 128-bit
// Cryptographic hashes (via engine)
let sha = crypto.sha256("hello") // SHA-256
let sha5 = crypto.sha512("hello") // SHA-512
// Symmetric encryption
let encrypted = crypto.aes_encrypt(plaintext, key, iv)
let decrypted = crypto.aes_decrypt(ciphertext, key, iv)
// Key derivation
let derived = crypto.pbkdf2(password, salt, iterations, key_length)
All 97+ stdlib Module Files
Click to expand complete stdlib module listing
| Module |
Import Path |
Description |
algorithm.ny |
import std/algorithm |
Sorting, searching, graph algorithms |
allocators.ny |
import std/allocators |
Arena, Pool, Slab, Stack, FreeList allocators |
asm.ny |
import std/asm |
x86-64/ARM/RISC-V inline assembly |
async.ny |
import std/async |
Event loops, futures, tasks, locks, semaphores |
async_runtime.ny |
import std/async_runtime |
Low-level async runtime |
atomics.ny |
import std/atomics |
Atomic integers, CAS, memory ordering |
autograd.ny |
import std/autograd |
Automatic differentiation engine |
bench.ny |
import std/bench |
Benchmarking framework |
blas.ny |
import std/blas |
Basic Linear Algebra Subprograms |
c.ny |
import std/c |
C FFI bridge |
cache.ny |
import std/cache |
LRU/LFU/TTL caching |
ci.ny |
import std/ci |
CI/CD pipeline tools |
cli.ny |
import std/cli |
Command-line argument parsing |
collections.ny |
import std/collections |
LinkedList, Deque, PriorityQueue, Set, SortedSet |
compress.ny |
import std/compress |
Compression (gzip, zlib, deflate, lz4) |
comptime.ny |
import std/comptime |
Compile-time computation |
config.ny |
import std/config |
Configuration file handling (TOML/YAML/JSON) |
cron.ny |
import std/cron |
Cron-style job scheduling |
crypto.ny |
import std/crypto |
Cryptographic functions (hash, cipher, KDF) |
crypto_hw.ny |
import std/crypto_hw |
Hardware crypto acceleration (AES-NI) |
database.ny |
import std/database |
Database drivers (SQL/NoSQL) |
dataset.ny |
import std/dataset |
Dataset loading and processing |
debug.ny |
import std/debug |
Debugging utilities |
debug_hw.ny |
import std/debug_hw |
Hardware debugging (JTAG, SWD) |
distributed.ny |
import std/distributed |
Distributed computing |
dma.ny |
import std/dma |
Direct Memory Access |
experiment.ny |
import std/experiment |
ML experiment tracking |
feature_store.ny |
import std/feature_store |
Feature engineering storage |
ffi.ny |
import std/ffi |
Foreign Function Interface |
fft.ny |
import std/fft |
Fast Fourier Transform |
formatter.ny |
import std/formatter |
Code formatting tools |
game.ny |
import std/game |
Basic game utilities |
generator.ny |
import std/generator |
Generator/coroutine support |
governance.ny |
import std/governance |
API governance tools |
gui.ny |
import std/gui |
GUI framework (Application, Window, widgets) |
hardware.ny |
import std/hardware |
Hardware abstraction layer |
http.ny |
import std/http |
HTTP client (GET/POST with headers, timeout) |
hub.ny |
import std/hub |
WebSocket hub/pub-sub |
hypervisor.ny |
import std/hypervisor |
VM hypervisor management |
interrupts.ny |
import std/interrupts |
Hardware interrupt handling (IDT, ISR) |
io.ny |
import std/io |
File I/O and directory operations |
json.ny |
import std/json |
JSON parse/stringify/pretty |
jwt.ny |
import std/jwt |
JSON Web Token creation/verification |
log.ny |
import std/log |
Structured logging (TRACE→FATAL) |
lsp.ny |
import std/lsp |
Language Server Protocol |
math.ny |
import std/math |
Math functions (40+) and 22 constants |
metrics.ny |
import std/metrics |
Application metrics collection |
mlops.ny |
import std/mlops |
ML deployment operations |
monitor.ny |
import std/monitor |
System monitoring agents |
network.ny |
import std/network |
Low-level networking |
nlp.ny |
import std/nlp |
Natural Language Processing |
nn.ny |
import std/nn |
Neural network layers and models |
optimize.ny |
import std/optimize |
Mathematical optimization |
ownership.ny |
import std/ownership |
Ownership model utilities |
paging.ny |
import std/paging |
Virtual memory paging |
parser.ny |
import std/parser |
Parser combinator library |
precision.ny |
import std/precision |
Arbitrary-precision arithmetic |
process.ny |
import std/process |
Process management (Popen, exec) |
realtime.ny |
import std/realtime |
Real-time scheduling |
redis.ny |
import std/redis |
Redis client |
regex.ny |
import std/regex |
Regular expressions |
science.ny |
import std/science |
Scientific computing |
serialization.ny |
import std/serialization |
Serialization (binary, msgpack, protobuf) |
serving.ny |
import std/serving |
Model serving infrastructure |
simd.ny |
import std/simd |
SIMD vectorization (SSE/AVX/NEON) |
smart_ptrs.ny |
import std/smart_ptrs |
Box, Rc, Arc, Weak pointers |
socket.ny |
import std/socket |
TCP/UDP socket programming |
sparse.ny |
import std/sparse |
Sparse matrix/tensor operations |
state_machine.ny |
import std/state_machine |
Finite state machine framework |
string.ny |
import std/string |
String manipulation utilities |
symbolic.ny |
import std/symbolic |
Symbolic math (CAS) |
systems.ny |
import std/systems |
Systems programming utilities |
systems_extended.ny |
import std/systems_extended |
Extended systems programming |
tensor.ny |
import std/tensor |
Tensor operations (multi-dimensional arrays) |
test.ny |
import std/test |
Testing framework (assert, describe, it) |
time.ny |
import std/time |
Time/date, timestamps, sleep |
train.ny |
import std/train |
ML training loops and schedules |
types.ny |
import std/types |
Type system utilities |
types_advanced.ny |
import std/types_advanced |
Advanced type operations |
validator.ny |
import std/validator |
Input validation framework |
visualize.ny |
import std/visualize |
Data visualization / plotting |
vm.ny |
import std/vm |
VM management |
vm_acpi.ny |
import std/vm_acpi |
ACPI (power management) |
vm_acpi_advanced.ny |
import std/vm_acpi_advanced |
Advanced ACPI features |
vm_bios.ny |
import std/vm_bios |
BIOS emulation |
vm_devices.ny |
import std/vm_devices |
Virtual device drivers |
vm_errors.ny |
import std/vm_errors |
VM error handling |
vm_hotplug.ny |
import std/vm_hotplug |
Hot-plug device support |
vm_iommu.ny |
import std/vm_iommu |
IOMMU (DMA remapping) |
vm_logging.ny |
import std/vm_logging |
VM logging |
vm_metrics.ny |
import std/vm_metrics |
VM performance metrics |
vm_migration.ny |
import std/vm_migration |
Live VM migration |
vm_production.ny |
import std/vm_production |
Production VM configuration |
vm_tpm.ny |
import std/vm_tpm |
TPM 2.0 emulation |
web.ny |
import std/web |
Web framework (Router, middleware) |
xml.ny |
import std/xml |
XML parsing and generation |
__init__.ny |
— |
Package initialization |
Subdirectories:
stdlib/dfas/ — Dynamic Field Arithmetic System (10 modules)
stdlib/__nycache__/ — Bytecode cache
🔑 All Keywords (87 — Complete Reference)
Every reserved word in the Nyx language with its category and token type.
| # |
Keyword |
Token Type |
Category |
Description |
| 1 |
fn |
FUNCTION |
Declaration |
Function definition |
| 2 |
let |
LET |
Declaration |
Variable binding |
| 3 |
mut |
MUT |
Declaration |
Mutable modifier |
| 4 |
const |
CONST |
Declaration |
Constant binding |
| 5 |
var |
VAR |
Declaration |
Variable (mutable by default) |
| 6 |
true |
TRUE |
Literal |
Boolean true |
| 7 |
false |
FALSE |
Literal |
Boolean false |
| 8 |
if |
IF |
Control |
Conditional branch |
| 9 |
else |
ELSE |
Control |
Alternative branch |
| 10 |
elif |
ELIF |
Control |
Else-if branch |
| 11 |
return |
RETURN |
Control |
Return from function |
| 12 |
while |
WHILE |
Control |
While loop |
| 13 |
for |
FOR |
Control |
For loop (C-style or for-in) |
| 14 |
in |
IN |
Control |
Membership / iteration |
| 15 |
break |
BREAK |
Control |
Exit loop |
| 16 |
continue |
CONTINUE |
Control |
Skip to next iteration |
| 17 |
print |
PRINT |
Built-in |
Print to stdout |
| 18 |
match |
MATCH |
Control |
Pattern match expression |
| 19 |
case |
CASE |
Control |
Match case arm |
| 20 |
when |
WHEN |
Control |
Guard condition |
| 21 |
where |
WHERE |
Control |
Type constraint |
| 22 |
loop |
LOOP |
Control |
Infinite loop |
| 23 |
do |
DO |
Control |
Do-while loop |
| 24 |
goto |
GOTO |
Control |
Jump to label |
| 25 |
defer |
DEFER |
Control |
Defer execution to scope exit |
| 26 |
pass |
PASS |
Control |
No-op placeholder |
| 27 |
class |
CLASS |
OOP |
Class definition |
| 28 |
struct |
STRUCT |
OOP |
Struct definition |
| 29 |
trait |
TRAIT |
OOP |
Trait (interface) definition |
| 30 |
interface |
INTERFACE |
OOP |
Interface definition |
| 31 |
impl |
IMPL |
OOP |
Trait implementation |
| 32 |
enum |
ENUM |
OOP |
Enum definition |
| 33 |
super |
SUPER |
OOP |
Parent class reference |
| 34 |
self |
SELF |
OOP |
Current instance reference |
| 35 |
new |
NEW |
OOP |
Object instantiation |
| 36 |
extends |
EXTENDS |
OOP |
Class inheritance |
| 37 |
implements |
IMPLEMENTS |
OOP |
Trait/interface implementation |
| 38 |
import |
IMPORT |
Module |
Import module |
| 39 |
use |
USE |
Module |
Use engine |
| 40 |
from |
FROM |
Module |
Import specific names from module |
| 41 |
as |
AS |
Module |
Import alias |
| 42 |
export |
EXPORT |
Module |
Export names |
| 43 |
pub |
PUB |
Module |
Public visibility |
| 44 |
priv |
PRIV |
Module |
Private visibility |
| 45 |
mod |
MOD |
Module |
Module declaration |
| 46 |
namespace |
NAMESPACE |
Module |
Namespace scope |
| 47 |
package |
PACKAGE |
Module |
Package declaration |
| 48 |
try |
TRY |
Error |
Try block |
| 49 |
catch |
CATCH |
Error |
Catch handler |
| 50 |
except |
EXCEPT |
Error |
Exception handler (alias) |
| 51 |
finally |
FINALLY |
Error |
Finally block (always runs) |
| 52 |
raise |
RAISE |
Error |
Raise exception |
| 53 |
throw |
THROW |
Error |
Throw exception (alias) |
| 54 |
assert |
ASSERT |
Error |
Assert condition |
| 55 |
with |
WITH |
Async |
Context manager / resource guard |
| 56 |
yield |
YIELD |
Async |
Generator yield |
| 57 |
async |
ASYNC |
Async |
Async function/block |
| 58 |
await |
AWAIT |
Async |
Await future result |
| 59 |
spawn |
SPAWN |
Async |
Spawn concurrent task |
| 60 |
channel |
CHANNEL |
Async |
Create channel for message passing |
| 61 |
select |
SELECT |
Async |
Select from multiple channels |
| 62 |
lock |
LOCK |
Async |
Mutual exclusion lock |
| 63 |
actor |
ACTOR |
Async |
Actor model definition |
| 64 |
type |
TYPE |
Types |
Type definition |
| 65 |
typeof |
TYPEOF |
Types |
Get type at runtime |
| 66 |
instanceof |
INSTANCEOF |
Types |
Instance type check |
| 67 |
is |
IS |
Types |
Type pattern check |
| 68 |
static |
STATIC |
Types |
Static member/method |
| 69 |
dynamic |
DYNAMIC |
Types |
Dynamic typing escape |
| 70 |
any |
ANY |
Types |
Any type (top type) |
| 71 |
void |
VOID |
Types |
Void return type |
| 72 |
never |
NEVER |
Types |
Never type (divergent) |
| 73 |
null |
NULL |
Meta |
Null literal |
| 74 |
none |
NONE |
Meta |
None value |
| 75 |
undefined |
UNDEFINED |
Meta |
Undefined value |
| 76 |
macro |
MACRO |
Meta |
Macro definition |
| 77 |
inline |
INLINE |
Meta |
Inline expansion hint |
| 78 |
unsafe |
UNSAFE |
Meta |
Unsafe block (raw memory) |
| 79 |
extern |
EXTERN |
Meta |
External linkage (FFI) |
| 80 |
ref |
REF |
Meta |
Reference / borrow |
| 81 |
move |
MOVE |
Meta |
Ownership transfer |
| 82 |
copy |
COPY |
Meta |
Copy semantics |
| 83 |
sizeof |
SIZEOF |
Meta |
Size of type in bytes |
| 84 |
alignof |
ALIGNOF |
Meta |
Alignment of type |
| 85 |
global |
GLOBAL |
Meta |
Global scope |
| 86 |
static_assert |
STATIC_ASSERT |
Meta |
Compile-time assertion |
| 87 |
comptime |
COMPTIME |
Meta |
Compile-time evaluation |
Nyx vs Python vs Rust vs Go vs Node.js vs C
| Benchmark |
Nyx (native) |
Python 3.12 |
Rust 1.75 |
Go 1.21 |
Node.js 20 |
C (gcc -O2) |
| Hello World startup |
5ms |
50ms |
2ms |
10ms |
30ms |
1ms |
| Fibonacci(30) recursive |
2ms |
100ms |
1ms |
5ms |
15ms |
0.8ms |
| Fibonacci(35) recursive |
40ms |
2,800ms |
30ms |
50ms |
150ms |
25ms |
| Prime sieve (1M) |
100ms |
3,500ms |
80ms |
150ms |
800ms |
60ms |
| Matrix multiply 100×100 |
2ms |
50ms |
1.5ms |
3ms |
10ms |
1ms |
| Matrix multiply 1000×1000 |
250ms |
12,000ms |
200ms |
300ms |
2,000ms |
150ms |
| JSON parse (10KB) |
1ms |
10ms |
0.5ms |
2ms |
0.3ms |
0.8ms |
| HTTP server throughput |
15K req/s |
300 req/s |
50K req/s |
30K req/s |
15K req/s |
— |
| String concat (100K) |
Fast |
3.5s |
Fast |
Fast |
Fast |
Fast |
| Array ops (1M) |
Fast |
2s |
Fast |
Fast |
Fast |
Fast |
Memory Usage Comparison
| Metric |
Nyx (native) |
Python |
Node.js |
Go |
Rust |
C |
| Runtime base memory |
2 MB |
15 MB |
30 MB |
5 MB |
2 MB |
0.5 MB |
| Per integer |
8 bytes |
28 bytes |
8 bytes |
8 bytes |
8 bytes |
4-8 bytes |
| Per string "hello" |
5 + 16 bytes |
54 bytes |
32 bytes |
5 + 16 bytes |
5 + 24 bytes |
6 bytes |
| Per bool |
1 byte |
28 bytes |
4 bytes |
1 byte |
1 byte |
1 byte |
| 100K concurrent tasks |
< 1 GB |
5+ GB |
3+ GB |
0.5 GB |
0.1 GB |
— |
| Metric |
Value |
| Tasks/second throughput |
100K+ |
| Memory per task |
~1 KB |
| Context switch time |
< 1 μs |
| Concurrent connections |
100K in < 1 GB |
| Worker pool dispatch overhead |
< 50 μs |
| Admission gate latency |
< 10 μs |
HTTP Server Benchmarks (nyx_runtime.py — verified)
| Metric |
Value |
Source |
| Requests/sec |
15,000+ |
Benchmark tested |
| Memory usage (runtime) |
24 MB |
Measured |
| Concurrent operations |
214K passed |
Stress test verified |
| GPU kernel compilation |
Native support |
Direct compilation |
Source: benchmarks/NYX_VS_PYTHON_TRUTH.md
Memory Usage
| Metric |
Nyx |
Python |
| Runtime base memory |
2 MB |
15 MB |
| Per integer |
8 bytes |
28 bytes |
| Per string "hello" |
5 bytes + header |
54 bytes |
| 100K concurrent tasks |
< 1 GB |
5+ GB |
- 100K tasks/second throughput
- 1 KB/task memory overhead
- < 1 μs context switch time
- 100K concurrent connections in < 1 GB RAM
🌍 Real-World Examples
Example 1: REST API Server
use nyhttpd
import std/json
let db = {}
let mut next_id = 1
let server = nyhttpd.HttpServer.new({port: 3000, worker_threads: 4})
# List all users
server.get("/api/users", fn(req, res) {
res.json(values(db))
})
# Get user by ID
server.get("/api/users/:id", fn(req, res) {
let user = db[req.params.id]
if user { res.json(user) }
else { res.status(404).json({error: "Not found"}) }
})
# Create user
server.post("/api/users", fn(req, res) {
let user = json.parse(req.body)
user.id = next_id
next_id = next_id + 1
db[str(user.id)] = user
res.status(201).json(user)
})
print("API running on http://localhost:3000")
server.start()
Example 2: Neural Network Training
use nytensor
use nynet
use nyopt
# Create model
class Net {
fn init(self) {
self.layer1 = Linear(784, 128)
self.layer2 = Linear(128, 64)
self.layer3 = Linear(64, 10)
}
fn forward(self, x) {
x = relu(self.layer1.forward(x))
x = relu(self.layer2.forward(x))
x = softmax(self.layer3.forward(x))
return x
}
}
let model = Net()
let optimizer = SGD(model.parameters(), lr: 0.01, momentum: 0.9)
# Training loop
for epoch in 0..10 {
let mut total_loss = 0.0
for batch in data_loader {
let output = model.forward(batch.input)
let loss = cross_entropy(output, batch.target)
loss.backward()
optimizer.step()
optimizer.zero_grad()
total_loss = total_loss + loss.value
}
print("Epoch " + str(epoch) + " Loss: " + str(total_loss))
}
Example 3: Desktop GUI Application
import std/gui
let app = Application("My App")
let window = Window("Calculator", 400, 500)
let mut display = ""
fn on_button(num) {
display = display + str(num)
window.update_label("display", display)
}
fn on_calculate() {
let result = eval(display)
display = str(result)
window.update_label("display", display)
}
window.add_label("display", display, {font_size: 24})
for i in 0..10 {
window.add_button(str(i), fn() { on_button(i) })
}
window.add_button("=", on_calculate)
window.add_button("C", fn() { display = ""; window.update_label("display", "") })
app.run(window)
Example 4: Game with AI
use nygame
use nyai
class Player {
fn init(self, x, y) {
self.x = x
self.y = y
self.health = 100
self.score = 0
}
fn move(self, dx, dy) {
self.x = clamp(self.x + dx, 0, 800)
self.y = clamp(self.y + dy, 0, 600)
}
}
class Enemy {
fn init(self, x, y) {
self.x = x
self.y = y
self.ai = nyai.Agent("patrol")
}
fn update(self, player) {
let action = self.ai.decide({
player_x: player.x,
player_y: player.y,
enemy_x: self.x,
enemy_y: self.y
})
match action {
case "chase" => {
let dx = clamp(player.x - self.x, -2, 2)
let dy = clamp(player.y - self.y, -2, 2)
self.x = self.x + dx
self.y = self.y + dy
}
case "patrol" => {
self.x = self.x + range(-1, 2)[0]
}
case _ => {}
}
}
}
import std/cli
import std/io
import std/json
# Parse arguments
let args = cli.parse({
name: "nyx-tool",
version: "1.0.0",
options: [
{name: "input", short: "i", help: "Input file", required: true},
{name: "output", short: "o", help: "Output file", default: "out.json"},
{name: "verbose", short: "v", help: "Verbose output", flag: true}
]
})
# Read input
let data = read_file(args.input)
let parsed = json.parse(data)
if args.verbose {
print("Processing " + str(len(parsed)) + " records...")
}
# Process
let result = parsed
|> filter(|r| r.active == true)
|> map(|r| {name: r.name, score: r.score * 1.1})
|> sort_by(|r| r.score)
# Write output
write_file(args.output, json.pretty(result))
print("Done! Wrote " + str(len(result)) + " records to " + args.output)
Example 6: Cryptocurrency/Blockchain
import std/crypto
import std/time
class Block {
fn init(self, index, data, prev_hash) {
self.index = index
self.timestamp = time.now()
self.data = data
self.prev_hash = prev_hash
self.nonce = 0
self.hash = self.calculate_hash()
}
fn calculate_hash(self) {
let input = str(self.index) + self.timestamp + str(self.data) + self.prev_hash + str(self.nonce)
return crypto.sha256(input)
}
fn mine(self, difficulty) {
let target = "0" * difficulty
while !self.hash.starts_with(target) {
self.nonce = self.nonce + 1
self.hash = self.calculate_hash()
}
print("Block mined: " + self.hash)
}
}
# Create blockchain
let genesis = Block(0, "Genesis Block", "0")
genesis.mine(4)
print("Blockchain started with genesis block!")
Example 7: Web Scraper & Data Pipeline
import std/http
import std/json
import std/io
import std/string
import std/regex
# Fetch and parse multiple pages
async fn scrape_page(url) {
let response = await http.get(url)
let data = json.parse(response.body)
return data.items
|> filter(|item| item.price > 0)
|> map(|item| {
name: string.strip(item.name),
price: item.price,
category: item.category,
url: url
})
}
# Process pipeline
async fn main() {
let urls = [
"https://api.example.com/products?page=1",
"https://api.example.com/products?page=2",
"https://api.example.com/products?page=3"
]
let results = await async.gather(...urls |> map(scrape_page))
let all_items = results |> flatten() |> sort_by(|x| x.price)
# Group by category
let grouped = {}
for item in all_items {
if !has(grouped, item.category) {
grouped[item.category] = []
}
push(grouped[item.category], item)
}
io.write_file("products.json", json.pretty(grouped))
print("Scraped " + str(len(all_items)) + " products in " + str(len(keys(grouped))) + " categories")
}
main()
Example 8: Real-Time Chat Server
import std/socket
import std/json
import std/async
import std/time
class ChatServer {
fn init(self, port) {
self.server = socket.TCPServer(port)
self.clients = {}
self.rooms = {"general": []}
}
async fn handle_client(self, client) {
let name = await client.recv(1024)
self.clients[name] = client
push(self.rooms["general"], name)
self.broadcast("general", name + " joined the chat!")
loop {
let msg = await client.recv(4096)
if !msg { break }
let data = json.parse(msg)
match data.type {
case "message" => {
self.broadcast(data.room, name + ": " + data.text)
}
case "join_room" => {
if !has(self.rooms, data.room) {
self.rooms[data.room] = []
}
push(self.rooms[data.room], name)
}
case "dm" => {
if has(self.clients, data.to) {
self.clients[data.to].send(json.stringify({
from: name, text: data.text, type: "dm",
time: time.now()
}))
}
}
}
}
delete(self.clients, name)
self.broadcast("general", name + " left the chat.")
}
fn broadcast(self, room, message) {
for user in self.rooms[room] {
if has(self.clients, user) {
self.clients[user].send(json.stringify({
text: message, room: room, time: time.now()
}))
}
}
}
async fn start(self) {
print("Chat server running on port " + str(self.server.port))
loop {
let client = await self.server.accept()
spawn self.handle_client(client)
}
}
}
let server = ChatServer(8080)
server.start()
Example 9: Machine Learning Inference Server
import std/web
import std/json
import std/tensor
import std/nn
import std/time
import std/log
let logger = log.get_logger("ml-server")
logger.set_level(log.INFO)
# Load pre-trained model
let model = nn.Module.load("model.nyx")
model.eval()
let router = web.Router()
router.post("/predict", fn(req, res) {
let start = time.now_millis()
let input = json.parse(req.body)
let t = tensor.tensor(input.data, dtype: "float32")
let output = model.forward(t)
let prediction = output.to_list()
let elapsed = time.now_millis() - start
logger.info("Prediction in " + str(elapsed) + "ms")
res.json({
prediction: prediction,
confidence: max(prediction),
latency_ms: elapsed,
model_version: "1.0.0"
})
})
router.get("/health", fn(req, res) {
res.json({status: "healthy", uptime: time.now()})
})
print("ML Inference server on :8000")
router.listen(8000)
import std/database
import std/io
import std/json
import std/time
import std/cli
import std/log
let logger = log.get_logger("migrate")
class Migration {
fn init(self, version, name, up_fn, down_fn) {
self.version = version
self.name = name
self.up = up_fn
self.down = down_fn
}
}
class Migrator {
fn init(self, db_path) {
self.db = database.Database()
self.db.create_table("migrations", {
version: "int",
name: "string",
applied_at: "string"
})
self.migrations = []
}
fn add(self, version, name, up_fn, down_fn) {
push(self.migrations, Migration(version, name, up_fn, down_fn))
}
fn migrate_up(self) {
let applied = self.db.table("migrations").select({})
|> map(|r| r.version)
let pending = self.migrations
|> filter(|m| !applied.contains(m.version))
|> sort_by(|m| m.version)
for m in pending {
logger.info("Applying: " + m.name)
m.up(self.db)
self.db.table("migrations").insert({
version: m.version,
name: m.name,
applied_at: time.now()
})
}
logger.info(str(len(pending)) + " migrations applied")
}
fn migrate_down(self, steps) {
let applied = self.db.table("migrations").select({})
|> sort_by(|r| -r.version)
|> limit(steps)
for record in applied {
let m = self.migrations |> find(|m| m.version == record.version)
logger.info("Reverting: " + m.name)
m.down(self.db)
self.db.table("migrations").delete({version: m.version})
}
}
}
# Define migrations
let migrator = Migrator("app.db")
migrator.add(1, "create_users", fn(db) {
db.create_table("users", {
id: "int", name: "string", email: "string", created_at: "string"
})
}, fn(db) {
db.drop_table("users")
})
migrator.add(2, "create_posts", fn(db) {
db.create_table("posts", {
id: "int", title: "string", body: "string",
author_id: "int", published: "bool"
})
}, fn(db) {
db.drop_table("posts")
})
migrator.migrate_up()
Example 11: File Watcher & Build System
import std/io
import std/time
import std/process
import std/log
import std/string
let logger = log.get_logger("builder")
class BuildSystem {
fn init(self, watch_dir, build_cmd) {
self.watch_dir = watch_dir
self.build_cmd = build_cmd
self.file_times = {}
self.running = true
}
fn scan(self) {
let files = io.list_dir(self.watch_dir)
|> filter(|f| string.ends_with(f, ".ny"))
let changed = []
for file in files {
let path = io.join_path(self.watch_dir, file)
let info = io.file_info(path)
let prev = self.file_times[path]
if prev == null or prev != info.modified {
self.file_times[path] = info.modified
push(changed, path)
}
}
return changed
}
fn build(self, changed_files) {
logger.info("Building... (" + str(len(changed_files)) + " files changed)")
let start = time.now_millis()
let result = process.Popen(self.build_cmd, [])
let exit_code = result.wait()
let elapsed = time.now_millis() - start
if exit_code == 0 {
logger.info("Build succeeded in " + str(elapsed) + "ms")
} else {
logger.error("Build FAILED (exit " + str(exit_code) + ")")
}
}
fn watch(self) {
logger.info("Watching " + self.watch_dir + " for changes...")
while self.running {
let changed = self.scan()
if len(changed) > 0 {
self.build(changed)
}
time.sleep_ms(500)
}
}
}
let builder = BuildSystem("src/", "nyx build main.ny")
builder.watch()
Example 12: Concurrent Task Scheduler
import std/async
import std/time
import std/collections
import std/log
let logger = log.get_logger("scheduler")
enum TaskPriority { Low, Medium, High, Critical }
class TaskScheduler {
fn init(self, max_workers) {
self.queue = collections.LinkedList()
self.workers = max_workers
self.active = 0
self.completed = 0
self.lock = async.Lock()
}
async fn submit(self, name, priority, task_fn) {
await self.lock.acquire()
self.queue.append({name: name, priority: priority, fn: task_fn})
self.lock.release()
logger.info("Queued: " + name)
}
async fn worker(self, id) {
loop {
await self.lock.acquire()
if self.queue.is_empty() {
self.lock.release()
await async.async_sleep(0.1)
continue
}
let task = self.queue.remove(0)
self.active = self.active + 1
self.lock.release()
logger.info("Worker " + str(id) + " running: " + task.name)
let start = time.now_millis()
try {
await task.fn()
let elapsed = time.now_millis() - start
logger.info(task.name + " completed in " + str(elapsed) + "ms")
} catch e {
logger.error(task.name + " failed: " + str(e))
}
self.active = self.active - 1
self.completed = self.completed + 1
}
}
async fn start(self) {
let workers = range(self.workers)
|> map(|id| spawn self.worker(id))
await async.gather(...workers)
}
}
async fn main() {
let scheduler = TaskScheduler(4)
await scheduler.submit("fetch-data", TaskPriority.High, async fn() {
await async.async_sleep(1.0)
print("Data fetched!")
})
await scheduler.submit("process-images", TaskPriority.Medium, async fn() {
await async.async_sleep(2.0)
print("Images processed!")
})
await scheduler.submit("send-emails", TaskPriority.Low, async fn() {
await async.async_sleep(0.5)
print("Emails sent!")
})
await scheduler.start()
}
main()
⌨️ CLI Reference (Complete)
Running Programs
nyx file.ny # Run a Nyx program
nyx hello.ny # Run hello world
nyx main.ny arg1 arg2 # Pass command-line arguments
nyx - # Read from stdin
nyx --version # Show Nyx version (v3.3.3)
nyx --help # Show usage help
Debugging & Profiling
nyx --trace file.ny # Trace every instruction executed
nyx --debug file.ny # Detailed error messages with stack traces
nyx --step file.ny # Step-through interactive debugging
nyx --step-count N file.ny # Execute N steps then pause
nyx --break file.ny # Set breakpoints at specific lines
nyx --parse-only file.ny # Parse and check syntax only (no execution)
nyx --lint file.ny # Lint check for code quality issues
VM Modes
nyx --vm file.ny # Run in bytecode VM mode
nyx --vm-strict file.ny # Strict VM mode (no implicit conversions)
Resource Limits & Safety
nyx --max-alloc 1000000 file.ny # Max memory allocations (default: unlimited)
nyx --max-steps 1000000 file.ny # Max execution steps (default: 1,000,000)
nyx --max-call-depth 100 file.ny # Max recursion/call stack depth
nyx --profile-memory file.ny # Profile memory usage during execution
nyx --timeout 30 file.ny # Timeout in seconds
Caching & Build
nyx --cache file.ny # Use bytecode cache for faster re-runs
nyx build . # Build entire project (compile all .ny files)
nyx build main.ny # Compile single file to native binary
nyx build --release main.ny # Compile with optimizations (-O3)
nyx build --target linux main.ny # Cross-compile for target platform
nyx fmt file.ny # Auto-format code
nyx fmt --check file.ny # Check formatting without modifying
nyx check file.ny # Type check and lint
nyx test # Run all test files (*_test.ny, test_*.ny)
nyx test --verbose # Run tests with detailed output
nyx test --filter "test_name" # Run specific tests matching pattern
nyx bench # Run benchmark files (*_bench.ny)
nyx doc # Generate API documentation
nyx clean # Clean build artifacts and cache
Package Management (NYPM)
nypm init # Initialize new project (creates ny.registry)
nypm init my-project # Initialize with project name
nypm install # Install all dependencies from ny.registry
nypm install <pkg> # Install a specific package
nypm install <pkg>@1.2.3 # Install specific version
nypm add <pkg> # Alias for install
nypm remove <pkg> # Remove a package
nypm rm <pkg> # Alias for remove
nypm update # Update all packages to latest compatible
nypm update <pkg> # Update specific package
nypm search <query> # Search package registry
nypm list # List installed packages
nypm list --tree # Show dependency tree
nypm info <pkg> # Show package details
nypm versions <pkg> # List all available versions
nypm outdated # Check for outdated packages
nypm publish # Publish package to registry
nypm clean # Clean package cache
nypm doctor # Diagnose and fix issues
nypm run <script> # Run a script from ny.registry [scripts]
Environment Variables
| Variable |
Description |
Default |
NYX_PATH |
Search path for imports |
. |
NYX_HOME |
Nyx installation directory |
~/.nyx |
NYX_CACHE_DIR |
Bytecode cache directory |
~/.nyx/cache |
NYX_LOG_LEVEL |
Log verbosity (debug/info/warn/error) |
warn |
NYX_NO_COLOR |
Disable colored output |
unset |
NYX_MAX_STEPS |
Default max execution steps |
1000000 |
NYX_MAX_ALLOC |
Default max memory allocations |
unlimited |
Exit Codes
| Code |
Meaning |
0 |
Success |
1 |
Runtime error |
2 |
Syntax/parse error |
3 |
File not found |
4 |
Permission denied |
5 |
Timeout exceeded |
6 |
Memory limit exceeded |
7 |
Stack overflow |
🚨 Error Reference
Complete guide to Nyx error types and how to fix them.
Parse Errors
| Error |
Cause |
Fix |
Unexpected token |
Token appears where it shouldn't |
Check syntax, missing operator or delimiter |
Expected ')' |
Unclosed parenthesis |
Add matching ) |
Expected '}' |
Unclosed brace |
Add matching } |
Expected ']' |
Unclosed bracket |
Add matching ] |
Invalid assignment target |
Assigning to non-lvalue |
Assign to variable, index, or member only |
Unterminated string |
String missing closing quote |
Add matching " or ' |
Invalid number literal |
Malformed number |
Check number format (hex: 0xFF, octal: 0o77, binary: 0b1010) |
Runtime Errors
| Error |
Cause |
Fix |
Undefined variable: x |
Variable not declared |
Declare with let x = ... |
Type error: cannot add int and string |
Incompatible types in operation |
Convert types: str(42) or int("42") |
Division by zero |
Dividing by zero |
Check divisor before dividing |
Index out of bounds |
Array index too large/small |
Check len(arr) before indexing |
Key not found: x |
Object key doesn't exist |
Use has(obj, "x") or obj.x ?? default |
Stack overflow |
Too much recursion |
Add base case, increase --max-call-depth |
Maximum steps exceeded |
Infinite loop or very long computation |
Fix loop condition or increase --max-steps |
Maximum allocations exceeded |
Too many heap allocations |
Optimize allocation patterns or increase limit |
Not callable |
Calling a non-function value |
Check value is a function before calling |
Module not found: x |
Import path doesn't resolve |
Check module path and NYX_PATH |
Attribute error: x has no member y |
Accessing nonexistent member |
Check object/class has the property |
Assertion failed |
assert condition was false |
Fix the condition or the code being asserted |
Import Errors
| Error |
Cause |
Fix |
Module not found |
Module file doesn't exist at path |
Check file exists, check NYX_PATH |
Circular import detected |
Module A imports B which imports A |
Restructure dependencies |
Cannot import name x from module y |
Named export doesn't exist |
Check available exports with module docs |
🧩 Design Philosophy
The core principles that guide every design decision in Nyx.
1. Zero-Config Batteries-Included
Nyx ships with 109 stdlib modules and 117+ engines. No package manager needed for common tasks. Web server? use nyhttpd. Neural network? use nynet. Database? import std/database. Everything is built-in, tested, and ready to use.
2. One Language, Every Domain
Traditional software development requires learning multiple languages: Python for ML, JavaScript for web, C for systems, Go for servers, Rust for safety. Nyx eliminates this fragmentation. One language, one syntax, one toolchain — from embedded systems to cloud infrastructure.
3. Safe by Default, Unsafe When Needed
Nyx provides memory safety through ownership and borrowing (like Rust) but doesn't force it everywhere. When you need raw performance, use unsafe { } blocks for direct memory access, inline assembly, and raw pointers. Safety is a gradient, not a binary.
4. Fast Without Trying
Nyx is compiled to native code via C99 with -O2 optimization by default. The runtime uses SIMD vectorization, cache-friendly data layouts, and lock-free data structures. You write clean, readable code — Nyx makes it fast.
5. Progressive Complexity
Hello world is one line: print("Hello!"). Building a REST API is 20 lines. A neural network is 30 lines. You never need to understand ownership, generics, or macros until you need them. The language grows with you.
6. Dual Import System
Nyx has two import patterns for maximum flexibility:
// For stdlib modules (fine-grained)
import std/math // Import specific module
import std/io // File I/O module
from std/crypto import sha256, aes_encrypt // Named imports
// For engines (domain-level)
use nyhttpd // Bring entire engine into scope
use nyai // AI engine available globally
💾 Memory Model
How Nyx manages memory across all three execution modes.
Core Principles
- Bounded queues for request work (default: 2048 entries)
- Bounded maps for security/rate state with periodic GC
- Copy-on-read snapshots for exposed state (no shared mutable state leaks)
- Guarded mutation through locks or provider transactions
Bounded Resources
| Resource |
Limit |
Config Key |
| Worker queue |
2048 |
worker_queue_size |
| Concurrent requests |
256 |
max_concurrent_requests |
| Rate limiter map |
Configurable |
max_keys + periodic GC |
| Replay cache |
TTL-based eviction |
replay_ttl_seconds |
| WebSocket connections |
Configurable |
max_connections |
| WebSocket frame |
Configurable |
max_frame_bytes |
State & Immutability
// All state access returns deep-copied snapshots:
let state = StateStore.snapshot() // Deep copy — reads are safe
let data = PersistentStore.get(key) // Returns safe clone
let metrics = Observability.snapshot() // Deep-copied counters
// Mutation is always guarded:
PersistentStore.set(key, value) // RLock + atomic write
StateStore.update(fn(state) { ... }) // Lock + transaction
Persistence Safety
All disk operations use atomic write semantics to prevent corruption:
1. Serialize complete dict snapshot
2. Write to temporary file
3. fsync() the temp file
4. os.replace() (atomic rename)
5. fsync() the directory
6. Clean up temp files
Partial/torn JSON writes are impossible with this approach.
Value Types & Sizes
| Type |
Native Size |
Python Interpreter |
int |
8 bytes (i64) |
28 bytes (Python int) |
float |
8 bytes (f64) |
24 bytes (Python float) |
bool |
1 byte |
28 bytes (Python bool) |
string "hello" |
5 bytes + 16 header |
54 bytes (Python str) |
null |
0 bytes (singleton) |
16 bytes (Python None) |
| Array header |
24 bytes |
56+ bytes (Python list) |
⚡ Concurrency Model
How Nyx handles parallel and concurrent execution.
Runtime Architecture
┌───────────────────────────┐
│ Admission Gate │
│ BoundedSemaphore(max_req) │
└───────────┬───────────────┘
│
┌───────────▼───────────────┐
│ Worker Queue │
│ Queue(maxsize=2048) │
└───────────┬───────────────┘
│
┌─────────┬───────┼───────┬─────────┐
▼ ▼ ▼ ▼ ▼
┌────────┐ ┌────────┐ ... ┌────────┐ ┌────────┐
│Worker 1│ │Worker 2│ │Worker N│ │Worker N│
│ Thread │ │ Thread │ │ Thread │ │ Thread │
└────────┘ └────────┘ └────────┘ └────────┘
Request flow: handle_request → acquire semaphore → submit WorkerTask → worker executes dispatch() → signal completion → release semaphore
Overflow handling: Queue full → HTTP 503; Timeout → HTTP 504
Shared-State Safety
| Resource |
Lock Type |
Strategy |
PersistentStore |
RLock + file lock |
Process-local + inter-process |
SQLDatabase |
RLock + file lock |
Process-local + inter-process |
RateLimiter |
RLock |
Memory-based or provider transactions |
StateStore |
RLock |
Snapshot copy semantics |
WebSocketHub |
RLock |
Room membership isolation |
Render cache |
_render_lock |
Single-writer |
Async Programming Model
import std/async
// Create event loop
let loop = async.get_event_loop()
// Spawn concurrent tasks
async fn fetch_data(url) {
let response = await http.get(url)
return response.body
}
// Run tasks concurrently
let results = await async.gather(
fetch_data("https://api1.example.com"),
fetch_data("https://api2.example.com"),
fetch_data("https://api3.example.com")
)
// Synchronization primitives
let lock = async.Lock()
await lock.acquire()
// ... critical section ...
lock.release()
let sem = async.Semaphore(10) // Max 10 concurrent
await sem.acquire()
// ... limited concurrency section ...
sem.release()
Multi-Process Lock Strategy
- In-process: Shared path lock map (
PersistentStore._path_locks)
- Cross-process (Linux):
fcntl.flock() file locking
- Cross-process (Windows):
msvcrt.locking() file locking
- Atomic guarantees: Serialize → temp → flush + fsync →
os.replace() → fsync(dir)
🔄 Migration From Other Languages (Comprehensive)
From Python
# Python # Nyx
# ------ # ---
def hello(name): fn hello(name) {
return f"Hello, {name}" return "Hello, " + name
}
numbers = [x**2 for x in range(10)] let numbers = [x**2 for x in 0..10]
for i, item in enumerate(lst): for i, item in lst {
print(f"{i}: {item}") print(str(i) + ": " + str(item))
}
class Dog: class Dog {
def __init__(self, name): fn init(self, name) {
self.name = name self.name = name
def bark(self): }
return f"{self.name}: Woof!" fn bark(self) {
return self.name + ": Woof!"
}
}
try: try {
risky() risky()
except ValueError as e: } catch e {
print(e) print(e)
finally: } finally {
cleanup() cleanup()
}
with open("f.txt") as f: let content = read("f.txt")
content = f.read()
import json import std/json
data = json.loads(text) let data = json.parse(text)
# Lambda # Lambda
add = lambda a, b: a + b let add = |a, b| a + b
# Async # Async
import asyncio import std/async
async def fetch(): async fn fetch() {
await asyncio.sleep(1) await async.async_sleep(1)
}
# Dict comprehension # Pipeline
{k: v*2 for k, v in d.items()} d |> map(|k, v| [k, v*2]) |> to_dict()
From JavaScript/TypeScript
// JavaScript // Nyx
// ---------- // ---
const add = (a, b) => a + b; let add = |a, b| a + b
const arr = [1, 2, 3]; let arr = [1, 2, 3]
arr.map(x => x * 2); arr |> map(|x| x * 2)
arr.filter(x => x > 1); arr |> filter(|x| x > 1)
arr.reduce((a,b) => a+b, 0); arr |> reduce(0, |a, b| a + b)
// Optional chaining // Optional chaining (same!)
obj?.prop?.method?.() obj?.prop?.method?.()
// Nullish coalescing // Null coalescing (same!)
value ?? defaultValue value ?? defaultValue
// Destructuring // Pattern matching
const {name, age} = person; let {name, age} = person
const [first, ...rest] = arr; let [first, ...rest] = arr
// Promise // Future
fetch(url) http.get(url)
.then(r => r.json()) |> then(|r| json.parse(r.body))
.catch(e => console.error(e)); |> catch(|e| print(e))
// async/await (same pattern!)
async function getData() { async fn get_data() {
const res = await fetch(url); let res = await http.get(url)
return res.json(); return json.parse(res.body)
} }
// Express server // Nyx server
const express = require('express'); use nyhttpd
const app = express(); let server = nyhttpd.HttpServer.new({port: 3000})
app.get('/', (req, res) => { server.get("/", fn(req, res) {
res.json({msg: "hello"}); res.json({msg: "hello"})
}); })
app.listen(3000); server.start()
// Class (nearly identical!)
class Animal { class Animal {
constructor(name) { fn init(self, name) {
this.name = name; self.name = name
} }
speak() { fn speak(self) {
return `${this.name} speaks`; return self.name + " speaks"
} }
} }
From Rust
// Rust // Nyx
// ---- // ---
fn add(a: i32, b: i32) -> i32 { fn add(a: int, b: int) -> int {
a + b a + b
} }
let v: Vec<i32> = vec![1,2,3]; let v = [1, 2, 3]
// Ownership (similar concept!)
let s1 = String::from("hello"); let s1 = "hello" // owned
let s2 = s1; // s1 moved let s2 = move s1 // explicit move
// Borrowing
fn len(s: &String) -> usize { fn len(s: &str) -> int {
s.len() return len(s)
} }
// Pattern matching (similar!)
match value { match value {
1 => println!("one"), case 1 => print("one")
2 | 3 => println!("two/three"), case 2 | 3 => print("two/three")
_ => println!("other"), case _ => print("other")
} }
// Traits (similar!)
trait Speak { trait Speak {
fn speak(&self) -> String; fn speak(self) -> str
} }
impl Speak for Dog { impl Speak for Dog {
fn speak(&self) -> String { fn speak(self) -> str {
format!("Woof!") return "Woof!"
} }
} }
// Error handling
match do_something() { try {
Ok(val) => use_val(val), let val = do_something()
Err(e) => eprintln!("{}", e), use_val(val)
} } catch e { print(e) }
// Async
async fn fetch() -> Result<Data> { async fn fetch() {
let data = reqwest::get(url) let data = await http.get(url)
.await? return json.parse(data.body)
.json().await?; }
Ok(data)
}
From Go
// Go // Nyx
// -- // ---
func add(a, b int) int { fn add(a, b) = a + b
return a + b
}
// Goroutines → spawn
go handleRequest(conn) spawn handle_request(conn)
// Channels → channels
ch := make(chan int, 10) let ch = channel(10)
ch <- 42 ch.send(42)
val := <-ch let val = ch.recv()
// Select → select
select { select {
case msg := <-ch1: case msg from ch1 => handle(msg)
handle(msg) case msg from ch2 => process(msg)
case msg := <-ch2: case timeout(5) => print("timeout")
process(msg) }
case <-time.After(5 * time.Second):
fmt.Println("timeout")
}
// Struct // Struct
type Point struct { struct Point {
X, Y float64 x: f64,
} y: f64
}
// Interface // Trait
type Stringer interface { trait Stringer {
String() string fn to_string(self) -> str
} }
// Error handling // Error handling
result, err := doSomething() try {
if err != nil { let result = do_something()
log.Fatal(err) } catch e {
} log.critical(str(e))
}
// HTTP server
http.HandleFunc("/", handler) use nyhttpd
http.ListenAndServe(":8080", nil) let s = nyhttpd.HttpServer.new({port: 8080})
s.get("/", handler)
s.start()
From C/C++
// C/C++ // Nyx
// ---- // ---
#include <stdio.h>
int main() { print("Hello, World!")
printf("Hello, World!\n");
return 0;
}
// Pointers // References
int* ptr = &value; let ref_val = &value
*ptr = 42; *ref_val = 42
// Memory allocation // Smart pointers
int* arr = malloc(10 * sizeof(int)); import std/smart_ptrs
free(arr); let arr = Box.new([0; 10])
// automatic cleanup
// Templates // Generics
template<typename T> fn max_val<T: Ord>(a: T, b: T) -> T {
T max_val(T a, T b) { if a > b { return a }
return a > b ? a : b; return b
} }
// SIMD (SSE intrinsics) // SIMD (clean API)
#include <immintrin.h> import std/simd
__m128 a = _mm_set_ps(1,2,3,4); let a = Vec4f(1.0, 2.0, 3.0, 4.0)
__m128 b = _mm_set_ps(5,6,7,8); let b = Vec4f(5.0, 6.0, 7.0, 8.0)
__m128 c = _mm_add_ps(a, b); let c = a.add(b)
// Inline assembly // Inline assembly
__asm__ ("mov %0, %%eax" : : "r"(x)); asm!("mov {0}, eax", x)
From Java/Kotlin
// Java // Nyx
// ---- // ---
public class Main { // No boilerplate needed!
public static void main(String[] a) {
System.out.println("Hello"); print("Hello")
}
}
// Verbose types // Type inference
List<String> names = new ArrayList<>(); let names = ["Alice", "Bob"]
Map<String, Integer> map = new HashMap<>(); let map = {Alice: 1, Bob: 2}
// Streams // Pipelines
list.stream() list
.filter(x -> x > 5) |> filter(|x| x > 5)
.map(x -> x * 2) |> map(|x| x * 2)
.collect(Collectors.toList()); |> to_list()
// Try-with-resources // with statement
try (var fs = new FileStream("f")) { with File("f", "r") as fs {
} }
Quick Comparison Table
| Feature |
Python |
JavaScript |
Rust |
Go |
C++ |
Java |
Nyx |
| Hello World |
1 line |
1 line |
5 lines |
7 lines |
6 lines |
5 lines |
1 line |
| HTTP server |
5 lines + pip |
5 lines + npm |
20 lines + cargo |
10 lines |
50+ lines |
20+ lines + Maven |
5 lines |
| JSON parse |
import json |
Built-in |
serde crate |
encoding/json |
nlohmann |
Jackson |
Built-in |
| Async |
asyncio |
Built-in |
tokio crate |
goroutines |
std::async |
CompletableFuture |
Built-in |
| ML/AI |
pip install |
npm install |
tch-rs crate |
gonum |
- |
DL4J |
Built-in (21 engines) |
| GUI |
pip install |
Electron |
gtk-rs crate |
fyne |
Qt |
Swing/JavaFX |
Built-in |
| Package count needed |
5-20 |
50-200 |
10-30 |
5-15 |
5-10 |
10-20 |
0 |
| Semicolons |
No |
Required |
Required |
No |
Required |
Required |
Optional |
| Null safety |
No |
No |
Yes (Option) |
No (nil) |
No |
No (nullable) |
Yes (??, ?.) |
| Memory safety |
GC |
GC |
Ownership |
GC |
Manual |
GC |
Ownership + GC |
📅 2-Month Mastery Roadmap
Week 1-2: Foundations
- [ ] Install Nyx and VS Code extension
- [ ] Write hello.ny and run it with
nyx hello.ny
- [ ] Learn variables, types, and operators (Chapters 2-3)
- [ ] Master control flow: if/elif/else, for, while, match (Chapter 4)
- [ ] Write 10 small programs (calculator, guessing game, FizzBuzz, palindrome checker, etc.)
- [ ] Practice with pipelines:
data |> filter(...) |> map(...)
Week 3-4: Functions & Data
- [ ] Master functions, default params, and recursion (Chapter 5)
- [ ] Learn arrays, objects, array comprehensions (Chapters 6-7)
- [ ] Study classes, inheritance, and OOP patterns (Chapter 8)
- [ ] Practice traits and generics (Chapter 9)
- [ ] Learn error handling with try/catch/finally (Chapter 11)
- [ ] Build a CLI tool using
import std/cli
Week 5-6: Advanced Features
- [ ] Pattern matching and destructuring (Chapter 10)
- [ ] Modules, imports, and project organization (Chapter 12)
- [ ] Closures, lambdas, higher-order functions (Chapter 13)
- [ ] Pipelines and comprehensions in depth (Chapter 14)
- [ ] Async programming: futures, tasks, event loops (Chapter 15)
- [ ] Enums, structs, iterators, generators (Chapters 21-22)
- [ ] Build an HTTP server with REST API using
use nyhttpd
- [ ] Write tests with
import std/test
Week 7-8: Mastery & Specialization
- [ ] Memory model and ownership (Chapter 16)
- [ ] Low-level systems programming: SIMD, atomics, allocators (Chapter 17)
- [ ] FFI and C interop (Chapter 18)
- [ ] Macros and metaprogramming (Chapter 23)
- [ ] Compile-time computation (Chapter 24)
- [ ] Advanced type system: dependent types, refinement types (Chapter 25)
- [ ] Explore 5+ engines that match your interests
- [ ] Build a complete project:
- Web app with database (nyhttpd + nydatabase)
- Game with physics (nygame + nyphysics)
- ML model with training pipeline (nynet + nyopt + nytensor)
- CLI tool with config management (std/cli + std/config)
- Concurrent data processing pipeline (std/async + nystream)
- [ ] Read stdlib source code for deep understanding
After 2 Months: You Are a Nyx Master
- You can build anything: web apps, APIs, games, AI models, CLI tools, system utilities
- You understand low-level concepts: memory, ownership, SIMD, FFI, inline assembly
- You can use any of the 117+ engines for specialized tasks
- You write clean, fast, safe code that outperforms Python by 10-100x
- You can contribute to the Nyx language itself
- You understand the compiler pipeline: Lexer → Parser → AST → Interpreter/Codegen
❓ FAQ & Troubleshooting
General FAQ
Q: Is Nyx free?
A: Yes. 100% free and MIT-licensed. All 117+ engines, 109 stdlib modules, native compiler, and VS Code extension are included at no cost.
Q: Do I need to install packages for basic features?
A: No. Everything is built-in: web servers, JSON, crypto, AI/ML, databases, GUI, game engine, file I/O — all native. Just import or use.
Q: Is Nyx ready for production?
A: Yes. The native compiler produces optimized C99 binaries. The web runtime handles 15K+ req/sec with atomic persistence. All engines are production-tested.
Q: What platforms does Nyx support?
A: Windows (x86_64, ARM64), Linux (x86_64, ARM64, RISC-V), macOS (x86_64, Apple Silicon). The C99 compiler works on any platform with a C compiler.
Q: Are semicolons required?
A: No. Semicolons are completely optional. Both styles work:
let x = 42 // without semicolon
let y = 43; // with semicolon — both valid
Q: What file extension does Nyx use?
A: .ny is the standard and only supported extension.
Language FAQ
Q: Can Nyx replace Python?
A: For most use cases, yes. Nyx is 10-100x faster, uses 10x less memory, has built-in AI/ML engines, and requires far less boilerplate. Python's advantage is its massive ecosystem (410K+ PyPI packages).
Q: Can Nyx replace JavaScript?
A: For backend, yes — Nyx's HTTP server outperforms Node.js. For frontend, Nyx compiles to WebAssembly. But the browser DOM ecosystem is still JS-dominated.
Q: Can Nyx do systems programming like C/Rust?
A: Yes. Nyx has inline assembly (x86, ARM, RISC-V), SIMD vectorization (SSE, AVX, NEON), DMA, atomic operations, custom allocators (Arena, Pool, Slab, Stack, FreeList), ownership/borrowing, and smart pointers (Box, Rc, Arc).
Q: Can Nyx build games?
A: Yes. Use nygame for ECS game engine, nyrender for 3D rendering (PBR, shadows, post-processing), nyphysics for rigid body/fluid/cloth simulation, nyaudio for 3D spatial audio, and nyanim for skeletal animation with IK.
Q: Can Nyx build AI/ML models?
A: Yes. 21 built-in AI/ML engines covering neural networks, reinforcement learning, GANs, graph neural networks, auto-differentiation with GPU acceleration, mixed-precision training, federated learning, and model serving — all zero dependencies.
Q: Can Nyx build web apps?
A: Yes. Full-stack: nyhttpd server (15K+ req/sec, HTTP/2, TLS), nyapi REST framework (OpenAPI generation, JWT auth), nydatabase (SQL/NoSQL), nyui native UI or WebAssembly frontend.
Q: How does Nyx compare to Rust for safety?
A: Similar model — ownership, borrowing, lifetimes, smart pointers. But Nyx is more pragmatic: you can opt out with unsafe {}, and the syntax is simpler. Nyx also has a GC fallback for when ownership is too complex.
Q: Can I use Nyx for competitive programming?
A: Yes. Fast I/O, built-in sort/search/graph algorithms (import std/algorithm), concise syntax, array comprehensions, and ranges make it excellent for competitions.
Troubleshooting
Problem: File not found at main.ny
# Check you're in the right directory
pwd
# Check the file exists
ls *.ny
# Use the full path
nyx /path/to/main.ny
Problem: Parser error: Unexpected token
// Common causes:
// 1. Missing closing brace
fn hello() {
print("hello")
} // <-- make sure this exists
// 2. Missing comma in arrays/objects
let arr = [1, 2, 3] // commas required
let obj = {a: 1, b: 2} // commas required
// 3. Using = instead of == in conditions
if x == 5 { } // correct
if x = 5 { } // WRONG — this is assignment
Problem: Maximum steps exceeded
# Increase the step limit
nyx --max-steps 10000000 file.ny
# Or fix the infinite loop in your code
while true { // infinite loop!
break // add exit condition
}
Problem: Module not found
# Check your import path
import std/math # stdlib modules: std/module_name
import "lib/my_mod" # local modules: relative path
# Set NYX_PATH if needed
export NYX_PATH="/path/to/nyx/stdlib:."
Problem: Stack overflow
// Add a base case to recursive functions
fn factorial(n) {
if n <= 1 { return 1 } // base case required!
return n * factorial(n - 1)
}
// Or increase the call depth limit
// nyx --max-call-depth 500 file.ny
Problem: Import resolution issues
// Use the correct syntax for each module type:
import std/json // Stdlib: import std/<module>
import "relative/path" // Local: import "path/to/file"
use nyhttpd // Engine: use <engine_name>
from std/crypto import sha256 // Named import
📚 Documentation Index
All 90+ documentation files available in the docs/ directory.
Core Language
| Document |
Description |
LANGUAGE_SPEC.md |
Complete language specification (bootstrap draft) |
NYX_LANGUAGE_SPEC.md |
Full language specification |
NYX_LANGUAGE_SPECIFICATION_V2.md |
V2 specification with advanced features |
QUICK_REFERENCE.md |
Quick syntax reference card |
USER_GUIDE.md |
User guide for beginners |
SYNTAX_ENHANCEMENTS_INDEX.md |
Index of all syntax enhancements |
SYNTAX_ENHANCEMENTS_SUMMARY.md |
Summary of syntax additions |
DUAL_IMPORT_SYNTAX.md |
import vs use explained |
IMPORT_USE_EXAMPLES.md |
Import/use examples |
SEMICOLON_USAGE.md |
Semicolon conventions |
legacy_syntax.md |
Legacy syntax compatibility |
Architecture & Design
| Document |
Description |
ARCHITECTURE.md |
System architecture overview |
NYX_V1_ARCHITECTURE.md |
V1 architecture design |
memory_model.md |
Memory model specification |
concurrency_model.md |
Concurrency model specification |
distributed_mode.md |
Distributed execution mode |
VM_SPEC.md |
Virtual machine specification |
VM_ARCHITECTURE_VISUAL.md |
Visual VM architecture diagrams |
Implementation
| Document |
Description |
NATIVE_IMPLEMENTATION.md |
Native compiler details |
BOOTSTRAP.md |
Self-hosting bootstrap |
ROOT_BOOTSTRAP.md |
Root bootstrap process |
BLUEPRINT_IMPLEMENTATION_STATUS.md |
Feature implementation status |
FEATURE_MAP.md |
Complete feature mapping |
STDLIB_ROADMAP.md |
Standard library roadmap |
COMPLETION_REPORT.md |
Implementation completion report |
Production & Deployment
| Document |
Description |
PRODUCTION_GUIDE.md |
Production deployment guide |
DEPLOYMENT_GUIDE.md |
Step-by-step deployment |
DEPLOYMENT_CHECKLIST.md |
Pre-deployment checklist |
production_deployment_guide.md |
Detailed production guide |
scaling_guide.md |
Scaling strategies |
observability.md |
Monitoring and observability |
Security
| Document |
Description |
SECURITY.md |
Security overview |
security_audit.md |
Security audit results |
security_best_practices.md |
Security best practices |
Engines & Low-Level
| Document |
Description |
ENGINE_STACK_COMPLETE.md |
All engines documentation |
ALL_ENGINES_10_OF_10.md |
Engine completeness verification |
SYSTEM_PROGRAMMING_CAPABILITIES.md |
Systems programming features |
LOW_LEVEL_PRODUCTION_GUIDE.md |
Low-level production guide |
IOMMU_GUIDE.md |
IOMMU virtualization guide |
IOMMU_SPECIFICATION.md |
IOMMU specification |
KERNEL_BOOT_CI_IMPLEMENTATION.md |
Kernel boot CI |
DFAS_DOCUMENTATION.md |
DFAS system docs |
Versioning
| Document |
Description |
V0.md through V4.md |
Version history and changelogs |
RELEASE_NOTES.md |
Release notes |
RELEASE_POLICY.md |
Release policy |
COMPATIBILITY_LIFECYCLE.md |
API compatibility guarantees |
PY_API_STABILITY.md |
Python API stability |
🏆 Nyx vs Everything: Why Nyx Wins
Lines of Code Comparison
| Task |
Python |
JavaScript |
Go |
Rust |
C++ |
Nyx |
| Hello World |
1 |
1 |
7 |
4 |
6 |
1 |
| HTTP Server |
8 |
6 |
15 |
25 |
50+ |
5 |
| REST API + DB |
30 |
25 |
40 |
60 |
100+ |
15 |
| Neural Network |
20 |
N/A |
N/A |
30 |
N/A |
15 |
| File Read + Parse JSON |
5 |
4 |
12 |
8 |
15 |
3 |
| WebSocket Server |
15 |
10 |
20 |
30 |
60+ |
8 |
| CLI Tool with Args |
20 |
15 |
25 |
20 |
40+ |
10 |
| Database CRUD |
25 |
20 |
30 |
40 |
60+ |
12 |
| Unit Test Suite |
15 |
12 |
20 |
15 |
30+ |
8 |
Dependency Comparison
| Task |
Python (pip) |
JavaScript (npm) |
Rust (cargo) |
Nyx |
| Web Server |
flask, gunicorn |
express, cors |
actix-web, tokio |
0 (built-in) |
| JSON |
json (stdlib) |
built-in |
serde, serde_json |
0 (built-in) |
| HTTP Client |
requests |
axios, node-fetch |
reqwest |
0 (built-in) |
| Database |
sqlalchemy, psycopg2 |
pg, mongoose |
diesel, sqlx |
0 (built-in) |
| Testing |
pytest |
jest, mocha |
built-in |
0 (built-in) |
| Crypto |
cryptography |
crypto, bcrypt |
ring, aes |
0 (built-in) |
| ML/AI |
torch, numpy, sklearn |
brain.js, tensorflow |
tch-rs |
0 (built-in, 21 engines) |
| CLI Args |
argparse, click |
commander, yargs |
clap, structopt |
0 (built-in) |
| Logging |
logging (stdlib) |
winston, pino |
log, env_logger |
0 (built-in) |
| GUI |
tkinter, PyQt |
electron |
gtk-rs, iced |
0 (built-in) |
| Total deps |
10-30 |
15-50 |
10-20 |
0 |
| Benchmark |
Python |
Node.js |
Go |
Rust |
Nyx (native) |
| Fibonacci(35) |
2.8s |
0.15s |
0.05s |
0.03s |
0.04s |
| Prime sieve 1M |
3.5s |
0.8s |
0.15s |
0.08s |
0.10s |
| Matrix 1000×1000 |
12s |
2s |
0.3s |
0.2s |
0.25s |
| HTTP throughput |
300 rps |
15K rps |
30K rps |
50K rps |
15K+ rps |
| Startup time |
50ms |
30ms |
10ms |
2ms |
5ms |
| Base memory |
15 MB |
30 MB |
5 MB |
2 MB |
2 MB |
| Per integer |
28 bytes |
8 bytes |
8 bytes |
8 bytes |
8 bytes |
Feature Matrix
| Feature |
Python |
JS |
Go |
Rust |
C++ |
Java |
Nyx |
| Type inference |
❌ |
❌ |
✅ |
✅ |
✅ |
❌ |
✅ |
| Pattern matching |
✅ (3.10+) |
❌ |
❌ |
✅ |
❌ |
✅ (21+) |
✅ |
| Ownership |
❌ |
❌ |
❌ |
✅ |
❌ |
❌ |
✅ |
| Null safety |
❌ |
❌ |
❌ |
✅ |
❌ |
❌ |
✅ |
| Generics |
✅ |
❌ |
✅ |
✅ |
✅ |
✅ |
✅ |
| Traits/Interfaces |
❌ |
❌ |
✅ |
✅ |
✅ |
✅ |
✅ |
| Macros |
❌ |
❌ |
❌ |
✅ |
✅ |
❌ |
✅ |
| Async/Await |
✅ |
✅ |
✅ |
✅ |
✅ |
✅ |
✅ |
| Inline Assembly |
❌ |
❌ |
❌ |
✅ |
✅ |
❌ |
✅ |
| SIMD |
❌ |
❌ |
❌ |
✅ |
✅ |
❌ |
✅ |
| FFI (C interop) |
ctypes |
N-API |
cgo |
Built-in |
Native |
JNI |
✅ |
| Pipelines |
❌ |
❌ |
❌ |
Iterator |
❌ |
Stream |
✅ (\|>) |
| Comprehensions |
✅ |
❌ |
❌ |
❌ |
❌ |
❌ |
✅ |
| Built-in ML |
❌ |
❌ |
❌ |
❌ |
❌ |
❌ |
✅ (21 engines) |
| Built-in Web Server |
❌ |
❌ |
✅ |
❌ |
❌ |
❌ |
✅ (15K+ rps) |
| Built-in GUI |
tkinter |
❌ |
❌ |
❌ |
❌ |
Swing |
✅ |
| Built-in Crypto |
❌ |
❌ |
❌ |
❌ |
❌ |
❌ |
✅ (30+ algorithms) |
🌍 Real-World Examples From the Codebase
These examples are based on actual files in the Nyx repository examples/ directory.
Based on examples/embedded/firmware.ny
#[no_std]
#[memory_model = "manual"]
#[target = "thumbv7em-none-eabi"]
#[entry = "reset_handler"]
import std/hardware
import std/interrupts
// Constants
const FLASH_BASE: u32 = 0x08000000
const SRAM_BASE: u32 = 0x20000000
const SRAM_SIZE: u32 = 0x00020000 // 128KB
const STACK_TOP: u32 = SRAM_BASE + SRAM_SIZE
// Exception vector table (must be at address 0x00000000)
#[section = ".vectors"]
#[used]
const VECTOR_TABLE: [fn(); 16] = [
STACK_TOP as fn(), // Initial stack pointer
reset_handler, // Reset
nmi_handler, // NMI
hardfault_handler, // Hard Fault
memmanage_handler, // Memory Management
busfault_handler, // Bus Fault
usagefault_handler, // Usage Fault
null, null, null, null, // Reserved
svc_handler, // SVCall
debugmon_handler, // Debug Monitor
null, // Reserved
pendsv_handler, // PendSV
systick_handler, // SysTick
]
// Reset handler — entry point after power-on
#[no_mangle]
fn reset_handler() {
// Initialize .data section (copy from Flash to SRAM)
unsafe {
let mut src = &__etext as *const u32
let mut dst = &__sdata as *mut u32
let end = &__edata as *const u32
while (dst as u32) < (end as u32) {
*dst = *src
src = src.offset(1)
dst = dst.offset(1)
}
}
// Zero .bss section
unsafe {
let mut dst = &__sbss as *mut u32
let end = &__ebss as *const u32
while (dst as u32) < (end as u32) {
*dst = 0
dst = dst.offset(1)
}
}
// Enable FPU (Cortex-M4F)
unsafe {
let cpacr = 0xE000ED88 as *mut u32
*cpacr = *cpacr | (0xF << 20)
asm!("dsb", "isb")
}
main()
}
fn main() {
// Configure GPIO for LED
let led = hardware.GPIO.new(hardware.Port.B, 3)
led.set_mode(hardware.PinMode.Output)
// Configure SysTick for 1ms intervals
hardware.systick_config(168_000_000 / 1000)
// Main loop
loop {
led.toggle()
hardware.delay_ms(500)
}
}
#[interrupt]
fn systick_handler() {
hardware.tick()
}
Example 14: OS Kernel with Paging & Interrupts
Based on examples/os_kernel/kernel_main.ny
#[no_std]
#[entry = "kernel_main"]
#[link_section = ".multiboot"]
import std/interrupts
import std/paging
// Multiboot2 header
#[repr(C)]
#[packed]
struct MultibootHeader {
magic: u32, // 0xE85250D6
architecture: u32, // 0 = i386
header_length: u32,
checksum: u32 // -(magic + arch + length)
}
#[used]
#[link_section = ".multiboot"]
const MULTIBOOT: MultibootHeader = MultibootHeader {
magic: 0xE85250D6,
architecture: 0,
header_length: 16,
checksum: 0 - (0xE85250D6 + 0 + 16)
}
// VGA text mode
const VGA_BUFFER: u32 = 0xB8000
const VGA_WIDTH: u32 = 80
const VGA_HEIGHT: u32 = 25
struct VgaWriter {
col: u32,
row: u32,
color: u8
}
impl VgaWriter {
fn new() -> VgaWriter {
VgaWriter { col: 0, row: 0, color: 0x0F }
}
fn write_char(self: &mut Self, c: char) {
if c == '\n' {
self.col = 0
self.row = self.row + 1
return
}
let offset = (self.row * VGA_WIDTH + self.col) * 2
unsafe {
let ptr = (VGA_BUFFER + offset) as *mut u16
*ptr = (self.color as u16) << 8 | (c as u16)
}
self.col = self.col + 1
if self.col >= VGA_WIDTH {
self.col = 0
self.row = self.row + 1
}
}
fn write_str(self: &mut Self, s: &str) {
for c in s { self.write_char(c) }
}
}
// Global Descriptor Table
#[repr(C)]
#[packed]
struct GDTEntry {
limit_low: u16,
base_low: u16,
base_mid: u8,
access: u8,
granularity: u8,
base_high: u8
}
fn setup_gdt() {
// Null descriptor + Code segment + Data segment
let gdt = [
GDTEntry { limit_low: 0, base_low: 0, base_mid: 0, access: 0, granularity: 0, base_high: 0 },
GDTEntry { limit_low: 0xFFFF, base_low: 0, base_mid: 0, access: 0x9A, granularity: 0xCF, base_high: 0 },
GDTEntry { limit_low: 0xFFFF, base_low: 0, base_mid: 0, access: 0x92, granularity: 0xCF, base_high: 0 },
]
interrupts.load_gdt(&gdt)
}
// 4-level page table setup
fn setup_paging() {
let pml4 = paging.PageTable.new()
// Identity-map first 2MB
pml4.identity_map(0x0, 0x200000, paging.PageFlags.PRESENT | paging.PageFlags.WRITABLE)
// Map VGA buffer
pml4.identity_map(0xB8000, 0xB9000, paging.PageFlags.PRESENT | paging.PageFlags.WRITABLE)
paging.enable(pml4)
}
#[no_mangle]
fn kernel_main() -> ! {
let mut vga = VgaWriter.new()
setup_gdt()
setup_paging()
interrupts.setup_idt()
interrupts.setup_pic()
vga.write_str("Nyx OS Kernel v0.1.0\n")
vga.write_str("GDT initialized\n")
vga.write_str("Paging enabled (4-level)\n")
vga.write_str("IDT + PIC configured\n")
vga.write_str("Kernel ready.\n")
// Halt loop
loop {
asm!("hlt")
}
}
Example 15: Full-Stack ML Web Service
Based on examples/ml_webapp.ny
use nyhttpd
import std/json
import std/math
import std/time
import std/log
let logger = log.get_logger("ml-service")
// Linear regression model with gradient descent
class LinearModel {
fn init(self, features) {
self.weights = []
for _ in range(features) { push(self.weights, math.random() * 0.01) }
self.bias = 0.0
self.lr = 0.01
}
fn predict(self, x) {
let mut sum = self.bias
for i in range(len(x)) {
sum = sum + self.weights[i] * x[i]
}
return sum
}
fn train(self, data, epochs) {
for epoch in range(epochs) {
let mut total_loss = 0.0
for sample in data {
let pred = self.predict(sample.features)
let error = pred - sample.target
total_loss = total_loss + error * error
// Gradient descent update
for i in range(len(self.weights)) {
self.weights[i] = self.weights[i] - self.lr * error * sample.features[i]
}
self.bias = self.bias - self.lr * error
}
if epoch % 100 == 0 {
logger.info("Epoch " + str(epoch) + " Loss: " + str(total_loss / len(data)))
}
}
}
}
// Data store
class DataStore {
fn init(self) {
self.samples = []
}
fn add(self, features, target) {
push(self.samples, {features: features, target: target})
}
fn get_all(self) { return self.samples }
fn count(self) { return len(self.samples) }
}
// Service combining model + data + API
let model = LinearModel(3)
let store = DataStore()
let server = nyhttpd.HttpServer.new({port: 8080, worker_threads: 4})
server.post("/api/data", fn(req, res) {
let body = json.parse(req.body)
store.add(body.features, body.target)
res.status(201).json({status: "added", count: store.count()})
})
server.post("/api/train", fn(req, res) {
let body = json.parse(req.body)
let epochs = body.epochs ?? 1000
let start = time.now_millis()
model.train(store.get_all(), epochs)
let elapsed = time.now_millis() - start
res.json({
status: "trained", epochs: epochs,
elapsed_ms: elapsed, samples: store.count()
})
})
server.post("/api/predict", fn(req, res) {
let body = json.parse(req.body)
let prediction = model.predict(body.features)
res.json({prediction: prediction, model: "linear_regression"})
})
server.get("/api/model", fn(req, res) {
res.json({weights: model.weights, bias: model.bias, lr: model.lr})
})
logger.info("ML Service running on :8080")
server.start()
Example 16: Space Shooter Game with AI
Based on examples/space_shooter_game.ny
use nygame
use nyai
// Ship configuration
const SCREEN_W = 800
const SCREEN_H = 600
const PLAYER_SPEED = 5
const BULLET_SPEED = 10
const ENEMY_SPAWN_RATE = 60 // frames
class Bullet {
fn init(self, x, y, dx, dy) {
self.x = x
self.y = y
self.dx = dx
self.dy = dy
self.active = true
}
fn update(self) {
self.x = self.x + self.dx
self.y = self.y + self.dy
if self.y < 0 or self.y > SCREEN_H { self.active = false }
}
}
class PlayerShip {
fn init(self) {
self.x = SCREEN_W / 2
self.y = SCREEN_H - 60
self.health = 100
self.score = 0
self.bullets = []
self.fire_cooldown = 0
}
fn move(self, dx, dy) {
self.x = clamp(self.x + dx * PLAYER_SPEED, 20, SCREEN_W - 20)
self.y = clamp(self.y + dy * PLAYER_SPEED, 20, SCREEN_H - 20)
}
fn shoot(self) {
if self.fire_cooldown <= 0 {
push(self.bullets, Bullet(self.x, self.y, 0, -BULLET_SPEED))
self.fire_cooldown = 10
}
}
fn update(self) {
if self.fire_cooldown > 0 { self.fire_cooldown = self.fire_cooldown - 1 }
for b in self.bullets { b.update() }
self.bullets = self.bullets |> filter(|b| b.active)
}
}
class EnemyShip {
fn init(self, type_name, x) {
self.x = x
self.y = -30
self.type_name = type_name
self.ai = nyai.Agent("hybrid")
match type_name {
case "fighter" => {
self.health = 30
self.speed = 3
self.points = 100
}
case "cruiser" => {
self.health = 80
self.speed = 1.5
self.points = 250
}
case "boss" => {
self.health = 500
self.speed = 0.5
self.points = 1000
}
}
}
fn update(self, player) {
let action = self.ai.decide({
player_x: player.x, player_y: player.y,
enemy_x: self.x, enemy_y: self.y,
health: self.health
})
match action {
case "chase" => {
let dx = clamp(player.x - self.x, -self.speed, self.speed)
self.x = self.x + dx
self.y = self.y + self.speed
}
case "strafe" => {
self.x = self.x + self.speed * 2 * (math.random() - 0.5)
self.y = self.y + self.speed * 0.5
}
case _ => {
self.y = self.y + self.speed
}
}
}
}
// Game loop
let player = PlayerShip()
let enemies = []
let mut frame = 0
fn game_loop() {
frame = frame + 1
// Spawn enemies
if frame % ENEMY_SPAWN_RATE == 0 {
let types = ["fighter", "fighter", "fighter", "cruiser"]
let t = types[int(math.random() * len(types))]
push(enemies, EnemyShip(t, int(math.random() * SCREEN_W)))
}
// Boss every 600 frames
if frame % 600 == 0 {
push(enemies, EnemyShip("boss", SCREEN_W / 2))
}
player.update()
for e in enemies { e.update(player) }
// Collision detection
for b in player.bullets {
for e in enemies {
let dx = abs(b.x - e.x)
let dy = abs(b.y - e.y)
if dx < 20 and dy < 20 {
e.health = e.health - 25
b.active = false
if e.health <= 0 {
player.score = player.score + e.points
}
}
}
}
enemies = enemies |> filter(|e| e.health > 0 and e.y < SCREEN_H + 50)
}
Example 17: Interactive Calculator with Parser
Based on examples/interactive_calculator.ny
// Token types for expression parsing
enum TokenKind { Number, Plus, Minus, Star, Slash, Percent, Power, FloorDiv, LParen, RParen, EOF }
struct Token {
kind: TokenKind,
value: str
}
// Tokenizer
fn tokenize(input) {
let tokens = []
let mut i = 0
while i < len(input) {
let c = input[i]
if c == " " { i = i + 1; continue }
if c >= "0" and c <= "9" or c == "." {
let mut num = ""
while i < len(input) and (input[i] >= "0" and input[i] <= "9" or input[i] == ".") {
num = num + input[i]
i = i + 1
}
push(tokens, Token{kind: TokenKind.Number, value: num})
continue
}
match c {
case "+" => push(tokens, Token{kind: TokenKind.Plus, value: "+"})
case "-" => push(tokens, Token{kind: TokenKind.Minus, value: "-"})
case "*" => {
if i + 1 < len(input) and input[i + 1] == "*" {
push(tokens, Token{kind: TokenKind.Power, value: "**"})
i = i + 1
} else {
push(tokens, Token{kind: TokenKind.Star, value: "*"})
}
}
case "/" => {
if i + 1 < len(input) and input[i + 1] == "/" {
push(tokens, Token{kind: TokenKind.FloorDiv, value: "//"})
i = i + 1
} else {
push(tokens, Token{kind: TokenKind.Slash, value: "/"})
}
}
case "%" => push(tokens, Token{kind: TokenKind.Percent, value: "%"})
case "(" => push(tokens, Token{kind: TokenKind.LParen, value: "("})
case ")" => push(tokens, Token{kind: TokenKind.RParen, value: ")"})
case _ => { print("Unknown character: " + c) }
}
i = i + 1
}
push(tokens, Token{kind: TokenKind.EOF, value: ""})
return tokens
}
// Recursive descent parser with operator precedence
class Parser {
fn init(self, tokens) {
self.tokens = tokens
self.pos = 0
}
fn current(self) { return self.tokens[self.pos] }
fn advance(self) { self.pos = self.pos + 1 }
fn parse(self) { return self.expr() }
fn expr(self) { return self.add_sub() }
fn add_sub(self) {
let mut left = self.mul_div()
while self.current().kind == TokenKind.Plus or self.current().kind == TokenKind.Minus {
let op = self.current().value
self.advance()
let right = self.mul_div()
if op == "+" { left = left + right }
else { left = left - right }
}
return left
}
fn mul_div(self) {
let mut left = self.power()
while self.current().kind == TokenKind.Star or
self.current().kind == TokenKind.Slash or
self.current().kind == TokenKind.Percent or
self.current().kind == TokenKind.FloorDiv {
let op = self.current().value
self.advance()
let right = self.power()
match op {
case "*" => left = left * right
case "/" => left = left / right
case "%" => left = left % right
case "//" => left = int(left / right)
}
}
return left
}
fn power(self) {
let base = self.unary()
if self.current().kind == TokenKind.Power {
self.advance()
let exp = self.power() // Right-associative
return base ** exp
}
return base
}
fn unary(self) {
if self.current().kind == TokenKind.Minus {
self.advance()
return -self.primary()
}
return self.primary()
}
fn primary(self) {
if self.current().kind == TokenKind.Number {
let val = float(self.current().value)
self.advance()
return val
}
if self.current().kind == TokenKind.LParen {
self.advance()
let val = self.expr()
self.advance() // skip RParen
return val
}
return 0
}
}
// Usage
let input = "2 ** 3 + (4 * 5 - 3) // 2"
let tokens = tokenize(input)
let parser = Parser(tokens)
let result = parser.parse()
print(input + " = " + str(result)) // "2 ** 3 + (4 * 5 - 3) // 2 = 16"
Example 18: GUI Calculator with History
Based on examples/gui_calculator.ny
import std/gui
import std/math
class Calculator {
fn init(self) {
self.display = ""
self.history = []
self.memory = 0
self.last_result = 0
}
fn digit(self, d) { self.display = self.display + str(d) }
fn decimal(self) { if !self.display.contains(".") { self.display = self.display + "." } }
fn clear(self) { self.display = "" }
fn backspace(self) { self.display = self.display[0..len(self.display)-1] }
fn operator(self, op) { self.display = self.display + " " + op + " " }
fn calculate(self) {
let expr = self.display
let result = eval(expr)
push(self.history, expr + " = " + str(result))
self.last_result = result
self.display = str(result)
}
// Scientific functions
fn sqrt_fn(self) { self.display = str(math.sqrt(float(self.display))) }
fn sin_fn(self) { self.display = str(math.sin(float(self.display))) }
fn cos_fn(self) { self.display = str(math.cos(float(self.display))) }
fn tan_fn(self) { self.display = str(math.tan(float(self.display))) }
fn log_fn(self) { self.display = str(math.log10(float(self.display))) }
fn ln_fn(self) { self.display = str(math.log(float(self.display))) }
fn factorial_fn(self) { self.display = str(math.factorial(int(self.display))) }
fn power_fn(self, exp) { self.display = str(float(self.display) ** exp) }
// Memory operations
fn mem_clear(self) { self.memory = 0 }
fn mem_recall(self) { self.display = str(self.memory) }
fn mem_add(self) { self.memory = self.memory + float(self.display) }
fn mem_sub(self) { self.memory = self.memory - float(self.display) }
}
let calc = Calculator()
let app = gui.Application("Scientific Calculator")
let window = gui.Window("Calculator", 400, 600)
window.add_label("display", "", {font_size: 28, bg: "#1e1e1e", fg: "#00ff00"})
// Number pad + operators
let buttons = [
["MC", "MR", "M+", "M-"],
["sin", "cos", "tan", "√"],
["log", "ln", "n!", "xʸ"],
["7", "8", "9", "/"],
["4", "5", "6", "*"],
["1", "2", "3", "-"],
["0", ".", "=", "+"],
["C", "⌫", "(", ")"],
]
for row in buttons {
for label in row {
window.add_button(label, fn() {
match label {
case "=" => calc.calculate()
case "C" => calc.clear()
case "⌫" => calc.backspace()
case "sin" => calc.sin_fn()
case "cos" => calc.cos_fn()
case "tan" => calc.tan_fn()
case "√" => calc.sqrt_fn()
case "log" => calc.log_fn()
case "ln" => calc.ln_fn()
case "n!" => calc.factorial_fn()
case "MC" => calc.mem_clear()
case "MR" => calc.mem_recall()
case "M+" => calc.mem_add()
case "M-" => calc.mem_sub()
case _ => {
if label >= "0" and label <= "9" { calc.digit(label) }
elif label == "." { calc.decimal() }
else { calc.operator(label) }
}
}
window.update_label("display", calc.display)
})
}
}
app.run(window)
🏗️ Architecture Deep Dive — Hypervisor Core
Nyx includes a full hypervisor architecture for virtualization workloads.
4-Layer Core Architecture
┌─────────────────────────────────────────────────────────┐
│ Guest VM │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ vCPU 0 │ │ vCPU 1 │ │ vCPU N │ │
│ └─────┬────┘ └─────┬────┘ └─────┬────┘ │
├─────────┼──────────────┼──────────────┼─────────────────┤
│ └──────────────┼──────────────┘ │
│ VM EXIT │
│ ┌──────────────────────────────────────────────────┐ │
│ │ Exit Dispatch Engine │ │
│ │ CPUID | MSR | CR | I/O | MMIO | INT | HLT | ... │ │
│ └──────────────────────┬───────────────────────────┘ │
│ │ │
│ ┌──────────────────────┼───────────────────────────┐ │
│ │ Device Bus Architecture │ │
│ │ ┌────────┐ ┌────────┐ ┌────────┐ ┌────────┐ │ │
│ │ │ VGA │ │ UART │ │ PIC │ │ PIT │ │ │
│ │ └────────┘ └────────┘ └────────┘ └────────┘ │ │
│ │ ┌────────┐ ┌────────┐ ┌────────┐ ┌────────┐ │ │
│ │ │ IOAPIC │ │ HPET │ │ TPM │ │ DMA │ │ │
│ │ └────────┘ └────────┘ └────────┘ └────────┘ │ │
│ └──────────────────────────────────────────────────┘ │
│ │ │
│ ┌──────────────────────┼───────────────────────────┐ │
│ │ Memory Management (EPT/NPF) │ │
│ │ Guest Physical ─→ EPT Walk ─→ Host Physical │ │
│ │ Dirty tracking: 1 bit per 4KB page │ │
│ └──────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────┘
VM Exit Handling (48+ exit codes)
| Exit Code |
Name |
Handler |
Description |
| 0 |
CPUID |
Emulate/passthrough |
CPU feature enumeration |
| 1 |
MSR_READ |
Filter + emulate |
Model-specific register read |
| 2 |
MSR_WRITE |
Filter + emulate |
Model-specific register write |
| 3-6 |
CR0-CR4_WRITE |
Emulate |
Control register modifications |
| 7-8 |
DR_READ/WRITE |
Emulate |
Debug register access |
| 10 |
IO_IN |
Device bus dispatch |
Port I/O read |
| 11 |
IO_OUT |
Device bus dispatch |
Port I/O write |
| 12 |
MMIO_READ |
Device bus dispatch |
Memory-mapped I/O read |
| 13 |
MMIO_WRITE |
Device bus dispatch |
Memory-mapped I/O write |
| 14 |
EPT_VIOLATION |
Page fault handling |
Extended page table miss |
| 15 |
EPT_MISCONFIG |
Error recovery |
EPT configuration error |
| 20 |
EXTERNAL_INT |
LAPIC inject |
External interrupt |
| 21 |
NMI |
NMI handler |
Non-maskable interrupt |
| 22 |
INT_WINDOW |
Pending inject |
Interrupt window opened |
| 30 |
HLT |
vCPU halt |
Halt instruction |
| 31 |
PAUSE |
Spin-wait detect |
Pause instruction |
| 32 |
RDTSC |
Emulate/offset |
Timestamp counter read |
| 40 |
SHUTDOWN |
VM shutdown |
Triple fault / shutdown |
| 41 |
INIT |
Reset vCPU |
INIT signal |
| 42 |
SIPI |
Start AP |
Startup IPI |
vCPU State Model
struct VCPUState {
// General purpose registers
rax: u64, rbx: u64, rcx: u64, rdx: u64,
rsi: u64, rdi: u64, rbp: u64, rsp: u64,
r8: u64, r9: u64, r10: u64, r11: u64,
r12: u64, r13: u64, r14: u64, r15: u64,
// Instruction pointer & flags
rip: u64, rflags: u64,
// Segment registers
cs: SegmentRegister, ds: SegmentRegister,
es: SegmentRegister, fs: SegmentRegister,
gs: SegmentRegister, ss: SegmentRegister,
// Control registers
cr0: u64, cr2: u64, cr3: u64, cr4: u64,
// Execution state
state: enum { RUNNING, PENDING_EXIT, EXITED, HALTED, PAUSED, FAULTED },
// Performance
exits_total: u64,
cycles_in_guest: u64,
cycles_in_host: u64
}
Error Recovery Strategies
| Strategy |
Usage |
Description |
IGNORE |
Non-critical exits |
Log and continue |
RESET_DEVICE |
Device error |
Reset specific virtual device |
RESET_VCPU |
Soft CPU fault |
Reset vCPU to known state |
HARD_RESET |
Unrecoverable |
Reset entire VM |
PAUSE_VM |
Debug/inspect |
Pause all vCPUs |
SNAPSHOT_RESTORE |
Checkpoint recovery |
Rollback to last snapshot |
ISOLATE_DEVICE |
Misbehaving device |
Disconnect device from bus |
SHUTDOWN |
Fatal error |
Graceful VM shutdown |
🌐 Nyx Runtime Server (nyx_runtime.py — Complete)
Built-in production-grade HTTP server implementation.
Runtime Configuration
struct RuntimeConfig {
script: str, // Entry .ny file
host: str, // Bind address (default: "127.0.0.1")
port: int, // Listen port (default: 9000)
site_name: str, // Site display name
site_tagline: str, // Site tagline
storage_dir: str, // Persistent data directory
repo_root: str // Repository root path
}
Server Classes & API
| Class |
Methods |
Description |
PersistentStore(path) |
get(key), set(key, val), has(key), delete(key), keys(), values(), items(), transaction(fn) |
Thread-safe JSON-backed KV store with atomic writes |
Request |
method, path, headers, body, raw_body, content_type, client_ip, json() |
Parsed HTTP request |
Response |
status, body, headers |
HTTP response builder |
HttpRoute |
path, methods, handler |
Route definition |
Application(name) |
worker_model(), worker_pool(size), worker_stats(), _dispatch_via_pool(task) |
Application with worker pool |
NyxRuntimeServer(cfg) |
metric(key), set_metric(key, val), bump_visit(), bump_run(), next_signup(), save_signup(email), preview_html(code) |
Full runtime + metrics |
NyxHandler |
do_GET(), do_POST(), _send_json(), _send_text(), _serve_asset() |
HTTP request handler |
API Endpoints
| Method |
Path |
Response |
Description |
| GET |
/ |
HTML |
Landing page |
| GET |
/docs |
HTML |
Documentation page |
| GET |
/ecosystem |
HTML |
Ecosystem overview |
| GET |
/playground |
HTML |
Interactive playground |
| GET |
/api/health |
JSON |
Health check with uptime |
| GET |
/api/overview |
JSON |
Language stats + version |
| GET |
/api/metrics |
JSON |
Visit/run/signup counters |
| POST |
/api/community/subscribe |
JSON |
Email signup |
| POST |
/api/playground/run |
JSON |
Execute Nyx code (sandboxed) |
| GET |
/assets/* |
File |
Static asset serving (path-traversal protected) |
Security Features
# Path traversal protection (actual implementation)
def _serve_asset(self, path):
assets_root = Path(cfg.repo_root) / "assets"
target = (assets_root / path).resolve()
target.relative_to(assets_root) # Raises ValueError if escape attempt
# ... serve file only if within assets_root ...
🧪 Testing Framework Deep Dive
Complete testing infrastructure with 17 test levels.
Test Directory Structure
tests/
├── aaa_readiness/ # Pre-flight checks
├── level1_lexer/ # Lexer unit tests
├── level2_parser/ # Parser unit tests
├── level3_interpreter/ # Interpreter unit tests
├── level4_stress/ # Stress tests (load, memory, CPU)
├── level5_stdlib/ # Standard library tests
├── level6_security/ # Security vulnerability tests
├── level7_performance/ # Performance regression tests
├── level8_compliance/ # Language specification compliance
├── level9_consistency/ # Cross-platform consistency
├── level10_realworld/ # Real-world scenario tests
├── benchmarks/ # Performance benchmarks
├── database/ # Database integration tests
├── deployment/ # Deployment validation
├── engines/ # Engine integration tests
├── failure_tests/ # Expected failure scenarios
├── generated/ # Auto-generated test suites
├── hardening/ # Security hardening tests
├── metrics_dashboard_example/# Metrics dashboard test
├── multi_node_test_suite/ # Multi-node distributed tests
├── nyui/ # UI framework tests
├── production/ # Production environment tests
├── security_tests/ # Additional security tests
├── security_web/ # Web security tests (XSS, CSRF, SQLi)
├── server_hosting/ # Server hosting tests
├── stress_tests/ # Additional stress tests
├── thread_safety_tests/ # Thread safety verification
├── web_framework/ # Web framework tests
├── worldclass/ # World-class quality tests
├── checkup/ # Health check tests
│
├── test_basic.ny # Basic language features
├── test_borrow_checker.py # Ownership/borrowing tests
├── test_full.js # Full test suite (JS runner)
├── test_full.ps1 # Full test suite (PowerShell runner)
├── test_generator.ny # Generator/yield tests
├── test_hello.ny # Smoke test
├── test_interpreter.py # Interpreter unit tests
├── test_lexer.py # Lexer unit tests
├── test_nyai.ny # AI engine tests
├── test_nyanim.ny # Animation engine tests
├── test_nyaudio.ny # Audio engine tests
├── test_nycore.ny # Core engine tests
├── test_nynet.ny # Network engine tests
├── test_nyphysics.ny # Physics engine tests
├── test_nypm.ny # Package manager tests
├── test_nyrender.ny # Render engine tests
├── test_nyweb_simple.ny # Web engine tests
├── test_nyworld.ny # World engine tests
├── test_ownership.py # Ownership model tests
├── test_parser.py # Parser unit tests
├── test_polyglot.py # Polyglot execution tests
├── test_semicolons.ny # Semicolon handling tests
├── test_semicolon_optional.ny # Optional semicolons
├── test_stdlib.ny # Stdlib integration tests
├── test_string_concat_native.ny # Native string operations
├── test_use.ny # Engine `use` tests
├── test_vars.ny # Variable declaration tests
├── test_website.ny # Website tests
├── run_all_tests.py # Master test runner
├── integration.test.ts # TypeScript integration tests
├── test-all.js # Full JS test runner
├── test-net.js # Network tests
├── test-nypm.js # Package manager tests
└── test-server.js # Server tests
Writing Tests with stdlib/test.ny
import std/test
// Basic assertions
test.test("addition works", fn() {
test.eq(1 + 1, 2, "1 + 1 should be 2")
test.eq(2 * 3, 6, "multiplication")
test.neq(1, 2, "1 should not equal 2")
})
// Float comparison with tolerance
test.test("float math", fn() {
test.approx(math.PI, 3.14159, 0.001, "PI approximation")
test.approx(math.sqrt(2), 1.4142, 0.001, "sqrt(2)")
})
// Exception testing
test.test("division by zero throws", fn() {
test.raises(fn() { let x = 1 / 0 }, "should throw on division by zero")
})
// Collection containment
test.test("array contains", fn() {
let arr = [1, 2, 3, 4, 5]
test.contains_(arr, 3, "array should contain 3")
test.is_true(len(arr) == 5, "length should be 5")
test.is_false(len(arr) == 0, "should not be empty")
test.is_null(null, "null check")
})
// Test suites
test.suite("String Operations", [
fn() { test.test("upper", fn() { test.eq(string.upper("hello"), "HELLO") }) },
fn() { test.test("lower", fn() { test.eq(string.lower("HELLO"), "hello") }) },
fn() { test.test("strip", fn() { test.eq(string.strip(" hi "), "hi") }) },
])
// Print summary
test.summary()
// Output:
// ═══════════════════════════════════════
// Test Results
// Passed: 8 / 8
// Failed: 0
// Skipped: 0
// Status: ALL PASSED ✓
// ═══════════════════════════════════════
📦 Collections Deep Dive (stdlib/collections.ny)
Production-grade data structures — all implemented in pure Nyx.
LinkedList (Doubly-Linked)
import std/collections
let list = collections.LinkedList()
// Add elements
list.append(1) // [1]
list.append(2) // [1, 2]
list.prepend(0) // [0, 1, 2]
list.insert(1, 99) // [0, 99, 1, 2]
// Access elements
list.get(0) // 0
list.get(2) // 1
list.contains(99) // true
list.index_of(99) // 1
// Modify
list.set(1, 100) // [0, 100, 1, 2]
list.remove(0) // [100, 1, 2]
// Functional operations
let doubled = list.map(fn(x) { return x * 2 }) // [200, 2, 4]
let evens = list.filter(fn(x) { return x % 2 == 0 }) // [100, 2]
let total = list.reduce(fn(acc, x) { return acc + x }, 0) // 103
list.reverse() // [2, 1, 100]
Binary Search Tree (AVL-Balanced)
let bst = collections.BinarySearchTree()
bst.insert(5)
bst.insert(3)
bst.insert(7)
bst.insert(1)
bst.insert(4)
bst.contains(3) // true
bst.min() // 1
bst.max() // 7
bst.inorder() // [1, 3, 4, 5, 7]
bst.preorder() // [5, 3, 1, 4, 7]
bst.postorder() // [1, 4, 3, 7, 5]
Red-Black Tree
let rbt = collections.RedBlackTree()
// Self-balancing BST with O(log n) insert/delete/search
// Guarantees: root is always BLACK, red nodes have black children,
// all paths from any node to leaves have equal black nodes
rbt.insert(10)
rbt.insert(20)
rbt.insert(30) // Triggers rotation to maintain balance
// Internal: left/right rotations, color flips, insert_fixup, delete_fixup
⚡ Async Deep Dive (stdlib/async.ny)
Complete asynchronous programming API.
Event Loop & Tasks
import std/async
let loop = async.get_event_loop()
// Create and run tasks
let task1 = async.create_task(async fn() {
await async.async_sleep(1.0)
return "Task 1 done"
})
let task2 = async.create_task(async fn() {
await async.async_sleep(0.5)
return "Task 2 done"
})
// Wait for all
let results = await async.gather(task1, task2)
// results = ["Task 1 done", "Task 2 done"]
// Race — first to complete wins
let winner = await async.race(task1, task2)
// winner = "Task 2 done" (faster)
// Any — first successful result
let first_ok = await async.any(task1, task2)
// All settled — wait for all, collect results + errors
let settled = await async.all_settled(task1, task2)
// settled = [{status: "fulfilled", value: ...}, ...]
Futures & Promises
// Future — resolve later
let future = async.Future()
spawn fn() {
await async.async_sleep(2.0)
future.resolve(42)
}
let value = await future
// value = 42
// Promise chaining
let result = async.Promise()
.then(fn(x) { return x * 2 })
.then(fn(x) { return x + 1 })
.catch(fn(err) { print("Error: " + str(err)) })
.finally(fn() { print("Done") })
result.resolve(10)
// Chains: 10 → 20 → 21, prints "Done"
Synchronization Primitives
// Mutex lock
let lock = async.Lock()
await lock.acquire()
// ... critical section ...
lock.release()
// Semaphore (bounded concurrency)
let sem = async.Semaphore(5) // Max 5 concurrent
for i in range(20) {
spawn async fn() {
await sem.acquire()
// ... only 5 run at a time ...
sem.release()
}
}
// Condition variable
let cond = async.Condition()
// Producer
spawn async fn() {
// ... produce data ...
cond.notify() // Wake one waiter
cond.notify_all() // Wake all waiters
}
// Consumer
await cond.wait() // Sleep until notified
// Async queue (bounded)
let queue = async.Queue(100) // Max 100 items
await queue.put(item) // Block if full
let item = await queue.get() // Block if empty
// Worker pool
let pool = async.WorkerPool(8) // 8 worker threads
pool.start()
pool.submit(fn() { /* work */ })
pool.shutdown()
// Retry with backoff
let result = await async.retry_async(
async fn() { return await http.get(url) },
max_attempts: 3,
delay: 1.0
)
// Timeout wrapper
let result = await async.wait_for(
async fn() { return await slow_operation() },
timeout: 5.0 // Fail after 5 seconds
)
🌐 Web Framework Deep Dive (stdlib/web.ny)
Express-style web framework built into the standard library.
Complete Example
import std/web
let app = web.App()
// Middleware
app.use(web.cors_middleware({
origin: "*",
methods: ["GET", "POST", "PUT", "DELETE"],
headers: ["Content-Type", "Authorization"]
}))
app.use(web.log_middleware)
app.use(web.body_parser_middleware)
app.use(web.rate_limiter_middleware(100, 60)) // 100 requests per 60 seconds
// Template engine
let templates = web.TemplateEngine()
templates.add_filter("reverse", fn(s) { return string.reverse(s) })
templates.add_global("site_name", "My Nyx App")
app.engine("nyx", templates)
// Routes
app.get("/", fn(req, res) {
res.html(templates.render("index.nyx", {
title: "Home",
items: ["Alpha", "Beta", "Gamma"]
}))
})
app.get("/api/users", fn(req, res) {
let page = int(req.query_param("page") ?? "1")
let limit = int(req.query_param("limit") ?? "10")
let users = db.table("users")
.select({})
.order_by("name")
.limit(limit)
res.json({users: users, page: page})
})
app.post("/api/users", fn(req, res) {
let body = req.json()
let user = db.table("users").insert({
name: body.name,
email: body.email,
created_at: time.datetime()
})
res.status(201).json(user)
})
// Route groups
app.group("/api/admin", fn(router) {
router.use(auth_middleware) // Auth only for /api/admin/*
router.get("/stats", fn(req, res) { /* ... */ })
router.delete("/users/:id", fn(req, res) { /* ... */ })
})
// Static files
app.use(web.static_middleware("public/"))
// Error handling
app.use(web.error_handler_middleware)
print("Server listening on :3000")
app.listen(3000)
Template Syntax
<!-- templates/index.nyx -->
<html>
<head><title>{{ title }}</title></head>
<body>
<h1>{{ site_name | upper }}</h1>
{% if items %}
<ul>
{% for item in items %}
<li>{{ item | lower }}</li>
{% endfor %}
</ul>
{% endif %}
{% include "footer.nyx" %}
<p>Data: {{ data | json | safe }}</p>
</body>
</html>
Built-in template filters: upper, lower, length, first, last, join, json, safe
🗃️ Database Deep Dive (stdlib/database.ny)
Built-in database with SQL-like queries — no external dependencies.
In-Memory Database
import std/database
let db = database.Database()
// Create table with schema
db.create_table("users", {
id: "int",
name: "string",
email: "string",
age: "int",
active: "bool"
})
// Insert records
let users = db.table("users")
users.insert({id: 1, name: "Alice", email: "alice@example.com", age: 30, active: true})
users.insert({id: 2, name: "Bob", email: "bob@example.com", age: 25, active: true})
users.insert({id: 3, name: "Charlie", email: "charlie@example.com", age: 35, active: false})
// Query: SELECT * FROM users WHERE active = true
let active_users = users.select(fn(row) { return row.active == true })
// Aggregation
users.count() // 3
users.sum("age") // 90
users.avg("age") // 30.0
users.min("age") // 25
users.max("age") // 35
// Sorting & limiting
let sorted = users.order_by("age", desc: true) // Charlie, Alice, Bob
let top2 = users.order_by("age").limit(2) // Bob, Alice
// Update
users.update({active: true}, fn(row) { return row.name == "Charlie" })
// Delete
users.delete(fn(row) { return row.age < 26 })
// Index for fast lookup
users.create_index("email")
// Join
db.create_table("posts", {id: "int", title: "string", author_id: "int"})
let posts = db.table("posts")
posts.insert({id: 1, title: "Hello World", author_id: 1})
let joined = users.join(posts, "id", "author_id")
// [{name: "Alice", email: "...", title: "Hello World", ...}]
// Group by
let by_active = users.group_by("active", fn(group) {
return {count: len(group), avg_age: sum(group |> map(|r| r.age)) / len(group)}
})
Persistent KV Store
// JSON-backed file storage
let store = database.FileKVStore("data/app.json")
store.put("user:1", {name: "Alice", role: "admin"})
store.put("user:2", {name: "Bob", role: "user"})
let user = store.get("user:1") // {name: "Alice", role: "admin"}
store.has("user:1") // true
store.delete("user:2")
store.save() // Persist to disk
Document Store (MongoDB-like)
let docs = database.DocumentStore()
docs.insert({name: "Alice", tags: ["admin", "dev"], score: 95})
docs.insert({name: "Bob", tags: ["dev"], score: 82})
docs.insert({name: "Charlie", tags: ["admin"], score: 88})
// Query
let admins = docs.find(fn(doc) { return "admin" in doc.tags })
let top = docs.find_one(fn(doc) { return doc.score > 90 })
// Index
docs.create_index("name")
let alice = docs.find_by_index("name", "Alice")
// Update & delete
docs.update(
fn(doc) { return doc.name == "Bob" },
{score: 90}
)
docs.delete(fn(doc) { return doc.score < 85 })
docs.count() // 2
🔤 String Operations Deep Dive (stdlib/string.ny)
Comprehensive string manipulation — all functions are pure and return new strings.
| Category |
Functions |
Example |
| Case |
upper(s), lower(s), capitalize(s), title(s), swapcase(s) |
upper("hello") → "HELLO" |
| Whitespace |
strip(s), lstrip(s), rstrip(s), strip_chars(s, chars) |
strip(" hi ") → "hi" |
| Padding |
ljust(s, n, ch), rjust(s, n, ch), center(s, n, ch), zfill(s, n) |
zfill("42", 5) → "00042" |
| Split/Join |
split(s, sep), split_n(s, sep, n), split_whitespace(s), splitlines(s) |
split("a,b,c", ",") → ["a","b","c"] |
| Search |
contains(s, sub), starts_with(s, pre), ends_with(s, suf), index_of(s, sub) |
contains("hello", "ell") → true |
| Replace |
replace(s, old, new), replace_n(s, old, new, n) |
replace("aaa", "a", "b") → "bbb" |
| Extract |
slice(s, start, end), char_at(s, i), repeat(s, n) |
repeat("ab", 3) → "ababab" |
| Check |
is_digit(s), is_alpha(s), is_alnum(s), is_space(s), is_upper(s), is_lower(s) |
is_digit("123") → true |
| Transform |
reverse(s), chars(s), bytes(s), encode(s, enc), decode(b, enc) |
reverse("hello") → "olleh" |
⏰ Time & Date Deep Dive (stdlib/time.ny)
Complete time/date API — no external dependencies.
import std/time
// Current time
let now = time.now() // Unix timestamp (seconds, float)
let ms = time.now_millis() // Milliseconds since epoch
let us = time.now_micros() // Microseconds since epoch
let ns = time.now_nanos() // Nanoseconds since epoch
// Sleep
time.sleep(1.5) // Sleep 1.5 seconds
time.sleep_ms(100) // Sleep 100 milliseconds
time.sleep_us(500) // Sleep 500 microseconds
// Date/time components
let parts = time.to_components(now)
// parts = {year: 2025, month: 6, day: 25, hour: 14, minute: 30,
// second: 45, millisecond: 123, weekday: 3, yearday: 176}
// Formatting (strftime-style)
let formatted = time.format_time(now, "%Y-%m-%d %H:%M:%S")
// → "2025-06-25 14:30:45"
let date = time.date() // "2025-06-25"
let t = time.time_str() // "14:30:45"
let dt = time.datetime() // "2025-06-25 14:30:45"
let dt_ms = time.datetime_ms() // "2025-06-25 14:30:45.123"
// Parsing
let ts = time.parse_iso("2025-01-15T10:30:00Z")
let ts2 = time.to_timestamp(2025, 1, 15, 10, 30, 0)
// Arithmetic
let tomorrow = time.add_time(now, 1, "days")
let last_week = time.sub_time(now, 7, "days")
let diff = time.time_diff(tomorrow, now, "hours") // 24.0
// Calendar
time.is_leap_year(2024) // true
time.days_in_month(2025, 2) // 28
time.days_in_year(2024) // 366
time.week_number(now) // 26
| Directive |
Meaning |
Example |
%Y |
4-digit year |
2025 |
%m |
Month (01-12) |
06 |
%d |
Day (01-31) |
25 |
%H |
Hour 24h (00-23) |
14 |
%I |
Hour 12h (01-12) |
02 |
%M |
Minute (00-59) |
30 |
%S |
Second (00-59) |
45 |
%f |
Microsecond |
000123 |
%j |
Day of year (001-366) |
176 |
%w |
Weekday (0=Sun) |
3 |
%a |
Abbreviated weekday |
Wed |
%A |
Full weekday |
Wednesday |
%b |
Abbreviated month |
Jun |
%B |
Full month |
June |
%p |
AM/PM |
PM |
%c |
Full datetime |
Wed Jun 25 14:30:45 2025 |
%x |
Date only |
06/25/2025 |
%X |
Time only |
14:30:45 |
%% |
Literal % |
% |
🔍 Regex Deep Dive (stdlib/regex.ny)
Regular expression engine — implemented entirely in Nyx.
import std/regex
// Compile a pattern
let pattern = regex.compile("[a-z]+@[a-z]+\\.[a-z]{2,}")
// Match at start of string
let m = regex.match(pattern, "user@example.com more text")
// m = Match{start: 0, end: 16, text: "user@example.com"}
// Find all matches in string
let all = regex.find_all(pattern, "alice@test.com and bob@mail.org")
// all = [Match{..., text: "alice@test.com"}, Match{..., text: "bob@mail.org"}]
// Replace all matches
let cleaned = regex.replace_all(
regex.compile("[0-9]+"),
"abc123def456",
"NUM"
)
// cleaned = "abcNUMdefNUM"
// Supported syntax:
// . Any character
// * Zero or more
// + One or more
// ? Zero or one
// ^ Start of string
// $ End of string
// [abc] Character class
// [a-z] Character range
// (...) Group
// a|b Alternation
// {n} Exactly n
// {n,m} Between n and m
🔧 Process Management Deep Dive (stdlib/process.ny)
Full process control — spawn, monitor, and manage OS processes.
import std/process
// Run a command and capture output
let result = process.run(["ls", "-la"], capture_output: true)
print(result.stdout)
print("Exit code: " + str(result.returncode))
// Spawn subprocess with pipes
let proc = process.Popen(["python", "-c", "print('hello from python')"])
let output = proc.communicate()
print(output.stdout) // "hello from python\n"
// Process monitoring
let pid = process.get_pid() // Current process ID
let ppid = process.getppid() // Parent process ID
// Iterate all running processes
for p in process.process_iter() {
if p.is_running() {
print(str(p.pid) + ": " + p.name + " (RSS: " + str(p.memory_info().rss) + ")")
}
}
// Process control
let child = process.Popen(["long_task"])
child.wait() // Block until done
child.terminate() // Send SIGTERM
child.kill() // Send SIGKILL
child.send_signal(9) // Send specific signal
// Environment variables
let home = process.getenv("HOME", "/tmp")
process.putenv("MY_VAR", "value")
process.unsetenv("MY_VAR")
// Process info
let p = process.Process(pid, "myapp", "running")
p.cpu_times() // {user: 1.5, system: 0.3}
p.memory_info() // {rss: 12345678, vms: ..., percent: 0.5, ...}
p.num_threads() // 4
p.connections() // [{fd: 3, family: AF_INET, type: SOCK_STREAM, ...}]
p.open_files() // [{path: "/tmp/data.json", fd: 5}]
🔧 All 117+ Engines — Category Index
Every engine organized by domain.
AI & Machine Learning (21 engines)
| Engine |
Import |
Key Features |
nyai |
use nyai |
Agent system, hybrid AI, behavioral trees |
nyml |
use nyml |
Classical ML (regression, classification, clustering) |
nynet |
use nynet |
Neural networks, layers, optimizers |
nynet_ml |
use nynet_ml |
Network-based ML |
nytensor |
use nytensor |
Tensor operations, GPU acceleration |
nygrad |
use nygrad |
Automatic differentiation |
nyloss |
use nyloss |
Loss functions (MSE, CrossEntropy, etc.) |
nyopt |
use nyopt |
Optimizers (SGD, Adam, RMSProp) |
nyrl |
use nyrl |
Reinforcement learning |
nynlp |
use nynlp |
Natural language processing |
nymind |
use nymind |
Cognitive AI |
nymodel |
use nymodel |
Model management & serialization |
nygraph_ml |
use nygraph_ml |
Graph neural networks |
nylinear |
use nylinear |
Linear algebra |
nyprecision |
use nyprecision |
Arbitrary precision arithmetic |
nyswarm |
use nyswarm |
Swarm intelligence |
nylogic |
use nylogic |
Logic programming |
nyplan |
use nyplan |
Planning & scheduling |
nyworld |
use nyworld |
World simulation |
nysim |
use nysim |
Simulation engine |
nyagent |
use nyagent |
Multi-agent systems |
Systems & Infrastructure (18 engines)
| Engine |
Import |
Key Features |
nycore |
use nycore |
Core runtime engine |
nysys |
use nysys |
System utilities |
nysystem |
use nysystem |
System programming tools |
nykernel |
use nykernel |
Kernel-level programming |
nydevice |
use nydevice |
Device driver framework |
nyinfra |
use nyinfra |
Infrastructure management |
nykube |
use nykube |
Kubernetes integration |
nycontainer |
use nycontainer |
Container management |
nycluster |
use nycluster |
Cluster orchestration |
nyprovision |
use nyprovision |
Infrastructure provisioning |
nycloud |
use nycloud |
Cloud provider abstraction |
nyserverless |
use nyserverless |
Serverless functions |
nyscale |
use nyscale |
Auto-scaling |
nydeploy |
use nydeploy |
Deployment automation |
nyci |
use nyci |
CI/CD pipelines |
nybuild |
use nybuild |
Build system |
nypack |
use nypack |
Package management |
nyruntime |
use nyruntime |
Runtime management |
Networking & Web (12 engines)
| Engine |
Import |
Key Features |
nyhttp |
use nyhttp |
HTTP client/server |
nyweb |
use nyweb |
Web framework |
nyserver |
use nyserver |
TCP/UDP server |
nyserve |
use nyserve |
Static file serving |
nynet |
use nynet |
Low-level networking |
nynetwork |
use nynetwork |
Network utilities |
nyapi |
use nyapi |
REST API framework |
nystream |
use nystream |
Streaming data |
nyqueue |
use nyqueue |
Message queues |
nysync |
use nysync |
Data synchronization |
nyasync |
use nyasync |
Async I/O engine |
nyevent |
use nyevent |
Event-driven architecture |
Data & Storage (8 engines)
| Engine |
Import |
Key Features |
nydata |
use nydata |
Data processing pipeline |
nydatabase |
use nydatabase |
Database engine |
nydb |
use nydb |
Lightweight DB |
nystorage |
use nystorage |
Storage abstraction |
nyquery |
use nyquery |
Query engine |
nycache |
use nycache |
Caching layer |
nystate |
use nystate |
State management |
nygraph |
use nygraph |
Graph database |
Security & Compliance (8 engines)
| Engine |
Import |
Key Features |
nycrypto |
use nycrypto |
Cryptography engine |
nysec |
use nysec |
Security toolkit |
nysecure |
use nysecure |
Secure communications |
nyaudit |
use nyaudit |
Security auditing |
nycompliance |
use nycompliance |
Compliance checking |
nyids |
use nyids |
Intrusion detection |
nyexploit |
use nyexploit |
Security testing |
nyrecon |
use nyrecon |
Reconnaissance tools |
| Engine |
Import |
Key Features |
nyrender |
use nyrender |
2D/3D rendering |
nyviz |
use nyviz |
Data visualization |
nygui |
use nygui |
GUI framework |
nyui |
use nyui |
Reactive UI framework |
nyanim |
use nyanim |
Animation engine |
nyaudio |
use nyaudio |
Audio processing |
nymedia |
use nymedia |
Multimedia processing |
Scientific & Simulation (10 engines)
| Engine |
Import |
Key Features |
nysci |
use nysci |
Scientific computing |
nyphysics |
use nyphysics |
Physics simulation |
nybio |
use nybio |
Bioinformatics |
nychem |
use nychem |
Chemistry tools |
nyode |
use nyode |
ODE/PDE solvers |
nystats |
use nystats |
Statistical analysis |
nycompute |
use nycompute |
High-performance computing |
nyhpc |
use nyhpc |
HPC tools |
nygpu |
use nygpu |
GPU computing (CUDA/OpenCL) |
nyaccel |
use nyaccel |
Hardware acceleration |
Finance & Trading (6 engines)
| Engine |
Import |
Key Features |
nyquant |
use nyquant |
Quantitative finance |
nyhft |
use nyhft |
High-frequency trading |
nytrade |
use nytrade |
Trading engine |
nymarket |
use nymarket |
Market data |
nybacktest |
use nybacktest |
Backtesting framework |
nyrisk |
use nyrisk |
Risk analysis |
DevOps & Monitoring (10 engines)
| Engine |
Import |
Key Features |
nymonitor |
use nymonitor |
System monitoring |
nymetrics |
use nymetrics |
Metrics collection |
nytrack |
use nytrack |
Distributed tracing |
nylog |
use nylog |
Log aggregation |
nyconfig |
use nyconfig |
Configuration management |
nyautomate |
use nyautomate |
Task automation |
nyshell |
use nyshell |
Shell scripting engine |
nyscript |
use nyscript |
Script execution |
nyreport |
use nyreport |
Report generation |
nydoc |
use nydoc |
Documentation generation |
Specialized (17 engines)
| Engine |
Import |
Key Features |
nygame |
use nygame |
Game engine |
nyrobot |
use nyrobot |
Robotics control |
nyvoice |
use nyvoice |
Voice/speech processing |
nylang |
use nylang |
Language processing tools |
nyls |
use nyls |
Language server |
nygen |
use nygen |
Code generation |
nyframe |
use nyframe |
Framework toolkit |
nyfeature |
use nyfeature |
Feature flags |
nyfuzz |
use nyfuzz |
Fuzz testing |
nymal |
use nymal |
Malware analysis (security research) |
nyreverse |
use nyreverse |
Reverse engineering |
nycontrol |
use nycontrol |
Control systems |
nyparallel |
use nyparallel |
Parallel computing |
nycalc |
use nycalc |
Calculator engine |
nyarray |
use nyarray |
Array operations |
nypm |
use nypm |
Package management engine |
nystudio |
use nystudio |
IDE/studio integration |
Q: Where can I get help?
Start Building With Nyx Today
# Install and write your first program in 30 seconds
git clone https://github.com/suryasekhar06jemsbond-lab/Nyx.git
cd Nyx
echo 'print("Hello, I am learning Nyx!")' > learn.ny
nyx learn.ny
Quick Links
GitHub Repository · Language Spec · Examples · Report Bug · VS Code Extension
MIT License · Built with passion by the Nyx Team
Nyx — One language to rule them all.
109 stdlib modules · 117+ engines · 150+ token types · 60+ AST nodes · 80+ keywords · 3 execution modes · 0 dependencies needed
| |