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"
dependencies = [
"lazy_static",
"phf",
"regex",
"rug",
"test-case",
@ -168,7 +167,7 @@ version = "0.3.3"
dependencies = [
"ansi_term",
"kalk",
"phf",
"lazy_static",
"regex",
"rug",
"rustyline",
@ -213,62 +212,6 @@ dependencies = [
"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]]
name = "proc-macro2"
version = "1.0.24"
@ -287,57 +230,6 @@ dependencies = [
"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]]
name = "redox_syscall"
version = "0.1.57"
@ -423,12 +315,6 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "siphasher"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa8f3741c7372e75519bd9346068370c9cdaabcc1f9599cbcf2a2719352286b7"
[[package]]
name = "syn"
version = "1.0.54"

View File

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

View File

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

View File

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

View File

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

View File

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