derw-lang/js/index.mjs

79 lines
1.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() {
let objs = Object.assign({},objsI);
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-29 20:09:54 -05:00
2024-11-29 21:04:43 -05:00
async function createBlocks(dat) {
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;
document.body.appendChild(div);
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) {
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();
}
2024-11-29 21:04:43 -05:00
return div;
}
2024-11-29 21:30:51 -05:00
runWrap();
createBlocks(data);