feat: interval iterator

This commit is contained in:
Stefan
2025-04-22 22:23:44 +02:00
parent 14b2a56ba4
commit f3c3d8176f
4 changed files with 56 additions and 20 deletions
+1 -1
View File
@@ -2,7 +2,7 @@ use thiserror::Error;
#[derive(Debug, PartialEq, Eq, Clone, Error)]
pub enum VMError {
#[error("Unsupported opcode type")]
#[error("Unsupported opcode type in '{opcode}'")]
OpcodeErr { opcode: String, message: String },
#[error("Opcode out of bounds")]
+20
View File
@@ -55,4 +55,24 @@ impl IntervalObject {
left,
})
}
pub(crate) fn to_array(&self) -> Option<Vec<Variable>> {
let start = match &self.left_bracket {
Bracket::LeftParenthesis => self.left.as_number()?.to_usize()? + 1,
Bracket::LeftSquareBracket => self.left.as_number()?.to_usize()?,
_ => return None,
};
let end = match &self.right_bracket {
Bracket::RightParenthesis => self.right.as_number()?.to_usize()? - 1,
Bracket::RightSquareBracket => self.right.as_number()?.to_usize()?,
_ => return None,
};
let list = (start..=end)
.map(|n| Variable::Number(Decimal::from(n)))
.collect::<Vec<_>>();
Some(list)
}
}
+29 -19
View File
@@ -1601,29 +1601,39 @@ impl<'arena, 'parent_ref, 'bytecode_ref> VMInner<'parent_ref, 'bytecode_ref> {
}
}
Opcode::Begin => {
let a = self.pop()?;
let arr_len = match &a {
let var = self.pop()?;
let maybe_scope = match &var {
Array(a) => {
let arr = a.borrow();
Some(arr.len())
}
_ => None,
};
match arr_len {
Some(len) => self.scopes.push(Scope {
array: a,
count: 0,
len,
iter: 0,
}),
None => {
return Err(OpcodeErr {
opcode: "Begin".into(),
message: "Unsupported type".into(),
Some(Scope {
len: arr.len(),
array: var.clone(),
count: 0,
iter: 0,
})
}
}
_ => match IntervalObject::try_from_object(var)
.map(|s| s.to_array())
.flatten()
{
None => None,
Some(arr) => Some(Scope {
len: arr.len(),
array: Variable::from_array(arr),
count: 0,
iter: 0,
}),
},
};
let Some(scope) = maybe_scope else {
return Err(OpcodeErr {
opcode: "Begin".into(),
message: "Unsupported type".into(),
});
};
self.scopes.push(scope);
}
Opcode::End => {
self.scopes.pop();
+6
View File
@@ -276,6 +276,12 @@ values(customer);{"customer": {"firstName": "John"}};["John"]
values(customer);{"customer": {"lastName": "Doe"}};["Doe"]
{};;{}
# Interval iterator
map([0..3], #);[0, 1, 2, 3]
map((0..3], #);[1, 2, 3]
map([0..3), #);[0, 1, 2]
map((0..3), #);[1, 2]
# Nullish coalescing
null ?? 'hello';;'hello'
null ?? 123 ?? 321;;123
Can't render this file because it contains an unexpected character in line 14 and column 13.