Kwargs: leg helpers (#6459)

legLen, legAngX, legAngY moved to keyword arguments
This commit is contained in:
Adam Chalmers
2025-04-24 09:53:19 -05:00
committed by GitHub
parent 668f6671a9
commit b02dbd4fe6
11 changed files with 95 additions and 65 deletions

View File

@ -362,7 +362,7 @@ describe('testing math operators', () => {
expect(mem['myVar']?.value).toBe(6)
})
it('with nested callExpression', async () => {
const code = 'const myVar = 2 + min(100, legLen(5, 3))'
const code = 'myVar = 2 + min(100, legLen(hypotenuse = 5, leg = 3))'
const mem = await exe(code)
expect(mem['myVar']?.value).toBe(6)
})
@ -372,15 +372,15 @@ describe('testing math operators', () => {
expect(mem['myVar']?.value).toBe(-3)
})
it('with unaryExpression in callExpression', async () => {
const code = 'const myVar = min(-legLen(5, 4), 5)'
const code2 = 'const myVar = min(5 , -legLen(5, 4))'
const code = 'const myVar = min(-legLen(hypotenuse = 5, leg = 4), 5)'
const code2 = 'const myVar = min(5 , -legLen(hypotenuse = 5, leg = 4))'
const mem = await exe(code)
const mem2 = await exe(code2)
expect(mem['myVar']?.value).toBe(-3)
expect(mem['myVar']?.value).toBe(mem2['myVar']?.value)
})
it('with unaryExpression in ArrayExpression', async () => {
const code = 'const myVar = [1,-legLen(5, 4)]'
const code = 'const myVar = [1,-legLen(hypotenuse = 5, leg = 4)]'
const mem = await exe(code)
expect(mem['myVar']?.value).toEqual([
{
@ -397,9 +397,9 @@ describe('testing math operators', () => {
})
it('with unaryExpression in ArrayExpression in CallExpression, checking nothing funny happens when used in a sketch', async () => {
const code = [
'const part001 = startSketchOn(XY)',
'part001 = startSketchOn(XY)',
' |> startProfileAt([0, 0], %)',
'|> line(end = [-2.21, -legLen(5, min(3, 999))])',
'|> line(end = [-2.21, -legLen(hypotenuse = 5, leg = min(3, 999))])',
].join('\n')
const mem = await exe(code)
const sketch = sketchFromKclValue(mem['part001'], 'part001')
@ -415,7 +415,7 @@ describe('testing math operators', () => {
` |> line(end = [3, 4], tag = $seg01)`,
` |> line(end = [`,
` min(segLen(seg01), myVar),`,
` -legLen(segLen(seg01), myVar)`,
` -legLen(hypotenuse = segLen(seg01), leg = myVar)`,
`])`,
``,
].join('\n')
@ -425,8 +425,8 @@ describe('testing math operators', () => {
expect((sketch as Sketch).paths?.[1]?.from).toEqual([3, 4])
expect((sketch as Sketch).paths?.[1]?.to).toEqual([6, 0])
const removedUnaryExp = code.replace(
`-legLen(segLen(seg01), myVar)`,
`legLen(segLen(seg01), myVar)`
`-legLen(hypotenuse = segLen(seg01), leg = myVar)`,
`legLen(hypotenuse = segLen(seg01), leg = myVar)`
)
const removedUnaryExpMem = await exe(removedUnaryExp)
const removedUnaryExpMemSketch = sketchFromKclValue(
@ -438,7 +438,8 @@ describe('testing math operators', () => {
expect((removedUnaryExpMemSketch as Sketch).paths?.[1]?.to).toEqual([6, 8])
})
it('with nested callExpression and binaryExpression', async () => {
const code = 'const myVar = 2 + min(100, -1 + legLen(5, 3))'
const code =
'const myVar = 2 + min(100, -1 + legLen(hypotenuse = 5, leg = 3))'
const mem = await exe(code)
expect(mem['myVar']?.value).toBe(5)
})

View File

@ -298,21 +298,21 @@ mySk1 = startSketchOn(XY)
describe('testing call Expressions in BinaryExpressions and UnaryExpressions', () => {
it('nested callExpression in binaryExpression', () => {
const code = 'myVar = 2 + min(100, legLen(5, 3))'
const code = 'myVar = 2 + min(100, legLen(hypotenuse = 5, leg = 3))'
const { ast } = code2ast(code)
const recasted = recast(ast)
if (err(recasted)) throw recasted
expect(recasted.trim()).toBe(code)
})
it('nested callExpression in unaryExpression', () => {
const code = 'myVar = -min(100, legLen(5, 3))'
const code = 'myVar = -min(100, legLen(hypotenuse = 5, leg = 3))'
const { ast } = code2ast(code)
const recasted = recast(ast)
if (err(recasted)) throw recasted
expect(recasted.trim()).toBe(code)
})
it('with unaryExpression in callExpression', () => {
const code = 'myVar = min(5, -legLen(5, 4))'
const code = 'myVar = min(5, -legLen(hypotenuse = 5, leg = 4))'
const { ast } = code2ast(code)
const recasted = recast(ast)
if (err(recasted)) throw recasted
@ -322,7 +322,7 @@ describe('testing call Expressions in BinaryExpressions and UnaryExpressions', (
const code = [
'part001 = startSketchOn(XY)',
' |> startProfileAt([0, 0])',
' |> line(end = [-2.21, -legLen(5, min(3, 999))])',
' |> line(end = [\n -2.21,\n -legLen(hypotenuse = 5, leg = min(3, 999))\n ])',
].join('\n')
const { ast } = code2ast(code)
const recasted = recast(ast)

View File

@ -308,11 +308,11 @@ part001 = startSketchOn(XY)
|> angledLine(angle = myAng2, length = segLen(seg01)) // ln-angledLineToY-angle should become angledLine
|> line(end = [
min(segLen(seg01), myVar),
legLen(segLen(seg01), myVar)
legLen(hypotenuse = segLen(seg01), leg = myVar)
]) // ln-should use legLen for y
|> line(end = [
min(segLen(seg01), myVar),
-legLen(segLen(seg01), myVar)
-legLen(hypotenuse = segLen(seg01), leg = myVar)
]) // ln-legLen but negative
|> angledLine(angle = -112, length = segLen(seg01)) // ln-should become angledLine
|> angledLine(angle = myVar, length = segLen(seg01)) // ln-use segLen for second arg
@ -321,11 +321,11 @@ part001 = startSketchOn(XY)
|> angledLine(angle = legAngX(segLen(seg01), myVar), lengthX = min(segLen(seg01), myVar)) // ln-should use legAngX to calculate angle
|> angledLine(angle = 180 + legAngX(segLen(seg01), myVar), lengthX = min(segLen(seg01), myVar)) // ln-same as above but should have + 180 to match original quadrant
|> line(end = [
legLen(segLen(seg01), myVar),
legLen(hypotenuse = segLen(seg01), leg = myVar),
min(segLen(seg01), myVar)
]) // ln-legLen again but yRelative
|> line(end = [
-legLen(segLen(seg01), myVar),
-legLen(hypotenuse = segLen(seg01), leg = myVar),
min(segLen(seg01), myVar)
]) // ln-negative legLen yRelative
|> angledLine(angle = 58, length = segLen(seg01)) // ln-angledLineOfYLength-free should become angledLine

View File

@ -453,7 +453,10 @@ const getMinAndSegLenVals = (
const segLenVal = createSegLen(referenceSegName)
return [
createCallExpression('min', [segLenVal, varVal]),
createCallExpression('legLen', [segLenVal, varVal]),
createCallExpressionStdLibKw('legLen', null, [
createLabeledArg('hypotenuse', segLenVal),
createLabeledArg('leg', varVal),
]),
]
}