some stuff

This commit is contained in:
biglyderv 2024-11-28 23:25:40 -05:00
parent 6f2d687ba7
commit 8c3c73b848
4 changed files with 134 additions and 58 deletions

View file

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

View file

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

100
index.mjs Normal file
View file

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

25
objs.mjs Normal file
View file

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