parser fixes

This commit is contained in:
biglyderv 2024-11-29 15:47:46 -05:00
parent 8c3c73b848
commit 1599a9b65d
2 changed files with 21 additions and 20 deletions

View file

@ -3,8 +3,7 @@ import objs from './objs.mjs';
let openers = {
'{': '}',
'[': ']',
'(': ')',
';': ';'
'(': ')'
};
let specialOpeners = {
@ -14,7 +13,8 @@ let specialOpeners = {
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);
let nextI = str.search(/[^A-Za-z0-9;\s]/gms);
if (context == '[') {
nextI = str.search(/[\]]/);
@ -23,15 +23,14 @@ function parser(str, context = '(') {
let nextChar = str[nextI];
let special = specialOpeners[context] || '';
let sliced = str.slice(0, nextI);
let rest = str.slice(nextI + 1);
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] };
if (nextChar == openers[context] || !(openers[context])) {
return { context, str: sliced, next: rest, end: nextChar == openers[context] || sliced.indexOf(';') != -1};
} else {
let args = [];
let nextStr = str.slice(nextI + 1);
let nextStr = rest;
while (nextStr && nextStr.length > 0) {
args.push(parser(nextStr, nextChar));
@ -43,7 +42,7 @@ function parser(str, context = '(') {
if (a.end) break;
}
let o = { context: str.slice(0, nextI), args, next: nextStr, op: context };
let o = { context: sliced, args, next: nextStr, op: nextChar };
return o;
}
@ -57,7 +56,11 @@ async function runner(script, objects) {
if (op == '{') {
h = (async function () {
return await runner(script.args[0], objects);
let output = false;
for (let a of script.args) {
output = await runner(a, objects);
}
return output;
})
} else if (op == '(') {
let arger = [];
@ -69,8 +72,8 @@ async function runner(script, objects) {
if (arger.length == 1) arger = arger[0];
h = arger;
} else if (script.str) {
h = script.str;
} else if (op == '[') {
h = script.args[0].str;
}
if (typeof obj != 'function') {
@ -84,8 +87,10 @@ let data = parser(`
on(
[start]
{
(echo(add([2][3])))
(echo([Hello World!]))
echo(add([2][3]))
echo([Hello World!])
echo(add([2][3]))
echo([Hello World!])
}
)
`);

View file

@ -1,9 +1,5 @@
let on = async function (input, objects) {
objects.events.addEventListener(input[0],(async function() {
for (let a of input[1]) {
await a();
}
}));
objects.events.addEventListener(input[0],input[1]);
}
let echo = async function (input, objects) {