This commit is contained in:
Greg Shuflin 2021-11-11 02:42:14 -08:00
parent 54b33282ef
commit 8c48f63a2d
2 changed files with 19 additions and 10 deletions

View File

@ -16,7 +16,7 @@ peg::parser! {
rule __ = quiet!{ [' ' | '\t' ]* }
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")+
@ -217,10 +217,18 @@ peg::parser! {
rule primary(struct_ok: bool) -> ExpressionKind =
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") } } /
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 =
"for" _ enumerators:for_enumerators() _ body:for_body() {
ExpressionKind::ForExpression { enumerators, body }

View File

@ -412,7 +412,7 @@ fn for_expression() {
fn lambda_expressions() {
use ExpressionKind::*;
assert_expr!(
assert_expr2!(
r#"\(x) { x + 1}"#,
expr(Lambda {
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;}"#,
expr(Lambda {
params: vec![
@ -440,7 +440,7 @@ fn lambda_expressions() {
})
);
assert_expr!(
assert_expr2!(
r#"\(x){y}(1)"#,
expr(Call {
f: bx(expr(Lambda {
@ -452,7 +452,7 @@ fn lambda_expressions() {
})
);
assert_expr!(
assert_expr2!(
r#"\(x: Int): String { "q" }"#,
expr(Lambda {
params: vec![FormalParam { name: rc!(x), anno: Some(ty_simple("Int")), default: None },],
@ -469,7 +469,7 @@ fn lambda_expressions() {
fn single_param_lambda() {
use ExpressionKind::*;
assert_expr!(
assert_expr2!(
r#"\x { x + 10 }"#,
expr(Lambda {
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 }"#,
expr(Lambda {
params: vec![FormalParam { name: rc!(x), anno: Some(ty_simple("Int")), default: None },],
@ -502,8 +502,9 @@ fn single_param_lambda() {
fn complex_lambdas() {
use ExpressionKind::*;
assert_ast! {
r#"fn wahoo() { let a = 10; \(x) { x + a } };
//TODO support this without the semicolon after the lambda
assert_ast2! {
r#"fn wahoo() { let a = 10; \(x) { x + a }; }
wahoo()(3) "#,
vec![
fn_decl(Signature { name: rc("wahoo"), operator: false, type_anno: None, params: vec![] },