From 5f0b1fa67ad9e106d00ab1c0f35e3c69820f67c3 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Mon, 15 Jun 2026 14:12:33 +0200 Subject: [PATCH] Working mapgen and fixed indentation --- crates/codegen/src/lib.rs | 17 +++++++---------- crates/codegen/src/statement.rs | 2 ++ crates/codegen/src/top_level.rs | 5 +++-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/crates/codegen/src/lib.rs b/crates/codegen/src/lib.rs index 0abb496..0c8929e 100644 --- a/crates/codegen/src/lib.rs +++ b/crates/codegen/src/lib.rs @@ -41,7 +41,7 @@ impl RustCodegen { output: String::new(), indent: 0, mapping: HashSet::new(), - position: RustMap(0, 0), + position: RustMap(1, 0), } } @@ -50,15 +50,12 @@ impl RustCodegen { } fn add(&mut self, s: &str) { - for c in s.chars() { - match c { - '\n' => { - self.position.0 += 1; - self.position.1 = 0; - } - _ => { - self.position.1 += 1; - } + for b in s.bytes() { + if b == b'\n' { + self.position.0 += 1; + self.position.1 = 0; + } else { + self.position.1 += 1; } } diff --git a/crates/codegen/src/statement.rs b/crates/codegen/src/statement.rs index 4d0229f..f1596ee 100644 --- a/crates/codegen/src/statement.rs +++ b/crates/codegen/src/statement.rs @@ -11,12 +11,14 @@ impl GenRust for Block { for stmt in &self.statements { ctx.expr_ensure_semicolon = true; + cg.add_indented(""); stmt.gen_rust(ctx, cg); cg.addln(""); } if let Some(soft_return) = &self.soft_return { ctx.expr_ensure_semicolon = false; + cg.add_indented(""); soft_return.gen_rust(ctx, cg); cg.addln(""); } diff --git a/crates/codegen/src/top_level.rs b/crates/codegen/src/top_level.rs index 39bfbb8..5bda42f 100644 --- a/crates/codegen/src/top_level.rs +++ b/crates/codegen/src/top_level.rs @@ -73,7 +73,7 @@ impl GenRust for ImplDecl { impl GenRust for FunctionDecl { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { - cg.add(&format!( + cg.add_indented(&format!( "{}fn {}{}(", self.visibility.get_rust(), self.name.get_rust(), @@ -97,8 +97,9 @@ impl GenRust for FunctionDecl { if let Some(body) = &self.body { cg.add(" "); body.gen_rust(ctx, cg); + cg.addln(""); } else { - cg.add(";"); + cg.addln(";"); } } }