use std::{
collections::{HashMap, HashSet},
path::PathBuf,
};
use source_map::{SourceId, Span, SpanWithSource};
use super::range_map::RangeMap;
use crate::{
structures::variables::VariableWithValue,
types::{TypeId, TypeStore},
FunctionId, GeneralContext, VariableId,
};
#[derive(Default, Debug)]
pub struct TypeMappings {
pub expressions_to_instances: RangeMap<Instance>,
pub variables_to_constraints: VariablesToTypes,
pub properties_to_types: RangeMap<TypeId>,
pub types_to_types: RangeMap<TypeId>,
pub import_statements_to_pointing_path: RangeMap<PathBuf>,
pub called_functions: HashSet<FunctionId>,
pub variable_restrictions: HashMap<(SourceId, u32), (TypeId, SpanWithSource)>,
}
#[derive(Default, Debug)]
pub struct VariablesToTypes(pub(crate) HashMap<VariableId, TypeId>);
impl TypeMappings {
pub fn print_called_functions(&self, source: &str) -> String {
let mut buf = "Called functions:\n".to_owned();
for func_id in self.called_functions.iter() {
buf.push_str(
source.get((func_id.1 as usize)..(func_id.1 as usize + 10)).unwrap_or_default(),
);
buf.push('\n')
}
buf
}
pub fn print_type_mappings(
&self,
source: &str,
env: &GeneralContext,
types: &TypeStore,
) -> String {
todo!()
}
}
#[derive(Clone, Debug)]
pub enum Instance {
LValue(VariableWithValue),
RValue(TypeId),
GValue(TypeId),
}
impl Instance {
pub fn get_variable_id(&self) -> Option<VariableId> {
match self {
Self::LValue(variable) => Some(variable.0.get_id()),
Self::RValue(_) | Self::GValue(_) => None,
}
}
pub fn get_value(&self) -> TypeId {
match self {
Instance::LValue(l) => l.1,
Instance::GValue(value) | Instance::RValue(value) => *value,
}
}
}