Var decl with repeat
This commit is contained in:
@@ -188,14 +188,35 @@ 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) {
|
||||||
self.name.gen_rust(ctx, cg);
|
if self.tuple_names.is_empty() {
|
||||||
|
self.name.gen_rust(ctx, cg);
|
||||||
|
cg.add(
|
||||||
|
&self
|
||||||
|
.type_
|
||||||
|
.as_ref()
|
||||||
|
.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(", ");
|
||||||
|
|
||||||
cg.add(
|
name.gen_rust(ctx, cg);
|
||||||
&self
|
}
|
||||||
.type_
|
cg.add(")");
|
||||||
.as_ref()
|
|
||||||
.map(|t| format!(": {}", t.get_rust()))
|
cg.add(
|
||||||
.unwrap_or_default(),
|
&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)]
|
#[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)]
|
||||||
|
|||||||
@@ -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 = {
|
||||||
|
|||||||
@@ -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)?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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();
|
||||||
|
|||||||
Reference in New Issue
Block a user