1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
use crate::nodes::Block; #[derive(Clone, Debug, PartialEq, Eq)] pub struct FunctionName { name: String, field_names: Vec<String>, method: Option<String>, } impl FunctionName { pub fn new(name: String, field_names: Vec<String>, method: Option<String>) -> Self { Self { name, field_names, method, } } pub fn from_name<S: Into<String>>(name: S) -> Self { Self { name: name.into(), field_names: Vec::new(), method: None, } } pub fn with_fields(mut self, field_names: Vec<String>) -> Self { self.field_names = field_names; self } pub fn with_method<S: Into<String>>(mut self, method: S) -> Self { self.method.replace(method.into()); self } pub fn push_field(&mut self, field: String) { self.field_names.push(field); } #[inline] pub fn remove_method(&mut self) -> Option<String> { self.method.take() } #[inline] pub fn get_method(&self) -> Option<&String> { self.method.as_ref() } #[inline] pub fn get_name(&self) -> &String { &self.name } #[inline] pub fn set_name(&mut self, name: String) { self.name = name; } #[inline] pub fn get_field_names(&self) -> &Vec<String> { &self.field_names } #[inline] pub fn mutate_identifier(&mut self) -> &mut String { &mut self.name } } #[derive(Clone, Debug, PartialEq, Eq)] pub struct FunctionStatement { name: FunctionName, block: Block, parameters: Vec<String>, is_variadic: bool, } impl FunctionStatement { pub fn new(name: FunctionName, block: Block, parameters: Vec<String>, is_variadic: bool) -> Self { Self { name, block, parameters, is_variadic, } } pub fn from_name<S: Into<String>>(name: S, block: Block) -> Self { Self { name: FunctionName::from_name(name), block, parameters: Vec::new(), is_variadic: false, } } pub fn with_parameter<S: Into<String>>(mut self, parameter: S) -> Self { self.parameters.push(parameter.into()); self } pub fn variadic(mut self) -> Self { self.is_variadic = true; self } #[inline] pub fn get_block(&self) -> &Block { &self.block } #[inline] pub fn get_name(&self) -> &FunctionName { &self.name } #[inline] pub fn get_parameters(&self) -> &Vec<String> { &self.parameters } #[inline] pub fn is_variadic(&self) -> bool { self.is_variadic } #[inline] pub fn mutate_block(&mut self) -> &mut Block { &mut self.block } #[inline] pub fn mutate_function_name(&mut self) -> &mut FunctionName { &mut self.name } #[inline] pub fn mutate_parameters(&mut self) -> &mut Vec<String> { &mut self.parameters } pub fn remove_method(&mut self) { if let Some(method_name) = self.name.remove_method() { self.name.push_field(method_name); self.parameters.insert(0, "self".to_owned()); } } }