haku: a very shitty tree-walk interpreter

This commit is contained in:
りき萌 2024-07-24 23:43:05 +02:00
parent d813675d47
commit b505c1bcfe
8 changed files with 403 additions and 26 deletions

View file

@ -14,9 +14,32 @@ export const domConsole = {
},
outputIndex,
});
}
},
};
let kernel = {
init() {
return {};
},
async evalModule(_state, source, language, _params) {
if (language == "javascript") {
let blobUrl = URL.createObjectURL(new Blob([source], { type: "text/javascript" }));
let module = await import(blobUrl);
for (let exportedKey in module) {
globalThis[exportedKey] = module[exportedKey];
}
return true;
} else {
return false;
}
},
};
export function getKernel() {
return kernel;
}
let evaluationComplete = null;
export async function evaluate(commands, { error, newOutput }) {
@ -27,17 +50,20 @@ export async function evaluate(commands, { error, newOutput }) {
let signalEvaluationComplete;
evaluationComplete = new Promise((resolve, _reject) => {
signalEvaluationComplete = resolve;
})
});
let kernelState = kernel.init();
outputIndex = 0;
try {
for (let command of commands) {
if (command.kind == "module") {
let blobUrl = URL.createObjectURL(new Blob([command.source], { type: "text/javascript" }));
let module = await import(blobUrl);
for (let exportedKey in module) {
globalThis[exportedKey] = module[exportedKey];
}
await kernel.evalModule(
kernelState,
command.source,
command.language,
command.kernelParameters,
);
} else if (command.kind == "output") {
if (newOutput != null) {
newOutput(outputIndex);
@ -63,4 +89,3 @@ export async function evaluate(commands, { error, newOutput }) {
}
signalEvaluationComplete();
}

View file

@ -42,8 +42,16 @@ function tokenize(text, syntax) {
for (let i = 1; i < match.indices.length; ++i) {
let [start, end] = match.indices[i];
if (match.indices[i] != null) {
pushToken(tokens, pattern.is.default, text.substring(lastMatchEnd, start));
pushToken(tokens, pattern.is.captures[i], text.substring(start, end));
pushToken(
tokens,
pattern.is.default,
text.substring(lastMatchEnd, start),
);
pushToken(
tokens,
pattern.is.captures[i - 1],
text.substring(start, end),
);
}
}
} else {