sleigh_rs/syntax/block/execution/
mod.rs

1pub mod assignment;
2pub mod branch;
3pub mod export;
4pub mod expr;
5pub mod op;
6
7use crate::preprocessor::token::Token;
8use crate::syntax::block::execution::assignment::{
9    Assignment, Declare, MemWrite,
10};
11use crate::syntax::parser::{ident, number, this_ident};
12use crate::{NumberUnsigned, SleighError, Span};
13use nom::branch::alt;
14use nom::combinator::map;
15use nom::multi::{many0, separated_list0};
16use nom::sequence::{delimited, pair, terminated};
17use nom::IResult;
18
19use self::branch::Branch;
20use self::export::Export;
21
22#[derive(Clone, Debug)]
23pub enum Statement {
24    Delayslot(Delayslot),
25    Label(Label),
26    Export(Export),
27    Branch(Branch),
28    Build(Build),
29    Call(UserCall),
30    Declare(Declare),
31    Assignment(Assignment),
32    MemWrite(MemWrite),
33}
34
35impl Statement {
36    pub fn parse(input: &[Token]) -> IResult<&[Token], Self, SleighError> {
37        alt((
38            //Move label out of statement?
39            map(Label::parse, Self::Label),
40            map(Delayslot::parse, Self::Delayslot),
41            map(Export::parse, Self::Export),
42            map(Branch::parse, Self::Branch),
43            map(Build::parse, Self::Build),
44            map(UserCall::parse_statement, Self::Call),
45            map(Declare::parse, Self::Declare),
46            map(Assignment::parse, Self::Assignment),
47            map(MemWrite::parse, Self::MemWrite),
48        ))(input)
49    }
50}
51
52#[derive(Clone, Debug)]
53pub struct Delayslot(pub NumberUnsigned);
54
55impl Delayslot {
56    fn parse(input: &[Token]) -> IResult<&[Token], Self, SleighError> {
57        map(
58            terminated(
59                delimited(
60                    pair(this_ident("delayslot"), tag!("(")),
61                    number,
62                    tag!(")"),
63                ),
64                tag!(";"),
65            ),
66            |(value, _)| Self(value),
67        )(input)
68    }
69}
70
71#[derive(Clone, Debug)]
72pub struct Label {
73    pub src: Span,
74    pub name: String,
75}
76
77impl Label {
78    fn parse(input: &[Token]) -> IResult<&[Token], Self, SleighError> {
79        map(
80            delimited(tag!("<"), ident, tag!(">")),
81            |(name, name_src)| Self {
82                name,
83                src: name_src.clone(),
84            },
85        )(input)
86    }
87}
88
89#[derive(Clone, Debug)]
90pub struct Build {
91    pub src: Span,
92    pub table_name: String,
93}
94impl Build {
95    pub fn parse(input: &[Token]) -> IResult<&[Token], Self, SleighError> {
96        map(
97            delimited(this_ident("build"), ident, tag!(";")),
98            |(table_name, name_src)| Self {
99                table_name,
100                src: name_src.clone(),
101            },
102        )(input)
103    }
104}
105
106#[derive(Clone, Debug)]
107pub struct UserCall {
108    //top of the stack contains the call (op::Op)
109    pub params: Vec<expr::Expr>,
110    pub src: Span,
111    pub name: String,
112}
113
114impl UserCall {
115    pub fn new(function: String, src: Span, params: Vec<expr::Expr>) -> Self {
116        Self {
117            name: function,
118            src,
119            params,
120        }
121    }
122    pub fn parse_statement(
123        input: &[Token],
124    ) -> IResult<&[Token], Self, SleighError> {
125        terminated(UserCall::parse_expr, tag!(";"))(input)
126    }
127    pub fn parse_expr(input: &[Token]) -> IResult<&[Token], Self, SleighError> {
128        map(
129            pair(
130                ident,
131                delimited(
132                    tag!("("),
133                    separated_list0(tag!(","), expr::Expr::parse),
134                    tag!(")"),
135                ),
136            ),
137            |((name, name_src), params)| Self {
138                params,
139                src: name_src.clone(),
140                name,
141            },
142        )(input)
143    }
144}
145
146#[derive(Clone, Debug, Default)]
147pub struct Execution {
148    pub statements: Vec<Statement>,
149}
150
151impl Execution {
152    pub fn parse(input: &[Token]) -> IResult<&[Token], Self, SleighError> {
153        map(many0(Statement::parse), |statements| Self { statements })(input)
154    }
155}