programming language / C backend

xlang

编译到本地,也能嵌入应用。

一种以 C 为后端的编程语言。生成本地二进制,或作为运行时接入现有 C/C++ 工程。

C backend本地二进制
C API可嵌入运行时
No VM无虚拟机依赖
xlang command center ⌘ K
RUN Compile & runNative binary · 34 ms ↵
API Embed runtimeGenerate C API bindings ⌥ E
ASY Async taskRun with native event loop ⌥ A
● Hello, xlang! exit 0 · 34ms
01 Compile to C
02 Native binary
01 / LANGUAGE

核心能力

编译、类型系统、异步、标准库与跨平台支持。

01

原生编译

源码生成 C,再由目标平台工具链产出本地二进制。

02

C / C++ 嵌入

通过稳定的 C API 接入现有工程,也可以作为脚本运行时使用。

03

静态与动态类型

显式类型、auto 推断与动态 var 可以按场景组合。

04

异步与并发

内置 async / await、future、channel 与任务编排。

05

标准库

网络、加密、模板、日志与文本处理随语言一并提供。

06

多平台

沿用成熟的 C 工具链,覆盖桌面、移动与嵌入式环境。

一套代码,跨全平台

xlang 以 C 为编译后端——凡是 C 编译器存在的地方,xlang 就能运行。从云服务器到嵌入式板卡,从 x86 到 RISC-V,无需修改一行代码。

操作系统

Windows Linux Android macOS iOS 鸿蒙 HarmonyOS 嵌入式 Linux 板卡

CPU 架构

x86 / x86_64 ARM / ARM64 RISC-V LoongArch (龙芯)
A1

无虚拟机依赖

不需要 JVM、不需要 Python 运行时、不需要 Node.js。编译为原生机器码,目标机零环境配置。

A2

交叉编译友好

C 后端天然支持交叉编译工具链。一次编写,使用目标平台的 C 编译器即可生成对应二进制。

A3

嵌入式友好

TCC 后端体积小、启动快,适合资源受限的嵌入式 Linux 板卡和 IoT 设备。

A4

国产化生态

原生支持鸿蒙 HarmonyOS 与龙芯 LoongArch 架构,满足信创和国产化替代需求。

代码是什么样的

C 风格类型前置 + end 块结构,编译期类型检查,内嵌 TCC 即时执行。

hello.xl
// 你的第一个 xlang 程序
func main(...) as int
    print("Hello, xlang!")
    return 0
end
f-string 与时间字面量
string name = "xlang"
print(f"Hello {name}!")

time t = #2026-07-06 12:30:00#
print(time.formatPattern(t, "yyyy-mm-dd hh:mm"))
类与继承
class Animal
    string name = ""

    create(string name)
        self.name = name
    end

    virtual func speak() as string
        return "..."
    end
end

class Dog extends Animal
    create(string name)
        base(name)
    end

    override func speak() as string
        return self.name + ": Woof!"
    end
end
async/await
async func addAsync(int a, int b) as int
    return a + b
end

future<int> a = addAsync(1, 2)
future<int> b = addAsync(3, 4)

array<int> all = await task.all([a, b])
print(all)
生成器
func numbers(int count) as generator<int>
    int i = 0
    while i < count
        i += 1
        yield i
    end
end

func main(...) as int
    for value in numbers(5)
        print(value)
    end
    return 0
end
derive 自动序列化
class Packet
    derive json
    derive copy

    int id = 0
    string name = ""
end

auto p = Packet()
p.id = 42
p.name = "hello"

string text = json.stringify(p)
print(text)

auto p2 = json.parse::<Packet>(text)
print(p2.name)

核心特性

既有脚本语言的开发效率,又有编译型语言的性能保障。

1

编译型脚本语言

源码转译为 C,通过内嵌 TCC 在内存中编译执行,无需生成中间文件。兼具脚本语言的即时执行体验和编译型语言的性能。

XL → C → TCC in-memory
2

完整类型系统

基础类型 int8-64/uint8-64/float32-64、动态类型 var、类型值 type、auto 推断、泛型容器 array/list/set/dict、C ABI 精确类型。is/as 运行时类型判断与向下转换。

int/i8/i64 var array<T> is/as
3

Async/Await

原生 async/await 语法,future<T> 异步值。task.all/race 组合编排,task.cancel 取消控制,完整的取消传播和失败处理语义。

async await future<T> task.all/race
4

生成器与 Channel

yield 惰性求值,generator<T> 类型化生成器。channel<T> 线程间通信,send/recvInto 阻塞与非阻塞接收。

yield generator<T> channel<T>
5

完整 OOP 系统

class 继承与 base 调用、virtual/override/final vtable 派发、public/private 访问控制、static 成员、get/let 属性(含多维重载)、运算符协议重载、__iter__ 迭代协议、derive 自动派生。class 默认引用语义,byval/byref/out 参数传递,显式 copy/clone 控制副本。

extends virtual byref derive
6

泛型与约束

泛型函数与泛型 class,显式实例化语法 ::<T>。约束语法 <T is Constraint>,多个约束用 and 组合。内置能力约束与 class 继承约束。

