1
0

Improve internal API

This commit is contained in:
Jeff 2023-12-05 17:08:22 -05:00
parent d6c679c0b3
commit ed4dd6a819
29 changed files with 137 additions and 152 deletions

View File

@ -63,7 +63,12 @@ impl AbstractTree for Assignment {
match operator { match operator {
AssignmentOperator::Equal => { AssignmentOperator::Equal => {
type_definition.check(&statement_type, context, statement_node, source)?; type_definition.inner().check(
&statement_type,
context,
statement_node,
source,
)?;
} }
AssignmentOperator::PlusEqual => { AssignmentOperator::PlusEqual => {
let identifier_type = identifier.expected_type(context)?; let identifier_type = identifier.expected_type(context)?;
@ -71,26 +76,31 @@ impl AbstractTree for Assignment {
if let Type::List(item_type) = type_definition.inner() { if let Type::List(item_type) = type_definition.inner() {
let item_type_definition = TypeDefinition::new(*item_type.clone()); let item_type_definition = TypeDefinition::new(*item_type.clone());
item_type_definition.check( item_type_definition.inner().check(
&identifier_type, &identifier_type,
context, context,
identifier_node, identifier_node,
source, source,
)?; )?;
item_type_definition.check( item_type_definition.inner().check(
&statement_type, &statement_type,
context, context,
statement_node, statement_node,
source, source,
)?; )?;
} else { } else {
type_definition.check( type_definition.inner().check(
&identifier_type, &identifier_type,
context, context,
identifier_node, identifier_node,
source, source,
)?; )?;
type_definition.check(&statement_type, context, statement_node, source)?; type_definition.inner().check(
&statement_type,
context,
statement_node,
source,
)?;
} }
} }
AssignmentOperator::MinusEqual => todo!(), AssignmentOperator::MinusEqual => todo!(),
@ -134,8 +144,8 @@ impl AbstractTree for Assignment {
Ok(Value::Empty) Ok(Value::Empty)
} }
fn expected_type(&self, _context: &Map) -> Result<TypeDefinition> { fn expected_type(&self, _context: &Map) -> Result<Type> {
Ok(TypeDefinition::new(Type::Empty)) Ok(Type::Empty)
} }
} }

View File

@ -4,7 +4,7 @@ use rayon::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; use tree_sitter::Node;
use crate::{AbstractTree, Error, Map, Result, Statement, Type, TypeDefinition, Value}; use crate::{AbstractTree, Error, Map, Result, Statement, Type, Value};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Block { pub struct Block {
@ -82,9 +82,9 @@ impl AbstractTree for Block {
} }
} }
fn expected_type(&self, context: &Map) -> Result<TypeDefinition> { fn expected_type(&self, context: &Map) -> Result<Type> {
if self.is_async { if self.is_async {
Ok(TypeDefinition::new(Type::Any)) Ok(Type::Any)
} else { } else {
self.statements.last().unwrap().expected_type(context) self.statements.last().unwrap().expected_type(context)
} }

View File

@ -2,8 +2,7 @@ use serde::{Deserialize, Serialize};
use tree_sitter::Node; use tree_sitter::Node;
use crate::{ use crate::{
value_node::ValueNode, AbstractTree, Error, Identifier, Index, Map, Result, TypeDefinition, value_node::ValueNode, AbstractTree, Error, Identifier, Index, Map, Result, Type, Value, Yield,
Value, Yield,
}; };
use super::{function_call::FunctionCall, logic::Logic, math::Math}; use super::{function_call::FunctionCall, logic::Logic, math::Math};
@ -72,7 +71,7 @@ impl AbstractTree for Expression {
} }
} }
fn expected_type(&self, context: &Map) -> Result<TypeDefinition> { fn expected_type(&self, context: &Map) -> Result<Type> {
match self { match self {
Expression::Value(value_node) => value_node.expected_type(context), Expression::Value(value_node) => value_node.expected_type(context),
Expression::Identifier(identifier) => identifier.expected_type(context), Expression::Identifier(identifier) => identifier.expected_type(context),

View File

@ -2,9 +2,7 @@ use rayon::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; use tree_sitter::Node;
use crate::{ use crate::{AbstractTree, Block, Error, Expression, Identifier, Map, Result, Type, Value};
AbstractTree, Block, Error, Expression, Identifier, Map, Result, Type, TypeDefinition, Value,
};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct For { pub struct For {
@ -79,7 +77,7 @@ impl AbstractTree for For {
Ok(Value::Empty) Ok(Value::Empty)
} }
fn expected_type(&self, _context: &Map) -> Result<TypeDefinition> { fn expected_type(&self, _context: &Map) -> Result<Type> {
Ok(TypeDefinition::new(Type::Empty)) Ok(Type::Empty)
} }
} }

View File

@ -1,9 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; use tree_sitter::Node;
use crate::{ use crate::{AbstractTree, Error, Map, Result, Type, Value, ValueNode, BUILT_IN_FUNCTIONS};
AbstractTree, Error, Map, Result, Type, TypeDefinition, Value, ValueNode, BUILT_IN_FUNCTIONS,
};
use super::expression::Expression; use super::expression::Expression;
@ -41,7 +39,7 @@ impl AbstractTree for FunctionCall {
} }
} }
let function_type = function_expression.expected_type(context)?.take_inner(); let function_type = function_expression.expected_type(context)?;
if let Type::Function { if let Type::Function {
parameter_types, parameter_types,
@ -53,7 +51,7 @@ impl AbstractTree for FunctionCall {
for (argument, r#type) in argument_type_pairs { for (argument, r#type) in argument_type_pairs {
let argument_type = argument.expected_type(context)?; let argument_type = argument.expected_type(context)?;
r#type.check(argument_type.inner(), context, node, source)?; r#type.check(&argument_type, context, node, source)?;
} }
} }
@ -102,28 +100,18 @@ impl AbstractTree for FunctionCall {
value.as_function()?.call(&self.arguments, source, context) value.as_function()?.call(&self.arguments, source, context)
} }
fn expected_type(&self, context: &Map) -> Result<TypeDefinition> { fn expected_type(&self, context: &Map) -> Result<Type> {
match &self.function_expression { match &self.function_expression {
Expression::Value(value_node) => { Expression::Value(value_node) => {
if let ValueNode::Function(function) = value_node { if let ValueNode::Function(function) = value_node {
let return_type = function.return_type()?.clone(); let return_type = function.return_type()?.clone();
Ok(TypeDefinition::new(return_type)) Ok(return_type)
} else { } else {
value_node.expected_type(context) value_node.expected_type(context)
} }
} }
Expression::Identifier(identifier) => { Expression::Identifier(identifier) => identifier.expected_type(context),
let function_name = identifier.inner();
if let Some(value) = context.variables()?.get(function_name) {
let return_type = value.as_function()?.return_type()?.clone();
Ok(TypeDefinition::new(return_type))
} else {
self.function_expression.expected_type(context)
}
}
Expression::Index(index) => index.expected_type(context), Expression::Index(index) => index.expected_type(context),
Expression::Math(math) => math.expected_type(context), Expression::Math(math) => math.expected_type(context),
Expression::Logic(logic) => logic.expected_type(context), Expression::Logic(logic) => logic.expected_type(context),

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; use tree_sitter::Node;
use crate::{AbstractTree, Error, Map, Result, Type, TypeDefinition, Value, BUILT_IN_FUNCTIONS}; use crate::{AbstractTree, Error, Map, Result, Type, Value, BUILT_IN_FUNCTIONS};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Identifier(String); pub struct Identifier(String);
@ -37,17 +37,17 @@ impl AbstractTree for Identifier {
} }
} }
fn expected_type(&self, context: &Map) -> Result<TypeDefinition> { fn expected_type(&self, context: &Map) -> Result<Type> {
if let Some(value) = context.variables()?.get(&self.0) { if let Some(value) = context.variables()?.get(&self.0) {
value.r#type(context) value.r#type(context)
} else { } else {
for built_in_function in BUILT_IN_FUNCTIONS { for built_in_function in BUILT_IN_FUNCTIONS {
if self.0 == built_in_function.name() { if self.0 == built_in_function.name() {
return Ok(built_in_function.type_definition()); return Ok(built_in_function.r#type());
} }
} }
Ok(TypeDefinition::new(Type::Any)) Ok(Type::Any)
} }
} }
} }

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; use tree_sitter::Node;
use crate::{AbstractTree, Block, Expression, Map, Result, TypeDefinition, Value}; use crate::{AbstractTree, Block, Expression, Map, Result, Type, Value};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct IfElse { pub struct IfElse {
@ -81,7 +81,7 @@ impl AbstractTree for IfElse {
} }
} }
fn expected_type(&self, context: &Map) -> Result<TypeDefinition> { fn expected_type(&self, context: &Map) -> Result<Type> {
self.if_block.expected_type(context) self.if_block.expected_type(context)
} }
} }

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; use tree_sitter::Node;
use crate::{AbstractTree, Error, Expression, List, Map, Result, TypeDefinition, Value}; use crate::{AbstractTree, Error, Expression, List, Map, Result, Type, Value};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Index { pub struct Index {
@ -78,7 +78,7 @@ impl AbstractTree for Index {
} }
} }
fn expected_type(&self, context: &Map) -> Result<TypeDefinition> { fn expected_type(&self, context: &Map) -> Result<Type> {
self.collection.expected_type(context) self.collection.expected_type(context)
} }
} }

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; use tree_sitter::Node;
use crate::{AbstractTree, Error, Index, Map, Result, Statement, Type, TypeDefinition, Value}; use crate::{AbstractTree, Error, Index, Map, Result, Statement, Type, Value};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct IndexAssignment { pub struct IndexAssignment {
@ -91,7 +91,7 @@ impl AbstractTree for IndexAssignment {
Ok(Value::Empty) Ok(Value::Empty)
} }
fn expected_type(&self, _context: &Map) -> Result<TypeDefinition> { fn expected_type(&self, _context: &Map) -> Result<Type> {
Ok(TypeDefinition::new(Type::Empty)) Ok(Type::Empty)
} }
} }

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; use tree_sitter::Node;
use crate::{AbstractTree, Error, Expression, Map, Result, Type, TypeDefinition, Value}; use crate::{AbstractTree, Error, Expression, Map, Result, Type, Value};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Logic { pub struct Logic {
@ -85,8 +85,8 @@ impl AbstractTree for Logic {
Ok(Value::Boolean(result)) Ok(Value::Boolean(result))
} }
fn expected_type(&self, _context: &Map) -> Result<TypeDefinition> { fn expected_type(&self, _context: &Map) -> Result<Type> {
Ok(TypeDefinition::new(Type::Boolean)) Ok(Type::Boolean)
} }
} }

