Skip to content

Repository files navigation

Candle


Logo

🔥 关于 Candle

Candle(汉译:,由"火"和"虫"组成)是一款现代化的多范式编程语言,专注于高性能、简洁语法和强大的并行计算能力。

设计理念

"通过关键字一键进行多核并行计算;Candle语法设计完后,剩下完全由Agent基于Rust实现编译器,基于GPT5.5、GPT5.6SOL、Opus4.8、Opus5、DeepseekV4pro、Grok4.5、Sonnet5这些LLM串行实现"

Candle完全由RemindAI设计

Candle 旨在提供:

  • 🚀 原生性能 - 编译到原生机器码,无运行时开销
  • 🧹 自动内存管理 - 内置垃圾回收(GC),无需手动管理内存
  • 并行计算 - 简单而强大的并行数据处理能力
  • 🌍 跨平台 - 支持 Windows、Linux 和 macOS
  • 🎯 类型安全 - 静态类型系统,编译时捕获错误
  • 🤖 AI 驱动开发 - 编译器由 AI 实现,语法由人类设计

✨ 特性

语言特性

  • 强类型系统 - 静态类型检查,支持类型推断
  • 面向对象 - 类、继承、接口、扩展方法
  • 泛型编程 - 泛型类、泛型函数、类型约束
  • 函数式编程 - Lambda 表达式、闭包、高阶函数
  • 异常处理 - Try-Catch-Finally 机制
  • 运算符重载 - 自定义运算符行为
  • 并行计算 - 内置 parallel 关键字,轻松实现多线程

技术特点

  • LLVM 后端 - 利用 LLVM 生成优化的机器码
  • 垃圾回收 - 自动内存管理,避免内存泄漏
  • 原生编译 - 编译到独立的可执行文件
  • 零依赖运行时 - 生成的程序无需额外运行时
  • 快速执行 - 接近 C/C++ 的运行性能

标准库

  • 集合类型 - List、Map、Set
  • 字符串处理 - 丰富的字符串 API
  • 数学库 - 完整的数学函数支持
  • I/O 操作 - 文件读写、网络通信
  • 并发原语 - AtomicInt、线程安全操作

🚀 快速开始

安装编译器

下载预编译的 candlec.exe 编译器,或从源码构建:

# 克隆仓库
git clone https://github.com/PythonnotJava/candle.git
cd candle

# 构建编译器(需要 Rust 工具链)
cargo build --release

# 编译器位于
./target/release/candlec.exe

Hello, World!

创建 hello.candle 文件:

int main() {
    print("Hello, Candle!\n");
    return 0;
}

编译并运行:

# 编译
candlec build hello.candle

# 运行
candlec run hello.candle

💻 代码示例

基础语法

/// 计算斐波那契数列
int fibonacci(int n) {
    if (n <= 1) {
        return n;
    }
    return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
    print("Fibonacci(10) = ");
    print(fibonacci(10));
    print("\n");
    return 0;
}

面向对象

/// 定义一个类
class Person inherit Object {
    String name;
    int age;

    Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    void greet() {
        print("Hello, I'm ");
        print(this.name);
        print("\n");
    }
}

int main() {
    Person person = Person("Alice", 30);
    person.greet();
    return 0;
}

泛型和集合

/// 泛型函数:查找列表中的最大值
T findMax<T>(List<T> items) {
    T max = items[0];
    for (T item in items) {
        if (item > max) {
            max = item;
        }
    }
    return max;
}

int main() {
    List<int> numbers = List<int>();
    numbers.add(10);
    numbers.add(50);
    numbers.add(30);
  
    int max = findMax<int>(numbers);
    print("Max: ");
    print(max);
    print("\n");
  
    return 0;
}

并行计算(Parallel 关键字)

