mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 17:05:03 +02:00
Make IR the default evaluator (#13718)
# Description Makes IR the default evaluator, in preparation to remove the non-IR evaluator in a future release. # User-Facing Changes * Remove `NU_USE_IR` option * Add `NU_DISABLE_IR` option * IR is enabled unless `NU_DISABLE_IR` is set # After Submitting - [ ] release notes
This commit is contained in:
@ -70,7 +70,7 @@ impl Command for Describe {
|
||||
Example {
|
||||
description: "Describe the type of a record in a detailed way",
|
||||
example:
|
||||
"{shell:'true', uwu:true, features: {bugs:false, multiplatform:true, speed: 10}, fib: [1 1 2 3 5 8], on_save: {|x| print $'Saving ($x)'}, first_commit: 2019-05-10, my_duration: (4min + 20sec)} | describe -d",
|
||||
"{shell:'true', uwu:true, features: {bugs:false, multiplatform:true, speed: 10}, fib: [1 1 2 3 5 8], on_save: {|x| $'Saving ($x)'}, first_commit: 2019-05-10, my_duration: (4min + 20sec)} | describe -d",
|
||||
result: Some(Value::test_record(record!(
|
||||
"type" => Value::test_string("record"),
|
||||
"columns" => Value::test_record(record!(
|
||||
|
@ -83,7 +83,7 @@ impl Command for Do {
|
||||
let eval_block_with_early_return = get_eval_block_with_early_return(engine_state);
|
||||
|
||||
// Applies to all block evaluation once set true
|
||||
callee_stack.use_ir = caller_stack.has_env_var(engine_state, "NU_USE_IR");
|
||||
callee_stack.use_ir = !caller_stack.has_env_var(engine_state, "NU_DISABLE_IR");
|
||||
|
||||
let result = eval_block_with_early_return(engine_state, &mut callee_stack, block, input);
|
||||
|
||||
|
@ -77,18 +77,18 @@ impl Command for Try {
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
Example {
|
||||
description: "Try to run a missing command",
|
||||
example: "try { asdfasdf }",
|
||||
description: "Try to run a division by zero",
|
||||
example: "try { 1 / 0 }",
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
description: "Try to run a missing command",
|
||||
example: "try { asdfasdf } catch { 'missing' }",
|
||||
result: Some(Value::test_string("missing")),
|
||||
description: "Try to run a division by zero and return a string instead",
|
||||
example: "try { 1 / 0 } catch { 'divided by zero' }",
|
||||
result: Some(Value::test_string("divided by zero")),
|
||||
},
|
||||
Example {
|
||||
description: "Try to run a missing command and report the message",
|
||||
example: "try { asdfasdf } catch { |err| $err.msg }",
|
||||
description: "Try to run a division by zero and report the message",
|
||||
example: "try { 1 / 0 } catch { |err| $err.msg }",
|
||||
result: None,
|
||||
},
|
||||
]
|
||||
|
@ -1,10 +1,7 @@
|
||||
use itertools::Itertools;
|
||||
use nu_engine::command_prelude::*;
|
||||
use nu_engine::{command_prelude::*, compile};
|
||||
use nu_protocol::{
|
||||
ast::Block,
|
||||
debugger::WithoutDebug,
|
||||
engine::{StateDelta, StateWorkingSet},
|
||||
report_shell_error, Range,
|
||||
ast::Block, debugger::WithoutDebug, engine::StateWorkingSet, report_shell_error, Range,
|
||||
};
|
||||
use std::{
|
||||
sync::Arc,
|
||||
@ -77,16 +74,25 @@ pub fn eval_pipeline_without_terminal_expression(
|
||||
cwd: &std::path::Path,
|
||||
engine_state: &mut Box<EngineState>,
|
||||
) -> Option<Value> {
|
||||
let (mut block, delta) = parse(src, engine_state);
|
||||
let (mut block, mut working_set) = parse(src, engine_state);
|
||||
if block.pipelines.len() == 1 {
|
||||
let n_expressions = block.pipelines[0].elements.len();
|
||||
Arc::make_mut(&mut block).pipelines[0]
|
||||
.elements
|
||||
.truncate(&n_expressions - 1);
|
||||
// Modify the block to remove the last element and recompile it
|
||||
{
|
||||
let mut_block = Arc::make_mut(&mut block);
|
||||
mut_block.pipelines[0].elements.truncate(n_expressions - 1);
|
||||
mut_block.ir_block = Some(compile(&working_set, mut_block).expect(
|
||||
"failed to compile block modified by eval_pipeline_without_terminal_expression",
|
||||
));
|
||||
}
|
||||
working_set.add_block(block.clone());
|
||||
engine_state
|
||||
.merge_delta(working_set.render())
|
||||
.expect("failed to merge delta");
|
||||
|
||||
if !block.pipelines[0].elements.is_empty() {
|
||||
let empty_input = PipelineData::empty();
|
||||
Some(eval_block(block, empty_input, cwd, engine_state, delta))
|
||||
Some(eval_block(block, empty_input, cwd, engine_state))
|
||||
} else {
|
||||
Some(Value::nothing(Span::test_data()))
|
||||
}
|
||||
@ -96,28 +102,30 @@ pub fn eval_pipeline_without_terminal_expression(
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse(contents: &str, engine_state: &EngineState) -> (Arc<Block>, StateDelta) {
|
||||
pub fn parse<'engine>(
|
||||
contents: &str,
|
||||
engine_state: &'engine EngineState,
|
||||
) -> (Arc<Block>, StateWorkingSet<'engine>) {
|
||||
let mut working_set = StateWorkingSet::new(engine_state);
|
||||
let output = nu_parser::parse(&mut working_set, None, contents.as_bytes(), false);
|
||||
|
||||
if let Some(err) = working_set.parse_errors.first() {
|
||||
panic!("test parse error in `{contents}`: {err:?}")
|
||||
panic!("test parse error in `{contents}`: {err:?}");
|
||||
}
|
||||
|
||||
(output, working_set.render())
|
||||
if let Some(err) = working_set.compile_errors.first() {
|
||||
panic!("test compile error in `{contents}`: {err:?}");
|
||||
}
|
||||
|
||||
(output, working_set)
|
||||
}
|
||||
|
||||
pub fn eval_block(
|
||||
block: Arc<Block>,
|
||||
input: PipelineData,
|
||||
cwd: &std::path::Path,
|
||||
engine_state: &mut Box<EngineState>,
|
||||
delta: StateDelta,
|
||||
engine_state: &EngineState,
|
||||
) -> Value {
|
||||
engine_state
|
||||
.merge_delta(delta)
|
||||
.expect("Error merging delta");
|
||||
|
||||
let mut stack = Stack::new().capture();
|
||||
|
||||
stack.add_env_var("PWD".to_string(), Value::test_string(cwd.to_string_lossy()));
|
||||
@ -191,8 +199,11 @@ fn eval(
|
||||
cwd: &std::path::Path,
|
||||
engine_state: &mut Box<EngineState>,
|
||||
) -> Value {
|
||||
let (block, delta) = parse(contents, engine_state);
|
||||
eval_block(block, input, cwd, engine_state, delta)
|
||||
let (block, working_set) = parse(contents, engine_state);
|
||||
engine_state
|
||||
.merge_delta(working_set.render())
|
||||
.expect("failed to merge delta");
|
||||
eval_block(block, input, cwd, engine_state)
|
||||
}
|
||||
|
||||
pub struct DebuggableValue<'a>(pub &'a Value);
|
||||
|
Reference in New Issue
Block a user