View File

@ -6,7 +6,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; use tree_sitter::Node;
use crate::{AbstractTree, Map, Result, TypeDefinition, Value}; use crate::{AbstractTree, Map, Result, Type, Value};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Match {} pub struct Match {}
@ -20,7 +20,7 @@ impl AbstractTree for Match {
todo!() todo!()
} }
fn expected_type(&self, _context: &Map) -> Result<TypeDefinition> { fn expected_type(&self, _context: &Map) -> Result<Type> {
todo!() todo!()
} }
} }

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; use tree_sitter::Node;
use crate::{AbstractTree, Error, Expression, Map, Result, TypeDefinition, Value}; use crate::{AbstractTree, Error, Expression, Map, Result, Type, Value};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Math { pub struct Math {
@ -56,7 +56,7 @@ impl AbstractTree for Math {
Ok(value) Ok(value)
} }
fn expected_type(&self, context: &Map) -> Result<TypeDefinition> { fn expected_type(&self, context: &Map) -> Result<Type> {
self.left.expected_type(context) self.left.expected_type(context)
} }
} }

View File

@ -66,7 +66,7 @@ impl AbstractTree for Root {
Ok(value) Ok(value)
} }
fn expected_type(&self, context: &Map) -> Result<TypeDefinition> { fn expected_type(&self, context: &Map) -> Result<Type> {
self.statements.last().unwrap().expected_type(context) self.statements.last().unwrap().expected_type(context)
} }
} }
@ -88,5 +88,5 @@ pub trait AbstractTree: Sized {
/// Execute dust code by traversing the tree. /// Execute dust code by traversing the tree.
fn run(&self, source: &str, context: &Map) -> Result<Value>; fn run(&self, source: &str, context: &Map) -> Result<Value>;
fn expected_type(&self, context: &Map) -> Result<TypeDefinition>; fn expected_type(&self, context: &Map) -> Result<Type>;
} }

