2022-01-21 17:43:50 -06:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
use crate::util::unescape;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
2022-01-23 05:49:54 -06:00
|
|
|
pub enum Type {
|
2022-01-21 17:43:50 -06:00
|
|
|
Null,
|
|
|
|
Bool(bool),
|
2022-01-23 05:49:54 -06:00
|
|
|
Number(i64),
|
|
|
|
Str(String),
|
2022-01-21 17:43:50 -06:00
|
|
|
Symbol(String),
|
2022-01-23 05:49:54 -06:00
|
|
|
List(Rc<Vec<Type>>, Rc<Type>),
|
|
|
|
Vector(Rc<Vec<Type>>, Rc<Type>),
|
|
|
|
// Function(fn(Arguments) -> Return, Rc<Type>),
|
2022-01-21 17:43:50 -06:00
|
|
|
}
|
|
|
|
|
2022-01-23 05:49:54 -06:00
|
|
|
impl std::fmt::Display for Type {
|
2022-01-21 17:43:50 -06:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
2022-01-23 05:49:54 -06:00
|
|
|
Type::Null => write!(f, "Null"),
|
|
|
|
Type::Bool(b) => write!(f, "{}", b),
|
|
|
|
Type::Number(n) => write!(f, "{}", n),
|
|
|
|
Type::Str(s) => write!(f, "\"{}\"", unescape(s.to_string())),
|
|
|
|
Type::Symbol(s) => write!(f, "{}", s),
|
|
|
|
Type::List(l, _) => write!(f, "({})", l.iter().map(|e| format!("{}", e)).collect::<Vec<String>>().join(" ")),
|
|
|
|
Type::Vector(l, _) => write!(f, "[{}]", l.iter().map(|e| format!("{}", e)).collect::<Vec<String>>().join(", ")),
|
|
|
|
// Type::Function(func, _) => write!(f, "<{:?}>", func),
|
2022-01-21 17:43:50 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
ErrorString(String),
|
|
|
|
}
|
|
|
|
|
2022-01-22 15:36:13 -06:00
|
|
|
impl std::fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
Error::ErrorString(s) => write!(f, "{}", s),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-23 05:49:54 -06:00
|
|
|
// pub type Arguments = Vec<Type>;
|
|
|
|
pub type Return = Result<Type, Error>;
|
2022-01-21 17:43:50 -06:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! list {
|
|
|
|
($seq:expr) => {{
|
|
|
|
List(Rc::new($seq),Rc::new(Null))
|
|
|
|
}};
|
|
|
|
[$($args:expr),*] => {{
|
2022-01-23 05:49:54 -06:00
|
|
|
let v: Vec<Type> = vec![$($args),*];
|
2022-01-21 17:43:50 -06:00
|
|
|
List(Rc::new(v),Rc::new(Null))
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! vector {
|
|
|
|
($seq:expr) => {{
|
|
|
|
Vector(Rc::new($seq), Rc::new(Null))
|
|
|
|
}};
|
|
|
|
[$($args:expr),*] => {{
|
2022-01-23 05:49:54 -06:00
|
|
|
let v: Vec<Type> = vec![$($args),*];
|
2022-01-21 17:43:50 -06:00
|
|
|
Vector(Rc::new(v), Rc::new(Null))
|
|
|
|
}}
|
|
|
|
}
|