/// 并行处理大数据集
int main() {
    List<int> data = List<int>();
  
    // 生成大量数据
    for (int i = 0; i < 1000000; i = i + 1) {
        data.add(i);
    }
  
    int sum = 0;
  
    // 使用 parallel 关键字并行计算
    parallel for (int value in data) {
        sum = sum + value;  // 原子操作,线程安全
    }
  
    print("Sum: ");
    print(sum);
    print("\n");
  
    return 0;
}
/// 并行 Map-Reduce 操作
void parallelMapReduce() {
    List<int> numbers = List<int>();
    for (int i = 1; i <= 100; i = i + 1) {
        numbers.add(i);
    }
  
    List<int> results = List<int>();
  
    // 并行映射:计算每个数的平方
    parallel for (int num in numbers) {
        int squared = num * num;
        results.add(squared);
    }
  
    // 并行归约:求和
    int total = 0;
    parallel for (int result in results) {
        total = total + result;
    }
  
    print("Sum of squares: ");
    print(total);
    print("\n");
}

int main() {
    parallelMapReduce();
    return 0;
}

Lambda 和函数式编程

/// 高阶函数:映射列表
List<int> map(List<int> items, int transform(int x)) {
    List<int> result = List<int>();
    for (int item in items) {
        result.add(transform(item));
    }
    return result;
}

int main() {
    List<int> numbers = List<int>();
    numbers.add(1);
    numbers.add(2);
    numbers.add(3);
  
    // 定义转换函数
    int square(int x) {
        return x * x;
    }
  
    List<int> squared = map(numbers, square);
  
    for (int num in squared) {
        print(num);
        print(" ");
    }
    print("\n");
  
    return 0;
}

运算符重载

/// 2D 向量类
class Vector inherit Object {
    double x;
    double y;

    Vector(double x, double y) {
        this.x = x;
        this.y = y;
    }

    /// 重载加法运算符
    @operator
    Vector __add__(Vector other) {
        return Vector(this.x + other.x, this.y + other.y);
    }
  
    /// 重载乘法运算符(标量乘法)
    @operator
    Vector __mul__(double scalar) {
        return Vector(this.x * scalar, this.y * scalar);
    }
}

int main() {
    Vector v1 = Vector(1.0, 2.0);
    Vector v2 = Vector(3.0, 4.0);
    Vector v3 = v1 + v2;  // 使用重载的 + 运算符
    Vector v4 = v3 * 2.0; // 使用重载的 * 运算符
  
    print("Result: (");
    print(v4.x);
    print(", ");
    print(v4.y);
    print(")\n");
  
    return 0;
}

异常处理

/// 安全的除法函数
double safeDivide(double a, double b) {
    try {
        if (b == 0.0) {
            throw Exception("除数不能为零");
        }
        return a / b;
    } catch (Exception e) {
        print("错误:除数为零,返回 0\n");
        return 0.0;
    } finally {
        print("除法操作完成\n");
    }
}

int main() {
    double result1 = safeDivide(10.0, 2.0);
    print("10 / 2 = ");
    print(result1);
    print("\n");
  
    double result2 = safeDivide(10.0, 0.0);
    print("10 / 0 = ");
    print(result2);
    print("\n");
  
    return 0;
}

更多示例请查看 examples 目录。


📚 文档

语言模板

完整的语法和 API 模板位于 template 目录:

  • 01_keywords.candle - 所有关键字用法
  • 02_operators.candle - 运算符和符号
  • 03_annotations.candle - 注解系统(@builtin, @operator 等)
  • 04_classes.candle - 类、继承、接口
  • 05_generics.candle - 泛型编程
  • 06_exceptions.candle - 异常处理
  • 07_stdlib_api.candle - 标准库 API
  • 08_lambda_functional.candle - Lambda 和函数式编程

编译器命令

# 编译项目
candlec build <file.candle> [-o output] [--debug|--release]

# 直接运行
candlec run <file.candle> [--backend native|interpreter]

# 语法检查
candlec check <file.candle>

# 查看中间表示
candlec emit cir <file.candle>     # CIR
candlec emit llvm <file.candle>    # LLVM IR
candlec emit ast <file.candle>     # 抽象语法树

