Revert changes to map type
This commit is contained in:
parent
ff6cc707d2
commit
d4487117eb
@ -4,8 +4,8 @@ use serde::{Deserialize, Serialize};
|
||||
use tree_sitter::Node;
|
||||
|
||||
use crate::{
|
||||
built_in_functions::string_functions, AbstractTree, BuiltInFunction, Function, Identifier,
|
||||
List, Map, Result, Type, TypeDefinition, Value,
|
||||
built_in_functions::string_functions, AbstractTree, BuiltInFunction, Function, List, Map,
|
||||
Result, Type, Value,
|
||||
};
|
||||
|
||||
static ARGS: OnceLock<Value> = OnceLock::new();
|
||||
@ -31,29 +31,12 @@ impl BuiltInValue {
|
||||
match self {
|
||||
BuiltInValue::Args => Type::list(Type::String),
|
||||
BuiltInValue::AssertEqual => BuiltInFunction::AssertEqual.r#type(),
|
||||
BuiltInValue::Fs => Type::Map(Vec::new()),
|
||||
BuiltInValue::Json => Type::Map(Vec::new()),
|
||||
BuiltInValue::Fs => Type::Map,
|
||||
BuiltInValue::Json => Type::Map,
|
||||
BuiltInValue::Length => BuiltInFunction::Length.r#type(),
|
||||
BuiltInValue::Output => BuiltInFunction::Output.r#type(),
|
||||
BuiltInValue::Random => Type::Map(vec![
|
||||
(
|
||||
Identifier::new("boolean".to_string()),
|
||||
TypeDefinition::new(BuiltInFunction::RandomBoolean.r#type()),
|
||||
),
|
||||
(
|
||||
Identifier::new("float".to_string()),
|
||||
TypeDefinition::new(BuiltInFunction::RandomFloat.r#type()),
|
||||
),
|
||||
(
|
||||
Identifier::new("from".to_string()),
|
||||
TypeDefinition::new(BuiltInFunction::RandomFrom.r#type()),
|
||||
),
|
||||
(
|
||||
Identifier::new("integer".to_string()),
|
||||
TypeDefinition::new(BuiltInFunction::RandomInteger.r#type()),
|
||||
),
|
||||
]),
|
||||
BuiltInValue::String => Type::Map(Vec::new()),
|
||||
BuiltInValue::Random => Type::Map,
|
||||
BuiltInValue::String => Type::Map,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -46,15 +46,17 @@ impl AbstractTree for FunctionCall {
|
||||
|
||||
fn check_type(&self, context: &Map) -> Result<()> {
|
||||
let function_expression_type = self.function_expression.expected_type(context)?;
|
||||
let parameter_types = if let Type::Function {
|
||||
parameter_types, ..
|
||||
} = &function_expression_type
|
||||
{
|
||||
parameter_types
|
||||
} else {
|
||||
return Err(Error::ExpectedFunctionType {
|
||||
actual: function_expression_type,
|
||||
});
|
||||
|
||||
let parameter_types = match function_expression_type {
|
||||
Type::Function {
|
||||
parameter_types, ..
|
||||
} => parameter_types,
|
||||
Type::Any => return Ok(()),
|
||||
_ => {
|
||||
return Err(Error::TypeCheckExpectedFunction {
|
||||
actual: function_expression_type,
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
for (index, expression) in self.arguments.iter().enumerate() {
|
||||
|
@ -92,17 +92,7 @@ impl AbstractTree for Index {
|
||||
fn expected_type(&self, context: &Map) -> Result<Type> {
|
||||
match self.collection.expected_type(context)? {
|
||||
Type::List(item_type) => Ok(*item_type.clone()),
|
||||
Type::Map(identifier_types) => {
|
||||
if let IndexExpression::Identifier(index_identifier) = &self.index {
|
||||
for (identifier, r#type) in identifier_types {
|
||||
if &identifier == index_identifier {
|
||||
return Ok(r#type.take_inner());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Type::None)
|
||||
}
|
||||
Type::Map => Ok(Type::Any),
|
||||
Type::None => Ok(Type::None),
|
||||
r#type => Ok(r#type),
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ use std::fmt::{self, Display, Formatter};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tree_sitter::Node;
|
||||
|
||||
use crate::{AbstractTree, Error, Identifier, Map, Result, Value};
|
||||
use crate::{AbstractTree, Error, Map, Result, Value};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
|
||||
pub struct TypeDefinition {
|
||||
@ -61,7 +61,7 @@ pub enum Type {
|
||||
},
|
||||
Integer,
|
||||
List(Box<Type>),
|
||||
Map(Vec<(Identifier, TypeDefinition)>),
|
||||
Map,
|
||||
None,
|
||||
Number,
|
||||
String,
|
||||
@ -92,12 +92,13 @@ impl Type {
|
||||
| (Type::Collection, Type::Collection)
|
||||
| (Type::Collection, Type::List(_))
|
||||
| (Type::List(_), Type::Collection)
|
||||
| (Type::Collection, Type::Map(_))
|
||||
| (Type::Map(_), Type::Collection)
|
||||
| (Type::Collection, Type::Map)
|
||||
| (Type::Map, Type::Collection)
|
||||
| (Type::Collection, Type::String)
|
||||
| (Type::String, Type::Collection)
|
||||
| (Type::Float, Type::Float)
|
||||
| (Type::Integer, Type::Integer)
|
||||
| (Type::Map, Type::Map)
|
||||
| (Type::Number, Type::Number)
|
||||
| (Type::Number, Type::Integer)
|
||||
| (Type::Number, Type::Float)
|
||||
@ -105,16 +106,6 @@ impl Type {
|
||||
| (Type::Float, Type::Number)
|
||||
| (Type::None, Type::None)
|
||||
| (Type::String, Type::String) => Ok(()),
|
||||
(Type::Map(left), Type::Map(right)) => {
|
||||
if left == right {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::TypeCheck {
|
||||
expected: self.clone(),
|
||||
actual: other.clone(),
|
||||
})
|
||||
}
|
||||
}
|
||||
(Type::Option(left), Type::Option(right)) => {
|
||||
if left == right {
|
||||
Ok(())
|
||||
@ -224,37 +215,10 @@ impl AbstractTree for Type {
|
||||
}
|
||||
}
|
||||
"int" => Type::Integer,
|
||||
|
||||
"map" => Type::Map,
|
||||
"num" => Type::Number,
|
||||
"none" => Type::None,
|
||||
"str" => Type::String,
|
||||
"{" => {
|
||||
let child_count = node.child_count();
|
||||
let mut identifier_types = Vec::new();
|
||||
let mut identifier = None;
|
||||
|
||||
for index in 1..child_count - 1 {
|
||||
let child = node.child(index).unwrap();
|
||||
|
||||
match child.kind() {
|
||||
"identifier" => {
|
||||
identifier =
|
||||
Some(Identifier::from_syntax_node(_source, child, _context)?);
|
||||
}
|
||||
"type_definition" => {
|
||||
if let Some(identifier) = &identifier {
|
||||
let type_definition =
|
||||
TypeDefinition::from_syntax_node(_source, child, _context)?;
|
||||
|
||||
identifier_types.push((identifier.clone(), type_definition));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
Type::Map(identifier_types)
|
||||
}
|
||||
"option" => {
|
||||
let inner_type_node = node.child(2).unwrap();
|
||||
let inner_type = Type::from_syntax_node(_source, inner_type_node, _context)?;
|
||||
@ -309,15 +273,7 @@ impl Display for Type {
|
||||
}
|
||||
Type::Integer => write!(f, "int"),
|
||||
Type::List(item_type) => write!(f, "[{item_type}]"),
|
||||
Type::Map(identifier_types) => {
|
||||
write!(f, "{{")?;
|
||||
|
||||
for (identifier, r#type) in identifier_types {
|
||||
write!(f, "{} {}", identifier.inner(), r#type)?;
|
||||
}
|
||||
|
||||
write!(f, "}}")
|
||||
}
|
||||
Type::Map => write!(f, "map"),
|
||||
Type::Number => write!(f, "num"),
|
||||
Type::None => write!(f, "none"),
|
||||
Type::String => write!(f, "str"),
|
||||
|
@ -203,18 +203,7 @@ impl AbstractTree for ValueNode {
|
||||
Type::None
|
||||
}
|
||||
}
|
||||
ValueNode::Map(statements) => {
|
||||
let mut identifier_types = Vec::new();
|
||||
|
||||
for (key, (statement, _)) in statements {
|
||||
identifier_types.push((
|
||||
Identifier::new(key.clone()),
|
||||
TypeDefinition::new(statement.expected_type(context)?),
|
||||
));
|
||||
}
|
||||
|
||||
Type::Map(identifier_types)
|
||||
}
|
||||
ValueNode::Map(_) => Type::Map,
|
||||
ValueNode::BuiltInValue(built_in_value) => built_in_value.expected_type(context)?,
|
||||
};
|
||||
|
||||
|
@ -41,6 +41,10 @@ pub enum Error {
|
||||
actual: Type,
|
||||
},
|
||||
|
||||
TypeCheckExpectedFunction {
|
||||
actual: Type,
|
||||
},
|
||||
|
||||
/// The 'assert' macro did not resolve successfully.
|
||||
AssertEqualFailed {
|
||||
expected: Value,
|
||||
@ -420,6 +424,9 @@ impl fmt::Display for Error {
|
||||
f,
|
||||
"Type check error. Expected type {expected} but got type {actual}."
|
||||
),
|
||||
TypeCheckExpectedFunction { actual } => {
|
||||
write!(f, "Type check error. Expected a function but got {actual}.")
|
||||
}
|
||||
WithContext {
|
||||
error,
|
||||
location,
|
||||
|
@ -84,7 +84,7 @@ impl Value {
|
||||
));
|
||||
}
|
||||
|
||||
Type::Map(identifier_types)
|
||||
Type::Map
|
||||
}
|
||||
Value::Function(function) => function.r#type().clone(),
|
||||
Value::String(_) => Type::String,
|
||||
|
@ -352,12 +352,6 @@ module.exports = grammar({
|
||||
'num',
|
||||
'str',
|
||||
seq('[', $.type, ']'),
|
||||
seq(
|
||||
'{',
|
||||
$.identifier,
|
||||
$.type_definition,
|
||||
'}',
|
||||
),
|
||||
seq(
|
||||
'(',
|
||||
repeat(
|
||||
|
Loading…
Reference in New Issue
Block a user