diff --git a/.gitignore b/.gitignore index eb5a316..c33a7d7 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ target +examples diff --git a/src/syntax/ast/mod.rs b/src/syntax/ast/mod.rs index 7374dd8..9b18d6f 100644 --- a/src/syntax/ast/mod.rs +++ b/src/syntax/ast/mod.rs @@ -1,4 +1,5 @@ pub mod functions; +pub mod statement; pub struct ModuleAST { pub declarations: Vec, @@ -17,7 +18,9 @@ pub struct FunctionDeclaration { } #[derive(Debug)] -pub struct Block {} +pub struct Block { + pub statements: Vec, +} #[derive(Debug)] pub struct ParamsList {} diff --git a/src/syntax/ast/statement.rs b/src/syntax/ast/statement.rs new file mode 100644 index 0000000..054a1bb --- /dev/null +++ b/src/syntax/ast/statement.rs @@ -0,0 +1,6 @@ +use super::functions::FunctionCall; + +#[derive(Debug)] +pub enum Statement { + FunctionCall(FunctionCall), +} diff --git a/src/syntax/block.rs b/src/syntax/block.rs index 37fd142..3f0201c 100644 --- a/src/syntax/block.rs +++ b/src/syntax/block.rs @@ -18,6 +18,21 @@ pub fn parse_block<'a>(tokens: &'a Vec, pos: usize) -> ParseResult { + current_pos = next_pos; + statements.push(statement); + } + ParseResult::Err(err) => return ParseResult::Err(err), + ParseResult::Unmatched => {} + ParseResult::Mismatch(_) => {} + } + + // Parse closing brace let (_closing_brace, next_pos) = match parse_token_type(tokens, current_pos, TokenType::RightBrace) { @@ -40,5 +55,10 @@ pub fn parse_block<'a>(tokens: &'a Vec, pos: usize) -> ParseResult(tokens: &'a Vec, pos: usize) -> ParseResult { + None.or_else(|| match function_call::try_parse(tokens, pos) { + ParseResult::Ok(f, next) => Some(ParseResult::Ok(Statement::FunctionCall(f), next)), + ParseResult::Err(err) => Some(ParseResult::Err(err)), + _ => None, + }) + .unwrap_or_else(|| ParseResult::Unmatched) +} + + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn should_parse_function_call() { + let input = String::from("f1()"); + let tokens = crate::lexic::get_tokens(&input).unwrap(); + let statement = try_parse(&tokens, 0); + + let statement = match statement { + ParseResult::Ok(s, _) => s, + _ => panic!("Expected a function call"), + }; + + match statement { + Statement::FunctionCall(_) => assert!(true), + _ => panic!("Expected a function call"), + } + } +}