1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use nom::{character::complete::space0, combinator::map, sequence::tuple, IResult};

use crate::{
    ast,
    ir::{quantity::local, RegisterName},
    utility::data_type::{self, Type},
};
use serde::{Deserialize, Serialize};
/// [`Parameter`] represents a function's parameter.
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub struct Parameter {
    /// Name of the parameter.
    pub name: RegisterName,
    /// Type of the parameter.
    pub data_type: Type,
}

pub fn parse(code: &str) -> IResult<&str, Parameter> {
    map(
        tuple((data_type::parse, space0, local::parse, space0)),
        |(data_type, _, name, _)| Parameter { name, data_type },
    )(code)
}

pub fn from_ast(ast: &ast::function_definition::Parameter) -> Parameter {
    let ast::function_definition::Parameter { name, data_type } = ast;
    Parameter {
        name: RegisterName(name.clone()),
        data_type: data_type.clone(),
    }
}