Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update KCL samples for kwargs work #139

Merged
merged 15 commits into from
Feb 5, 2025
Merged

Update KCL samples for kwargs work #139

merged 15 commits into from
Feb 5, 2025

Conversation

adamchalmers
Copy link
Contributor

@adamchalmers adamchalmers commented Jan 16, 2025

Copy link
Contributor

The following files failed to export to STEP format:
80-20-rail/main.kcl
a-parametric-bearing-pillow-block/main.kcl
ball-bearing/main.kcl
bracket/main.kcl
car-wheel-assembly/main.kcl
dodecahedron/main.kcl
enclosure/main.kcl
flange-with-patterns/main.kcl
flange-xy/main.kcl
focusrite-scarlett-mounting-bracket/main.kcl
food-service-spatula/main.kcl
french-press/main.kcl
gear-rack/main.kcl
gear/main.kcl
hex-nut/main.kcl
i-beam/main.kcl
kitt/main.kcl
lego/main.kcl
mounting-plate/main.kcl
multi-axis-robot/main.kcl
pipe-flange-assembly/main.kcl
pipe/main.kcl
poopy-shoe/main.kcl
router-template-cross-bar/main.kcl
router-template-slate/main.kcl
sheet-metal-bracket/main.kcl
socket-head-cap-screw/main.kcl
walkie-talkie/main.kcl
washer/main.kcl

@adamchalmers adamchalmers force-pushed the achalmers/kwargs-line branch from 3af5919 to 3c0e2a7 Compare January 16, 2025 17:42
Copy link
Contributor

The following files failed to export to STEP format:
80-20-rail/main.kcl
a-parametric-bearing-pillow-block/main.kcl
ball-bearing/main.kcl
bracket/main.kcl
car-wheel-assembly/main.kcl
dodecahedron/main.kcl
enclosure/main.kcl
flange-with-patterns/main.kcl
flange-xy/main.kcl
focusrite-scarlett-mounting-bracket/main.kcl
food-service-spatula/main.kcl
french-press/main.kcl
gear-rack/main.kcl
gear/main.kcl
hex-nut/main.kcl
i-beam/main.kcl
kitt/main.kcl
lego/main.kcl
mounting-plate/main.kcl
multi-axis-robot/main.kcl
pipe-flange-assembly/main.kcl
pipe/main.kcl
poopy-shoe/main.kcl
router-template-cross-bar/main.kcl
router-template-slate/main.kcl
sheet-metal-bracket/main.kcl
socket-head-cap-screw/main.kcl
walkie-talkie/main.kcl
washer/main.kcl

Copy link
Contributor

The following files failed to export to STEP format:
80-20-rail/main.kcl
a-parametric-bearing-pillow-block/main.kcl
ball-bearing/main.kcl
bracket/main.kcl
car-wheel-assembly/main.kcl
dodecahedron/main.kcl
enclosure/main.kcl
flange-with-patterns/main.kcl
flange-xy/main.kcl
focusrite-scarlett-mounting-bracket/main.kcl
food-service-spatula/main.kcl
french-press/main.kcl
gear-rack/main.kcl
gear/main.kcl
hex-nut/main.kcl
i-beam/main.kcl
kitt/main.kcl
lego/main.kcl
mounting-plate/main.kcl
multi-axis-robot/main.kcl
pipe-flange-assembly/main.kcl
pipe/main.kcl
poopy-shoe/main.kcl
router-template-cross-bar/main.kcl
router-template-slate/main.kcl
sheet-metal-bracket/main.kcl
socket-head-cap-screw/main.kcl
walkie-talkie/main.kcl
washer/main.kcl

adamchalmers added a commit to KittyCAD/modeling-app that referenced this pull request Feb 4, 2025
Part of #4600.

PR: #4826

# Changes to KCL stdlib

