Skip to content

Commit

Permalink
Refactor executor and context code in wca module
Browse files Browse the repository at this point in the history
Removed unused files command.rs and runtime.rs under the executor module and simplified executor.rs structure for clearer logic and readability. Updated context.rs to use get() function instead of get_ref(). Also, revised the function signature in help.rs for enhanced clarity.
  • Loading branch information
Barsik-sus committed Mar 25, 2024
1 parent 8f66ae8 commit 85803ca
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 189 deletions.
46 changes: 0 additions & 46 deletions module/move/wca/src/ca/executor/command.rs

This file was deleted.

9 changes: 5 additions & 4 deletions module/move/wca/src/ca/executor/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@ pub( crate ) mod private
/// let ctx = Context::default();
///
/// ctx.insert( 42 );
/// assert_eq!( 42, *ctx.get_ref().unwrap() );
/// assert_eq!( 42, ctx.get().unwrap() );
/// ```
///
/// ```
/// # use wca::{ Routine, Context, Value, Args, Props };
/// # use std::sync::{ Arc, Mutex };
/// let routine = Routine::new_with_ctx
/// (
/// | ( args, props ), ctx |
/// {
/// let first_arg : i32 = args.get_owned( 0 ).unwrap_or_default();
/// let ctx_value : &mut i32 = ctx.get_or_default();
/// let ctx_value : Arc< Mutex< i32 > > = ctx.get_or_default();
///
/// *ctx_value += first_arg;
/// *ctx_value.lock().unwrap() += first_arg;
///
/// Ok( () )
/// }
Expand All @@ -35,7 +36,7 @@ pub( crate ) mod private
/// {
/// callback( ( Args( vec![ Value::Number( 1.0 ) ] ), Props( Default::default() ) ), ctx.clone() ).unwrap();
/// }
/// assert_eq!( 1, *ctx.get_ref().unwrap() );
/// assert_eq!( 1, *ctx.get::< Arc< Mutex< i32 > > >().unwrap().lock().unwrap() );
/// ```
// CloneAny needs to deep clone of Context
// qqq : ?
Expand Down
Empty file.
62 changes: 35 additions & 27 deletions module/move/wca/src/ca/executor/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ pub( crate ) mod private
{
use crate::*;

use ca::executor::runtime::_exec_command;
use wtools::error::Result;
use std::sync::Arc;
use std::sync::atomic::Ordering;

// aaa : for Bohdan : how is it useful? where is it used?
// aaa : `ExecutorType` has been removed
Expand All @@ -25,45 +22,56 @@ pub( crate ) mod private
{
/// Executes a program
///
/// Setup runtimes for each namespace into program and run it with specified execution type
/// Iterates over the commands in the program and executes each command using the provided dictionary.
/// This method returns a `Result` indicating whether the execution was successful or not.
///
/// # Arguments
///
/// * `dictionary` - A reference to the dictionary used to look up the command routine.
/// * `program` - The program to be executed, which is a `Program` object consisting of a list of commands.
///
/// # Returns
///
/// A `Result` with `Ok(())` if the execution was successful, or an `Err` containing an error message if an error occurred.
///
pub fn program( &self, dictionary : &Dictionary, program : Program< VerifiedCommand > ) -> Result< () >
{
let context = self.context.clone();
let runtime = Runtime
for command in program.commands
{
context,
pos : 0,
namespace : program.commands,
};

Self::sequential_execution_loop( dictionary, runtime )?;
self.command( dictionary, command )?;
}

Ok( () )
}

/// Executes a command
/// Executes a given command using a provided dictionary and command.
///
/// Calls the command callback with the given context if it is necessary.
///
/// # Arguments
///
/// * `dictionary` - A reference to the dictionary used to look up the command routine.
/// * `command` - The verified command that needs to be executed.
///
/// Call command callback with context if it is necessary.
/// # Returns
///
/// Returns a Result indicating success or failure. If successful, returns `Ok(())`, otherwise returns an error.
pub fn command( &self, dictionary : &Dictionary, command : VerifiedCommand ) -> Result< () >
{
let routine = dictionary.command( &command.phrase ).unwrap().routine.clone();
_exec_command( command, routine, self.context.clone() )
}

// qqq : for Bohdan : probably redundant
// aaa : for Bohdan : probably redundant
// aaa : removed `parallel_execution_loop`

fn sequential_execution_loop( dictionary : &Dictionary, mut runtime : Runtime ) -> Result< () >
}

fn _exec_command( command : VerifiedCommand, routine : Routine, ctx : Context ) -> Result< () >
{
match routine
{
while !runtime.is_finished()
{
let state = runtime.context.get_or_default::< Arc< RuntimeState > >();
state.pos.store( runtime.pos + 1, Ordering::Release );
runtime.r#do( &dictionary )?;
runtime.pos = runtime.context.get::< Arc< RuntimeState > >().unwrap().pos.load( Ordering::Relaxed );
}

Ok( () )
Routine::WithoutContext( routine ) => routine(( Args( command.subjects ), Props( command.properties ) )),
Routine::WithContext( routine ) => routine( ( Args( command.subjects ), Props( command.properties ) ), ctx ),
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions module/move/wca/src/ca/executor/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
crate::mod_interface!
{

/// Executor that is responsible for executing the program’s commands
layer executor;
/// Represents the state of the program's runtime
layer runtime;
/// Container for contexts values
layer context;
/// Executor that is responsible for executing the program’s commands
layer executor;
/// Command callback representation
layer routine;

Expand Down
107 changes: 0 additions & 107 deletions module/move/wca/src/ca/executor/runtime.rs

This file was deleted.

2 changes: 1 addition & 1 deletion module/move/wca/src/ca/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ pub( crate ) mod private
/// # use wca::ca::help::{ HelpGeneratorArgs, HelpGeneratorFn };
/// use wca::{ Command, Dictionary };
///
/// fn my_help_generator( grammar : &Dictionary, command : Option< &Command > ) -> String
/// fn my_help_generator( dictionary : &Dictionary, args : HelpGeneratorArgs< '_ > ) -> String
/// {
/// format!( "Help content based on grammar and command" )
/// }
Expand Down

0 comments on commit 85803ca

Please sign in to comment.