replaced phf with lazy_static

This commit is contained in:
PaddiM8 2020-12-10 23:40:29 +01:00
parent bc8cf4a22c
commit 29f8ac94f3
6 changed files with 119 additions and 213 deletions

116
Cargo.lock generated
View File

@ -156,7 +156,6 @@ name = "kalk"
version = "0.2.3" version = "0.2.3"
dependencies = [ dependencies = [
"lazy_static", "lazy_static",
"phf",
"regex", "regex",
"rug", "rug",
"test-case", "test-case",
@ -168,7 +167,7 @@ version = "0.3.3"
dependencies = [ dependencies = [
"ansi_term", "ansi_term",
"kalk", "kalk",
"phf", "lazy_static",
"regex", "regex",
"rug", "rug",
"rustyline", "rustyline",
@ -213,62 +212,6 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "phf"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3dfb61232e34fcb633f43d12c58f83c1df82962dcdfa565a4e866ffc17dafe12"
dependencies = [
"phf_macros",
"phf_shared",
"proc-macro-hack",
]
[[package]]
name = "phf_generator"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17367f0cc86f2d25802b2c26ee58a7b23faeccf78a396094c13dced0d0182526"
dependencies = [
"phf_shared",
"rand",
]
[[package]]
name = "phf_macros"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f6fde18ff429ffc8fe78e2bf7f8b7a5a5a6e2a8b58bc5a9ac69198bbda9189c"
dependencies = [
"phf_generator",
"phf_shared",
"proc-macro-hack",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "phf_shared"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c00cf8b9eafe68dde5e9eaa2cef8ee84a9336a47d566ec55ca16589633b65af7"
dependencies = [
"siphasher",
]
[[package]]
name = "ppv-lite86"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
[[package]]
name = "proc-macro-hack"
version = "0.5.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.24" version = "1.0.24"
@ -287,57 +230,6 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "rand"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
dependencies = [
"getrandom",
"libc",
"rand_chacha",
"rand_core",
"rand_hc",
"rand_pcg",
]
[[package]]
name = "rand_chacha"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
dependencies = [
"getrandom",
]
[[package]]
name = "rand_hc"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
dependencies = [
"rand_core",
]
[[package]]
name = "rand_pcg"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16abd0c1b639e9eb4d7c50c0b8100b0d0f849be2349829c740fe8e6eb4816429"
dependencies = [
"rand_core",
]
[[package]] [[package]]
name = "redox_syscall" name = "redox_syscall"
version = "0.1.57" version = "0.1.57"
@ -423,12 +315,6 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "siphasher"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa8f3741c7372e75519bd9346068370c9cdaabcc1f9599cbcf2a2719352286b7"
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.54" version = "1.0.54"

View File

@ -11,7 +11,6 @@ keywords = ["math", "calculator", "evaluator"]
categories = ["mathematics", "parser-implementations"] categories = ["mathematics", "parser-implementations"]
[dependencies] [dependencies]
phf = { version = "0.8", features = ["macros"] }
rug = "1.11.0" rug = "1.11.0"
test-case = "1.0.0" test-case = "1.0.0"
regex = "1" regex = "1"

View File

@ -4,34 +4,40 @@ use crate::parser::CalcError;
use crate::parser::DECL_UNIT; use crate::parser::DECL_UNIT;
use crate::prelude; use crate::prelude;
use crate::symbol_table::SymbolTable; use crate::symbol_table::SymbolTable;
use lazy_static::lazy_static;
use std::collections::HashMap;
pub const INVERSE_UNARY_FUNCS: phf::Map<&'static str, &'static str> = phf::phf_map! { lazy_static! {
"cos" => "acos", pub static ref INVERSE_UNARY_FUNCS: HashMap<&'static str, &'static str> = {
"cosec" => "acosec", let mut m = HashMap::new();
"cosech" => "cosech", m.insert("cos", "acos");
"cosh" => "acosh", m.insert("cosec", "acosec");
"cot" => "acot", m.insert("cosech", "cosech");
"coth" => "acoth", m.insert("cosh", "acosh");
"sec" => "asec", m.insert("cot", "acot");
"sech" => "asech", m.insert("coth", "acoth");
"sin" => "asin", m.insert("sec", "asec");
"sinh" => "asinh", m.insert("sech", "asech");
"tan" => "atan", m.insert("sin", "asin");
"tanh" => "atanh", m.insert("sinh", "asinh");
m.insert("tan", "atan");
m.insert("tanh", "atanh");
"acos" => "cos", m.insert("acos", "cos");
"acosec" => "cosec", m.insert("acosec", "cosec");
"acosech" => "cosech", m.insert("acosech", "cosech");
"acosh" => "cosh", m.insert("acosh", "cosh");
"acot" => "cot", m.insert("acot", "cot");
"acoth" => "coth", m.insert("acoth", "coth");
"asec" => "sec", m.insert("asec", "sec");
"asech" => "sech", m.insert("asech", "sech");
"asin" => "sin", m.insert("asin", "sin");
"asinh" => "sinh", m.insert("asinh", "sinh");
"atan" => "tan", m.insert("atan", "tan");
"atanh" => "tanh", m.insert("atanh", "tanh");
}; m
};
}
impl Expr { impl Expr {
pub fn invert(&self, symbol_table: &mut SymbolTable) -> Result<Self, CalcError> { pub fn invert(&self, symbol_table: &mut SymbolTable) -> Result<Self, CalcError> {

View File

@ -1,70 +1,79 @@
use crate::ast::Expr; use crate::ast::Expr;
use crate::interpreter; use crate::interpreter;
use funcs::*;
use lazy_static::lazy_static;
use rug::Float; use rug::Float;
use std::collections::HashMap;
use FuncType::*; use FuncType::*;
pub const INIT: &'static str = "unit deg = (rad*180)/pi"; pub const INIT: &'static str = "unit deg = (rad*180)/pi";
pub const CONSTANTS: phf::Map<&'static str, &'static str> = phf::phf_map! { lazy_static! {
"pi" => "3.14159265", pub static ref CONSTANTS: HashMap<&'static str, &'static str> = {
"π" => "3.14159265", let mut m = HashMap::new();
"e" => "2.71828182", m.insert("pi", "3.14159265");
"tau" => "6.28318530", m.insert("π", "3.14159265");
"τ" => "6.28318530", m.insert("e", "2.71828182");
"phi" => "1.61803398", m.insert("tau", "6.28318530");
"ϕ" => "1.61803398", m.insert("τ", "6.28318530");
}; m.insert("phi", "1.61803398");
m.insert("ϕ", "1.61803398");
m
};
pub static ref UNARY_FUNCS: HashMap<&'static str, (UnaryFuncInfo, &'static str)> = {
let mut m = HashMap::new();
m.insert("cos", (UnaryFuncInfo(cos, Trig), ""));
m.insert("cosec", (UnaryFuncInfo(cosec, Trig), ""));
m.insert("cosech", (UnaryFuncInfo(cosech, Trig), ""));
m.insert("cosh", (UnaryFuncInfo(cosh, Trig), ""));
m.insert("cot", (UnaryFuncInfo(cot, Trig), ""));
m.insert("coth", (UnaryFuncInfo(coth, Trig), ""));
m.insert("sec", (UnaryFuncInfo(sec, Trig), ""));
m.insert("sech", (UnaryFuncInfo(sech, Trig), ""));
m.insert("sin", (UnaryFuncInfo(sin, Trig), ""));
m.insert("sinh", (UnaryFuncInfo(sinh, Trig), ""));
m.insert("tan", (UnaryFuncInfo(tan, Trig), ""));
m.insert("tanh", (UnaryFuncInfo(tanh, Trig), ""));
use funcs::*; m.insert("acos", (UnaryFuncInfo(acos, InverseTrig), "rad"));
pub const UNARY_FUNCS: phf::Map<&'static str, (UnaryFuncInfo, &'static str)> = phf::phf_map! { m.insert("acosec", (UnaryFuncInfo(acosec, InverseTrig), "rad"));
"cos" => (UnaryFuncInfo(cos, Trig), ""), m.insert("acosech", (UnaryFuncInfo(acosech, InverseTrig), "rad"));
"cosec" => (UnaryFuncInfo(cosec, Trig), ""), m.insert("acosh", (UnaryFuncInfo(acosh, InverseTrig), "rad"));
"cosech" => (UnaryFuncInfo(cosech, Trig), ""), m.insert("acot", (UnaryFuncInfo(acot, InverseTrig), "rad"));
"cosh" => (UnaryFuncInfo(cosh, Trig), ""), m.insert("acoth", (UnaryFuncInfo(acoth, InverseTrig), "rad"));
"cot" => (UnaryFuncInfo(cot, Trig), ""), m.insert("asec", (UnaryFuncInfo(asec, InverseTrig), "rad"));
"coth" => (UnaryFuncInfo(coth, Trig), ""), m.insert("asech", (UnaryFuncInfo(asech, InverseTrig), "rad"));
"sec" => (UnaryFuncInfo(sec, Trig), ""), m.insert("asin", (UnaryFuncInfo(asin, InverseTrig), "rad"));
"sech" => (UnaryFuncInfo(sech, Trig), ""), m.insert("asinh", (UnaryFuncInfo(asinh, InverseTrig), "rad"));
"sin" => (UnaryFuncInfo(sin, Trig), ""), m.insert("atan", (UnaryFuncInfo(atan, InverseTrig), "rad"));
"sinh" => (UnaryFuncInfo(sinh, Trig), ""), m.insert("atanh", (UnaryFuncInfo(atanh, InverseTrig), "rad"));
"tan" => (UnaryFuncInfo(tan, Trig), ""),
"tanh" => (UnaryFuncInfo(tanh, Trig), ""),
"acos" => (UnaryFuncInfo(acos, InverseTrig), "rad"), m.insert("abs", (UnaryFuncInfo(abs, Other), ""));
"acosec" => (UnaryFuncInfo(acosec, InverseTrig), "rad"), m.insert("cbrt", (UnaryFuncInfo(cbrt, Other), ""));
"acosech" => (UnaryFuncInfo(acosech, InverseTrig), "rad"), m.insert("ceil", (UnaryFuncInfo(ceil, Other), ""));
"acosh" => (UnaryFuncInfo(acosh, InverseTrig), "rad"), m.insert("exp", (UnaryFuncInfo(exp, Other), ""));
"acot" => (UnaryFuncInfo(acot, InverseTrig), "rad"), m.insert("floor", (UnaryFuncInfo(floor, Other), ""));
"acoth" => (UnaryFuncInfo(acoth, InverseTrig), "rad"), m.insert("frac", (UnaryFuncInfo(frac, Other), ""));
"asec" => (UnaryFuncInfo(asec, InverseTrig), "rad"), m.insert("gamma", (UnaryFuncInfo(gamma, Other), ""));
"asech" => (UnaryFuncInfo(asech, InverseTrig), "rad"), m.insert("Γ", (UnaryFuncInfo(gamma, Other), ""));
"asin" => (UnaryFuncInfo(asin, InverseTrig), "rad"), m.insert("log", (UnaryFuncInfo(log, Other), ""));
"asinh" => (UnaryFuncInfo(asinh, InverseTrig), "rad"), m.insert("ln", (UnaryFuncInfo(ln, Other), ""));
"atan" => (UnaryFuncInfo(atan, InverseTrig), "rad"), m.insert("round", (UnaryFuncInfo(round, Other), ""));
"atanh" => (UnaryFuncInfo(atanh, InverseTrig), "rad"), m.insert("sqrt", (UnaryFuncInfo(sqrt, Other), ""));
m.insert("", (UnaryFuncInfo(sqrt, Other), ""));
"abs" => (UnaryFuncInfo(abs, Other), ""), m.insert("trunc", (UnaryFuncInfo(trunc, Other), ""));
"cbrt" => (UnaryFuncInfo(cbrt, Other), ""), m
"ceil" => (UnaryFuncInfo(ceil, Other), ""), };
"exp" => (UnaryFuncInfo(exp, Other), ""), pub static ref BINARY_FUNCS: HashMap<&'static str, (BinaryFuncInfo, &'static str)> = {
"floor" => (UnaryFuncInfo(floor, Other), ""), let mut m = HashMap::new();
"frac" => (UnaryFuncInfo(frac, Other), ""), m.insert("max", (BinaryFuncInfo(max, Other), ""));
"gamma" => (UnaryFuncInfo(gamma, Other), ""), m.insert("min", (BinaryFuncInfo(min, Other), ""));
"Γ" => (UnaryFuncInfo(gamma, Other), ""), m.insert("hyp", (BinaryFuncInfo(hyp, Other), ""));
"log" => (UnaryFuncInfo(log, Other), ""), m.insert("log", (BinaryFuncInfo(logx, Other), ""));
"ln" => (UnaryFuncInfo(ln, Other), ""), m.insert("root", (BinaryFuncInfo(nth_root, Other), ""));
"round" => (UnaryFuncInfo(round, Other), ""), m
"sqrt" => (UnaryFuncInfo(sqrt, Other), ""), };
"" => (UnaryFuncInfo(sqrt, Other), ""), }
"trunc" => (UnaryFuncInfo(trunc, Other), ""),
};
pub const BINARY_FUNCS: phf::Map<&'static str, (BinaryFuncInfo, &'static str)> = phf::phf_map! {
"max" => (BinaryFuncInfo(max, Other), ""),
"min" => (BinaryFuncInfo(min, Other), ""),
"hyp" => (BinaryFuncInfo(hyp, Other), ""),
"log" => (BinaryFuncInfo(logx, Other), ""),
"root" => (BinaryFuncInfo(nth_root, Other), ""),
};
enum FuncType { enum FuncType {
Trig, Trig,

View File

@ -19,5 +19,5 @@ kalk = { path = "../kalk", version = "^0.2.3" }
rustyline = "7.0.0" rustyline = "7.0.0"
ansi_term = "0.12" ansi_term = "0.12"
regex = "1" regex = "1"
phf = { version = "0.8", features = ["macros"] }
rug = "1.11.0" rug = "1.11.0"
lazy_static = "1.4.0"

View File

@ -1,6 +1,7 @@
use crate::output; use crate::output;
use ansi_term::Colour::{self, Cyan}; use ansi_term::Colour::{self, Cyan};
use kalk::parser; use kalk::parser;
use lazy_static::lazy_static;
use regex::Captures; use regex::Captures;
use regex::Regex; use regex::Regex;
use rustyline::completion::Completer; use rustyline::completion::Completer;
@ -14,6 +15,7 @@ use rustyline::validate::Validator;
use rustyline::{Editor, Helper}; use rustyline::{Editor, Helper};
use std::borrow::Cow; use std::borrow::Cow;
use std::borrow::Cow::Owned; use std::borrow::Cow::Owned;
use std::collections::HashMap;
use std::process; use std::process;
pub fn start(mut parser: &mut parser::Context) { pub fn start(mut parser: &mut parser::Context) {
@ -90,18 +92,22 @@ struct RLHelper {
impl Helper for RLHelper {} impl Helper for RLHelper {}
const COMPLETION_FUNCS: phf::Map<&'static str, &'static str> = phf::phf_map! { lazy_static! {
"ceil" => "⌈⌉", pub static ref COMPLETION_FUNCS: HashMap<&'static str, &'static str> = {
"deg" => "°", let mut m = HashMap::new();
"floor" => "⌊⌋", m.insert("ceil", "⌈⌉");
"gamma" => "Γ", m.insert("deg", "°");
"sum" => "Σ()", m.insert("floor", "⌊⌋");
"phi" => "ϕ", m.insert("gamma", "Γ");
"pi" => "π", m.insert("sum", "Σ()");
"sqrt" => "", m.insert("phi", "ϕ");
"tau" => "τ", m.insert("pi", "π");
"(" => "()", m.insert("sqrt", "");
}; m.insert("tau", "τ");
m.insert("(", "()");
m
};
}
impl Completer for RLHelper { impl Completer for RLHelper {
type Candidate = String; type Candidate = String;