elna/cli/cl.cpp

51 lines
1.7 KiB
C++

#include "elna/cli/cl.hpp"
#include "elna/backend/target.hpp"
#include "elna/source/semantic.hpp"
#include "elna/source/optimizer.hpp"
#include <iostream>
namespace elna::cli
{
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)
{
try
{
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());
source::intermediate_code_generator intermediate_code_generator{ global_scope };
intermediate_code_generator.visit(ast.get());
riscv::riscv32_elf(ast.get(), intermediate_code_generator, global_scope, out_file);
}
catch (std::ios_base::failure&)
{
return 3;
}
return 0;
}
}