View File

@ -3,7 +3,7 @@ use tree_sitter::Node;
use crate::{ use crate::{
AbstractTree, Assignment, Block, Error, Expression, For, IfElse, IndexAssignment, Map, Match, AbstractTree, Assignment, Block, Error, Expression, For, IfElse, IndexAssignment, Map, Match,
Result, TypeDefinition, Use, Value, While, Result, Type, Use, Value, While,
}; };
/// Abstract representation of a statement. /// Abstract representation of a statement.
@ -82,7 +82,7 @@ impl AbstractTree for Statement {
} }
} }
fn expected_type(&self, context: &Map) -> Result<TypeDefinition> { fn expected_type(&self, context: &Map) -> Result<Type> {
match self { match self {
Statement::Assignment(assignment) => assignment.expected_type(context), Statement::Assignment(assignment) => assignment.expected_type(context),
Statement::Return(expression) => expression.expected_type(context), Statement::Return(expression) => expression.expected_type(context),

View File

@ -22,16 +22,6 @@ impl TypeDefinition {
pub fn take_inner(self) -> Type { pub fn take_inner(self) -> Type {
self.r#type self.r#type
} }
pub fn check(
&self,
other: &TypeDefinition,
context: &Map,
node: Node,
source: &str,
) -> Result<()> {
self.r#type.check(&other.r#type, context, node, source)
}
} }
impl AbstractTree for TypeDefinition { impl AbstractTree for TypeDefinition {
@ -48,7 +38,7 @@ impl AbstractTree for TypeDefinition {
self.r#type.run(source, context) self.r#type.run(source, context)
} }
fn expected_type(&self, context: &Map) -> Result<TypeDefinition> { fn expected_type(&self, context: &Map) -> Result<Type> {
self.r#type.expected_type(context) self.r#type.expected_type(context)
} }
} }
@ -175,7 +165,7 @@ impl AbstractTree for Type {
"str" => Type::String, "str" => Type::String,
_ => { _ => {
return Err(Error::UnexpectedSyntaxNode { return Err(Error::UnexpectedSyntaxNode {
expected: "any, bool, float, fn, int, list, map, num, str or table", expected: "any, bool, float, fn, int, list, map, num or str",
actual: type_node.kind(), actual: type_node.kind(),
location: type_node.start_position(), location: type_node.start_position(),
relevant_source: source[type_node.byte_range()].to_string(), relevant_source: source[type_node.byte_range()].to_string(),
@ -190,8 +180,8 @@ impl AbstractTree for Type {
Ok(Value::Empty) Ok(Value::Empty)
} }
fn expected_type(&self, _context: &Map) -> Result<TypeDefinition> { fn expected_type(&self, _context: &Map) -> Result<Type> {
Ok(TypeDefinition::new(Type::Empty)) Ok(Type::Empty)
} }
} }

View File

@ -3,7 +3,7 @@ use std::fs::read_to_string;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; use tree_sitter::Node;
use crate::{evaluate_with_context, AbstractTree, Error, Map, Result, Type, TypeDefinition, Value}; use crate::{evaluate_with_context, AbstractTree, Error, Map, Result, Type, Value};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Use { pub struct Use {
@ -29,7 +29,7 @@ impl AbstractTree for Use {
Ok(Value::Map(file_context)) Ok(Value::Map(file_context))
} }
fn expected_type(&self, _context: &Map) -> Result<TypeDefinition> { fn expected_type(&self, _context: &Map) -> Result<Type> {
Ok(TypeDefinition::new(Type::Map)) Ok(Type::Map)
} }
} }

View File

@ -5,7 +5,7 @@ use tree_sitter::Node;
use crate::{ use crate::{
AbstractTree, Block, Error, Expression, Function, Identifier, List, Map, Result, Statement, AbstractTree, Block, Error, Expression, Function, Identifier, List, Map, Result, Statement,
Type, TypeDefinition, Value, Type, Value,
}; };
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
@ -144,13 +144,13 @@ impl AbstractTree for ValueNode {
Ok(value) Ok(value)
} }
fn expected_type(&self, context: &Map) -> Result<TypeDefinition> { fn expected_type(&self, context: &Map) -> Result<Type> {
let type_definition = match self { let type_definition = match self {
ValueNode::Boolean(_) => TypeDefinition::new(Type::Boolean), ValueNode::Boolean(_) => Type::Boolean,
ValueNode::Float(_) => TypeDefinition::new(Type::Float), ValueNode::Float(_) => Type::Float,
ValueNode::Function(function) => TypeDefinition::new(function.r#type().clone()), ValueNode::Function(function) => function.r#type().clone(),
ValueNode::Integer(_) => TypeDefinition::new(Type::Integer), ValueNode::Integer(_) => Type::Integer,
ValueNode::String(_) => TypeDefinition::new(Type::String), ValueNode::String(_) => Type::String,
ValueNode::List(expressions) => { ValueNode::List(expressions) => {
let mut previous_type = None; let mut previous_type = None;
@ -159,7 +159,7 @@ impl AbstractTree for ValueNode {
if let Some(previous) = previous_type { if let Some(previous) = previous_type {
if expression_type != previous { if expression_type != previous {
return Ok(TypeDefinition::new(Type::List(Box::new(Type::Any)))); return Ok(Type::List(Box::new(Type::Any)));
} }
} }
@ -167,13 +167,13 @@ impl AbstractTree for ValueNode {
} }
if let Some(previous) = previous_type { if let Some(previous) = previous_type {
TypeDefinition::new(Type::List(Box::new(previous.take_inner()))) Type::List(Box::new(previous))
} else { } else {
TypeDefinition::new(Type::List(Box::new(Type::Any))) Type::List(Box::new(Type::Any))
} }
} }
ValueNode::Empty => TypeDefinition::new(Type::Any), ValueNode::Empty => Type::Any,
ValueNode::Map(_) => TypeDefinition::new(Type::Map), ValueNode::Map(_) => Type::Map,
}; };
Ok(type_definition) Ok(type_definition)

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; use tree_sitter::Node;
use crate::{AbstractTree, Block, Expression, Map, Result, TypeDefinition, Value}; use crate::{AbstractTree, Block, Expression, Map, Result, Type, Value};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct While { pub struct While {
@ -30,7 +30,7 @@ impl AbstractTree for While {
Ok(Value::Empty) Ok(Value::Empty)
} }
fn expected_type(&self, context: &Map) -> Result<TypeDefinition> { fn expected_type(&self, context: &Map) -> Result<Type> {
self.block.expected_type(context) self.block.expected_type(context)
} }
} }

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; use tree_sitter::Node;
use crate::{AbstractTree, Expression, FunctionCall, Map, Result, TypeDefinition, Value}; use crate::{AbstractTree, Expression, FunctionCall, Map, Result, Type, Value};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Yield { pub struct Yield {
@ -39,7 +39,7 @@ impl AbstractTree for Yield {
self.call.run(source, context) self.call.run(source, context)
} }
fn expected_type(&self, context: &Map) -> Result<TypeDefinition> { fn expected_type(&self, context: &Map) -> Result<Type> {
self.call.expected_type(context) self.call.expected_type(context)
} }
} }

View File

@ -1,4 +1,4 @@
use crate::{BuiltInFunction, Error, Map, Result, Type, TypeDefinition, Value}; use crate::{BuiltInFunction, Error, Map, Result, Type, Value};
pub struct Assert; pub struct Assert;
@ -17,11 +17,11 @@ impl BuiltInFunction for Assert {
Ok(Value::Empty) Ok(Value::Empty)
} }
fn type_definition(&self) -> TypeDefinition { fn r#type(&self) -> Type {
TypeDefinition::new(Type::Function { Type::Function {
parameter_types: vec![Type::Any], parameter_types: vec![Type::Any],
return_type: Box::new(Type::Empty), return_type: Box::new(Type::Empty),
}) }
} }
} }
@ -48,10 +48,10 @@ impl BuiltInFunction for AssertEqual {
} }
} }
fn type_definition(&self) -> TypeDefinition { fn r#type(&self) -> Type {
TypeDefinition::new(Type::Function { Type::Function {
parameter_types: vec![Type::Any, Type::Any], parameter_types: vec![Type::Any, Type::Any],
return_type: Box::new(Type::Boolean), return_type: Box::new(Type::Boolean),
}) }
} }
} }

