wip: (broken) working on Call reference challenges

This commit is contained in:
Devyn Cairns 2024-06-17 15:58:26 -07:00
parent f02ab3e231
commit 13e64e5f73
No known key found for this signature in database
9 changed files with 50 additions and 18 deletions

View File

@ -1,7 +1,7 @@
pub use crate::CallExt; pub use crate::CallExt;
pub use nu_protocol::{ pub use nu_protocol::{
ast::{Call, CellPath}, ast::CellPath,
engine::{Command, EngineState, Stack, StateWorkingSet}, engine::{Call, Command, EngineState, Stack, StateWorkingSet},
record, ByteStream, ByteStreamType, Category, ErrSpan, Example, IntoInterruptiblePipelineData, record, ByteStream, ByteStreamType, Category, ErrSpan, Example, IntoInterruptiblePipelineData,
IntoPipelineData, IntoSpanned, PipelineData, Record, ShellError, Signature, Span, Spanned, IntoPipelineData, IntoSpanned, PipelineData, Record, ShellError, Signature, Span, Spanned,
SyntaxShape, Type, Value, SyntaxShape, Type, Value,

View File

@ -45,10 +45,12 @@ fn nu_highlight_string(code_string: &str, engine_state: &EngineState, stack: &mu
if let Some(highlighter) = engine_state.find_decl(b"nu-highlight", &[]) { if let Some(highlighter) = engine_state.find_decl(b"nu-highlight", &[]) {
let decl = engine_state.get_decl(highlighter); let decl = engine_state.get_decl(highlighter);
let call = Call::new(Span::unknown());
if let Ok(output) = decl.run( if let Ok(output) = decl.run(
engine_state, engine_state,
stack, stack,
&Call::new(Span::unknown()), &(&call).into(),
Value::string(code_string, Span::unknown()).into_pipeline_data(), Value::string(code_string, Span::unknown()).into_pipeline_data(),
) { ) {
let result = output.into_value(Span::unknown()); let result = output.into_value(Span::unknown());
@ -269,11 +271,12 @@ fn get_documentation(
let _ = write!(long_desc, "\n > {}\n", example.example); let _ = write!(long_desc, "\n > {}\n", example.example);
} else if let Some(highlighter) = engine_state.find_decl(b"nu-highlight", &[]) { } else if let Some(highlighter) = engine_state.find_decl(b"nu-highlight", &[]) {
let decl = engine_state.get_decl(highlighter); let decl = engine_state.get_decl(highlighter);
let call = Call::new(Span::unknown());
match decl.run( match decl.run(
engine_state, engine_state,
stack, stack,
&Call::new(Span::unknown()), &(&call).into(),
Value::string(example.example, Span::unknown()).into_pipeline_data(), Value::string(example.example, Span::unknown()).into_pipeline_data(),
) { ) {
Ok(output) => { Ok(output) => {
@ -299,12 +302,13 @@ fn get_documentation(
let table = engine_state let table = engine_state
.find_decl("table".as_bytes(), &[]) .find_decl("table".as_bytes(), &[])
.and_then(|decl_id| { .and_then(|decl_id| {
let call = Call::new(Span::unknown());
engine_state engine_state
.get_decl(decl_id) .get_decl(decl_id)
.run( .run(
engine_state, engine_state,
stack, stack,
&Call::new(Span::new(0, 0)), &(&call).into(),
PipelineData::Value(result.clone(), None), PipelineData::Value(result.clone(), None),
) )
.ok() .ok()

View File

@ -176,7 +176,7 @@ pub fn eval_call<D: DebugContext>(
// We pass caller_stack here with the knowledge that internal commands // We pass caller_stack here with the knowledge that internal commands
// are going to be specifically looking for global state in the stack // are going to be specifically looking for global state in the stack
// rather than any local state. // rather than any local state.
decl.run(engine_state, caller_stack, call, input) decl.run(engine_state, caller_stack, &call.into(), input)
} }
} }
@ -223,7 +223,7 @@ fn eval_external(
} }
} }
command.run(engine_state, stack, &call, input) command.run(engine_state, stack, &(&call).into(), input)
} }
pub fn eval_expression<D: DebugContext>( pub fn eval_expression<D: DebugContext>(

View File

@ -1,8 +1,7 @@
use crate::util::MutableCow; use crate::util::MutableCow;
use nu_engine::{get_eval_block_with_early_return, get_full_help, ClosureEvalOnce}; use nu_engine::{get_eval_block_with_early_return, get_full_help, ClosureEvalOnce};
use nu_protocol::{ use nu_protocol::{
ast::Call, engine::{Call, Closure, EngineState, Redirection, Stack},
engine::{Closure, EngineState, Redirection, Stack},
Config, IntoSpanned, OutDest, PipelineData, PluginIdentity, ShellError, Span, Spanned, Value, Config, IntoSpanned, OutDest, PipelineData, PluginIdentity, ShellError, Span, Spanned, Value,
}; };
use std::{ use std::{
@ -56,7 +55,7 @@ pub struct PluginExecutionCommandContext<'a> {
identity: Arc<PluginIdentity>, identity: Arc<PluginIdentity>,
engine_state: Cow<'a, EngineState>, engine_state: Cow<'a, EngineState>,
stack: MutableCow<'a, Stack>, stack: MutableCow<'a, Stack>,
call: Cow<'a, Call>, call: Cow<'a, Call<'a>>,
} }
impl<'a> PluginExecutionCommandContext<'a> { impl<'a> PluginExecutionCommandContext<'a> {

View File

@ -1,6 +1,6 @@
use crate::{ use crate::{
ast::{Call, Expression}, ast::Expression,
engine::{Command, CommandType, EngineState, Stack}, engine::{Call, Command, CommandType, EngineState, Stack},
PipelineData, ShellError, Signature, PipelineData, ShellError, Signature,
}; };

View File

@ -1,4 +1,8 @@
use crate::{ast, ir, Span}; use crate::{
ast,
ir::{self, Instruction},
DeclId, Span,
};
/// This is a HACK to help [`Command`](super::Command) support both the old AST evaluator and the /// This is a HACK to help [`Command`](super::Command) support both the old AST evaluator and the
/// new IR evaluator at the same time. It should be removed once we are satisfied with the new /// new IR evaluator at the same time. It should be removed once we are satisfied with the new
@ -6,6 +10,7 @@ use crate::{ast, ir, Span};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Call<'a> { pub struct Call<'a> {
pub head: Span, pub head: Span,
pub decl_id: DeclId,
pub inner: CallImpl<'a>, pub inner: CallImpl<'a>,
} }
@ -14,3 +19,26 @@ pub enum CallImpl<'a> {
Ast(&'a ast::Call), Ast(&'a ast::Call),
Ir(ir::Call<'a>), Ir(ir::Call<'a>),
} }
impl<'a> From<&'a ast::Call> for Call<'a> {
fn from(call: &'a ast::Call) -> Self {
Call {
head: call.head,
decl_id: call.decl_id,
inner: CallImpl::Ast(call),
}
}
}
impl<'a> From<ir::Call<'a>> for Call<'a> {
fn from(call: ir::Call<'a>) -> Self {
let Instruction::Call { decl_id, .. } = *call.instruction else {
panic!("ir::Call instruction was not Call: {:?}", call.instruction);
};
Call {
head: *call.head,
decl_id,
inner: CallImpl::Ir(call),
}
}
}

View File

@ -1,5 +1,7 @@
use super::{EngineState, Stack, StateWorkingSet}; use super::{EngineState, Stack, StateWorkingSet};
use crate::{ast::Call, Alias, BlockId, Example, OutDest, PipelineData, ShellError, Signature}; use crate::{
ast, engine::Call, Alias, BlockId, Example, OutDest, PipelineData, ShellError, Signature,
};
use std::fmt::Display; use std::fmt::Display;
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
@ -52,7 +54,7 @@ pub trait Command: Send + Sync + CommandClone {
fn run_const( fn run_const(
&self, &self,
working_set: &StateWorkingSet, working_set: &StateWorkingSet,
call: &Call, call: &ast::Call,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
Err(ShellError::MissingConstEvalImpl { span: call.head }) Err(ShellError::MissingConstEvalImpl { span: call.head })

View File

@ -568,7 +568,7 @@ impl PipelineData {
self.write_all_and_flush(engine_state, no_newline, to_stderr) self.write_all_and_flush(engine_state, no_newline, to_stderr)
} else { } else {
let call = Call::new(Span::new(0, 0)); let call = Call::new(Span::new(0, 0));
let table = command.run(engine_state, stack, &call, self)?; let table = command.run(engine_state, stack, &(&call).into(), self)?;
table.write_all_and_flush(engine_state, no_newline, to_stderr) table.write_all_and_flush(engine_state, no_newline, to_stderr)
} }
} else { } else {

View File

@ -1,6 +1,5 @@
use crate::{ use crate::{
ast::Call, engine::{Call, Command, CommandType, EngineState, Stack},
engine::{Command, CommandType, EngineState, Stack},
BlockId, PipelineData, ShellError, SyntaxShape, Type, Value, VarId, BlockId, PipelineData, ShellError, SyntaxShape, Type, Value, VarId,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};