pipeline_script/ast/
declaration.rs

1use crate::ast::data::Data;
2
3use crate::ast::expr::ExprNode;
4use crate::ast::r#type::Type;
5#[derive(Clone, Debug)]
6pub struct VariableDeclaration {
7    pub name: String,
8    pub default: Option<ExprNode>,
9    pub declaration_type: Option<Type>,
10    pub is_var_arg: bool,
11    pub is_closure: bool,
12    pub is_env: bool,
13}
14
15impl VariableDeclaration {
16    pub fn new(name: impl Into<String>) -> Self {
17        Self {
18            name: name.into(),
19            default: None,
20            declaration_type: None,
21            is_var_arg: false,
22            is_env: false,
23            is_closure: false,
24        }
25    }
26    pub fn set_name(&mut self, name: impl Into<String>) {
27        self.name = name.into();
28    }
29
30    pub fn with_type(mut self, dec: Type) -> Self {
31        self.declaration_type = Some(dec);
32        self
33    }
34    pub fn with_env(mut self, is_env: bool) -> Self {
35        self.is_env = is_env;
36        self
37    }
38    pub fn is_env(&self) -> bool {
39        self.is_env
40    }
41    pub fn with_closure(mut self, is_closure: bool) -> Self {
42        self.is_closure = is_closure;
43        self
44    }
45    pub fn with_var_arg(mut self, is_var_arg: bool) -> Self {
46        self.is_var_arg = is_var_arg;
47        self
48    }
49    pub fn is_var_arg(&self) -> bool {
50        self.is_var_arg
51    }
52    pub fn r#type(&self) -> Option<Type> {
53        self.declaration_type.clone()
54    }
55    pub fn name(&self) -> String {
56        self.name.clone()
57    }
58    pub fn has_default(&self) -> bool {
59        self.default.is_some()
60    }
61    pub fn get_default(&self) -> Option<&ExprNode> {
62        match &self.default {
63            None => None,
64            Some(s) => Some(s),
65        }
66    }
67    pub fn get_mut_default(&mut self) -> Option<&mut ExprNode> {
68        match &mut self.default {
69            None => None,
70            Some(s) => Some(s),
71        }
72    }
73    pub fn get_data(&self, key: &str) -> Option<Data> {
74        match key {
75            "name" => Some(Data::String(self.name.clone())),
76            "type" => self
77                .declaration_type
78                .as_ref()
79                .map(|s| Data::Type(s.clone())),
80            "is_var_arg" => Some(Data::Boolean(self.is_var_arg)),
81            _ => None,
82        }
83    }
84    pub fn set_type(&mut self, ty: Type) {
85        self.declaration_type = Some(ty)
86    }
87    pub fn set_default(&mut self, expr: ExprNode) {
88        self.default = Some(expr)
89    }
90    pub fn with_default(mut self, default: ExprNode) -> Self {
91        self.default = Some(default);
92        self
93    }
94
95    /// 克隆默认值表达式,如果存在
96    pub fn clone_default(&self) -> Option<ExprNode> {
97        self.default.clone()
98    }
99}