y_lang/ast/
statement.rs

1use pest::iterators::Pair;
2
3use super::{CompilerDirective, Expression, Import, InlineAssembly, Intrinsic, Rule};
4
5#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
6pub enum Statement<T> {
7    Import(Import),
8    Expression(Expression<T>),
9    Intrinsic(Intrinsic<T>),
10    CompilerDirective(CompilerDirective<T>),
11    InlineAssembly(InlineAssembly<T>),
12}
13
14impl Statement<()> {
15    pub fn from_pair(pair: Pair<Rule>, file: &str) -> Statement<()> {
16        match pair.as_rule() {
17            Rule::importDirective => Statement::Import(Import::from_pair(pair, file)),
18            Rule::declaration | Rule::definition | Rule::assignment | Rule::whileLoop => {
19                Statement::Intrinsic(Intrinsic::from_pair(pair, file))
20            }
21            Rule::expr => Statement::Expression(Expression::from_pair(pair, file)),
22            Rule::compiler_directive => {
23                Statement::CompilerDirective(CompilerDirective::from_pair(pair, file))
24            }
25            Rule::inlineAsm => Statement::InlineAssembly(InlineAssembly::from_pair(pair, file)),
26            rule => unreachable!("Can not parse rule {rule:?} as expression"),
27        }
28    }
29}
30
31impl<T> Statement<T>
32where
33    T: Clone + Default,
34{
35    pub fn info(&self) -> T {
36        match self {
37            Statement::Expression(expression) => expression.info(),
38            Statement::Intrinsic(intrinsic) => intrinsic.info(),
39            Statement::CompilerDirective(compiler_directive) => compiler_directive.info(),
40            Statement::InlineAssembly(inline_assembly) => inline_assembly.info(),
41            _ => unreachable!(),
42        }
43    }
44}