Change pattern functions to call user function with keyword args (#6772)

* Change pattern functions to call user function with keyword args

* Fix KCL code to use unlabeled syntax

* Update generated output
This commit is contained in:
Jonathan Tran
2025-05-08 14:43:50 -04:00
committed by GitHub
parent 90cb26c6d9
commit b65ea8e0a9
8 changed files with 92 additions and 31 deletions

View File

@ -18,10 +18,10 @@ screenSketch = startSketchOn(XZ)
|> close()
// Create transform functions for the speaker grid pattern
fn transformX(i) {
fn transformX(@i) {
return { translate = [.125 * i, 0] }
}
fn transformY(i) {
fn transformY(@i) {
return { translate = [0, -.125 * i] }
}

View File

@ -46,7 +46,7 @@ shellExtrude = startSketchOn(s, "start")
|> close()
|> extrude(length = -(height - t))
fn tr(i) {
fn tr(@i) {
j = i + 1
x = (j/wbumps) * pitch
y = (j % wbumps) * pitch

View File

@ -1324,7 +1324,7 @@ impl Node<CallExpressionKw> {
},
self.into(),
ctx.clone(),
exec_state.mod_local.pipe_value.clone().map(|v| Arg::new(v, callsite)),
exec_state.pipe_value().map(|v| Arg::new(v.clone(), callsite)),
);
match ctx.stdlib.get_either(fn_name) {
FunctionKind::Core(func) => {
@ -2194,7 +2194,7 @@ impl FunctionSource {
},
callsite,
ctx.clone(),
exec_state.mod_local.pipe_value.clone().map(|v| Arg::new(v, callsite)),
exec_state.pipe_value().map(|v| Arg::new(v.clone(), callsite)),
);
self.call_kw(fn_name, exec_state, ctx, args, callsite).await
} else {

View File

@ -1741,7 +1741,7 @@ foo
#[tokio::test(flavor = "multi_thread")]
async fn test_pattern_transform_function_cannot_access_future_definitions() {
let ast = r#"
fn transform(replicaId) {
fn transform(@replicaId) {
// x shouldn't be defined yet.
scale = x
return {

View File

@ -283,6 +283,10 @@ impl ExecState {
source_ranges: vec![source_range],
})
}
pub(crate) fn pipe_value(&self) -> Option<&KclValue> {
self.mod_local.pipe_value.as_ref()
}
}
impl GlobalState {

View File

@ -16,7 +16,7 @@ use serde::Serialize;
use uuid::Uuid;
use super::{
args::Arg,
args::{Arg, KwArgs},
utils::{point_3d_to_mm, point_to_mm},
};
use crate::{
@ -427,9 +427,18 @@ async fn make_transform<T: GeometryTrait>(
ty: NumericType::count(),
meta: vec![source_range.into()],
};
let transform_fn_args = vec![Arg::synthetic(repetition_num)];
let kw_args = KwArgs {
unlabeled: Some(Arg::new(repetition_num, source_range)),
labeled: Default::default(),
};
let transform_fn_args = Args::new_kw(
kw_args,
source_range,
ctxt.clone(),
exec_state.pipe_value().map(|v| Arg::new(v.clone(), source_range)),
);
let transform_fn_return = transform
.call(None, exec_state, ctxt, transform_fn_args, source_range)
.call_kw(None, exec_state, ctxt, transform_fn_args, source_range)
.await?;
// Unpack the returned transform object.

View File

@ -47,23 +47,23 @@ flowchart LR
142[Solid2d]
end
subgraph path23 [Path]
23["Path<br>[998, 1045, 3]"]
64["Segment<br>[1051, 1092, 3]"]
65["Segment<br>[1098, 1140, 3]"]
66["Segment<br>[1146, 1188, 3]"]
67["Segment<br>[1194, 1201, 3]"]
23["Path<br>[1000, 1047, 3]"]
64["Segment<br>[1053, 1094, 3]"]
65["Segment<br>[1100, 1142, 3]"]
66["Segment<br>[1148, 1190, 3]"]
67["Segment<br>[1196, 1203, 3]"]
150[Solid2d]
end
subgraph path24 [Path]
24["Path<br>[1459, 1610, 3]"]
68["Segment<br>[1616, 1692, 3]"]
69["Segment<br>[1698, 1851, 3]"]
70["Segment<br>[1857, 1933, 3]"]
71["Segment<br>[1939, 2095, 3]"]
72["Segment<br>[2101, 2178, 3]"]
73["Segment<br>[2184, 2339, 3]"]
74["Segment<br>[2345, 2421, 3]"]
75["Segment<br>[2427, 2434, 3]"]
24["Path<br>[1461, 1612, 3]"]
68["Segment<br>[1618, 1694, 3]"]
69["Segment<br>[1700, 1853, 3]"]
70["Segment<br>[1859, 1935, 3]"]
71["Segment<br>[1941, 2097, 3]"]
72["Segment<br>[2103, 2180, 3]"]
73["Segment<br>[2186, 2341, 3]"]
74["Segment<br>[2347, 2423, 3]"]
75["Segment<br>[2429, 2436, 3]"]
139[Solid2d]
end
subgraph path25 [Path]
@ -181,13 +181,13 @@ flowchart LR
end
1["Plane<br>[386, 403, 2]"]
2["Plane<br>[473, 490, 3]"]
3["Plane<br>[975, 992, 3]"]
4["Plane<br>[1436, 1453, 3]"]
5["Plane<br>[2585, 2602, 3]"]
6["Plane<br>[2682, 2699, 3]"]
7["Plane<br>[2781, 2798, 3]"]
8["Plane<br>[2879, 2896, 3]"]
9["Plane<br>[2977, 2994, 3]"]
3["Plane<br>[977, 994, 3]"]
4["Plane<br>[1438, 1455, 3]"]
5["Plane<br>[2587, 2604, 3]"]
6["Plane<br>[2684, 2701, 3]"]
7["Plane<br>[2783, 2800, 3]"]
8["Plane<br>[2881, 2898, 3]"]
9["Plane<br>[2979, 2996, 3]"]
10["Plane<br>[325, 342, 5]"]
11["Plane<br>[553, 592, 5]"]
12["Plane<br>[256, 273, 6]"]
@ -200,7 +200,7 @@ flowchart LR
158["Sweep Extrusion<br>[1767, 1810, 2]"]
159["Sweep Extrusion<br>[2169, 2212, 2]"]
160["Sweep Extrusion<br>[2464, 2497, 2]"]
161["Sweep Extrusion<br>[3035, 3066, 3]"]
161["Sweep Extrusion<br>[3037, 3068, 3]"]
162["Sweep Loft<br>[932, 975, 5]"]
163["Sweep Extrusion<br>[609, 661, 6]"]
164["Sweep Revolve<br>[540, 557, 7]"]

View File

@ -48,6 +48,48 @@ description: Operations executed multi_transform.kcl
"sourceRange": []
}
},
{
"type": "GroupBegin",
"group": {
"type": "FunctionCall",
"name": null,
"functionSourceRange": [],
"unlabeledArg": {
"value": {
"type": "Number",
"value": 1.0,
"ty": {
"type": "Known",
"type": "Count"
}
},
"sourceRange": []
},
"labeledArgs": {}
},
"sourceRange": []
},
{
"type": "GroupBegin",
"group": {
"type": "FunctionCall",
"name": null,
"functionSourceRange": [],
"unlabeledArg": {
"value": {
"type": "Number",
"value": 2.0,
"ty": {
"type": "Known",
"type": "Count"
}
},
"sourceRange": []
},
"labeledArgs": {}
},
"sourceRange": []
},
{
"labeledArgs": {
"instances": {
@ -85,5 +127,11 @@ description: Operations executed multi_transform.kcl
},
"sourceRange": []
}
},
{
"type": "GroupEnd"
},
{
"type": "GroupEnd"
}
]