View File

@ -1,4 +1,4 @@
use crate::{BuiltInFunction, Error, Map, Result, Type, TypeDefinition, Value}; use crate::{BuiltInFunction, Error, Map, Result, Type, Value};
pub struct Length; pub struct Length;
@ -15,10 +15,10 @@ impl BuiltInFunction for Length {
Ok(Value::Integer(length as i64)) Ok(Value::Integer(length as i64))
} }
fn type_definition(&self) -> TypeDefinition { fn r#type(&self) -> Type {
TypeDefinition::new(Type::Function { Type::Function {
parameter_types: vec![Type::List(Box::new(Type::Any))], parameter_types: vec![Type::List(Box::new(Type::Any))],
return_type: Box::new(Type::Integer), return_type: Box::new(Type::Integer),
}) }
} }
} }

View File

@ -1,4 +1,4 @@
use crate::{BuiltInFunction, Error, Map, Result, Type, TypeDefinition, Value}; use crate::{BuiltInFunction, Error, Map, Result, Type, Value};
pub struct FromJson; pub struct FromJson;
@ -16,11 +16,11 @@ impl BuiltInFunction for FromJson {
Ok(value) Ok(value)
} }
fn type_definition(&self) -> crate::TypeDefinition { fn r#type(&self) -> Type {
TypeDefinition::new(Type::Function { Type::Function {
parameter_types: vec![Type::String], parameter_types: vec![Type::String],
return_type: Box::new(Type::Any), return_type: Box::new(Type::Any),
}) }
} }
} }
@ -40,10 +40,10 @@ impl BuiltInFunction for ToJson {
Ok(Value::String(json_string)) Ok(Value::String(json_string))
} }
fn type_definition(&self) -> crate::TypeDefinition { fn r#type(&self) -> Type {
TypeDefinition::new(Type::Function { Type::Function {
parameter_types: vec![Type::Any], parameter_types: vec![Type::Any],
return_type: Box::new(Type::String), return_type: Box::new(Type::String),
}) }
} }
} }

