Implement custom type

This commit is contained in:
Jeff 2024-01-10 14:25:35 -05:00
parent b7db177bd2
commit c4908dc00d

View File

@ -3,13 +3,14 @@ use std::fmt::{self, Display, Formatter};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node; use tree_sitter::Node;
use crate::{AbstractTree, Error, Format, Map, Result, Structure, Value}; use crate::{AbstractTree, Error, Format, Identifier, Map, Result, Structure, Value};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub enum Type { pub enum Type {
Any, Any,
Boolean, Boolean,
Collection, Collection,
Custom(Identifier),
Float, Float,
Function { Function {
parameter_types: Vec<Type>, parameter_types: Vec<Type>,
@ -62,6 +63,16 @@ impl Type {
| (Type::Float, Type::Number) | (Type::Float, Type::Number)
| (Type::None, Type::None) | (Type::None, Type::None)
| (Type::String, Type::String) => Ok(()), | (Type::String, Type::String) => Ok(()),
(Type::Custom(left), Type::Custom(right)) => {
if left == right {
Ok(())
} else {
Err(Error::TypeCheck {
expected: self.clone(),
actual: other.clone(),
})
}
}
(Type::Option(left), Type::Option(right)) => { (Type::Option(left), Type::Option(right)) => {
if left == right { if left == right {
Ok(()) Ok(())
@ -209,6 +220,8 @@ impl Format for Type {
Type::Any => output.push_str("any"), Type::Any => output.push_str("any"),
Type::Boolean => output.push_str("bool"), Type::Boolean => output.push_str("bool"),
Type::Collection => output.push_str("collection"), Type::Collection => output.push_str("collection"),
Type::Custom(_) => todo!(),
Type::Float => output.push_str("float"), Type::Float => output.push_str("float"),
Type::Function { Type::Function {
parameter_types, parameter_types,
@ -258,6 +271,7 @@ impl Display for Type {
Type::Any => write!(f, "any"), Type::Any => write!(f, "any"),
Type::Boolean => write!(f, "bool"), Type::Boolean => write!(f, "bool"),
Type::Collection => write!(f, "collection"), Type::Collection => write!(f, "collection"),
Type::Custom(identifier) => write!(f, "{identifier}"),
Type::Float => write!(f, "float"), Type::Float => write!(f, "float"),
Type::Function { Type::Function {
parameter_types, parameter_types,