Skip to content

Commit

Permalink
Merge branch '0.10.0' into 0.10.0-1
Browse files Browse the repository at this point in the history
  • Loading branch information
sagiegurari authored Sep 28, 2024
2 parents b6cbe75 + dd8adf1 commit b76c80b
Show file tree
Hide file tree
Showing 109 changed files with 670 additions and 111 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## CHANGELOG

### v0.10.0

* Enhancement: Runtime - \[Breaking Change\] New Env struct enabling commands to redirect out/err to provided streams #440

### v0.9.4 (2024-09-28)

* Enhancement: Runtime - Adding halt interrupt to env #448 (thanks @nickheyer)
Expand Down
8 changes: 5 additions & 3 deletions docs/_includes/content.md
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ The run_with_context signature is as follows:
/// * `instructions` - The entire list of instructions which make up the currently running script
/// * `commands` - The currently known commands
/// * `line` - The current instruction line number (global line number after including all scripts into one global script)
/// * `env` - The current runtime env with access to out/err writers, etc...
fn run_with_context(
&self,
arguments: Vec<String>,
Expand All @@ -466,6 +467,7 @@ fn run_with_context(
instructions: &Vec<Instruction>,
commands: &mut Commands,
line: usize,
env: &mut Env,
) -> CommandResult;
```
Expand All @@ -479,7 +481,7 @@ The duckscript cli basically embeds duckscript so you can look at it as a refere
```rust
let mut context = Context::new();
duckscriptsdk::load(&mut context.commands)?;
runner::run_script_file(file, context)?;
runner::run_script_file(file, context, None)?;
```
That's it!<br>
Expand All @@ -502,10 +504,10 @@ The following public functions are available:
```rust
/// Executes the provided script with the given context
pub fn run_script(text: &str, context: Context) -> Result<Context, ScriptError>;
pub fn run_script(text: &str, context: Context, env: Option<Env>) -> Result<Context, ScriptError>;
/// Executes the provided script file with the given context
pub fn run_script_file(file: &str, context: Context) -> Result<Context, ScriptError>;
pub fn run_script_file(file: &str, context: Context, env: Option<Env>) -> Result<Context, ScriptError>;
```
<a name="editor-support"></a>
Expand Down
50 changes: 39 additions & 11 deletions duckscript/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,40 @@ mod runner_test;
use crate::expansion::{self, ExpandedValue};
use crate::parser;
use crate::types::command::{CommandResult, Commands, GoToValue};
use crate::types::env::Env;
use crate::types::error::ScriptError;
use crate::types::instruction::{
Instruction, InstructionMetaInfo, InstructionType, ScriptInstruction,
};
use crate::types::runtime::{Context, Runtime, StateValue};
use std::collections::HashMap;
use std::io::stdin;
use std::sync::atomic::Ordering;

#[derive(Debug)]
enum EndReason {
ExitCalled,
ReachedEnd,
Crash(ScriptError),
Halted
}

/// Executes the provided script with the given context
pub fn run_script(text: &str, context: Context) -> Result<Context, ScriptError> {
pub fn run_script(text: &str, context: Context, env: Option<Env>) -> Result<Context, ScriptError> {
match parser::parse_text(text) {
Ok(instructions) => run(instructions, context),
Ok(instructions) => run(instructions, context, env),
Err(error) => Err(error),
}
}

/// Executes the provided script file with the given context
pub fn run_script_file(file: &str, context: Context) -> Result<Context, ScriptError> {
pub fn run_script_file(
file: &str,
context: Context,
env: Option<Env>,
) -> Result<Context, ScriptError> {
match parser::parse_file(file) {
Ok(instructions) => run(instructions, context),
Ok(instructions) => run(instructions, context, env),
Err(error) => Err(error),
}
}
Expand All @@ -58,7 +65,7 @@ pub fn repl(mut context: Context) -> Result<Context, ScriptError> {

// add new instructions
instructions.append(&mut new_instructions);
let runtime = create_runtime(instructions.clone(), context);
let runtime = create_runtime(instructions.clone(), context, None);

let (updated_context, end_reason) = run_instructions(runtime, start, true)?;

Expand All @@ -83,17 +90,21 @@ pub fn repl(mut context: Context) -> Result<Context, ScriptError> {
}
}

fn run(instructions: Vec<Instruction>, context: Context) -> Result<Context, ScriptError> {
let runtime = create_runtime(instructions, context);
fn run(
instructions: Vec<Instruction>,
context: Context,
env: Option<Env>,
) -> Result<Context, ScriptError> {
let runtime = create_runtime(instructions, context, env);

match run_instructions(runtime, 0, false) {
Ok((context, _)) => Ok(context),
Err(error) => Err(error),
}
}

fn create_runtime(instructions: Vec<Instruction>, context: Context) -> Runtime {
let mut runtime = Runtime::new(context);
fn create_runtime(instructions: Vec<Instruction>, context: Context, env: Option<Env>) -> Runtime {
let mut runtime = Runtime::new(context, env);

let mut line = 0;
for instruction in &instructions {
Expand Down Expand Up @@ -126,6 +137,11 @@ fn run_instructions(

let mut end_reason = EndReason::ReachedEnd;
loop {
if runtime.env.halt.load(Ordering::SeqCst) {
end_reason = EndReason::Halted;
break;
}

let (instruction, meta_info) = if instructions.len() > line {
let instruction = instructions[line].clone();
let meta_info = instruction.meta_info.clone();
Expand All @@ -141,6 +157,7 @@ fn run_instructions(
instructions,
instruction,
line,
&mut runtime.env,
);

match command_result {
Expand Down Expand Up @@ -185,6 +202,7 @@ fn run_instructions(
instructions,
error,
meta_info.clone(),
&mut runtime.env,
) {
return Err(ScriptError::Runtime(error, Some(meta_info.clone())));
};
Expand Down Expand Up @@ -249,6 +267,7 @@ fn run_on_error_instruction(
instructions: &Vec<Instruction>,
error: String,
meta_info: InstructionMetaInfo,
env: &mut Env,
) -> Result<(), String> {
if commands.exists("on_error") {
let mut script_instruction = ScriptInstruction::new();
Expand All @@ -263,8 +282,15 @@ fn run_on_error_instruction(
instruction_type: InstructionType::Script(script_instruction),
};

let (command_result, output_variable) =
run_instruction(commands, variables, state, instructions, instruction, 0);
let (command_result, output_variable) = run_instruction(
commands,
variables,
state,
instructions,
instruction,
0,
env,
);

match command_result {
CommandResult::Exit(output) => {
Expand All @@ -288,6 +314,7 @@ pub fn run_instruction(
instructions: &Vec<Instruction>,
instruction: Instruction,
line: usize,
env: &mut Env,
) -> (CommandResult, Option<String>) {
let mut output_variable = None;
let command_result = match instruction.instruction_type {
Expand All @@ -314,6 +341,7 @@ pub fn run_instruction(
instructions,
commands,
line,
env,
)
} else {
command_instance.run(command_arguments)
Expand Down
Loading

0 comments on commit b76c80b

Please sign in to comment.