Top level
This commit is contained in:
+32
-6
@@ -57,13 +57,17 @@ impl RustCodegen {
|
|||||||
self.add(&line);
|
self.add(&line);
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub fn generate(&mut self, toplevels: Vec<TopLevel>) -> String {
|
pub fn generate(&mut self, toplevels: Vec<TopLevel>) -> String {
|
||||||
// for tl in toplevels {
|
let mut ctx = Context {
|
||||||
// tl.gen_rust(self);
|
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>) {
|
pub fn ensure_brackets(&mut self, ctx: &mut Context, stmt: &Box<Statement>) {
|
||||||
match &**stmt {
|
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> {
|
impl<T: GetRust> GetRust for Spanned<T> {
|
||||||
fn get_rust(&self) -> String {
|
fn get_rust(&self) -> String {
|
||||||
format!(
|
format!(
|
||||||
|
|||||||
@@ -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) {
|
impl GenRust for (&Vec<Spanned<FieldDeclStmt>>, &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_indentedln(&format!(
|
||||||
|
|||||||
Reference in New Issue
Block a user