一个独立的编译器,类似于 clang,一个程序能完成所有编译流程。
riu(发音通 U,非英语)是一门自举的编程语言,使用 ANTLR4 解析语法,LLVM 作为编译后端。编译器将 .ut(U Text)源文件编译为可执行文件;模块声明缓存为 .ud。
- 静态类型系统,无隐式类型转换
- 结构体和成员函数
- 泛型类型(Ref, Rc, Ptr, Array)
- 自动内存管理(Rc 引用计数)
- 与 C/Windows API 互操作
; 注释,顶行 ;,可加空格缩进
fn add(a i32, b i32) i32 = a + b
fn main() {
let a = add(1, 2)
println(a.to_string())
; if 表达式
let max = if a > 0 { a } else { -a }
println(max.to_string())
}
详细语法说明请参考 文档。
- 编译器: Clang
- 构建工具: GN(PATH);Ninja / cloc 由
./sync-deps.ps1下载到bin/(Python 3 供 GN 脚本;LLVM 用其自带llvm/utils/gn) - 系统: Windows
build/windows/x64/debug/bin的绝对路径添加到PATH,以便调用
项目依赖以下库(位于 third_party/ 目录):
| 依赖 | 说明 |
|---|---|
| LLVM | 编译器后端基础设施 |
| ANTLR4 | 语法解析器生成器 |
| utfcpp | UTF-8 编码处理 |
| zlib | 压缩库 |
git clone --recurse-submodules <repo-url>
# 若已克隆未拉子模块:
git submodule update --init scripts/ps-sync-deps根目录 PowerShell 脚本(无需 init):
| 脚本 | 作用 |
|---|---|
./sync-deps.ps1 |
按 DEPS.json 同步 third_party/ 与 bin/(调用 scripts/ps-sync-deps) |
./build.ps1 |
GN + Ninja 构建入口 |
./gen-antlr.ps1 |
从 riu/ast/riu*.g4 生成 C++ 解析器到 riu/ast/gen/riu/ |
./count-lines.ps1 |
cloc 统计(可选 commit,默认 HEAD) |
./lint.ps1 |
clang-tidy(默认 git 变动文件;--all / 路径参数) |
./format.ps1 |
clang-format(默认 git 变动;--all / --check / 路径参数) |
./sync-deps.ps1
./sync-deps.ps1 -DryRun # 预览
./sync-deps.ps1 cli11 zlib # 只同步指定项若 llvm 源码 commit 有变,下次 ./build.ps1 riu(或 ./build.ps1 llvm)会按 stamp 自动重新 gn gen 并编译 LLVM(首次/升级可能很久)。
修改 riu/ast/riu*.g4 后:
./gen-antlr.ps1需要先有 bin/antlr-4.13.2-complete.jar(由 sync-deps 下载)。
使用 cloc(bin/cloc-2.10.exe,由 sync-deps 下载):
./count-lines.ps1 # HEAD
./count-lines.ps1 <commit>排除 lock 文件,并用 riu_lang_def.txt 识别 riu。
./lint.ps1 # lint git 已变动文件
./lint.ps1 --all # target 全量
./lint.ps1 src/foo.cpp # 指定文件
./format.ps1 # 格式化 git 已变动文件
./format.ps1 --all # 全仓
./format.ps1 --check # 只检查不改,有差异退出码 1# 构建 riu 编译器
./build.ps1 riu
# 可选:附属工具
./build.ps1 riu-lsp # LSP 服务器(编辑器插件用)
./build.ps1 riu-ast # 仅 ANTLR parse tree 转储工具
./build.ps1 # 全部默认目标在项目根目录(含 riu.toml)执行:
riu build # 等价于 riu build <toml-name>;当前每个项目仅一个目标
riu build <name> # 显式给出时 <name> 必须与 riu.toml 的 name 一致
# 入口取 toml 的 entry,产物落在 <projectRoot>/build/<name>/<name>.exe| 字段 | 说明 |
|---|---|
name |
项目 / 可执行文件名;riu build 默认取它,显式 riu build <name> 必须与它匹配 |
entry |
入口 .ut,相对项目根 |
version |
版本号(当前仅记录) |
最小示例:
name="test"
version="1.0.0"
entry="main.ut"| 参数 | 说明 |
|---|---|
build <name> |
项目构建子命令 |
--emit-ir |
输出 LLVM IR 到 .ll 文件 |
--emit-ir-dir <dir> |
指定 .ll 输出目录(默认 build/) |
-d, --debug |
输出编译 IR 调试信息(仅 Debug 构建) |
lsp |
以 stdio 启动语言服务器(供 riu-vscode / riu-idea 等编辑器集成使用) |
附属可执行文件(与 riu.exe 同目录):
| 命令 | 说明 |
|---|---|
riu-ast <input.ut> [-o <file>] [--oneline] |
转储 ANTLR parse tree;仅词法 + 语法,遇到语法错也输出含 <error> 节点的树 |
riu-lsp |
独立 LSP 服务器二进制 |
plugins/riu-vscode/—— VSCode 扩展plugins/riu-idea/—— IntelliJ 系插件,通过 LSP4IJ 接入riu-lsp
测试分为三级,覆盖不同层面:
| 层级 | 命令 | 用例位置 | 说明 |
|---|---|---|---|
| 项目编译+运行 | ./build.ps1 test |
tests/projects/ |
每目录一个 riu.toml + expected.txt;编译产物并比对 stdout |
| 格式化回归 | ./build.ps1 test |
tests/projects/ |
expected_format 文件,比对外格式化输出 |
| 诊断回归 | riu-check test |
tests/check-cases/ |
diag_*.ut,行尾 ; check: EXXXX 注解精确匹配 |
| 单元/行为测试 | riu test |
sdk/riu/src/riu/core/*.test.ut |
#Test 注解,DLL + 多子进程并行 |
运行方式:
# 项目 / 格式化测试
./build.ps1 riu
./build.ps1 test # 全部(默认并行,jobs = CPU 核数)
./build.ps1 test -Jobs 1 # 强制串行
./build.ps1 test <name> # 单个(tests/projects/<name>)
./build.ps1 test -Group format # 只跑格式化
# 诊断回归
riu-check test tests/check-cases/
# SDK 单元测试(主测试集)
cd sdk/riu && riu test
riu test --verbose # 打印每个测试 stdout/stderr
riu test --test-mod riu.core.array # 只测指定模块语法以 riu/ast/riu*.g4 和 文档 为准,用例需符合这两者。
测试逻辑:./build.ps1 test 定义在 tests/run.ps1;riu test 流程为 riu build --test → 并行 spawn riu-test-runner 子进程加载 DLL 执行。
Copyright (c) 2025-2026. Yin-Jinlong@github