From 94687a76030e4fe749c9d98198af6d4876b6c6e9 Mon Sep 17 00:00:00 2001 From: JT Date: Fri, 3 Sep 2021 06:21:37 +1200 Subject: [PATCH] Back to working state --- crates/nu-cli/src/default_context.rs | 39 +++++++++--------- crates/nu-cli/src/errors.rs | 2 +- crates/nu-cli/src/syntax_highlight.rs | 8 ++-- crates/nu-engine/src/eval.rs | 38 +++++++++--------- crates/nu-engine/src/lib.rs | 6 --- crates/nu-parser/src/errors.rs | 2 +- crates/nu-parser/src/flatten.rs | 4 +- crates/nu-parser/src/lib.rs | 4 +- crates/nu-parser/src/parser.rs | 5 ++- crates/nu-parser/src/type_check.rs | 6 ++- crates/nu-parser/tests/test_parser.rs | 40 ++++++++++--------- crates/nu-protocol/src/{ => ast}/block.rs | 2 +- crates/nu-protocol/src/{ => ast}/call.rs | 3 +- crates/nu-protocol/src/{ => ast}/expr.rs | 3 +- .../nu-protocol/src/{ => ast}/expression.rs | 3 +- crates/nu-protocol/src/ast/mod.rs | 15 +++++++ crates/nu-protocol/src/{ => ast}/operator.rs | 0 crates/nu-protocol/src/{ => ast}/pipeline.rs | 2 +- crates/nu-protocol/src/{ => ast}/statement.rs | 3 +- crates/nu-protocol/src/engine/call_info.rs | 8 ++++ .../nu-protocol/src/{ => engine}/command.rs | 12 ++++-- crates/nu-protocol/src/engine/command_args.rs | 7 ++++ .../src/{ => engine}/engine_state.rs | 3 +- .../src/engine/evaluation_context.rs} | 10 ++--- crates/nu-protocol/src/engine/mod.rs | 11 +++++ crates/nu-protocol/src/lib.rs | 20 +--------- crates/nu-protocol/src/signature.rs | 14 ++++++- src/main.rs | 16 ++++---- 28 files changed, 170 insertions(+), 116 deletions(-) rename crates/nu-protocol/src/{ => ast}/block.rs (97%) rename crates/nu-protocol/src/{ => ast}/call.rs (90%) rename crates/nu-protocol/src/{ => ast}/expr.rs (86%) rename crates/nu-protocol/src/{ => ast}/expression.rs (96%) create mode 100644 crates/nu-protocol/src/ast/mod.rs rename crates/nu-protocol/src/{ => ast}/operator.rs (100%) rename crates/nu-protocol/src/{ => ast}/pipeline.rs (90%) rename crates/nu-protocol/src/{ => ast}/statement.rs (69%) create mode 100644 crates/nu-protocol/src/engine/call_info.rs rename crates/nu-protocol/src/{ => engine}/command.rs (79%) create mode 100644 crates/nu-protocol/src/engine/command_args.rs rename crates/nu-protocol/src/{ => engine}/engine_state.rs (99%) rename crates/{nu-engine/src/state.rs => nu-protocol/src/engine/evaluation_context.rs} (93%) create mode 100644 crates/nu-protocol/src/engine/mod.rs diff --git a/crates/nu-cli/src/default_context.rs b/crates/nu-cli/src/default_context.rs index 4e349ca52c..2c59c6748e 100644 --- a/crates/nu-cli/src/default_context.rs +++ b/crates/nu-cli/src/default_context.rs @@ -1,6 +1,9 @@ use std::{cell::RefCell, rc::Rc}; -use nu_protocol::{EngineState, Signature, StateWorkingSet, SyntaxShape}; +use nu_protocol::{ + engine::{EngineState, StateWorkingSet}, + Signature, SyntaxShape, +}; pub fn create_default_context() -> Rc> { let engine_state = Rc::new(RefCell::new(EngineState::new())); @@ -10,7 +13,7 @@ pub fn create_default_context() -> Rc> { let sig = Signature::build("where").required("cond", SyntaxShape::RowCondition, "condition"); - working_set.add_decl(sig.into()); + working_set.add_decl(sig.predeclare()); let sig = Signature::build("if") .required("cond", SyntaxShape::Expression, "condition") @@ -20,7 +23,7 @@ pub fn create_default_context() -> Rc> { SyntaxShape::Keyword(b"else".to_vec(), Box::new(SyntaxShape::Expression)), "optional else followed by else block", ); - working_set.add_decl(sig.into()); + working_set.add_decl(sig.predeclare()); let sig = Signature::build("let") .required("var_name", SyntaxShape::VarWithOptType, "variable name") @@ -29,7 +32,7 @@ pub fn create_default_context() -> Rc> { SyntaxShape::Keyword(b"=".to_vec(), Box::new(SyntaxShape::Expression)), "equals sign followed by value", ); - working_set.add_decl(sig.into()); + working_set.add_decl(sig.predeclare()); let sig = Signature::build("let-env") .required("var_name", SyntaxShape::String, "variable name") @@ -38,7 +41,7 @@ pub fn create_default_context() -> Rc> { SyntaxShape::Keyword(b"=".to_vec(), Box::new(SyntaxShape::String)), "equals sign followed by value", ); - working_set.add_decl(sig.into()); + working_set.add_decl(sig.predeclare()); let sig = Signature::build("alias") .required("name", SyntaxShape::String, "name of the alias") @@ -47,16 +50,16 @@ pub fn create_default_context() -> Rc> { SyntaxShape::Keyword(b"=".to_vec(), Box::new(SyntaxShape::Expression)), "equals sign followed by value", ); - working_set.add_decl(sig.into()); + working_set.add_decl(sig.predeclare()); let sig = Signature::build("build-string").rest(SyntaxShape::String, "list of string"); - working_set.add_decl(sig.into()); + working_set.add_decl(sig.predeclare()); let sig = Signature::build("def") .required("def_name", SyntaxShape::String, "definition name") .required("params", SyntaxShape::Signature, "parameters") .required("block", SyntaxShape::Block, "body of the definition"); - working_set.add_decl(sig.into()); + working_set.add_decl(sig.predeclare()); let sig = Signature::build("for") .required( @@ -70,11 +73,11 @@ pub fn create_default_context() -> Rc> { "range of the loop", ) .required("block", SyntaxShape::Block, "the block to run"); - working_set.add_decl(sig.into()); + working_set.add_decl(sig.predeclare()); let sig = Signature::build("benchmark").required("block", SyntaxShape::Block, "the block to run"); - working_set.add_decl(sig.into()); + working_set.add_decl(sig.predeclare()); // let sig = Signature::build("foo").named("--jazz", SyntaxShape::Int, "jazz!!", Some('j')); // working_set.add_decl(sig.into()); @@ -84,25 +87,25 @@ pub fn create_default_context() -> Rc> { // .switch("--rock", "rock!!", Some('r')); // working_set.add_decl(sig.into()); let sig = Signature::build("exit"); - working_set.add_decl(sig.into()); + working_set.add_decl(sig.predeclare()); let sig = Signature::build("vars"); - working_set.add_decl(sig.into()); + working_set.add_decl(sig.predeclare()); let sig = Signature::build("decls"); - working_set.add_decl(sig.into()); + working_set.add_decl(sig.predeclare()); let sig = Signature::build("blocks"); - working_set.add_decl(sig.into()); + working_set.add_decl(sig.predeclare()); let sig = Signature::build("stack"); - working_set.add_decl(sig.into()); + working_set.add_decl(sig.predeclare()); let sig = Signature::build("add"); - working_set.add_decl(sig.into()); + working_set.add_decl(sig.predeclare()); let sig = Signature::build("add it"); - working_set.add_decl(sig.into()); + working_set.add_decl(sig.predeclare()); let sig = Signature::build("add it together") .required("x", SyntaxShape::Int, "x value") .required("y", SyntaxShape::Int, "y value"); - working_set.add_decl(sig.into()); + working_set.add_decl(sig.predeclare()); working_set.render() }; diff --git a/crates/nu-cli/src/errors.rs b/crates/nu-cli/src/errors.rs index 7115dc174a..7c620c9b9c 100644 --- a/crates/nu-cli/src/errors.rs +++ b/crates/nu-cli/src/errors.rs @@ -3,7 +3,7 @@ use core::ops::Range; use codespan_reporting::diagnostic::{Diagnostic, Label}; use codespan_reporting::term::termcolor::{ColorChoice, StandardStream}; use nu_parser::ParseError; -use nu_protocol::{ShellError, Span, StateWorkingSet}; +use nu_protocol::{engine::StateWorkingSet, ShellError, Span}; fn convert_span_to_diag( working_set: &StateWorkingSet, diff --git a/crates/nu-cli/src/syntax_highlight.rs b/crates/nu-cli/src/syntax_highlight.rs index ac58ff9068..9a57dd71ed 100644 --- a/crates/nu-cli/src/syntax_highlight.rs +++ b/crates/nu-cli/src/syntax_highlight.rs @@ -1,6 +1,6 @@ use nu_ansi_term::Style; -use nu_parser::FlatShape; -use nu_protocol::{EngineState, StateWorkingSet}; +use nu_parser::{flatten_block, parse_source, FlatShape}; +use nu_protocol::engine::{EngineState, StateWorkingSet}; use reedline::{Highlighter, StyledText}; use std::{cell::RefCell, rc::Rc}; @@ -13,9 +13,9 @@ impl Highlighter for NuHighlighter { let (shapes, global_span_offset) = { let engine_state = self.engine_state.borrow(); let mut working_set = StateWorkingSet::new(&*engine_state); - let (block, _) = working_set.parse_source(line.as_bytes(), false); + let (block, _) = parse_source(&mut working_set, line.as_bytes(), false); - let shapes = working_set.flatten_block(&block); + let shapes = flatten_block(&working_set, &block); (shapes, engine_state.next_span_start()) }; diff --git a/crates/nu-engine/src/eval.rs b/crates/nu-engine/src/eval.rs index 5013c39955..f173c19f5c 100644 --- a/crates/nu-engine/src/eval.rs +++ b/crates/nu-engine/src/eval.rs @@ -1,7 +1,7 @@ use std::time::Instant; -use crate::state::State; -use nu_protocol::{Block, Call, Expr, Expression, Operator, Statement}; +use nu_protocol::ast::{Block, Call, Expr, Expression, Operator, Statement}; +use nu_protocol::engine::EvaluationContext; use nu_protocol::{IntoRowStream, IntoValueStream, ShellError, Span, Value}; pub fn eval_operator(op: &Expression) -> Result { @@ -14,16 +14,16 @@ pub fn eval_operator(op: &Expression) -> Result { } } -fn eval_call(state: &State, call: &Call) -> Result { +fn eval_call(state: &EvaluationContext, call: &Call) -> Result { let engine_state = state.engine_state.borrow(); let decl = engine_state.get_decl(call.decl_id); - if let Some(block_id) = decl.body { + if let Some(block_id) = decl.get_custom_command() { let state = state.enter_scope(); for (arg, param) in call.positional.iter().zip( - decl.signature + decl.signature() .required_positional .iter() - .chain(decl.signature.optional_positional.iter()), + .chain(decl.signature().optional_positional.iter()), ) { let result = eval_expression(&state, arg)?; let var_id = param @@ -35,7 +35,7 @@ fn eval_call(state: &State, call: &Call) -> Result { let engine_state = state.engine_state.borrow(); let block = engine_state.get_block(block_id); eval_block(&state, block) - } else if decl.signature.name == "let" { + } else if decl.signature().name == "let" { let var_id = call.positional[0] .as_var() .expect("internal error: missing variable"); @@ -52,7 +52,7 @@ fn eval_call(state: &State, call: &Call) -> Result { Ok(Value::Nothing { span: call.positional[0].span, }) - } else if decl.signature.name == "let-env" { + } else if decl.signature().name == "let-env" { let env_var = call.positional[0] .as_string() .expect("internal error: missing variable"); @@ -70,7 +70,7 @@ fn eval_call(state: &State, call: &Call) -> Result { Ok(Value::Nothing { span: call.positional[0].span, }) - } else if decl.signature.name == "if" { + } else if decl.signature().name == "if" { let cond = &call.positional[0]; let then_block = call.positional[1] .as_block() @@ -103,7 +103,7 @@ fn eval_call(state: &State, call: &Call) -> Result { } _ => Err(ShellError::CantConvert("bool".into(), result.span())), } - } else if decl.signature.name == "build-string" { + } else if decl.signature().name == "build-string" { let mut output = vec![]; for expr in &call.positional { @@ -115,7 +115,7 @@ fn eval_call(state: &State, call: &Call) -> Result { val: output.join(""), span: call.head, }) - } else if decl.signature.name == "benchmark" { + } else if decl.signature().name == "benchmark" { let block = call.positional[0] .as_block() .expect("internal error: expected block"); @@ -130,7 +130,7 @@ fn eval_call(state: &State, call: &Call) -> Result { Ok(Value::Nothing { span: call.positional[0].span, }) - } else if decl.signature.name == "for" { + } else if decl.signature().name == "for" { let var_id = call.positional[0] .as_var() .expect("internal error: missing variable"); @@ -167,26 +167,26 @@ fn eval_call(state: &State, call: &Call) -> Result { Ok(Value::Nothing { span: call.positional[0].span, }) - } else if decl.signature.name == "vars" { + } else if decl.signature().name == "vars" { state.engine_state.borrow().print_vars(); Ok(Value::Nothing { span: call.head }) - } else if decl.signature.name == "decls" { + } else if decl.signature().name == "decls" { state.engine_state.borrow().print_decls(); Ok(Value::Nothing { span: call.head }) - } else if decl.signature.name == "blocks" { + } else if decl.signature().name == "blocks" { state.engine_state.borrow().print_blocks(); Ok(Value::Nothing { span: call.head }) - } else if decl.signature.name == "stack" { + } else if decl.signature().name == "stack" { state.print_stack(); Ok(Value::Nothing { span: call.head }) - } else if decl.signature.name == "def" || decl.signature.name == "alias" { + } else if decl.signature().name == "def" || decl.signature().name == "alias" { Ok(Value::Nothing { span: call.head }) } else { Err(ShellError::Unsupported(call.head)) } } -pub fn eval_expression(state: &State, expr: &Expression) -> Result { +pub fn eval_expression(state: &EvaluationContext, expr: &Expression) -> Result { match &expr.expr { Expr::Bool(b) => Ok(Value::Bool { val: *b, @@ -278,7 +278,7 @@ pub fn eval_expression(state: &State, expr: &Expression) -> Result Result { +pub fn eval_block(state: &EvaluationContext, block: &Block) -> Result { let mut last = Ok(Value::Nothing { span: Span { start: 0, end: 0 }, }); diff --git a/crates/nu-engine/src/lib.rs b/crates/nu-engine/src/lib.rs index 4143f4ae64..c912ee4772 100644 --- a/crates/nu-engine/src/lib.rs +++ b/crates/nu-engine/src/lib.rs @@ -1,9 +1,3 @@ -mod command; mod eval; -mod example; -mod state; -pub use command::Command; pub use eval::{eval_block, eval_expression, eval_operator}; -pub use example::Example; -pub use state::{Stack, State}; diff --git a/crates/nu-parser/src/errors.rs b/crates/nu-parser/src/errors.rs index db82f95008..5f16a34711 100644 --- a/crates/nu-parser/src/errors.rs +++ b/crates/nu-parser/src/errors.rs @@ -1,4 +1,4 @@ -use nu_protocol::{Span, StateWorkingSet, Type}; +use nu_protocol::{engine::StateWorkingSet, Span, Type}; use std::ops::Range; #[derive(Debug)] diff --git a/crates/nu-parser/src/flatten.rs b/crates/nu-parser/src/flatten.rs index dea7795c5b..a2b490bf82 100644 --- a/crates/nu-parser/src/flatten.rs +++ b/crates/nu-parser/src/flatten.rs @@ -1,5 +1,5 @@ -use nu_protocol::Span; -use nu_protocol::{Block, Expr, Expression, Pipeline, StateWorkingSet, Statement}; +use nu_protocol::ast::{Block, Expr, Expression, Pipeline, Statement}; +use nu_protocol::{engine::StateWorkingSet, Span}; #[derive(Debug)] pub enum FlatShape { diff --git a/crates/nu-parser/src/lib.rs b/crates/nu-parser/src/lib.rs index a15f2dfb42..fb2d2e8e2e 100644 --- a/crates/nu-parser/src/lib.rs +++ b/crates/nu-parser/src/lib.rs @@ -6,7 +6,7 @@ mod parser; mod type_check; pub use errors::ParseError; -pub use flatten::FlatShape; +pub use flatten::{flatten_block, FlatShape}; pub use lex::{lex, Token, TokenContents}; pub use lite_parse::{lite_parse, LiteBlock}; -pub use parser::{Import, VarDecl}; +pub use parser::{parse_file, parse_source, Import, VarDecl}; diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index 0e1afd1ef4..12e6584df8 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -5,8 +5,9 @@ use crate::{ }; use nu_protocol::{ - span, Block, BlockId, Call, DeclId, Expr, Expression, Flag, Operator, Pipeline, PositionalArg, - Signature, Span, StateWorkingSet, Statement, SyntaxShape, Type, VarId, + ast::{Block, Call, Expr, Expression, Operator, Pipeline, Statement}, + engine::StateWorkingSet, + span, BlockId, DeclId, Flag, PositionalArg, Signature, Span, SyntaxShape, Type, VarId, }; #[derive(Debug, Clone)] diff --git a/crates/nu-parser/src/type_check.rs b/crates/nu-parser/src/type_check.rs index 0e88dac135..9ea5f68eaf 100644 --- a/crates/nu-parser/src/type_check.rs +++ b/crates/nu-parser/src/type_check.rs @@ -1,5 +1,9 @@ use crate::ParseError; -use nu_protocol::{Expr, Expression, Operator, StateWorkingSet, Type}; +use nu_protocol::{ + ast::{Expr, Expression, Operator}, + engine::StateWorkingSet, + Type, +}; pub fn type_compatible(lhs: &Type, rhs: &Type) -> bool { match (lhs, rhs) { diff --git a/crates/nu-parser/tests/test_parser.rs b/crates/nu-parser/tests/test_parser.rs index e13075196b..959925d96a 100644 --- a/crates/nu-parser/tests/test_parser.rs +++ b/crates/nu-parser/tests/test_parser.rs @@ -1,13 +1,17 @@ use nu_parser::ParseError; use nu_parser::*; -use nu_protocol::{EngineState, Signature, SyntaxShape}; +use nu_protocol::{ + ast::{Expr, Expression, Statement}, + engine::{EngineState, StateWorkingSet}, + Signature, SyntaxShape, +}; #[test] pub fn parse_int() { let engine_state = EngineState::new(); let mut working_set = StateWorkingSet::new(&engine_state); - let (block, err) = working_set.parse_source(b"3", true); + let (block, err) = parse_source(&mut working_set, b"3", true); assert!(err.is_none()); assert!(block.len() == 1); @@ -26,9 +30,9 @@ pub fn parse_call() { let mut working_set = StateWorkingSet::new(&engine_state); let sig = Signature::build("foo").named("--jazz", SyntaxShape::Int, "jazz!!", Some('j')); - working_set.add_decl(sig.into()); + working_set.add_decl(sig.predeclare()); - let (block, err) = working_set.parse_source(b"foo", true); + let (block, err) = parse_source(&mut working_set, b"foo", true); assert!(err.is_none()); assert!(block.len() == 1); @@ -50,9 +54,9 @@ pub fn parse_call_missing_flag_arg() { let mut working_set = StateWorkingSet::new(&engine_state); let sig = Signature::build("foo").named("--jazz", SyntaxShape::Int, "jazz!!", Some('j')); - working_set.add_decl(sig.into()); + working_set.add_decl(sig.predeclare()); - let (_, err) = working_set.parse_source(b"foo --jazz", true); + let (_, err) = parse_source(&mut working_set, b"foo --jazz", true); assert!(matches!(err, Some(ParseError::MissingFlagParam(..)))); } @@ -62,9 +66,9 @@ pub fn parse_call_missing_short_flag_arg() { let mut working_set = StateWorkingSet::new(&engine_state); let sig = Signature::build("foo").named("--jazz", SyntaxShape::Int, "jazz!!", Some('j')); - working_set.add_decl(sig.into()); + working_set.add_decl(sig.predeclare()); - let (_, err) = working_set.parse_source(b"foo -j", true); + let (_, err) = parse_source(&mut working_set, b"foo -j", true); assert!(matches!(err, Some(ParseError::MissingFlagParam(..)))); } @@ -76,8 +80,8 @@ pub fn parse_call_too_many_shortflag_args() { let sig = Signature::build("foo") .named("--jazz", SyntaxShape::Int, "jazz!!", Some('j')) .named("--math", SyntaxShape::Int, "math!!", Some('m')); - working_set.add_decl(sig.into()); - let (_, err) = working_set.parse_source(b"foo -mj", true); + working_set.add_decl(sig.predeclare()); + let (_, err) = parse_source(&mut working_set, b"foo -mj", true); assert!(matches!( err, Some(ParseError::ShortFlagBatchCantTakeArg(..)) @@ -90,8 +94,8 @@ pub fn parse_call_unknown_shorthand() { let mut working_set = StateWorkingSet::new(&engine_state); let sig = Signature::build("foo").switch("--jazz", "jazz!!", Some('j')); - working_set.add_decl(sig.into()); - let (_, err) = working_set.parse_source(b"foo -mj", true); + working_set.add_decl(sig.predeclare()); + let (_, err) = parse_source(&mut working_set, b"foo -mj", true); assert!(matches!(err, Some(ParseError::UnknownFlag(..)))); } @@ -101,8 +105,8 @@ pub fn parse_call_extra_positional() { let mut working_set = StateWorkingSet::new(&engine_state); let sig = Signature::build("foo").switch("--jazz", "jazz!!", Some('j')); - working_set.add_decl(sig.into()); - let (_, err) = working_set.parse_source(b"foo -j 100", true); + working_set.add_decl(sig.predeclare()); + let (_, err) = parse_source(&mut working_set, b"foo -j 100", true); assert!(matches!(err, Some(ParseError::ExtraPositional(..)))); } @@ -112,8 +116,8 @@ pub fn parse_call_missing_req_positional() { let mut working_set = StateWorkingSet::new(&engine_state); let sig = Signature::build("foo").required("jazz", SyntaxShape::Int, "jazz!!"); - working_set.add_decl(sig.into()); - let (_, err) = working_set.parse_source(b"foo", true); + working_set.add_decl(sig.predeclare()); + let (_, err) = parse_source(&mut working_set, b"foo", true); assert!(matches!(err, Some(ParseError::MissingPositional(..)))); } @@ -123,7 +127,7 @@ pub fn parse_call_missing_req_flag() { let mut working_set = StateWorkingSet::new(&engine_state); let sig = Signature::build("foo").required_named("--jazz", SyntaxShape::Int, "jazz!!", None); - working_set.add_decl(sig.into()); - let (_, err) = working_set.parse_source(b"foo", true); + working_set.add_decl(sig.predeclare()); + let (_, err) = parse_source(&mut working_set, b"foo", true); assert!(matches!(err, Some(ParseError::MissingRequiredFlag(..)))); } diff --git a/crates/nu-protocol/src/block.rs b/crates/nu-protocol/src/ast/block.rs similarity index 97% rename from crates/nu-protocol/src/block.rs rename to crates/nu-protocol/src/ast/block.rs index 3b2f66bef1..365d2e76c9 100644 --- a/crates/nu-protocol/src/block.rs +++ b/crates/nu-protocol/src/ast/block.rs @@ -1,6 +1,6 @@ use std::ops::{Index, IndexMut}; -use crate::Statement; +use super::Statement; #[derive(Debug, Clone)] pub struct Block { diff --git a/crates/nu-protocol/src/call.rs b/crates/nu-protocol/src/ast/call.rs similarity index 90% rename from crates/nu-protocol/src/call.rs rename to crates/nu-protocol/src/ast/call.rs index df90a12060..efe5d7b482 100644 --- a/crates/nu-protocol/src/call.rs +++ b/crates/nu-protocol/src/ast/call.rs @@ -1,4 +1,5 @@ -use crate::{DeclId, Expression, Span}; +use super::Expression; +use crate::{DeclId, Span}; #[derive(Debug, Clone)] pub struct Call { diff --git a/crates/nu-protocol/src/expr.rs b/crates/nu-protocol/src/ast/expr.rs similarity index 86% rename from crates/nu-protocol/src/expr.rs rename to crates/nu-protocol/src/ast/expr.rs index cfc74799c2..818db24cd5 100644 --- a/crates/nu-protocol/src/expr.rs +++ b/crates/nu-protocol/src/ast/expr.rs @@ -1,4 +1,5 @@ -use crate::{BlockId, Call, Expression, Operator, Signature, Span, VarId}; +use super::{Call, Expression, Operator}; +use crate::{BlockId, Signature, Span, VarId}; #[derive(Debug, Clone)] pub enum Expr { diff --git a/crates/nu-protocol/src/expression.rs b/crates/nu-protocol/src/ast/expression.rs similarity index 96% rename from crates/nu-protocol/src/expression.rs rename to crates/nu-protocol/src/ast/expression.rs index 176bb65ce4..d17b05428c 100644 --- a/crates/nu-protocol/src/expression.rs +++ b/crates/nu-protocol/src/ast/expression.rs @@ -1,4 +1,5 @@ -use crate::{BlockId, Expr, Operator, Signature, Span, Type, VarId}; +use super::{Expr, Operator}; +use crate::{BlockId, Signature, Span, Type, VarId}; #[derive(Debug, Clone)] pub struct Expression { diff --git a/crates/nu-protocol/src/ast/mod.rs b/crates/nu-protocol/src/ast/mod.rs new file mode 100644 index 0000000000..90c3901bde --- /dev/null +++ b/crates/nu-protocol/src/ast/mod.rs @@ -0,0 +1,15 @@ +mod block; +mod call; +mod expr; +mod expression; +mod operator; +mod pipeline; +mod statement; + +pub use block::*; +pub use call::*; +pub use expr::*; +pub use expression::*; +pub use operator::*; +pub use pipeline::*; +pub use statement::*; diff --git a/crates/nu-protocol/src/operator.rs b/crates/nu-protocol/src/ast/operator.rs similarity index 100% rename from crates/nu-protocol/src/operator.rs rename to crates/nu-protocol/src/ast/operator.rs diff --git a/crates/nu-protocol/src/pipeline.rs b/crates/nu-protocol/src/ast/pipeline.rs similarity index 90% rename from crates/nu-protocol/src/pipeline.rs rename to crates/nu-protocol/src/ast/pipeline.rs index cdeb722158..fd4425290c 100644 --- a/crates/nu-protocol/src/pipeline.rs +++ b/crates/nu-protocol/src/ast/pipeline.rs @@ -1,4 +1,4 @@ -use crate::Expression; +use crate::ast::Expression; #[derive(Debug, Clone)] pub struct Pipeline { diff --git a/crates/nu-protocol/src/statement.rs b/crates/nu-protocol/src/ast/statement.rs similarity index 69% rename from crates/nu-protocol/src/statement.rs rename to crates/nu-protocol/src/ast/statement.rs index 27b86f005e..24703a8cfb 100644 --- a/crates/nu-protocol/src/statement.rs +++ b/crates/nu-protocol/src/ast/statement.rs @@ -1,4 +1,5 @@ -use crate::{DeclId, Expression, Pipeline}; +use super::{Expression, Pipeline}; +use crate::DeclId; #[derive(Debug, Clone)] pub enum Statement { diff --git a/crates/nu-protocol/src/engine/call_info.rs b/crates/nu-protocol/src/engine/call_info.rs new file mode 100644 index 0000000000..a30dc2d848 --- /dev/null +++ b/crates/nu-protocol/src/engine/call_info.rs @@ -0,0 +1,8 @@ +use crate::ast::Call; +use crate::Span; + +#[derive(Debug, Clone)] +pub struct UnevaluatedCallInfo { + pub args: Call, + pub name_span: Span, +} diff --git a/crates/nu-protocol/src/command.rs b/crates/nu-protocol/src/engine/command.rs similarity index 79% rename from crates/nu-protocol/src/command.rs rename to crates/nu-protocol/src/engine/command.rs index 08caf637c4..8e8118fc8d 100644 --- a/crates/nu-protocol/src/command.rs +++ b/crates/nu-protocol/src/engine/command.rs @@ -1,4 +1,6 @@ -use crate::{Example, Signature}; +use crate::{BlockId, Example, ShellError, Signature, Value}; + +use super::CommandArgs; pub trait Command { fn name(&self) -> &str; @@ -13,6 +15,8 @@ pub trait Command { "" } + fn run(&self, args: CommandArgs) -> Result; + // fn run(&self, args: CommandArgs) -> Result { // let context = args.context.clone(); // let stream = self.run_with_actions(args)?; @@ -53,8 +57,8 @@ pub trait Command { false } - // Is a custom command i.e. def blah [] { } - fn is_custom(&self) -> bool { - false + // If command is a custom command i.e. def blah [] { }, get the block id + fn get_custom_command(&self) -> Option { + None } } diff --git a/crates/nu-protocol/src/engine/command_args.rs b/crates/nu-protocol/src/engine/command_args.rs new file mode 100644 index 0000000000..a9468e0118 --- /dev/null +++ b/crates/nu-protocol/src/engine/command_args.rs @@ -0,0 +1,7 @@ +use super::{EvaluationContext, UnevaluatedCallInfo}; + +pub struct CommandArgs { + pub context: EvaluationContext, + pub call_info: UnevaluatedCallInfo, + pub input: crate::Value, +} diff --git a/crates/nu-protocol/src/engine_state.rs b/crates/nu-protocol/src/engine/engine_state.rs similarity index 99% rename from crates/nu-protocol/src/engine_state.rs rename to crates/nu-protocol/src/engine/engine_state.rs index 8d96c9bdab..af1f940ac6 100644 --- a/crates/nu-protocol/src/engine_state.rs +++ b/crates/nu-protocol/src/engine/engine_state.rs @@ -1,4 +1,5 @@ -use crate::{Block, BlockId, Command, DeclId, Span, Type, VarId}; +use super::Command; +use crate::{ast::Block, BlockId, DeclId, Span, Type, VarId}; use core::panic; use std::{collections::HashMap, ops::Range, slice::Iter}; diff --git a/crates/nu-engine/src/state.rs b/crates/nu-protocol/src/engine/evaluation_context.rs similarity index 93% rename from crates/nu-engine/src/state.rs rename to crates/nu-protocol/src/engine/evaluation_context.rs index e9b5cff4ff..f1baa94949 100644 --- a/crates/nu-engine/src/state.rs +++ b/crates/nu-protocol/src/engine/evaluation_context.rs @@ -1,19 +1,19 @@ -use nu_protocol::EngineState; +use super::EngineState; use std::{cell::RefCell, collections::HashMap, rc::Rc}; -use nu_protocol::{ShellError, Value, VarId}; +use crate::{ShellError, Value, VarId}; -pub struct State { +pub struct EvaluationContext { pub engine_state: Rc>, pub stack: Stack, } -impl State { +impl EvaluationContext { pub fn get_var(&self, var_id: VarId) -> Result { self.stack.get_var(var_id) } - pub fn enter_scope(&self) -> State { + pub fn enter_scope(&self) -> EvaluationContext { Self { engine_state: self.engine_state.clone(), stack: self.stack.clone().enter_scope(), diff --git a/crates/nu-protocol/src/engine/mod.rs b/crates/nu-protocol/src/engine/mod.rs new file mode 100644 index 0000000000..68ffac9616 --- /dev/null +++ b/crates/nu-protocol/src/engine/mod.rs @@ -0,0 +1,11 @@ +mod call_info; +mod command; +mod command_args; +mod engine_state; +mod evaluation_context; + +pub use call_info::*; +pub use command::*; +pub use command_args::*; +pub use engine_state::*; +pub use evaluation_context::*; diff --git a/crates/nu-protocol/src/lib.rs b/crates/nu-protocol/src/lib.rs index d4e1166cf4..3e7f61639c 100644 --- a/crates/nu-protocol/src/lib.rs +++ b/crates/nu-protocol/src/lib.rs @@ -1,35 +1,19 @@ -mod block; -mod call; -mod command; -mod engine_state; +pub mod ast; +pub mod engine; mod example; -mod expr; -mod expression; mod id; -mod operator; -mod pipeline; mod shell_error; mod signature; mod span; -mod statement; mod syntax_shape; mod ty; mod value; -pub use block::*; -pub use call::*; -pub use command::*; -pub use engine_state::*; pub use example::*; -pub use expr::*; -pub use expression::*; pub use id::*; -pub use operator::*; -pub use pipeline::*; pub use shell_error::*; pub use signature::*; pub use span::*; -pub use statement::*; pub use syntax_shape::*; pub use ty::*; pub use value::*; diff --git a/crates/nu-protocol/src/signature.rs b/crates/nu-protocol/src/signature.rs index f5fc981223..a797ecbf4b 100644 --- a/crates/nu-protocol/src/signature.rs +++ b/crates/nu-protocol/src/signature.rs @@ -1,5 +1,5 @@ +use crate::engine::Command; use crate::BlockId; -use crate::Command; use crate::SyntaxShape; use crate::VarId; @@ -314,6 +314,10 @@ impl Command for Predeclaration { fn usage(&self) -> &str { &self.signature.usage } + + fn run(&self, _args: crate::engine::CommandArgs) -> Result { + panic!("Internal error: can't run a predeclaration without a body") + } } struct BlockCommand { @@ -333,4 +337,12 @@ impl Command for BlockCommand { fn usage(&self) -> &str { &self.signature.usage } + + fn run(&self, _args: crate::engine::CommandArgs) -> Result { + panic!("Internal error: can't run custom command with 'run', use block_id"); + } + + fn get_custom_command(&self) -> Option { + Some(self.block_id) + } } diff --git a/src/main.rs b/src/main.rs index 1b0f7f3dcc..030386a055 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use nu_cli::{create_default_context, report_parsing_error, report_shell_error, NuHighlighter}; use nu_engine::eval_block; -use nu_protocol::{EngineState, StateWorkingSet}; +use nu_parser::parse_file; +use nu_protocol::engine::{EngineState, EvaluationContext, StateWorkingSet}; #[cfg(test)] mod tests; @@ -14,7 +15,7 @@ fn main() -> std::io::Result<()> { let (block, delta) = { let engine_state = engine_state.borrow(); let mut working_set = StateWorkingSet::new(&*engine_state); - let (output, err) = working_set.parse_file(&path, &file, false); + let (output, err) = parse_file(&mut working_set, &path, &file, false); if let Some(err) = err { let _ = report_parsing_error(&working_set, &err); @@ -25,9 +26,9 @@ fn main() -> std::io::Result<()> { EngineState::merge_delta(&mut *engine_state.borrow_mut(), delta); - let state = nu_engine::State { + let state = EvaluationContext { engine_state: engine_state.clone(), - stack: nu_engine::Stack::new(), + stack: nu_protocol::engine::Stack::new(), }; match eval_block(&state, &block) { @@ -59,7 +60,7 @@ fn main() -> std::io::Result<()> { let prompt = DefaultPrompt::new(1); let mut current_line = 1; - let stack = nu_engine::Stack::new(); + let stack = nu_protocol::engine::Stack::new(); loop { let input = line_editor.read_line(&prompt); @@ -73,7 +74,8 @@ fn main() -> std::io::Result<()> { let (block, delta) = { let engine_state = engine_state.borrow(); let mut working_set = StateWorkingSet::new(&*engine_state); - let (output, err) = working_set.parse_file( + let (output, err) = parse_file( + &mut working_set, &format!("line_{}", current_line), s.as_bytes(), false, @@ -87,7 +89,7 @@ fn main() -> std::io::Result<()> { EngineState::merge_delta(&mut *engine_state.borrow_mut(), delta); - let state = nu_engine::State { + let state = nu_protocol::engine::EvaluationContext { engine_state: engine_state.clone(), stack: stack.clone(), };