pipe working for everything except sketches

This commit is contained in:
Kurt Hutten IrevDev
2022-12-03 22:50:46 +11:00
parent 15bddcc842
commit f0076309ef
4 changed files with 481 additions and 149 deletions

View File

@ -648,8 +648,6 @@ describe('structures specific to this lang', () => {
]) ])
}) })
}) })
describe('testing hasPipeOperator', () => { describe('testing hasPipeOperator', () => {
test('hasPipeOperator is true', () => { test('hasPipeOperator is true', () => {
let code = `sketch mySketch { let code = `sketch mySketch {
@ -669,10 +667,13 @@ describe('testing hasPipeOperator', () => {
} |> rx(45, %) |> rx(45, %) } |> rx(45, %) |> rx(45, %)
` `
const tokens = lexer(code) const tokens = lexer(code)
expect(hasPipeOperator(tokens, 0)).toEqual({ const result = hasPipeOperator(tokens, 0)
expect(result).toEqual({
index: 16, index: 16,
token: { end: 37, start: 35, type: 'operator', value: '|>' }, token: { end: 37, start: 35, type: 'operator', value: '|>' },
}) })
if (!result) throw new Error('should not happen')
expect(code.slice(result.token.start, result.token.end)).toEqual('|>')
}) })
test('hasPipeOperator is false when the pipe operator is after a new variable declaration', () => { test('hasPipeOperator is false when the pipe operator is after a new variable declaration', () => {
let code = `sketch mySketch { let code = `sketch mySketch {
@ -684,114 +685,235 @@ const yo = myFunc(9()
const tokens = lexer(code) const tokens = lexer(code)
expect(hasPipeOperator(tokens, 0)).toEqual(false) expect(hasPipeOperator(tokens, 0)).toEqual(false)
}) })
test('hasPipeOperator with binary expression', () => {
let code = `const myVar2 = 5 + 1 |> myFn(%)`
const tokens = lexer(code)
const result = hasPipeOperator(tokens, 1)
expect(result).toEqual({
index: 12,
token: { end: 23, start: 21, type: 'operator', value: '|>' },
})
if (!result) throw new Error('should not happen')
expect(code.slice(result.token.start, result.token.end)).toEqual('|>')
})
test('hasPipeOperator of called mid sketchExpression on a callExpression, and called at the start of the sketchExpression at "{"', () => {
const code = [
'sketch mySk1 {',
' lineTo(1,1)',
' path myPath = lineTo(0, 1)',
' lineTo(1,1)',
'} |> rx(90, %)',
'show(mySk1)',
].join('\n')
const tokens = lexer(code)
const tokenWithMyPathIndex = tokens.findIndex(
({ value }) => value === 'myPath'
)
const tokenWithLineToIndexForVarDecIndex = tokens.findIndex(
({ value }, index) => value === 'lineTo' && index > tokenWithMyPathIndex
)
const result = hasPipeOperator(tokens, tokenWithLineToIndexForVarDecIndex)
expect(result).toBe(false)
const braceTokenIndex = tokens.findIndex(({ value }) => value === '{')
const result2 = hasPipeOperator(tokens, braceTokenIndex)
expect(result2).toEqual({
index: 36,
token: { end: 76, start: 74, type: 'operator', value: '|>' },
})
if (!result2) throw new Error('should not happen')
expect(code.slice(result2?.token?.start, result2?.token?.end)).toEqual('|>')
})
}) })
describe('testing pipe operator', () => { describe('testing pipe operator special', () => {
test('pipe operator with sketch', () => { test('pipe operator with sketch', () => {
let code = `sketch mySketch { let code = `sketch mySketch {
lineTo(2, 3) lineTo(2, 3)
path myPath = lineTo(0, 1)
lineTo(1,1)
} |> rx(45, %) } |> rx(45, %)
` `
const tokens = lexer(code) const tokens = lexer(code)
const { body } = abstractSyntaxTree(tokens) const { body } = abstractSyntaxTree(tokens)
expect(body).toEqual([ expect(body).toEqual([
{ {
"type": "VariableDeclaration", type: 'VariableDeclaration',
"start": 0, start: 0,
"end": 47, end: 90,
"kind": "sketch", kind: 'sketch',
"declarations": [ declarations: [
{ {
"type": "VariableDeclarator", type: 'VariableDeclarator',
"start": 7, start: 7,
"end": 47, end: 90,
"id": { id: {
"type": "Identifier", type: 'Identifier',
"start": 7, start: 7,
"end": 15, end: 15,
"name": "mySketch" name: 'mySketch',
}, },
"init": { init: {
"type": "PipeExpression", type: 'PipeExpression',
"start": 16, start: 16,
"end": 47, end: 90,
"body": [ body: [
{ {
"type": "SketchExpression", type: 'SketchExpression',
"start": 16, start: 16,
"end": 34, end: 77,
"body": { body: {
"type": "BlockStatement", type: 'BlockStatement',
"start": 16, start: 16,
"end": 34, end: 77,
"body": [ body: [
{ {
"type": "ExpressionStatement", type: 'ExpressionStatement',
"start": 20, start: 20,
"end": 32, end: 32,
"expression": { expression: {
"type": "CallExpression", type: 'CallExpression',
"start": 20, start: 20,
"end": 32, end: 32,
"callee": { callee: {
"type": "Identifier", type: 'Identifier',
"start": 20, start: 20,
"end": 26, end: 26,
"name": "lineTo" name: 'lineTo',
}, },
"arguments": [ arguments: [
{ {
"type": "Literal", type: 'Literal',
"start": 27, start: 27,
"end": 28, end: 28,
"value": 2, value: 2,
"raw": "2" raw: '2',
}, },
{ {
"type": "Literal", type: 'Literal',
"start": 30, start: 30,
"end": 31, end: 31,
"value": 3, value: 3,
"raw": "3" raw: '3',
} },
], ],
"optional": false optional: false,
} },
}
]
}
}, },
{ {
"type": "CallExpression", type: 'VariableDeclaration',
"start": 38, start: 35,
"end": 47, end: 61,
"callee": { kind: 'path',
"type": "Identifier", declarations: [
"start": 38,
"end": 40,
"name": "rx"
},
"arguments": [
{ {
"type": "Literal", type: 'VariableDeclarator',
"start": 41, start: 40,
"end": 43, end: 61,
"value": 45, id: {
"raw": "45" type: 'Identifier',
start: 40,
end: 46,
name: 'myPath',
},
init: {
type: 'CallExpression',
start: 49,
end: 61,
callee: {
type: 'Identifier',
start: 49,
end: 55,
name: 'lineTo',
},
arguments: [
{
type: 'Literal',
start: 56,
end: 57,
value: 0,
raw: '0',
}, },
{ {
"type": "PipeSubstitution", type: 'Literal',
"start": 45, start: 59,
"end": 46 end: 60,
} value: 1,
raw: '1',
},
], ],
"optional": false optional: false,
} },
] },
} ],
} },
] {
} type: 'ExpressionStatement',
start: 64,
end: 75,
expression: {
type: 'CallExpression',
start: 64,
end: 75,
callee: {
type: 'Identifier',
start: 64,
end: 70,
name: 'lineTo',
},
arguments: [
{
type: 'Literal',
start: 71,
end: 72,
value: 1,
raw: '1',
},
{
type: 'Literal',
start: 73,
end: 74,
value: 1,
raw: '1',
},
],
optional: false,
},
},
],
},
},
{
type: 'CallExpression',
start: 81,
end: 90,
callee: {
type: 'Identifier',
start: 81,
end: 83,
name: 'rx',
},
arguments: [
{
type: 'Literal',
start: 84,
end: 86,
value: 45,
raw: '45',
},
{
type: 'PipeSubstitution',
start: 88,
end: 89,
},
],
optional: false,
},
],
},
},
],
},
]) ])
}) })
test('pipe operator with binary expression', () => { test('pipe operator with binary expression', () => {
@ -800,77 +922,77 @@ describe('testing pipe operator', () => {
const { body } = abstractSyntaxTree(tokens) const { body } = abstractSyntaxTree(tokens)
expect(body).toEqual([ expect(body).toEqual([
{ {
"type": "VariableDeclaration", type: 'VariableDeclaration',
"start": 0, start: 0,
"end": 36, end: 36,
"kind": "const", kind: 'const',
"declarations": [ declarations: [
{ {
"type": "VariableDeclarator", type: 'VariableDeclarator',
"start": 6, start: 6,
"end": 36, end: 36,
"id": { id: {
"type": "Identifier", type: 'Identifier',
"start": 6, start: 6,
"end": 11, end: 11,
"name": "myVar" name: 'myVar',
}, },
"init": { init: {
"type": "PipeExpression", type: 'PipeExpression',
"start": 12, start: 12,
"end": 36, end: 36,
"body": [ body: [
{ {
"type": "BinaryExpression", type: 'BinaryExpression',
"start": 14, start: 14,
"end": 19, end: 19,
"left": { left: {
"type": "Literal", type: 'Literal',
"start": 14, start: 14,
"end": 15, end: 15,
"value": 5, value: 5,
"raw": "5" raw: '5',
},
operator: '+',
right: {
type: 'Literal',
start: 18,
end: 19,
value: 6,
raw: '6',
}, },
"operator": "+",
"right": {
"type": "Literal",
"start": 18,
"end": 19,
"value": 6,
"raw": "6"
}
}, },
{ {
"type": "CallExpression", type: 'CallExpression',
"start": 23, start: 23,
"end": 36, end: 36,
"callee": { callee: {
"type": "Identifier", type: 'Identifier',
"start": 23, start: 23,
"end": 29, end: 29,
"name": "myFunc" name: 'myFunc',
}, },
"arguments": [ arguments: [
{ {
"type": "Literal", type: 'Literal',
"start": 30, start: 30,
"end": 32, end: 32,
"value": 45, value: 45,
"raw": "45" raw: '45',
}, },
{ {
"type": "PipeSubstitution", type: 'PipeSubstitution',
"start": 34, start: 34,
"end": 35 end: 35,
} },
], ],
"optional": false optional: false,
} },
] ],
} },
} },
] ],
} },
]) ])
}) })
}) })

View File

@ -233,6 +233,7 @@ function makeVariableDeclaration(
tokens: Token[], tokens: Token[],
index: number index: number
): { declaration: VariableDeclaration; lastIndex: number } { ): { declaration: VariableDeclaration; lastIndex: number } {
// token index should point to a declaration keyword i.e. const, fn, sketch, path
const currentToken = tokens[index] const currentToken = tokens[index]
const declarationStartToken = nextMeaningfulToken(tokens, index) const declarationStartToken = nextMeaningfulToken(tokens, index)
const { declarations, lastIndex } = makeVariableDeclarators( const { declarations, lastIndex } = makeVariableDeclarators(
@ -283,7 +284,10 @@ function makeValue(
lastIndex, lastIndex,
} }
} }
if ((currentToken.type === 'word' || currentToken.type === 'number') && nextToken.type === 'operator') { if (
(currentToken.type === 'word' || currentToken.type === 'number') &&
nextToken.type === 'operator'
) {
const { expression, lastIndex } = makeBinaryExpression(tokens, index) const { expression, lastIndex } = makeBinaryExpression(tokens, index)
return { return {
value: expression, value: expression,
@ -321,12 +325,13 @@ function makeVariableDeclarators(
declarations: VariableDeclarator[] declarations: VariableDeclarator[]
lastIndex: number lastIndex: number
} { } {
const nextPipeOperator = hasPipeOperator(tokens, 0)
const currentToken = tokens[index] const currentToken = tokens[index]
const assignmentToken = nextMeaningfulToken(tokens, index) const assignmentToken = nextMeaningfulToken(tokens, index)
const declarationToken = previousMeaningfulToken(tokens, index) const declarationToken = previousMeaningfulToken(tokens, index)
const contentsStartToken = nextMeaningfulToken(tokens, assignmentToken.index) const contentsStartToken = nextMeaningfulToken(tokens, assignmentToken.index)
const nextAfterInit = nextMeaningfulToken(tokens, contentsStartToken.index) const nextAfterInit = nextMeaningfulToken(tokens, contentsStartToken.index)
const pipeStartIndex = assignmentToken?.token?.type === 'operator' ? contentsStartToken.index : assignmentToken.index
const nextPipeOperator = hasPipeOperator(tokens, pipeStartIndex)
let init: Value let init: Value
let lastIndex = contentsStartToken.index let lastIndex = contentsStartToken.index
if (nextPipeOperator) { if (nextPipeOperator) {
@ -814,16 +819,86 @@ export function findNextDeclarationKeyword(
) { ) {
return nextToken return nextToken
} }
if (nextToken.token.type === 'brace' && nextToken.token.value === '(') {
const closingBraceIndex = findClosingBrace(tokens, nextToken.index)
const arrowToken = nextMeaningfulToken(tokens, closingBraceIndex)
if (
arrowToken?.token?.type === 'operator' &&
arrowToken.token.value === '=>'
) {
return nextToken
}
// return findNextDeclarationKeyword(tokens, nextToken.index)
// probably should do something else here
// throw new Error('Unexpected token')
}
return findNextDeclarationKeyword(tokens, nextToken.index) return findNextDeclarationKeyword(tokens, nextToken.index)
} }
export function findNextCallExpression(
tokens: Token[],
index: number
): { token: Token | null; index: number } {
const nextToken = nextMeaningfulToken(tokens, index)
const veryNextToken = tokens[nextToken.index + 1] // i.e. without whitespace
if (nextToken.index >= tokens.length) {
return { token: null, index: tokens.length - 1 }
}
if (nextToken.token.type === 'word' && veryNextToken?.type === 'brace' && veryNextToken?.value === '(') {
return nextToken
}
return findNextCallExpression(tokens, nextToken.index)
}
export function findNextClosingCurlyBrace(
tokens: Token[],
index: number
): { token: Token | null; index: number } {
const nextToken = nextMeaningfulToken(tokens, index)
if (nextToken.index >= tokens.length) {
return { token: null, index: tokens.length - 1 }
}
if (nextToken.token.type === 'brace' && nextToken.token.value === '}') {
return nextToken
}
if (nextToken.token.type === 'brace' && nextToken.token.value === '{') {
const closingBraceIndex = findClosingBrace(tokens, nextToken.index)
const tokenAfterClosingBrace = nextMeaningfulToken(
tokens,
closingBraceIndex
)
return findNextClosingCurlyBrace(tokens, tokenAfterClosingBrace.index)
}
return findNextClosingCurlyBrace(tokens, nextToken.index)
}
export function hasPipeOperator( export function hasPipeOperator(
tokens: Token[], tokens: Token[],
index: number, index: number,
_limitIndex = -1 _limitIndex = -1
): { token: Token; index: number } | false { ): { token: Token; index: number } | false {
// this probably still needs some work
// should be called on expression statuments (i.e "lineTo" for lineTo(10, 10)) or "{" for sketch declarations
let limitIndex = _limitIndex let limitIndex = _limitIndex
if (limitIndex === -1) { if (limitIndex === -1) {
const callExpressionEnd = isCallExpression(tokens, index)
if (callExpressionEnd !== -1) {
const tokenAfterCallExpression = nextMeaningfulToken(tokens, callExpressionEnd)
if (tokenAfterCallExpression?.token?.type === 'operator' && tokenAfterCallExpression.token.value === '|>') {
return tokenAfterCallExpression
}
return false
}
const currentToken = tokens[index]
if (currentToken?.type === 'brace' && currentToken?.value === '{') {
const closingBraceIndex = findClosingBrace(tokens, index)
const tokenAfterClosingBrace = nextMeaningfulToken(tokens, closingBraceIndex)
if (tokenAfterClosingBrace?.token?.type === 'operator' && tokenAfterClosingBrace.token.value === '|>') {
return tokenAfterClosingBrace
}
return false
}
const nextDeclaration = findNextDeclarationKeyword(tokens, index) const nextDeclaration = findNextDeclarationKeyword(tokens, index)
limitIndex = nextDeclaration.index limitIndex = nextDeclaration.index
} }
@ -843,6 +918,7 @@ export function findClosingBrace(
_braceCount: number = 0, _braceCount: number = 0,
_searchOpeningBrace: string = '' _searchOpeningBrace: string = ''
): number { ): number {
// should be called with the index of the opening brace
const closingBraceMap: { [key: string]: string } = { const closingBraceMap: { [key: string]: string } = {
'(': ')', '(': ')',
'{': '}', '{': '}',
@ -1127,3 +1203,50 @@ function getSketchStatement(
index: sketchStatementIndex, index: sketchStatementIndex,
} }
} }
function isCallExpression(
tokens: Token[],
index: number
): number {
const currentToken = tokens[index]
const veryNextToken = tokens[index + 1] // i.e. no whitespace
if(currentToken.type === 'word' && veryNextToken.type === 'brace' &&veryNextToken.value === '(') {
return findClosingBrace(tokens, index + 1)
}
return -1
}
function debuggerr(tokens: Token[], indexes: number[], msg=''): string {
// return ''
const sortedIndexes = [...indexes].sort((a, b) => a - b)
const min = Math.min(...indexes)
const start = Math.min(Math.abs(min - 1), 0)
const max = Math.max(...indexes)
const end = Math.min(Math.abs(max + 1), tokens.length)
const debugTokens = tokens.slice(start, end)
const debugIndexes = indexes.map((i) => i - start)
const debugStrings: [string, string][] = debugTokens.map((token, index) => {
if (debugIndexes.includes(index)) {
return [
`${token.value.replaceAll('\n', ' ')}`,
'^'.padEnd(token.value.length, '_'),
]
}
return [
token.value.replaceAll('\n', ' '),
' '.padEnd(token.value.length, ' '),
]
})
let topString = ''
let bottomString = ''
debugStrings.forEach(([top, bottom]) => {
topString += top
bottomString += bottom
})
const result = [`${msg} - debuggerr: ${sortedIndexes}`, topString, bottomString].join(
'\n'
)
console.log(result)
return result
}

View File

@ -87,6 +87,17 @@ show(mySketch)
}, },
]) ])
}) })
it('pipe binary expression into call expression', () => {
const code = [
'fn myFn = (a) => { return a + 1 }',
'const myVar = 5 + 1 |> myFn(%)',
].join('\n')
const { root } = exe(code)
expect(root.myVar).toBe(7)
})
}) })
// helpers // helpers

