Top level

This commit is contained in:
2026-05-21 13:43:42 +02:00
parent 7b94969379
commit 70a1fa332b
2 changed files with 52 additions and 6 deletions
+32 -6
View File
@@ -57,13 +57,17 @@ impl RustCodegen {
self.add(&line);
}
// pub fn generate(&mut self, toplevels: Vec<TopLevel>) -> String {
// for tl in toplevels {
// tl.gen_rust(self);
// }
pub fn generate(&mut self, toplevels: Vec<TopLevel>) -> 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<Statement>) {
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<T: GetRust> GetRust for Spanned<T> {
fn get_rust(&self) -> String {
format!(
+20
View File
@@ -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<Spanned<FieldDeclStmt>>, &ClassConstructor) {
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
cg.add_indentedln(&format!(