derw-lang/js/index.mjs
2024-11-29 21:30:51 -05:00

79 lines
No EOL
1.9 KiB
JavaScript

import { unparser } from "./unparser.mjs";
import { parser, runner, openers } from "./parser.mjs";
import objsI from "./objs.mjs";
let data = parser(`
on(
[compile]
{
echo(add([2][3]))
echo([Hello World!])
echo(add([2][3]))
echo([Hello World!])
}
)
`);
async function runWrap() {
let objs = Object.assign({},objsI);
objs.events = new EventTarget();
await runner(data, objs);
let ev = new Event('compile'); // TODO: more events
objs.events.dispatchEvent(ev);
}
let clickedBlock = false;
let clickedArea = false;
async function createBlocks(dat) {
let args = Object.assign([], dat.args);
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;
document.body.appendChild(div);
for (let arg of args) {
if (arg) div.appendChild(arg);
}
div.onclick = function (e) {
if (clickedBlock) {
if (clickedArea != dat) {
let p = clickedBlock.parentElement;
div.parentElement.appendChild(clickedBlock);
p.appendChild(div);
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();
}
return div;
}
runWrap();
createBlocks(data);