diff --git a/src/lang/executor.test.ts b/src/lang/executor.test.ts index 3d00c4403..892f724f6 100644 --- a/src/lang/executor.test.ts +++ b/src/lang/executor.test.ts @@ -59,8 +59,9 @@ log(5, myVar)`; path rightPath = lineTo(1,0) close() } +show(mySketch) `; - const { root } = exe(code); + const { root, return: _return } = exe(code); expect(root.mySketch.map(({ previousPath, ...rest }: any) => rest)).toEqual( [ { type: "base", from: [0, 0] }, @@ -71,6 +72,15 @@ log(5, myVar)`; ] ); expect(root.mySketch[0]).toEqual(root.mySketch[4].firstPath); + // hmm not sure what handle the "show" function + expect(_return).toEqual([ + { + type: "Identifier", + start: 108, + end: 116, + name: "mySketch", + }, + ]); }); }); diff --git a/src/lang/executor.ts b/src/lang/executor.ts index 657ed7c75..6a1351839 100644 --- a/src/lang/executor.ts +++ b/src/lang/executor.ts @@ -10,7 +10,7 @@ export interface ProgramMemory { export const executor = ( node: Program, programMemory: ProgramMemory = { root: {}, _sketch: [] }, - options: { bodyType: "default" | "sketch" } = { bodyType: "default" } + options: { bodyType: "root" | "sketch" | "block" } = { bodyType: "root" } ): any => { const _programMemory: ProgramMemory = { root: { @@ -65,7 +65,7 @@ export const executor = ( fnInit.params.forEach((param, index) => { fnMemory.root[param.name] = args[index]; }); - return executor(fnInit.body, fnMemory).return; + return executor(fnInit.body, fnMemory, {bodyType: 'block'}).return; }; } else if (declaration.init.type === "CallExpression") { const fnName = declaration.init.callee.name; @@ -115,6 +115,14 @@ export const executor = ( } const result = sketchFns[functionName](_programMemory, "", ...args); _programMemory._sketch = [...result.programMemory._sketch]; + } else if("show" === functionName) { + if (options.bodyType !== "root") { + throw new Error( + `Cannot call ${functionName} outside of a root` + ); + } + _programMemory.return = expression.arguments; + } else { _programMemory.root[functionName](...args); }