Adding C# Support. Badly tested currently, but kindof working

This commit is contained in:
K35
2022-01-05 21:16:17 +00:00
parent 94832ef682
commit 49425cab39
20 changed files with 3825 additions and 71 deletions

View File

@ -6,18 +6,21 @@ import tokenize, { TokenizerError } from "./tokenizer";
import parse, { Parsed, ParserError } from "./parser";
import get_ir, { IR, IRError } from "./ir";
import compile, { CompileTarget } from "./compile";
import { ESMTypescriptTarget, NodeJSTypescriptTarget } from "./targets/typescript";
import {
ESMTypescriptTarget,
NodeJSTypescriptTarget,
} from "./targets/typescript";
import { CSharpTarget } from "./targets/csharp";
class CatchedError extends Error {}
const log = dbg("app");
const Targets = new Map<string, typeof CompileTarget>();
const Targets = new Map<string,typeof CompileTarget>();
Targets.set("ts-esm", ESMTypescriptTarget)
Targets.set("ts-node", NodeJSTypescriptTarget)
Targets.set("ts-esm", ESMTypescriptTarget);
Targets.set("ts-node", NodeJSTypescriptTarget);
Targets.set("c#", CSharpTarget as typeof CompileTarget);
function indexToLineAndCol(src: string, index: number) {
let line = 1;
@ -77,7 +80,11 @@ type ProcessContext = {
processedFiles: Set<string>;
};
function processFile(ctx: ProcessContext, file: string, root = false): Parsed | null {
function processFile(
ctx: ProcessContext,
file: string,
root = false
): Parsed | null {
file = Path.resolve(file);
if (ctx.processedFiles.has(file)) {
log("Skipping file %s since it has already be processed", file);
@ -99,7 +106,9 @@ function processFile(ctx: ProcessContext, file: string, root = false): Parsed |
.map((statement) => {
if (statement.type == "import") {
const base = Path.dirname(file);
const resolved = Path.resolve(Path.join(base, statement.path + ".jrpc"));
const resolved = Path.resolve(
Path.join(base, statement.path + ".jrpc")
);
return processFile(ctx, resolved);
} else {
return statement;
@ -111,16 +120,14 @@ function processFile(ctx: ProcessContext, file: string, root = false): Parsed |
} catch (err) {
if (err instanceof TokenizerError) {
printError(err, file, err.index);
if(!root)
throw new CatchedError();
if (!root) throw new CatchedError();
} else if (err instanceof ParserError) {
printError(err, file, err.token.startIdx);
if(!root)
throw new CatchedError();
} else if(root && err instanceof CatchedError) {
if (!root) throw new CatchedError();
} else if (root && err instanceof CatchedError) {
return null;
} else {
throw err;
throw err;
}
}
}
@ -132,18 +139,21 @@ export default function startCompile(options: CompileOptions) {
} as ProcessContext;
let ir: IR | undefined = undefined;
if(options.input.endsWith(".json")) {
if (options.input.endsWith(".json")) {
ir = JSON.parse(FS.readFileSync(options.input, "utf-8"));
} else {
const parsed = processFile(ctx, options.input, true);
if(!parsed)
process.exit(1); // Errors should have already been emitted
if (!parsed) process.exit(1); // Errors should have already been emitted
try {
ir = get_ir(parsed);
} catch(err) {
if(err instanceof IRError) {
printError(err, err.statement.location.file, err.statement.location.idx);
ir = get_ir(parsed);
} catch (err) {
if (err instanceof IRError) {
printError(
err,
err.statement.location.file,
err.statement.location.idx
);
process.exit(1);
} else {
throw err;
@ -151,23 +161,25 @@ export default function startCompile(options: CompileOptions) {
}
}
if(!ir)
throw new Error("Error compiling: Cannot get IR");
if (!ir) throw new Error("Error compiling: Cannot get IR");
if(options.emitDefinitions) {
FS.writeFileSync(options.emitDefinitions, JSON.stringify(ir, undefined, 3));
if (options.emitDefinitions) {
FS.writeFileSync(
options.emitDefinitions,
JSON.stringify(ir, undefined, 3)
);
}
if(options.targets.length <= 0) {
if (options.targets.length <= 0) {
console.log(Color.yellow("WARNING:"), "No targets selected!");
}
options.targets.forEach(target => {
const tg = Targets.get(target.type) as any
if(!tg) {
options.targets.forEach((target) => {
const tg = Targets.get(target.type) as any;
if (!tg) {
console.log(Color.red("ERROR:"), "Target not supported!");
return;
}
compile(ir, new tg(target.output));
})
compile(ir, new tg(target.output, ir.options));
});
}