elna/cli/cl.cpp
2024-03-14 08:52:45 +01:00

58 lines
1.7 KiB
C++

#include "elna/cli/cl.hpp"
#include "elna/backend/target.hpp"
#include "elna/source/semantic.hpp"
#include <cstddef>
#include <fstream>
#include <sstream>
#include <iostream>
namespace elna::cli
{
char *readSource(const char *source)
{
const std::size_t bufferSize = 255;
std::ifstream input_stream{ source };
std::stringstream buffer;
buffer << input_stream.rdbuf();
input_stream.close();
std::string contents = buffer.str();
char *result = reinterpret_cast<char *>(malloc(contents.size() + 1));
std::copy(std::cbegin(contents), std::cend(contents), result);
result[contents.size()] = '\0';
return result;
}
int compile(const std::filesystem::path& in_file, const std::filesystem::path& out_file)
{
auto sourceText = readSource(in_file.c_str());
if (sourceText == nullptr)
{
return 3;
}
size_t tokensCount{ 0 };
auto lex_result = source::lex(sourceText);
free(sourceText);
if (lex_result.has_errors())
{
for (const auto& compile_error : lex_result.errors())
{
std::cout << compile_error->line() << ':' << compile_error->column()
<< ": " << compile_error->what() << std::endl;
}
return 1;
}
auto ast = source::parser(std::move(lex_result.success())).parse();
if (ast == nullptr)
{
return 2;
}
source::name_analysis_visitor().visit(ast.get());
source::allocator_visitor().visit(ast.get());
riscv::riscv32_elf(ast.get(), out_file);
return 0;
}
}