From 6ad8fa114b72fee2562c672e3d81725cd467e5e7 Mon Sep 17 00:00:00 2001 From: Raphael Forment Date: Sun, 6 Aug 2023 10:09:10 +0200 Subject: [PATCH] add blank OSCConnection --- src/API.ts | 8 -------- src/Evaluator.ts | 3 ++- src/IO/OSCConnection.ts | 0 src/Walker.ts | 31 +++++++++++++++++++------------ 4 files changed, 21 insertions(+), 21 deletions(-) create mode 100644 src/IO/OSCConnection.ts diff --git a/src/API.ts b/src/API.ts index 1f4f049..bf836d8 100644 --- a/src/API.ts +++ b/src/API.ts @@ -4,8 +4,6 @@ import { tryEvaluate } from "./Evaluator"; import { MidiConnection } from "./IO/MidiConnection"; // @ts-ignore import { webaudioOutput, samples } from '@strudel.cycles/webaudio'; -import { MiniLanguage } from "./Walker"; -import * as astring from 'astring'; const sound = (value: any) => ({ @@ -946,10 +944,4 @@ export class UserAPI { sound = async (values: object) => { webaudioOutput(sound(values), 0.00) } - - ast(code: string) { - const ast = MiniLanguage.parse(code, { ecmaVersion: 2020 }); - console.log(astring.generate(ast)) - return - } } diff --git a/src/Evaluator.ts b/src/Evaluator.ts index 826f5e0..dacf6e7 100644 --- a/src/Evaluator.ts +++ b/src/Evaluator.ts @@ -1,5 +1,6 @@ import type { Editor } from './main'; import type { File } from './AppSettings'; +import { parseUserCode } from './Walker'; const delay = (ms: number) => new Promise((_, reject) => setTimeout(() => reject(new Error('Operation took too long')), ms)); @@ -15,7 +16,7 @@ const tryCatchWrapper = (application: Editor, code: string): Promise => */ return new Promise((resolve, _) => { try { - Function(`with (this) {try{${code}} catch (e) {console.log(e)}};`).call(application.api); + Function(`with (this) {try{${parseUserCode(code)}} catch (e) {console.log(e)}};`).call(application.api); resolve(true); } catch (error) { console.log(error); diff --git a/src/IO/OSCConnection.ts b/src/IO/OSCConnection.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/Walker.ts b/src/Walker.ts index 3e88019..df12dcb 100644 --- a/src/Walker.ts +++ b/src/Walker.ts @@ -14,17 +14,14 @@ function myPlugin(Parser: typeof acorn.Parser): any { // Replace the Literal node with an ArrayExpression node return { - type: 'SpreadElement', - argument: { type: 'ArrayExpression', elements: transformed.map(value => ({ - type: 'Literal', - value, - raw: value.toString() - })) - }, - start: node.start, - end: node.end + type: 'Literal', + value, + raw: value.toString() + })), + start: node.start, + end: node.end }; } @@ -45,6 +42,13 @@ function myPlugin(Parser: typeof acorn.Parser): any { } }); + matches?.forEach(match => { + const parts = match.split('~'); + const begin = parseInt(parts[0]) + const end = parseInt(parts[1]) + values.push(Math.floor(Math.random() * (end) + begin)) + }) + return values; } }; @@ -52,10 +56,13 @@ function myPlugin(Parser: typeof acorn.Parser): any { export const MiniLanguage = acorn.Parser.extend(myPlugin); +export function parseUserCode(code: string): string { + let constructor = MiniLanguage.parse(code, { ecmaVersion: 2020 }); + return astring.generate(constructor) +} + // Sample code -// const code = ` -// const a = '3!4 5'; // This should become const a = [...[3,3,3,3,5]]; -// `; +// const code = `const a = '3!4 5'; // This should become const a = [...[3,3,3,3,5]];`; // Parse the code // const ast = MiniLanguage.parse(code, { ecmaVersion: 2020 });