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-09 08:36:07 +01:00
|
|
|
#include <iostream>
|
2024-03-07 09:15:11 +01:00
|
|
|
|
|
|
|
namespace elna::cli
|
|
|
|
{
|
2024-03-30 00:21:58 +01:00
|
|
|
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;
|
2024-03-07 09:15:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int compile(const std::filesystem::path& in_file, const std::filesystem::path& out_file)
|
|
|
|
{
|
2024-03-30 00:21:58 +01:00
|
|
|
try
|
|
|
|
{
|
2024-04-06 16:10:07 +02:00
|
|
|
source::result<source::lexer> lex_result = source::tokenize(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);
|
2024-03-30 00:21:58 +01:00
|
|
|
}
|
|
|
|
catch (std::ios_base::failure&)
|
2024-03-07 09:15:11 +01:00
|
|
|
{
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|