Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

awk/calc: Remove lazy_static #352

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion awk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }

Expand Down
344 changes: 198 additions & 146 deletions awk/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,172 +14,224 @@ 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,
min_args: u16,
max_args: u16,
}

lazy_static::lazy_static! {
static ref BUILTIN_FUNCTIONS: HashMap<Rule, BuiltinFunctionInfo> = 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<Rule> = {
use pest::pratt_parser::{Assoc, Op};
static BUILTIN_FUNCTIONS: LazyLock<HashMap<Rule, BuiltinFunctionInfo>> = 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<PrattParser<Rule>> = 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"]
Expand Down
Loading