Skip to content

Latest commit

 

History

23 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Behl

Lua-inspired scripting language with C-like syntax, implemented in C++20.

License: MIT

Features

  • C-like Syntax - Optional ; terminators, {} blocks, and C-style comments
  • Dynamic Typing - Lua-inspired type system with runtime flexibility
  • First-Class Functions - Functions as values with closures and lexical scoping
  • Powerful Tables - 0-indexed associative arrays (unlike Lua's 1-indexed)
  • Modern VM - Register-based bytecode with incremental garbage collection
  • JIT Compilation - Hand-written baseline JIT for x86-64, x86 and AArch64 with automatic interpreter fallback
  • Metatables - Customize table behavior with metamethods
  • C++ API - Easy embedding with a Lua-like API
  • Optimizations - Constant folding, loop optimization, tail call optimization, dead store elimination

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

Installation

Prerequisites

  • CMake 3.26 or later
  • C++20 compiler (MSVC 2022+, GCC 11+, Clang 14+)

Building from Source

# Clone the repository
git clone https://github.com/behl-lang/behl.git
cd behl

# Configure with CMake
cmake -B build -DCMAKE_BUILD_TYPE=Release

# Build (cross-platform)
cmake --build build --config Release

# Run tests (optional)
cd build
ctest -C Release --output-on-failure

The executable will be located in:

  • Multi-config generators (MSVC, Xcode): build/Release/behl[.exe]
  • Single-config generators (Make, Ninja): build/behl

Getting Started

Create a file hello.behl:

print("Hello, World!")

let name = "Behl"
print("Welcome to " + name + "!")

Run it:

./behl hello.behl

Language Highlights

Variables and Types

let x = 42                     // integer
let pi = 3.14159               // number (float)
let name = "Behl"              // string
let active = true              // boolean
let data = nil                 // nil

const MAX_SIZE = 1000          // constant (semicolons optional)

Control Flow

// If/else with elseif
if (x > 10) {
    print("Greater")
} elseif (x == 10) {
    print("Equal")
} else {
    print("Less")
}

// C-style for loops
for (let i = 0; i < 10; i++) {
    print(i)
}

// While loops
while (condition) {
    // ...
}

Functions

function greet(name) {
    return "Hello, " + name + "!"
}

// Functions are first-class values
let fn = greet
print(fn("World"))

Tables (0-indexed)

// Array-like
let arr = {10, 20, 30}
print(arr[0])  // 10 (first element)

// Dictionary-like
let person = {
    ["name"] = "Alice",
    ["age"] = 30
}
print(person["name"])

// Iteration
for (k, v in pairs(person)) {
    print(k + ": " + tostring(v))
}

Embedding in C++

#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;
}

Link against the behl library:

target_link_libraries(your_target PRIVATE behl)

Documentation

Full Documentation

The complete documentation is available online and includes:

Language & Core:

C++ Integration:

Advanced:

Project Structure

behl/
├── src/                  # Source code
│   ├── frontend/         # Lexer, parser, semantic analysis
│   ├── backend/          # Compiler, bytecode generation
│   ├── vm/               # Virtual machine execution
│   ├── optimization/     # AST optimization passes
│   ├── api/              # Public C++ API
│   └── gc/               # Garbage collector
├── include/              # Public headers
├── tests/                # Unit tests (GoogleTest)
└── docs/                 # Documentation

The language server and VS Code extension live in their own repositories: behl-lsp and behl-vscode.

Key Differences from Lua

  • C-like syntax: Optional ; terminators, {} blocks, // and /* */ comments
  • 0-indexed tables: First element is at index 0, not 1
  • Operators: ** for power (not ^), != for not equal (not ~=)
  • Logical operators: &&, ||, ! instead of and, or, not
  • String concatenation: + instead of ..
  • Type introspection: typeof() instead of type()
  • Module system: import() instead of require()
  • Variable declarations: Explicit let and const keywords required

Testing

Run the test suite:

cd build
ctest -C Release --output-on-failure

License

Behl is distributed under the MIT License. See LICENSE for details.

Contributing

Contributions are welcome! Please feel free to submit issues and pull requests.

Acknowledgments

Behl is inspired by Lua's elegant design and semantics, while bringing the familiarity of C-like syntax to scripting.

About

Lua-inspired scripting language with C-like syntax, implemented in C++20.

Topics

Resources

Stars

30 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages