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

View File

@ -3,19 +3,19 @@ use std::{cell::RefCell, collections::HashMap, rc::Rc};
use crate::{value::Value, ShellError}; use crate::{value::Value, ShellError};
pub struct State<'a> { pub struct State {
pub parser_state: &'a ParserState, pub parser_state: Rc<RefCell<ParserState>>,
pub stack: Stack, pub stack: Stack,
} }
impl<'a> State<'a> { impl State {
pub fn get_var(&self, var_id: VarId) -> Result<Value, ShellError> { pub fn get_var(&self, var_id: VarId) -> Result<Value, ShellError> {
self.stack.get_var(var_id) self.stack.get_var(var_id)
} }
pub fn enter_scope(&self) -> State<'a> { pub fn enter_scope(&self) -> State {
Self { Self {
parser_state: self.parser_state, parser_state: self.parser_state.clone(),
stack: self.stack.clone().enter_scope(), 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); ParserState::merge_delta(&mut *parser_state.borrow_mut(), delta);
let state = nu_engine::State { let state = nu_engine::State {
parser_state: &*parser_state.borrow(), parser_state: parser_state.clone(),
stack: nu_engine::Stack::new(), stack: nu_engine::Stack::new(),
}; };
@ -88,7 +88,7 @@ fn main() -> std::io::Result<()> {
ParserState::merge_delta(&mut *parser_state.borrow_mut(), delta); ParserState::merge_delta(&mut *parser_state.borrow_mut(), delta);
let state = nu_engine::State { let state = nu_engine::State {
parser_state: &*parser_state.borrow(), parser_state: parser_state.clone(),
stack: stack.clone(), stack: stack.clone(),
}; };