[Syntax] Parse function call as a statement
This commit is contained in:
parent
9af450eaa0
commit
0630287e34
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
||||
target
|
||||
examples
|
||||
|
@ -1,4 +1,5 @@
|
||||
pub mod functions;
|
||||
pub mod statement;
|
||||
|
||||
pub struct ModuleAST {
|
||||
pub declarations: Vec<TopLevelDeclaration>,
|
||||
@ -17,7 +18,9 @@ pub struct FunctionDeclaration {
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Block {}
|
||||
pub struct Block {
|
||||
pub statements: Vec<statement::Statement>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ParamsList {}
|
||||
|
6
src/syntax/ast/statement.rs
Normal file
6
src/syntax/ast/statement.rs
Normal file
@ -0,0 +1,6 @@
|
||||
use super::functions::FunctionCall;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Statement {
|
||||
FunctionCall(FunctionCall),
|
||||
}
|
@ -18,6 +18,21 @@ pub fn parse_block<'a>(tokens: &'a Vec<Token>, pos: usize) -> ParseResult<Block,
|
||||
};
|
||||
current_pos = next_pos;
|
||||
|
||||
// Parse block statements
|
||||
let mut statements = Vec::new();
|
||||
|
||||
// Only 1 statement for now
|
||||
match super::statement::try_parse(tokens, current_pos) {
|
||||
ParseResult::Ok(statement, next_pos) => {
|
||||
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<Token>, pos: usize) -> ParseResult<Block,
|
||||
};
|
||||
current_pos = next_pos;
|
||||
|
||||
ParseResult::Ok(Block {}, current_pos)
|
||||
ParseResult::Ok(
|
||||
Block {
|
||||
statements,
|
||||
},
|
||||
current_pos,
|
||||
)
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ mod block;
|
||||
mod expression;
|
||||
mod functions;
|
||||
mod params_list;
|
||||
mod statement;
|
||||
mod utils;
|
||||
|
||||
pub mod ast;
|
||||
|
35
src/syntax/statement.rs
Normal file
35
src/syntax/statement.rs
Normal file
@ -0,0 +1,35 @@
|
||||
use crate::lexic::token::Token;
|
||||
|
||||
use super::{ast::statement::Statement, functions::function_call, ParseResult};
|
||||
|
||||
pub fn try_parse<'a>(tokens: &'a Vec<Token>, pos: usize) -> ParseResult<Statement, ()> {
|
||||
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"),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user