kodept_ast/node/
code_flow.rs

1#[cfg(feature = "serde")]
2use serde::{Deserialize, Serialize};
3
4use kodept_core::structure::rlt;
5use kodept_core::structure::span::CodeHolder;
6
7use crate::graph::Identity;
8use crate::graph::tags::PRIMARY;
9use crate::graph::NodeId;
10use crate::graph::{SyntaxTreeBuilder};
11use crate::traits::{Linker, PopulateTree};
12use crate::{node, Body, Operation, node_sub_enum};
13
14node_sub_enum! {
15    #[derive(Debug, PartialEq)]
16    pub enum CodeFlow {
17        If(IfExpr)
18    }
19}
20
21node! {
22    #[derive(Debug, PartialEq)]
23    #[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
24    pub struct IfExpr {;
25        pub condition: Identity<Operation> as PRIMARY,
26        pub body: Identity<Body> as 0,
27        pub elifs: Vec<ElifExpr> as 0,
28        pub elses: Option<ElseExpr> as 0,
29    }
30}
31
32node! {
33    #[derive(Debug, PartialEq)]
34    #[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
35    pub struct ElifExpr {;
36        pub condition: Identity<Operation>,
37        pub body: Identity<Body>,
38    }
39}
40
41node! {
42    #[derive(Debug, PartialEq)]
43    #[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
44    pub struct ElseExpr {;
45        pub body: Identity<Body>,
46    }
47}
48
49impl PopulateTree for rlt::IfExpr {
50    type Output = IfExpr;
51
52    fn convert(
53        &self,
54        builder: &mut SyntaxTreeBuilder,
55        context: &mut (impl Linker + CodeHolder),
56    ) -> NodeId<Self::Output> {
57        builder
58            .add_node(IfExpr::uninit())
59            .with_children_from([&self.condition], context)
60            .with_children_from([&self.body], context)
61            .with_children_from(self.elif.as_ref(), context)
62            .with_children_from(self.el.as_slice(), context)
63            .with_rlt(context, self)
64            .id()
65    }
66}
67
68impl PopulateTree for rlt::ElifExpr {
69    type Output = ElifExpr;
70
71    fn convert(
72        &self,
73        builder: &mut SyntaxTreeBuilder,
74        context: &mut (impl Linker + CodeHolder),
75    ) -> NodeId<Self::Output> {
76        builder
77            .add_node(ElifExpr::uninit())
78            .with_children_from([&self.condition], context)
79            .with_children_from([&self.body], context)
80            .with_rlt(context, self)
81            .id()
82    }
83}
84
85impl PopulateTree for rlt::ElseExpr {
86    type Output = ElseExpr;
87
88    fn convert(
89        &self,
90        builder: &mut SyntaxTreeBuilder,
91        context: &mut (impl Linker + CodeHolder),
92    ) -> NodeId<Self::Output> {
93        builder
94            .add_node(ElseExpr::uninit())
95            .with_children_from([&self.body], context)
96            .with_rlt(context, self)
97            .id()
98    }
99}