Introduction
Introduction
BT (BT Programming Language) is an interpreted language implemented in Rust. It adopts a syntax style close to JavaScript and executes code through lexical analysis, syntax analysis, bytecode compilation and register-based virtual machine. It is oriented to scenarios such as scripts, web services, desktop applications, network communications, and device communications. The goal is to keep the runtime lightweight and resident while keeping the syntax intuitive.
The core feature of BT is "expressions have values". The function body, if code block, files and template fragments introduced by include() will all return the value of the last statement; return can also be used in the function to return early. This allows BT to organize business logic with less boilerplate code.
Design goal
- JavaScript-like intuitive syntax to reduce learning costs.
- The standard library is split according to object capabilities, supporting chain calls and on-demand expansion.
- Supports three main running environments: CLI, Web service and desktop App.
- Suitable for resident memory services, cache and background tasks must be controllable.
Execution mechanism
BT source code will be compiled into bytecode first and then executed by VM:
BT source code ↓ Lexical analysis ↓ Parsing ↓ Bytecode compilation ↓ Register-based virtual machine execution
Ordinary scripts are run by bt_cli; desktop projects are read by bt_app and then enter static, server or remote mode; Web The service is started with net.listen({type:'web'}) and the web context is injected in the request.
Basic syntax
BT maintains JS-like writing while retaining code block return value semantics:
fn level(score) { if score >= 60 { 'pass' } else { 'fail' } } println(level(72))
Arrays, objects and callbacks can be chained together:
users = [ {name:'Lisa', age:18} {name:'Tom', age:25} {name:'Jack', age:16} ] names = users .filter(user -> user.age >= 18) .map(user -> user.name) println(names)
Modules and templates
include() is used to read and execute other BT files. The relative path is resolved based on the directory where the current source code file is located, and @ represents the project root directory.
config = include('@/config/app.bt') util = include('common/util.bt')
Web pages can use the # TPL template file to embed BT expressions and code snippets in HTML, which is suitable for official websites, background pages and server-side rendering scenarios.
Standard library
The current standard library covers common basic capabilities:
-
fs: file reading and writing, copying, moving, deleting, directory traversal and path information. -
net: Web, TCP, UDP, WebSocket, DNS and native network information. -
web: Request context reading, response headers, status codes, jumps and Cookie/Session writeback. -
reqwest: HTTP client request, supports header, body, json, form, query, multipart, timeout and other chain configurations. -
device: Device scanning, opening, reading and writing, closing, currently focusing on serial port capabilities. -
bytes: Binary byte value, serving serial port, network and industrial protocol boundaries.
modbus: Modbus RTU/TCP request frame, response parsing and CRC16 auxiliary capabilities.
-
process: Command parameters, environment variables, working directory and sub-process management. -
ffi: Native dynamic library calling capabilities, supporting full C ABI signature, return hints and limited declaration-free calling of native.dll,.soor.dylib. -
date,math,md5,base64,mysql: Date, math, summarization, encoding and database related abilities.
Web service
BT's Web service configures the site, entry script, static resources and SSL through net.listen({type:'web'}):
net.listen({ type:'web' bind:'0.0.0.0:8080' sites:[ { domains:['0.0.0.0'] root:'www/' entry:'main.bt' static:{ route:'/static/{**}' path:'www/static/' } } ] })
After the request enters, the entry BT file can read web.get, web.post, web.server, web.cookie, web.session context, and output the response content through the last statement or print.
Desktop application
bt_app is BT's desktop application runner and packaging tool. It reads the app.json creation window, supports:
-
static: loads HTML resources within the project, and accesses packaged resources throughbt://app/.... -
server: Execute local BT web service, then load local HTTP address. -
remote: Load trusted remote page.
The desktop page will be injected with window.bt. The front end can call window.bt.call() to execute the functions in main.bt, and can also use desktop APIs such as windows, trays, clipboards, notifications, dialog boxes, and drag and drop. When building, bt_app will write resources into the exe tail Bundle to facilitate single file distribution.
Network and Devices
net standard library is for long connections and local services, supporting TCP, UDP, WebSocket server/client, DNS resolution and native IP query. Event callbacks uniformly use snake_case names such as on_connect, on_message, on_close, and on_error. Binary callbacks can return Bytes via binary: true.
device standard library is used for device communication. It can currently scan and operate the serial port:
ports = device.scan('serial') println(ports)
Application scenario
- CLI script and automation tool.
- Web website, backend management system and API service.
- TCP, UDP, WebSocket long connection service.
- Serial device control, Modbus industrial automation and IoT prototypes.
Summary
BT puts script syntax, bytecode VM, web services, network communication, device access and desktop packaging in the same language system. It is suitable for projects that require rapid development, long-term operation, extensible standard library and lightweight deployment.