elna/cli/cl.cpp

72 lines
2.5 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-04-18 12:15:26 +02:00
#include "elna/source/optimizer.hpp"
#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
{
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
}
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)
{
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 = 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);
}
catch (std::ios_base::failure&)
2024-03-07 09:15:11 +01:00
{
return 3;
}
return 0;
}
}