use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub struct Name(pub u32);
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub struct StrId(pub u32);
#[derive(Default)]
struct Interner {
map: HashMap<Rc<str>, u32>,
items: Vec<Rc<str>>,
}
impl Interner {
fn get(&mut self, s: &str) -> u32 {
if let Some(&i) = self.map.get(s) {
return i;
}
let i = self.items.len() as u32;
let r: Rc<str> = Rc::from(s);
self.items.push(r.clone());
self.map.insert(r, i);
i
}
}
thread_local! {
static NAMES: RefCell<Interner> = RefCell::new(Interner::default());
static STRS: RefCell<Interner> = RefCell::new(Interner::default());
}
pub fn name(s: &str) -> Name {
NAMES.with(|n| Name(n.borrow_mut().get(s)))
}
pub fn name_str(n: Name) -> Rc<str> {
NAMES.with(|t| t.borrow().items[n.0 as usize].clone())
}
pub fn str_id(s: &str) -> StrId {
STRS.with(|n| StrId(n.borrow_mut().get(s)))
}
pub fn str_of(id: StrId) -> Rc<str> {
STRS.with(|t| t.borrow().items[id.0 as usize].clone())
}
impl std::fmt::Display for Name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&name_str(*self))
}
}
thread_local! {
static MV_TYPES: RefCell<Vec<u8>> = const { RefCell::new(Vec::new()) };
}
thread_local! {
static ANY_TYPED: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}
pub fn model_value(name: &str) -> StrId {
let b = name.as_bytes();
if b.len() > 2 && b[1] == b'_' {
ANY_TYPED.with(|c| c.set(true));
}
str_id(name)
}
pub fn model_type(id: StrId) -> Option<u8> {
if !ANY_TYPED.with(|c| c.get()) {
return None;
}
MV_TYPES.with(|c| {
let mut c = c.borrow_mut();
let i = id.0 as usize;
if i >= c.len() {
c.resize(i + 1, 255);
}
if c[i] == 255 {
let s = str_of(id);
let b = s.as_bytes();
c[i] = if b.len() > 2 && b[1] == b'_' { b[0] } else { 0 };
}
if c[i] == 0 { None } else { Some(c[i]) }
})
}
pub type Loc = [u32; 4];
#[derive(Debug, Clone)]
pub struct TlaError {
pub msg: String,
pub loc: Option<Loc>,
pub module: Option<String>,
pub kind: &'static str,
}
#[derive(Debug)]
pub enum Fail {
Found,
Tla(TlaError),
Check { status: &'static str, message: String },
}
pub type R<T> = Result<T, Box<Fail>>;
pub fn err<T>(kind: &'static str, msg: String) -> R<T> {
Err(Box::new(Fail::Tla(TlaError { msg, loc: None, module: None, kind })))
}
pub fn eval_err<T>(msg: String) -> R<T> {
err("eval", msg)
}