From 79b973fc0aff7c312559383190319b60d7fe1d2d Mon Sep 17 00:00:00 2001 From: fox0 <15684995+fox0@users.noreply.github.com> Date: Mon, 21 Oct 2024 20:40:39 +0700 Subject: [PATCH] awk/calc: Remove lazy_static --- Cargo.lock | 2 - awk/Cargo.toml | 1 - awk/src/compiler.rs | 344 ++++++++++++++++++++++++----------------- calc/Cargo.toml | 1 - calc/bc_util/parser.rs | 24 ++- 5 files changed, 209 insertions(+), 163 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b89f8b4a..d2169d44 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1271,7 +1271,6 @@ version = "0.2.1" dependencies = [ "clap", "gettext-rs", - "lazy_static", "lexical", "libc", "pest", @@ -1287,7 +1286,6 @@ dependencies = [ "bigdecimal", "clap", "gettext-rs", - "lazy_static", "pest", "pest_derive", "plib", diff --git a/awk/Cargo.toml b/awk/Cargo.toml index 7a204133..b6842b71 100644 --- a/awk/Cargo.toml +++ b/awk/Cargo.toml @@ -12,7 +12,6 @@ clap.workspace = true libc.workspace = true pest = "2.7" pest_derive = "2.7" -lazy_static = "1.4" lexical = { version = "6.1", features = ["format"] } rand = {version = "0.8", default-features = false, features = ["small_rng"] } diff --git a/awk/src/compiler.rs b/awk/src/compiler.rs index e5ed63ef..4f6c2e38 100644 --- a/awk/src/compiler.rs +++ b/awk/src/compiler.rs @@ -14,20 +14,18 @@ use crate::program::{ SourceLocation, SpecialVar, VarId, }; use crate::regex::Regex; + use pest::error::InputLocation; -use pest::{ - iterators::{Pair, Pairs}, - pratt_parser::PrattParser, - Parser, -}; +use pest::iterators::{Pair, Pairs}; +use pest::pratt_parser::{Assoc, Op, PrattParser}; +use pest::Parser; +use std::cell::{Cell, RefCell}; +use std::collections::HashMap; use std::ffi::CString; +use std::hash::Hash; use std::rc::Rc; use std::str::Chars; -use std::{ - cell::{Cell, RefCell}, - collections::HashMap, - hash::Hash, -}; +use std::sync::LazyLock; struct BuiltinFunctionInfo { function: BuiltinFunction, @@ -35,151 +33,205 @@ struct BuiltinFunctionInfo { max_args: u16, } -lazy_static::lazy_static! { - static ref BUILTIN_FUNCTIONS: HashMap = HashMap::from([ - (Rule::atan2, BuiltinFunctionInfo { - function: BuiltinFunction::Atan2, - min_args: 2, - max_args: 2, - }), - (Rule::cos, BuiltinFunctionInfo { - function: BuiltinFunction::Cos, - min_args: 1, - max_args: 1, - }), - (Rule::sin, BuiltinFunctionInfo { - function: BuiltinFunction::Sin, - min_args: 1, - max_args: 1, - }), - (Rule::exp, BuiltinFunctionInfo { - function: BuiltinFunction::Exp, - min_args: 1, - max_args: 1, - }), - (Rule::log, BuiltinFunctionInfo { - function: BuiltinFunction::Log, - min_args: 1, - max_args: 1, - }), - (Rule::sqrt, BuiltinFunctionInfo { - function: BuiltinFunction::Sqrt, - min_args: 1, - max_args: 1, - }), - (Rule::int, BuiltinFunctionInfo { - function: BuiltinFunction::Int, - min_args: 1, - max_args: 1, - }), - (Rule::rand, BuiltinFunctionInfo { - function: BuiltinFunction::Rand, - min_args: 0, - max_args: 0, - }), - (Rule::srand, BuiltinFunctionInfo { - function: BuiltinFunction::Srand, - min_args: 0, - max_args: 1, - }), - - (Rule::gsub, BuiltinFunctionInfo { - function: BuiltinFunction::Gsub, - min_args: 2, - max_args: 3, - }), - (Rule::index, BuiltinFunctionInfo { - function: BuiltinFunction::Index, - min_args: 2, - max_args: 2, - }), - (Rule::length, BuiltinFunctionInfo { - function: BuiltinFunction::Length, - min_args: 0, - max_args: 1, - }), - (Rule::match_fn, BuiltinFunctionInfo { - function: BuiltinFunction::Match, - min_args: 2, - max_args: 2, - }), - (Rule::split, BuiltinFunctionInfo { - function: BuiltinFunction::Split, - min_args: 2, - max_args: 3, - }), - (Rule::sprintf, BuiltinFunctionInfo { - function: BuiltinFunction::Sprintf, - min_args: 1, - max_args: u16::MAX, - }), - (Rule::sub, BuiltinFunctionInfo { - function: BuiltinFunction::Sub, - min_args: 2, - max_args: 3, - }), - (Rule::substr, BuiltinFunctionInfo { - function: BuiltinFunction::Substr, - min_args: 2, - max_args: 3, - }), - (Rule::tolower, BuiltinFunctionInfo { - function: BuiltinFunction::ToLower, - min_args: 1, - max_args: 1, - }), - (Rule::toupper, BuiltinFunctionInfo { - function: BuiltinFunction::ToUpper, - min_args: 1, - max_args: 1, - }), - - (Rule::close, BuiltinFunctionInfo { - function: BuiltinFunction::Close, - min_args: 1, - max_args: 1, - }), - (Rule::fflush, BuiltinFunctionInfo { - function: BuiltinFunction::FFlush, - min_args: 0, - max_args: 1, - }), - (Rule::system, BuiltinFunctionInfo { - function: BuiltinFunction::System, - min_args: 1, - max_args: 1, - }) - ]); -} - -lazy_static::lazy_static! { - static ref PRATT_PARSER: PrattParser = { - use pest::pratt_parser::{Assoc, Op}; +static BUILTIN_FUNCTIONS: LazyLock> = LazyLock::new(|| { + HashMap::from([ + ( + Rule::atan2, + BuiltinFunctionInfo { + function: BuiltinFunction::Atan2, + min_args: 2, + max_args: 2, + }, + ), + ( + Rule::cos, + BuiltinFunctionInfo { + function: BuiltinFunction::Cos, + min_args: 1, + max_args: 1, + }, + ), + ( + Rule::sin, + BuiltinFunctionInfo { + function: BuiltinFunction::Sin, + min_args: 1, + max_args: 1, + }, + ), + ( + Rule::exp, + BuiltinFunctionInfo { + function: BuiltinFunction::Exp, + min_args: 1, + max_args: 1, + }, + ), + ( + Rule::log, + BuiltinFunctionInfo { + function: BuiltinFunction::Log, + min_args: 1, + max_args: 1, + }, + ), + ( + Rule::sqrt, + BuiltinFunctionInfo { + function: BuiltinFunction::Sqrt, + min_args: 1, + max_args: 1, + }, + ), + ( + Rule::int, + BuiltinFunctionInfo { + function: BuiltinFunction::Int, + min_args: 1, + max_args: 1, + }, + ), + ( + Rule::rand, + BuiltinFunctionInfo { + function: BuiltinFunction::Rand, + min_args: 0, + max_args: 0, + }, + ), + ( + Rule::srand, + BuiltinFunctionInfo { + function: BuiltinFunction::Srand, + min_args: 0, + max_args: 1, + }, + ), + ( + Rule::gsub, + BuiltinFunctionInfo { + function: BuiltinFunction::Gsub, + min_args: 2, + max_args: 3, + }, + ), + ( + Rule::index, + BuiltinFunctionInfo { + function: BuiltinFunction::Index, + min_args: 2, + max_args: 2, + }, + ), + ( + Rule::length, + BuiltinFunctionInfo { + function: BuiltinFunction::Length, + min_args: 0, + max_args: 1, + }, + ), + ( + Rule::match_fn, + BuiltinFunctionInfo { + function: BuiltinFunction::Match, + min_args: 2, + max_args: 2, + }, + ), + ( + Rule::split, + BuiltinFunctionInfo { + function: BuiltinFunction::Split, + min_args: 2, + max_args: 3, + }, + ), + ( + Rule::sprintf, + BuiltinFunctionInfo { + function: BuiltinFunction::Sprintf, + min_args: 1, + max_args: u16::MAX, + }, + ), + ( + Rule::sub, + BuiltinFunctionInfo { + function: BuiltinFunction::Sub, + min_args: 2, + max_args: 3, + }, + ), + ( + Rule::substr, + BuiltinFunctionInfo { + function: BuiltinFunction::Substr, + min_args: 2, + max_args: 3, + }, + ), + ( + Rule::tolower, + BuiltinFunctionInfo { + function: BuiltinFunction::ToLower, + min_args: 1, + max_args: 1, + }, + ), + ( + Rule::toupper, + BuiltinFunctionInfo { + function: BuiltinFunction::ToUpper, + min_args: 1, + max_args: 1, + }, + ), + ( + Rule::close, + BuiltinFunctionInfo { + function: BuiltinFunction::Close, + min_args: 1, + max_args: 1, + }, + ), + ( + Rule::fflush, + BuiltinFunctionInfo { + function: BuiltinFunction::FFlush, + min_args: 0, + max_args: 1, + }, + ), + ( + Rule::system, + BuiltinFunctionInfo { + function: BuiltinFunction::System, + min_args: 1, + max_args: 1, + }, + ), + ]) +}); - // Precedence is defined lowest to highest - PrattParser::new() +static PRATT_PARSER: LazyLock> = LazyLock::new(|| { + // Precedence is defined lowest to highest + PrattParser::new() .op(Op::infix(Rule::or, Assoc::Left)) .op(Op::infix(Rule::and, Assoc::Left)) - .op(Op::infix(Rule::in_op, Assoc::Left)) - .op(Op::infix(Rule::match_op, Assoc::Left) - | Op::infix(Rule::not_match, Assoc::Left)) + .op(Op::infix(Rule::in_op, Assoc::Left)) + .op(Op::infix(Rule::match_op, Assoc::Left) | Op::infix(Rule::not_match, Assoc::Left)) .op(Op::infix(Rule::comp_op, Assoc::Left)) .op(Op::infix(Rule::concat, Assoc::Left)) - .op(Op::infix(Rule::add, Assoc::Left) - | Op::infix(Rule::binary_sub, Assoc::Left)) + .op(Op::infix(Rule::add, Assoc::Left) | Op::infix(Rule::binary_sub, Assoc::Left)) .op(Op::infix(Rule::mul, Assoc::Left) | Op::infix(Rule::div, Assoc::Left) | Op::infix(Rule::modulus, Assoc::Left)) - .op(Op::prefix(Rule::not) - | Op::prefix(Rule::negate) - | Op::prefix(Rule::unary_plus)) + .op(Op::prefix(Rule::not) | Op::prefix(Rule::negate) | Op::prefix(Rule::unary_plus)) .op(Op::infix(Rule::pow, Assoc::Right)) - .op(Op::prefix(Rule::pre_inc) - | Op::prefix(Rule::pre_dec)) - .op(Op::postfix(Rule::post_inc) - | Op::postfix(Rule::post_dec)) - }; -} + .op(Op::prefix(Rule::pre_inc) | Op::prefix(Rule::pre_dec)) + .op(Op::postfix(Rule::post_inc) | Op::postfix(Rule::post_dec)) +}); #[derive(pest_derive::Parser, Default)] #[grammar = "grammar.pest"] diff --git a/calc/Cargo.toml b/calc/Cargo.toml index a568378d..f7bef9a3 100644 --- a/calc/Cargo.toml +++ b/calc/Cargo.toml @@ -12,7 +12,6 @@ regex.workspace = true clap.workspace = true pest = { version = "2.7", default-features = false } pest_derive = "2.7" -lazy_static = "1.4" bigdecimal = "0.4" rustyline = { version = "14.0", default-features = false } diff --git a/calc/bc_util/parser.rs b/calc/bc_util/parser.rs index cae469ed..2764e970 100644 --- a/calc/bc_util/parser.rs +++ b/calc/bc_util/parser.rs @@ -7,27 +7,25 @@ // SPDX-License-Identifier: MIT // -use std::rc::Rc; - -use pest::{error::InputLocation, iterators::Pair, pratt_parser::PrattParser, Parser, Position}; - use super::instructions::*; -lazy_static::lazy_static! { - static ref PRATT_PARSER: PrattParser = { - use pest::pratt_parser::{Assoc, Op}; +use pest::error::InputLocation; +use pest::iterators::Pair; +use pest::pratt_parser::{Assoc, Op, PrattParser}; +use pest::{Parser, Position}; +use std::rc::Rc; +use std::sync::LazyLock; - // Precedence is defined lowest to highest - PrattParser::new() - .op(Op::infix(Rule::add, Assoc::Left) - | Op::infix(Rule::sub, Assoc::Left)) +static PRATT_PARSER: LazyLock> = LazyLock::new(|| { + // Precedence is defined lowest to highest + PrattParser::new() + .op(Op::infix(Rule::add, Assoc::Left) | Op::infix(Rule::sub, Assoc::Left)) .op(Op::infix(Rule::mul, Assoc::Left) | Op::infix(Rule::div, Assoc::Left) | Op::infix(Rule::modulus, Assoc::Left)) .op(Op::infix(Rule::pow, Assoc::Right)) .op(Op::prefix(Rule::neg)) - }; -} +}); fn first_char(s: &str) -> char { s.chars().next().unwrap()