elna/cli/cl.cpp

65 lines
2.0 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-23 14:53:26 +01:00
source::parser parser{ std::move(lex_result.success()) };
auto ast = parser.parse();
2024-03-07 09:15:11 +01:00
if (ast == nullptr)
{
2024-03-23 14:53:26 +01:00
for (const auto& compile_error : parser.errors())
{
std::cout << compile_error->line() << ':' << compile_error->column()
<< ": " << compile_error->what() << std::endl;
}
2024-03-07 09:15:11 +01:00
return 2;
}
2024-03-14 08:52:45 +01:00
source::name_analysis_visitor().visit(ast.get());
2024-03-23 14:53:26 +01:00
source::type_analysis_visitor().visit(ast.get());
2024-03-14 08:52:45 +01:00
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;
}
}