Prettier codegen output

This commit is contained in:
2026-05-21 15:14:51 +02:00
parent 4112dcac3b
commit 119f93cf79
4 changed files with 37 additions and 34 deletions
+1
View File
@@ -119,6 +119,7 @@ impl<T: GetRust> GetRust for Spanned<T> {
impl<T: GenRust> GenRust for Spanned<T> { impl<T: GenRust> GenRust for Spanned<T> {
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
cg.add_indentedln(&format!("/* {}:{} */", self.line, self.column)); cg.add_indentedln(&format!("/* {}:{} */", self.line, self.column));
cg.add_indented("");
self.item.gen_rust(ctx, cg); self.item.gen_rust(ctx, cg);
} }
} }
+11 -9
View File
@@ -6,29 +6,29 @@ use crate::{GenRust, GetRust, RustCodegen};
impl GenRust for Block { impl GenRust for Block {
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
cg.addln("{");
cg.indent += 1;
for stmt in &self.0 { for stmt in &self.0 {
ctx.expr_ensure_semicolon = true; ctx.expr_ensure_semicolon = true;
cg.add_indented("");
stmt.gen_rust(ctx, cg); stmt.gen_rust(ctx, cg);
cg.addln("");
} }
if let Some(soft_return) = &self.1 { if let Some(soft_return) = &self.1 {
ctx.expr_ensure_semicolon = false; ctx.expr_ensure_semicolon = false;
soft_return.gen_rust(ctx, cg); soft_return.gen_rust(ctx, cg);
} }
cg.indent -= 1;
cg.add_indentedln("}");
} }
} }
impl GenRust for Statement { impl GenRust for Statement {
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
match self { match self {
Statement::Block(block) => { Statement::Block(block) => block.gen_rust(ctx, cg),
cg.add_indentedln("{");
cg.indent += 1;
block.gen_rust(ctx, cg);
cg.indent -= 1;
cg.add_indentedln("}");
}
Statement::VarDecl(VarDeclStmt { decl, init }) => { Statement::VarDecl(VarDeclStmt { decl, init }) => {
cg.add("let "); cg.add("let ");
@@ -48,6 +48,8 @@ impl GenRust for Statement {
for (pat, body) in match_items { for (pat, body) in match_items {
for (i, p) in pat.iter().enumerate() { for (i, p) in pat.iter().enumerate() {
cg.addln("");
cg.add_indented("");
if i > 0 { if i > 0 {
cg.add(" | "); cg.add(" | ");
} }
@@ -55,7 +57,7 @@ impl GenRust for Statement {
p.gen_rust(ctx, cg); p.gen_rust(ctx, cg);
} }
cg.add("=> {"); cg.add(" => {");
cg.indent += 1; cg.indent += 1;
body.gen_rust(ctx, cg); body.gen_rust(ctx, cg);
+25 -24
View File
@@ -73,7 +73,7 @@ impl GenRust for ImplDecl {
impl GenRust for FunctionDecl { impl GenRust for FunctionDecl {
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
cg.add_indentedln(&format!( cg.add(&format!(
"{}fn {}{}(", "{}fn {}{}(",
self.visibility.get_rust(), self.visibility.get_rust(),
self.name.get_rust(), self.name.get_rust(),
@@ -82,7 +82,7 @@ impl GenRust for FunctionDecl {
for (i, param) in self.params.0.iter().enumerate() { for (i, param) in self.params.0.iter().enumerate() {
if i > 0 { if i > 0 {
cg.add(","); cg.add(", ");
} }
param.gen_rust(ctx, cg); param.gen_rust(ctx, cg);
@@ -92,11 +92,8 @@ impl GenRust for FunctionDecl {
cg.add(&self.return_type.get_rust()); cg.add(&self.return_type.get_rust());
if let Some(body) = &self.body { if let Some(body) = &self.body {
cg.add_indentedln(" {\n"); cg.add(" ");
cg.indent += 1;
body.gen_rust(ctx, cg); body.gen_rust(ctx, cg);
cg.indent -= 1;
cg.add_indentedln("}\n");
} else { } else {
cg.add(";"); cg.add(";");
} }
@@ -170,17 +167,17 @@ impl GenRust for TopLevel {
} }
} }
impl GenRust for (&Vec<Spanned<FieldDeclStmt>>, &ClassConstructor) { impl GenRust for (&Vec<Spanned<FieldDeclStmt>>, &Spanned<ClassConstructor>) {
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
cg.add_indentedln(&format!( cg.add_indented(&format!(
"{}fn new{}(", "{}fn new{}(",
self.1.visibility.get_rust(), self.1.item.visibility.get_rust(),
self.1.generics.get_rust() self.1.item.generics.get_rust()
)); ));
self.1.params.gen_rust(ctx, cg); self.1.item.params.gen_rust(ctx, cg);
cg.add(") -> Self {"); cg.addln(") -> Self {");
cg.indent += 1; cg.indent += 1;
cg.add_indentedln("let mut this: Self = unsafe { std::mem::MaybeUninit::<Self>::zeroed().assume_init() };"); cg.add_indentedln("let mut this: Self = unsafe { std::mem::MaybeUninit::<Self>::zeroed().assume_init() };");
@@ -197,9 +194,9 @@ impl GenRust for (&Vec<Spanned<FieldDeclStmt>>, &ClassConstructor) {
} }
} }
cg.add_indentedln(&format!("this.constructor(")); cg.add_indented(&format!("this.constructor("));
for (i, param) in self.1.params.0.iter().enumerate() { for (i, param) in self.1.item.params.0.iter().enumerate() {
if i > 0 { if i > 0 {
cg.add(", "); cg.add(", ");
} }
@@ -207,7 +204,7 @@ impl GenRust for (&Vec<Spanned<FieldDeclStmt>>, &ClassConstructor) {
param.name.gen_rust(ctx, cg); param.name.gen_rust(ctx, cg);
} }
cg.add(");"); cg.addln(");");
cg.add_indentedln("this"); cg.add_indentedln("this");
@@ -223,15 +220,19 @@ impl GenRust for (&Vec<Spanned<FieldDeclStmt>>, &ClassConstructor) {
)), )),
}]; }];
constructor_params.append(&mut self.1.params.0.clone()); constructor_params.append(&mut self.1.item.params.0.clone());
FunctionDecl { Spanned {
visibility: self.1.visibility.clone(), line: self.1.line,
name: Identifier(String::from("constructor")), column: self.1.column,
generics: self.1.generics.clone(), item: FunctionDecl {
params: ParamList(constructor_params), visibility: self.1.item.visibility.clone(),
return_type: TypeExpr::no_px(TypeExprKind::Tuple(Vec::new())), name: Identifier(String::from("constructor")),
body: Some(self.1.body.clone()), generics: self.1.item.generics.clone(),
params: ParamList(constructor_params),
return_type: TypeExpr::no_px(TypeExprKind::Tuple(Vec::new())),
body: Some(self.1.item.body.clone()),
},
} }
.gen_rust(ctx, cg); .gen_rust(ctx, cg);
} }
@@ -369,7 +370,7 @@ impl GenRust for TopLevelKind {
cg.add_indentedln("#[allow(invalid_value)]"); cg.add_indentedln("#[allow(invalid_value)]");
cg.add_indentedln(&constructor_comment); cg.add_indentedln(&constructor_comment);
(fields, &constructor.item).gen_rust(ctx, cg); (fields, constructor).gen_rust(ctx, cg);
for item in items.clone() { for item in items.clone() {
match item { match item {
-1
View File
@@ -19,7 +19,6 @@ impl fmt::Display for LogLevel {
LogLevel::Warning => write!(f, "[WARN]"); LogLevel::Warning => write!(f, "[WARN]");
LogLevel::Error => write!(f, "[ERROR]"); LogLevel::Error => write!(f, "[ERROR]");
} }
} }
} }