Skip to content

Commit

Permalink
feat: add basic internal compiler fs
Browse files Browse the repository at this point in the history
  • Loading branch information
Mati365 committed Nov 19, 2023
1 parent ddf1dd9 commit 8bf0428
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { clexer } from '../../parser/lexer/clexer';
import { evalTokens } from './evalTokens';

import { ExpressionResultTreeVisitor } from './ExpressionResultTreeVisitor';
import { CInternalCompilerFsResolver } from '../../../fs';
import { CPreprocessorError, CPreprocessorErrorCode } from '../grammar';

export type CInterpreterScope = {
Expand Down Expand Up @@ -39,14 +40,14 @@ export const createInterpreterContext = ({
defineMacro: (name: string, macro: CPreprocessorMacro) => {
scope.macros[name] = macro;
},

appendFinalTokens: finalTokens => {
reduced.push(...finalTokens);
},
evalExpression: expression => {
const visitor = new ExpressionResultTreeVisitor(ctx);

return visitor.visit(expression).value;
},
evalExpression: expression =>
new ExpressionResultTreeVisitor(ctx).visit(expression).value,

includeFile: path => {
if (!fsIncludeResolver) {
throw new CPreprocessorError(
Expand All @@ -59,7 +60,13 @@ export const createInterpreterContext = ({
pipe(
E.Do,
E.bind('resolverOutput', () =>
fsIncludeResolver.read(currentFilePath)(path),
pipe(
new CInternalCompilerFsResolver().read()(path),
E.fold(
() => fsIncludeResolver.read(currentFilePath)(path),
E.right,
),
),
),
E.bindW('tokens', ({ resolverOutput }) =>
clexer({})(resolverOutput.content),
Expand Down
47 changes: 47 additions & 0 deletions packages/compiler-pico-c/src/fs/CInternalCompilerFsResolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as E from 'fp-ts/Either';

import {
CPreprocessorError,
CPreprocessorErrorCode,
} from '../frontend/preprocessor/grammar/CPreprocessorError';

import type {
CInterpreterIncludeResolver,
CInterpreterSourceFile,
CInterpreterSourcePath,
} from '../frontend/preprocessor';

import { STD_ARG_CONTENT_HEADER } from './stdarg.h';

export class CInternalCompilerFsResolver
implements CInterpreterIncludeResolver
{
read =
() =>
(
path: CInterpreterSourcePath,
): E.Either<CPreprocessorError, CInterpreterSourceFile> => {
const vfsFile = CInternalCompilerFsResolver.FILES[path.filename];

if (!path.system || !vfsFile) {
return E.left(
new CPreprocessorError(
CPreprocessorErrorCode.CANNOT_INCLUDE_FILE,
null,
{
name: path.filename,
},
),
);
}

return E.right({
absolutePath: path.filename,
content: vfsFile,
});
};

private static FILES = {
'stdarg.h': STD_ARG_CONTENT_HEADER,
};
}
2 changes: 2 additions & 0 deletions packages/compiler-pico-c/src/fs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './CInternalCompilerFsResolver';
export * from './stdarg.h';
5 changes: 5 additions & 0 deletions packages/compiler-pico-c/src/fs/stdarg.h.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const STD_ARG_CONTENT_HEADER = /* c */ `
#define va_start(v, l) __builtin_va_start(v, l)
#define va_end(v) __builtin_va_end(v)
#define va_arg(v,l) __builtin_va_arg(v, l)
`;

0 comments on commit 8bf0428

Please sign in to comment.