diff --git a/src/interpreter/evaluators/expression.rs b/src/interpreter/evaluators/expression.rs index 2836479..6227335 100644 --- a/src/interpreter/evaluators/expression.rs +++ b/src/interpreter/evaluators/expression.rs @@ -604,6 +604,10 @@ impl AtomValue { None => None, }; + // TODO: add unit tests + let (input, has_previous) = opt(tuple((tag("previous"), ws1)))(input)?; + let has_previous = has_previous.is_some(); + // variable? let variable_parse_result = match postfix_separator { Some(postfix_separator) => alt(( @@ -619,8 +623,13 @@ impl AtomValue { ))(input), }; if let Ok((input, var)) = variable_parse_result { - // TODO function call - return Ok((input, AtomValue::Value(var.get_value().clone()))); + // TODO: function call + let value = if has_previous { + var.get_previous_value().clone() + } else { + var.get_value().clone() + }; + return Ok((input, AtomValue::Value(value))); } // func def diff --git a/src/interpreter/runtime/state.rs b/src/interpreter/runtime/state.rs index cbbaa1d..650f97b 100644 --- a/src/interpreter/runtime/state.rs +++ b/src/interpreter/runtime/state.rs @@ -421,7 +421,9 @@ impl VariableState { self.0.insert( name.to_string(), Variable { + previous: value.clone(), value, + // TODO: whats the default? line, type_, life_time, @@ -471,6 +473,7 @@ impl VariableState { #[derive(Debug, Clone)] pub struct Variable { value: Value, + previous: Value, line: usize, type_: VarType, life_time: Option, @@ -482,6 +485,10 @@ impl Variable { &self.value } + pub fn get_previous_value(&self) -> &Value { + &self.previous + } + pub fn set_value( &mut self, args: PosWithInfo, @@ -491,6 +498,7 @@ impl Variable { if postfix.is_empty() { // TODO: concrete error? if matches!(self.type_, VarType::VarConst | VarType::VarVar) { + self.previous = self.value.clone(); self.value = value; } return Ok(()); @@ -513,14 +521,15 @@ impl Variable { return Ok(()); }; - // TODO concrete error + // TODO: concrete error let Some(var) = var else { return Err(Error::Type("Cannot read properties of null".to_string())); }; let mut var = var.lock().unwrap(); - // TODO reuse code from postfix + self.previous = self.value.clone(); + // TODO: reuse code from postfix match postfix_last { AtomPostfix::DotNotation(identifier) => var.set_property(identifier, value), AtomPostfix::BracketNotation(expr) => {