From 60359ec9b0628404a88f458f4eea4000c1d74401 Mon Sep 17 00:00:00 2001 From: Kasey White Date: Wed, 28 Sep 2022 00:49:12 -0400 Subject: [PATCH] add pipeline and logical ops to expr parsing Co-authored-by: rvcas --- crates/lang/src/parser.rs | 125 +++++++++++++++++++++++++++++++++----- 1 file changed, 110 insertions(+), 15 deletions(-) diff --git a/crates/lang/src/parser.rs b/crates/lang/src/parser.rs index be7be489..5b968426 100644 --- a/crates/lang/src/parser.rs +++ b/crates/lang/src/parser.rs @@ -1,4 +1,5 @@ use chumsky::prelude::*; +use vec1::Vec1; use crate::{ ast::{self, BinOp, TodoKind}, @@ -175,9 +176,7 @@ pub fn fn_parser() -> impl Parser impl Parser impl Parser { recursive(|_r| { + // Product let op = choice(( just(Token::Star).to(BinOp::MultInt), just(Token::Slash).to(BinOp::DivInt), @@ -277,12 +277,13 @@ pub fn expr_parser() -> impl Parser impl Parser List(Int) { - let wow = - [1, 2, 3] - |> list.map(fn(x) { x + a }) - |> list.filter(fn(x: Int) -> Bool { x % 2 == 0 }) - let who = - wow |> list.map(fn(x) { x + a }) - - who + pub fn thing(thing a: Int) { + a + 2 + |> add_one + |> add_one } - } "#; let len = code.chars().count(); @@ -817,6 +865,53 @@ mod tests { return_annotation: None, return_type: (), }, + ast::UntypedDefinition::Fn { + arguments: vec![ast::Arg { + arg_name: ast::ArgName::NamedLabeled { + name: "a".to_string(), + label: "thing".to_string(), + location: Span::new(SrcId::empty(), 487..494), + }, + location: Span::new(SrcId::empty(), 487..499), + annotation: Some(ast::Annotation::Constructor { + location: Span::new(SrcId::empty(), 496..499), + module: None, + name: "Int".to_string(), + arguments: vec![], + },), + tipo: (), + },], + body: expr::UntypedExpr::PipeLine { + expressions: vec1::vec1![ + expr::UntypedExpr::BinOp { + location: Span::new(SrcId::empty(), 519..524), + name: ast::BinOp::AddInt, + left: Box::new(expr::UntypedExpr::Var { + location: Span::new(SrcId::empty(), 519..520), + name: "a".to_string(), + }), + right: Box::new(expr::UntypedExpr::Int { + location: Span::new(SrcId::empty(), 523..524), + value: "2".to_string(), + }), + }, + expr::UntypedExpr::Var { + location: Span::new(SrcId::empty(), 544..551), + name: "add_one".to_string(), + }, + expr::UntypedExpr::Var { + location: Span::new(SrcId::empty(), 571..578), + name: "add_one".to_string(), + }, + ], + }, + doc: None, + location: Span::new(SrcId::empty(), 474..592), + name: "thing".to_string(), + public: true, + return_annotation: None, + return_type: (), + }, ] }, );