elna/source/main.d

69 lines
1.6 KiB
D
Raw Normal View History

2022-06-05 15:16:04 +02:00
import core.stdc.stdio;
import core.stdc.string;
import core.stdc.stdlib;
import elna.lexer;
import elna.parser;
import elna.generator;
import elna.ir;
2022-06-06 22:56:28 +02:00
import elna.extended;
import std.sumtype;
import std.typecons;
import tanya.container.array;
2022-06-05 15:16:04 +02:00
import tanya.container.string;
import tanya.memory.allocator;
import tanya.memory.mmappool;
2022-06-06 22:56:28 +02:00
import tanya.os.error;
2022-06-05 15:16:04 +02:00
2022-06-06 22:56:28 +02:00
private Nullable!String readSource(string source) @nogc
2022-06-05 15:16:04 +02:00
{
2022-06-06 22:56:28 +02:00
enum size_t bufferSize = 255;
auto sourceFilename = String(source);
2022-06-05 15:16:04 +02:00
2022-06-06 22:56:28 +02:00
return readFile(sourceFilename).match!(
(ErrorCode errorCode) {
perror(sourceFilename.toStringz);
return Nullable!String();
},
(Array!ubyte contents) => nullable(String(cast(char[]) contents.get))
);
2022-06-05 15:16:04 +02:00
}
int main(string[] args)
{
defaultAllocator = MmapPool.instance;
if (args.length < 2)
{
return 4;
}
2022-06-06 22:56:28 +02:00
auto sourceText = readSource(args[1]);
if (sourceText.isNull)
2022-06-05 15:16:04 +02:00
{
return 3;
}
2022-06-06 22:56:28 +02:00
auto tokens = lex(sourceText.get.get);
2022-06-05 15:16:04 +02:00
if (tokens.length == 0)
{
printf("Lexical analysis failed.\n");
return 1;
}
auto ast = parse(tokens);
if (!ast.valid)
{
auto compileError = ast.error.get;
printf("%lu:%lu: %s\n", compileError.line, compileError.column, compileError.message.ptr);
return 2;
}
auto ir = transform(ast.result);
String outputFilename = String("build/");
outputFilename.insertBack(args[1][0 .. $ - 4]);
outputFilename.insertBack("o");
writeObject(ir, outputFilename);
auto code = generate(ir);
printf("%s", code.toStringz());
return 0;
}