From 62ea01c532b195d818ad8f1ae0d7bbc72c5a6997 Mon Sep 17 00:00:00 2001 From: Jooris Hadeler Date: Fri, 16 Jan 2026 21:31:38 +0100 Subject: [PATCH] feat: Add support for module parsing. --- example/simple.bky | 16 ++++++++++++++-- src/ast.rs | 5 +++++ src/main.rs | 2 +- src/parser.rs | 14 ++++++++++++++ 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/example/simple.bky b/example/simple.bky index e78fafd..facf581 100644 --- a/example/simple.bky +++ b/example/simple.bky @@ -1,3 +1,15 @@ -fn add(a: i32, b: i32): i32 { - return a + b; +fn min(a: i32, b: i32): i32 { + if a < b { + return a; + } + + return b; +} + +fn max(a: i32, b: i32): i32 { + if a > b { + return a; + } + + return b; } \ No newline at end of file diff --git a/src/ast.rs b/src/ast.rs index 24b7984..5a1c72b 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -121,3 +121,8 @@ pub struct Parameter { pub name_span: Span, pub type_: Type, } + +#[derive(Debug)] +pub struct Module { + pub declarations: Vec, +} diff --git a/src/main.rs b/src/main.rs index af83521..6ff3f61 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,5 +7,5 @@ pub mod token; fn main() { let mut parser = Parser::new(include_str!("../example/simple.bky")); - println!("{:#?}", parser.parse_declaration()); + println!("{:#?}", parser.parse_module()); } diff --git a/src/parser.rs b/src/parser.rs index 9a4896d..13591ac 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -40,6 +40,10 @@ impl<'src> Parser<'src> { self.peek().is_some_and(|tok| tok.kind == kind) } + fn at_eof(&mut self) -> bool { + self.peek().is_none() + } + fn consume(&mut self) -> Option> { self.tokens.next() } @@ -56,6 +60,16 @@ impl<'src> Parser<'src> { } } + pub fn parse_module(&mut self) -> ParserResult { + let mut declarations = Vec::new(); + + while !self.at_eof() { + declarations.push(self.parse_declaration()?); + } + + Ok(Module { declarations }) + } + pub fn parse_declaration(&mut self) -> ParserResult { let peek_tok = self.peek_no_eof()?;