Nox is a minimal scripting language inspired by Lox with support for variables, functions, recursion, loops, arithmetic expressions, and closures. It is designed for algorithmic expression and educational compiler development.
- Syntax Overview
- Variables
- Functions
- Control Flow
- Recursion Examples
- Scoping & Closures
- Sample Programs
- Running Nox Code
- License
var name = "value";
var number = 123;fun functionName(arg1, arg2) {
// statements
return value;
}print(expression);Arithmetic: +, -, *, /, %
Comparison: ==, !=, <, >, <=, >=
Builtin: floor(x) returns the largest integer ≤ x
if (condition) {
// true block
} else {
// false block
}for (var i = 0; i < 5; i = i + 1) {
print(i);
}fun sumDigits(a) {
if (floor(a) == 0.0) {
return 0;
}
return (a % 10) + sumDigits(a / 10);
}
print(floor(sumDigits(1234))); // Output: 10fun reverse(a, res) {
if (floor(a) == 0.0) {
return res;
}
return reverse(floor(a / 10), floor(res * 10 + (a % 10)));
}
print(floor(reverse(1234, 0))); // Output: 4321fun toBinary(n) {
if (floor(n) == 0.0) {
return;
}
toBinary(n / 2);
print(floor(n % 2));
}
toBinary(10); // Output: 1010var a = "global";
{
fun showA() {
print(a);
}
showA(); // prints "global"
var a = "block";
showA(); // prints "nil" due to shadowing
}fun fact(n) {
var result = 1;
for (var i = 2; i <= n; i = i + 1) {
result = result * i;
}
return result;
}
print(fact(5)); // Output: 120fun printNested(n) {
if (n == 0) {
return;
}
for (var i = 0; i < n; i = i + 1) {
print("n = " + n + ", i = " + i);
}
printNested(n - 1);
}
printNested(3);Expected Output:
n = 3, i = 0
n = 3, i = 1
n = 3, i = 2
n = 2, i = 0
n = 2, i = 1
n = 1, i = 0
fun fib(n) {
if (n <= 1) {
return n;
}
return fib(n - 1) + fib(n - 2);
}
for (var i = 0; i < 6; i = i + 1) {
print(fib(i));
}Output:
0
1
1
2
3
5
To run a .nox file:
./target/debug/lox_lang path/to/file.noxExample:
./target/debug/lox_lang test.noxThis language is built for educational and experimental use.
Inspired by Crafting Interpreters by Robert Nystrom.
Made with ❤️ in Rust — for recursion, reasoning, and runtime magic.
