Skip to content

Commit

Permalink
feat: proper error handle in code runner
Browse files Browse the repository at this point in the history
  • Loading branch information
Mati365 committed Feb 11, 2024
1 parent 6a4a1b3 commit eaa623b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Buffer } from 'buffer';
import { pipe } from 'fp-ts/function';
import { either as E } from 'fp-ts';
import { either as E, array as A } from 'fp-ts';
import { useState } from 'react';
import { useControlStrict } from '@under-control/forms';
import constate from 'constate';
Expand All @@ -11,9 +11,12 @@ import {
ccompiler,
getX86BootsectorPreloaderBinary,
wrapWithX86BootsectorAsm,
type CCompilerError,
} from '@ts-c-compiler/compiler';

import type { CompilerError } from '@ts-c-compiler/core';
import type {
EditorCompileResultError,
EditorCompileResultValue,
EditorEmulationValue,
EditorStateValue,
Expand Down Expand Up @@ -43,13 +46,28 @@ const useEditorStateValue = () => {
} = control;

const run = () => {
const mapAssemblerErrors = A.map(
(error: CompilerError<any>): EditorCompileResultError => ({
loc: error.loc ?? null,
message: error.message ?? 'Unknown assembler error!',
}),
);

const mapCErrors = A.map(
(error: CCompilerError): EditorCompileResultError => ({
loc: error.loc ?? null,
message: error.getCompilerMessage() ?? 'Unknown c compiler error!',
}),
);

const result = (() => {
switch (lang) {
case 'nasm':
return pipe(
code,
asm(),
E.map(
E.bimap(
mapAssemblerErrors,
({ output }): EditorCompileResultValue => ({
asmPassOutput: output,
blob: Buffer.from(output.getBinary()),
Expand All @@ -66,7 +84,9 @@ const useEditorStateValue = () => {
enabled: true,
},
}),
E.map(compilerResult => wrapWithX86BootsectorAsm(compilerResult.codegen.asm)),
E.bimap(mapCErrors, compilerResult =>
wrapWithX86BootsectorAsm(compilerResult.codegen.asm),
),
E.chainW(asmRaw =>
pipe(
asmRaw,
Expand All @@ -77,6 +97,7 @@ const useEditorStateValue = () => {
externalLinkerAddrGenerator: () => 0xff_ff,
},
}),
E.mapLeft(mapAssemblerErrors),
),
),
E.map(
Expand Down
8 changes: 7 additions & 1 deletion apps/site/src/modules/Editor/EditorStateProvider/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { either as E } from 'fp-ts';
import type { SecondPassResult } from '@ts-c-compiler/x86-assembler';
import type { TokenLocation } from '@ts-c-compiler/lexer';

export type EditorCompileLang = 'nasm' | 'c';

Expand All @@ -12,12 +13,17 @@ type AbstractEmulationState<S extends string, P = {}> = P & {
state: S;
};

export type EditorCompileResultError = {
loc: TokenLocation | null;
message: string;
};

export type EditorCompileResultValue = {
blob: Buffer;
asmPassOutput: SecondPassResult;
};

type EditorCompileResult = E.Either<unknown, EditorCompileResultValue>;
type EditorCompileResult = E.Either<EditorCompileResultError[], EditorCompileResultValue>;

export type EditorEmulationValue =
| AbstractEmulationState<'stop'>
Expand Down
17 changes: 15 additions & 2 deletions packages/compiler-pico-c/src/ccompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,23 @@ import { X86TargetCPU } from '@ts-c-compiler/x86-assembler';
import { createCCompilerTimings } from './frontend/utils/createCCompilerTimings';
import { CCompilerConfig, CCompilerArch } from './constants/config';

import { cIRCompiler } from './frontend';
import { CPreprocessorError, cIRCompiler } from './frontend';
import { genASMIRCode } from './backend';

import { CCompilerOutput } from './output/CCompilerOutput';

import type { CBackendError } from './backend/errors/CBackendError';
import type { CTypeCheckError } from './frontend/analyze';
import type { CGrammarError } from './frontend/parser/grammar/errors/CGrammarError';
import type { IRError } from './frontend/ir/errors/IRError';

export type CCompilerError =
| CBackendError
| CPreprocessorError
| CTypeCheckError
| CGrammarError
| IRError;

/**
* Main compiler entry, compiles code to binary
*
Expand All @@ -27,7 +40,7 @@ export const ccompiler =
},
},
) =>
(code: string) => {
(code: string): E.Either<CCompilerError[], CCompilerOutput> => {
const timings = createCCompilerTimings();

return pipe(
Expand Down

0 comments on commit eaa623b

Please sign in to comment.