y_lang/ast/
intrinsic.rs

1use super::{Declaration, Rule, WhileLoop};
2
3use pest::iterators::Pair;
4
5use super::{Assignment, Definition};
6
7#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
8pub enum Intrinsic<T> {
9    Declaration(Declaration),
10    Definition(Definition<T>),
11    Assignment(Assignment<T>),
12    WhileLoop(WhileLoop<T>),
13}
14
15impl Intrinsic<()> {
16    pub fn from_pair(pair: Pair<Rule>, file: &str) -> Intrinsic<()> {
17        match pair.as_rule() {
18            Rule::declaration => Intrinsic::Declaration(Declaration::from_pair(pair, file)),
19            Rule::definition => Intrinsic::Definition(Definition::from_pair(pair, file)),
20            Rule::assignment => Intrinsic::Assignment(Assignment::from_pair(pair, file)),
21            Rule::whileLoop => Intrinsic::WhileLoop(WhileLoop::from_pair(pair, file)),
22            _ => panic!("Unexpected intrinsic '{pair:#?}'"),
23        }
24    }
25}
26
27impl<T> Intrinsic<T>
28where
29    T: Clone,
30{
31    pub fn info(&self) -> T {
32        match self {
33            Intrinsic::Definition(Definition { info, .. })
34            | Intrinsic::Assignment(Assignment { info, .. }) => info.clone(),
35            _ => unimplemented!(),
36        }
37    }
38}