View File

@ -1,4 +1,4 @@
import { Program, BinaryPart, BinaryExpression } from './abstractSyntaxTree' import { Program, BinaryPart, BinaryExpression, PipeExpression } from './abstractSyntaxTree'
import { Path, Transform, sketchFns } from './sketch' import { Path, Transform, sketchFns } from './sketch'
import { BufferGeometry } from 'three' import { BufferGeometry } from 'three'
@ -25,7 +25,12 @@ export const executor = (
if (statement.type === 'VariableDeclaration') { if (statement.type === 'VariableDeclaration') {
statement.declarations.forEach((declaration) => { statement.declarations.forEach((declaration) => {
const variableName = declaration.id.name const variableName = declaration.id.name
if (declaration.init.type === 'Literal') { if (declaration.init.type === 'PipeExpression') {
_programMemory.root[variableName] = getPipeExpressionResult(
declaration.init,
_programMemory
)
} else if (declaration.init.type === 'Literal') {
_programMemory.root[variableName] = declaration.init.value _programMemory.root[variableName] = declaration.init.value
} else if (declaration.init.type === 'BinaryExpression') { } else if (declaration.init.type === 'BinaryExpression') {
_programMemory.root[variableName] = getBinaryExpressionResult( _programMemory.root[variableName] = getBinaryExpressionResult(
@ -184,6 +189,77 @@ function getBinaryExpressionResult(
return left + right return left + right
} }
function getPipeExpressionResult(
expression: PipeExpression,
programMemory: ProgramMemory
) {
const executedBody = executePipeBody(expression.body, programMemory)
const result = executedBody[executedBody.length - 1]
return result
}
function executePipeBody(body: PipeExpression['body'], programMemory: ProgramMemory, expressionIndex = 0, previousResults: any[] = []): any[] {
if (expressionIndex === body.length) {
return previousResults
}
const expression = body[expressionIndex]
if (expression.type === 'BinaryExpression') {
const result = getBinaryExpressionResult(expression, programMemory)
return executePipeBody(body, programMemory, expressionIndex + 1, [...previousResults, result])
} else if (expression.type === 'CallExpression') {
const fnName = expression.callee.name
const fnArgs = expression.arguments.map((arg) => {
if (arg.type === 'Literal') {
return arg.value
} else if (arg.type === 'Identifier') {
return programMemory.root[arg.name]
} else if (arg.type === 'PipeSubstitution') {
return previousResults[expressionIndex-1]
}
console.log('yo',arg)
throw new Error('Invalid argument type')
})
if (fnName === 'rx') {
console.log('rx', fnArgs[1])
const result = sketchFns[fnName](
programMemory,
[expression.start, expression.end],
fnArgs[0],
fnArgs[1]
)
return executePipeBody(body, programMemory, expressionIndex + 1, [...previousResults, result])
}
const result = programMemory.root[fnName](...fnArgs)
return executePipeBody(body, programMemory, expressionIndex + 1, [...previousResults, result])
} else if (expression.type === 'SketchExpression') {
const sketchBody = expression.body
const fnMemory: ProgramMemory = {
root: {
...programMemory.root,
},
_sketch: [],
}
let { _sketch } = executor(sketchBody, fnMemory, {
bodyType: 'sketch',
})
if (_sketch.length === 0) {
const { programMemory: newProgramMemory } = sketchFns.base(
fnMemory,
'',
[0, 0],
0,
0
)
_sketch = newProgramMemory._sketch
}
// _programMemory.root[variableName] = _sketch
return executePipeBody(body, programMemory, expressionIndex + 1, [...previousResults, _sketch])
}
throw new Error('Invalid pipe expression')
}
type SourceRange = [number, number] type SourceRange = [number, number]
export type ViewerArtifact = export type ViewerArtifact =