datex_core/compiler/precompiler/
precompiled_ast.rs1use crate::ast::expressions::{DatexExpression, VariableKind};
2use crate::stdlib::{cell::RefCell, rc::Rc};
3use crate::values::core_values::r#type::Type;
4use core::fmt::Display;
5
6#[derive(Clone, Debug)]
7pub struct VariableMetadata {
8 pub original_realm_index: usize,
9 pub is_cross_realm: bool,
10 pub shape: VariableShape,
11 pub var_type: Option<Type>,
12 pub name: String,
13}
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
16pub enum VariableShape {
17 Type,
18 Value(VariableKind),
19}
20
21impl From<VariableKind> for VariableShape {
22 fn from(value: VariableKind) -> Self {
23 VariableShape::Value(value)
24 }
25}
26
27impl Display for VariableShape {
28 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
29 match self {
30 VariableShape::Type => core::write!(f, "type"),
31 VariableShape::Value(kind) => core::write!(f, "{kind}"),
32 }
33 }
34}
35
36#[derive(Default, Debug)]
37pub struct AstMetadata {
38 pub variables: Vec<VariableMetadata>,
39 pub is_terminated: bool,
42}
43
44impl AstMetadata {
45 pub fn variable_metadata(&self, id: usize) -> Option<&VariableMetadata> {
46 self.variables.get(id)
47 }
48
49 pub fn variable_metadata_mut(
50 &mut self,
51 id: usize,
52 ) -> Option<&mut VariableMetadata> {
53 self.variables.get_mut(id)
54 }
55}
56
57#[derive(Debug, Clone, Default)]
58pub struct RichAst {
59 pub ast: DatexExpression,
60 pub metadata: Rc<RefCell<AstMetadata>>,
61}
62
63impl RichAst {
64 pub fn new(
65 ast: DatexExpression,
66 metadata: &Rc<RefCell<AstMetadata>>,
67 ) -> Self {
68 RichAst {
69 ast,
70 metadata: metadata.clone(),
71 }
72 }
73
74 pub fn new_without_metadata(ast: DatexExpression) -> Self {
75 RichAst {
76 ast,
77 metadata: Rc::new(RefCell::new(AstMetadata::default())),
78 }
79 }
80}