use crate::ast::node::expression::Expression; use crate::ast::node::generics::GenericParameters; use crate::ast::node::names::Identifier; use crate::ast::node::operators::Operator; use crate::ast::node::statement::BlockStatement; use crate::ast::node::type_use::{PrimitiveTypeUse, TypeUse}; #[derive(Debug)] pub enum FunctionTypeModifier { Cons, MutRef, Mut, Ref, } #[derive(Debug)] pub struct Parameters(Vec>); impl Parameters { pub fn new(parameters: Vec>) -> Self { Self(parameters) } pub fn is_empty(&self) -> bool { self.0.is_empty() } pub fn parameters(&self) -> Vec<&Parameter> { self.0.iter().map(Box::as_ref).collect() } pub fn parameters_mut(&mut self) -> Vec<&mut Parameter> { self.0.iter_mut().map(Box::as_mut).collect() } } impl Default for Parameters { fn default() -> Self { Parameters(Vec::new()) } } #[derive(Debug)] pub struct Parameter { identifier: Box, type_use: Box, } impl Parameter { pub fn new(identifier: Box, type_use: Box) -> Self { Self { identifier, type_use, } } pub fn identifier(&self) -> &Identifier { &self.identifier } pub fn identifier_mut(&mut self) -> &mut Identifier { &mut self.identifier } pub fn type_use(&self) -> &TypeUse { &self.type_use } pub fn type_use_mut(&mut self) -> &mut TypeUse { &mut self.type_use } } #[derive(Debug)] pub struct ReturnType { declared_type: Box, references: Box, } impl ReturnType { pub fn new(declared_type: Box, references: Box) -> Self { Self { declared_type, references, } } pub fn void() -> Self { ReturnType { declared_type: Box::new(TypeUse::Primitive(Box::new(PrimitiveTypeUse::Void))), references: Box::new(References::default()), } } pub fn declared_type(&self) -> &TypeUse { &self.declared_type } pub fn declared_type_mut(&mut self) -> &mut TypeUse { &mut self.declared_type } pub fn references(&self) -> &References { &self.references } pub fn references_mut(&mut self) -> &mut References { &mut self.references } } #[derive(Debug)] pub struct References(Vec>); impl References { pub fn new(identifiers: Vec>) -> Self { Self(identifiers) } pub fn is_empty(&self) -> bool { self.0.is_empty() } pub fn identifiers(&self) -> Vec<&Identifier> { self.0.iter().map(Box::as_ref).collect() } pub fn identifiers_mut(&mut self) -> Vec<&mut Identifier> { self.0.iter_mut().map(Box::as_mut).collect() } } impl Default for References { fn default() -> Self { References(Vec::new()) } } #[derive(Debug)] pub struct FunctionDefinition { is_public: bool, modifier: Option, generics: Box, identifier: Box, parameters: Box, return_type: Box, body: Box, } impl FunctionDefinition { pub fn new( is_public: bool, modifier: Option, generics: Box, identifier: Box, parameters: Box, return_type: Box, body: Box, ) -> Self { Self { is_public, modifier, generics, identifier, parameters, return_type, body, } } pub fn is_public(&self) -> bool { self.is_public } pub fn modifier(&self) -> Option<&FunctionModifier> { self.modifier.as_ref() } pub fn generics(&self) -> &GenericParameters { &self.generics } pub fn generics_mut(&mut self) -> &mut GenericParameters { &mut self.generics } pub fn identifier(&self) -> &Identifier { &self.identifier } pub fn identifier_mut(&mut self) -> &mut Identifier { &mut self.identifier } pub fn parameters(&self) -> &Parameters { &self.parameters } pub fn parameters_mut(&mut self) -> &mut Parameters { &mut self.parameters } pub fn return_type(&self) -> &ReturnType { &self.return_type } pub fn return_type_mut(&mut self) -> &mut ReturnType { &mut self.return_type } pub fn body(&self) -> &FunctionBody { &self.body } pub fn body_mut(&mut self) -> &mut FunctionBody { &mut self.body } } #[derive(Debug)] pub struct OperatorFunctionDefinition { is_public: bool, modifier: Option, generics: Box, operator: Operator, parameters: Box, return_type: Box, body: Box, } impl OperatorFunctionDefinition { pub fn new( is_public: bool, modifier: Option, generics: Box, operator: Operator, parameters: Box, return_type: Box, body: Box, ) -> Self { Self { is_public, modifier, generics, operator, parameters, return_type, body, } } pub fn is_public(&self) -> bool { self.is_public } pub fn modifier(&self) -> Option<&FunctionModifier> { self.modifier.as_ref() } pub fn generics(&self) -> &GenericParameters { &self.generics } pub fn generics_mut(&mut self) -> &mut GenericParameters { &mut self.generics } pub fn operator(&self) -> &Operator { &self.operator } pub fn parameters(&self) -> &Parameters { &self.parameters } pub fn parameters_mut(&mut self) -> &mut Parameters { &mut self.parameters } pub fn return_type(&self) -> &ReturnType { &self.return_type } pub fn return_type_mut(&mut self) -> &mut ReturnType { &mut self.return_type } pub fn body(&self) -> &FunctionBody { &self.body } pub fn body_mut(&mut self) -> &mut FunctionBody { &mut self.body } } #[derive(Debug)] pub struct PlatformFunctionDeclaration { is_public: bool, modifier: Option, generics: Box, identifier: Box, parameters: Box, return_type: Box, } impl PlatformFunctionDeclaration { pub fn new( is_public: bool, modifier: Option, generics: Box, identifier: Box, parameters: Box, return_type: Box, ) -> Self { Self { is_public, modifier, generics, identifier, parameters, return_type, } } pub fn is_public(&self) -> bool { self.is_public } pub fn modifier(&self) -> Option<&FunctionModifier> { self.modifier.as_ref() } pub fn generics(&self) -> &GenericParameters { &self.generics } pub fn generics_mut(&mut self) -> &mut GenericParameters { &mut self.generics } pub fn identifier(&self) -> &Identifier { &self.identifier } pub fn identifier_mut(&mut self) -> &mut Identifier { &mut self.identifier } pub fn parameters(&self) -> &Parameters { &self.parameters } pub fn parameters_mut(&mut self) -> &mut Parameters { &mut self.parameters } pub fn return_type(&self) -> &ReturnType { &self.return_type } pub fn return_type_mut(&mut self) -> &mut ReturnType { &mut self.return_type } } #[derive(Debug)] pub struct InterfaceFunctionDeclaration { modifier: Option, generics: Box, identifier: Box, parameters: Box, return_type: Box, body: Option>, } impl InterfaceFunctionDeclaration { pub fn new( modifier: Option, generics: Box, identifier: Box, parameters: Box, return_type: Box, body: Option>, ) -> Self { Self { modifier, generics, identifier, parameters, return_type, body, } } pub fn modifier(&self) -> Option<&FunctionModifier> { self.modifier.as_ref() } pub fn generics(&self) -> &GenericParameters { &self.generics } pub fn generics_mut(&mut self) -> &mut GenericParameters { &mut self.generics } pub fn identifier(&self) -> &Identifier { &self.identifier } pub fn identifier_mut(&mut self) -> &mut Identifier { &mut self.identifier } pub fn parameters(&self) -> &Parameters { &self.parameters } pub fn parameters_mut(&mut self) -> &mut Parameters { &mut self.parameters } pub fn return_type(&self) -> &ReturnType { &self.return_type } pub fn return_type_mut(&mut self) -> &mut ReturnType { &mut self.return_type } pub fn body(&self) -> Option<&FunctionBody> { FunctionBody::unwrap_option(&self.body) } pub fn body_mut(&mut self) -> Option<&mut FunctionBody> { FunctionBody::unwrap_option_mut(&mut self.body) } } #[derive(Debug)] pub struct InterfaceOperatorFunctionDeclaration { modifier: Option, generics: Box, operator: Operator, parameters: Box, return_type: Box, body: Option>, } impl InterfaceOperatorFunctionDeclaration { pub fn new( modifier: Option, generics: Box, operator: Operator, parameters: Box, return_type: Box, body: Option>, ) -> Self { Self { modifier, generics, operator, parameters, return_type, body, } } pub fn modifier(&self) -> Option<&FunctionModifier> { self.modifier.as_ref() } pub fn generics(&self) -> &GenericParameters { &self.generics } pub fn generics_mut(&mut self) -> &mut GenericParameters { &mut self.generics } pub fn operator(&self) -> &Operator { &self.operator } pub fn parameters(&self) -> &Box { &self.parameters } pub fn parameters_mut(&mut self) -> &mut Box { &mut self.parameters } pub fn return_type(&self) -> &ReturnType { &self.return_type } pub fn return_type_mut(&mut self) -> &mut ReturnType { &mut self.return_type } pub fn body(&self) -> Option<&FunctionBody> { FunctionBody::unwrap_option(&self.body) } pub fn body_mut(&mut self) -> Option<&mut FunctionBody> { FunctionBody::unwrap_option_mut(&mut self.body) } } #[derive(Debug)] pub enum FunctionModifier { Static, Cons, Mut, Ref, MutRef, } #[derive(Debug)] pub enum FunctionBody { Equals(Box), Block(Box), Alias(Box), } impl FunctionBody { fn unwrap_option(opt: &Option>) -> Option<&FunctionBody> { if let Some(body) = opt { Some(body.as_ref()) } else { None } } fn unwrap_option_mut(opt: &mut Option>) -> Option<&mut FunctionBody> { if let Some(body) = opt { Some(body.as_mut()) } else { None } } }