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

@ -1742,13 +1742,13 @@ describe('testing findEndofBinaryExpression', () => {
const code = `1 + 2 * 3\nconst yo = 5`
const tokens = lexer(code)
const end = findEndOfBinaryExpression(tokens, 0)
expect(end).toBe(8)
expect(tokens[end].value).toBe('3')
})
it('(1 + 2) / 5 - 3', () => {
const code = `(1 + 25) / 5 - 3\nconst yo = 5`
const tokens = lexer(code)
const end = findEndOfBinaryExpression(tokens, 0)
expect(end).toBe(14)
expect(tokens[end].value).toBe('3')
// expect to have the same end if started later in the string at a legitimate place
const indexOf5 = code.indexOf('5')
@ -1759,30 +1759,103 @@ describe('testing findEndofBinaryExpression', () => {
const code = '((1 + 2) / 5 - 3)\nconst yo = 5'
const tokens = lexer(code)
const end = findEndOfBinaryExpression(tokens, 0)
expect(end).toBe(code.indexOf('3)') + 1)
expect(tokens[end].end).toBe(code.indexOf('3)') + 2)
})
it('whole thing wraped but given index after the first brace: ((1 + 2) / 5 - 3)', () => {
const code = '((1 + 2) / 5 - 3)\nconst yo = 5'
const tokens = lexer(code)
const end = findEndOfBinaryExpression(tokens, 1)
expect(end).toBe(code.indexOf('3'))
expect(tokens[end].value).toBe('3')
})
it('given the index of a small wrapped section i.e. `1 + 2` in ((1 + 2) / 5 - 3)', () => {
const code = '((1 + 2) / 5 - 3)\nconst yo = 5'
const tokens = lexer(code)
const end = findEndOfBinaryExpression(tokens, 2)
expect(end).toBe(code.indexOf('2'))
expect(tokens[end].value).toBe('2')
})
it('lots of silly nesting: (1 + 2) / (5 - (3))', () => {
const code = '(1 + 2) / (5 - (3))\nconst yo = 5'
const tokens = lexer(code)
const end = findEndOfBinaryExpression(tokens, 0)
expect(end).toBe(code.indexOf('))') + 1)
expect(tokens[end].end).toBe(code.indexOf('))') + 2)
})
it('with pipe operator at the end', () => {
const code = '(1 + 2) / (5 - (3))\n |> fn(%)'
const tokens = lexer(code)
const end = findEndOfBinaryExpression(tokens, 0)
expect(end).toBe(code.indexOf('))') + 1)
expect(tokens[end].end).toBe(code.indexOf('))') + 2)
})
it('with call expression at the start of binary expression', () => {
const code = 'yo(2) + 3\n |> fn(%)'
const tokens = lexer(code)
const end = findEndOfBinaryExpression(tokens, 0)
expect(tokens[end].value).toBe('3')
})
it('with call expression at the end of binary expression', () => {
const code = '3 + yo(2)\n |> fn(%)'
const tokens = lexer(code)
const end = findEndOfBinaryExpression(tokens, 0)
expect(tokens[end].value).toBe(')')
})
})
describe('test UnaryExpression', () => {
it('should parse a unary expression in simple var dec situation', () => {
const code = `const myVar = -min(4, 100)`
const { body } = abstractSyntaxTree(lexer(code))
const myVarInit = (body?.[0] as any).declarations[0]?.init
expect(myVarInit).toEqual({
type: 'UnaryExpression',
operator: '-',
start: 14,
end: 26,
argument: {
type: 'CallExpression',
start: 15,
end: 26,
callee: { type: 'Identifier', start: 15, end: 18, name: 'min' },
arguments: [
{ type: 'Literal', start: 19, end: 20, value: 4, raw: '4' },
{ type: 'Literal', start: 22, end: 25, value: 100, raw: '100' },
],
optional: false,
},
})
})
})
describe('testing nested call expressions', () => {
it('callExp in a binExp in a callExp', () => {
const code = 'const myVar = min(100, 1 + legLen(5, 3))'
const { body } = abstractSyntaxTree(lexer(code))
const myVarInit = (body?.[0] as any).declarations[0]?.init
expect(myVarInit).toEqual({
type: 'CallExpression',
start: 14,
end: 40,
callee: { type: 'Identifier', start: 14, end: 17, name: 'min' },
arguments: [
{ type: 'Literal', start: 18, end: 21, value: 100, raw: '100' },
{
type: 'BinaryExpression',
operator: '+',
start: 23,
end: 39,
left: { type: 'Literal', value: 1, raw: '1', start: 23, end: 24 },
right: {
type: 'CallExpression',
start: 27,
end: 39,
callee: { type: 'Identifier', start: 27, end: 33, name: 'legLen' },
arguments: [
{ type: 'Literal', start: 34, end: 35, value: 5, raw: '5' },
{ type: 'Literal', start: 37, end: 38, value: 3, raw: '3' },
],
optional: false,
},
},
],
optional: false,
})
})
})