View File

@ -4,7 +4,7 @@ use std::{
path::PathBuf, path::PathBuf,
}; };
use crate::{BuiltInFunction, List, Map, Result, Type, TypeDefinition, Value}; use crate::{BuiltInFunction, List, Map, Result, Type, Value};
pub struct Read; pub struct Read;
@ -47,11 +47,11 @@ impl BuiltInFunction for Read {
Ok(Value::String(file_content)) Ok(Value::String(file_content))
} }
fn type_definition(&self) -> TypeDefinition { fn r#type(&self) -> Type {
TypeDefinition::new(Type::Function { Type::Function {
parameter_types: vec![Type::String], parameter_types: vec![Type::String],
return_type: Box::new(Type::String), return_type: Box::new(Type::String),
}) }
} }
} }
@ -71,11 +71,11 @@ impl BuiltInFunction for Write {
Ok(Value::Empty) Ok(Value::Empty)
} }
fn type_definition(&self) -> crate::TypeDefinition { fn r#type(&self) -> Type {
TypeDefinition::new(Type::Function { Type::Function {
parameter_types: vec![Type::String], parameter_types: vec![Type::String],
return_type: Box::new(Type::Empty), return_type: Box::new(Type::Empty),
}) }
} }
} }
@ -99,10 +99,10 @@ impl BuiltInFunction for Append {
Ok(Value::Empty) Ok(Value::Empty)
} }
fn type_definition(&self) -> crate::TypeDefinition { fn r#type(&self) -> Type {
TypeDefinition::new(Type::Function { Type::Function {
parameter_types: vec![Type::String, Type::String], parameter_types: vec![Type::String, Type::String],
return_type: Box::new(Type::Empty), return_type: Box::new(Type::Empty),
}) }
} }
} }

