This commit is contained in:
JT
2021-10-25 19:31:39 +13:00
parent 397a31e69c
commit b5965ee8ef
60 changed files with 502 additions and 455 deletions

View File

@ -1,5 +1,5 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::engine::{Command, EngineState, EvaluationContext, Stack};
use nu_protocol::{PipelineData, Signature, SyntaxShape, Value};
#[derive(Clone)]
@ -26,7 +26,8 @@ impl Command for Alias {
fn run(
&self,
_context: &EvaluationContext,
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {

View File

@ -1,5 +1,5 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::engine::{Command, EngineState, EvaluationContext, Stack};
use nu_protocol::{PipelineData, Signature, SyntaxShape, Value};
#[derive(Clone)]
@ -27,7 +27,8 @@ impl Command for Def {
fn run(
&self,
_context: &EvaluationContext,
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {

View File

@ -1,6 +1,6 @@
use nu_engine::{eval_block, CallExt};
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::engine::{Command, EngineState, EvaluationContext, Stack};
use nu_protocol::{PipelineData, Signature, SyntaxShape, Value};
#[derive(Clone)]
@ -28,18 +28,19 @@ impl Command for Do {
fn run(
&self,
context: &EvaluationContext,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let block_id = call.positional[0]
.as_block()
.expect("internal error: expected block");
let rest: Vec<Value> = call.rest(context, 1)?;
let rest: Vec<Value> = call.rest(engine_state, stack, 1)?;
let block = context.engine_state.get_block(block_id);
let block = engine_state.get_block(block_id);
let mut state = context.enter_scope();
let mut stack = stack.enter_scope();
let params: Vec<_> = block
.signature
@ -50,7 +51,7 @@ impl Command for Do {
for param in params.iter().zip(&rest) {
if let Some(var_id) = param.0.var_id {
state.add_var(var_id, param.1.clone())
stack.add_var(var_id, param.1.clone())
}
}
@ -68,7 +69,7 @@ impl Command for Do {
call.head
};
state.add_var(
stack.add_var(
param
.var_id
.expect("Internal error: rest positional parameter lacks var_id"),
@ -79,6 +80,6 @@ impl Command for Do {
)
}
}
eval_block(&state, block, input)
eval_block(&engine_state, &mut stack, block, input)
}
}

View File

@ -1,5 +1,5 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::engine::{Command, EngineState, EvaluationContext, Stack};
use nu_protocol::{PipelineData, Signature, SyntaxShape, Value};
#[derive(Clone)]
@ -27,7 +27,8 @@ impl Command for ExportDef {
fn run(
&self,
_context: &EvaluationContext,
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {

View File

@ -1,6 +1,6 @@
use nu_engine::{eval_block, eval_expression};
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::engine::{Command, EngineState, EvaluationContext, Stack};
use nu_protocol::{Example, IntoPipelineData, PipelineData, Signature, Span, SyntaxShape, Value};
#[derive(Clone)]
@ -37,7 +37,8 @@ impl Command for For {
fn run(
&self,
context: &EvaluationContext,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
@ -48,25 +49,25 @@ impl Command for For {
let keyword_expr = call.positional[1]
.as_keyword()
.expect("internal error: missing keyword");
let values = eval_expression(context, keyword_expr)?;
let values = eval_expression(engine_state, stack, keyword_expr)?;
let block = call.positional[2]
.as_block()
.expect("internal error: expected block");
let context = context.clone();
let engine_state = engine_state.clone();
let mut stack = stack.enter_scope();
match values {
Value::List { vals, span } => Ok(vals
.into_iter()
.map(move |x| {
let block = context.engine_state.get_block(block);
let block = engine_state.get_block(block);
let mut state = context.enter_scope();
let mut stack = stack.clone();
stack.add_var(var_id, x);
state.add_var(var_id, x);
match eval_block(&state, block, PipelineData::new()) {
match eval_block(&engine_state, &mut stack, block, PipelineData::new()) {
Ok(value) => Value::List {
vals: value.collect(),
span,
@ -78,13 +79,13 @@ impl Command for For {
Value::Range { val, span } => Ok(val
.into_range_iter()?
.map(move |x| {
let block = context.engine_state.get_block(block);
let block = engine_state.get_block(block);
let mut state = context.enter_scope();
let mut stack = stack.enter_scope();
state.add_var(var_id, x);
stack.add_var(var_id, x);
match eval_block(&state, block, PipelineData::new()) {
match eval_block(&engine_state, &mut stack, block, PipelineData::new()) {
Ok(value) => Value::List {
vals: value.collect(),
span,
@ -94,13 +95,13 @@ impl Command for For {
})
.into_pipeline_data()),
x => {
let block = context.engine_state.get_block(block);
let block = engine_state.get_block(block);
let mut state = context.enter_scope();
let mut stack = stack.enter_scope();
state.add_var(var_id, x);
stack.add_var(var_id, x);
eval_block(&state, block, PipelineData::new())
eval_block(&engine_state, &mut stack, block, PipelineData::new())
}
}
}

View File

@ -1,6 +1,6 @@
use nu_protocol::{
ast::Call,
engine::{Command, EvaluationContext},
engine::{Command, EngineState, EvaluationContext, Stack},
span, Example, IntoPipelineData, PipelineData, ShellError, Signature, Spanned, SyntaxShape,
Value,
};
@ -36,11 +36,12 @@ impl Command for Help {
fn run(
&self,
context: &EvaluationContext,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
help(context, call)
help(engine_state, stack, call)
}
fn examples(&self) -> Vec<Example> {
@ -74,12 +75,16 @@ impl Command for Help {
}
}
fn help(context: &EvaluationContext, call: &Call) -> Result<PipelineData, ShellError> {
fn help(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
) -> Result<PipelineData, ShellError> {
let head = call.head;
let find: Option<Spanned<String>> = call.get_flag(context, "find")?;
let rest: Vec<Spanned<String>> = call.rest(context, 0)?;
let find: Option<Spanned<String>> = call.get_flag(engine_state, stack, "find")?;
let rest: Vec<Spanned<String>> = call.rest(engine_state, stack, 0)?;
let full_commands = context.get_signatures_with_examples();
let full_commands = engine_state.get_signatures_with_examples();
if let Some(f) = find {
let search_string = f.item;
@ -164,7 +169,7 @@ fn help(context: &EvaluationContext, call: &Call) -> Result<PipelineData, ShellE
for cmd in full_commands {
if cmd.0.name == name {
let help = get_full_help(&cmd.0, &cmd.1, context);
let help = get_full_help(&cmd.0, &cmd.1, engine_state);
output.push_str(&help);
}
}

View File

@ -1,5 +1,5 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::engine::{Command, EngineState, EvaluationContext, Stack};
use nu_protocol::{PipelineData, Signature, SyntaxShape, Value};
#[derive(Clone)]
@ -20,7 +20,8 @@ impl Command for Hide {
fn run(
&self,
_context: &EvaluationContext,
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {

View File

@ -1,6 +1,6 @@
use nu_engine::{eval_block, eval_expression};
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::engine::{Command, EngineState, EvaluationContext, Stack};
use nu_protocol::{IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Value};
#[derive(Clone)]
@ -28,7 +28,8 @@ impl Command for If {
fn run(
&self,
context: &EvaluationContext,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
@ -38,24 +39,26 @@ impl Command for If {
.expect("internal error: expected block");
let else_case = call.positional.get(2);
let result = eval_expression(context, cond)?;
let result = eval_expression(engine_state, stack, cond)?;
match result {
Value::Bool { val, span } => {
if val {
let block = context.engine_state.get_block(then_block);
let state = context.enter_scope();
eval_block(&state, block, input)
let block = engine_state.get_block(then_block);
let mut stack = stack.enter_scope();
eval_block(&engine_state, &mut stack, block, input)
} else if let Some(else_case) = else_case {
if let Some(else_expr) = else_case.as_keyword() {
if let Some(block_id) = else_expr.as_block() {
let block = context.engine_state.get_block(block_id);
let state = context.enter_scope();
eval_block(&state, block, input)
let block = engine_state.get_block(block_id);
let mut stack = stack.enter_scope();
eval_block(&engine_state, &mut stack, block, input)
} else {
eval_expression(context, else_expr).map(|x| x.into_pipeline_data())
eval_expression(&engine_state, stack, else_expr)
.map(|x| x.into_pipeline_data())
}
} else {
eval_expression(context, else_case).map(|x| x.into_pipeline_data())
eval_expression(&engine_state, stack, else_case)
.map(|x| x.into_pipeline_data())
}
} else {
Ok(PipelineData::new())

View File

@ -1,6 +1,6 @@
use nu_engine::eval_expression;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::engine::{Command, EngineState, EvaluationContext, Stack};
use nu_protocol::{PipelineData, Signature, SyntaxShape, Value};
#[derive(Clone)]
@ -27,7 +27,8 @@ impl Command for Let {
fn run(
&self,
context: &EvaluationContext,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
@ -39,11 +40,11 @@ impl Command for Let {
.as_keyword()
.expect("internal error: missing keyword");
let rhs = eval_expression(context, keyword_expr)?;
let rhs = eval_expression(&engine_state, stack, keyword_expr)?;
//println!("Adding: {:?} to {}", rhs, var_id);
context.add_var(var_id, rhs);
stack.add_var(var_id, rhs);
Ok(PipelineData::new())
}
}

View File

@ -1,5 +1,5 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::engine::{Command, EngineState, EvaluationContext, Stack};
use nu_protocol::{PipelineData, Signature, SyntaxShape, Value};
#[derive(Clone)]
@ -26,7 +26,8 @@ impl Command for Module {
fn run(
&self,
_context: &EvaluationContext,
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {

View File

@ -1,6 +1,6 @@
use nu_engine::{eval_block, CallExt};
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::engine::{Command, EngineState, EvaluationContext, Stack};
use nu_protocol::{PipelineData, ShellError, Signature, SyntaxShape, Value};
/// Source a file for environment variables.
@ -26,15 +26,16 @@ impl Command for Source {
fn run(
&self,
context: &EvaluationContext,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
// Note: this hidden positional is the block_id that corresponded to the 0th position
// it is put here by the parser
let block_id: i64 = call.req(context, 1)?;
let block_id: i64 = call.req(engine_state, stack, 1)?;
let block = context.engine_state.get_block(block_id as usize).clone();
eval_block(context, &block, input)
let block = engine_state.get_block(block_id as usize).clone();
eval_block(engine_state, stack, &block, input)
}
}

View File

@ -1,5 +1,5 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::engine::{Command, EngineState, EvaluationContext, Stack};
use nu_protocol::{PipelineData, Signature, SyntaxShape, Value};
#[derive(Clone)]
@ -20,7 +20,8 @@ impl Command for Use {
fn run(
&self,
_context: &EvaluationContext,
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {