Skip to main content

module_header_source/
module_header_source.rs

1//! Header/source splitting: describe a translation unit once and get a matching
2//! `.h` and `.c`. Function definitions become a prototype in the header and a
3//! body in the source automatically.
4//!
5//! Run with: `cargo run --example module_header_source`
6
7use tamago::*;
8
9// Small helpers so the expression trees stay readable.
10fn ident(name: &str) -> Expr {
11    Expr::new_ident_with_str(name)
12}
13fn member(object: &str, field: &str) -> Expr {
14    Expr::new_mem_access_with_str(ident(object), field)
15}
16
17fn main() {
18    let float = Type::base(BaseType::Float);
19    let vec2 = Type::base(BaseType::TypeDef("Vec2".to_string()));
20
21    // struct Vec2 { float x; float y; };
22    let vec2_struct = StructBuilder::new_with_str("Vec2")
23        .field(FieldBuilder::new_with_str("x", float.clone()).build())
24        .field(FieldBuilder::new_with_str("y", float.clone()).build())
25        .build();
26
27    // typedef struct Vec2 Vec2;
28    let vec2_typedef =
29        TypeDefBuilder::new_with_str(Type::base(BaseType::Struct("Vec2".to_string())), "Vec2")
30            .build();
31
32    // Vec2 vec2_add(Vec2 a, Vec2 b) { return (Vec2){a.x + b.x, a.y + b.y}; }
33    let add = FunctionBuilder::new_with_str("vec2_add", vec2.clone())
34        .param(ParameterBuilder::new_with_str("a", vec2.clone()).build())
35        .param(ParameterBuilder::new_with_str("b", vec2.clone()).build())
36        .body(
37            BlockBuilder::new()
38                .statement(Statement::Return(Some(Expr::new_cast(
39                    vec2.clone(),
40                    Expr::new_init_struct_in_order(vec![
41                        Expr::new_binary(member("a", "x"), BinOp::Add, member("b", "x")),
42                        Expr::new_binary(member("a", "y"), BinOp::Add, member("b", "y")),
43                    ]),
44                ))))
45                .build(),
46        )
47        .build();
48
49    // float vec2_dot(Vec2 a, Vec2 b) { return a.x * b.x + a.y * b.y; }
50    let dot = FunctionBuilder::new_with_str("vec2_dot", float.clone())
51        .param(ParameterBuilder::new_with_str("a", vec2.clone()).build())
52        .param(ParameterBuilder::new_with_str("b", vec2.clone()).build())
53        .body(
54            BlockBuilder::new()
55                .statement(Statement::Return(Some(Expr::new_binary(
56                    Expr::new_binary(member("a", "x"), BinOp::Mul, member("b", "x")),
57                    BinOp::Add,
58                    Expr::new_binary(member("a", "y"), BinOp::Mul, member("b", "y")),
59                ))))
60                .build(),
61        )
62        .build();
63
64    let m = Module::new("vec2")
65        .struct_(vec2_struct)
66        .typedef(vec2_typedef)
67        .header_newline()
68        .function(add)
69        .function(dot)
70        .build();
71
72    println!("// ===== vec2.h =====");
73    print!("{}", m.header());
74    println!("\n// ===== vec2.c =====");
75    print!("{}", m.source());
76}