add deparser
This commit is contained in:
parent
1599a9b65d
commit
d172d38e5e
5 changed files with 55 additions and 27 deletions
28
js/index.mjs
Normal file
28
js/index.mjs
Normal 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() {
|
||||
|
||||
}
|
21
js/objs.mjs
Normal file
21
js/objs.mjs
Normal file
|
@ -0,0 +1,21 @@
|
|||
let on = async function (input, objects) {
|
||||
objects.events.addEventListener(input[0],input[1]);
|
||||
}
|
||||
|
||||
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 {
|
||||
on,
|
||||
events,
|
||||
echo,
|
||||
add
|
||||
};
|
86
js/parser.mjs
Normal file
86
js/parser.mjs
Normal file
|
@ -0,0 +1,86 @@
|
|||
//import objs from './objs.mjs';
|
||||
|
||||
let openers = {
|
||||
'{': '}',
|
||||
'[': ']',
|
||||
'(': ')'
|
||||
};
|
||||
|
||||
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;\s]/gms);
|
||||
|
||||
if (context == '[') {
|
||||
nextI = str.search(/[\]]/);
|
||||
}
|
||||
if (nextI == -1) nextI = 0;
|
||||
|
||||
let nextChar = str[nextI];
|
||||
|
||||
let sliced = str.slice(0, nextI);
|
||||
let rest = str.slice(nextI + 1);
|
||||
|
||||
if (nextChar == openers[context] || !(openers[context])) {
|
||||
return { context, str: sliced, next: rest, end: nextChar == openers[context] || sliced.indexOf(';') != -1};
|
||||
} else {
|
||||
let args = [];
|
||||
let nextStr = rest;
|
||||
|
||||
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: sliced, args, next: nextStr, op: nextChar };
|
||||
|
||||
return o;
|
||||
}
|
||||
}
|
||||
|
||||
async function runner(script, objects) {
|
||||
let { op, context } = script;
|
||||
let obj = objects[context.trim()];
|
||||
|
||||
let h = false;
|
||||
|
||||
if (op == '{') {
|
||||
h = (async function () {
|
||||
let output = false;
|
||||
for (let a of script.args) {
|
||||
output = await runner(a, objects);
|
||||
}
|
||||
return output;
|
||||
})
|
||||
} 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 (op == '[') {
|
||||
h = script.args[0].str;
|
||||
}
|
||||
|
||||
if (typeof obj != 'function') {
|
||||
return h;
|
||||
}
|
||||
|
||||
return await obj(h, objects);
|
||||
}
|
||||
|
||||
export {
|
||||
parser,
|
||||
runner,
|
||||
openers
|
||||
};
|
19
js/unparser.mjs
Normal file
19
js/unparser.mjs
Normal 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
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue