81 lines
2.4 KiB
C++
81 lines
2.4 KiB
C++
#include "elna/cli/cl.hpp"
|
|
#include "elna/backend/target.hpp"
|
|
#include "elna/source/semantic.hpp"
|
|
#include <cstddef>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
|
|
namespace elna::cli
|
|
{
|
|
std::string read_source(const char *source)
|
|
{
|
|
constexpr std::size_t buffer_size = 4096;
|
|
|
|
std::ifstream input_stream{ source, std::ios::binary | std::ios::in };
|
|
std::string output;
|
|
if (input_stream.fail())
|
|
{
|
|
throw std::ios_base::failure("File does not exist");
|
|
}
|
|
while (true)
|
|
{
|
|
const std::size_t old_size = output.size();
|
|
output.resize(old_size + buffer_size);
|
|
input_stream.read(&output[old_size], buffer_size);
|
|
|
|
if (input_stream.eof())
|
|
{
|
|
output.resize(old_size + input_stream.gcount());
|
|
break;
|
|
}
|
|
else if (input_stream.fail())
|
|
{
|
|
throw std::ios_base::failure("Unable to complete reading the source file");
|
|
}
|
|
}
|
|
return output;
|
|
}
|
|
|
|
void print_error(const std::unique_ptr<source::error>& compile_error)
|
|
{
|
|
std::cerr << compile_error->path().string() << ":"
|
|
<< compile_error->line() << ':' << compile_error->column()
|
|
<< ": " << compile_error->what() << std::endl;
|
|
}
|
|
|
|
int compile(const std::filesystem::path& in_file, const std::filesystem::path& out_file)
|
|
{
|
|
std::string source_text;
|
|
try
|
|
{
|
|
source_text = read_source(in_file.c_str());
|
|
}
|
|
catch (std::ios_base::failure&)
|
|
{
|
|
return 3;
|
|
}
|
|
size_t tokensCount{ 0 };
|
|
auto lex_result = source::lex(source_text, in_file);
|
|
|
|
if (lex_result.has_errors())
|
|
{
|
|
print_errors(lex_result.errors().cbegin(), lex_result.errors().cend());
|
|
return 1;
|
|
}
|
|
source::parser parser{ std::move(lex_result.success()) };
|
|
auto ast = parser.parse();
|
|
if (ast == nullptr)
|
|
{
|
|
print_errors(parser.errors().cbegin(), parser.errors().cend());
|
|
return 2;
|
|
}
|
|
auto global_scope = std::make_shared<source::symbol_table>();
|
|
source::name_analysis_visitor(global_scope).visit(ast.get());
|
|
source::type_analysis_visitor().visit(ast.get());
|
|
source::allocator_visitor(global_scope).visit(ast.get());
|
|
riscv::riscv32_elf(ast.get(), global_scope, out_file);
|
|
|
|
return 0;
|
|
}
|
|
}
|