Add automatic syntax checker, fix editor bugs

This commit is contained in:
Nilay Majorwar
2022-01-22 21:22:38 +05:30
parent dbccab5244
commit 94dce5bfa9
12 changed files with 163 additions and 15 deletions
+16 -3
View File
@@ -77,9 +77,6 @@ export const useExecController = <RS>(langName: string) => {
(async () => {
if (workerRef.current) throw new Error("Tried to reinitialize worker");
workerRef.current = new Worker(`../workers/${langName}.js`);
workerRef.current!.addEventListener("error", (ev) =>
console.log("Ev: ", ev)
);
const res = await requestWorker({ type: "Init" });
if (res.type === "ack" && res.data === "init") setWorkerState("empty");
else throwUnexpectedRes("init", res);
@@ -137,6 +134,21 @@ export const useExecController = <RS>(langName: string) => {
else throwUnexpectedRes("resetState", res);
}, []);
/**
* Validate the syntax of the user's program code
* @param code Code content of the user's program
*/
const validateCode = React.useCallback(
async (code: string): Promise<WorkerParseError | undefined> => {
const res = await requestWorker(
{ type: "ValidateCode", params: { code } },
(res) => res.type !== "validate"
);
return res.error;
},
[]
);
/**
* Pause program execution
*/
@@ -206,6 +218,7 @@ export const useExecController = <RS>(langName: string) => {
resetState,
prepare,
pauseExecution,
validateCode,
execute,
executeStep,
updateBreakpoints,