feat: Add support for module parsing.
This commit is contained in:
@@ -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;
|
||||||
}
|
}
|
||||||
@@ -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>,
|
||||||
|
}
|
||||||
|
|||||||
@@ -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());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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()?;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user