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

View File

@ -4,7 +4,7 @@ use rayon::prelude::*;
use serde::{Deserialize, Serialize};
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)]
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 {
Ok(TypeDefinition::new(Type::Any))
Ok(Type::Any)
} else {
self.statements.last().unwrap().expected_type(context)
}

View File

@ -2,8 +2,7 @@ use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{
value_node::ValueNode, AbstractTree, Error, Identifier, Index, Map, Result, TypeDefinition,
Value, Yield,
value_node::ValueNode, AbstractTree, Error, Identifier, Index, Map, Result, Type, Value, Yield,
};
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 {
Expression::Value(value_node) => value_node.expected_type(context),
Expression::Identifier(identifier) => identifier.expected_type(context),

View File

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

View File

@ -1,9 +1,7 @@
use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{
AbstractTree, Error, Map, Result, Type, TypeDefinition, Value, ValueNode, BUILT_IN_FUNCTIONS,
};
use crate::{AbstractTree, Error, Map, Result, Type, Value, ValueNode, BUILT_IN_FUNCTIONS};
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 {
parameter_types,
@ -53,7 +51,7 @@ impl AbstractTree for FunctionCall {
for (argument, r#type) in argument_type_pairs {
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)
}
fn expected_type(&self, context: &Map) -> Result<TypeDefinition> {
fn expected_type(&self, context: &Map) -> Result<Type> {
match &self.function_expression {
Expression::Value(value_node) => {
if let ValueNode::Function(function) = value_node {
let return_type = function.return_type()?.clone();
Ok(TypeDefinition::new(return_type))
Ok(return_type)
} else {
value_node.expected_type(context)
}
}
Expression::Identifier(identifier) => {
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::Identifier(identifier) => identifier.expected_type(context),
Expression::Index(index) => index.expected_type(context),
Expression::Math(math) => math.expected_type(context),
Expression::Logic(logic) => logic.expected_type(context),

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
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)]
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) {
value.r#type(context)
} else {
for built_in_function in BUILT_IN_FUNCTIONS {
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 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)]
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)
}
}

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
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)]
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)
}
}

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
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)]
pub struct IndexAssignment {
@ -91,7 +91,7 @@ impl AbstractTree for IndexAssignment {
Ok(Value::Empty)
}
fn expected_type(&self, _context: &Map) -> Result<TypeDefinition> {
Ok(TypeDefinition::new(Type::Empty))
fn expected_type(&self, _context: &Map) -> Result<Type> {
Ok(Type::Empty)
}
}

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
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)]
pub struct Logic {
@ -85,8 +85,8 @@ impl AbstractTree for Logic {
Ok(Value::Boolean(result))
}
fn expected_type(&self, _context: &Map) -> Result<TypeDefinition> {
Ok(TypeDefinition::new(Type::Boolean))
fn expected_type(&self, _context: &Map) -> Result<Type> {
Ok(Type::Boolean)
}
}

View File

@ -6,7 +6,7 @@
use serde::{Deserialize, Serialize};
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)]
pub struct Match {}
@ -20,7 +20,7 @@ impl AbstractTree for Match {
todo!()
}
fn expected_type(&self, _context: &Map) -> Result<TypeDefinition> {
fn expected_type(&self, _context: &Map) -> Result<Type> {
todo!()
}
}

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
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)]
pub struct Math {
@ -56,7 +56,7 @@ impl AbstractTree for Math {
Ok(value)
}
fn expected_type(&self, context: &Map) -> Result<TypeDefinition> {
fn expected_type(&self, context: &Map) -> Result<Type> {
self.left.expected_type(context)
}
}

View File

@ -66,7 +66,7 @@ impl AbstractTree for Root {
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)
}
}
@ -88,5 +88,5 @@ pub trait AbstractTree: Sized {
/// Execute dust code by traversing the tree.
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::{
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.
@ -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 {
Statement::Assignment(assignment) => assignment.expected_type(context),
Statement::Return(expression) => expression.expected_type(context),

View File

@ -22,16 +22,6 @@ impl TypeDefinition {
pub fn take_inner(self) -> 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 {
@ -48,7 +38,7 @@ impl AbstractTree for TypeDefinition {
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)
}
}
@ -175,7 +165,7 @@ impl AbstractTree for Type {
"str" => Type::String,
_ => {
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(),
location: type_node.start_position(),
relevant_source: source[type_node.byte_range()].to_string(),
@ -190,8 +180,8 @@ impl AbstractTree for Type {
Ok(Value::Empty)
}
fn expected_type(&self, _context: &Map) -> Result<TypeDefinition> {
Ok(TypeDefinition::new(Type::Empty))
fn expected_type(&self, _context: &Map) -> Result<Type> {
Ok(Type::Empty)
}
}

View File

@ -3,7 +3,7 @@ use std::fs::read_to_string;
use serde::{Deserialize, Serialize};
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)]
pub struct Use {
@ -29,7 +29,7 @@ impl AbstractTree for Use {
Ok(Value::Map(file_context))
}
fn expected_type(&self, _context: &Map) -> Result<TypeDefinition> {
Ok(TypeDefinition::new(Type::Map))
fn expected_type(&self, _context: &Map) -> Result<Type> {
Ok(Type::Map)
}
}

View File

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

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
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)]
pub struct While {
@ -30,7 +30,7 @@ impl AbstractTree for While {
Ok(Value::Empty)
}
fn expected_type(&self, context: &Map) -> Result<TypeDefinition> {
fn expected_type(&self, context: &Map) -> Result<Type> {
self.block.expected_type(context)
}
}

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
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)]
pub struct Yield {
@ -39,7 +39,7 @@ impl AbstractTree for Yield {
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)
}
}

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;
@ -17,11 +17,11 @@ impl BuiltInFunction for Assert {
Ok(Value::Empty)
}
fn type_definition(&self) -> TypeDefinition {
TypeDefinition::new(Type::Function {
fn r#type(&self) -> Type {
Type::Function {
parameter_types: vec![Type::Any],
return_type: Box::new(Type::Empty),
})
}
}
}
@ -48,10 +48,10 @@ impl BuiltInFunction for AssertEqual {
}
}
fn type_definition(&self) -> TypeDefinition {
TypeDefinition::new(Type::Function {
fn r#type(&self) -> Type {
Type::Function {
parameter_types: vec![Type::Any, Type::Any],
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;
@ -15,10 +15,10 @@ impl BuiltInFunction for Length {
Ok(Value::Integer(length as i64))
}
fn type_definition(&self) -> TypeDefinition {
TypeDefinition::new(Type::Function {
fn r#type(&self) -> Type {
Type::Function {
parameter_types: vec![Type::List(Box::new(Type::Any))],
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;
@ -16,11 +16,11 @@ impl BuiltInFunction for FromJson {
Ok(value)
}
fn type_definition(&self) -> crate::TypeDefinition {
TypeDefinition::new(Type::Function {
fn r#type(&self) -> Type {
Type::Function {
parameter_types: vec![Type::String],
return_type: Box::new(Type::Any),
})
}
}
}
@ -40,10 +40,10 @@ impl BuiltInFunction for ToJson {
Ok(Value::String(json_string))
}
fn type_definition(&self) -> crate::TypeDefinition {
TypeDefinition::new(Type::Function {
fn r#type(&self) -> Type {
Type::Function {
parameter_types: vec![Type::Any],
return_type: Box::new(Type::String),
})
}
}
}

View File

@ -4,7 +4,7 @@ use std::{
path::PathBuf,
};
use crate::{BuiltInFunction, List, Map, Result, Type, TypeDefinition, Value};
use crate::{BuiltInFunction, List, Map, Result, Type, Value};
pub struct Read;
@ -47,11 +47,11 @@ impl BuiltInFunction for Read {
Ok(Value::String(file_content))
}
fn type_definition(&self) -> TypeDefinition {
TypeDefinition::new(Type::Function {
fn r#type(&self) -> Type {
Type::Function {
parameter_types: vec![Type::String],
return_type: Box::new(Type::String),
})
}
}
}
@ -71,11 +71,11 @@ impl BuiltInFunction for Write {
Ok(Value::Empty)
}
fn type_definition(&self) -> crate::TypeDefinition {
TypeDefinition::new(Type::Function {
fn r#type(&self) -> Type {
Type::Function {
parameter_types: vec![Type::String],
return_type: Box::new(Type::Empty),
})
}
}
}
@ -99,10 +99,10 @@ impl BuiltInFunction for Append {
Ok(Value::Empty)
}
fn type_definition(&self) -> crate::TypeDefinition {
TypeDefinition::new(Type::Function {
fn r#type(&self) -> Type {
Type::Function {
parameter_types: vec![Type::String, Type::String],
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 collections;
@ -29,5 +29,5 @@ pub const BUILT_IN_FUNCTIONS: [&dyn BuiltInFunction; 14] = [
pub trait BuiltInFunction {
fn name(&self) -> &'static str;
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 crate::{BuiltInFunction, Error, Map, Result, Type, TypeDefinition, Value};
use crate::{BuiltInFunction, Error, Map, Result, Type, Value};
pub struct Download;
@ -18,10 +18,10 @@ impl BuiltInFunction for Download {
Ok(Value::String(response.text()?))
}
fn type_definition(&self) -> TypeDefinition {
TypeDefinition::new(Type::Function {
fn r#type(&self) -> Type {
Type::Function {
parameter_types: vec![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;
@ -15,10 +15,10 @@ impl BuiltInFunction for Output {
Ok(Value::Empty)
}
fn type_definition(&self) -> crate::TypeDefinition {
TypeDefinition::new(Type::Function {
fn r#type(&self) -> Type {
Type::Function {
parameter_types: vec![Type::Any],
return_type: Box::new(Type::Empty),
})
}
}
}

View File

@ -1,6 +1,6 @@
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;
@ -20,11 +20,11 @@ impl BuiltInFunction for Random {
Ok(random_argument.clone())
}
fn type_definition(&self) -> TypeDefinition {
TypeDefinition::new(Type::Function {
fn r#type(&self) -> Type {
Type::Function {
parameter_types: vec![Type::List(Box::new(Type::Any))],
return_type: Box::new(Type::Any),
})
}
}
}
@ -41,11 +41,11 @@ impl BuiltInFunction for RandomInteger {
Ok(Value::Integer(random()))
}
fn type_definition(&self) -> crate::TypeDefinition {
TypeDefinition::new(Type::Function {
fn r#type(&self) -> Type {
Type::Function {
parameter_types: Vec::with_capacity(0),
return_type: Box::new(Type::Integer),
})
}
}
}
@ -62,11 +62,11 @@ impl BuiltInFunction for RandomFloat {
Ok(Value::Float(random()))
}
fn type_definition(&self) -> crate::TypeDefinition {
TypeDefinition::new(Type::Function {
fn r#type(&self) -> Type {
Type::Function {
parameter_types: Vec::with_capacity(0),
return_type: Box::new(Type::Float),
})
}
}
}
@ -83,10 +83,10 @@ impl BuiltInFunction for RandomBoolean {
Ok(Value::Boolean(random()))
}
fn type_definition(&self) -> crate::TypeDefinition {
TypeDefinition::new(Type::Function {
fn r#type(&self) -> Type {
Type::Function {
parameter_types: Vec::with_capacity(0),
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;
@ -32,10 +32,10 @@ impl BuiltInFunction for TypeFunction {
}
}
fn type_definition(&self) -> crate::TypeDefinition {
TypeDefinition::new(Type::Function {
fn r#type(&self) -> Type {
Type::Function {
parameter_types: vec![Type::String],
return_type: Box::new(Type::Any),
})
}
}
}

View File

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