add base sketch function to language

This commit is contained in:
Kurt Hutten IrevDev
2022-11-26 21:03:38 +11:00
parent 83b8333694
commit 022788b2a3
3 changed files with 29 additions and 4 deletions

View File

@ -65,13 +65,13 @@ show(mySketch)
expect( expect(
root.mySketch.map(({ previousPath, geo, ...rest }: any) => rest) root.mySketch.map(({ previousPath, geo, ...rest }: any) => rest)
).toEqual([ ).toEqual([
{ type: 'base', from: [0, 0] }, { type: 'base', from: [0, 0], sourceRange: [0, 0] },
{ type: 'toPoint', to: [0, 1], sourceRange: [25, 45], name: 'myPath' }, { type: 'toPoint', to: [0, 1], sourceRange: [25, 45], name: 'myPath' },
{ type: 'toPoint', to: [1, 1], sourceRange: [48, 59] }, { type: 'toPoint', to: [1, 1], sourceRange: [48, 59] },
{ type: 'toPoint', to: [1, 0], sourceRange: [67, 90], name: 'rightPath' }, { type: 'toPoint', to: [1, 0], sourceRange: [67, 90], name: 'rightPath' },
{ {
type: 'close', type: 'close',
firstPath: { type: 'base', from: [0, 0] }, firstPath: { type: 'base', from: [0, 0], sourceRange: [0, 0] },
sourceRange: [93, 100], sourceRange: [93, 100],
}, },
]) ])

View File

@ -76,7 +76,7 @@ export const executor = (
return _programMemory.root[arg.name] return _programMemory.root[arg.name]
} }
}) })
if ('lineTo' === fnName || 'close' === fnName) { if ('lineTo' === fnName || 'close' === fnName || 'base' === fnName) {
if (options.bodyType !== 'sketch') { if (options.bodyType !== 'sketch') {
throw new Error( throw new Error(
`Cannot call ${fnName} outside of a sketch declaration` `Cannot call ${fnName} outside of a sketch declaration`
@ -108,7 +108,7 @@ export const executor = (
return _programMemory.root[arg.name] return _programMemory.root[arg.name]
} }
}) })
if ('lineTo' === functionName || 'close' === functionName) { if ('lineTo' === functionName || 'close' === functionName || 'base' === functionName) {
if (options.bodyType !== 'sketch') { if (options.bodyType !== 'sketch') {
throw new Error( throw new Error(
`Cannot call ${functionName} outside of a sketch declaration` `Cannot call ${functionName} outside of a sketch declaration`

View File

@ -49,12 +49,14 @@ export type Path =
| { | {
type: 'base' type: 'base'
from: Coords2d from: Coords2d
sourceRange: SourceRange
} }
function addBasePath(programMemory: ProgramMemory) { function addBasePath(programMemory: ProgramMemory) {
const base: Path = { const base: Path = {
type: 'base', type: 'base',
from: [0, 0], from: [0, 0],
sourceRange: [0, 0],
} }
if (programMemory._sketch?.length === 0) { if (programMemory._sketch?.length === 0) {
return { return {
@ -90,6 +92,29 @@ function getCoordsFromPaths(paths: Path[], index = 0): Coords2d {
} }
export const sketchFns = { export const sketchFns = {
base: (programMemory: ProgramMemory,
name: string = '',
sourceRange: SourceRange,
...args: any[]
): PathReturn => {
if(programMemory._sketch?.length > 0) {
throw new Error('Base can only be called once')
}
const [x, y] = args as [number, number]
let from: [number, number] = [x, y]
const newPath: Path = {
type: 'base',
from,
sourceRange,
}
return {
programMemory: {
...programMemory,
_sketch: [...(programMemory?._sketch || []), newPath],
},
currentPath: newPath,
}
},
close: ( close: (
programMemory: ProgramMemory, programMemory: ProgramMemory,
name: string = '', name: string = '',