1
0
dust/src/value/function.rs

84 lines
2.3 KiB
Rust
Raw Normal View History

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> {
let mut parameters = Vec::new();
let mut previous_identifier = None;
for index in 1..node.child_count() - 2 {
let child = node.child(index).unwrap();
if child.kind() == "identifier" {
previous_identifier = Some(Identifier::from_syntax_node(source, child)?);
}
if child.kind() == "type" {
let identifier = previous_identifier.take().unwrap();
let r#type = Type::from_syntax_node(source, child)?;
parameters.push((identifier, r#type))
}
}
let return_type_node = node.child_by_field_name("return_type");
let return_type = if let Some(child) = return_type_node {
Some(Type::from_syntax_node(source, child)?)
} else {
None
};
let body_node = node.child_by_field_name("body").unwrap();
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> {
println!("{self}");
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-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
}
}