Added incomplete flag

pull/2/head
Mark 2023-08-03 13:56:50 -07:00
parent 522917da63
commit f130bed7aa
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
3 changed files with 14 additions and 5 deletions

View File

@ -15,7 +15,7 @@ use termion::{
use super::promptbuffer::PromptBuffer;
use crate::parser;
use crate::command;
use crate::evaluate::evaluate;
use crate::evaluate;
use crate::context::Context;
use crate::parser::substitute;
@ -69,7 +69,7 @@ fn do_expression(
// Evaluate expression
#[cfg(debug_assertions)]
RawTerminal::suspend_raw_mode(&stdout).unwrap();
let g_evaluated = evaluate(&g, context);
let g_evaluated = evaluate::evaluate(&g, context, false);
#[cfg(debug_assertions)]
RawTerminal::activate_raw_mode(&stdout).unwrap();

View File

@ -7,7 +7,7 @@ use super::operator::eval_operator;
use super::function::eval_function;
use super::EvalError;
pub fn evaluate(t: &Expression, context: &mut Context) -> Result<Expression, (LineLocation, EvalError)> {
pub fn evaluate(t: &Expression, context: &mut Context, allow_incomplete: bool) -> Result<Expression, (LineLocation, EvalError)> {
// Keeps track of our position in the expression tree.
// For example, the coordinates [0, 2, 1] are interpreted as follows:
@ -44,7 +44,7 @@ pub fn evaluate(t: &Expression, context: &mut Context) -> Result<Expression, (Li
let new = match g {
Expression::Quantity(_, _) => None,
Expression::Constant(_, c) => { Some(evaluate(&c.value(), context).unwrap()) },
Expression::Constant(_, c) => { Some(evaluate(&c.value(), context, false).unwrap()) },
Expression::Variable(l, s) => {
// Don't move up, re-evaluate
// This makes variables containing floating variables work properly
@ -71,6 +71,15 @@ pub fn evaluate(t: &Expression, context: &mut Context) -> Result<Expression, (Li
}
*g = new;
} else {
if !allow_incomplete {
if let Expression::Quantity(_, _) = g {}
else {
let l = g.get_linelocation();
return Err((l, EvalError::EvaluationError))
}
}
// Always move up if we couldn't evaluate this node.
move_up = true;
}

View File

@ -10,7 +10,7 @@ fn eval_to_str(s: &str) -> Result<String, ()> {
};
//let out_str = g.print();
return match evaluate(&g, &mut Context::new()) {
return match evaluate(&g, &mut Context::new(), false) {
Ok(x) => Ok(x.to_string_outer()),
Err(_) => Err(())
};