Skip to content

Commit

Permalink
Merge branch 'alpha' of github.com:Wandalen/wTools into alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
Wandalen committed Mar 26, 2024
2 parents fcf7a13 + 5c6b65d commit e383d9b
Show file tree
Hide file tree
Showing 26 changed files with 687 additions and 356 deletions.
6 changes: 2 additions & 4 deletions module/move/wca/src/ca/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub( crate ) mod private
ProgramParser,
Command,
grammar::command::private::CommandFormer,
help::{ HelpGeneratorFn, HelpGeneratorArgs, HelpVariants, dot_command },
help::{ HelpGeneratorFn, HelpGeneratorOptions, HelpVariants },
};

use std::collections::HashSet;
Expand Down Expand Up @@ -202,7 +202,7 @@ pub( crate ) mod private
/// ```
pub fn help< HelpFunction >( mut self, func : HelpFunction ) -> Self
where
HelpFunction : Fn( &Dictionary, HelpGeneratorArgs< '_ > ) -> String + 'static
HelpFunction : Fn( &Dictionary, HelpGeneratorOptions< '_ > ) -> String + 'static
{
self.storage.help_generator = Some( HelpGeneratorFn::new( func ) );
self
Expand Down Expand Up @@ -255,8 +255,6 @@ pub( crate ) mod private
}
}

dot_command( &help_generator, &mut ca.dictionary );

ca
}

Expand Down
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.
137 changes: 111 additions & 26 deletions module/move/wca/src/ca/executor/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ 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;
use error_tools::return_err;
use ca::help::private::{ HelpGeneratorOptions, LevelOfDetail, generate_help_content };

// aaa : for Bohdan : how is it useful? where is it used?
// aaa : `ExecutorType` has been removed
Expand All @@ -25,46 +24,132 @@ 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.
///
/// Call command callback with 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.
///
/// # 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() )
if command.internal_command
{
_exec_internal_command( dictionary, command )
}
else
{
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
{
Routine::WithoutContext( routine ) => routine(( Args( command.subjects ), Props( command.properties ) )),
Routine::WithContext( routine ) => routine( ( Args( command.subjects ), Props( command.properties ) ), ctx ),
}
}

fn _exec_internal_command( dictionary : &Dictionary, command : VerifiedCommand ) -> Result< () >
{
match command.phrase.as_str()
{
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 );
let generator_args = HelpGeneratorOptions::former()
.command_prefix( "." )
.form();

let content = generate_help_content( dictionary, generator_args );
println!( "{content}" );
}
".?" =>
{
let generator_args = HelpGeneratorOptions::former()
.description_detailing( LevelOfDetail::Simple )
.subject_detailing( LevelOfDetail::Simple )
.property_detailing( LevelOfDetail::Simple )
.form();

let content = generate_help_content( dictionary, generator_args );
println!( "{content}" );
}
name if name.ends_with( '.' ) =>
{
let name = name.strip_suffix( '.' ).unwrap();
let commands = dictionary.search( name.strip_prefix( '.' ).unwrap_or( name ) );
if commands.is_empty()
{
return_err!( "Not found command that starts with `.{}`.", name );
}
let generator_args = HelpGeneratorOptions::former()
.command_prefix( "." )
.for_commands( commands )
.form();

Ok( () )
let content = generate_help_content( dictionary, generator_args );
println!( "{content}" );
}
name if name.ends_with( ".?" ) =>
{
let name = name.strip_suffix( ".?" ).unwrap();
let command = dictionary.command( &name.strip_prefix( '.' ).unwrap_or( name ).to_string() );
if let Some( command ) = command
{
let generator_args = HelpGeneratorOptions::former()
.for_commands([ command ])
.description_detailing( LevelOfDetail::Detailed )
.subject_detailing( LevelOfDetail::Simple )
.property_detailing( LevelOfDetail::Simple )
.with_footer( true )
.form();

let content = generate_help_content( dictionary, generator_args );
println!( "{content}" );
}
else
{
return_err!( "Not found command that starts with `.{}`.", name );
}
}
unexpected => return_err!( "Encountered an unrecognized internal command: `.{}`.", unexpected ),
}

Ok( () )
}
}

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.

Loading

0 comments on commit e383d9b

Please sign in to comment.