elna/cli/cl.cpp

79 lines
2.9 KiB
C++
Raw Permalink 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
}
2024-05-19 00:43:59 +02:00
constexpr std::size_t pointer_size = 4;
static std::shared_ptr<source::symbol_table> add_builtin_symbols()
{
source::symbol_table result;
2024-05-19 00:43:59 +02:00
std::vector<std::shared_ptr<const source::type>> intrinsic_arguments;
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);
2024-05-19 00:43:59 +02:00
intrinsic_arguments.push_back(int_info->type());
auto writei = std::make_shared<source::intrinsic_info>(
source::procedure_type{ intrinsic_arguments, pointer_size });
result.enter("writei", writei);
2024-05-19 00:43:59 +02:00
intrinsic_arguments.clear();
2024-05-19 00:43:59 +02:00
intrinsic_arguments.push_back(boolean_info->type());
auto writeb = std::make_shared<source::intrinsic_info>(
source::procedure_type{ intrinsic_arguments, pointer_size });
result.enter("writeb", writeb);
2024-05-19 00:43:59 +02:00
intrinsic_arguments.clear();
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-19 00:43:59 +02:00
source::name_analysis_visitor(global_scope, in_file, pointer_size).visit(ast.get());
source::type_analysis_visitor(global_scope, in_file, pointer_size).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;
}
}