diff options
Diffstat (limited to 'users/glittershark/achilles/src/common')
-rw-r--r-- | users/glittershark/achilles/src/common/env.rs | 59 | ||||
-rw-r--r-- | users/glittershark/achilles/src/common/error.rs | 59 | ||||
-rw-r--r-- | users/glittershark/achilles/src/common/mod.rs | 6 | ||||
-rw-r--r-- | users/glittershark/achilles/src/common/namer.rs | 122 |
4 files changed, 0 insertions, 246 deletions
diff --git a/users/glittershark/achilles/src/common/env.rs b/users/glittershark/achilles/src/common/env.rs deleted file mode 100644 index 59a5e46c466f..000000000000 --- a/users/glittershark/achilles/src/common/env.rs +++ /dev/null @@ -1,59 +0,0 @@ -use std::borrow::Borrow; -use std::collections::HashMap; -use std::hash::Hash; -use std::mem; - -/// A lexical environment -#[derive(Debug, PartialEq, Eq)] -pub struct Env<K: Eq + Hash, V>(Vec<HashMap<K, V>>); - -impl<K, V> Default for Env<K, V> -where - K: Eq + Hash, -{ - fn default() -> Self { - Self::new() - } -} - -impl<K, V> Env<K, V> -where - K: Eq + Hash, -{ - pub fn new() -> Self { - Self(vec![Default::default()]) - } - - pub fn push(&mut self) { - self.0.push(Default::default()); - } - - pub fn pop(&mut self) { - self.0.pop(); - } - - pub fn save(&mut self) -> Self { - mem::take(self) - } - - pub fn restore(&mut self, saved: Self) { - *self = saved; - } - - pub fn set(&mut self, k: K, v: V) { - self.0.last_mut().unwrap().insert(k, v); - } - - pub fn resolve<'a, Q>(&'a self, k: &Q) -> Option<&'a V> - where - K: Borrow<Q>, - Q: Hash + Eq + ?Sized, - { - for ctx in self.0.iter().rev() { - if let Some(res) = ctx.get(k) { - return Some(res); - } - } - None - } -} diff --git a/users/glittershark/achilles/src/common/error.rs b/users/glittershark/achilles/src/common/error.rs deleted file mode 100644 index 51575a895e91..000000000000 --- a/users/glittershark/achilles/src/common/error.rs +++ /dev/null @@ -1,59 +0,0 @@ -use std::{io, result}; - -use thiserror::Error; - -use crate::{codegen, interpreter, parser, tc}; - -#[derive(Error, Debug)] -pub enum Error { - #[error(transparent)] - IOError(#[from] io::Error), - - #[error("Error parsing input: {0}")] - ParseError(#[from] parser::Error), - - #[error("Error evaluating expression: {0}")] - EvalError(#[from] interpreter::Error), - - #[error("Compile error: {0}")] - CodegenError(#[from] codegen::Error), - - #[error("Type error: {0}")] - TypeError(#[from] tc::Error), - - #[error("{0}")] - Message(String), -} - -impl From<String> for Error { - fn from(s: String) -> Self { - Self::Message(s) - } -} - -impl<'a> From<&'a str> for Error { - fn from(s: &'a str) -> Self { - Self::Message(s.to_owned()) - } -} - -impl<'a> From<nom::Err<nom::error::Error<&'a str>>> for Error { - fn from(e: nom::Err<nom::error::Error<&'a str>>) -> Self { - use nom::error::Error as NomError; - use nom::Err::*; - - Self::ParseError(match e { - Incomplete(i) => Incomplete(i), - Error(NomError { input, code }) => Error(NomError { - input: input.to_owned(), - code, - }), - Failure(NomError { input, code }) => Failure(NomError { - input: input.to_owned(), - code, - }), - }) - } -} - -pub type Result<T> = result::Result<T, Error>; diff --git a/users/glittershark/achilles/src/common/mod.rs b/users/glittershark/achilles/src/common/mod.rs deleted file mode 100644 index 8368a6dd180f..000000000000 --- a/users/glittershark/achilles/src/common/mod.rs +++ /dev/null @@ -1,6 +0,0 @@ -pub(crate) mod env; -pub(crate) mod error; -pub(crate) mod namer; - -pub use error::{Error, Result}; -pub use namer::{Namer, NamerOf}; diff --git a/users/glittershark/achilles/src/common/namer.rs b/users/glittershark/achilles/src/common/namer.rs deleted file mode 100644 index 016e9f6ed99a..000000000000 --- a/users/glittershark/achilles/src/common/namer.rs +++ /dev/null @@ -1,122 +0,0 @@ -use std::fmt::Display; -use std::marker::PhantomData; - -pub struct Namer<T, F> { - make_name: F, - counter: u64, - _phantom: PhantomData<T>, -} - -impl<T, F> Namer<T, F> { - pub fn new(make_name: F) -> Self { - Namer { - make_name, - counter: 0, - _phantom: PhantomData, - } - } -} - -impl Namer<String, Box<dyn Fn(u64) -> String>> { - pub fn with_prefix<T>(prefix: T) -> Self - where - T: Display + 'static, - { - Namer::new(move |i| format!("{}{}", prefix, i)).boxed() - } - - pub fn with_suffix<T>(suffix: T) -> Self - where - T: Display + 'static, - { - Namer::new(move |i| format!("{}{}", i, suffix)).boxed() - } - - pub fn alphabetic() -> Self { - Namer::new(|i| { - if i <= 26 { - std::char::from_u32((i + 96) as u32).unwrap().to_string() - } else { - format!( - "{}{}", - std::char::from_u32(((i % 26) + 96) as u32).unwrap(), - i - 26 - ) - } - }) - .boxed() - } -} - -impl<T, F> Namer<T, F> -where - F: Fn(u64) -> T, -{ - pub fn make_name(&mut self) -> T { - self.counter += 1; - (self.make_name)(self.counter) - } - - pub fn boxed(self) -> NamerOf<T> - where - F: 'static, - { - Namer { - make_name: Box::new(self.make_name), - counter: self.counter, - _phantom: self._phantom, - } - } - - pub fn map<G, U>(self, f: G) -> NamerOf<U> - where - G: Fn(T) -> U + 'static, - T: 'static, - F: 'static, - { - Namer { - counter: self.counter, - make_name: Box::new(move |x| f((self.make_name)(x))), - _phantom: PhantomData, - } - } -} - -pub type NamerOf<T> = Namer<T, Box<dyn Fn(u64) -> T>>; - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn prefix() { - let mut namer = Namer::with_prefix("t"); - assert_eq!(namer.make_name(), "t1"); - assert_eq!(namer.make_name(), "t2"); - } - - #[test] - fn suffix() { - let mut namer = Namer::with_suffix("t"); - assert_eq!(namer.make_name(), "1t"); - assert_eq!(namer.make_name(), "2t"); - } - - #[test] - fn alphabetic() { - let mut namer = Namer::alphabetic(); - assert_eq!(namer.make_name(), "a"); - assert_eq!(namer.make_name(), "b"); - (0..25).for_each(|_| { - namer.make_name(); - }); - assert_eq!(namer.make_name(), "b2"); - } - - #[test] - fn custom_callback() { - let mut namer = Namer::new(|n| n + 1); - assert_eq!(namer.make_name(), 2); - assert_eq!(namer.make_name(), 3); - } -} |