max<T> is Constraint and
7

derive 自动派生

一行 derive 即可自动生成 copy/clone/json/xson/value/hash/equals 协议实现。配合 @json/@persist 字段级注解控制序列化行为。

derive copy derive json @persist
8

深度 C 互操作

extern 声明 C 函数,export 导出给 C,.xl 与 .c 混合编译。native-backed class 对接 C opaque handle,C ABI 类型跨平台精确对齐,ABI 元数据自动生成。

extern export native-backed ABI
9

515 个标准库函数

覆盖 math / string / time / file / dir / path / json / crypto / http / net / tcp / udp / template / regex 等完整领域。包含完整 HTTP 服务器(TLS/SNI/路由/静态文件/流式 body)。

HTTP server crypto json template
10

线程与同步原语

thread 线程封装、mutex 互斥锁、sem 信号量、cond 条件变量、rwlock 读写锁。配合 async/channel 构建完整并发模型。

thread mutex rwlock cond
11

嵌入式 DLL

xl.dll 轻量运行版与 xlc.dll 编译版,76 个公开 C API(xl.h 43 + xlc.h 33),零外部依赖。支持 C 宿主创建语言上下文、运行脚本、注册原生函数、编译到 exe。

xl.dll 70 API zero-dep
12

错误处理与断言

try/catch/finally 结构化错误处理,throw 抛出异常,assert 断言语句。#doc ... #end 文档注释块支持 desc/param/return/example 字段。

try/catch throw assert #doc

编译管线

从源码到执行,四阶段编译管线,每阶段可独立调试。

词法分析 Flex 词法生成器 — Token 流,支持 UTF-8 标识符、f-string、时间字面量
↓
语法分析 Lemon LALR 解析器 — AST 构建
↓
语义检查与 IR 类型检查、协议校验、泛型实例化、重载决议、作用域管理
↓
代码生成 145 个 cgen 文件 / 756 条规则 → C 源码 → libtcc 内存编译 → 即时执行

构建产物

产物 说明
xl.exe编译器/运行器(lex/parse/check/ir/cgen/abi/run/build/impdef)
xl.dll轻量嵌入式运行 DLL(43 个公开 API,零外部依赖)
xlc.dll带编译能力的嵌入式 DLL(内嵌完整 TCC 资源)
xl.def / xlc.defDLL 导出定义文件

515 个标准库函数

覆盖核心、容器、数据、网络、并发、安全等完整领域。默认注入环境,无需 import。115 个常量。

全局

print / println / write / writeln / range

math

5 常量 + 34 函数:sqrt/sin/cos/floor/log 等

string

44 函数:split/join/replace/trim 等(UTF-8)

time

67 函数:now/format/parse/addDays 等

file

31 函数:readText/writeText/copy 等(UTF-8 路径)

dir

23 函数:list/create/delete/walk 等

path

19 函数:join/normalize/exists 等

json / xson

json 7 / xson 8 函数:stringify/parse/read/write

crypto

54 函数:SHA/HMAC/AES-GCM/Ed25519 等

http

客户端 + 完整服务器(TLS/SNI/路由)

net / tcp / udp

网络编程:connect/listen/bind

codec

base64/hex/percent 编解码

encoding

字符集探测与转换

hash

u32/u64 哈希算法

random

range/seed/string

regex

find / isMatch

log

16 函数:trace/info/warn/error 等

template

compile / render / renderFile

process

exec / pipeline / start

url / query

URL 解析与查询字符串

id

xid 唯一标识生成

快速开始

编译、运行、构建,三步上手 xlang。

1. 构建

Windows
# 进入 demo6 目录
cd demo6

# 一键构建 xl.exe + xl.dll + xlc.dll
build.bat

2. 运行与构建

运行 XL 程序
# 直接运行脚本(TCC 内存编译,不生成临时文件)
release\xl.exe run examples\hello.xl

# 传递命令行参数
release\xl.exe run examples\args.xl -- arg1 arg2

# 构建 native exe
release\xl.exe build examples\hello.xl -o hello.exe
hello.exe

# 混合编译 .xl 与 .c
release\xl.exe build main.xl helper.c -o app.exe

3. 管线调试

xl.exe 编译阶段命令
# 查看每个编译阶段
release\xl.exe lex   test.xl   # Token 流
release\xl.exe parse test.xl   # AST 树
release\xl.exe check test.xl   # 语义检查
release\xl.exe ir    test.xl   # 中间 IR
release\xl.exe cgen  test.xl   # 生成的 C 代码
release\xl.exe abi   test.xl   # ABI 导出符号表

# 一次性输出全部阶段产物
release\xl.exe cgen test.xl --dump-dir output/

4. 测试

运行测试
# 主回归测试
test.bat

# 功能范例测试
test_feature_suite.bat

# 冒烟测试
test_smoke.bat

# RC0 工程守门
rc0_guard.bat

项目规模

880
测试用例
515
标准库函数
153
范例程序
145
cgen 源文件
756
cgenrule 规则
76
DLL 公开 API