Merge pull request #1126 from aiken-lang/capture_constructors
Allow captures within constructor calls.
This commit is contained in:
commit
38252ef449
|
@ -6,6 +6,7 @@
|
|||
|
||||
- **aiken**: New `-S` flag on `check` and `build` that blocks the printing of warnings but it still shows the total warning count. @rvcas
|
||||
- **aiken-lang**: Allow types to be used as namespaces for constructors. Importing each constructor variants independently is no longer required in neither pattern-matches nor value construction. One can simply use the type name as a prefix/namespace now. @KtorZ
|
||||
- **aiken-lang**: Allow capture on constructor calls. @KtorZ
|
||||
|
||||
### Changed
|
||||
|
||||
|
|
|
@ -29,24 +29,16 @@ pub fn parser(
|
|||
.then_ignore(just(Token::Colon))
|
||||
.then(choice((
|
||||
r.clone(),
|
||||
select! {Token::DiscardName {name} => name }.validate(
|
||||
|_name, span, emit| {
|
||||
emit(ParseError::expected_input_found(
|
||||
span,
|
||||
None,
|
||||
Some(error::Pattern::Discard),
|
||||
));
|
||||
|
||||
UntypedExpr::Var {
|
||||
choice((select! {Token::DiscardName {name} => name }.map_with_span(
|
||||
|name, span| UntypedExpr::Var {
|
||||
location: span,
|
||||
name: ast::CAPTURE_VARIABLE.to_string(),
|
||||
}
|
||||
name,
|
||||
},
|
||||
),
|
||||
),)),
|
||||
)))
|
||||
.map_with_span(|(label, value), span| ast::CallArg {
|
||||
location: span,
|
||||
value,
|
||||
value: Some(value),
|
||||
label: Some(label),
|
||||
}),
|
||||
choice((
|
||||
|
@ -111,7 +103,7 @@ pub fn parser(
|
|||
)
|
||||
.map(|(value, name)| ast::CallArg {
|
||||
location: value.location(),
|
||||
value,
|
||||
value: Some(value),
|
||||
label: Some(name),
|
||||
}),
|
||||
))
|
||||
|
@ -144,24 +136,16 @@ pub fn parser(
|
|||
.or_not()
|
||||
.then(choice((
|
||||
r.clone(),
|
||||
select! {Token::DiscardName {name} => name }.validate(
|
||||
|_name, span, emit| {
|
||||
emit(ParseError::expected_input_found(
|
||||
span,
|
||||
None,
|
||||
Some(error::Pattern::Discard),
|
||||
));
|
||||
|
||||
select! {Token::DiscardName {name} => name }.map_with_span(|name, span| {
|
||||
UntypedExpr::Var {
|
||||
location: span,
|
||||
name: ast::CAPTURE_VARIABLE.to_string(),
|
||||
name,
|
||||
}
|
||||
},
|
||||
),
|
||||
}),
|
||||
)))
|
||||
.map(|(_label, value)| ast::CallArg {
|
||||
location: value.location(),
|
||||
value,
|
||||
value: Some(value),
|
||||
label: None,
|
||||
})
|
||||
.separated_by(just(Token::Comma))
|
||||
|
@ -208,11 +192,7 @@ pub fn parser(
|
|||
},
|
||||
};
|
||||
|
||||
UntypedExpr::Call {
|
||||
arguments,
|
||||
fun: Box::new(fun),
|
||||
location: span,
|
||||
}
|
||||
fun.call(arguments, span)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
name = "aiken-lang/121"
|
||||
version = "0.0.0"
|
||||
license = "Apache-2.0"
|
||||
description = "Aiken contracts for project 'aiken-lang/121'"
|
||||
|
||||
[repository]
|
||||
user = "aiken-lang"
|
||||
project = "121"
|
||||
platform = "github"
|
|
@ -0,0 +1,34 @@
|
|||
pub type Foo {
|
||||
i: Int,
|
||||
b: Bool,
|
||||
}
|
||||
|
||||
const give_i: fn(Int) -> Foo = Foo { i: _, b: True }
|
||||
|
||||
const give_b: fn(Bool) -> Foo = Foo(1337, _)
|
||||
|
||||
fn foo_i(i: Int) -> fn(Bool) -> Foo {
|
||||
Foo { i: i, b: _bool }
|
||||
}
|
||||
|
||||
fn foo_b(b: Bool) -> fn(Int) -> Foo {
|
||||
Foo(_, b)
|
||||
}
|
||||
|
||||
test test_1() {
|
||||
let bar = foo_i(14)
|
||||
and {
|
||||
foo_i(42)(True) == Foo(42, True),
|
||||
bar(False) == Foo { i: 14, b: False },
|
||||
give_i(1337) == Foo { i: 1337, b: True },
|
||||
}
|
||||
}
|
||||
|
||||
test test_2() {
|
||||
let bar = foo_b(False)
|
||||
and {
|
||||
foo_b(True)(42) == Foo(42, True),
|
||||
bar(14) == Foo { i: 14, b: False },
|
||||
give_b(False) == Foo { i: 1337, b: False },
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue