Use named fields for EpBinding::Sequence (#1325)

This commit is contained in:
Adam Chalmers
2024-01-26 18:38:54 +11:00
committed by GitHub
parent 05f9e3c290
commit 98f7a564ea
3 changed files with 22 additions and 16 deletions

View File

@ -17,7 +17,7 @@ pub enum EpBinding {
/// A KCL value which gets stored in a particular address in KCEP memory.
Single(Address),
/// A sequence of KCL values, indexed by their position in the sequence.
Sequence(Vec<EpBinding>),
Sequence { elements: Vec<EpBinding> },
/// A sequence of KCL values, indexed by their identifier.
Map(HashMap<String, EpBinding>),
/// Not associated with a KCEP address.
@ -38,9 +38,11 @@ impl EpBinding {
LiteralIdentifier::Literal(litval) => match litval.value {
// Arrays can be indexed by integers.
LiteralValue::IInteger(i) => match self {
EpBinding::Sequence(seq) => {
EpBinding::Sequence { elements } => {
let i = usize::try_from(i).map_err(|_| CompileError::InvalidIndex(i.to_string()))?;
seq.get(i).ok_or(CompileError::IndexOutOfBounds { i, len: seq.len() })
elements
.get(i)
.ok_or(CompileError::IndexOutOfBounds { i, len: elements.len() })
}
EpBinding::Map(_) => Err(CompileError::CannotIndex),
EpBinding::Single(_) => Err(CompileError::CannotIndex),
@ -50,7 +52,7 @@ impl EpBinding {
LiteralValue::String(property) => match self {
EpBinding::Single(_) => Err(CompileError::NoProperties),
EpBinding::Function(_) => Err(CompileError::NoProperties),
EpBinding::Sequence(_) => Err(CompileError::ArrayDoesNotHaveProperties),
EpBinding::Sequence { .. } => Err(CompileError::ArrayDoesNotHaveProperties),
EpBinding::Map(map) => map.get(&property).ok_or(CompileError::UndefinedProperty { property }),
},
// It's never valid to index by a fractional number.

View File

@ -465,7 +465,7 @@ impl Planner {
acc_instrs.extend(instructions);
Ok(seq)
})
.map(EpBinding::Sequence)?;
.map(|elements| EpBinding::Sequence { elements })?;
acc_bindings.insert(key.name, binding);
}
KclValueGroup::ObjectExpression(expr) => {
@ -528,7 +528,7 @@ impl Planner {
seq.push(binding);
Ok(seq)
})
.map(EpBinding::Sequence)?;
.map(|elements| EpBinding::Sequence { elements })?;
acc_bindings.push(binding);
}
KclValueGroup::ObjectExpression(expr) => {
@ -555,7 +555,7 @@ impl Planner {
)?;
Ok(EvalPlan {
instructions,
binding: EpBinding::Sequence(bindings),
binding: EpBinding::Sequence { elements: bindings },
})
}
}

View File

@ -721,10 +721,12 @@ fn store_object_with_array_property() {
("a".to_owned(), EpBinding::Single(Address::ZERO),),
(
"b".to_owned(),
EpBinding::Sequence(vec![
EpBinding::Single(Address::ZERO.offset(1)),
EpBinding::Single(Address::ZERO.offset(2)),
])
EpBinding::Sequence {
elements: vec![
EpBinding::Single(Address::ZERO.offset(1)),
EpBinding::Single(Address::ZERO.offset(2)),
]
}
),
]))
)
@ -786,10 +788,12 @@ fn arrays_as_parameters() {
assert_eq!(plan, expected_plan);
assert_eq!(
scope.get("array").unwrap(),
&EpBinding::Sequence(vec![
EpBinding::Single(Address::ZERO),
EpBinding::Single(Address::ZERO + 1),
EpBinding::Single(Address::ZERO + 2),
])
&EpBinding::Sequence {
elements: vec![
EpBinding::Single(Address::ZERO),
EpBinding::Single(Address::ZERO + 1),
EpBinding::Single(Address::ZERO + 2),
]
}
)
}