8 releases (5 stable)

Uses new Rust 2024

new 1.4.0 Feb 2, 2026
1.3.0 Feb 1, 2026
1.2.0 Jan 31, 2026
0.3.0 Jan 26, 2026
0.1.0 Jan 24, 2026

#1067 in Rust patterns

MIT license

56KB
1.5K SLoC

cral

This crate is used to generate C code based on AST.

This crate versions 1.0.0 and above is modified based on crustal crate.

This crate has streamlined crustal and added the following content:

  • Function pointer.
  • __attribute__(()) macro gen.

Remove the following content:

  • C++ code gen part.

Exaple : HelloWorld

use cral::*;

let mut scope = Scope::new();
scope.new_include("stdio.h", true);

let mut main_block = Block::new();
main_block.new_expr(Expr::fn_call("printf", vec![Expr::str("Hello World!")]));
scope.new_function("main", Type::void()).set_body(main_block);
scope.to_file("main.c", false).unwrap();

Generated c code

#include<stdio.h>
void main(void) {
    printf("HelloWorld");
}

No runtime deps