View File

@ -1,4 +1,4 @@
use crate::{Map, Result, TypeDefinition, Value}; use crate::{Map, Result, Type, Value};
mod assert; mod assert;
mod collections; mod collections;
@ -29,5 +29,5 @@ pub const BUILT_IN_FUNCTIONS: [&dyn BuiltInFunction; 14] = [
pub trait BuiltInFunction { pub trait BuiltInFunction {
fn name(&self) -> &'static str; fn name(&self) -> &'static str;
fn run(&self, arguments: &[Value], context: &Map) -> Result<Value>; fn run(&self, arguments: &[Value], context: &Map) -> Result<Value>;
fn type_definition(&self) -> TypeDefinition; fn r#type(&self) -> Type;
} }

View File

@ -1,6 +1,6 @@
use reqwest::blocking::get; use reqwest::blocking::get;
use crate::{BuiltInFunction, Error, Map, Result, Type, TypeDefinition, Value}; use crate::{BuiltInFunction, Error, Map, Result, Type, Value};
pub struct Download; pub struct Download;
@ -18,10 +18,10 @@ impl BuiltInFunction for Download {
Ok(Value::String(response.text()?)) Ok(Value::String(response.text()?))
} }
fn type_definition(&self) -> TypeDefinition { fn r#type(&self) -> Type {
TypeDefinition::new(Type::Function { Type::Function {
parameter_types: vec![Type::String], parameter_types: vec![Type::String],
return_type: Box::new(Type::String), return_type: Box::new(Type::String),
}) }
} }
} }

