From 092dc941176c5083006509bd5986055cf77983e3 Mon Sep 17 00:00:00 2001 From: LunarAkai Date: Thu, 7 Aug 2025 03:10:20 +0200 Subject: [PATCH] parser --- .../abstract_syntax_tree/parser.rs | 56 +++++++++++++++++-- 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/src/language_frontend/abstract_syntax_tree/parser.rs b/src/language_frontend/abstract_syntax_tree/parser.rs index 4d92b3d..aabcdf1 100644 --- a/src/language_frontend/abstract_syntax_tree/parser.rs +++ b/src/language_frontend/abstract_syntax_tree/parser.rs @@ -1,5 +1,5 @@ use chumsky::{ - combinator::Or, error::Rich, extra, input::ValueInput, prelude::{choice, just, recursive}, recursive, select, select_ref, span::SimpleSpan, text::{self, ascii::ident, whitespace}, IterParser, Parser + combinator::Or, error::Rich, extra, input::ValueInput, prelude::{choice, just, recursive}, primitive::select, recursive, select, select_ref, span::SimpleSpan, text::{self, ascii::{ident, keyword}, whitespace}, Boxed, IterParser, Parser }; use crate::{language_frontend::abstract_syntax_tree::ast::Expression, language_frontend::lexer::tokens::Token}; @@ -7,17 +7,32 @@ use crate::{language_frontend::abstract_syntax_tree::ast::Expression, language_f // goal of parsing is to construct an abstract syntax tree #[allow(clippy::let_and_return)] -pub fn parser<'tokens, 'src: 'tokens, I>() -> impl Parser<'tokens, I, Expression<'src>, extra::Err>>> +pub fn parser<'tokens, 'src: 'tokens, I>() + -> impl Parser<'tokens, I, Expression<'src>, extra::Err>>> where I: ValueInput<'tokens, Token = Token<'src>, Span = SimpleSpan>, { + let ident = select! { + Token::Ident(s) => s, + }; + + + let keyword = |kw: &'static str| { + select! { + Token::Keyword(k) if k == kw => (), + } + }; + let eq = just(Token::Equals).labelled("="); + let expr = recursive(|expr| { - + + let atom = select! { Token::Float(x) => Expression::Float(x), Token::Integer(x) => Expression::Integer(x), - }; + }.or(expr.clone().delimited_by(just(Token::ParenBegin), just(Token::ParenEnd))); + let unary = just(Token::Substract) .repeated() @@ -40,8 +55,37 @@ where _ => unreachable!(), }, ); - + add_sub }); - expr + + let decl = recursive(|decl| { + let var = keyword("var") + .ignore_then(ident) + .then_ignore(eq.clone()) + .then(expr.clone()) + .then(decl.clone()) + .map(|((name, rhs), then)| Expression::Var { + name, + rhs: Box::new(rhs), + then: Box::new(then), + }); + + let fun = keyword("fun") + .ignore_then(ident.clone()) + .then(ident.repeated().collect::>()) + .then_ignore(eq.clone()) + .then(expr.clone()) + .then(decl) + .map(|(((name, args), body), then)| Expression::Function { + name, + args, + body: Box::new(body), + then: Box::new(then), + }); + + var.or(fun).or(expr) + }); + + decl }