From 92db6f7f6e069c8a651ae1d93d2489dfa02edd1f Mon Sep 17 00:00:00 2001 From: LunarAkai Date: Fri, 8 Aug 2025 21:10:35 +0200 Subject: [PATCH] . --- .../abstract_syntax_tree/ast.rs | 8 ++--- .../abstract_syntax_tree/definitions.rs | 35 +++++++++++++++---- .../abstract_syntax_tree/parser.rs | 18 +++++++++- src/main.rs | 20 ++--------- 4 files changed, 51 insertions(+), 30 deletions(-) diff --git a/src/language_frontend/abstract_syntax_tree/ast.rs b/src/language_frontend/abstract_syntax_tree/ast.rs index 74463da..b0569ed 100644 --- a/src/language_frontend/abstract_syntax_tree/ast.rs +++ b/src/language_frontend/abstract_syntax_tree/ast.rs @@ -22,10 +22,7 @@ pub enum Expr { Err, // todo - Call { - callee: Box, - arguments: Vec, - }, + Call(Call), Unary { operator: UnaryOp, @@ -42,5 +39,8 @@ pub enum Expr { target: Box, value: Box, }, + + Function(Function), + Error, } \ No newline at end of file diff --git a/src/language_frontend/abstract_syntax_tree/definitions.rs b/src/language_frontend/abstract_syntax_tree/definitions.rs index 21e8861..8e4a3d0 100644 --- a/src/language_frontend/abstract_syntax_tree/definitions.rs +++ b/src/language_frontend/abstract_syntax_tree/definitions.rs @@ -50,8 +50,8 @@ pub enum Literal { #[derive(Clone, Debug, PartialEq)] pub enum Type { - UnsignedInteger, - SignedInteger, + Integer, + Float, Bool, Char, String, @@ -59,8 +59,8 @@ pub enum Type { #[derive(Clone, Debug, PartialEq)] pub enum Value { - UnsignedInteger(u32), - SignedInteger(i32), + Integer(i64), + Float(f64), Bool(bool), Char(char), String(String), @@ -71,8 +71,8 @@ impl Value { match (ty, self) { (Type::Bool, Value::Bool(_)) => true, (Type::Char, Value::Char(_)) => true, - (Type::SignedInteger, Value::SignedInteger(_)) => true, - (Type::UnsignedInteger, Value::UnsignedInteger(_)) => true, + (Type::Integer, Value::Integer(_)) => true, + (Type::Float, Value::Float(_)) => true, (Type::String, Value::String(_)) => true, _ => false, } @@ -86,7 +86,6 @@ impl Value { #[derive(Clone, Debug, PartialEq)] pub struct Ident(pub Rc); - #[derive(Clone, Debug, PartialEq)] pub struct While { pub condition: Expr, @@ -100,11 +99,33 @@ pub struct Condition { pub else_body: Option, } +/// Represents the Structure of a `Function` in AkaiLang +/// +/// Examples: +///```AkaiLang +///fun helloWorld() { +/// print("Hello World") +///} +///``` +///
+/// +///```AkaiLang +///fun returnsIntPlusOne(i: i32): i32 { +/// -> i + 1 +///} +///``` #[derive(Clone, Debug, PartialEq)] pub struct Function { pub name: Rc, pub params: Vec<(Ident, Type)>, pub return_type: Option, pub body: Vec, + pub body_expr: Option } + +#[derive(Clone, Debug, PartialEq)] +pub struct Call { + callee: Box, + arguments: Vec, +} diff --git a/src/language_frontend/abstract_syntax_tree/parser.rs b/src/language_frontend/abstract_syntax_tree/parser.rs index c954b8d..173bfbd 100644 --- a/src/language_frontend/abstract_syntax_tree/parser.rs +++ b/src/language_frontend/abstract_syntax_tree/parser.rs @@ -9,7 +9,6 @@ use crate::language_frontend::{abstract_syntax_tree::{ast::Expr, definitions::*} pub fn parse(source: &str) ->Result, Vec>> { let token_iter = Token::lexer(source).spanned().map(|(token, span)| (token.unwrap_or(Token::Error), span.into())); - let end_of_input: SimpleSpan = (0..source.len()).into(); let token_stream = Stream::from_iter(token_iter) // Tell chumsky to split the (Token, SimpleSpan) stream into its parts so that it can handle the spans for us @@ -86,6 +85,18 @@ where target: Box::new(Expr::Ident(name)), value: Box::new(rhs), }); + /* + let fun = just(Token::Fun) + .ignore_then(ident.clone()) + .then_ignore(just(Token::LParen)) + .then_ignore(just(Token::RParen)) + .map(|(((name, args), ret), (body, body_expr))| Expr::Function(Function { + name, + params: args, + return_type: ret, + body, + body_expr + })); */ var.or(expr) }); @@ -93,6 +104,11 @@ where } +pub fn lex(source: &str) -> Vec { + Token::lexer(&source) + .filter_map(|t| t.ok()).collect() +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/main.rs b/src/main.rs index d2cbcad..e9bbeff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ use logos::Logos; mod language_frontend; +use crate::language_frontend::abstract_syntax_tree::parser::lex; use crate::{ language_frontend::lexer::tokens::Token, language_frontend::abstract_syntax_tree::parser::parse}; @@ -26,25 +27,8 @@ fn main() { println!("{:?}", sourcecode); - - let lexer = Token::lexer(&sourcecode) - .spanned() - .collect::>(); - - for token in lexer { - println!("{:?}", token); - } - - let token_iter = Token::lexer(&sourcecode).spanned().map(|(tok, span)| tok.map(|t| (t, span))).filter_map(Result::ok); - - let token_stream = Stream::from_iter(token_iter) - // Tell chumsky to split the (Token, SimpleSpan) stream into its parts so that it can handle the spans for us - // This involves giving chumsky an 'end of input' span: we just use a zero-width span at the end of the string - .map((0..sourcecode.len()).into(), |(t, s): (_, _)| (t, s)); - - match parse(&sourcecode) { - Ok(res) => println!("{:?}", res), + Ok(res) => println!("{:#?}", res), Err(e) => { panic!("{:#?}", e) }