This commit is contained in:
JT
2021-09-02 20:25:22 +12:00
parent 3d252a9797
commit e1be8f61fc
29 changed files with 2965 additions and 2754 deletions

View File

@ -1,7 +1,7 @@
use std::time::Instant;
use crate::state::State;
use nu_parser::{Block, Call, Expr, Expression, Operator, Statement};
use nu_protocol::{Block, Call, Expr, Expression, Operator, Statement};
use nu_protocol::{IntoRowStream, IntoValueStream, ShellError, Span, Value};
pub fn eval_operator(op: &Expression) -> Result<Operator, ShellError> {
@ -15,8 +15,8 @@ pub fn eval_operator(op: &Expression) -> Result<Operator, ShellError> {
}
fn eval_call(state: &State, call: &Call) -> Result<Value, ShellError> {
let parser_state = state.parser_state.borrow();
let decl = parser_state.get_decl(call.decl_id);
let engine_state = state.engine_state.borrow();
let decl = engine_state.get_decl(call.decl_id);
if let Some(block_id) = decl.body {
let state = state.enter_scope();
for (arg, param) in call.positional.iter().zip(
@ -32,8 +32,8 @@ fn eval_call(state: &State, call: &Call) -> Result<Value, ShellError> {
state.add_var(var_id, result);
}
let parser_state = state.parser_state.borrow();
let block = parser_state.get_block(block_id);
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" {
let var_id = call.positional[0]
@ -80,15 +80,15 @@ fn eval_call(state: &State, call: &Call) -> Result<Value, ShellError> {
let result = eval_expression(state, cond)?;
match result {
Value::Bool { val, span } => {
let parser_state = state.parser_state.borrow();
let engine_state = state.engine_state.borrow();
if val {
let block = parser_state.get_block(then_block);
let block = engine_state.get_block(then_block);
let state = state.enter_scope();
eval_block(&state, block)
} 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 = parser_state.get_block(block_id);
let block = engine_state.get_block(block_id);
let state = state.enter_scope();
eval_block(&state, block)
} else {
@ -119,8 +119,8 @@ fn eval_call(state: &State, call: &Call) -> Result<Value, ShellError> {
let block = call.positional[0]
.as_block()
.expect("internal error: expected block");
let parser_state = state.parser_state.borrow();
let block = parser_state.get_block(block);
let engine_state = state.engine_state.borrow();
let block = engine_state.get_block(block);
let state = state.enter_scope();
let start_time = Instant::now();
@ -143,8 +143,8 @@ fn eval_call(state: &State, call: &Call) -> Result<Value, ShellError> {
let block = call.positional[2]
.as_block()
.expect("internal error: expected block");
let parser_state = state.parser_state.borrow();
let block = parser_state.get_block(block);
let engine_state = state.engine_state.borrow();
let block = engine_state.get_block(block);
let state = state.enter_scope();
@ -168,13 +168,13 @@ fn eval_call(state: &State, call: &Call) -> Result<Value, ShellError> {
span: call.positional[0].span,
})
} else if decl.signature.name == "vars" {
state.parser_state.borrow().print_vars();
state.engine_state.borrow().print_vars();
Ok(Value::Nothing { span: call.head })
} else if decl.signature.name == "decls" {
state.parser_state.borrow().print_decls();
state.engine_state.borrow().print_decls();
Ok(Value::Nothing { span: call.head })
} else if decl.signature.name == "blocks" {
state.parser_state.borrow().print_blocks();
state.engine_state.borrow().print_blocks();
Ok(Value::Nothing { span: call.head })
} else if decl.signature.name == "stack" {
state.print_stack();
@ -228,8 +228,8 @@ pub fn eval_expression(state: &State, expr: &Expression) -> Result<Value, ShellE
}
Expr::Subexpression(block_id) => {
let parser_state = state.parser_state.borrow();
let block = parser_state.get_block(*block_id);
let engine_state = state.engine_state.borrow();
let block = engine_state.get_block(*block_id);
let state = state.enter_scope();
eval_block(&state, block)

View File

@ -1,5 +1,9 @@
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};

View File

@ -1,10 +1,10 @@
use nu_parser::ParserState;
use nu_protocol::EngineState;
use std::{cell::RefCell, collections::HashMap, rc::Rc};
use nu_protocol::{ShellError, Value, VarId};
pub struct State {
pub parser_state: Rc<RefCell<ParserState>>,
pub engine_state: Rc<RefCell<EngineState>>,
pub stack: Stack,
}
@ -15,7 +15,7 @@ impl State {
pub fn enter_scope(&self) -> State {
Self {
parser_state: self.parser_state.clone(),
engine_state: self.engine_state.clone(),
stack: self.stack.clone().enter_scope(),
}
}