From ce5024789574f7a4350e34019c45aebe6ca60d39 Mon Sep 17 00:00:00 2001 From: Selimaj Dev Date: Tue, 5 May 2026 14:53:07 +0200 Subject: [PATCH] Attribute codegen --- src/codegen.rs | 32 +++++++++++++++++++++++++++++--- test/src/main.mist | 8 +++++++- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/codegen.rs b/src/codegen.rs index b6038d5..94df494 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -1,7 +1,7 @@ use parser::ast::{ - BinaryOp, Block, Expression, IfStmt, Literal, Path, Postfix, Prefix, Statement, TopLevel, - TopLevelKind, TypeExpr, TypeExprKind, TypePostfix, VarAssignStmt, VarDecl, VarDeclStmt, - WhileStmt, + Attribute, BinaryOp, Block, Expression, IfStmt, Literal, Path, Postfix, Prefix, Statement, + TopLevel, TopLevelKind, TypeExpr, TypeExprKind, TypePostfix, VarAssignStmt, VarDecl, + VarDeclStmt, WhileStmt, }; // --------------------------------------------------------------------------- @@ -228,10 +228,36 @@ impl ToRust for Block { impl ToRust for TopLevel { fn to_rust(&self, cg: &mut RustCodegen) { + for attr in &self.1 { + cg.addln(&format!("#[{}]", attr.get_rust())); + } + self.0.to_rust(cg); } } +impl GetRust for Attribute { + fn get_rust(&self) -> String { + match self { + Self::Path(path) => path.get_rust(), + Self::NameValue { path, value } => { + format!("{} = {}", path.get_rust(), value.get_rust()) + } + Self::List { path, items } => { + format!( + "{}({})", + path.get_rust(), + items + .iter() + .map(Attribute::get_rust) + .collect::>() + .join(", ") + ) + } + } + } +} + impl ToRust for TopLevelKind { fn to_rust(&self, cg: &mut RustCodegen) { match self { diff --git a/test/src/main.mist b/test/src/main.mist index 0ebece7..a9c3257 100644 --- a/test/src/main.mist +++ b/test/src/main.mist @@ -2,4 +2,10 @@ public struct ApiCall { i32 x, i32 y, -} \ No newline at end of file +} + +void main() { + ApiCall call = ApiCall { x: 10, y: 12 }; + + println!("{call:?}"); +}