WIP: Start fixing frontend
This commit is contained in:
committed by
Nick Cameron
parent
725ffd952c
commit
235e526e2d
@ -7,6 +7,7 @@ import {
|
|||||||
Program,
|
Program,
|
||||||
PipeExpression,
|
PipeExpression,
|
||||||
CallExpression,
|
CallExpression,
|
||||||
|
CallExpressionKw,
|
||||||
VariableDeclarator,
|
VariableDeclarator,
|
||||||
Expr,
|
Expr,
|
||||||
VariableDeclaration,
|
VariableDeclaration,
|
||||||
@ -44,7 +45,9 @@ import {
|
|||||||
createLiteral,
|
createLiteral,
|
||||||
createTagDeclarator,
|
createTagDeclarator,
|
||||||
createCallExpression,
|
createCallExpression,
|
||||||
|
createCallExpressionStdLibKw,
|
||||||
createArrayExpression,
|
createArrayExpression,
|
||||||
|
createLabeledArg,
|
||||||
createPipeSubstitution,
|
createPipeSubstitution,
|
||||||
createObjectExpression,
|
createObjectExpression,
|
||||||
mutateArrExp,
|
mutateArrExp,
|
||||||
@ -58,6 +61,9 @@ import { TagDeclarator } from 'wasm-lib/kcl/bindings/TagDeclarator'
|
|||||||
import { EdgeCutInfo } from 'machines/modelingMachine'
|
import { EdgeCutInfo } from 'machines/modelingMachine'
|
||||||
import { Node } from 'wasm-lib/kcl/bindings/Node'
|
import { Node } from 'wasm-lib/kcl/bindings/Node'
|
||||||
|
|
||||||
|
const ARG_END = 'end'
|
||||||
|
const ARG_END_ABSOLUTE = 'endAbsolute'
|
||||||
|
|
||||||
const STRAIGHT_SEGMENT_ERR = new Error(
|
const STRAIGHT_SEGMENT_ERR = new Error(
|
||||||
'Invalid input, expected "straight-segment"'
|
'Invalid input, expected "straight-segment"'
|
||||||
)
|
)
|
||||||
@ -90,8 +96,6 @@ export function createFirstArg(
|
|||||||
'angledLineOfYLength',
|
'angledLineOfYLength',
|
||||||
'angledLineToX',
|
'angledLineToX',
|
||||||
'angledLineToY',
|
'angledLineToY',
|
||||||
'line',
|
|
||||||
'lineTo',
|
|
||||||
].includes(sketchFn)
|
].includes(sketchFn)
|
||||||
)
|
)
|
||||||
return createArrayExpression(val)
|
return createArrayExpression(val)
|
||||||
@ -415,10 +419,11 @@ export const line: SketchLineHelper = {
|
|||||||
!replaceExistingCallback &&
|
!replaceExistingCallback &&
|
||||||
pipe.type === 'PipeExpression'
|
pipe.type === 'PipeExpression'
|
||||||
) {
|
) {
|
||||||
const callExp = createCallExpression('line', [
|
const callExp = createCallExpressionStdLibKw(
|
||||||
createArrayExpression([newXVal, newYVal]),
|
'line',
|
||||||
createPipeSubstitution(),
|
createPipeSubstitution(),
|
||||||
])
|
[createLabeledArg(ARG_END, createArrayExpression([newXVal, newYVal]))]
|
||||||
|
)
|
||||||
const pathToNodeIndex = pathToNode.findIndex(
|
const pathToNodeIndex = pathToNode.findIndex(
|
||||||
(x) => x[1] === 'PipeExpression'
|
(x) => x[1] === 'PipeExpression'
|
||||||
)
|
)
|
||||||
@ -463,10 +468,11 @@ export const line: SketchLineHelper = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const callExp = createCallExpression('line', [
|
const callExp = createCallExpressionStdLibKw(
|
||||||
createArrayExpression([newXVal, newYVal]),
|
'line',
|
||||||
createPipeSubstitution(),
|
createPipeSubstitution(),
|
||||||
])
|
[createLabeledArg(ARG_END, createArrayExpression([newXVal, newYVal]))]
|
||||||
|
)
|
||||||
if (pipe.type === 'PipeExpression') {
|
if (pipe.type === 'PipeExpression') {
|
||||||
pipe.body = [...pipe.body, callExp]
|
pipe.body = [...pipe.body, callExp]
|
||||||
return {
|
return {
|
||||||
@ -2349,12 +2355,21 @@ function getFirstArgValuesForXYFns(callExpression: CallExpression):
|
|||||||
| Error {
|
| Error {
|
||||||
// used for lineTo, line
|
// used for lineTo, line
|
||||||
const firstArg = callExpression.arguments[0]
|
const firstArg = callExpression.arguments[0]
|
||||||
if (firstArg.type === 'ArrayExpression') {
|
return getValuesForXYFns(firstArg)
|
||||||
return { val: [firstArg.elements[0], firstArg.elements[1]] }
|
|
||||||
}
|
}
|
||||||
if (firstArg.type === 'ObjectExpression') {
|
|
||||||
const to = firstArg.properties.find((p) => p.key.name === 'to')?.value
|
function getValuesForXYFns(arg: Expr):
|
||||||
const tag = firstArg.properties.find((p) => p.key.name === 'tag')?.value
|
| {
|
||||||
|
val: [Expr, Expr]
|
||||||
|
tag?: Expr
|
||||||
|
}
|
||||||
|
| Error {
|
||||||
|
if (arg.type === 'ArrayExpression') {
|
||||||
|
return { val: [arg.elements[0], arg.elements[1]] }
|
||||||
|
}
|
||||||
|
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') {
|
if (to?.type === 'ArrayExpression') {
|
||||||
const [x, y] = to.elements
|
const [x, y] = to.elements
|
||||||
return { val: [x, y], tag }
|
return { val: [x, y], tag }
|
||||||
@ -2471,6 +2486,33 @@ const getAngledLineThatIntersects = (
|
|||||||
return new Error('expected ArrayExpression or ObjectExpression')
|
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):
|
export function getFirstArg(callExp: CallExpression):
|
||||||
| {
|
| {
|
||||||
val: Expr | [Expr, Expr] | [Expr, Expr, Expr]
|
val: Expr | [Expr, Expr] | [Expr, Expr, Expr]
|
||||||
@ -2478,9 +2520,6 @@ export function getFirstArg(callExp: CallExpression):
|
|||||||
}
|
}
|
||||||
| Error {
|
| Error {
|
||||||
const name = callExp?.callee?.name
|
const name = callExp?.callee?.name
|
||||||
if (['lineTo', 'line'].includes(name)) {
|
|
||||||
return getFirstArgValuesForXYFns(callExp)
|
|
||||||
}
|
|
||||||
if (
|
if (
|
||||||
[
|
[
|
||||||
'angledLine',
|
'angledLine',
|
||||||
|
@ -575,21 +575,21 @@ async function helperThing(
|
|||||||
|
|
||||||
describe('testing getConstraintLevelFromSourceRange', () => {
|
describe('testing getConstraintLevelFromSourceRange', () => {
|
||||||
it('should divide up lines into free, partial and fully contrained', () => {
|
it('should divide up lines into free, partial and fully contrained', () => {
|
||||||
const code = `const baseLength = 3
|
const code = `baseLength = 3
|
||||||
const baseThick = 1
|
baseThick = 1
|
||||||
const armThick = 0.5
|
armThick = 0.5
|
||||||
const totalHeight = 4
|
totalHeight = 4
|
||||||
const armAngle = 60
|
armAngle = 60
|
||||||
const totalLength = 9.74
|
totalLength = 9.74
|
||||||
const yDatum = 0
|
yDatum = 0
|
||||||
|
|
||||||
const baseThickHalf = baseThick / 2
|
baseThickHalf = baseThick / 2
|
||||||
const halfHeight = totalHeight / 2
|
halfHeight = totalHeight / 2
|
||||||
const halfArmAngle = armAngle / 2
|
halfArmAngle = armAngle / 2
|
||||||
|
|
||||||
part001 = startSketchOn('XY')
|
part001 = startSketchOn('XY')
|
||||||
|> startProfileAt([-0.01, -0.05], %)
|
|> startProfileAt([-0.01, -0.05], %)
|
||||||
|> line([0.01, 0.94 + 0], %) // partial
|
|> line(end = [0.01, 0.94 + 0]) // partial
|
||||||
|> xLine(3.03, %) // partial
|
|> xLine(3.03, %) // partial
|
||||||
|> angledLine({
|
|> angledLine({
|
||||||
angle: halfArmAngle,
|
angle: halfArmAngle,
|
||||||
@ -599,9 +599,9 @@ part001 = startSketchOn('XY')
|
|||||||
|> yLine(-1, %) // partial
|
|> yLine(-1, %) // partial
|
||||||
|> xLine(-4.2 + 0, %) // full
|
|> xLine(-4.2 + 0, %) // full
|
||||||
|> angledLine([segAng(seg01bing) + 180, 1.79], %) // partial
|
|> angledLine([segAng(seg01bing) + 180, 1.79], %) // partial
|
||||||
|> line([1.44, -0.74], %) // free
|
|> line(end = [1.44, -0.74]) // free
|
||||||
|> xLine(3.36, %) // partial
|
|> xLine(3.36, %) // partial
|
||||||
|> line([-1.49, 1.06], %) // free
|
|> line(end = [1.49, 1.06]) // free
|
||||||
|> xLine(-3.43 + 0, %) // full
|
|> xLine(-3.43 + 0, %) // full
|
||||||
|> angledLineOfXLength([243 + 0, 1.2 + 0], %) // full`
|
|> angledLineOfXLength([243 + 0, 1.2 + 0], %) // full`
|
||||||
const ast = assertParse(code)
|
const ast = assertParse(code)
|
||||||
|
@ -11,6 +11,7 @@ import { Selections } from 'lib/selections'
|
|||||||
import { cleanErrs, err } from 'lib/trap'
|
import { cleanErrs, err } from 'lib/trap'
|
||||||
import {
|
import {
|
||||||
CallExpression,
|
CallExpression,
|
||||||
|
CallExpressionKw,
|
||||||
Program,
|
Program,
|
||||||
Expr,
|
Expr,
|
||||||
BinaryPart,
|
BinaryPart,
|
||||||
@ -32,7 +33,9 @@ import {
|
|||||||
createBinaryExpression,
|
createBinaryExpression,
|
||||||
createBinaryExpressionWithUnary,
|
createBinaryExpressionWithUnary,
|
||||||
createCallExpression,
|
createCallExpression,
|
||||||
|
createCallExpressionStdLibKw,
|
||||||
createIdentifier,
|
createIdentifier,
|
||||||
|
createLabeledArg,
|
||||||
createLiteral,
|
createLiteral,
|
||||||
createObjectExpression,
|
createObjectExpression,
|
||||||
createPipeSubstitution,
|
createPipeSubstitution,
|
||||||
@ -43,6 +46,7 @@ import {
|
|||||||
createFirstArg,
|
createFirstArg,
|
||||||
getConstraintInfo,
|
getConstraintInfo,
|
||||||
getFirstArg,
|
getFirstArg,
|
||||||
|
getArgForEnd,
|
||||||
replaceSketchLine,
|
replaceSketchLine,
|
||||||
} from './sketch'
|
} from './sketch'
|
||||||
import {
|
import {
|
||||||
@ -113,6 +117,29 @@ function createCallWrapper(
|
|||||||
tag?: Expr,
|
tag?: Expr,
|
||||||
valueUsedInTransform?: number
|
valueUsedInTransform?: number
|
||||||
): CreatedSketchExprResult {
|
): 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 =
|
const args =
|
||||||
tooltip === 'circle'
|
tooltip === 'circle'
|
||||||
? []
|
? []
|
||||||
@ -1845,6 +1872,7 @@ export function getConstraintLevelFromSourceRange(
|
|||||||
ast: Program | Error
|
ast: Program | Error
|
||||||
): Error | { range: [number, number]; level: ConstraintLevel } {
|
): Error | { range: [number, number]; level: ConstraintLevel } {
|
||||||
if (err(ast)) return ast
|
if (err(ast)) return ast
|
||||||
|
let partsOfCallNode = (() => {
|
||||||
const nodeMeta = getNodeFromPath<Node<CallExpression>>(
|
const nodeMeta = getNodeFromPath<Node<CallExpression>>(
|
||||||
ast,
|
ast,
|
||||||
getNodePathFromSourceRange(ast, cursorRange),
|
getNodePathFromSourceRange(ast, cursorRange),
|
||||||
@ -1855,9 +1883,30 @@ export function getConstraintLevelFromSourceRange(
|
|||||||
const { node: sketchFnExp } = nodeMeta
|
const { node: sketchFnExp } = nodeMeta
|
||||||
const name = sketchFnExp?.callee?.name as ToolTip
|
const name = sketchFnExp?.callee?.name as ToolTip
|
||||||
const range: [number, number] = [sketchFnExp.start, sketchFnExp.end]
|
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 }
|
if (!toolTips.includes(name)) return { level: 'free', range: range }
|
||||||
|
|
||||||
const firstArg = getFirstArg(sketchFnExp)
|
|
||||||
if (err(firstArg)) return firstArg
|
if (err(firstArg)) return firstArg
|
||||||
|
|
||||||
// check if the function is fully constrained
|
// check if the function is fully constrained
|
||||||
|
@ -166,6 +166,7 @@ export type SimplifiedArgDetails =
|
|||||||
| Omit<ObjectPropertyInput<null>, 'expr' | 'argType'>
|
| Omit<ObjectPropertyInput<null>, 'expr' | 'argType'>
|
||||||
| Omit<ArrayOrObjItemInput<null>, 'expr' | 'argType'>
|
| Omit<ArrayOrObjItemInput<null>, 'expr' | 'argType'>
|
||||||
| Omit<ArrayInObject<null>, 'expr' | 'argType'>
|
| Omit<ArrayInObject<null>, 'expr' | 'argType'>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents the result of creating a sketch expression (line, tangentialArcTo, angledLine, circle, etc.).
|
* Represents the result of creating a sketch expression (line, tangentialArcTo, angledLine, circle, etc.).
|
||||||
*
|
*
|
||||||
|
@ -98,6 +98,7 @@ export type SyntaxType =
|
|||||||
| 'ExpressionStatement'
|
| 'ExpressionStatement'
|
||||||
| 'BinaryExpression'
|
| 'BinaryExpression'
|
||||||
| 'CallExpression'
|
| 'CallExpression'
|
||||||
|
| 'CallExpressionKw'
|
||||||
| 'Identifier'
|
| 'Identifier'
|
||||||
| 'ReturnStatement'
|
| 'ReturnStatement'
|
||||||
| 'VariableDeclaration'
|
| 'VariableDeclaration'
|
||||||
|
Reference in New Issue
Block a user