diff --git a/src/Evaluator.ts b/src/Evaluator.ts index d43dfbc..6896d28 100644 --- a/src/Evaluator.ts +++ b/src/Evaluator.ts @@ -1,6 +1,5 @@ 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)); @@ -16,7 +15,7 @@ const tryCatchWrapper = (application: Editor, code: string): Promise => */ return new Promise((resolve, _) => { try { - Function(`with (this) {try{${parseUserCode(code)}} catch (e) {console.log(e)}};`).call(application.api); + Function(`with (this) {try{${code}} catch (e) {console.log(e)}};`).call(application.api); resolve(true); } catch (error) { console.log(error); diff --git a/src/Walker.ts b/src/Walker.ts deleted file mode 100644 index 848ce3f..0000000 --- a/src/Walker.ts +++ /dev/null @@ -1,75 +0,0 @@ -import * as acorn from 'acorn'; -// import * as walk from 'acorn-walk'; -import * as astring from 'astring'; - -// Create a custom Acorn plugin -function myPlugin(Parser: typeof acorn.Parser): any { - return class extends Parser { - // @ts-ignore - parseLiteral(...args: Parameters) { - // @ts-ignore - const node = super.parseLiteral(...args); - - // Check if the literal is a string and if it's wrapped in single quotes - if (typeof node.value === 'string' && this.input.slice(node.start, node.end).startsWith("'")) { - const transformed = this.transformMyString(node.value); - - // Replace the Literal node with an ArrayExpression node - return { - type: 'ArrayExpression', - elements: transformed.map(value => ({ - type: 'Literal', - value, - raw: value.toString() - })), - start: node.start, - end: node.end - }; - } - - return node; - } - - transformMyString(string: string): number[] { - const matches = string.match(/\d+!*\d*/g); - const values: number[] = []; - - matches?.forEach(match => { - const parts = match.split('!'); - const number = parseInt(parts[0]); - const times = parts[1] ? parseInt(parts[1]) : 1; - - for (let i = 0; i < times; i++) { - values.push(number); - } - }); - - 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; - } - }; -} - -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]];`; - -// Parse the code -// const ast = MiniLanguage.parse(code, { ecmaVersion: 2020 }); - -// Convert the transformed AST back into source code -// const newCode = astring.generate(ast); - -// console.log(newCode); \ No newline at end of file