Nexus Programming Language Specification
Overview
Nexus is a statically-typed, expression-based programming language designed for multi-language orchestration. It provides a clean syntax with strong typing and familiar control flow constructs.
Goals of the Language
- Enable seamless integration of multiple programming languages
- Provide type safety across language boundaries
- Support multiple compilation targets (bytecode, WASM, native)
- Maintain pragmatic simplicity without academic complexity
- Enable secure, verifiable program execution
Design Philosophy
Nexus is built on three core principles:
- FFI-first: Foreign Function Interface is a first-class feature, not an afterthought
- Orchestration-first: Connect languages, don't replace them
- Pragmatic simplicity: Clean syntax, no unnecessary complexity
Strong Types
Nexus is statically typed with explicit type annotations. Type inference is available for local variables when the type can be unambiguously determined.
- int - 64-bit signed integer
- string - UTF-8 encoded string
- bool - Boolean (true/false)
- [T] - Array of type T
- Result<T, E> - Result type for error handling
- void - No return value
Syntax
Variables
Variables are declared with `let` (immutable) or `var` (mutable):
let name: string = "Nexus";
var counter: int = 0;
counter = counter + 1; // OK: var is mutable
// name = "Other"; // Error: let is immutableFunctions
Functions are defined with the `func` keyword and require explicit return types:
func add(a: int, b: int): int {
return a + b;
}
func greet(name: string): void {
print("Hello " + name);
}Control Flow
Nexus supports if/else, while, for, and match statements:
if x > 40 {
print("Large");
} else if x > 20 {
print("Medium");
} else {
print("Small");
}
while i < 5 {
print(i);
i = i + 1;
}
for var i = 0; i < 5; i = i + 1 {
total = total + i;
}
match status {
"pending" => print("Waiting..."),
"running" => print("In progress"),
_ => print("Unknown")
}Expressions
Nexus is expression-based. Most constructs return values:
let result = if x > 0 { x * 2 } else { 0 };
let value = match status {
"ok" => 1,
"error" => 0,
_ => -1
};Assignment
Assignment uses the `=` operator. Only `var` variables can be reassigned:
var x: int = 10;
x = 20; // OK: var is mutable
let y: int = 10;
// y = 20; // Error: let is immutableComments
Single-line comments use `//`, multi-line comments use `/* */`:
// Single-line comment
/*
Multi-line comment
Spans multiple lines
*/
func main(): void {
print("Hello!"); // Inline comment
}Modules & Imports
Nexus supports multi-language imports through FFI declarations:
// FFI declarations
ffi js fetch(url: string): string;
ffi py analyze(text: string): int;
ffi rs encrypt(data: string): string;
// Usage
func main(): void {
let data = fetch("https://api.example.com");
let result = analyze(data);
let encrypted = encrypt(result);
}Grammar Summary
Simplified EBNF grammar (key constructs):
// Simplified EBNF grammar (key constructs)
Program ::= Statement*
Statement ::= FunctionDecl | VarDecl | Assignment | Expression
FunctionDecl::= "func" Identifier "(" Params? ")" ":" Type Block
VarDecl ::= ("let" | "var") Identifier ":" Type "=" Expression
Assignment ::= Identifier "=" Expression
Expression ::= IfExpr | MatchExpr | CallExpr | Literal | Identifier
IfExpr ::= "if" Expression Block ("else" Block)?
MatchExpr ::= "match" Expression "{" Case* "}"
CallExpr ::= Identifier "(" Args? ")"
Block ::= "{" Statement* "}"
Type ::= "int" | "string" | "bool" | "[" Type "]" | "Result<" Type "," Type ">" | "void"Example Program
ffi js fetch(url: string): string;
ffi py analyze(text: string): int;
ffi rs encrypt(data: string): string;
func process_data(url: string): Result<string, string> {
let raw = fetch(url);
if raw == "" {
return Err("Failed to fetch data");
}
let score = analyze(raw);
let encrypted = encrypt(raw);
return Ok(encrypted);
}
func main(): void {
let result = process_data("https://api.example.com/data");
match result {
Ok(data) => print("Success: " + data),
Err(msg) => print("Error: " + msg)
}
}