From c7c11d1bd531dcc57d55954189a78983ad86770b Mon Sep 17 00:00:00 2001 From: rvcas Date: Sat, 1 Oct 2022 21:37:45 -0400 Subject: [PATCH] feat: finish when clauses --- crates/lang/src/lexer.rs | 3 + crates/lang/src/parser.rs | 39 ++++--- crates/lang/src/tests/parser.rs | 192 +++++++++++++++++++++++++++++++- 3 files changed, 215 insertions(+), 19 deletions(-) diff --git a/crates/lang/src/lexer.rs b/crates/lang/src/lexer.rs index a1ed001d..75d2ddba 100644 --- a/crates/lang/src/lexer.rs +++ b/crates/lang/src/lexer.rs @@ -25,6 +25,9 @@ pub fn lexer() -> impl Parser, Error = ParseError> { just("|>").to(Token::Pipe), just(',').to(Token::Comma), just(':').to(Token::Colon), + just('|').to(Token::Vbar), + just("||").to(Token::VbarVbar), + just("&&").to(Token::AmperAmper), )); let grouping = choice(( diff --git a/crates/lang/src/parser.rs b/crates/lang/src/parser.rs index 9a9e664e..245e5c23 100644 --- a/crates/lang/src/parser.rs +++ b/crates/lang/src/parser.rs @@ -236,7 +236,7 @@ pub fn expr_seq_parser() -> impl Parser impl Parser impl Parser { +pub fn expr_parser( + seq_r: Recursive<'_, Token, expr::UntypedExpr, ParseError>, +) -> impl Parser + '_ { recursive(|r| { let string_parser = select! {Token::String {value} => value}.map_with_span(|value, span| { @@ -323,30 +325,38 @@ pub fn expr_parser() -> impl Parser impl Parser 3 - 1 | 2 -> 5 - 3 if a == 3 -> 9 + let b = { + let x = 4 + + x + 5 + } + + when a, b is { + 1, 2 -> 3 + 1 | 4, 5 -> { + let amazing = 5 + + amazing + } + 3 -> 9 _ -> 4 - } + } } "#; let len = code.chars().count(); @@ -445,6 +455,178 @@ fn module() { return_annotation: None, return_type: (), }, + ast::UntypedDefinition::Fn { + arguments: vec![ast::Arg { + arg_name: ast::ArgName::Named { + name: "a".to_string(), + location: Span::new(SrcId::empty(), 866..867), + }, + location: Span::new(SrcId::empty(), 866..872), + annotation: Some(ast::Annotation::Constructor { + location: Span::new(SrcId::empty(), 869..872), + module: None, + name: "Int".to_string(), + arguments: vec![], + },), + tipo: (), + },], + body: expr::UntypedExpr::Sequence { + location: Span::new(SrcId::empty(), 889..1225), + expressions: vec![ + expr::UntypedExpr::Assignment { + location: Span::new(SrcId::empty(), 889..980), + value: Box::new(expr::UntypedExpr::Sequence { + location: Span::new(SrcId::empty(), 915..964), + expressions: vec![ + expr::UntypedExpr::Assignment { + location: Span::new(SrcId::empty(), 915..924), + value: Box::new(expr::UntypedExpr::Int { + location: Span::new(SrcId::empty(), 923..924), + value: "4".to_string(), + }), + pattern: ast::Pattern::Var { + location: Span::new(SrcId::empty(), 919..920), + name: "x".to_string(), + }, + kind: ast::AssignmentKind::Let, + annotation: None, + }, + expr::UntypedExpr::BinOp { + location: Span::new(SrcId::empty(), 959..964), + name: ast::BinOp::AddInt, + left: Box::new(expr::UntypedExpr::Var { + location: Span::new(SrcId::empty(), 959..960), + name: "x".to_string(), + }), + right: Box::new(expr::UntypedExpr::Int { + location: Span::new(SrcId::empty(), 963..964), + value: "5".to_string(), + }), + }, + ], + }), + pattern: ast::Pattern::Var { + location: Span::new(SrcId::empty(), 893..894), + name: "b".to_string(), + }, + kind: ast::AssignmentKind::Let, + annotation: None, + }, + expr::UntypedExpr::When { + location: Span::new(SrcId::empty(), 996..1225), + subjects: vec![ + expr::UntypedExpr::Var { + location: Span::new(SrcId::empty(), 1001..1002), + name: "a".to_string(), + }, + expr::UntypedExpr::Var { + location: Span::new(SrcId::empty(), 1004..1005), + name: "b".to_string(), + }, + ], + clauses: vec![ + ast::Clause { + location: Span::new(SrcId::empty(), 1027..1036), + pattern: vec![ + ast::Pattern::Int { + location: Span::new(SrcId::empty(), 1027..1028), + value: "1".to_string(), + }, + ast::Pattern::Int { + location: Span::new(SrcId::empty(), 1030..1031), + value: "2".to_string(), + }, + ], + alternative_patterns: vec![], + guard: None, + then: expr::UntypedExpr::Int { + location: Span::new(SrcId::empty(), 1035..1036), + value: "3".to_string(), + }, + }, + ast::Clause { + location: Span::new(SrcId::empty(), 1053..1163), + pattern: vec![ast::Pattern::Int { + location: Span::new(SrcId::empty(), 1053..1054), + value: "1".to_string(), + },], + alternative_patterns: vec![vec![ + ast::Pattern::Int { + location: Span::new(SrcId::empty(), 1057..1058), + value: "4".to_string(), + }, + ast::Pattern::Int { + location: Span::new(SrcId::empty(), 1060..1061), + value: "5".to_string(), + }, + ],], + guard: None, + then: expr::UntypedExpr::Sequence { + location: Span::new(SrcId::empty(), 1085..1145), + expressions: vec![ + expr::UntypedExpr::Assignment { + location: Span::new(SrcId::empty(), 1085..1100), + value: Box::new(expr::UntypedExpr::Int { + location: Span::new( + SrcId::empty(), + 1099..1100 + ), + value: "5".to_string(), + }), + pattern: ast::Pattern::Var { + location: Span::new( + SrcId::empty(), + 1089..1096 + ), + name: "amazing".to_string(), + }, + kind: ast::AssignmentKind::Let, + annotation: None, + }, + expr::UntypedExpr::Var { + location: Span::new(SrcId::empty(), 1138..1145), + name: "amazing".to_string(), + }, + ], + }, + }, + ast::Clause { + location: Span::new(SrcId::empty(), 1180..1186), + pattern: vec![ast::Pattern::Int { + location: Span::new(SrcId::empty(), 1180..1181), + value: "3".to_string(), + },], + alternative_patterns: vec![], + guard: None, + then: expr::UntypedExpr::Int { + location: Span::new(SrcId::empty(), 1185..1186), + value: "9".to_string(), + }, + }, + ast::Clause { + location: Span::new(SrcId::empty(), 1203..1209), + pattern: vec![ast::Pattern::Discard { + name: "_".to_string(), + location: Span::new(SrcId::empty(), 1203..1204), + },], + alternative_patterns: vec![], + guard: None, + then: expr::UntypedExpr::Int { + location: Span::new(SrcId::empty(), 1208..1209), + value: "4".to_string(), + }, + }, + ], + }, + ], + }, + doc: None, + location: Span::new(SrcId::empty(), 854..1239), + name: "wow2".to_string(), + public: true, + return_annotation: None, + return_type: (), + }, ] }, );