tamago 0.2.0

A code generator library for C, with a builder-pattern API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
// Copyright (c) 2025 Nobuharu Shimazu
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

//! # Block Module
//!
//! This module provides functionality for creating and managing code blocks in C programs.
//! Code blocks represent structured segments of code such as function bodies, loop bodies,
//! conditional branches, and other scoped statements.
//!
//! The primary components in this module include:
//! - `Block`: Represents a sequence of statements within curly braces in C
//! - `BlockBuilder`: Facilitates constructing blocks using the builder pattern
//! - `Statement`: Enumerates the various statement types that can appear within a block
//!
//! Use this module to programmatically generate well-structured C code with proper
//! scoping and nesting of statements.

use std::fmt::{self, Write};

use crate::{
    Comment, DoWhile, ErrorDirective, Expr, For, Format, Formatter, If, IfDefDirective,
    IfDirective, Include, LineDirective, Macro, PragmaDirective, SourceLoc, StaticAssert, Switch,
    Variable, WarningDirective, While,
};
use tamacro::DisplayFromFormat;

/// Represents a scoped block of code in C, delimited by curly braces `{}`.
/// A `Block` contains a sequence of statements that are executed together
/// within the same lexical scope.
///
/// Blocks are fundamental to C's structure and are used in various contexts:
/// - Function bodies
/// - Loop bodies (for, while, do-while)
/// - Conditional branches (if, else, switch cases)
///
/// ## Memory Management
///
/// In C, blocks create a new scope for variables, meaning variables declared within
/// a block are destroyed when execution leaves the block, unless they're declared with
/// `static` storage duration.
///
/// ## Examples
///
/// ### Function Body
/// ```c
/// int calculate_sum(int a, int b) {
///     int result;      // Variable declaration
///     result = a + b;  // Assignment expression
///     return result;   // Return statement
/// }
/// ```
///
/// ### Conditional Branch
/// ```c
/// if (condition) {
///     perform_action();
///     update_state();
/// }
/// ```
#[derive(Debug, Clone, DisplayFromFormat)]
pub struct Block {
    /// Collection of statements that make up the block's content.
    /// These statements are executed sequentially when the block is entered.
    pub stmts: Vec<Statement>,
}

impl Block {
    /// Creates and returns a new `BlockBuilder` to construct a `Block` using the builder pattern.
    ///
    /// This method is the recommended entry point for creating blocks as it provides
    /// a fluent API for adding statements and other content.
    ///
    /// ## Example
    ///
    /// ```rust
    /// // Create a block with multiple statements
    /// let block = Block::new()
    ///     .statement(Statement::Variable(var_declaration))
    ///     .statement(Statement::Expr(some_expression))
    ///     .statement(Statement::Return(Some(return_value)))
    ///     .build();
    /// ```
    pub fn new() -> BlockBuilder {
        BlockBuilder::new()
    }
}

impl Format for Block {
    fn format(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
        for stmt in &self.stmts {
            stmt.format(fmt)?;
        }

        Ok(())
    }
}

/// A builder for constructing `Block` instances in a fluent, chainable manner.
///
/// The builder pattern provides a convenient way to incrementally construct blocks
/// by adding statements one at a time or in batches. It also provides utility methods
/// for common operations like adding blank lines or merging blocks.
///
/// ## Usage Pattern
///
/// 1. Create a new builder with `BlockBuilder::new()` or `Block::new()`
/// 2. Add statements using the various builder methods
/// 3. Call `build()` to create the final `Block` instance
pub struct BlockBuilder {
    stmts: Vec<Statement>,
}

impl Default for BlockBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl BlockBuilder {
    /// Creates and returns a new `BlockBuilder` instance.
    ///
    /// The new builder starts with an empty collection of statements.
    ///
    /// ## Example
    ///
    /// ```rust
    /// // Create a new block builder
    /// let builder = BlockBuilder::new();
    ///
    /// // Add statements and build the block
    /// let block = builder
    ///     .statement(Statement::Expr(Expr::UInt(42)))
    ///     .statement(Statement::Return(None))
    ///     .build();
    /// ```
    pub fn new() -> Self {
        Self { stmts: vec![] }
    }