View File

@ -1,4 +1,4 @@
use crate::{BuiltInFunction, Map, Result, Type, TypeDefinition, Value}; use crate::{BuiltInFunction, Map, Result, Type, Value};
pub struct Output; pub struct Output;
@ -15,10 +15,10 @@ impl BuiltInFunction for Output {
Ok(Value::Empty) Ok(Value::Empty)
} }
fn type_definition(&self) -> crate::TypeDefinition { fn r#type(&self) -> Type {
TypeDefinition::new(Type::Function { Type::Function {
parameter_types: vec![Type::Any], parameter_types: vec![Type::Any],
return_type: Box::new(Type::Empty), return_type: Box::new(Type::Empty),
}) }
} }
} }

View File

@ -1,6 +1,6 @@
use rand::{random, thread_rng, Rng}; use rand::{random, thread_rng, Rng};
use crate::{BuiltInFunction, Error, Map, Result, Type, TypeDefinition, Value}; use crate::{BuiltInFunction, Error, Map, Result, Type, Value};
pub struct Random; pub struct Random;
@ -20,11 +20,11 @@ impl BuiltInFunction for Random {
Ok(random_argument.clone()) Ok(random_argument.clone())
} }
fn type_definition(&self) -> TypeDefinition { fn r#type(&self) -> Type {
TypeDefinition::new(Type::Function { Type::Function {
parameter_types: vec![Type::List(Box::new(Type::Any))], parameter_types: vec![Type::List(Box::new(Type::Any))],
return_type: Box::new(Type::Any), return_type: Box::new(Type::Any),
}) }
} }
} }
@ -41,11 +41,11 @@ impl BuiltInFunction for RandomInteger {
Ok(Value::Integer(random())) Ok(Value::Integer(random()))
} }
fn type_definition(&self) -> crate::TypeDefinition { fn r#type(&self) -> Type {
TypeDefinition::new(Type::Function { Type::Function {
parameter_types: Vec::with_capacity(0), parameter_types: Vec::with_capacity(0),
return_type: Box::new(Type::Integer), return_type: Box::new(Type::Integer),
}) }
} }
} }
@ -62,11 +62,11 @@ impl BuiltInFunction for RandomFloat {
Ok(Value::Float(random())) Ok(Value::Float(random()))
} }
fn type_definition(&self) -> crate::TypeDefinition { fn r#type(&self) -> Type {
TypeDefinition::new(Type::Function { Type::Function {
parameter_types: Vec::with_capacity(0), parameter_types: Vec::with_capacity(0),
return_type: Box::new(Type::Float), return_type: Box::new(Type::Float),
}) }
} }
} }
@ -83,10 +83,10 @@ impl BuiltInFunction for RandomBoolean {
Ok(Value::Boolean(random())) Ok(Value::Boolean(random()))
} }
fn type_definition(&self) -> crate::TypeDefinition { fn r#type(&self) -> Type {
TypeDefinition::new(Type::Function { Type::Function {
parameter_types: Vec::with_capacity(0), parameter_types: Vec::with_capacity(0),
return_type: Box::new(Type::Boolean), return_type: Box::new(Type::Boolean),
}) }
} }
} }

