This commit is contained in:
Kurt Hutten IrevDev
2022-11-27 14:07:03 +11:00
parent 6f24e75f92
commit 1831aad321
5 changed files with 34 additions and 31 deletions

View File

@ -108,7 +108,11 @@ export const executor = (
return _programMemory.root[arg.name] return _programMemory.root[arg.name]
} }
}) })
if ('lineTo' === functionName || 'close' === functionName || 'base' === 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

@ -50,14 +50,13 @@ log(5, myVar)`
expect(recasted).toBe(code.trim()) expect(recasted).toBe(code.trim())
}) })
it('function declaration with call', () => { it('function declaration with call', () => {
const code = const code = [
[ 'fn funcN = (a, b) => {',
'fn funcN = (a, b) => {', ' return a + b',
' return a + b', '}',
'}', 'const theVar = 60',
'const theVar = 60', 'const magicNum = funcN(9, theVar)',
'const magicNum = funcN(9, theVar)', ].join('\n')
].join('\n')
const { ast } = code2ast(code) const { ast } = code2ast(code)
const recasted = recast(ast) const recasted = recast(ast)
expect(recasted).toBe(code.trim()) expect(recasted).toBe(code.trim())

View File

@ -113,6 +113,6 @@ function recastSketchExpression(
indentation: string indentation: string
): string { ): string {
return `{ return `{
${recast(expression.body, '', indentation + ' ')} ${recast(expression.body, '', indentation + ' ')}
}` }`
} }

View File

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

View File

@ -21,7 +21,7 @@ describe('testing helpers', () => {
expect(isNumber('-5')).toBe(true) expect(isNumber('-5')).toBe(true)
expect(isNumber('5.5')).toBe(true) expect(isNumber('5.5')).toBe(true)
expect(isNumber('-5.5')).toBe(true) expect(isNumber('-5.5')).toBe(true)
expect(isNumber('a')).toBe(false) expect(isNumber('a')).toBe(false)
expect(isNumber('?')).toBe(false) expect(isNumber('?')).toBe(false)
expect(isNumber('?5')).toBe(false) expect(isNumber('?5')).toBe(false)
@ -299,7 +299,6 @@ describe('testing lexer', () => {
"whitespace ' ' from 6 to 7", "whitespace ' ' from 6 to 7",
"number '2.5' from 7 to 10", "number '2.5' from 7 to 10",
]) ])
}) })
}) })