Skip to content

Commit

Permalink
added previous keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
Eddio0141 committed May 21, 2024
1 parent f438bc3 commit d90c01e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/interpreter/evaluators/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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((
Expand All @@ -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
Expand Down
13 changes: 11 additions & 2 deletions src/interpreter/runtime/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -471,6 +473,7 @@ impl VariableState {
#[derive(Debug, Clone)]
pub struct Variable {
value: Value,
previous: Value,
line: usize,
type_: VarType,
life_time: Option<LifeTime>,
Expand All @@ -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,
Expand All @@ -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(());
Expand All @@ -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) => {
Expand Down

0 comments on commit d90c01e

Please sign in to comment.