205 lines
4.6 KiB
Rust
205 lines
4.6 KiB
Rust
use crate::ast::node::expression::Expression;
|
|
use crate::ast::node::names::Identifier;
|
|
use crate::ast::node::statement::Statement;
|
|
use crate::ast::node::type_use::TypeUse;
|
|
|
|
#[derive(Debug)]
|
|
pub struct Closure {
|
|
modifier: Option<ClosureModifier>,
|
|
is_move: bool,
|
|
captures: Box<ClosureCaptures>,
|
|
parameters: Box<ClosureParameters>,
|
|
statements: Vec<Box<Statement>>,
|
|
expression: Option<Box<Expression>>,
|
|
}
|
|
|
|
impl Closure {
|
|
pub fn new(
|
|
modifier: Option<ClosureModifier>,
|
|
is_move: bool,
|
|
captures: Box<ClosureCaptures>,
|
|
parameters: Box<ClosureParameters>,
|
|
statements: Vec<Box<Statement>>,
|
|
expression: Option<Box<Expression>>,
|
|
) -> Self {
|
|
Self {
|
|
modifier,
|
|
is_move,
|
|
captures,
|
|
parameters,
|
|
statements,
|
|
expression,
|
|
}
|
|
}
|
|
|
|
pub fn modifier(&self) -> Option<&ClosureModifier> {
|
|
self.modifier.as_ref()
|
|
}
|
|
|
|
pub fn is_move(&self) -> bool {
|
|
self.is_move
|
|
}
|
|
|
|
pub fn captures(&self) -> &ClosureCaptures {
|
|
&self.captures
|
|
}
|
|
|
|
pub fn captures_mut(&mut self) -> &mut ClosureCaptures {
|
|
&mut self.captures
|
|
}
|
|
|
|
pub fn parameters(&self) -> &ClosureParameters {
|
|
&self.parameters
|
|
}
|
|
|
|
pub fn parameters_mut(&mut self) -> &mut ClosureParameters {
|
|
&mut self.parameters
|
|
}
|
|
|
|
pub fn statements(&self) -> Vec<&Statement> {
|
|
self.statements.iter().map(Box::as_ref).collect()
|
|
}
|
|
|
|
pub fn statements_mut(&mut self) -> Vec<&mut Statement> {
|
|
self.statements.iter_mut().map(Box::as_mut).collect()
|
|
}
|
|
|
|
pub fn expression(&self) -> Option<&Expression> {
|
|
if let Some(expression) = &self.expression {
|
|
Some(expression)
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
pub fn expression_mut(&mut self) -> Option<&mut Expression> {
|
|
if let Some(expression) = &mut self.expression {
|
|
Some(expression)
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum ClosureModifier {
|
|
Cons,
|
|
Mut,
|
|
}
|
|
|
|
#[derive(Debug, Default)]
|
|
pub struct ClosureCaptures(Vec<Box<ClosureCapture>>);
|
|
|
|
impl ClosureCaptures {
|
|
pub fn new(captures: Vec<Box<ClosureCapture>>) -> Self {
|
|
Self(captures)
|
|
}
|
|
|
|
pub fn captures(&self) -> Vec<&ClosureCapture> {
|
|
self.0.iter().map(Box::as_ref).collect()
|
|
}
|
|
|
|
pub fn captures_mut(&mut self) -> Vec<&mut ClosureCapture> {
|
|
self.0.iter_mut().map(Box::as_mut).collect()
|
|
}
|
|
}
|
|
|
|
impl ClosureCaptures {
|
|
pub fn is_empty(&self) -> bool {
|
|
self.0.is_empty()
|
|
}
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct ClosureCapture {
|
|
borrow_count: usize,
|
|
is_mutable: bool,
|
|
identifier: Box<Identifier>,
|
|
}
|
|
|
|
impl ClosureCapture {
|
|
pub fn new(borrow_count: usize, is_mutable: bool, identifier: Box<Identifier>) -> Self {
|
|
Self {
|
|
borrow_count,
|
|
is_mutable,
|
|
identifier,
|
|
}
|
|
}
|
|
|
|
pub fn borrow_count(&self) -> usize {
|
|
self.borrow_count
|
|
}
|
|
|
|
pub fn is_mutable(&self) -> bool {
|
|
self.is_mutable
|
|
}
|
|
|
|
pub fn identifier(&self) -> &Identifier {
|
|
&self.identifier
|
|
}
|
|
|
|
pub fn identifier_mut(&mut self) -> &mut Identifier {
|
|
&mut self.identifier
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Default)]
|
|
pub struct ClosureParameters(Vec<Box<ClosureParameter>>);
|
|
|
|
impl ClosureParameters {
|
|
pub fn new(parameters: Vec<Box<ClosureParameter>>) -> Self {
|
|
Self(parameters)
|
|
}
|
|
|
|
pub fn parameters(&self) -> Vec<&ClosureParameter> {
|
|
self.0.iter().map(Box::as_ref).collect()
|
|
}
|
|
|
|
pub fn parameters_mut(&mut self) -> Vec<&mut ClosureParameter> {
|
|
self.0.iter_mut().map(Box::as_mut).collect()
|
|
}
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
self.0.is_empty()
|
|
}
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct ClosureParameter {
|
|
identifier: Box<Identifier>,
|
|
type_use: Option<Box<TypeUse>>,
|
|
}
|
|
|
|
impl ClosureParameter {
|
|
pub fn new(identifier: Box<Identifier>, type_use: Option<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) -> Option<&TypeUse> {
|
|
if let Some(type_use) = &self.type_use {
|
|
Some(type_use)
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
pub fn type_use_mut(&mut self) -> Option<&mut TypeUse> {
|
|
if let Some(type_use) = &mut self.type_use {
|
|
Some(type_use.as_mut())
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
}
|