derw-lang/js/index.mjs

113 lines
2.9 KiB
JavaScript
Raw Normal View History

2024-11-29 20:09:54 -05:00
import { unparser } from "./unparser.mjs";
2024-11-29 21:04:43 -05:00
import { parser, runner, openers } from "./parser.mjs";
2024-11-29 21:30:51 -05:00
import objsI from "./objs.mjs";
2024-11-29 20:09:54 -05:00
let data = parser(`
on(
2024-11-29 21:30:51 -05:00
[compile]
2024-11-29 20:09:54 -05:00
{
echo(add([2][3]))
echo([Hello World!])
echo(add([2][3]))
echo([Hello World!])
}
)
`);
2024-11-29 21:30:51 -05:00
async function runWrap() {
2024-11-30 01:21:03 -05:00
let objs = Object.assign({}, objsI);
2024-11-29 21:30:51 -05:00
objs.events = new EventTarget();
2024-11-29 20:09:54 -05:00
await runner(data, objs);
2024-11-29 21:30:51 -05:00
let ev = new Event('compile'); // TODO: more events
2024-11-29 21:04:43 -05:00
2024-11-29 21:30:51 -05:00
objs.events.dispatchEvent(ev);
}
let clickedBlock = false;
let clickedArea = false;
2024-11-30 01:21:03 -05:00
let bodyThing = document.body;
2024-11-29 20:09:54 -05:00
2024-11-30 01:21:03 -05:00
async function createBlocks(dat, parent = bodyThing) {
2024-11-29 21:30:51 -05:00
let args = Object.assign([], dat.args);
2024-11-29 21:04:43 -05:00
if (!args || !args[0]) args = [dat.args];
if (!args || !args[0]) args = [];
for (let i in args) {
args[i] = await createBlocks(args[i])
}
let o = dat.op ? dat.op.charCodeAt() : 'nil';
let div = document.createElement('div');
div.classList.add(`op-${o}`)
div.classList.add(`block-${dat.context}`)
div.classList.add(`block`)
div.textContent = dat.str || dat.context;
2024-11-30 01:21:03 -05:00
parent.appendChild(div);
if (dat.op == '{') {
//todo: clean
let button = document.createElement('div');
button.classList.add(`block`)
button.classList.add(`block-push`)
button.textContent = 'push a block';
div.appendChild(button)
button.onclick = function() {
dat.args.push(parser(`comment([placeholder])`));
bodyThing.innerHTML = '';
createBlocks(data);
}
button = document.createElement('div');
button.classList.add(`block`)
button.classList.add(`block-pop`)
button.textContent = 'pop a block';
div.appendChild(button)
button.onclick = function() {
dat.args.pop();
bodyThing.innerHTML = '';
createBlocks(data);
}
}
2024-11-29 21:04:43 -05:00
for (let arg of args) {
if (arg) div.appendChild(arg);
}
2024-11-29 20:09:54 -05:00
2024-11-29 21:30:51 -05:00
div.onclick = function (e) {
if (clickedBlock) {
if (clickedArea != dat) {
2024-11-30 01:06:08 -05:00
let next1 = clickedBlock.nextSibling;
let next2 = div.nextSibling;
2024-11-29 21:30:51 -05:00
let p = clickedBlock.parentElement;
2024-11-30 01:21:03 -05:00
div.parentElement.insertBefore(clickedBlock, next2);
p.insertBefore(div, next1);
2024-11-29 21:30:51 -05:00
let swap = {};
console.log(clickedArea, dat)
Object.assign(swap, clickedArea);
Object.assign(clickedArea, dat);
Object.assign(dat, swap);
}
runWrap();
clickedBlock = false;
clickedArea = false;
} else {
clickedBlock = div;
clickedArea = dat;
}
e.stopPropagation();
}
2024-11-29 21:04:43 -05:00
return div;
}
2024-11-29 21:30:51 -05:00
runWrap();
createBlocks(data);