Neutron Examples

Explore practical examples of Neutron code to get familiar with the language syntax and features.

Hello, World!

The classic first program in Neutron

hello.nt
say("Hello, World!");

Variables and Functions

Working with variables and defining functions

greet.nt
fun greet(name) {
    return "Hello, " + name + "!";
}

var message = greet("Neutron");
say(message);

// Output: Hello, Neutron!

Classes and Objects

Object-oriented programming with classes

person.nt
class Person {
    var name;
    var age;

    fun initialize(name, age) {
        this.name = name;
        this.age = age;
    }

    fun greet() {
        say("Hello, my name is " + this.name + " and I am " + this.age + " years old.");
    }
}

var person = Person();
person.initialize("Alice", 30);
person.greet();

// Output: Hello, my name is Alice and I am 30 years old.

Control Flow

Conditional statements and loops

control.nt
var age = 25;

if (age >= 18) {
    say("You are an adult");
} else {
    say("You are a minor");
}

// Loop example
for (var i = 1; i <= 5; i = i + 1) {
    say("Count: " + i);
}

// While loop
var counter = 0;
while (counter < 3) {
    say("Counter: " + counter);
    counter = counter + 1;
}

Math Module

Using the math module for calculations

math_demo.nt
use math;

var sum = math.add(5, 3);           // 8
var diff = math.subtract(10, 4);    // 6
var product = math.multiply(6, 7);  // 42
var quotient = math.divide(15, 3);  // 5
var power = math.pow(2, 3);         // 8
var root = math.sqrt(16);           // 4
var absolute = math.abs(-5);        // 5

say("Sum: " + sum);
say("Difference: " + diff);
say("Product: " + product);
say("Quotient: " + quotient);
say("Power: " + power);
say("Square root: " + root);
say("Absolute: " + absolute);

Working with Modules

Using the standard library modules

modules.nt
use sys;
use json;

// File operations
sys.write("data.txt", "Hello, Neutron!");
var content = sys.read("data.txt");
say("File content: " + content);

// JSON operations
var data = {
  "name": "Alice",
  "age": 30,
  "active": true
};

var jsonStr = json.stringify(data);
say("JSON: " + jsonStr);

var parsed = json.parse(jsonStr);
say("Parsed name: " + parsed["name"]);

// Cleanup
sys.rm("data.txt");

String Operations

Working with strings and text processing

strings.nt
var greeting = "Hello";
var name = "World";
var message = greeting + ", " + name + "!";

say(message);
say("Message length: " + string_length(message));

// Character operations
var char = string_get_char_at(message, 0);  // "H"
var ascii = char_to_int(char);              // 72
var backToChar = int_to_char(ascii);        // "H"

say("First character: " + char);
say("ASCII value: " + ascii);
say("Back to char: " + backToChar);

// Binary conversion
var number = 10;
var binary = int_to_bin(number);    // "1010"
var decimal = bin_to_int(binary);   // 10

say("10 in binary: " + binary);
say("Binary back to decimal: " + decimal);

Fibonacci Sequence

Recursive function example

fibonacci.nt
fun fibonacci(n) {
    if (n <= 1) {
        return n;
    } else {
        return fibonacci(n - 1) + fibonacci(n - 2);
    }
}

// Print first 10 Fibonacci numbers
for (var i = 0; i < 10; i = i + 1) {
    say("F(" + i + ") = " + fibonacci(i));
}

// Output:
// F(0) = 0
// F(1) = 1
// F(2) = 1
// F(3) = 2
// F(4) = 3
// F(5) = 5
// F(6) = 8
// F(7) = 13
// F(8) = 21
// F(9) = 34

Advanced Examples

More complex applications demonstrating Neutron's capabilities

Object-Oriented Calculator

Advanced class-based programming with methods and properties

oop_calculator.nt
use math;

class Calculator {
    var history;
    
    fun initialize() {
        this.history = "Calculator initialized\n";
    }
    
    fun add(a, b) {
        var result = math.add(a, b);
        this.history = this.history + "Added " + a + " + " + b + " = " + result + "\n";
        return result;
    }
    
    fun power(base, exp) {
        var result = math.pow(base, exp);
        this.history = this.history + "Power " + base + "^" + exp + " = " + result + "\n";
        return result;
    }
    
    fun show_history() {
        say("=== Calculator History ===");
        say(this.history);
    }
}

var calc = Calculator();
calc.initialize();

var sum = calc.add(15, 25);        // 40
var power_result = calc.power(2, 10); // 1024

say("Sum: " + sum);
say("Power: " + power_result);
calc.show_history();

File Processing

Working with files, JSON, and system operations

file_processor.nt
use sys;
use json;
use time;

class FileProcessor {
    fun process_data(input_file, output_file) {
        if (!sys.exists(input_file)) {
            say("Error: Input file '" + input_file + "' not found");
            return false;
        }
        
        say("Processing file: " + input_file);
        
        // Read input
        var content = sys.read(input_file);
        say("Read " + string_length(content) + " characters");
        
        // Create processing report
        var report = {
            "input_file": input_file,
            "output_file": output_file,
            "processed_at": time.now(),
            "content_length": string_length(content),
            "status": "completed"
        };
        
        // Write report
        var report_json = json.stringify(report, true);
        sys.write(output_file, report_json);
        
        say("Processing complete. Report saved to: " + output_file);
        return true;
    }
}

// Create test input file
sys.write("input.txt", "Sample data for processing.");

// Process the file
var processor = FileProcessor();
var success = processor.process_data("input.txt", "report.json");

if (success) {
    say("File processing successful!");
    var report_content = sys.read("report.json");
    say("Generated report:");
    say(report_content);
}

// Cleanup
sys.rm("input.txt");
sys.rm("report.json");

Binary Compilation

Compile your Neutron scripts to standalone executables

Compiling to Binary

Turn your Neutron script into a standalone executable

terminal
# Compile your script
./neutron -b myapp.nt myapp

# Run the compiled binary
./myapp

# No interpreter needed!
# The binary includes all necessary code

Benefits of Binary Compilation

  • Standalone Distribution: No need to install the Neutron interpreter
  • Better Performance: Optimized compilation with C++ compiler
  • Code Protection: Source code is embedded and not easily readable
  • Easy Deployment: Single executable file for distribution

More Examples

Explore additional code samples and tutorials

Standard Library

Learn how to use built-in modules for math, HTTP, and JSON operations.

View Modules →

Advanced Patterns

Explore advanced programming patterns and best practices in Neutron.

Read Docs →

Performance Tips

Learn how to write efficient Neutron code and optimize performance.

View Performance →