From d172d38e5ea831abef76551d2aff3dbf127db8ec Mon Sep 17 00:00:00 2001 From: biglyderv Date: Fri, 29 Nov 2024 20:09:54 -0500 Subject: [PATCH] add deparser --- index.html | 2 +- js/index.mjs | 28 ++++++++++++++++++++++++++++ objs.mjs => js/objs.mjs | 2 +- index.mjs => js/parser.mjs | 31 ++++++------------------------- js/unparser.mjs | 19 +++++++++++++++++++ 5 files changed, 55 insertions(+), 27 deletions(-) create mode 100644 js/index.mjs rename objs.mjs => js/objs.mjs (94%) rename index.mjs => js/parser.mjs (81%) create mode 100644 js/unparser.mjs diff --git a/index.html b/index.html index 739dc56..689e2bb 100644 --- a/index.html +++ b/index.html @@ -1,7 +1,7 @@ - + diff --git a/js/index.mjs b/js/index.mjs new file mode 100644 index 0000000..8e98135 --- /dev/null +++ b/js/index.mjs @@ -0,0 +1,28 @@ +import { unparser } from "./unparser.mjs"; +import { parser, runner } from "./parser.mjs"; + +let data = parser(` + on( + [start] + { + echo(add([2][3])) + echo([Hello World!]) + echo(add([2][3])) + echo([Hello World!]) + } + ) +`); + +console.log(unparser(data)); +console.log(unparser(parser(unparser(data)))); + +(async function () { + await runner(data, objs); + let ev = new Event('start'); + + objs.events.dispatchEvent(ev); +})() + +function createBlock() { + +} \ No newline at end of file diff --git a/objs.mjs b/js/objs.mjs similarity index 94% rename from objs.mjs rename to js/objs.mjs index f1f8669..ada1c55 100644 --- a/objs.mjs +++ b/js/objs.mjs @@ -13,7 +13,7 @@ let add = async function (input, objects) { let events = new EventTarget(); -export default { +export { on, events, echo, diff --git a/index.mjs b/js/parser.mjs similarity index 81% rename from index.mjs rename to js/parser.mjs index 2380cfe..6e2ad7a 100644 --- a/index.mjs +++ b/js/parser.mjs @@ -1,4 +1,4 @@ -import objs from './objs.mjs'; +//import objs from './objs.mjs'; let openers = { '{': '}', @@ -6,10 +6,6 @@ let openers = { '(': ')' }; -let specialOpeners = { - //':': 'wildcard' -}; - function parser(str, context = '(') { str = str.replaceAll(/^\s+([^\s]+)/gms, '$1') str = str.replaceAll(/([^\s]+)\s+$/gms, '$1') @@ -83,23 +79,8 @@ async function runner(script, objects) { return await obj(h, objects); } -let data = parser(` - on( - [start] - { - echo(add([2][3])) - echo([Hello World!]) - 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); -})() +export { + parser, + runner, + openers +}; diff --git a/js/unparser.mjs b/js/unparser.mjs new file mode 100644 index 0000000..623caa9 --- /dev/null +++ b/js/unparser.mjs @@ -0,0 +1,19 @@ +import { openers } from "./parser.mjs"; + +function unparser(data) { + if (data.str) return data.str; + let output = data.args.map(x => unparser(x, false)).join('\n'); + + let ctx = data.context; + + if (data.op != '[') output = output.replaceAll(/^/gm,' '); + + let context = (ctx) ? `${ctx.trim()} ` : ''; + + if (data.op == '[') return `${context}${data.op}${output}${openers[data.op]}`; + return `${context}${data.op}\n${output}\n${openers[data.op]}`; +} + +export { + unparser +}; \ No newline at end of file