Lambdas
This commit is contained in:
parent
721a499384
commit
88d2571401
@ -454,13 +454,13 @@ fn primary_expr(allow_struct: bool) -> impl FnMut(Span) -> ParseResult<Expressio
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn primary_expr_no_struct(input: Span) -> ParseResult<ExpressionKind> {
|
fn primary_expr_no_struct(input: Span) -> ParseResult<ExpressionKind> {
|
||||||
println!("PRIM EXPR: {}", input.fragment());
|
|
||||||
context(
|
context(
|
||||||
"primary-expr-no-struct",
|
"primary-expr-no-struct",
|
||||||
alt((
|
alt((
|
||||||
while_expr,
|
while_expr,
|
||||||
for_expr,
|
for_expr,
|
||||||
if_expr,
|
if_expr,
|
||||||
|
lambda_expr,
|
||||||
list_expr,
|
list_expr,
|
||||||
paren_expr,
|
paren_expr,
|
||||||
string_literal,
|
string_literal,
|
||||||
@ -487,6 +487,15 @@ fn record_block(input: Span) -> ParseResult<Vec<(Rc<String>, Expression)>> {
|
|||||||
delimited(tok(char('{')), separated_list0(tok(char(',')), record_entry), tok(char('}')))(input)
|
delimited(tok(char('{')), separated_list0(tok(char(',')), record_entry), tok(char('}')))(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn lambda_expr(input: Span) -> ParseResult<ExpressionKind> {
|
||||||
|
alt((
|
||||||
|
map(preceded(tok(char('\\')), tuple((formal_params, opt(type_anno), block))),
|
||||||
|
|(params, type_anno, body)| ExpressionKind::Lambda { params, type_anno, body }),
|
||||||
|
map(preceded(tok(char('\\')), tuple((formal_param, opt(type_anno), block))),
|
||||||
|
|(param, type_anno, body)| ExpressionKind::Lambda { params: vec![param], type_anno, body }),
|
||||||
|
))(input)
|
||||||
|
}
|
||||||
|
|
||||||
fn while_expr(input: Span) -> ParseResult<ExpressionKind> {
|
fn while_expr(input: Span) -> ParseResult<ExpressionKind> {
|
||||||
let id = fresh_id(&input);
|
let id = fresh_id(&input);
|
||||||
map(preceded(kw("while"), pair(opt(expression_no_struct), block)), move |(condition, body)| {
|
map(preceded(kw("while"), pair(opt(expression_no_struct), block)), move |(condition, body)| {
|
||||||
|
@ -416,7 +416,7 @@ fn for_expression() {
|
|||||||
fn lambda_expressions() {
|
fn lambda_expressions() {
|
||||||
use ExpressionKind::*;
|
use ExpressionKind::*;
|
||||||
|
|
||||||
assert_expr!(
|
assert_expr_comb!(
|
||||||
r#"\(x) { x + 1}"#,
|
r#"\(x) { x + 1}"#,
|
||||||
expr(Lambda {
|
expr(Lambda {
|
||||||
params: vec![FormalParam { name: rc!(x), anno: None, default: None }],
|
params: vec![FormalParam { name: rc!(x), anno: None, default: None }],
|
||||||
@ -427,7 +427,7 @@ fn lambda_expressions() {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_expr!(
|
assert_expr_comb!(
|
||||||
r#"\ (x: Int, y) { a;b;c;}"#,
|
r#"\ (x: Int, y) { a;b;c;}"#,
|
||||||
expr(Lambda {
|
expr(Lambda {
|
||||||
params: vec![
|
params: vec![
|
||||||
@ -444,7 +444,7 @@ fn lambda_expressions() {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_expr!(
|
assert_expr_comb!(
|
||||||
r#"\(x){y}(1)"#,
|
r#"\(x){y}(1)"#,
|
||||||
expr(Call {
|
expr(Call {
|
||||||
f: bx(expr(Lambda {
|
f: bx(expr(Lambda {
|
||||||
@ -456,7 +456,7 @@ fn lambda_expressions() {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_expr!(
|
assert_expr_comb!(
|
||||||
r#"\(x: Int): String { "q" }"#,
|
r#"\(x: Int): String { "q" }"#,
|
||||||
expr(Lambda {
|
expr(Lambda {
|
||||||
params: vec![FormalParam { name: rc!(x), anno: Some(ty_simple("Int")), default: None },],
|
params: vec![FormalParam { name: rc!(x), anno: Some(ty_simple("Int")), default: None },],
|
||||||
@ -473,7 +473,7 @@ fn lambda_expressions() {
|
|||||||
fn single_param_lambda() {
|
fn single_param_lambda() {
|
||||||
use ExpressionKind::*;
|
use ExpressionKind::*;
|
||||||
|
|
||||||
assert_expr!(
|
assert_expr_comb!(
|
||||||
r#"\x { x + 10 }"#,
|
r#"\x { x + 10 }"#,
|
||||||
expr(Lambda {
|
expr(Lambda {
|
||||||
params: vec![FormalParam { name: rc!(x), anno: None, default: None },],
|
params: vec![FormalParam { name: rc!(x), anno: None, default: None },],
|
||||||
@ -487,7 +487,7 @@ fn single_param_lambda() {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_expr!(
|
assert_expr_comb!(
|
||||||
r#"\x: Int { x + 10 }"#,
|
r#"\x: Int { x + 10 }"#,
|
||||||
expr(Lambda {
|
expr(Lambda {
|
||||||
params: vec![FormalParam { name: rc!(x), anno: Some(ty_simple("Int")), default: None },],
|
params: vec![FormalParam { name: rc!(x), anno: Some(ty_simple("Int")), default: None },],
|
||||||
@ -507,7 +507,7 @@ fn complex_lambdas() {
|
|||||||
use ExpressionKind::*;
|
use ExpressionKind::*;
|
||||||
|
|
||||||
//TODO support this without the semicolon after the lambda
|
//TODO support this without the semicolon after the lambda
|
||||||
assert_ast! {
|
assert_ast_comb! {
|
||||||
r#"fn wahoo() { let a = 10; \(x) { x + a }; }
|
r#"fn wahoo() { let a = 10; \(x) { x + a }; }
|
||||||
wahoo()(3) "#,
|
wahoo()(3) "#,
|
||||||
vec![
|
vec![
|
||||||
|
Loading…
Reference in New Issue
Block a user