From 3342893aabd149419d2593afcbad472e8d45bf40 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Thu, 7 May 2026 21:10:27 +0200 Subject: [PATCH] Enum codegen --- src/codegen.rs | 71 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 65 insertions(+), 6 deletions(-) diff --git a/src/codegen.rs b/src/codegen.rs index 14c9982..ed12620 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -1,6 +1,6 @@ use parser::ast::{ - Attribute, BinaryOp, Block, Expression, FunctionDecl, Identifier, Literal, Path, Postfix, - Prefix, Statement, StatementBranch, TopLevel, TopLevelKind, TypeExpr, TypeExprKind, + Attribute, BinaryOp, Block, EnumItem, Expression, FunctionDecl, Identifier, Literal, Path, + Postfix, Prefix, Statement, StatementBranch, TopLevel, TopLevelKind, TypeExpr, TypeExprKind, TypePostfix, VarAssignStmt, VarDecl, VarDeclStmt, Visibility, }; @@ -311,12 +311,35 @@ impl ToRust for TopLevelKind { Self::Import(path) => cg.addln(&format!("use {};", path.get_rust())), Self::Mod(id) => cg.addln(&format!("mod {};", id.get_rust())), Self::FunctionDecl(decl) => decl.to_rust(cg), + Self::EnumDecl { + visibility, + name, + fields, + } => { + cg.addln(&format!( + "{}struct {} {{", + visibility.get_rust(), + name.get_rust() + )); + cg.indent += 1; + + for field in fields { + cg.add_indentedln(&(format!("{}", field.get_rust()) + ",")); + } + + cg.indent -= 1; + cg.addln("}\n"); + } Self::StructDecl { visibility, name, fields, } => { - cg.addln(&format!("{}struct {} {{", visibility.get_rust(), name.get_rust())); + cg.addln(&format!( + "{}struct {} {{", + visibility.get_rust(), + name.get_rust() + )); cg.indent += 1; for (field_name, _, ty) in &fields.0 { @@ -335,7 +358,11 @@ impl ToRust for TopLevelKind { methods, } => { // Struct decl - cg.addln(&format!("{}struct {} {{", visibility.get_rust(), name.get_rust())); + cg.addln(&format!( + "{}struct {} {{", + visibility.get_rust(), + name.get_rust() + )); cg.indent += 1; for field in fields { @@ -501,7 +528,11 @@ impl ToRust for Statement { iterator, body, } => { - cg.add_indentedln(&format!("for {} in {}", pattern.get_rust(), iterator.get_rust())); + cg.add_indentedln(&format!( + "for {} in {}", + pattern.get_rust(), + iterator.get_rust() + )); cg.ensure_brackets(body); } @@ -556,7 +587,11 @@ impl GetRust for VarDecl { impl GetRust for Path { fn get_rust(&self) -> String { - self.0.iter().map(Identifier::get_rust).collect::>().join("::") + self.0 + .iter() + .map(Identifier::get_rust) + .collect::>() + .join("::") } } @@ -585,6 +620,30 @@ impl GetRust for Identifier { } } +impl GetRust for EnumItem { + fn get_rust(&self) -> String { + match self { + Self::Named(id) => id.get_rust(), + Self::Struct(id, s) => format!( + "{} {{{}}}", + id.get_rust(), + s.0.iter() + .map(|(id, _, ty)| format!("{}: {}", id.get_rust(), ty.get_rust())) + .collect::>() + .join(", ") + ), + Self::Tuple(id, t) => format!( + "{} ({})", + id.get_rust(), + t.iter() + .map(TypeExpr::get_rust) + .collect::>() + .join(", ") + ), + } + } +} + pub fn get_static_type_path(path: &Path) -> String { let rust_path = path.get_rust();