-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcIRcompiler.ts
49 lines (45 loc) · 1.45 KB
/
cIRcompiler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import * as E from 'fp-ts/Either';
import { pipe } from 'fp-ts/function';
import { CCompilerConfig, CCompilerArch } from '../constants/config';
import { CCompilerTimer, createCCompilerTimings } from './utils/createCCompilerTimings';
import { safePreprocess } from './preprocessor';
import { safeGenerateTree, clexer } from './parser';
import { safeBuildIRCode } from './ir';
import { safeBuildTypedTree, type ScopeTreeBuilderResult } from './analyze';
type IRCompilerConfig = CCompilerConfig & {
timings?: CCompilerTimer;
};
export const cIRCompiler =
(
{ timings = createCCompilerTimings(), ...ccompilerConfig }: IRCompilerConfig = {
arch: CCompilerArch.X86_16,
optimization: {
enabled: true,
},
},
) =>
(code: string) =>
pipe(
code,
timings.chainIO('lexer', clexer(ccompilerConfig.lexer)),
E.chain(
timings.chainIO('preprocessor', safePreprocess(ccompilerConfig.preprocessor)),
),
E.chainW(timings.chainIO('ast', safeGenerateTree)),
E.chainW(timings.chainIO('analyze', safeBuildTypedTree(ccompilerConfig))),
E.chainW(
timings.chainIO(
'analyze',
({ scope, ...analyzeResult }: ScopeTreeBuilderResult) =>
pipe(
scope,
safeBuildIRCode(ccompilerConfig),
E.map(ir => ({
...analyzeResult,
scope,
ir,
})),
),
),
),
);