Skip to content

Commit

Permalink
Add base for statement, block and add parsing for block in function d…
Browse files Browse the repository at this point in the history
…eclaration
  • Loading branch information
thi8v committed Feb 22, 2024
1 parent b290451 commit 1542212
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
33 changes: 33 additions & 0 deletions stage1/zom_parser/src/block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//! Module responsible for parsing blocks.
use crate::prelude::*;
use crate::stmt::Statement;

#[derive(Debug)]
pub struct Block {
pub stmts: Vec<Statement>,
pub span: Range<usize>,
}

impl Parse for Block {
type Output = Self;

fn parse(parser: &mut Parser) -> ParsingResult<Self::Output> {
let mut parsed_tokens = Vec::new();

expect_token!(parser => [T::OpenBrace, ()], OpenBrace, parsed_tokens);
let start = span_toks!(start parsed_tokens);

// TODO(Larsouille25): Statments parsing here

expect_token!(parser => [T::CloseBrace, ()], CloseBrace, parsed_tokens);
let end = span_toks!(end parsed_tokens);

Good(
Block {
stmts: Vec::new(),
span: start..end,
},
parsed_tokens,
)
}
}
17 changes: 13 additions & 4 deletions stage1/zom_parser/src/declaration.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Module responsible for parsing top level declarations.
use crate::{prelude::*, types::Type};
use crate::{block::Block, prelude::*, types::Type};

#[derive(Debug)]
pub struct TopLevelDeclaration {
Expand Down Expand Up @@ -41,6 +41,7 @@ pub enum Declaration {
name: String,
args: Vec<Arg>,
ret_ty: Type,
block: Block,
},
}

Expand Down Expand Up @@ -73,9 +74,17 @@ pub fn parse_fn_decl(parser: &mut Parser) -> ParsingResult<Declaration> {

let ret_ty = parse_try!(parser => Type, parsed_tokens);

// TODO(Larsouille25): Add parsing for the block.

Good(Declaration::Function { name, args, ret_ty }, parsed_tokens)
let block = parse_try!(parser => Block, parsed_tokens);

Good(
Declaration::Function {
name,
args,
ret_ty,
block,
},
parsed_tokens,
)
}

#[derive(Debug)]
Expand Down
2 changes: 2 additions & 0 deletions stage1/zom_parser/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::prelude::*;
use crate::source_file::SourceFile;

pub mod block;
pub mod declaration;
pub(crate) mod err;
pub(crate) mod prelude;
pub mod source_file;
pub mod stmt;
pub mod types;

pub struct Parser<'a> {
Expand Down
7 changes: 7 additions & 0 deletions stage1/zom_parser/src/stmt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//! Module responsible for parsing statement.
use crate::prelude::*;

#[derive(Debug)]
pub struct Statement {
pub span: Range<usize>,
}

0 comments on commit 1542212

Please sign in to comment.