feat: Add support for module parsing.

This commit is contained in:
Jooris Hadeler
2026-01-16 21:31:38 +01:00
parent cfac708644
commit 62ea01c532
4 changed files with 34 additions and 3 deletions

View File

@@ -1,3 +1,15 @@
fn add(a: i32, b: i32): i32 { fn min(a: i32, b: i32): i32 {
return a + b; if a < b {
return a;
}
return b;
}
fn max(a: i32, b: i32): i32 {
if a > b {
return a;
}
return b;
} }

View File

@@ -121,3 +121,8 @@ pub struct Parameter {
pub name_span: Span, pub name_span: Span,
pub type_: Type, pub type_: Type,
} }
#[derive(Debug)]
pub struct Module {
pub declarations: Vec<Declaration>,
}

View File

@@ -7,5 +7,5 @@ pub mod token;
fn main() { fn main() {
let mut parser = Parser::new(include_str!("../example/simple.bky")); let mut parser = Parser::new(include_str!("../example/simple.bky"));
println!("{:#?}", parser.parse_declaration()); println!("{:#?}", parser.parse_module());
} }

View File

@@ -40,6 +40,10 @@ impl<'src> Parser<'src> {
self.peek().is_some_and(|tok| tok.kind == kind) self.peek().is_some_and(|tok| tok.kind == kind)
} }
fn at_eof(&mut self) -> bool {
self.peek().is_none()
}
fn consume(&mut self) -> Option<Token<'src>> { fn consume(&mut self) -> Option<Token<'src>> {
self.tokens.next() self.tokens.next()
} }
@@ -56,6 +60,16 @@ impl<'src> Parser<'src> {
} }
} }
pub fn parse_module(&mut self) -> ParserResult<Module> {
let mut declarations = Vec::new();
while !self.at_eof() {
declarations.push(self.parse_declaration()?);
}
Ok(Module { declarations })
}
pub fn parse_declaration(&mut self) -> ParserResult<Declaration> { pub fn parse_declaration(&mut self) -> ParserResult<Declaration> {
let peek_tok = self.peek_no_eof()?; let peek_tok = self.peek_no_eof()?;