delete unused
This commit is contained in:
parent
d6f2be49a5
commit
5d563527fd
125
src/sym.rs
125
src/sym.rs
@ -1,125 +0,0 @@
|
||||
#![allow(unused_variables)]
|
||||
use std::collections::HashMap;
|
||||
|
||||
use rand::random;
|
||||
|
||||
use crate::checked::Type;
|
||||
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
pub struct Sym {
|
||||
pub id: u64,
|
||||
pub uid: u64,
|
||||
pub typ: Type,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Syms {
|
||||
tables: Vec<SymTable>,
|
||||
current_id: usize,
|
||||
}
|
||||
|
||||
impl Syms {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
tables: vec![SymTable::new()],
|
||||
current_id: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn enter_scope(&mut self) {
|
||||
let new_id = self.tables.len();
|
||||
self.tables.push(SymTable::from(self.current_id));
|
||||
self.current_id = new_id;
|
||||
}
|
||||
|
||||
pub fn leave_scope(&mut self) -> Result<(), ()> {
|
||||
let parent = self.tables[self.current_id].parent.ok_or(())?;
|
||||
self.current_id = parent;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get(&self, id: u64) -> Option<&Sym> {
|
||||
self.get_rec(self.current_id, id)
|
||||
}
|
||||
|
||||
fn get_rec(&self, table_id: usize, id: u64) -> Option<&Sym> {
|
||||
if let Some(sym) = self.tables[table_id].get(id) {
|
||||
return Some(sym);
|
||||
}
|
||||
if let Some(parent_id) = self.tables[table_id].parent {
|
||||
return self.get_rec(parent_id, id);
|
||||
};
|
||||
None
|
||||
}
|
||||
|
||||
pub fn defined_locally(&self, id: u64) -> bool {
|
||||
self.tables[self.current_id].defined(id)
|
||||
}
|
||||
|
||||
pub fn define(&mut self, id: u64, typ: Type) {
|
||||
self.tables[self.current_id].define(id, typ);
|
||||
}
|
||||
|
||||
pub fn table_id(&self) -> usize {
|
||||
self.current_id
|
||||
}
|
||||
|
||||
pub fn view(&self, table_id: usize) -> SymView {
|
||||
SymView {
|
||||
syms: self,
|
||||
current_id: table_id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SymView<'a> {
|
||||
syms: &'a Syms,
|
||||
current_id: usize,
|
||||
}
|
||||
|
||||
impl<'a> SymView<'a> {
|
||||
pub fn get(&self, id: u64) -> Option<&Sym> {
|
||||
self.syms.get_rec(self.current_id, id)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct SymTable {
|
||||
map: HashMap<u64, Sym>,
|
||||
parent: Option<usize>,
|
||||
}
|
||||
|
||||
impl SymTable {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
map: HashMap::new(),
|
||||
parent: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from(parent: usize) -> Self {
|
||||
Self {
|
||||
map: HashMap::new(),
|
||||
parent: Some(parent),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn defined(&self, id: u64) -> bool {
|
||||
self.map.contains_key(&id)
|
||||
}
|
||||
|
||||
pub fn get(&self, id: u64) -> Option<&Sym> {
|
||||
self.map.get(&id)
|
||||
}
|
||||
|
||||
pub fn define(&mut self, id: u64, typ: Type) {
|
||||
self.map.insert(
|
||||
id,
|
||||
Sym {
|
||||
id,
|
||||
uid: random(),
|
||||
typ,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
36
src/util.rs
36
src/util.rs
@ -1,36 +0,0 @@
|
||||
use std::hash::{DefaultHasher, Hash, Hasher};
|
||||
|
||||
pub fn hash<H: Hash>(value: H) -> u64 {
|
||||
let mut hasher = DefaultHasher::new();
|
||||
value.hash(&mut hasher);
|
||||
hasher.finish()
|
||||
}
|
||||
enum Duplicate<T: std::hash::Hash> {
|
||||
None(std::collections::HashMap<T, T>),
|
||||
Found(T),
|
||||
}
|
||||
|
||||
pub trait Itertewls<T>
|
||||
where
|
||||
Self: Iterator<Item = T> + Sized,
|
||||
{
|
||||
fn find_first_duplicate(self) -> Option<T>;
|
||||
}
|
||||
|
||||
impl<I, Item> Itertewls<Item> for I
|
||||
where
|
||||
I: Iterator<Item = Item> + Sized,
|
||||
Item: std::cmp::PartialEq + Clone,
|
||||
{
|
||||
fn find_first_duplicate(mut self) -> Option<Item> {
|
||||
self.try_fold(Vec::new(), |mut used, item| {
|
||||
if used.contains(&item) {
|
||||
Err(item)
|
||||
} else {
|
||||
used.push(item);
|
||||
Ok(used)
|
||||
}
|
||||
})
|
||||
.err()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user