From 8c3c73b848b6389b364e55806f2dca6980196ec1 Mon Sep 17 00:00:00 2001 From: biglyderv Date: Thu, 28 Nov 2024 23:25:40 -0500 Subject: [PATCH] some stuff --- index.html | 9 +++++ index.js | 58 ------------------------------- index.mjs | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++ objs.mjs | 25 ++++++++++++++ 4 files changed, 134 insertions(+), 58 deletions(-) delete mode 100644 index.js create mode 100644 index.mjs create mode 100644 objs.mjs diff --git a/index.html b/index.html index e69de29..739dc56 100644 --- a/index.html +++ b/index.html @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/index.js b/index.js deleted file mode 100644 index d62885c..0000000 --- a/index.js +++ /dev/null @@ -1,58 +0,0 @@ -let openers = { - '{': '}', - '[': ']', - '(': ')' -}; - -let specialOpeners = { - ':': 'wildcard' -}; - -function parser(str, context = '{') { - str = str.replaceAll(/^\s+([^\s]+)/gm,'$1') - str = str.replaceAll(/([^\s]+)\s+$/gm,'$1') - let nextI = str.search(/[^A-Za-z0-9\s]/); - - if (context == '[') { - nextI = str.search(/[\]]/); - } - if (nextI == -1) nextI = 0; - - let nextChar = str[nextI]; - - let special = specialOpeners[context] || ''; - - let sp = (special == 'wildcard'); - - if (nextChar == openers[context]) { - return { context, str: str.slice(0, nextI), next: str.slice(nextI + 1), end: true}; - } else if (openers[context] || sp) { - let args = []; - let nextStr = str.slice(nextI + 1); - - while (nextStr && nextStr.length > 0) { - args.push(parser(nextStr, nextChar)); - let a = args[args.length - 1]; - nextStr = a.next; - if (a.str == '') { - args.pop(); - } - if (a.end) break; - } - - let o = { context: str.slice(0, nextI), args, next: nextStr, op: context }; - - return o; - } else { - return { context, str: str.slice(0, nextI), next: str.slice(nextI + 1)}; - } -} - -let data = parser(` - on:start { - echo[Hello World!] - echo[Hello World!] - } -`); - -console.log(JSON.stringify(data, undefined, 1)); \ No newline at end of file diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..f04a928 --- /dev/null +++ b/index.mjs @@ -0,0 +1,100 @@ +import objs from './objs.mjs'; + +let openers = { + '{': '}', + '[': ']', + '(': ')', + ';': ';' +}; + +let specialOpeners = { + //':': 'wildcard' +}; + +function parser(str, context = '(') { + str = str.replaceAll(/^\s+([^\s]+)/gms, '$1') + str = str.replaceAll(/([^\s]+)\s+$/gms, '$1') + let nextI = str.search(/[^A-Za-z0-9 ]/gms); + + if (context == '[') { + nextI = str.search(/[\]]/); + } + if (nextI == -1) nextI = 0; + + let nextChar = str[nextI]; + + let special = specialOpeners[context] || ''; + + let sp = (special == 'wildcard'); + + if (nextChar == openers[context] || !(openers[context] || sp)) { + return { context, str: str.slice(0, nextI), next: str.slice(nextI + 1), end: nextChar == openers[context] }; + } else { + let args = []; + let nextStr = str.slice(nextI + 1); + + while (nextStr && nextStr.length > 0) { + args.push(parser(nextStr, nextChar)); + let a = args[args.length - 1]; + nextStr = a.next; + if (a.str == '') { + args.pop(); + } + if (a.end) break; + } + + let o = { context: str.slice(0, nextI), args, next: nextStr, op: context }; + + return o; + } +} + +async function runner(script, objects) { + let { op, context } = script; + let obj = objects[context.trim()]; + + let h = false; + + if (op == '{') { + h = (async function () { + return await runner(script.args[0], objects); + }) + } else if (op == '(') { + let arger = []; + + for (let arg of script.args) { + arger.push(await runner(arg, objects)); + } + + if (arger.length == 1) arger = arger[0]; + + h = arger; + } else if (script.str) { + h = script.str; + } + + if (typeof obj != 'function') { + return h; + } + + return await obj(h, objects); +} + +let data = parser(` + on( + [start] + { + (echo(add([2][3]))) + (echo([Hello World!])) + } + ) +`); + +console.log(data); + +(async function () { + await runner(data, objs); + let ev = new Event('start'); + + objs.events.dispatchEvent(ev); +})() diff --git a/objs.mjs b/objs.mjs new file mode 100644 index 0000000..4a1b500 --- /dev/null +++ b/objs.mjs @@ -0,0 +1,25 @@ +let on = async function (input, objects) { + objects.events.addEventListener(input[0],(async function() { + for (let a of input[1]) { + await a(); + } + })); +} + +let echo = async function (input, objects) { + console.log(input); + return input; +} + +let add = async function (input, objects) { + return input[0] * 1 + input[1] * 1; +} + +let events = new EventTarget(); + +export default { + on, + events, + echo, + add +}; \ No newline at end of file