View File

@ -1,4 +1,4 @@
use crate::{BuiltInFunction, Error, List, Map, Result, Type, TypeDefinition, Value}; use crate::{BuiltInFunction, Error, List, Map, Result, Type, Value};
pub struct TypeFunction; pub struct TypeFunction;
@ -32,10 +32,10 @@ impl BuiltInFunction for TypeFunction {
} }
} }
fn type_definition(&self) -> crate::TypeDefinition { fn r#type(&self) -> Type {
TypeDefinition::new(Type::Function { Type::Function {
parameter_types: vec![Type::String], parameter_types: vec![Type::String],
return_type: Box::new(Type::Any), return_type: Box::new(Type::Any),
}) }
} }
} }

View File

@ -1,7 +1,7 @@
//! Types that represent runtime values. //! Types that represent runtime values.
use crate::{ use crate::{
error::{Error, Result}, error::{Error, Result},
Function, List, Map, Type, TypeDefinition, Function, List, Map, Type,
}; };
use serde::{ use serde::{
@ -42,7 +42,7 @@ pub enum Value {
} }
impl Value { impl Value {
pub fn r#type(&self, context: &Map) -> Result<TypeDefinition> { pub fn r#type(&self, context: &Map) -> Result<Type> {
let r#type = match self { let r#type = match self {
Value::List(list) => { Value::List(list) => {
let mut previous_type = None; let mut previous_type = None;
@ -52,7 +52,7 @@ impl Value {
if let Some(previous) = &previous_type { if let Some(previous) = &previous_type {
if &value_type != previous { if &value_type != previous {
return Ok(TypeDefinition::new(Type::List(Box::new(Type::Any)))); return Ok(Type::List(Box::new(Type::Any)));
} }
} }
@ -60,13 +60,13 @@ impl Value {
} }
if let Some(previous) = previous_type { if let Some(previous) = previous_type {
Type::List(Box::new(previous.take_inner())) Type::List(Box::new(previous))
} else { } else {
Type::List(Box::new(Type::Any)) Type::List(Box::new(Type::Any))
} }
} }
Value::Map(_) => Type::Map, Value::Map(_) => Type::Map,
Value::Function(function) => return Ok(TypeDefinition::new(function.r#type().clone())), Value::Function(function) => function.r#type().clone(),
Value::String(_) => Type::String, Value::String(_) => Type::String,
Value::Float(_) => Type::Float, Value::Float(_) => Type::Float,
Value::Integer(_) => Type::Integer, Value::Integer(_) => Type::Integer,
@ -74,7 +74,7 @@ impl Value {
Value::Empty => Type::Empty, Value::Empty => Type::Empty,
}; };
Ok(TypeDefinition::new(r#type)) Ok(r#type)
} }
pub fn is_string(&self) -> bool { pub fn is_string(&self) -> bool {