use crate::diagnostics::Pos;
use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct TypesFile {
pub enums: Vec<EnumDecl>,
pub consts: Vec<ConstDecl>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConstDecl {
pub name: String,
pub pos: Pos,
pub params: Vec<TypeParam>,
pub ty: Option<Type>,
pub value: Value,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EnumDecl {
pub name: String,
pub pos: Pos,
pub params: Vec<TypeParam>,
pub variants: Vec<Variant>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TypeParam {
pub name: String,
pub pos: Pos,
pub underscore: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Variant {
pub name: String,
pub pos: Pos,
pub body: VariantBody,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VariantBody {
Unit,
Record(Vec<Field>),
Tuple(Vec<Parameter>),
Array(Type),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Parameter {
pub name: Option<String>,
pub pos: Pos,
pub ty: Type,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Field {
pub name: String,
pub pos: Pos,
pub ty: Type,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Type {
Ref {
name: String,
pos: Pos,
args: Vec<Type>,
},
Array(Box<Type>),
Record(Vec<Field>),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Value {
pub pos: Pos,
pub kind: ValueKind,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ValueKind {
Nat(String),
Int(String),
Float(String),
Str(String),
Array(Vec<Value>),
Record(Vec<Member>),
Tagged {
name: String,
body: Option<TaggedBody>,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TaggedBody {
Record(Vec<Member>),
Tuple(Vec<Argument>),
Array(Vec<Value>),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Argument {
pub name: Option<String>,
pub pos: Pos,
pub value: Value,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Member {
pub name: String,
pub pos: Pos,
pub value: Value,
}
impl fmt::Display for Type {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Type::Ref { name, args, .. } => {
write!(f, "{}", name)?;
if !args.is_empty() {
let args: Vec<String> = args.iter().map(|t| t.to_string()).collect();
write!(f, "<{}>", args.join(", "))?;
}
Ok(())
}
Type::Array(element) => write!(f, "[{}]", element),
Type::Record(fields) if fields.is_empty() => write!(f, "{{}}"),
Type::Record(fields) => {
let fields: Vec<String> = fields
.iter()
.map(|field| format!("{}: {}", field.name, field.ty))
.collect();
write!(f, "{{ {} }}", fields.join(", "))
}
}
}
}
impl Value {
pub fn names<'a>(&'a self, out: &mut Vec<(&'a str, Pos)>) {
match &self.kind {
ValueKind::Array(items) => items.iter().for_each(|item| item.names(out)),
ValueKind::Record(members) => members.iter().for_each(|m| m.value.names(out)),
ValueKind::Tagged { name, body } => {
out.push((name, self.pos));
match body {
Some(TaggedBody::Record(members)) => members.iter().for_each(|m| m.value.names(out)),
Some(TaggedBody::Tuple(items)) => items.iter().for_each(|item| item.value.names(out)),
Some(TaggedBody::Array(items)) => items.iter().for_each(|item| item.names(out)),
None => {}
}
}
ValueKind::Nat(_) | ValueKind::Int(_) | ValueKind::Float(_) | ValueKind::Str(_) => {}
}
}
}