Lambdas
This commit is contained in:
parent
54b33282ef
commit
8c48f63a2d
@ -16,7 +16,7 @@ peg::parser! {
|
|||||||
rule __ = quiet!{ [' ' | '\t' ]* }
|
rule __ = quiet!{ [' ' | '\t' ]* }
|
||||||
|
|
||||||
pub rule program() -> AST =
|
pub rule program() -> AST =
|
||||||
n:(statement() ** delimiter() ) { AST { id: Default::default(), statements: n.into() } }
|
statements:(statement() ** delimiter() ) { AST { id: Default::default(), statements: statements.into() } }
|
||||||
|
|
||||||
rule delimiter() = (";" / "\n")+
|
rule delimiter() = (";" / "\n")+
|
||||||
|
|
||||||
@ -217,10 +217,18 @@ peg::parser! {
|
|||||||
|
|
||||||
rule primary(struct_ok: bool) -> ExpressionKind =
|
rule primary(struct_ok: bool) -> ExpressionKind =
|
||||||
while_expr() / for_expr() / float_literal() / nat_literal() / bool_literal() / string_literal() / paren_expr() /
|
while_expr() / for_expr() / float_literal() / nat_literal() / bool_literal() / string_literal() / paren_expr() /
|
||||||
list_expr() / if_expr() /
|
list_expr() / if_expr() / lambda_expr() /
|
||||||
item:named_struct() {? if struct_ok { Ok(item) } else { Err("no-struct-allowed") } } /
|
item:named_struct() {? if struct_ok { Ok(item) } else { Err("no-struct-allowed") } } /
|
||||||
identifier_expr()
|
identifier_expr()
|
||||||
|
|
||||||
|
rule lambda_expr() -> ExpressionKind =
|
||||||
|
r#"\"# __ "(" _ params:formal_params() _ ")" _ type_anno:(type_anno()?) _ body:block() {
|
||||||
|
ExpressionKind::Lambda { params, type_anno, body }
|
||||||
|
} /
|
||||||
|
r#"\"# param:formal_param() _ type_anno:(type_anno()?) _ body:block() {
|
||||||
|
ExpressionKind::Lambda { params: vec![param], type_anno, body }
|
||||||
|
}
|
||||||
|
|
||||||
rule for_expr() -> ExpressionKind =
|
rule for_expr() -> ExpressionKind =
|
||||||
"for" _ enumerators:for_enumerators() _ body:for_body() {
|
"for" _ enumerators:for_enumerators() _ body:for_body() {
|
||||||
ExpressionKind::ForExpression { enumerators, body }
|
ExpressionKind::ForExpression { enumerators, body }
|
||||||
|
@ -412,7 +412,7 @@ fn for_expression() {
|
|||||||
fn lambda_expressions() {
|
fn lambda_expressions() {
|
||||||
use ExpressionKind::*;
|
use ExpressionKind::*;
|
||||||
|
|
||||||
assert_expr!(
|
assert_expr2!(
|
||||||
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 }],
|
||||||
@ -423,7 +423,7 @@ fn lambda_expressions() {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_expr!(
|
assert_expr2!(
|
||||||
r#"\ (x: Int, y) { a;b;c;}"#,
|
r#"\ (x: Int, y) { a;b;c;}"#,
|
||||||
expr(Lambda {
|
expr(Lambda {
|
||||||
params: vec![
|
params: vec![
|
||||||
@ -440,7 +440,7 @@ fn lambda_expressions() {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_expr!(
|
assert_expr2!(
|
||||||
r#"\(x){y}(1)"#,
|
r#"\(x){y}(1)"#,
|
||||||
expr(Call {
|
expr(Call {
|
||||||
f: bx(expr(Lambda {
|
f: bx(expr(Lambda {
|
||||||
@ -452,7 +452,7 @@ fn lambda_expressions() {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_expr!(
|
assert_expr2!(
|
||||||
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 },],
|
||||||
@ -469,7 +469,7 @@ fn lambda_expressions() {
|
|||||||
fn single_param_lambda() {
|
fn single_param_lambda() {
|
||||||
use ExpressionKind::*;
|
use ExpressionKind::*;
|
||||||
|
|
||||||
assert_expr!(
|
assert_expr2!(
|
||||||
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 },],
|
||||||
@ -483,7 +483,7 @@ fn single_param_lambda() {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_expr!(
|
assert_expr2!(
|
||||||
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 },],
|
||||||
@ -502,8 +502,9 @@ fn single_param_lambda() {
|
|||||||
fn complex_lambdas() {
|
fn complex_lambdas() {
|
||||||
use ExpressionKind::*;
|
use ExpressionKind::*;
|
||||||
|
|
||||||
assert_ast! {
|
//TODO support this without the semicolon after the lambda
|
||||||
r#"fn wahoo() { let a = 10; \(x) { x + a } };
|
assert_ast2! {
|
||||||
|
r#"fn wahoo() { let a = 10; \(x) { x + a }; }
|
||||||
wahoo()(3) "#,
|
wahoo()(3) "#,
|
||||||
vec![
|
vec![
|
||||||
fn_decl(Signature { name: rc("wahoo"), operator: false, type_anno: None, params: vec![] },
|
fn_decl(Signature { name: rc("wahoo"), operator: false, type_anno: None, params: vec![] },
|
||||||
|
Loading…
Reference in New Issue
Block a user