1
0

Begin implementing structures

This commit is contained in:
Jeff 2024-03-19 17:18:36 -04:00
parent 6d50ac5b37
commit c7b189a83f
3 changed files with 7 additions and 22 deletions

View File

@ -12,7 +12,6 @@ use super::{AbstractTree, Action};
pub enum Type { pub enum Type {
Any, Any,
Boolean, Boolean,
Custom(Identifier),
Float, Float,
Function { Function {
parameter_types: Vec<Type>, parameter_types: Vec<Type>,
@ -26,6 +25,10 @@ pub enum Type {
None, None,
Range, Range,
String, String,
Structure {
name: Identifier,
fields: Vec<(Identifier, Type)>,
},
} }
impl Type { impl Type {
@ -45,16 +48,6 @@ impl Type {
| (Type::None, Type::None) | (Type::None, Type::None)
| (Type::Range, Type::Range) | (Type::Range, Type::Range)
| (Type::String, Type::String) => Ok(()), | (Type::String, Type::String) => Ok(()),
(Type::Custom(left), Type::Custom(right)) => {
if left == right {
Ok(())
} else {
Err(TypeConflict {
actual: other.clone(),
expected: self.clone(),
})
}
}
(Type::ListOf(left), Type::ListOf(right)) => { (Type::ListOf(left), Type::ListOf(right)) => {
if let Ok(()) = left.check(right) { if let Ok(()) = left.check(right) {
Ok(()) Ok(())
@ -113,7 +106,6 @@ impl Display for Type {
match self { match self {
Type::Any => write!(f, "any"), Type::Any => write!(f, "any"),
Type::Boolean => write!(f, "boolean"), Type::Boolean => write!(f, "boolean"),
Type::Custom(name) => write!(f, "{name}"),
Type::Float => write!(f, "float"), Type::Float => write!(f, "float"),
Type::Integer => write!(f, "integer"), Type::Integer => write!(f, "integer"),
Type::List => write!(f, "list"), Type::List => write!(f, "list"),
@ -147,6 +139,7 @@ impl Display for Type {
write!(f, ") : {return_type}") write!(f, ") : {return_type}")
} }
Type::Structure { .. } => todo!(),
} }
} }
} }
@ -159,10 +152,6 @@ mod tests {
fn check_same_types() { fn check_same_types() {
assert_eq!(Type::Any.check(&Type::Any), Ok(())); assert_eq!(Type::Any.check(&Type::Any), Ok(()));
assert_eq!(Type::Boolean.check(&Type::Boolean), Ok(())); assert_eq!(Type::Boolean.check(&Type::Boolean), Ok(()));
assert_eq!(
Type::Custom(Identifier::new("foo")).check(&Type::Custom(Identifier::new("foo"))),
Ok(())
);
assert_eq!(Type::Float.check(&Type::Float), Ok(())); assert_eq!(Type::Float.check(&Type::Float), Ok(()));
assert_eq!(Type::Integer.check(&Type::Integer), Ok(())); assert_eq!(Type::Integer.check(&Type::Integer), Ok(()));
assert_eq!(Type::List.check(&Type::List), Ok(())); assert_eq!(Type::List.check(&Type::List), Ok(()));
@ -183,8 +172,8 @@ mod tests {
#[test] #[test]
fn errors() { fn errors() {
let foo = Type::Custom(Identifier::new("foo")); let foo = Type::Integer;
let bar = Type::Custom(Identifier::new("bar")); let bar = Type::String;
assert_eq!( assert_eq!(
foo.check(&bar), foo.check(&bar),

View File

@ -101,9 +101,6 @@ pub fn parser<'src>() -> impl Parser<
just(Token::Keyword("range")).to(Type::Range), just(Token::Keyword("range")).to(Type::Range),
just(Token::Keyword("str")).to(Type::String), just(Token::Keyword("str")).to(Type::String),
just(Token::Keyword("list")).to(Type::List), just(Token::Keyword("list")).to(Type::List),
identifier
.clone()
.map(|identifier| Type::Custom(identifier)),
)) ))
}) })
.map_with(|r#type, state| r#type.with_position(state.span())); .map_with(|r#type, state| r#type.with_position(state.span()));

View File

@ -3,7 +3,6 @@ use std::{
collections::BTreeMap, collections::BTreeMap,
fmt::{self, Display, Formatter}, fmt::{self, Display, Formatter},
io::stdin, io::stdin,
num::ParseIntError,
ops::Range, ops::Range,
sync::{Arc, OnceLock}, sync::{Arc, OnceLock},
}; };