Add tuple type variable decleration

This commit is contained in:
2026-07-04 19:23:10 +02:00
parent 517a2e5a2b
commit df34c72c97
5 changed files with 21 additions and 11 deletions
+11 -9
View File
@@ -190,6 +190,17 @@ 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);
} else {
cg.add("(");
self.name.gen_rust(ctx, cg);
for name in &self.tuple_names {
cg.add(", ");
name.gen_rust(ctx, cg);
}
cg.add(")");
}
if self.true_type {
cg.add(
&self
.type_
@@ -198,15 +209,6 @@ impl GenRust for VarDecl {
.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_
+1
View File
@@ -46,6 +46,7 @@ pub struct MatchItem(pub Vec<Pattern>, pub Expression);
pub struct VarDecl {
pub type_: Option<TypeExpr>,
pub name: Pattern,
pub true_type: bool,
pub tuple_names: Vec<Pattern>,
}
+3 -1
View File
@@ -225,9 +225,11 @@ attributes = {
// ======================================================
// DECLARATION HELPERS
// ======================================================
as_kw = { "as" }
var_decl = {
("let" ~ pattern ~ ("," ~ pattern)*)
| type_expr ~ (!tuple_pattern ~ pattern ~ ("," ~ pattern)*)
| type_expr ~ as_kw? ~ (!tuple_pattern ~ pattern ~ ("," ~ pattern)*)
}
field = {
+2 -1
View File
@@ -2,7 +2,7 @@ use crate::{
Rule,
ast::*,
error::{AstError, collect_recovered},
parser::consume_rule,
parser::{consume_rule, listen_rule},
};
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDeclStmt {
@@ -55,6 +55,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDecl {
type_: consume_rule(&mut inner, Rule::type_expr)
.map(TypeExpr::try_from)
.transpose()?,
true_type: listen_rule(&mut inner, Rule::as_kw),
name: Pattern::try_from(inner.next().unwrap())?,
tuple_names: collect_recovered(inner)?,
})
+4
View File
@@ -42,6 +42,10 @@ pub void test_let_repeat_typed()
f64 f, f1 = (3.14, 5.5);
assert_eq!(f, 3.14);
assert_eq!(f1, 5.5);
(i32, f64) as a, b = (70, 1.4);
assert_eq!(a, 70);
assert_eq!(b, 1.4);
}
pub void test_let_mut()