more transform functions for various line combos (#56)
This commit is contained in:
@ -1859,3 +1859,39 @@ describe('testing nested call expressions', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('should recognise callExpresions in binaryExpressions', () => {
|
||||||
|
const code = "xLineTo(segEndX('seg02', %) + 1, %)"
|
||||||
|
it('should recognise the callExp', () => {
|
||||||
|
const tokens = lexer(code)
|
||||||
|
const { body } = abstractSyntaxTree(tokens)
|
||||||
|
const callExpArgs = (body?.[0] as any).expression?.arguments
|
||||||
|
expect(callExpArgs).toEqual([
|
||||||
|
{
|
||||||
|
type: 'BinaryExpression',
|
||||||
|
operator: '+',
|
||||||
|
start: 8,
|
||||||
|
end: 31,
|
||||||
|
left: {
|
||||||
|
type: 'CallExpression',
|
||||||
|
start: 8,
|
||||||
|
end: 27,
|
||||||
|
callee: { type: 'Identifier', start: 8, end: 15, name: 'segEndX' },
|
||||||
|
arguments: [
|
||||||
|
{
|
||||||
|
type: 'Literal',
|
||||||
|
start: 16,
|
||||||
|
end: 23,
|
||||||
|
value: 'seg02',
|
||||||
|
raw: "'seg02'",
|
||||||
|
},
|
||||||
|
{ type: 'PipeSubstitution', start: 25, end: 26 },
|
||||||
|
],
|
||||||
|
optional: false,
|
||||||
|
},
|
||||||
|
right: { type: 'Literal', value: 1, raw: '1', start: 30, end: 31 },
|
||||||
|
},
|
||||||
|
{ type: 'PipeSubstitution', start: 33, end: 34 },
|
||||||
|
])
|
||||||
|
})
|
||||||
|
})
|
||||||
|
@ -183,10 +183,8 @@ export function makeCallExpression(
|
|||||||
} {
|
} {
|
||||||
const currentToken = tokens[index]
|
const currentToken = tokens[index]
|
||||||
const braceToken = nextMeaningfulToken(tokens, index)
|
const braceToken = nextMeaningfulToken(tokens, index)
|
||||||
// const firstArgumentToken = nextMeaningfulToken(tokens, braceToken.index);
|
|
||||||
const callee = makeIdentifier(tokens, index)
|
const callee = makeIdentifier(tokens, index)
|
||||||
const args = makeArguments(tokens, braceToken.index)
|
const args = makeArguments(tokens, braceToken.index)
|
||||||
// const closingBraceToken = nextMeaningfulToken(tokens, args.lastIndex);
|
|
||||||
const closingBraceToken = tokens[args.lastIndex]
|
const closingBraceToken = tokens[args.lastIndex]
|
||||||
return {
|
return {
|
||||||
expression: {
|
expression: {
|
||||||
@ -321,6 +319,25 @@ function makeArguments(
|
|||||||
nextBraceOrCommaToken.token.type === 'brace' &&
|
nextBraceOrCommaToken.token.type === 'brace' &&
|
||||||
nextBraceOrCommaToken.token.value === '('
|
nextBraceOrCommaToken.token.value === '('
|
||||||
) {
|
) {
|
||||||
|
const closingBrace = findClosingBrace(tokens, nextBraceOrCommaToken.index)
|
||||||
|
const tokenAfterClosingBrace = nextMeaningfulToken(tokens, closingBrace)
|
||||||
|
if (
|
||||||
|
tokenAfterClosingBrace.token.type === 'operator' &&
|
||||||
|
tokenAfterClosingBrace.token.value !== '|>'
|
||||||
|
) {
|
||||||
|
const { expression, lastIndex } = makeBinaryExpression(
|
||||||
|
tokens,
|
||||||
|
argumentToken.index
|
||||||
|
)
|
||||||
|
const nextCommarOrBraceTokenIndex = nextMeaningfulToken(
|
||||||
|
tokens,
|
||||||
|
lastIndex
|
||||||
|
).index
|
||||||
|
return makeArguments(tokens, nextCommarOrBraceTokenIndex, [
|
||||||
|
...previousArgs,
|
||||||
|
expression,
|
||||||
|
])
|
||||||
|
}
|
||||||
const { expression, lastIndex } = makeCallExpression(
|
const { expression, lastIndex } = makeCallExpression(
|
||||||
tokens,
|
tokens,
|
||||||
argumentToken.index
|
argumentToken.index
|
||||||
|
@ -460,15 +460,26 @@ export const xLine: SketchLineHelper = {
|
|||||||
|
|
||||||
const newVal = createLiteral(roundOff(to[0] - from[0], 2))
|
const newVal = createLiteral(roundOff(to[0] - from[0], 2))
|
||||||
const firstArg = newVal
|
const firstArg = newVal
|
||||||
const newLine = createCallback
|
|
||||||
? createCallback([firstArg, firstArg]).callExp
|
if (replaceExisting && createCallback) {
|
||||||
: createCallExpression('xLine', [firstArg, createPipeSubstitution()])
|
const callIndex = getLastIndex(pathToNode)
|
||||||
const callIndex = getLastIndex(pathToNode)
|
const { callExp, valueUsedInTransform } = createCallback([
|
||||||
if (replaceExisting) {
|
firstArg,
|
||||||
pipe.body[callIndex] = newLine
|
firstArg,
|
||||||
} else {
|
])
|
||||||
pipe.body = [...pipe.body, newLine]
|
pipe.body[callIndex] = callExp
|
||||||
|
return {
|
||||||
|
modifiedAst: _node,
|
||||||
|
pathToNode,
|
||||||
|
valueUsedInTransform,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const newLine = createCallExpression('xLine', [
|
||||||
|
firstArg,
|
||||||
|
createPipeSubstitution(),
|
||||||
|
])
|
||||||
|
pipe.body = [...pipe.body, newLine]
|
||||||
return { modifiedAst: _node, pathToNode }
|
return { modifiedAst: _node, pathToNode }
|
||||||
},
|
},
|
||||||
updateArgs: ({ node, pathToNode, to, from }) => {
|
updateArgs: ({ node, pathToNode, to, from }) => {
|
||||||
@ -513,15 +524,22 @@ export const yLine: SketchLineHelper = {
|
|||||||
const getNode = getNodeFromPathCurry(_node, pathToNode)
|
const getNode = getNodeFromPathCurry(_node, pathToNode)
|
||||||
const { node: pipe } = getNode<PipeExpression>('PipeExpression')
|
const { node: pipe } = getNode<PipeExpression>('PipeExpression')
|
||||||
const newVal = createLiteral(roundOff(to[1] - from[1], 2))
|
const newVal = createLiteral(roundOff(to[1] - from[1], 2))
|
||||||
const newLine = createCallback
|
if (replaceExisting && createCallback) {
|
||||||
? createCallback([newVal, newVal]).callExp
|
const callIndex = getLastIndex(pathToNode)
|
||||||
: createCallExpression('yLine', [newVal, createPipeSubstitution()])
|
const { callExp, valueUsedInTransform } = createCallback([newVal, newVal])
|
||||||
const callIndex = getLastIndex(pathToNode)
|
pipe.body[callIndex] = callExp
|
||||||
if (replaceExisting) {
|
return {
|
||||||
pipe.body[callIndex] = newLine
|
modifiedAst: _node,
|
||||||
} else {
|
pathToNode,
|
||||||
pipe.body = [...pipe.body, newLine]
|
valueUsedInTransform,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const newLine = createCallExpression('yLine', [
|
||||||
|
newVal,
|
||||||
|
createPipeSubstitution(),
|
||||||
|
])
|
||||||
|
pipe.body = [...pipe.body, newLine]
|
||||||
return { modifiedAst: _node, pathToNode }
|
return { modifiedAst: _node, pathToNode }
|
||||||
},
|
},
|
||||||
updateArgs: ({ node, pathToNode, to, from }) => {
|
updateArgs: ({ node, pathToNode, to, from }) => {
|
||||||
@ -958,18 +976,23 @@ export const angledLineToY: SketchLineHelper = {
|
|||||||
)
|
)
|
||||||
const angle = createLiteral(roundOff(getAngle(from, to), 0))
|
const angle = createLiteral(roundOff(getAngle(from, to), 0))
|
||||||
const yArg = createLiteral(roundOff(to[1], 2))
|
const yArg = createLiteral(roundOff(to[1], 2))
|
||||||
const newLine = createCallback
|
|
||||||
? createCallback([angle, yArg]).callExp
|
if (replaceExisting && createCallback) {
|
||||||
: createCallExpression('angledLineToY', [
|
const { callExp, valueUsedInTransform } = createCallback([angle, yArg])
|
||||||
createArrayExpression([angle, yArg]),
|
const callIndex = getLastIndex(pathToNode)
|
||||||
createPipeSubstitution(),
|
pipe.body[callIndex] = callExp
|
||||||
])
|
return {
|
||||||
const callIndex = getLastIndex(pathToNode)
|
modifiedAst: _node,
|
||||||
if (replaceExisting) {
|
pathToNode,
|
||||||
pipe.body[callIndex] = newLine
|
valueUsedInTransform,
|
||||||
} else {
|
}
|
||||||
pipe.body = [...pipe.body, newLine]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const newLine = createCallExpression('angledLineToY', [
|
||||||
|
createArrayExpression([angle, yArg]),
|
||||||
|
createPipeSubstitution(),
|
||||||
|
])
|
||||||
|
pipe.body = [...pipe.body, newLine]
|
||||||
return {
|
return {
|
||||||
modifiedAst: _node,
|
modifiedAst: _node,
|
||||||
pathToNode,
|
pathToNode,
|
||||||
|
@ -78,6 +78,23 @@ type TransformMap = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const xyLineSetLength =
|
||||||
|
(
|
||||||
|
xOrY: 'xLine' | 'yLine',
|
||||||
|
referenceSeg = false
|
||||||
|
): TransformInfo['createNode'] =>
|
||||||
|
({ referenceSegName, tag, forceValueUsedInTransform }) =>
|
||||||
|
(args) => {
|
||||||
|
console.log('args', args)
|
||||||
|
const segRef = createSegLen(referenceSegName)
|
||||||
|
const lineVal = forceValueUsedInTransform
|
||||||
|
? forceValueUsedInTransform
|
||||||
|
: referenceSeg
|
||||||
|
? segRef
|
||||||
|
: args[0]
|
||||||
|
return createCallWrapper(xOrY, lineVal, tag, getArgLiteralVal(args[0]))
|
||||||
|
}
|
||||||
|
|
||||||
const basicAngledLineCreateNode =
|
const basicAngledLineCreateNode =
|
||||||
(
|
(
|
||||||
referenceSeg: 'ang' | 'len' | 'none' = 'none',
|
referenceSeg: 'ang' | 'len' | 'none' = 'none',
|
||||||
@ -397,6 +414,20 @@ const transformMap: TransformMap = {
|
|||||||
() =>
|
() =>
|
||||||
createCallWrapper('yLineTo', varValB, tag),
|
createCallWrapper('yLineTo', varValB, tag),
|
||||||
},
|
},
|
||||||
|
setAngle: {
|
||||||
|
tooltip: 'angledLineToY',
|
||||||
|
createNode:
|
||||||
|
({ varValB, tag, forceValueUsedInTransform }) =>
|
||||||
|
(args) => {
|
||||||
|
console.log(getArgLiteralVal(args[0]))
|
||||||
|
return createCallWrapper(
|
||||||
|
'angledLineToY',
|
||||||
|
[forceValueUsedInTransform || args[0], varValB],
|
||||||
|
tag,
|
||||||
|
getArgLiteralVal(args[0])
|
||||||
|
)
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
angledLine: {
|
angledLine: {
|
||||||
@ -675,8 +706,40 @@ const transformMap: TransformMap = {
|
|||||||
tooltip: 'xLine',
|
tooltip: 'xLine',
|
||||||
createNode:
|
createNode:
|
||||||
({ referenceSegName, tag }) =>
|
({ referenceSegName, tag }) =>
|
||||||
() =>
|
(arg) => {
|
||||||
createCallWrapper('xLine', createSegLen(referenceSegName), tag),
|
const argVal = getArgLiteralVal(arg[0])
|
||||||
|
const segLen = createSegLen(referenceSegName) as BinaryPart
|
||||||
|
const val = argVal > 0 ? segLen : createUnaryExpression(segLen)
|
||||||
|
return createCallWrapper('xLine', val, tag, argVal)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
setHorzDistance: {
|
||||||
|
tooltip: 'xLineTo',
|
||||||
|
createNode: ({ referenceSegName, tag, forceValueUsedInTransform }) => {
|
||||||
|
return (args, referencedSegment) => {
|
||||||
|
console.log('args', args)
|
||||||
|
const valueUsedInTransform = roundOff(
|
||||||
|
getArgLiteralVal(args?.[0]) - (referencedSegment?.to?.[0] || 0),
|
||||||
|
2
|
||||||
|
)
|
||||||
|
const makeBinExp = createBinaryExpression([
|
||||||
|
createSegEnd(referenceSegName, true),
|
||||||
|
'+',
|
||||||
|
(forceValueUsedInTransform as BinaryPart) ||
|
||||||
|
createLiteral(valueUsedInTransform),
|
||||||
|
])
|
||||||
|
return createCallWrapper(
|
||||||
|
'xLineTo',
|
||||||
|
makeBinExp,
|
||||||
|
tag,
|
||||||
|
valueUsedInTransform
|
||||||
|
)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
setLength: {
|
||||||
|
tooltip: 'xLine',
|
||||||
|
createNode: xyLineSetLength('xLine'),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -689,6 +752,10 @@ const transformMap: TransformMap = {
|
|||||||
() =>
|
() =>
|
||||||
createCallWrapper('yLine', createSegLen(referenceSegName), tag),
|
createCallWrapper('yLine', createSegLen(referenceSegName), tag),
|
||||||
},
|
},
|
||||||
|
setLength: {
|
||||||
|
tooltip: 'yLine',
|
||||||
|
createNode: xyLineSetLength('yLine'),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
xLineTo: {
|
xLineTo: {
|
||||||
|
Reference in New Issue
Block a user