elna/cli/cl.cpp

58 lines
1.7 KiB
C++
Raw Normal View History

2024-03-07 09:15:11 +01:00
#include "elna/cli/cl.hpp"
#include "elna/backend/target.hpp"
2024-03-14 08:52:45 +01:00
#include "elna/source/semantic.hpp"
2024-03-07 09:15:11 +01:00
#include <cstddef>
#include <fstream>
#include <sstream>
2024-03-09 08:36:07 +01:00
#include <iostream>
2024-03-07 09:15:11 +01:00
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())
{
2024-03-09 08:36:07 +01:00
std::cout << compile_error->line() << ':' << compile_error->column()
<< ": " << compile_error->what() << std::endl;
2024-03-07 09:15:11 +01:00
}
return 1;
}
2024-03-14 08:52:45 +01:00
auto ast = source::parser(std::move(lex_result.success())).parse();
2024-03-07 09:15:11 +01:00
if (ast == nullptr)
{
return 2;
}
2024-03-14 08:52:45 +01:00
source::name_analysis_visitor().visit(ast.get());
source::allocator_visitor().visit(ast.get());
2024-03-09 08:36:07 +01:00
riscv::riscv32_elf(ast.get(), out_file);
2024-03-07 09:15:11 +01:00
return 0;
}
}