Skip to content

Commit

Permalink
Don't pass context to built-in functions
Browse files Browse the repository at this point in the history
  • Loading branch information
arendjr committed Mar 24, 2024
1 parent 73e866f commit 746fc0a
Showing 1 changed file with 26 additions and 78 deletions.
104 changes: 26 additions & 78 deletions crates/core/src/pattern/built_in_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl GritCall for CallBuiltIn {
context: &Context<'a>,
logs: &mut AnalysisLogs,
) -> Result<ResolvedPattern<'a>> {
context.built_ins.call(self, state, context, logs)
context.built_ins.call(self, context, state, logs)
}
}

Expand All @@ -73,12 +73,7 @@ impl Name for CallBuiltIn {
// eg. capitalize returns an owned string_constant pattern, but unique would return a borrowed
// value.

type F = dyn for<'a> Fn(
&'a [Option<Pattern>],
&Context<'a>,
&mut State<'a>,
&mut AnalysisLogs,
) -> Result<ResolvedPattern<'a>>
type F = dyn for<'a> Fn(Vec<Option<ResolvedPattern<'a>>>, &mut State<'a>) -> Result<ResolvedPattern<'a>>
+ Send
+ Sync;

Expand All @@ -91,12 +86,10 @@ pub struct BuiltInFunction {
impl BuiltInFunction {
fn call<'a>(
&self,
args: &'a [Option<Pattern>],
args: Vec<Option<ResolvedPattern<'a>>>,
state: &mut State<'a>,
context: &Context<'a>,
logs: &mut AnalysisLogs,
) -> Result<ResolvedPattern<'a>> {
(self.func)(args, context, state, logs)
(self.func)(args, state)
}

pub fn new(name: &'static str, params: Vec<&'static str>, func: Box<F>) -> Self {
Expand All @@ -117,14 +110,16 @@ impl std::fmt::Debug for BuiltInFunction {
pub struct BuiltIns(Vec<BuiltInFunction>);

impl BuiltIns {
pub(crate) fn call<'a>(
fn call<'a>(
&self,
call: &'a CallBuiltIn,
state: &mut State<'a>,
context: &Context<'a>,
state: &mut State<'a>,
logs: &mut AnalysisLogs,
) -> Result<ResolvedPattern<'a>> {
self.0[call.index].call(&call.args, state, context, logs)
let args = patterns_to_resolved(&call.args, state, context, logs)?;

self.0[call.index].call(args, state)
}

pub fn extend_builtins(&mut self, other: BuiltIns) -> Result<()> {
Expand Down Expand Up @@ -180,13 +175,9 @@ impl From<Vec<BuiltInFunction>> for BuiltIns {

/// Turn an arbitrary path into a resolved and normalized absolute path
fn resolve_path_fn<'a>(
args: &'a [Option<Pattern>],
context: &Context<'a>,
args: Vec<Option<ResolvedPattern<'a>>>,
state: &mut State<'a>,
logs: &mut AnalysisLogs,
) -> Result<ResolvedPattern<'a>> {
let args = patterns_to_resolved(args, state, context, logs)?;

let current_file = get_absolute_file_name(state)?;
let target_path = match &args[0] {
Some(resolved_pattern) => resolved_pattern.text(&state.files)?,
Expand All @@ -207,13 +198,9 @@ fn capitalize(s: &str) -> String {
}

fn capitalize_fn<'a>(
args: &'a [Option<Pattern>],
context: &Context<'a>,
args: Vec<Option<ResolvedPattern<'a>>>,
state: &mut State<'a>,
logs: &mut AnalysisLogs,
) -> Result<ResolvedPattern<'a>> {
let args = patterns_to_resolved(args, state, context, logs)?;

let s = match &args[0] {
Some(resolved_pattern) => resolved_pattern.text(&state.files)?,
None => return Err(anyhow!("No argument provided for capitalize function")),
Expand All @@ -222,13 +209,9 @@ fn capitalize_fn<'a>(
}

fn lowercase_fn<'a>(
args: &'a [Option<Pattern>],
context: &Context<'a>,
args: Vec<Option<ResolvedPattern<'a>>>,
state: &mut State<'a>,
logs: &mut AnalysisLogs,
) -> Result<ResolvedPattern<'a>> {
let args = patterns_to_resolved(args, state, context, logs)?;

let s = match &args[0] {
Some(resolved_pattern) => resolved_pattern.text(&state.files)?,
None => return Err(anyhow!("lowercase takes 1 argument")),
Expand All @@ -237,13 +220,9 @@ fn lowercase_fn<'a>(
}

fn uppercase_fn<'a>(
args: &'a [Option<Pattern>],
context: &Context<'a>,
args: Vec<Option<ResolvedPattern<'a>>>,
state: &mut State<'a>,
logs: &mut AnalysisLogs,
) -> Result<ResolvedPattern<'a>> {
let args = patterns_to_resolved(args, state, context, logs)?;

