Skip to content

Commit

Permalink
added function parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
lexa-diky committed May 8, 2024
1 parent a29fe53 commit 89a666f
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions src/cst/parser.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::path::PathBuf;
use clap::builder::Str;

use crate::ast::{AstNode, AstNodeType};
use crate::cst::{CstActualParameter, CstAtom, CstConstantStatement, CstEmitStatement, CstFunctionStatement};
Expand Down Expand Up @@ -61,7 +60,7 @@ fn parse_function_body(node: &AstNode) -> Result<BodyParsingResult, CstParserErr
match child.node_type {
AstNodeType::StatementConst => {}
AstNodeType::StatementEmit => emits.push(parse_emit_statement(child)?),
AstNodeType::StatementFn => {}
AstNodeType::StatementFn => functions.push(parse_function(child)?),
_ => return Err(CstParserError::UnexpectedNode {
actual: child.node_type,
expected: vec![
Expand All @@ -76,6 +75,42 @@ fn parse_function_body(node: &AstNode) -> Result<BodyParsingResult, CstParserErr
Ok((emits, functions, constants))
}

fn parse_function(node: &AstNode) -> Result<CstFunctionStatement, CstParserError> {
let mut emits = None;
let mut functions = None;
let mut constants = None;

for child in &node.children {
match child.node_type {
AstNodeType::StatementFnName => {}
AstNodeType::StatementFnBody => {
let (emits_r, functions_r, constants_r) = parse_function_body(child)?;
emits = Some(emits_r);
functions = Some(functions_r);
constants = Some(constants_r);
}
_ => return Err(
CstParserError::UnexpectedNode {
actual: child.node_type,
expected: vec![
AstNodeType::StatementFnName,
AstNodeType::StatementFnBody,
],
}),
}
}

return Ok(
CstFunctionStatement {
name: parse_value_of(&node.children[0])?,
params: Vec::new(),
emits: emits.unwrap_or(Vec::new()),
functions: functions.unwrap_or(Vec::new()),
constants: constants.unwrap_or(Vec::new()),
}
);
}

fn parse_emit_statement(node: &AstNode) -> Result<CstEmitStatement, CstParserError> {
guard_node_type(node, AstNodeType::StatementEmit)?;
let mut atoms = Vec::new();
Expand Down

0 comments on commit 89a666f

Please sign in to comment.