Var decl with repeat

This commit is contained in:
2026-07-04 09:55:23 +02:00
parent eeac773c06
commit 517a2e5a2b
5 changed files with 62 additions and 11 deletions
+22 -1
View File
@@ -188,8 +188,8 @@ impl GenRust for Statement {
impl GenRust for VarDecl { impl GenRust for VarDecl {
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
if self.tuple_names.is_empty() {
self.name.gen_rust(ctx, cg); self.name.gen_rust(ctx, cg);
cg.add( cg.add(
&self &self
.type_ .type_
@@ -197,5 +197,26 @@ impl GenRust for VarDecl {
.map(|t| format!(": {}", t.get_rust())) .map(|t| format!(": {}", t.get_rust()))
.unwrap_or_default(), .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(),
);
}
} }
} }
+2 -1
View File
@@ -44,8 +44,9 @@ pub struct MatchItem(pub Vec<Pattern>, pub Expression);
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub struct VarDecl { pub struct VarDecl {
pub name: Pattern,
pub type_: Option<TypeExpr>, pub type_: Option<TypeExpr>,
pub name: Pattern,
pub tuple_names: Vec<Pattern>,
} }
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
+2 -2
View File
@@ -226,8 +226,8 @@ attributes = {
// DECLARATION HELPERS // DECLARATION HELPERS
// ====================================================== // ======================================================
var_decl = { var_decl = {
("let" ~ pattern) ("let" ~ pattern ~ ("," ~ pattern)*)
| type_expr ~ (!tuple_pattern ~ pattern) | type_expr ~ (!tuple_pattern ~ pattern ~ ("," ~ pattern)*)
} }
field = { field = {
+1
View File
@@ -56,6 +56,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDecl {
.map(TypeExpr::try_from) .map(TypeExpr::try_from)
.transpose()?, .transpose()?,
name: Pattern::try_from(inner.next().unwrap())?, name: Pattern::try_from(inner.next().unwrap())?,
tuple_names: collect_recovered(inner)?,
}) })
} }
+28
View File
@@ -6,6 +6,13 @@ pub void test_let_basic()
assert_eq!(x, 42); 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() pub void test_let_typed()
{ {
i32 x = 42; i32 x = 42;
@@ -18,6 +25,25 @@ pub void test_let_typed()
assert_eq!(f, 3.14); 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() pub void test_let_mut()
{ {
let mut _x = 42; let mut _x = 42;
@@ -79,7 +105,9 @@ pub void test_let_bool()
pub void run_tests() pub void run_tests()
{ {
test_let_basic(); test_let_basic();
test_let_repeat();
test_let_typed(); test_let_typed();
test_let_repeat_typed();
test_let_mut(); test_let_mut();
test_let_tuple_destructure(); test_let_tuple_destructure();
test_let_nested_tuple(); test_let_nested_tuple();