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-04-18 12:15:26 +02:00
|
|
|
#include "elna/source/optimizer.hpp"
|
2024-05-05 01:52:08 +02:00
|
|
|
#include "elna/source/types.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
|
|
|
}
|
|
|
|
|
2024-05-05 01:52:08 +02:00
|
|
|
static std::shared_ptr<source::symbol_table> add_builtin_symbols()
|
|
|
|
{
|
|
|
|
source::symbol_table result;
|
|
|
|
|
|
|
|
auto boolean_info = std::make_shared<source::type_info>(source::boolean_type);
|
|
|
|
auto int_info = std::make_shared<source::type_info>(source::int_type);
|
|
|
|
result.enter("Boolean", boolean_info);
|
|
|
|
result.enter("Int", int_info);
|
|
|
|
|
|
|
|
auto writei = std::make_shared<source::intrinsic_info>();
|
|
|
|
writei->parameter_infos.emplace_back(int_info->type());
|
|
|
|
result.enter("writei", writei);
|
|
|
|
|
|
|
|
auto writeb = std::make_shared<source::intrinsic_info>();
|
|
|
|
writeb->parameter_infos.emplace_back(boolean_info->type());
|
|
|
|
result.enter("writeb", writeb);
|
|
|
|
|
|
|
|
return std::make_shared<source::symbol_table>(std::move(result));
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2024-05-05 01:52:08 +02:00
|
|
|
auto global_scope = add_builtin_symbols();
|
2024-05-16 21:01:11 +02:00
|
|
|
source::name_analysis_visitor(global_scope, in_file).visit(ast.get());
|
|
|
|
source::type_analysis_visitor(global_scope, in_file, 4).visit(ast.get());
|
2024-04-06 16:10:07 +02:00
|
|
|
source::allocator_visitor(global_scope).visit(ast.get());
|
2024-04-18 12:15:26 +02:00
|
|
|
|
|
|
|
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);
|
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;
|
|
|
|
}
|
|
|
|
}
|