diff --git a/crates/codegen/src/statement.rs b/crates/codegen/src/statement.rs index cb0f318..a133d0b 100644 --- a/crates/codegen/src/statement.rs +++ b/crates/codegen/src/statement.rs @@ -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_ diff --git a/crates/parser/src/ast/statement.rs b/crates/parser/src/ast/statement.rs index 8dfb7d3..ef77140 100644 --- a/crates/parser/src/ast/statement.rs +++ b/crates/parser/src/ast/statement.rs @@ -46,6 +46,7 @@ pub struct MatchItem(pub Vec, pub Expression); pub struct VarDecl { pub type_: Option, pub name: Pattern, + pub true_type: bool, pub tuple_names: Vec, } diff --git a/crates/parser/src/grammar.pest b/crates/parser/src/grammar.pest index c299570..a727cea 100644 --- a/crates/parser/src/grammar.pest +++ b/crates/parser/src/grammar.pest @@ -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 = { diff --git a/crates/parser/src/parser/common/decl.rs b/crates/parser/src/parser/common/decl.rs index 02d2f7c..2750c6b 100644 --- a/crates/parser/src/parser/common/decl.rs +++ b/crates/parser/src/parser/common/decl.rs @@ -2,7 +2,7 @@ use crate::{ Rule, ast::*, error::{AstError, collect_recovered}, - parser::consume_rule, + parser::{consume_rule, listen_rule}, }; impl<'a> TryFrom> for VarDeclStmt { @@ -55,6 +55,7 @@ impl<'a> TryFrom> 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)?, }) diff --git a/mist-test/src/common/decl_test.mist b/mist-test/src/common/decl_test.mist index 539081d..bbbf735 100644 --- a/mist-test/src/common/decl_test.mist +++ b/mist-test/src/common/decl_test.mist @@ -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()