544 lines
12 KiB
Rust
544 lines
12 KiB
Rust
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<Box<Parameter>>);
|
|
|
|
impl Parameters {
|
|
pub fn new(parameters: Vec<Box<Parameter>>) -> 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<Identifier>,
|
|
type_use: Box<TypeUse>,
|
|
}
|
|
|
|
impl Parameter {
|
|
pub fn new(identifier: Box<Identifier>, type_use: Box<TypeUse>) -> 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<TypeUse>,
|
|
references: Box<References>,
|
|
}
|
|
|
|
impl ReturnType {
|
|
pub fn new(declared_type: Box<TypeUse>, references: Box<References>) -> 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<Box<Identifier>>);
|
|
|
|
impl References {
|
|
pub fn new(identifiers: Vec<Box<Identifier>>) -> 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<FunctionModifier>,
|
|
generics: Box<GenericParameters>,
|
|
identifier: Box<Identifier>,
|
|
parameters: Box<Parameters>,
|
|
return_type: Box<ReturnType>,
|
|
body: Box<FunctionBody>,
|
|
}
|
|
|
|
impl FunctionDefinition {
|
|
pub fn new(
|
|
is_public: bool,
|
|
modifier: Option<FunctionModifier>,
|
|
generics: Box<GenericParameters>,
|
|
identifier: Box<Identifier>,
|
|
parameters: Box<Parameters>,
|
|
return_type: Box<ReturnType>,
|
|
body: Box<FunctionBody>,
|
|
) -> 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<FunctionModifier>,
|
|
generics: Box<GenericParameters>,
|
|
operator: Operator,
|
|
parameters: Box<Parameters>,
|
|
return_type: Box<ReturnType>,
|
|
body: Box<FunctionBody>,
|
|
}
|
|
|
|
impl OperatorFunctionDefinition {
|
|
pub fn new(
|
|
is_public: bool,
|
|
modifier: Option<FunctionModifier>,
|
|
generics: Box<GenericParameters>,
|
|
operator: Operator,
|
|
parameters: Box<Parameters>,
|
|
return_type: Box<ReturnType>,
|
|
body: Box<FunctionBody>,
|
|
) -> 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<FunctionModifier>,
|
|
generics: Box<GenericParameters>,
|
|
identifier: Box<Identifier>,
|
|
parameters: Box<Parameters>,
|
|
return_type: Box<ReturnType>,
|
|
}
|
|
|
|
impl PlatformFunctionDeclaration {
|
|
pub fn new(
|
|
is_public: bool,
|
|
modifier: Option<FunctionModifier>,
|
|
generics: Box<GenericParameters>,
|
|
identifier: Box<Identifier>,
|
|
parameters: Box<Parameters>,
|
|
return_type: Box<ReturnType>,
|
|
) -> 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<FunctionModifier>,
|
|
generics: Box<GenericParameters>,
|
|
identifier: Box<Identifier>,
|
|
parameters: Box<Parameters>,
|
|
return_type: Box<ReturnType>,
|
|
body: Option<Box<FunctionBody>>,
|
|
}
|
|
|
|
impl InterfaceFunctionDeclaration {
|
|
pub fn new(
|
|
modifier: Option<FunctionModifier>,
|
|
generics: Box<GenericParameters>,
|
|
identifier: Box<Identifier>,
|
|
parameters: Box<Parameters>,
|
|
return_type: Box<ReturnType>,
|
|
body: Option<Box<FunctionBody>>,
|
|
) -> 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<FunctionModifier>,
|
|
generics: Box<GenericParameters>,
|
|
operator: Operator,
|
|
parameters: Box<Parameters>,
|
|
return_type: Box<ReturnType>,
|
|
body: Option<Box<FunctionBody>>,
|
|
}
|
|
|
|
impl InterfaceOperatorFunctionDeclaration {
|
|
pub fn new(
|
|
modifier: Option<FunctionModifier>,
|
|
generics: Box<GenericParameters>,
|
|
operator: Operator,
|
|
parameters: Box<Parameters>,
|
|
return_type: Box<ReturnType>,
|
|
body: Option<Box<FunctionBody>>,
|
|
) -> 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<Parameters> {
|
|
&self.parameters
|
|
}
|
|
|
|
pub fn parameters_mut(&mut self) -> &mut Box<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 enum FunctionModifier {
|
|
Static,
|
|
Cons,
|
|
Mut,
|
|
Ref,
|
|
MutRef,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum FunctionBody {
|
|
Equals(Box<Expression>),
|
|
Block(Box<BlockStatement>),
|
|
Alias(Box<Identifier>),
|
|
}
|
|
|
|
impl FunctionBody {
|
|
fn unwrap_option(opt: &Option<Box<FunctionBody>>) -> Option<&FunctionBody> {
|
|
if let Some(body) = opt {
|
|
Some(body.as_ref())
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
fn unwrap_option_mut(opt: &mut Option<Box<FunctionBody>>) -> Option<&mut FunctionBody> {
|
|
if let Some(body) = opt {
|
|
Some(body.as_mut())
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
}
|