let s = match &args[0] {
Some(resolved_pattern) => resolved_pattern.text(&state.files)?,
None => return Err(anyhow!("uppercase takes 1 argument")),
Expand All @@ -252,28 +231,20 @@ fn uppercase_fn<'a>(
}

fn text_fn<'a>(
args: &'a [Option<Pattern>],
context: &Context<'a>,
args: Vec<Option<ResolvedPattern<'a>>>,
state: &mut State<'a>,
logs: &mut AnalysisLogs,
) -> Result<ResolvedPattern<'a>> {
let args = patterns_to_resolved(args, state, context, logs)?;

let s = match &args[0] {
Some(resolved_pattern) => resolved_pattern.text(&state.files)?,
None => return Err(anyhow!("text takes 1 argument")),
let s = match args.first() {
Some(Some(resolved_pattern)) => resolved_pattern.text(&state.files)?,
_ => return Err(anyhow!("text takes 1 argument")),
};
Ok(ResolvedPattern::from_string(s.to_string()))
}

fn trim_fn<'a>(
args: &'a [Option<Pattern>],
context: &Context<'a>,
args: Vec<Option<ResolvedPattern<'a>>>,
state: &mut State<'a>,
logs: &mut AnalysisLogs,
) -> Result<ResolvedPattern<'a>> {
let args = patterns_to_resolved(args, state, context, logs)?;

let trim_chars = match &args[1] {
Some(resolved_pattern) => resolved_pattern.text(&state.files)?,
None => return Err(anyhow!("trim takes 2 arguments: string and trim_chars")),
Expand All @@ -291,13 +262,9 @@ fn trim_fn<'a>(
}

fn split_fn<'a>(
args: &'a [Option<Pattern>],
context: &Context<'a>,
args: Vec<Option<ResolvedPattern<'a>>>,
state: &mut State<'a>,
logs: &mut AnalysisLogs,
) -> Result<ResolvedPattern<'a>> {
let args = patterns_to_resolved(args, state, context, logs)?;

let string = if let Some(string) = &args[0] {
string.text(&state.files)?
} else {
Expand All @@ -316,14 +283,10 @@ fn split_fn<'a>(
}

fn random_fn<'a>(
args: &'a [Option<Pattern>],
context: &Context<'a>,
args: Vec<Option<ResolvedPattern<'a>>>,
state: &mut State<'a>,
logs: &mut AnalysisLogs,
) -> Result<ResolvedPattern<'a>> {
let args = patterns_to_resolved(args, state, context, logs)?;

match &args[..] {
match args.as_slice() {
[Some(start), Some(end)] => {
let start = start.text(&state.files)?;
let end = end.text(&state.files)?;
Expand All @@ -348,13 +311,9 @@ fn random_fn<'a>(
}

fn join_fn<'a>(
args: &'a [Option<Pattern>],
context: &Context<'a>,
args: Vec<Option<ResolvedPattern<'a>>>,
state: &mut State<'a>,
logs: &mut AnalysisLogs,
) -> Result<ResolvedPattern<'a>> {
let args = patterns_to_resolved(args, state, context, logs)?;

let separator = &args[1];
let separator = match separator {
Some(resolved_pattern) => resolved_pattern.text(&state.files)?,
Expand All @@ -380,13 +339,9 @@ fn join_fn<'a>(
}

fn distinct_fn<'a>(
args: &'a [Option<Pattern>],
context: &Context<'a>,
state: &mut State<'a>,
logs: &mut AnalysisLogs,
args: Vec<Option<ResolvedPattern<'a>>>,
_state: &mut State<'a>,
) -> Result<ResolvedPattern<'a>> {
let args = patterns_to_resolved(args, state, context, logs)?;

let list = args.into_iter().next().unwrap();
match list {
Some(ResolvedPattern::List(list)) => {
Expand Down Expand Up @@ -429,12 +384,9 @@ fn distinct_fn<'a>(

// Shuffle a list
fn shuffle_fn<'a>(
args: &'a [Option<Pattern>],
context: &Context<'a>,
args: Vec<Option<ResolvedPattern<'a>>>,
state: &mut State<'a>,
logs: &mut AnalysisLogs,
) -> Result<ResolvedPattern<'a>> {
let args = patterns_to_resolved(args, state, context, logs)?;
let list = args
.into_iter()
.next()
Expand Down Expand Up @@ -476,13 +428,9 @@ fn shuffle_fn<'a>(
}

fn length_fn<'a>(
args: &'a [Option<Pattern>],
context: &Context<'a>,
args: Vec<Option<ResolvedPattern<'a>>>,
state: &mut State<'a>,
logs: &mut AnalysisLogs,
) -> Result<ResolvedPattern<'a>> {
let args = patterns_to_resolved(args, state, context, logs)?;

let list = args.into_iter().next().unwrap();
match &list {
Some(ResolvedPattern::List(list)) => {
Expand Down

0 comments on commit 746fc0a

Please sign in to comment.