# 版本信息
candlec version [--verbose]

项目结构

Candle/
├── crates/          # 编译器源代码
│   ├── candle-cli/       # 命令行接口
│   ├── candle-cir/       # CIR 中间表示
│   ├── candle-codegen/   # 代码生成
│   ├── candle-lexer/     # 词法分析
│   ├── candle-parser/    # 语法分析
│   └── candle-runtime/   # 运行时库
├── lib/             # 标准库
│   └── core/             # 核心库
├── examples/        # 示例代码
├── template/        # 语法模板
├── scripts/         # 构建脚本
└── target/          # 编译输出
    └── release/
        └── candlec.exe   # 编译器

🛠️ 技术栈

  • 实现语言: Rust
  • 编译后端: LLVM
  • 运行时: 自定义 GC + Rust 运行时
  • 目标平台: x86_64 (Windows, Linux, macOS)

🎯 Panum 数组库

Candle 内置高性能的 Panum 数组计算库,支持:

  • N 维数组 - 多维数组支持
  • 内存零拷贝 - 高效的视图和切片
  • 并行计算 - SIMD 和多线程优化
  • 自举实现 - 90% Candle 代码 + 10% Rust runtime

示例:

class ArrayFloat inherit Object {
    @builtin
    static ArrayFloat _allocate(List<int> shape, int dtype);
  
    @builtin
    double _get_flat(int index);
  
    @builtin
    void _set_flat(int index, double value);
  
    @builtin
    double _raw_sum();
  
    @builtin
    ArrayFloat _raw_add(ArrayFloat other);
}

int main() {
    // 创建形状为 [5] 的数组
    List<int> shape = List<int>();
    shape.add(5);
  
    ArrayFloat arr1 = ArrayFloat._allocate(shape, 2);
    ArrayFloat arr2 = ArrayFloat._allocate(shape, 2);
  
    // 填充数据
    for (int i = 0; i < 5; i = i + 1) {
        arr1._set_flat(i, i + 1.0);
        arr2._set_flat(i, (i + 1.0) * 2.0);
    }
  
    // 数组加法
    ArrayFloat result = arr1._raw_add(arr2);
  
    // 计算总和
    print("Sum: ");
    print(result._raw_sum());  // 输出: 45.0
    print("\n");
  
    return 0;
}

⚡ 并行计算特性

Candle 的 parallel 关键字让并行编程变得简单:

并行 For 循环

// 串行版本
for (int i in data) {
    process(i);
}

// 并行版本 - 只需添加 parallel 关键字
parallel for (int i in data) {
    process(i);
}

线程安全的数据结构

class AtomicInt {
    @builtin
    void add(int value);
  
    @builtin
    int get();
}

int main() {
    AtomicInt counter = AtomicInt();
  
    List<int> data = List<int>();
    for (int i = 0; i < 1000; i = i + 1) {
        data.add(i);
    }
  
    // 并行累加,无需担心竞态条件
    parallel for (int value in data) {
        counter.add(value);
    }
  
    print("Total: ");
    print(counter.get());
    print("\n");
  
    return 0;
}

性能优势

  • 自动负载均衡 - 编译器自动分配任务到多个线程
  • 零开销抽象 - 并行代码编译为高效的原生多线程代码
  • 内存安全 - 编译时检查数据竞争,避免运行时错误
  • SIMD 优化 - 自动向量化支持,充分利用 CPU 指令集

🤝 贡献

欢迎贡献代码、报告问题或提出建议!

开发环境设置

# 1. 安装 Rust(需要 1.70+)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# 2. 克隆项目
git clone https://github.com/PythonnotJava/candle.git
cd candle

# 3. 运行测试
cargo test

# 4. 构建编译器
cargo build --release

📄 许可证

本项目采用 Apache-2.0 许可证 - 详见 LICENSE 文件。

About

Candle. In Chinese, it is composed of the two characters fire and worm.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages