stuart_core/functions/parsers/
begin.rs

1use crate::functions::{Function, FunctionParser};
2use crate::parse::{ParseError, RawArgument, RawFunction};
3use crate::process::stack::StackFrame;
4use crate::process::{ProcessError, Scope};
5use crate::{quiet_assert, TracebackError};
6
7/// Parses the `begin` function.
8pub struct BeginParser;
9
10#[derive(Debug, Clone)]
11pub struct BeginFunction {
12    label: String,
13    #[allow(dead_code)]
14    custom: bool,
15}
16
17impl FunctionParser for BeginParser {
18    fn name(&self) -> &'static str {
19        "begin"
20    }
21
22    fn parse(&self, raw: RawFunction) -> Result<Box<dyn Function>, ParseError> {
23        quiet_assert!(raw.positional_args.len() == 1)?;
24        quiet_assert!(raw.named_args.is_empty())?;
25
26        match &raw.positional_args[0] {
27            RawArgument::String(label) => Ok(Box::new(BeginFunction {
28                label: label.to_string(),
29                custom: true,
30            })),
31            RawArgument::Ident(label) => Ok(Box::new(BeginFunction {
32                label: label.to_string(),
33                custom: false,
34            })),
35            _ => Err(ParseError::InvalidArgument),
36        }
37    }
38}
39
40impl Function for BeginFunction {
41    fn name(&self) -> &'static str {
42        "begin"
43    }
44
45    fn execute(&self, scope: &mut Scope) -> Result<(), TracebackError<ProcessError>> {
46        scope
47            .stack
48            .push(StackFrame::new(format!("begin:{}", self.label)));
49
50        Ok(())
51    }
52}