Initial commit
This commit is contained in:
50
cli/cl.cpp
Normal file
50
cli/cl.cpp
Normal file
@ -0,0 +1,50 @@
|
||||
#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;
|
||||
}
|
||||
}
|
47
cli/main.cpp
Normal file
47
cli/main.cpp
Normal file
@ -0,0 +1,47 @@
|
||||
#include "elna/cli/cl.hpp"
|
||||
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <boost/program_options.hpp>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
boost::program_options::options_description visible{ "Allowed options" };
|
||||
boost::program_options::options_description description;
|
||||
boost::program_options::positional_options_description positional;
|
||||
boost::program_options::variables_map variables_map;
|
||||
|
||||
visible.add_options()
|
||||
("help", "Print this help message")
|
||||
("output,o", boost::program_options::value<std::filesystem::path>(), "Output object file")
|
||||
;
|
||||
description.add_options()
|
||||
("input", boost::program_options::value<std::filesystem::path>()->required())
|
||||
;
|
||||
description.add(visible);
|
||||
positional.add("input", 1);
|
||||
|
||||
auto parsed = boost::program_options::command_line_parser(argc, argv)
|
||||
.options(description).positional(positional)
|
||||
.run();
|
||||
boost::program_options::store(parsed, variables_map);
|
||||
boost::program_options::notify(variables_map);
|
||||
|
||||
if (variables_map.count("help"))
|
||||
{
|
||||
std::cout << description << std::endl;
|
||||
return 0;
|
||||
}
|
||||
const auto in_file = variables_map["input"].as<std::filesystem::path>();
|
||||
std::filesystem::path out_file;
|
||||
|
||||
if (variables_map.count("output"))
|
||||
{
|
||||
out_file = variables_map["output"].as<std::filesystem::path>();
|
||||
}
|
||||
else
|
||||
{
|
||||
out_file = in_file.filename().replace_extension(".o");
|
||||
}
|
||||
return elna::cli::compile(in_file, out_file);
|
||||
}
|
Reference in New Issue
Block a user