fix execution of piped sketches
This commit is contained in:
@ -1243,10 +1243,10 @@ function debuggerr(tokens: Token[], indexes: number[], msg=''): string {
|
|||||||
topString += top
|
topString += top
|
||||||
bottomString += bottom
|
bottomString += bottom
|
||||||
})
|
})
|
||||||
const result = [`${msg} - debuggerr: ${sortedIndexes}`, topString, bottomString].join(
|
const debugResult = [`${msg} - debuggerr: ${sortedIndexes}`, topString, bottomString].join(
|
||||||
'\n'
|
'\n'
|
||||||
)
|
)
|
||||||
console.log(result)
|
console.log(debugResult)
|
||||||
return result
|
return debugResult
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ import fs from 'node:fs'
|
|||||||
import { abstractSyntaxTree } from './abstractSyntaxTree'
|
import { abstractSyntaxTree } from './abstractSyntaxTree'
|
||||||
import { lexer } from './tokeniser'
|
import { lexer } from './tokeniser'
|
||||||
import { executor, ProgramMemory } from './executor'
|
import { executor, ProgramMemory } from './executor'
|
||||||
|
import { Transform } from './sketch'
|
||||||
|
|
||||||
describe('test', () => {
|
describe('test', () => {
|
||||||
it('test assigning two variables, the second summing with the first', () => {
|
it('test assigning two variables, the second summing with the first', () => {
|
||||||
@ -97,7 +98,61 @@ show(mySketch)
|
|||||||
expect(root.myVar).toBe(7)
|
expect(root.myVar).toBe(7)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('rotated sketch', () => {
|
||||||
|
const code = [
|
||||||
|
'sketch mySk1 {',
|
||||||
|
' lineTo(1,1)',
|
||||||
|
' path myPath = lineTo(0, 1)',
|
||||||
|
' lineTo(1,1)',
|
||||||
|
'}',
|
||||||
|
'const rotated = rx(90, mySk1)',
|
||||||
|
// 'show(mySk1)',
|
||||||
|
].join('\n')
|
||||||
|
const { root } = exe(code)
|
||||||
|
expect(root.mySk1).toHaveLength(4)
|
||||||
|
expect(root?.rotated?.type).toBe('transform')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('execute pipe sketch into call expression', () => {
|
||||||
|
const code = [
|
||||||
|
'sketch mySk1 {',
|
||||||
|
' lineTo(1,1)',
|
||||||
|
' path myPath = lineTo(0, 1)',
|
||||||
|
' lineTo(1,1)',
|
||||||
|
'} |> rx(90, %)',
|
||||||
|
].join('\n')
|
||||||
|
const { root } = exe(code)
|
||||||
|
const striptVersion = removeGeoFromSketch(root.mySk1)
|
||||||
|
expect(striptVersion).toEqual({
|
||||||
|
type: 'transform',
|
||||||
|
rotation: [1.5707963267948966, 0, 0],
|
||||||
|
transform: [0, 0, 0],
|
||||||
|
sketch: [
|
||||||
|
{
|
||||||
|
type: 'base',
|
||||||
|
from: [0, 0],
|
||||||
|
sourceRange: [0, 0],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'toPoint',
|
||||||
|
to: [1, 1],
|
||||||
|
sourceRange: [17, 28],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'toPoint',
|
||||||
|
to: [0, 1],
|
||||||
|
sourceRange: [36, 57],
|
||||||
|
name: 'myPath',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'toPoint',
|
||||||
|
to: [1, 1],
|
||||||
|
sourceRange: [60, 71],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
sourceRange: [77, 86],
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
// helpers
|
// helpers
|
||||||
@ -110,3 +165,13 @@ function exe(
|
|||||||
const ast = abstractSyntaxTree(tokens)
|
const ast = abstractSyntaxTree(tokens)
|
||||||
return executor(ast, programMemory)
|
return executor(ast, programMemory)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function removeGeoFromSketch(sketch: Transform): any {
|
||||||
|
if (!Array.isArray(sketch.sketch)) {
|
||||||
|
return removeGeoFromSketch(sketch.sketch)
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
...sketch,
|
||||||
|
sketch: sketch.sketch.map(({ geo, previousPath, ...rest }: any) => rest),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -107,14 +107,14 @@ export const executor = (
|
|||||||
_programMemory._sketch = result.programMemory._sketch
|
_programMemory._sketch = result.programMemory._sketch
|
||||||
_programMemory.root[variableName] = result.currentPath
|
_programMemory.root[variableName] = result.currentPath
|
||||||
} else if ('rx' === fnName) {
|
} else if ('rx' === fnName) {
|
||||||
if (declaration.init.arguments[1].type !== 'Identifier')
|
const sketch = declaration.init.arguments[1]
|
||||||
throw new Error('rx must be called with an identifier')
|
if(sketch.type !== 'Identifier') throw new Error('rx must be called with an identifier')
|
||||||
const id = declaration.init.arguments[1].name
|
const sketchVal = _programMemory.root[sketch.name]
|
||||||
const result = sketchFns[fnName](
|
const result = sketchFns[fnName](
|
||||||
_programMemory,
|
_programMemory,
|
||||||
[declaration.start, declaration.end],
|
[declaration.start, declaration.end],
|
||||||
fnArgs[0],
|
fnArgs[0],
|
||||||
id
|
sketchVal
|
||||||
)
|
)
|
||||||
_programMemory.root[variableName] = result
|
_programMemory.root[variableName] = result
|
||||||
} else {
|
} else {
|
||||||
@ -216,11 +216,9 @@ function executePipeBody(body: PipeExpression['body'], programMemory: ProgramMem
|
|||||||
} else if (arg.type === 'PipeSubstitution') {
|
} else if (arg.type === 'PipeSubstitution') {
|
||||||
return previousResults[expressionIndex-1]
|
return previousResults[expressionIndex-1]
|
||||||
}
|
}
|
||||||
console.log('yo',arg)
|
|
||||||
throw new Error('Invalid argument type')
|
throw new Error('Invalid argument type')
|
||||||
})
|
})
|
||||||
if (fnName === 'rx') {
|
if (fnName === 'rx') {
|
||||||
console.log('rx', fnArgs[1])
|
|
||||||
const result = sketchFns[fnName](
|
const result = sketchFns[fnName](
|
||||||
programMemory,
|
programMemory,
|
||||||
[expression.start, expression.end],
|
[expression.start, expression.end],
|
||||||
@ -302,7 +300,7 @@ export const processShownObjects =
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
} else if (geoMeta.type === 'transform') {
|
} else if (geoMeta.type === 'transform') {
|
||||||
const referencedVar = programMemory.root[geoMeta.id]
|
const referencedVar = geoMeta.sketch
|
||||||
const parentArtifact: ViewerArtifact = {
|
const parentArtifact: ViewerArtifact = {
|
||||||
type: 'parent',
|
type: 'parent',
|
||||||
sourceRange: geoMeta.sourceRange,
|
sourceRange: geoMeta.sourceRange,
|
||||||
|
@ -59,7 +59,7 @@ export interface Transform {
|
|||||||
type: 'transform'
|
type: 'transform'
|
||||||
rotation: Rotation3
|
rotation: Rotation3
|
||||||
transform: Translate3
|
transform: Translate3
|
||||||
id: string
|
sketch: Path[] | Transform
|
||||||
sourceRange: SourceRange
|
sourceRange: SourceRange
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -207,15 +207,14 @@ export const sketchFns = {
|
|||||||
programMemory: ProgramMemory,
|
programMemory: ProgramMemory,
|
||||||
sourceRange: SourceRange,
|
sourceRange: SourceRange,
|
||||||
rotationD: number,
|
rotationD: number,
|
||||||
id: string
|
sketch: Path[] | Transform
|
||||||
): Transform => {
|
): Transform => {
|
||||||
if (!programMemory.root[id]) throw new Error(`No variable with name ${id}`)
|
|
||||||
const rotationR = rotationD * (Math.PI / 180)
|
const rotationR = rotationD * (Math.PI / 180)
|
||||||
return {
|
return {
|
||||||
type: 'transform',
|
type: 'transform',
|
||||||
rotation: [rotationR, 0, 0],
|
rotation: [rotationR, 0, 0],
|
||||||
transform: [0, 0, 0],
|
transform: [0, 0, 0],
|
||||||
id,
|
sketch,
|
||||||
sourceRange,
|
sourceRange,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user