Remove lifetime from eval state

This commit is contained in:
JT 2021-08-16 14:30:31 +12:00
parent 579814895d
commit ceea7e5aeb
3 changed files with 23 additions and 17 deletions

View File

@ -29,7 +29,8 @@ pub fn eval_operator(op: &Expression) -> Result<Operator, ShellError> {
}
fn eval_call(state: &State, call: &Call) -> Result<Value, ShellError> {
let decl = state.parser_state.get_decl(call.decl_id);
let parser_state = state.parser_state.borrow();
let decl = parser_state.get_decl(call.decl_id);
if let Some(block_id) = decl.body {
let state = state.enter_scope();
for (arg, param) in call
@ -44,7 +45,8 @@ fn eval_call(state: &State, call: &Call) -> Result<Value, ShellError> {
state.add_var(var_id, result);
}
let block = state.parser_state.get_block(block_id);
let parser_state = state.parser_state.borrow();
let block = parser_state.get_block(block_id);
eval_block(&state, block)
} else if decl.signature.name == "let" {
let var_id = call.positional[0]
@ -91,14 +93,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();
if val {
let block = state.parser_state.get_block(then_block);
let block = parser_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 = state.parser_state.get_block(block_id);
let block = parser_state.get_block(block_id);
let state = state.enter_scope();
eval_block(&state, block)
} else {
@ -129,7 +132,8 @@ fn eval_call(state: &State, call: &Call) -> Result<Value, ShellError> {
let block = call.positional[0]
.as_block()
.expect("internal error: expected block");
let block = state.parser_state.get_block(block);
let parser_state = state.parser_state.borrow();
let block = parser_state.get_block(block);
let state = state.enter_scope();
let start_time = Instant::now();
@ -152,7 +156,8 @@ fn eval_call(state: &State, call: &Call) -> Result<Value, ShellError> {
let block = call.positional[2]
.as_block()
.expect("internal error: expected block");
let block = state.parser_state.get_block(block);
let parser_state = state.parser_state.borrow();
let block = parser_state.get_block(block);
let state = state.enter_scope();
@ -176,15 +181,15 @@ fn eval_call(state: &State, call: &Call) -> Result<Value, ShellError> {
span: call.positional[0].span,
})
} else if decl.signature.name == "vars" {
state.parser_state.print_vars();
state.parser_state.borrow().print_vars();
Ok(Value::Nothing {
span: call.positional[0].span,
})
} else if decl.signature.name == "decls" {
state.parser_state.print_decls();
state.parser_state.borrow().print_decls();
Ok(Value::Nothing { span: call.head })
} else if decl.signature.name == "blocks" {
state.parser_state.print_blocks();
state.parser_state.borrow().print_blocks();
Ok(Value::Nothing { span: call.head })
} else if decl.signature.name == "stack" {
state.print_stack();
@ -229,7 +234,8 @@ pub fn eval_expression(state: &State, expr: &Expression) -> Result<Value, ShellE
}
Expr::Subexpression(block_id) => {
let block = state.parser_state.get_block(*block_id);
let parser_state = state.parser_state.borrow();
let block = parser_state.get_block(*block_id);
let state = state.enter_scope();
eval_block(&state, block)

View File

@ -3,19 +3,19 @@ use std::{cell::RefCell, collections::HashMap, rc::Rc};
use crate::{value::Value, ShellError};
pub struct State<'a> {
pub parser_state: &'a ParserState,
pub struct State {
pub parser_state: Rc<RefCell<ParserState>>,
pub stack: Stack,
}
impl<'a> State<'a> {
impl State {
pub fn get_var(&self, var_id: VarId) -> Result<Value, ShellError> {
self.stack.get_var(var_id)
}
pub fn enter_scope(&self) -> State<'a> {
pub fn enter_scope(&self) -> State {
Self {
parser_state: self.parser_state,
parser_state: self.parser_state.clone(),
stack: self.stack.clone().enter_scope(),
}
}

View File

@ -26,7 +26,7 @@ fn main() -> std::io::Result<()> {
ParserState::merge_delta(&mut *parser_state.borrow_mut(), delta);
let state = nu_engine::State {
parser_state: &*parser_state.borrow(),
parser_state: parser_state.clone(),
stack: nu_engine::Stack::new(),
};
@ -88,7 +88,7 @@ fn main() -> std::io::Result<()> {
ParserState::merge_delta(&mut *parser_state.borrow_mut(), delta);
let state = nu_engine::State {
parser_state: &*parser_state.borrow(),
parser_state: parser_state.clone(),
stack: stack.clone(),
};