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

View file

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