⎇ f250fd0765d4b7b91b1bf9f58efa17c429924abf
crates/modules/charka/charka-codegen/src/emit.rs
History
Who touched what
raw
feat(charka): charka-codegen — emisión de Rust desde el IR
1//! `Emitter` — un acumulador de código Rust con control de sangría.
2
3/// Acumula líneas de código Rust generado, llevando la sangría actual.
4pub(crate) struct Emitter {
5 out: String,
6 depth: usize,
7}
8
9impl Emitter {
10 pub(crate) fn new() -> Self {
11 Self {
12 out: String::new(),
13 depth: 0,
14 }
15 }
16
17 /// Escribe una línea con la sangría actual.
18 pub(crate) fn line(&mut self, s: &str) {
19 for _ in 0..self.depth {
20 self.out.push_str(" ");
21 }
22 self.out.push_str(s);
23 self.out.push('\n');
24 }
25
26 /// Una línea en blanco.
27 pub(crate) fn blank(&mut self) {
28 self.out.push('\n');
29 }
30
31 /// Aumenta un nivel de sangría.
32 pub(crate) fn indent(&mut self) {
33 self.depth += 1;
34 }
35
36 /// Reduce un nivel de sangría.
37 pub(crate) fn dedent(&mut self) {
38 self.depth = self.depth.saturating_sub(1);
39 }
40
41 /// Entrega el código acumulado.
42 pub(crate) fn finish(self) -> String {
43 self.out
44 }
45}