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

Execute async #162

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ path-absolutize = { version = "3.1.1", optional = false, features = [
"use_unix_paths_on_wasm",
] }
getrandom = { version = "0.2.11", optional = true }
futures = "0.3.30"

[dev-dependencies]
similar = "2.2.1"
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub trait Context {

fn ignore_limit_pattern(&self) -> bool;

fn call_built_in<'a>(
async fn call_built_in<'a>(
&self,
call: &'a CallBuiltIn,
context: &'a Self,
Expand Down
13 changes: 7 additions & 6 deletions crates/core/src/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ impl Problem {
})
}

fn execute_and_send(
async fn execute_and_send(
&self,
tx: &Sender<Vec<MatchResult>>,
files: &[impl TryIntoInputFile + FileName],
Expand All @@ -463,7 +463,7 @@ impl Problem {
context: &ExecutionContext,
mut done_files: Vec<MatchResult>,
) {
let mut outputs = match self.execute(binding, owned_files, context) {
let mut outputs = match self.execute(binding, owned_files, context).await {
Result::Err(err) => files
.iter()
.map(|file| {
Expand Down Expand Up @@ -653,7 +653,7 @@ impl Problem {
}
}

fn execute(
async fn execute(
&self,
binding: FilePattern,
owned_files: &FileOwners,
Expand Down Expand Up @@ -696,7 +696,8 @@ impl Problem {
let binding = binding.into();
if self
.pattern
.execute(&binding, &mut state, &context, &mut user_logs)?
.execute(&binding, &mut state, &context, &mut user_logs)
.await?
{
for file in state.files.files() {
if let Some(result) = MatchResult::file_to_match_result(file, &self.language)? {
Expand Down Expand Up @@ -839,14 +840,14 @@ impl<'a> Context for MarzanoContext<'a> {
self.runtime.ignore_limit_pattern
}

fn call_built_in<'b>(
async fn call_built_in<'b>(
&self,
call: &'b CallBuiltIn,
context: &'b Self,
state: &mut State<'b>,
logs: &mut AnalysisLogs,
) -> Result<ResolvedPattern<'b>> {
self.built_ins.call(call, context, state, logs)
self.built_ins.call(call, context, state, logs).await
}

#[cfg(all(
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/pattern/accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl Name for Accessor {
}

impl Matcher for Accessor {
fn execute<'a>(
async fn execute<'a>(
&'a self,
binding: &ResolvedPattern<'a>,
state: &mut State<'a>,
Expand All @@ -181,7 +181,7 @@ impl Matcher for Accessor {
Some(PatternOrResolved::ResolvedBinding(r)) => {
execute_resolved_with_binding(&r, binding, state)
}
Some(PatternOrResolved::Pattern(p)) => p.execute(binding, state, context, logs),
Some(PatternOrResolved::Pattern(p)) => p.execute(binding, state, context, logs).await,
None => Ok(
matches!(binding, ResolvedPattern::Constant(Constant::Boolean(false)))
|| binding.matches_undefined(),
Expand Down
16 changes: 10 additions & 6 deletions crates/core/src/pattern/accumulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl Name for Accumulate {
}

impl Matcher for Accumulate {
fn execute<'a>(
async fn execute<'a>(
&'a self,
context_node: &ResolvedPattern<'a>,
state: &mut State<'a>,
Expand All @@ -148,7 +148,7 @@ impl Matcher for Accumulate {
) -> Result<bool> {
if let Pattern::Variable(var) = &self.left {
let var = state.trace_var(var);
let append = ResolvedPattern::from_pattern(&self.right, state, context, logs)?;
let append = ResolvedPattern::from_pattern(&self.right, state, context, logs).await?;
if let Some(base) = state.bindings[var.scope].back_mut().unwrap()[var.index]
.value
.as_mut()
Expand All @@ -162,7 +162,11 @@ impl Matcher for Accumulate {
)
}
} else {
let resolved = if !self.left.execute(context_node, state, context, logs)? {
let resolved = if !self
.left
.execute(context_node, state, context, logs)
.await?
{
return Ok(false);
} else {
Cow::Borrowed(context_node)
Expand Down Expand Up @@ -197,7 +201,7 @@ impl Matcher for Accumulate {
}
};
let mut replacement: ResolvedPattern<'_> =
ResolvedPattern::from_dynamic_pattern(dynamic_right, state, context, logs)?;
ResolvedPattern::from_dynamic_pattern(dynamic_right, state, context, logs).await?;
let effects: Result<Vec<Effect>> = bindings
.iter()
.map(|b| {
Expand All @@ -218,15 +222,15 @@ impl Matcher for Accumulate {
}

impl Evaluator for Accumulate {
fn execute_func<'a>(
async fn execute_func<'a>(
&'a self,
state: &mut State<'a>,
context: &'a impl Context,
logs: &mut AnalysisLogs,
) -> Result<FuncEvaluation> {
if let Pattern::Variable(var) = &self.left {
let var = state.trace_var(var);
let append = ResolvedPattern::from_pattern(&self.right, state, context, logs)?;
let append = ResolvedPattern::from_pattern(&self.right, state, context, logs).await?;
if let Some(base) = state.bindings[var.scope].back_mut().unwrap()[var.index]
.value
.as_mut()
Expand Down
14 changes: 7 additions & 7 deletions crates/core/src/pattern/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,24 @@ impl Add {
Ok(Self::new(left, right))
}

pub(crate) fn call<'a>(
pub(crate) async fn call<'a>(
&'a self,
state: &mut State<'a>,
context: &'a impl Context,
logs: &mut AnalysisLogs,
) -> Result<ResolvedPattern<'a>> {
let res = self.evaluate(state, context, logs)?;
let res = self.evaluate(state, context, logs).await?;
Ok(ResolvedPattern::Constant(Constant::Float(res)))
}

fn evaluate<'a>(
async fn evaluate<'a>(
&'a self,
state: &mut State<'a>,
context: &'a impl Context,
logs: &mut AnalysisLogs,
) -> Result<f64> {
let lhs = self.lhs.float(state, context, logs)?;
let rhs = self.rhs.float(state, context, logs)?;
let lhs = self.lhs.float(state, context, logs).await?;
let rhs = self.rhs.float(state, context, logs).await?;
let res = lhs + rhs;
Ok(res)
}
Expand All @@ -93,7 +93,7 @@ impl Name for Add {
}

impl Matcher for Add {
fn execute<'a>(
async fn execute<'a>(
&'a self,
binding: &ResolvedPattern<'a>,
state: &mut State<'a>,
Expand All @@ -102,7 +102,7 @@ impl Matcher for Add {
) -> Result<bool> {
let binding_text = binding.text(&state.files)?;
let binding_int = binding_text.parse::<f64>()?;
let target = self.evaluate(state, context, logs)?;
let target = self.evaluate(state, context, logs).await?;
Ok(binding_int == target)
}
}
22 changes: 13 additions & 9 deletions crates/core/src/pattern/after.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ impl After {
Ok(Self::new(pattern))
}

pub(crate) fn next_pattern<'a>(
pub(crate) async fn next_pattern<'a>(
&'a self,
state: &mut State<'a>,
context: &'a impl Context,
logs: &mut AnalysisLogs,
) -> Result<ResolvedPattern<'a>> {
let binding = pattern_to_binding(&self.after, state, context, logs)?;
let binding = pattern_to_binding(&self.after, state, context, logs).await?;
let Some(node) = binding.as_node() else {
bail!("cannot get the node after this binding")
};
Expand All @@ -79,7 +79,7 @@ impl Name for After {
}

impl Matcher for After {
fn execute<'a>(
async fn execute<'a>(
&'a self,
binding: &ResolvedPattern<'a>,
init_state: &mut State<'a>,
Expand All @@ -101,12 +101,16 @@ impl Matcher for After {
return Ok(true);
};
let prev_node = resolve!(node.previous_named_node());
if !self.after.execute(
&ResolvedPattern::from_node(prev_node),
&mut cur_state,
context,
logs,
)? {
if !self
.after
.execute(
&ResolvedPattern::from_node(prev_node),
&mut cur_state,
context,
logs,
)
.await?
{
return Ok(false);
}
*init_state = cur_state;
Expand Down
8 changes: 4 additions & 4 deletions crates/core/src/pattern/and.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ impl Name for And {
}

impl Matcher for And {
fn execute<'a>(
async fn execute<'a>(
&'a self,
binding: &ResolvedPattern<'a>,
state: &mut State<'a>,
context: &'a impl Context,
logs: &mut AnalysisLogs,
) -> Result<bool> {
for p in self.patterns.iter() {
if !p.execute(binding, state, context, logs)? {
if !p.execute(binding, state, context, logs).await? {
return Ok(false);
};
}
Expand Down Expand Up @@ -132,14 +132,14 @@ impl Name for PrAnd {
}

impl Evaluator for PrAnd {
fn execute_func<'a>(
async fn execute_func<'a>(
&'a self,
state: &mut State<'a>,
context: &'a impl Context,
logs: &mut AnalysisLogs,
) -> Result<FuncEvaluation> {
for p in self.predicates.iter() {
let res = p.execute_func(state, context, logs)?;
let res = p.execute_func(state, context, logs).await?;
match res.predicator {
true => {}
false => return Ok(res),
Expand Down
12 changes: 8 additions & 4 deletions crates/core/src/pattern/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl Matcher for Any {
// apply all successful updates to the state
// must have at least one successful match
// return soft and failed on failure
fn execute<'a>(
async fn execute<'a>(
&'a self,
binding: &ResolvedPattern<'a>,
init_state: &mut State<'a>,
Expand All @@ -79,7 +79,10 @@ impl Matcher for Any {
let mut cur_state = init_state.clone();
for pattern in &self.patterns {
let state = cur_state.clone();
if pattern.execute(binding, &mut cur_state, context, logs)? {
if pattern
.execute(binding, &mut cur_state, context, logs)
.await?
{
matched = true;
} else {
cur_state = state;
Expand Down Expand Up @@ -143,7 +146,7 @@ impl Name for PrAny {
}

impl Evaluator for PrAny {
fn execute_func<'a>(
async fn execute_func<'a>(
&'a self,
init_state: &mut State<'a>,
context: &'a impl Context,
Expand All @@ -154,7 +157,8 @@ impl Evaluator for PrAny {
for predicate in &self.predicates {
let state = cur_state.clone();
if predicate
.execute_func(&mut cur_state, context, logs)?
.execute_func(&mut cur_state, context, logs)
.await?
.predicator
{
matched = true;
Expand Down
8 changes: 4 additions & 4 deletions crates/core/src/pattern/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,28 +75,28 @@ impl Name for Assignment {
}

impl Matcher for Assignment {
fn execute<'a>(
async fn execute<'a>(
&'a self,
_context_node: &ResolvedPattern<'a>,
state: &mut State<'a>,
context: &'a impl Context,
logs: &mut AnalysisLogs,
) -> Result<bool> {
let resolved = ResolvedPattern::from_pattern(&self.pattern, state, context, logs)?;
let resolved = ResolvedPattern::from_pattern(&self.pattern, state, context, logs).await?;
self.container.set_resolved(state, resolved)?;
Ok(true)
}
}

impl Evaluator for Assignment {
fn execute_func<'a>(
async fn execute_func<'a>(
&'a self,
state: &mut State<'a>,
context: &'a impl Context,
logs: &mut AnalysisLogs,
) -> Result<FuncEvaluation> {
let resolved: ResolvedPattern<'_> =
ResolvedPattern::from_pattern(&self.pattern, state, context, logs)?;
ResolvedPattern::from_pattern(&self.pattern, state, context, logs).await?;
self.container.set_resolved(state, resolved)?;
Ok(FuncEvaluation {
predicator: true,
Expand Down
Loading