diff --git a/codegen/src/lib.rs b/codegen/src/lib.rs index 103e281..c406d09 100644 --- a/codegen/src/lib.rs +++ b/codegen/src/lib.rs @@ -57,13 +57,17 @@ impl RustCodegen { self.add(&line); } - // pub fn generate(&mut self, toplevels: Vec) -> String { - // for tl in toplevels { - // tl.gen_rust(self); - // } + pub fn generate(&mut self, toplevels: Vec) -> String { + let mut ctx = Context { + expr_ensure_semicolon: true, + }; - // self.output.clone() - // } + for tl in toplevels { + tl.gen_rust(&mut ctx, self); + } + + self.output.clone() + } pub fn ensure_brackets(&mut self, ctx: &mut Context, stmt: &Box) { match &**stmt { @@ -79,6 +83,28 @@ impl RustCodegen { } } +impl GenRust for Attribute { + fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { + match self { + Self::Path(path) => cg.add(&path.get_rust()), + Self::NameValue { path, value } => { + cg.add(&format!("{} =", path.get_rust())); + value.gen_rust(ctx, cg); + } + Self::List { path, items } => { + cg.add(&path.get_rust()); + for (i, item) in items.iter().enumerate() { + if i > 0 { + cg.add(", "); + } + + item.gen_rust(ctx, cg); + } + } + } + } +} + impl GetRust for Spanned { fn get_rust(&self) -> String { format!( diff --git a/codegen/src/top_level.rs b/codegen/src/top_level.rs index 7ceb09e..54bb444 100644 --- a/codegen/src/top_level.rs +++ b/codegen/src/top_level.rs @@ -150,6 +150,26 @@ impl GenRust for ParamList { } } +impl GenRust for TopLevel { + fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { + if let TopLevelKind::ModAttribute = self.0.item { + for attr in &self.1 { + cg.add("#!["); + attr.gen_rust(ctx, cg); + cg.addln("]"); + } + } else { + for attr in &self.1 { + cg.add("#["); + attr.gen_rust(ctx, cg); + cg.addln("]"); + } + } + + self.0.gen_rust(ctx, cg); + } +} + impl GenRust for (&Vec>, &ClassConstructor) { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { cg.add_indentedln(&format!(