Var decl with repeat
This commit is contained in:
@@ -188,8 +188,8 @@ impl GenRust for Statement {
|
||||
|
||||
impl GenRust for VarDecl {
|
||||
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
|
||||
if self.tuple_names.is_empty() {
|
||||
self.name.gen_rust(ctx, cg);
|
||||
|
||||
cg.add(
|
||||
&self
|
||||
.type_
|
||||
@@ -197,5 +197,26 @@ impl GenRust for VarDecl {
|
||||
.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(", ");
|
||||
|
||||
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)?,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,13 @@ pub void test_let_basic()
|
||||
assert_eq!(x, 42);
|
||||
}
|
||||
|
||||
pub void test_let_repeat()
|
||||
{
|
||||
let x, y = (42, 67);
|
||||
assert_eq!(x, 42);
|
||||
assert_eq!(y, 67);
|
||||
}
|
||||
|
||||
pub void test_let_typed()
|
||||
{
|
||||
i32 x = 42;
|
||||
@@ -18,6 +25,25 @@ pub void test_let_typed()
|
||||
assert_eq!(f, 3.14);
|
||||
}
|
||||
|
||||
pub void test_let_repeat_typed()
|
||||
{
|
||||
i32 x, x1 = (42, 67);
|
||||
assert_eq!(x, 42);
|
||||
assert_eq!(x1, 67);
|
||||
|
||||
str& s, s1 = ("hello", "world");
|
||||
assert_eq!(s, "hello");
|
||||
assert_eq!(s1, "world");
|
||||
|
||||
bool b, b1 = (true, false);
|
||||
assert_eq!(b, true);
|
||||
assert_eq!(b1, false);
|
||||
|
||||
f64 f, f1 = (3.14, 5.5);
|
||||
assert_eq!(f, 3.14);
|
||||
assert_eq!(f1, 5.5);
|
||||
}
|
||||
|
||||
pub void test_let_mut()
|
||||
{
|
||||
let mut _x = 42;
|
||||
@@ -79,7 +105,9 @@ pub void test_let_bool()
|
||||
pub void run_tests()
|
||||
{
|
||||
test_let_basic();
|
||||
test_let_repeat();
|
||||
test_let_typed();
|
||||
test_let_repeat_typed();
|
||||
test_let_mut();
|
||||
test_let_tuple_destructure();
|
||||
test_let_nested_tuple();
|
||||
|
||||
Reference in New Issue
Block a user