73 lines
1.6 KiB
D
73 lines
1.6 KiB
D
|
import core.stdc.stdio;
|
||
|
import core.stdc.string;
|
||
|
import core.stdc.stdlib;
|
||
|
import elna.lexer;
|
||
|
import elna.parser;
|
||
|
import elna.generator;
|
||
|
import elna.ir;
|
||
|
import tanya.container.string;
|
||
|
import tanya.memory.allocator;
|
||
|
import tanya.memory.mmappool;
|
||
|
|
||
|
private char[] readSource(size_t N)(string source, out char[N] buffer) @nogc
|
||
|
{
|
||
|
memcpy(buffer.ptr, source.ptr, source.length + 1);
|
||
|
buffer[source.length] = '\0';
|
||
|
auto handle = fopen(buffer.ptr, "r");
|
||
|
if (handle is null)
|
||
|
{
|
||
|
perror(buffer.ptr);
|
||
|
return null;
|
||
|
}
|
||
|
fseek(handle, 0, SEEK_END);
|
||
|
size_t fsize = ftell(handle);
|
||
|
rewind(handle);
|
||
|
|
||
|
fread(buffer.ptr, fsize, 1, handle);
|
||
|
fclose(handle);
|
||
|
buffer[fsize] = '\0';
|
||
|
|
||
|
return buffer[0 .. fsize];
|
||
|
}
|
||
|
|
||
|
int main(string[] args)
|
||
|
{
|
||
|
char[255] buffer;
|
||
|
|
||
|
defaultAllocator = MmapPool.instance;
|
||
|
|
||
|
if (args.length < 2)
|
||
|
{
|
||
|
return 4;
|
||
|
}
|
||
|
auto sourceText = readSource(args[1], buffer);
|
||
|
if (sourceText is null)
|
||
|
{
|
||
|
return 3;
|
||
|
}
|
||
|
auto tokens = lex(sourceText);
|
||
|
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;
|
||
|
}
|