pipeline_script/ast/
declaration.rs1use 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 is_array_vararg(&self) -> bool {
53 let ty = self.declaration_type.as_ref();
54 if ty.is_some_and(|ty| ty.is_array_vararg()) {
55 return true;
56 }
57 false
58 }
59 pub fn r#type(&self) -> Option<Type> {
60 self.declaration_type.clone()
61 }
62 pub fn name(&self) -> String {
63 self.name.clone()
64 }
65 pub fn has_default(&self) -> bool {
66 self.default.is_some()
67 }
68 pub fn get_default(&self) -> Option<&ExprNode> {
69 match &self.default {
70 None => None,
71 Some(s) => Some(s),
72 }
73 }
74 pub fn get_mut_default(&mut self) -> Option<&mut ExprNode> {
75 match &mut self.default {
76 None => None,
77 Some(s) => Some(s),
78 }
79 }
80 pub fn get_data(&self, key: &str) -> Option<Data> {
81 match key {
82 "name" => Some(Data::String(self.name.clone())),
83 "type" => self
84 .declaration_type
85 .as_ref()
86 .map(|s| Data::Type(s.clone())),
87 "is_var_arg" => Some(Data::Boolean(self.is_var_arg)),
88 _ => None,
89 }
90 }
91 pub fn set_type(&mut self, ty: Type) {
92 self.declaration_type = Some(ty)
93 }
94 pub fn set_default(&mut self, expr: ExprNode) {
95 self.default = Some(expr)
96 }
97 pub fn with_default(mut self, default: ExprNode) -> Self {
98 self.default = Some(default);
99 self
100 }
101
102 pub fn clone_default(&self) -> Option<ExprNode> {
104 self.default.clone()
105 }
106}