English | 简体中文
Cavvy (Cay) 是一个静态类型的面向对象编程语言,编译为原生机器码,无运行时依赖,无 VM,无 GC。
核心特性:
- 🚀 原生性能:编译为 Windows EXE / Linux ELF,零开销抽象
- 🛡️ 内存安全:显式内存管理,RAII 模式支持
- ☕ Java 风格语法:熟悉的面向对象编程体验
- 🔧 完整工具链:从源码到可执行文件的一站式编译
- 🌉 FFI 支持:无缝调用 C 函数和系统库
- 📦 字节码系统:支持
.caybc格式和代码混淆
创建文件 hello.cay:
public class Hello {
public static void main() {
println("Hello, World!");
}
}
或使用顶层 main 函数(0.4.3+):
public int main() {
println("Hello from top-level main!");
return 0;
}
# 使用 cayc 一站式编译
./target/release/cayc hello.cay hello.exe
# 运行
./hello.exe# 克隆仓库
git clone https://github.com/cavvy-lang/cavvy.git
cd cavvy
# 构建编译器(Release 模式)
cargo build --release
# 运行测试
cargo test --release- Windows: Windows 10/11 x64
- Linux: x86_64 Linux 发行版
- 依赖: LLVM 17.0+, MinGW-w64 13.2+ (Windows)
// 整数类型
int a = 10;
long b = 100L;
// 浮点类型
float f = 3.14f;
double d = 3.14159;
// 其他基础类型
boolean flag = true;
char c = 'A';
String s = "Hello, Cavvy!";
// 自动类型推断(0.4.3+)
auto x = 42; // int
auto pi = 3.14; // double
auto msg = "hi"; // String
// 一维数组
int[] arr = new int[5];
int[] initArr = {1, 2, 3, 4, 5};
// 多维数组
int[][] matrix = new int[3][3];
int[][] grid = {{1, 2}, {3, 4}, {5, 6}};
// 数组长度
int len = arr.length;
// 数组访问
arr[0] = 100;
int val = arr[0];
// if-else
if (a > b) {
println("a is greater");
} else if (a == b) {
println("a equals b");
} else {
println("a is smaller");
}
// switch 语句
switch (value) {
case 1:
println("one");
break;
case 2:
println("two");
break;
default:
println("other");
break;
}
// 循环
for (int i = 0; i < 10; i++) {
println(i);
}
long j = 0;
while (j < 10) {
println(j);
j++;
}
// do-while
int k = 0;
do {
println(k);
k++;
} while (k < 5);
// 类定义与继承
public class Animal {
protected String name;
public Animal(String name) {
this.name = name;
}
public void speak() {
println("Some sound");
}
}
public class Dog extends Animal {
public Dog(String name) {
super(name);
}
@Override
public void speak() {
println(name + " says: Woof!");
}
}
// 抽象类与接口
public abstract class Shape {
public abstract double area();
}
public interface Drawable {
void draw();
}
public class Calculator {
// 方法重载
public static int add(int a, int b) {
return a + b;
}
public static double add(double a, double b) {
return a + b;
}
// 可变参数
public static int sum(int... numbers) {
int total = 0;
for (int i = 0; i < numbers.length; i++) {
total = total + numbers[i];
}
return total;
}
}
// Lambda 表达式
var add = (int a, int b) -> { return a + b; };
int result = add(3, 4);
// 简写形式
var multiply = (int a, int b) -> a * b;
// 方法引用
var ref = Calculator::add;
String s = "Hello World";
// 字符串方法
int len = s.length();
String sub = s.substring(0, 5);
int idx = s.indexOf("World");
String replaced = s.replace("World", "Cavvy");
char ch = s.charAt(0);
// 字符串拼接
String msg = "Hello, " + name + "!";
// 声明外部 C 函数
extern {
int abs(int x);
double sqrt(double x);
int strlen(String s);
}
public class MathExample {
public static void main() {
int result = abs(-42);
double root = sqrt(2.0);
println("Abs: " + result);
println("Sqrt: " + root);
}
}
public class Constants {
// 编译期常量
public static final double PI = 3.14159;
// 静态初始化块
static {
println("Class initialized");
}
// Final 类/方法
public final class Immutable { }
}
本项目提供六个可执行文件:
| 工具 | 功能 | 用法 |
|---|---|---|
cayc |
Cavvy → EXE (一站式) | cayc source.cay output.exe |
cay-ir |
Cavvy → LLVM IR | cay-ir source.cay output.ll |
ir2exe |
LLVM IR → EXE | ir2exe input.ll output.exe |
cay-check |
语法检查 | cay-check source.cay |
cay-run |
直接运行 | cay-run source.cay |
cay-bcgen |
生成字节码 | cay-bcgen source.cay output.caybc |
# 基础编译
cayc hello.cay hello.exe
# 优化级别
cayc -O3 hello.cay hello.exe # 最高优化
cayc -O0 hello.cay hello.exe # 无优化(调试)
# 字节码混淆
cayc --obfuscate --obfuscate-level deep hello.cay hello.exe
# 链接库
cayc -lm hello.cay hello.exe # 链接数学库
# 跨平台目标
cay-ir --target x86_64-linux-gnu hello.cay hello.llpublic class Multiplication {
public static void main() {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
print(j + "x" + i + "=" + (i*j) + "\t");
}
println("");
}
}
}
public class Fibonacci {
// 递归实现
public static long fib(int n) {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}
// 迭代实现
public static long fibIterative(int n) {
if (n <= 1) return n;
long a = 0, b = 1;
for (int i = 2; i <= n; i++) {
long temp = a + b;
a = b;
b = temp;
}
return b;
}
public static void main() {
for (int i = 0; i < 20; i++) {
println("fib(" + i + ") = " + fibIterative(i));
}
}
}
public class Sorting {
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
public static void main() {
int[] numbers = {64, 34, 25, 12, 22, 11, 90};
bubbleSort(numbers);
print("Sorted: ");
for (int i = 0; i < numbers.length; i++) {
print(numbers[i] + " ");
}
println("");
}
}
cavvy/
├── src/ # 源代码
│ ├── bin/ # 可执行文件
│ │ ├── cayc.rs # 一站式编译器
│ │ ├── cay-ir.rs # Cavvy → IR 编译器
│ │ ├── ir2exe.rs # IR → EXE 编译器
│ │ ├── cay-check.rs # 语法检查工具
│ │ ├── cay-run.rs # 直接运行工具
│ │ ├── cay-bcgen.rs # 字节码生成器
│ │ └── cay-lsp.rs # LSP 语言服务器
│ ├── lexer/ # 词法分析器
│ ├── parser/ # 语法分析器
│ ├── semantic/ # 语义分析器
│ ├── codegen/ # 代码生成器
│ ├── ast.rs # AST 定义
│ ├── types.rs # 类型系统
│ └── error.rs # 错误处理
├── examples/ # 示例程序
├── caylibs/ # 标准库
├── docs/ # 文档
│ └── README/images/ # README 图片资源
├── tests/ # 测试套件
├── llvm-minimal/ # LLVM 工具链
├── mingw-minimal/ # MinGW 链接器
└── Cargo.toml # Rust 项目配置
已完成功能 (0.4.x):
- 基础类型系统 (int, long, float, double, boolean, char, String, void)
- 变量声明和赋值(支持 var/let/auto)
- 算术运算符 (+, -, *, /, %)
- 比较运算符 (==, !=, <, <=, >, >=)
- 逻辑运算符 (&&, ||, !)
- 位运算符 (&, |, ^, ~, <<, >>)
- 自增自减运算符 (++, --)
- 复合赋值运算符 (+=, -=, *=, /=, %=)
- 条件语句 (if-else, switch)
- 循环语句 (while, for, do-while)
- break/continue 支持
- 数组(一维和多维)
- 数组初始化器和长度属性
- 字符串拼接和方法
- 类型转换(显式和隐式)
- 方法重载
- 可变参数
- Lambda 表达式
- 方法引用
- 类和单继承
- 抽象类和接口
- 访问控制 (public/private/protected)
- 构造函数和析构函数
- Final 类和 Final 方法
- 静态成员和静态初始化
- @Override 注解
- 顶层 main 函数
- FFI 外部函数接口
- 自动链接器
- 字节码系统 (CayBC)
- 代码混淆
- LSP 语言服务器
- Windows / Linux 跨平台支持
详见 ROADMAP.md
即将推出 (0.5.x):
- 分配器接口和 Arena 分配器
- 泛型集合 (ArrayList, HashMap)
- 智能指针 (UniquePtr, ScopedPtr)
- Result<T, E> 错误处理
- 操作系统线程封装
- 前端: Rust 实现的词法分析、语法分析、语义分析
- 中端: LLVM IR 代码生成
- 后端: MinGW-w64 / GCC 工具链
- 字节码: 自定义 CayBC 格式(基于栈的虚拟机)
本项目采用 GPL3 许可证。详见 LICENSE 文件。
欢迎提交 Issue 和 Pull Request。
- 🐛 Bug 报告: 使用 GitHub Issues
- 💡 功能建议: 查看 ROADMAP.md 后提交 PR
- 📖 文档改进: 直接编辑文档并提交
Cavvy - 编译未来