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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use crate::{
    ir::{
        function::IsIRStatement,
        quantity::{self, Quantity},
        RegisterName,
    },
    utility::data_type::Type,
};
use nom::{
    bytes::complete::tag,
    character::complete::space0,
    combinator::{map, opt},
    sequence::tuple,
    IResult,
};
use serde::{Deserialize, Serialize};
use std::fmt;
/// [`Ret`] instruction.
#[derive(Debug, Eq, PartialEq, Clone, Hash, Serialize, Deserialize)]
pub struct Ret {
    pub value: Option<Quantity>,
}

impl IsIRStatement for Ret {
    fn on_register_change(&mut self, from: &RegisterName, to: Quantity) {
        if let Some(Quantity::RegisterName(local)) = &mut self.value {
            if local == from {
                *local = to.unwrap_local();
            }
        }
    }
    fn generate_register(&self) -> Option<(RegisterName, Type)> {
        None
    }
    fn use_register(&self) -> Vec<RegisterName> {
        if let Some(Quantity::RegisterName(register)) = &self.value {
            vec![register.clone()]
        } else {
            Vec::new()
        }
    }
}

impl fmt::Display for Ret {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(value) = &self.value {
            write!(f, "ret {value}")
        } else {
            write!(f, "ret")
        }
    }
}

/// Parse a [`Ret`] instruction.
pub fn parse(code: &str) -> IResult<&str, Ret> {
    map(
        tuple((tag("ret"), space0, opt(quantity::parse))),
        |(_, _, value)| Ret { value },
    )(code)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse() {
        let result = parse("ret").unwrap().1;
        assert_eq!(result, Ret { value: None });
        let result = parse("ret %1").unwrap().1;
        assert_eq!(
            result,
            Ret {
                value: Some(RegisterName("1".to_string()).into())
            }
        )
    }
}