Behl Language
Lua-inspired scripting language with C-like syntax, implemented in C++20.
What is Behl?
Behl combines the simplicity and flexibility of Lua’s semantics with the familiar syntax of C-style languages. It features:
- C-like Syntax - Familiar
;terminators,{}blocks, and C-style comments - Dynamic Typing - Lua-inspired type system with runtime flexibility
- First-Class Functions - Functions are values that can be passed around
- Tables - 0-indexed associative arrays (unlike Lua’s 1-indexed)
- Modern VM - Register-based bytecode with incremental garbage collection
- C++ API - Easy embedding with a Lua-like API:
push_integer(),call(), etc. - Fast Performance - Competitive with Lua in many benchmarks
Quick Example
// C-like syntax with dynamic typing
function fibonacci(n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
let result = fibonacci(10);
print(result); // 55
Getting Started
Requirements
- CMake 3.26 or later
- C++20 compiler (MSVC 2022+, GCC 11+, Clang 14+)
Building
# Configure
cmake -B build -DCMAKE_BUILD_TYPE=Release
# Build
cmake --build build --config Release
# Run tests
cd build
ctest -C Release
Your First Script
Create a file hello.behl:
print("Hello, behl!");
let name = "World";
print("Hello, " + name);
Run it:
./behl hello.behl
Key Features
Familiar Syntax
If you know C, JavaScript, or Java, you already know Behl’s syntax:
// Variables with let
let x = 10;
const PI = 3.14159;
// For loops
for (let i = 0; i < 10; i++) {
print(i);
}
// If/else
if (x > 5) {
print("Greater");
} else {
print("Smaller");
}
Lua Semantics
Under the hood, Behl follows Lua’s powerful model:
- Dynamic typing with
typeof()introspection - Tables as the primary data structure
- Closures and lexical scoping
- Metatables for customizing behavior
- Garbage collection for automatic memory management
Easy Embedding
#include <behl/behl.hpp>
int main() {
behl::State* S = behl::new_state();
behl::load_stdlib(S);
// Load and run a script
behl::load_string(S, "return 2 + 3");
behl::call(S, 0, 1);
int result = behl::to_integer(S, -1);
// result == 5
behl::close(S);
return 0;
}
Documentation
Guides
Tutorials and practical examples:
- Getting Started - Installation, first program, basic concepts
- Examples - Code samples and patterns
Language
Complete language reference:
- Syntax - Comments, keywords, and structure
- Values & Types - Numbers, strings, booleans, and more
- Variables - Declarations, scope, and constants
- Operators - Arithmetic, logical, and bitwise
- Control Flow - If/else, loops, and branches
- Functions - Definitions, closures, and calls
- Tables - Arrays, dictionaries, and metatables
- Error Handling - Errors and protected calls
- Modules - Importing and organizing code
API
Standard library and embedding:
Standard Library:
- Core Functions - print, typeof, pcall, and more
- Math Module - Mathematical operations
- String Module - String manipulation
- Table Module - Table utilities
- OS Module - Operating system interface
- FS Module - File system operations
- Debug Module - Debugging utilities
- GC Module - Garbage collector control
C++ Embedding:
- Embedding Overview - Integrate Behl in C++ applications
- Getting Started - State management and setup
- Stack Operations - Manipulate values
- Calling Functions - Execute scripts
- Tables - Work with tables from C++
- Userdata - Expose C++ objects
- Error Handling - Handle errors
- Creating Modules - Build C++ modules
- Debugging - Debug API for tools
- API Reference - Complete function list
Advanced Topics
Technical documentation and advanced usage patterns:
- Calling Scripts - Execute Behl from C++
- Callbacks - Store script functions in C++
- Optimizations - Performance and compiler details
- Differences from Lua - For Lua developers
License
Behl is distributed under the MIT License. See LICENSE file for details.