Skip to content

mohammad-ayan-008/nox_lang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Nox Logo

🌌 Nox Language

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.


📜 Table of Contents


📌 Syntax Overview

Variables

var name = "value";
var number = 123;

Functions

fun functionName(arg1, arg2) {
  // statements
  return value;
}

Printing

print(expression);

Operators

Arithmetic: +, -, *, /, %
Comparison: ==, !=, <, >, <=, >=
Builtin: floor(x) returns the largest integer ≤ x


🔁 Control Flow

If Condition

if (condition) {
  // true block
} else {
  // false block
}

Loops

for (var i = 0; i < 5; i = i + 1) {
  print(i);
}

🧠 Recursion Examples

Sum of Digits

fun sumDigits(a) {
  if (floor(a) == 0.0) {
    return 0;
  }
  return (a % 10) + sumDigits(a / 10);
}
print(floor(sumDigits(1234))); // Output: 10

Reverse Digits

fun 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: 4321

Binary Conversion

fun toBinary(n) {
  if (floor(n) == 0.0) {
    return;
  }
  toBinary(n / 2);
  print(floor(n % 2));
}
toBinary(10); // Output: 1010

📚 Scoping & Closures

var a = "global";
{
  fun showA() {
    print(a);
  }

  showA();         // prints "global"
  var a = "block";
  showA();         // prints "nil" due to shadowing
}

🧪 Sample Programs

Factorial Using For Loop

fun fact(n) {
  var result = 1;
  for (var i = 2; i <= n; i = i + 1) {
    result = result * i;
  }
  return result;
}
print(fact(5)); // Output: 120

Loop Inside Recursion

fun 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

Fibonacci Series

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

🚀 Running Nox Code

To run a .nox file:

./target/debug/lox_lang path/to/file.nox

Example:

./target/debug/lox_lang test.nox

📖 License

This 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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages