WIP: Start fixing frontend

This commit is contained in:
Adam Chalmers
2025-01-07 15:08:53 -06:00
committed by Nick Cameron
parent 725ffd952c
commit 235e526e2d
5 changed files with 129 additions and 39 deletions

View File

@ -7,6 +7,7 @@ import {
Program,
PipeExpression,
CallExpression,
CallExpressionKw,
VariableDeclarator,
Expr,
VariableDeclaration,
@ -44,7 +45,9 @@ import {
createLiteral,
createTagDeclarator,
createCallExpression,
createCallExpressionStdLibKw,
createArrayExpression,
createLabeledArg,
createPipeSubstitution,
createObjectExpression,
mutateArrExp,
@ -58,6 +61,9 @@ import { TagDeclarator } from 'wasm-lib/kcl/bindings/TagDeclarator'
import { EdgeCutInfo } from 'machines/modelingMachine'
import { Node } from 'wasm-lib/kcl/bindings/Node'
const ARG_END = 'end'
const ARG_END_ABSOLUTE = 'endAbsolute'
const STRAIGHT_SEGMENT_ERR = new Error(
'Invalid input, expected "straight-segment"'
)
@ -90,8 +96,6 @@ export function createFirstArg(
'angledLineOfYLength',
'angledLineToX',
'angledLineToY',
'line',
'lineTo',
].includes(sketchFn)
)
return createArrayExpression(val)
@ -415,10 +419,11 @@ export const line: SketchLineHelper = {
!replaceExistingCallback &&
pipe.type === 'PipeExpression'
) {
const callExp = createCallExpression('line', [
createArrayExpression([newXVal, newYVal]),
const callExp = createCallExpressionStdLibKw(
'line',
createPipeSubstitution(),
])
[createLabeledArg(ARG_END, createArrayExpression([newXVal, newYVal]))]
)
const pathToNodeIndex = pathToNode.findIndex(
(x) => x[1] === 'PipeExpression'
)
@ -463,10 +468,11 @@ export const line: SketchLineHelper = {
}
}
const callExp = createCallExpression('line', [
createArrayExpression([newXVal, newYVal]),
const callExp = createCallExpressionStdLibKw(
'line',
createPipeSubstitution(),
])
[createLabeledArg(ARG_END, createArrayExpression([newXVal, newYVal]))]
)
if (pipe.type === 'PipeExpression') {
pipe.body = [...pipe.body, callExp]
return {
@ -2349,12 +2355,21 @@ function getFirstArgValuesForXYFns(callExpression: CallExpression):
| Error {
// used for lineTo, line
const firstArg = callExpression.arguments[0]
if (firstArg.type === 'ArrayExpression') {
return { val: [firstArg.elements[0], firstArg.elements[1]] }
return getValuesForXYFns(firstArg)
}
function getValuesForXYFns(arg: Expr):
| {
val: [Expr, Expr]
tag?: Expr
}
| Error {
if (arg.type === 'ArrayExpression') {
return { val: [arg.elements[0], arg.elements[1]] }
}
if (firstArg.type === 'ObjectExpression') {
const to = firstArg.properties.find((p) => p.key.name === 'to')?.value
const tag = firstArg.properties.find((p) => p.key.name === 'tag')?.value
if (arg.type === 'ObjectExpression') {
const to = arg.properties.find((p) => p.key.name === 'to')?.value
const tag = arg.properties.find((p) => p.key.name === 'tag')?.value
if (to?.type === 'ArrayExpression') {
const [x, y] = to.elements
return { val: [x, y], tag }
@ -2471,6 +2486,33 @@ const getAngledLineThatIntersects = (
return new Error('expected ArrayExpression or ObjectExpression')
}
/**
Get the argument corresponding to 'end' or 'endAbsolute' or wherever the line actually ends.
*/
export function getArgForEnd(lineCall: CallExpressionKw):
| {
val: Expr | [Expr, Expr] | [Expr, Expr, Expr]
tag?: Expr
}
| Error {
const name = lineCall?.callee?.name
let arg
if (name == 'line') {
arg = lineCall.arguments.find((labeledArg) => {
return (
labeledArg.label.name === ARG_END ||
labeledArg.label.name === ARG_END_ABSOLUTE
)
})
} else {
return new Error('cannot find end of line function: ' + name)
}
if (arg == undefined) {
return new Error('no end of the line was found')
}
return getValuesForXYFns(arg.arg)
}
export function getFirstArg(callExp: CallExpression):
| {
val: Expr | [Expr, Expr] | [Expr, Expr, Expr]
@ -2478,9 +2520,6 @@ export function getFirstArg(callExp: CallExpression):
}
| Error {
const name = callExp?.callee?.name
if (['lineTo', 'line'].includes(name)) {
return getFirstArgValuesForXYFns(callExp)
}
if (
[
'angledLine',

View File

@ -575,21 +575,21 @@ async function helperThing(
describe('testing getConstraintLevelFromSourceRange', () => {
it('should divide up lines into free, partial and fully contrained', () => {
const code = `const baseLength = 3
const baseThick = 1
const armThick = 0.5
const totalHeight = 4
const armAngle = 60
const totalLength = 9.74
const yDatum = 0
const code = `baseLength = 3
baseThick = 1
armThick = 0.5
totalHeight = 4
armAngle = 60
totalLength = 9.74
yDatum = 0
const baseThickHalf = baseThick / 2
const halfHeight = totalHeight / 2
const halfArmAngle = armAngle / 2
baseThickHalf = baseThick / 2
halfHeight = totalHeight / 2
halfArmAngle = armAngle / 2
part001 = startSketchOn('XY')
|> startProfileAt([-0.01, -0.05], %)
|> line([0.01, 0.94 + 0], %) // partial
|> line(end = [0.01, 0.94 + 0]) // partial
|> xLine(3.03, %) // partial
|> angledLine({
angle: halfArmAngle,
@ -599,9 +599,9 @@ part001 = startSketchOn('XY')
|> yLine(-1, %) // partial
|> xLine(-4.2 + 0, %) // full
|> angledLine([segAng(seg01bing) + 180, 1.79], %) // partial
|> line([1.44, -0.74], %) // free
|> line(end = [1.44, -0.74]) // free
|> xLine(3.36, %) // partial
|> line([-1.49, 1.06], %) // free
|> line(end = [1.49, 1.06]) // free
|> xLine(-3.43 + 0, %) // full
|> angledLineOfXLength([243 + 0, 1.2 + 0], %) // full`
const ast = assertParse(code)

View File

@ -11,6 +11,7 @@ import { Selections } from 'lib/selections'
import { cleanErrs, err } from 'lib/trap'
import {
CallExpression,
CallExpressionKw,
Program,
Expr,
BinaryPart,
@ -32,7 +33,9 @@ import {
createBinaryExpression,
createBinaryExpressionWithUnary,
createCallExpression,
createCallExpressionStdLibKw,
createIdentifier,
createLabeledArg,
createLiteral,
createObjectExpression,
createPipeSubstitution,
@ -43,6 +46,7 @@ import {
createFirstArg,
getConstraintInfo,
getFirstArg,
getArgForEnd,
replaceSketchLine,
} from './sketch'
import {
@ -113,6 +117,29 @@ function createCallWrapper(
tag?: Expr,
valueUsedInTransform?: number
): CreatedSketchExprResult {
if (Array.isArray(val)) {
if (tooltip === 'line') {
return {
callExp: createCallExpressionStdLibKw(
'line',
createPipeSubstitution(),
[createLabeledArg('end', createArrayExpression(val))]
),
valueUsedInTransform,
}
}
if (tooltip === 'lineTo') {
return {
callExp: createCallExpressionStdLibKw(
'line',
createPipeSubstitution(),
[createLabeledArg('endAbsolute', createArrayExpression(val))]
),
valueUsedInTransform,
}
}
}
const args =
tooltip === 'circle'
? []
@ -1845,19 +1872,41 @@ export function getConstraintLevelFromSourceRange(
ast: Program | Error
): Error | { range: [number, number]; level: ConstraintLevel } {
if (err(ast)) return ast
const nodeMeta = getNodeFromPath<Node<CallExpression>>(
ast,
getNodePathFromSourceRange(ast, cursorRange),
'CallExpression'
)
if (err(nodeMeta)) return nodeMeta
let partsOfCallNode = (() => {
const nodeMeta = getNodeFromPath<Node<CallExpression>>(
ast,
getNodePathFromSourceRange(ast, cursorRange),
'CallExpression'
)
if (err(nodeMeta)) return nodeMeta
const { node: sketchFnExp } = nodeMeta
const name = sketchFnExp?.callee?.name as ToolTip
const range: [number, number] = [sketchFnExp.start, sketchFnExp.end]
const { node: sketchFnExp } = nodeMeta
const name = sketchFnExp?.callee?.name as ToolTip
const range: [number, number] = [sketchFnExp.start, sketchFnExp.end]
const firstArg = getFirstArg(sketchFnExp)
return { name, range, firstArg }
})()
const partsOfCallKwNode = () => {
const nodeMeta = getNodeFromPath<Node<CallExpressionKw>>(
ast,
getNodePathFromSourceRange(ast, cursorRange),
'CallExpressionKw'
)
if (err(nodeMeta)) return nodeMeta
const { node: sketchFnExp } = nodeMeta
const name = sketchFnExp?.callee?.name as ToolTip
const range: [number, number] = [sketchFnExp.start, sketchFnExp.end]
const firstArg = getArgForEnd(sketchFnExp)
return { name, range, firstArg }
}
if (err(partsOfCallNode)) {
partsOfCallNode = partsOfCallKwNode()
}
if (err(partsOfCallNode)) return partsOfCallNode
const { name, range, firstArg } = partsOfCallNode
if (!toolTips.includes(name)) return { level: 'free', range: range }
const firstArg = getFirstArg(sketchFnExp)
if (err(firstArg)) return firstArg
// check if the function is fully constrained

View File

@ -166,6 +166,7 @@ export type SimplifiedArgDetails =
| Omit<ObjectPropertyInput<null>, 'expr' | 'argType'>
| Omit<ArrayOrObjItemInput<null>, 'expr' | 'argType'>
| Omit<ArrayInObject<null>, 'expr' | 'argType'>
/**
* Represents the result of creating a sketch expression (line, tangentialArcTo, angledLine, circle, etc.).
*

View File

@ -98,6 +98,7 @@ export type SyntaxType =
| 'ExpressionStatement'
| 'BinaryExpression'
| 'CallExpression'
| 'CallExpressionKw'
| 'Identifier'
| 'ReturnStatement'
| 'VariableDeclaration'