Usage and Examples
A whirlwind tour of Bnlang — every snippet here runs as-is with bnl <file>.bnl.
Hello, World
print("Hello, World!");
Run it:
bnl hello.bnl
Variables and basic types
Bnlang has one declaration keyword: var. Values are dynamically typed but predictable.
var name = "Alice";
var age = 25;
var is_admin = true;
var nothing = null;
var items = [1, 2, 3];
var user = { id: 1, name: "Alice" };
print(name, age, is_admin);
print("items:", items.length);
print("user id:", user.id);
Control flow
var score = 85;
if (score >= 80) {
print("Grade: A+");
} else if (score >= 70) {
print("Grade: A");
} else {
print("Grade: below A");
}
Loops
// for ... of — iterate over a list
for (var n of [1, 2, 3, 4, 5]) {
print(n);
}
// classic while
var i = 0;
while (i < 3) {
print("tick", i);
i = i + 1;
}
There is no for (i = 0; i < n; i++) C-style loop and no ++/--. Use for ... of over a list, or while for counter-driven loops. break and continue work in both.
Functions
function greet(name) {
return "Hello, " + name + "!";
}
print(greet("Alice"));
Functions are fixed-arity — no defaults, no overloads, no varargs. Split into separately-named functions if you need different shapes.
Importing the standard library
import "io" as io;
import "path" as path;
var text = io.read_file("notes.txt");
print("read", text.byte_length, "bytes from", path.basename("notes.txt"));
Async (callback-style)
There are no async/await keywords. Async I/O takes a final callback that fires when the work is done:
import "io" as io;
io.read_file_async("notes.txt", function (err, data) {
if (err != null) {
print("read failed:", err);
return;
}
print("got", data.byte_length, "bytes");
});
print("scheduled");
For composing many async steps, use the async library:
import "async" as async;
async.parallel([
function (cb) { io.read_file_async("a.txt", cb); },
function (cb) { io.read_file_async("b.txt", cb); }
], function (err, results) {
print("got", results.length, "files");
});
Error handling
function divide(a, b) {
if (b == 0) { throw "divide by zero"; }
return a / b;
}
try {
print(divide(10, 0));
} catch (e) {
print("caught:", e);
} finally {
print("done");
}
A tiny web server
import "web" as web;
var app = web.app();
app.get("/", function (req) { return "Hello from Bnlang!"; });
app.get("/echo/:name", function (req) {
return "Hi, " + req.params.name;
});
app.listen({ port: 3000 });
That's the language end-to-end. Browse the rest of the docs for module references and the keyword pages for syntax details.