Var decl with repeat
This commit is contained in:
@@ -188,14 +188,35 @@ impl GenRust for Statement {
|
||||
|
||||
impl GenRust for VarDecl {
|
||||
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
|
||||
self.name.gen_rust(ctx, cg);
|
||||
if self.tuple_names.is_empty() {
|
||||
self.name.gen_rust(ctx, cg);
|
||||
cg.add(
|
||||
&self
|
||||
.type_
|
||||
.as_ref()
|
||||
.map(|t| format!(": {}", t.get_rust()))
|
||||
.unwrap_or_default(),
|
||||
);
|
||||
} else {
|
||||
cg.add("(");
|
||||
self.name.gen_rust(ctx, cg);
|
||||
for name in &self.tuple_names {
|
||||
cg.add(", ");
|
||||
|
||||
cg.add(
|
||||
&self
|
||||
.type_
|
||||
.as_ref()
|
||||
.map(|t| format!(": {}", t.get_rust()))
|
||||
.unwrap_or_default(),
|
||||
);
|
||||
name.gen_rust(ctx, cg);
|
||||
}
|
||||
cg.add(")");
|
||||
|
||||
cg.add(
|
||||
&self
|
||||
.type_
|
||||
.as_ref()
|
||||
.map(|t| {
|
||||
let t = vec![t.get_rust(); self.tuple_names.len() + 1];
|
||||
format!(": ({})", t.join(", "))
|
||||
})
|
||||
.unwrap_or_default(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,8 +44,9 @@ pub struct MatchItem(pub Vec<Pattern>, pub Expression);
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct VarDecl {
|
||||
pub name: Pattern,
|
||||
pub type_: Option<TypeExpr>,
|
||||
pub name: Pattern,
|
||||
pub tuple_names: Vec<Pattern>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
|
||||
@@ -226,8 +226,8 @@ attributes = {
|
||||
// DECLARATION HELPERS
|
||||
// ======================================================
|
||||
var_decl = {
|
||||
("let" ~ pattern)
|
||||
| type_expr ~ (!tuple_pattern ~ pattern)
|
||||
("let" ~ pattern ~ ("," ~ pattern)*)
|
||||
| type_expr ~ (!tuple_pattern ~ pattern ~ ("," ~ pattern)*)
|
||||
}
|
||||
|
||||
field = {
|
||||
|
||||
@@ -56,6 +56,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDecl {
|
||||
.map(TypeExpr::try_from)
|
||||
.transpose()?,
|
||||
name: Pattern::try_from(inner.next().unwrap())?,
|
||||
tuple_names: collect_recovered(inner)?,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user