mirror of
https://github.com/typst/typst
synced 2025-05-15 01:25:28 +08:00
Index + value iteration for arrays 🦚
This commit is contained in:
parent
e7cc35073f
commit
dae3dad540
@ -460,6 +460,9 @@ impl Eval for ExprFor {
|
|||||||
(ForPattern::Value(v), Value::Array(array)) => {
|
(ForPattern::Value(v), Value::Array(array)) => {
|
||||||
iter!(for (v => value) in array.into_iter());
|
iter!(for (v => value) in array.into_iter());
|
||||||
}
|
}
|
||||||
|
(ForPattern::KeyValue(i, v), Value::Array(array)) => {
|
||||||
|
iter!(for (i => idx, v => value) in array.into_iter().enumerate());
|
||||||
|
}
|
||||||
(ForPattern::Value(v), Value::Dict(dict)) => {
|
(ForPattern::Value(v), Value::Dict(dict)) => {
|
||||||
iter!(for (v => value) in dict.into_iter().map(|p| p.1));
|
iter!(for (v => value) in dict.into_iter().map(|p| p.1));
|
||||||
}
|
}
|
||||||
@ -467,8 +470,7 @@ impl Eval for ExprFor {
|
|||||||
iter!(for (k => key, v => value) in dict.into_iter());
|
iter!(for (k => key, v => value) in dict.into_iter());
|
||||||
}
|
}
|
||||||
|
|
||||||
(ForPattern::KeyValue(_, _), Value::Str(_))
|
(ForPattern::KeyValue(_, _), Value::Str(_)) => {
|
||||||
| (ForPattern::KeyValue(_, _), Value::Array(_)) => {
|
|
||||||
ctx.diag(error!(self.pattern.span(), "mismatched pattern"));
|
ctx.diag(error!(self.pattern.span(), "mismatched pattern"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -590,6 +590,12 @@ primitive! { ValueTemplate: "template", Value::Template }
|
|||||||
primitive! { ValueFunc: "function", Value::Func }
|
primitive! { ValueFunc: "function", Value::Func }
|
||||||
primitive! { ValueArgs: "arguments", Value::Args }
|
primitive! { ValueArgs: "arguments", Value::Args }
|
||||||
|
|
||||||
|
impl From<usize> for Value {
|
||||||
|
fn from(v: usize) -> Self {
|
||||||
|
Self::Int(v as i64)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<&str> for Value {
|
impl From<&str> for Value {
|
||||||
fn from(v: &str) -> Self {
|
fn from(v: &str) -> Self {
|
||||||
Self::Str(v.to_string())
|
Self::Str(v.to_string())
|
||||||
|
@ -735,7 +735,7 @@ mod tests {
|
|||||||
// Simple values.
|
// Simple values.
|
||||||
test_value(Value::None, "none");
|
test_value(Value::None, "none");
|
||||||
test_value(false, "false");
|
test_value(false, "false");
|
||||||
test_value(12, "12");
|
test_value(12i64, "12");
|
||||||
test_value(3.14, "3.14");
|
test_value(3.14, "3.14");
|
||||||
test_value(Length::pt(5.5), "5.5pt");
|
test_value(Length::pt(5.5), "5.5pt");
|
||||||
test_value(Angle::deg(90.0), "90.0deg");
|
test_value(Angle::deg(90.0), "90.0deg");
|
||||||
|
@ -9,6 +9,11 @@
|
|||||||
out += (v,)
|
out += (v,)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Indices and values of array.
|
||||||
|
#for i, v in ("1", "2", "3") {
|
||||||
|
test(repr(i + 1), v)
|
||||||
|
}
|
||||||
|
|
||||||
// Values of dictionary.
|
// Values of dictionary.
|
||||||
#for v in (a: 4, b: 5) {
|
#for v in (a: 4, b: 5) {
|
||||||
out += (v,)
|
out += (v,)
|
||||||
@ -23,8 +28,8 @@
|
|||||||
#test(out, (1, 2, 3, 4, 5, "a", 6, "b", 7))
|
#test(out, (1, 2, 3, 4, 5, "a", 6, "b", 7))
|
||||||
|
|
||||||
---
|
---
|
||||||
// Keys and values of array.
|
// Keys and values of strings.
|
||||||
// Error: 6-10 mismatched pattern
|
// Error: 6-10 mismatched pattern
|
||||||
#for k, v in (-1, -2, -3) {
|
#for k, v in "hi" {
|
||||||
dont-care
|
dont-care
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user