add deparser

This commit is contained in:
biglyderv 2024-11-29 20:09:54 -05:00
parent 1599a9b65d
commit d172d38e5e
5 changed files with 55 additions and 27 deletions

View file

@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<script src='/index.mjs' type='module'></script>
<script src='/js/index.mjs' type='module'></script>
</head>
<body>

28
js/index.mjs Normal file
View file

@ -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() {
}

View file

@ -13,7 +13,7 @@ let add = async function (input, objects) {
let events = new EventTarget();
export default {
export {
on,
events,
echo,

View file

@ -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
};

19
js/unparser.mjs Normal file
View file

@ -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
};