2023-08-22 15:40:50 +00:00
|
|
|
use std::fmt::{self, Display, Formatter};
|
|
|
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
2023-11-27 20:02:08 +00:00
|
|
|
use tree_sitter::Node;
|
2023-08-22 15:40:50 +00:00
|
|
|
|
2023-11-27 20:02:08 +00:00
|
|
|
use crate::{AbstractTree, Block, Error, Identifier, Map, Result, Type, Value};
|
2023-08-22 15:40:50 +00:00
|
|
|
|
2023-09-30 21:52:37 +00:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
|
|
|
|
pub struct Function {
|
2023-11-27 20:02:08 +00:00
|
|
|
parameters: Vec<(Identifier, Type)>,
|
|
|
|
return_type: Option<Type>,
|
|
|
|
body: Block,
|
2023-09-30 21:52:37 +00:00
|
|
|
}
|
2023-08-22 15:40:50 +00:00
|
|
|
|
|
|
|
impl Function {
|
2023-11-27 20:02:08 +00:00
|
|
|
pub fn parameters(&self) -> &Vec<(Identifier, Type)> {
|
|
|
|
&self.parameters
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
2023-11-27 20:02:08 +00:00
|
|
|
}
|
2023-10-06 21:11:50 +00:00
|
|
|
|
2023-11-27 20:02:08 +00:00
|
|
|
impl AbstractTree for Function {
|
|
|
|
fn from_syntax_node(source: &str, node: Node) -> Result<Self> {
|
2023-11-28 15:29:42 +00:00
|
|
|
Error::expect_syntax_node(source, "function", node)?;
|
|
|
|
|
|
|
|
let child_count = node.child_count();
|
2023-11-27 20:02:08 +00:00
|
|
|
let mut parameters = Vec::new();
|
|
|
|
|
2023-11-28 15:29:42 +00:00
|
|
|
for index in 1..child_count - 2 {
|
|
|
|
let parameter_node = {
|
|
|
|
let child = node.child(index).unwrap();
|
|
|
|
|
|
|
|
if child.is_named() {
|
|
|
|
child
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Error::expect_syntax_node(source, "parameter", parameter_node)?;
|
2023-11-27 20:02:08 +00:00
|
|
|
|
2023-11-28 15:29:42 +00:00
|
|
|
let identifier_node = parameter_node.child(0).unwrap();
|
|
|
|
let identifier = Identifier::from_syntax_node(source, identifier_node)?;
|
2023-11-27 20:02:08 +00:00
|
|
|
|
2023-11-28 15:29:42 +00:00
|
|
|
let type_node = parameter_node.child(1).unwrap();
|
|
|
|
let r#type = Type::from_syntax_node(source, type_node)?;
|
2023-11-27 20:02:08 +00:00
|
|
|
|
2023-11-28 15:29:42 +00:00
|
|
|
parameters.push((identifier, r#type))
|
2023-11-27 20:02:08 +00:00
|
|
|
}
|
|
|
|
|
2023-11-28 15:29:42 +00:00
|
|
|
let return_type_node = node.child(child_count - 2).unwrap();
|
|
|
|
let return_type = if return_type_node.is_named() {
|
|
|
|
Some(Type::from_syntax_node(source, return_type_node)?)
|
2023-11-27 20:02:08 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2023-11-28 15:29:42 +00:00
|
|
|
let body_node = node.child(child_count - 1).unwrap();
|
2023-11-27 20:02:08 +00:00
|
|
|
let body = Block::from_syntax_node(source, body_node)?;
|
|
|
|
|
|
|
|
Ok(Function {
|
|
|
|
parameters,
|
|
|
|
return_type,
|
|
|
|
body,
|
|
|
|
})
|
2023-10-06 21:11:50 +00:00
|
|
|
}
|
|
|
|
|
2023-11-27 20:02:08 +00:00
|
|
|
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
|
|
|
|
let return_value = self.body.run(source, context)?;
|
|
|
|
|
|
|
|
if let Some(r#type) = &self.return_type {
|
|
|
|
r#type.check(&return_value)?;
|
|
|
|
} else if !return_value.is_empty() {
|
|
|
|
return Err(Error::ExpectedEmpty {
|
|
|
|
actual: return_value.clone(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(return_value)
|
2023-10-06 21:11:50 +00:00
|
|
|
}
|
2023-10-05 18:29:13 +00:00
|
|
|
}
|
2023-08-22 15:40:50 +00:00
|
|
|
|
|
|
|
impl Display for Function {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
2023-09-30 21:52:37 +00:00
|
|
|
write!(
|
|
|
|
f,
|
2023-11-27 20:02:08 +00:00
|
|
|
"Function {{ parameters: {:?}, return_type: {:?}, body: {:?} }}",
|
|
|
|
self.parameters, self.return_type, self.body
|
2023-09-30 21:52:37 +00:00
|
|
|
)
|
2023-08-22 15:40:50 +00:00
|
|
|
}
|
|
|
|
}
|