From c8090b4137b2fd0bb9c76099a9d944efccd3597f Mon Sep 17 00:00:00 2001 From: Raphael Forment Date: Sat, 19 Aug 2023 23:38:31 +0200 Subject: [PATCH] weird string replacement experiments --- src/API.ts | 53 ++++++++++++++++++++++++++---------------------- src/Evaluator.ts | 18 ++++++++++++---- 2 files changed, 43 insertions(+), 28 deletions(-) diff --git a/src/API.ts b/src/API.ts index 1f3a9df..b628bea 100644 --- a/src/API.ts +++ b/src/API.ts @@ -55,7 +55,7 @@ export class UserAPI { */ private variables: { [key: string]: any } = {}; - private iterators: { [key: string]: any } = {}; + private counters: { [key: string]: any } = {}; private _drunk: DrunkWalk = new DrunkWalk(-100, 100, false); MidiConnection: MidiConnection = new MidiConnection(); @@ -298,56 +298,60 @@ export class UserAPI { }; // ============================================================= - // Iterator related functions + // Counter related functions // ============================================================= - public iterator = (name: string, limit?: number, step?: number): number => { + public counter = ( + name: string | number, + limit?: number, + step?: number + ): number => { /** - * Returns the current value of an iterator, and increments it by the step value. + * Returns the current value of a counter, and increments it by the step value. * - * @param name - The name of the iterator - * @param limit - The upper limit of the iterator - * @param step - The step value of the iterator - * @returns The current value of the iterator + * @param name - The name of the counter + * @param limit - The upper limit of the counter + * @param step - The step value of the counter + * @returns The current value of the counter */ - if (!(name in this.iterators)) { - // Create new iterator with default step of 1 - this.iterators[name] = { + if (!(name in this.counters)) { + // Create new counter with default step of 1 + this.counters[name] = { value: 0, step: step ?? 1, limit, }; } else { // Check if limit has changed - if (this.iterators[name].limit !== limit) { + if (this.counters[name].limit !== limit) { // Reset value to 0 and update limit - this.iterators[name].value = 0; - this.iterators[name].limit = limit; + this.counters[name].value = 0; + this.counters[name].limit = limit; } // Check if step has changed - if (this.iterators[name].step !== step) { + if (this.counters[name].step !== step) { // Update step - this.iterators[name].step = step ?? this.iterators[name].step; + this.counters[name].step = step ?? this.counters[name].step; } // Increment existing iterator by step value - this.iterators[name].value += this.iterators[name].step; + this.counters[name].value += this.counters[name].step; // Check for limit overshoot if ( - this.iterators[name].limit !== undefined && - this.iterators[name].value > this.iterators[name].limit + this.counters[name].limit !== undefined && + this.counters[name].value > this.counters[name].limit ) { - this.iterators[name].value = 0; + this.counters[name].value = 0; } } // Return current iterator value - return this.iterators[name].value; + return this.counters[name].value; }; - $ = this.iterator; + $ = this.counter; // ============================================================= // Drunk mechanism @@ -910,7 +914,7 @@ export class UserAPI { // Rythmic generators // ============================================================= - euclid = ( + public euclid = ( iterator: number, pulses: number, length: number, @@ -927,6 +931,7 @@ export class UserAPI { */ return this._euclidean_cycle(pulses, length, rotate)[iterator % length]; }; + ec = this.euclid; _euclidean_cycle( pulses: number, @@ -1119,7 +1124,7 @@ export class UserAPI { sound = (sound: string) => { return new Sound(sound, this.app); }; - + snd = this.sound; samples = samples; log = console.log; diff --git a/src/Evaluator.ts b/src/Evaluator.ts index ff21cf1..e9d832e 100644 --- a/src/Evaluator.ts +++ b/src/Evaluator.ts @@ -6,6 +6,10 @@ const delay = (ms: number) => setTimeout(() => reject(new Error("Operation took too long")), ms) ); +const codeReplace = (code: string): string => { + let new_code = code.replace(/->/g, "&&").replace(/::/g, "&&"); + return new_code; +}; const tryCatchWrapper = ( application: Editor, @@ -13,7 +17,9 @@ const tryCatchWrapper = ( ): Promise => { return new Promise((resolve, _) => { try { - Function(`"use strict";try{${code}} catch (e) {console.log(e)};`).call(application.api); + Function( + `"use strict";try{${codeReplace(code)}} catch (e) {console.log(e)};` + ).call(application.api); resolve(true); } catch (error) { console.log(error); @@ -31,7 +37,7 @@ const addFunctionToCache = (code: string, fn: Function) => { cache.delete(cache.keys().next().value); } cache.set(code, fn); -} +}; export const tryEvaluate = async ( application: Editor, @@ -41,7 +47,7 @@ export const tryEvaluate = async ( try { code.evaluations!++; const candidateCode = code.candidate; - + if (cache.has(candidateCode)) { // If the code is already in cache, use it cache.get(candidateCode)!.call(application.api); @@ -54,7 +60,11 @@ export const tryEvaluate = async ( ]); if (isCodeValid) { code.committed = code.candidate; - const newFunction = new Function(`"use strict";try{${wrappedCode}} catch (e) {console.log(e)};`); + const newFunction = new Function( + `"use strict";try{${codeReplace( + wrappedCode + )}} catch (e) {console.log(e)};` + ); addFunctionToCache(candidateCode, newFunction); } else { await evaluate(application, code, timeout);