Add equal-length constraints & implement UnaryExpressions (#35)

* add segLen help to lang std

* adding helpers functions to sketchConstraints

* update tokeniser tests because they were annoying me not being 100%

* compare async lexer with sync lexer instead

* add helper functions

* remove unneeded nesting

* update add ast modifier function for angledLine

* initial equal ast modification

It adds a tag to the primary line, and converts any secondary lines to angledLine, but doesn't reference the taged/primary line yet

* Update fn call with refernce to previous line using segLen

* add test for giveSketchFnCallTag

* fix excutor bug, executing call expression in array expression

* fix small issue in executor

* add CallExpressions to BinaryExpressions

* add unary Expressions

* tweaks to unaryExpression logic

* add recasting for unaryExpressions and CallExpressions in BinaryExpressions

* ensure pipe substitution info is passed down to unary expressions and others

* allow binary expressions in function argumentns

* inital setup, new way of organising sketch fn transforms

Starting with equal length

* overhaul equalLength button

* add equal length support for angledLine

* line with one variable supports signed legLength

* fix indentation when recasting long arrayExpressions in a pipeExpression

* improve modifyAst consision

* further modify ast tidy

* equalLength transfroms far angledLineOfXLength

* add transforms for line-yRelative

* add equal constraint for angledLineOfYLength

* quick test fix

* add equal length constrain transforms for lineTo

* add equal length constraints for angledLineToX

* add equalLength constraints for angledLineToY

* test tidy

* setup new vertical-horizontal constraints

* Add equal Length constraints for vertical/horizontal lines

* migrate old tests, and refactor callback tag

* tweaks and refactor horzVert component

* fix leg len with small negative leg length
This commit is contained in:
Kurt Hutten
2023-03-02 21:19:11 +11:00
committed by GitHub
parent f70f0f7bc3
commit 6446601a67
21 changed files with 2280 additions and 666 deletions

View File

@ -1,3 +1,4 @@
import { Range, TooTip } from '../useStore'
import {
Program,
CallExpression,
@ -12,9 +13,16 @@ import {
Identifier,
ArrayExpression,
ObjectExpression,
getNodePathFromSourceRange,
UnaryExpression,
BinaryExpression,
} from './abstractSyntaxTree'
import { PathToNode, ProgramMemory } from './executor'
import { addTagForSketchOnFace } from './std/sketch'
import {
addTagForSketchOnFace,
getFirstArg,
createFirstArg,
} from './std/sketch'
export function addSketchTo(
node: Program,
@ -101,23 +109,7 @@ function addToShow(node: Program, name: string): Program {
const dumbyStartend = { start: 0, end: 0 }
const showCallIndex = getShowIndex(_node)
if (showCallIndex === -1) {
const showCall: CallExpression = {
type: 'CallExpression',
...dumbyStartend,
callee: {
type: 'Identifier',
...dumbyStartend,
name: 'show',
},
optional: false,
arguments: [
{
type: 'Identifier',
...dumbyStartend,
name,
},
],
}
const showCall = createCallExpression('show', [createIdentifier(name)])
const showExpressionStatement: ExpressionStatement = {
type: 'ExpressionStatement',
...dumbyStartend,
@ -128,25 +120,8 @@ function addToShow(node: Program, name: string): Program {
}
const showCall = { ..._node.body[showCallIndex] } as ExpressionStatement
const showCallArgs = (showCall.expression as CallExpression).arguments
const newShowCallArgs: Value[] = [
...showCallArgs,
{
type: 'Identifier',
...dumbyStartend,
name,
},
]
const newShowExpression: CallExpression = {
type: 'CallExpression',
...dumbyStartend,
callee: {
type: 'Identifier',
...dumbyStartend,
name: 'show',
},
optional: false,
arguments: newShowCallArgs,
}
const newShowCallArgs: Value[] = [...showCallArgs, createIdentifier(name)]
const newShowExpression = createCallExpression('show', newShowCallArgs)
_node.body[showCallIndex] = {
...showCall,
@ -247,40 +222,23 @@ export function extrudeSketch(
const { node: variableDeclorator, path: pathToDecleration } =
getNodeFromPath<VariableDeclarator>(_node, pathToNode, 'VariableDeclarator')
const extrudeCall: CallExpression = {
type: 'CallExpression',
...dumbyStartend,
callee: {
type: 'Identifier',
...dumbyStartend,
name: 'extrude',
},
optional: false,
arguments: [
createLiteral(4),
shouldPipe
? createPipeSubstitution()
: {
type: 'Identifier',
...dumbyStartend,
name: variableDeclorator.id.name,
},
],
}
if (shouldPipe) {
const pipeChain: PipeExpression = isInPipeExpression
? {
type: 'PipeExpression',
nonCodeMeta: {},
...dumbyStartend,
body: [...pipeExpression.body, extrudeCall],
}
const extrudeCall = createCallExpression('extrude', [
createLiteral(4),
shouldPipe
? createPipeSubstitution()
: {
type: 'PipeExpression',
nonCodeMeta: {},
type: 'Identifier',
...dumbyStartend,
body: [sketchExpression as any, extrudeCall], // TODO fix this #25
}
name: variableDeclorator.id.name,
},
])
if (shouldPipe) {
const pipeChain = createPipeExpression(
isInPipeExpression
? [...pipeExpression.body, extrudeCall]
: [sketchExpression as any, extrudeCall]
)
variableDeclorator.init = pipeChain
const pathToExtrudeArg = [
@ -299,23 +257,7 @@ export function extrudeSketch(
}
}
const name = findUniqueName(node, 'part')
const VariableDeclaration: VariableDeclaration = {
type: 'VariableDeclaration',
...dumbyStartend,
declarations: [
{
type: 'VariableDeclarator',
...dumbyStartend,
id: {
type: 'Identifier',
...dumbyStartend,
name,
},
init: extrudeCall,
},
],
kind: 'const',
}
const VariableDeclaration = createVariableDeclaration(name, extrudeCall)
const showCallIndex = getShowIndex(_node)
_node.body.splice(showCallIndex, 0, VariableDeclaration)
const pathToExtrudeArg = [
@ -509,3 +451,55 @@ export function createObjectExpression(properties: {
})),
}
}
export function createUnaryExpression(
argument: UnaryExpression['argument'],
operator: UnaryExpression['operator'] = '-'
): UnaryExpression {
return {
type: 'UnaryExpression',
start: 0,
end: 0,
operator,
argument,
}
}
export function createBinaryExpression([left, operator, right]: [
BinaryExpression['left'],
BinaryExpression['operator'],
BinaryExpression['right']
]): BinaryExpression {
return {
type: 'BinaryExpression',
start: 0,
end: 0,
operator,
left,
right,
}
}
export function giveSketchFnCallTag(
ast: Program,
range: Range
): { modifiedAst: Program; tag: string } {
const { node: primaryCallExp } = getNodeFromPath<CallExpression>(
ast,
getNodePathFromSourceRange(ast, range)
)
const firstArg = getFirstArg(primaryCallExp)
const tagValue = (firstArg.tag ||
createLiteral(findUniqueName(ast, 'seg', 2))) as Literal
const tagStr = String(tagValue.value)
const newFirstArg = createFirstArg(
primaryCallExp.callee.name as TooTip,
firstArg.val,
tagValue
)
primaryCallExp.arguments[0] = newFirstArg
return {
modifiedAst: ast,
tag: tagStr,
}
}