Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions ndc_lib/src/interpreter/evaluate/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//! | Backward index | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
//! +----------------+-----+----+----+----+----+----+----+----+----+----+

use super::{EvaluationError, IntoEvaluationResult, evaluate_expression};
use super::{EvaluationError, EvaluationResult, IntoEvaluationResult, evaluate_expression};
use crate::interpreter::environment::Environment;
use crate::{
ast::{Expression, ExpressionLocation},
Expand Down Expand Up @@ -174,6 +174,7 @@ pub fn get_at_index(
lhs: &Value,
index: EvaluatedIndex,
span: Span,
environment: &Rc<RefCell<Environment>>,
) -> Result<Value, FunctionCarrier> {
let Some(size) = lhs.sequence_length() else {
return Err(EvaluationError::new(
Expand Down Expand Up @@ -210,9 +211,7 @@ pub fn get_at_index(
.collect::<String>(),
}))
}
Value::Sequence(Sequence::Map(map, _)) => {
let map = map.try_borrow().into_evaluation_result(span)?;

Value::Sequence(Sequence::Map(map, default)) => {
let key = match index {
EvaluatedIndex::Index(idx) => idx,
EvaluatedIndex::Slice { .. } => {
Expand All @@ -224,10 +223,17 @@ pub fn get_at_index(
}
};

Ok(map
.get(&key)
.ok_or_else(|| EvaluationError::key_not_found(&key, span))?
.clone())
let value = map.try_borrow().into_evaluation_result(span)?.get(&key).cloned();

if let Some(value) = value {
Ok(value)
} else if let Some(default) = default {
let default_value = produce_default_value(default, environment, span)?;
map.try_borrow_mut().into_evaluation_result(span)?.insert(key, default_value.clone());
Ok(default_value)
} else {
Err(EvaluationError::key_not_found(&key, span).into())
}
}
_ => Err(EvaluationError::syntax_error(
format!("cannot insert into {} at index", lhs.static_type()),
Expand All @@ -236,6 +242,27 @@ pub fn get_at_index(
.into()),
}
}

pub(super) fn produce_default_value(
default: &Value,
environment: &Rc<RefCell<Environment>>,
span: Span,
) -> EvaluationResult {
match default {
Value::Function(function) => {
match function.call_checked(&mut [], environment) {
Err(FunctionCarrier::FunctionTypeMismatch) => {
Err(FunctionCarrier::EvaluationError(EvaluationError::new(
"default function is not callable without arguments".to_string(),
span,
)))
}
a => a,
}
}
value => Ok(value.clone()),
}
}
pub fn set_at_index(
lhs: &mut Value,
rhs: Value,
Expand Down
25 changes: 2 additions & 23 deletions ndc_lib/src/interpreter/evaluate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ pub(crate) fn evaluate_expression(
} => {
let mut lhs_value = evaluate_expression(lhs_expression, environment)?;
let index = evaluate_as_index(index_expression, environment)?;
let value_at_index = get_at_index(&lhs_value, index.clone(), span)?;
let value_at_index = get_at_index(&lhs_value, index.clone(), span, environment)?;

let right_value = evaluate_expression(r_value, environment)?;

Expand Down Expand Up @@ -565,7 +565,7 @@ pub(crate) fn evaluate_expression(
return if let Some(value) = value {
Ok(value)
} else if let Some(default) = default {
let default_value = produce_default_value(
let default_value = index::produce_default_value(
&default,
environment,
// NOTE: this span points at the entire expression instead of the
Expand Down Expand Up @@ -647,27 +647,6 @@ pub(crate) fn evaluate_expression(
Ok(literal)
}

fn produce_default_value(
default: &Value,
environment: &Rc<RefCell<Environment>>,
span: Span,
) -> EvaluationResult {
match default {
Value::Function(function) => {
match function.call_checked(&mut [], environment) {
Err(FunctionCarrier::FunctionTypeMismatch) => {
Err(FunctionCarrier::EvaluationError(EvaluationError::new(
"default function is not callable without arguments".to_string(),
span,
)))
}
a => a,
}
// test
}
value => Ok(value.clone()),
}
}

fn declare_or_assign_variable(
l_value: &Lvalue,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let map = %{:0}; // map with default 0
map["foo"] += 3;
map[0] += 10;
map[0] += 10;

assert_eq(map["foo"], 3);
assert_eq(map[0], 20);