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) { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
if self.tuple_names.is_empty() { if self.tuple_names.is_empty() {
self.name.gen_rust(ctx, cg); 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( cg.add(
&self &self
.type_ .type_
@@ -198,15 +209,6 @@ impl GenRust for VarDecl {
.unwrap_or_default(), .unwrap_or_default(),
); );
} else { } 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( cg.add(
&self &self
.type_ .type_
+1
View File
@@ -46,6 +46,7 @@ pub struct MatchItem(pub Vec<Pattern>, pub Expression);
pub struct VarDecl { pub struct VarDecl {
pub type_: Option<TypeExpr>, pub type_: Option<TypeExpr>,
pub name: Pattern, pub name: Pattern,
pub true_type: bool,
pub tuple_names: Vec<Pattern>, pub tuple_names: Vec<Pattern>,
} }
+3 -1
View File
@@ -225,9 +225,11 @@ attributes = {
// ====================================================== // ======================================================
// DECLARATION HELPERS // DECLARATION HELPERS
// ====================================================== // ======================================================
as_kw = { "as" }
var_decl = { var_decl = {
("let" ~ pattern ~ ("," ~ pattern)*) ("let" ~ pattern ~ ("," ~ pattern)*)
| type_expr ~ (!tuple_pattern ~ pattern ~ ("," ~ pattern)*) | type_expr ~ as_kw? ~ (!tuple_pattern ~ pattern ~ ("," ~ pattern)*)
} }
field = { field = {
+2 -1
View File
@@ -2,7 +2,7 @@ use crate::{
Rule, Rule,
ast::*, ast::*,
error::{AstError, collect_recovered}, error::{AstError, collect_recovered},
parser::consume_rule, parser::{consume_rule, listen_rule},
}; };
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDeclStmt { 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) type_: consume_rule(&mut inner, Rule::type_expr)
.map(TypeExpr::try_from) .map(TypeExpr::try_from)
.transpose()?, .transpose()?,
true_type: listen_rule(&mut inner, Rule::as_kw),
name: Pattern::try_from(inner.next().unwrap())?, name: Pattern::try_from(inner.next().unwrap())?,
tuple_names: collect_recovered(inner)?, 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); f64 f, f1 = (3.14, 5.5);
assert_eq!(f, 3.14); assert_eq!(f, 3.14);
assert_eq!(f1, 5.5); 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() pub void test_let_mut()