    /// Appends a single statement to the block being built.
    ///
    /// This method consumes the builder and returns it, allowing for method chaining.
    ///
    /// ## Parameters
    ///
    /// - `stmt`: The statement to add to the block
    ///
    /// ## Returns
    ///
    /// The builder instance for method chaining
    ///
    /// ## Example
    ///
    /// ```rust
    /// let block = BlockBuilder::new()
    ///     .statement(Statement::Expr(Expr::UInt(1)))
    ///     .statement(Statement::Break)
    ///     .build();
    /// ```
    pub fn statement(mut self, stmt: Statement) -> Self {
        self.stmts.push(stmt);
        self
    }

    /// Replaces all existing statements with the provided collection of statements.
    ///
    /// This method is useful when you want to set multiple statements at once,
    /// potentially overwriting any statements that were previously added.
    ///
    /// ## Parameters
    ///
    /// - `stmts`: A vector of statements to set as the block's content
    ///
    /// ## Returns
    ///
    /// The builder instance for method chaining
    ///
    /// ## Example
    ///
    /// ```rust
    /// let predefined_statements = vec![
    ///     Statement::Variable(var_decl),
    ///     Statement::Expr(expr),
    ///     Statement::Return(Some(return_val))
    /// ];
    ///
    /// let block = BlockBuilder::new()
    ///     .statements(predefined_statements)
    ///     .build();
    /// ```
    pub fn statements(mut self, stmts: Vec<Statement>) -> Self {
        self.stmts = stmts;
        self
    }

    /// Adds an empty line (newline character) to the block.
    ///
    /// This method is useful for formatting purposes, allowing you to add
    /// vertical spacing between statements for improved readability.
    ///
    /// ## Returns
    ///
    /// The builder instance for method chaining
    ///
    /// ## Example
    ///
    /// ```rust
    /// let block = BlockBuilder::new()
    ///     .statement(Statement::Variable(var_decl))
    ///     .new_line()  // Add vertical space for readability
    ///     .statement(Statement::Expr(some_calculation))
    ///     .build();
    /// ```
    pub fn new_line(self) -> Self {
        self.statement(Statement::NewLine)
    }

    /// Merges the statements from another `Block` into this builder.
    /// The statements from the other block are appended after any existing statements.
    ///
    /// This method is useful for combining blocks or for reusing parts of existing blocks.
    ///
    /// ## Parameters
    ///
    /// - `other`: Another block whose statements will be merged into this builder
    ///
    /// ## Returns
    ///
    /// The builder instance for method chaining
    ///
    /// ## Example
    ///
    /// ```rust
    /// // Create a common initialization block
    /// let init_block = Block::new()
    ///     .statement(Statement::Variable(var_init1))
    ///     .statement(Statement::Variable(var_init2))
    ///     .build();
    ///
    /// // Reuse the initialization block in another context
    /// let full_block = BlockBuilder::new()
    ///     .merge(init_block)
    ///     .statement(Statement::Expr(main_logic))
    ///     .statement(Statement::Return(Some(result)))
    ///     .build();
    /// ```
    pub fn merge(mut self, mut other: Block) -> Self {
        self.stmts.append(&mut other.stmts);
        self
    }

    /// Consumes the builder and creates a new `Block` instance.
    ///
    /// This method finalizes the building process and returns the constructed block
    /// containing all the statements that were added to the builder.
    ///
    /// ## Returns
    ///
    /// A new `Block` instance containing the statements added to the builder
    ///
    /// ## Example
    ///
    /// ```rust
    /// // Build a complete function body
    /// let function_body = BlockBuilder::new()
    ///     .statement(Statement::Variable(local_var))
    ///     .statement(Statement::Expr(calculation))
    ///     .statement(Statement::Return(Some(result)))
    ///     .build();
    /// ```
    pub fn build(self) -> Block {
        Block { stmts: self.stmts }
    }
}

/// Represents the various types of statements and preprocessor directives
/// that can appear within a C code block.
///
/// This enum covers the full range of C language constructs, including:
/// - Basic statements (expressions, returns, breaks, continues)
/// - Control flow statements (if, switch, loops)
/// - Variable declarations
/// - Labels and gotos
/// - Preprocessor directives
/// - Comments and raw code insertion
///
/// Each variant corresponds to a specific type of C statement or directive,
/// with associated data where necessary.
#[derive(Debug, Clone, DisplayFromFormat)]
pub enum Statement {
    /// A C-style comment (either line comment `//` or block comment `/* */`)
    Comment(Comment),