- `line(point, sketch, tag)` and `lineTo(point, sketch, tag)` are combined into `line(@Sketch, end?, endAbsolute?, tag?)`
- `close(sketch, tag?)` is now `close(@Sketch, tag?)`
- `extrude(length, sketch)` is now `extrude(@Sketch, length)`

Note that if a parameter starts with `@` like `@sketch`, it doesn't have any label when called, so you call it like this:

```
sketch = startSketchAt([0, 0])
line(sketch, end = [3, 3], tag = $hi)
```

Note also that if you're using a `|>` pipeline, you can omit the `@` argument and it will be assumed to be the LHS of the `|>`. So the above could be written as

```
sketch = startSketchAt([0, 0])
|> line(end = [3, 3], tag = $hi)
```

Also changes frontend tests to use KittyCAD/kcl-samples#139 instead of its main

The regex find-and-replace I use for migrating code (note these don't work with multi-line expressions) are:

```
 line\(([^=]*), %\)
 line(end = $1)

 line\((.*), %, (.*)\)
 line(end = $1, tag = $2)

 lineTo\((.*), %\)
 line(endAbsolute = $1)

 lineTo\((.*), %, (.*)\)
 line(endAbsolute = $1, tag = $2)

 extrude\((.*), %\)
 extrude(length = $1)

extrude\(([^=]*), ([a-zA-Z0-9]+)\)
extrude($2, length = $1)

 close\(%, (.*)\)
 close(tag = $1)
```

# Selected notes from commits before I squash them all

* Fix test 'yRelative to horizontal distance'

Fixes:
 - Make a lineTo helper
 - Fix pathToNode to go through the labeled arg .arg property

* Fix test by changing lookups into transformMap

Parts of the code assumed that `line` is always a relative call. But
actually now it might be absolute, if it's got an `endAbsolute` parameter.

So, change whether to look up `line` or `lineTo` and the relevant absolute
or relative line types based on that parameter.

* Stop asserting on exact source ranges

When I changed line to kwargs, all the source ranges we assert on became
slightly different. I find these assertions to be very very low value.
So I'm removing them.

* Fix more tests: getConstraintType calls weren't checking if the
'line' fn was absolute or relative.

* Fixed another queryAst test

There were 2 problems:
 - Test was looking for the old style of `line` call to choose an offset
   for pathToNode
 - Test assumed that the `tag` param was always the third one, but in
   a kwarg call, you have to look it up by label

* Fix test: traverse was not handling CallExpressionKw

* Fix another test, addTagKw

addTag helper was not aware of kw args.

* Convert close from positional to kwargs

If the close() call has 0 args, or a single unlabeled arg, the parser
interprets it as a CallExpression (positional) not a CallExpressionKw.

But then if a codemod wants to add a tag to it, it tries adding a kwarg
called 'tag', which fails because the CallExpression doesn't need
kwargs inserted into it.

The fix is: change the node from CallExpression to CallExpressionKw, and
update getNodeFromPath to take a 'replacement' arg, so we can replace
the old node with the new node in the AST.

* Fix the last test

Test was looking for `lineTo` as a substring of the input KCL program.
But there's no more lineTo function, so I changed it to look for
line() with an endAbsolute arg, which is the new equivalent.

Also changed the getConstraintInfo code to look up the lineTo if using
line with endAbsolute.

* Fix many bad regex find-replaces

I wrote a regex find-and-replace which converted `line` calls from
positional to keyword calls. But it was accidentally applied to more
places than it should be, for example, angledLine, xLine and yLine calls.

Fixes this.

* Fixes test 'Basic sketch › code pane closed at start'

Problem was, the getNodeFromPath call might not actually find a callExpressionKw,
it might find a callExpression. So the `giveSketchFnCallTag` thought
it was modifying a kwargs call, but it was actually modifying a positional
call.

This meant it tried to push a labeled argument in, rather than a normal
arg, and a lot of other problems. Fixed by doing runtime typechecking.

* Fix: Optional args given with wrong type were silently ignored

Optional args don't have to be given. But if the user gives them, they
should be the right type.

Bug: if the KCL interpreter found an optional arg, which was given, but
was the wrong type, it would ignore it and pretend the arg was never
given at all. This was confusing for users.

Fix: Now if you give an optional arg, but it's the wrong type, KCL will
emit a type error just like it would for a mandatory argument.

---------

Signed-off-by: Nick Cameron <[email protected]>
Co-authored-by: Nick Cameron <[email protected]>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Frank Noirot <[email protected]>
Co-authored-by: Kevin Nadro <[email protected]>
Co-authored-by: Jonathan Tran <[email protected]>
Copy link
Contributor

github-actions bot commented Feb 4, 2025

The following files failed to export to STEP format:
80-20-rail/main.kcl
a-parametric-bearing-pillow-block/main.kcl
ball-bearing/main.kcl
bracket/main.kcl
car-wheel-assembly/main.kcl
dodecahedron/main.kcl
enclosure/main.kcl
flange-with-patterns/main.kcl
flange-xy/main.kcl
focusrite-scarlett-mounting-bracket/main.kcl
food-service-spatula/main.kcl
french-press/main.kcl
gear-rack/main.kcl
gear/main.kcl
hex-nut/main.kcl
i-beam/main.kcl
kitt/main.kcl
lego/main.kcl
mounting-plate/main.kcl
multi-axis-robot/main.kcl
pipe-flange-assembly/main.kcl
pipe/main.kcl
poopy-shoe/main.kcl
router-template-cross-bar/main.kcl
router-template-slate/main.kcl
sheet-metal-bracket/main.kcl
socket-head-cap-screw/main.kcl
walkie-talkie/main.kcl
washer/main.kcl

Copy link
Contributor

github-actions bot commented Feb 4, 2025

The following files failed to export to STEP format:
80-20-rail/main.kcl
a-parametric-bearing-pillow-block/main.kcl
ball-bearing/main.kcl
bracket/main.kcl
car-wheel-assembly/main.kcl
dodecahedron/main.kcl
enclosure/main.kcl
flange-with-patterns/main.kcl
flange-xy/main.kcl
focusrite-scarlett-mounting-bracket/main.kcl
food-service-spatula/main.kcl
french-press/main.kcl
gear-rack/main.kcl
gear/main.kcl
hex-nut/main.kcl
i-beam/main.kcl
kitt/main.kcl
lego/main.kcl
mounting-plate/main.kcl
multi-axis-robot/main.kcl
pipe-flange-assembly/main.kcl
pipe/main.kcl
poopy-shoe/main.kcl
router-template-cross-bar/main.kcl
router-template-slate/main.kcl
sheet-metal-bracket/main.kcl
socket-head-cap-screw/main.kcl
walkie-talkie/main.kcl
washer/main.kcl

lf94 pushed a commit to KittyCAD/modeling-app that referenced this pull request Feb 4, 2025
Part of #4600.

PR: #4826

# Changes to KCL stdlib

- `line(point, sketch, tag)` and `lineTo(point, sketch, tag)` are combined into `line(@Sketch, end?, endAbsolute?, tag?)`
- `close(sketch, tag?)` is now `close(@Sketch, tag?)`
- `extrude(length, sketch)` is now `extrude(@Sketch, length)`

Note that if a parameter starts with `@` like `@sketch`, it doesn't have any label when called, so you call it like this:

```
sketch = startSketchAt([0, 0])
line(sketch, end = [3, 3], tag = $hi)
```

Note also that if you're using a `|>` pipeline, you can omit the `@` argument and it will be assumed to be the LHS of the `|>`. So the above could be written as

```
sketch = startSketchAt([0, 0])
|> line(end = [3, 3], tag = $hi)
```

Also changes frontend tests to use KittyCAD/kcl-samples#139 instead of its main

The regex find-and-replace I use for migrating code (note these don't work with multi-line expressions) are:

```
 line\(([^=]*), %\)
 line(end = $1)

 line\((.*), %, (.*)\)
 line(end = $1, tag = $2)

 lineTo\((.*), %\)
 line(endAbsolute = $1)

 lineTo\((.*), %, (.*)\)
 line(endAbsolute = $1, tag = $2)

 extrude\((.*), %\)
 extrude(length = $1)

extrude\(([^=]*), ([a-zA-Z0-9]+)\)
extrude($2, length = $1)

 close\(%, (.*)\)
 close(tag = $1)
```

# Selected notes from commits before I squash them all

* Fix test 'yRelative to horizontal distance'

Fixes:
 - Make a lineTo helper
 - Fix pathToNode to go through the labeled arg .arg property

* Fix test by changing lookups into transformMap

Parts of the code assumed that `line` is always a relative call. But
actually now it might be absolute, if it's got an `endAbsolute` parameter.

So, change whether to look up `line` or `lineTo` and the relevant absolute
or relative line types based on that parameter.

* Stop asserting on exact source ranges

When I changed line to kwargs, all the source ranges we assert on became
slightly different. I find these assertions to be very very low value.
So I'm removing them.

* Fix more tests: getConstraintType calls weren't checking if the
'line' fn was absolute or relative.

* Fixed another queryAst test

There were 2 problems:
 - Test was looking for the old style of `line` call to choose an offset
   for pathToNode
 - Test assumed that the `tag` param was always the third one, but in
   a kwarg call, you have to look it up by label

* Fix test: traverse was not handling CallExpressionKw

* Fix another test, addTagKw

addTag helper was not aware of kw args.

* Convert close from positional to kwargs

If the close() call has 0 args, or a single unlabeled arg, the parser
interprets it as a CallExpression (positional) not a CallExpressionKw.

But then if a codemod wants to add a tag to it, it tries adding a kwarg
called 'tag', which fails because the CallExpression doesn't need
kwargs inserted into it.

The fix is: change the node from CallExpression to CallExpressionKw, and
update getNodeFromPath to take a 'replacement' arg, so we can replace
the old node with the new node in the AST.

* Fix the last test

Test was looking for `lineTo` as a substring of the input KCL program.
But there's no more lineTo function, so I changed it to look for
line() with an endAbsolute arg, which is the new equivalent.

Also changed the getConstraintInfo code to look up the lineTo if using
line with endAbsolute.

* Fix many bad regex find-replaces

I wrote a regex find-and-replace which converted `line` calls from
positional to keyword calls. But it was accidentally applied to more
places than it should be, for example, angledLine, xLine and yLine calls.

Fixes this.

* Fixes test 'Basic sketch › code pane closed at start'

Problem was, the getNodeFromPath call might not actually find a callExpressionKw,
it might find a callExpression. So the `giveSketchFnCallTag` thought
it was modifying a kwargs call, but it was actually modifying a positional
call.

This meant it tried to push a labeled argument in, rather than a normal
arg, and a lot of other problems. Fixed by doing runtime typechecking.

* Fix: Optional args given with wrong type were silently ignored

Optional args don't have to be given. But if the user gives them, they
should be the right type.

Bug: if the KCL interpreter found an optional arg, which was given, but
was the wrong type, it would ignore it and pretend the arg was never
given at all. This was confusing for users.

Fix: Now if you give an optional arg, but it's the wrong type, KCL will
emit a type error just like it would for a mandatory argument.

---------

Signed-off-by: Nick Cameron <[email protected]>
Co-authored-by: Nick Cameron <[email protected]>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Frank Noirot <[email protected]>
Co-authored-by: Kevin Nadro <[email protected]>
Co-authored-by: Jonathan Tran <[email protected]>
@r-barton
Copy link
Collaborator

r-barton commented Feb 5, 2025

closes #155

Copy link

@pierremtb pierremtb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pierremtb pierremtb merged commit 13543b7 into main Feb 5, 2025
@pierremtb pierremtb deleted the achalmers/kwargs-line branch February 5, 2025 18:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants