101 lines
2.1 KiB
JavaScript
101 lines
2.1 KiB
JavaScript
|
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);
|
||
|
})()
|