    /// A variable declaration statement, which may include initialization
    ///
    /// Example: `int counter = 0;`
    Variable(Variable),

    /// An expression statement, typically ending with a semicolon
    ///
    /// Examples:
    /// - `func();`
    /// - `a = b + c;`
    /// - `counter++;`
    Expr(Expr),

    /// A return statement, optionally with an expression to return
    ///
    /// Examples:
    /// - `return;` (represented as `Return(None)`)
    /// - `return value;` (represented as `Return(Some(expr))`)
    Return(Option<Expr>),

    /// A break statement to exit a loop or switch
    ///
    /// Example: `break;`
    Break,

    /// A continue statement to skip to the next iteration of a loop
    ///
    /// Example: `continue;`
    Continue,

    /// A goto statement that jumps to a labeled statement
    ///
    /// Example: `goto error_handler;`
    GoTo(String),

    /// A label declaration that can be targeted by goto statements
    ///
    /// Example: `some_label:`
    Label(String),

    /// An if statement with optional else and else if branches
    ///
    /// Example: `if (condition) { ... } else if (another) { ... } else { ... }`
    If(If),

    /// A switch statement with multiple case branches
    ///
    /// Example: `switch (value) { case 1: ... break; default: ... }`
    Switch(Switch),

    /// A while loop
    ///
    /// Example: `while (condition) { ... }`
    While(While),

    /// A do-while loop
    ///
    /// Example: `do { ... } while (condition);`
    DoWhile(DoWhile),

    /// A for loop
    ///
    /// Example: `for (int i = 0; i < 10; i++) { ... }`
    For(For),

    /// A `#error` preprocessor directive that causes compilation to fail with a message
    ///
    /// Example: `#error "This platform is not supported"`
    ErrorDirective(ErrorDirective),

    /// A `#ifdef`, `#ifndef`, or `#elifdef` preprocessor directive for conditional compilation
    ///
    /// Example: `#ifdef DEBUG ... #endif`
    IfDefDirective(IfDefDirective),

    /// A `#if`, `#elif`, or `#else` preprocessor directive for conditional compilation
    ///
    /// Example: `#if PLATFORM == WINDOWS ... #else ... #endif`
    IfDirective(IfDirective),

    /// An `#include` directive to include a header file
    ///
    /// Examples:
    /// - `#include <stdio.h>`
    /// - `#include "myheader.h"`
    Include(Include),

    /// A `#line` preprocessor directive to control line numbering in error messages
    ///
    /// Example: `#line 50 "some_file.c"`
    LineDirective(LineDirective),

    /// A macro definition
    ///
    /// Examples:
    /// - `#define MAX_SIZE 100`
    /// - `#define SUM(a, b) ((a) + (b))`
    Macro(Macro),

    /// A `#pragma` directive for implementation-specific behaviors
    ///
    /// Example: `#pragma once`
    PragmaDirective(PragmaDirective),

    /// A `#warning` directive that emits a compiler warning
    ///
    /// Example: `#warning "This code is deprecated!"`
    WarningDirective(WarningDirective),

    /// Raw C code inserted verbatim without processing
    ///
    /// Useful for edge cases not covered by other statement types
    Raw(String),

    /// A compile-time assertion, e.g. `_Static_assert(sizeof(T) == 8, "...")`.
    StaticAssert(StaticAssert),

    /// A statement tagged with a source location.
    ///
    /// When rendered with line directives enabled (see
    /// [`render`](crate::render) with [`RenderOptions::line_directives`](crate::RenderOptions)),
    /// a `#line N "file"` directive is emitted before the inner statement
    /// whenever the location changes. Otherwise it renders as the inner
    /// statement, transparently
    Located {
        /// The originating source location.
        loc: SourceLoc,
        /// The wrapped statement.
        stmt: Box<Statement>,
    },

    /// A standalone newline for formatting purposes
    ///
    /// Adds vertical whitespace between statements for improved readability
    NewLine,
}

impl Statement {
    /// Tags this statement with a source location for `#line` mapping.
    ///
    /// # Examples
    /// ```rust
    /// let s = Statement::Return(None).located(SourceLoc::new("main.abc", 12));
    /// ```
    pub fn located(self, loc: SourceLoc) -> Statement {
        Statement::Located {
            loc,
            stmt: Box::new(self),
        }
    }
}

impl Format for Statement {
    fn format(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
        use Statement::*;
        match self {
            Comment(comment) => comment.format(fmt),
            Variable(variable) => {
                variable.format(fmt)?;
                writeln!(fmt, ";")
            }
            Expr(expr) => {
                expr.format(fmt)?;
                writeln!(fmt, ";")
            }
            Return(None) => writeln!(fmt, "return;"),
            Return(Some(expr)) => {
                write!(fmt, "return ")?;
                expr.format(fmt)?;
                writeln!(fmt, ";")
            }
            Break => writeln!(fmt, "break;"),
            Continue => writeln!(fmt, "continue;"),
            GoTo(s) => writeln!(fmt, "goto {s};"),
            Label(s) => writeln!(fmt, "{s}:"),
            If(i) => i.format(fmt),
            Switch(s) => s.format(fmt),
            While(w) => w.format(fmt),
            DoWhile(w) => w.format(fmt),
            For(f) => f.format(fmt),
            ErrorDirective(e) => e.format(fmt),
            IfDefDirective(i) => i.format(fmt),
            IfDirective(i) => i.format(fmt),
            Include(i) => i.format(fmt),
            LineDirective(l) => l.format(fmt),
            Macro(m) => m.format(fmt),
            PragmaDirective(p) => p.format(fmt),
            WarningDirective(w) => w.format(fmt),
            Raw(s) => writeln!(fmt, "{s}"),
            StaticAssert(sa) => {
                sa.format(fmt)?;
                writeln!(fmt, ";")
            }
            Located { loc, stmt } => {
                fmt.sync_line(loc)?;
                stmt.format(fmt)
            }
            NewLine => writeln!(fmt),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::*;

    #[test]
    fn statement() {
        let mut s = Statement::Comment(Comment::new().comment_with_str("Hello").build());
        assert_eq!(s.to_string(), "// Hello\n");

        let t = Type::new(BaseType::Size)
            .make_const()
            .make_pointer()
            .build();
        s = Statement::Variable(VariableBuilder::new_with_str("abc", t).build());
        assert_eq!(s.to_string(), "const size_t *abc;\n");

        s = Statement::Return(None);
        assert_eq!(s.to_string(), "return;\n");
        s = Statement::Return(Some(Expr::UInt(123)));
        assert_eq!(s.to_string(), "return 123u;\n");

        s = Statement::Break;
        assert_eq!(s.to_string(), "break;\n");

        s = Statement::Continue;
        assert_eq!(s.to_string(), "continue;\n");

        s = Statement::GoTo("some_label".to_string());
        assert_eq!(s.to_string(), "goto some_label;\n");

        s = Statement::Label("some_label".to_string());
        assert_eq!(s.to_string(), "some_label:\n");
    }

    #[test]
    fn blocks() {
        let b1 = Block::new()
            .statement(Statement::Raw("abc;".to_string()))
            .new_line()
            .new_line()
            .statement(Statement::Expr(Expr::FnCall {
                name: Box::new(Expr::new_ident_with_str("some_func")),
                args: vec![],
            }))
            .build();

        assert_eq!(b1.stmts.len(), 4);
        assert_eq!(b1.to_string(), "abc;\n\n\nsome_func();\n");

        let b2 = Block::new()
            .statements(vec![Statement::Raw("something else".to_string())])
            .merge(b1)
            .build();

        assert_eq!(b2.to_string(), "something else\nabc;\n\n\nsome_func();\n");
    }

    #[test]
    fn located_line_directives() {
        let block = Block::new()
            .statement(
                Statement::Expr(Expr::FnCall {
                    name: Box::new(Expr::new_ident_with_str("foo")),
                    args: vec![],
                })
                .located(SourceLoc::new("main.tamago", 10)),
            )
            .statement(Statement::Return(None).located(SourceLoc::new("main.tamago", 11)))
            .build();

        // without the line-directive mode, locations are invisible
        assert_eq!(block.to_string(), "foo();\nreturn;\n");

        // with it on, a #line is emitted whenever the location changes.
        let out = render(
            &block,
            RenderOptions {
                line_directives: true,
                ..Default::default()
            },
        );
        assert_eq!(
            out,
            "#line 10 \"main.tamago\"\nfoo();\n#line 11 \"main.tamago\"\nreturn;\n"
        );
    }
}